Files
OmagCUT/CurrentProjectPageUC.xaml.vb
T
Dario Sassi 47f5ce124a ImagCUT 1.6j2 :
- aggiunto prototipo di simulazione
- migliorie collegamento con macchina fotografica.
2015-10-23 08:02:18 +00:00

386 lines
17 KiB
VB.net

Imports EgtUILib
Imports System.IO
Public Class CurrentProjectPageUC
' Dichiarazione eventi
Friend Event OnMouseDownScene(sender As Object, e As System.Windows.Forms.MouseEventArgs)
Friend Event OnMouseMoveScene(sender As Object, e As System.Windows.Forms.MouseEventArgs)
Friend Event OnMouseUpScene(sender As Object, e As System.Windows.Forms.MouseEventArgs)
' Riferimento alla MainWindow
Private m_MainWindow As MainWindow = Application.Current.MainWindow
' Dichiarazione delle Page UserControl
Friend m_SceneButtons As SceneButtonsUC
' Properties
Private m_bFirst As Boolean = True
' Dichiarazione Scene
Friend WithEvents CurrentProjectScene As New Scene
Private CurrentProjectSceneHost As New System.Windows.Forms.Integration.WindowsFormsHost
' Identificativo progetto corrente
Private m_nCurrProj As Integer = 0
' Dichiarazione altezza grezzo
Friend m_dRawHeight As Double
' Identificativo del grezzo
Friend m_nRawId As Integer = GDB_ID.NULL
' Costanti
Friend Const MACH_GROUP As String = "Mach01"
Friend Const MAIN_TAB As String = "MainTab"
Friend Const PHOTO_GRP As String = "Photos"
Friend Const PHOTO_NAME As String = "Raw"
Private Sub CurrentProjectPage_Initialized(sender As Object, e As EventArgs)
'Creazione delle Page UserControl
m_SceneButtons = New SceneButtonsUC
'Posizionemento nella griglia delle Page UserControl
m_SceneButtons.SetValue(Grid.ColumnProperty, 5)
m_MainWindow.m_DirectCutPageUC.SetValue(Grid.RowSpanProperty, 3)
m_MainWindow.m_DirectCutPageUC.SetValue(Grid.ColumnSpanProperty, 2)
m_MainWindow.m_CadCutPageUC.SetValue(Grid.RowSpanProperty, 3)
m_MainWindow.m_CadCutPageUC.SetValue(Grid.ColumnSpanProperty, 2)
m_MainWindow.m_FrameCutPageUC.SetValue(Grid.RowSpanProperty, 3)
m_MainWindow.m_FrameCutPageUC.SetValue(Grid.ColumnSpanProperty, 2)
'Assegno SceneButtons alla pagina
UpperButtonGrid.Children.Add(m_SceneButtons)
'Assegnazione scena all'host e posizionamento nella PlacePageGrid
CurrentProjectSceneHost.Child = CurrentProjectScene
CurrentProjectSceneHost.SetValue(Grid.ColumnProperty, 1)
CurrentProjectSceneHost.SetValue(Grid.RowProperty, 1)
Me.CurrentProjectPageGrid.Children.Add(CurrentProjectSceneHost)
'Imposto i messaggi letti dal file dei messaggi
MaterialLbl.Content = EgtMsg(MSG_ROWPARTPAGEUC + 9) 'Material - Materiale
HeightLbl.Content = EgtMsg(MSG_ROWPARTPAGEUC + 5) 'Height - Spessore
ToolLbl.Content = EgtMsg(MSG_CADCUTPAGEUC + 11) 'Tool - Utensile
MachiningLbl.Content = EgtMsg(MSG_CADCUTPAGEUC + 12) 'Machining - Lavorazione
End Sub
Private Sub CurrentProjectPage_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())
CurrentProjectScene.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())
CurrentProjectScene.SetViewBackground(BackTopColor, BackBotColor)
' imposto colore di evidenziazione
Dim MarkColor As New Color3d(255, 255, 0)
GetPrivateProfileColor(S_SCENE, K_MARK, MarkColor, m_MainWindow.GetIniFile())
CurrentProjectScene.SetMarkMaterial(MarkColor)
' imposto colore per superfici selezionate
Dim SelSurfColor As New Color3d(255, 255, 192)
GetPrivateProfileColor(S_SCENE, K_SELSURF, SelSurfColor, m_MainWindow.GetIniFile())
CurrentProjectScene.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())
CurrentProjectScene.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())
CurrentProjectScene.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())
CurrentProjectScene.SetViewAttributes(nDriver, b2Buff, nColorBits, nDepthBits)
' inizializzo la scena (DB geometrico + visualizzazione) e verifico presenza chiave
If Not CurrentProjectScene.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
' inizializzo gestore lavorazioni
EgtInitMachMgr(m_MainWindow.GetMachinesRootDir())
m_bFirst = False
' Se richiesto, carico ultimo progetto
Dim bAutoLoadLast As Boolean = GetPrivateProfileInt(S_GENERAL, K_AUTOLOADLASTPROJ, 0, m_MainWindow.GetIniFile()) <> 0
If bAutoLoadLast Then
m_nCurrProj = GetPrivateProfileInt(S_GENERAL, K_LASTPROJ, 0, m_MainWindow.GetIniFile())
Dim sPath As String = m_MainWindow.GetSaveDir() & "\" & m_nCurrProj.ToString("D4") & ".nge"
If Not LoadProject(sPath, False) Then
NewProject()
End If
EgtResetModified()
' Altrimenti ne imposto uno nuovo
Else
m_nCurrProj = GetPrivateProfileInt(S_GENERAL, K_LASTPROJ, 0, m_MainWindow.GetIniFile())
NewProject()
End If
Else
EgtSetCurrentContext(CurrentProjectScene.GetCtx())
End If
' inibisco selezione diretta da Scene
CurrentProjectScene.SetStatusNull()
' Imposto dati progetto
If m_MainWindow.m_nCurrentMaterial >= 0 And
m_MainWindow.m_nCurrentMaterial < m_MainWindow.m_MaterialsList.Count() Then
MaterialTxBx.Text = m_MainWindow.m_MaterialsList(m_MainWindow.m_nCurrentMaterial).Name
Else
MaterialTxBx.Text = ""
End If
End Sub
Private Sub OnMyMouseDownScene(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles CurrentProjectScene.OnMouseDownScene
RaiseEvent OnMouseDownScene(sender, e)
End Sub
Private Sub OnMyMouseMoveScene(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles CurrentProjectScene.OnMouseMoveScene
RaiseEvent OnMouseMoveScene(sender, e)
End Sub
Private Sub OnMyMouseUpScene(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles CurrentProjectScene.OnMouseUpScene
RaiseEvent OnMouseUpScene(sender, e)
End Sub
Private Sub SetNextProjectIndex()
' se indice negativo, non devo calcolarne uno nuovo
If m_nCurrProj < 0 Then
Return
End If
' Recupero massimo indice ammesso
Dim nMaxProj As Integer = GetPrivateProfileInt(S_GENERAL, K_MAXPROJ, 100, m_MainWindow.GetIniFile())
' Imposto nuovo indice di progetto
m_nCurrProj = -(Math.Abs(m_nCurrProj) + 1)
If m_nCurrProj < -nMaxProj Then
m_nCurrProj = -1
End If
End Sub
Friend Function NewProject() As Boolean
' Imposto nuovo indice di progetto
SetNextProjectIndex()
' Eseguo
EgtNewFile()
' Carico la macchina corrente
Dim sCurrMach As String = String.Empty
GetPrivateProfileString(S_MACH, K_CURRMACH, "", sCurrMach, m_MainWindow.GetIniFile())
If EgtAddMachGroup(MACH_GROUP, sCurrMach) = GDB_ID.NULL Then
Return False
End If
' Imposto la tavola corrente
If Not EgtSetTable(MAIN_TAB) Then
Return False
End If
EgtShowOnlyTable(True)
' Reset spessore grezzo
m_dRawHeight = 0
HeightTxBx.Text = DoubleToString(m_dRawHeight, 2)
' Dichiaro progetto non modificato
EgtResetModified()
Return True
End Function
Friend Function LoadProject(ByVal sPath As String, Optional ByVal bUpdateIndex As Boolean = True) As Boolean
' Se richiesto, imposto nuovo indice di progetto
If bUpdateIndex Then
SetNextProjectIndex()
End If
' Eseguo
If Not LoadFile(sPath) Then
Return False
End If
' aggiorno eventuale identificativo grezzo
m_nRawId = EgtGetFirstRawPart()
' aggiorno spessore grezzo
m_dRawHeight = m_MainWindow.m_RawPartPage.GetRawHeight()
HeightTxBx.Text = DoubleToString(m_dRawHeight, 2)
' Dichiaro progetto non modificato
EgtResetModified()
Return True
End Function
Friend Function LoadFile(ByVal sPath As String) As Boolean
' Eseguo
If Not EgtOpenFile(sPath) Then
Return False
End If
' attivo il gruppo di lavoro corrente
Dim nMachGrpId As Integer = EgtGetFirstMachGroup()
If Not EgtSetCurrMachGroup(nMachGrpId) Then
Return False
End If
EgtShowOnlyTable(True)
' Ripristino visualizzazione di eventuali pezzi in parcheggio
ShowParkedParts()
Return True
End Function
Friend Function SaveProject() As Boolean
' Determino nome del progetto
m_nCurrProj = Math.Abs(m_nCurrProj)
Dim sPath As String = m_MainWindow.GetSaveDir() & "\" & m_nCurrProj.ToString("D4") & ".nge"
' Aggiorno file Ini
WritePrivateProfileString(S_GENERAL, K_LASTPROJ, m_nCurrProj.ToString(), m_MainWindow.GetIniFile())
' Rinomino eventuale fotografia
Dim nPhotoId As Integer = GetPhoto()
If nPhotoId <> GDB_ID.NULL Then
' Path originale
Dim sPhoto As String = String.Empty
EgtGetPhotoPath(nPhotoId, sPhoto)
' Nuova path
Dim sNewPhoto As String = Path.ChangeExtension(sPath, Path.GetExtension(sPhoto))
' Se diverse, eseguo copia
If Not String.Equals(sPhoto, sNewPhoto, StringComparison.InvariantCultureIgnoreCase) Then
Try
File.Copy(sPhoto, sNewPhoto, True)
Catch ex As Exception
Return False
End Try
End If
' Notifico a foto il cambio di path
EgtChangePhotoPath(nPhotoId, sNewPhoto)
End If
' Se assente, inserisco contrassegno di progetto OmagCut valido
Dim nMarkId As Integer = EgtGetFirstNameInGroup(GDB_ID.ROOT, NAME_PROJMARK)
If nMarkId = GDB_ID.NULL Then
nMarkId = EgtCreateGroup(GDB_ID.ROOT)
EgtSetName(nMarkId, NAME_PROJMARK)
EgtSetLevel(nMarkId, GDB_LV.SYSTEM)
End If
' Eseguo
If Not SaveFile(sPath) Then
Return False
End If
' Dichiaro progetto non modificato
EgtResetModified()
Return True
End Function
Friend Function SaveFile(ByVal sPath As String) As Boolean
' Eseguo salvataggio
Dim bOk As Boolean = EgtSaveFile(sPath, NGE.CMPTEXT)
' Ripristino visualizzazione di eventuali pezzi in parcheggio (save li nasconde)
ShowParkedParts()
Return bOk
End Function
Friend Function LoadPhoto(ByVal sPath As String) As Boolean
' Verifico esistenza file immagine
If Not File.Exists(sPath) Then
Return False
End If
' Leggo file dati aggiuntivi se esiste
Dim ptOri As New Point3d(0, 0, 0)
Dim ptCen As New Point3d(0, 0, 1)
Dim dMMxPixel As Double = 1
Dim sAuxPath As String = Path.ChangeExtension(sPath, ".txt")
Try
Dim sLine As String = String.Empty
Dim sr As StreamReader = New StreamReader(sAuxPath)
Do While sr.Peek() > -1
sLine = sr.ReadLine()
sLine = sLine.Replace(" ", "")
If sLine.StartsWith("X=") Then
StringToDouble(sLine.Substring(2), ptOri.x)
ElseIf sLine.StartsWith("Y=") Then
StringToDouble(sLine.Substring(2), ptOri.y)
ElseIf sLine.StartsWith("Z_Lastra=") Then
StringToDouble(sLine.Substring(9), ptOri.z)
ElseIf sLine.StartsWith("X_ScaleCenter=") Then
StringToDouble(sLine.Substring(14), ptCen.x)
ElseIf sLine.StartsWith("Y_ScaleCenter=") Then
StringToDouble(sLine.Substring(14), ptCen.y)
ElseIf sLine.StartsWith("Z_ScaleCenter=") Then
StringToDouble(sLine.Substring(14), ptCen.z)
ElseIf sLine.StartsWith("Pixelxmm=") Then
Dim dTmp As Double
StringToDouble(sLine.Substring(9), dTmp)
If dTmp > EPS_SMALL Then
dMMxPixel = 1 / dTmp
End If
End If
Loop
sr.Close()
Catch ex As Exception
EgtOutLog("LoadPhoto Error on auxfile : " & sAuxPath)
End Try
' Aggiusto dati per spessore grezzo
If Math.Abs(m_dRawHeight) > EPS_SMALL Then
' Coefficiente di scalatura
Dim dFsca As Double = (ptCen.z - m_dRawHeight) / ptCen.z
dMMxPixel *= dFsca
ptOri.x = ptCen.x + (ptOri.x - ptCen.x) * dFsca
ptOri.y = ptCen.y + (ptOri.y - ptCen.y) * dFsca
ptOri.z = m_dRawHeight
End If
' Recupero origine della tavola e porto i punti in globale
Dim ptTab As Point3d
EgtGetTableRef1(ptTab)
ptOri.ToGlob(New Frame3d(ptTab))
ptCen.ToGlob(New Frame3d(ptTab))
' Recupero le dimensioni della tavola
Dim nAreaId As Integer
Dim ptMin, ptMax As Point3d
If Not EgtGetTableArea1(nAreaId) Or
Not EgtGetBBoxGlob(nAreaId, GDB_BB.STANDARD, ptMin, ptMax) Then
Return False
End If
' Elimino eventuale precedente foto
Dim nOldPhotoId = GetPhoto()
If nOldPhotoId <> GDB_ID.NULL Then
EgtErase(nOldPhotoId)
End If
' Se non esiste il gruppo per le foto, lo creo
Dim nPhGrpId As Integer = EgtGetFirstNameInGroup(GDB_ID.ROOT, PHOTO_GRP)
If nPhGrpId = GDB_ID.NULL Then
nPhGrpId = EgtCreateGroup(GDB_ID.ROOT)
If nPhGrpId = GDB_ID.NULL Then
Return False
End If
EgtSetName(nPhGrpId, PHOTO_GRP)
End If
EgtSetLevel(nPhGrpId, GDB_LV.SYSTEM)
' Carico la fotografia
Return EgtAddPhoto(PHOTO_NAME, sPath, ptOri, ptCen, dMMxPixel, nPhGrpId, ptMin, ptMax) <> GDB_ID.NULL
End Function
Friend Function ShowPhoto(ByVal bShow As Boolean) As Boolean
' Recupero la foto
Dim nId As Integer = GetPhoto()
If nId = GDB_ID.NULL Then
Return False
End If
' Ne cambio lo stato
Return EgtSetStatus(nId, If(bShow, GDB_ST.ON_, GDB_ST.OFF))
End Function
Friend Function GetPhoto() As Integer
Dim nPhGrpId As Integer = EgtGetFirstNameInGroup(GDB_ID.ROOT, PHOTO_GRP)
Return EgtGetFirstNameInGroup(nPhGrpId, PHOTO_NAME)
End Function
Friend Function GetPhotoTextureRef(ByRef refTxr As Frame3d) As Boolean
' Recupero la foto
Dim nId As Integer = GetPhoto()
If nId = GDB_ID.NULL Then
Return False
End If
' Recupero il riferimento in globale
Return EgtGetTextureFrame(nId, GDB_ID.ROOT, refTxr)
End Function
End Class