Imports System.Collections.ObjectModel
Imports EgtUILib

Public Class ChooseMachining

   ' Riferimento alla MainWindow
   Private m_MainWindow As MainWindow = DirectCast(Application.Current.MainWindow, MainWindow)
   Private m_CurrentMachine As CurrentMachine = m_MainWindow.m_CurrentMachine
   ' Liste che contengono gli elementi appartenenti alle ComboBox
   Private m_SawingList As New List(Of String)
   Private m_AuxMachTypeList As New List(Of StringIdCmBx)
   Private m_DrillingList As New List(Of String)
   Private m_MillingList As New List(Of String)
   Private m_WJettingList As New List(Of String)
   Private m_PocketingList As New List(Of String)
   ' Numero righe della finestra, necessario per non ridimensionarla quando si cambia il tipo di utensile ausiliario
   Private m_RowNumber As Integer = 6

   ' attulmente questa ComboBox è spenta (ma potrebbe essere richiesta in futuro?)
   Private m_bNotShowAuxilaryCmBx As Boolean = False

   Private m_MachIsModified As Boolean = False
   Public ReadOnly Property MachIsModified As Boolean
      Get
         Return m_MachIsModified
      End Get
   End Property

   Sub New(Owner As Window)
      Me.Owner = Owner
      InitializeComponent()
      'ShowDialog()
   End Sub

   Private Sub ChooseMachining_Initialized(sender As Object, e As EventArgs) Handles Me.Initialized
      Me.Top = Owner.Top + Owner.Height / 2 - Me.Height
      Me.Left = Owner.Left + Owner.Width / 2 - Me.Width / 2
      CurrSawingCmBx.ItemsSource = m_SawingList
      AuxiliaryMachiningCmBx.ItemsSource = m_AuxMachTypeList
      CurrDrillingCmBx.ItemsSource = m_DrillingList
      CurrMillingCmBx.ItemsSource = m_MillingList
      CurrPocketingCmBx.ItemsSource = m_PocketingList
      CurrWJettingCmBx.ItemsSource = m_WJettingList
      CurrWJettingQualityCmBx.ItemsSource = m_CurrentMachine.Qualities

      CurrSawingTxBl.Text = EgtMsg(MSG_CHOOSEMACHININGPAGEUC + 1)
      AuxiliaryMachiningTxBl.Text = EgtMsg(MSG_CHOOSEMACHININGPAGEUC + 2)
      CurrDrillingTxBl.Text = EgtMsg(MSG_CHOOSEMACHININGPAGEUC + 3)
      CurrMillingTxBl.Text = EgtMsg(MSG_CHOOSEMACHININGPAGEUC + 4)
      CurrWJettingTxBl.Text = EgtMsg(MSG_CHOOSEMACHININGPAGEUC + 10)
      CurrPocketingTxBl.Text = EgtMsg(90550)

      ' carico l'elenco delle lavorazioni della pagina (anche senza aver caricato la pagina)
      InitializeMachiningLists()
   End Sub

   Private Sub ChooseMachining_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
      ' InitializeMachiningLists()
   End Sub

   Private Sub InitializeMachiningLists()

      ' in assenza dell'uscita H1 nascondo i parametri lama
      If EgtGetHeadId("H1") = GDB_ID.NULL Then
         CurrSawingTxBl.Visibility = Visibility.Hidden
         CurrSawingCmBx.Visibility = Visibility.Hidden
      End If

      ' -- TAGLIO --
      CreateMachiningList(MCH_MY.SAWING, m_CurrentMachine.sCurrSaw, m_SawingList)
      ' aggiungo un campo vuoto
      m_SawingList.Add( "")

      ' Verifico la configurazione della macchina per creare i combobox
      Select Case m_CurrentMachine.MountedToolConfig
         Case CurrentMachine.MountedToolConfigs.SAW
            ' se la macchina non è configurata con altre uscite rimuovo le ComboBox
            ChooseMachiningGrid.Children.Remove(AuxiliaryMachiningTxBl)
            ChooseMachiningGrid.Children.Remove(AuxiliaryMachiningCmBx)

         Case CurrentMachine.MountedToolConfigs.SAWANDAUXTOOL, CurrentMachine.MountedToolConfigs.MANUALTOOLCHANGER, CurrentMachine.MountedToolConfigs.TOOLCHANGER
            ' Creo lista lavorazioni foretto e fresa
            If m_CurrentMachine.MountedToolConfig = CurrentMachine.MountedToolConfigs.SAWANDAUXTOOL Then
               CreateMachiningList(MCH_MY.DRILLING, m_CurrentMachine.sCurrDrill, m_DrillingList)
               ' Se la lista è vuota cancello la lavorazione corrente di questo tipo
               If m_DrillingList.Count = 0 Then
                  m_CurrentMachine.sCurrDrilling = String.Empty
               Else
                  ' aggiungo il campo vuoto
                  m_DrillingList.Add("")
               End If
               CreateMachiningList(MCH_MY.MILLING, m_CurrentMachine.sCurrMill, m_MillingList)
               ' Se la lista è vuota cancello la lavorazione corrente di questo tipo
               If m_MillingList.Count = 0 Then
                  m_CurrentMachine.sCurrMilling = String.Empty
               Else
                  ' aggiungo il campo vuoto
                  m_MillingList.Add("")
               End If
               ' -- SVUOTATURA --
               CreateMachiningList(MCH_MY.POCKETING, m_CurrentMachine.sCurrMillNoTip, m_PocketingList)
               If m_PocketingList.Count = 0 Then
                  m_CurrentMachine.sCurrPocketing = String.Empty
               Else
                  m_PocketingList.Add("")
               End If
               CreateMachiningList(MCH_MY.WATERJETTING, m_CurrentMachine.sCurrWaterJet, m_WJettingList)
               ' Se la lista è vuota cancello la lavorazione corrente di questo tipo
               If m_WJettingList.Count = 0 Then
                  m_CurrentMachine.sCurrWaterJetting = String.Empty
               Else
                  m_WJettingList.Add("")
               End If

               ' ---- MACCHINA CON CAMBIO UTENSILE ----
            ElseIf m_CurrentMachine.MountedToolConfig = CurrentMachine.MountedToolConfigs.MANUALTOOLCHANGER Or
                   m_CurrentMachine.MountedToolConfig = CurrentMachine.MountedToolConfigs.TOOLCHANGER Then
               ' -- FORATURA -- 
               CreateMachiningList(MCH_MY.DRILLING, m_DrillingList)
               ' Se la lista è vuota cancello la lavorazione corrente di questo tipo
               If m_DrillingList.Count = 0 Then
                  m_CurrentMachine.sCurrDrilling = String.Empty
               Else
                  ' aggiungo il campo vuoto
                  m_DrillingList.Add("")
               End If
               ' -- FRESATURA -- 
               CreateMachiningList(MCH_MY.MILLING, m_MillingList)
               ' Se la lista è vuota cancello la lavorazione corrente di questo tipo
               If m_MillingList.Count = 0 Then
                  m_CurrentMachine.sCurrMilling = String.Empty
               Else
                  ' aggiungo il campo vuoto
                  m_MillingList.Add("")
               End If
               ' -- SVUOTATURA --
               CreateMachiningList(MCH_MY.POCKETING, m_PocketingList)
               If m_PocketingList.Count = 0 Then
                  m_CurrentMachine.sCurrPocketing = String.Empty
               Else
                  ' aggiungo il campo vuoto
                  m_PocketingList.Add("")
               End If
               ' -- WATERJET --
               CreateMachiningList(MCH_MY.WATERJETTING, m_WJettingList)
               ' Se la lista è vuota cancello la lavorazione corrente di questo tipo
               If m_WJettingList.Count = 0 Then
                  m_CurrentMachine.sCurrWaterJetting = String.Empty
               Else
                  ' aggiungo il campo vuoto
                  m_WJettingList.Add("")
               End If
            End If

            ' preparazione dello spazio necessario ad ospitare la ComboBox per selezionare la lavorazione secondaria -- DA RIMUOVERE COMPLETAMENTE --
            If m_bNotShowAuxilaryCmBx Then
               ' Preparazione interfaccia, definizione di due righe della tabella con la giusta altezza (per inserire la lavorazione secondaria)
               For Index As Integer = 0 To 1
                  Dim Row As New RowDefinition
                  Row.Height = New GridLength(0.5, GridUnitType.Star)
                  ChooseMachiningGrid.RowDefinitions.Add(Row)
               Next
               AuxiliaryMachiningTxBl.SetValue(Grid.RowProperty, 2)
               AuxiliaryMachiningCmBx.SetValue(Grid.RowProperty, 3)
               ButtonsGrid.SetValue(Grid.RowProperty, 5)
               AuxiliaryMachiningTxBl.Visibility = Windows.Visibility.Visible
               AuxiliaryMachiningCmBx.Visibility = Windows.Visibility.Visible
               Me.Height = 341.2
               m_RowNumber += 2
            Else
               ' se non visualizzato allora rimuovo
               ChooseMachiningGrid.Children.Remove(AuxiliaryMachiningTxBl)
               ChooseMachiningGrid.Children.Remove(AuxiliaryMachiningCmBx)
            End If

      End Select

      ' verifico che lista delle lavorazioni di lama non sia vuoto
      If m_SawingList.Count > 0 Then
         ' provo ad assegnare la lama corrente (se l'associazione fallisce corrispoende ad impostare un campo vuoto)
         CurrSawingCmBx.SelectedItem = m_MainWindow.m_CurrentMachine.sCurrSawing
      Else
         m_MainWindow.m_CurrentMachine.sCurrSawing = String.Empty
      End If
      ' se non riesco a fare l'assegnazione della lama allora elimino il nome della lama salvata
      If String.IsNullOrEmpty(CurrSawingCmBx.SelectedItem) Then
         ' verifico che il nome della lama esista
         If CurrSawingCmBx.SelectedItem <> m_MainWindow.m_CurrentMachine.sCurrSawing Then
            m_MachIsModified = True
         End If
         m_MainWindow.m_CurrentMachine.sCurrSawing = String.Empty
      End If

      ' definizione della lista delle lavorazioni secondarie -- DA RIMUOVERE COMPLETAMENTE --
      If m_bNotShowAuxilaryCmBx Then
         If Not m_CurrentMachine.MountedToolConfig = CurrentMachine.MountedToolConfigs.SAW Then
            If m_CurrentMachine.bDrilling Then
               m_AuxMachTypeList.Add(New StringIdCmBx(1, EgtMsg(MSG_CHOOSEMACHININGPAGEUC + 6)))
               If m_CurrentMachine.bMilling Then
                  m_AuxMachTypeList.Add(New StringIdCmBx(2, EgtMsg(MSG_CHOOSEMACHININGPAGEUC + 7)))
                  If m_CurrentMachine.bPocketing Then
                     m_AuxMachTypeList.Add(New StringIdCmBx(4, EgtMsg(91069)))
                  End If
                  If m_CurrentMachine.MountedToolConfig = CurrentMachine.MountedToolConfigs.MANUALTOOLCHANGER Or
                     m_CurrentMachine.MountedToolConfig = CurrentMachine.MountedToolConfigs.TOOLCHANGER Then
                     m_AuxMachTypeList.Add(New StringIdCmBx(3, EgtMsg(MSG_CHOOSEMACHININGPAGEUC + 8)))
                  End If
               End If
            ElseIf m_CurrentMachine.bMilling Then
               m_AuxMachTypeList.Add(New StringIdCmBx(2, EgtMsg(MSG_CHOOSEMACHININGPAGEUC + 7)))
            ElseIf m_CurrentMachine.bPocketing Then
               m_AuxMachTypeList.Add(New StringIdCmBx(4, EgtMsg(91069)))
            ElseIf m_CurrentMachine.bWaterJetting Then
               m_AuxMachTypeList.Add(New StringIdCmBx(5, EgtMsg(MSG_CHOOSEMACHININGPAGEUC + 11)))
            End If
            ' Aggiungo "Nessuna" come ultimo elemento della lista
            m_AuxMachTypeList.Add(New StringIdCmBx(0, EgtMsg(MSG_CHOOSEMACHININGPAGEUC + 5)))

            If m_CurrentMachine.sCurrDrilling <> String.Empty Then
               If m_CurrentMachine.sCurrMilling <> String.Empty Then
                  AuxiliaryMachiningCmBx.SelectedItem = StringIdCmBx.FromIdToStringIdCmBx(3, m_AuxMachTypeList)
               Else
                  AuxiliaryMachiningCmBx.SelectedItem = StringIdCmBx.FromIdToStringIdCmBx(1, m_AuxMachTypeList)
               End If
            ElseIf m_CurrentMachine.sCurrMilling <> String.Empty Then
               AuxiliaryMachiningCmBx.SelectedItem = StringIdCmBx.FromIdToStringIdCmBx(2, m_AuxMachTypeList)
            ElseIf m_CurrentMachine.sCurrWaterJetting <> String.Empty Then
               AuxiliaryMachiningCmBx.SelectedItem = StringIdCmBx.FromIdToStringIdCmBx(4, m_AuxMachTypeList)
            Else
               AuxiliaryMachiningCmBx.SelectedItem = StringIdCmBx.FromIdToStringIdCmBx(0, m_AuxMachTypeList)
            End If
         End If
      End If

      RefreshMachiningPage()

   End Sub

   Private Sub CreateMachiningList(MachiningType As Integer, CurrTool As String, MachiningList As List(Of String))
      ' Recupero UUID dell'utensile
      Dim sTuuid As String = String.Empty
      EgtTdbSetCurrTool(CurrTool)
      EgtTdbGetCurrToolParam(MCH_TP.UUID, sTuuid)
      ' Cerco tutte le lavorazioni che utilizzano l'utensile corrente e le aggiungo alla lista
      Dim MachiningName As String = String.Empty
      Dim bOk As Boolean = EgtMdbGetFirstMachining(MachiningType, MachiningName)
      While bOk
         EgtMdbSetCurrMachining(MachiningName)
         Dim sMachTuuid As String = String.Empty
         EgtMdbGetCurrMachiningParam(MCH_MP.TUUID, sMachTuuid)
         If sMachTuuid = sTuuid And VerifyMatThickCompatibility() Then
            MachiningList.Add(MachiningName)
         End If
         bOk = EgtMdbGetNextMachining(MachiningType, MachiningName)
      End While
   End Sub

   ' Funzione che data la stringa di materiali e spessori della lavorazione, e i dati del grezzo corrente, restituisce se la lavorazione è valida oppure no
   Private Function VerifyMatThickCompatibility() As Boolean
      If Not IsNothing(m_CurrentMachine.CurrMat) Then
         Dim SysNotes As String = String.Empty
         EgtMdbGetCurrMachiningParam(MCH_MP.SYSNOTES, SysNotes)
         If SysNotes <> String.Empty Then
            Dim MachiningMaterials() = SysNotes.Split(";".ToCharArray)
            SysNotes = String.Empty
            If m_CurrentMachine.bWaterJet And m_CurrentMachine.bFromDBWaterJet Then
               For Each Material As Object In MachiningMaterials
                  Dim Param() As String = Material.Split(",".ToCharArray)
                  Dim SubParam() As String = Param(0).Split(".".ToCharArray)
                  If SubParam(0) = m_CurrentMachine.CurrMat.nId.ToString() AndAlso SubParam.Count > 1 AndAlso SubParam(1) = m_CurrentMachine.CurrMat.SubId.ToString() Then
                     Dim dRawHeight = GetRawHeight()
                     Dim MatMinH As Double = 0
                     StringToDouble(Param(1), MatMinH)
                     Dim MatMaxH As Double = 0
                     StringToDouble(Param(2), MatMaxH)
                     If dRawHeight > MatMinH - EPS_SMALL And dRawHeight < MatMaxH + EPS_SMALL Then
                        Return True
                     End If
                  End If
               Next
            Else
               For Each Material As Object In MachiningMaterials
                  Dim Param() As String = Material.Split(",".ToCharArray)
                  If Param(0) = m_CurrentMachine.CurrMat.nId.ToString() Then
                     Dim dRawHeight = GetRawHeight()
                     Dim MatMinH As Double = 0
                     StringToDouble(Param(1), MatMinH)
                     Dim MatMaxH As Double = 0
                     StringToDouble(Param(2), MatMaxH)
                     If dRawHeight > MatMinH - EPS_SMALL And dRawHeight < MatMaxH + EPS_SMALL Then
                        Return True
                     End If
                  End If
               Next
            End If


            Return False
         Else
            Return False
         End If
      Else
         Return True
      End If
   End Function

   Private Sub CreateMachiningList(MachiningType As Integer, MachiningList As List(Of String))
      ' Recupero UUID di tutti gli utensili attrezzati (nel ToolChanger e nel ManualToolChanger)
      Dim TuuidList As New List(Of String)
      For Each ToolChangerPos As ToolChangerPos In m_CurrentMachine.ToolChanger
         Dim sTuuid As String = String.Empty
         EgtTdbSetCurrTool(ToolChangerPos.sTool)
         EgtTdbGetCurrToolParam(MCH_TP.UUID, sTuuid)
         TuuidList.Add(sTuuid)
      Next
      For Each ToolChangerPos As ToolChangerPos In m_CurrentMachine.ManualToolChanger
         Dim sTuuid As String = String.Empty
         EgtTdbSetCurrTool(ToolChangerPos.sTool)
         EgtTdbGetCurrToolParam(MCH_TP.UUID, sTuuid)
         TuuidList.Add(sTuuid)
      Next
      ' Cerco tutte le lavorazioni che utilizzano un utensile presente e le aggiungo alla lista
      Dim MachiningName As String = String.Empty
      Dim bOk As Boolean = EgtMdbGetFirstMachining(MachiningType, MachiningName)
      While bOk
         ' Recupero UUID dell'utensile richiamato dalla lavorazione
         EgtMdbSetCurrMachining(MachiningName)
         Dim sMachTuuid As String = String.Empty
         EgtMdbGetCurrMachiningParam(MCH_MP.TUUID, sMachTuuid)
         ' Cerco UUID nella lista degli attrezzati
         For Each Tuuid As String In TuuidList
            If sMachTuuid = Tuuid Then
               If VerifyMatThickCompatibility() Then
                  MachiningList.Add(MachiningName)
                  Exit For
               End If
            End If
         Next
         ' Passo alla lavorazione successiva
         bOk = EgtMdbGetNextMachining(MachiningType, MachiningName)
      End While
   End Sub

   Private Sub AuxiliaryMachiningCmBx_SelectionChanged(sender As Object, e As SelectionChangedEventArgs) Handles AuxiliaryMachiningCmBx.SelectionChanged
      CurrDrillingTxBl.Visibility = Windows.Visibility.Hidden
      CurrDrillingCmBx.Visibility = Windows.Visibility.Hidden
      CurrMillingTxBl.Visibility = Windows.Visibility.Hidden
      CurrMillingCmBx.Visibility = Windows.Visibility.Hidden
      CurrPocketingTxBl.Visibility = Windows.Visibility.Hidden
      CurrPocketingCmBx.Visibility = Windows.Visibility.Hidden
      CurrWJettingTxBl.Visibility = Windows.Visibility.Hidden
      CurrWJettingCmBx.Visibility = Windows.Visibility.Hidden
      Dim SelectedItem As StringIdCmBx = DirectCast(AuxiliaryMachiningCmBx.SelectedItem, StringIdCmBx)
      Select Case SelectedItem.nId
         Case 0         ' Nessuna
            If m_RowNumber > 8 Then
               For Index As Integer = m_RowNumber - 1 To 8 Step -1
                  ChooseMachiningGrid.RowDefinitions.RemoveAt(Index)
               Next
               m_RowNumber = 8
            End If
            ButtonsGrid.SetValue(Grid.RowProperty, 5)
            m_MainWindow.m_CurrentMachine.sCurrDrilling = String.Empty
            m_MainWindow.m_CurrentMachine.sCurrMilling = String.Empty
            m_MainWindow.m_CurrentMachine.sCurrWaterJetting = String.Empty
            Me.Height = 341.2
         Case 1        ' Foratura
            ' Definizione di due righe della tabella con la giusta altezza
            If m_RowNumber < 10 Then
               For Index As Integer = 1 To 10 - m_RowNumber
                  Dim Row As New RowDefinition
                  Row.Height = New GridLength(0.5, GridUnitType.Star)
                  ChooseMachiningGrid.RowDefinitions.Add(Row)
               Next
               m_RowNumber = 10
            End If
            CurrDrillingTxBl.SetValue(Grid.RowProperty, 4)
            CurrDrillingCmBx.SetValue(Grid.RowProperty, 5)
            ButtonsGrid.SetValue(Grid.RowProperty, 7)
            If m_CurrentMachine.sCurrDrilling <> String.Empty Then
               CurrDrillingCmBx.SelectedItem = m_CurrentMachine.sCurrDrilling
            End If
            m_MainWindow.m_CurrentMachine.sCurrMilling = String.Empty
            m_MainWindow.m_CurrentMachine.sCurrWaterJetting = String.Empty
            m_MainWindow.m_CurrentMachine.sCurrPocketing = String.Empty
            CurrDrillingTxBl.Visibility = Windows.Visibility.Visible
            CurrDrillingCmBx.Visibility = Windows.Visibility.Visible
            Me.Height = 426.5
         Case 2        ' Fresatura
            ' Definizione di due righe della tabella con la giusta altezza
            If m_RowNumber < 10 Then
               For Index As Integer = 1 To 10 - m_RowNumber
                  Dim Row As New RowDefinition
                  Row.Height = New GridLength(0.5, GridUnitType.Star)
                  ChooseMachiningGrid.RowDefinitions.Add(Row)
               Next
               m_RowNumber = 10
            End If
            CurrMillingTxBl.SetValue(Grid.RowProperty, 4)
            CurrMillingCmBx.SetValue(Grid.RowProperty, 5)
            ButtonsGrid.SetValue(Grid.RowProperty, 7)
            If m_CurrentMachine.sCurrMilling <> String.Empty Then
               CurrMillingCmBx.SelectedItem = m_CurrentMachine.sCurrMilling
            End If
            m_MainWindow.m_CurrentMachine.sCurrDrilling = String.Empty
            m_MainWindow.m_CurrentMachine.sCurrWaterJetting = String.Empty
            m_MainWindow.m_CurrentMachine.sCurrPocketing = String.Empty
            CurrMillingTxBl.Visibility = Windows.Visibility.Visible
            CurrMillingCmBx.Visibility = Windows.Visibility.Visible
            Me.Height = 426.5
         Case 3      ' Foratura + Fresatura (Entrambe)
            ' Definizione di quattro righe della tabella con la giusta altezza
            If m_RowNumber < 12 Then
               For Index As Integer = 1 To 12 - m_RowNumber
                  Dim Row As New RowDefinition
                  Row.Height = New GridLength(0.5, GridUnitType.Star)
                  ChooseMachiningGrid.RowDefinitions.Add(Row)
               Next
               m_RowNumber = 12
            End If
            If m_CurrentMachine.sCurrDrilling <> String.Empty Then
               CurrDrillingCmBx.SelectedItem = m_CurrentMachine.sCurrDrilling
            End If
            If m_CurrentMachine.sCurrMilling <> String.Empty Then
               CurrMillingCmBx.SelectedItem = m_CurrentMachine.sCurrMilling
            End If
            m_MainWindow.m_CurrentMachine.sCurrWaterJetting = String.Empty
            CurrDrillingTxBl.SetValue(Grid.RowProperty, 4)
            CurrDrillingCmBx.SetValue(Grid.RowProperty, 5)
            CurrMillingTxBl.SetValue(Grid.RowProperty, 6)
            CurrMillingCmBx.SetValue(Grid.RowProperty, 7)
            ButtonsGrid.SetValue(Grid.RowProperty, 9)
            CurrDrillingTxBl.Visibility = Windows.Visibility.Visible
            CurrDrillingCmBx.Visibility = Windows.Visibility.Visible
            CurrMillingTxBl.Visibility = Windows.Visibility.Visible
            CurrMillingCmBx.Visibility = Windows.Visibility.Visible
            Me.Height = 511.8
         Case 4    ' Pocketing
            ' Definizione di due righe della tabella con la giusta altezza
            If m_RowNumber < 10 Then
               For Index As Integer = 1 To 10 - m_RowNumber
                  Dim Row As New RowDefinition
                  Row.Height = New GridLength(0.5, GridUnitType.Star)
                  ChooseMachiningGrid.RowDefinitions.Add(Row)
               Next
               m_RowNumber = 10
            End If
            CurrPocketingTxBl.SetValue(Grid.RowProperty, 4)
            CurrPocketingCmBx.SetValue(Grid.RowProperty, 5)
            ButtonsGrid.SetValue(Grid.RowProperty, 7)
            If m_CurrentMachine.sCurrPocketing <> String.Empty Then
               CurrPocketingCmBx.SelectedItem = m_CurrentMachine.sCurrPocketing
            End If
            m_MainWindow.m_CurrentMachine.sCurrDrilling = String.Empty
            m_MainWindow.m_CurrentMachine.sCurrMilling = String.Empty
            m_MainWindow.m_CurrentMachine.sCurrWaterJetting = String.Empty
            CurrPocketingTxBl.Visibility = Windows.Visibility.Visible
            CurrPocketingCmBx.Visibility = Windows.Visibility.Visible
            Me.Height = 426.5

         Case 5    ' WaterJetting
            ' Definizione di due righe della tabella con la giusta altezza
            If m_RowNumber < 10 Then
               For Index As Integer = 1 To 10 - m_RowNumber
                  Dim Row As New RowDefinition
                  Row.Height = New GridLength(0.5, GridUnitType.Star)
                  ChooseMachiningGrid.RowDefinitions.Add(Row)
               Next
               m_RowNumber = 10
            End If
            CurrWJettingTxBl.SetValue(Grid.RowProperty, 4)
            CurrWJettingCmBx.SetValue(Grid.RowProperty, 5)
            ButtonsGrid.SetValue(Grid.RowProperty, 7)
            If m_CurrentMachine.sCurrWaterJetting <> String.Empty Then
               CurrWJettingCmBx.SelectedItem = m_CurrentMachine.sCurrWaterJetting
            End If
            m_MainWindow.m_CurrentMachine.sCurrDrilling = String.Empty
            m_MainWindow.m_CurrentMachine.sCurrMilling = String.Empty
            CurrWJettingTxBl.Visibility = Windows.Visibility.Visible
            CurrWJettingCmBx.Visibility = Windows.Visibility.Visible
            Me.Height = 426.5
      End Select
   End Sub

   Private Sub RefreshMachiningPage()
      Dim nDeltaRow As Integer = 4
      Dim nNewRow As Integer = 2
      ' nascondo tutte le CmBx e TxBl
      CurrDrillingTxBl.Visibility = Windows.Visibility.Hidden
      CurrDrillingCmBx.Visibility = Windows.Visibility.Hidden
      CurrMillingTxBl.Visibility = Windows.Visibility.Hidden
      CurrMillingCmBx.Visibility = Windows.Visibility.Hidden
      CurrPocketingTxBl.Visibility = Windows.Visibility.Hidden
      CurrPocketingCmBx.Visibility = Windows.Visibility.Hidden
      CurrWJettingTxBl.Visibility = Windows.Visibility.Hidden
      CurrWJettingCmBx.Visibility = Windows.Visibility.Hidden

      ' -- FORATURA -- se foretto presente
      If m_CurrentMachine.bDrill And m_DrillingList.Count() > 0 Then
         ' Definizione di due righe della tabella con la giusta altezza       
         For Index As Integer = 1 To nNewRow
            Dim Row As New RowDefinition
            Row.Height = New GridLength(0.5, GridUnitType.Star)
            ChooseMachiningGrid.RowDefinitions.Add(Row)
         Next
         m_RowNumber = m_RowNumber + nNewRow
         ' titolo della ComboBox
         CurrDrillingTxBl.SetValue(Grid.RowProperty, m_RowNumber - nDeltaRow - nNewRow)
         ' ComboBox (nella riga successiva al titolo)
         CurrDrillingCmBx.SetValue(Grid.RowProperty, m_RowNumber - nDeltaRow - nNewRow + 1)
         'ButtonsGrid.SetValue(Grid.RowProperty, 5)
         If m_CurrentMachine.sCurrDrilling <> String.Empty Then
            CurrDrillingCmBx.SelectedItem = m_CurrentMachine.sCurrDrilling
         End If
         ' verifico che la selezione sia andata a buon fine, altrimenti comunico l'avvenuta modifica
         If String.IsNullOrEmpty(CurrDrillingCmBx.SelectedItem) Then
            ' verifico che sia stato inserito veramente una lavorazione prima di comunicare una modifica
            If CurrDrillingCmBx.SelectedItem <> m_CurrentMachine.sCurrDrilling Then m_MachIsModified = True
         End If
         CurrDrillingTxBl.Visibility = Windows.Visibility.Visible
         CurrDrillingCmBx.Visibility = Windows.Visibility.Visible
      End If

      ' -- FRESATURA -- se fresa presente
      If m_CurrentMachine.bMill And m_MillingList.Count() > 0 Then
         ' Definizione di due righe della tabella con la giusta altezza
         For Index As Integer = 1 To nNewRow
            Dim Row As New RowDefinition
            Row.Height = New GridLength(0.5, GridUnitType.Star)
            ChooseMachiningGrid.RowDefinitions.Add(Row)
         Next
         m_RowNumber = m_RowNumber + nNewRow
         CurrMillingTxBl.SetValue(Grid.RowProperty, m_RowNumber - nDeltaRow - nNewRow)
         CurrMillingCmBx.SetValue(Grid.RowProperty, m_RowNumber - nDeltaRow - nNewRow + 1)
         ButtonsGrid.SetValue(Grid.RowProperty, 7)
         If m_CurrentMachine.sCurrMilling <> String.Empty Then
            CurrMillingCmBx.SelectedItem = m_CurrentMachine.sCurrMilling
         End If
         ' verifico che la selezione sia andata a buon fine, altrimenti comunico l'avvenuta modifica
         If String.IsNullOrEmpty(CurrMillingCmBx.SelectedItem) Then
            ' verifico che sia stato inserito veramente una lavorazione prima di comunicare una modifica
            If CurrMillingCmBx.SelectedItem <> m_CurrentMachine.sCurrMilling Then m_MachIsModified = True
         End If
         CurrMillingTxBl.Visibility = Windows.Visibility.Visible
         CurrMillingCmBx.Visibility = Windows.Visibility.Visible
      End If

      ' -- SVUOTATURA -- se fresa senza  punta presente
      If m_CurrentMachine.bCupWheel And m_PocketingList.Count > 0 Then
         ' Definizione di due righe della tabella con la giusta altezza
         For Index As Integer = 1 To nNewRow
            Dim Row As New RowDefinition
            Row.Height = New GridLength(0.5, GridUnitType.Star)
            ChooseMachiningGrid.RowDefinitions.Add(Row)
         Next
         m_RowNumber = m_RowNumber + nNewRow
         CurrPocketingTxBl.SetValue(Grid.RowProperty, m_RowNumber - nDeltaRow - nNewRow)
         CurrPocketingCmBx.SetValue(Grid.RowProperty, m_RowNumber - nDeltaRow - nNewRow + 1)
         ButtonsGrid.SetValue(Grid.RowProperty, 7)
         If m_CurrentMachine.sCurrPocketing <> String.Empty Then
            CurrPocketingCmBx.SelectedItem = m_CurrentMachine.sCurrPocketing
         End If
         ' verifico che la selezione sia andata a buon fine, altrimenti comunico l'avvenuta modifica
         If String.IsNullOrEmpty(CurrPocketingCmBx.SelectedItem) Then
            ' verifico che sia stato inserito veramente una lavorazione prima di comunicare una modifica
            If CurrMillingCmBx.SelectedItem <> m_CurrentMachine.sCurrPocketing Then m_MachIsModified = True
         End If
         CurrPocketingTxBl.Visibility = Windows.Visibility.Visible
         CurrPocketingCmBx.Visibility = Windows.Visibility.Visible
      End If

      ' riga 8/9
      If m_CurrentMachine.bWaterJetting And m_WJettingList.Count > 0 Then
         ' Definizione di due righe della tabella con la giusta altezza
         For Index As Integer = 1 To nNewRow
            Dim Row As New RowDefinition
            Row.Height = New GridLength(0.5, GridUnitType.Star)
            ChooseMachiningGrid.RowDefinitions.Add(Row)
         Next
         m_RowNumber = m_RowNumber + nNewRow
         CurrWJettingTxBl.SetValue(Grid.RowProperty, m_RowNumber - nDeltaRow - nNewRow)
         'CurrWJettingCmBx.SetValue(Grid.RowProperty, m_RowNumber - nDeltaRow - nNewRow + 1)
         WaterJettingUGrd.SetValue(Grid.RowProperty, m_RowNumber - nDeltaRow - nNewRow + 1)
         If m_CurrentMachine.sCurrWaterJetting <> String.Empty Then
            CurrWJettingCmBx.SelectedItem = m_CurrentMachine.sCurrWaterJetting
         End If
         ' verifico che la selezione sia andata a buon fine, altrimenti comunico l'avvenuta modifica
         If String.IsNullOrEmpty(CurrWJettingCmBx.SelectedItem) Then
            ' verifico che sia stato inserito veramente una lavorazione prima di comunicare una modifica
            If CurrMillingCmBx.SelectedItem <> m_CurrentMachine.sCurrWaterJetting Then m_MachIsModified = True
         End If
         CurrWJettingTxBl.Visibility = Windows.Visibility.Visible
         CurrWJettingCmBx.Visibility = Windows.Visibility.Visible
         ' se DB WaterJet abilitato avrò anche la CmBx della Quality altrimenti espando la CmBx del Waterjetting corrente su tutta la riga
         If m_MainWindow.m_CurrentMachine.bFromDBWaterJet Then
            CurrWJettingCmBx.SetValue(Grid.ColumnSpanProperty, 1)
            If String.IsNullOrEmpty(CurrWJettingQualityCmBx.SelectedItem) Then
               CurrWJettingQualityCmBx.SelectedItem = m_CurrentMachine.sCurrWaterJettingQuality
            End If
            ' verifico che la selezione sia andata a buon fine, altrimenti comunico l'avvenuta modifica
            If String.IsNullOrEmpty(CurrWJettingQualityCmBx.SelectedItem) Then
               ' verifico che sia stato inserito veramente una lavorazione prima di comunicare una modifica
               If CurrWJettingQualityCmBx.SelectedItem <> m_CurrentMachine.sCurrWaterJettingQuality Then m_MachIsModified = True
            End If
            CurrWJettingQualityCmBx.Visibility = Windows.Visibility.Visible
         Else
            CurrWJettingCmBx.SetValue(Grid.ColumnSpanProperty, 3)
            CurrWJettingQualityCmBx.Visibility = Windows.Visibility.Collapsed
         End If
      End If

      Me.Height = 42 * m_RowNumber
      ButtonsGrid.SetValue(Grid.RowProperty, m_RowNumber - nDeltaRow + 1)
   End Sub

   Private Sub CurrSawingCmBx_SelectionChanged(sender As Object, e As SelectionChangedEventArgs) Handles CurrSawingCmBx.SelectionChanged
      m_MainWindow.m_CurrentMachine.sCurrSawing = CurrSawingCmBx.SelectedItem.ToString()
   End Sub

   Private Sub CurrDrillingCmBx_SelectionChanged(sender As Object, e As SelectionChangedEventArgs) Handles CurrDrillingCmBx.SelectionChanged
      m_MainWindow.m_CurrentMachine.sCurrDrilling = CurrDrillingCmBx.SelectedItem.ToString()
   End Sub

   Private Sub CurrMillingCmBx_SelectionChanged(sender As Object, e As SelectionChangedEventArgs) Handles CurrMillingCmBx.SelectionChanged
      m_MainWindow.m_CurrentMachine.sCurrMilling = CurrMillingCmBx.SelectedItem.ToString()
   End Sub

   Private Sub CurrPocketingCmBx_SekectionChanged(sender As Object, e As SelectionChangedEventArgs) Handles CurrPocketingCmBx.SelectionChanged
      m_MainWindow.m_CurrentMachine.sCurrPocketing = CurrPocketingCmBx.SelectedItem.ToString()
   End Sub

   Private Sub CurrWJettingCmBx_SelectionChanged(sender As Object, e As SelectionChangedEventArgs) Handles CurrWJettingCmBx.SelectionChanged
      m_MainWindow.m_CurrentMachine.sCurrWaterJetting = CurrWJettingCmBx.SelectedItem.ToString()
   End Sub

   Private Sub CurrQaulityWJettingCmBx_SelectionChanged(sender As Object, e As SelectionChangedEventArgs) Handles CurrWJettingQualityCmBx.SelectionChanged
      m_MainWindow.m_CurrentMachine.sCurrWaterJettingQuality = CurrWJettingQualityCmBx.SelectedItem.ToString()
   End Sub

   Private Sub OkBtn_Click(sender As Object, e As RoutedEventArgs) Handles OkBtn.Click
      DialogResult = True
      ' Aggiorno i parametri dei tagli diretti
      m_MainWindow.m_DirectCutPageUC.ReloadParam()
   End Sub

   Private Sub ExitBtn_Click(sender As Object, e As RoutedEventArgs) Handles ExitBtn.Click
      DialogResult = False
      ' Aggiorno i parametri dei tagli diretti
      m_MainWindow.m_DirectCutPageUC.ReloadParam()
   End Sub

End Class

Class StringIdCmBx

   Private m_nId As Integer
   Private m_sName As String

   Friend Overloads Shared Function FromIdToStringIdCmBx(nId As Integer, List As List(Of StringIdCmBx)) As StringIdCmBx
      For Each Item As StringIdCmBx In List
         If Item.nId = nId Then
            Return Item
         End If
      Next
      Return Nothing
   End Function

   Friend Overloads Shared Function FromIdToStringIdCmBx(nId As Integer, List As ObservableCollection(Of StringIdCmBx)) As StringIdCmBx
      For Each Item As StringIdCmBx In List
         If Item.nId = nId Then
            Return Item
         End If
      Next
      Return Nothing
   End Function

   Friend ReadOnly Property nId As Integer
      Get
         Return m_nId
      End Get
   End Property

   Public ReadOnly Property sName As String
      Get
         Return m_sName
      End Get
   End Property

   Sub New(Id As Integer, sName As String)
      m_nId = Id
      m_sName = sName
   End Sub

End Class