Imports EgtUILib
Imports EgtWPFLib

Public Class OtherRefTabWD
   Dim m_MainWindow As MainWindow = DirectCast(Application.Current.MainWindow, MainWindow)
   Dim m_RawPartPage As RawPartPageUC

   Private m_sPosX As String = "0"
   Private m_sPosY As String = "0"

   Private m_dPrevOffsetX As Double = 0
   Private m_dPrevOffsetY As Double = 0

   Private m_bModifX As Boolean = False
   Private m_bModifY As Boolean = False

   Private m_SelIndex As Integer = 0

   Private m_SelRef As Integer = 0

   Sub New(Owner As Window, PosX As String, PosY As String, OffsetX As Double, OffsetY As Double, OtherRefMode As Integer, SelRef As Integer)
      Me.Owner = Owner
      ' La chiamata è richiesta dalla finestra di progettazione.
      InitializeComponent()

      ' Aggiungere le eventuali istruzioni di inizializzazione dopo la chiamata a InitializeComponent().
      m_sPosX = PosX
      m_sPosY = PosY
      m_dPrevOffsetX = OffsetX
      m_dPrevOffsetY = OffsetY
      m_SelIndex = OtherRefMode
      m_SelRef = SelRef
   End Sub

   Private Sub OtherRrefTabWD_Initialized(sender As Object, e As EventArgs) Handles Me.Initialized
      ' costruisco il riferimento alla pagina Raw
      m_RawPartPage = m_MainWindow.m_RawPartPage
      ' posiziono la finestra
      Me.Top = Owner.Top + Owner.Height / 4 - Me.Height / 2
      Me.Left = Owner.Left + Owner.Width / 6 - Me.Width / 2
      RefRawTxbl.Text = EgtMsg(91248)     ' Raw reference
      RefTabXTxbl.Text = EgtMsg(91249)    ' Position X
      RefTabYTxbl.Text = EgtMsg(91250)    ' Position Y
      SelRefTxbl.Text = EgtMsg(91251)     ' Sel reference
      RefTabOrientationCmBx.ItemsSource = m_RawPartPage.m_OtherRefTabOrientationList
      RefTabOrientationCmBx.SelectedIndex = m_RawPartPage.m_OtherRefTabPos
      RefTabCmBx.ItemsSource = m_RawPartPage.m_OtherRefTabList
      RefTabCmBx.SelectedIndex = m_RawPartPage.m_CurrRefTab
   End Sub

   Private Sub OtherRrefTabWD_Load(sender As Object, e As EventArgs) Handles Me.Loaded
      RefTabXTxBx.Text = m_sPosX
      RefTabYTxBx.Text = m_sPosY
   End Sub

   Private Sub RefTabCmBx_SelectionChanged(sender As Object, e As SelectionChangedEventArgs) Handles RefTabCmBx.SelectionChanged
      If m_bModifX Or m_bModifY Then
         Dim SaveCurrToolWnd = New EgtMsgBox(m_MainWindow, "", EgtMsg(91102), EgtMsgBox.Buttons.YES_NO_CANCEL, EgtMsgBox.Icons.NULL)   ' Salvare l'utensile corrente?
         Select Case SaveCurrToolWnd.m_nPressedBtn
            Case 0    ' Annulla
               ' annullo l'operazione di cambio indice
               RefTabCmBx.SelectedIndex = m_RawPartPage.m_CurrRefTab
               Return
            Case 1    ' Si
               ' salvo i valori modificati
               UpdateCurrOffset()
               ' recupero l'oggetto precedentemente selezionato e lo aggiorno (per evitare di rileggere il file ini)
               Dim temp_RefTab As ReferenceTable = m_RawPartPage.m_OtherRefTabList(m_RawPartPage.m_CurrRefTab)
               temp_RefTab.dOffsetX = m_RawPartPage.m_OtherRefTabX
               temp_RefTab.dOffsetY = m_RawPartPage.m_OtherRefTabY
               temp_RefTab.nPos = m_RawPartPage.m_OtherRefTabPos
            Case 2    ' No
               ' Non salvo e vado oltre
               m_bModifX = False
               m_bModifY = False
         End Select
      End If
      ' recupero l'oggetto selezionato
      Dim local_RefTab As ReferenceTable = m_RawPartPage.m_OtherRefTabList(RefTabCmBx.SelectedIndex)
      m_RawPartPage.m_CurrRefTab = RefTabCmBx.SelectedIndex
      ' Imposto gli Offset del riferimento selezionato
      RefTabXTxBx.Text = LenToString(local_RefTab.dOffsetX, 2)
      m_RawPartPage.m_OtherRefTabX = local_RefTab.dOffsetX
      RefTabYTxBx.Text = LenToString(local_RefTab.dOffsetY, 2)
      m_RawPartPage.m_OtherRefTabY = local_RefTab.dOffsetY
      ' Aggiorno la posizine del grezzo
      m_RawPartPage.RefTabModeChange()
      m_bModifX = False
      m_bModifY = False
   End Sub

   Private Sub RefTabOrientationCmBx_SelectionChanged(sender As Object, e As SelectionChangedEventArgs) Handles RefTabOrientationCmBx.SelectionChanged
      m_RawPartPage.m_OtherRefTabPos = RefTabOrientationCmBx.SelectedIndex
      m_RawPartPage.RefTabModeChange()
   End Sub

   Private Sub RefTabXTxBx_EgtClosed(sender As Object, e As EventArgs) Handles RefTabXTxBx.EgtClosed
      Dim ptMin, ptMax As Point3d
      If EgtGetTableArea(1, ptMin, ptMax) Then
         Dim dX As Double = 0
         StringToLen(RefTabXTxBx.Text, dX)
         If Not (dX > 0 And dX < ptMax.x - ptMin.x) Then
            If dX > 0 Then
               dX = ptMax.x - ptMin.x
            Else
               dX = 0
            End If
            RefTabXTxBx.Text = LenToString(dX, 2)
            ' errore, posizione fuori dalla tavola
         End If
         m_RawPartPage.m_OtherRefTabX = dX
         m_bModifX = True
         m_RawPartPage.RefTabModeChange()
      End If
   End Sub

   Private Sub RefTabYTxBx_EgtClosed(sender As Object, e As EventArgs) Handles RefTabYTxBx.EgtClosed
      Dim ptMin, ptMax As Point3d
      If EgtGetTableArea(1, ptMin, ptMax) Then
         Dim dY As Double = 0
         StringToLen(RefTabYTxBx.Text, dY)
         If Not (dY > 0 And dY < ptMax.y - ptMin.y) Then
            If dY > 0 Then
               dY = ptMax.y - ptMin.y
            Else
               dY = 0
            End If
            RefTabYTxBx.Text = LenToString(dY, 2)
            ' errore, posizione fuori dalla tavola
         End If
         m_RawPartPage.m_OtherRefTabY = dY
         m_bModifY = True
         m_RawPartPage.RefTabModeChange()
      End If
   End Sub

   Private Sub OkBtn_Click() Handles OkBtn.Click
      WritePrivateProfileString(S_RAWPART, K_CURRENTREFTAB, m_RawPartPage.m_CurrRefTab.ToString, m_MainWindow.GetIniFile())
      ' Aggiorno il valore del riferimento correntemente modificato
      UpdateCurrOffset()
      m_RawPartPage.RefTabModeChange()
      Me.Close()
   End Sub

   Private Sub UpdateCurrOffset()
      Dim sKeyMsg As String = K_REFTAB & m_RawPartPage.m_CurrRefTab.ToString
      Dim sValMsg As String = DoubleToString(m_RawPartPage.m_OtherRefTabX, 2) & "," & DoubleToString(m_RawPartPage.m_OtherRefTabY, 2) & "," & m_RawPartPage.m_OtherRefTabPos.ToString
      WritePrivateProfileString(S_RAWPART, sKeyMsg, sValMsg, m_MainWindow.GetIniFile())
   End Sub

   Private Sub ExitBtn_Click() Handles ExitBtn.Click
      ' Recupero la prima selezione eseguita
      m_RawPartPage.m_CurrRefTab = m_SelRef
      Dim temp_RefTab As ReferenceTable = m_RawPartPage.m_OtherRefTabList(m_RawPartPage.m_CurrRefTab)
      ' aggiorno i valori
      m_RawPartPage.m_OtherRefTabX = temp_RefTab.dOffsetX
      m_RawPartPage.m_OtherRefTabY = temp_RefTab.dOffsetY
      m_RawPartPage.m_OtherRefTabPos = temp_RefTab.nPos
      m_RawPartPage.RefTabModeChange()
   End Sub

