Files
egtbeamwall/EgtBEAMWALL.Supervisor/LeftPanel/LeftPanelVM.vb
T
2021-10-22 18:16:21 +02:00

444 lines
18 KiB
VB.net

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
Public ReadOnly Property AxisList As ObservableCollection(Of Axis)
Get
Return Map.refMachCommandMessagePanelVM.AxisList
End Get
End Property
Public ReadOnly Property sOPState As String
Get
Return Map.refMachCommandMessagePanelVM.sOPState
End Get
End Property
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
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
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
' 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(5000)
AddHandler m_PrintTimer.Tick, AddressOf PrintTimer_Tick
End Sub
#Region "METHODS"
Friend Sub UpdateView()
NotifyPropertyChanged("ViewPage_Visibility")
NotifyPropertyChanged("MachiningPage_Visibility")
End Sub
Private Sub PrintTimer_Tick()
m_PrintTimer.Stop()
EgtOutLog("Print label timeout!")
End Sub
#End Region ' METHODS
#Region "COMMANDS"
#Region "ProduceAll"
''' <summary>
''' Returns a command that do Exec.
''' </summary>
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
''' <summary>
''' </summary>
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"
''' <summary>
''' Returns a command that do Exec.
''' </summary>
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
''' <summary>
''' Execute the Exec. This method is invoked by the ExecCommand.
''' </summary>
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"
''' <summary>
''' Returns a command that do Exec.
''' </summary>
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
''' <summary>
''' Execute the Exec. This method is invoked by the ExecCommand.
''' </summary>
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"
''' <summary>
''' Returns a command that do Exec.
''' </summary>
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
''' <summary>
''' Execute the Exec. This method is invoked by the ExecCommand.
''' </summary>
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)
' se avanzamento singolo, sblocco produzione di tutti gli altri
If CurrentMachine.Flow = FlowTypes.ONEBYONE Then
MyMachGroupVM.UpdateProduceIsEnabledForAll()
End If
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))
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))
End If
End If
Part.NotifyPropertyChanged(NameOf(Part.Production_Background))
End Sub
#End Region ' DonePart
#Region "MoveUpRawPart"
''' <summary>
''' Returns a command that do Exec.
''' </summary>
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
''' <summary>
''' Execute the Exec. This method is invoked by the ExecCommand.
''' </summary>
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"
''' <summary>
''' Returns a command that do Exec.
''' </summary>
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
''' <summary>
''' Execute the Exec. This method is invoked by the ExecCommand.
''' </summary>
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"
''' <summary>
''' Returns a command that do Exec.
''' </summary>
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
''' <summary>
''' Execute the Exec. This method is invoked by the ExecCommand.
''' </summary>
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