Imports System.IO
Imports EgtUILib
Imports EgtWPFLib

Public Class OptionsPageUC

   Dim m_MainWindow As MainWindow = DirectCast(Application.Current.MainWindow, MainWindow)

   Private UnitsList() As String = {"inch", "mm"}
   Private ThemesList() As String = {"Classic", "Dark"}

   ' "Last Project", "New Project", "Open Folder", "Show Window"
   Private StartLauncherList() As String = {EgtMsg(91013), EgtMsg(91014), EgtMsg(91015), EgtMsg(91016)}

   Private Sub OptionsPageUC_Initialized(sender As Object, e As EventArgs) Handles Me.Initialized

      ' Associazione della lista linguaggi alla combobox
      LanguageCmBx.ItemsSource = m_MainWindow.m_LanguagesList
      ' Associazione della lista unità di misura alla combobox
      UnitsOfMeasureCmBx.ItemsSource = UnitsList
      ' Associazione della lista temi alla combobox
      ThemesCmBx.ItemsSource = ThemesList

      ' Imposto la lingua corrente
      LanguageCmBx.SelectedItem = m_MainWindow.m_CurrLanguage

      ' Imposto l'unità di misura corrente
      UnitsOfMeasureCmBx.SelectedIndex = If(EgtUiUnitsAreMM(), 1, 0)

      ' Imposto il tema corrente
      ThemesCmBx.SelectedIndex = GetPrivateProfileInt(S_GENERAL, K_THEME, 0, m_MainWindow.GetIniFile())
      Themes_StartUp()

      ' Carico la lista con il tipo di avvio
      StartLauncherCmBx.ItemsSource = StartLauncherList
      ' 0-New, 1-Last, 2-Open, 3-Window
      StartLauncherCmBx.SelectedIndex = GetPrivateProfileInt(S_GENERAL, K_AUTOLOADLASTPROJ, 0, m_MainWindow.GetIniFile())

      ' Rendo invisibile il GroupBox dei Temi la versione corrente
      ' ThemesGpBx.Visibility = Windows.Visibility.Collapsed

      ' Leggo se la modalità FastGrid è abilitata, e controllo bit su chiave
      If GetPrivateProfileInt(S_FASTGRID, K_FG_ENABLE, 0, m_MainWindow.GetIniFile()) <> 0 Then
         FastGridGpBx.Visibility = Windows.Visibility.Visible
         ' Leggo se la modalità FastGrid è attiva
         Dim bFgOn As Boolean = GetPrivateProfileInt(S_FASTGRID, K_FG_ACTIVE, 0, m_MainWindow.GetIniFile()) <> 0
         EnableFastGridBtn.IsChecked = bFgOn
         ' Abilitazione importazione Dxf e pezzi parametrici
         m_MainWindow.m_CadCutPageUC.DrawBtn.IsEnabled = Not bFgOn
         m_MainWindow.m_CadCutPageUC.ImportBtn.IsEnabled = Not bFgOn
         ' Abilitazione Csv, Vein Matching e Registrazione
         m_MainWindow.m_CadCutPageUC.m_ProjectMgr.CSVBtn.IsEnabled = Not bFgOn
         m_MainWindow.m_CadCutPageUC.m_ProjectMgr.VeinMatchingBtn.IsEnabled = Not bFgOn
         m_MainWindow.m_CadCutPageUC.m_ProjectMgr.RegisterBtn.IsEnabled = Not bFgOn
      Else
         FastGridGpBx.Visibility = Windows.Visibility.Collapsed
         ' Attivo importazione Dxf e pezzi parametrici
         m_MainWindow.m_CadCutPageUC.DrawBtn.IsEnabled = True
         m_MainWindow.m_CadCutPageUC.ImportBtn.IsEnabled = True
         ' Attivo Csv, Vein Matching e Registrazione
         m_MainWindow.m_CadCutPageUC.m_ProjectMgr.CSVBtn.IsEnabled = True
         m_MainWindow.m_CadCutPageUC.m_ProjectMgr.VeinMatchingBtn.IsEnabled = True
         m_MainWindow.m_CadCutPageUC.m_ProjectMgr.RegisterBtn.IsEnabled = True
      End If

      ' Se abilitato (bit di chiave + INI), leggo i dati del Box SlabDxf
      If m_MainWindow.GetKeyOption(MainWindow.KEY_OPT.IMPORT_SLABDXF) And
         GetPrivateProfileInt(S_SLABDXF, K_IMPORTSLABDXF, 0, m_MainWindow.GetIniFile()) <> 0 Then
         SlabGpBx.Visibility = Windows.Visibility.Visible
         UpdateSlabDxf()
      Else
         SlabGpBx.Visibility = Windows.Visibility.Collapsed
      End If

      ' Leggo i dati di corrispondenza Colore-Angolo e Tallone
      UpdateAllCSTA()

      ' Leggo i dati di corrispondenza Colore-Affondamento incisione
      UpdateAllCTE()

      ' I parametri di TextColor presenti nell'INI vengono caricati
      Dim sTextColor As String = " "
      If GetPrivateProfileString(S_NEST, K_TEXTCOLOR, " ", sTextColor, m_MainWindow.GetIniFile()) <> 0 Then
         Dim sTextColorArray() As String = sTextColor.Split(",")
         TextColorBtn.Background = New SolidColorBrush(Color.FromRgb(sTextColorArray(0), sTextColorArray(1), sTextColorArray(2)))
      End If

      ' Messaggi
      LanguageGpBx.Header = EgtMsg(MSG_OPTIONSPAGEUC + 1)
      LanguageMsgTxBl.Text = EgtMsg(MSG_OPTIONSPAGEUC + 2)
      UnitsOfMeasureGpBx.Header = EgtMsg(MSG_OPTIONSPAGEUC + 3)
      FastGridGpBx.Header = EgtMsg(MSG_OPTIONSPAGEUC + 11)
      FastGridTxBl.Text = EgtMsg(MSG_OPTIONSPAGEUC + 12)
      TextColorGpBx.Header = EgtMsg(MSG_OPTIONSPAGEUC + 13)
      SlabGpBx.Header = EgtMsg(MSG_OPTIONSPAGEUC + 14)
      SlabLayerMsgTxBl.Text = EgtMsg(MSG_OPTIONSPAGEUC + 15)
      PartsLayerMsgTxBl.Text = EgtMsg(MSG_OPTIONSPAGEUC + 16)
      ScrapLayerMsgTxBl.Text = EgtMsg(MSG_OPTIONSPAGEUC + 17)
      StdThickMsgTxBl.Text = EgtMsg(MSG_OPTIONSPAGEUC + 18)
      ColorToSideAngGpBx.Header = EgtMsg(MSG_OPTIONSPAGEUC + 19)
      CTSAboxEnableMsgTxBl.Text = EgtMsg(MSG_OPTIONSPAGEUC + 20)
      ToleranceMsgTxBl.Text = EgtMsg(MSG_OPTIONSPAGEUC + 21)
      TalloneMsgTxBl.Text = EgtMsg(MSG_OPTIONSPAGEUC + 22)
      InclinazioneMsgTxBl.Text = EgtMsg(MSG_OPTIONSPAGEUC + 23)
      FeedbackGpBx.Header = EgtMsg(MSG_OPTIONSPAGEUC + 24)
      ThemesGpBx.Header = EgtMsg(MSG_OPTIONSPAGEUC + 25)
      ' Affonamento inciosioni da colore
      ColorToEngraveGpBx.Header = EgtMsg(MSG_OPTIONSPAGEUC + 26)
      CTEboxEnableMsgTxBl.Text = EgtMsg(MSG_OPTIONSPAGEUC + 20)
      ToleranceEMsgTxBl.Text = EgtMsg(MSG_OPTIONSPAGEUC + 21)
      AffondamentoMsgTxBl.Text = EgtMsg(MSG_OPTIONSPAGEUC + 27)
      LarghezzaMsgTxBl.Text = EgtMsg(MSG_OPTIONSPAGEUC + 28)
      StartLauncherGpBx.Header = EgtMsg(91009)
   End Sub

   Private Sub UpdateSlabDxf()
      ' Verifico abilitazione
      If SlabGpBx.Visibility <> Windows.Visibility.Visible Then Return
      ' Leggo parametri SlabDxf
      Dim sVal As String = " "
      GetPrivateProfileString(S_SLABDXF, K_SLABLAYER, " ", sVal, m_MainWindow.GetIniFile())
      SlabLayerMsgTxBx.Text = sVal
      GetPrivateProfileString(S_SLABDXF, K_PARTSLAYER, " ", sVal, m_MainWindow.GetIniFile())
      PartsLayerMsgTxBx.Text = sVal
      GetPrivateProfileString(S_SLABDXF, K_SCRAPLAYER, " ", sVal, m_MainWindow.GetIniFile())
      ScrapLayerMsgTxBx.Text = sVal
      GetPrivateProfileString(S_SLABDXF, K_STDTHICK, " ", sVal, m_MainWindow.GetIniFile())
      Dim dThick As Double : If Not StringToDouble(sVal, dThick) Then dThick = 0
      StdThickMsgTxBx.Text = LenToString(dThick, 4)
   End Sub

   Private Sub UpdateAllCSTA()
      ' Leggo se la modalità ColorToSideAng è abilitata
      CTSAboxEnableChBx.IsChecked = (GetPrivateProfileInt(S_COLORTOSIDEANG, K_CTSA_ENABLE, 0, m_MainWindow.GetIniFile()) <> 0)

      ' I parametri di ColorSideToAng presenti nell'INI vengono caricati a prescindere dall'abilitazione
      Dim nTol As Integer = GetPrivateProfileInt(S_COLORTOSIDEANG, K_CTSA_TOLERANCE, 0, m_MainWindow.GetIniFile())
      ToleranceMsgTxBx.Text = nTol
      ' Nel caso dei colori singoli viene verificata la presenza della Key corrispondente a ciascuno dei 
      ' 10 singoli colori (CTSAindex) e il Valore associato viene splittato in un array di stringhe
      For index As Integer = 1 To 10
         Dim sColor As String = " "
         If GetPrivateProfileString(S_COLORTOSIDEANG, K_CTSA & index, " ", sColor, m_MainWindow.GetIniFile()) <> 0 Then
            Dim sArray() As String = sColor.Split(",")
            Select Case index
               Case 1
                  ' Se non c'è il 6° elemento abilitazione di default a True
                  CTSA1EnableChBx.IsChecked = If(sArray.Length > 5, sArray(5) = "1", True)
                  CTSA1Btn.Background = New SolidColorBrush(Color.FromArgb(255, sArray(0), sArray(1), sArray(2)))
                  CTSA1InclinazioneTxBx.Text = sArray(3)
                  Dim dHeel As Double : If Not StringToDouble(sArray(4), dHeel) Then dHeel = 0
                  CTSA1TalloneTxBx.Text = LenToString(dHeel, 4)
               Case 2
                  CTSA2EnableChBx.IsChecked = If(sArray.Length > 5, sArray(5) = "1", True)
                  CTSA2Btn.Background = New SolidColorBrush(Color.FromArgb(255, sArray(0), sArray(1), sArray(2)))
                  CTSA2InclinazioneTxBx.Text = sArray(3)
                  Dim dHeel As Double : If Not StringToDouble(sArray(4), dHeel) Then dHeel = 0
                  CTSA2TalloneTxBx.Text = LenToString(dHeel, 4)
               Case 3
                  CTSA3EnableChBx.IsChecked = If(sArray.Length > 5, sArray(5).Equals("1"), True)
                  CTSA3Btn.Background = New SolidColorBrush(Color.FromArgb(255, sArray(0), sArray(1), sArray(2)))
                  CTSA3InclinazioneTxBx.Text = sArray(3)
                  Dim dHeel As Double : If Not StringToDouble(sArray(4), dHeel) Then dHeel = 0
                  CTSA3TalloneTxBx.Text = LenToString(dHeel, 4)
               Case 4
                  CTSA4EnableChBx.IsChecked = If(sArray.Length > 5, sArray(5).Equals("1"), True)
                  CTSA4Btn.Background = New SolidColorBrush(Color.FromArgb(255, sArray(0), sArray(1), sArray(2)))
                  CTSA4InclinazioneTxBx.Text = sArray(3)
                  Dim dHeel As Double : If Not StringToDouble(sArray(4), dHeel) Then dHeel = 0
                  CTSA4TalloneTxBx.Text = LenToString(dHeel, 4)
               Case 5
                  CTSA5EnableChBx.IsChecked = If(sArray.Length > 5, sArray(5).Equals("1"), True)
                  CTSA5Btn.Background = New SolidColorBrush(Color.FromArgb(255, sArray(0), sArray(1), sArray(2)))
                  CTSA5InclinazioneTxBx.Text = sArray(3)
                  Dim dHeel As Double : If Not StringToDouble(sArray(4), dHeel) Then dHeel = 0
                  CTSA5TalloneTxBx.Text = LenToString(dHeel, 4)
               Case 6
                  CTSA6EnableChBx.IsChecked = If(sArray.Length > 5, sArray(5).Equals("1"), True)
                  CTSA6Btn.Background = New SolidColorBrush(Color.FromArgb(255, sArray(0), sArray(1), sArray(2)))
                  CTSA6InclinazioneTxBx.Text = sArray(3)
                  Dim dHeel As Double : If Not StringToDouble(sArray(4), dHeel) Then dHeel = 0
                  CTSA6TalloneTxBx.Text = LenToString(dHeel, 4)
               Case 7
                  CTSA7EnableChBx.IsChecked = If(sArray.Length > 5, sArray(5).Equals("1"), True)
                  CTSA7Btn.Background = New SolidColorBrush(Color.FromArgb(255, sArray(0), sArray(1), sArray(2)))
                  CTSA7InclinazioneTxBx.Text = sArray(3)
                  Dim dHeel As Double : If Not StringToDouble(sArray(4), dHeel) Then dHeel = 0
                  CTSA7TalloneTxBx.Text = LenToString(dHeel, 4)
               Case 8
                  CTSA8EnableChBx.IsChecked = If(sArray.Length > 5, sArray(5).Equals("1"), True)
                  CTSA8Btn.Background = New SolidColorBrush(Color.FromArgb(255, sArray(0), sArray(1), sArray(2)))
                  CTSA8InclinazioneTxBx.Text = sArray(3)
                  Dim dHeel As Double : If Not StringToDouble(sArray(4), dHeel) Then dHeel = 0
                  CTSA8TalloneTxBx.Text = LenToString(dHeel, 4)
               Case 9
                  CTSA9EnableChBx.IsChecked = If(sArray.Length > 5, sArray(5).Equals("1"), True)
                  CTSA9Btn.Background = New SolidColorBrush(Color.FromArgb(255, sArray(0), sArray(1), sArray(2)))
                  CTSA9InclinazioneTxBx.Text = sArray(3)
                  Dim dHeel As Double : If Not StringToDouble(sArray(4), dHeel) Then dHeel = 0
                  CTSA9TalloneTxBx.Text = LenToString(dHeel, 4)
               Case 10
                  CTSA10EnableChBx.IsChecked = If(sArray.Length > 5, sArray(5).Equals("1"), True)
                  CTSA10Btn.Background = New SolidColorBrush(Color.FromArgb(255, sArray(0), sArray(1), sArray(2)))
                  CTSA10InclinazioneTxBx.Text = sArray(3)
                  Dim dHeel As Double : If Not StringToDouble(sArray(4), dHeel) Then dHeel = 0
                  CTSA10TalloneTxBx.Text = LenToString(dHeel, 4)
            End Select
         End If
      Next
   End Sub

   Private Sub UpdateAllCTE()
      ' Leggo se la modalità ColorToEngrave è abilitata
      CTEboxEnableChBx.IsChecked = (GetPrivateProfileInt(S_COLORTOENGRAVE, K_CTE_ENABLE, 0, m_MainWindow.GetIniFile()) <> 0)
      ' Leggo il valore della tolleranza
      Dim nTol As Integer = GetPrivateProfileInt(S_COLORTOENGRAVE, K_CTE_TOLERANCE, 0, m_MainWindow.GetIniFile())
      ToleranceEMsgTxBx.Text = nTol
      ' Nel caso dei colori singoli viene verificata la presenza della Key corrispondente a ciascuno dei 
      ' 10 singoli colori (CTSAindex) e il Valore associato viene splittato in un array di stringhe
      For index As Integer = 1 To 10
         Dim sColor As String = " "
         If GetPrivateProfileString(S_COLORTOENGRAVE, K_CTE & index, " ", sColor, m_MainWindow.GetIniFile()) <> 0 Then
            Dim sArray() As String = sColor.Split(",")
            Select Case index
               Case 1
                  ' Se non c'è il 6° elemento abilitazione di default a True
                  CTE1EnableChBx.IsChecked = If(sArray.Length > 5, sArray(5) = "1", True)
                  CTE1Btn.Background = New SolidColorBrush(Color.FromArgb(255, sArray(0), sArray(1), sArray(2)))
                  Dim dDepth As Double : If Not StringToDouble(sArray(3), dDepth) Then dDepth = 0
                  CTE1AffondamentoTxBx.Text = LenToString(dDepth, 4)
                  Dim dWidth As Double : If Not StringToDouble(sArray(4), dWidth) Then dWidth = 0
                  CTE1LarghezzaTxBx.Text = LenToString(dWidth, 4)
               Case 2
                  ' Se non c'è il 6° elemento abilitazione di default a True
                  CTE2EnableChBx.IsChecked = If(sArray.Length > 5, sArray(5) = "1", True)
                  CTE2Btn.Background = New SolidColorBrush(Color.FromArgb(255, sArray(0), sArray(1), sArray(2)))
                  Dim dDepth As Double : If Not StringToDouble(sArray(3), dDepth) Then dDepth = 0
                  CTE2AffondamentoTxBx.Text = LenToString(dDepth, 4)
                  Dim dWidth As Double : If Not StringToDouble(sArray(4), dWidth) Then dWidth = 0
                  CTE2LarghezzaTxBx.Text = LenToString(dWidth, 4)
               Case 3
                  ' Se non c'è il 6° elemento abilitazione di default a True
                  CTE3EnableChBx.IsChecked = If(sArray.Length > 5, sArray(5) = "1", True)
                  CTE3Btn.Background = New SolidColorBrush(Color.FromArgb(255, sArray(0), sArray(1), sArray(2)))
                  Dim dDepth As Double : If Not StringToDouble(sArray(3), dDepth) Then dDepth = 0
                  CTE3AffondamentoTxBx.Text = LenToString(dDepth, 4)
                  Dim dWidth As Double : If Not StringToDouble(sArray(4), dWidth) Then dWidth = 0
                  CTE3LarghezzaTxBx.Text = LenToString(dWidth, 4)
               Case 4
                  ' Se non c'è il 6° elemento abilitazione di default a True
                  CTE4EnableChBx.IsChecked = If(sArray.Length > 5, sArray(5) = "1", True)
                  CTE4Btn.Background = New SolidColorBrush(Color.FromArgb(255, sArray(0), sArray(1), sArray(2)))
                  Dim dDepth As Double : If Not StringToDouble(sArray(3), dDepth) Then dDepth = 0
                  CTE4AffondamentoTxBx.Text = LenToString(dDepth, 4)
                  Dim dWidth As Double : If Not StringToDouble(sArray(4), dWidth) Then dWidth = 0
                  CTE4LarghezzaTxBx.Text = LenToString(dWidth, 4)
               Case 5
                  ' Se non c'è il 6° elemento abilitazione di default a True
                  CTE5EnableChBx.IsChecked = If(sArray.Length > 5, sArray(5) = "1", True)
                  CTE5Btn.Background = New SolidColorBrush(Color.FromArgb(255, sArray(0), sArray(1), sArray(2)))
                  Dim dDepth As Double : If Not StringToDouble(sArray(3), dDepth) Then dDepth = 0
                  CTE5AffondamentoTxBx.Text = LenToString(dDepth, 4)
                  Dim dWidth As Double : If Not StringToDouble(sArray(4), dWidth) Then dWidth = 0
                  CTE5LarghezzaTxBx.Text = LenToString(dWidth, 4)
               Case 6
                  ' Se non c'è il 6° elemento abilitazione di default a True
                  CTE6EnableChBx.IsChecked = If(sArray.Length > 5, sArray(5) = "1", True)
                  CTE6Btn.Background = New SolidColorBrush(Color.FromArgb(255, sArray(0), sArray(1), sArray(2)))
                  Dim dDepth As Double : If Not StringToDouble(sArray(3), dDepth) Then dDepth = 0
                  CTE6AffondamentoTxBx.Text = LenToString(dDepth, 4)
                  Dim dWidth As Double : If Not StringToDouble(sArray(4), dWidth) Then dWidth = 0
                  CTE6LarghezzaTxBx.Text = LenToString(dWidth, 4)
               Case 7
                  ' Se non c'è il 6° elemento abilitazione di default a True
                  CTE7EnableChBx.IsChecked = If(sArray.Length > 5, sArray(5) = "1", True)
                  CTE7Btn.Background = New SolidColorBrush(Color.FromArgb(255, sArray(0), sArray(1), sArray(2)))
                  Dim dDepth As Double : If Not StringToDouble(sArray(3), dDepth) Then dDepth = 0
                  CTE7AffondamentoTxBx.Text = LenToString(dDepth, 4)
                  Dim dWidth As Double : If Not StringToDouble(sArray(4), dWidth) Then dWidth = 0
                  CTE7LarghezzaTxBx.Text = LenToString(dWidth, 4)
               Case 8
                  ' Se non c'è il 6° elemento abilitazione di default a True
                  CTE8EnableChBx.IsChecked = If(sArray.Length > 5, sArray(5) = "1", True)
                  CTE8Btn.Background = New SolidColorBrush(Color.FromArgb(255, sArray(0), sArray(1), sArray(2)))
                  Dim dDepth As Double : If Not StringToDouble(sArray(3), dDepth) Then dDepth = 0
                  CTE8AffondamentoTxBx.Text = LenToString(dDepth, 4)
                  Dim dWidth As Double : If Not StringToDouble(sArray(4), dWidth) Then dWidth = 0
                  CTE8LarghezzaTxBx.Text = LenToString(dWidth, 4)
               Case 9
                  ' Se non c'è il 6° elemento abilitazione di default a True
                  CTE9EnableChBx.IsChecked = If(sArray.Length > 5, sArray(5) = "1", True)
                  CTE9Btn.Background = New SolidColorBrush(Color.FromArgb(255, sArray(0), sArray(1), sArray(2)))
                  Dim dDepth As Double : If Not StringToDouble(sArray(3), dDepth) Then dDepth = 0
                  CTE9AffondamentoTxBx.Text = LenToString(dDepth, 4)
                  Dim dWidth As Double : If Not StringToDouble(sArray(4), dWidth) Then dWidth = 0
                  CTE9LarghezzaTxBx.Text = LenToString(dWidth, 4)
               Case 10
                  ' Se non c'è il 6° elemento abilitazione di default a True
                  CTE10EnableChBx.IsChecked = If(sArray.Length > 5, sArray(5) = "1", True)
                  CTE10Btn.Background = New SolidColorBrush(Color.FromArgb(255, sArray(0), sArray(1), sArray(2)))
                  Dim dDepth As Double : If Not StringToDouble(sArray(3), dDepth) Then dDepth = 0
                  CTE10AffondamentoTxBx.Text = LenToString(dDepth, 4)
                  Dim dWidth As Double : If Not StringToDouble(sArray(4), dWidth) Then dWidth = 0
                  CTE10LarghezzaTxBx.Text = LenToString(dWidth, 4)
            End Select
         End If
      Next
   End Sub

   Private Sub LanguageCmBx_SelectionChanged(sender As Object, e As SelectionChangedEventArgs) Handles LanguageCmBx.SelectionChanged
      m_MainWindow.m_CurrLanguage = LanguageCmBx.SelectedItem
      WritePrivateProfileString(S_GENERAL, K_MESSAGES, m_MainWindow.m_CurrLanguage.LanguageName, m_MainWindow.GetIniFile())
   End Sub

   Private Sub StartLauncherCmBx_SelectionChanged(sender As Object, e As SelectionChangedEventArgs) Handles StartLauncherCmBx.SelectionChanged
      Dim nIndex As Integer = StartLauncherCmBx.SelectedIndex
      WritePrivateProfileString(S_GENERAL, K_AUTOLOADLASTPROJ, StartLauncherCmBx.SelectedIndex, m_MainWindow.GetIniFile())
   End Sub

   Private Sub UnitsOfMeasureCmBx_SelectionChanged(sender As Object, e As SelectionChangedEventArgs) Handles UnitsOfMeasureCmBx.SelectionChanged
      Dim bMM As Boolean = (UnitsOfMeasureCmBx.SelectedIndex <> 0)
      EgtSetUiUnits(bMM)
      m_MainWindow.m_CurrentProjectPageUC.UpdateHeightTxBx()
      WritePrivateProfileString(S_GENERAL, K_MMUNITS, If(bMM, "1", "0"), m_MainWindow.GetIniFile())
      ' Aggiorno dati nella pagina dipendenti da unità di misura
      UpdateSlabDxf()
      UpdateAllCSTA()
   End Sub

   Private Sub ThemesCmBx_SelectionChanged(sender As Object, e As SelectionChangedEventArgs) Handles ThemesCmBx.SelectionChanged
      Dim app As Application = Application.Current
      If (ThemesCmBx.SelectedIndex = 0) Then
         'app.ChangeTheme(New Uri("/OmagCUT;component/OmagCUTClassicDictionary.xaml", UriKind.Relative))
         'm_MainWindow.EgtWPFInit()
         WritePrivateProfileString(S_GENERAL, K_THEME, 0, m_MainWindow.GetIniFile())
      End If
      If (ThemesCmBx.SelectedIndex = 1) Then
         'app.ChangeTheme(New Uri("/OmagCUT;component/OmagCUTDarkDictionary.xaml", UriKind.Relative))
         'Application.Current.Resources("GroupBox_CornerRadius") = 3
         'Application.Current.Resources("Button_CornerRadius") = 3
         'Application.Current.Resources("EmptyBorder_CornerRadius") = New CornerRadius(3)
         'm_MainWindow.EgtWPFInit()
         WritePrivateProfileString(S_GENERAL, K_THEME, 1, m_MainWindow.GetIniFile())
      End If
   End Sub

   Private Sub Themes_StartUp()
      Dim app As Application = Application.Current
      If ThemesCmBx.SelectedIndex = 0 Then
         app.ChangeTheme(New Uri("/OmagCUT;component/OmagCUTClassicDictionary.xaml", UriKind.Relative))
         m_MainWindow.EgtWPFInit()
      End If
      If ThemesCmBx.SelectedIndex = 1 Then
         app.ChangeTheme(New Uri("/OmagCUT;component/OmagCUTDarkDictionary.xaml", UriKind.Relative))
         Application.Current.Resources("GroupBox_CornerRadius") = 3
         Application.Current.Resources("Button_CornerRadius") = 3
         Application.Current.Resources("EmptyBorder_CornerRadius") = New CornerRadius(3)
         m_MainWindow.EgtWPFInit()
      End If
   End Sub

   Private Sub EnableFastGridBtn_Checked(sender As Object, e As RoutedEventArgs) Handles EnableFastGridBtn.Checked
      WritePrivateProfileString(S_FASTGRID, K_FG_ACTIVE, 1, m_MainWindow.GetIniFile())
      ' Disattivo importazione Dxf e pezzi parametrici
      m_MainWindow.m_CadCutPageUC.DrawBtn.IsEnabled = False
      m_MainWindow.m_CadCutPageUC.ImportBtn.IsEnabled = False
      ' Disattivo Csv, Vein Matching e Registrazione
      m_MainWindow.m_CadCutPageUC.m_ProjectMgr.CSVBtn.IsEnabled = False
      m_MainWindow.m_CadCutPageUC.m_ProjectMgr.VeinMatchingBtn.IsEnabled = False
      m_MainWindow.m_CadCutPageUC.m_ProjectMgr.RegisterBtn.IsEnabled = False
   End Sub

   Private Sub EnableFastGridBtn_Unchecked(sender As Object, e As RoutedEventArgs) Handles EnableFastGridBtn.Unchecked
      WritePrivateProfileString(S_FASTGRID, K_FG_ACTIVE, 0, m_MainWindow.GetIniFile())
      ' Attivo importazione Dxf e pezzi parametrici
      m_MainWindow.m_CadCutPageUC.DrawBtn.IsEnabled = True
      m_MainWindow.m_CadCutPageUC.ImportBtn.IsEnabled = True
      ' Attivo Csv, Vein Matching e Registrazione
      m_MainWindow.m_CadCutPageUC.m_ProjectMgr.CSVBtn.IsEnabled = True
      m_MainWindow.m_CadCutPageUC.m_ProjectMgr.VeinMatchingBtn.IsEnabled = True
      m_MainWindow.m_CadCutPageUC.m_ProjectMgr.RegisterBtn.IsEnabled = True
   End Sub

   Private Sub WriteCTSA(BtnCol As Button, sInc As String, sTall As String, bCheck As Boolean, nInd As Integer)
      Dim Rvalue As String = (BtnCol.Background.GetValue(SolidColorBrush.ColorProperty)).R
      Dim Gvalue As String = (BtnCol.Background.GetValue(SolidColorBrush.ColorProperty)).G
      Dim Bvalue As String = (BtnCol.Background.GetValue(SolidColorBrush.ColorProperty)).B
      Dim dAng As Double : If Not StringToDouble(sInc, dAng) Then dAng = 0
      Dim dHeel As Double : If Not StringToLen(sTall, dHeel) Then dHeel = 0
      ' Costruisco la string RgbCTSA che corrisponderà al valore da scrivere nell'INI;
      ' in caso di valore null o " " verrà scritto nella stringa il valore 0 (o 1 nel caso dell'Enable)
      Dim RgbCTSA As String = If(String.IsNullOrWhiteSpace(Rvalue), "0", Rvalue) & "," &
                              If(String.IsNullOrWhiteSpace(Gvalue), "0", Gvalue) & "," &
                              If(String.IsNullOrWhiteSpace(Bvalue), "0", Bvalue) & "," &
                              DoubleToString(dAng, 1) & "," &
                              DoubleToString(dHeel, 4) & "," &
                              If(bCheck, "1", "0")
      WritePrivateProfileString(S_COLORTOSIDEANG, K_CTSA & nInd.ToString(), RgbCTSA, m_MainWindow.GetIniFile())
   End Sub

   Private Sub WriteCTE(BtnCol As Button, sAff As String, sLar As String, bCheck As Boolean, nInd As Integer)
      Dim Rvalue As String = (BtnCol.Background.GetValue(SolidColorBrush.ColorProperty)).R
      Dim Gvalue As String = (BtnCol.Background.GetValue(SolidColorBrush.ColorProperty)).G
      Dim Bvalue As String = (BtnCol.Background.GetValue(SolidColorBrush.ColorProperty)).B
      Dim dAff As Double : If Not StringToDouble(sAff, dAff) Then dAff = 0
      Dim dLar As Double : If Not StringToDouble(sLar, dLar) Then sLar = 0
      ' Costruisco la string RgbCTSA che corrisponderà al valore da scrivere nell'INI;
      ' in caso di valore null o " " verrà scritto nella stringa il valore 0 (o 1 nel caso dell'Enable)
      Dim RgbCTE As String = If(String.IsNullOrWhiteSpace(Rvalue), "0", Rvalue) & "," &
                              If(String.IsNullOrWhiteSpace(Gvalue), "0", Gvalue) & "," &
                              If(String.IsNullOrWhiteSpace(Bvalue), "0", Bvalue) & "," &
                              DoubleToString(dAff, 1) & "," &
                              DoubleToString(dLar, 1) & "," &
                              If(bCheck, "1", "0")
      WritePrivateProfileString(S_COLORTOENGRAVE, K_CTE & nInd.ToString(), RgbCTE, m_MainWindow.GetIniFile())
   End Sub

   Private Sub CTSA1Btn_Click(sender As Object, e As RoutedEventArgs) Handles CTSA1Btn.Click
      ' Il click sul bottone aprirà la finestra ChooseColor che permetterà la scelta del colore
      m_MainWindow.m_brCurrentColor = CTSA1Btn.Background
      Dim ChooseColor As New ChooseColor(m_MainWindow)
      If ChooseColor.ShowDialog() Then
         CTSA1Btn.Background = m_MainWindow.m_brCurrentColor
         WriteCTSA(CTSA1Btn, CTSA1InclinazioneTxBx.Text, CTSA1TalloneTxBx.Text, CTSA1EnableChBx.IsChecked, 1)
      End If
   End Sub

   Private Sub CTSA2Btn_Click(sender As Object, e As RoutedEventArgs) Handles CTSA2Btn.Click
      m_MainWindow.m_brCurrentColor = CTSA2Btn.Background
      Dim ChooseColor As New ChooseColor(m_MainWindow)
      If ChooseColor.ShowDialog() Then
         CTSA2Btn.Background = m_MainWindow.m_brCurrentColor
         WriteCTSA(CTSA2Btn, CTSA2InclinazioneTxBx.Text, CTSA2TalloneTxBx.Text, CTSA2EnableChBx.IsChecked, 2)
      End If
   End Sub

   Private Sub CTSA3Btn_Click(sender As Object, e As RoutedEventArgs) Handles CTSA3Btn.Click
      m_MainWindow.m_brCurrentColor = CTSA3Btn.Background
      Dim ChooseColor As New ChooseColor(m_MainWindow)
      If ChooseColor.ShowDialog() Then
         CTSA3Btn.Background = m_MainWindow.m_brCurrentColor
         WriteCTSA(CTSA3Btn, CTSA3InclinazioneTxBx.Text, CTSA3TalloneTxBx.Text, CTSA3EnableChBx.IsChecked, 3)
      End If
   End Sub

   Private Sub CTSA4Btn_Click(sender As Object, e As RoutedEventArgs) Handles CTSA4Btn.Click
      m_MainWindow.m_brCurrentColor = CTSA4Btn.Background
      Dim ChooseColor As New ChooseColor(m_MainWindow)
      If ChooseColor.ShowDialog() Then
         CTSA4Btn.Background = m_MainWindow.m_brCurrentColor
         WriteCTSA(CTSA4Btn, CTSA4InclinazioneTxBx.Text, CTSA4TalloneTxBx.Text, CTSA4EnableChBx.IsChecked, 4)
      End If
   End Sub

   Private Sub CTSA5Btn_Click(sender As Object, e As RoutedEventArgs) Handles CTSA5Btn.Click
      m_MainWindow.m_brCurrentColor = CTSA5Btn.Background
      Dim ChooseColor As New ChooseColor(m_MainWindow)
      If ChooseColor.ShowDialog() Then
         CTSA5Btn.Background = m_MainWindow.m_brCurrentColor
         WriteCTSA(CTSA5Btn, CTSA5InclinazioneTxBx.Text, CTSA5TalloneTxBx.Text, CTSA5EnableChBx.IsChecked, 5)
      End If
   End Sub

   Private Sub CTSA6Btn_Click(sender As Object, e As RoutedEventArgs) Handles CTSA6Btn.Click
      m_MainWindow.m_brCurrentColor = CTSA6Btn.Background
      Dim ChooseColor As New ChooseColor(m_MainWindow)
      If ChooseColor.ShowDialog() Then
         CTSA6Btn.Background = m_MainWindow.m_brCurrentColor
         WriteCTSA(CTSA6Btn, CTSA6InclinazioneTxBx.Text, CTSA6TalloneTxBx.Text, CTSA6EnableChBx.IsChecked, 6)
      End If
   End Sub

   Private Sub CTSA7Btn_Click(sender As Object, e As RoutedEventArgs) Handles CTSA7Btn.Click
      m_MainWindow.m_brCurrentColor = CTSA7Btn.Background
      Dim ChooseColor As New ChooseColor(m_MainWindow)
      If ChooseColor.ShowDialog() Then
         CTSA7Btn.Background = m_MainWindow.m_brCurrentColor
         WriteCTSA(CTSA7Btn, CTSA7InclinazioneTxBx.Text, CTSA7TalloneTxBx.Text, CTSA7EnableChBx.IsChecked, 7)
      End If
   End Sub

   Private Sub CTSA8Btn_Click(sender As Object, e As RoutedEventArgs) Handles CTSA8Btn.Click
      m_MainWindow.m_brCurrentColor = CTSA8Btn.Background
      Dim ChooseColor As New ChooseColor(m_MainWindow)
      If ChooseColor.ShowDialog() Then
         CTSA8Btn.Background = m_MainWindow.m_brCurrentColor
         WriteCTSA(CTSA8Btn, CTSA8InclinazioneTxBx.Text, CTSA8TalloneTxBx.Text, CTSA8EnableChBx.IsChecked, 8)
      End If
   End Sub

   Private Sub CTSA9Btn_Click(sender As Object, e As RoutedEventArgs) Handles CTSA9Btn.Click
      m_MainWindow.m_brCurrentColor = CTSA9Btn.Background
      Dim ChooseColor As New ChooseColor(m_MainWindow)
      If ChooseColor.ShowDialog() Then
         CTSA9Btn.Background = m_MainWindow.m_brCurrentColor
         WriteCTSA(CTSA9Btn, CTSA9InclinazioneTxBx.Text, CTSA9TalloneTxBx.Text, CTSA9EnableChBx.IsChecked, 9)
      End If
   End Sub

   Private Sub CTSA10Btn_Click(sender As Object, e As RoutedEventArgs) Handles CTSA10Btn.Click
      m_MainWindow.m_brCurrentColor = CTSA10Btn.Background
      Dim ChooseColor As New ChooseColor(m_MainWindow)
      If ChooseColor.ShowDialog() Then
         CTSA10Btn.Background = m_MainWindow.m_brCurrentColor
         WriteCTSA(CTSA10Btn, CTSA10InclinazioneTxBx.Text, CTSA10TalloneTxBx.Text, CTSA10EnableChBx.IsChecked, 10)
      End If
   End Sub

   Private Sub CTSA1EnableChBx_Click(sender As Object, e As RoutedEventArgs) Handles CTSA1EnableChBx.Click
      WriteCTSA(CTSA1Btn, CTSA1InclinazioneTxBx.Text, CTSA1TalloneTxBx.Text, CTSA1EnableChBx.IsChecked, 1)
   End Sub

   Private Sub CTSA2EnableChBx_Click(sender As Object, e As RoutedEventArgs) Handles CTSA2EnableChBx.Click
      WriteCTSA(CTSA2Btn, CTSA2InclinazioneTxBx.Text, CTSA2TalloneTxBx.Text, CTSA2EnableChBx.IsChecked, 2)
   End Sub

   Private Sub CTSA3EnableChBx_Click(sender As Object, e As RoutedEventArgs) Handles CTSA3EnableChBx.Click
      WriteCTSA(CTSA3Btn, CTSA3InclinazioneTxBx.Text, CTSA3TalloneTxBx.Text, CTSA3EnableChBx.IsChecked, 3)
   End Sub

   Private Sub CTSA4EnableChBx_Click(sender As Object, e As RoutedEventArgs) Handles CTSA4EnableChBx.Click
      WriteCTSA(CTSA4Btn, CTSA4InclinazioneTxBx.Text, CTSA4TalloneTxBx.Text, CTSA4EnableChBx.IsChecked, 4)
   End Sub

   Private Sub CTSA5EnableChBx_Click(sender As Object, e As RoutedEventArgs) Handles CTSA5EnableChBx.Click
      WriteCTSA(CTSA5Btn, CTSA5InclinazioneTxBx.Text, CTSA5TalloneTxBx.Text, CTSA5EnableChBx.IsChecked, 5)
   End Sub

   Private Sub CTSA6EnableChBx_Click(sender As Object, e As RoutedEventArgs) Handles CTSA6EnableChBx.Click
      WriteCTSA(CTSA6Btn, CTSA6InclinazioneTxBx.Text, CTSA6TalloneTxBx.Text, CTSA6EnableChBx.IsChecked, 6)
   End Sub

   Private Sub CTSA7EnableChBx_Click(sender As Object, e As RoutedEventArgs) Handles CTSA7EnableChBx.Click
      WriteCTSA(CTSA7Btn, CTSA7InclinazioneTxBx.Text, CTSA7TalloneTxBx.Text, CTSA7EnableChBx.IsChecked, 7)
   End Sub

   Private Sub CTSA8EnableChBx_Click(sender As Object, e As RoutedEventArgs) Handles CTSA8EnableChBx.Click
      WriteCTSA(CTSA8Btn, CTSA8InclinazioneTxBx.Text, CTSA8TalloneTxBx.Text, CTSA8EnableChBx.IsChecked, 8)
   End Sub

   Private Sub CTSA9EnableChBx_Click(sender As Object, e As RoutedEventArgs) Handles CTSA9EnableChBx.Click
      WriteCTSA(CTSA9Btn, CTSA9InclinazioneTxBx.Text, CTSA9TalloneTxBx.Text, CTSA9EnableChBx.IsChecked, 9)
   End Sub

   Private Sub CTSA10EnableChBx_Click(sender As Object, e As RoutedEventArgs) Handles CTSA10EnableChBx.Click
      WriteCTSA(CTSA10Btn, CTSA10InclinazioneTxBx.Text, CTSA10TalloneTxBx.Text, CTSA10EnableChBx.IsChecked, 10)
   End Sub

   Private Sub CTSA1TalloneTxBx_TextChanged(sender As Object, e As TextChangedEventArgs) Handles CTSA1TalloneTxBx.TextChanged
      Dim dHeel As Double
      If Not StringToLen(CTSA1TalloneTxBx.Text, dHeel) Then
         CTSA1TalloneTxBx.Text = ""
      ElseIf dHeel < 0 Then
         CTSA1TalloneTxBx.Text = LenToString(Math.Abs(dHeel), 4)
      End If
      CTSA1EnableChBx_Click(sender, e)
   End Sub

   Private Sub CTSA2TalloneTxBx_TextChanged(sender As Object, e As TextChangedEventArgs) Handles CTSA2TalloneTxBx.TextChanged
      Dim dHeel As Double
      If Not StringToLen(CTSA2TalloneTxBx.Text, dHeel) Then
         CTSA2TalloneTxBx.Text = ""
      ElseIf dHeel < 0 Then
         CTSA2TalloneTxBx.Text = LenToString(Math.Abs(dHeel), 4)
      End If
      CTSA2EnableChBx_Click(sender, e)
   End Sub

   Private Sub CTSA3TalloneTxBx_TextChanged(sender As Object, e As TextChangedEventArgs) Handles CTSA3TalloneTxBx.TextChanged
      Dim dHeel As Double
      If Not StringToLen(CTSA3TalloneTxBx.Text, dHeel) Then
         CTSA3TalloneTxBx.Text = ""
      ElseIf dHeel < 0 Then
         CTSA3TalloneTxBx.Text = LenToString(Math.Abs(dHeel), 4)
      End If
      CTSA3EnableChBx_Click(sender, e)
   End Sub

   Private Sub CTSA4TalloneTxBx_TextChanged(sender As Object, e As TextChangedEventArgs) Handles CTSA4TalloneTxBx.TextChanged
      Dim dHeel As Double
      If Not StringToLen(CTSA4TalloneTxBx.Text, dHeel) Then
         CTSA4TalloneTxBx.Text = ""
      ElseIf dHeel < 0 Then
         CTSA4TalloneTxBx.Text = LenToString(Math.Abs(dHeel), 4)
      End If
      CTSA4EnableChBx_Click(sender, e)
   End Sub

   Private Sub CTSA5TalloneTxBx_TextChanged(sender As Object, e As TextChangedEventArgs) Handles CTSA5TalloneTxBx.TextChanged
      Dim dHeel As Double
      If Not StringToLen(CTSA5TalloneTxBx.Text, dHeel) Then
         CTSA5TalloneTxBx.Text = ""
      ElseIf dHeel < 0 Then
         CTSA5TalloneTxBx.Text = LenToString(Math.Abs(dHeel), 4)
      End If
      CTSA5EnableChBx_Click(sender, e)
   End Sub

   Private Sub CTSA6TalloneTxBx_TextChanged(sender As Object, e As TextChangedEventArgs) Handles CTSA6TalloneTxBx.TextChanged
      Dim dHeel As Double
      If Not StringToLen(CTSA6TalloneTxBx.Text, dHeel) Then
         CTSA6TalloneTxBx.Text = ""
      ElseIf dHeel < 0 Then
         CTSA6TalloneTxBx.Text = LenToString(Math.Abs(dHeel), 4)
      End If
      CTSA6EnableChBx_Click(sender, e)
   End Sub

   Private Sub CTSA7TalloneTxBx_TextChanged(sender As Object, e As TextChangedEventArgs) Handles CTSA7TalloneTxBx.TextChanged
      Dim dHeel As Double
      If Not StringToLen(CTSA7TalloneTxBx.Text, dHeel) Then
         CTSA7TalloneTxBx.Text = ""
      ElseIf dHeel < 0 Then
         CTSA7TalloneTxBx.Text = LenToString(Math.Abs(dHeel), 4)
      End If
      CTSA7EnableChBx_Click(sender, e)
   End Sub

   Private Sub CTSA8TalloneTxBx_TextChanged(sender As Object, e As TextChangedEventArgs) Handles CTSA8TalloneTxBx.TextChanged
      Dim dHeel As Double
      If Not StringToLen(CTSA8TalloneTxBx.Text, dHeel) Then
         CTSA8TalloneTxBx.Text = ""
      ElseIf dHeel < 0 Then
         CTSA8TalloneTxBx.Text = LenToString(Math.Abs(dHeel), 4)
      End If
      CTSA8EnableChBx_Click(sender, e)
   End Sub

   Private Sub CTSA9TalloneTxBx_TextChanged(sender As Object, e As TextChangedEventArgs) Handles CTSA9TalloneTxBx.TextChanged
      Dim dHeel As Double
      If Not StringToLen(CTSA9TalloneTxBx.Text, dHeel) Then
         CTSA9TalloneTxBx.Text = ""
      ElseIf dHeel < 0 Then
         CTSA9TalloneTxBx.Text = LenToString(Math.Abs(dHeel), 4)
      End If
      CTSA9EnableChBx_Click(sender, e)
   End Sub

   Private Sub CTSA10TalloneTxBx_TextChanged(sender As Object, e As TextChangedEventArgs) Handles CTSA10TalloneTxBx.TextChanged
      Dim dHeel As Double
      If Not StringToLen(CTSA10TalloneTxBx.Text, dHeel) Then
         CTSA10TalloneTxBx.Text = ""
      ElseIf dHeel < 0 Then
         CTSA10TalloneTxBx.Text = LenToString(Math.Abs(dHeel), 4)
      End If
      CTSA10EnableChBx_Click(sender, e)
   End Sub

   Private Sub CTSA1InclinazioneTxBx_TextChanged(sender As Object, e As TextChangedEventArgs) Handles CTSA1InclinazioneTxBx.TextChanged
      If CTSA1InclinazioneTxBx.Text <> "-" Then
         Dim dAng As Double : If Not StringToLen(CTSA1InclinazioneTxBx.Text, dAng) Then CTSA1InclinazioneTxBx.Text = ""
      End If
      CTSA1EnableChBx_Click(sender, e)
   End Sub

   Private Sub CTSA2InclinazioneTxBx_TextChanged(sender As Object, e As TextChangedEventArgs) Handles CTSA2InclinazioneTxBx.TextChanged
      If CTSA2InclinazioneTxBx.Text <> "-" Then
         Dim dAng As Double : If Not StringToLen(CTSA2InclinazioneTxBx.Text, dAng) Then CTSA2InclinazioneTxBx.Text = ""
      End If
      CTSA2EnableChBx_Click(sender, e)
   End Sub

   Private Sub CTSA3InclinazioneTxBx_TextChanged(sender As Object, e As TextChangedEventArgs) Handles CTSA3InclinazioneTxBx.TextChanged
      If CTSA3InclinazioneTxBx.Text <> "-" Then
         Dim dAng As Double : If Not StringToLen(CTSA3InclinazioneTxBx.Text, dAng) Then CTSA3InclinazioneTxBx.Text = ""
      End If
      CTSA3EnableChBx_Click(sender, e)
   End Sub

   Private Sub CTSA4InclinazioneTxBx_TextChanged(sender As Object, e As TextChangedEventArgs) Handles CTSA4InclinazioneTxBx.TextChanged
      If CTSA4InclinazioneTxBx.Text <> "-" Then
         Dim dAng As Double : If Not StringToLen(CTSA4InclinazioneTxBx.Text, dAng) Then CTSA4InclinazioneTxBx.Text = ""
      End If
      CTSA4EnableChBx_Click(sender, e)
   End Sub

   Private Sub CTSA5InclinazioneTxBx_TextChanged(sender As Object, e As TextChangedEventArgs) Handles CTSA5InclinazioneTxBx.TextChanged
      If CTSA5InclinazioneTxBx.Text <> "-" Then
         Dim dAng As Double : If Not StringToLen(CTSA5InclinazioneTxBx.Text, dAng) Then CTSA5InclinazioneTxBx.Text = ""
      End If
      CTSA5EnableChBx_Click(sender, e)
   End Sub

   Private Sub CTSA6InclinazioneTxBx_TextChanged(sender As Object, e As TextChangedEventArgs) Handles CTSA6InclinazioneTxBx.TextChanged
      If CTSA6InclinazioneTxBx.Text <> "-" Then
         Dim dAng As Double : If Not StringToLen(CTSA6InclinazioneTxBx.Text, dAng) Then CTSA6InclinazioneTxBx.Text = ""
      End If
      CTSA6EnableChBx_Click(sender, e)
   End Sub

   Private Sub CTSA7InclinazioneTxBx_TextChanged(sender As Object, e As TextChangedEventArgs) Handles CTSA7InclinazioneTxBx.TextChanged
      If CTSA7InclinazioneTxBx.Text <> "-" Then
         Dim dAng As Double : If Not StringToLen(CTSA7InclinazioneTxBx.Text, dAng) Then CTSA7InclinazioneTxBx.Text = ""
      End If
      CTSA7EnableChBx_Click(sender, e)
   End Sub

   Private Sub CTSA8InclinazioneTxBx_TextChanged(sender As Object, e As TextChangedEventArgs) Handles CTSA8InclinazioneTxBx.TextChanged
      If CTSA8InclinazioneTxBx.Text <> "-" Then
         Dim dAng As Double : If Not StringToLen(CTSA8InclinazioneTxBx.Text, dAng) Then CTSA8InclinazioneTxBx.Text = ""
      End If
      CTSA8EnableChBx_Click(sender, e)
   End Sub

   Private Sub CTSA9InclinazioneTxBx_TextChanged(sender As Object, e As TextChangedEventArgs) Handles CTSA9InclinazioneTxBx.TextChanged
      If CTSA9InclinazioneTxBx.Text <> "-" Then
         Dim dAng As Double : If Not StringToLen(CTSA9InclinazioneTxBx.Text, dAng) Then CTSA9InclinazioneTxBx.Text = ""
      End If
      CTSA9EnableChBx_Click(sender, e)
   End Sub

   Private Sub CTSA10InclinazioneTxBx_TextChanged(sender As Object, e As TextChangedEventArgs) Handles CTSA10InclinazioneTxBx.TextChanged
      If CTSA10InclinazioneTxBx.Text <> "-" Then
         Dim dAng As Double : If Not StringToLen(CTSA10InclinazioneTxBx.Text, dAng) Then CTSA10InclinazioneTxBx.Text = ""
      End If
      CTSA10EnableChBx_Click(sender, e)
   End Sub

   Private Sub ToleranceMsgTxBx_TextChanged(sender As Object, e As TextChangedEventArgs) Handles ToleranceMsgTxBx.TextChanged
      Dim nToler As Integer
      If Not StringToInt(ToleranceMsgTxBx.Text, nToler) Then
         nToler = 25
         ToleranceMsgTxBx.Text = nToler.ToString()
      Else
         If nToler > 50 Then
            nToler = 50
            ToleranceMsgTxBx.Text = nToler.ToString()
         ElseIf nToler < 0 Then
            nToler = 0
            ToleranceMsgTxBx.Text = nToler.ToString()
         End If
      End If
      WritePrivateProfileString(S_COLORTOSIDEANG, K_CTSA_TOLERANCE, nToler.ToString(), m_MainWindow.GetIniFile())
   End Sub

   Private Sub SlabLayerMsgTxBx_TextChanged(sender As Object, e As TextChangedEventArgs) Handles SlabLayerMsgTxBx.TextChanged
      WritePrivateProfileString(S_SLABDXF, K_SLABLAYER, SlabLayerMsgTxBx.Text, m_MainWindow.GetIniFile())
   End Sub

   Private Sub PartsLayerMsgTxBx_TextChanged(sender As Object, e As TextChangedEventArgs) Handles PartsLayerMsgTxBx.TextChanged
      WritePrivateProfileString(S_SLABDXF, K_PARTSLAYER, PartsLayerMsgTxBx.Text, m_MainWindow.GetIniFile())
   End Sub

   Private Sub ScrapLayerMsgTxBx_TextChanged(sender As Object, e As TextChangedEventArgs) Handles ScrapLayerMsgTxBx.TextChanged
      WritePrivateProfileString(S_SLABDXF, K_SCRAPLAYER, ScrapLayerMsgTxBx.Text, m_MainWindow.GetIniFile())
   End Sub

   Private Sub StdThickMsgTxBx_TextChanged(sender As Object, e As TextChangedEventArgs) Handles StdThickMsgTxBx.TextChanged
      Dim dThick As Double
      If Not StringToLen(StdThickMsgTxBx.Text, dThick) Then
         dThick = 25.4
         StdThickMsgTxBx.Text = LenToString(dThick, 3)
      End If
      WritePrivateProfileString(S_SLABDXF, K_STDTHICK, DoubleToString(dThick, 3), m_MainWindow.GetIniFile())
   End Sub

   Private Sub CTSAboxEnableChBx_Click(sender As Object, e As RoutedEventArgs) Handles CTSAboxEnableChBx.Click
      If CTSAboxEnableChBx.IsChecked = True Then
         WritePrivateProfileString(S_COLORTOSIDEANG, K_CTSA_ENABLE, 1, m_MainWindow.GetIniFile())
      ElseIf CTSAboxEnableChBx.IsChecked = False Then
         WritePrivateProfileString(S_COLORTOSIDEANG, K_CTSA_ENABLE, 0, m_MainWindow.GetIniFile())
      End If
   End Sub

