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

Public Class ControlsMachineButtonUC
   Implements INotifyPropertyChanged

   ' Costanti per nomi stati dei bottoni
   Private Const BTN_STATE_NOTHING As String = "Nothing"

   Private Enum MachineButtonType As Integer
      NOSTATE = 0
      TWOSTATE = 1
      THREESTATE = 2
      DOUBLECOMMAND = 3
      PRESSEDCOMMAND = 4
   End Enum

   ' Dichiarazione delle Page UserControl
   Private m_MainWindow As MainWindow = DirectCast(Application.Current.MainWindow, MainWindow)
   Private m_DirectCutPage As DirectCutPageUC
   Friend m_CN As CN_generico
   Private m_bFirst As Boolean = True

   ' Creazione converter da String a ImageSource
   Dim ImageConverter As New ImageSourceConverter

   Private m_ButtonList As New ObservableCollection(Of MachineButton)
   Public ReadOnly Property ButtonList As ObservableCollection(Of MachineButton)
      Get
         Return m_ButtonList
      End Get
   End Property

   Private Sub ControlsMachineButtonUC_Initialized(sender As Object, e As EventArgs) Handles Me.Initialized
      Me.DataContext = Me
   End Sub

   Private Sub ControlsMachineButtonUC_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
      If m_bFirst Then
         m_DirectCutPage = m_MainWindow.m_DirectCutPageUC
         m_bFirst = False

         ' Lettura configurazione bottoni da Ini di macchina
         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 = GetPrivateProfileMachineButtons(S_CONTROLMACHBUTTONS, sNameBtn, sMachineButtonType, sTImageName, sTLuaScriptName, sFImageName, sFLuaScriptName, sStateFlag, m_MainWindow.GetMachIniFile())
            If bFoundBtn Then
               m_nCount += 1
               Dim ButtonToAdd As MachineButton = Nothing
               Select Case sMachineButtonType
                  Case MachineButtonType.NOSTATE
                     ButtonToAdd = New NoStateButton(sTImageName, sTLuaScriptName, sFImageName, sFLuaScriptName, sStateFlag)
                  Case MachineButtonType.TWOSTATE
                     ButtonToAdd = New TwoStateButton(sTImageName, sTLuaScriptName, sFImageName, sFLuaScriptName, sStateFlag)
                  Case MachineButtonType.THREESTATE
                     ButtonToAdd = New ThreeStateButton(sTImageName, sTLuaScriptName, sFImageName, sFLuaScriptName, sStateFlag)
                  Case MachineButtonType.DOUBLECOMMAND
                     ButtonToAdd = New DoubleCommandButton(sTImageName, sTLuaScriptName, sFImageName, sFLuaScriptName, sStateFlag)
                  Case MachineButtonType.PRESSEDCOMMAND
                     ButtonToAdd = New PressedCommandButton(sTImageName, sTLuaScriptName, sFImageName, sFLuaScriptName, sStateFlag)
               End Select
               If Not IsNothing(ButtonToAdd) Then m_ButtonList.Add(ButtonToAdd)
            End If
         End While
         NotifyPropertyChanged("ButtonList")
      End If
   End Sub

   Friend Sub SpindleStateChanged(SpindleState As Boolean)
      Dim SpindleButton As TwoStateButton = Nothing
      For Each MachineButton As MachineButton In m_ButtonList
         If MachineButton.StateFlag = K_SPINDLE Then
            SpindleButton = MachineButton
         End If
      Next
      If Not IsNothing(SpindleButton) Then
         SpindleButton.SetIsChecked(SpindleState)
      End If
   End Sub

   Friend Sub LaserStateChanged(LaserState As Boolean)
      Dim LaserButton As TwoStateButton = Nothing
      For Each MachineButton As MachineButton In m_ButtonList
         If MachineButton.StateFlag = K_LASER Then
            LaserButton = MachineButton
         End If
      Next
      If Not IsNothing(LaserButton) Then
         LaserButton.SetIsChecked(LaserState)
      End If
   End Sub

   Friend Sub FeedHoldChanged(FeedHoldState As Boolean)
      Dim FeedHoldButton As TwoStateButton = Nothing
      For Each MachineButton As MachineButton In m_ButtonList
         If MachineButton.StateFlag = K_FEEDHOLD Then
            FeedHoldButton = MachineButton
         End If
      Next
      If Not IsNothing(FeedHoldButton) Then
         FeedHoldButton.SetIsChecked(FeedHoldState)
      End If
   End Sub

   Friend Sub SpeedHoldChanged(SpeedHoldState As Boolean)
      Dim SpeedHoldButton As TwoStateButton = Nothing
      For Each MachineButton As MachineButton In m_ButtonList
         If MachineButton.StateFlag = K_SPEEDHOLD Then
            SpeedHoldButton = MachineButton
         End If
      Next
      If Not IsNothing(SpeedHoldButton) Then
         SpeedHoldButton.SetIsChecked(SpeedHoldState)
      End If
   End Sub

   Friend Sub LaserTracStateChanged(LaserTracState As Boolean)
      Dim LaserTracButton As TwoStateButton = Nothing
      For Each MachineButton As MachineButton In m_ButtonList
         If MachineButton.StateFlag = K_LASERTRAC Then
            LaserTracButton = MachineButton
         End If
      Next
      If Not IsNothing(LaserTracButton) Then
         LaserTracButton.SetIsChecked(LaserTracState)
      End If
   End Sub

   Friend Sub CoolantStateChanged(CoolantState As Boolean)
      Dim CoolantButton As TwoStateButton = Nothing
      For Each MachineButton As MachineButton In m_ButtonList
         If MachineButton.StateFlag = K_COOLANT Then
            CoolantButton = MachineButton
         End If
      Next
      If Not IsNothing(CoolantButton) Then
         CoolantButton.SetIsChecked(CoolantState)
      End If
   End Sub

   Friend Sub InternalCoolantStateChanged(InternalCoolantState As Boolean)
      Dim IntCoolantButton As TwoStateButton = Nothing
      For Each MachineButton As MachineButton In m_ButtonList
         If MachineButton.StateFlag = K_INTERNALCOOLANT Then
            IntCoolantButton = MachineButton
         End If
      Next
      If Not IsNothing(IntCoolantButton) Then
         IntCoolantButton.SetIsChecked(InternalCoolantState)
      End If
   End Sub

   Friend Sub ExternCoolantStateChanged(ExternCoolantState As Boolean)
      Dim ExtCoolantButton As TwoStateButton = Nothing
      For Each MachineButton As MachineButton In m_ButtonList
         If MachineButton.StateFlag = K_EXTERNCOOLANT Then
            ExtCoolantButton = MachineButton
         End If
      Next
      If Not IsNothing(ExtCoolantButton) Then
         ExtCoolantButton.SetIsChecked(ExternCoolantState)
      End If
   End Sub

   Friend Sub CBAxesStateChanged(CBAxesState As Boolean)
      Dim CAxesButton As TwoStateButton = Nothing
      Dim BAxesButton As TwoStateButton = Nothing
      For Each MachineButton As MachineButton In m_ButtonList
         If MachineButton.StateFlag = K_CAXES Then
            CAxesButton = MachineButton
         ElseIf MachineButton.StateFlag = K_BAXES Then
            BAxesButton = MachineButton
         End If
      Next
      If Not IsNothing(CAxesButton) Then
         CAxesButton.SetIsChecked(CBAxesState)
      End If
      If Not IsNothing(BAxesButton) Then
         BAxesButton.SetIsChecked(Not CBAxesState)
      End If
   End Sub

   Friend Sub AirBlowStateChanged(AirBlowState As Boolean)
      Dim AirBlowButton As TwoStateButton = Nothing
      For Each MachineButton As MachineButton In m_ButtonList
         If MachineButton.StateFlag = K_AIRBLOW Then
            AirBlowButton = MachineButton
         End If
      Next
      If Not IsNothing(AirBlowButton) Then
         AirBlowButton.SetIsChecked(AirBlowState)
      End If
   End Sub

   Friend Sub HomeStateChanged(HomeState As Boolean)
      Dim HomeButton As TwoStateButton = Nothing
      For Each MachineButton As MachineButton In m_ButtonList
         If MachineButton.StateFlag = K_HOME Then
            HomeButton = MachineButton
         End If
      Next
      If Not IsNothing(HomeButton) Then
         HomeButton.SetIsChecked(HomeState)
      End If
   End Sub

   Friend Sub TableUpChanged(TableUpState As Boolean)
      Dim TableUpButton As PressedCommandButton = Nothing
      For Each MachineButton As MachineButton In m_ButtonList
         If MachineButton.StateFlag = K_TABLEUP Then
            TableUpButton = MachineButton
         End If
      Next
      If Not IsNothing(TableUpButton) Then
         TableUpButton.SetIsPressed(TableUpState)
      End If
   End Sub

   Friend Sub TableDownChanged(TableDownState As Boolean)
      Dim TableDownButton As PressedCommandButton = Nothing
      For Each MachineButton As MachineButton In m_ButtonList
         If MachineButton.StateFlag = K_TABLEDOWN Then
            TableDownButton = MachineButton
         End If
      Next
      If Not IsNothing(TableDownButton) Then
         TableDownButton.SetIsPressed(TableDownState)
      End If
   End Sub

   Friend Sub HSMChanged(HsmState As Boolean)
      Dim HsmButton As TwoStateButton = Nothing
      For Each MachineButton As MachineButton In m_ButtonList
         If MachineButton.StateFlag = K_HSM Then
            HsmButton = MachineButton
         End If
      Next
      If Not IsNothing(HsmButton) Then
         HsmButton.SetIsChecked(HsmState)
      End If
   End Sub

   Friend Sub DoorClosedChanged(DoorClosedState As Integer)
      Dim DoorClosedButton As ThreeStateButton = Nothing
      Dim DoorOpenedButton As ThreeStateButton = Nothing
      For Each MachineButton As MachineButton In m_ButtonList
         If MachineButton.StateFlag = K_DOORCLOSED Then
            'If TypeOf MachineButton Is NoStateButton Then
            '   DoorClosedButton = DirectCast(MachineButton, NoStateButton)
            'ElseIf TypeOf MachineButton Is TwoStateButton Then
            '   DoorClosedButton = DirectCast(MachineButton, TwoStateButton)
            'ElseIf TypeOf MachineButton Is ThreeStateButton Then
            '   DoorClosedButton = DirectCast(MachineButton, ThreeStateButton)
            'ElseIf TypeOf MachineButton Is DoubleCommandButton Then
            '   DoorClosedButton = DirectCast(MachineButton, DoubleCommandButton)
            'ElseIf TypeOf MachineButton Is PressedCommandButton Then
            '   DoorClosedButton = DirectCast(MachineButton, PressedCommandButton)
            'End If
            DoorClosedButton = MachineButton
         End If
         If MachineButton.StateFlag = "DoorOpened" Then
            DoorOpenedButton = MachineButton
         End If
      Next
      If Not IsNothing(DoorClosedButton) Then
         Select Case DoorClosedState
            Case 0
               DoorClosedButton.Background = Application.Current.FindResource("OmagCut_Red")
            Case 1, 2
               DoorClosedButton.Background = Application.Current.FindResource("OmagCut_LightGray")
         End Select
      End If
      If Not IsNothing(DoorOpenedButton) Then
         Select Case DoorClosedState
            Case 0
               DoorOpenedButton.Background = Application.Current.FindResource("OmagCut_LightGray")
            Case 1
               DoorOpenedButton.Background = Application.Current.FindResource("OmagCut_Yellow")
            Case 2
               DoorOpenedButton.Background = Application.Current.FindResource("OmagCut_Green")
         End Select
      End If
   End Sub

   Friend Sub LimitZChanged(LimitZState As Boolean)
      Dim TwoLimitZButton As TwoStateButton = Nothing
      Dim DoubleLimitZButton As DoubleCommandButton = Nothing
      For Each MachineButton As MachineButton In m_ButtonList
         If MachineButton.StateFlag = K_LIMITZ Then
            If TypeOf MachineButton Is DoubleCommandButton Then
               DoubleLimitZButton = MachineButton
               If Not IsNothing(DoubleLimitZButton) Then
                  DoubleLimitZButton.SetIsChecked(LimitZState)
               End If
            ElseIf TypeOf MachineButton Is TwoStateButton Then
               TwoLimitZButton = MachineButton
               If Not IsNothing(TwoLimitZButton) Then
                  TwoLimitZButton.SetIsChecked(LimitZState)
               End If
            End If
         End If
      Next
   End Sub

   Friend Sub FiveAxisStateChanged(FiveAxisState As Boolean)
      Dim ThreeAxesButton As TwoStateButton = Nothing
      Dim FiveAxesButton As TwoStateButton = Nothing
      For Each MachineButton As MachineButton In m_ButtonList
         If MachineButton.StateFlag = K_THREEAXES Then
            ThreeAxesButton = MachineButton
         ElseIf MachineButton.StateFlag = K_FIVEAXES Then
            FiveAxesButton = MachineButton
         End If
      Next
      If Not IsNothing(ThreeAxesButton) Then
         ThreeAxesButton.SetIsChecked(Not FiveAxisState)
      End If
      If Not IsNothing(FiveAxesButton) Then
         FiveAxesButton.SetIsChecked(FiveAxisState)
      End If
   End Sub

   Friend Sub ParkingStateChanged(ParkingState As Boolean)
      Dim ParkingButton As TwoStateButton = Nothing
      For Each MachineButton As MachineButton In m_ButtonList
         If MachineButton.StateFlag = K_PARKING Then
            ParkingButton = MachineButton
         End If
      Next
      If Not IsNothing(ParkingButton) Then
         ParkingButton.SetIsChecked(ParkingState)
      End If
   End Sub

   Friend Sub ZoneStateChanged(ZoneState As Integer)
      'Dim sFlag As String = String.Empty
      'If ZoneState = 1 Then
      '   sFlag = BTN_ENABLE_ZONE_1
      'ElseIf ZoneState = 2 Then
      '   sFlag = BTN_ENABLE_ZONE_2
      'End If
      'Dim EnableZoneButton As TwoStateButton = Nothing
      'For Each MachineButton As MachineButton In m_ButtonList
      '   If MachineButton.StateFlag = K_ENABLEZONE Then
      '      EnableZoneButton = MachineButton
      '   End If
      'Next
      'If Not IsNothing(EnableZoneButton) Then
      '   EnableZoneButton.SetIsChecked(EnableZone)
      'End If
   End Sub

   Friend Sub PcStateChanged(EnablePC As Boolean)
      'Dim EnablePCButton As TwoStateButton = Nothing
      'For Each MachineButton As MachineButton In m_ButtonList
      '   If MachineButton.StateFlag = K_ENABLEPC Then
      '      EnablePCButton = MachineButton
      '   End If
      'Next
      'If Not IsNothing(EnablePCButton) Then
      '   EnablePCButton.SetIsChecked(EnablePC)
      'End If
   End Sub


   Public Function GetPrivateProfileMachineButtons(
                         ByVal lpAppName As String,
                         ByVal lpKeyName As String,
                         ByRef lpMachineButtonType As String,
                         ByRef lpTImageName As String,
                         ByRef lpTLuaScriptName As String,
                         ByRef lpFImageName As String,
                         ByRef lpFLuaScriptName As String,
                         ByRef lpStateFlag As String,
                         ByVal lpFileName As String) As Boolean

      Dim sVal As String = String.Empty
      GetPrivateProfileString(lpAppName, lpKeyName, "", sVal, lpFileName)
      Dim sItems() As String = sVal.Split(",".ToCharArray)
      If sItems.Count() >= 3 Then
         lpMachineButtonType = sItems(0)
         lpTImageName = sItems(1)
         lpTLuaScriptName = sItems(2)
         If sItems.Count() >= 4 Then
            lpStateFlag = sItems(3)
            If sItems.Count() >= 6 Then
               lpFImageName = sItems(4)
               lpFLuaScriptName = sItems(5)
            End If
         Else
            lpStateFlag = BTN_STATE_NOTHING
         End If
         Return True
      End If
      Return False
   End Function

   Public Event PropertyChanged(sender As Object, e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged

   Public Sub NotifyPropertyChanged(propName As String)
      RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propName))
   End Sub

