Imports System.Collections.ObjectModel Imports System.IO Imports System.Windows.Threading Imports EgtBEAMWALL.Core Imports EgtUILib Imports EgtWPFLib5 Public Class LeftPanelVM Inherits VMBase #Region "FIELDS & PROPERTIES" Private m_PrintTimer As New DispatcherTimer Private _OPStateList As List(Of OPState) Public Property OPStateList As List(Of OPState) Get Return _OPStateList End Get Set(ByVal value As List(Of OPState)) _OPStateList = value End Set End Property Private _SelOPState As OPState Public Property SelOPState As OPState Get Return _SelOPState End Get Set(ByVal value As OPState) MachManaging.CommandList.Add(ThreadCommand.CreateCommand(CommandTypes.SETOP, value.Id)) _SelOPState = value End Set End Property Friend Sub SetOPState(value As OPState) _SelOPState = value NotifyPropertyChanged(NameOf(SelOPState)) End Sub Private m_bRestart As Boolean = False Public Property bRestart As Boolean Get Return m_bRestart End Get Set(value As Boolean) m_bRestart = value If value Then If Not IsNothing(Map.refPartInRawPartListVM.colPartInRawPart_Redo) Then Map.refPartInRawPartListVM.colPartInRawPart_Redo.ColumnVisibility = Visibility.Visible End If If Not IsNothing(Map.refFeatureInPartInRawPartListVM.colFeatureInPartInRawPart_Redo) Then Map.refFeatureInPartInRawPartListVM.colFeatureInPartInRawPart_Redo.ColumnVisibility = Visibility.Visible End If ' disabilito interfaccia Map.refProjectVM.ManageIsEnabled(False) Else If Not IsNothing(Map.refPartInRawPartListVM.colPartInRawPart_Redo) Then Map.refPartInRawPartListVM.colPartInRawPart_Redo.ColumnVisibility = Visibility.Collapsed End If If Not IsNothing(Map.refFeatureInPartInRawPartListVM.colFeatureInPartInRawPart_Redo) Then Map.refFeatureInPartInRawPartListVM.colFeatureInPartInRawPart_Redo.ColumnVisibility = Visibility.Collapsed End If ' riabilito interfaccia Map.refProjectVM.ManageIsEnabled(True) End If NotifyPropertyChanged(NameOf(FeatureList_Visibility)) End Set End Property Public ReadOnly Property FeatureList_Visibility As Visibility Get Return If(m_bRestart, Visibility.Visible, Visibility.Collapsed) End Get End Property Private m_LeftPanel_IsEnabled As Boolean = True Public ReadOnly Property LeftPanel_IsEnabled As Boolean Get Return m_LeftPanel_IsEnabled End Get End Property ' Definizione comandi Private m_cmdDeleteRawPart As ICommand Private m_cmdMoveUpRawPart As ICommand Private m_cmdMoveDownRawPart As ICommand Private m_cmdProduceAll As ICommand Private m_cmdDoneRawPart As ICommand Private m_cmdDonePart As ICommand Private m_cmdPrintLabel As ICommand #End Region 'FIELDS & PROPERTIES Sub New() ' imposto riferimento su mappa Map.SetRefLeftPanelVM(Me) m_PrintTimer.Interval = TimeSpan.FromMilliseconds(1000) AddHandler m_PrintTimer.Tick, AddressOf PrintTimer_Tick ' carico stati della macchina Select Case NCType Case NCTypes.TPA _OPStateList = New List(Of OPState) From { New OPState("Start", TPA_OPState.Start), New OPState("Stop", TPA_OPState.Stop), New OPState("End", TPA_OPState.End), New OPState("SetPoint", TPA_OPState.SetPoint), New OPState("Pending", TPA_OPState.Pending), New OPState("Unspecified", TPA_OPState.Unspecified) } Case NCTypes.NUM_FLEXIUM _OPStateList = New List(Of OPState) From { New OPState("Auto", NUM_OPState.Auto), New OPState("Single", NUM_OPState.Single), New OPState("Mdi", NUM_OPState.Mdi), New OPState("Manual", NUM_OPState.Manual), New OPState("Home", NUM_OPState.Home) } End Select End Sub #Region "METHODS" Friend Sub UpdateView() NotifyPropertyChanged("ViewPage_Visibility") NotifyPropertyChanged("MachiningPage_Visibility") End Sub Friend Sub SetLeftPanelIsEnabled(bIsEnabled As Boolean) m_LeftPanel_IsEnabled = bIsEnabled NotifyPropertyChanged(NameOf(LeftPanel_IsEnabled)) End Sub Private Sub PrintTimer_Tick() m_PrintTimer.Stop() EgtOutLog("Print label timeout!") End Sub #End Region ' METHODS #Region "COMMANDS" #Region "ProduceAll" ''' ''' Returns a command that do Exec. ''' Public ReadOnly Property ProduceAll_Command As ICommand Get If m_cmdProduceAll Is Nothing Then m_cmdProduceAll = New Command(AddressOf ProduceAll) End If Return m_cmdProduceAll End Get End Property ''' ''' Public Sub ProduceAll() If IsNothing(Map.refSupervisorManagerVM.CurrProd) Then Return If IsNothing(Map.refProjectVM.SupervisorMachGroupPanelVM.MachGroupVMList.Count = 0) Then Return If Not Map.refProjectVM.SupervisorMachGroupPanelVM.MachGroupVMList.Any(Function(x As MyMachGroupVM) x.nProduction_State <= 1) Then Return Dim ToDoList As List(Of MachGroupVM) = Map.refProjectVM.SupervisorMachGroupPanelVM.MachGroupVMList.Where(Function(x As MyMachGroupVM) x.nProduction_State <= 1 AndAlso Not x.bResetWhileCutting AndAlso x.dtStartTime = DateTime.MinValue AndAlso Not x.bToBeProduced).ToList() If Not IsNothing(ToDoList) AndAlso ToDoList.Count > 0 Then MyMachGroupVM.Produce(ToDoList) End If End Sub #End Region ' ProduceAll #Region "DoneRawPart" ''' ''' Returns a command that do Exec. ''' Public ReadOnly Property DoneRawPart_Command As ICommand Get If m_cmdDoneRawPart Is Nothing Then m_cmdDoneRawPart = New Command(AddressOf DoneRawPart) End If Return m_cmdDoneRawPart End Get End Property ''' ''' Execute the Exec. This method is invoked by the ExecCommand. ''' Public Sub DoneRawPart() If IsNothing(Map.refSupervisorManagerVM.CurrProd) Then Return If IsNothing(Map.refProjectVM.SupervisorMachGroupPanelVM.SelectedMachGroup) Then Return Dim SelMachGroup As MyMachGroupVM = Map.refProjectVM.SupervisorMachGroupPanelVM.SelectedMachGroup ' scrivo tutti pezzi completati For PartIndex = 0 To SelMachGroup.PartVMList.Count - 1 Dim Part As PartVM = SelMachGroup.PartVMList(PartIndex) SetDonePart(SelMachGroup, Part) Next ' se avanzamento singolo, sblocco produzione di tutti gli altri If CurrentMachine.Flow = FlowTypes.ONEBYONE Then MyMachGroupVM.UpdateProduceIsEnabledForAll() End If End Sub #End Region ' DoneRawPart #Region "DeleteRawPart" ''' ''' Returns a command that do Exec. ''' Public ReadOnly Property DeleteRawPart_Command As ICommand Get If m_cmdDeleteRawPart Is Nothing Then m_cmdDeleteRawPart = New Command(AddressOf DeleteRawPart) End If Return m_cmdDeleteRawPart End Get End Property ''' ''' Execute the Exec. This method is invoked by the ExecCommand. ''' Public Sub DeleteRawPart() If IsNothing(Map.refSupervisorManagerVM.CurrProd) Then Return If IsNothing(Map.refProjectVM.SupervisorMachGroupPanelVM.SelectedMachGroup) Then Return Dim SelMachGroup As MyMachGroupVM = Map.refProjectVM.SupervisorMachGroupPanelVM.SelectedMachGroup ' se gia' prodotto, o in produzione non lo posso eliminare If SelMachGroup.dtStartTime <> Date.MinValue OrElse SelMachGroup.dtEndTime <> Date.MinValue OrElse SelMachGroup.bReadyForMachining OrElse SelMachGroup.bSentToMachine OrElse SelMachGroup.bResetWhileCutting Then MessageBox.Show("Impossibile cancellare il grezzo") Return End If If DbControllers.m_MachGroupController.RemoveFromSupervisor(Map.refSupervisorManagerVM.CurrProd.nProdId, SelMachGroup.Id) Then ' se rimozione da Db ok, lo rimuovo da lista Map.refProjectVM.SupervisorMachGroupPanelVM.MachGroupVMList.Remove(SelMachGroup) End If End Sub #End Region ' DeleteRawPart #Region "DonePart" ''' ''' Returns a command that do Exec. ''' Public ReadOnly Property DonePart_Command As ICommand Get If m_cmdDonePart Is Nothing Then m_cmdDonePart = New Command(AddressOf DonePart) End If Return m_cmdDonePart End Get End Property ''' ''' Execute the Exec. This method is invoked by the ExecCommand. ''' Public Sub DonePart() If IsNothing(Map.refSupervisorManagerVM.CurrProd) Then Return If IsNothing(Map.refProjectVM.SupervisorMachGroupPanelVM.SelectedMachGroup) Then Return Dim SelMachGroup As MyMachGroupVM = Map.refProjectVM.SupervisorMachGroupPanelVM.SelectedMachGroup If IsNothing(SelMachGroup.SelPart) Then Return ' verifico se pezzi precedenti nella barra siano lavorati For Index = 0 To SelMachGroup.PartVMList.Count - 1 Dim CurrPart As PartVM = SelMachGroup.PartVMList(Index) If CurrPart Is SelMachGroup.SelPart Then Exit For ElseIf CurrPart.nProduction_State < ItemState.Produced Then MessageBox.Show("It is not possible to set as done a part that has before not already produced parts.", "Error", MessageBoxButton.OK, MessageBoxImage.Error) Return End If Next SetDonePart(SelMachGroup, SelMachGroup.SelPart) End Sub Public Sub SetDonePart(MachGroup As MyMachGroupVM, Part As PartVM) If IsNothing(MachGroup) OrElse IsNothing(Part) Then Return ' se ora d'inizio non e' gia' fissata Dim Time As DateTime = DateTime.Now() If Part.dtStartTime = DateTime.MinValue Then DbControllers.m_PartController.UpdateStart(Map.refSupervisorManagerVM.CurrProd.nProdId, MachGroup.Id, Part.nPartId, Time) DbControllers.m_PartController.UpdateStatus(Map.refSupervisorManagerVM.CurrProd.nProdId, MachGroup.Id, Part.nPartId, ItemState.WIP) Part.dtStartTime = Time Part.nProduction_State = ItemState.WIP ' se e' il primo ad essere lavorato If Not MachGroup.PartVMList.Any(Function(x) x IsNot Part AndAlso x.dtStartTime > DateTime.MinValue) Then ' se non e' gia' in coda per essere prodotto If Not (MachGroup.bToBeProduced Or MachGroup.bReadyForMachining Or MachGroup.bSentToMachine) Then ' riposiziono il gruppo come primo dei non lavorati assegnandogli indice corretto ' cerco primo indice di pezzo non in produzione Dim NewIndex As Integer = Map.refProjectVM.SupervisorMachGroupPanelVM.MachGroupVMList.IndexOf(Map.refProjectVM.SupervisorMachGroupPanelVM.MachGroupVMList.FirstOrDefault(Function(x) DirectCast(x, MyMachGroupVM).dtStartTime = DateTime.MinValue AndAlso DirectCast(x, MyMachGroupVM).bToBeProduced = False)) ' sposto MachGroup in lista come ultimo dei pronti da produrre Dim OldIndex As Integer = Map.refProjectVM.SupervisorMachGroupPanelVM.MachGroupVMList.IndexOf(MachGroup) MachGroup.SetProductionIndex(NewIndex + 1) DbControllers.m_MachGroupController.UpdateOrder(Map.refSupervisorManagerVM.CurrProd.nProdId, MachGroup.Id, MachGroup.nProductionIndex) Map.refProjectVM.SupervisorMachGroupPanelVM.MachGroupVMList.Move(OldIndex, NewIndex) End If ' aggiorno anche start MachGroup DbControllers.m_MachGroupController.UpdateStart(Map.refSupervisorManagerVM.CurrProd.nProdId, MachGroup.Id, Time) DbControllers.m_MachGroupController.UpdateStatus(Map.refSupervisorManagerVM.CurrProd.nProdId, MachGroup.Id, ItemState.WIP) MachGroup.dtStartTime = Time MachGroup.nProduction_State = ItemState.WIP MachGroup.NotifyPropertyChanged(NameOf(MachGroup.Production_Background)) ' blocco produzione di tutti gli altri perche' iniziato questo MyMachGroupVM.UpdateProduceIsEnabledForAll() End If End If If Part.dtEndTime = DateTime.MinValue Then DbControllers.m_PartController.UpdateEnd(Map.refSupervisorManagerVM.CurrProd.nProdId, MachGroup.Id, Part.nPartId, Time) DbControllers.m_PartController.UpdateStatus(Map.refSupervisorManagerVM.CurrProd.nProdId, MachGroup.Id, Part.nPartId, ItemState.Produced) Part.dtEndTime = Time Part.nProduction_State = ItemState.Produced If MachGroup.PartVMList.All(Function(x) x.nProduction_State >= ItemState.Produced) Then 'aggiorno anche fine MachGroup DbControllers.m_MachGroupController.UpdateEnd(Map.refSupervisorManagerVM.CurrProd.nProdId, MachGroup.Id, Time) DbControllers.m_MachGroupController.UpdateStatus(Map.refSupervisorManagerVM.CurrProd.nProdId, MachGroup.Id, ItemState.Produced) MachGroup.dtEndTime = Time MachGroup.nProduction_State = ItemState.Produced MachGroup.NotifyPropertyChanged(NameOf(MachGroup.Production_Background)) MachGroup.NotifyPropertyChanged(NameOf(MachGroup.Produce_IsEnabled)) ' se avanzamento singolo, sblocco produzione di tutti gli altri If CurrentMachine.Flow = FlowTypes.ONEBYONE Then MyMachGroupVM.UpdateProduceIsEnabledForAll() End If End If End If Part.NotifyPropertyChanged(NameOf(Part.Production_Background)) End Sub #End Region ' DonePart #Region "MoveUpRawPart" ''' ''' Returns a command that do Exec. ''' Public ReadOnly Property MoveUpRawPart_Command As ICommand Get If m_cmdMoveUpRawPart Is Nothing Then m_cmdMoveUpRawPart = New Command(AddressOf MoveUpRawPart) End If Return m_cmdMoveUpRawPart End Get End Property ''' ''' Execute the Exec. This method is invoked by the ExecCommand. ''' Public Sub MoveUpRawPart() If IsNothing(Map.refSupervisorManagerVM.CurrProd) Then Return If IsNothing(Map.refProjectVM.SupervisorMachGroupPanelVM.SelectedMachGroup) Then Return Dim SelMachGroup As MyMachGroupVM = Map.refProjectVM.SupervisorMachGroupPanelVM.SelectedMachGroup If SelMachGroup.nProduction_State >= ItemState.WIP Or SelMachGroup.bReadyForMachining Then Return Dim nSelMGIndex = Map.refProjectVM.SupervisorMachGroupPanelVM.MachGroupVMList.IndexOf(SelMachGroup) If nSelMGIndex = 0 Then Return Dim PreviousMG As MyMachGroupVM = Map.refProjectVM.SupervisorMachGroupPanelVM.MachGroupVMList(nSelMGIndex - 1) If PreviousMG.nProduction_State >= ItemState.WIP Or PreviousMG.bReadyForMachining Then Return Map.refProjectVM.SupervisorMachGroupPanelVM.MachGroupVMList.Move(nSelMGIndex, nSelMGIndex - 1) End Sub #End Region ' MoveUpRawPart #Region "MoveDownRawPart" ''' ''' Returns a command that do Exec. ''' Public ReadOnly Property MoveDownRawPart_Command As ICommand Get If m_cmdMoveDownRawPart Is Nothing Then m_cmdMoveDownRawPart = New Command(AddressOf MoveDownRawPart) End If Return m_cmdMoveDownRawPart End Get End Property ''' ''' Execute the Exec. This method is invoked by the ExecCommand. ''' Public Sub MoveDownRawPart() If IsNothing(Map.refSupervisorManagerVM.CurrProd) Then Return If IsNothing(Map.refProjectVM.SupervisorMachGroupPanelVM.SelectedMachGroup) Then Return Dim SelMachGroup As MyMachGroupVM = Map.refProjectVM.SupervisorMachGroupPanelVM.SelectedMachGroup If SelMachGroup.nProduction_State >= ItemState.WIP Or SelMachGroup.bReadyForMachining Then Return Dim nSelMGIndex = Map.refProjectVM.SupervisorMachGroupPanelVM.MachGroupVMList.IndexOf(SelMachGroup) If nSelMGIndex = Map.refProjectVM.SupervisorMachGroupPanelVM.MachGroupVMList.Count - 1 Then Return Map.refProjectVM.SupervisorMachGroupPanelVM.MachGroupVMList.Move(nSelMGIndex, nSelMGIndex + 1) End Sub #End Region ' MoveDownRawPart #Region "PrintLabel" ''' ''' Returns a command that do Exec. ''' Public ReadOnly Property PrintLabel_Command As ICommand Get If m_cmdPrintLabel Is Nothing Then m_cmdPrintLabel = New Command(AddressOf PrintLabelComm) End If Return m_cmdPrintLabel End Get End Property ''' ''' Execute the Exec. This method is invoked by the ExecCommand. ''' Public Sub PrintLabelComm() If IsNothing(Map.refSupervisorManagerVM.CurrProd) Then Return If IsNothing(Map.refProjectVM.SupervisorMachGroupPanelVM.SelectedMachGroup) Then Return Dim SelMachGroup As MyMachGroupVM = Map.refProjectVM.SupervisorMachGroupPanelVM.SelectedMachGroup If IsNothing(SelMachGroup.SelPart) Then Return PrintLabel(SelMachGroup, SelMachGroup.SelPart) '' recupero file template 'Dim FileTEMPLATE As String = "" 'GetMainPrivateProfileString(S_PRINTER, K_TEMPLATE, "", FileTEMPLATE) 'Dim FileName As String = DateTime.Now.ToString 'FileName = FileName.Replace("/"c, "_") 'FileName = FileName.Replace(":"c, "_") 'FileName = FileName.Replace(" "c, "&") 'Dim FileINI As String = Map.refMainWindowVM.MainWindowM.sTempDir & "\" & FileName & ".ini" '' creo file dati 'CreateDataFile(FileINI, SelMachGroup.SelPart, SelMachGroup) 'Dim Proc = New Process() 'Proc.StartInfo.FileName = Map.refMainWindowVM.MainWindowM.sZebraPrinterExe 'Proc.StartInfo.Arguments = FileTEMPLATE & " " & FileINI 'Proc.StartInfo.CreateNoWindow = True 'Proc.StartInfo.UseShellExecute = False 'If Proc.Start() Then ' Dim y = 1 'End If End Sub Public Sub PrintLabel(SelMachGroup As MyMachGroupVM, SelPart As PartVM) If Not Map.refMachCommandMessagePanelVM.bPrinterLink Then Return If IsNothing(SelMachGroup) Then Return If IsNothing(SelPart) Then Return ' recupero file template Dim TemplateFilePath As String = "" GetMainPrivateProfileString(S_PRINTER, K_TEMPLATE, "", TemplateFilePath) Dim FileName As String = DateTime.Now.ToString FileName = FileName.Replace("/"c, "_") FileName = FileName.Replace(":"c, "_") FileName = FileName.Replace(" "c, "&") Dim FileINI As String = Map.refMainWindowVM.MainWindowM.sTempDir & "\" & FileName & ".ini" ' creo file dati CreateDataFile(FileINI, SelPart, SelMachGroup) Dim Proc = New Process() Proc.StartInfo.FileName = Map.refMainWindowVM.MainWindowM.sZebraPrinterExe Proc.StartInfo.Arguments = TemplateFilePath & " " & FileINI Proc.StartInfo.CreateNoWindow = True Proc.StartInfo.UseShellExecute = False AddHandler Proc.Exited, AddressOf Proc_Exited For Inde = 0 To 100 If Not m_PrintTimer.IsEnabled Then Exit For Threading.Thread.Sleep(100) Next If Not m_PrintTimer.IsEnabled AndAlso Proc.Start() Then m_PrintTimer.Start() End If End Sub Private Sub Proc_Exited() m_PrintTimer.Stop() End Sub Private Sub CreateDataFile(sFilePath As String, SelPart As PartVM, SelMachGroup As MyMachGroupVM) Dim sFileText As New List(Of String) ' inserisco il capitolo del [Main] sFileText.Add("[Main]") sFileText.Add("Var1=$PRODID$," & Map.refSupervisorManagerVM.CurrProd.sProdId) sFileText.Add("Var2=$MACHGROUPID$," & SelMachGroup.Id) sFileText.Add("Var3=$PARTID$," & SelPart.nPDN) sFileText.Add("Var4=$MATERIAL$," & SelPart.sMATERIAL) 'sFileText.Add("Var5=$Thickness$," & SelPart.Thickness) 'sFileText.Add("Var6=$Storage$," & SelPart.WarehousePosition) Try File.WriteAllLines(sFilePath, sFileText) Catch ex As Exception EgtOutLog("Prepare print file: " & ex.ToString) End Try End Sub #End Region ' PrintLabel #End Region ' COMMANDS End Class Public Class OPState Private m_Name As String Public ReadOnly Property Name As String Get Return m_Name End Get End Property Private m_Id As Integer Public ReadOnly Property Id As Integer Get Return m_Id End Get End Property Sub New(Name As String, Id As Integer) m_Name = Name m_Id = Id End Sub End Class