Imports EgtWPFLib5
Imports EgtUILib

Public Class MovePartInRawPartWndVM
   Inherits VMBase

#Region "FIELDS & PROPERTIES"

   Friend Event m_CloseWindow(bDialogResult As Boolean)

   Private m_dMoveLen As Double
   Public ReadOnly Property dMoveLen As Double
      Get
         Return m_dMoveLen
      End Get
   End Property
   Public Property sMoveLen As String
      Get
         Return LenToString(m_dMoveLen, 3)
      End Get
      Set(value As String)
         Dim dTempMoveLen As Double
         If StringToLenAdv(value, dTempMoveLen, True) AndAlso dTempMoveLen > 0 Then
            m_dMoveLen = dTempMoveLen
         Else
            m_dMoveLen = 0
            NotifyPropertyChanged(NameOf(sMoveLen))
         End If
      End Set
   End Property

   Private m_LeftIsChecked As Boolean
   Public Property LeftIsChecked As Boolean
      Get
         Return m_LeftIsChecked
      End Get
      Set(value As Boolean)
         If value <> m_LeftIsChecked Then
            m_LeftIsChecked = value
         End If
      End Set
   End Property

   Private m_RightIsChecked As Boolean
   Public Property RightIsChecked As Boolean
      Get
         Return m_RightIsChecked
      End Get
      Set(value As Boolean)
         If value <> m_RightIsChecked Then
            m_RightIsChecked = value
         End If
      End Set
   End Property

   ' Definizione comandi
   Private m_cmdOk As ICommand

#End Region    ' FIELDS & PROPERTIES

#Region "MESSAGES"

   Public ReadOnly Property MoveDirMsg As String
      Get
         Return EgtMsg(61945)
      End Get
   End Property

   Public ReadOnly Property MoveLenMsg As String
      Get
         Return EgtMsg(61946)
      End Get
   End Property

#End Region    ' MESSAGES

#Region "CONSTRUCTOR"

   Sub New()
      ' setto il bottone Left a premuto
      LeftIsChecked = True
   End Sub

#End Region    ' CONSTRUCTOR

#Region "COMMANDS"

#Region "Ok"

   Public ReadOnly Property Ok_Command As ICommand
      Get
         If m_cmdOk Is Nothing Then
            m_cmdOk = New Command(AddressOf Ok)
         End If
         Return m_cmdOk
      End Get
   End Property

   Public Sub Ok()
      ' verifico che il campo dello spostamento abbia un valore valido
      If Not IsNothing(m_dMoveLen) AndAlso m_dMoveLen > 0 Then
         ' calcolo BBox dell'outline del grezzo
         Dim b3RawPart As New BBox3d
         Dim nRawId As Integer = Map.refProjectVM.MachGroupPanelVM.SelectedMachGroup.MyMachGroupM.nRawPartId
         Dim nRawOutlineId As Integer = EgtGetFirstNameInGroup(nRawId, RAWOUTLINE)
         EgtGetBBoxGlob(nRawOutlineId, GDB_BB.STANDARD, b3RawPart)
         ' calcolo BBox globale dei pezzi aggiunti al grezzo
         Dim b3Parts As New BBox3d
         For Each Part In Map.refProjectVM.MachGroupPanelVM.SelectedMachGroup.PartVMList
            Dim b3Part As New BBox3d
            ' leggo outline del pezzo
            Dim nOutlineLayerId As Integer = EgtGetFirstNameInGroup(Part.nPartId, Core.ConstBeam.OUTLINE)
            Dim nOutlineId As Integer = EgtGetFirstNameInGroup(nOutlineLayerId, "ON_TMP")
            ' se ON_TMP è stato trovato (il primo trovato equivale al percorso più grande) ricavo BBox di quello altrimenti ricavo BBox del pezzo
            Dim nId As Integer = 0
            If nOutlineId <> GDB_ID.NULL Then
               nId = nOutlineId
            Else
               nId = Part.nPartId
            End If
            EgtGetBBoxGlob(nId, GDB_BB.STANDARD, b3Part)
            b3Parts.Add(b3Part)
         Next
         ' ricavo coordinate minime e massime sull'asse X dei BBox del grezzo e dei pezzi 
         Dim dRawPartMinL As Double = b3RawPart.Min.x
         Dim dRawPartMaxL As Double = b3RawPart.Max.x
         Dim dPartsMinL As Double = b3Parts.Min.x
         Dim dPartsMaxL As Double = b3Parts.Max.x
         ' ricavo il vettore di spostamento
         Dim vtMove As Vector3d = New Vector3d(If(LeftIsChecked, dMoveLen, -dMoveLen), 0, 0)
         ' verifico che i pezzi venendo mossi non escano dal box
         If dPartsMinL + vtMove.x > dRawPartMinL And dPartsMaxL + vtMove.x < dRawPartMaxL Then
            ' muovo i pezzi di quanto possibile
            If vtMove.SqLen() > EPS_SMALL * EPS_SMALL Then
               ' muovo tutti i pezzi della lista
               For Each Part In Map.refProjectVM.MachGroupPanelVM.SelectedMachGroup.PartVMList
                  EgtMovePartInRawPart(Part.nPartId, vtMove)
                  ' aggiorno valori nella tabella
                  Part.NotifyPropertyChanged(NameOf(PartVM.dPOSX))
                  Part.NotifyPropertyChanged(NameOf(PartVM.sPOSX))
                  Part.NotifyPropertyChanged(NameOf(PartVM.dPOSY))
                  Part.NotifyPropertyChanged(NameOf(PartVM.sPOSY))
                  EgtZoom(ZM.ALL)
               Next
            End If
            RaiseEvent m_CloseWindow(True)
         Else
            MessageBox.Show(EgtMsg(61947), EgtMsg(30007))
         End If
      Else
         MessageBox.Show(EgtMsg(61948), EgtMsg(30007))
      End If
   End Sub

#End Region ' Ok

#End Region    ' COMMANDS

End Class