#Region "EVENTS"

   ' qesti eventi evsnti sono generati direttamente dei bottoni di tipo "Pressed"
   Private Sub PressedCommandButton_PreviewMouseUp(sender As Object, e As MouseButtonEventArgs)
      Dim Button As Button = DirectCast(sender, Button)
      If TypeOf Button.DataContext Is PressedCommandButton Then
         Dim PressedButton As PressedCommandButton = DirectCast(Button.DataContext, PressedCommandButton)
         PressedButton.OnMouseUp()
      End If
   End Sub

   Private Sub PressedCommandButton_PreviewMouseDown(sender As Object, e As MouseButtonEventArgs)
      Dim Button As Button = DirectCast(sender, Button)
      If TypeOf Button.DataContext Is PressedCommandButton Then
         Dim PressedButton As PressedCommandButton = DirectCast(Button.DataContext, PressedCommandButton)
         PressedButton.OnMouseDown()
      End If
   End Sub

#End Region

End Class

Public MustInherit Class MachineButton
   Implements INotifyPropertyChanged

   ' Costanti per nomi stati dei bottoni
   Friend Const BTN_STATE_SPINDLE As String = "Spindle"
   Friend Const BTN_STATE_COOLANT As String = "Coolant"
   Friend Const BTN_STATE_LASER As String = "Laser"
   Friend Const BTN_STATE_LASERTRAC As String = "LaserTrac"
   Friend Const BTN_STATE_THREEAXIS As String = "ThreeAxis"
   Friend Const BTN_STATE_FIVEAXIS As String = "FiveAxis"
   Friend Const BTN_STATE_NOTHING As String = "Nothing"
   Friend Const BTN_ENABLE_ZONE_1 As String = "EnableZone1"
   Friend Const BTN_ENABLE_ZONE_2 As String = "EnableZone2"

   ' Dichiarazione delle Page UserControl
   Friend Shared m_MainWindow As MainWindow = DirectCast(Application.Current.MainWindow, MainWindow)

   Friend Shared m_DirectCutPage As DirectCutPageUC = m_MainWindow.m_DirectCutPageUC
   Friend Shared m_CN As CN_generico = m_MainWindow.m_DirectCutPageUC.m_CN

   Private m_TImageName As String
   Public ReadOnly Property TImageName As String
      Get
         Return m_TImageName
      End Get
   End Property

   Private m_TLuaScriptName As String
   Public ReadOnly Property TLuaScriptName As String
      Get
         Return m_TLuaScriptName
      End Get
   End Property

   Private m_FImageName As String
   Public ReadOnly Property FImageName As String
      Get
         Return m_FImageName
      End Get
   End Property

   Private m_FLuaScriptName As String
   Public ReadOnly Property FLuaScriptName As String
      Get
         Return m_FLuaScriptName
      End Get
   End Property

   Private m_StateFlag As String
   Public ReadOnly Property StateFlag As String
      Get
         Return m_StateFlag
      End Get
   End Property

   Sub New(TImagePath As String, TLuaScriptPath As String, FImagePath As String, FLuaScriptPath As String, StateFlag As String)
      m_TImageName = TImagePath
      m_TLuaScriptName = TLuaScriptPath
      m_FImageName = FImagePath
      m_FLuaScriptName = FLuaScriptPath
      m_StateFlag = StateFlag
   End Sub

