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

Public Class BeamM
   Inherits PartM

   Protected m_dOffset As Double
   Public Property dOffset As Double
      Get
         Return m_dOffset
      End Get
      Set(value As Double)
         m_dOffset = value
      End Set
   End Property

   ' Id del grezzo
   Protected m_nRawPartId As Integer = GDB_ID.NULL

   '#Region "CONSTRUCTOR"

   '   Sub New(nParentMachGroup As MyMachGroupM, nPartId As Integer, nRawPartId As Integer)
   '      MyBase.New(nParentMachGroup, nPartId)
   '      m_nRawPartId = nRawPartId
   '   End Sub

   '#End Region    ' CONSTRUCTOR

#Region "CONSTRUCTORS"

   Protected Sub New(nParentMachGroup As MyMachGroupM, nPartId As Integer)
      MyBase.New(nParentMachGroup, nPartId)
   End Sub

   'Public Shared Function CreateNewBeam() As BeamM
   '   Return New BeamM
   'End Function

   Public Shared Function CreateBeam(nParentMachGroup As MyMachGroupM, nPartId As Integer, nRawPartId As Integer) As BeamM
      Dim NewBeamM As New BeamM(nParentMachGroup, nPartId)
      NewBeamM.m_nRawPartId = nRawPartId
      ' leggo info pezzo
      EgtGetInfo(nPartId, BTL_PRT_PROJ, NewBeamM.m_nProjId)
      EgtGetInfo(nPartId, BTL_PRT_PDN, NewBeamM.m_nPDN)
      EgtGetInfo(nPartId, BTL_PRT_NAM, NewBeamM.m_sNAM)
      EgtGetInfo(nPartId, BTL_PRT_L, NewBeamM.m_dBtlL)
      EgtGetInfo(nPartId, BTL_PRT_W, NewBeamM.m_dBtlW)
      EgtGetInfo(nPartId, BTL_PRT_H, NewBeamM.m_dBtlH)
      EgtGetInfo(nPartId, BTL_PRT_CNT, NewBeamM.m_nCNT)
      EgtGetInfo(nPartId, BTL_PRT_GROUP, NewBeamM.m_sGROUP)
      EgtGetInfo(nPartId, BTL_PRT_MATERIAL, NewBeamM.m_sMATERIAL)
      Dim nTemp As Integer = 0
      If Not EgtGetInfo(nPartId, BTL_PRT_ROTATED, nTemp) Then
         nTemp = 0
      End If
      NewBeamM.SetRotated(nTemp)
      If Not EgtGetInfo(nPartId, BTL_PRT_INVERTED, nTemp) Then
         nTemp = 0
      End If
      NewBeamM.SetInverted(nTemp)
      ' leggo PosX
      Dim sInfo As String = ""
      Dim Index = 1
      Dim sSplitInfo() As String = Nothing
      Dim bFound As Boolean = False
      While EgtGetInfo(NewBeamM.m_ParentMachGroup.Id, MGR_RPT_PART & Index, sInfo)
         If Not String.IsNullOrWhiteSpace(sInfo) Then
            sSplitInfo = sInfo.Split(","c)
            If sSplitInfo(0) = nPartId Then
               bFound = True
               Exit While
            End If
         End If
         Index += 1
      End While
      If bFound Then
         StringToDouble(sSplitInfo(1), NewBeamM.m_dPOSX)
      End If
      ' leggo feature
      NewBeamM.m_FeatureMList = LoadBTLFeatures(nPartId)
      ' leggo calc error
      Dim nErr As Integer = 0
      Dim nRot As Integer = 0
      Dim nFall As Integer = 0
      Dim sMsg As String = ""
      Dim bCalc As Boolean = False
      bCalc = EgtGetInfo(nPartId, ITG_PROD_ERR, nErr)
      EgtGetInfo(nPartId, ITG_PROD_ROT, nRot)
      EgtGetInfo(nPartId, ITG_PROD_FALL, nFall)
      EgtGetInfo(nPartId, ITG_PROD_MSG, sMsg)
      If Not bCalc Then
         NewBeamM.m_nCALC_State = CalcStates.NOTCALCULATED
         NewBeamM.m_nCALC_ERR = -1
         NewBeamM.m_nCALC_ROT = 0
         NewBeamM.m_nCALC_FALL = 0
         NewBeamM.m_sCALC_MSG = ""
      Else
         NewBeamM.m_nCALC_ERR = nErr
         NewBeamM.m_nCALC_ROT = nRot
         NewBeamM.m_nCALC_FALL = nFall
         NewBeamM.m_sCALC_MSG = sMsg
         If nErr = 0 Then
            NewBeamM.m_nCALC_State = CalcStates.OK
         ElseIf nErr > 1 Then
            NewBeamM.m_nCALC_State = CalcStates.ERROR_
         ElseIf nErr < 1 Then
            NewBeamM.m_nCALC_State = CalcStates.NOTCALCULATED
         End If
      End If
      EgtGetInfo(nPartId, ITG_PROD_TIME, NewBeamM.m_nCALC_TIME)
      ReadMachGroupData(NewBeamM)
      Return NewBeamM
   End Function

