Imports EgtUILib

Public Class FiloTopUC

   ' Riferimento alla MainWindow
   Private m_MainWindow As MainWindow = DirectCast(Application.Current.MainWindow, MainWindow)
   ' Riferimento alla pagina che ha aperto SideAngleUC
   Private m_CallingPage As MainWindow.Pages

   Private m_SelList As New List(Of Integer)

   ' Offset della sfilettatura
   Private m_dFiloTopOffset As Double = 0
   Friend ReadOnly Property FiloTopOffset As Double
      Get
         Return m_dFiloTopOffset
      End Get
   End Property

   ' Profondità della sfilettatura
   Private m_dFiloTopDepth As Double = 0
   Friend ReadOnly Property FiloTopDepth As Double
      Get
         Return m_dFiloTopDepth
      End Get
   End Property

   ' esegue l'arrotindamento sugli angoli del percorso utensile
   Private m_bCornerradius As Boolean = False
   Friend ReadOnly Property bCornerRadius As Boolean
      Get
         Return m_bCornerradius
      End Get
   End Property

   Private Sub FiloTopUC_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
      ' Imposto riferimenti ad altre pagine
      m_CallingPage = m_MainWindow.m_ActivePage

      ' Imposto messaggi
      ParameterTxBl.Text = EgtMsg(MSG_IMPORTPAGEUC + 10)       ' Offset
      Parameter2TxBl.Text = EgtMsg(MSG_IMPORTPAGEUC + 11)      ' Affondamento
      Parameter3TxBl.Text = EgtMsg(91138)

      If m_CallingPage = MainWindow.Pages.Draw Then
         ' Imposto questa come pagina correntemente visualizzata nella drawpage
         m_MainWindow.m_DrawPageUC.m_ActiveComponentPage = DrawPageUC.Pages.FiloTop
         ' Imposto contesto corrente
         EgtSetCurrentContext(m_MainWindow.m_DrawPageUC.DrawScene.GetCtx())
      ElseIf m_CallingPage = MainWindow.Pages.Import Then
         ' Imposto contesto corrente
         EgtSetCurrentContext(m_MainWindow.m_ImportPageUC.ImportScene.GetCtx())
      End If

      ' Gestisco visualizzazione dei parametri
      ParameterTxBl.Visibility = Windows.Visibility.Visible
      Parameter2TxBx.Visibility = Windows.Visibility.Visible
      Parameter3TxBl.Visibility = Windows.Visibility.Visible

      ' Aggiorno valori
      m_dFiloTopOffset = GetPrivateProfileDouble(S_SIDES, K_FILOTOPOFFSET, 5, m_MainWindow.GetIniFile())
      ParameterTxBx.Text = LenToString(m_dFiloTopOffset, 3)
      m_dFiloTopDepth = GetPrivateProfileDouble(S_SIDES, K_FILOTOPDEPTH, 5, m_MainWindow.GetIniFile())
      Parameter2TxBx.Text = LenToString(m_dFiloTopDepth, 3)
      m_bCornerradius = GetPrivateProfileInt(S_SIDES, K_ROUNDOFF, 0, m_MainWindow.GetIniFile()) <> 0
      Parameter3ChBx.IsChecked = m_bCornerradius
      m_SelList.Clear()
   End Sub

   Private Sub FiloTopUC_Unloaded(sender As Object, e As RoutedEventArgs) Handles Me.Unloaded
      EgtDraw()
   End Sub

   Private Sub ParameterTxBx_EgtClosed(sender As Object, e As EventArgs) Handles ParameterTxBx.EgtClosed
      ' Recupero il valore
      StringToLen(ParameterTxBx.Text, m_dFiloTopOffset)
      WritePrivateProfileString(S_SIDES, K_FILOTOPOFFSET, DoubleToString(m_dFiloTopOffset, 2), m_MainWindow.GetIniFile())
      ' Aggiorno affondamento del profilo
      Dim nPartId As Integer = EgtGetFirstPart()
      While nPartId <> GDB_ID.NULL
         Dim nLayId As Integer = EgtGetFirstLayer(nPartId)
         While nLayId <> GDB_ID.NULL
            Dim sLayName As String = ""
            If EgtGetName(nLayId, sLayName) AndAlso sLayName = NAME_INLOOP AndAlso EgtExistsInfo(nLayId, INFO_FILOTOP) Then
               EgtSetInfo(nLayId, INFO_OFFSET, m_dFiloTopOffset)
            End If
            nLayId = EgtGetNextLayer(nLayId)
         End While
         nPartId = EgtGetNextPart(nPartId)
      End While
      ' aggiorno la preview
      CreateFiloTopPreView(m_SelList, False)
   End Sub

   Private Sub Parameter2TxBx_EgtClosed(sender As Object, e As EventArgs) Handles Parameter2TxBx.EgtClosed
      ' Recupero il valore
      StringToLen(Parameter2TxBx.Text, m_dFiloTopDepth)
      WritePrivateProfileString(S_SIDES, K_FILOTOPDEPTH, DoubleToString(m_dFiloTopDepth, 2), m_MainWindow.GetIniFile())
      ' Aggiorno affondamento del profilo
      Dim nPartId As Integer = EgtGetFirstPart()
      While nPartId <> GDB_ID.NULL
         Dim nLayId As Integer = EgtGetFirstLayer(nPartId)
         While nLayId <> GDB_ID.NULL
            Dim sLayName As String = ""
            If EgtGetName(nLayId, sLayName) AndAlso sLayName = NAME_INLOOP AndAlso EgtExistsInfo(nLayId, INFO_FILOTOP) Then
               EgtSetInfo(nLayId, INFO_DEPTH, m_dFiloTopDepth)
            End If
            nLayId = EgtGetNextLayer(nLayId)
         End While
         nPartId = EgtGetNextPart(nPartId)
      End While
   End Sub

   Private Sub Parameter3ChBx_Checked() Handles Parameter3ChBx.Click
      ' Assegno il valore
      m_bCornerradius = Parameter3ChBx.IsChecked
      WritePrivateProfileString(S_SIDES, K_ROUNDOFF, If(m_bCornerradius, "1", "0"), m_MainWindow.GetIniFile())
      ' Aggiorno le info del profilo
      Dim nPartId As Integer = EgtGetFirstPart()
      While nPartId <> GDB_ID.NULL
         Dim nLayId As Integer = EgtGetFirstLayer(nPartId)
         While nLayId <> GDB_ID.NULL
            Dim sLayName As String = ""
            If EgtGetName(nLayId, sLayName) AndAlso sLayName = NAME_INLOOP AndAlso EgtExistsInfo(nLayId, INFO_FILOTOP) Then
               EgtSetInfo(nLayId, INFO_ROUNDOFF, If(m_bCornerradius, 1, 0))
            End If
            nLayId = EgtGetNextLayer(nLayId)
         End While
         nPartId = EgtGetNextPart(nPartId)
      End While
      ' aggiorno la preview (False = blocco la lettura dei dati della componente)
      CreateFiloTopPreView(m_SelList, False)
   End Sub

   Public Sub CreateFiloTopPreView(SelList As List(Of Integer), Optional bReadInfo As Boolean = True)
      m_SelList = SelList
      If m_SelList.Count < 1 Then Return
      Dim nPartId As Integer = EgtGetFirstPart()
      While nPartId <> GDB_ID.NULL
         Dim nLayId As Integer = EgtGetFirstLayer(nPartId)
         While nLayId <> GDB_ID.NULL
            Dim sLayName As String = ""
            ' verifico che lemento corrente appartenga all'elenco degli elementi selezionati
            Dim bIsSelected As Boolean = m_SelList.Find(Function(value As Integer)
                                                           Return value = nLayId
                                                        End Function) <> 0
            If EgtGetName(nLayId, sLayName) AndAlso sLayName = NAME_INLOOP AndAlso EgtExistsInfo(nLayId, INFO_FILOTOP) AndAlso bIsSelected Then
               ' ---------------------- Inizio GESTIONE PREVIEW percorso -------------------------------
               Dim nLayFiloTop As Integer = GDB_ID.NULL
               Dim nInLoopRef As Integer = GDB_ID.NULL
               nLayFiloTop = EgtGetFirstNameInGroup(nPartId, "FiloTop")
               EgtGetInfo(nLayFiloTop, "InLoopRef", nInLoopRef)
               ' verifico il Layer "FiloTop" sia associato all'InLoop corrente
               While (nLayFiloTop <> GDB_ID.NULL AndAlso nInLoopRef <> nLayId)
                  nLayFiloTop = EgtGetNextName(nLayFiloTop, "FiloTop")
                  EgtGetInfo(nLayFiloTop, "InLoopRef", nInLoopRef)
               End While

               ' se non esiste il Layer "FiloTop"
               If nLayFiloTop = GDB_ID.NULL Then
                  ' lo creo
                  nLayFiloTop = EgtCreateGroup(nPartId)
                  EgtSetName(nLayFiloTop, "FiloTop")
                  EgtSetInfo(nLayFiloTop, "InLoopRef", nLayId)
                  EgtSetInfo(nLayId, "FiloTopRef", nLayFiloTop)
               End If

               ' carico i dati del filo top selezionato
               If bReadInfo Then
                  EgtGetInfo(nLayFiloTop, INFO_OFFSET, m_dFiloTopOffset)
                  ParameterTxBx.Text = LenToString(m_dFiloTopOffset, 3)
                  EgtGetInfo(nLayFiloTop, INFO_DEPTH, m_dFiloTopDepth)
                  Parameter2TxBx.Text = LenToString(m_dFiloTopDepth, 3)
                  Dim nVal As Integer = If(m_bCornerradius, 1, 0)
                  EgtGetInfo(nLayFiloTop, INFO_ROUNDOFF, nVal)
                  Parameter3ChBx.IsChecked = If(nVal = 1, True, False)
                  m_bCornerradius = Parameter3ChBx.IsChecked
               End If

               ' procedo alla creazione della curva
               Dim nIdCompoCurve As Integer = GDB_ID.NULL
               ' cancello un eventuale curva composita creata in precedenza
               nIdCompoCurve = EgtGetFirstNameInGroup(nLayFiloTop, "OffsetPreView")
               If nIdCompoCurve <> GDB_ID.NULL Then
                  EgtErase(nIdCompoCurve)
               End If
               Dim nCompoCurve As New List(Of Integer)
               ' costruisco il percorso chiuso dati i lati di contorno 
               Dim nLine As Integer = EgtGetFirstInGroup(nLayId)
               While nLine <> GDB_ID.NULL
                  nCompoCurve.Add(nLine)
                  nLine = EgtGetNext(nLine)
               End While
               Dim PtNearStart As Point3d
               Dim nInfoRoundOff As Integer = If(m_bCornerradius, 1, 0)

               nIdCompoCurve = EgtCreateCurveCompoByChain(nLayFiloTop, nCompoCurve.ToArray, PtNearStart, False)
               EgtSetName(nIdCompoCurve, "OffsetPreView")

               ' salvo i dati nel layer FiloTop
               EgtSetInfo(nLayFiloTop, INFO_OFFSET, m_dFiloTopOffset)
               EgtSetInfo(nLayFiloTop, INFO_DEPTH, m_dFiloTopDepth)
               EgtSetInfo(nLayFiloTop, INFO_ROUNDOFF, nInfoRoundOff)

               Dim bOk As Boolean = False
               ' leggo che tipo di offset applicare (Round, Extend)
               If nInfoRoundOff <> 1 Then
                  bOk = EgtOffsetCurve(nIdCompoCurve, -m_dFiloTopOffset, OFF_TYPE.EXTEND)
               Else
                  bOk = EgtOffsetCurve(nIdCompoCurve, -m_dFiloTopOffset, OFF_TYPE.FILLET)
               End If

               ' se la generazione della curva non va a buon fine
               If Not bOk Then
                  EgtErase(nLayFiloTop)
                  EgtRemoveInfo(nLayId, "FiloTopRef")
                  EgtOutLog("Error in generation OffsetPreView in TopEgde ")
               Else
                  m_MainWindow.m_DrawPageUC.MessageTxBx.Text = ""
                  m_MainWindow.m_DrawPageUC.MessageBrd.Background = Brushes.Transparent
               End If
               ' ---------------------- Fine GESTIONE PREVIEW percorso -------------------------------
            End If
            nLayId = EgtGetNextLayer(nLayId)
         End While
         nPartId = EgtGetNextPart(nPartId)
      End While
      EgtDraw()
   End Sub

   ' riceve la lista dei LayerFiloTop che devono essere eliminati
   Public Sub EraseFiloTopPreview(EraseList As List(Of Integer))
      Dim m_EraseList As List(Of Integer) = EraseList
      If m_EraseList.Count < 1 Then Return
      Dim nPartId As Integer = EgtGetFirstPart()
      While nPartId <> GDB_ID.NULL
         Dim nLayId As Integer = EgtGetFirstLayer(nPartId)
         While nLayId <> GDB_ID.NULL
            Dim sLayName As String = ""
            Dim bIsSelected As Boolean = m_EraseList.Find(Function(value As Integer)
                                                             Return value = nLayId
                                                          End Function) <> 0
            If EgtGetName(nLayId, sLayName) AndAlso sLayName = NAME_INLOOP AndAlso bIsSelected Then
               ' ---------------------- Inizio GESTIONE PREVIEW percorso -------------------------------
               Dim nLayFiloTop As Integer = GDB_ID.NULL
               Dim nInLoopRef As Integer = GDB_ID.NULL
               nLayFiloTop = EgtGetFirstNameInGroup(nPartId, "FiloTop")
               EgtGetInfo(nLayFiloTop, "InLoopRef", nInLoopRef)
               ' cerco il Layer associato all'InLoop corrente
               While (nLayFiloTop <> GDB_ID.NULL AndAlso nInLoopRef <> nLayId)
                  nLayFiloTop = EgtGetNextName(nLayFiloTop, "FiloTop")
                  EgtGetInfo(nLayFiloTop, "InLoopRef", nInLoopRef)
               End While

               ' se non esiste il Layer
               If nLayFiloTop = GDB_ID.NULL Then
                  ' esco 
                  Return
               Else
                  ' lo elimino
                  EgtErase(nLayFiloTop)
                  EraseList.Remove(nLayId)
               End If
               ' ---------------------- Fine GESTIONE PREVIEW percorso -------------------------------
            End If
            nLayId = EgtGetNextLayer(nLayId)
         End While
         nPartId = EgtGetNextPart(nPartId)
      End While
      EgtDraw()
   End Sub


End Class