#Region "METHODS"

   Friend Sub ExecuteMDICommand(sLuaFileName As String)
      If String.IsNullOrWhiteSpace(sLuaFileName) Then Return
      Dim CmdString As String = String.Empty
      Dim b2Start As Boolean = False
      Dim EActive As Boolean = False
      Dim ENumber As String = String.Empty
      Dim EValue As String = String.Empty
      Dim EType As String = 0
      Dim ENumber2 As String = String.Empty
      Dim EValue2 As String = String.Empty
      Dim EType2 As String = 0
      ' eseguo la lettura del file .lua associato al comando
      ExecuteDirectCmdLua(sLuaFileName, CmdString, b2Start, EActive, ENumber, EValue, EType, ENumber2, EValue2, EType2)
      '--------------------------------------------------------- PLC ---------------------------------------------------------
      If EActive Then
         Select Case m_MainWindow.m_CNCommunication.m_nNCType
            Case 1, 2
               ' se scrittura delle varibili PLC
               If m_CN.m_NewVariable And m_MainWindow.m_CNCommunication.m_nNCType = 2 Then
                  ' solo per Flexium
                  m_CN.DPlcVariables_WriteVariables(ENumber, EValue)
                  If Not String.IsNullOrEmpty(ENumber2) And Not String.IsNullOrEmpty(EValue2) Then
                     m_CN.DPlcVariables_WriteVariables(ENumber2, EValue2)
                  End If
                  ' altrimenti scrittura delle variabili E
               Else
                  m_CN.DVariables_WriteVariables2(ENumber, EValue)
               End If
               Return
            Case 3
               ' scrivo prima variabile
               Select Case CShort(EType)
                  Case 1
                     m_CN.DVariables_WriteVariables3(ENumber, CShort(EType), CInt(EValue), 0, 0, "")
                     EgtOutLog("ENumber= " & ENumber & "EType= " & CShort(EType) & "EValue= " & CInt(EValue))
                  Case 2
                     m_CN.DVariables_WriteVariables3(ENumber, CShort(EType), 0, CLng(EValue), 0, "")
                     EgtOutLog("ENumber= " & ENumber & "EType= " & CShort(EType) & "EValue= " & CLng(EValue))
                  Case 3
                     Dim d As Double = 0
                     StringToDouble(EValue, d)
                     m_CN.DVariables_WriteVariables3(ENumber, CShort(EType), 0, 0, d, "")
                     EgtOutLog("ENumber= " & ENumber & "EType= " & CShort(EType) & "EValue= " & d)
                  Case 4
                     m_CN.DVariables_WriteVariables3(ENumber, CShort(EType), 0, 0, 0, EValue)
                     EgtOutLog("ENumber= " & ENumber & "EType= " & CShort(EType) & "EValue= " & EValue)
               End Select
               EgtOutLog("dopo scrittura prima variabile")
               If Not IsNothing(EType2) AndAlso CShort(EType) > 0 Then
                  System.Threading.Thread.Sleep(100)
                  ' scrivo seconda variabile
                  Select Case CShort(EType2)
                     Case 1
                        m_CN.DVariables_WriteVariables3(ENumber2, CShort(EType2), CInt(EValue2), 0, 0, "")
                        EgtOutLog("ENumber2= " & ENumber2 & "EType2= " & CShort(EType2) & "EValue2= " & CInt(EValue2))
                     Case 2
                        m_CN.DVariables_WriteVariables3(ENumber2, CShort(EType2), 0, CLng(EValue2), 0, "")
                        EgtOutLog("ENumber2= " & ENumber2 & "EType2= " & CShort(EType2) & "EValue2= " & CLng(EValue2))
                     Case 3
                        Dim d As Double = 0
                        StringToDouble(EValue2, d)
                        m_CN.DVariables_WriteVariables3(ENumber2, CShort(EType2), 0, 0, d, "")
                        EgtOutLog("ENumber2= " & ENumber2 & "EType2= " & CShort(EType2) & "EValue2= " & d)
                     Case 4
                        m_CN.DVariables_WriteVariables3(ENumber2, CShort(EType2), 0, 0, 0, EValue2)
                        EgtOutLog("ENumber2= " & ENumber2 & "EType2= " & CShort(EType2) & "EValue2= " & EValue2)
                  End Select
                  EgtOutLog("dopo scrittura seconda variabile")
               End If
               Return
         End Select
      End If
      '--------------------------------------------------------- MDI ---------------------------------------------------------
      Dim nResult As Short
      ' assegno la modalità di funzionamento
      Select Case m_MainWindow.m_CNCommunication.m_nNCType
         Case 1, 2
            nResult = m_CN.DGeneralFunctions_WriteCncMode(2)     ' Modalità MDI per controlli num
         Case 3
            'Per il siemens non devo fare nulla
      End Select
      ' salvo la stringa di comando ricevuta dal file lua
      m_CN.sz_ManualDataInput = CmdString
      ' esguo la funzione che ha il compito di scrivere il comando MDI
      m_CN.MDI_command()
      ' avvio l'esecuzione del comando
      Select Case m_MainWindow.m_CNCommunication.m_nNCType
         Case 1, 2
            System.Threading.Thread.Sleep(500)
            m_CN.DGeneralFunctions_CycleStart()
         Case 3
            'Per il siemens non devo fare nulla
      End Select
      ' se richiesto ripeto l'operazione 
      If b2Start Then
         EgtOutLog("Avvio secondo cliclo start")
         System.Threading.Thread.Sleep(800)
         m_CN.DGeneralFunctions_CycleStart()
      End If
      System.Threading.Thread.Sleep(300)
      ' riprostino la modalità manuale
      nResult = m_CN.DGeneralFunctions_WriteCncMode(7)     ' Modalità manuale
   End Sub

   ' lettura delle variavili scritte nel file Lua
   Friend Sub ExecuteDirectCmdLua(LuaFileName As String, ByRef CmdString As String, ByRef b2Start As Boolean, ByRef EActive As Boolean, ByRef ENumber As String, ByRef EValue As String, ByRef EType As String, ByRef ENumber2 As String, ByRef EValue2 As String, ByRef EType2 As String)
      Dim sBaseDir As String = m_MainWindow.m_CurrentMachine.sMachDir & "\DirectCmd\"
      If EgtLuaExecFile(sBaseDir & LuaFileName) Then
         EgtOutLog("Lua " & sBaseDir & LuaFileName & " eseguito")
      Else
         EgtOutLog("Lua " & sBaseDir & LuaFileName & " non eseguito")
      End If

      Dim dFeed As Double = 0
      If Not String.IsNullOrWhiteSpace(m_DirectCutPage.m_ManualAxesMove.FeedTxBx.Text) Then
         StringToLen(m_DirectCutPage.m_ManualAxesMove.FeedTxBx.Text, dFeed)
      Else
         EgtTdbSetCurrTool(m_MainWindow.m_CurrentProjectPageUC.ToolTxBx.Text)
         EgtTdbGetCurrToolParam(MCH_TP.FEED, dFeed)
      End If
      EgtLuaSetGlobNumVar("CMD.F", dFeed)
      Dim dSpeed As Double = 0
      If Not String.IsNullOrWhiteSpace(m_DirectCutPage.m_ManualAxesMove.SpeedTxBx.Text) Then
         StringToDouble(m_DirectCutPage.m_ManualAxesMove.SpeedTxBx.Text, dSpeed)
      Else
         EgtTdbSetCurrTool(m_MainWindow.m_CurrentProjectPageUC.ToolTxBx.Text)
         EgtTdbGetCurrToolParam(MCH_TP.SPEED, dSpeed)
      End If
      EgtLuaSetGlobNumVar("CMD.S", dSpeed)
      Dim dL1, dL2, dL3, dR1, dR2 As Double
      m_MainWindow.m_CNCommunication.GetAxesPositions(dL1, dL2, dL3, dR1, dR2)
      EgtLuaSetGlobNumVar("CMD.L1", dL1)
      EgtLuaSetGlobNumVar("CMD.L2", dL2)
      EgtLuaSetGlobNumVar("CMD.L3", dL3)
      EgtLuaSetGlobNumVar("CMD.R1", dR1)
      EgtLuaSetGlobNumVar("CMD.R2", dR2)
      EgtLuaSetGlobBoolVar("CMD.INCHES", m_MainWindow.m_CNCommunication.GetMachineInInches())
      EgtLuaCallFunction("CmdString")
      ' Leggo variabili
      CmdString = String.Empty
      EgtLuaGetGlobStringVar("CMD.CMDSTRING", CmdString)
      b2Start = False
      EgtLuaGetGlobBoolVar("CMD.DOUBLESTART", b2Start)
      EActive = False
      EgtLuaGetGlobBoolVar("CMD.EACTIVE", EActive)
      ENumber = String.Empty
      EgtLuaGetGlobStringVar("CMD.ENUMBER", ENumber)
      EValue = String.Empty
      EgtLuaGetGlobStringVar("CMD.EVALUE", EValue)
      EType = String.Empty
      EgtLuaGetGlobStringVar("CMD.ETYPE", EType)
      EgtOutLog("ENUMBER: " & ENumber & " EVALUE: " & EValue & " ETYPE: " & EType)
      ENumber2 = String.Empty
      EgtLuaGetGlobStringVar("CMD.ENUMBER2", ENumber2)
      EValue2 = String.Empty
      EgtLuaGetGlobStringVar("CMD.EVALUE2", EValue2)
      EType2 = String.Empty
      EgtLuaGetGlobStringVar("CMD.ETYPE2", EType2)
      EgtOutLog("ENUMBER2: " & ENumber2 & " EVALUE2: " & EValue2 & " ETYPE2: " & EType2)
      ' Reset lua
      EgtLuaResetGlobVar("CmdString")
      EgtLuaResetGlobVar("CMD")
      ' Log del comando
      EgtOutLog("CmdString=" & If(Not String.IsNullOrEmpty(CmdString), CmdString, "") &
                " b2Start=" & b2Start.ToString() &
                " EActive=" & EActive.ToString &
                " ENumber=" & If(Not String.IsNullOrEmpty(ENumber), ENumber, "") &
                " EValue=" & If(Not String.IsNullOrEmpty(EValue), EValue, "") &
                " EType=" & If(Not String.IsNullOrEmpty(EType), CInt(EType).ToString, "") &
                " ENumber2=" & If(Not String.IsNullOrEmpty(ENumber2), ENumber2, "") &
                " EValue2=" & If(Not String.IsNullOrEmpty(EValue2), EValue2, "") &
                " EType2=" & If(Not String.IsNullOrEmpty(EType2), CInt(EType2).ToString, ""))
   End Sub