#Region "ENGRAVE COLOR"

   '-------------- Riga 1 --------------
   Private Sub CTE1EnableChBx_Click(sender As Object, e As RoutedEventArgs) Handles CTE1EnableChBx.Click
      WriteCTE(CTE1Btn, CTE1AffondamentoTxBx.Text, CTE1LarghezzaTxBx.Text, CTE1EnableChBx.IsChecked, 1)
   End Sub

   Private Sub CTE1Btn_Click(sender As Object, e As RoutedEventArgs) Handles CTE1Btn.Click
      ' Il click sul bottone aprirà la finestra ChooseColor che permetterà la scelta del colore
      m_MainWindow.m_brCurrentColor = CTE1Btn.Background
      Dim ChooseColor As New ChooseColor(m_MainWindow)
      If ChooseColor.ShowDialog() Then
         CTE1Btn.Background = m_MainWindow.m_brCurrentColor
         WriteCTE(CTE1Btn, CTE1AffondamentoTxBx.Text, CTE1LarghezzaTxBx.Text, CTE1EnableChBx.IsChecked, 1)
      End If
   End Sub

   Private Sub CTE1AffondamentoTxBx_TextChanged(sender As Object, e As TextChangedEventArgs) Handles CTE1AffondamentoTxBx.TextChanged
      Dim dAff As Double
      If Not StringToLen(CTE1AffondamentoTxBx.Text, dAff) Then
         CTE1AffondamentoTxBx.Text = ""
      Else
         CTE1AffondamentoTxBx.Text = LenToString(Math.Abs(dAff), 4)
      End If
      CTE1EnableChBx_Click(sender, e)
   End Sub

   Private Sub CTE1ALarghezzaTxBx_TextChanged(sender As Object, e As TextChangedEventArgs) Handles CTE1LarghezzaTxBx.TextChanged
      Dim dLar As Double
      If Not StringToLen(CTE1LarghezzaTxBx.Text, dLar) Then
         CTE1LarghezzaTxBx.Text = ""
      Else
         CTE1LarghezzaTxBx.Text = LenToString(Math.Abs(dLar), 4)
      End If
      CTE1EnableChBx_Click(sender, e)
   End Sub

   '-------------- Riga 2 --------------
   Private Sub CTE2EnableChBx_Click(sender As Object, e As RoutedEventArgs) Handles CTE2EnableChBx.Click
      WriteCTE(CTE2Btn, CTE2AffondamentoTxBx.Text, CTE2LarghezzaTxBx.Text, CTE2EnableChBx.IsChecked, 2)
   End Sub

   Private Sub CTE2Btn_Click(sender As Object, e As RoutedEventArgs) Handles CTE2Btn.Click
      ' Il click sul bottone aprirà la finestra ChooseColor che permetterà la scelta del colore
      m_MainWindow.m_brCurrentColor = CTE2Btn.Background
      Dim ChooseColor As New ChooseColor(m_MainWindow)
      If ChooseColor.ShowDialog() Then
         CTE2Btn.Background = m_MainWindow.m_brCurrentColor
         WriteCTE(CTE2Btn, CTE2AffondamentoTxBx.Text, CTE2LarghezzaTxBx.Text, CTE2EnableChBx.IsChecked, 2)
      End If
   End Sub

   Private Sub CTE2AffondamentoTxBx_TextChanged(sender As Object, e As TextChangedEventArgs) Handles CTE2AffondamentoTxBx.TextChanged
      Dim dAff As Double
      If Not StringToLen(CTE2AffondamentoTxBx.Text, dAff) Then
         CTE2AffondamentoTxBx.Text = ""
      Else
         CTE2AffondamentoTxBx.Text = LenToString(Math.Abs(dAff), 4)
      End If
      CTE2EnableChBx_Click(sender, e)
   End Sub

   Private Sub CTE2ALarghezzaTxBx_TextChanged(sender As Object, e As TextChangedEventArgs) Handles CTE2LarghezzaTxBx.TextChanged
      Dim dLar As Double
      If Not StringToLen(CTE2LarghezzaTxBx.Text, dLar) Then
         CTE2LarghezzaTxBx.Text = ""
      Else
         CTE2LarghezzaTxBx.Text = LenToString(Math.Abs(dLar), 4)
      End If
      CTE2EnableChBx_Click(sender, e)
   End Sub

   '-------------- Riga 3 --------------
   Private Sub CTE3EnableChBx_Click(sender As Object, e As RoutedEventArgs) Handles CTE3EnableChBx.Click
      WriteCTE(CTE3Btn, CTE3AffondamentoTxBx.Text, CTE3LarghezzaTxBx.Text, CTE3EnableChBx.IsChecked, 3)
   End Sub

   Private Sub CTE3Btn_Click(sender As Object, e As RoutedEventArgs) Handles CTE3Btn.Click
      ' Il click sul bottone aprirà la finestra ChooseColor che permetterà la scelta del colore
      m_MainWindow.m_brCurrentColor = CTE3Btn.Background
      Dim ChooseColor As New ChooseColor(m_MainWindow)
      If ChooseColor.ShowDialog() Then
         CTE3Btn.Background = m_MainWindow.m_brCurrentColor
         WriteCTE(CTE3Btn, CTE3AffondamentoTxBx.Text, CTE3LarghezzaTxBx.Text, CTE3EnableChBx.IsChecked, 3)
      End If
   End Sub

   Private Sub CTE3AffondamentoTxBx_TextChanged(sender As Object, e As TextChangedEventArgs) Handles CTE3AffondamentoTxBx.TextChanged
      Dim dAff As Double
      If Not StringToLen(CTE3AffondamentoTxBx.Text, dAff) Then
         CTE3AffondamentoTxBx.Text = ""
      Else
         CTE3AffondamentoTxBx.Text = LenToString(Math.Abs(dAff), 4)
      End If
      CTE3EnableChBx_Click(sender, e)
   End Sub

   Private Sub CTE3ALarghezzaTxBx_TextChanged(sender As Object, e As TextChangedEventArgs) Handles CTE3LarghezzaTxBx.TextChanged
      Dim dLar As Double
      If Not StringToLen(CTE3LarghezzaTxBx.Text, dLar) Then
         CTE3LarghezzaTxBx.Text = ""
      Else
         CTE3LarghezzaTxBx.Text = LenToString(Math.Abs(dLar), 4)
      End If
      CTE3EnableChBx_Click(sender, e)
   End Sub

   '-------------- Riga 4 --------------
   Private Sub CTE4EnableChBx_Click(sender As Object, e As RoutedEventArgs) Handles CTE4EnableChBx.Click
      WriteCTE(CTE4Btn, CTE4AffondamentoTxBx.Text, CTE4LarghezzaTxBx.Text, CTE4EnableChBx.IsChecked, 4)
   End Sub

   Private Sub CTE4Btn_Click(sender As Object, e As RoutedEventArgs) Handles CTE4Btn.Click
      ' Il click sul bottone aprirà la finestra ChooseColor che permetterà la scelta del colore
      m_MainWindow.m_brCurrentColor = CTE4Btn.Background
      Dim ChooseColor As New ChooseColor(m_MainWindow)
      If ChooseColor.ShowDialog() Then
         CTE4Btn.Background = m_MainWindow.m_brCurrentColor
         WriteCTE(CTE4Btn, CTE4AffondamentoTxBx.Text, CTE4LarghezzaTxBx.Text, CTE4EnableChBx.IsChecked, 4)
      End If
   End Sub

   Private Sub CTE4AffondamentoTxBx_TextChanged(sender As Object, e As TextChangedEventArgs) Handles CTE4AffondamentoTxBx.TextChanged
      Dim dAff As Double
      If Not StringToLen(CTE4AffondamentoTxBx.Text, dAff) Then
         CTE4AffondamentoTxBx.Text = ""
      Else
         CTE4AffondamentoTxBx.Text = LenToString(Math.Abs(dAff), 4)
      End If
      CTE4EnableChBx_Click(sender, e)
   End Sub

   Private Sub CTE4ALarghezzaTxBx_TextChanged(sender As Object, e As TextChangedEventArgs) Handles CTE4LarghezzaTxBx.TextChanged
      Dim dLar As Double
      If Not StringToLen(CTE4LarghezzaTxBx.Text, dLar) Then
         CTE4LarghezzaTxBx.Text = ""
      Else
         CTE4LarghezzaTxBx.Text = LenToString(Math.Abs(dLar), 4)
      End If
      CTE4EnableChBx_Click(sender, e)
   End Sub

   '-------------- Riga 5 --------------
   Private Sub CTE5EnableChBx_Click(sender As Object, e As RoutedEventArgs) Handles CTE5EnableChBx.Click
      WriteCTE(CTE5Btn, CTE5AffondamentoTxBx.Text, CTE5LarghezzaTxBx.Text, CTE5EnableChBx.IsChecked, 5)
   End Sub

   Private Sub CTE5Btn_Click(sender As Object, e As RoutedEventArgs) Handles CTE5Btn.Click
      ' Il click sul bottone aprirà la finestra ChooseColor che permetterà la scelta del colore
      m_MainWindow.m_brCurrentColor = CTE5Btn.Background
      Dim ChooseColor As New ChooseColor(m_MainWindow)
      If ChooseColor.ShowDialog() Then
         CTE5Btn.Background = m_MainWindow.m_brCurrentColor
         WriteCTE(CTE5Btn, CTE5AffondamentoTxBx.Text, CTE5LarghezzaTxBx.Text, CTE5EnableChBx.IsChecked, 5)
      End If
   End Sub

   Private Sub CTE5AffondamentoTxBx_TextChanged(sender As Object, e As TextChangedEventArgs) Handles CTE5AffondamentoTxBx.TextChanged
      Dim dAff As Double
      If Not StringToLen(CTE5AffondamentoTxBx.Text, dAff) Then
         CTE5AffondamentoTxBx.Text = ""
      Else
         CTE5AffondamentoTxBx.Text = LenToString(Math.Abs(dAff), 4)
      End If
      CTE5EnableChBx_Click(sender, e)
   End Sub

   Private Sub CTE5ALarghezzaTxBx_TextChanged(sender As Object, e As TextChangedEventArgs) Handles CTE5LarghezzaTxBx.TextChanged
      Dim dLar As Double
      If Not StringToLen(CTE5LarghezzaTxBx.Text, dLar) Then
         CTE5LarghezzaTxBx.Text = ""
      Else
         CTE5LarghezzaTxBx.Text = LenToString(Math.Abs(dLar), 4)
      End If
      CTE5EnableChBx_Click(sender, e)
   End Sub

   '-------------- Riga 6 --------------
   Private Sub CTE6EnableChBx_Click(sender As Object, e As RoutedEventArgs) Handles CTE6EnableChBx.Click
      WriteCTE(CTE6Btn, CTE6AffondamentoTxBx.Text, CTE6LarghezzaTxBx.Text, CTE6EnableChBx.IsChecked, 6)
   End Sub

   Private Sub CTE6Btn_Click(sender As Object, e As RoutedEventArgs) Handles CTE6Btn.Click
      ' Il click sul bottone aprirà la finestra ChooseColor che permetterà la scelta del colore
      m_MainWindow.m_brCurrentColor = CTE6Btn.Background
      Dim ChooseColor As New ChooseColor(m_MainWindow)
      If ChooseColor.ShowDialog() Then
         CTE6Btn.Background = m_MainWindow.m_brCurrentColor
         WriteCTE(CTE6Btn, CTE6AffondamentoTxBx.Text, CTE6LarghezzaTxBx.Text, CTE6EnableChBx.IsChecked, 6)
      End If
   End Sub

   Private Sub CTE6AffondamentoTxBx_TextChanged(sender As Object, e As TextChangedEventArgs) Handles CTE6AffondamentoTxBx.TextChanged
      Dim dAff As Double
      If Not StringToLen(CTE6AffondamentoTxBx.Text, dAff) Then
         CTE6AffondamentoTxBx.Text = ""
      Else
         CTE6AffondamentoTxBx.Text = LenToString(Math.Abs(dAff), 4)
      End If
      CTE6EnableChBx_Click(sender, e)
   End Sub

   Private Sub CTE6ALarghezzaTxBx_TextChanged(sender As Object, e As TextChangedEventArgs) Handles CTE6LarghezzaTxBx.TextChanged
      Dim dLar As Double
      If Not StringToLen(CTE6LarghezzaTxBx.Text, dLar) Then
         CTE6LarghezzaTxBx.Text = ""
      Else
         CTE6LarghezzaTxBx.Text = LenToString(Math.Abs(dLar), 4)
      End If
      CTE6EnableChBx_Click(sender, e)
   End Sub

   '-------------- Riga 7 --------------
   Private Sub CTE7EnableChBx_Click(sender As Object, e As RoutedEventArgs) Handles CTE7EnableChBx.Click
      WriteCTE(CTE7Btn, CTE7AffondamentoTxBx.Text, CTE7LarghezzaTxBx.Text, CTE7EnableChBx.IsChecked, 7)
   End Sub

   Private Sub CTE7Btn_Click(sender As Object, e As RoutedEventArgs) Handles CTE7Btn.Click
      ' Il click sul bottone aprirà la finestra ChooseColor che permetterà la scelta del colore
      m_MainWindow.m_brCurrentColor = CTE7Btn.Background
      Dim ChooseColor As New ChooseColor(m_MainWindow)
      If ChooseColor.ShowDialog() Then
         CTE7Btn.Background = m_MainWindow.m_brCurrentColor
         WriteCTE(CTE7Btn, CTE7AffondamentoTxBx.Text, CTE7LarghezzaTxBx.Text, CTE7EnableChBx.IsChecked, 7)
      End If
   End Sub

   Private Sub CTE7AffondamentoTxBx_TextChanged(sender As Object, e As TextChangedEventArgs) Handles CTE7AffondamentoTxBx.TextChanged
      Dim dAff As Double
      If Not StringToLen(CTE7AffondamentoTxBx.Text, dAff) Then
         CTE7AffondamentoTxBx.Text = ""
      Else
         CTE7AffondamentoTxBx.Text = LenToString(Math.Abs(dAff), 4)
      End If
      CTE7EnableChBx_Click(sender, e)
   End Sub

   Private Sub CTE7ALarghezzaTxBx_TextChanged(sender As Object, e As TextChangedEventArgs) Handles CTE7LarghezzaTxBx.TextChanged
      Dim dLar As Double
      If Not StringToLen(CTE7LarghezzaTxBx.Text, dLar) Then
         CTE7LarghezzaTxBx.Text = ""
      Else
         CTE7LarghezzaTxBx.Text = LenToString(Math.Abs(dLar), 4)
      End If
      CTE7EnableChBx_Click(sender, e)
   End Sub

   '-------------- Riga 8 --------------
   Private Sub CTE8EnableChBx_Click(sender As Object, e As RoutedEventArgs) Handles CTE8EnableChBx.Click
      WriteCTE(CTE8Btn, CTE8AffondamentoTxBx.Text, CTE8LarghezzaTxBx.Text, CTE8EnableChBx.IsChecked, 8)
   End Sub

   Private Sub CTE8Btn_Click(sender As Object, e As RoutedEventArgs) Handles CTE8Btn.Click
      ' Il click sul bottone aprirà la finestra ChooseColor che permetterà la scelta del colore
      m_MainWindow.m_brCurrentColor = CTE8Btn.Background
      Dim ChooseColor As New ChooseColor(m_MainWindow)
      If ChooseColor.ShowDialog() Then
         CTE8Btn.Background = m_MainWindow.m_brCurrentColor
         WriteCTE(CTE8Btn, CTE8AffondamentoTxBx.Text, CTE8LarghezzaTxBx.Text, CTE8EnableChBx.IsChecked, 8)
      End If
   End Sub

   Private Sub CTE8AffondamentoTxBx_TextChanged(sender As Object, e As TextChangedEventArgs) Handles CTE8AffondamentoTxBx.TextChanged
      Dim dAff As Double
      If Not StringToLen(CTE8AffondamentoTxBx.Text, dAff) Then
         CTE8AffondamentoTxBx.Text = ""
      Else
         CTE8AffondamentoTxBx.Text = LenToString(Math.Abs(dAff), 4)
      End If
      CTE8EnableChBx_Click(sender, e)
   End Sub

   Private Sub CTE8ALarghezzaTxBx_TextChanged(sender As Object, e As TextChangedEventArgs) Handles CTE8LarghezzaTxBx.TextChanged
      Dim dLar As Double
      If Not StringToLen(CTE8LarghezzaTxBx.Text, dLar) Then
         CTE8LarghezzaTxBx.Text = ""
      Else
         CTE8LarghezzaTxBx.Text = LenToString(Math.Abs(dLar), 4)
      End If
      CTE8EnableChBx_Click(sender, e)
   End Sub

   '-------------- Riga 9 --------------
   Private Sub CTE9EnableChBx_Click(sender As Object, e As RoutedEventArgs) Handles CTE9EnableChBx.Click
      WriteCTE(CTE9Btn, CTE9AffondamentoTxBx.Text, CTE9LarghezzaTxBx.Text, CTE9EnableChBx.IsChecked, 9)
   End Sub

   Private Sub CTE9Btn_Click(sender As Object, e As RoutedEventArgs) Handles CTE9Btn.Click
      ' Il click sul bottone aprirà la finestra ChooseColor che permetterà la scelta del colore
      m_MainWindow.m_brCurrentColor = CTE9Btn.Background
      Dim ChooseColor As New ChooseColor(m_MainWindow)
      If ChooseColor.ShowDialog() Then
         CTE9Btn.Background = m_MainWindow.m_brCurrentColor
         WriteCTE(CTE9Btn, CTE9AffondamentoTxBx.Text, CTE9LarghezzaTxBx.Text, CTE9EnableChBx.IsChecked, 9)
      End If
   End Sub

   Private Sub CTE9AffondamentoTxBx_TextChanged(sender As Object, e As TextChangedEventArgs) Handles CTE9AffondamentoTxBx.TextChanged
      Dim dAff As Double
      If Not StringToLen(CTE9AffondamentoTxBx.Text, dAff) Then
         CTE9AffondamentoTxBx.Text = ""
      Else
         CTE9AffondamentoTxBx.Text = LenToString(Math.Abs(dAff), 4)
      End If
      CTE9EnableChBx_Click(sender, e)
   End Sub

   Private Sub CTE9ALarghezzaTxBx_TextChanged(sender As Object, e As TextChangedEventArgs) Handles CTE9LarghezzaTxBx.TextChanged
      Dim dLar As Double
      If Not StringToLen(CTE9LarghezzaTxBx.Text, dLar) Then
         CTE9LarghezzaTxBx.Text = ""
      Else
         CTE9LarghezzaTxBx.Text = LenToString(Math.Abs(dLar), 4)
      End If
      CTE9EnableChBx_Click(sender, e)
   End Sub

   '-------------- Riga 10 --------------
   Private Sub CTE10EnableChBx_Click(sender As Object, e As RoutedEventArgs) Handles CTE10EnableChBx.Click
      WriteCTE(CTE10Btn, CTE10AffondamentoTxBx.Text, CTE10LarghezzaTxBx.Text, CTE10EnableChBx.IsChecked, 10)
   End Sub

   Private Sub CTE10Btn_Click(sender As Object, e As RoutedEventArgs) Handles CTE10Btn.Click
      ' Il click sul bottone aprirà la finestra ChooseColor che permetterà la scelta del colore
      m_MainWindow.m_brCurrentColor = CTE10Btn.Background
      Dim ChooseColor As New ChooseColor(m_MainWindow)
      If ChooseColor.ShowDialog() Then
         CTE10Btn.Background = m_MainWindow.m_brCurrentColor
         WriteCTE(CTE10Btn, CTE9AffondamentoTxBx.Text, CTE10LarghezzaTxBx.Text, CTE10EnableChBx.IsChecked, 10)
      End If
   End Sub

   Private Sub CTE10AffondamentoTxBx_TextChanged(sender As Object, e As TextChangedEventArgs) Handles CTE10AffondamentoTxBx.TextChanged
      Dim dAff As Double
      If Not StringToLen(CTE10AffondamentoTxBx.Text, dAff) Then
         CTE10AffondamentoTxBx.Text = ""
      Else
         CTE10AffondamentoTxBx.Text = LenToString(Math.Abs(dAff), 4)
      End If
      CTE10EnableChBx_Click(sender, e)
   End Sub

   Private Sub CTE10ALarghezzaTxBx_TextChanged(sender As Object, e As TextChangedEventArgs) Handles CTE10LarghezzaTxBx.TextChanged
      Dim dLar As Double
      If Not StringToLen(CTE10LarghezzaTxBx.Text, dLar) Then
         CTE10LarghezzaTxBx.Text = ""
      Else
         CTE10LarghezzaTxBx.Text = LenToString(Math.Abs(dLar), 4)
      End If
      CTE10EnableChBx_Click(sender, e)
   End Sub

   Private Sub ToleranceEMsgTxBx_TextChanged(sender As Object, e As TextChangedEventArgs) Handles ToleranceEMsgTxBx.TextChanged
      Dim nToler As Integer
      If Not StringToInt(ToleranceEMsgTxBx.Text, nToler) Then
         nToler = 25
         ToleranceEMsgTxBx.Text = nToler.ToString()
      Else
         If nToler > 50 Then
            nToler = 50
            ToleranceEMsgTxBx.Text = nToler.ToString()
         ElseIf nToler < 0 Then
            nToler = 0
            ToleranceEMsgTxBx.Text = nToler.ToString()
         End If
      End If
      WritePrivateProfileString(S_COLORTOENGRAVE, K_CTE_TOLERANCE, nToler.ToString(), m_MainWindow.GetIniFile())
   End Sub

   Private Sub CTEboxEnableChBx_Click(sender As Object, e As RoutedEventArgs) Handles CTEboxEnableChBx.Click
      If CTEboxEnableChBx.IsChecked = True Then
         WritePrivateProfileString(S_COLORTOENGRAVE, K_CTE_ENABLE, 1, m_MainWindow.GetIniFile())
      ElseIf CTEboxEnableChBx.IsChecked = False Then
         WritePrivateProfileString(S_COLORTOENGRAVE, K_CTE_ENABLE, 0, m_MainWindow.GetIniFile())
      End If
   End Sub

