Imports System.Collections.ObjectModel
Imports OmagCUT.TreeViewItem
Imports System.ComponentModel
Imports EgtUILib
Imports EgtWPFLib
Imports EgtWPFLib5

Public Class ToolsDbPageUC

   ' Riferimento alla MainWindow
   Private m_MainWindow As MainWindow = DirectCast(Application.Current.MainWindow, MainWindow)

   ' Riferimento alla macchina corrente
   Private m_CurrMachine As CurrentMachine = m_MainWindow.m_CurrentMachine

   ' Lista degli utensili
   Dim ToolsList As New ObservableCollection(Of CathegoryItem)
   ' Proprietà
   Private m_bFirst As Boolean = True
   ' Flag nuova gestione parametri lama in disegno
   Private m_bNewSawbladeMaker As Boolean = False
   ' Stringa identificativa variabile geometrica in modifica
   Private m_sCurrVar As String = String.Empty
   ' Parametri dell'utensile corrente
   Private m_nToolType As Integer = MCH_TY.NONE
   Private m_sToolName As String = String.Empty
   Private m_sEndLife As String = String.Empty

   ' Array che contengono i tipi di Coolant
   Dim ToolCoolant() As String
   Dim SawCoolant() As String

   ' Conservo l'utensile precedentemente selezionato per usarlo nel caso si selezioni annulla quando si cambia utensile selezionato
   Private m_OldItem As Object

   ' Dichiarazione Scene
   Friend WithEvents ToolScene As New Scene
   Dim ToolSceneHost As New System.Windows.Forms.Integration.WindowsFormsHost

   Private Sub ToolsDbPage_Initialized(sender As Object, e As EventArgs)

      ' Assegnazione scena all'host e posizionamento nella ToolsDbPageGrid
      ToolSceneHost.Child = ToolScene
      ToolSceneHost.SetValue(Grid.ColumnProperty, 2)
      ToolSceneHost.SetValue(Grid.RowProperty, 0)
      Me.ToolsDbPageGrid.Children.Add(ToolSceneHost)

      ' Assegno la lista degli utensili come sorgente del treeview
      ToolTreeView.ItemsSource = ToolsList

      ' Imposto testo delle combobox da file dei messaggi: No, Interna, Esterna, Entrambe
      ToolCoolant = {EgtMsg(MSG_COMBOBOXPARAM + 1), EgtMsg(MSG_COMBOBOXPARAM + 2), EgtMsg(MSG_COMBOBOXPARAM + 3), EgtMsg(MSG_COMBOBOXPARAM + 4)}
      SawCoolant = {EgtMsg(MSG_COMBOBOXPARAM + 1), EgtMsg(MSG_COMBOBOXPARAM + 3)}

      ' Imposto i messaggi letti dal file dei messaggi
      NewBtn.Content = EgtMsg(MSG_MACHINEPAGEUC + 15)
      SaveBtn.Content = EgtMsg(MSG_MACHINEPAGEUC + 17)
      RemoveBtn.Content = EgtMsg(MSG_MACHINEPAGEUC + 18)
      ExportBtn.Content = EgtMsg(91126)
      ImportBtn.Content = EgtMsg(91127)

      NameTxBl.Text = EgtMsg(MSG_TOOLSDBPAGEUC + 1)
      TCPosTxBl.Text = EgtMsg(MSG_TOOLSDBPAGEUC + 2)
      HeadTxBl.Text = EgtMsg(MSG_TOOLSDBPAGEUC + 3)
      ExitTxBl.Text = EgtMsg(MSG_TOOLSDBPAGEUC + 4)
      SpeedGpBx.Header = EgtMsg(MSG_TOOLSDBPAGEUC + 5)
      SpeedTxBl.Text = EgtMsg(MSG_TOOLSDBPAGEUC + 6)
      MaxSpeedTxBl.Text = EgtMsg(MSG_TOOLSDBPAGEUC + 7)
      FeedGpBx.Header = EgtMsg(MSG_TOOLSDBPAGEUC + 8)
      FeedTxBl.Text = EgtMsg(MSG_TOOLSDBPAGEUC + 9)
      TipFeedTxBl.Text = EgtMsg(MSG_TOOLSDBPAGEUC + 10)
      StartFeedTxBl.Text = EgtMsg(MSG_TOOLSDBPAGEUC + 11)
      EndFeedTxBl.Text = EgtMsg(MSG_TOOLSDBPAGEUC + 12)
      CoolantTxBl.Text = EgtMsg(MSG_TOOLSDBPAGEUC + 13)
      CorrTxBl.Text = EgtMsg(MSG_TOOLSDBPAGEUC + 14)
      OffsetGpBx.Header = EgtMsg(MSG_TOOLSDBPAGEUC + 15)
      LonOffsetTxBl.Text = EgtMsg(MSG_TOOLSDBPAGEUC + 16)
      RadOffsetTxBl.Text = EgtMsg(MSG_TOOLSDBPAGEUC + 17)
      MaxAbsorptionTxBl.Text = EgtMsg(MSG_TOOLSDBPAGEUC + 18)
      MinFeedTxBl.Text = EgtMsg(MSG_TOOLSDBPAGEUC + 19)
      MaxMatTxBl.Text = EgtMsg(MSG_TOOLSDBPAGEUC + 20)
      UserNotesTxBl.Text = EgtMsg(MSG_TOOLSDBPAGEUC + 25)
      SerNbrTxBl.Text = EgtMsg(MSG_TOOLSDBPAGEUC + 26)       ' Serial Number
      CodeTxBl.Text = EgtMsg(MSG_TOOLSDBPAGEUC + 27)         ' Code
      SupplierTxBl.Text = EgtMsg(MSG_TOOLSDBPAGEUC + 28)     ' Supplier 
      EndLifeTxBl.Text = EgtMsg(MSG_TOOLSDBPAGEUC + 29)      ' End Life

      ' Associo TextBox e Label
      Dim TempLabel1 As New Label
      TempLabel1.Content = SpeedTxBl.Text
      SpeedTxBx.Tag = TempLabel1
      Dim TempLabel2 As New Label
      TempLabel2.Content = MaxSpeedTxBl.Text
      MaxSpeedTxBx.Tag = TempLabel2
      Dim TempLabel3 As New Label
      TempLabel3.Content = FeedTxBl.Text
      FeedTxBx.Tag = TempLabel3
      Dim TempLabel4 As New Label
      TempLabel4.Content = TipFeedTxBl.Text
      TipFeedTxBx.Tag = TempLabel4
      Dim TempLabel5 As New Label
      TempLabel5.Content = StartFeedTxBl.Text
      StartFeedTxBx.Tag = TempLabel5
      Dim TempLabel6 As New Label
      TempLabel6.Content = EndFeedTxBl.Text
      EndFeedTxBx.Tag = TempLabel6
      Dim TempLabel7 As New Label
      TempLabel7.Content = CorrTxBl.Text
      CorrTxBx.Tag = TempLabel7
      Dim TempLabel8 As New Label
      TempLabel8.Content = LonOffsetTxBl.Text
      LonOffsetTxBx.Tag = TempLabel8
      Dim TempLabel9 As New Label
      TempLabel9.Content = RadOffsetTxBl.Text
      RadOffsetTxBx.Tag = TempLabel9
      Dim TempLabel10 As New Label
      TempLabel10.Content = MaxAbsorptionTxBl.Text
      MaxAbsorptionTxBx.Tag = TempLabel10
      Dim TempLabel11 As New Label
      TempLabel11.Content = MinFeedTxBl.Text
      MinFeedTxBx.Tag = TempLabel11
      Dim TempLabel12 As New Label
      TempLabel12.Content = MaxMatTxBl.Text
      MaxMatTxBx.Tag = TempLabel12

   End Sub

   Private Sub ToolsDbPage_Loaded(sender As Object, e As RoutedEventArgs)
      If m_bFirst Then
         ' imposto colore di default
         Dim DefColor As New Color3d(0, 0, 0)
         GetPrivateProfileColor(S_GEOMDB, K_DEFAULTCOLOR, DefColor, m_MainWindow.GetIniFile())
         ToolScene.SetDefaultMaterial(DefColor)
         ' imposto colori sfondo
         ToolScene.SetViewBackground(GetBackTopColor(), GetBackBottomColor())
         ' imposto colore di evidenziazione
         Dim MarkColor As New Color3d(255, 255, 0)
         GetPrivateProfileColor(S_SCENE, K_MARK, MarkColor, m_MainWindow.GetIniFile())
         ToolScene.SetMarkMaterial(MarkColor)
         ' imposto colore per superfici selezionate
         Dim SelSurfColor As New Color3d(255, 255, 192)
         GetPrivateProfileColor(S_SCENE, K_SELSURF, SelSurfColor, m_MainWindow.GetIniFile())
         ToolScene.SetSelSurfMaterial(SelSurfColor)
         ' imposto tipo e colore del rettangolo di zoom
         Dim bOutline As Boolean = True
         Dim ZwColor As New Color3d(0, 0, 0)
         GetPrivateProfileZoomWin(S_SCENE, K_ZOOMWIN, bOutline, ZwColor, m_MainWindow.GetIniFile())
         ToolScene.SetZoomWinAttribs(bOutline, ZwColor)
         ' imposto colore della linea di distanza
         Dim DstLnColor As New Color3d(255, 0, 0)
         GetPrivateProfileColor(S_SCENE, K_DISTLINE, DstLnColor, m_MainWindow.GetIniFile())
         ToolScene.SetDistLineMaterial(DstLnColor)
         ' imposto parametri OpenGL
         Dim nDriver As Integer = GetPrivateProfileInt(S_OPENGL, K_DRIVER, 3, m_MainWindow.GetIniFile())
         Dim b2Buff As Boolean = (GetPrivateProfileInt(S_OPENGL, K_DOUBLEBUFFER, 1, m_MainWindow.GetIniFile()) <> 0)
         Dim nColorBits As Integer = GetPrivateProfileInt(S_OPENGL, K_COLORBITS, 32, m_MainWindow.GetIniFile())
         Dim nDepthBits As Integer = GetPrivateProfileInt(S_OPENGL, K_DEPTHBITS, 32, m_MainWindow.GetIniFile())
         ToolScene.SetViewAttributes(nDriver, b2Buff, nColorBits, nDepthBits)
         ' inizializzo la scena (DB geometrico + visualizzazione)
         ToolScene.Init()
         ' dimensione lineare max in pixel delle textures
         Dim nTxrMaxLinPix As Integer = GetPrivateProfileInt(S_SCENE, K_TEXMAXLINPIX, 4096, m_MainWindow.GetIniFile())
         EgtSetTextureMaxLinPixels(nTxrMaxLinPix)
         ' inibisco selezione diretta da Scene
         ToolScene.SetStatusNull()
         ' Inizializzo le famiglie di utensili nell'albero
         EgtSetCurrentContext(m_MainWindow.m_CurrentProjectPageUC.CurrentProjectScene.GetCtx())
         InitializeToolGroup(m_CurrMachine.bSaw, MCH_TY.SAW_STD, EgtMsg(90751))
         InitializeToolGroup(m_CurrMachine.bDrill, MCH_TY.DRILL_STD, EgtMsg(90752))
         InitializeToolGroup(m_CurrMachine.bMill, MCH_TY.MILL_STD, EgtMsg(90753))
         InitializeToolGroup(m_CurrMachine.bCupWheel, MCH_TY.MILL_NOTIP, EgtMsg(90754))
         InitializeToolGroup(m_CurrMachine.bPolishingWheel, MCH_TY.MILL_POLISHING, EgtMsg(90756))
         InitializeToolGroup(m_CurrMachine.bWaterJet, MCH_TY.WATERJET, EgtMsg(90755))
         ' Sposto tutti i parametri in su di una riga se testa e uscita sono disattivati
         If Not m_CurrMachine.bShowHeadExit Then
            MoveParam()
         End If
         ' Verifico se macchina con nuovo SawbladeMaker
         Dim sSawBladeMaker As String = ""
         GetPrivateProfileString(S_TOOLS, K_SAWBLADEMAKER, "", sSawBladeMaker, m_MainWindow.GetIniFile())
         m_bNewSawbladeMaker = String.Compare(sSawBladeMaker, "MakeStoneSawBlade.lua", True)
         m_bFirst = False
      Else
         EgtSetCurrentContext(m_MainWindow.m_CurrentProjectPageUC.CurrentProjectScene.GetCtx())
      End If

      InitializeFirstSelectedItem()

   End Sub

   'Funzione che permette l'inizializzazione di albero e parametri all'apertura della pagina ToolsDb
   Private Sub InitializeFirstSelectedItem()
      If ToolsList.Count > 0 Then
         For Each ToolFamily As CathegoryItem In ToolsList
            If ToolFamily.Items.Count > 0 Then
               ToolFamily.IsExpanded = True
               ToolFamily.Items(0).IsSelected = True
               EgtTdbSetCurrTool(ToolFamily.Items(0).Name)
               m_OldItem = ToolFamily.Items(0)
               GetToolParams()
               ViewToolParams()
               ViewToolDraw()
               Exit Sub
            End If
         Next
         ToolsList.Item(0).IsSelected = True
         HideToolParams()
      End If
   End Sub

   Private Sub MoveParam()
      SpeedGpBx.SetValue(Grid.RowProperty, 3)
      FeedGpBx.SetValue(Grid.RowProperty, 7)
      CoolantTxBl.SetValue(Grid.RowProperty, 13)
      CoolantCmBx.SetValue(Grid.RowProperty, 13)
      CorrTxBl.SetValue(Grid.RowProperty, 13)
      CorrTxBx.SetValue(Grid.RowProperty, 13)
      OffsetGpBx.SetValue(Grid.RowProperty, 16)
      AbsorptionBrd.SetValue(Grid.RowProperty, 23)
      MaxMatTxBl.SetValue(Grid.RowProperty, 20)
      MaxMatTxBx.SetValue(Grid.RowProperty, 20)
      SerNbrTxBl.SetValue(Grid.RowProperty, 27)
      SerNbrTxBx.SetValue(Grid.RowProperty, 27)
      CodeTxBl.SetValue(Grid.RowProperty, 27)
      CodeTxBx.SetValue(Grid.RowProperty, 27)
      SupplierTxBl.SetValue(Grid.RowProperty, 29)
      SupplierTxBx.SetValue(Grid.RowProperty, 29)
      EndLifeTxBl.SetValue(Grid.RowProperty, 29)
      EndLifeChBx.SetValue(Grid.RowProperty, 29)
   End Sub

   Private Sub NewBtn_Click(sender As Object, e As RoutedEventArgs) Handles NewBtn.Click
      EgtSetCurrentContext(m_MainWindow.m_CurrentProjectPageUC.CurrentProjectScene.GetCtx())
      If TypeOf ToolTreeView.SelectedItem Is CathegoryItem Then
         Dim SelectedCathegory As CathegoryItem = ToolTreeView.SelectedItem
         Dim NewName As String = SelectedCathegory.Name
         EgtTdbGetToolNewName(NewName)
         If EgtTdbAddTool(NewName, SelectedCathegory.nTType) Then
            For Each ToolFamily As CathegoryItem In ToolsList
               If ToolFamily.nTType = SelectedCathegory.nTType Then
                  Dim NewToolItem As New CustomItem(NewName, SelectedCathegory.nTType)
                  ToolFamily.Items.Add(NewToolItem)
                  NewToolItem.IsSelected = True
                  ToolFamily.IsExpanded = True
                  m_OldItem = ToolTreeView.SelectedItem
                  Exit For
               End If
            Next
            ' Determino il tipo di utensile
            Dim nType As Integer
            EgtTdbGetCurrToolParam(MCH_TP.TYPE, nType)
            ' Imposto testa e uscita
            ' Sempre lame su H1.1 e utensili foretto, fresa e mola da scasso su H1.2
            If (nType And MCH_TF.WATERJET) = 0 Then
               Dim bSaw As Boolean = ((nType And MCH_TF.SAWBLADE) <> 0)
               EgtTdbSetCurrToolParam(MCH_TP.HEAD, "H1")
               EgtTdbSetCurrToolParam(MCH_TP.EXIT_, If(bSaw, 1, 2))
               ' Waterjet sempre su H2.1
            Else
               EgtTdbSetCurrToolParam(MCH_TP.HEAD, "H2")
               EgtTdbSetCurrToolParam(MCH_TP.EXIT_, 1)
            End If
            ' Per macchine senza ToolChanger, resetto la posizione su questo
            If m_CurrMachine.ShowToolChanger = 0 Then
               EgtTdbSetCurrToolParam(MCH_TP.TCPOS, "")
            End If
            ' Imposto dimensioni standard
            If nType = MCH_TY.DRILL_STD Then
               EgtTdbSetCurrToolParam(MCH_TP.LEN, 120.0)
               EgtTdbSetCurrToolParam(MCH_TP.TOTLEN, 120.0)
               EgtTdbSetCurrToolParam(MCH_TP.DIAM, 20.0)
               EgtTdbSetCurrToolParam(MCH_TP.TOTDIAM, 20.0)
               EgtTdbSetCurrToolParam(MCH_TP.MAXMAT, 40.0)
            ElseIf nType = MCH_TY.SAW_STD Then
               EgtTdbSetCurrToolParam(MCH_TP.LEN, 80.0)
               EgtTdbSetCurrToolParam(MCH_TP.TOTLEN, 80.0)
               EgtTdbSetCurrToolParam(MCH_TP.DIAM, 600.0)
               EgtTdbSetCurrToolParam(MCH_TP.TOTDIAM, 600.0)
               EgtTdbSetCurrToolParam(MCH_TP.THICK, 5.0)
               EgtTdbSetCurrToolParam(MCH_TP.MAXMAT, 40.0)
            ElseIf nType = MCH_TY.MILL_STD Then
               EgtTdbSetCurrToolParam(MCH_TP.LEN, 120.0)
               EgtTdbSetCurrToolParam(MCH_TP.TOTLEN, 120.0)
               EgtTdbSetCurrToolParam(MCH_TP.DIAM, 20.0)
               EgtTdbSetCurrToolParam(MCH_TP.TOTDIAM, 20.0)
               EgtTdbSetCurrToolParam(MCH_TP.MAXMAT, 40.0)
            ElseIf nType = MCH_TY.MILL_NOTIP Then
               EgtTdbSetCurrToolParam(MCH_TP.LEN, 120.0)
               EgtTdbSetCurrToolParam(MCH_TP.TOTLEN, 120.0)
               EgtTdbSetCurrToolParam(MCH_TP.DIAM, 90.0)
               EgtTdbSetCurrToolParam(MCH_TP.TOTDIAM, 90.0)
               EgtTdbSetCurrToolParam(MCH_TP.MAXMAT, 10.0)
            ElseIf nType = MCH_TY.MILL_POLISHING Then
               EgtTdbSetCurrToolParam(MCH_TP.LEN, 120.0)
               EgtTdbSetCurrToolParam(MCH_TP.TOTLEN, 120.0)
               EgtTdbSetCurrToolParam(MCH_TP.DIAM, 100.0)
               EgtTdbSetCurrToolParam(MCH_TP.TOTDIAM, 100.0)
               EgtTdbSetCurrToolParam(MCH_TP.MAXMAT, 10.0)
            ElseIf nType = MCH_TY.WATERJET Then
               EgtTdbSetCurrToolParam(MCH_TP.LEN, 5.0)
               EgtTdbSetCurrToolParam(MCH_TP.TOTLEN, 5.0)
               EgtTdbSetCurrToolParam(MCH_TP.DIAM, 1.0)
               EgtTdbSetCurrToolParam(MCH_TP.TOTDIAM, 1.0)
               EgtTdbSetCurrToolParam(MCH_TP.MAXMAT, 45.0)
            End If
            ' Gestione speciale eventuali note tipo Hofmann
            ManageSpecialNotes()
            ' Salvo e aggiorno interfaccia utente
            EgtTdbSaveCurrTool()
            GetToolParams()
            ViewToolParams()
            ViewToolDraw()
         End If
      ElseIf TypeOf ToolTreeView.SelectedItem Is CustomItem Then
         Dim SelectedCathegory As CustomItem = ToolTreeView.SelectedItem
         Dim NewName As String = SelectedCathegory.Name
         EgtTdbGetToolNewName(NewName)
         If EgtTdbCopyTool(SelectedCathegory.Name, NewName) Then
            ' Gestione speciale eventuali note tipo Hofmann
            ManageSpecialNotes()
            ' Annullo disegno
            EgtTdbSetCurrToolParam(MCH_TP.DRAW, "")
            EgtTdbSaveCurrTool()
            ' Aggiorno lista utensili
            Dim CurrType As Integer
            EgtTdbGetCurrToolParam(MCH_TP.TYPE, CurrType)
            For Each ToolFamily As CathegoryItem In ToolsList
               If ToolFamily.nTType = CurrType Then
                  Dim NewToolItem As New CustomItem(NewName, CurrType)
                  ToolFamily.Items.Add(NewToolItem)
                  NewToolItem.IsSelected = True
                  GetToolParams()
                  m_OldItem = ToolTreeView.SelectedItem
                  Exit For
               End If
            Next
         End If
      End If

   End Sub

   Private Function GetSpecials() As Boolean
      ' Note speciali devono essere visualizzate con l'utensile corrente
      If m_CurrMachine.ShowSpecials = 0 Then Return False
      Dim nType As Integer
      EgtTdbGetCurrToolParam(MCH_TP.TYPE, nType)
      If nType <> MCH_TY.SAW_STD And m_CurrMachine.ShowSpecials = 2 Then Return False
      ' Imposto valori di default
      CodeTxBx.Text = ""
      SupplierTxBx.Text = ""
      SerNbrTxBx.Text = ""
      EndLifeChBx.IsChecked = False
      m_sEndLife = ""
      UserNotesTxBx.Text = ""
      ' Recupero le note utente
      Dim sUserNotes As String = ""
      EgtTdbGetCurrToolParam(MCH_TP.USERNOTES, sUserNotes)
      ' Se esistono
      If Not String.IsNullOrWhiteSpace(sUserNotes) Then
         ' Stringa con note rimanenti
         Dim sToShow As String = ""
         ' Le divido in parti
         Dim sItems() As String = sUserNotes.Split(";".ToCharArray)
         For i As Integer = 0 To sItems.Count() - 1
            If sItems(i).Contains("CODE=") Then
               CodeTxBx.Text = sItems(i).Substring(5)
            ElseIf sItems(i).Contains("SUPPL=") Then
               SupplierTxBx.Text = sItems(i).Substring(6)
            ElseIf sItems(i).Contains("S/N=") Then
               SerNbrTxBx.Text = sItems(i).Substring(4)
            ElseIf sItems(i).Contains("END=") Then
               EndLifeChBx.IsChecked = True
               m_sEndLife = sItems(i).Substring(4)
            ElseIf Not String.IsNullOrWhiteSpace(sItems(i)) Then
               sToShow &= sItems(i) & ";"
            End If
         Next
         UserNotesTxBx.Text = sToShow
      End If
      Return True
   End Function

   Private Function SetSpecials() As Boolean
      ' Note speciali devono essere visualizzate con l'utensile corrente
      If m_CurrMachine.ShowSpecials = 0 Then Return False
      Dim nType As Integer
      EgtTdbGetCurrToolParam(MCH_TP.TYPE, nType)
      If nType <> MCH_TY.SAW_STD And m_CurrMachine.ShowSpecials = 2 Then Return False
      ' Ricreo la stringa
      Dim sUserNotes As String = ""
      sUserNotes &= "CODE=" & CodeTxBx.Text & ";"
      sUserNotes &= "SUPPL=" & SupplierTxBx.Text & ";"
      sUserNotes &= "S/N=" & SerNbrTxBx.Text & ";"
      If EndLifeChBx.IsChecked Then
         If String.IsNullOrWhiteSpace(m_sEndLife) Then
            m_sEndLife = My.Computer.Clock.LocalTime.ToString("dd.MM.yyyy HH:mm:ss")
         End If
         sUserNotes &= "END=" & m_sEndLife & ";"
      End If
      sUserNotes &= UserNotesTxBx.Text
      EgtTdbSetCurrToolParam(MCH_TP.USERNOTES, sUserNotes)
      Return True
   End Function

   Private Sub ManageSpecialNotes()
      ' Note utente devono essere visualizzate con l'utensile corrente
      If m_CurrMachine.ShowUserNotes = 0 And m_CurrMachine.ShowSpecials = 0 Then Return
      Dim nType As Integer
      EgtTdbGetCurrToolParam(MCH_TP.TYPE, nType)
      If nType <> MCH_TY.SAW_STD And m_CurrMachine.ShowUserNotes = 2 Then Return
      ' Recupero le note utente
      Dim sUserNotes As String = ""
      EgtTdbGetCurrToolParam(MCH_TP.USERNOTES, sUserNotes)
      ' Se esistono
      If Not String.IsNullOrEmpty(sUserNotes) Then
         ' Le divido in parti
         Dim bIsCode As Boolean = False
         Dim bIsSuppl As Boolean = False
         Dim bIsSN As Boolean = False
         Dim sItems() As String = sUserNotes.Split(";".ToCharArray)
         For i As Integer = 0 To sItems.Count() - 1
            If sItems(i).Contains("CODE=") Then
               bIsCode = True
            ElseIf sItems(i).Contains("SUPPL=") Then
               bIsSuppl = True
            ElseIf sItems(i).Contains("S/N=") Then
               bIsSN = True
            ElseIf sItems(i).Contains("START=") Then
               sItems(i) = ""
            ElseIf sItems(i).Contains("PROD=") Then
               sItems(i) = ""
            ElseIf sItems(i).Contains("END=") Then
               sItems(i) = ""
            End If
         Next
         sUserNotes = ""
         For i As Integer = 0 To sItems.Count() - 1
            If Not String.IsNullOrWhiteSpace(sItems(i)) Then
               sUserNotes &= sItems(i) & ";"
            End If
         Next
         If Not bIsCode Then sUserNotes &= "CODE= ;"
         If Not bIsSuppl Then sUserNotes &= "SUPPL= ;"
         If Not bIsSN Then sUserNotes &= "S/N= ;"
      Else
         sUserNotes = "CODE= ; SUPPL= ; S/N= ;"
      End If
      sUserNotes &= "START=" & My.Computer.Clock.LocalTime.ToString("dd.MM.yyyy HH:mm:ss") & ";"
      sUserNotes &= "PROD=0;"
      EgtTdbSetCurrToolParam(MCH_TP.USERNOTES, sUserNotes)
   End Sub

   Private Sub SaveBtn_Click(sender As Object, e As RoutedEventArgs) Handles SaveBtn.Click
      EgtSetCurrentContext(m_MainWindow.m_CurrentProjectPageUC.CurrentProjectScene.GetCtx())
      SetToolParams()
      EgtTdbSaveCurrTool()
      m_sToolName = NameTxBx.Text
      Dim CurrTool As CustomItem = TryCast(m_OldItem, CustomItem)
      If CurrTool IsNot Nothing Then
         CurrTool.Name = m_sToolName
      End If
   End Sub

   Private Sub RemoveBtn_Click(sender As Object, e As RoutedEventArgs) Handles RemoveBtn.Click
      If TypeOf ToolTreeView.SelectedItem Is CustomItem Then
         Dim SelectedItem As CustomItem = ToolTreeView.SelectedItem
         EgtSetCurrentContext(m_MainWindow.m_CurrentProjectPageUC.CurrentProjectScene.GetCtx())
         ' Cancello l'utensile
         EgtTdbRemoveTool(SelectedItem.Name)
         ' Rimuovo il nome dell'albero
         For Each ToolFamily As CathegoryItem In ToolsList
            If (ToolFamily.nTType And SelectedItem.nType) <> 0 Then
               ToolFamily.Items.Remove(SelectedItem)
               If ToolFamily.Items.Count = 0 Then
                  ToolFamily.IsSelected = True
                  HideToolParams()
                  HideToolDraw()
               Else
                  ToolFamily.Items(0).IsSelected = True
                  EgtTdbSetCurrTool(ToolFamily.Items(0).Name)
                  m_OldItem = ToolFamily.Items(0)
                  GetToolParams()
                  ViewToolParams()
                  ViewToolDraw()
               End If
            End If
         Next
      End If
   End Sub

   Private Sub ExportBtn_Click(Sender As Object, e As RoutedEventArgs) Handles ExportBtn.Click
      ' contesto corrente
      EgtSetCurrentContext(m_MainWindow.m_CurrentProjectPageUC.CurrentProjectScene.GetCtx())

      ' creo lista utensili per esportazione
      Dim ToolToExportList = New ObservableCollection(Of ImpExpToolFamily)
      Dim Family As CathegoryItem
      Dim Tool As CustomItem
      For Each Family In ToolsList
         Dim ImpExpToolFamily As New ImpExpToolFamily(Family.Name, Family.nTType)
         ImpExpToolFamily.PictureString = "/Resources/ToolsTreeviewImages/Folder.png"
         For Each Tool In Family.Items
            Dim CurrTool = New ImpExpToolItem(Tool.Name, False)
            ImpExpToolFamily.ToolList.Add(CurrTool)
         Next
         ToolToExportList.Add(ImpExpToolFamily)
      Next

      Dim ExportWndVM As New ImportExportToolWindowVM(ToolToExportList, True)
      Dim ExportWnd As New ImportExportToolWD(Application.Current.MainWindow, ExportWndVM)

      ExportWnd.ShowDialog()
   End Sub

   Private Sub ImportBtn_Click(Sender As Object, e As RoutedEventArgs) Handles ImportBtn.Click
      ' contesto corrente
      EgtSetCurrentContext(m_MainWindow.m_CurrentProjectPageUC.CurrentProjectScene.GetCtx())
      ' apro dialogo di scelta file
      Dim ImportFileDlg As New Microsoft.Win32.OpenFileDialog() With {
         .Title = EgtMsg(31161) & " " & EgtMsg(31163),
         .DefaultExt = ".data",
         .Filter = "Tools (.data)|*.data",
         .CheckFileExists = True,
         .ValidateNames = True
      }
      If ImportFileDlg.ShowDialog() <> True Then Return
      Dim ImportFilePath As String = ImportFileDlg.FileName
      ' recupero liste utensili da importare
      Dim ImportFileToolNameList As String() = Nothing
      Dim ImportFileToolFamilyList As Integer() = Nothing
      If Not EgtTdbToBeImported(ImportFilePath, ImportFileToolNameList, ImportFileToolFamilyList) Then Return
      ' li inserisco in lista per finestra di scelta
      Dim ToolToImportList As New ObservableCollection(Of ImpExpToolFamily)
      ' creo famiglie di utensili in base a quelle trovate in lista importata
      For ToolIndex As Integer = 0 To ImportFileToolNameList.Count - 1
         Dim ToolName As String = ImportFileToolNameList(ToolIndex)
         Dim ToolFamily As Integer = ImportFileToolFamilyList(ToolIndex)
         ToolFamily = ToolFamily And
            (MCH_TF.DRILLBIT Or
            MCH_TF.SAWBLADE Or
            MCH_TF.MILL Or
            MCH_TF.MORTISE Or
            MCH_TF.CHISEL Or
            MCH_TF.WATERJET Or
            MCH_TF.COMPO)
         Dim bFounded As Boolean = False
         Dim ToolToImportFamily As ImpExpToolFamily
         For Each ToolToImportFamily In ToolToImportList
            If (ToolFamily = ToolToImportFamily.FamilyType) Then
               ToolToImportFamily.ToolList.Add(New ImpExpToolItem(ToolName, AlreadyExist(ToolName)))
               bFounded = True
               Exit For
            End If
         Next
         If Not bFounded Then
            Dim NewFamily As New ImpExpToolFamily(GetToolFamilyName(ToolFamily), ToolFamily)
            NewFamily.ToolList.Add(New ImpExpToolItem(ToolName, AlreadyExist(ToolName)))
            ToolToImportList.Add(NewFamily)
         End If
      Next
      Dim ImportWndVM As New ImportExportToolWindowVM(ToolToImportList, False, ImportFilePath, ImportFileToolNameList)
      Dim ImportWnd As New ImportExportToolWD(Application.Current.MainWindow, ImportWndVM)

      ImportWnd.ShowDialog()
      ' Aggiungo all'albero visualizzato gli utensili appena importati
      If ImportWnd.OkResult Then
         LoadImportedMachineTools(ImportWndVM.vsImported)
      End If
   End Sub

   Private Function AlreadyExist(ToolName As String) As Boolean
      Dim ToolFamily As CathegoryItem
      For Each ToolFamily In ToolsList
         Dim ToolItem As CustomItem
         For Each ToolItem In ToolFamily.Items
            If ToolName = ToolItem.Name Then
               Return True
            End If
         Next
      Next
      Return False
   End Function

   Private Function GetToolFamilyName(NewToolFamily As Integer) As String
      Dim ToolFamily As CathegoryItem
      For Each ToolFamily In ToolsList
         If NewToolFamily = ToolFamily.nTType Then
            Return ToolFamily.Name
         End If
      Next
      Return ""
   End Function

   Private Sub LoadImportedMachineTools(vsImportedTools As String())
      ' Leggo tutti gli utensili presenti nella Macchina (quindi anche quelli appena importati). 
      Dim ActiveToolsFamilies() As ToolsFamily = MyReadToolFamilies(m_CurrMachine.sMachIniFile)
      Dim ToolsFamilyIndex As Integer = 0
      Dim ToolsFamilyItem As ToolsFamily
      For Each ToolsFamilyItem In ActiveToolsFamilies
         ' dalla lista che ho ricavato dalla libreria recupero gli utensili associati
         Dim FamilyTreeView As New CathegoryItem(ToolsFamilyItem.Name, ToolsFamilyItem.Id)
         Dim ToolFamilyItem = ToolsList.FirstOrDefault(Function(ToolFamily) ToolFamily.nTType = ToolsFamilyItem.Id)
         If IsNothing(ToolFamilyItem) Then
            ToolsList.Insert(ToolsFamilyIndex, FamilyTreeView)
         Else
            ' ricerco l'indice nella lista Tools

         End If
         Dim nType As Integer = 0
         Dim ToolName As String = String.Empty
         Dim ToolIndex As Integer = 0
         'EgtSetCurrentContext(ToolScene.GetCtx())
         EgtSetCurrentContext(m_MainWindow.m_CurrentProjectPageUC.CurrentProjectScene.GetCtx())
         If EgtTdbGetFirstTool(ToolsFamilyItem.Id, ToolName, nType) Then
            Dim ToolItem = ToolsList(ToolsFamilyIndex).Items.FirstOrDefault(Function(Tool) Tool.Name = ToolName)
            If IsNothing(ToolItem) Then
               ' Se leggo un utensile non presente nella ToolsList visualizzata lo aggiungo ad essa. 
               Dim NewToolName As New CustomItem(ToolName, nType)
               ToolsList(ToolsFamilyIndex).Items.Insert(ToolIndex, NewToolName)
               'NewToolName.IsSelected = True
            ElseIf vsImportedTools.Contains(ToolName) Then
               ' Se un utensile è presente nella ToolsList visualizzata ma nel contempo è nell'array vsImported 
               ' vuol dire che è stato sovrascritto perciò lo rimuovo e lo riaggiungo alla ToolsList.
               ToolsList(ToolsFamilyIndex).Items.Remove(ToolsList(ToolsFamilyIndex).Items.FirstOrDefault(Function(Tool) Tool.Name = ToolName))
               Dim NewToolName As New CustomItem(ToolName, nType)
               ToolsList(ToolsFamilyIndex).Items.Insert(ToolIndex, NewToolName)
               'NewToolName.IsSelected = True
            End If
            ToolIndex += 1
            While EgtTdbGetNextTool(ToolsFamilyItem.Id, ToolName, nType)
               ToolItem = ToolsList(ToolsFamilyIndex).Items.FirstOrDefault(Function(Tool) Tool.Name = ToolName)
               If IsNothing(ToolItem) Then
                  ' Se leggo un utensile non presente nella ToolsList visualizzata lo aggiungo ad essa. 
                  Dim NewToolName As New CustomItem(ToolName, nType)
                  ToolsList(ToolsFamilyIndex).Items.Insert(ToolIndex, NewToolName)
                  'NewToolName.IsSelected = True
               ElseIf vsImportedTools.Contains(ToolName) Then
                  ' Se un utensile è presente nella ToolsList visualizzata ma nel contempo è nell'array vsImported 
                  ' vuol dire che è stato sovrascritto perciò lo rimuovo e lo riaggiungo alla ToolsList.
                  ToolsList(ToolsFamilyIndex).Items.Remove(ToolsList(ToolsFamilyIndex).Items.FirstOrDefault(Function(Tool) Tool.Name = ToolName))
                  Dim NewToolName As New CustomItem(ToolName, nType)
                  ToolsList(ToolsFamilyIndex).Items.Insert(ToolIndex, NewToolName)
                  'NewToolName.IsSelected = True
               End If
               ToolIndex += 1
            End While
         End If
         ToolsFamilyIndex += 1
      Next

   End Sub

   Public Function MyReadToolFamilies(sMachineIniPath As String) As ToolsFamily()
      Dim ActiveToolsFamiliesList As New List(Of ToolsFamily)

      ' Se il materiale lavorato è Marmo l'ordine di lettura e inserimento di Punta e Lama nelle famiglie di utensili attivi è invertito 
      If EgtUILib.GetPrivateProfileInt(S_TOOLS, K_SAWBLADE, 0, sMachineIniPath) <> 0 Then
         ActiveToolsFamiliesList.Add(New ToolsFamily(MCH_TF.SAWBLADE, EgtMsg(31000 + 2)))
      End If
      If EgtUILib.GetPrivateProfileInt(S_TOOLS, K_DRILLBIT, 0, sMachineIniPath) <> 0 Then
         ActiveToolsFamiliesList.Add(New ToolsFamily(MCH_TF.DRILLBIT, EgtMsg(31000 + 1)))
      End If


      If EgtUILib.GetPrivateProfileInt(S_TOOLS, K_MILL, 0, sMachineIniPath) <> 0 Then
         ActiveToolsFamiliesList.Add(New ToolsFamily(MCH_TF.MILL, EgtMsg(31000 + 3)))
      End If
      If EgtUILib.GetPrivateProfileInt(S_TOOLS, "CupWheel", 0, sMachineIniPath) <> 0 Then
         ActiveToolsFamiliesList.Add(New ToolsFamily(MCH_TF.MILL, EgtMsg(90754)))
      End If
      If EgtUILib.GetPrivateProfileInt(S_TOOLS, "PolishingWheel", 0, sMachineIniPath) <> 0 Then
         ActiveToolsFamiliesList.Add(New ToolsFamily(MCH_TF.MILL, EgtMsg(90756)))
      End If

      If EgtUILib.GetPrivateProfileInt(S_TOOLS, K_MORTISE, 0, sMachineIniPath) <> 0 Then
         ActiveToolsFamiliesList.Add(New ToolsFamily(MCH_TF.MORTISE, EgtMsg(31000 + 4)))
      End If
      If EgtUILib.GetPrivateProfileInt(S_TOOLS, K_CHISEL, 0, sMachineIniPath) <> 0 Then
         ActiveToolsFamiliesList.Add(New ToolsFamily(MCH_TF.CHISEL, EgtMsg(31000 + 9)))
      End If
      If EgtUILib.GetPrivateProfileInt(S_TOOLS, K_COMPO, 0, sMachineIniPath) <> 0 Then
         ActiveToolsFamiliesList.Add(New ToolsFamily(MCH_TF.COMPO, EgtMsg(31000 + 5)))
      End If
      If EgtUILib.GetPrivateProfileInt(S_TOOLS, K_WATERJET, 0, sMachineIniPath) <> 0 Then
         ActiveToolsFamiliesList.Add(New ToolsFamily(MCH_TF.WATERJET, EgtMsg(31010)))
      End If
      Return ActiveToolsFamiliesList.ToArray
   End Function

   Friend Sub InitializeToolGroup(bEnabled As Boolean, nTType As Integer, sFName As String)
      If Not bEnabled Then Return
      ' Inserisco categoria ed eventuali elementi
      Dim ToolCathegory As New CathegoryItem(sFName, nTType)
      Dim nType As Integer = 0
      Dim ToolName As String = String.Empty
      Dim bFound As Boolean = EgtTdbGetFirstTool(nTType, ToolName, nType)
      While bFound
         If nType = nTType Then ToolCathegory.Items.Add(New CustomItem(ToolName, nType))
         bFound = EgtTdbGetNextTool(nTType, ToolName, nType)
      End While
      ToolsList.Add(ToolCathegory)
   End Sub

   Private Sub GetToolParams()
      EgtSetCurrentContext(m_MainWindow.m_CurrentProjectPageUC.CurrentProjectScene.GetCtx())
      EgtTdbGetCurrToolParam(MCH_TP.TYPE, m_nToolType)
      Dim ToolString As String = String.Empty
      Dim ToolInt As Integer = 0
      Dim ToolDouble As Double = 0
      EgtTdbGetCurrToolParam(MCH_TP.NAME, m_sToolName)
      NameTxBx.Text = m_sToolName
      EgtTdbGetCurrToolParam(MCH_TP.TCPOS, ToolString)
      TCPosTxBx.Text = ToolString
      EgtTdbGetCurrToolParam(MCH_TP.MAXMAT, ToolDouble)
      MaxMatTxBx.Text = LenToString(ToolDouble, 3)
      EgtTdbGetCurrToolParam(MCH_TP.LONOFFSET, ToolDouble)
      LonOffsetTxBx.Text = LenToString(ToolDouble, 3)
      EgtTdbGetCurrToolParam(MCH_TP.RADOFFSET, ToolDouble)
      RadOffsetTxBx.Text = LenToString(ToolDouble, 3)
      EgtTdbGetCurrToolParam(MCH_TP.COOLANT, ToolInt)
      Dim ToolType As Integer
      EgtTdbGetCurrToolParam(MCH_TP.TYPE, ToolType)
      If (ToolType And MCH_TF.SAWBLADE) <> 0 Then
         CoolantCmBx.ItemsSource = SawCoolant
         If ToolInt = 0 Then
            CoolantCmBx.SelectedIndex = 0
         ElseIf ToolInt = 2 Then
            CoolantCmBx.SelectedIndex = 1
         End If
      Else
         CoolantCmBx.ItemsSource = ToolCoolant
         CoolantCmBx.SelectedIndex = ToolInt
      End If
      EgtTdbGetCurrToolParam(MCH_TP.CORR, ToolInt)
      CorrTxBx.Text = ToolInt.ToString()
      EgtTdbGetCurrToolParam(MCH_TP.MAXSPEED, ToolDouble)
      MaxSpeedTxBx.Text = DoubleToString(ToolDouble, 3)
      EgtTdbGetCurrToolParam(MCH_TP.SPEED, ToolDouble)
      SpeedTxBx.Text = DoubleToString(ToolDouble, 3)
      EgtTdbGetCurrToolParam(MCH_TP.FEED, ToolDouble)
      FeedTxBx.Text = LenToString(ToolDouble, 3)
      EgtTdbGetCurrToolParam(MCH_TP.TIPFEED, ToolDouble)
      TipFeedTxBx.Text = LenToString(ToolDouble, 3)
      EgtTdbGetCurrToolParam(MCH_TP.STARTFEED, ToolDouble)
      StartFeedTxBx.Text = LenToString(ToolDouble, 3)
      EgtTdbGetCurrToolParam(MCH_TP.ENDFEED, ToolDouble)
      EndFeedTxBx.Text = LenToString(ToolDouble, 3)
      EgtTdbGetCurrToolParam(MCH_TP.MAXABSORPTION, ToolDouble)
      MaxAbsorptionTxBx.Text = DoubleToString(ToolDouble, 3)
      EgtTdbGetCurrToolParam(MCH_TP.MINFEED, ToolDouble)
      MinFeedTxBx.Text = LenToString(ToolDouble, 3)
      EgtTdbGetCurrToolParam(MCH_TP.HEAD, ToolString)
      HeadTxBx.Text = ToolString
      EgtTdbGetCurrToolParam(MCH_TP.EXIT_, ToolInt)
      ExitTxBx.Text = ToolInt.ToString()
      If Not GetSpecials() Then
         EgtTdbGetCurrToolParam(MCH_TP.USERNOTES, ToolString)
         UserNotesTxBx.Text = ToolString
      End If
   End Sub

   Private Sub SetToolParams()
      EgtSetCurrentContext(m_MainWindow.m_CurrentProjectPageUC.CurrentProjectScene.GetCtx())
      Dim dTemp As Double
      Dim nTemp As Integer
      EgtTdbSetCurrToolParam(MCH_TP.NAME, NameTxBx.Text)
      EgtTdbSetCurrToolParam(MCH_TP.TCPOS, TCPosTxBx.Text)
      If m_nToolType = MCH_TY.SAW_STD Or m_nToolType = MCH_TY.WATERJET Then
         StringToLen(MaxMatTxBx.Text, dTemp)
         EgtTdbSetCurrToolParam(MCH_TP.MAXMAT, dTemp)
      End If
      If m_nToolType <> MCH_TY.DRILL_STD Then
         StringToLen(LonOffsetTxBx.Text, dTemp)
         EgtTdbSetCurrToolParam(MCH_TP.LONOFFSET, dTemp)
         StringToLen(RadOffsetTxBx.Text, dTemp)
         EgtTdbSetCurrToolParam(MCH_TP.RADOFFSET, dTemp)
      End If
      Dim sUuid As String = ""
      EgtTdbGetCurrToolParam(MCH_TP.UUID, sUuid)
      EgtTdbSetCurrToolParam(MCH_TP.DRAW, sUuid & ".nge")
      If m_nToolType = MCH_TY.SAW_STD Then
         If CoolantCmBx.SelectedIndex = 0 Then
            nTemp = 0
         ElseIf CoolantCmBx.SelectedIndex = 1 Then
            nTemp = 2
         End If
      Else
         nTemp = CoolantCmBx.SelectedIndex
      End If
      EgtTdbSetCurrToolParam(MCH_TP.COOLANT, nTemp)
      Int32.TryParse(CorrTxBx.Text, nTemp)
      EgtTdbSetCurrToolParam(MCH_TP.CORR, nTemp)
      StringToDouble(MaxSpeedTxBx.Text, dTemp)
      EgtTdbSetCurrToolParam(MCH_TP.MAXSPEED, dTemp)
      StringToDouble(SpeedTxBx.Text, dTemp)
      EgtTdbSetCurrToolParam(MCH_TP.SPEED, dTemp)
      StringToLen(FeedTxBx.Text, dTemp)
      EgtTdbSetCurrToolParam(MCH_TP.FEED, dTemp)
      StringToLen(TipFeedTxBx.Text, dTemp)
      EgtTdbSetCurrToolParam(MCH_TP.TIPFEED, dTemp)
      StringToLen(StartFeedTxBx.Text, dTemp)
      EgtTdbSetCurrToolParam(MCH_TP.STARTFEED, dTemp)
      StringToLen(EndFeedTxBx.Text, dTemp)
      EgtTdbSetCurrToolParam(MCH_TP.ENDFEED, dTemp)
      StringToDouble(MaxAbsorptionTxBx.Text, dTemp)
      EgtTdbSetCurrToolParam(MCH_TP.MAXABSORPTION, dTemp)
      StringToLen(MinFeedTxBx.Text, dTemp)
      EgtTdbSetCurrToolParam(MCH_TP.MINFEED, dTemp)
      EgtTdbSetCurrToolParam(MCH_TP.HEAD, HeadTxBx.Text)
      Int32.TryParse(ExitTxBx.Text, nTemp)
      EgtTdbSetCurrToolParam(MCH_TP.EXIT_, nTemp)
      If Not SetSpecials() Then
         EgtTdbSetCurrToolParam(MCH_TP.USERNOTES, UserNotesTxBx.Text)
      End If
   End Sub

   Private Sub ViewToolParams()
      NameTxBl.Visibility = Windows.Visibility.Visible
      NameTxBx.Visibility = Windows.Visibility.Visible
      Dim bShowTcPos As Boolean = ((m_nToolType = MCH_TY.SAW_STD And m_CurrMachine.ShowToolChanger <> 0) Or m_CurrMachine.ShowToolChanger = 1)
      TCPosBrd.Visibility = If(bShowTcPos, Windows.Visibility.Visible, Windows.Visibility.Hidden)
      HeadExitBrd.Visibility = If(m_CurrMachine.bShowHeadExit, Windows.Visibility.Visible, Windows.Visibility.Hidden)
      SpeedGpBx.Visibility = If(m_nToolType <> MCH_TY.WATERJET, Windows.Visibility.Visible, Windows.Visibility.Hidden)
      FeedGpBx.Visibility = Windows.Visibility.Visible
      TipFeedTxBl.Text = If(m_nToolType <> MCH_TY.WATERJET, EgtMsg(90730), EgtMsg(90792))
      StartFeedTxBl.Visibility = If(m_nToolType <> MCH_TY.WATERJET, Windows.Visibility.Visible, Windows.Visibility.Hidden)
      StartFeedTxBx.Visibility = If(m_nToolType <> MCH_TY.WATERJET, Windows.Visibility.Visible, Windows.Visibility.Hidden)
      EndFeedTxBl.Visibility = If(m_nToolType <> MCH_TY.WATERJET, Windows.Visibility.Visible, Windows.Visibility.Hidden)
      EndFeedTxBx.Visibility = If(m_nToolType <> MCH_TY.WATERJET, Windows.Visibility.Visible, Windows.Visibility.Hidden)
      CoolantTxBl.Visibility = If(m_nToolType <> MCH_TY.WATERJET, Windows.Visibility.Visible, Windows.Visibility.Hidden)
      CoolantCmBx.Visibility = If(m_nToolType <> MCH_TY.WATERJET, Windows.Visibility.Visible, Windows.Visibility.Hidden)
      CorrTxBl.Visibility = Windows.Visibility.Hidden
      CorrTxBx.Visibility = Windows.Visibility.Hidden
      MaxMatTxBl.Visibility = If(m_nToolType = MCH_TY.SAW_STD Or m_nToolType = MCH_TY.WATERJET, Windows.Visibility.Visible, Windows.Visibility.Hidden)
      MaxMatTxBx.Visibility = If(m_nToolType = MCH_TY.SAW_STD Or m_nToolType = MCH_TY.WATERJET, Windows.Visibility.Visible, Windows.Visibility.Hidden)
      OffsetGpBx.Visibility = If(m_nToolType <> MCH_TY.DRILL_STD, Windows.Visibility.Visible, Windows.Visibility.Hidden)
      LonOffsetTxBl.Visibility = If(m_nToolType <> MCH_TY.WATERJET, Windows.Visibility.Visible, Windows.Visibility.Hidden)
      LonOffsetTxBx.Visibility = If(m_nToolType <> MCH_TY.WATERJET, Windows.Visibility.Visible, Windows.Visibility.Hidden)
      If m_nToolType = MCH_TY.DRILL_STD Then
         AbsorptionBrd.SetValue(Grid.RowProperty, 19)
      Else
         AbsorptionBrd.SetValue(Grid.RowProperty, 23)
      End If
      AbsorptionBrd.Visibility = If(m_nToolType <> MCH_TY.WATERJET, Windows.Visibility.Visible, Windows.Visibility.Hidden)
      Dim bShowUserNotes As Boolean = ((m_nToolType = MCH_TY.SAW_STD And m_CurrMachine.ShowUserNotes <> 0) Or m_CurrMachine.ShowUserNotes = 1)
      UserNotesTxBl.Visibility = If(bShowUserNotes, Windows.Visibility.Visible, Windows.Visibility.Hidden)
      UserNotesTxBx.Visibility = If(bShowUserNotes, Windows.Visibility.Visible, Windows.Visibility.Hidden)
      Dim bShowSpecials As Boolean = ((m_nToolType = MCH_TY.SAW_STD And m_CurrMachine.ShowSpecials <> 0) Or m_CurrMachine.ShowSpecials = 1)
      SerNbrTxBl.Visibility = If(bShowSpecials, Windows.Visibility.Visible, Windows.Visibility.Hidden)
      SerNbrTxBx.Visibility = If(bShowSpecials, Windows.Visibility.Visible, Windows.Visibility.Hidden)
      CodeTxBl.Visibility = If(bShowSpecials, Windows.Visibility.Visible, Windows.Visibility.Hidden)
      CodeTxBx.Visibility = If(bShowSpecials, Windows.Visibility.Visible, Windows.Visibility.Hidden)
      SupplierTxBl.Visibility = If(bShowSpecials, Windows.Visibility.Visible, Windows.Visibility.Hidden)
      SupplierTxBx.Visibility = If(bShowSpecials, Windows.Visibility.Visible, Windows.Visibility.Hidden)
      EndLifeTxBl.Visibility = If(bShowSpecials, Windows.Visibility.Visible, Windows.Visibility.Hidden)
      EndLifeChBx.Visibility = If(bShowSpecials, Windows.Visibility.Visible, Windows.Visibility.Hidden)
   End Sub

   Private Sub HideToolParams()
      NameTxBl.Visibility = Windows.Visibility.Hidden
      NameTxBx.Visibility = Windows.Visibility.Hidden
      TCPosBrd.Visibility = Windows.Visibility.Hidden
      HeadExitBrd.Visibility = Windows.Visibility.Hidden
      SpeedGpBx.Visibility = Windows.Visibility.Hidden
      FeedGpBx.Visibility = Windows.Visibility.Hidden
      CoolantTxBl.Visibility = Windows.Visibility.Hidden
      CoolantCmBx.Visibility = Windows.Visibility.Hidden
      CorrTxBl.Visibility = Windows.Visibility.Hidden
      CorrTxBx.Visibility = Windows.Visibility.Hidden
      MaxMatTxBl.Visibility = Windows.Visibility.Hidden
      MaxMatTxBx.Visibility = Windows.Visibility.Hidden
      OffsetGpBx.Visibility = Windows.Visibility.Hidden
      AbsorptionBrd.Visibility = Windows.Visibility.Hidden
      UserNotesTxBl.Visibility = Windows.Visibility.Hidden
      UserNotesTxBx.Visibility = Windows.Visibility.Hidden
      SerNbrTxBl.Visibility = Windows.Visibility.Hidden
      SerNbrTxBx.Visibility = Windows.Visibility.Hidden
      CodeTxBl.Visibility = Windows.Visibility.Hidden
      CodeTxBx.Visibility = Windows.Visibility.Hidden
      SupplierTxBl.Visibility = Windows.Visibility.Hidden
      SupplierTxBx.Visibility = Windows.Visibility.Hidden
      EndLifeTxBl.Visibility = Windows.Visibility.Hidden
      EndLifeChBx.Visibility = Windows.Visibility.Hidden
   End Sub

   Private Sub ToolTreeView_PreviewMouseUp(sender As Object, e As MouseButtonEventArgs) Handles ToolTreeView.PreviewMouseUp
      ' Se necessario, chiedo se salvare l'utensile corrente
      If Not SaveCurrTool() Then
         Dim PreviousTool As CustomItem = m_OldItem
         PreviousTool.IsSelected = True
         Exit Sub
      End If
      ' Aggiorno
      If TypeOf ToolTreeView.SelectedItem Is CathegoryItem Then
         Dim SelectedCathegory As CathegoryItem = ToolTreeView.SelectedItem
         SelectedCathegory.IsExpanded = Not SelectedCathegory.IsExpanded
         HideToolParams()
         HideToolDraw()
      ElseIf TypeOf ToolTreeView.SelectedItem Is CustomItem Then
         Dim SelectedTool As CustomItem = ToolTreeView.SelectedItem
         EgtSetCurrentContext(m_MainWindow.m_CurrentProjectPageUC.CurrentProjectScene.GetCtx())
         EgtTdbSetCurrTool(SelectedTool.Name)
         GetToolParams()
         ViewToolParams()
         ViewToolDraw()
      End If
      m_OldItem = ToolTreeView.SelectedItem
   End Sub

   Private Sub ViewToolDraw()
      ' Preparo dati per modello dell'utensile e lo creo
      If Not PrepareToolDraw() OrElse Not CreateToolDraw() Then
         EgtSetCurrentContext(ToolScene.GetCtx())
         EgtNewFile()
      End If
      ' Aggiorno visualizzazione
      EgtSetCurrentContext(ToolScene.GetCtx())
      EgtSetView(VT.TOP, False)
      'EgtSetGenericView(150, -150)
      'EgtSetView(VT.ISO_SE, False)
      EgtZoom(ZM.ALL)
   End Sub

   Private Sub HideToolDraw()
      EgtSetCurrentContext(ToolScene.GetCtx())
      EgtNewFile()
      EgtDraw()
   End Sub

   Private Sub OnMyMouseDownScene(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles ToolScene.OnMouseDownScene
      ' Annullo nome variabile corrente
      m_sCurrVar = String.Empty
      ' Si può selezionare solo con il tasto sinistro e se stato NULL
      If e.Button <> Windows.Forms.MouseButtons.Left Or
         Not ToolScene.IsStatusNull() Then
         Return
      End If
      ' Verifico se selezionato testo di quota
      EgtSetObjFilterForSelWin(False, False, False, False, True)
      Dim nSel As Integer
      EgtSelect(e.Location, Scene.DIM_SEL, Scene.DIM_SEL, nSel)
      Dim nId As Integer = EgtGetFirstObjInSelWin()
      While nId <> GDB_ID.NULL
         ' Recupero eventuale info di variabile
         Dim sVar As String = String.Empty
         If EgtGetInfo(nId, "Var", sVar) Then
            m_sCurrVar = sVar
            EgtSelectObj(nId)
            EgtDraw()
            Exit While
         End If
         nId = EgtGetNextObjInSelWin()
      End While
      ' Se non modifica variabile
      If String.IsNullOrWhiteSpace(m_sCurrVar) Then Return
      ' Recupero il valore della variabile
      Dim dVal As Double
      If Not GetVariableValue(dVal) Then Return
      ' Recupero indice messaggio titolo per calcolatrice
      Dim nMsg As Integer = 0
      Select Case GetVariableType()
         Case MCH_TP.LEN
            nMsg = MSG_TOOLSDBPAGEUC + 21       ' Lunghezza
         Case MCH_TP.DIAM
            nMsg = MSG_TOOLSDBPAGEUC + 22       ' Diametro
         Case MCH_TP.THICK
            nMsg = MSG_TOOLSDBPAGEUC + 23       ' Spessore
         Case MCH_TP.MAXMAT
            nMsg = MSG_TOOLSDBPAGEUC + 24       ' Tagliente
         Case MCH_TP.CORE
            nMsg = 90720                        ' Anima
         Case STEM
            nMsg = 90719                        ' Lunghezza portautensile
      End Select
      ' Predispongo calcolatrice (converto sempre in UIUnits perchè tutte lunghezze)
      Dim EgtCalculator As New EgtCalculatorWD(m_MainWindow, EgtToUiUnits(dVal), 300, WidthType.PIXEL, 0, 0, EgtMsg(nMsg))
      ' Se inserito valore valido e confermato
      If EgtCalculator.DialogResult Then
         RecreateToolDraw(EgtFromUiUnits(EgtCalculator.dResult))
      End If
      ' Aggiorno visualizzazione
      EgtSetCurrentContext(ToolScene.GetCtx())
      EgtDeselectAll()
      EgtSetView(VT.TOP, False)
      EgtZoom(ZM.ALL)
   End Sub

   Private Sub RecreateToolDraw(ByVal dVal As Double)
      ' Salvo il vecchio valore
      Dim dOldVal As Double
      GetVariableValue(dOldVal)
      ' Aggiorno dati utensile
      SetVariableValue(dVal)
      ' Creo utensile
      If CreateToolDraw() Then Return
      ' Creazione fallita, ritorno ai valori precedenti
      SetVariableValue(dOldVal)
      CreateToolDraw()
   End Sub

   Private Function GetVariableValue(ByRef dVal As Double) As Boolean
      ' Recupero valore variabile
      EgtSetCurrentContext(m_MainWindow.m_CurrentProjectPageUC.CurrentProjectScene.GetCtx())
      Dim nVarType As Integer = GetVariableType()
      If m_nToolType <> MCH_TY.SAW_STD OrElse nVarType <> STEM Then
         Return EgtTdbGetCurrToolParam(nVarType, dVal)
      Else
         Dim dL, dCore, dTh As Double
         If EgtTdbGetCurrToolParam(MCH_TP.LEN, dL) AndAlso
            EgtTdbGetCurrToolParam(MCH_TP.CORE, dCore) AndAlso
            EgtTdbGetCurrToolParam(MCH_TP.THICK, dTh) Then
            dVal = Math.Max(dL - (dCore + dTh) / 2, 0.0)
            Return True
         Else
            Return False
         End If
      End If
   End Function

   Private Function SetVariableValue(ByVal dVal As Double) As Boolean
      ' Recupero tipo di variabile
      EgtSetCurrentContext(m_MainWindow.m_CurrentProjectPageUC.CurrentProjectScene.GetCtx())
      Dim nType As Integer = GetVariableType()
      If m_nToolType = MCH_TY.SAW_STD Then
         Select Case nType
            Case MCH_TP.LEN
               ' lunghezza totale maggiorata di 1 mm per non essere riconosciuta come lama piatta
               EgtTdbSetCurrToolParam(MCH_TP.TOTLEN, dVal + 1)
            Case MCH_TP.DIAM
               EgtTdbSetCurrToolParam(MCH_TP.TOTDIAM, dVal)
            Case MCH_TP.THICK
               If m_bNewSawbladeMaker Then
                  Dim dTh, dLen As Double
                  If EgtTdbGetCurrToolParam(MCH_TP.THICK, dTh) AndAlso
                     EgtTdbGetCurrToolParam(MCH_TP.LEN, dLen) Then
                     Dim dNewLen As Double = dLen + ((dVal - dTh) / 2)
                     EgtTdbSetCurrToolParam(MCH_TP.LEN, dNewLen)
                     EgtTdbSetCurrToolParam(MCH_TP.TOTLEN, dNewLen + 1)
                  Else
                     Return False
                  End If
               End If
            Case MCH_TP.CORE
               If m_bNewSawbladeMaker Then
                  Dim dCore, dLen As Double
                  If EgtTdbGetCurrToolParam(MCH_TP.CORE, dCore) AndAlso
                     EgtTdbGetCurrToolParam(MCH_TP.LEN, dLen) Then
                     Dim dNewLen As Double = dLen + ((dVal - dCore) / 2)
                     EgtTdbSetCurrToolParam(MCH_TP.LEN, dNewLen)
                     EgtTdbSetCurrToolParam(MCH_TP.TOTLEN, dNewLen + 1)
                  Else
                     Return False
                  End If
               End If
         End Select
      ElseIf m_nToolType = MCH_TY.DRILL_STD Then
         Select Case nType
            Case MCH_TP.LEN
               EgtTdbSetCurrToolParam(MCH_TP.TOTLEN, dVal)
            Case MCH_TP.DIAM
               EgtTdbSetCurrToolParam(MCH_TP.TOTDIAM, dVal)
         End Select
      ElseIf m_nToolType = MCH_TY.MILL_STD Or
             m_nToolType = MCH_TY.MILL_NOTIP Or
             m_nToolType = MCH_TY.MILL_POLISHING Then
         Select Case nType
            Case MCH_TP.LEN
               EgtTdbSetCurrToolParam(MCH_TP.TOTLEN, dVal)
            Case MCH_TP.DIAM
               EgtTdbSetCurrToolParam(MCH_TP.TOTDIAM, dVal)
         End Select
      ElseIf m_nToolType = MCH_TY.WATERJET Then
         Select Case nType
            Case MCH_TP.LEN
               EgtTdbSetCurrToolParam(MCH_TP.TOTLEN, dVal)
            Case MCH_TP.DIAM
               EgtTdbSetCurrToolParam(MCH_TP.TOTDIAM, dVal)
         End Select
      End If
      If m_nToolType <> MCH_TY.SAW_STD OrElse nType <> STEM Then
         Return EgtTdbSetCurrToolParam(nType, dVal)
      Else
         Dim dCore, dTh As Double
         If EgtTdbGetCurrToolParam(MCH_TP.CORE, dCore) AndAlso
            EgtTdbGetCurrToolParam(MCH_TP.THICK, dTh) Then
            Dim dL As Double = Math.Max(dVal, 0.0) + ((dCore + dTh) / 2)
            EgtTdbSetCurrToolParam(MCH_TP.TOTLEN, dL + 1)
            Return EgtTdbSetCurrToolParam(MCH_TP.LEN, dL)
         Else
            Return False
         End If
      End If
   End Function

   Private Function GetVariableType() As Integer
      Dim nType As Integer = MCH_TP.NONE
      If m_sCurrVar = "LEN" Then
         nType = MCH_TP.LEN
      ElseIf m_sCurrVar = "DIAM" Then
         nType = MCH_TP.DIAM
      ElseIf m_sCurrVar = "THICK" Then
         nType = MCH_TP.THICK
      ElseIf m_sCurrVar = "MAXMAT" Then
         nType = MCH_TP.MAXMAT
      ElseIf m_sCurrVar = "CORE" Then
         nType = MCH_TP.CORE
      ElseIf m_sCurrVar = "STEM" Then
         nType = STEM
      End If
      Return nType
   End Function

   Private Function PrepareToolDraw() As Boolean
      Select Case m_nToolType
         Case MCH_TY.DRILL_STD
            ' assegno parametri geometrici
            EgtSetCurrentContext(m_MainWindow.m_CurrentProjectPageUC.CurrentProjectScene.GetCtx())
            Dim dToolLen As Double
            EgtTdbGetCurrToolParam(MCH_TP.LEN, dToolLen)
            If dToolLen < EPS_SMALL Then
               dToolLen = 120
               EgtTdbSetCurrToolParam(MCH_TP.LEN, dToolLen)
               EgtTdbSetCurrToolParam(MCH_TP.TOTLEN, dToolLen)
               EgtOutLog("ToolLen too small ->" & DoubleToString(dToolLen, 1))
            End If
            Dim dToolDiam As Double
            EgtTdbGetCurrToolParam(MCH_TP.DIAM, dToolDiam)
            If dToolDiam < EPS_SMALL Then
               dToolDiam = 20
               EgtTdbSetCurrToolParam(MCH_TP.DIAM, dToolDiam)
               EgtTdbSetCurrToolParam(MCH_TP.TOTDIAM, dToolDiam)
               EgtOutLog("ToolDiam too small ->" & DoubleToString(dToolDiam, 1))
            End If
            Dim dToolMaxMat As Double
            EgtTdbGetCurrToolParam(MCH_TP.MAXMAT, dToolMaxMat)
            If dToolMaxMat < EPS_SMALL Then
               dToolMaxMat = 40
               EgtTdbSetCurrToolParam(MCH_TP.MAXMAT, dToolMaxMat)
               EgtOutLog("ToolMaxMat too small ->" & DoubleToString(dToolMaxMat, 1))
            End If
            ' passo all'ambiente di disegno dell'utensile
            EgtSetCurrentContext(ToolScene.GetCtx())
            Return True
         Case MCH_TY.SAW_STD
            ' assegno parametri geometrici
            EgtSetCurrentContext(m_MainWindow.m_CurrentProjectPageUC.CurrentProjectScene.GetCtx())
            Dim dToolLen As Double
            EgtTdbGetCurrToolParam(MCH_TP.LEN, dToolLen)
            If dToolLen < EPS_SMALL Then
               dToolLen = 5
               EgtTdbSetCurrToolParam(MCH_TP.LEN, dToolLen)
               EgtTdbSetCurrToolParam(MCH_TP.TOTLEN, dToolLen)
               EgtOutLog("ToolLen too small ->" & DoubleToString(dToolLen, 1))
            End If
            Dim dToolDiam As Double
            EgtTdbGetCurrToolParam(MCH_TP.DIAM, dToolDiam)
            If dToolDiam < EPS_SMALL Then
               dToolDiam = 200
               EgtTdbSetCurrToolParam(MCH_TP.DIAM, dToolDiam)
               EgtTdbSetCurrToolParam(MCH_TP.TOTDIAM, dToolDiam)
               EgtOutLog("ToolDiam too small ->" & DoubleToString(dToolDiam, 1))
            End If
            Dim dToolThick As Double
            EgtTdbGetCurrToolParam(MCH_TP.THICK, dToolThick)
            If dToolThick < EPS_SMALL Then
               dToolThick = 5
               EgtTdbSetCurrToolParam(MCH_TP.THICK, dToolThick)
               EgtOutLog("ToolThick too small ->" & DoubleToString(dToolThick, 1))
            End If
            Dim dToolCore As Double
            EgtTdbGetCurrToolParam(MCH_TP.CORE, dToolCore)
            If dToolCore < EPS_SMALL Then
               dToolCore = If(dToolLen >= dToolThick, dToolThick - 1, 2 * dToolLen - dToolThick)
               EgtTdbSetCurrToolParam(MCH_TP.CORE, dToolCore)
               EgtOutLog("ToolCore too small ->" & DoubleToString(dToolCore, 1))
            End If
            Dim dToolMaxMat As Double
            EgtTdbGetCurrToolParam(MCH_TP.MAXMAT, dToolMaxMat)
            If dToolMaxMat < EPS_SMALL Then
               dToolMaxMat = 40
               EgtTdbSetCurrToolParam(MCH_TP.MAXMAT, dToolMaxMat)
               EgtOutLog("ToolMaxMat too small ->" & DoubleToString(dToolMaxMat, 1))
            End If
            ' passo all'ambiente di disegno dell'utensile
            EgtSetCurrentContext(ToolScene.GetCtx())
            Return True
         Case MCH_TY.MILL_STD
            ' assegno parametri geometrici
            EgtSetCurrentContext(m_MainWindow.m_CurrentProjectPageUC.CurrentProjectScene.GetCtx())
            Dim dToolLen As Double
            EgtTdbGetCurrToolParam(MCH_TP.LEN, dToolLen)
            If dToolLen < EPS_SMALL Then
               dToolLen = 120
               EgtTdbSetCurrToolParam(MCH_TP.LEN, dToolLen)
               EgtTdbSetCurrToolParam(MCH_TP.TOTLEN, dToolLen)
               EgtOutLog("ToolLen too small ->" & DoubleToString(dToolLen, 1))
            End If
            Dim dToolDiam As Double
            EgtTdbGetCurrToolParam(MCH_TP.DIAM, dToolDiam)
            If dToolDiam < EPS_SMALL Then
               dToolDiam = 20
               EgtTdbSetCurrToolParam(MCH_TP.DIAM, dToolDiam)
               EgtTdbSetCurrToolParam(MCH_TP.TOTDIAM, dToolDiam)
               EgtOutLog("ToolDiam too small ->" & DoubleToString(dToolDiam, 1))
            End If
            Dim dToolMaxMat As Double
            EgtTdbGetCurrToolParam(MCH_TP.MAXMAT, dToolMaxMat)
            If dToolMaxMat < EPS_SMALL Then
               dToolMaxMat = 40
               EgtTdbSetCurrToolParam(MCH_TP.MAXMAT, dToolMaxMat)
               EgtOutLog("ToolMaxMat too small ->" & DoubleToString(dToolMaxMat, 1))
            End If
            ' passo all'ambiente di disegno dell'utensile
            EgtSetCurrentContext(ToolScene.GetCtx())
            Return True
         Case MCH_TY.MILL_NOTIP, MCH_TY.MILL_POLISHING
            ' assegno parametri geometrici
            EgtSetCurrentContext(m_MainWindow.m_CurrentProjectPageUC.CurrentProjectScene.GetCtx())
            Dim dToolLen As Double
            EgtTdbGetCurrToolParam(MCH_TP.LEN, dToolLen)
            If dToolLen < EPS_SMALL Then
               dToolLen = 120
               EgtTdbSetCurrToolParam(MCH_TP.LEN, dToolLen)
               EgtTdbSetCurrToolParam(MCH_TP.TOTLEN, dToolLen)
               EgtOutLog("ToolLen too small ->" & DoubleToString(dToolLen, 1))
            End If
            Dim dToolDiam As Double
            EgtTdbGetCurrToolParam(MCH_TP.DIAM, dToolDiam)
            If dToolDiam < EPS_SMALL Then
               dToolDiam = 20
               EgtTdbSetCurrToolParam(MCH_TP.DIAM, dToolDiam)
               EgtTdbSetCurrToolParam(MCH_TP.TOTDIAM, dToolDiam)
               EgtOutLog("ToolDiam too small ->" & DoubleToString(dToolDiam, 1))
            End If
            Dim dToolMaxMat As Double
            EgtTdbGetCurrToolParam(MCH_TP.MAXMAT, dToolMaxMat)
            If dToolMaxMat < EPS_SMALL Then
               dToolMaxMat = 40
               EgtTdbSetCurrToolParam(MCH_TP.MAXMAT, dToolMaxMat)
               EgtOutLog("ToolMaxMat too small ->" & DoubleToString(dToolMaxMat, 1))
            End If
            ' passo all'ambiente di disegno dell'utensile
            EgtSetCurrentContext(ToolScene.GetCtx())
            Return True
         Case MCH_TY.WATERJET
            ' assegno parametri geometrici
            EgtSetCurrentContext(m_MainWindow.m_CurrentProjectPageUC.CurrentProjectScene.GetCtx())
            Dim dToolLen As Double
            EgtTdbGetCurrToolParam(MCH_TP.LEN, dToolLen)
            If dToolLen < EPS_SMALL Then
               dToolLen = 120
               EgtTdbSetCurrToolParam(MCH_TP.LEN, dToolLen)
               EgtTdbSetCurrToolParam(MCH_TP.TOTLEN, dToolLen)
               EgtOutLog("ToolLen too small ->" & DoubleToString(dToolLen, 1))
            End If
            Dim dToolDiam As Double
            EgtTdbGetCurrToolParam(MCH_TP.DIAM, dToolDiam)
            If dToolDiam < EPS_SMALL Then
               dToolDiam = 20
               EgtTdbSetCurrToolParam(MCH_TP.DIAM, dToolDiam)
               EgtTdbSetCurrToolParam(MCH_TP.TOTDIAM, dToolDiam)
               EgtOutLog("ToolDiam too small ->" & DoubleToString(dToolDiam, 1))
            End If
            Dim dToolMaxMat As Double
            EgtTdbGetCurrToolParam(MCH_TP.MAXMAT, dToolMaxMat)
            If dToolMaxMat < EPS_SMALL Then
               dToolMaxMat = 40
               EgtTdbSetCurrToolParam(MCH_TP.MAXMAT, dToolMaxMat)
               EgtOutLog("ToolMaxMat too small ->" & DoubleToString(dToolMaxMat, 1))
            End If
            ' passo all'ambiente di disegno dell'utensile
            EgtSetCurrentContext(ToolScene.GetCtx())
            Return True
      End Select
      Return False
   End Function

   Private Function CreateToolDraw() As Boolean
      Dim nErr As Integer = EgtTdbCurrToolDraw(m_MainWindow.m_CurrentProjectPageUC.CurrentProjectScene.GetCtx(), ToolScene.GetCtx())
      EgtSetCurrentContext(ToolScene.GetCtx())
      Return (nErr = 0)
   End Function

   Friend Function SaveCurrTool() As Boolean
      SetToolParams()
      ' Se dati utensile modificati
      If EgtTdbIsCurrToolModified() Then
         Dim SaveCurrToolWnd As EgtMsgBox
         If m_MainWindow.IsSiemensPc Then
            SaveCurrToolWnd = New EgtMsgBox(m_MainWindow, Me.ActualWidth / 15 * 5, EgtMsgBox.WidthType.PIXEL, "", EgtMsg(MSG_EGTMSGBOX + 2), EgtMsgBox.Buttons.YES_NO_CANCEL, EgtMsgBox.Icons.NULL)
         Else
            SaveCurrToolWnd = New EgtMsgBox(m_MainWindow, "", EgtMsg(MSG_EGTMSGBOX + 2), EgtMsgBox.Buttons.YES_NO_CANCEL, EgtMsgBox.Icons.NULL)
         End If
         Select Case SaveCurrToolWnd.m_nPressedBtn
            Case 0    ' Annulla
               Return False
            Case 1    ' Si
               EgtTdbSaveCurrTool()
               m_sToolName = NameTxBx.Text
               Dim CurrTool As CustomItem = TryCast(m_OldItem, CustomItem)
               If CurrTool IsNot Nothing Then
                  CurrTool.Name = m_sToolName
               End If
            Case 2    ' No
               EgtTdbSetCurrTool(m_sToolName)
               GetToolParams()
         End Select
      End If
      Return True
   End Function

   Private Sub NameTxBx_EgtClosed(sender As Object, e As EventArgs) Handles NameTxBx.EgtClosed
      ' Elimino spazi iniziali e finali
      NameTxBx.Text = NameTxBx.Text.Trim()
      ' Se cambiato nome
      If NameTxBx.Text <> m_sToolName Then
         ' Imposto contesto macchina
         EgtSetCurrentContext(m_MainWindow.m_CurrentProjectPageUC.CurrentProjectScene.GetCtx())
         ' Verifico se nome già esistente
         Dim TempName As String = NameTxBx.Text
         EgtTdbGetToolNewName(TempName)
         ' se già esistente, avviso e rifiuto
         If TempName <> NameTxBx.Text Then
            ' Nome già utilizzato
            Dim InfoBox As New EgtMsgBox(m_MainWindow, "", EgtMsg(91104), EgtMsgBox.Buttons.OK, EgtMsgBox.Icons.ESCLAMATION)
            ' Ripristino il precedente
            NameTxBx.Text = m_sToolName
         End If
      End If
   End Sub

End Class