#End Region    ' METHODS

   Public Event PropertyChanged(sender As Object, e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged

   Public Sub NotifyPropertyChanged(propName As String)
      RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propName))
   End Sub

End Class

Public Class TwoStateButton
   Inherits MachineButton

   Private m_IsChecked As Boolean
   Public Property IsChecked As Boolean
      Get
         Return m_IsChecked
      End Get
      Set(value As Boolean)
         ' accendo il comando, anche se dovrei aspettare di leggere lo stato da PLC
         m_IsChecked = value
         If value Then
            ExecuteMDICommand(TLuaScriptName)
         Else
            ExecuteMDICommand(FLuaScriptName)
         End If
      End Set
   End Property
   Friend Sub SetIsChecked(value As Boolean)
      m_IsChecked = value
      NotifyPropertyChanged("IsChecked")
      NotifyPropertyChanged("ImageSource")
   End Sub

   Public ReadOnly Property ImageSource As String
      Get
         Dim sNewIcons As String
         If (m_MainWindow.m_OptionsPageUC.ThemesCmBx.SelectedIndex = 0) Then
            sNewIcons = ""
         Else
            sNewIcons = "NewIcons\"
         End If
         ' Se variabile di stato "Nothing" o non collegato a CN, imposto immagine fissa
         If StateFlag = BTN_STATE_NOTHING Or Not m_MainWindow.m_bNCLink Or IsNothing(FImageName) Or String.IsNullOrWhiteSpace(FImageName) Then
            ' ritorno immagine dello stato true
            Return m_MainWindow.GetResourcesDir() & "\MachineButtonsImage\" & sNewIcons & TImageName
         End If
         If m_IsChecked Then
            ' ritorno immagine dello stato true
            Return m_MainWindow.GetResourcesDir() & "\MachineButtonsImage\" & sNewIcons & TImageName
         Else
            ' ritorno immagine dello stato false
            Return m_MainWindow.GetResourcesDir() & "\MachineButtonsImage\" & sNewIcons & FImageName
         End If
      End Get
   End Property

