Imports System.IO Imports EgtUILib Namespace EgtCAM5 Public Class SimulationExpanderViewModel Inherits ViewModelBase #Region "FIELDS & PROPERTIES" 'EGT PROPERTIES ' Stato di visualizzazione della macchina Private m_nMachLook As Integer = MCH_LOOK.ALL ' Utensile corrente Private m_sCurrTool As String = String.Empty ' 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 'GRAPHICAL PROPERTIES Private m_IsExpanded As Boolean Public Property IsExpanded As Boolean Get Return m_IsExpanded End Get Set(value As Boolean) If value <> m_IsExpanded Then If value Then EgtDeselectAll() Application.Msn.NotifyColleagues(Application.REMOVEMARKFROMLASTOPERATION) Application.Msn.NotifyColleagues(Application.GETDISTANCE_ISCHECKED, False) InitializeSimulation() Else CloseSimulation() End If m_IsExpanded = value Application.Msn.NotifyColleagues(Application.SIMULATIONEXPANDER_GET_ISEXPANDED, value) OnPropertyChanged("IsExpanded") End If End Set End Property Private m_L1Name As String Public Property L1Name As String Get Return m_L1Name End Get Set(value As String) If value <> m_L1Name Then m_L1Name = value OnPropertyChanged("L1Name") End If End Set End Property Private m_L1Value As String Public Property L1Value As String Get Return m_L1Value End Get Set(value As String) If value <> m_L1Value Then m_L1Value = value OnPropertyChanged("L1Value") End If End Set End Property Private m_L2Name As String Public Property L2Name As String Get Return m_L2Name End Get Set(value As String) If value <> m_L2Name Then m_L2Name = value OnPropertyChanged("L2Name") End If End Set End Property Private m_L2Value As String Public Property L2Value As String Get Return m_L2Value End Get Set(value As String) If value <> m_L2Value Then m_L2Value = value OnPropertyChanged("L2Value") End If End Set End Property Private m_L3Name As String Public Property L3Name As String Get Return m_L3Name End Get Set(value As String) If value <> m_L3Name Then m_L3Name = value OnPropertyChanged("L3Name") End If End Set End Property Private m_L3Value As String Public Property L3Value As String Get Return m_L3Value End Get Set(value As String) If value <> m_L3Value Then m_L3Value = value OnPropertyChanged("L3Value") End If End Set End Property Private m_R1Name As String Public Property R1Name As String Get Return m_R1Name End Get Set(value As String) If value <> m_R1Name Then m_R1Name = value OnPropertyChanged("R1Name") End If End Set End Property Private m_R1Value As String Public Property R1Value As String Get Return m_R1Value End Get Set(value As String) If value <> m_R1Value Then m_R1Value = value OnPropertyChanged("R1Value") End If End Set End Property Private m_R2Name As String Public Property R2Name As String Get Return m_R2Name End Get Set(value As String) If value <> m_R2Name Then m_R2Name = value OnPropertyChanged("R2Name") End If End Set End Property Private m_R2Value As String Public Property R2Value As String Get Return m_R2Value End Get Set(value As String) If value <> m_R2Value Then m_R2Value = value OnPropertyChanged("R2Value") End If End Set End Property Private m_GCode As String Public Property GCode As String Get Return m_GCode End Get Set(value As String) If value <> m_GCode Then m_GCode = value OnPropertyChanged("GCode") End If End Set End Property Private m_FValue As String Public Property FValue As String Get Return m_FValue End Get Set(value As String) If value <> m_FValue Then m_FValue = value OnPropertyChanged("FValue") End If End Set End Property Private m_TName As String Public Property TName As String Get Return m_TName End Get Set(value As String) If value <> m_TName Then m_TName = value OnPropertyChanged("TName") End If End Set End Property Private m_SValue As String Public Property SValue As String Get Return m_SValue End Get Set(value As String) If value <> m_SValue Then m_SValue = value OnPropertyChanged("SValue") End If End Set End Property Private m_PlayPauseImage As String Public ReadOnly Property PlayPauseImage As String Get Select Case m_bPlay Case False Return "/Resources/OptionPanel/MachiningOptionPanel/SimulationExpander/Pause.png" Case True Return "/Resources/OptionPanel/MachiningOptionPanel/SimulationExpander/Play.png" Case Else Return "" End Select End Get End Property Private m_SliderValue As Double Public Property SliderValue As Double Get Return m_SliderValue End Get Set(value As Double) If value <> m_SliderValue Then m_SliderValue = value EgtSimSetStep(value * m_SliderX) OnPropertyChanged("SliderValue") End If End Set End Property ' Definizione comandi Private m_cmdStep As ICommand Private m_cmdPlayPause As ICommand Private m_cmdStop As ICommand Private m_cmdGenerate As ICommand #Region "Messages" Public ReadOnly Property SimulationMsg As String Get Return EgtMsg(MSG_SIMULATION + 7) End Get End Property Public ReadOnly Property GenerateMsg As String Get Return EgtMsg(MSG_SIMULATION + 30) End Get End Property #End Region #Region "ToolTip" Public ReadOnly Property OneStepToolTip As String Get Return EgtMsg(MSG_SIMULATION + 8) End Get End Property Public ReadOnly Property PlayPauseToolTip As String Get Return EgtMsg(MSG_SIMULATION + 9) End Get End Property Public ReadOnly Property StopHomeToolTip As String Get Return EgtMsg(MSG_SIMULATION + 10) End Get End Property #End Region #End Region #Region "CONSTRUCTOR" Sub New() Application.Msn.Register(Application.CLOSEAPPLICATION, Sub() If IsExpanded Then ResetSimulation() End If End Sub) Application.Msn.Register(Application.SIMULATIONEXPANDER_SET_ISEXPANDED, Sub(bValue As Boolean) IsExpanded = bValue End Sub) End Sub #End Region #Region "COMMANDS" #Region "StepCommand" ''' ''' Returns a command that create a new tool. ''' Public ReadOnly Property StepCommand As ICommand Get If m_cmdStep Is Nothing Then m_cmdStep = New RelayCommand(AddressOf StepCmd) End If Return m_cmdStep End Get End Property ''' ''' Creata the new tool. This method is invoked by the NewCommand. ''' Public Sub StepCmd(ByVal param As Object) 'm_CurrProjPage.ClearMessage() ' Se stato stop, devo avviare simulazione If m_nStatus = SIM_ST.ST_STOP Then m_nStatus = SIM_ST.ST_STEP ExecSim() ' Aggiorno bottone m_bPlay = True OnPropertyChanged("PlayPauseImage") 'SetPlayPauseBtnToPlay() ' Alrimenti imposto solo il nuovo stato Else m_nStatus = SIM_ST.ST_STEP ' Aggiornamenti per bottone Play/Pause m_bPlay = False OnPropertyChanged("PlayPauseImage") 'SetPlayPauseBtnToPause() End If End Sub #End Region ' StepCommand #Region "PlayPauseCommand" ''' ''' Returns a command that create a new tool. ''' Public ReadOnly Property PlayPauseCommand As ICommand Get If m_cmdPlayPause Is Nothing Then m_cmdPlayPause = New RelayCommand(AddressOf PlayPause) End If Return m_cmdPlayPause End Get End Property ''' ''' Creata the new tool. This method is invoked by the NewCommand. ''' Public Sub PlayPause(ByVal param As Object) 'm_CurrProjPage.ClearMessage() If m_bPlay Then ' Aggiorno bottone m_bPlay = False OnPropertyChanged("PlayPauseImage") ' Eseguo If m_nStatus = SIM_ST.ST_STOP Then ' Lancio simulazione m_nStatus = SIM_ST.ST_PLAY ExecSim() ' Aggiorno bottone m_bPlay = True OnPropertyChanged("PlayPauseImage") ElseIf m_nStatus = SIM_ST.ST_PAUSE Then m_nStatus = SIM_ST.ST_PLAY End If Else ' Aggiorno bottone m_bPlay = True OnPropertyChanged("PlayPauseImage") ' 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 #End Region ' PlayPauseCommand #Region "StopCommand" ''' ''' Returns a command that create a new tool. ''' Public ReadOnly Property StopCommand As ICommand Get If m_cmdStop Is Nothing Then m_cmdStop = New RelayCommand(AddressOf StopCmd) End If Return m_cmdStop End Get End Property ''' ''' Creata the new tool. This method is invoked by the NewCommand. ''' Public Sub StopCmd(ByVal param As Object) 'm_CurrProjPage.ClearMessage() ' Se stato già stop, porto in home If m_nStatus = SIM_ST.ST_STOP Then ' Vado in home EgtSimHome() ' Imposto prima fase EgtSetCurrPhase(1, True) ' Aggiorno visualizzazione EgtDraw() ' Aggiorno dati CNC ShowCncData() End If ' Imposto il nuovo stato m_nStatus = SIM_ST.ST_STOP End Sub #End Region ' StopCommand #Region "GenerateCommand" ''' ''' Returns a command that create a new tool. ''' Public ReadOnly Property GenerateCommand As ICommand Get If m_cmdGenerate Is Nothing Then m_cmdGenerate = New RelayCommand(AddressOf Generate) End If Return m_cmdGenerate End Get End Property ''' ''' Creata the new tool. This method is invoked by the NewCommand. ''' Public Sub Generate(ByVal param As Object) Dim sCurrFilePath As String = String.Empty EgtGetCurrFilePath(sCurrFilePath) If String.IsNullOrEmpty(sCurrFilePath) Then MessageBox.Show(EgtMsg(MSG_SIMULATION + 31), EgtMsg(MSG_SIMULATION + 5), MessageBoxButton.OK, MessageBoxImage.Stop) End If Dim sInfo As String = "EgtCAM5 - " & sCurrFilePath If Not EgtGenerate(Path.ChangeExtension(sCurrFilePath, ".cnc"), sInfo) Then MessageBox.Show(EgtMsg(MSG_SIMULATION + 6), EgtMsg(MSG_SIMULATION + 5), MessageBoxButton.OK, MessageBoxImage.Stop) Else Application.Msn.NotifyColleagues(Application.NOTIFYSTATUSOUTPUT, EgtMsg(MSG_SIMULATION + 32)) End If End Sub #End Region ' GenerateCommand #End Region #Region "METHODS" Private Sub InitializeSimulation() ' Imposto prima fase EgtSetCurrPhase(1) ' Costringo ad aggiornare UI UpdateUI() ' Imposto stato corrente m_nStatus = SIM_ST.ST_STOP m_bPlay = True m_SliderX = GetPrivateProfileDouble(S_SIMUL, K_SLIDERX, 1) Dim SliderVal As Double = GetPrivateProfileDouble(S_SIMUL, K_SLIDERVAL, 10) SliderValue = SliderVal ' Porto la testa in home EgtSimStart() EgtSimHome() LoadCurrTools() EgtDraw() ShowCncData() End Sub Private Sub CloseSimulation() ' Mi assicuro di terminare la simulazione ResetSimulation() EgtDraw() End Sub Private Sub ResetSimulation() ' Termino la simulazione m_nStatus = SIM_ST.ST_STOP EgtSimStop() ' Salvo valore dello slider Dim sVal As String = DoubleToString(SliderValue, 1) WritePrivateProfileString(S_SIMUL, K_SLIDERVAL, sVal) ' Torno alla prima fase EgtSetCurrPhase(1, True) ' Ripristino visibilità standard 'EgtSetMachineLook(MCH_LOOK.TAB) End Sub Private Sub ExecSim() EgtSimStart() LoadCurrTools() EgtSimSetStep(SliderValue * 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 m_bPlay = True OnPropertyChanged("PlayPauseImage") End If ' Se movimento non riuscito Else m_nStatus = SIM_ST.ST_STOP ' Aggiornamenti per bottone Play/Pause m_bPlay = True OnPropertyChanged("PlayPauseImage") Select Case nMove Case MCH_SIM.END_ 'MsgBox(EgtMsg(MSG_SIMULATION + 1)) 'Simulazione completata Case MCH_SIM.OUTSTROKE Dim sInfo As String = String.Empty EgtGetOutstrokeInfo(sInfo) MessageBox.Show(EgtMsg(MSG_SIMULATION + 2) & " " & sInfo, EgtMsg(MSG_SIMULATION + 5), MessageBoxButton.OK, MessageBoxImage.Stop) 'Extracorsa ... Case MCH_SIM.DIR_ERR MessageBox.Show(EgtMsg(MSG_SIMULATION + 3), EgtMsg(MSG_SIMULATION + 5), MessageBoxButton.OK, MessageBoxImage.Stop) 'Direzione utensile irraggiungibile Case Else MessageBox.Show(EgtMsg(MSG_SIMULATION + 4), EgtMsg(MSG_SIMULATION + 5), MessageBoxButton.OK, MessageBoxImage.Stop) 'Errore End Select End If ' Aggiorno stato visualizzazione macchina (dipende anche da utensile) 'UpdateMachView() ' 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 UpdateUI() End While End Sub Private Sub ShowCncData() ' Assi Dim nInd As Integer = 0 ShowAxisNameVal(nInd, L1Name, L1Value) ShowAxisNameVal(nInd, L2Name, L2Value) ShowAxisNameVal(nInd, L3Name, L3Value) ShowAxisNameVal(nInd, R1Name, R1Value) ShowAxisNameVal(nInd, R2Name, R2Value) ' Tipo di movimento e feed ShowMoveTypeFeed() ' Nome utensile e speed ShowToolNameSpeed() End Sub Private Function ShowAxisNameVal(ByRef 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 If sInfo <> "**" Then sName = sInfo sVal = If(nInd < 3, LenToString(dVal, -3), DoubleToString(dVal, -3)) nInd += 1 Return True Else nInd += 1 Return ShowAxisNameVal(nInd, sName, sVal) End If Else sName = String.Empty sVal = String.Empty nInd += 1 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 GCode = "G" & nG.ToString() FValue = "F" & LenToString(dFeed, 0) Else GCode = "G" & nG.ToString() FValue = "" End If Return True Else GCode = "" FValue = "" 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 TName = sTool SValue = "S" & DoubleToString(dSpeed, 0) Return True Else TName = "" SValue = "" Return False End If End Function Private Function LoadCurrTools() As Boolean '' Imposto la lama corrente 'Dim sSaw As String = m_MainWindow.m_CurrentMachine.sCurrSaw 'If Not EgtLoadTool("H1", 1, sSaw) Then ' Return False 'End If '' Imposto eventuale secondo utensile montato 'If m_MainWindow.m_CurrentMachine.MountedToolConfig = CurrentMachine.MountedToolConfigs.SAWANDAUXTOOL Then ' Dim sTool As String = m_MainWindow.m_CurrentMachine.sCurrDrill ' If String.IsNullOrEmpty(sTool) Then sTool = m_MainWindow.m_CurrentMachine.sCurrMill ' If Not String.IsNullOrEmpty(sTool) AndAlso Not EgtLoadTool("H1", 2, sTool) Then ' Return False ' End If 'End If Return True End Function Private Sub UpdateMachView() ' Se cambiato utensile, aggiorno stato visualizzazione macchina Dim sTool As String = String.Empty Dim dSpeed As Double = 0 If EgtSimGetToolInfo(sTool, dSpeed) Then If sTool <> m_sCurrTool Then m_sCurrTool = sTool EgtSetMachineLook(m_nMachLook) End If End If End Sub #End Region End Class End Namespace