Files
TestEIn/Scene.vb
T
Dario Sassi 18426ab481 TestEIn 1.5j1 :
- aggiornamenti per selezione con mouse.
2014-10-10 06:44:14 +00:00

503 lines
18 KiB
VB.net

Imports System.Math
Imports System.IO
Imports System.Globalization
Imports TestEIn.EgtInterface
Public Class Scene
'---- Members ---------
Private m_nGseContext As Integer
Private m_nStatus As Integer
Private Enum ST As Integer
NULL = 0
PAN = 1
ROT = 2
ZOOMWIN = 3
End Enum
Private m_PrevPoint As Point
Private m_nDefMatRed As Integer
Private m_nDefMatGreen As Integer
Private m_nDefMatBlue As Integer
Private m_nDriver As Integer
Private m_b2Buff As Boolean
Private m_nColorBits As Integer
Private m_nDepthBits As Integer
Private m_nBackTopRed As Integer
Private m_nBackTopGreen As Integer
Private m_nBackTopBlue As Integer
Private m_nBackBotRed As Integer
Private m_nBackBotGreen As Integer
Private m_nBackBotBlue As Integer
'---- Constructor -----
Sub New()
' Chiamata richiesta dalla finestra di progettazione.
InitializeComponent()
' Istruzioni di inizializzazione.
Me.BorderStyle = Windows.Forms.BorderStyle.FixedSingle
SetStyle(ControlStyles.Opaque, True)
SetStyle(ControlStyles.UserPaint, True)
SetStyle(ControlStyles.AllPaintingInWmPaint, True)
m_nGseContext = 0
m_nStatus = ST.NULL
m_PrevPoint = Point.Empty
m_nDefMatRed = 255
m_nDefMatGreen = 165
m_nDefMatBlue = 0
m_nDriver = 3
m_b2Buff = True
m_nColorBits = 24
m_nDepthBits = 32
m_nBackTopRed = 140
m_nBackTopGreen = 154
m_nBackTopBlue = 168
m_nBackBotRed = 40
m_nBackBotGreen = 44
m_nBackBotBlue = 48
Cursor = New Cursor(Me.GetType(), "Select.cur")
End Sub
'---- Initials --------
Public Sub SetDefaultMaterial(ByVal nRed As Integer, ByVal nGreen As Integer, ByVal nBlue As Integer)
m_nDefMatRed = nRed
m_nDefMatGreen = nGreen
m_nDefMatBlue = nBlue
End Sub
Public Sub SetViewAttributes(ByVal nDriver As Integer, ByVal b2Buff As Boolean, ByVal nColorBits As Integer, ByVal nDepthBits As Integer)
m_nDriver = nDriver
m_b2Buff = b2Buff
m_nColorBits = nColorBits
m_nDepthBits = nDepthBits
End Sub
Public Sub SetViewBackground(ByVal nTopRed As Integer, ByVal nTopGreen As Integer, ByVal nTopBlue As Integer,
ByVal nBottomRed As Integer, ByVal nBottomGreen As Integer, ByVal nBottomBlue As Integer)
m_nBackTopRed = nTopRed
m_nBackTopGreen = nTopGreen
m_nBackTopBlue = nTopBlue
m_nBackBotRed = nBottomRed
m_nBackBotGreen = nBottomGreen
m_nBackBotBlue = nBottomBlue
End Sub
'Mettere EgtInit, EgtSetKey e EgtSetFont nell'evento Load del Form, prima di inizializzare la o le Scene
'Mettere EgtExit nell'evento FormClosing
Public Sub Init()
m_nGseContext = EgtInitGeomDB()
EgtSetDefaultMaterial(m_nGseContext, m_nDefMatRed, m_nDefMatGreen, m_nDefMatBlue)
EgtInitScene(m_nGseContext, Handle, m_nDriver, m_b2Buff, m_nColorBits, m_nDepthBits)
EgtSetBackground(m_nGseContext, m_nBackTopRed, m_nBackTopGreen, m_nBackTopBlue,
m_nBackBotRed, m_nBackBotGreen, m_nBackBotBlue)
EgtInitTscExec(m_nGseContext)
End Sub
Public Function GetCtx() As Integer
Return m_nGseContext
End Function
Protected Overrides Sub OnHandleDestroyed(e As EventArgs)
'EgtExit va spostata nell'evento FormClosing del Form
'EgtExit()
End Sub
Protected Overrides Sub OnPaint(e As PaintEventArgs)
MyBase.OnPaint(e)
EgtDraw(m_nGseContext)
End Sub
Protected Overrides Sub OnPaintBackground(pevent As PaintEventArgs)
MyBase.OnPaintBackground(pevent)
End Sub
Protected Overrides Sub OnResize(e As System.EventArgs)
MyBase.OnResize(e)
EgtResize(m_nGseContext, Width, Height)
End Sub
'---- Events ----------
Public Event OnCursorPos(ByVal sender As Object, ByVal sCursorPos As String)
Public Event OnNewProject(ByVal sender As Object)
Public Event OnOpeningProject(ByVal sender As Object)
Public Event OnOpenProject(ByVal sender As Object, ByVal sFile As String)
Public Event OnSavingProject(ByVal sender As Object, ByVal sFile As String)
Public Event OnSavedProject(ByVal sender As Object)
Public Event OnImportingProject(ByVal sender As Object)
Public Event OnImportedProject(ByVal sender As Object, ByVal sFile As String)
Public Event OnExportingProject(ByVal sender As Object, ByVal sFile As String)
Public Event OnExportedProject(ByVal sender As Object)
Public Event OnExecutingScript(ByVal sender As Object, ByVal sFile As String)
Public Event OnExecutedScript(ByVal sender As Object)
'---- Mouse -----------
Protected Overrides Sub OnMouseEnter(e As System.EventArgs)
MyBase.OnMouseEnter(e)
Focus()
End Sub
Protected Overrides Sub OnMouseDown(e As System.Windows.Forms.MouseEventArgs)
If e.Button = Windows.Forms.MouseButtons.Left Then
Dim nSel As Integer
EgtSelect(m_nGseContext, e.Location, 13, 13, nSel)
If nSel = 1 Then
Dim nId = EgtGetFirstSelectedObj(m_nGseContext)
If EgtIsSelectedObj(m_nGseContext, nId) Then
EgtDeselectObj(m_nGseContext, nId)
Else
EgtSelectObj(m_nGseContext, nId)
End If
EgtDraw(m_nGseContext)
ElseIf nSel > 1 Then
Dim MselDlg As New SelectMulti
MselDlg.SetContext(m_nGseContext)
If MselDlg.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
Dim nId = MselDlg.GetId()
If EgtIsSelectedObj(m_nGseContext, nId) Then
EgtDeselectObj(m_nGseContext, nId)
Else
EgtSelectObj(m_nGseContext, nId)
End If
EgtDraw(m_nGseContext)
End If
End If
ElseIf e.Button = Windows.Forms.MouseButtons.Middle Then
If (Control.ModifierKeys And Keys.Shift) = Keys.Shift Then
m_nStatus = ST.ZOOMWIN
Cursor = New Cursor(Me.GetType(), "ZoomWin.cur")
ElseIf (Control.ModifierKeys And Keys.Control) = Keys.Control Then
m_nStatus = ST.ROT
Cursor = New Cursor(Me.GetType(), "Rotate.cur")
Else
m_nStatus = ST.PAN
Cursor = New Cursor(Me.GetType(), "Pan.cur")
End If
m_PrevPoint = e.Location
Else
MyBase.OnMouseDown(e)
End If
Focus()
End Sub
Protected Overrides Sub OnMouseUp(e As System.Windows.Forms.MouseEventArgs)
If e.Button = Windows.Forms.MouseButtons.Middle Then
If m_nStatus = ST.ZOOMWIN Then
EgtResetWinRect(m_nGseContext, False)
EgtZoomWin(m_nGseContext, m_PrevPoint, e.Location, True)
End If
If m_nStatus <> ST.NULL Then
m_nStatus = ST.NULL
Cursor = New Cursor(Me.GetType(), "Select.cur")
End If
Else
MyBase.OnMouseUp(e)
End If
End Sub
Protected Overrides Sub OnMouseMove(e As System.Windows.Forms.MouseEventArgs)
'Visualizzo le coordinate del mouse
ShowCursorPos(e.Location)
'Secondo lo stato...
If e.Button = Windows.Forms.MouseButtons.Middle Then
If m_nStatus = ST.ZOOMWIN Then
Cursor = New Cursor(Me.GetType(), "ZoomWin.cur")
EgtSetWinRect(m_nGseContext, m_PrevPoint, e.Location, True)
'Il punto di riferimento deve rimanere quello originale
ElseIf m_nStatus = ST.ROT Then
Cursor = New Cursor(Me.GetType(), "Rotate.cur")
EgtRotateCamera(m_nGseContext, m_PrevPoint, e.Location, True)
m_PrevPoint = e.Location
ElseIf m_nStatus = ST.PAN Then
Cursor = New Cursor(Me.GetType(), "Pan.cur")
EgtPanCamera(m_nGseContext, m_PrevPoint, e.Location, True)
m_PrevPoint = e.Location
Else
m_nStatus = ST.NULL
Cursor = New Cursor(Me.GetType(), "Select.cur")
End If
Else
MyBase.OnMouseMove(e)
End If
End Sub
Protected Overrides Sub OnMouseWheel(e As System.Windows.Forms.MouseEventArgs)
If Abs(e.Delta) < 30 Then
Return
End If
' calcolo coefficiente
Const WHEEL_DELTA As Double = 120
Dim dCoeff As Double = 1 - 0.1 * Abs(e.Delta) / WHEEL_DELTA
If e.Delta < 0 Then
dCoeff = 1 / dCoeff
End If
' eseguo zoom
EgtZoomOnPoint(m_nGseContext, e.Location, dCoeff, True)
End Sub
Private Sub ShowCursorPos(ByVal WinXY As Point)
'ricavo punto 3d
Dim ptWorld As Point3d
EgtUnProjectPoint(m_nGseContext, WinXY, ptWorld)
'ricavo direzione di vista
Dim nDir As Integer
EgtGetCameraDir(m_nGseContext, nDir)
'costruisco stringa con dati
Dim sCursorPos As New System.Text.StringBuilder
Select Case nDir
Case CT_TOP, CT_BOTTOM
sCursorPos.Append("X=")
sCursorPos.Append(ptWorld.x.ToString("F4", CultureInfo.InvariantCulture))
sCursorPos.Append(" Y=")
sCursorPos.Append(ptWorld.y.ToString("F4", CultureInfo.InvariantCulture))
Case CT_FRONT, CT_BACK
sCursorPos.Append("X=")
sCursorPos.Append(ptWorld.x.ToString("F4", CultureInfo.InvariantCulture))
sCursorPos.Append(" Z=")
sCursorPos.Append(ptWorld.z.ToString("F4", CultureInfo.InvariantCulture))
Case CT_LEFT, CT_RIGHT
sCursorPos.Append("Y=")
sCursorPos.Append(ptWorld.y.ToString("F4", CultureInfo.InvariantCulture))
sCursorPos.Append(" Z=")
sCursorPos.Append(ptWorld.z.ToString("F4", CultureInfo.InvariantCulture))
Case Else
sCursorPos.Append(" ")
End Select
' lancio l'evento per visualizzare la stringa
RaiseEvent OnCursorPos(Me, sCursorPos.ToString)
End Sub
'---- Main Buttons ----
Public Function NewProject() As Boolean
Dim bOk As Boolean = EgtNewFile(m_nGseContext)
EgtZoom(m_nGseContext, ZM_ALL)
If bOk Then
RaiseEvent OnNewProject(Me)
Else
MessageBox.Show("Error on new file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
Return bOk
End Function
Public Function OpenProject()
'Scelta file con dialogo
Dim OpenFileDialog As New OpenFileDialog
OpenFileDialog.Title = "Open"
OpenFileDialog.Filter = "New geometry EgalTech(*.nge)|*.nge" &
"|New font EgalTech(*.nfe)|*.nfe" &
"|All Files (*.*)|*.*"
OpenFileDialog.FilterIndex = 1
If OpenFileDialog.ShowDialog <> Windows.Forms.DialogResult.OK Then
Return True
End If
'Prima del caricamento
RaiseEvent OnOpeningProject(Me)
'Caricamento del progetto
Cursor = Cursors.WaitCursor
Dim bOk As Boolean = EgtOpenFile(m_nGseContext, OpenFileDialog.FileName)
EgtZoom(m_nGseContext, ZM_ALL)
Cursor = Cursors.Default
'Gestione risultato
If bOk Then
RaiseEvent OnOpenProject(Me, OpenFileDialog.FileName)
Else
MessageBox.Show("Error opening file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
Return bOk
End Function
Public Function SaveProject(Optional ByVal sFile As String = "")
'Assegnazione nome file con dialogo
Dim SaveFileDialog As New SaveFileDialog
SaveFileDialog.Title = "Save"
SaveFileDialog.Filter = "New geometry EgalTech(*.nge)|*.nge"
SaveFileDialog.FileName = sFile
If SaveFileDialog.ShowDialog <> Windows.Forms.DialogResult.OK Then
Return True
End If
'Prima del salvataggio
RaiseEvent OnSavingProject(Me, SaveFileDialog.FileName)
'Salvataggio del progetto
Cursor = Cursors.WaitCursor
Dim bOk As Boolean = EgtSaveFile(m_nGseContext, SaveFileDialog.FileName, NGE_CMPTEXT)
Cursor = Cursors.Default
'Gestione risultato
If bOk Then
RaiseEvent OnSavedProject(Me)
Else
MessageBox.Show("Error saving file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
Return bOk
End Function
Public Function ImportProject()
'Scelta file con dialogo
Dim OpenFileDialog As New OpenFileDialog
OpenFileDialog.Title = "Import"
OpenFileDialog.Filter = "Drawing Exchange Fmt(*.dxf)|*.dxf" &
"|Stereolithography (*.stl)|*.stl" &
"|Part program ISO (*.cnc)|*.cnc" &
"|All Files (*.*)|*.*"
OpenFileDialog.FilterIndex = 1
If OpenFileDialog.ShowDialog <> Windows.Forms.DialogResult.OK Then
Return True
End If
'Riconoscimento tipo file
Dim nFileType As Integer = EgtGetFileType(OpenFileDialog.FileName)
If nFileType <> FT_DXF And nFileType <> FT_STL And nFileType <> FT_CNC Then
MessageBox.Show("File type unknown", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return False
End If
'Prima del caricamento
RaiseEvent OnImportingProject(Me)
'Pulizia GeomDB
Cursor = Cursors.WaitCursor
Dim bOk As Boolean = EgtNewFile(m_nGseContext)
'Importazione
If nFileType = FT_DXF Then
bOk = bOk And EgtImportDxf(m_nGseContext, OpenFileDialog.FileName)
ElseIf nFileType = FT_STL Then
bOk = bOk And EgtImportStl(m_nGseContext, OpenFileDialog.FileName)
ElseIf nFileType = FT_CNC Then
bOk = bOk And EgtImportCnc(m_nGseContext, OpenFileDialog.FileName)
End If
EgtZoom(m_nGseContext, ZM_ALL)
Cursor = Cursors.Default
'Gestione risultato
If bOk Then
RaiseEvent OnImportedProject(Me, OpenFileDialog.FileName)
Else
MessageBox.Show("Error importing file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
Return bOk
End Function
Public Function ExportProject(Optional ByVal sFile As String = "")
'Assegnazione nome file con dialogo
Dim SaveFileDialog As New SaveFileDialog
SaveFileDialog.Title = "Export"
SaveFileDialog.Filter = "Drawing Exchange Fmt(*.dxf)|*.dxf" &
"|Stereolithography (*.stl)|*.stl" &
"|All Files (*.*)|*.*"
SaveFileDialog.FileName = sFile
If SaveFileDialog.ShowDialog <> Windows.Forms.DialogResult.OK Then
Return True
End If
'Riconoscimento tipo file
Dim nFileType As Integer = EgtGetFileType(SaveFileDialog.FileName)
If nFileType <> FT_DXF And nFileType <> FT_STL Then
MessageBox.Show("File type unknown", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return False
End If
'Prima dell'esportazione
RaiseEvent OnExportingProject(Me, SaveFileDialog.FileName)
'Esportazione del progetto
Cursor = Cursors.WaitCursor
Dim bOk As Boolean = False
If nFileType = FT_DXF Then
bOk = EgtExportDxf(m_nGseContext, GDB_ID_ROOT, SaveFileDialog.FileName)
ElseIf nFileType = FT_STL Then
bOk = EgtExportStl(m_nGseContext, GDB_ID_ROOT, SaveFileDialog.FileName)
End If
Cursor = Cursors.Default
'Gestione risultato
If bOk Then
RaiseEvent OnExportedProject(Me)
Else
MessageBox.Show("Error exporting file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
Return bOk
End Function
Public Function Exec()
'Scelta file con dialogo
Dim OpenFileDialog As New OpenFileDialog
OpenFileDialog.Title = "Exec Script"
OpenFileDialog.Filter = "Lua commands(*.lua)|*.lua" &
"|Test commands(*.tsc)|*.tsc" &
"|All Files (*.*)|*.*"
If OpenFileDialog.ShowDialog <> Windows.Forms.DialogResult.OK Then
Return True
End If
'Ne verifico il tipo
Dim sExt As String = UCase(Path.GetExtension(OpenFileDialog.FileName))
If (sExt <> ".LUA" And sExt <> ".TSC") Then
MessageBox.Show("Script type unknow", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return False
End If
'Prima dell'esecuzione
RaiseEvent OnExecutingScript(Me, OpenFileDialog.FileName)
'Esecuzione
Cursor = Cursors.WaitCursor
Dim bOk As Boolean = False
If (sExt = ".LUA") Then
bOk = EgtLuaExecFile(OpenFileDialog.FileName)
Else
bOk = EgtTscExecFile(m_nGseContext, OpenFileDialog.FileName)
End If
EgtZoom(m_nGseContext, ZM_ALL)
Cursor = Cursors.Default
'Gestione risultato
If bOk Then
RaiseEvent OnExecutedScript(Me)
ElseIf (sExt = ".LUA") Then
Dim sError As String = String.Empty
EgtLuaGetLastError(sError)
MessageBox.Show(sError, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
MessageBox.Show("Error executing file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
Return bOk
End Function
'---- Zoom Buttons ----
Public Sub ZoomAll()
EgtZoom(m_nGseContext, ZM_ALL)
End Sub
Public Sub ZoomIn()
EgtZoom(m_nGseContext, ZM_IN)
End Sub
Public Sub ZoomOut()
EgtZoom(m_nGseContext, ZM_OUT)
End Sub
'---- Rendering Buttons ----
Public Sub WireFrame()
EgtSetShowMode(m_nGseContext, SM_WIREFRAME)
End Sub
Public Sub HiddenLine()
EgtSetShowMode(m_nGseContext, SM_HIDDENLINE)
End Sub
Public Sub Shading()
EgtSetShowMode(m_nGseContext, SM_SHADING)
End Sub
'---- LookFrom Buttons ----
Public Sub TopView()
EgtSetView(m_nGseContext, CT_TOP)
End Sub
Public Sub IsoView()
EgtSetView(m_nGseContext, CT_ISO_SW)
End Sub
Public Sub FrontView()
EgtSetView(m_nGseContext, CT_FRONT)
End Sub
Public Sub BackView()
EgtSetView(m_nGseContext, CT_BACK)
End Sub
Public Sub LeftView()
EgtSetView(m_nGseContext, CT_LEFT)
End Sub
Public Sub RightView()
EgtSetView(m_nGseContext, CT_RIGHT)
End Sub
End Class