#Region "METHODS"

   Sub New(TImagePath As String, TLuaScriptPath As String, FImagePath As String, FLuaScriptPath As String, StateFlag As String)
      MyBase.New(TImagePath, TLuaScriptPath, FImagePath, FLuaScriptPath, StateFlag)
   End Sub

#End Region    ' METHODS

End Class

Public Class NoStateButton
   Inherits MachineButton

   ' definizione comandi
   Private m_cmdExecute As ICommand

   Public ReadOnly Property ImageSource As String
      Get
         Dim sNewIcons As String
         If (m_MainWindow.m_OptionsPageUC.ThemesCmBx.SelectedIndex = 0) Then
            sNewIcons = ""
         Else
            sNewIcons = "NewIcons\"
         End If
         Return m_MainWindow.GetResourcesDir() & "\MachineButtonsImage\" & sNewIcons & TImageName
      End Get
   End Property

#Region "METHODS"

   Sub New(TImagePath As String, TLuaScriptPath As String, FImagePath As String, FLuaScriptPath As String, StateFlag As String)
      MyBase.New(TImagePath, TLuaScriptPath, Nothing, Nothing, StateFlag)
   End Sub

#End Region    ' METHODS

#Region "COMMANDS"

#Region "Execute"

   ' Returns a command that manage the MainWindow_Unloaded command
   Public ReadOnly Property ExecuteCommand() As ICommand
      Get
         If m_cmdExecute Is Nothing Then
            m_cmdExecute = New Command(AddressOf Execute)
         End If
         Return m_cmdExecute
      End Get
   End Property

   ' Manage the MainWindow_Unloaded event. This method is invoked by the cmdMainWindow_Unloaded.
   Public Sub Execute()
      If m_MainWindow.m_CNCommunication.m_nNCType = 1 Or m_MainWindow.m_CNCommunication.m_nNCType = 2 Then
         If StateFlag = K_MANUAL Then
            m_CN.DGeneralFunctions_WriteCncMode(7)     ' Modalità manuale
         Else
            ExecuteMDICommand(TLuaScriptName)
         End If
      Else
         ExecuteMDICommand(TLuaScriptName)
      End If
   End Sub