End Class

Public Class ReferenceTable

   Private m_dOffsetX As Double
   Private m_dOffsetY As Double
   Private m_nPos As RawPartPageUC.OTHERREFMODE = RawPartPageUC.OTHERREFMODE.BL
   Private m_nIndexRef As Integer = 0

   Public Property dOffsetX As Double
      Get
         Return m_dOffsetX
      End Get
      Set(value As Double)
         m_dOffsetX = value
      End Set
   End Property

   Public Property dOffsetY As Double
      Get
         Return m_dOffsetY
      End Get
      Set(value As Double)
         m_dOffsetY = value
      End Set
   End Property

   Public Property nPos As Integer
      Get
         Return m_nPos
      End Get
      Set(value As Integer)
         m_nPos = value
      End Set
   End Property

   Public Property nIndexRef As Integer
      Get
         Return m_nIndexRef
      End Get
      Set(value As Integer)
         m_nIndexRef = value
      End Set
   End Property

   Public ReadOnly Property Name As String
      Get
         Return "REF_" & m_nIndexRef.ToString()
      End Get
   End Property

   Public ReadOnly bOk As Boolean = True

   Sub New(sStringFromIni As String, IndexRef As Integer)
      Dim sItems() As String = sStringFromIni.Split(","c)
      If sItems.Count = 3 Then
         bOk = bOk And StringToLen(sItems(0), m_dOffsetX)
         bOk = bOk And StringToLen(sItems(1), m_dOffsetY)
         bOk = bOk And StringToInt(sItems(2), m_nPos)
         m_nIndexRef = IndexRef
      Else
         bOk = False
      End If
   End Sub

End Class