Imports System.Collections.ObjectModel Imports System.IO Imports EgtUILib Imports EgtWPFLib5 Public Class SimulationExpanderVM Inherits SimulationVM #Region "FIELDS & PROPERTIES" 'GRAPHICAL PROPERTIES Private m_Simulation_IsExpanded As Boolean Public Overridable Property Simulation_IsExpanded As Boolean Get Return m_Simulation_IsExpanded End Get Set(value As Boolean) If value <> m_Simulation_IsExpanded Then Map.refMachiningOptionPanelVM.Set_IsExpanded(MachiningOptionPanelVM.MachiningOptionPanelExpander.SIMULATION, value) End If End Set End Property Private m_Simulation_IsEnabled As Boolean = False Public Property Simulation_IsEnabled As Boolean Get Return m_Simulation_IsEnabled End Get Set(value As Boolean) m_Simulation_IsEnabled = value NotifyPropertyChanged("Simulation_IsEnabled") End Set End Property Public Overrides Property SliderValue As Double Get Return m_SliderValue End Get Set(value As Double) value = Math.Max(1, Math.Min(value, 100)) If Math.Abs(value - m_SliderValue) > 1 Then m_SliderValue = value NotifyPropertyChanged("SliderValue") End If EgtSimSetStep(value * GetSliderX()) End Set End Property Private m_bStopOnNextCollision As Boolean = True #End Region ' FIELDS & PROPERTIES #Region "CONSTRUCTOR" Sub New() SetRefSimulationExpanderVM(Me) SimulationMsg_Const = MSG_SIMULATION SetPlayImagePath("/Resources/OptionPanel/MachiningOptionPanel/SimulationExpander/Play.png") SetPauseImagePath("/Resources/OptionPanel/MachiningOptionPanel/SimulationExpander/Pause.png") SetPauseMsg(EgtMsg(5311)) ' PAUSA SetStopMsg(EgtMsg(5312)) ' STOP SetHomeMsg(EgtMsg(5314)) ' HOME End Sub #End Region ' CONSTRUCTOR #Region "COMMANDS" #Region "GenerateCommand" ''' ''' Creata the new tool. This method is invoked by the NewCommand. ''' Public Sub Generate(ByVal param As Object) ' Verifico se richiesta anche visualizzazione del CN Dim bNcView As Boolean = ((Keyboard.Modifiers And ModifierKeys.Shift) = ModifierKeys.Shift) ' Recupero la fase corrente Dim nPhase As Integer = EgtGetCurrPhase() ' Aggiorno le lavorazioni If Not UpdateAllMachinings() Then EgtSetCurrPhase(If(nPhase = 0, 1, nPhase), True) Return End If ' Recupero nome file CN (dopo aver verificato salvataggio progetto) Dim sCncFile As String = "" Dim sInfo As String = "" If Not GetCncFileName(True, sCncFile, sInfo) Then Return ' Eseguo If Not EgtGenerate(sCncFile, sInfo) Then Dim nErr = EgtGetLastMachMgrErrorId() If nErr = 1000 Then MessageBox.Show(EgtMsg(5333), EgtMsg(5320), MessageBoxButton.OK, MessageBoxImage.Information) ElseIf nErr <> 0 Then Dim sErr As String = EgtGetLastMachMgrErrorString() MessageBox.Show(sErr, EgtMsg(5305), MessageBoxButton.OK, MessageBoxImage.Exclamation) Else MessageBox.Show(EgtMsg(5306), EgtMsg(5305), MessageBoxButton.OK, MessageBoxImage.Error) End If bNcView = False Else Map.refStatusBarVM.NotifyStatusOutput(EgtMsg(5332) & " (" & sCncFile & ")") End If ' Torno alla fase originale (o alla prima se non definita) EgtSetCurrPhase(If(nPhase = 0, 1, nPhase), True) ' Se richiesta anche la visualizzazione del CN corrente, procedo If bNcView Then ' Se esiste ne lancio l'editing If File.Exists(sCncFile) Then Process.Start("Notepad.exe", sCncFile) ' altrimenti lo segnalo Else Map.refStatusBarVM.NotifyStatusOutput("Edit failed, missing part program file" & " (" & sCncFile & ")") End If End If End Sub #End Region ' GenerateCommand #End Region ' COMMANDS #Region "METHODS" Friend Function InitSimulation() As Boolean EgtDeselectAll() Map.refInstrumentPanelVM.GetDistIsChecked = False ' Disabilito la selezione dei Percorsi di Lavorazione Map.refProjectVM.SceneSelType = SceneSelTypeOpt.NULL ' Disabilito la selezione di tutti i tipi di geometria Map.refProjectVM.SceneSelMode = SceneSelModeOpt.NULL Map.refTopCommandBarVM.SaveIsEnabled = False SelData.ClearIdSub() If IniFile.m_bShowOnlyTable Then EgtShowOnlyTable(False) EgtZoom(ZM.ALL, False) End If InitializeSimulation() EgtDraw() m_Simulation_IsExpanded = True Map.refOperationParametersExpanderVM.OperParamsViewIsEnabled = False Map.refOperationsListExpanderVM.SimulationExpander_Get_IsExpanded(True) NotifyPropertyChanged("Simulation_IsExpanded") Map.refMachiningOptionPanelVM.OnPropertyChanged("GenerateIsEnabled") ' disattivo database utensili, lavorazioni e setup se vado in simulazione Map.refMachinePanelVM.ToolMachSetUpIsEnabled(False, False, False) ' disattivo MTable e SpecialPanel se vado in simulazione Map.refDoorPanelVM.MTableIsEnabled(False) Map.refSpecialPanelVM.SpecialPanelIsEnabled(False) Return True End Function Friend Function ExitSimulation() As Boolean If bSimExecuting Then Return False ResetSimulation() If IniFile.m_bShowOnlyTable Then EgtShowOnlyTable(True) EgtZoom(ZM.ALL) End If Map.refTopCommandBarVM.SaveIsEnabled = True m_Simulation_IsExpanded = False Map.refOperationParametersExpanderVM.OperParamsViewIsEnabled = True Map.refOperationsListExpanderVM.SimulationExpander_Get_IsExpanded(False) NotifyPropertyChanged("Simulation_IsExpanded") Map.refMachiningOptionPanelVM.OnPropertyChanged("GenerateIsEnabled") ' attivo database utensili, lavorazioni e setup se esco dalla simulazione Map.refMachinePanelVM.ToolMachSetUpIsEnabled(True, True, True) ' disattivo MTable e SpecialPanel se esco dalla simulazione Map.refDoorPanelVM.MTableIsEnabled(True) Map.refSpecialPanelVM.SpecialPanelIsEnabled(True) Return True End Function Friend Function UpdateAllMachinings() As Boolean ' Se disabilitato, esco If GetPrivateProfileInt( S_MACH, K_AUTOUPDATEALLMACHININGS, 1) = 0 Then Return True ' Eseguo ricalcolo Dim bModified As Boolean = EgtGetModified() Dim sErr As String = String.Empty Dim bOk As Boolean = EgtApplyAllMachinings(False, False, sErr) ' In caso di errori, li segnalo If Not bOk Then If Not String.IsNullOrEmpty(sErr) Then MessageBox.Show(sErr, EgtMsg(MSG_SIMULATION + 5), MessageBoxButton.OK, MessageBoxImage.Exclamation) Else MessageBox.Show(EgtMsg(MSG_SIMULATION + 6), EgtMsg(MSG_SIMULATION + 5), MessageBoxButton.OK, MessageBoxImage.Error) End If End If If Not bModified Then EgtResetModified() Return bOk End Function Private Sub InitializeSimulation() ' Recupero la fase corrente Dim nPhase As Integer = EgtGetCurrPhase() ' Aggiorno le lavorazioni If Not UpdateAllMachinings() Then EgtSetCurrPhase(If(nPhase = 0, 1, nPhase), True) ' Inizio simulazione Dim bOk As Boolean = EgtSimInit() AndAlso EgtSimStart() ' Costringo ad aggiornare UI UpdateUI() ' Messaggio in caso di errore If Not bOk Then If EgtGetLastMachMgrErrorId() <> 0 Then Dim sErr As String = EgtGetLastMachMgrErrorString() MessageBox.Show(sErr, EgtMsg(MSG_SIMULATION + 5), MessageBoxButton.OK, MessageBoxImage.Exclamation) '.... - ERRORE Else MessageBox.Show(EgtMsg(MSG_MESSAGEBOX + 10), EgtMsg(MSG_SIMULATION + 5), MessageBoxButton.OK, MessageBoxImage.Error) 'Errore sconosciuto - ERRORE End If End If ' Imposto stato corrente SetSimulationStatus(MCH_SIM_ST.UI_STOP) SetShowPlay(True) SetSliderX(GetPrivateProfileDouble(S_SIMUL, K_SLIDERX, 1)) Dim SliderVal As Double = GetPrivateProfileDouble(S_SIMUL, K_SLIDERVAL, 10) SliderValue = SliderVal ' Gestione check VMill If IsKeyEnabledVirtualMilling() And EgtUILib.GetPrivateProfileInt(S_VMILL, K_VM_ENABLE, 0, m_sCurrMachIniFilePath) <> 0 Then VMill_Visibility = Visibility.Visible VMill_IsEnabled = True Dim bVal As Boolean SetVMillActive(EgtGetInfo(EgtGetCurrMachGroup(), KEY_MCHGRP_VM, bVal) And bVal) Else VMill_Visibility = Visibility.Collapsed VMill_IsEnabled = False ' Disabilito Vmill, ma inibisco dichiarazione progetto modificato EgtDisableModified() VMillActive = False EgtEnableModified() End If ' Aggiorno visualizzazione dati macchina ShowCncData() StatusMsg = EgtMsg(MSG_SIMULATION + 14) ' HOME End Sub Public Overrides Sub ExecSim() SetSimExecuting(True) IniFile.m_bSimulExecuting = True Map.refTopCommandBarVM.DrawIsEnabled = False Map.refOperationsListExpanderVM.OpersListViewIsEnabled = False Map.refOperationParametersExpanderVM.OperParamsViewIsEnabled = False Map.refMachGroupPanelVM.SetMachGroupState(False) ' Map.refMachGroupPanelVM.MachGroupPanelIsEnabled = False Map.refEstimationsExpanderVM.NotifyPropertyChanged("Estimation_IsEnabled") m_bStopOnNextCollision = True If Not EgtSimStart(False) Then If EgtGetLastMachMgrErrorId() <> 0 Then Dim sErr As String = EgtGetLastMachMgrErrorString() MessageBox.Show(sErr, EgtMsg(MSG_SIMULATION + 5), MessageBoxButton.OK, MessageBoxImage.Exclamation) '.... - ERRORE Else MessageBox.Show(EgtMsg(MSG_MESSAGEBOX + 10), EgtMsg(MSG_SIMULATION + 5), MessageBoxButton.OK, MessageBoxImage.Error) 'Errore sconosciuto - ERRORE End If End If Dim nShowDataCounter As Integer = 0 While GetStatus() <> MCH_SIM_ST.UI_STOP ' Se simulazione in svolgimento If GetStatus() = MCH_SIM_ST.UI_PLAY Or GetStatus() = MCH_SIM_ST.UI_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 GetStatus() = MCH_SIM_ST.UI_STEP And nMove = MCH_SIM.END_STEP Then ' Imposto stato Pausa SetSimulationStatus(MCH_SIM_ST.UI_PAUSE) StatusMsg = EgtMsg(MSG_SIMULATION + 11) ' PAUSA ' Aggiornamenti per bottone Play/Pause SetShowPlay(True) NotifyPropertyChanged("PlayPauseImage") End If ' Se movimento con collisione ElseIf nMove = MCH_SIM.COLLISION Then If m_bStopOnNextCollision Then ' Imposto stato Pausa SetSimulationStatus(MCH_SIM_ST.UI_PAUSE) StatusMsg = EgtMsg(MSG_SIMULATION + 11) ' PAUSA ' Aggiornamenti per bottone Play/Pause SetShowPlay(True) NotifyPropertyChanged("PlayPauseImage") ' Messaggio MessageBox.Show(EgtMsg(MSG_SIMULATION + 19), EgtMsg(MSG_SIMULATION + 15), MessageBoxButton.OK, MessageBoxImage.Warning) Else m_bStopOnNextCollision = True End If ' Altrimenti movimento non riuscito Else SetSimulationStatus(MCH_SIM_ST.UI_STOP) ' Aggiornamenti per bottone Play/Pause SetShowPlay(True) NotifyPropertyChanged("PlayPauseImage") ' Abilito check VMill VMill_IsEnabled = True Select Case nMove Case MCH_SIM.END_ StatusMsg = EgtMsg(MSG_SIMULATION + 1) ' END Case MCH_SIM.STOP_ ' Simulazione fermata dall'utente (non necessita messaggio) 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 If EgtGetLastMachMgrErrorId() <> 0 Then Dim sErr As String = EgtGetLastMachMgrErrorString() MessageBox.Show(sErr, EgtMsg(MSG_SIMULATION + 5), MessageBoxButton.OK, MessageBoxImage.Exclamation) '.... - ERRORE Else MessageBox.Show(EgtMsg(MSG_SIMULATION + 4), EgtMsg(MSG_SIMULATION + 5), MessageBoxButton.OK, MessageBoxImage.Error) 'Errore - ERRORE End If End Select End If ' Aggiorno stato visualizzazione macchina (dipende anche da utensile) UpdateMachView() ' Aggiorno visualizzazione EgtDraw() ' Aggiorno dati CNC If nShowDataCounter = 5 Or GetSimulationStatus() = MCH_SIM_ST.UI_PAUSE Or GetSimulationStatus() = MCH_SIM_ST.UI_STOP Then ShowCncData() nShowDataCounter = 0 ElseIf nShowDataCounter > 5 Then nShowDataCounter = 0 Else nShowDataCounter += 1 End If Else ' Per evitare di ciclare rapidissimamente e consumare inutilmente CPU System.Threading.Thread.Sleep(4) End If ' Costringo ad aggiornare UI UpdateUI() End While SetSimExecuting(False) IniFile.m_bSimulExecuting = False Map.refTopCommandBarVM.DrawIsEnabled = True Map.refOperationsListExpanderVM.OpersListViewIsEnabled = True Map.refOperationParametersExpanderVM.OperParamsViewIsEnabled = True Map.refMachGroupPanelVM.SetMachGroupState(True) ' Map.refMachGroupPanelVM.MachGroupPanelIsEnabled = True Map.refEstimationsExpanderVM.NotifyPropertyChanged("Estimation_IsEnabled") If IniFile.m_bSimulEndExitApp Then refMainWindowVM.CloseApplication( Nothing) End If End Sub Public Overrides Sub OnPostSetSimulationStatus(value As MCH_SIM_ST) If IniFile.m_nUserLevel >= 5 AndAlso (value = MCH_SIM_ST.UI_PAUSE OrElse value = MCH_SIM_ST.UI_STOP) Then For Index = 0 To MachineAxisList.Count - 1 MachineAxisList(Index).IsReadOnlyAxesValue = False Next Else For Index = 0 To MachineAxisList.Count - 1 MachineAxisList(Index).IsReadOnlyAxesValue = True Next End If EgtSimSetUiStatus(GetStatus()) End Sub Public Overrides Sub VMillActiveInfoTitle(VMillActive As Boolean) If VMillActive Then EgtSetInfo(EgtGetCurrMachGroup(), KEY_MCHGRP_VM, VMillActive) Else EgtRemoveInfo(EgtGetCurrMachGroup(), KEY_MCHGRP_VM) End If Map.refProjectVM.EmitTitle() End Sub Private Function GetCncFileName(bAskSave As Boolean, ByRef sCncFile As String, ByRef sInfo As String) As Boolean ' Recupero e verifico la path del progetto corrente Dim sCurrFilePath As String = String.Empty EgtGetCurrFilePath(sCurrFilePath) If bAskSave Then If String.IsNullOrEmpty(sCurrFilePath) OrElse EgtGetModified() Then ' Il progetto deve essere salvato prima di poter essere generato. Vuoi farlo ? If MessageBox.Show(EgtMsg(MSG_SIMULATION + 31), EgtMsg(MSG_SIMULATION + 15), MessageBoxButton.YesNo, MessageBoxImage.Warning) = MessageBoxResult.No Then ' Abbandono Return False Else ' Lancio salvataggio Map.refProjectVM.SaveProject() ' Se non salvato, abbandono If EgtGetModified() Then Return False ' Aggiorno path EgtGetCurrFilePath(sCurrFilePath) End If End If End If sCncFile = "" #If PLATFORM = "x64" Then sInfo = "EgtCAM5 64bit ver." & Map.refMainWindowVM.Version() & " - " & sCurrFilePath #Else sInfo = "EgtCAM5 ver." & Map.refMainWindowVM.Version() & " - " & sCurrFilePath #End If If EgtGetInfo(EgtGetCurrMachGroup(), KEY_MCHGRP_NCNAME, sCncFile) AndAlso Not String.IsNullOrWhiteSpace(sCncFile) Then sCncFile = Path.GetDirectoryName(sCurrFilePath) & "\" & sCncFile Else sCncFile = Path.ChangeExtension(sCurrFilePath, Nothing) If IniFile.m_bMachiningGroup Then Dim sMGrpName As String = String.Empty If EgtGetMachGroupName(EgtGetCurrMachGroup(), sMGrpName) Then sCncFile &= "_" & sMGrpName & ".cnc" sInfo &= "-" & sMGrpName End If Else sCncFile &= ".cnc" End If End If Return True End Function Friend Sub SimulationExpander_Update_CncData( nFlag As Integer) If Simulation_IsExpanded Then ShowCncData() ' Se fermo per Collisione rilevata da script Lua If nFlag = 11 Then ' Imposto stato Pausa SetSimulationStatus(MCH_SIM_ST.UI_PAUSE) StatusMsg = EgtMsg(MSG_SIMULATION + 11) ' PAUSA ' Aggiornamenti per bottone Play/Pause SetShowPlay(True) NotifyPropertyChanged("PlayPauseImage") ' Dichiaro di non arrestarsi alla successiva notifica di collisione m_bStopOnNextCollision = False End If End If End Sub #End Region ' METHODS End Class