Imports System.Collections.ObjectModel
Imports System.IO
Imports EgtUILib
Imports EgtWPFLib5

Public Class BeamMachGroupM
   Inherits MyMachGroupM

   Protected m_dStartCut As Double
   Public Property dStartCut As Double
      Get
         Return m_dStartCut
      End Get
      Set(value As Double)
         m_dStartCut = value
      End Set
   End Property

#Region "CONSTRUCTORS"

   Protected Sub New()
   End Sub

   Public Shared Function CreateNewBeamMachGroup() As MachGroupM
      Return New MachGroupM
   End Function

   Public Shared Function CreateBeamMachGroup(nId As Integer, sName As String, sMachine As String) As MachGroupM
      Dim NewMachGroupM As New BeamMachGroupM
      NewMachGroupM.m_Id = nId
      NewMachGroupM.m_Name = sName
      NewMachGroupM.m_Machine = sMachine
      ' NewMachGroupM.SetMachineType(MachineType.BEAM)
      EgtGetInfo(NewMachGroupM.m_Id, MGR_RPT_BARLEN, NewMachGroupM.m_dL)
      EgtGetInfo(NewMachGroupM.m_Id, MGR_RPT_BARHEIGHT, NewMachGroupM.m_dH)
      EgtGetInfo(NewMachGroupM.m_Id, MGR_RPT_BARWIDTH, NewMachGroupM.m_dW)
      NewMachGroupM.m_dTotMat = NewMachGroupM.m_dL
      NewMachGroupM.m_PartMList = LoadParts(NewMachGroupM)
      For Each Part As BeamM In NewMachGroupM.m_PartMList
         Part.UpdateOffset()
         NewMachGroupM.m_dMatForPart += Part.dL
      Next
      ReadMachGroupData(NewMachGroupM)
      Return NewMachGroupM
   End Function

   Public Shared Function CreateBeamMachGroup(sName As String, sMachine As String) As MachGroupM
      Dim NewMachGroupM As New BeamMachGroupM
      ' Creo il nuovo gruppo di lavorazione con i dati ottenuti a seconda del caso in cui mi trovo
      NewMachGroupM.m_Id = EgtAddMachGroup(sName, sMachine)
      If NewMachGroupM.m_Id = GDB_ID.NULL Then Return Nothing
      NewMachGroupM.m_Name = sName
      NewMachGroupM.m_Machine = sMachine
      'NewMachGroupM.SetMachineType(MachineType.BEAM)
      EgtGetInfo(NewMachGroupM.m_Id, MGR_RPT_BARLEN, NewMachGroupM.m_dL)
      EgtGetInfo(NewMachGroupM.m_Id, MGR_RPT_BARHEIGHT, NewMachGroupM.m_dH)
      EgtGetInfo(NewMachGroupM.m_Id, MGR_RPT_BARWIDTH, NewMachGroupM.m_dW)
      NewMachGroupM.m_dTotMat = NewMachGroupM.m_dL
      For Each Part In NewMachGroupM.m_PartMList
         NewMachGroupM.m_dMatForPart += Part.dL
      Next
      ReadMachGroupData(NewMachGroupM)
      Return NewMachGroupM
   End Function

#End Region    ' CONSTRUCTORS

#Region "X DataLayer"

   Public Shared Function CreateBeamMachGroupDL(nId As Integer, sName As String, sMachine As String, PartMList As List(Of PartM)) As MachGroupM
      Dim NewMachGroupM As New BeamMachGroupM
      NewMachGroupM.m_Id = nId
      NewMachGroupM.m_Name = sName
      NewMachGroupM.m_Machine = sMachine
      'NewMachGroupM.SetMachineType(MachineType.BEAM)
      'EgtGetInfo(NewMachGroupM.m_Id, MGR_RPT_BARLEN, NewMachGroupM.m_dL)
      'EgtGetInfo(NewMachGroupM.m_Id, MGR_RPT_BARHEIGHT, NewMachGroupM.m_dH)
      'EgtGetInfo(NewMachGroupM.m_Id, MGR_RPT_BARWIDTH, NewMachGroupM.m_dW)
      'NewMachGroupM.m_dTotMat = NewMachGroupM.m_dL
      NewMachGroupM.m_PartMList = PartMList
      Return NewMachGroupM
   End Function

#End Region

   Private Shared Function LoadParts(NewBeamMachGroupM As BeamMachGroupM) As List(Of PartM)
      Dim TempList As New List(Of PartM)
      Dim nRawPartId As Integer = EgtGetFirstRawPart()
      While nRawPartId <> GDB_ID.NULL
         Dim nPartId As Integer = EgtGetFirstPartInRawPart(nRawPartId)
         If nPartId <> GDB_ID.NULL Then
            Dim NewBeam As BeamM = BeamM.CreateBeam(NewBeamMachGroupM, nPartId, nRawPartId)
            TempList.Add(NewBeam)
         End If
         nRawPartId = EgtGetNextRawPart(nRawPartId)
      End While
      Return TempList
   End Function

   Public Overrides Sub RefreshPartList()
      ' leggo StartCut da posizione primo pezzo
      Dim sInfo As String = ""
      Dim sSplitInfo() As String
      Dim bFound As Boolean = False
      EgtGetInfo(Id, MGR_RPT_PART & 1, sInfo)
      If Not String.IsNullOrWhiteSpace(sInfo) Then
         sSplitInfo = sInfo.Split(","c)
         StringToDouble(sSplitInfo(1), m_dStartCut)
      Else
         m_dStartCut = 0
      End If
      ' aggiorno lista pezzi
      RemoveAllParts()
      Dim nRawPartId As Integer = EgtGetFirstRawPart()
      While nRawPartId <> GDB_ID.NULL
         Dim nPartId As Integer = EgtGetFirstPartInRawPart(nRawPartId)
         If nPartId <> GDB_ID.NULL Then
            Dim NewBeam As BeamM = BeamM.CreateBeam(Me, nPartId, nRawPartId)
            AddPart(NewBeam)
            NewBeam.UpdateOffset()
         End If
         nRawPartId = EgtGetNextRawPart(nRawPartId)
      End While
   End Sub

   Public Overrides Sub RefreshGroupData()
      EgtGetInfo(Id, MGR_RPT_BARLEN, dL)
      EgtGetInfo(Id, MGR_RPT_BARHEIGHT, dH)
      EgtGetInfo(Id, MGR_RPT_BARWIDTH, dW)
      EgtGetInfo(Id, MGR_RPT_MATERIAL, sMATERIAL)
   End Sub

End Class