#End Region    ' Execute

#End Region    ' COMMANDS

End Class

Public Class ThreeStateButton
   Inherits MachineButton

   ' definizione comandi
   Private m_cmdExecute As ICommand

   Private m_Background As SolidColorBrush
   Public Property Background As SolidColorBrush
      Get
         Return m_Background
      End Get
      Set(value As SolidColorBrush)
         m_Background = value
         NotifyPropertyChanged("Background")
      End Set
   End Property
   Public ReadOnly Property ImageSource As String
      Get
         Dim sNewIcons As String
         If (m_MainWindow.m_OptionsPageUC.ThemesCmBx.SelectedIndex = 0) Then
            sNewIcons = ""
         Else
            sNewIcons = "NewIcons\"
         End If
         ' ritorno immagine dello stato true
         Return m_MainWindow.GetResourcesDir() & "\MachineButtonsImage\" & sNewIcons & TImageName
      End Get
   End Property

#Region "METHODS"

   Sub New(TImagePath As String, TLuaScriptPath As String, FImagePath As String, FLuaScriptPath As String, StateFlag As String)
      MyBase.New(TImagePath, TLuaScriptPath, FImagePath, FLuaScriptPath, StateFlag)
   End Sub

#End Region    ' METHODS

#Region "COMMANDS"

