Imports EgtUILib

Public Class MachinePageUC

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

   'Dichiarazione delle Page UserControl
   Friend m_AlarmsPageUC As AlarmsPageUC
   Friend m_ToolsDbPageUC As ToolsDbPageUC
   Friend m_MachiningDbPageUC As MachiningDbPageUC
   Friend m_PolishingsPageUC As PolishingsPageUC
   Friend m_WaterJetPageUC As WaterJetPageUC
   Friend m_MachineCNPageUC As MachineCNPageUC
   Friend m_StatisticsPageUC As StatisticsPageUC

   ' Riferimento alla pagina correntemente attiva
   Friend m_ActiveMachinePage As MachinePages

   Enum MachinePages
      Alarms
      ToolsDb
      MachiningDb
      Polishings
      WaterJet
      MachineCN
      Statistics
   End Enum

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

      'Creazione delle Page UserControl
      m_AlarmsPageUC = New AlarmsPageUC
      m_ToolsDbPageUC = New ToolsDbPageUC
      m_MachiningDbPageUC = New MachiningDbPageUC
      m_PolishingsPageUC = New PolishingsPageUC
      m_WaterJetPageUC = New WaterJetPageUC
      m_MachineCNPageUC = New MachineCNPageUC
      m_StatisticsPageUC = New StatisticsPageUC

      'Posizionemento nella griglia delle Page UserControl
      m_AlarmsPageUC.SetValue(Grid.RowProperty, 1)
      m_ToolsDbPageUC.SetValue(Grid.RowProperty, 1)
      m_MachiningDbPageUC.SetValue(Grid.RowProperty, 1)
      m_PolishingsPageUC.SetValue(Grid.RowProperty, 1)
      m_WaterJetPageUC.SetValue(Grid.RowProperty, 1)
      m_MachineCNPageUC.SetValue(Grid.RowProperty, 1)
      m_StatisticsPageUC.SetValue(Grid.RowProperty, 1)

      AlarmsBtn.ToolTip = EgtMsg(MSG_MACHINEPAGEUC + 5)
      ToolsDBBtn.ToolTip = EgtMsg(MSG_MACHINEPAGEUC + 6)
      MachiningDBBtn.ToolTip = EgtMsg(MSG_MACHINEPAGEUC + 7)
      PolishingsBtn.Content = EgtMsg(91090)
      WaterJetBtn.Content = EgtMsg(91128)
      TestingPageBtn.ToolTip = EgtMsg(MSG_MACHINEPAGEUC + 8)
      StatisticsBtn.Content = EgtMsg(90709)

      ' Apro pagina dati macchina
      MachinePageGrid.Children.Add(m_AlarmsPageUC)
      m_ActiveMachinePage = MachinePages.Alarms

      ' Se lucidature non abilitate, disattivo la pagina DB lucidature
      If Not m_MainWindow.m_CurrentMachine.bPolishing Then
         PolishingsBtn.IsEnabled = False
      End If

      If Not m_MainWindow.m_CurrentMachine.bWaterJet Or Not m_MainWindow.m_CurrentMachine.bFromDBWaterJet Then
         WaterJetBtn.IsEnabled = False
      End If

      ' Se controllo numerico 0, disattivo la pagina macchina
      If GetPrivateProfileInt(S_NUMERICALCONTROL, K_TYPE, 0, m_MainWindow.GetMachIniFile()) = 0 Then
         TestingPageBtn.IsEnabled = False
      End If

      ' Se statistiche 0, disattivo la pagina statistiche
      If GetPrivateProfileInt(S_STATISTICS, K_SHOWAREAS, 0, m_MainWindow.GetMachIniFile()) = 0 Then
         StatisticsBtn.Visibility = Visibility.Hidden
      End If

   End Sub

   Private Sub MachinePage_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded

      ' Inizializzazione della pagina di apertura all'apertura della pagina MachinePage
      If Not m_MainWindow.m_MachineStatusUC.m_bIsClicked Then
         AlarmsBtn.IsChecked = True
         m_ActiveMachinePage = MachinePages.Alarms
      Else
         TestingPageBtn.IsChecked = True
         m_ActiveMachinePage = MachinePages.MachineCN
         m_MainWindow.m_MachineStatusUC.m_bIsClicked = False
      End If

   End Sub

   Private Sub AlarmsBtn_Click(sender As Object, e As RoutedEventArgs) Handles AlarmsBtn.Click
      Select Case m_ActiveMachinePage
         Case MachinePages.Alarms
            AlarmsBtn.IsChecked = True
         Case MachinePages.ToolsDb
            ' Verifica ed eventuale salvataggio utensile corrente
            If Not m_ToolsDbPageUC.SaveCurrTool() Then
               AlarmsBtn.IsChecked = False
               ToolsDBBtn.IsChecked = True
               Return
            End If
            ' salvo il DB utensili
            EgtSetCurrentContext(m_MainWindow.m_CurrentProjectPageUC.CurrentProjectScene.GetCtx())
            EgtTdbSave()
            ' passo alla pagina Dati macchina
            AlarmsBtn.IsChecked = True
            ToolsDBBtn.IsChecked = False
            MachinePageGrid.Children.Remove(m_ToolsDbPageUC)
            MachinePageGrid.Children.Add(m_AlarmsPageUC)
            m_ActiveMachinePage = MachinePages.Alarms
         Case MachinePages.MachiningDb
            ' Verifica ed eventuale salvataggio lavorazione corrente
            If Not m_MachiningDbPageUC.SaveCurrMachining() Then
               AlarmsBtn.IsChecked = False
               MachiningDBBtn.IsChecked = True
               Return
            End If
            ' salvo il DB lavorazioni
            EgtSetCurrentContext(m_MainWindow.m_CurrentProjectPageUC.CurrentProjectScene.GetCtx())
            EgtMdbSave()
            ' passo alla pagina Dati macchina
            AlarmsBtn.IsChecked = True
            MachiningDBBtn.IsChecked = False
            MachinePageGrid.Children.Remove(m_MachiningDbPageUC)
            MachinePageGrid.Children.Add(m_AlarmsPageUC)
            m_ActiveMachinePage = MachinePages.Alarms
         Case MachinePages.Polishings
            ' Verifica ed eventuale salvataggio Lucidatura corrente
            If Not m_PolishingsPageUC.SaveKit() Then
               AlarmsBtn.IsChecked = False
               PolishingsBtn.IsChecked = True
               Return
            End If
            ' passo alla pagina Dati Macchina
            AlarmsBtn.IsChecked = True
            PolishingsBtn.IsChecked = False
            MachinePageGrid.Children.Remove(m_PolishingsPageUC)
            MachinePageGrid.Children.Add(m_AlarmsPageUC)
            m_ActiveMachinePage = MachinePages.Alarms
         Case MachinePages.WaterJet
            ' Verifica ed eventuale salvataggio Db Waterjet
            Dim nPressedBtn As Integer = DirectCast(m_WaterJetPageUC.DataContext, EgtWPFLib5.WaterjetDbWindowVM).CloseWaterjetDb_OmagCut(m_MainWindow)
            If nPressedBtn = EgtWPFLib5.WaterjetDbWindowVM.SaveWndBtnEnum.CANCEL Then
               AlarmsBtn.IsChecked = False
               WaterJetBtn.IsChecked = True
               Return
            ElseIf nPressedBtn = EgtWPFLib5.WaterjetDbWindowVM.SaveWndBtnEnum.NO Then
               m_WaterJetPageUC.WaterJetPage_Reinitialize()
            End If
            ' passo alla pagina Dati Macchina
            AlarmsBtn.IsChecked = True
            WaterJetBtn.IsChecked = False
            MachinePageGrid.Children.Remove(m_WaterJetPageUC)
            MachinePageGrid.Children.Add(m_AlarmsPageUC)
            m_ActiveMachinePage = MachinePages.Alarms
         Case MachinePages.MachineCN
            AlarmsBtn.IsChecked = True
            TestingPageBtn.IsChecked = False
            MachinePageGrid.Children.Remove(m_MachineCNPageUC)
            MachinePageGrid.Children.Add(m_AlarmsPageUC)
            m_ActiveMachinePage = MachinePages.Alarms
         Case MachinePages.Statistics
            AlarmsBtn.IsChecked = True
            StatisticsBtn.IsChecked = False
            MachinePageGrid.Children.Remove(m_StatisticsPageUC)
            MachinePageGrid.Children.Add(m_AlarmsPageUC)
            m_ActiveMachinePage = MachinePages.Alarms
      End Select
   End Sub

   Private Sub ToolsDBBtn_Click(sender As Object, e As RoutedEventArgs) Handles ToolsDBBtn.Click
      Select Case m_ActiveMachinePage
         Case MachinePages.Alarms
            AlarmsBtn.IsChecked = False
            ToolsDBBtn.IsChecked = True
            MachinePageGrid.Children.Remove(m_AlarmsPageUC)
            MachinePageGrid.Children.Add(m_ToolsDbPageUC)
            m_ActiveMachinePage = MachinePages.ToolsDb
         Case MachinePages.ToolsDb
            ToolsDBBtn.IsChecked = True
         Case MachinePages.MachiningDb
            ' Verifica ed eventuale salvataggio lavorazione corrente
            If Not m_MachiningDbPageUC.SaveCurrMachining() Then
               ToolsDBBtn.IsChecked = False
               MachiningDBBtn.IsChecked = True
               Return
            End If
            ' salvo il DB lavorazioni
            EgtSetCurrentContext(m_MainWindow.m_CurrentProjectPageUC.CurrentProjectScene.GetCtx())
            EgtMdbSave()
            ' passo alla pagina DB utensili
            ToolsDBBtn.IsChecked = True
            MachiningDBBtn.IsChecked = False
            MachinePageGrid.Children.Remove(m_MachiningDbPageUC)
            MachinePageGrid.Children.Add(m_ToolsDbPageUC)
            m_ActiveMachinePage = MachinePages.ToolsDb
         Case MachinePages.Polishings
            ' Verifica ed eventuale salvataggio Lucidatura corrente
            If Not m_PolishingsPageUC.SaveKit() Then
               ToolsDBBtn.IsChecked = False
               PolishingsBtn.IsChecked = True
               Return
            End If
            ' passo alla pagina DB Utensili
            ToolsDBBtn.IsChecked = True
            PolishingsBtn.IsChecked = False
            MachinePageGrid.Children.Remove(m_PolishingsPageUC)
            MachinePageGrid.Children.Add(m_ToolsDbPageUC)
            m_ActiveMachinePage = MachinePages.ToolsDb
         Case MachinePages.WaterJet
            'Verifica ed eventuale salvataggio Db Waterjet
            Dim nPressedBtn As Integer = DirectCast(m_WaterJetPageUC.DataContext, EgtWPFLib5.WaterjetDbWindowVM).CloseWaterjetDb_OmagCut(m_MainWindow)
            If nPressedBtn = EgtWPFLib5.WaterjetDbWindowVM.SaveWndBtnEnum.CANCEL Then
               ToolsDBBtn.IsChecked = False
               WaterJetBtn.IsChecked = True
               Return
            ElseIf nPressedBtn = EgtWPFLib5.WaterjetDbWindowVM.SaveWndBtnEnum.NO Then
               m_WaterJetPageUC.WaterJetPage_Reinitialize()
            End If
            ' passo alla pagina Dati Macchina
            ToolsDBBtn.IsChecked = True
            WaterJetBtn.IsChecked = False
            MachinePageGrid.Children.Remove(m_WaterJetPageUC)
            MachinePageGrid.Children.Add(m_ToolsDbPageUC)
            m_ActiveMachinePage = MachinePages.ToolsDb
         Case MachinePages.MachineCN
            ToolsDBBtn.IsChecked = True
            TestingPageBtn.IsChecked = False
            MachinePageGrid.Children.Remove(m_MachineCNPageUC)
            MachinePageGrid.Children.Add(m_ToolsDbPageUC)
            m_ActiveMachinePage = MachinePages.ToolsDb
         Case MachinePages.Statistics
            ToolsDBBtn.IsChecked = True
            StatisticsBtn.IsChecked = False
            MachinePageGrid.Children.Remove(m_StatisticsPageUC)
            MachinePageGrid.Children.Add(m_ToolsDbPageUC)
            m_ActiveMachinePage = MachinePages.ToolsDb
      End Select
   End Sub

   Private Sub MachiningDBBtn_Click(sender As Object, e As RoutedEventArgs) Handles MachiningDBBtn.Click
      Select Case m_ActiveMachinePage
         Case MachinePages.Alarms
            AlarmsBtn.IsChecked = False
            MachiningDBBtn.IsChecked = True
            MachinePageGrid.Children.Remove(m_AlarmsPageUC)
            MachinePageGrid.Children.Add(m_MachiningDbPageUC)
            m_ActiveMachinePage = MachinePages.MachiningDb
         Case MachinePages.ToolsDb
            ' Verifica ed eventuale salvataggio utensile corrente
            If Not m_ToolsDbPageUC.SaveCurrTool() Then
               MachiningDBBtn.IsChecked = False
               ToolsDBBtn.IsChecked = True
               Return
            End If
            ' salvo il DB utensili
            EgtSetCurrentContext(m_MainWindow.m_CurrentProjectPageUC.CurrentProjectScene.GetCtx())
            EgtTdbSave()
            ' passo alla pagina DB lavorazioni
            MachiningDBBtn.IsChecked = True
            ToolsDBBtn.IsChecked = False
            MachinePageGrid.Children.Remove(m_ToolsDbPageUC)
            MachinePageGrid.Children.Add(m_MachiningDbPageUC)
            m_ActiveMachinePage = MachinePages.MachiningDb
         Case MachinePages.MachiningDb
            MachiningDBBtn.IsChecked = True
         Case MachinePages.Polishings
            ' Verifica ed eventuale salvataggio Lucidatura corrente
            If Not m_PolishingsPageUC.SaveKit() Then
               MachiningDBBtn.IsChecked = False
               PolishingsBtn.IsChecked = True
               Return
            End If
            ' passo alla pagina DB lavorazioni
            MachiningDBBtn.IsChecked = True
            PolishingsBtn.IsChecked = False
            MachinePageGrid.Children.Remove(m_PolishingsPageUC)
            MachinePageGrid.Children.Add(m_MachiningDbPageUC)
            m_ActiveMachinePage = MachinePages.MachiningDb
         Case MachinePages.WaterJet
            ' Verifica ed eventuale salvataggio Db Waterjet
            Dim nPressedBtn As Integer = DirectCast(m_WaterJetPageUC.DataContext, EgtWPFLib5.WaterjetDbWindowVM).CloseWaterjetDb_OmagCut(m_MainWindow)
            If nPressedBtn = EgtWPFLib5.WaterjetDbWindowVM.SaveWndBtnEnum.CANCEL Then
               MachiningDBBtn.IsChecked = False
               WaterJetBtn.IsChecked = True
               Return
            ElseIf nPressedBtn = EgtWPFLib5.WaterjetDbWindowVM.SaveWndBtnEnum.NO Then
               m_WaterJetPageUC.WaterJetPage_Reinitialize()
            End If
            ' passo alla pagina Dati Macchina
            MachiningDBBtn.IsChecked = True
            WaterJetBtn.IsChecked = False
            MachinePageGrid.Children.Remove(m_WaterJetPageUC)
            MachinePageGrid.Children.Add(m_MachiningDbPageUC)
            m_ActiveMachinePage = MachinePages.MachiningDb
         Case MachinePages.MachineCN
            MachiningDBBtn.IsChecked = True
            TestingPageBtn.IsChecked = False
            MachinePageGrid.Children.Remove(m_MachineCNPageUC)
            MachinePageGrid.Children.Add(m_MachiningDbPageUC)
            m_ActiveMachinePage = MachinePages.MachiningDb
         Case MachinePages.Statistics
            MachiningDBBtn.IsChecked = True
            StatisticsBtn.IsChecked = False
            MachinePageGrid.Children.Remove(m_StatisticsPageUC)
            MachinePageGrid.Children.Add(m_MachiningDbPageUC)
            m_ActiveMachinePage = MachinePages.MachiningDb
      End Select
   End Sub

   Private Sub PolishingsBtn_Click(sender As Object, e As RoutedEventArgs) Handles PolishingsBtn.Click
      Select Case m_ActiveMachinePage
         Case MachinePages.Alarms
            AlarmsBtn.IsChecked = False
            PolishingsBtn.IsChecked = True
            MachinePageGrid.Children.Remove(m_AlarmsPageUC)
            MachinePageGrid.Children.Add(m_PolishingsPageUC)
            m_ActiveMachinePage = MachinePages.Polishings
         Case MachinePages.ToolsDb
            ' Verifica ed eventuale salvataggio utensile corrente
            If Not m_ToolsDbPageUC.SaveCurrTool() Then
               PolishingsBtn.IsChecked = False
               ToolsDBBtn.IsChecked = True
               Return
            End If
            ' salvo il DB utensili
            EgtSetCurrentContext(m_MainWindow.m_CurrentProjectPageUC.CurrentProjectScene.GetCtx())
            EgtTdbSave()
            ' passo alla pagina Lucidature
            PolishingsBtn.IsChecked = True
            ToolsDBBtn.IsChecked = False
            MachinePageGrid.Children.Remove(m_ToolsDbPageUC)
            MachinePageGrid.Children.Add(m_PolishingsPageUC)
            m_ActiveMachinePage = MachinePages.Polishings
         Case MachinePages.MachiningDb
            ' Verifica ed eventuale salvataggio lavorazione corrente
            If Not m_MachiningDbPageUC.SaveCurrMachining() Then
               PolishingsBtn.IsChecked = False
               MachiningDBBtn.IsChecked = True
               Return
            End If
            ' salvo il DB lavorazioni
            EgtSetCurrentContext(m_MainWindow.m_CurrentProjectPageUC.CurrentProjectScene.GetCtx())
            EgtMdbSave()
            ' passo alla pagina Lucidature
            PolishingsBtn.IsChecked = True
            MachiningDBBtn.IsChecked = False
            MachinePageGrid.Children.Remove(m_MachiningDbPageUC)
            MachinePageGrid.Children.Add(m_PolishingsPageUC)
            m_ActiveMachinePage = MachinePages.Polishings
         Case MachinePages.Polishings
            PolishingsBtn.IsChecked = True
         Case MachinePages.WaterJet
            ' Verifica ed eventuale salvataggio Db Waterjet
            Dim nPressedBtn As Integer = DirectCast(m_WaterJetPageUC.DataContext, EgtWPFLib5.WaterjetDbWindowVM).CloseWaterjetDb_OmagCut(m_MainWindow)
            If nPressedBtn = EgtWPFLib5.WaterjetDbWindowVM.SaveWndBtnEnum.CANCEL Then
               PolishingsBtn.IsChecked = False
               WaterJetBtn.IsChecked = True
               Return
            ElseIf nPressedBtn = EgtWPFLib5.WaterjetDbWindowVM.SaveWndBtnEnum.NO Then
               m_WaterJetPageUC.WaterJetPage_Reinitialize()
            End If
            ' passo alla pagina Dati Macchina
            PolishingsBtn.IsChecked = True
            WaterJetBtn.IsChecked = False
            MachinePageGrid.Children.Remove(m_WaterJetPageUC)
            MachinePageGrid.Children.Add(m_PolishingsPageUC)
            m_ActiveMachinePage = MachinePages.Polishings
         Case MachinePages.MachineCN
            PolishingsBtn.IsChecked = True
            TestingPageBtn.IsChecked = False
            MachinePageGrid.Children.Remove(m_MachineCNPageUC)
            MachinePageGrid.Children.Add(m_PolishingsPageUC)
            m_ActiveMachinePage = MachinePages.Polishings
         Case MachinePages.Statistics
            PolishingsBtn.IsChecked = True
            StatisticsBtn.IsChecked = False
            MachinePageGrid.Children.Remove(m_StatisticsPageUC)
            MachinePageGrid.Children.Add(m_PolishingsPageUC)
            m_ActiveMachinePage = MachinePages.Polishings
      End Select
   End Sub

   Private Sub WaterJetBtn_Click(sender As Object, e As RoutedEventArgs) Handles WaterJetBtn.Click
      Select Case m_ActiveMachinePage
         Case MachinePages.Alarms
            AlarmsBtn.IsChecked = False
            WaterJetBtn.IsChecked = True
            MachinePageGrid.Children.Remove(m_AlarmsPageUC)
            MachinePageGrid.Children.Add(m_WaterJetPageUC)
            m_ActiveMachinePage = MachinePages.WaterJet
            'DirectCast(m_WaterJetPageUC.DataContext, WaterjetDbWindowVM).RefreshDImensionPage()
         Case MachinePages.ToolsDb
            ' Verifica ed eventuale salvataggio utensile corrente
            If Not m_ToolsDbPageUC.SaveCurrTool() Then
               PolishingsBtn.IsChecked = False
               ToolsDBBtn.IsChecked = True
               Return
            End If
            ' salvo il DB utensili
            EgtSetCurrentContext(m_MainWindow.m_CurrentProjectPageUC.CurrentProjectScene.GetCtx())
            EgtTdbSave()
            ' passo alla pagina WaterJet
            WaterJetBtn.IsChecked = True
            ToolsDBBtn.IsChecked = False
            MachinePageGrid.Children.Remove(m_ToolsDbPageUC)
            MachinePageGrid.Children.Add(m_WaterJetPageUC)
            m_ActiveMachinePage = MachinePages.WaterJet
         Case MachinePages.MachiningDb
            ' Verifica ed eventuale salvataggio lavorazione corrente
            If Not m_MachiningDbPageUC.SaveCurrMachining() Then
               WaterJetBtn.IsChecked = False
               MachiningDBBtn.IsChecked = True
               Return
            End If
            ' salvo il DB lavorazioni
            EgtSetCurrentContext(m_MainWindow.m_CurrentProjectPageUC.CurrentProjectScene.GetCtx())
            EgtMdbSave()
            ' passo alla pagina WaterJet
            WaterJetBtn.IsChecked = True
            MachiningDBBtn.IsChecked = False
            MachinePageGrid.Children.Remove(m_MachiningDbPageUC)
            MachinePageGrid.Children.Add(m_WaterJetPageUC)
            m_ActiveMachinePage = MachinePages.WaterJet
         Case MachinePages.Polishings
            ' Verifica ed eventuale salvataggio Lucidatura corrente
            If Not m_PolishingsPageUC.SaveKit() Then
               WaterJetBtn.IsChecked = False
               PolishingsBtn.IsChecked = True
               Return
            End If
            ' passo alla pagina WaterJet
            WaterJetBtn.IsChecked = True
            PolishingsBtn.IsChecked = False
            MachinePageGrid.Children.Remove(m_PolishingsPageUC)
            MachinePageGrid.Children.Add(m_WaterJetPageUC)
            m_ActiveMachinePage = MachinePages.WaterJet
         Case MachinePages.WaterJet
            WaterJetBtn.IsChecked = True
         Case MachinePages.MachineCN
            WaterJetBtn.IsChecked = True
            TestingPageBtn.IsChecked = False
            MachinePageGrid.Children.Remove(m_MachineCNPageUC)
            MachinePageGrid.Children.Add(m_WaterJetPageUC)
            m_ActiveMachinePage = MachinePages.WaterJet
         Case MachinePages.Statistics
            WaterJetBtn.IsChecked = True
            StatisticsBtn.IsChecked = False
            MachinePageGrid.Children.Remove(m_StatisticsPageUC)
            MachinePageGrid.Children.Add(m_WaterJetPageUC)
            m_ActiveMachinePage = MachinePages.WaterJet
      End Select
   End Sub

   Private Sub TestingPageBtn_Checked(sender As Object, e As RoutedEventArgs) Handles TestingPageBtn.Checked
      Select Case m_ActiveMachinePage
         Case MachinePages.Alarms
            AlarmsBtn.IsChecked = False
            TestingPageBtn.IsChecked = True
            MachinePageGrid.Children.Remove(m_AlarmsPageUC)
            MachinePageGrid.Children.Add(m_MachineCNPageUC)
            m_ActiveMachinePage = MachinePages.MachineCN
         Case MachinePages.ToolsDb
            ' Verifica ed eventuale salvataggio utensile corrente
            If Not m_ToolsDbPageUC.SaveCurrTool() Then
               TestingPageBtn.IsChecked = False
               ToolsDBBtn.IsChecked = True
               Return
            End If
            ' salvo il DB utensili
            EgtSetCurrentContext(m_MainWindow.m_CurrentProjectPageUC.CurrentProjectScene.GetCtx())
            EgtTdbSave()
            ' passo alla pagina Macchina
            TestingPageBtn.IsChecked = True
            ToolsDBBtn.IsChecked = False
            MachinePageGrid.Children.Remove(m_ToolsDbPageUC)
            MachinePageGrid.Children.Add(m_MachineCNPageUC)
            m_ActiveMachinePage = MachinePages.MachineCN
         Case MachinePages.MachiningDb
            ' Verifica ed eventuale salvataggio lavorazione corrente
            If Not m_MachiningDbPageUC.SaveCurrMachining() Then
               TestingPageBtn.IsChecked = False
               MachiningDBBtn.IsChecked = True
               Return
            End If
            ' salvo il DB lavorazioni
            EgtSetCurrentContext(m_MainWindow.m_CurrentProjectPageUC.CurrentProjectScene.GetCtx())
            EgtMdbSave()
            ' passo alla pagina Macchina
            TestingPageBtn.IsChecked = True
            MachiningDBBtn.IsChecked = False
            MachinePageGrid.Children.Remove(m_MachiningDbPageUC)
            MachinePageGrid.Children.Add(m_MachineCNPageUC)
            m_ActiveMachinePage = MachinePages.MachineCN
         Case MachinePages.Polishings
            ' Verifica ed eventuale salvataggio Lucidatura corrente
            If Not m_PolishingsPageUC.SaveKit() Then
               TestingPageBtn.IsChecked = False
               PolishingsBtn.IsChecked = True
               Return
            End If
            ' passo alla pagina DB lavorazioni
            TestingPageBtn.IsChecked = True
            PolishingsBtn.IsChecked = False
            MachinePageGrid.Children.Remove(m_PolishingsPageUC)
            MachinePageGrid.Children.Add(m_MachineCNPageUC)
            m_ActiveMachinePage = MachinePages.MachineCN
         Case MachinePages.WaterJet
            ' Verifica ed eventuale salvataggio Db Waterjet
            Dim nPressedBtn As Integer = DirectCast(m_WaterJetPageUC.DataContext, EgtWPFLib5.WaterjetDbWindowVM).CloseWaterjetDb_OmagCut(m_MainWindow)
            If nPressedBtn = EgtWPFLib5.WaterjetDbWindowVM.SaveWndBtnEnum.CANCEL Then
               TestingPageBtn.IsChecked = False
               WaterJetBtn.IsChecked = True
               Return
            ElseIf nPressedBtn = EgtWPFLib5.WaterjetDbWindowVM.SaveWndBtnEnum.NO Then
               m_WaterJetPageUC.WaterJetPage_Reinitialize()
            End If
            ' passo alla pagina Dati Macchina
            TestingPageBtn.IsChecked = True
            WaterJetBtn.IsChecked = False
            MachinePageGrid.Children.Remove(m_WaterJetPageUC)
            MachinePageGrid.Children.Add(m_MachineCNPageUC)
            m_ActiveMachinePage = MachinePages.MachineCN
         Case MachinePages.MachineCN
            TestingPageBtn.IsChecked = True
         Case MachinePages.Statistics
            TestingPageBtn.IsChecked = True
            StatisticsBtn.IsChecked = False
            MachinePageGrid.Children.Remove(m_StatisticsPageUC)
            MachinePageGrid.Children.Add(m_MachineCNPageUC)
            m_ActiveMachinePage = MachinePages.MachineCN
      End Select
   End Sub

   Private Sub StatisticsBtn_Checked(sender As Object, e As RoutedEventArgs) Handles StatisticsBtn.Checked
      Select Case m_ActiveMachinePage
         Case MachinePages.Alarms
            AlarmsBtn.IsChecked = False
            StatisticsBtn.IsChecked = True
            MachinePageGrid.Children.Remove(m_AlarmsPageUC)
            MachinePageGrid.Children.Add(m_StatisticsPageUC)
            m_ActiveMachinePage = MachinePages.Statistics
         Case MachinePages.ToolsDb
            ' Verifica ed eventuale salvataggio utensile corrente
            If Not m_ToolsDbPageUC.SaveCurrTool() Then
               StatisticsBtn.IsChecked = False
               ToolsDBBtn.IsChecked = True
               Return
            End If
            ' salvo il DB utensili
            EgtSetCurrentContext(m_MainWindow.m_CurrentProjectPageUC.CurrentProjectScene.GetCtx())
            EgtTdbSave()
            ' passo alla pagina Statistiche
            StatisticsBtn.IsChecked = True
            ToolsDBBtn.IsChecked = False
            MachinePageGrid.Children.Remove(m_ToolsDbPageUC)
            MachinePageGrid.Children.Add(m_StatisticsPageUC)
            m_ActiveMachinePage = MachinePages.Statistics
         Case MachinePages.MachiningDb
            ' Verifica ed eventuale salvataggio lavorazione corrente
            If Not m_MachiningDbPageUC.SaveCurrMachining() Then
               StatisticsBtn.IsChecked = False
               MachiningDBBtn.IsChecked = True
               Return
            End If
            ' salvo il DB lavorazioni
            EgtSetCurrentContext(m_MainWindow.m_CurrentProjectPageUC.CurrentProjectScene.GetCtx())
            EgtMdbSave()
            ' passo alla pagina Statistiche
            StatisticsBtn.IsChecked = True
            MachiningDBBtn.IsChecked = False
            MachinePageGrid.Children.Remove(m_MachiningDbPageUC)
            MachinePageGrid.Children.Add(m_StatisticsPageUC)
            m_ActiveMachinePage = MachinePages.Statistics
         Case MachinePages.Polishings
            ' Verifica ed eventuale salvataggio Lucidatura corrente
            If Not m_PolishingsPageUC.SaveKit() Then
               StatisticsBtn.IsChecked = False
               PolishingsBtn.IsChecked = True
               Return
            End If
            ' passo alla pagina DB lavorazioni
            StatisticsBtn.IsChecked = True
            PolishingsBtn.IsChecked = False
            MachinePageGrid.Children.Remove(m_PolishingsPageUC)
            MachinePageGrid.Children.Add(m_StatisticsPageUC)
            m_ActiveMachinePage = MachinePages.Statistics
         Case MachinePages.WaterJet
            ' Verifica ed eventuale salvataggio Db Waterjet
            Dim nPressedBtn As Integer = DirectCast(m_WaterJetPageUC.DataContext, EgtWPFLib5.WaterjetDbWindowVM).CloseWaterjetDb_OmagCut(m_MainWindow)
            If nPressedBtn = EgtWPFLib5.WaterjetDbWindowVM.SaveWndBtnEnum.CANCEL Then
               StatisticsBtn.IsChecked = False
               WaterJetBtn.IsChecked = True
               Return
            ElseIf nPressedBtn = EgtWPFLib5.WaterjetDbWindowVM.SaveWndBtnEnum.NO Then
               m_WaterJetPageUC.WaterJetPage_Reinitialize()
            End If
            ' passo alla pagina Dati Macchina
            TestingPageBtn.IsChecked = True
            WaterJetBtn.IsChecked = False
            MachinePageGrid.Children.Remove(m_WaterJetPageUC)
            MachinePageGrid.Children.Add(m_StatisticsPageUC)
            m_ActiveMachinePage = MachinePages.Statistics
         Case MachinePages.MachineCN
            StatisticsBtn.IsChecked = True
            TestingPageBtn.IsChecked = False
            MachinePageGrid.Children.Remove(m_MachineCNPageUC)
            MachinePageGrid.Children.Add(m_StatisticsPageUC)
            m_ActiveMachinePage = MachinePages.Statistics
         Case MachinePages.Statistics
            StatisticsBtn.IsChecked = True
      End Select
   End Sub

   Private Sub MachinePage_Unloaded(sender As Object, e As RoutedEventArgs) Handles Me.Unloaded
      Select Case m_ActiveMachinePage
         Case MachinePages.Alarms
            If Not IsNothing(m_AlarmsPageUC.m_SetUpPage) And MachinePageGrid.Children.Contains(m_AlarmsPageUC.m_SetUpPage) Then
               MachinePageGrid.Children.Remove(m_AlarmsPageUC.m_SetUpPage)
               MachinePageGrid.Children.Add(m_AlarmsPageUC)
            End If
         Case MachinePages.ToolsDb
            AlarmsBtn.IsChecked = True
            ToolsDBBtn.IsChecked = False
            MachinePageGrid.Children.Remove(m_ToolsDbPageUC)
            MachinePageGrid.Children.Add(m_AlarmsPageUC)
            m_ActiveMachinePage = MachinePages.Alarms
         Case MachinePages.MachiningDb
            AlarmsBtn.IsChecked = True
            MachiningDBBtn.IsChecked = False
            MachinePageGrid.Children.Remove(m_MachiningDbPageUC)
            MachinePageGrid.Children.Add(m_AlarmsPageUC)
            m_ActiveMachinePage = MachinePages.Alarms
         Case MachinePages.Polishings
            AlarmsBtn.IsChecked = True
            PolishingsBtn.IsChecked = False
            MachinePageGrid.Children.Remove(m_PolishingsPageUC)
            MachinePageGrid.Children.Add(m_AlarmsPageUC)
            m_ActiveMachinePage = MachinePages.Alarms
         Case MachinePages.WaterJet
            ' Verifica ed eventuale salvataggio Lucidatura corrente
            'If Not m_PolishingsPageUC.SaveKit() Then
            '   AlarmsBtn.IsChecked = False
            '   PolishingsBtn.IsChecked = True
            '   Return
            'End If
            ' passo alla pagina Dati Macchina
            AlarmsBtn.IsChecked = True
            WaterJetBtn.IsChecked = False
            MachinePageGrid.Children.Remove(m_WaterJetPageUC)
            MachinePageGrid.Children.Add(m_AlarmsPageUC)
            m_ActiveMachinePage = MachinePages.Alarms
         Case MachinePages.MachineCN
            AlarmsBtn.IsChecked = True
            TestingPageBtn.IsChecked = False
            MachinePageGrid.Children.Remove(m_MachineCNPageUC)
            MachinePageGrid.Children.Add(m_AlarmsPageUC)
            m_ActiveMachinePage = MachinePages.Alarms
         Case MachinePages.Statistics
            AlarmsBtn.IsChecked = True
            StatisticsBtn.IsChecked = False
            MachinePageGrid.Children.Remove(m_StatisticsPageUC)
            MachinePageGrid.Children.Add(m_AlarmsPageUC)
            m_ActiveMachinePage = MachinePages.Alarms
      End Select
   End Sub

End Class