#End Region    ' CONSTRUCTORS

#Region "X DataLayer"

   Public Shared Function CreateBeamDL(nParentMachGroup As MyMachGroupM, nPartId As Integer, nRawPartId As Integer) As BeamM
      Dim NewBeamM As New BeamM(nParentMachGroup, nPartId)
      NewBeamM.m_nRawPartId = nRawPartId
      '' leggo info pezzo
      'EgtGetInfo(nPartId, BTL_PRT_PROJ, NewBeamM.nProjId)
      'EgtGetInfo(nPartId, BTL_PRT_PDN, NewBeamM.nPDN)
      'EgtGetInfo(nPartId, BTL_PRT_NAM, NewBeamM.sNAM)
      'EgtGetInfo(nPartId, BTL_PRT_L, NewBeamM.dL)
      'EgtGetInfo(nPartId, BTL_PRT_W, NewBeamM.dW)
      'EgtGetInfo(nPartId, BTL_PRT_H, NewBeamM.dH)
      'EgtGetInfo(nPartId, BTL_PRT_CNT, NewBeamM.nCNT)
      'Dim nTemp As Integer = 0
      'If Not EgtGetInfo(nPartId, BTL_PRT_ROTATED, nTemp) Then
      '   nTemp = 0
      'End If
      'NewBeamM.SetRotated(nTemp)
      'If Not EgtGetInfo(nPartId, BTL_PRT_INVERTED, nTemp) Then
      '   nTemp = 0
      'End If
      'NewBeamM.SetInverted(nTemp)
      '' leggo PosX
      'Dim sInfo As String = ""
      'Dim Index = 1
      'Dim sSplitInfo() As String
      'Dim bFound As Boolean = False
      'While EgtGetInfo(NewBeamM.m_ParentMachGroup.Id, MGR_RPT_PART & Index, sInfo)
      '   If Not String.IsNullOrWhiteSpace(sInfo) Then
      '      sSplitInfo = sInfo.Split(","c)
      '      If sSplitInfo(0) = nPartId Then
      '         bFound = True
      '         Exit While
      '      End If
      '   End If
      '   Index += 1
      'End While
      'If bFound Then
      '   StringToDouble(sSplitInfo(1), NewBeamM.dPOSX)
      'End If
      '' leggo feature
      'NewBeamM.m_FeatureMList = LoadBTLFeatures(nPartId)
      Return NewBeamM
   End Function

#End Region

   Private Shared Function LoadBTLFeatures(nPartId As Integer) As List(Of BTLFeatureM)
      Dim TempList As New List(Of BTLFeatureM)
      ' Aggiungo outline
      Dim nOutLayerId As Integer = EgtGetFirstNameInGroup(nPartId, OUTLINE)
      Dim nOutlineId As Integer = EgtGetFirstInGroup(nOutLayerId)
      While nOutlineId <> GDB_ID.NULL
         ' verifico che sia una feature
         Dim nGRP As Integer
         If EgtGetInfo(nOutlineId, BTL_FTR_GRP, nGRP) AndAlso Not EgtExistsInfo(nOutlineId, BTL_FTR_MAINID) Then
            ' creo la feature
            TempList.Add(BTLFeatureM.CreateBTLFeature(Nothing, nOutlineId))
         End If
         nOutlineId = EgtGetNext(nOutlineId)
      End While
      ' Aggiungo feature
      Dim nProcessingId As Integer = EgtGetFirstNameInGroup(nPartId, PROCESSINGS)
      Dim nFeatureId As Integer = EgtGetFirstInGroup(nProcessingId)
      While nFeatureId <> GDB_ID.NULL
         ' verifico che sia una feature
         Dim nGRP As Integer
         If EgtGetInfo(nFeatureId, BTL_FTR_GRP, nGRP) AndAlso Not EgtExistsInfo(nFeatureId, BTL_FTR_MAINID) Then
            ' creo la feature
            TempList.Add(BTLFeatureM.CreateBTLFeature(Nothing, nFeatureId))
         End If
         nFeatureId = EgtGetNext(nFeatureId)
      End While
      Return TempList
   End Function

   Public Sub UpdateOffset()
      ' calcolo offset
      Dim nBeamIndex As Integer = ParentMachGroup.PartMList.IndexOf(Me)
      If nBeamIndex > 0 Then
         m_dOffset = dPOSX - (ParentMachGroup.PartMList(nBeamIndex - 1).dPOSX + ParentMachGroup.PartMList(nBeamIndex - 1).dL)
      Else
         m_dOffset = dPOSX
      End If
   End Sub

End Class
