Files
OmagCUT/VeinMatchingWindow.xaml.vb
T
Dario Sassi 1580adb4d5 OmagCUT :
- modifiche per veinmatching multilastra.
2016-09-28 07:44:35 +00:00

471 lines
20 KiB
VB.net

Imports System.Windows.Interop
Imports System.Runtime.InteropServices
Imports EgtUILib
Public Class VeinMatchingWindow
' Riferimento alla MainWindow
Private m_MainWindow As MainWindow = DirectCast(Application.Current.MainWindow, MainWindow)
Private m_bFirst As Boolean = True
Private m_bPositioned As Boolean = False
' Dichiarazione Scene
Friend WithEvents VeinMatchingScene As New Scene
Private VeinMatchingSceneHost As New System.Windows.Forms.Integration.WindowsFormsHost
Private Sub Window_Initialized(sender As Object, e As EventArgs)
' Assegnazione scena all'host e posizionamento nella PlacePageGrid
VeinMatchingSceneHost.Child = VeinMatchingScene
VeinMatchingSceneHost.SetValue(Grid.ColumnProperty, 1)
Me.VeinMatchingGrid.Children.Add(VeinMatchingSceneHost)
' Per non farla visualizzare alla creazione
Me.Left = 32000
' Assegno messaggi
NewBtn.Content = EgtMsg(MSG_CADCUTPAGEUC + 3)
LoadBtn.Content = EgtMsg(MSG_CADCUTPAGEUC + 4)
SaveBtn.Content = EgtMsg(MSG_CADCUTPAGEUC + 5)
ExportBtn.Content = EgtMsg(MSG_CADCUTPAGEUC + 10)
' Disabilitazione provvisoria
LoadBtn.IsEnabled = False
SaveBtn.IsEnabled = False
ExportBtn.IsEnabled = False
End Sub
Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
If m_bFirst Then
' Imposto finestra senza SystemMenu
Dim hwnd As IntPtr = New WindowInteropHelper(Me).Handle
SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) And Not WS_SYSMENU)
' imposto colore di default
Dim DefColor As New Color3d(0, 0, 0)
GetPrivateProfileColor(S_GEOMDB, K_DEFAULTCOLOR, DefColor, m_MainWindow.GetIniFile())
VeinMatchingScene.SetDefaultMaterial(DefColor)
' imposto colori sfondo
Dim BackTopColor As New Color3d(211, 211, 211)
GetPrivateProfileColor(S_SCENE, K_BACKTOP, BackTopColor, m_MainWindow.GetIniFile())
Dim BackBotColor As New Color3d(211, 211, 211)
GetPrivateProfileColor(S_SCENE, K_BACKBOTTOM, BackBotColor, m_MainWindow.GetIniFile())
VeinMatchingScene.SetViewBackground(BackTopColor, BackBotColor)
' imposto colore di evidenziazione
Dim MarkColor As New Color3d(255, 255, 0)
GetPrivateProfileColor(S_SCENE, K_MARK, MarkColor, m_MainWindow.GetIniFile())
VeinMatchingScene.SetMarkMaterial(MarkColor)
' imposto colore per superfici selezionate
Dim SelSurfColor As New Color3d(255, 255, 192)
GetPrivateProfileColor(S_SCENE, K_SELSURF, SelSurfColor, m_MainWindow.GetIniFile())
VeinMatchingScene.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())
VeinMatchingScene.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())
VeinMatchingScene.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())
VeinMatchingScene.SetViewAttributes(nDriver, b2Buff, nColorBits, nDepthBits)
' inizializzo la scena (DB geometrico + visualizzazione) e verifico presenza chiave
If Not VeinMatchingScene.Init() Then
m_MainWindow.m_CadCutPageUC.m_ProjectMgr.VeinMatchingBtn.IsChecked = False
Me.Close()
End If
m_bFirst = False
End If
' inibisco selezione diretta da Scene
VeinMatchingScene.SetStatusNull()
End Sub
Private Sub Window_Closed(sender As Object, e As EventArgs) Handles Me.Closed
' Salvo posizione Window (se posizionata e non minimizzata)
If m_bPositioned And Me.WindowState <> WindowState.Minimized Then
Dim nFlag As Integer = If(Me.WindowState = WindowState.Maximized, 1, 0)
WritePrivateProfileWinPos(S_VEINMATCHING, K_VEINMA_PLACE, nFlag, CInt(Me.Left), CInt(Me.Top), CInt(Me.Width), CInt(Me.Height), m_MainWindow.GetIniFile())
End If
End Sub
Friend Sub AdjustPosition()
' Se già pozizionata, esco subito
If m_bPositioned Then Return
' Imposto posizione e dimensioni della MainWindow da INI
Dim nFlag, nLeft, nTop, nWidth, nHeight As Integer
If GetPrivateProfileWinPos(S_VEINMATCHING, K_VEINMA_PLACE, nFlag, nLeft, nTop, nWidth, nHeight, m_MainWindow.GetIniFile()) Then
Dim PtTL = New System.Drawing.Point(nLeft, nTop)
Dim s As System.Windows.Forms.Screen = System.Windows.Forms.Screen.FromPoint(PtTL)
If s.Bounds.Contains(PtTL) Then
Me.WindowStartupLocation = Windows.WindowStartupLocation.Manual
Me.Top = nTop
Me.Left = nLeft
Me.Height = nHeight
Me.Width = nWidth
Me.WindowState = If(nFlag = 1, WindowState.Maximized, WindowState.Normal)
m_bPositioned = True
Return
End If
End If
' Imposto in posizione standard
Me.WindowStartupLocation = Windows.WindowStartupLocation.Manual
Me.Top = m_MainWindow.Top
Me.Left = m_MainWindow.Left
Me.Height = nHeight
Me.Width = nWidth
Me.WindowState = If(nFlag = 1, WindowState.Maximized, WindowState.Normal)
m_bPositioned = True
End Sub
Private Sub NewBtn_Click(sender As Object, e As RoutedEventArgs) Handles NewBtn.Click
VeinMatching.Clear()
End Sub
End Class
Friend Module VeinMatching
' Riferimento alla MainWindow
Private m_MainWindow As MainWindow = DirectCast(Application.Current.MainWindow, MainWindow)
' Contesto del VeinMatching
Friend m_nVeinCtx As Integer = 0
' Nome fotografia
Private m_nPhoto As Integer = 0
' Costanti
Const REF_NAME As String = "Ref"
Const KEY_ORI_REF As String = "OriRef"
Friend Function SetRefOnAllParts(nCtx As Integer) As Boolean
' Si opera nel contesto indicato
EgtSetCurrentContext(nCtx)
Dim nId As Integer = EgtGetFirstPart()
While nId <> GDB_ID.NULL
' Gruppo regione
Dim nRegLayId As Integer = EgtGetFirstNameInGroup(nId, NAME_REGION)
' Entità superficie regione piatta
Dim nRegId As Integer = EgtGetFirstInGroup(nRegLayId)
While nRegId <> GDB_ID.NULL
If EgtGetType(nRegId) = GDB_TY.SRF_FRGN Then
Exit While
End If
nRegId = EgtGetNext(nRegId)
End While
' Ne recupero il centroide
Dim ptCen As Point3d
EgtCentroid(nRegId, GDB_ID.ROOT, ptCen)
' Inserisco il riferimento
Dim frRef As New Frame3d(ptCen)
Dim nRefId As Integer = EgtCreateGeoFrame(nRegLayId, frRef, GDB_RT.GLOB)
EgtSetName(nRefId, REF_NAME)
' salvo nelle info il riferimento originale
EgtSetInfo(nRefId, KEY_ORI_REF, frRef)
' nascondo l'oggetto appena inserito
EgtSetMode(nRefId, GDB_MD.HIDDEN)
' Passo al pezzo successivo
nId = EgtGetNextPart(nId)
End While
Return True
End Function
Friend Function Clear() As Boolean
' Verifico esista il contesto del VeinMatching
If m_nVeinCtx = 0 Then Return True
' Imposto VeinMatching context
Dim nCurrCtx = SetVeinContext()
If nCurrCtx = 0 Then Return False
' Pulisco il DB
EgtNewFile()
EgtDraw()
' Reset contatore foto
m_nPhoto = 0
' Se necessario, ripristino il contesto originale
If nCurrCtx > 0 Then EgtSetCurrentContext(nCurrCtx)
Return True
End Function
Friend Function AddPart(sPartFile As String, nPartId As Integer) As Boolean
' Verifico esista il contesto del VeinMatching
If m_nVeinCtx = 0 Then Return True
' Imposto VeinMatching context
Dim nMainCtx = SetVeinContext()
If nMainCtx = 0 Then Return False
' Inserisco il pezzo
EgtInsertFile(sPartFile)
' Ne recupero l'Id
Dim nVeinId2 As Integer = EgtGetLastPart()
' Assegno Id originale
EgtSetInfo(nVeinId2, KEY_ORI_ID, nPartId)
' Nascondo scritte, sono nel layer "Region"
Dim nVeinRegId As Integer = EgtGetFirstNameInGroup(nVeinId2, NAME_REGION)
Dim nCurrId As Integer = EgtGetFirstInGroup(nVeinRegId)
While nCurrId <> GDB_ID.NULL
If EgtGetType(nCurrId) = GDB_TY.EXT_TEXT Then EgtSetStatus(nCurrId, GDB_ST.OFF)
nCurrId = EgtGetNext(nCurrId)
End While
' Nascondo layer con valori angoli dei lati
nCurrId = EgtGetFirstNameInGroup(nVeinId2, SIDE_ANGLE_LAYER)
EgtSetStatus(nCurrId, GDB_ST.OFF)
' Se necessario, ripristino il contesto originale
If nMainCtx > 0 Then EgtSetCurrentContext(nMainCtx)
Return True
End Function
Friend Function OnInsertPartInRaw(nPartId As Integer) As Boolean
' Verifico esista il contesto del VeinMatching
If m_nVeinCtx = 0 Then Return True
' Recupero il contesto corrente (principale)
Dim nMainCtx = EgtGetCurrentContext()
' Se non c'è la fotografia, esco
Dim nPhotoId As Integer = m_MainWindow.m_CurrentProjectPageUC.GetPhoto()
If nPhotoId = GDB_ID.NULL Then Return True
' Recupero il nome originale del pezzo
Dim nOriId As Integer = nPartId
EgtGetInfo(nPartId, KEY_ORI_ID, nOriId)
' Verifico se esiste già la foto del progetto corrente nel VeinMatching
Dim sPhoto As String = String.Empty
EgtGetPhotoPath(nPhotoId, sPhoto)
Dim sVeinPhoto As String = String.Empty
GetVeinPhotoPath(sVeinPhoto)
' Se necessario, copio la foto
If String.Compare(sPhoto, sVeinPhoto, True) <> 0 Then
If Not CopyPhoto(nPhotoId) Then Return False
End If
' Recupero il pezzo nel VeinMatching
EgtSetCurrentContext(m_nVeinCtx)
Dim nId = GetVeinPartId(nOriId)
If nId <> GDB_ID.NULL Then
' Recupero la regione del pezzo
Dim nRegId As Integer = GetVeinPartRegionId(nId)
' Gli assegno la texture della foto
EgtSetTextureName(nRegId, GetPhotoName())
' Sistemo il riferimento della texture
Dim refTxr As New Frame3d
GetVeinRefPhoto(nMainCtx, nPartId, nId, refTxr)
EgtSetTextureFrame(nRegId, refTxr, GDB_RT.GLOB)
' Sistemo il colore
Dim colWhite As New Color3d(255, 255, 255, 100)
EgtSetColor(nRegId, colWhite)
End If
EgtDraw()
EgtSetCurrentContext(nMainCtx)
Return True
End Function
Friend Function OnMovePartInRaw(nPartId As Integer) As Boolean
' Verifico esista il contesto del VeinMatching
If m_nVeinCtx = 0 Then Return True
' Recupero il contesto corrente (principale)
Dim nMainCtx = EgtGetCurrentContext()
' Se non c'è la fotografia, esco
Dim nPhotoId As Integer = m_MainWindow.m_CurrentProjectPageUC.GetPhoto()
If nPhotoId = GDB_ID.NULL Then Return True
' Recupero il nome originale del pezzo
Dim nOriId As Integer = nPartId
EgtGetInfo(nPartId, KEY_ORI_ID, nOriId)
' Recupero il pezzo nel VeinMatching
EgtSetCurrentContext(m_nVeinCtx)
Dim nId = GetVeinPartId(nOriId)
If nId <> GDB_ID.NULL Then
' Recupero la regione del pezzo
Dim nRegId As Integer = GetVeinPartRegionId(nId)
' Sistemo il riferimento della texture
Dim refTxr As New Frame3d
GetVeinRefPhoto(nMainCtx, nPartId, nId, refTxr)
EgtSetTextureFrame(nRegId, refTxr, GDB_RT.GLOB)
End If
EgtDraw()
EgtSetCurrentContext(nMainCtx)
Return True
End Function
Friend Function OnRemovePartFromRaw(nPartId As Integer) As Boolean
' Verifico esista il contesto del VeinMatching
If m_nVeinCtx = 0 Then Return True
' Recupero il contesto corrente (principale)
Dim nMainCtx = EgtGetCurrentContext()
' Se non c'è la fotografia, esco
Dim nPhotoId As Integer = m_MainWindow.m_CurrentProjectPageUC.GetPhoto()
If nPhotoId = GDB_ID.NULL Then Return True
' Recupero il nome originale del pezzo
Dim nOriId As Integer = nPartId
EgtGetInfo(nPartId, KEY_ORI_ID, nOriId)
' Tolgo la texture dal pezzo
EgtSetCurrentContext(m_nVeinCtx)
Dim nId As Integer = GetVeinPartId(nOriId)
If nId <> GDB_ID.NULL Then
' Recupero la regione del pezzo
Dim nRegId As Integer = GetVeinPartRegionId(nId)
' Gli tolgo la texture
EgtRemoveTextureData(nRegId)
' Sistemo il colore
Dim colAqua As New Color3d(0, 255, 255, 25)
EgtSetColor(nRegId, colAqua)
End If
EgtDraw()
EgtSetCurrentContext(nMainCtx)
Return True
End Function
Friend Function ZoomAll() As Boolean
' Verifico esista il contesto del VeinMatching
If m_nVeinCtx = 0 Then Return True
' Imposto VeinMatching context
Dim nCurrCtx = SetVeinContext()
If nCurrCtx = 0 Then Return False
' Eseguo Zoom
EgtZoom(ZM.ALL)
' Se necessario, ripristino il contesto originale
If nCurrCtx > 0 Then EgtSetCurrentContext(nCurrCtx)
Return True
End Function
Private Function GetPhotoName() As String
Return PHOTO_NAME & m_nPhoto.ToString()
End Function
Private Function CopyPhoto(nPhotoId As Integer) As Boolean
' Recupero il contesto corrente
Dim nCurrCtx = EgtGetCurrentContext()
' Recupero i dati della fotografia
Dim sPath As String = String.Empty
EgtGetPhotoPath(nPhotoId, sPath)
Dim ptOri As Point3d
EgtGetPhotoOrigin(nPhotoId, ptOri)
Dim ptCen As Point3d
EgtGetPhotoCenter(nPhotoId, ptCen)
Dim dMMxPixel As Double
EgtGetPhotoMMxPixel(nPhotoId, dMMxPixel)
Dim ptMin, ptMax As Point3d
EgtGetBBoxGlob(nPhotoId, GDB_BB.STANDARD, ptMin, ptMax)
' Passo al contesto del VeinMatching
EgtSetCurrentContext(m_nVeinCtx)
' 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
EgtSetCurrentContext(nCurrCtx)
Return False
End If
EgtSetName(nPhGrpId, PHOTO_GRP)
End If
EgtSetLevel(nPhGrpId, GDB_LV.SYSTEM)
EgtSetStatus(nPhGrpId, GDB_ST.OFF)
' Carico la fotografia
m_nPhoto += 1
Dim nNewId As Integer = EgtAddPhoto(GetPhotoName(), sPath, ptOri, ptCen, dMMxPixel, nPhGrpId, ptMin, ptMax)
' Ritorno al contesto corrente
EgtSetCurrentContext(nCurrCtx)
Return nNewId <> GDB_ID.NULL
End Function
Private Function SetVeinContext() As Integer
' Verifico esista il contesto del VeinMatching
If m_nVeinCtx = 0 Then Return 0
' Recupero il contesto corrente
Dim nCurrCtx = EgtGetCurrentContext()
' Se necessario, cambio contesto
If m_nVeinCtx <> nCurrCtx Then
If EgtSetCurrentContext(m_nVeinCtx) Then
Return nCurrCtx
Else
Return 0
End If
Else
Return -1
End If
End Function
Private Function GetVeinPhoto() As Integer
' Imposto VeinMatching context
Dim nCurrCtx = SetVeinContext()
If nCurrCtx = 0 Then Return GDB_ID.NULL
' Recupero Id foto
Dim nId As Integer = EgtGetFirstNameInGroup(EgtGetFirstNameInGroup(GDB_ID.ROOT, PHOTO_GRP), GetPhotoName())
' Se necessario, ripristino il contesto originale
If nCurrCtx > 0 Then EgtSetCurrentContext(nCurrCtx)
Return nId
End Function
Private Function GetVeinPhotoPath(ByRef sPath As String) As Boolean
' Imposto VeinMatching context
Dim nCurrCtx = SetVeinContext()
If nCurrCtx = 0 Then Return False
' Recupero path dell'immagine della foto
Dim nId As Integer = EgtGetFirstNameInGroup(EgtGetFirstNameInGroup(GDB_ID.ROOT, PHOTO_GRP), GetPhotoName())
Dim bOk As Boolean = EgtGetPhotoPath(nId, sPath)
' Se necessario, ripristino il contesto originale
If nCurrCtx > 0 Then EgtSetCurrentContext(nCurrCtx)
Return bOk
End Function
Private Function GetVeinRefPhoto(nMainCtx As Integer, nPartId As Integer, nVePartId As Integer, ByRef refPhoto As Frame3d)
' Riferimento della foto rispetto al riferimento del pezzo nel contesto principale
If Not EgtSetCurrentContext(nMainCtx) Then Return False
' riferimento della foto in globale
If Not m_MainWindow.m_CurrentProjectPageUC.GetPhotoTextureRef(refPhoto) Then Return False
' riferimento del pezzo in globale
Dim nRefId As Integer = EgtGetFirstNameInGroup(EgtGetFirstNameInGroup(nPartId, NAME_REGION), REF_NAME)
Dim refPart As New Frame3d
If Not EgtFrame(nRefId, GDB_ID.ROOT, refPart) Then Return False
' porto il riferimento della foto in quello del pezzo
refPhoto.ToLoc(refPart)
' Riferimento della foto rispetto al riferimento del pezzo in VeinMatching
If Not EgtSetCurrentContext(m_nVeinCtx) Then Return False
' riferimento del pezzo in globale
Dim nVeRefId As Integer = EgtGetFirstNameInGroup(EgtGetFirstNameInGroup(nVePartId, NAME_REGION), REF_NAME)
Dim refVePart As New Frame3d
If Not EgtFrame(nVeRefId, GDB_ID.ROOT, refVePart) Then Return False
refPhoto.ToGlob(refVePart)
Return True
End Function
Private Function GetVeinPartId(nPartId As Integer) As Integer
' Imposto VeinMatching context
Dim nCurrCtx = SetVeinContext()
If nCurrCtx = 0 Then Return GDB_ID.NULL
' Cerco il pezzo in Vein che corrisponde al pezzo desiderato
Dim nId As Integer = EgtGetFirstPart()
While nId <> GDB_ID.NULL
Dim nOriId As Integer
If EgtGetInfo(nId, KEY_ORI_ID, nOriId) AndAlso nOriId = nPartId Then
Exit While
End If
nId = EgtGetNextPart(nId)
End While
' Se necessario, ripristino il contesto originale
If nCurrCtx > 0 Then EgtSetCurrentContext(nCurrCtx)
Return nId
End Function
Private Function GetVeinPartRegionId(nVeinPartId As Integer) As Integer
' Imposto VeinMatching context
Dim nCurrCtx = SetVeinContext()
If nCurrCtx = 0 Then Return GDB_ID.NULL
' Gruppo regione
Dim nRegLayId As Integer = EgtGetFirstNameInGroup(nVeinPartId, NAME_REGION)
' Entità superficie regione piatta
Dim nRegId As Integer = EgtGetFirstInGroup(nRegLayId)
While nRegId <> GDB_ID.NULL
If EgtGetType(nRegId) = GDB_TY.SRF_FRGN Then
Exit While
End If
nRegId = EgtGetNext(nRegId)
End While
' Se necessario, ripristino il contesto originale
If nCurrCtx > 0 Then EgtSetCurrentContext(nCurrCtx)
Return nRegId
End Function
End Module