Files
SarmaxWall/PlacePageUC.xaml.vb
Dario Sassi f6e028d12e SarmaxWall 1.6l9 :
- adattamenti per modifiche a libreria nesting.
2016-01-21 18:06:31 +00:00

748 lines
34 KiB
VB.net

Imports System.Threading
Imports System.IO
Imports EgtUILib
Public Class PlacePageUC
' Riferimento alla MainWindow
Private m_MainWindow As MainWindow = Application.Current.MainWindow
' Dichiarazione del UserControl SceneButtons
Friend m_SceneButtons As SceneButtonsUC
' Properties
Private m_bFirst As Boolean = True
' Dichiarazione Scene
Friend WithEvents PlaceScene As New Scene
Private PlaceSceneHost As New System.Windows.Forms.Integration.WindowsFormsHost
' Flag per pezzi selezionati in tavola o fuori
Private m_nPartPos As Integer = PART_POS.NONE_TABLE
Enum PART_POS As Integer
IN_TABLE = -1
NONE_TABLE = 0
OUT_TABLE = 1
End Enum
' Dati per Drag
Private m_bEnableDrag As Boolean = False
Private m_bDrag As Boolean = False
Private m_ptPrev As Point3d
' Dati macchina
Friend m_dLength As Double = 0
Friend m_dWidth As Double = 0
Friend m_dOffsetX As Double = 0
Friend m_dOffsetY As Double = 0
Friend m_dPlankX As Double = 0
Friend m_dMinDist As Double = 0
Friend m_dParkMaxY As Double = 0
' Dati movimento
Friend m_dStep As Double = 0
Friend m_dMaxStep As Double = 0
Private m_bMaximizeMove As Boolean = False
' Spessore layer (strato)
Private m_dLayerThick As Double = 0
' Numero tavole rimaste in cima alla catasta
Private m_nPlankNumOnTop As Integer = 0
Private Sub PlacePage_Initialized(sender As Object, e As EventArgs)
'Creazione del UserCOntrol SceneButtons
m_SceneButtons = New SceneButtonsUC
'Posizionemento nella griglia del UserControl SceneButtons
m_SceneButtons.SetValue(Grid.ColumnProperty, 0)
BottomButtonsGrid.Children.Add(m_SceneButtons)
'Assegnazione scena all'host e posizionamento nella PlacePageGrid
PlaceSceneHost.Child = PlaceScene
PlaceSceneHost.SetValue(Grid.ColumnProperty, 1)
PlaceSceneHost.SetValue(Grid.RowProperty, 0)
Me.PlacePageGrid.Children.Add(PlaceSceneHost)
'Imposto i messaggi letti dal file dei messaggi
GenerateBtn.Content = EgtMsg(MSG_PLACEPAGEUC + 1) 'Generate - Genera
SendBtn.Content = EgtMsg(MSG_PLACEPAGEUC + 2) 'Send - Trasmetti
WallPositioningLbl.Content = EgtMsg(MSG_PLACEPAGEUC + 3) 'Wall position - Posizionamento parete
InsertWallTxt.Text = EgtMsg(MSG_PLACEPAGEUC + 4) 'Insert wall - Inserisci parete
StoreWallTxt.Text = EgtMsg(MSG_PLACEPAGEUC + 5) 'Store wall - Parcheggia parete
RemoveWallBtn.Content = EgtMsg(MSG_PLACEPAGEUC + 6) 'Remove wall - Elimina parete
LayerThicknessLbl.Text = EgtMsg(MSG_PLACEPAGEUC + 7) 'Layer thickness - Spessore strato
PlankNumFirstLayerLbl.Text = EgtMsg(MSG_PLACEPAGEUC + 8) ' - Numero tavola primo piano
' Associo TextBox e Label
Dim TempLabel1 As New Label
TempLabel1.Content = LayerThicknessLbl.Text
LayerThicknessTxBx.Tag = TempLabel1
Dim TempLabel2 As New Label
TempLabel2.Content = PlankNumFirstLayerLbl.Text
PlankNumFirstLayerTxBx.Tag = TempLabel2
End Sub
Private Sub PlacePage_Loaded(sender As Object, e As RoutedEventArgs)
If m_bFirst Then
' imposto colore di default
Dim DefColor As New Color3d(0, 0, 0)
GetPrivateProfileColor(S_GEOMDB, K_DEFAULTCOLOR, DefColor, m_MainWindow.GetIniFile())
PlaceScene.SetDefaultMaterial(DefColor)
' imposto colori sfondo
Dim BackTopColor As New Color3d(192, 192, 192)
GetPrivateProfileColor(S_SCENE, K_BACKTOP, BackTopColor, m_MainWindow.GetIniFile())
Dim BackBotColor As New Color3d(BackTopColor)
GetPrivateProfileColor(S_SCENE, K_BACKBOTTOM, BackBotColor, m_MainWindow.GetIniFile())
PlaceScene.SetViewBackground(BackTopColor, BackBotColor)
' imposto colore di evidenziazione
Dim MarkColor As New Color3d(255, 255, 0)
GetPrivateProfileColor(S_SCENE, K_MARK, MarkColor, m_MainWindow.GetIniFile())
PlaceScene.SetMarkMaterial(MarkColor)
' imposto colore per superfici selezionate
Dim SelSurfColor As New Color3d(255, 255, 192)
GetPrivateProfileColor(S_SCENE, K_SELSURF, SelSurfColor, m_MainWindow.GetIniFile())
PlaceScene.SetSelSurfMaterial(SelSurfColor)
' imposto tipo e colore del rettangolo di zoom
Dim bOutline As Boolean = True
Dim ZwColor As New Color3d(0, 0, 0)
GetPrivateProfileZoomWin(S_SCENE, K_ZOOMWIN, bOutline, ZwColor, m_MainWindow.GetIniFile())
PlaceScene.SetZoomWinAttribs(bOutline, ZwColor)
' imposto colore della linea di distanza
Dim DstLnColor As New Color3d(255, 0, 0)
GetPrivateProfileColor(S_SCENE, K_DISTLINE, DstLnColor, m_MainWindow.GetIniFile())
PlaceScene.SetDistLineMaterial(DstLnColor)
' imposto parametri OpenGL
Dim nDriver As Integer = GetPrivateProfileInt(S_OPENGL, K_DRIVER, 3, m_MainWindow.GetIniFile())
Dim b2Buff As Boolean = (GetPrivateProfileInt(S_OPENGL, K_DOUBLEBUFFER, 1, m_MainWindow.GetIniFile()) <> 0)
Dim nColorBits As Integer = GetPrivateProfileInt(S_OPENGL, K_COLORBITS, 32, m_MainWindow.GetIniFile())
Dim nDepthBits As Integer = GetPrivateProfileInt(S_OPENGL, K_DEPTHBITS, 32, m_MainWindow.GetIniFile())
PlaceScene.SetViewAttributes(nDriver, b2Buff, nColorBits, nDepthBits)
' inizializzo la scena (DB geometrico + visualizzazione) e verifico presenza chiave
If Not PlaceScene.Init() Then
Dim m_MissingKeyWindow As New MissingKeyWD
m_MissingKeyWindow.Top = m_MainWindow.Top + (m_MainWindow.Height / 2 - m_MissingKeyWindow.Height / 2)
m_MissingKeyWindow.Left = m_MainWindow.Left + (m_MainWindow.Width / 2 - m_MissingKeyWindow.Width / 2)
m_MainWindow.Close()
m_MissingKeyWindow.Show()
End If
' Se richiesto, carico l'ultimo progetto
Dim nAutoLoad As Integer = GetPrivateProfileInt(S_GENERAL, K_AUTOLOADLASTPROJ, 0, m_MainWindow.GetIniFile())
If nAutoLoad <> 0 Then
If Not LoadLastProject() Then
EgtOutLog("Error loading last project")
End If
End If
' Carico la macchina
EgtDisableModified()
If Not LoadMachine() Then
EgtOutLog("Error loading machine")
End If
EgtEnableModified()
' carico i parametri di posizionamento
' quelli modificabili da Options si leggono sempre appena sotto
m_dMinDist = GetPrivateProfileDouble(S_MACHINE, K_MINDIST, 0, m_MainWindow.GetIniFile())
' carico e calcolo i parametri di movimento
m_dStep = GetPrivateProfileDouble(S_MACHINE, K_STEP, 0, m_MainWindow.GetIniFile())
m_dMaxStep = Math.Max(m_dLength - m_dOffsetX, m_dWidth - m_dOffsetY)
' carico abilitazione drag
m_bEnableDrag = (GetPrivateProfileInt(S_MACHINE, K_ENABLEDRAG, 0, m_MainWindow.GetIniFile()) <> 0)
' carico lo spessore dei layer e lo visualizzo
m_dLayerThick = GetPrivateProfileDouble(S_MACHINE, K_LAYERTHICK, 0, m_MainWindow.GetIniFile())
LayerThicknessTxBx.Text = m_dLayerThick
' carico il numero di tavole rimaste sulla cima della catasta
m_nPlankNumOnTop = GetPrivateProfileInt(S_MACHINE, K_PLANKNUMONTOP, 0, m_MainWindow.GetIniFile())
PlankNumFirstLayerTxBx.Text = m_nPlankNumOnTop
' carico la griglia
LoadGridData()
m_bFirst = False
Else
EgtSetCurrentContext(PlaceScene.GetCtx())
End If
' carico parametri di posizionamento modificabili da Options
m_dOffsetX = GetPrivateProfileDouble(S_MACHINE, K_OFFSETX, 0, m_MainWindow.GetIniFile())
m_dOffsetX = Math.Max(0, m_dOffsetX)
m_dOffsetY = GetPrivateProfileDouble(S_MACHINE, K_OFFSETY, 0, m_MainWindow.GetIniFile())
m_dOffsetY = Math.Max(0, m_dOffsetY)
m_dPlankX = GetPrivateProfileDouble(S_MACHINE, K_PLANKX, 200, m_MainWindow.GetIniFile())
' inibisco selezione diretta da Scene
PlaceScene.SetStatusNull()
End Sub
Private Sub MaximizeMoveBtn_Click(sender As Object, e As RoutedEventArgs) Handles MaximizeMoveBtn.Click
If MaximizeMoveBtn.IsChecked Then
m_bMaximizeMove = True
Else
m_bMaximizeMove = False
End If
End Sub
Private Function LoadLastProject() As Boolean
' Carico l'ultimo progetto
Dim sPath As String = m_MainWindow.GetDataDir() & "\0000.nge"
If Not EgtOpenFile(sPath) Then
Return False
End If
' Recupero il contrassegno
Dim nMarkId As Integer = EgtGetFirstNameInGroup(GDB_ID.ROOT, NAME_PROJMARK)
If nMarkId = GDB_ID.NULL Then
EgtNewFile()
EgtResetModified()
Return False
End If
' Leggo lo stato di modificato
Dim nModif As Integer = 1
EgtGetInfo(nMarkId, INFO_MODIF, nModif)
' Cancello macchina e contrassegno
Dim nMachineId As Integer = EgtGetFirstNameInGroup(GDB_ID.ROOT, NAME_MACHINE)
EgtErase(nMachineId)
EgtErase(nMarkId)
' Imposto lo stato originale di modificato
If nModif <> 0 Then
EgtSetModified()
Else
EgtResetModified()
End If
Return True
End Function
Private Function LoadMachine() As Boolean
' Recupero path del file macchina
Dim sMachFile As String = String.Empty
GetPrivateProfileString(S_MACHINE, K_GEOMETRY, "", sMachFile, m_MainWindow.GetIniFile())
' Carico la macchina
If Not EgtInsertFile(sMachFile) Then
Return False
End If
' Recupero la macchina e la imposto a livello System
Dim nMachineId As Integer = EgtGetFirstNameInGroup(GDB_ID.ROOT, NAME_MACHINE)
If nMachineId = GDB_ID.NULL Or EgtGetType(nMachineId) <> GDB_TY.GROUP Then
Return False
End If
EgtSetLevel(nMachineId, GDB_LV.SYSTEM)
' Eseguo zoom all
EgtZoom(ZM.ALL)
' Recupero la tavola
Dim nTableId As Integer = EgtGetFirstNameInGroup(nMachineId, NAME_TABLE)
If nTableId = GDB_ID.NULL Or EgtGetType(nMachineId) <> GDB_TY.GROUP Then
Return False
End If
' Recupero dimensioni tavola
EgtGetInfo(nTableId, KEY_TABLENGTH, m_dLength)
EgtGetInfo(nTableId, KEY_TABWIDTH, m_dWidth)
m_dParkMaxY = -m_dWidth - 1000
' Inserisco contrassegno di progetto SarmaxWall valido
Dim nMarkId As Integer = EgtCreateGroup(GDB_ID.ROOT)
EgtSetName(nMarkId, NAME_PROJMARK)
EgtSetLevel(nMarkId, GDB_LV.SYSTEM)
Return True
End Function
Private Sub LoadGridData()
Dim dSnapStep As Double = GetPrivateProfileDouble(S_GRID, K_SNAPSTEP, 10, m_MainWindow.GetIniFile())
Dim nMinLineSStep As Integer = GetPrivateProfileInt(S_GRID, K_MINLINESSTEP, 1, m_MainWindow.GetIniFile())
Dim nMajLineSStep As Integer = GetPrivateProfileInt(S_GRID, K_MAJLINESSTEP, 10, m_MainWindow.GetIniFile())
Dim nExtSStep As Integer = GetPrivateProfileInt(S_GRID, K_EXTSSTEP, 50, m_MainWindow.GetIniFile())
Dim MinLnColor As New Color3d(160, 160, 160)
GetPrivateProfileColor(S_GRID, K_MINLNCOLOR, MinLnColor, m_MainWindow.GetIniFile())
Dim MajLnColor As New Color3d(160, 160, 160)
GetPrivateProfileColor(S_GRID, K_MAJLNCOLOR, MajLnColor, m_MainWindow.GetIniFile())
EgtSetGridFrame(Frame3d.GLOB)
EgtSetGridGeo(dSnapStep, nMinLineSStep, nMajLineSStep, nExtSStep)
EgtSetGridColor(MinLnColor, MajLnColor)
Dim frGrid As New Frame3d(New Point3d(0, 0, 0))
EgtSetGridFrame(frGrid)
Dim bShowGrid As Boolean = (GetPrivateProfileInt(S_GRID, K_SHOWGRID, 1, m_MainWindow.GetIniFile()) <> 0)
EgtSetGridShow(bShowGrid, False)
End Sub
Friend Sub MyDraw()
EgtDraw()
If EgtGetModified() Then
Me.MessageTxBl.Text = ""
Me.MessageBrd.Background = Brushes.White
End If
End Sub
Private Sub OnMouseDownScene(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles PlaceScene.OnMouseDownScene
' Si può selezionare solo con il tasto sinistro e se stato NULL
If e.Button <> Windows.Forms.MouseButtons.Left Or Not PlaceScene.IsStatusNull() Then
Return
End If
' Verifico se selezionato indicativo di pezzo
EgtSetObjFilterForSelect(True, True, True, True, True)
Dim nSel As Integer
EgtSelect(e.Location, Scene.DIM_SEL, Scene.DIM_SEL, nSel)
Dim nId As Integer = EgtGetFirstObjInSelWin()
While nId <> GDB_ID.NULL
' Recupero l'identificativo del pezzo cui appartiene
Dim nPartId As Integer = EgtGetParent(EgtGetParent(nId))
If EgtIsPart(nPartId) Then
Dim nStat As Integer = GDB_ST.ON_
EgtGetStatus(nPartId, nStat)
Dim bInTable As Boolean = IsPartInTable(nPartId)
' Se selezionato o posizione oggetto incompatibile con flag posizione selezionati
If nStat = GDB_ST.SEL Or
(bInTable And m_nPartPos = PART_POS.OUT_TABLE) Or
(Not bInTable And m_nPartPos = PART_POS.IN_TABLE) Then
' Deseleziono
EgtDeselectObj(nPartId)
' Se nessun pezzo selezionato, reset flag posizione selezionati
If EgtGetSelectedObjCount() = 0 Then
m_nPartPos = PART_POS.NONE_TABLE
End If
Else
EgtSelectObj(nPartId)
' Set flag posizione selezionati
m_nPartPos = IIf(bInTable, PART_POS.IN_TABLE, PART_POS.OUT_TABLE)
End If
MyDraw()
Exit While
End If
nId = EgtGetNextObjInSelWin()
End While
' Dati per drag
m_bDrag = m_bEnableDrag And EgtUnProjectPoint(e.Location, m_ptPrev)
End Sub
Private Sub OnMouseMoveScene(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles PlaceScene.OnMouseMoveScene
If Not m_bDrag Or m_nPartPos <> PART_POS.IN_TABLE Then
Return
End If
' Ricavo il punto corrente in coordinate mondo
Dim ptCurr As Point3d
EgtUnProjectPoint(e.Location, ptCurr)
' Ricavo il vettore di movimento
Dim vtMove As Vector3d = ptCurr - m_ptPrev
' Muovo i pezzi selezionati di quanto possibile
EgtMoveBox(GDB_ID.SEL, vtMove, 0, -m_dWidth, m_dLength, 0, m_dMinDist)
MyDraw()
' Aggiorno il punto precedente
'm_ptPrev += vtMove
m_ptPrev = ptCurr
End Sub
Private Sub OnMouseUpScene(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles PlaceScene.OnMouseUpScene
' Disabilito modalità drag
m_bDrag = False
End Sub
Private Function IsPartInTable(ByVal nPartId As Integer) As Boolean
If Not EgtIsPart(nPartId) Then
Return False
End If
Dim ptMin, ptMax As Point3d
Dim BBF_FLAG As Integer = GDB_BB.ONLY_VISIBLE + GDB_BB.IGNORE_TEXT + GDB_BB.IGNORE_DIM
If Not EgtGetBBoxGlob(nPartId, BBF_FLAG, ptMin, ptMax) Then
Return False
End If
If ptMax.x < 0 - EPS_SMALL Or ptMin.x > m_dLength + EPS_SMALL Or
ptMax.y < -m_dWidth - EPS_SMALL Or ptMin.y > 0 + EPS_SMALL Then
Return False
End If
Return True
End Function
Private Sub UpBtn_Click(sender As Object, e As RoutedEventArgs) Handles UpBtn.Click
Dim dStep As Double = IIf(m_bMaximizeMove, m_dMaxStep, m_dStep)
EgtMoveBox(GDB_ID.SEL, New Vector3d(0, dStep, 0), 0, -m_dWidth, m_dLength, 0, m_dMinDist)
MyDraw()
End Sub
Private Sub DownBtn_Click(sender As Object, e As RoutedEventArgs) Handles DownBtn.Click
Dim dStep As Double = IIf(m_bMaximizeMove, m_dMaxStep, m_dStep)
EgtMoveBox(GDB_ID.SEL, New Vector3d(0, -dStep, 0), 0, -m_dWidth, m_dLength, 0, m_dMinDist)
MyDraw()
End Sub
Private Sub RightBtn_Click(sender As Object, e As RoutedEventArgs) Handles RightBtn.Click
Dim dStep As Double = IIf(m_bMaximizeMove, m_dMaxStep, m_dStep)
EgtMoveBox(GDB_ID.SEL, New Vector3d(dStep, 0, 0), 0, -m_dWidth, m_dLength, 0, m_dMinDist)
MyDraw()
End Sub
Private Sub LeftBtn_Click(sender As Object, e As RoutedEventArgs) Handles LeftBtn.Click
Dim dStep As Double = IIf(m_bMaximizeMove, m_dMaxStep, m_dStep)
EgtMoveBox(GDB_ID.SEL, New Vector3d(-dStep, 0, 0), 0, -m_dWidth, m_dLength, 0, m_dMinDist)
MyDraw()
End Sub
Private Sub RotateCounterClockwiseBtn_Click(sender As Object, e As RoutedEventArgs) Handles RotateCounterClockwiseBtn.Click
RotateCluster(90)
MyDraw()
End Sub
Private Sub RotateClockwiseBtn_Click(sender As Object, e As RoutedEventArgs) Handles RotateClockwiseBtn.Click
RotateCluster(-90)
MyDraw()
End Sub
Private Sub RotateHalfTurnBtn_Click(sender As Object, e As RoutedEventArgs) Handles RotateHalfTurnBtn.Click
' Se non ci sono pezzi selezionati, esco
Dim nCount As Integer = EgtGetSelectedObjCount()
If nCount = 0 Then
Return
End If
' Se un solo pezzo, non sono necessarie verifiche
If nCount = 1 Then
' Recupero il box del cluster di pezzi
Dim ptMin As Point3d
Dim ptMax As Point3d
If Not EgtGetPartBBoxGlob(GDB_ID.SEL, ptMin, ptMax) Then
Return
End If
' Ne derivo il centro di rotazione come centro del cluster
Dim ptCen As Point3d = Point3d.Media(ptMin, ptMax, 0.5)
' Rotazione dei pezzi attorno al loro centro
EgtRotate(GDB_ID.SEL, ptCen, Vector3d.Z_AX(), 180, GDB_RT.GLOB)
MyDraw()
Return
End If
' Caso generale
RotateCluster(180)
MyDraw()
End Sub
Private Function RotateCluster(ByVal dAngRotDeg As Double) As Boolean
' Se non ci sono pezzi selezionati, esco
If EgtGetSelectedObjCount() = 0 Then
Return True
End If
' Recupero il box del cluster di pezzi
Dim ptMin As Point3d
Dim ptMax As Point3d
If Not EgtGetPartBBoxGlob(GDB_ID.SEL, ptMin, ptMax) Then
Return False
End If
' Ne derivo il centro di rotazione come centro del cluster
Dim ptCen As Point3d = Point3d.Media(ptMin, ptMax, 0.5)
' Rotazione dei pezzi attorno al loro centro
EgtRotate(GDB_ID.SEL, ptCen, Vector3d.Z_AX(), dAngRotDeg, GDB_RT.GLOB)
' Se pezzi in parcheggio, li risistemo qui (sempre possibile)
If m_nPartPos <> PART_POS.IN_TABLE Then
EgtPackBox(GDB_ID.SEL, m_dOffsetX, -INFINITO, m_dLength, m_dParkMaxY, m_dMinDist, False)
Return True
End If
' Se pezzi in tavola, cerco di sistemarli qui (se impossibile, annullo la rotazione)
' Recupero nuovo box
EgtGetPartBBoxGlob(GDB_ID.SEL, ptMin, ptMax)
' Se non esce dalla tavola, provo un pack sul posto
If ptMin.x > 0 - EPS_SMALL And ptMin.y > -m_dWidth - EPS_SMALL And
ptMax.x < m_dLength + EPS_SMALL And ptMax.y < 0 + EPS_SMALL Then
If EgtPackBox(GDB_ID.SEL, ptMin.x, ptMin.y, ptMax.x + 1, ptMax.y + 1, m_dMinDist, True) Then
Return True
End If
End If
' Provo un pack un poco più esteso
Dim dDim As Double = Math.Max(ptMax.x - ptMin.x, ptMax.y - ptMin.y)
ptMin.x = ptCen.x - 0.5 * dDim
ptMin.y = ptCen.y - 0.5 * dDim
ptMax.x = ptCen.x + 0.5 * dDim
ptMax.y = ptCen.y + 0.5 * dDim
If (ptMin.x < 0) Then
ptMax.x += 0 - ptMin.x
ptMax.x = Math.Min(ptMax.x, m_dLength)
ptMin.x = 0
End If
If (ptMin.y < -m_dWidth) Then
ptMax.y += -m_dWidth - ptMin.y
ptMax.y = Math.Min(ptMax.y, 0)
ptMin.y = -m_dWidth
End If
If (ptMax.x > m_dLength) Then
ptMin.x += m_dLength - ptMax.x
ptMin.x = Math.Max(ptMin.x, m_dOffsetX)
ptMax.x = m_dLength
End If
If (ptMax.y > 0) Then
ptMin.y += 0 - ptMax.y
ptMin.y = Math.Max(ptMin.y, -m_dWidth)
ptMax.y = 0
End If
If EgtPackBox(GDB_ID.SEL, ptMin.x, ptMin.y, ptMax.x + 1, ptMax.y + 1, m_dMinDist, True) Then
Return True
End If
' Provo un pack sull'intera tavola
If EgtPackBox(GDB_ID.SEL, 0, -m_dWidth, m_dLength, 0, m_dMinDist, True) Then
Return True
End If
' Non è possibile ruotare rimanendo in tavola, annullo l'azione
EgtRotate(GDB_ID.SEL, ptCen, Vector3d.Z_AX(), -dAngRotDeg, GDB_RT.GLOB)
Return False
End Function
Private Sub InsertWallBtn_Click(sender As Object, e As RoutedEventArgs) Handles InsertWallBtn.Click
' Ciclo di inserimento in tavola dei pezzi selezionati
Dim bAllOk As Boolean = True
Dim nId As Integer = EgtGetFirstSelectedObj()
While nId <> GDB_ID.NULL
' Recupero successivo selezionato
Dim nNextId = EgtGetNextSelectedObj()
' Se pezzo in parcheggio, metto nella tavola
If EgtIsPart(nId) And Not IsPartInTable(nId) Then
If Not EgtPackBox(nId, 0, -m_dWidth, m_dLength, 0, m_dMinDist, False) Then
EgtDeselectObj(nId)
bAllOk = False
End If
End If
' Passo al successivo selezionato
nId = nNextId
End While
' Aggiorno flag selezionati
m_nPartPos = IIf(EgtGetSelectedObjCount() > 0, PART_POS.IN_TABLE, PART_POS.NONE_TABLE)
' Aggiorno vista
MyDraw()
' Se almeno una parete non piazzata, lo segnalo
If Not bAllOk Then
Me.MessageTxBl.Text = EgtMsg(MSG_PLACEPAGEUC + 9) 'Inserimento non riuscito
Me.MessageBrd.Background = Brushes.Tomato
End If
End Sub
Private Sub StoreWallBtn_Click(sender As Object, e As RoutedEventArgs) Handles StoreWallBtn.Click
' Parametri di caricamento
Dim dMinX As Double = 0
Dim dMinY As Double = -m_dWidth
Dim dMaxX As Double = m_dLength
Dim dMaxY As Double = 0
Dim dOffset As Double = 100
Dim dParkMaxY As Double = m_dParkMaxY
' Ciclo di parcheggio dei pezzi selezionati
Dim nId As Integer = EgtGetFirstSelectedObj()
While nId <> GDB_ID.NULL
' Se pezzo, metto in parcheggio (sempre possibile)
If EgtIsPart(nId) Then
EgtPackBox(nId, dMinX, -INFINITO, dMaxX, dParkMaxY, dOffset, False)
End If
' Passo al successivo selezionato
nId = EgtGetNextSelectedObj()
End While
' Imposto flag posizione pezzi su parcheggio
m_nPartPos = PART_POS.OUT_TABLE
' Aggiorno vista
MyDraw()
End Sub
Private Sub RemoveWallBtn_Click(sender As Object, e As RoutedEventArgs) Handles RemoveWallBtn.Click
' Ciclo di cancellazione dei pezzi selezionati
Dim nId As Integer = EgtGetFirstSelectedObj()
While nId <> GDB_ID.NULL
' Recupero indice del successivo
Dim nNextId = EgtGetNextSelectedObj()
' Se pezzo cancello
If EgtIsPart(nId) Then
EgtErase(nId)
End If
' Passo al successivo selezionato
nId = nNextId
End While
' Imposto flag posizione pezzi
m_nPartPos = PART_POS.NONE_TABLE
' Aggiorno vista
MyDraw()
End Sub
Private Sub GenerateBtn_Click(sender As Object, e As RoutedEventArgs) Handles GenerateBtn.Click
' Recupero numero progressivo valido come nome ultimo progetto
Dim nLastProj As Integer = GetPrivateProfileInt(S_GENERAL, K_LASTPROJ, 1, m_MainWindow.GetIniFile())
' Se modificato, lo incremento e lo salvo
If EgtGetModified() Then
' Recupero massimo numero di progetti
Dim nMaxProj As Integer = GetPrivateProfileInt(S_GENERAL, K_MAXPROJ, 100, m_MainWindow.GetIniFile())
' Sistemo nuovo nome numerico
nLastProj += 1
If nLastProj > nMaxProj Then
nLastProj = 1
End If
WritePrivateProfileString(S_GENERAL, K_LASTPROJ, nLastProj.ToString(), m_MainWindow.GetIniFile())
' Salvo il progetto corrente
Dim sPath As String = m_MainWindow.GetDataDir() & "\" & nLastProj.ToString("D4") & ".nge"
If Not EgtSaveFile(sPath, NGE.CMPTEXT) Then
Dim sOut As String = "Error saving file " & sPath
EgtOutLog(sOut)
Else
EgtResetModified()
End If
' Cancello eventuali vecchi file di dati e report
Dim sDPath As String = m_MainWindow.GetDataDir() & "\" & nLastProj.ToString("D4") & ".dat"
If My.Computer.FileSystem.FileExists(sDPath) Then
My.Computer.FileSystem.DeleteFile(sDPath)
End If
Dim sRPath As String = m_MainWindow.GetDataDir() & "\" & nLastProj.ToString("D4") & ".txt"
If My.Computer.FileSystem.FileExists(sRPath) Then
My.Computer.FileSystem.DeleteFile(sRPath)
End If
End If
' Lancio la generazione con Lua
' assegno variabili
EgtLuaCreateGlobTable("PROC")
Dim sDataFile As String = m_MainWindow.GetDataDir() & "\" & nLastProj.ToString("D4") & ".dat"
EgtLuaSetGlobStringVar("PROC.DATAFILE", sDataFile)
Dim sReportFile As String = m_MainWindow.GetDataDir() & "\" & nLastProj.ToString("D4") & ".txt"
EgtLuaSetGlobStringVar("PROC.REPORTFILE", sReportFile)
EgtLuaSetGlobStringVar("PROC.TEMPDIR", m_MainWindow.GetTempDir())
EgtLuaSetGlobNumVar("PROC.TABLEN", m_dLength)
EgtLuaSetGlobNumVar("PROC.TABWIDTH", m_dWidth)
EgtLuaSetGlobNumVar("PROC.OFFSETX", m_dOffsetX)
EgtLuaSetGlobNumVar("PROC.OFFSETY", m_dOffsetY)
' Carico direttamente da ini
Dim dExtraGlue As Double = GetPrivateProfileDouble(S_MACHINE, K_OFFSETGLUE, 0, m_MainWindow.GetIniFile())
EgtLuaSetGlobNumVar("PROC.EXTRAGLUE", Math.Max(0, dExtraGlue))
Dim dOnAdvance As Double = GetPrivateProfileDouble(S_MACHINE, K_ONADVANCE, 0, m_MainWindow.GetIniFile())
EgtLuaSetGlobNumVar("PROC.ONADVANCE", Math.Max(0, dOnAdvance))
Dim dOffAdvance As Double = GetPrivateProfileDouble(S_MACHINE, K_OFFADVANCE, 0, m_MainWindow.GetIniFile())
EgtLuaSetGlobNumVar("PROC.OFFADVANCE", Math.Max(0, dOffAdvance))
Dim bPlankOnHoles As Boolean = (GetPrivateProfileInt(S_MACHINE, K_PLANKONHOLES, 1, m_MainWindow.GetIniFile()) <> 0)
EgtLuaSetGlobBoolVar("PROC.PLANKONHOLES", bPlankOnHoles)
Dim dPlankWidth As Double = GetPrivateProfileDouble(S_MACHINE, K_PLANKX, 0, m_MainWindow.GetIniFile())
EgtLuaSetGlobNumVar("PROC.PLANKWIDTH", dPlankWidth)
Dim nPlankNumOnLay = GetPrivateProfileInt(S_MACHINE, K_PLANKNUMINLAYER, 0, m_MainWindow.GetIniFile())
EgtLuaSetGlobNumVar("PROC.LAYPLANKNBR", nPlankNumOnLay)
' Carico da interfaccia
EgtLuaEvalNumExpr(LayerThicknessTxBx.Text, m_dLayerThick)
WritePrivateProfileString(S_MACHINE, K_LAYERTHICK, m_dLayerThick, m_MainWindow.GetIniFile())
EgtLuaSetGlobNumVar("PROC.LAYTH", m_dLayerThick)
Dim dTemp As Double
EgtLuaEvalNumExpr(PlankNumFirstLayerTxBx.Text, dTemp)
m_nPlankNumOnTop = CInt(dTemp + 0.1)
WritePrivateProfileString(S_MACHINE, K_PLANKNUMONTOP, m_nPlankNumOnTop, m_MainWindow.GetIniFile())
EgtLuaSetGlobNumVar("PROC.TOPPLANKNBR", m_nPlankNumOnTop)
' eseguo
Dim sLuaPath As String = String.Empty
GetPrivateProfileString(S_MACHINE, K_PROCESSOR, "", sLuaPath, m_MainWindow.GetIniFile())
EgtDisableModified()
If Not EgtLuaExecFile(sLuaPath) Then
Dim sOut As String = "Error running processor " & sLuaPath
EgtOutLog(sOut)
End If
EgtEnableModified()
' recupero stato di esecuzione
Dim nErr As Integer = 1000
EgtLuaGetGlobIntVar("PROC.ERR", nErr)
If nErr = 0 Then
Me.MessageTxBl.Text = EgtMsg(MSG_PLACEPAGEUC + 10) ' Generazione riuscita
Me.MessageBrd.Background = Brushes.Green
Else
Me.MessageTxBl.Text = EgtMsg(MSG_PLACEPAGEUC + 11) ' Errore in generazione
Me.MessageBrd.Background = Brushes.Tomato
End If
' cancello variabili
EgtLuaResetGlobVar("PROC")
End Sub
Private Sub SendBtn_Click(sender As Object, e As RoutedEventArgs) Handles SendBtn.Click
' path dell'eseguibile di trasmissione
Dim sExeFile As String = String.Empty
GetPrivateProfileString(S_MACHINE, K_TRANSMITTER, "", sExeFile, m_MainWindow.GetIniFile())
' flag per ponti da pilotare
Dim nFlag As Integer = GetPrivateProfileInt(S_MACHINE, K_GANTRIES, 3, m_MainWindow.GetIniFile())
' Path del file da trasmettere e del file di risultato
Dim nLastProj As Integer = GetPrivateProfileInt(S_GENERAL, K_LASTPROJ, 1, m_MainWindow.GetIniFile())
Dim sDataFile As String = m_MainWindow.GetDataDir() & "\" & nLastProj.ToString("D4") & ".dat"
Dim sResFile As String = m_MainWindow.GetDataDir() & "\" & nLastProj.ToString("D4") & ".err"
' Rimuovo eventuale file di errore già presente
Try
If My.Computer.FileSystem.FileExists(sResFile) Then
My.Computer.FileSystem.DeleteFile(sResFile)
End If
Catch ex As Exception
End Try
' Verifico esistenza file da trasmettere
If Not My.Computer.FileSystem.FileExists(sDataFile) Then
Me.MessageTxBl.Text = EgtMsg(MSG_PLACEPAGEUC + 17) ' File di Generazione mancante
Me.MessageBrd.Background = Brushes.Yellow
Return
End If
' Se nessun ponte abilitato, salto tutto
If nFlag = 0 Then
Me.MessageTxBl.Text = EgtMsg(MSG_PLACEPAGEUC + 16) ' Ponti tutti disabilitati
Me.MessageBrd.Background = Brushes.Yellow
Return
End If
' Lancio la trasmissione
Try
Process.Start(sExeFile, sDataFile & " " & nFlag.ToString())
Me.MessageTxBl.Text = EgtMsg(MSG_PLACEPAGEUC + 12) ' Lancio trasmissione riuscito
Me.MessageBrd.Background = Brushes.Green
Catch ex As Exception
Me.MessageTxBl.Text = EgtMsg(MSG_PLACEPAGEUC + 13) ' Errore in lancio trasmissione
Me.MessageBrd.Background = Brushes.Tomato
End Try
Dim nDummy As Integer
Application.Current.Dispatcher.Invoke(Windows.Threading.DispatcherPriority.Background, _
New Action(Function() nDummy = 0))
' Attendo la lettura del risultato
Dim bOk As Boolean = WaitSendResult(sResFile)
If bOk Then
Me.MessageTxBl.Text = EgtMsg(MSG_PLACEPAGEUC + 14) ' Trasmissione completata con successo
Me.MessageBrd.Background = Brushes.Green
Else
Me.MessageTxBl.Text = EgtMsg(MSG_PLACEPAGEUC + 15) ' Trasmissione non riuscita
Me.MessageBrd.Background = Brushes.Tomato
End If
End Sub
Private Function WaitSendResult(ByVal sResFile As String) As Boolean
' Ciclo di ricerca foto scattata (timeout = 10)
Dim nTimeout As Integer = GetPrivateProfileInt(S_MACHINE, K_SENDTIMEOUT, 10, m_MainWindow.GetIniFile())
Dim nMaxThick = 10 * nTimeout
For nThick As Integer = 0 To nMaxThick
' Se esiste il file di risultato
Dim nErr = 999
If VerifyResult(sResFile, nErr) Then
If nErr = 0 Then
Return True
Else
EgtOutLog("Send err=" & nErr.ToString())
Return False
End If
' Altrimenti aspetto
Else
' Aspetto 100 ms
Thread.Sleep(100)
End If
Next
EgtOutLog("Send generic error")
Return False
End Function
Private Function VerifyResult(ByVal sResFile As String, ByRef nErr As Integer) As Boolean
' Se non esiste il file con il risultato
If Not My.Computer.FileSystem.FileExists(sResFile) Then
Return False
End If
' Leggo il file
Dim bOk As Boolean = False
Try
' Controllo errori nel file di info
Dim sLine As String = String.Empty
Dim sr As StreamReader = New StreamReader(sResFile)
Do While sr.Peek() > -1
sLine = sr.ReadLine()
sLine = sLine.Replace(" ", "")
If sLine.StartsWith("Err=") Then
If Int32.TryParse(sLine.Substring(4), nErr) Then
bOk = True
Exit Do
End If
End If
Loop
sr.Close()
Catch ex As Exception
bOk = False
End Try
Return bOk
End Function
Private Sub MessageBrd_MouseDown(sender As Object, e As MouseButtonEventArgs) Handles MessageBrd.MouseDown
Me.MessageTxBl.Text = ""
Me.MessageBrd.Background = Brushes.White
End Sub
End Class