#Region "Execute"

   ' Returns a command that manage the MainWindow_Unloaded command
   Public ReadOnly Property ExecuteCommand() As ICommand
      Get
         If m_cmdExecute Is Nothing Then
            m_cmdExecute = New Command(AddressOf Execute)
         End If
         Return m_cmdExecute
      End Get
   End Property

   ' Manage the MainWindow_Unloaded event. This method is invoked by the cmdMainWindow_Unloaded.
   Public Sub Execute()
      ExecuteMDICommand(TLuaScriptName)
      ExecuteMDICommand(FLuaScriptName)
   End Sub

#End Region    ' Execute

#End Region    ' COMMANDS

End Class

Public Class DoubleCommandButton
   Inherits MachineButton

   Private m_IsChecked As Boolean
   Public Property IsChecked As Boolean
      Get
         Return m_IsChecked
      End Get
      Set(value As Boolean)
         m_IsChecked = value
         ExecuteMDICommand(TLuaScriptName)
         System.Threading.Thread.Sleep(100)
         ExecuteMDICommand(FLuaScriptName)
      End Set
   End Property
   Friend Sub SetIsChecked(value As Boolean)
      m_IsChecked = value
      NotifyPropertyChanged("IsChecked")
      NotifyPropertyChanged("ImageSource")
   End Sub

   Public ReadOnly Property ImageSource As String
      Get
         Dim sNewIcons As String
         If (m_MainWindow.m_OptionsPageUC.ThemesCmBx.SelectedIndex = 0) Then
            sNewIcons = ""
         Else
            sNewIcons = "NewIcons\"
         End If
         ' Se variabile di stato "Nothing" o non collegato a CN, imposto immagine fissa
         If StateFlag = BTN_STATE_NOTHING Or Not m_MainWindow.m_bNCLink Or IsNothing(FImageName) Or String.IsNullOrWhiteSpace(FImageName) Then
            ' ritorno immagine dello stato true
            Return m_MainWindow.GetResourcesDir() & "\MachineButtonsImage\" & sNewIcons & TImageName
         End If
         If m_IsChecked Then
            ' ritorno immagine dello stato true
            Return m_MainWindow.GetResourcesDir() & "\MachineButtonsImage\" & sNewIcons & TImageName
         Else
            ' ritorno immagine dello stato false
            Return m_MainWindow.GetResourcesDir() & "\MachineButtonsImage\" & sNewIcons & FImageName
         End If
      End Get
   End Property

