Files
OmagCUT/ComponentiUC.xaml.vb
T
Emmanuele Sassi c529c9dd59 OmagCUT :
- aggiungo file mancanti.
2015-08-23 09:50:47 +00:00

429 lines
16 KiB
VB.net

Imports EgtUILib
Imports System.Globalization
Public Class ComponentiUC
Dim MainWindowUC As MainWindow = Application.Current.MainWindow
Private WithEvents m_Controller3 As New Controller
'Per creazione Scene
Friend Shared WithEvents ScenaComponenti As New Scene
Dim ScenaComponentiHost As New System.Windows.Forms.Integration.WindowsFormsHost
'Per Scena Componenti
' Constants
Private Const NUM_VAR As Integer = 10
Private Const LUA_CMP_VARS As String = "CMP"
Private Const LUA_CMP_DRAW As String = "CMP_Draw"
' Properties
Private m_sCompoDir As String = String.Empty
Private m_sCompoName As String = String.Empty
Private m_CVars(NUM_VAR - 1) As CompoVar
Private m_bDrawOk As Boolean = False
Private m_bFirst As Boolean = True
Private Sub ComponentiUC_Initialized(sender As Object, e As EventArgs)
ScenaComponentiHost.Child = ScenaComponenti
ScenaComponentiHost.SetValue(Grid.ColumnProperty, 1)
ScenaComponentiHost.SetValue(Grid.RowProperty, 0)
ScenaComponentiHost.SetValue(Grid.RowSpanProperty, 2)
Me.BackGroundGridComponenti.Children.Add(ScenaComponentiHost)
End Sub
Private Sub ComponentiUC_Loaded(sender As Object, e As RoutedEventArgs)
If (Not m_bFirst) Then
EgtSetCurrentContext(ScenaComponenti.GetCtx())
Return
End If
' imposto colore di default
Dim DefColor As New Color3d(0, 0, 0)
GetPrivateProfileColor(S_GEOMDB, K_DEFAULTCOLOR, DefColor, MainWindow.GetIniFile())
ScenaComponenti.SetDefaultMaterial(DefColor)
' imposto colori sfondo
Dim BackTopColor As New Color3d(192, 192, 192)
GetPrivateProfileColor(S_SCENE, K_BACKTOP, BackTopColor, MainWindow.GetIniFile())
Dim BackBotColor As New Color3d(BackTopColor)
GetPrivateProfileColor(S_SCENE, K_BACKBOTTOM, BackBotColor, MainWindow.GetIniFile())
ScenaComponenti.SetViewBackground(BackTopColor, BackBotColor)
' imposto colore di evidenziazione
Dim MarkColor As New Color3d(255, 255, 0)
GetPrivateProfileColor(S_SCENE, K_MARK, MarkColor, MainWindow.GetIniFile())
ScenaComponenti.SetMarkMaterial(MarkColor)
' imposto colore per superfici selezionate
Dim SelSurfColor As New Color3d(255, 255, 192)
GetPrivateProfileColor(S_SCENE, K_SELSURF, SelSurfColor, MainWindow.GetIniFile())
ScenaComponenti.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, MainWindow.GetIniFile())
ScenaComponenti.SetZoomWinAttribs(bOutline, ZwColor)
' imposto colore della linea di distanza
Dim DstLnColor As New Color3d(255, 0, 0)
GetPrivateProfileColor(S_SCENE, K_DISTLINE, DstLnColor, MainWindow.GetIniFile())
ScenaComponenti.SetDistLineMaterial(DstLnColor)
' imposto parametri OpenGL
Dim nDriver As Integer = GetPrivateProfileInt(S_OPENGL, K_DRIVER, 3, MainWindow.GetIniFile())
Dim b2Buff As Boolean = (GetPrivateProfileInt(S_OPENGL, K_DOUBLEBUFFER, 1, MainWindow.GetIniFile()) <> 0)
Dim nColorBits As Integer = GetPrivateProfileInt(S_OPENGL, K_COLORBITS, 32, MainWindow.GetIniFile())
Dim nDepthBits As Integer = GetPrivateProfileInt(S_OPENGL, K_DEPTHBITS, 32, MainWindow.GetIniFile())
ScenaComponenti.SetViewAttributes(nDriver, b2Buff, nColorBits, nDepthBits)
'Inizializzazione delle viste
ScenaComponenti.Init()
m_Controller3.SetScene(ScenaComponenti)
' leggo direttorio componenti
GetPrivateProfileString(S_COMPO, K_COMPODIR, "", m_sCompoDir, MainWindow.GetIniFile())
m_bFirst = False
End Sub
Private Sub TriangleBtnUC_Click(sender As Object, e As RoutedEventArgs)
' Carico componente
LoadCurrentCompo("Triangolo3L")
End Sub
Private Sub RectangleTrapeziumBtnUC_Click(sender As Object, e As RoutedEventArgs)
End Sub
Private Sub LoadCurrentCompo(ByVal sCompo As String)
' verifico se cambiato
'If sCompo = m_sCompoName Then
' Return
'End If
m_sCompoName = sCompo
'Pulisco l'ambiente lua
ResetLuaVariables()
' Carico il file ed eseguo in modalità anteprima
Dim bOk As Boolean = ExecCompoFile()
Dim sMsg As String = String.Empty
bOk = bOk AndAlso MakePreview(sMsg)
If Not bOk Then
EgtNewFile()
End If
tbMsg.Text = sMsg
tbMsg.Background = If(m_bDrawOk, Brushes.White, Brushes.Tomato)
ScenaComponenti.ZoomAll()
' leggo variabili e aggiorno griglia
ReadAndShowVariables()
' abilito bottoni Vista e Inserisci
btnView.IsEnabled = True
btnInsert.IsEnabled = True
' un pezzo da inserire
tbNbr.Text = "1"
End Sub
Private Sub ViewBtnUC_Click(sender As Object, e As RoutedEventArgs)
ViewRefresh()
End Sub
Private Sub ViewRefresh()
' aggiorno le variabili dalla griglia
UpdateVariables()
' ricalcolo il disegno
Dim sMsg As String = String.Empty
MakePreview(sMsg)
tbMsg.Text = sMsg
tbMsg.Background = If(m_bDrawOk, Brushes.White, Brushes.Tomato)
' aggiorno visualizzazione
EgtSetView(VT.TOP, False)
EgtZoom(ZM.ALL)
End Sub
Private Sub InsertBtnUC_Click(sender As Object, e As RoutedEventArgs)
' aggiorno visualizzazione
ViewRefresh()
' se errore esco
If Not m_bDrawOk Then
Return
End If
' Leggo numero di pezzi da inserire
Dim InsNbr As Integer = tbNbr.Text
'Pulisco la scena dl componente
EgtNewFile()
' Passo al contesto principale
EgtSetCurrentContext(MainWindowUC.ActiveScene.GetCtx())
' Inserisco il componente nel DB geometrico principale
MakeInsert(InsNbr)
' Aggiorno ambiente principale
EgtZoom(ZM.ALL)
'Reset nome componente corrente
m_sCompoName = String.Empty
ResetLuaVariables()
' Chiudo il dialogo
MainWindowUC.TagliCadContent.Content = MainWindowUC.m_TagliCadUC
End Sub
Private Function ExecCompoFile() As Boolean
' Costruisco path completa del componente
Dim sPath = m_sCompoDir & "\" & m_sCompoName & ".lua"
' Carico il file
Return EgtLuaExecFile(sPath)
End Function
Private Function ReadAndShowVariables() As Boolean
' recupero nome, tipo e valore delle variabili globali
For i As Integer = 1 To NUM_VAR
Dim CVar = New CompoVar
If CVar.NameTypeValueFromLua(i) Then
m_CVars(i - 1) = CVar
Else
m_CVars(i - 1) = Nothing
End If
Next
' aggiorno la griglia dalle variabili
For i As Integer = 1 To NUM_VAR
If m_CVars(i - 1) IsNot Nothing Then
GetNameEdit(i).Text = m_CVars(i - 1).m_sName
GetNameEdit(i).Visibility = Windows.Visibility.Visible
GetValueEdit(i).Text = m_CVars(i - 1).ToString()
GetValueEdit(i).Visibility = Windows.Visibility.Visible
Else
GetNameEdit(i).Visibility = Windows.Visibility.Hidden
GetValueEdit(i).Visibility = Windows.Visibility.Hidden
End If
Next
Return True
End Function
Private Function UpdateVariables() As Boolean
' aggiorno le variabili
For i As Integer = 1 To NUM_VAR
If m_CVars(i - 1) IsNot Nothing Then
' interpreto il valore, se non riesco ripristino default
If Not m_CVars(i - 1).FromString(GetValueEdit(i).Text) Then
GetValueEdit(i).Text = m_CVars(i - 1).ToString()
End If
' aggiorno la corrispondente variabile lua
If Not m_CVars(i - 1).ToLua(i) Then
Dim sErr As String = String.Empty
EgtLuaGetLastError(sErr)
EgtOutLog(sErr)
End If
End If
Next
Return True
End Function
Private Function ResetLuaVariables() As Boolean
EgtLuaResetGlobVar(LUA_CMP_VARS)
EgtLuaResetGlobVar(LUA_CMP_DRAW)
Return False
End Function
Private Function MakePreview(ByRef sMsg As String) As Boolean
If Not EgtLuaExecLine(LUA_CMP_DRAW & "(true)") Then
sMsg = "Error in component execution"
m_bDrawOk = False
Else
EgtLuaGetGlobStringVar(LUA_CMP_VARS & ".MSG", sMsg)
Dim nErr As Integer = 0
EgtLuaGetGlobIntVar(LUA_CMP_VARS & ".ERR", nErr)
m_bDrawOk = (nErr = 0)
End If
Return m_bDrawOk
End Function
Private Function MakeInsert(ByVal nNbr As Integer) As Boolean
' abilito registrazione
EgtEnableCommandLogger()
' ricarico componente corrente
ExecCompoFile()
' aggiorno variabili
UpdateVariables()
' elimino eventuali precedenti pezzi vuoti
EgtEraseEmptyParts()
' eseguo inserimento
For i As Integer = 1 To nNbr
' Inserisco il componente
If Not EgtLuaExecLine(LUA_CMP_DRAW & "(false)") Then
Dim sErr As String = String.Empty
EgtLuaGetLastError(sErr)
EgtOutLog(sErr)
Exit For
End If
' Ne recupero l'Id
Dim nId2 As Integer = EgtGetLastPart()
' Lo posiziono in ordine
EgtPackPart(nId2, PACK_MAX_X, PACK_OFFS)
Next
' disabilito registrazione
EgtDisableCommandLogger()
Return True
End Function
Private Function GetNameEdit(ByVal nInd As Integer) As TextBox
Select Case nInd
Case 1
Return tbName1
Case 2
Return tbName2
Case 3
Return tbName3
Case 4
Return tbName4
Case 5
Return tbName5
Case 6
Return tbName6
Case 7
Return tbName7
Case 8
Return tbName8
Case 9
Return tbName9
Case Else
Return tbName10
End Select
End Function
Private Function GetValueEdit(ByVal nInd As Integer) As TextBox
Select Case nInd
Case 1
Return tbValue1
Case 2
Return tbValue2
Case 3
Return tbValue3
Case 4
Return tbValue4
Case 5
Return tbValue5
Case 6
Return tbValue6
Case 7
Return tbValue7
Case 8
Return tbValue8
Case 9
Return tbValue9
Case Else
Return tbValue10
End Select
End Function
Private Class CompoVar
' Public Members
Public m_sName As String
Public m_nType As Integer
Public m_bVal As Boolean
Public m_nVal As Integer
Public m_dVal As Double
Public m_sVal As String
' Constants
Const LUA_NAME As String = LUA_CMP_VARS & ".N"
Const LUA_TYPE As String = LUA_CMP_VARS & ".T"
Const LUA_VALUE As String = LUA_CMP_VARS & ".V"
Public Sub New()
m_nType = 0
End Sub
Public Overrides Function ToString() As String
Select Case m_nType
Case 1 ' booleano
Return m_bVal.ToString()
Case 2 ' intero
Return m_nVal.ToString()
Case 3 ' lunghezza
Return EgtToUiUnits(m_dVal).ToString("F4", CultureInfo.InvariantCulture)
Case 4 ' double
Return m_dVal.ToString("F4", CultureInfo.InvariantCulture)
Case 5 ' stringa
Return m_sVal
End Select
Return ""
End Function
Public Function FromString(ByVal sVal As String) As Boolean
Select Case m_nType
Case 1 ' booleano
Dim bVal As Boolean = False
If Boolean.TryParse(sVal, bVal) Then
m_bVal = bVal
Return True
End If
Case 2 ' intero
Dim dVal As Double
If EgtLuaEvalNumExpr(sVal, dVal) Then
m_nVal = CInt(dVal)
Return True
End If
Case 3 ' lunghezza
Dim dVal As Double
If EgtLuaEvalNumExpr(sVal, dVal) Then
m_dVal = EgtFromUiUnits(dVal)
Return True
End If
Case 4 ' double
Dim dVal As Double
If EgtLuaEvalNumExpr(sVal, dVal) Then
m_dVal = dVal
Return True
End If
Case 5 'stringa
m_sVal = sVal
Return True
End Select
Return False
End Function
Public Function ToLua(ByVal nInd As Integer) As Boolean
Select Case m_nType
Case 1
Return EgtLuaSetGlobBoolVar(LUA_VALUE & nInd.ToString(), m_bVal)
Case 2
Return EgtLuaSetGlobIntVar(LUA_VALUE & nInd.ToString(), m_nVal)
Case 3, 4
Return EgtLuaSetGlobNumVar(LUA_VALUE & nInd.ToString(), m_dVal)
Case 5
Return EgtLuaSetGlobStringVar(LUA_VALUE & nInd.ToString(), m_sVal)
End Select
Return False
End Function
Public Function NameTypeValueFromLua(ByVal nInd As Integer) As Boolean
Dim bOk As Boolean = True
bOk = bOk AndAlso EgtLuaGetGlobStringVar(LUA_NAME & nInd.ToString(), m_sName)
bOk = bOk AndAlso EgtLuaGetGlobIntVar(LUA_TYPE & nInd.ToString(), m_nType)
Select Case m_nType
Case 1
Return bOk AndAlso EgtLuaGetGlobBoolVar(LUA_VALUE & nInd.ToString(), m_bVal)
Case 2
Return bOk AndAlso EgtLuaGetGlobIntVar(LUA_VALUE & nInd.ToString(), m_nVal)
Case 3, 4
Return bOk AndAlso EgtLuaGetGlobNumVar(LUA_VALUE & nInd.ToString(), m_dVal)
Case 5
Return bOk AndAlso EgtLuaGetGlobStringVar(LUA_VALUE & nInd.ToString(), m_sVal)
End Select
Return False
End Function
End Class
Private Sub CloseBtnUC_Click(sender As Object, e As RoutedEventArgs)
'Pulisco la scena dl componente
EgtNewFile()
' Passo al contesto principale
EgtSetCurrentContext(MainWindowUC.ActiveScene.GetCtx())
' Aggiorno ambiente principale
EgtZoom(ZM.ALL)
'Reset nome componente corrente
m_sCompoName = String.Empty
ResetLuaVariables()
' Chiudo il dialogo
MainWindowUC.TagliCadContent.Content = MainWindowUC.m_TagliCadUC
End Sub
Private Sub ComponentiUC_Unloaded(sender As Object, e As RoutedEventArgs)
'ScenaComponenti.Terminate()
End Sub
End Class