Files
OmagCUT/SimulationPageUC.xaml.vb
T
Dario Sassi 3eb8929578 OmagCUT :
- aggiunta gestione mm/inch in interfaccia utente (da fare verso macchina e in generazione CN).
2016-01-19 18:01:10 +00:00

292 lines
11 KiB
VB.net

Imports EgtUILib
Public Class SimulationPageUC
' Riferimenti a pagine
Private m_MainWindow As MainWindow = Application.Current.MainWindow
Private m_CurrProjPage As CurrentProjectPageUC
' Stato di visualizzazione della macchina
Private m_nMachLook As Integer = MCH_LOOK.ALL
' Stato e comando correnti
Public Enum SIM_ST As Integer
ST_STOP = 1
ST_PLAY = 2
ST_STEP = 3
ST_PAUSE = 4
End Enum
Private m_nStatus As Integer = SIM_ST.ST_STOP
' Stato bottone Play
Private m_bPlay As Boolean = True
' Coefficiente per valore Slider
Private m_SliderX As Double = 1
Friend Sub ResetSimulation()
' Termino la simulazione
m_nStatus = SIM_ST.ST_STOP
EgtSimStop()
' Salvo valore dello slider
Dim sVal As String = DoubleToString(SpeedSlider.Value, 1)
WritePrivateProfileString(S_SIMUL, K_SLIDERVAL, sVal, m_MainWindow.GetIniFile())
' Ripristino visibilità standard
ShowParkedParts()
EgtSetMachineLook(MCH_LOOK.TAB)
End Sub
Private Sub SimulationPage_Initialized(sender As Object, e As EventArgs)
PlayPauseImage.Source = New System.Windows.Media.Imaging.BitmapImage(New Uri("/Resources/Play.png", UriKind.Relative))
End Sub
Private Sub SimulationPage_Loaded(sender As Object, e As RoutedEventArgs)
m_CurrProjPage = m_MainWindow.m_CurrentProjectPageUC
Dim bOk As Boolean = True
' Se non c'è ordine delle lavorazioni, ne faccio uno automatico
If Not m_CurrProjPage.GetOrderMachiningFlag() Then
bOk = SortAllMachinings()
If bOk Then
m_CurrProjPage.SetOrderMachiningFlag()
End If
End If
' Disabilito impostazione modificato
EgtDisableModified()
' Aggiorno le lavorazioni
bOk = UpdateAllMachiningsToolpaths() And bOk
' Se errore in generazione, segnalo l'errore ed esco
If Not bOk Then
m_CurrProjPage.SetErrorMessage(EgtMsg(90314)) 'Errore nella generazione del programma CN
End If
' Salvo il progetto con le lavorazioni
Dim sMchPath As String = m_MainWindow.GetTempDir() & "\" & "MachProj.nge"
m_CurrProjPage.SaveFile(sMchPath)
' Nascondo eventuali pezzi in parcheggio
HideParkedParts()
' Visualizzo opportunamente la macchina
m_nMachLook = MCH_LOOK.ALL
EgtSetMachineLook(m_nMachLook)
' Imposto vista 3d isometrica di tutto
EgtSetView(VT.ISO_SE, False)
EgtZoom(ZM.ALL)
' Imposto stato corrente
m_nStatus = SIM_ST.ST_STOP
m_bPlay = True
m_SliderX = GetPrivateProfileDouble(S_SIMUL, K_SLIDERX, 1, m_MainWindow.GetIniFile())
Dim SliderVal As Double = GetPrivateProfileDouble(S_SIMUL, K_SLIDERVAL, 10, m_MainWindow.GetIniFile())
SpeedSlider.Value = SliderVal
' Porto la testa in home
EgtSimStart()
EgtSimHome()
EgtDraw()
ShowCncData()
End Sub
Private Sub StepBtn_Click(sender As Object, e As RoutedEventArgs) Handles StepBtn.Click
' Se stato stop, devo avviare simulazione
If m_nStatus = SIM_ST.ST_STOP Then
m_nStatus = SIM_ST.ST_STEP
ExecSim()
' Alrimenti imposto solo il nuovo stato
Else
m_nStatus = SIM_ST.ST_STEP
End If
' Aggiornamenti per bottone Play/Pause
m_bPlay = False
PlayPauseImage.Source = New System.Windows.Media.Imaging.BitmapImage(New Uri("/Resources/Pause.png", UriKind.Relative))
End Sub
Private Sub PlayPauseBtn_Click(sender As Object, e As RoutedEventArgs) Handles PlayPauseBtn.Click
If m_bPlay Then
' Aggiorno bottone
SetPlayPauseBtnToPause()
' Eseguo
If m_nStatus = SIM_ST.ST_STOP Then
' Lancio simulazione
m_nStatus = SIM_ST.ST_PLAY
ExecSim()
' Aggiorno bottone
SetPlayPauseBtnToPlay()
ElseIf m_nStatus = SIM_ST.ST_PAUSE Then
m_nStatus = SIM_ST.ST_PLAY
End If
Else
' Aggiorno bottone
SetPlayPauseBtnToPlay()
' Se play o step, imposto stato pausa
If m_nStatus = SIM_ST.ST_PLAY Or m_nStatus = SIM_ST.ST_STEP Then
m_nStatus = SIM_ST.ST_PAUSE
End If
End If
End Sub
Private Sub SetPlayPauseBtnToPlay()
m_bPlay = True
PlayPauseImage.Source = New System.Windows.Media.Imaging.BitmapImage(New Uri("/Resources/Play.png", UriKind.Relative))
End Sub
Private Sub SetPlayPauseBtnToPause()
m_bPlay = False
PlayPauseImage.Source = New System.Windows.Media.Imaging.BitmapImage(New Uri("/Resources/Pause.png", UriKind.Relative))
End Sub
Private Sub StopBtn_Click(sender As Object, e As RoutedEventArgs) Handles StopBtn.Click
' Se stato già stop, porto in home
If m_nStatus = SIM_ST.ST_STOP Then
' Vado in home
EgtSimHome()
' Aggiorno visualizzazione
EgtDraw()
' Aggiorno dati CNC
ShowCncData()
End If
' Imposto il nuovo stato
m_nStatus = SIM_ST.ST_STOP
End Sub
Private Sub SpeedSlider_ValueChanged(sender As Object, e As RoutedPropertyChangedEventArgs(Of Double))
EgtSimSetStep(SpeedSlider.Value * m_SliderX)
End Sub
Private Sub ExecSim()
EgtSimStart()
EgtSimSetStep(SpeedSlider.Value * m_SliderX)
While m_nStatus <> SIM_ST.ST_STOP
' Se simulazione in svolgimento
If m_nStatus = SIM_ST.ST_PLAY Or m_nStatus = SIM_ST.ST_STEP Then
' Eseguo movimento
Dim nMove As Integer
Dim bMove As Boolean = EgtSimMove(nMove)
' Se arrivato a fine step e sono in step
If bMove Then
If m_nStatus = SIM_ST.ST_STEP And nMove = MCH_SIM.END_STEP Then
' Imposto stato Pausa
m_nStatus = SIM_ST.ST_PAUSE
' Aggiornamenti per bottone Play/Pause
SetPlayPauseBtnToPlay()
End If
' Se movimento non riuscito
Else
m_nStatus = SIM_ST.ST_STOP
' Aggiornamenti per bottone Play/Pause
SetPlayPauseBtnToPlay()
Select Case nMove
Case MCH_SIM.END_
m_CurrProjPage.SetInfoMessage(EgtMsg(MSG_SIMULATIONPAGEUC + 1)) 'Simulazione completata
Case MCH_SIM.OUTSTROKE
Dim sInfo As String = String.Empty
EgtGetOutstrokeInfo(sInfo)
m_CurrProjPage.SetErrorMessage(EgtMsg(MSG_SIMULATIONPAGEUC + 2) & " " & sInfo) 'Extracorsa ...
Case MCH_SIM.DIR_ERR
m_CurrProjPage.SetErrorMessage(EgtMsg(MSG_SIMULATIONPAGEUC + 3)) 'Direzione utensile irraggiungibile
Case Else
m_CurrProjPage.SetErrorMessage(EgtMsg(MSG_SIMULATIONPAGEUC + 4)) 'Errore
End Select
End If
' Aggiorno visualizzazione
EgtDraw()
' Aggiorno dati CNC
ShowCncData()
Else
' Per evitare di ciclare rapidissimamente e consumare inutilmente CPU
System.Threading.Thread.Sleep(1)
End If
' Costringo ad aggiornare UI
Dim nDummy As Integer
Application.Current.Dispatcher.Invoke(Windows.Threading.DispatcherPriority.Background, _
New Action(Function() nDummy = 0))
End While
End Sub
Private Sub ShowCncData()
' Assi
ShowAxisNameVal(0, L1NameTxBl.Text, L1ValueTxBl.Text)
ShowAxisNameVal(1, L2NameTxBl.Text, L2ValueTxBl.Text)
ShowAxisNameVal(2, L3NameTxBl.Text, L3ValueTxBl.Text)
ShowAxisNameVal(3, R1NameTxBl.Text, R1ValueTxBl.Text)
ShowAxisNameVal(4, R2NameTxBl.Text, R2ValueTxBl.Text)
' Tipo di movimento e feed
ShowMoveTypeFeed()
' Nome utensile e speed
ShowToolNameSpeed()
End Sub
Private Function ShowAxisNameVal(ByVal nInd As Integer, ByRef sName As String, ByRef sVal As String) As Boolean
Dim sInfo As String = String.Empty
Dim dVal As Double = 0
If EgtSimGetAxisInfoPos(nInd, sInfo, dVal) Then
sName = sInfo
sVal = If(nInd < 3, LenToString(dVal, -3), DoubleToString(dVal, -3))
Return True
Else
sName = String.Empty
sVal = String.Empty
Return False
End If
End Function
Private Function ShowMoveTypeFeed() As Boolean
Dim nG As Integer = 0
Dim dFeed As Double = 0
If EgtSimGetMoveInfo(nG, dFeed) Then
If nG <> 0 Then
GCodeTxBl.Text = "G" & nG.ToString()
FValueTxBl.Text = "F" & LenToString(dFeed, 0)
Else
GCodeTxBl.Text = "G" & nG.ToString()
FValueTxBl.Text = ""
End If
Return True
Else
GCodeTxBl.Text = ""
FValueTxBl.Text = ""
Return False
End If
End Function
Private Function ShowToolNameSpeed() As Boolean
Dim sTool As String = String.Empty
Dim dSpeed As Double = 0
If EgtSimGetToolInfo(sTool, dSpeed) Then
TNameTxBl.Text = sTool
SValueTxBl.Text = "S" & DoubleToString(dSpeed, 0)
Return True
Else
TNameTxBl.Text = ""
SValueTxBl.Text = ""
Return False
End If
End Function
Private Sub MachViewModeBtn_Click(sender As Object, e As RoutedEventArgs) Handles MachViewModeBtn.Click
' aggiorno lo stato
Select Case m_nMachLook
Case MCH_LOOK.ALL
m_nMachLook = MCH_LOOK.TAB_HEAD
Case MCH_LOOK.TAB_HEAD
m_nMachLook = MCH_LOOK.TAB_TOOL
Case Else
m_nMachLook = MCH_LOOK.ALL
End Select
' aggiorno lo stato della macchina e la sua visualizzazione
EgtSetMachineLook(m_nMachLook)
EgtDraw()
End Sub
Private Sub ExitBtnUC_Click(sender As Object, e As RoutedEventArgs)
' Mi assicuro di terminare la simulazione
ResetSimulation()
' Cancello eventuali messaggi
m_CurrProjPage.ClearMessage()
' Imposto vista 2D
EgtSetView(VT.TOP, False)
EgtZoom(ZM.ALL)
' Abilito impostazione modificato
EgtEnableModified()
' Esco dalla pagina
m_MainWindow.m_CurrentProjectPageUC.CurrentProjectPageGrid.Children.Remove(Me)
m_CurrProjPage.CurrProjGrid.Visibility = Windows.Visibility.Visible
m_MainWindow.m_CurrentProjectPageUC.CurrentProjectPageGrid.Children.Add(m_MainWindow.m_CadCutPageUC)
m_MainWindow.m_ActivePage = MainWindow.Pages.CadCut
End Sub
End Class