#Region "METHODS"

   Sub New(TImagePath As String, TLuaScriptPath As String, FImagePath As String, FLuaScriptPath As String, StateFlag As String)
      MyBase.New(TImagePath, TLuaScriptPath, FImagePath, FLuaScriptPath, StateFlag)
   End Sub

#End Region    ' METHODS

End Class

Public Class PressedCommandButton
   Inherits MachineButton

   Private m_IsPressed As Boolean
   Friend Sub SetIsPressed(value As Boolean)
      m_IsPressed = value
      NotifyPropertyChanged("ImageSource")
   End Sub

   Public ReadOnly Property ImageSource As String
      Get
         Dim sNewIcons As String
         If (m_MainWindow.m_OptionsPageUC.ThemesCmBx.SelectedIndex = 0) Then
            sNewIcons = ""
         Else
            sNewIcons = "NewIcons\"
         End If
         ' Se variabile di stato "Nothing" o non collegato a CN, imposto immagine fissa
         If StateFlag = BTN_STATE_NOTHING OrElse Not m_MainWindow.m_bNCLink OrElse IsNothing(FImageName) OrElse String.IsNullOrWhiteSpace(FImageName) Then
            ' ritorno immagine dello stato true
            Return m_MainWindow.GetResourcesDir() & "\MachineButtonsImage\" & sNewIcons & TImageName
         End If
         If m_IsPressed Then
            ' ritorno immagine dello stato true
            Return m_MainWindow.GetResourcesDir() & "\MachineButtonsImage\" & sNewIcons & TImageName
         Else
            ' ritorno immagine dello stato false
            Return m_MainWindow.GetResourcesDir() & "\MachineButtonsImage\" & sNewIcons & FImageName
         End If
      End Get
   End Property

#Region "METHODS"

   Sub New(TImagePath As String, TLuaScriptPath As String, FImagePath As String, FLuaScriptPath As String, StateFlag As String)
      MyBase.New(TImagePath, TLuaScriptPath, FImagePath, FLuaScriptPath, StateFlag)
   End Sub

   Friend Sub OnMouseDown()
      Try
         ExecuteMDICommand(TLuaScriptName)
         EgtOutLog("Comando MDI eseguito")
         SetIsPressed(True)
         EgtOutLog("Aggiornamento immagine bottone eseguito")
      Catch ex As Exception
         EgtOutLog("Eccezione riga 909 di ControlsMachineButtonUC.xaml.vb : " & ex.ToString)
      End Try

   End Sub

   Friend Sub OnMouseUp()
      Try
         ExecuteMDICommand(FLuaScriptName)
         EgtOutLog("Comando MDI eseguito")
         SetIsPressed(False)
         EgtOutLog("Aggiornamento immagine bottone eseguito")
      Catch ex As Exception
         EgtOutLog("Eccezione riga 925 di ControlsMachineButtonUC.xaml.vb : " & ex.ToString)
      End Try
   End Sub

#End Region    ' METHODS

End Class
