Imports EgtUILib
Imports EgtWPFLib

Public Class DirectCutPageUC

   ' Dichiarazione delle Page UserControl
   Private m_MainWindow As MainWindow = DirectCast(Application.Current.MainWindow, MainWindow)
   Friend m_MachineButtons As MachineButtonsUC
   Friend m_ManualAxesMove As ManualAxesMoveUC
   Friend m_SingleCut As SingleCutUC
   Friend m_SingleDrill As SingleDrillUC
   ' nuova finestra per i comandi per il taglio manuale nuovo
   Friend m_SingleCutAuto As SingleCutAuto
   Friend m_MultipleCut As MultipleCut
   Friend m_GridCut As GridCut
   Friend m_FlatteningCut As FlatteningCut
   Friend m_Polishing As Polishing
   Friend m_CopyTemplate As CopyTemplateUC
   Friend m_SawTest As SawTestUC
   Friend m_ControlsMachineButton As ControlsMachineButtonUC
   Friend m_VacuumMachineButton As VacuumMachineButtonUC

   ' Stato di visualizzazione della macchina
   Friend m_bShowMachine As Boolean = False
   Friend m_nMachLook As Integer = MCH_LOOK.ALL
   ' Dati generali CN
   Friend m_CN As CN_generico
   Private m_bFirst As Boolean = True
   ' Riferimento alla pagina correntemente attiva
   Friend m_ActiveDirectCutPage As DirectCutPages = DirectCutPages.DirectCut
   ' Variabili che indicano attivazione paginette con bottoni macchina
   Private m_ControlsBtn_IsActive As Boolean

   Public Shared m_dZSafe As Double = 52

   ' Variabile che indica se sono attivi i bottoni macchina nuovi o vecchi
   Friend m_NewMachineButtonsType As Boolean
   ' Indica che la finestra del talio Singolo deve essere di tipo manuale
   Friend m_bManulaCut As Boolean = False

   ' elenco dei nuovi bottoni
   Private m_ButtonJogList As New List(Of MachineButton)

   Enum DirectCutPages
      DirectCut
      ManualAxesMove
      SingleCut
      MultipleCut
      GridCut
      FlatteningCut
      Polishing
      CopyTemplate
      SawTest
      SingleCutAuto
      SingleDrill
   End Enum

   Private Sub DirectCutPage_Initialized(sender As Object, e As EventArgs) Handles Me.Initialized
      ' leggo la configurazione del programma per i nuovi tagli manuali
      m_bManulaCut = GetPrivateProfileInt(S_GENERAL, K_MANUAL_CUT, 0, m_MainWindow.GetIniFile()) <> 0

      'Creazione delle Page UserControl
      m_MachineButtons = New MachineButtonsUC
      m_ManualAxesMove = New ManualAxesMoveUC
      m_SingleCut = New SingleCutUC
      m_SingleDrill = New SingleDrillUC
      m_MultipleCut = New MultipleCut
      m_GridCut = New GridCut
      m_FlatteningCut = New FlatteningCut
      m_Polishing = New Polishing
      m_CopyTemplate = New CopyTemplateUC
      m_SawTest = New SawTestUC
      m_ControlsMachineButton = New ControlsMachineButtonUC
      m_VacuumMachineButton = New VacuumMachineButtonUC
      ' Costruisco la finestra per il nuovo tipo di taglio manuale
      m_SingleCutAuto = New SingleCutAuto

      'Posizionemento nella griglia delle Page UserControl
      m_MachineButtons.SetValue(Grid.RowProperty, 2)
      m_MachineButtons.SetValue(Grid.ColumnSpanProperty, 7)
      m_ManualAxesMove.SetValue(Grid.RowProperty, 1)
      If m_bManulaCut Then
         m_SingleCutAuto.SetValue(Grid.RowProperty, 1)
      Else
         m_SingleCut.SetValue(Grid.RowProperty, 1)
      End If
      m_SingleDrill.SetValue(Grid.RowProperty, 1)
      m_MultipleCut.SetValue(Grid.RowProperty, 1)
      m_GridCut.SetValue(Grid.RowProperty, 1)
      m_FlatteningCut.SetValue(Grid.RowProperty, 1)
      m_Polishing.SetValue(Grid.RowProperty, 1)
      m_CopyTemplate.SetValue(Grid.RowProperty, 1)
      m_SawTest.SetValue(Grid.RowProperty, 1)
      m_ControlsMachineButton.SetValue(Grid.RowProperty, 1)
      m_VacuumMachineButton.SetValue(Grid.RowProperty, 1)

      ' verifico che sia presente almeno uno dei bottoni della nuova console (SOLO PER MACCHINA CON paragrafo [ControlMachButtons])
      Dim bCollapsedNewBottonsConsole As Boolean = Not m_ControlsMachineButton.GetPrivateProfileMachineButtons("JogButtons", "Button1", Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, m_MainWindow.GetMachIniFile())
      m_NewMachineButtonsType = m_ControlsMachineButton.GetPrivateProfileMachineButtons(S_CONTROLMACHBUTTONS, "Button1", Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, m_MainWindow.GetMachIniFile())
      If Not m_NewMachineButtonsType Then
         'Assegno MachineButtons alla pagina
         LowerButtonGrid.Children.Add(m_MachineButtons)
         ControlsMachineBtn.Visibility = Windows.Visibility.Collapsed
         VacuumMachineBtn.Visibility = Windows.Visibility.Collapsed
      End If

      If GetPrivateProfileInt(S_NUMERICALCONTROL, K_TYPE, 0, m_MainWindow.GetMachIniFile()) = 0 Then
         ManualBtn.IsEnabled = False
         ManualModeBtn.Visibility = Windows.Visibility.Hidden
      End If

      ' ManualBtn.Content = EgtMsg(90201)
      ManualTxt.Text = EgtMsg(90201)
      'SingleCutBtn.Content = EgtMsg(90202)
      SingleCutTxt.Text = EgtMsg(90202)
      'SingleDrillBtn.Content = "Foro singolo"
      SingleDrillTxt.Text = EgtMsg(90258)
      'MultipleCutBtn.Content = EgtMsg(90203)
      MultipleCutTxt.Text = EgtMsg(90203)
      'GridCutBtn.Content = EgtMsg(90204)
      GridCutTxt.Text = EgtMsg(90204)
      'FlatteningCutBtn.Content = EgtMsg(90206)
      FlatteningCutTxt.Text = EgtMsg(90206)
      'PolishingBtn.Content = EgtMsg(90231)
      PolishingTxt.Text = EgtMsg(90231)
      'CopyTemplateBtn.Content = EgtMsg(90209)
      CopyTemplateTxt.Text = EgtMsg(90209)
      'SawTestBtn.Content = EgtMsg(90207)
      SawTestTxt.text = EgtMsg(90207)


      ' nuovi bottoni 20/05/2021 - modifica specifiche 04/08/2021
      ' leggo il numero di assi attivi nella macchina
      Dim AxesNumber = GetPrivateProfileInt(S_AXES, K_AXESNUM, 5, m_MainWindow.GetMachIniFile())

   End Sub

   Private Sub DirectCutPage_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
      '------ INIZIO NUOVI BOTTONI ----------
      ' Lettura configurazione bottoni (per impostare l'uso del Joystick) da Ini di macchina
      m_ButtonJogList.Clear()
      Dim m_nCount As Integer = 1
      Dim bFoundBtn As Boolean = True
      While bFoundBtn
         Dim sNameBtn As String = K_BUTTON & (m_nCount).ToString()
         Dim sMachineButtonType As String = String.Empty
         Dim sTImageName As String = String.Empty
         Dim sTLuaScriptName As String = String.Empty
         Dim sFImageName As String = String.Empty
         Dim sFLuaScriptName As String = String.Empty
         Dim sStateFlag As String = String.Empty
         bFoundBtn = m_ControlsMachineButton.GetPrivateProfileMachineButtons("JogButtons", sNameBtn, sMachineButtonType, sTImageName, sTLuaScriptName, sFImageName, sFLuaScriptName, sStateFlag, m_MainWindow.GetMachIniFile())
         If bFoundBtn Then
            m_nCount += 1
            Dim ButtonToAdd As MachineButton = Nothing
            ' la selezione dei comandi si trova nella pagina m_ControlsMachineButton.MachineButtonType (tipo Enum)
            Select Case sMachineButtonType
               Case 1
                  ButtonToAdd = New NoStateButton(sTImageName, sTLuaScriptName, sFImageName, sFLuaScriptName, sStateFlag)
               Case 2
                  ButtonToAdd = New TwoStateButton(sTImageName, sTLuaScriptName, sFImageName, sFLuaScriptName, sStateFlag)
               Case 3
                  ButtonToAdd = New ThreeStateButton(sTImageName, sTLuaScriptName, sFImageName, sFLuaScriptName, sStateFlag)
               Case 4
                  ButtonToAdd = New DoubleCommandButton(sTImageName, sTLuaScriptName, sFImageName, sFLuaScriptName, sStateFlag)
               Case 5
                  ButtonToAdd = New PressedCommandButton(sTImageName, sTLuaScriptName, sFImageName, sFLuaScriptName, sStateFlag)
            End Select
            If Not IsNothing(ButtonToAdd) Then
               m_ButtonJogList.Add(ButtonToAdd)
            End If
         End If
      End While
      ' attivo la visualizzazione dei comandi
      SetVisibilityJogButtuns()
      ' lo stato di questi comandi vieni carico dalla lettura del PLC (refresh)
      '----------- FINE NUOVI BOTTONI------------

      ' Se rientro da simulazione
      If m_ActiveDirectCutPage = DirectCutPages.SingleCut Then
         LeftButtonGrid.Children.Add(m_SingleCut)
         Return
      ElseIf m_ActiveDirectCutPage = DirectCutPages.SingleCutAuto Then
         LeftButtonGrid.Children.Add(m_SingleCutAuto)
         Return
      ElseIf m_ActiveDirectCutPage = DirectCutPages.MultipleCut Then
         LeftButtonGrid.Children.Add(m_MultipleCut)
         Return
      ElseIf m_ActiveDirectCutPage = DirectCutPages.GridCut Then
         LeftButtonGrid.Children.Add(m_GridCut)
         Return
      ElseIf m_ActiveDirectCutPage = DirectCutPages.FlatteningCut Then
         LeftButtonGrid.Children.Add(m_FlatteningCut)
         Return
      ElseIf m_ActiveDirectCutPage = DirectCutPages.Polishing Then
         LeftButtonGrid.Children.Add(m_Polishing)
         Return
      ElseIf m_ActiveDirectCutPage = DirectCutPages.SawTest Then
         LeftButtonGrid.Children.Add(m_SawTest)
         Return
      ElseIf m_ActiveDirectCutPage = DirectCutPages.SingleDrill Then
         LeftButtonGrid.Children.Add(m_SingleDrill)
         Return
      End If

      ' Caso standard
      m_ActiveDirectCutPage = DirectCutPages.DirectCut

      ' Se macchina fotografica abilitata
      PhotoBtn.IsEnabled = m_MainWindow.GetKeyOption(MainWindow.KEY_OPT.MAN_PHOTO)

      ' Attivo le lavorazioni solo se esiste il grezzo e se presente testa H1
      Dim bRawOk As Boolean = (GetRawHeight() > EPS_SMALL)
      Dim bH1Exists As Boolean = (EgtGetHeadId("H1") <> GDB_ID.NULL)

      ManualBtn.IsEnabled = bH1Exists
      ' sviluppo rimasto in sospeso
      If m_bManulaCut Then
         ' anche se non è presente un grezzo mantengo abilitato il bottone
         SingleCutBtn.IsEnabled = True
      Else
         SingleCutBtn.IsEnabled = bRawOk
      End If
      MultipleCutBtn.IsEnabled = bRawOk
      GridCutBtn.IsEnabled = bRawOk
      SingleDrillBtn.IsEnabled = (bRawOk And bH1Exists)
      FlatteningCutBtn.IsEnabled = (bRawOk And bH1Exists)
      PolishingBtn.IsEnabled = (bRawOk And m_MainWindow.m_CurrentMachine.bPolishing)
      SawTestBtn.IsEnabled = (bRawOk And bH1Exists)
      CopyTemplateBtn.IsEnabled = bH1Exists

      ' aggiorno la visualizzazione dei parametri
      ReloadParam()

      ' Nascondo i pezzi in parcheggio
      HideParkedParts()
      EgtZoom(ZM.ALL)

   End Sub

   Public Sub ReloadParam()
      Dim sCurrSawing As String = m_MainWindow.m_CurrentMachine.sCurrSawing
      Dim sCurrSaw As String = m_MainWindow.m_CurrentMachine.sCurrSaw
      Dim bSaw As Boolean = False

      ' se esiste una lavorazione di lama corrente vuol dire che deve essere preferita
      If Not String.IsNullOrEmpty(sCurrSaw) And Not String.IsNullOrEmpty(sCurrSawing) Then
         bSaw = EgtSetCalcTool(sCurrSaw, "H1", 1)
      End If
      m_SingleCut.SetEnableParam(bSaw)
      m_MultipleCut.SetEnableParam(bSaw)
      m_GridCut.SetEnableParam(bSaw)
   End Sub

   Private Sub SetVisibilityJogButtuns()
      XYBtn.Visibility = Visibility.Collapsed
      ZCBtn.Visibility = Visibility.Collapsed
      ZBBtn.Visibility = Visibility.Collapsed
      RemoteBtn.Visibility = Visibility.Collapsed

      Dim Item As MachineButton = Nothing
      For Each Item In m_ButtonJogList
         Select Case Item.StateFlag.Trim
            Case K_XYJOG
               XYBtn.Visibility = Visibility.Visible
               XYBtn.Foreground = Brushes.White
               XYBtn.ToolTip = "X - Y"
            Case K_ZCJOG
               ZCBtn.Visibility = Visibility.Visible
               ZCBtn.Foreground = Brushes.White
               ZCBtn.ToolTip = "Z - C"
            Case K_ZBJOG
               ZBBtn.Visibility = Visibility.Visible
               ZBBtn.Foreground = Brushes.White
               ZBBtn.ToolTip = "Z - B"
            Case K_REMOTE
               RemoteBtn.Visibility = Visibility.Visible
               RemoteBtn.Foreground = Brushes.White
               RemoteBtn.Content = "Remote"
         End Select
      Next
   End Sub

   Private Sub PhotoBtn_Click(sender As Object, e As RoutedEventArgs) Handles PhotoBtn.Click
      EgtSetCurrentContext(m_MainWindow.m_CurrentProjectPageUC.CurrentProjectScene.GetCtx())
      ' Se macchina fotografica collegata, faccio una foto
      If m_MainWindow.m_Camera.GetCameraLink() Then
         If Not m_MainWindow.m_Camera.CameraClick() Then
            m_MainWindow.m_CurrentProjectPageUC.SetErrorMessage(EgtMsg(90313))    'Fotografia non riuscita
         End If
         ' Altrimenti lancio browser di immagini
      Else
         m_MainWindow.MainWindowGrid.Children.Remove(m_MainWindow.m_CurrentProjectPageUC)
         m_MainWindow.MainWindowGrid.Children.Add(m_MainWindow.m_PhotoPage)
         m_MainWindow.m_ActivePage = MainWindow.Pages.Photo
         m_MainWindow.m_PrevActivePage = MainWindow.Pages.DirectCut
      End If
   End Sub

   Private Sub RawPartBtn_Click(sender As Object, e As RoutedEventArgs) Handles RawPartBtn.Click
      m_MainWindow.m_CurrentProjectPageUC.CurrProjGrid.Visibility = Windows.Visibility.Hidden
      m_MainWindow.m_CurrentProjectPageUC.CurrentProjectPageGrid.Children.Remove(Me)
      m_MainWindow.m_CurrentProjectPageUC.CurrentProjectPageGrid.Children.Add(m_MainWindow.m_RawPartPage)
      m_MainWindow.m_ActivePage = MainWindow.Pages.RawPart
      m_MainWindow.m_PrevActivePage = MainWindow.Pages.DirectCut
   End Sub

   Private Sub ManualBtn_Click(sender As Object, e As RoutedEventArgs) Handles ManualBtn.Click
      LeftButtonGrid.Children.Add(m_ManualAxesMove)
      m_ActiveDirectCutPage = DirectCutPages.ManualAxesMove
   End Sub

   Private Sub SingleCutBtn_Click(sender As Object, e As RoutedEventArgs) Handles SingleCutBtn.Click
      If m_bManulaCut Then
         LeftButtonGrid.Children.Add(m_SingleCutAuto)
         m_ActiveDirectCutPage = DirectCutPages.SingleCutAuto
      Else
         LeftButtonGrid.Children.Add(m_SingleCut)
         m_ActiveDirectCutPage = DirectCutPages.SingleCut
      End If

   End Sub

   Private Sub MultipleCutBtn_Click(sender As Object, e As RoutedEventArgs) Handles MultipleCutBtn.Click
      LeftButtonGrid.Children.Add(m_MultipleCut)
      m_ActiveDirectCutPage = DirectCutPages.MultipleCut
   End Sub

   Private Sub GridCutBtn_Click(sender As Object, e As RoutedEventArgs) Handles GridCutBtn.Click
      LeftButtonGrid.Children.Add(m_GridCut)
      m_ActiveDirectCutPage = DirectCutPages.GridCut
   End Sub

   Private Sub FlatteningCutBtn_Click(sender As Object, e As RoutedEventArgs) Handles FlatteningCutBtn.Click
      LeftButtonGrid.Children.Add(m_FlatteningCut)
      m_ActiveDirectCutPage = DirectCutPages.FlatteningCut
   End Sub

   Private Sub PolishingBtn_Click(sender As Object, e As RoutedEventArgs) Handles PolishingBtn.Click
      LeftButtonGrid.Children.Add(m_Polishing)
      m_ActiveDirectCutPage = DirectCutPages.Polishing
   End Sub

   Private Sub CopyTemplateBtn_Click(sender As Object, e As RoutedEventArgs) Handles CopyTemplateBtn.Click
      LeftButtonGrid.Children.Add(m_CopyTemplate)
      m_ActiveDirectCutPage = DirectCutPages.CopyTemplate
   End Sub

   Private Sub SawTestBtn_Click(sender As Object, e As RoutedEventArgs) Handles SawTestBtn.Click
      LeftButtonGrid.Children.Add(m_SawTest)
      m_ActiveDirectCutPage = DirectCutPages.SawTest
   End Sub

   Private Sub SingleDrillBtn_Click(sender As Object, e As RoutedEventArgs) Handles SingleDrillBtn.Click
      LeftButtonGrid.Children.Add(m_SingleDrill)
      m_ActiveDirectCutPage = DirectCutPages.SingleDrill
   End Sub

   Private Sub ControlsMachineBtn_Click(sender As Object, e As RoutedEventArgs) Handles ControlsMachineBtn.Click
      If ControlsMachineBtn.IsChecked Then
         m_ControlsMachineButton = New ControlsMachineButtonUC
         m_ControlsMachineButton.SetValue(Grid.RowProperty, 1)
         m_NewMachineButtonsType = m_ControlsMachineButton.GetPrivateProfileMachineButtons(S_CONTROLMACHBUTTONS, "Button1", Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, m_MainWindow.GetMachIniFile())
         MachineButtonGrid.Children.Add(m_ControlsMachineButton)
         If VacuumMachineBtn.IsChecked Then
            MachineButtonGrid.Children.Remove(m_VacuumMachineButton)
            VacuumMachineBtn.IsChecked = False
         End If
      Else
         MachineButtonGrid.Children.Remove(m_ControlsMachineButton)
      End If
   End Sub

   Private Sub VacuumMachineBtn_Click(sender As Object, e As RoutedEventArgs) Handles VacuumMachineBtn.Click
      If VacuumMachineBtn.IsChecked Then
         MachineButtonGrid.Children.Add(m_VacuumMachineButton)
         If ControlsMachineBtn.IsChecked Then
            MachineButtonGrid.Children.Remove(m_ControlsMachineButton)
            ControlsMachineBtn.IsChecked = False
         End If
      Else
         MachineButtonGrid.Children.Remove(m_VacuumMachineButton)
      End If
   End Sub

   '------------------NUOVI BOTTONI PER GESTIONE JOYSTICK-------------------------------------------------
   Private Sub XYBtn_Click(sender As Object, e As RoutedEventArgs) Handles XYBtn.Click
      ' riverco in elenco il bottone XYJog
      Dim XYJogButton As MachineButton = Nothing
      Dim ItemButton As MachineButton = Nothing
      For Each ItemButton In m_ButtonJogList
         If ItemButton.StateFlag = K_XYJOG Then
            XYJogButton = ItemButton
            Exit For
         End If
      Next
      If IsNothing(XYJogButton) Then Return
      ' eseguo lo script lua associato
      XYJogButton.ExecuteMDICommand(XYJogButton.TLuaScriptName)
      ' che comunica al PLC di cambiare lo stato, la modifica dello stato è intercettata dalla funzione Refresh che avvia il metodo Changed
   End Sub

   Friend Sub XYJogChanged(bXYAxes As Boolean)
      ' devo decodificare il tipo di pulsante, quindi eseguire la conversione...
      Dim XYJogButton As TwoStateButton = Nothing
      For Each MachineButton As MachineButton In m_ButtonJogList
         ' elimino gli spazi ad inizio e fine della stringa
         If MachineButton.StateFlag.Trim = K_XYJOG Then
            XYJogButton = MachineButton
            Exit For
         End If
      Next
      If Not IsNothing(XYJogButton) Then
         XYJogButton.SetIsChecked(bXYAxes)
         XYBtn.IsChecked = bXYAxes
      End If
   End Sub

   Private Sub ZCBtn_Click(sender As Object, e As RoutedEventArgs) Handles ZCBtn.Click
      ' riverco in elenco il bottone ZCYJog
      Dim ZJogButton As MachineButton = Nothing
      Dim ItemButton As MachineButton = Nothing
      For Each ItemButton In m_ButtonJogList
         If ItemButton.StateFlag = K_ZCJOG Then
            ZJogButton = ItemButton
            Exit For
         End If
      Next
      If IsNothing(ZJogButton) Then Return
      ' eseguo lo script lua associato
      ZJogButton.ExecuteMDICommand(ZJogButton.TLuaScriptName)
   End Sub

   Friend Sub ZCJogChanged(bZAxes As Boolean)
      ' devo decodificare il tipo di pulsante, quindi eseguire la conversione...
      Dim ZJogButton As TwoStateButton = Nothing
      For Each MachineButton As MachineButton In m_ButtonJogList
         If MachineButton.StateFlag.Trim = K_ZCJOG Then
            ZJogButton = MachineButton
            Exit For
         End If
      Next
      If Not IsNothing(ZJogButton) Then
         ZJogButton.SetIsChecked(bZAxes)
         ZCBtn.IsChecked = bZAxes
      End If
   End Sub

   Private Sub ZBBtn_Click(sender As Object, e As RoutedEventArgs) Handles ZBBtn.Click
      ' riverco in elenco il bottone ZCYJog
      Dim ZBJogButton As MachineButton = Nothing
      Dim ItemButton As MachineButton = Nothing
      For Each ItemButton In m_ButtonJogList
         If ItemButton.StateFlag = K_ZBJOG Then
            ZBJogButton = ItemButton
            Exit For
         End If
      Next
      If IsNothing(ZBJogButton) Then Return
      ' eseguo lo script lua associato
      ZBJogButton.ExecuteMDICommand(ZBJogButton.TLuaScriptName)
   End Sub

   Friend Sub ZBJogChanged(bBCAxes As Boolean)
      ' devo decodificare il tipo di pulsante, quindi eseguire la conversione...
      Dim BCJogButton As TwoStateButton = Nothing
      For Each MachineButton As MachineButton In m_ButtonJogList
         If MachineButton.StateFlag.Trim = K_ZBJOG Then
            BCJogButton = MachineButton
            Exit For
         End If
      Next
      If Not IsNothing(BCJogButton) Then
         BCJogButton.SetIsChecked(bBCAxes)
         ZBBtn.IsChecked = bBCAxes
      End If
   End Sub

   Private Sub RemoteBtn_Click(sender As Object, e As RoutedEventArgs) Handles RemoteBtn.Click
      Dim RemoteButton As MachineButton = Nothing
      Dim ItemButton As MachineButton = Nothing
      For Each ItemButton In m_ButtonJogList
         If ItemButton.StateFlag = K_REMOTE Then
            RemoteButton = ItemButton
            Exit For
         End If
      Next
      If IsNothing(RemoteButton) Then Return
      ' eseguo lo script lua associato
      Dim CurrentBtn As Primitives.ToggleButton = e.Source
      Dim sLuaFileName As String = String.Empty
      ' verifico lo stato del bottone per avviare il giusto script
      If CurrentBtn.IsChecked() Then
         sLuaFileName = RemoteButton.TLuaScriptName
      Else
         sLuaFileName = RemoteButton.FLuaScriptName
      End If
      RemoteButton.ExecuteMDICommand(sLuaFileName)
   End Sub

   Friend Sub RemoteChanged(bRemote As Boolean)
      ' devo decodificare il tipo di pulsante, quindi eseguire la conversione...
      Dim RemoteButton As TwoStateButton = Nothing
      For Each MachineButton As MachineButton In m_ButtonJogList
         If MachineButton.StateFlag.Trim = K_REMOTE Then
            RemoteButton = MachineButton
            Exit For
         End If
      Next
      If Not IsNothing(RemoteButton) Then
         RemoteButton.SetIsChecked(bRemote)
         RemoteBtn.IsChecked = bRemote
      End If
   End Sub

   '------------------NUOVI BOTTONI PER GESTIONE JOYSTICK-------------------------------------------------
   Private Sub MachViewModeBtn_Click(sender As Object, e As RoutedEventArgs) Handles MachViewModeBtn.Click
      If m_bShowMachine Then
         ' aggiorno lo stato
         Select Case m_nMachLook
         Case MCH_LOOK.ALL
            m_nMachLook = MCH_LOOK.TAB_HEAD
         Case MCH_LOOK.TAB_HEAD
            m_nMachLook = MCH_LOOK.TAB_TOOL
         Case Else
            m_nMachLook = MCH_LOOK.ALL
         End Select
         ' aggiorno lo stato della macchina e la sua visualizzazione
         EgtSetMachineLook(m_nMachLook)
         EgtDraw()
      End If
   End Sub

   Friend Sub DirectCutPage_Unloaded(sender As Object, e As RoutedEventArgs) Handles Me.Unloaded
      Select Case m_ActiveDirectCutPage
         Case DirectCutPages.DirectCut Or DirectCutPages.ManualAxesMove
            ' Non è necessario fare alcunché
         Case DirectCutPages.SingleCut
            LeftButtonGrid.Children.Remove(m_SingleCut)
         Case DirectCutPages.SingleCutAuto
            LeftButtonGrid.Children.Remove(m_SingleCutAuto)
         Case DirectCutPages.MultipleCut
            LeftButtonGrid.Children.Remove(m_MultipleCut)
         Case DirectCutPages.GridCut
            LeftButtonGrid.Children.Remove(m_GridCut)
         Case DirectCutPages.FlatteningCut
            LeftButtonGrid.Children.Remove(m_FlatteningCut)
         Case DirectCutPages.Polishing
            LeftButtonGrid.Children.Remove(m_Polishing)
         Case DirectCutPages.CopyTemplate
            LeftButtonGrid.Children.Remove(m_CopyTemplate)
         Case DirectCutPages.SawTest
            LeftButtonGrid.Children.Remove(m_SawTest)
         Case DirectCutPages.SingleDrill
            LeftButtonGrid.Children.Remove(m_SingleDrill)
      End Select
      If ControlsMachineBtn.IsChecked Then
         MachineButtonGrid.Children.Remove(m_ControlsMachineButton)
         ControlsMachineBtn.IsChecked = False
      End If
      If VacuumMachineBtn.IsChecked Then
         MachineButtonGrid.Children.Remove(m_VacuumMachineButton)
         VacuumMachineBtn.IsChecked = False
      End If
   End Sub

   Private Sub ManualModeBtn_Click(sender As Object, e As RoutedEventArgs) Handles ManualModeBtn.Click
      ' Imposto modalità manuale della macchina
      Dim nResult As Short = m_CN.DGeneralFunctions_WriteCncMode(7)     ' Modalità manuale

   End Sub

End Class
