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

Public Class SetUpPage

   ' 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

   Private m_ToolTreeList As New ObservableCollection(Of CathegoryItem)
   Private m_DrillbitsList As CathegoryItem
   Private m_MillsList As CathegoryItem
   Private m_CupsList As CathegoryItem
   Private m_PolishingsList As CathegoryItem
   Private m_SetUpToolsList As New CathegoryItem(String.Empty, 0)

   Private Sub SetUpPage_Initialized(sender As Object, e As EventArgs) Handles Me.Initialized
      ' Posiziono la pagina
      Me.SetValue(Grid.RowProperty, 1)

      ' Assegno la lista utensili come sorgente dell'albero
      ToolTree.ItemsSource = m_ToolTreeList

      If m_CurrMachine.MountedToolConfig = CurrentMachine.MountedToolConfigs.TOOLCHANGER Then
         '' Cambio utensile macchina
         ' Modifico numero colonne della griglia in base al numero di porta utensili disponibili sulla macchina
         Dim ColNum As Integer = CInt( Math.Ceiling( m_CurrMachine.ToolChangerNbr / 5.0))
         If ColNum > 12 Then
            EgtOutLog("TC warning : too many tools (max 12 * 5)")
            ColNum = 12
         End If
         For Index As Integer = 12 To ColNum + 1 Step -1
            For Index2 As Integer = Index * 5 To (Index - 1) * 5 + 1 Step -1
               TlChGrid.Children.Remove(GetTlChPosFromIndex(Index2))
               TlChGrid.Children.Remove(GetTxBxFromIndex(Index2))
            Next
            TlChGrid.ColumnDefinitions.RemoveAt(Index * 2 - 1)
            TlChGrid.ColumnDefinitions.RemoveAt(Index * 2 - 2)
         Next
         ' Modifico il numero di elementi presenti sull'ultima colonna
         For Index As Integer = ColNum * 5 To m_CurrMachine.ToolChangerNbr + 1 Step -1
            TlChGrid.Children.Remove(GetTlChPosFromIndex(Index))
            TlChGrid.Children.Remove(GetTxBxFromIndex(Index))
         Next
         ' Assegno lunghezza alla scrollviewer
         TlChGrid.Width = m_MainWindow.ActualWidth / 15 * 1.6 * ColNum * 2
         ' Carico i nomi dei portautensili
         For Index As Integer = 1 To m_CurrMachine.ToolChangerNbr
            GetTlChPosFromIndex(Index).PositionNameTxBl.Text = m_CurrMachine.ToolChangerName(Index - 1)
         Next
      Else
         SetUpPageGrid.Children.Remove(TlChScVw)
      End If

      '' Cambio utensile manuale
      ' Modifico numero colonne della griglia in base al numero di porta utensili disponibili sulla macchina
      Dim ManColNum As Integer = CInt( Math.Ceiling( m_CurrMachine.ManualToolChangerNbr / 5.0))
         If ManColNum > 12 Then
            EgtOutLog("ManTC warning : too many tools (max 12 * 5)")
            ManColNum = 12
         End If
      For Index As Integer = 12 To ManColNum + 1 Step -1
         For Index2 As Integer = Index * 5 To (Index - 1) * 5 + 1 Step -1
            ManTlChGrid.Children.Remove(GetManTlChPosFromIndex(Index2))
            ManTlChGrid.Children.Remove(GetManTxBxFromIndex(Index2))
         Next
         ManTlChGrid.ColumnDefinitions.RemoveAt(Index * 2 - 1)
         ManTlChGrid.ColumnDefinitions.RemoveAt(Index * 2 - 2)
      Next
      ' Modifico il numero di elementi presenti sull'ultima colonna
      For Index As Integer = ManColNum * 5 To m_CurrMachine.ManualToolChangerNbr + 1 Step -1
         ManTlChGrid.Children.Remove(GetManTlChPosFromIndex(Index))
         ManTlChGrid.Children.Remove(GetManTxBxFromIndex(Index))
      Next
      ' Assegno lunghezza alla scrollviewer
      ManTlChGrid.Width = m_MainWindow.ActualWidth / 15 * 1.6 * ManColNum * 2
      ' Carico i nomi dei portautensili
      For Index As Integer = 1 To m_CurrMachine.ManualToolChangerNbr
         GetManTlChPosFromIndex(Index).PositionNameTxBl.Text = m_CurrMachine.ManualToolChangerName(Index - 1)
      Next

      ' Se portautensili della macchina presente
      If m_CurrMachine.MountedToolConfig = CurrentMachine.MountedToolConfigs.TOOLCHANGER Then
         ' Apro quello
         TlChScVw.Visibility = Windows.Visibility.Visible
         ToolChangerBtn.IsChecked = True
         ' Lascio bottoni di scelta solo se ci sono anche utensili manuali
         If m_CurrMachine.ManualToolChangerNbr = 0 Then
            ToolChangerBtn.Visibility = Windows.Visibility.Hidden
            ManualToolChangerBtn.Visibility = Windows.Visibility.Hidden
         End If
      Else
         ' Altimenti apro portautensili manuale ed elimino bottoni di scelta
         ManTlChScVw.Visibility = Windows.Visibility.Visible
         ToolChangerBtn.Visibility = Windows.Visibility.Hidden
         ManualToolChangerBtn.Visibility = Windows.Visibility.Hidden
      End If

   End Sub

   Private Sub SetUpPage_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
      ' Inizializzo le famiglie di utensili nell'albero
      EgtSetCurrentContext(m_MainWindow.m_CurrentProjectPageUC.CurrentProjectScene.GetCtx())
      InitializeToolGroup( m_CurrMachine.bDrill, MCH_TY.DRILL_STD, EgtMsg( 90752), m_DrillbitsList)
      InitializeToolGroup( m_CurrMachine.bMill, MCH_TY.MILL_STD, EgtMsg( 90753), m_MillsList)
      InitializeToolGroup( m_CurrMachine.bCupWheel, MCH_TY.MILL_NOTIP, EgtMsg( 90754), m_CupsList)
      InitializeToolGroup( m_CurrMachine.bPolishingWheel, MCH_TY.MILL_POLISHING, EgtMsg( 90756), m_PolishingsList)

      ' Posiziono utensili già posizionati sul portautensili
      For Each ToolPosition As ToolChangerPos In m_CurrMachine.ToolChanger
         If ToolPosition.sTool <> String.Empty Then
            If Not IsNothing( m_DrillbitsList) Then
               For Each Item As CustomItem In m_DrillbitsList.Items
                  If Item.Name = ToolPosition.sTool Then
                     m_SetUpToolsList.Items.Add(Item)
                     m_DrillbitsList.Items.Remove(Item)
                     Exit For
                  End If
               Next
            End If
            If Not IsNothing( m_MillsList) Then
               For Each Item As CustomItem In m_MillsList.Items
                  If Item.Name = ToolPosition.sTool Then
                     m_SetUpToolsList.Items.Add(Item)
                     m_MillsList.Items.Remove(Item)
                     Exit For
                  End If
               Next
            End If
            If Not IsNothing( m_CupsList) Then
               For Each Item As CustomItem In m_CupsList.Items
                  If Item.Name = ToolPosition.sTool Then
                     m_SetUpToolsList.Items.Add(Item)
                     m_CupsList.Items.Remove(Item)
                     Exit For
                  End If
               Next
            End If
            If Not IsNothing( m_PolishingsList) Then
               For Each Item As CustomItem In m_PolishingsList.Items
                  If Item.Name = ToolPosition.sTool Then
                     m_SetUpToolsList.Items.Add(Item)
                     m_PolishingsList.Items.Remove(Item)
                     Exit For
                  End If
               Next
            End If
            GetTlChPosFromIndex(ToolPosition.nPosition).Tool.IsChecked = True
            GetTxBxFromIndex(ToolPosition.nPosition).Text = ToolPosition.sTool
         End If
      Next

      ' Posiziono utensili già posizionati sul portautensili manuale
      For Each ToolPosition As ToolChangerPos In m_CurrMachine.ManualToolChanger
         If ToolPosition.sTool <> String.Empty Then
            If Not IsNothing( m_DrillbitsList) Then
               For Each Item As CustomItem In m_DrillbitsList.Items
                  If Item.Name = ToolPosition.sTool Then
                     m_SetUpToolsList.Items.Add(Item)
                     m_DrillbitsList.Items.Remove(Item)
                     Exit For
                  End If
               Next
            End If
            If Not IsNothing( m_MillsList) Then
               For Each Item As CustomItem In m_MillsList.Items
                  If Item.Name = ToolPosition.sTool Then
                     m_SetUpToolsList.Items.Add(Item)
                     m_MillsList.Items.Remove(Item)
                     Exit For
                  End If
               Next
            End If
            If Not IsNothing( m_CupsList) Then
               For Each Item As CustomItem In m_CupsList.Items
                  If Item.Name = ToolPosition.sTool Then
                     m_SetUpToolsList.Items.Add(Item)
                     m_CupsList.Items.Remove(Item)
                     Exit For
                  End If
               Next
            End If
            If Not IsNothing( m_PolishingsList) Then
               For Each Item As CustomItem In m_PolishingsList.Items
                  If Item.Name = ToolPosition.sTool Then
                     m_SetUpToolsList.Items.Add(Item)
                     m_PolishingsList.Items.Remove(Item)
                     Exit For
                  End If
               Next
            End If
            GetManTlChPosFromIndex(ToolPosition.nPosition).Tool.IsChecked = True
            GetManTxBxFromIndex(ToolPosition.nPosition).Text = ToolPosition.sTool
         End If
      Next

   End Sub

   Private Function GetTlChPosFromIndex(nIndex As Integer) As ToolChangerPosUC
      Select Case nIndex
         Case 1
            Return TlChPos1
         Case 2
            Return TlChPos2
         Case 3
            Return TlChPos3
         Case 4
            Return TlChPos4
         Case 5
            Return TlChPos5
         Case 6
            Return TlChPos6
         Case 7
            Return TlChPos7
         Case 8
            Return TlChPos8
         Case 9
            Return TlChPos9
         Case 10
            Return TlChPos10
         Case 11
            Return TlChPos11
         Case 12
            Return TlChPos12
         Case 13
            Return TlChPos13
         Case 14
            Return TlChPos14
         Case 15
            Return TlChPos15
         Case 16
            Return TlChPos16
         Case 17
            Return TlChPos17
         Case 18
            Return TlChPos18
         Case 19
            Return TlChPos19
         Case 20
            Return TlChPos20
         Case 21
            Return TlChPos21
         Case 22
            Return TlChPos22
         Case 23
            Return TlChPos23
         Case 24
            Return TlChPos24
         Case 25
            Return TlChPos25
         Case 26
            Return TlChPos26
         Case 27
            Return TlChPos27
         Case 28
            Return TlChPos28
         Case 29
            Return TlChPos29
         Case 30
            Return TlChPos30
         Case 31
            Return TlChPos31
         Case 32
            Return TlChPos32
         Case 33
            Return TlChPos33
         Case 34
            Return TlChPos34
         Case 35
            Return TlChPos35
         Case 36
            Return TlChPos36
         Case 37
            Return TlChPos37
         Case 38
            Return TlChPos38
         Case 39
            Return TlChPos39
         Case 40
            Return TlChPos40
         Case 41
            Return TlChPos41
         Case 42
            Return TlChPos42
         Case 43
            Return TlChPos43
         Case 44
            Return TlChPos44
         Case 45
            Return TlChPos45
         Case 46
            Return TlChPos46
         Case 47
            Return TlChPos47
         Case 48
            Return TlChPos48
         Case 49
            Return TlChPos49
         Case 50
            Return TlChPos50
         Case 51
            Return TlChPos51
         Case 52
            Return TlChPos52
         Case 53
            Return TlChPos53
         Case 54
            Return TlChPos54
         Case 55
            Return TlChPos55
         Case 56
            Return TlChPos56
         Case 57
            Return TlChPos57
         Case 58
            Return TlChPos58
         Case 59
            Return TlChPos59
         Case 60
            Return TlChPos60
         Case Else
            Return Nothing
      End Select
   End Function

   Private Function GetManTlChPosFromIndex(nIndex As Integer) As ToolChangerPosUC
      Select Case nIndex
         Case 1
            Return ManTlChPos1
         Case 2
            Return ManTlChPos2
         Case 3
            Return ManTlChPos3
         Case 4
            Return ManTlChPos4
         Case 5
            Return ManTlChPos5
         Case 6
            Return ManTlChPos6
         Case 7
            Return ManTlChPos7
         Case 8
            Return ManTlChPos8
         Case 9
            Return ManTlChPos9
         Case 10
            Return ManTlChPos10
         Case 11
            Return ManTlChPos11
         Case 12
            Return ManTlChPos12
         Case 13
            Return ManTlChPos13
         Case 14
            Return ManTlChPos14
         Case 15
            Return ManTlChPos15
         Case 16
            Return ManTlChPos16
         Case 17
            Return ManTlChPos17
         Case 18
            Return ManTlChPos18
         Case 19
            Return ManTlChPos19
         Case 20
            Return ManTlChPos20
         Case 21
            Return ManTlChPos21
         Case 22
            Return ManTlChPos22
         Case 23
            Return ManTlChPos23
         Case 24
            Return ManTlChPos24
         Case 25
            Return ManTlChPos25
         Case 26
            Return ManTlChPos26
         Case 27
            Return ManTlChPos27
         Case 28
            Return ManTlChPos28
         Case 29
            Return ManTlChPos29
         Case 30
            Return ManTlChPos30
         Case 31
            Return ManTlChPos31
         Case 32
            Return ManTlChPos32
         Case 33
            Return ManTlChPos33
         Case 34
            Return ManTlChPos34
         Case 35
            Return ManTlChPos35
         Case 36
            Return ManTlChPos36
         Case 37
            Return ManTlChPos37
         Case 38
            Return ManTlChPos38
         Case 39
            Return ManTlChPos39
         Case 40
            Return ManTlChPos40
         Case 41
            Return ManTlChPos41
         Case 42
            Return ManTlChPos42
         Case 43
            Return ManTlChPos43
         Case 44
            Return ManTlChPos44
         Case 45
            Return ManTlChPos45
         Case 46
            Return ManTlChPos46
         Case 47
            Return ManTlChPos47
         Case 48
            Return ManTlChPos48
         Case 49
            Return ManTlChPos49
         Case 50
            Return ManTlChPos50
         Case 51
            Return ManTlChPos51
         Case 52
            Return ManTlChPos52
         Case 53
            Return ManTlChPos53
         Case 54
            Return ManTlChPos54
         Case 55
            Return ManTlChPos55
         Case 56
            Return ManTlChPos56
         Case 57
            Return ManTlChPos57
         Case 58
            Return ManTlChPos58
         Case 59
            Return ManTlChPos59
         Case 60
            Return ManTlChPos60
         Case Else
            Return Nothing
      End Select
   End Function

   Private Function GetTxBxFromIndex(nIndex As Integer) As TextBox
      Select Case nIndex
         Case 1
            Return TxBx1
         Case 2
            Return TxBx2
         Case 3
            Return TxBx3
         Case 4
            Return TxBx4
         Case 5
            Return TxBx5
         Case 6
            Return TxBx6
         Case 7
            Return TxBx7
         Case 8
            Return TxBx8
         Case 9
            Return TxBx9
         Case 10
            Return TxBx10
         Case 11
            Return TxBx11
         Case 12
            Return TxBx12
         Case 13
            Return TxBx13
         Case 14
            Return TxBx14
         Case 15
            Return TxBx15
         Case 16
            Return TxBx16
         Case 17
            Return TxBx17
         Case 18
            Return TxBx18
         Case 19
            Return TxBx19
         Case 20
            Return TxBx20
         Case 21
            Return TxBx21
         Case 22
            Return TxBx22
         Case 23
            Return TxBx23
         Case 24
            Return TxBx24
         Case 25
            Return TxBx25
         Case 26
            Return TxBx26
         Case 27
            Return TxBx27
         Case 28
            Return TxBx28
         Case 29
            Return TxBx29
         Case 30
            Return TxBx30
         Case 31
            Return TxBx31
         Case 32
            Return TxBx32
         Case 33
            Return TxBx33
         Case 34
            Return TxBx34
         Case 35
            Return TxBx35
         Case 36
            Return TxBx36
         Case 37
            Return TxBx37
         Case 38
            Return TxBx38
         Case 39
            Return TxBx39
         Case 40
            Return TxBx40
         Case 41
            Return TxBx41
         Case 42
            Return TxBx42
         Case 43
            Return TxBx43
         Case 44
            Return TxBx44
         Case 45
            Return TxBx45
         Case 46
            Return TxBx46
         Case 47
            Return TxBx47
         Case 48
            Return TxBx48
         Case 49
            Return TxBx49
         Case 50
            Return TxBx50
         Case 51
            Return TxBx51
         Case 52
            Return TxBx52
         Case 53
            Return TxBx53
         Case 54
            Return TxBx54
         Case 55
            Return TxBx55
         Case 56
            Return TxBx56
         Case 57
            Return TxBx57
         Case 58
            Return TxBx58
         Case 59
            Return TxBx59
         Case 60
            Return TxBx60
         Case Else
            Return Nothing
      End Select
   End Function

   Private Function GetManTxBxFromIndex(nIndex As Integer) As TextBox
      Select Case nIndex
         Case 1
            Return ManTxBx1
         Case 2
            Return ManTxBx2
         Case 3
            Return ManTxBx3
         Case 4
            Return ManTxBx4
         Case 5
            Return ManTxBx5
         Case 6
            Return ManTxBx6
         Case 7
            Return ManTxBx7
         Case 8
            Return ManTxBx8
         Case 9
            Return ManTxBx9
         Case 10
            Return ManTxBx10
         Case 11
            Return ManTxBx11
         Case 12
            Return ManTxBx12
         Case 13
            Return ManTxBx13
         Case 14
            Return ManTxBx14
         Case 15
            Return ManTxBx15
         Case 16
            Return ManTxBx16
         Case 17
            Return ManTxBx17
         Case 18
            Return ManTxBx18
         Case 19
            Return ManTxBx19
         Case 20
            Return ManTxBx20
         Case 21
            Return ManTxBx21
         Case 22
            Return ManTxBx22
         Case 23
            Return ManTxBx23
         Case 24
            Return ManTxBx24
         Case 25
            Return ManTxBx25
         Case 26
            Return ManTxBx26
         Case 27
            Return ManTxBx27
         Case 28
            Return ManTxBx28
         Case 29
            Return ManTxBx29
         Case 30
            Return ManTxBx30
         Case 31
            Return ManTxBx31
         Case 32
            Return ManTxBx32
         Case 33
            Return ManTxBx33
         Case 34
            Return ManTxBx34
         Case 35
            Return ManTxBx35
         Case 36
            Return ManTxBx36
         Case 37
            Return ManTxBx37
         Case 38
            Return ManTxBx38
         Case 39
            Return ManTxBx39
         Case 40
            Return ManTxBx40
         Case 41
            Return ManTxBx41
         Case 42
            Return ManTxBx42
         Case 43
            Return ManTxBx43
         Case 44
            Return ManTxBx44
         Case 45
            Return ManTxBx45
         Case 46
            Return ManTxBx46
         Case 47
            Return ManTxBx47
         Case 48
            Return ManTxBx48
         Case 49
            Return ManTxBx49
         Case 50
            Return ManTxBx50
         Case 51
            Return ManTxBx51
         Case 52
            Return ManTxBx52
         Case 53
            Return ManTxBx53
         Case 54
            Return ManTxBx54
         Case 55
            Return ManTxBx55
         Case 56
            Return ManTxBx56
         Case 57
            Return ManTxBx57
         Case 58
            Return ManTxBx58
         Case 59
            Return ManTxBx59
         Case 60
            Return ManTxBx60
         Case Else
            Return Nothing
      End Select
   End Function

   ' Funzione che carica gli utensili del Db nell'albero
   Friend Sub InitializeToolGroup( bEnabled As Boolean, nTType As Integer, sFName As String, ByRef ToolCathegory As CathegoryItem)
      If Not bEnabled Then Return
      ' Inserisco categoria ed eventuali elementi
      ToolCathegory = 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
      m_ToolTreeList.Add(ToolCathegory)
   End Sub

   ' Funzione che attrezza l'utensile quando viene clickato il portautensile
   Private Sub ToolHolder_ToolClick(sender As Object, e As RoutedEventArgs) Handles TlChPos1.ToolClick, TlChPos2.ToolClick, TlChPos3.ToolClick, TlChPos4.ToolClick, TlChPos5.ToolClick, TlChPos6.ToolClick, TlChPos7.ToolClick, TlChPos8.ToolClick,
                                                                                    TlChPos9.ToolClick, TlChPos10.ToolClick, TlChPos11.ToolClick, TlChPos12.ToolClick, TlChPos13.ToolClick, TlChPos14.ToolClick, TlChPos15.ToolClick, TlChPos16.ToolClick,
                                                                                    TlChPos17.ToolClick, TlChPos18.ToolClick, TlChPos19.ToolClick, TlChPos20.ToolClick, TlChPos21.ToolClick, TlChPos22.ToolClick, TlChPos23.ToolClick, TlChPos24.ToolClick,
                                                                                    TlChPos25.ToolClick, TlChPos26.ToolClick, TlChPos27.ToolClick, TlChPos28.ToolClick, TlChPos29.ToolClick, TlChPos30.ToolClick, TlChPos31.ToolClick, TlChPos32.ToolClick,
                                                                                    TlChPos33.ToolClick, TlChPos34.ToolClick, TlChPos35.ToolClick, TlChPos36.ToolClick, TlChPos37.ToolClick, TlChPos38.ToolClick, TlChPos39.ToolClick, TlChPos40.ToolClick,
                                                                                    TlChPos41.ToolClick, TlChPos42.ToolClick, TlChPos43.ToolClick, TlChPos44.ToolClick, TlChPos45.ToolClick, TlChPos46.ToolClick, TlChPos47.ToolClick, TlChPos48.ToolClick,
                                                                                    TlChPos49.ToolClick, TlChPos50.ToolClick, TlChPos51.ToolClick, TlChPos52.ToolClick, TlChPos53.ToolClick, TlChPos54.ToolClick, TlChPos55.ToolClick, TlChPos56.ToolClick,
                                                                                    TlChPos57.ToolClick, TlChPos58.ToolClick, TlChPos59.ToolClick, TlChPos60.ToolClick,
                                                                                    ManTlChPos1.ToolClick, ManTlChPos2.ToolClick, ManTlChPos3.ToolClick, ManTlChPos4.ToolClick, ManTlChPos5.ToolClick, ManTlChPos6.ToolClick, ManTlChPos7.ToolClick,
                                                                                    ManTlChPos8.ToolClick, ManTlChPos9.ToolClick, ManTlChPos10.ToolClick, ManTlChPos11.ToolClick, ManTlChPos12.ToolClick, ManTlChPos13.ToolClick, ManTlChPos14.ToolClick,
                                                                                    ManTlChPos15.ToolClick, ManTlChPos16.ToolClick, ManTlChPos17.ToolClick, ManTlChPos18.ToolClick, ManTlChPos19.ToolClick, ManTlChPos20.ToolClick, ManTlChPos21.ToolClick,
                                                                                    ManTlChPos22.ToolClick, ManTlChPos23.ToolClick, ManTlChPos24.ToolClick, ManTlChPos25.ToolClick, ManTlChPos26.ToolClick, ManTlChPos27.ToolClick, ManTlChPos28.ToolClick,
                                                                                    ManTlChPos29.ToolClick, ManTlChPos30.ToolClick, ManTlChPos31.ToolClick, ManTlChPos32.ToolClick, ManTlChPos33.ToolClick, ManTlChPos34.ToolClick, ManTlChPos35.ToolClick,
                                                                                    ManTlChPos36.ToolClick, ManTlChPos37.ToolClick, ManTlChPos38.ToolClick, ManTlChPos39.ToolClick, ManTlChPos40.ToolClick, ManTlChPos41.ToolClick, ManTlChPos42.ToolClick,
                                                                                    ManTlChPos43.ToolClick, ManTlChPos44.ToolClick, ManTlChPos45.ToolClick, ManTlChPos46.ToolClick, ManTlChPos47.ToolClick, ManTlChPos48.ToolClick, ManTlChPos49.ToolClick,
                                                                                    ManTlChPos50.ToolClick, ManTlChPos51.ToolClick, ManTlChPos52.ToolClick, ManTlChPos53.ToolClick, ManTlChPos54.ToolClick, ManTlChPos55.ToolClick, ManTlChPos56.ToolClick,
                                                                                    ManTlChPos57.ToolClick, ManTlChPos58.ToolClick, ManTlChPos59.ToolClick, ManTlChPos60.ToolClick
      Dim ToolHolderClicked As ToolChangerPosUC = e.Source
      ' Se da posizionare
      If ToolHolderClicked.Tool.IsChecked() Then
         ' Se utensile valido 
         If Not IsNothing(ToolTree.SelectedItem) And TypeOf ToolTree.SelectedItem Is CustomItem Then
            Dim Tool As CustomItem = DirectCast(ToolTree.SelectedItem, CustomItem)
            m_SetUpToolsList.Items.Add(Tool)
            ' inserisco in posizione del TC
            GetTxBxFromTlChPos(ToolHolderClicked.Name).Text = Tool.Name
            ' tolgo dalla lista dei disponibili
            RemoveToolFromList()
            ' aggiorno posizione in DB utensili
            EgtTdbSetCurrTool(Tool.Name)
            EgtTdbSetCurrToolParam(MCH_TP.TCPOS, ToolHolderClicked.PositionNameTxBl.Text)
            EgtTdbSaveCurrTool()
         Else
            ToolHolderClicked.Tool.IsChecked = False
            GetTxBxFromTlChPos(ToolHolderClicked.Name).Text = String.Empty
         End If
      ' altrimenti da levare
      Else
         RestoreToolInList(GetTxBxFromTlChPos(ToolHolderClicked.Name).Text)
         GetTxBxFromTlChPos(ToolHolderClicked.Name).Text = String.Empty
      End If

   End Sub

   ' Funzione che rimuove l'utensile selezionato dall'albero
   Private Sub RemoveToolFromList()
      If Not IsNothing( m_DrillbitsList) AndAlso m_DrillbitsList.Items.Contains( ToolTree.SelectedItem) Then
         m_DrillbitsList.Items.Remove( ToolTree.SelectedItem)
      ElseIf Not IsNothing( m_MillsList) AndAlso  m_MillsList.Items.Contains( ToolTree.SelectedItem) Then
         m_MillsList.Items.Remove( ToolTree.SelectedItem)
      ElseIf Not IsNothing( m_CupsList) AndAlso  m_CupsList.Items.Contains( ToolTree.SelectedItem) Then
         m_CupsList.Items.Remove( ToolTree.SelectedItem)
      ElseIf Not IsNothing( m_PolishingsList) AndAlso  m_PolishingsList.Items.Contains( ToolTree.SelectedItem) Then
         m_PolishingsList.Items.Remove( ToolTree.SelectedItem)
      End If
   End Sub

   ' Funzione che rimette un utensile nell'albero
   Private Sub RestoreToolInList(sToolName As String)
      For Each Tool As CustomItem In m_SetUpToolsList.Items
         If Tool.Name = sToolName Then
            If Tool.nType = MCH_TY.DRILL_STD Then
               m_DrillbitsList.Items.Add(Tool)
            ElseIf Tool.nType = MCH_TY.MILL_STD Then
               m_MillsList.Items.Add(Tool)
            ElseIf Tool.nType = MCH_TY.MILL_NOTIP Then
               m_CupsList.Items.Add(Tool)
            ElseIf Tool.nType = MCH_TY.MILL_POLISHING Then
               m_PolishingsList.Items.Add(Tool)
            End If
            m_SetUpToolsList.Items.Remove(Tool)
            Return
         End If
      Next
   End Sub

   Private Function GetTxBxFromTlChPos(TlChPos As String) As TextBox
      Select Case TlChPos
         Case "TlChPos1"
            Return TxBx1
         Case "TlChPos2"
            Return TxBx2
         Case "TlChPos3"
            Return TxBx3
         Case "TlChPos4"
            Return TxBx4
         Case "TlChPos5"
            Return TxBx5
         Case "TlChPos6"
            Return TxBx6
         Case "TlChPos7"
            Return TxBx7
         Case "TlChPos8"
            Return TxBx8
         Case "TlChPos9"
            Return TxBx9
         Case "TlChPos10"
            Return TxBx10
         Case "TlChPos11"
            Return TxBx11
         Case "TlChPos12"
            Return TxBx12
         Case "TlChPos13"
            Return TxBx13
         Case "TlChPos14"
            Return TxBx14
         Case "TlChPos15"
            Return TxBx15
         Case "TlChPos16"
            Return TxBx16
         Case "TlChPos17"
            Return TxBx17
         Case "TlChPos18"
            Return TxBx18
         Case "TlChPos19"
            Return TxBx19
         Case "TlChPos20"
            Return TxBx20
         Case "TlChPos21"
            Return TxBx21
         Case "TlChPos22"
            Return TxBx22
         Case "TlChPos23"
            Return TxBx23
         Case "TlChPos24"
            Return TxBx24
         Case "TlChPos25"
            Return TxBx25
         Case "TlChPos26"
            Return TxBx26
         Case "TlChPos27"
            Return TxBx27
         Case "TlChPos28"
            Return TxBx28
         Case "TlChPos29"
            Return TxBx29
         Case "TlChPos30"
            Return TxBx30
         Case "TlChPos31"
            Return TxBx31
         Case "TlChPos32"
            Return TxBx32
         Case "TlChPos33"
            Return TxBx33
         Case "TlChPos34"
            Return TxBx34
         Case "TlChPos35"
            Return TxBx35
         Case "TlChPos36"
            Return TxBx36
         Case "TlChPos37"
            Return TxBx37
         Case "TlChPos38"
            Return TxBx38
         Case "TlChPos39"
            Return TxBx39
         Case "TlChPos40"
            Return TxBx40
         Case "TlChPos41"
            Return TxBx41
         Case "TlChPos42"
            Return TxBx42
         Case "TlChPos43"
            Return TxBx43
         Case "TlChPos44"
            Return TxBx44
         Case "TlChPos45"
            Return TxBx45
         Case "TlChPos46"
            Return TxBx46
         Case "TlChPos47"
            Return TxBx47
         Case "TlChPos48"
            Return TxBx48
         Case "TlChPos49"
            Return TxBx49
         Case "TlChPos50"
            Return TxBx50
         Case "TlChPos51"
            Return TxBx51
         Case "TlChPos52"
            Return TxBx52
         Case "TlChPos53"
            Return TxBx53
         Case "TlChPos54"
            Return TxBx54
         Case "TlChPos55"
            Return TxBx55
         Case "TlChPos56"
            Return TxBx56
         Case "TlChPos57"
            Return TxBx57
         Case "TlChPos58"
            Return TxBx58
         Case "TlChPos59"
            Return TxBx59
         Case "TlChPos60"
            Return TxBx60
         Case "ManTlChPos1"
            Return ManTxBx1
         Case "ManTlChPos2"
            Return ManTxBx2
         Case "ManTlChPos3"
            Return ManTxBx3
         Case "ManTlChPos4"
            Return ManTxBx4
         Case "ManTlChPos5"
            Return ManTxBx5
         Case "ManTlChPos6"
            Return ManTxBx6
         Case "ManTlChPos7"
            Return ManTxBx7
         Case "ManTlChPos8"
            Return ManTxBx8
         Case "ManTlChPos9"
            Return ManTxBx9
         Case "ManTlChPos10"
            Return ManTxBx10
         Case "ManTlChPos11"
            Return ManTxBx11
         Case "ManTlChPos12"
            Return ManTxBx12
         Case "ManTlChPos13"
            Return ManTxBx13
         Case "ManTlChPos14"
            Return ManTxBx14
         Case "ManTlChPos15"
            Return ManTxBx15
         Case "ManTlChPos16"
            Return ManTxBx16
         Case "ManTlChPos17"
            Return ManTxBx17
         Case "ManTlChPos18"
            Return ManTxBx18
         Case "ManTlChPos19"
            Return ManTxBx19
         Case "ManTlChPos20"
            Return ManTxBx20
         Case "ManTlChPos21"
            Return ManTxBx21
         Case "ManTlChPos22"
            Return ManTxBx22
         Case "ManTlChPos23"
            Return ManTxBx23
         Case "ManTlChPos24"
            Return ManTxBx24
         Case "ManTlChPos25"
            Return ManTxBx25
         Case "ManTlChPos26"
            Return ManTxBx26
         Case "ManTlChPos27"
            Return ManTxBx27
         Case "ManTlChPos28"
            Return ManTxBx28
         Case "ManTlChPos29"
            Return ManTxBx29
         Case "ManTlChPos30"
            Return ManTxBx30
         Case "ManTlChPos31"
            Return ManTxBx31
         Case "ManTlChPos32"
            Return ManTxBx32
         Case "ManTlChPos33"
            Return ManTxBx33
         Case "ManTlChPos34"
            Return ManTxBx34
         Case "ManTlChPos35"
            Return ManTxBx35
         Case "ManTlChPos36"
            Return ManTxBx36
         Case "ManTlChPos37"
            Return ManTxBx37
         Case "ManTlChPos38"
            Return ManTxBx38
         Case "ManTlChPos39"
            Return ManTxBx39
         Case "ManTlChPos40"
            Return ManTxBx40
         Case "ManTlChPos41"
            Return ManTxBx41
         Case "ManTlChPos42"
            Return ManTxBx42
         Case "ManTlChPos43"
            Return ManTxBx43
         Case "ManTlChPos44"
            Return ManTxBx44
         Case "ManTlChPos45"
            Return ManTxBx45
         Case "ManTlChPos46"
            Return ManTxBx46
         Case "ManTlChPos47"
            Return ManTxBx47
         Case "ManTlChPos48"
            Return ManTxBx48
         Case "ManTlChPos49"
            Return ManTxBx49
         Case "ManTlChPos50"
            Return ManTxBx50
         Case "ManTlChPos51"
            Return ManTxBx51
         Case "ManTlChPos52"
            Return ManTxBx52
         Case "ManTlChPos53"
            Return ManTxBx53
         Case "ManTlChPos54"
            Return ManTxBx54
         Case "ManTlChPos55"
            Return ManTxBx55
         Case "ManTlChPos56"
            Return ManTxBx56
         Case "ManTlChPos57"
            Return ManTxBx57
         Case "ManTlChPos58"
            Return ManTxBx58
         Case "ManTlChPos59"
            Return ManTxBx59
         Case "ManTlChPos60"
            Return ManTxBx60
         Case Else
            Return Nothing
      End Select
   End Function

   Private Sub AutomaticToolChangerBtn_Click(sender As Object, e As RoutedEventArgs) Handles ToolChangerBtn.Click
      If ToolChangerBtn.IsChecked() Then
         ManualToolChangerBtn.IsChecked = False
         TlChScVw.Visibility = Windows.Visibility.Visible
         ManTlChScVw.Visibility = Windows.Visibility.Hidden
      Else
         ToolChangerBtn.IsChecked = True
         ManualToolChangerBtn.IsChecked = False
      End If
   End Sub

   Private Sub ManualToolChangerBtn_Click(sender As Object, e As RoutedEventArgs) Handles ManualToolChangerBtn.Click
      If ManualToolChangerBtn.IsChecked() Then
         ToolChangerBtn.IsChecked = False
         ManTlChScVw.Visibility = Windows.Visibility.Visible
         TlChScVw.Visibility = Windows.Visibility.Hidden
      Else
         ToolChangerBtn.IsChecked = False
         ManualToolChangerBtn.IsChecked = True
      End If
   End Sub

   ' Funzione che salva lo stato dei portautensili
   Private Sub SaveToolChangerStatus()
      ' Salvo attrezzaggio automatico
      For Index As Integer = 1 To m_CurrMachine.ToolChangerNbr
         m_CurrMachine.ToolPos(Index - 1) = GetTxBxFromIndex(Index).Text
      Next
      ' Salvo attrezzaggio manuale
      For Index As Integer = 1 To m_CurrMachine.ManualToolChangerNbr
         m_CurrMachine.ManualToolPos(Index - 1) = GetManTxBxFromIndex(Index).Text
      Next
      ' Salvo DB utensili
      EgtTdbSave()
   End Sub

   ' Evento che permette di aprire le cartelle degli utensili con un click singolo
   Private Sub ToolTree_PreviewMouseUp(sender As Object, e As MouseButtonEventArgs) Handles ToolTree.PreviewMouseUp
      If TypeOf ToolTree.SelectedItem Is CathegoryItem Then
         Dim SelectedCathegory As CathegoryItem = ToolTree.SelectedItem
         SelectedCathegory.IsExpanded = Not SelectedCathegory.IsExpanded
      End If
   End Sub

   Private Sub OkBtn_Click(sender As Object, e As RoutedEventArgs) Handles OkBtn.Click
      ' Eseguo salvataggio (DB utensili + attrezzaggio)
      SaveToolChangerStatus()
      ' Esco
      m_MainWindow.m_MachinePageUC.MachinePageGrid.Children.Add(m_MainWindow.m_MachinePageUC.m_AlarmsPageUC)
      m_MainWindow.m_MachinePageUC.MachinePageGrid.Children.Remove(Me)
   End Sub

   Private Sub ExitBtn_Click(sender As Object, e As RoutedEventArgs) Handles ExitBtn.Click
      ' Ricarico DB utensili (attrezzaggio non viene salvato)
      EgtTdbReload()
      ' Esco
      m_MainWindow.m_MachinePageUC.MachinePageGrid.Children.Add(m_MainWindow.m_MachinePageUC.m_AlarmsPageUC)
      m_MainWindow.m_MachinePageUC.MachinePageGrid.Children.Remove(Me)
   End Sub

   ' Evento che posiziona l'utensile nel suo portautensile predefinito dopo un doppio click
   Private Sub ToolTree_PreviewMouseDoubleClick(sender As Object, e As MouseButtonEventArgs) Handles ToolTree.PreviewMouseDoubleClick
      If TypeOf ToolTree.SelectedItem Is CustomItem Then
         ' Ottengo utensile selezionato
         Dim SelectedTool As CustomItem = ToolTree.SelectedItem
         ' Lo rendo corrente nel DbUtensili per ricavarne la posizione suggerita
         EgtTdbSetCurrTool(SelectedTool.Name)
         Dim ToolPositionName As String = String.Empty
         EgtTdbGetCurrToolParam(MCH_TP.TCPOS, ToolPositionName)
         If String.IsNullOrEmpty(ToolPositionName) Then Return
         ' Cerco il portautensile nella lista degli automatici per vederne lo stato
         For Each TlChPos As ToolChangerPos In m_CurrMachine.ToolChanger
            If TlChPos.sName = ToolPositionName Then
               ' Se la posizione è libera
               If GetTxBxFromIndex(TlChPos.nPosition).Text = String.Empty Then
                  ' Aggiungo l'utensile
                  Dim Tool As CustomItem = ToolTree.SelectedItem
                  m_SetUpToolsList.Items.Add(Tool)
                  GetTxBxFromIndex(TlChPos.nPosition).Text = Tool.Name
                  GetTlChPosFromIndex(TlChPos.nPosition).Tool.IsChecked = True
                  RemoveToolFromList()
               Else
                  ' Altrimenti segnalo che la posizione è già occupata
                  Dim InfoMsgBox As New EgtMsgBox(m_MainWindow, "", EgtMsg( 90759), EgtMsgBox.Buttons.OK, EgtMsgBox.Icons.ESCLAMATION)
               End If
               Return
            End If
         Next
         ' Cerco il portautensile nella lista dei manuali per vederne lo stato
         For Each ManTlChPos As ToolChangerPos In m_CurrMachine.ManualToolChanger
            If ManTlChPos.sName = ToolPositionName Then
               ' Se la posizione è libera
               If GetManTxBxFromIndex(ManTlChPos.nPosition).Text = String.Empty Then
                  ' Aggiungo l'utensile
                  Dim Tool As CustomItem = ToolTree.SelectedItem
                  m_SetUpToolsList.Items.Add(Tool)
                  GetManTxBxFromIndex(ManTlChPos.nPosition).Text = Tool.Name
                  GetManTlChPosFromIndex(ManTlChPos.nPosition).Tool.IsChecked = True
                  RemoveToolFromList()
               Else
                  ' Altrimenti segnalo che la posizione è già occupata
                  Dim InfoMsgBox As New EgtMsgBox(m_MainWindow, "", EgtMsg( 90759), EgtMsgBox.Buttons.OK, EgtMsgBox.Icons.ESCLAMATION)
               End If
               Return
            End If
         Next
      End If
   End Sub

End Class
