Imports System.Collections.ObjectModel Imports EgtUILib Imports EgtWPFLib5 Public Class SimulTabVM Inherits VMBase #Region "FIELDS & PROPERTIES" ' Stato e comando correnti Public Enum SIM_ST As Integer ST_STOP = 1 ST_PLAY = 2 ST_STEP = 3 ST_PAUSE = 4 End Enum ' Stato di visualizzazione della macchina Private m_nMachLook As MCH_LOOK = MCH_LOOK.ALL ' Utensile corrente Private m_sCurrTool As String = String.Empty Private m_nStatus As SIM_ST = SIM_ST.ST_STOP Private Property SimulationStatus As SIM_ST Get Return m_nStatus End Get Set(value As SIM_ST) m_nStatus = value If OmagOFFICE.refMainWindowVM.MainWindowM.nUserLevel >= 10 AndAlso (value = SIM_ST.ST_PAUSE OrElse value = SIM_ST.ST_STOP) Then For Index = 0 To m_MachineAxisList.Count - 1 m_MachineAxisList(Index).IsReadOnlyAxesValue = False Next Else For Index = 0 To m_MachineAxisList.Count - 1 m_MachineAxisList(Index).IsReadOnlyAxesValue = True Next End If End Set End Property ' Stato bottone Play Private m_bShowPlay As Boolean = True ' Coefficiente per valore Slider Private m_SliderX As Double = 1 ' lista degli assi Private m_MachineAxisList As New ObservableCollection(Of MachineAxis) Public ReadOnly Property MachineAxisList As ObservableCollection(Of MachineAxis) Get Return m_MachineAxisList End Get 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 NotifyPropertyChanged("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 NotifyPropertyChanged("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 NotifyPropertyChanged("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 NotifyPropertyChanged("SValue") End If End Set End Property Private m_PlayPauseImage As String Public ReadOnly Property PlayPauseImage As String Get If m_bShowPlay Then Return "/Resources/SimulTab/Play.png" Else Return "/Resources/SimulTab/Pause.png" End If 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) NotifyPropertyChanged("SliderValue") End If End Set End Property ' Definizione comandi Private m_cmdToolMode As ICommand Private m_cmdHeadMode As ICommand Private m_cmdMachMode As ICommand Private m_cmdStep As ICommand Private m_cmdPlayPause As ICommand Private m_cmdStop As ICommand #Region "Messages" Public ReadOnly Property MachViewMsg As String Get Return "Vista macchina" End Get End Property #End Region ' Messages #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 ' ToolTip #End Region ' FIELDS & PROPERTIES #Region "CONSTRUCTOR" Sub New() ' Creo riferimento a questa classe in OmagOFFICEMap OmagOFFICEMap.SetRefSimulTabVM(Me) End Sub #End Region ' CONSTRUCTOR #Region "METHODS" Friend Sub Refresh(PrevMeasureUnit As MeasureUnitOpt) End Sub Friend Sub InitSimulation() Dim bOk As Boolean = True ' Verifico l'attrezzaggio degli utensili utilizzati Dim sMissingTools As String = String.Empty Dim bSetup As Boolean = CamAuto.VerifySetup(sMissingTools) ' Costringo ad aggiornare UI UpdateUI() ' Aggiorno flag per lavaggio EstCalc.UpdateWashingFlag() ' Se non c'è ordine delle lavorazioni, ne faccio uno automatico If Not EstCalc.GetOrderMachiningFlag() Then bOk = SortAllMachinings() If bOk Then EstCalc.SetOrderMachiningFlag() End If Dim bModif As Boolean = TestAllMachiningsForStrict() If bModif Then ' Ridotte alcune lavorazioni per evitare interferenze OmagOFFICEMap.refStatusBarVM.SetOutputMessage(EgtMsg(90321), 3, MSG_TYPE.WARNING) End If End If ' Costringo ad aggiornare UI UpdateUI() ' Disabilito impostazione modificato EgtDisableModified() ' Aggiorno le lavorazioni bOk = UpdateAllMachiningsToolpaths() And bOk ' Se errore in setup, lo segnalo If Not bSetup Then ' Mancano gli utensili : ... OmagOFFICEMap.refStatusBarVM.SetOutputMessage(EgtMsg(90322) & " " & sMissingTools, MSG_TYPE.ERROR_) ' Se errore in generazione, segnalo l'errore ElseIf Not bOk Then ' Errore nella generazione del programma CN OmagOFFICEMap.refStatusBarVM.SetOutputMessage(EgtMsg(90314), MSG_TYPE.ERROR_) End If ' Nascondo eventuali pezzi in parcheggio HideParkedParts() ' Nascondo eventuale contorno da foto EstPhoto.ShowContour(False) ' Imposto prima fase EgtSetCurrPhase(1) ShowAllCurrPhaseMachinings() ' Costringo ad aggiornare UI UpdateUI() 'Cambio la vista della scena EgtSetView(VT.ISO_SE, False) m_nMachLook = MCH_LOOK.ALL EgtSetMachineLook(m_nMachLook) EgtZoom(ZM.ALL) ' Imposto stato corrente SimulationStatus = SIM_ST.ST_STOP m_bShowPlay = True m_SliderX = GetMainPrivateProfileDouble(S_SIMUL, K_SLIDERX, 1) Dim SliderVal As Double = GetMainPrivateProfileDouble(S_SIMUL, K_SLIDERVAL, 10) SliderValue = SliderVal ' Porto la testa in home EgtSimStart() EgtSimHome() LoadCurrTools() ShowCncData() End Sub Friend Sub ResetSimulation() ' Termino la simulazione SimulationStatus = SIM_ST.ST_STOP EgtSimStop() ' Salvo valore dello slider Dim sVal As String = DoubleToString(SliderValue, 1) WriteMainPrivateProfileString(S_SIMUL, K_SLIDERVAL, sVal) ' Torno alla prima fase EgtSetCurrPhase(1, True) End Sub Private Sub ExecSim() EgtSimStart() EgtSimSetStep(SliderValue * m_SliderX) Dim nShowDataCounter As Integer = 0 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 SimulationStatus = SIM_ST.ST_PAUSE OmagOFFICEMap.refStatusBarVM.SetOutputMessage(EgtMsg(MSG_SIMULATIONPAGEUC + 11)) ' Pausa ' Aggiornamenti per bottone Play/Pause m_bShowPlay = True NotifyPropertyChanged("PlayPauseImage") End If ' Se movimento non riuscito Else SimulationStatus = SIM_ST.ST_STOP ' Aggiornamenti per bottone Play/Pause m_bShowPlay = True NotifyPropertyChanged("PlayPauseImage") Select Case nMove Case MCH_SIM.END_ OmagOFFICEMap.refStatusBarVM.SetOutputMessage(EgtMsg(MSG_SIMULATIONPAGEUC + 1), 5) 'Simulazione completata Case MCH_SIM.OUTSTROKE Dim sInfo As String = String.Empty EgtGetOutstrokeInfo(sInfo) MessageBox.Show(EgtMsg(MSG_SIMULATIONPAGEUC + 2) & " " & sInfo, EgtMsg(MSG_SIMULATIONPAGEUC + 5), MessageBoxButton.OK, MessageBoxImage.Stop) 'Extracorsa ... Case MCH_SIM.DIR_ERR MessageBox.Show(EgtMsg(MSG_SIMULATIONPAGEUC + 3), EgtMsg(MSG_SIMULATIONPAGEUC + 5), MessageBoxButton.OK, MessageBoxImage.Stop) 'Direzione utensile irraggiungibile Case Else MessageBox.Show(EgtMsg(MSG_SIMULATIONPAGEUC + 4), EgtMsg(MSG_SIMULATIONPAGEUC + 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 nEgtIndex As Integer = 0 Dim nAxisIndex As Integer = 0 Dim sName As String = String.Empty Dim sToken As String = String.Empty Dim bLinear As Boolean = True Dim dVal As Double = 0 While EgtSimGetAxisInfoPos(nEgtIndex, sName, sToken, bLinear, dVal) If sToken <> "**" Then IsValidAxisIndex(nAxisIndex) m_MachineAxisList(nAxisIndex).Name = sName m_MachineAxisList(nAxisIndex).Token = sToken m_MachineAxisList(nAxisIndex).Linear = bLinear m_MachineAxisList(nAxisIndex).Value = If(bLinear, LenToString(dVal, -3), DoubleToString(dVal, -3)) nAxisIndex += 1 End If nEgtIndex += 1 End While For ClearIndex = m_MachineAxisList.Count - 1 To nAxisIndex Step -1 m_MachineAxisList.RemoveAt(ClearIndex) Next ' Tipo di movimento e feed ShowMoveTypeFeed() ' Nome utensile e speed ShowToolNameSpeed() End Sub Private Sub IsValidAxisIndex(nIndex As Integer) For Index = m_MachineAxisList.Count To nIndex m_MachineAxisList.Add(New MachineAxis) Next End Sub 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 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 Private Function LoadCurrTools() As Boolean ' Se macchina con cambio utensile non devo fare alcunché If CurrentMachine.MountedToolConfig = CurrentMachine.MountedToolConfigs.TOOLCHANGER Then Return True End If ' Imposto la lama corrente Dim sSaw As String = CurrentMachine.sCurrSaw If Not EgtLoadTool("H1", 1, sSaw) Then Return False End If ' Imposto eventuale secondo utensile montato If CurrentMachine.MountedToolConfig = CurrentMachine.MountedToolConfigs.SAWANDAUXTOOL Then Dim sTool As String = CurrentMachine.sCurrDrill If String.IsNullOrEmpty(sTool) Then sTool = CurrentMachine.sCurrMill If Not String.IsNullOrEmpty(sTool) AndAlso Not EgtLoadTool("H1", 2, sTool) Then Return False End If End If Return True End Function Friend Sub ExitSimulation() OmagOFFICEMap.refStatusBarVM.ClearOutputMessage() ' Mi assicuro di terminare la simulazione ResetSimulation() ' Ripristino visibilità standard m_nMachLook = MCH_LOOK.TAB EgtSetMachineLook(m_nMachLook) ' Nascondo tutte le lavorazioni CamAuto.HideAllMachinings() ' Abilito impostazione modificato EgtEnableModified() ' Cambio la vista della scena EgtSetView(VT.TOP, False) EgtZoom(ZM.ALL) End Sub #End Region #Region "COMMANDS" #Region "ToolModeCommand" ''' ''' Returns a command that create a new tool. ''' Public ReadOnly Property ToolModeCommand As ICommand Get If m_cmdToolMode Is Nothing Then m_cmdToolMode = New Command(AddressOf ToolMode) End If Return m_cmdToolMode End Get End Property ''' ''' Creata the new tool. This method is invoked by the NewCommand. ''' Public Sub ToolMode(ByVal param As Object) ' aggiorno lo stato If m_nMachLook <> MCH_LOOK.TAB_TOOL Then m_nMachLook = MCH_LOOK.TAB_TOOL End If ' aggiorno lo stato della macchina e la sua visualizzazione EgtSetMachineLook(m_nMachLook) EgtDraw() End Sub #End Region ' ToolModeCommand #Region "HeadModeCommand" ''' ''' Returns a command that create a new tool. ''' Public ReadOnly Property HeadModeCommand As ICommand Get If m_cmdHeadMode Is Nothing Then m_cmdHeadMode = New Command(AddressOf HeadMode) End If Return m_cmdHeadMode End Get End Property ''' ''' Creata the new tool. This method is invoked by the NewCommand. ''' Public Sub HeadMode(ByVal param As Object) ' aggiorno lo stato If m_nMachLook <> MCH_LOOK.TAB_HEAD Then m_nMachLook = MCH_LOOK.TAB_HEAD End If ' aggiorno lo stato della macchina e la sua visualizzazione EgtSetMachineLook(m_nMachLook) EgtDraw() End Sub #End Region ' HeadModeCommand #Region "MachModeCommand" ''' ''' Returns a command that create a new tool. ''' Public ReadOnly Property MachModeCommand As ICommand Get If m_cmdMachMode Is Nothing Then m_cmdMachMode = New Command(AddressOf MachMode) End If Return m_cmdMachMode End Get End Property ''' ''' Creata the new tool. This method is invoked by the NewCommand. ''' Public Sub MachMode(ByVal param As Object) ' aggiorno lo stato If m_nMachLook <> MCH_LOOK.ALL Then m_nMachLook = MCH_LOOK.ALL End If ' aggiorno lo stato della macchina e la sua visualizzazione EgtSetMachineLook(m_nMachLook) EgtDraw() End Sub #End Region ' MachModeCommand #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 Command(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) OmagOFFICEMap.refStatusBarVM.ClearOutputMessage() ' Se stato stop, devo avviare simulazione If m_nStatus = SIM_ST.ST_STOP Then SimulationStatus = SIM_ST.ST_STEP ' Aggiorno bottone m_bShowPlay = False NotifyPropertyChanged("PlayPauseImage") ExecSim() ' Aggiorno bottone m_bShowPlay = True NotifyPropertyChanged("PlayPauseImage") ' Alrimenti imposto solo il nuovo stato Else SimulationStatus = SIM_ST.ST_STEP ' Aggiornamenti per bottone Play/Pause m_bShowPlay = False NotifyPropertyChanged("PlayPauseImage") 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 Command(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) OmagOFFICEMap.refStatusBarVM.ClearOutputMessage() If m_bShowPlay Then ' Aggiorno bottone m_bShowPlay = False NotifyPropertyChanged("PlayPauseImage") ' Eseguo If m_nStatus = SIM_ST.ST_STOP Then ' Lancio simulazione SimulationStatus = SIM_ST.ST_PLAY ExecSim() ' Aggiorno bottone m_bShowPlay = True NotifyPropertyChanged("PlayPauseImage") ElseIf m_nStatus = SIM_ST.ST_PAUSE Then SimulationStatus = SIM_ST.ST_PLAY End If Else ' Aggiorno bottone m_bShowPlay = True NotifyPropertyChanged("PlayPauseImage") ' Se play o step, imposto stato pausa If m_nStatus = SIM_ST.ST_PLAY Or m_nStatus = SIM_ST.ST_STEP Then SimulationStatus = SIM_ST.ST_PAUSE OmagOFFICEMap.refStatusBarVM.SetOutputMessage(EgtMsg(MSG_SIMULATIONPAGEUC + 11)) ' Pausa 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 Command(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) OmagOFFICEMap.refStatusBarVM.ClearOutputMessage() ' Se stato già stop, porto in home If m_nStatus = SIM_ST.ST_STOP Then ' Mi riporto all'inizio EgtSimStart() EgtDraw() ' Aggiorno dati CNC ShowCncData() Else OmagOFFICEMap.refStatusBarVM.SetOutputMessage(EgtMsg(MSG_SIMULATIONPAGEUC + 12)) ' Simulazione interrotta End If ' Aggiorno bottone m_bShowPlay = True NotifyPropertyChanged("PlayPauseImage") ' Imposto il nuovo stato SimulationStatus = SIM_ST.ST_STOP End Sub #End Region ' StopCommand #End Region End Class