#End Region    'Engrave Color

   Private Sub TextColorBtn_Click(sender As Object, e As RoutedEventArgs) Handles TextColorBtn.Click
      ' Il click sul bottone aprirà la finestra ChooseColor che permetterà la scelta del colore
      m_MainWindow.m_brCurrentColor = TextColorBtn.Background
      Dim ChooseColor As New ChooseColor(m_MainWindow)
      If ChooseColor.ShowDialog() Then
         TextColorBtn.Background = m_MainWindow.m_brCurrentColor
         Dim Rvalue As String = (TextColorBtn.Background.GetValue(SolidColorBrush.ColorProperty)).R
         Dim Gvalue As String = (TextColorBtn.Background.GetValue(SolidColorBrush.ColorProperty)).G
         Dim Bvalue As String = (TextColorBtn.Background.GetValue(SolidColorBrush.ColorProperty)).B
         ' Costruisco la string RgbCTSA che corrisponderà al valore da scrivere nell'INI;
         ' in caso di valore null o " " verrà scritto nella stringa il valore 0
         Dim RgbCTSA As String = If(String.IsNullOrWhiteSpace(Rvalue), "0", Rvalue) & "," &
                                 If(String.IsNullOrWhiteSpace(Gvalue), "0", Gvalue) & "," &
                                 If(String.IsNullOrWhiteSpace(Bvalue), "0", Bvalue)
         WritePrivateProfileString(S_NEST, K_TEXTCOLOR, RgbCTSA, m_MainWindow.GetIniFile())
      End If
   End Sub

   Private Sub FeedbackBtn_Click(sender As Object, e As RoutedEventArgs) Handles FeedbackBtn.Click

      ' Recupero indirizzo a cui spedire la mail
      Dim sSupportAddress As String = String.Empty
      GetPrivateProfileString(S_GENERAL, K_SUPPORT, "support@egaltech.com", sSupportAddress, m_MainWindow.GetIniFile())
      ' se vuoto do messaggio di errore ed esco
      If String.IsNullOrWhiteSpace(sSupportAddress) Then
         Dim MissingAddress As New EgtMsgBox(m_MainWindow, EgtMsg(MSG_EGTSAVEFILE + 13), EgtMsg(MSG_EGTSAVEFILE + 10), EgtMsgBox.Buttons.OK, EgtMsgBox.Icons.NULL, 1)
         Return
      End If

      ' Recupero numero chiave
      Dim sKey As String = String.Empty
      EgtGetKeyInfo(sKey)

      ' Recupero file del progetto corrente
      Dim sCurrProject As String = String.Empty
      EgtGetCurrFilePath(sCurrProject)
      ' Verifica se progetto corrente da salvare
      Dim bPrjToSave As Boolean = False
      EgtSetCurrentContext(m_MainWindow.m_CurrentProjectPageUC.CurrentProjectScene.GetCtx())
      If EgtGetModified() Or String.IsNullOrWhiteSpace(sCurrProject) Then
         Dim SaveCurrProj As EgtMsgBox
         If m_MainWindow.IsSiemensPc() Then
            SaveCurrProj = New EgtMsgBox(m_MainWindow, Me.ActualWidth / 17 * 5, EgtMsgBox.WidthType.PIXEL, "", EgtMsg(MSG_EGTMSGBOX + 1), EgtMsgBox.Buttons.YES_NO_CANCEL, EgtMsgBox.Icons.NULL)
         Else
            SaveCurrProj = New EgtMsgBox(m_MainWindow, "", EgtMsg(MSG_EGTMSGBOX + 1), EgtMsgBox.Buttons.YES_NO_CANCEL, EgtMsgBox.Icons.NULL)
         End If
         Select Case SaveCurrProj.m_nPressedBtn
            Case 0    ' Annulla
               Return
            Case 1    ' Si
               bPrjToSave = True
            Case 2    ' No
               bPrjToSave = False
         End Select
      End If
      ' Eventuale salvataggio progetto corrente
      If bPrjToSave Then
         EgtSetCurrentContext(m_MainWindow.m_CurrentProjectPageUC.CurrentProjectScene.GetCtx())
         m_MainWindow.m_CurrentProjectPageUC.SaveNamedProject()
         m_MainWindow.m_CurrentProjectPageUC.SaveProject()
      End If
      EgtGetCurrFilePath(sCurrProject)

      ' Recupero macchine dei gruppi di lavoro del progetto
      Dim Machines As New List(Of String)
      Dim nMchGrpId As Integer = EgtGetFirstMachGroup()
      While nMchGrpId <> GDB_ID.NULL
         Dim sMachineName As String = String.Empty
         EgtGetMachGroupMachineName(nMchGrpId, sMachineName)
         If Not String.IsNullOrWhiteSpace(sMachineName) Then
            Machines.Add(sMachineName)
         End If
         nMchGrpId = EgtGetNextMachGroup(nMchGrpId)
      End While

      ' Recupero altri file con lo stesso nome del progetto
      Dim OtherFiles As New List(Of String)
      If Not String.IsNullOrWhiteSpace(sCurrProject) Then
         Dim sCurrProjectDir As String = Path.GetDirectoryName(sCurrProject)
         If Not String.IsNullOrWhiteSpace(sCurrProjectDir) Then
            Dim TempFiles() As String = Directory.GetFiles(sCurrProjectDir)
            For FileIndex As Integer = 0 To TempFiles.Count - 1
               If Path.GetFileNameWithoutExtension(TempFiles(FileIndex)).Contains(Path.GetFileNameWithoutExtension(sCurrProject)) AndAlso TempFiles(FileIndex) <> sCurrProject Then
                  OtherFiles.Add(TempFiles(FileIndex))
               End If
            Next
         End If
      End If

      ' Creo zip file da allegare
      Dim sZipToCreate As String = m_MainWindow.GetTempDir() & "\Feedback.zip"
      If File.Exists(sZipToCreate) Then
         File.Delete(sZipToCreate)
      End If
      Try
         Using zip As New Ionic.Zip.ZipFile(sZipToCreate, Console.Out)
            ' aggiungo file macchine
            For Each sMachineName As String In Machines
               Dim sMachineDir As String = m_MainWindow.GetMachinesRootDir() & "\" & sMachineName
               If Directory.Exists(sMachineDir) Then
                  zip.AddItem(sMachineDir, sMachineName)
               End If
            Next
            ' aggiungo progetto corrente
            If File.Exists(sCurrProject) Then
               zip.AddItem(sCurrProject, "")
            End If
            ' aggiungo file log
            If File.Exists(m_MainWindow.GetLogFile()) Then
               zip.AddItem(m_MainWindow.GetLogFile(), "")
            End If
            ' aggiungo file ausiliari
            For Each sOther As String In OtherFiles
               zip.AddItem(sOther, "")
            Next
            ' salvo lo zip
            zip.Save()
         End Using
      Catch ex1 As Exception
         EgtOutLog("Exception in zip: " & ex1.ToString())
      End Try
      ' preparo la mail per il supporto
      Dim bEx As Boolean = False
      Try
         Dim sAddressArray As String() = sSupportAddress.Split(CType(",", Char()))
         Dim SendFeedbackWindow As New EgtWPFLib5.MapiMailMessage("OmagCUT Feedback - " & sKey)
         SendFeedbackWindow.Recipients.Add(sAddressArray(0))
         For index As Integer = 1 To sAddressArray.Length() - 1
            SendFeedbackWindow.Recipients.Add(sAddressArray(index), EgtWPFLib5.MapiMailMessage.RecipientType.CC)
         Next
         If Not String.IsNullOrWhiteSpace(sZipToCreate) AndAlso File.Exists(sZipToCreate) Then
            SendFeedbackWindow.Files.Add(m_MainWindow.GetTempDir() & "\Feedback.zip")
         End If
         SendFeedbackWindow.ShowDialog()
      Catch ex As Exception
         EgtOutLog("Feedback exception: " & ex.ToString)
         bEx = True
      End Try
      Dim SendError As EgtMsgBox
      If bEx OrElse EgtWPFLib5.MapiMailMessage.m_ErrorCode <> 0 Then
         SendError = New EgtMsgBox(m_MainWindow, Me.ActualWidth / 17 * 5, EgtMsgBox.WidthType.PIXEL, "", EgtMsg(MSG_EGTMSGBOX + 1), EgtMsgBox.Buttons.OK, EgtMsgBox.Icons.NULL)
      End If
   End Sub

End Class
