Imports System.Collections.ObjectModel
Imports EgtWPFLib5
Imports EgtUILib

Public Class CurrentMachine

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

   ' Nome macchina corrente
   Private m_sMachineName As String = String.Empty

   ' Direttorio macchina corrente
   Private m_sMachDir As String = String.Empty

   ' File ini della macchina
   Private m_sMachIniFile As String = String.Empty

   ' Estensione dei file programma pezzo (detti anche file Iso)
   Private m_sIsoFileExt As String = String.Empty

   ' Incremento da dare all'asse C per far coincidere la linea della lama con l'asse X
   Private m_dDeltaC As Double = 0

   ' Ritardo tra reset e invio programma per NUM
   Private m_nResetSendDelay As Integer = 1000

   ' tempo di arresto sospensione dei Thread prima dopo aver lanciato il comando CN
   Private m_nThreadSleep As Integer = 150

   ' Dati su linea di produzione
   Private m_bDemo As Boolean = False
   Public nLastProjSentToViewDEMO As Integer = 0

   Private m_bProdLine As Boolean = False
   Private m_sVarProg1 As String = "E80021"
   Private m_nProg1 As Integer = 901
   Private m_sVarProg2 As String = "E80022"
   Private m_nProg2 As Integer = 902
   Private m_sLineDataDir As String = ""
   Private m_sVarProjFinished As String = "E81025"
   Private m_sVarProjCopy As String = "E80025"
   Private m_bSpecialUnload As Boolean = False
   Private m_nSaveProduced As Integer = 0
   Private m_sVarProgTwins As String = "E80027"
   Private m_bAskNewOnResend As Boolean = False
   Private m_nProdLiSawProbe As Integer = 0
   Private m_sProdLiProbingStateVar As String = ""
   Private m_sProdLiProbingTcPosVar As String = ""
   Private m_sProdLiSawDiameterVar As String = ""
   Private m_sProdLiProbingState2Var As String = ""
   Private m_sProdLiProbingTcPos2Var As String = ""
   Private m_sProdLiSawDiameter2Var As String = ""

   ' Numero e tipo di utensili correntemente disponibili sulla macchina
   Private m_MountedToolConfig As MountedToolConfigs = MountedToolConfigs.SAW

   ' Distanza di sicurezza
   Private m_dSafeZ As Double = 0

   ' Massimo diametro lama per utilizzo ventose movimento pezzi
   Private m_dMaxSawDiamForVac As Double = 630

   ' Massimo diametro lama per tastatura grezzo
   Private m_dMaxSawDiamForProbe As Double = 630

   ' Possibilità di ruotare i grezzi durante separa e muovi
   Private m_bRawSplMovRotate As Boolean = False
   ' Possibilità di muovere i pezzi alla fine delle lavorazioni
   Private m_bRawFinalMove As Boolean = False

   ' Flag che indicano stato tipologia utensili (attivo/non attivo)
   Private m_bSaw As Boolean = False
   Private m_bDrill As Boolean = False
   Private m_bMill As Boolean = False
   Private m_bCupWheel As Boolean = False
   Private m_bPolishingWheel As Boolean = False
   Private m_bWaterJet As Boolean = False

   ' Abilitazione DB WaterJet
   Private m_bFromDBWaterJet As Boolean = False

   ' Flag per visualizzazione TcPos, Testa/uscita e Note utente
   Private m_nShowToolChanger As Integer = 0                 ' 0=no, 1=tutti utensili, 2=solo lame
   Private m_bShowHeadExit As Boolean = False
   Private m_nShowUserNotes As Integer = 0                   ' 0=no, 1=tutti utensili, 2=solo lame
   Private m_nShowSpecials As Integer = 0                    ' 0=no, 1=tutti utensili, 2=solo lame

   ' Flag che indicano presenza tipologia lavorazioni (attivo/non attivo)
   Private m_bSawing As Boolean = False
   Private m_bDrilling As Boolean = False
   Private m_bMilling As Boolean = False
   Private m_bPocketing As Boolean = False
   Private m_bPolishing As Boolean = False
   Private m_bSawRoughing As Boolean = False
   Private m_bSawFinishing As Boolean = False
   Private m_bWaterJetting As Boolean = False
   Private m_bSawingSpecialLeadIn As Boolean = False

   ' Variabili che contengono il nome degli utensili disponibili per tipo
   Private m_sCurrSaw As String = String.Empty
   Private m_sCurrDrill As String = String.Empty
   Private m_sCurrMill As String = String.Empty
   Private m_sCurrMillNoTip As String = String.Empty
   Private m_sCurrDripSaw As String = String.Empty
   Private m_sCurrDripDrill As String = String.Empty
   Private m_sCurrWaterJet As String = String.Empty

   ' Variabili che contengono le lavorazioni correntemente attive (utilizzate per definire lavorazioni nel programma)
   Private m_sCurrSawing As String = String.Empty
   Private m_sCurrDrilling As String = String.Empty
   Private m_sCurrMilling As String = String.Empty
   Private m_sCurrPocketing As String = String.Empty
   Private m_sCurrSawRoughing As String = String.Empty
   Private m_sCurrSawFinishing As String = String.Empty
   Private m_sCurrSawSideFinishing As String = String.Empty
   Private m_sCurrSawSingle As String = String.Empty
   Private m_sCurrDripSawing As String = String.Empty
   Private m_sCurrDripDrilling As String = String.Empty
   Private m_sCurrWaterJetting As String = String.Empty
   Private m_sCurrWaterJettingQuality As String = String.Empty

   ' Spessore sottopezzo
   Private m_dAdditionalTable As Double = 0
   Private m_dTab2AdditionalTable As Double = 0
   Private m_dTab3AdditionalTable As Double = 0
   Private m_bCenterRawOnX As Boolean = False

   ' Offset aggiuntivi per fotografia
   Private m_dPhotoOffsetX As Double = 0
   Private m_dPhotoOffsetY As Double = 0
   Private m_dTab2PhotoOffsetX As Double = 0
   Private m_dTab2PhotoOffsetY As Double = 0

   ' Abilitazione scelta restart
   Private m_bEnableRestart As Boolean = False
   ' Abilitazione scelta pausa a fine lavorazione
   Private m_bEnablePause As Boolean = False

   ' Flag home finale per tagli diretti
   Private m_bDirectCutsFinalHome As Boolean = True
   ' Flag aggiunta spessore lama a movimento manuale 5assi
   Private m_bAddSawThTo5AxMove As Boolean = False

   ' Variazione iniziale/finale della feed dei tagli
   Private m_bFsevEnable As Boolean = False
   Private m_dFsevLength As Double = 0
   Private m_dFsevPerc As Double = 0

   ' Numero portautensili presenti
   Private m_ToolChangerNbr As Integer = 0

   ' Lista portautensili
   Private m_ToolChanger As New List(Of ToolChangerPos)

   ' Numero portautensili manuali
   Private m_ManualToolChangerNbr As Integer = 0

   ' Lista portautensili
   Private m_ManualToolChanger As New List(Of ToolChangerPos)

   ' Lista dei materiali
   Private m_Materials As New ObservableCollection(Of Material)

   ' Lista delle qualità di lavorazioni disponibili nel WaterJet
   Private m_Qualities As New ObservableCollection(Of String)

   ' Massimo id della lista materiali nel file ini
   Private m_MaxIdMat As Integer = 0

   ' Materiale correntemente attivo (dell'ultimo grezzo impostato)
   Private m_CurrMat As Material = Nothing

   ' Indica se la macchina ha il tastatore della lastra
   Private m_HasRawProbe As Boolean = True

   ' Indica se la macchina ha il lettore di codice a barre
   Private m_nEnableBarCodeReader As Integer = 0

   ' Tipi di attrezzaggio
   Friend Enum MountedToolConfigs As Integer
      SAW = 0
      SAWANDAUXTOOL = 1
      MANUALTOOLCHANGER = 2
      TOOLCHANGER = 3
   End Enum

   ' Massimo numero di utensili su Tc
   Friend MAX_TC_TOOLS As Integer = 60

   ' Massimo numero di utensili su Tc manuale
   Friend MAX_TCMAN_TOOLS As Integer = 60


#Region "Proprietà che leggono e scrivono i valori anche da o su file ini"

   Friend ReadOnly Property sMachineName As String
      Get
         Return m_sMachineName
      End Get
   End Property

   Friend ReadOnly Property sMachDir As String
      Get
         Return m_sMachDir
      End Get
   End Property

   Friend ReadOnly Property sMachIniFile As String
      Get
         Return m_sMachIniFile
      End Get
   End Property

   Friend ReadOnly Property sIsoFileExt As String
      Get
         Return m_sIsoFileExt
      End Get
   End Property

   Friend ReadOnly Property dDeltaC As Double
      Get
         Return m_dDeltaC
      End Get
   End Property

   Friend ReadOnly Property nResetSendDelay As Integer
      Get
         Return m_nResetSendDelay
      End Get
   End Property

   Friend ReadOnly Property nThreadSleep As Integer
      Get
         Return m_nThreadSleep
      End Get
   End Property

   Friend ReadOnly Property bDemo As Boolean
      Get
         Return m_bDemo
      End Get
   End Property

   Friend ReadOnly Property bProdLine As Boolean
      Get
         Return m_bProdLine
      End Get
   End Property

   Friend ReadOnly Property sVarProg1 As String
      Get
         Return m_sVarProg1
      End Get
   End Property

   Friend ReadOnly Property nProg1 As Integer
      Get
         Return m_nProg1
      End Get
   End Property

   Friend ReadOnly Property sVarProg2 As String
      Get
         Return m_sVarProg2
      End Get
   End Property

   Friend ReadOnly Property nProg2 As Integer
      Get
         Return m_nProg2
      End Get
   End Property

   Friend ReadOnly Property sLineDataDir As String
      Get
         Return m_sLineDataDir
      End Get
   End Property

   Friend ReadOnly Property sVarProjFinished As String
      Get
         Return m_sVarProjFinished
      End Get
   End Property

   Friend ReadOnly Property sVarProjCopy As String
      Get
         Return m_sVarProjCopy
      End Get
   End Property

   Friend ReadOnly Property bSpecialUnload As Boolean
      Get
         Return m_bSpecialUnload
      End Get
   End Property

   Friend ReadOnly Property nSaveProduced As Integer
      Get
         Return m_nSaveProduced
      End Get
   End Property

   Friend ReadOnly Property sVarProgTwins As String
      Get
         Return m_sVarProgTwins
      End Get
   End Property

   Friend ReadOnly Property bAskNewOnResend As Boolean
      Get
         Return m_bAskNewOnResend
      End Get
   End Property
   

   Friend ReadOnly Property nProdLiSawProbe As Integer
      Get
         Return m_nProdLiSawProbe
      End Get
   End Property

   Friend Function sProdLiProbingStateVar(nI As Integer) As String
      If nI = 1 Then Return m_sProdLiProbingStateVar
      If nI = 2 Then Return m_sProdLiProbingState2Var
      Return Nothing
   End Function

   Friend Function sProdLiProbingTcPosVar(nI As Integer) As String
      If nI = 1 Then Return m_sProdLiProbingTcPosVar
      If nI = 2 Then Return m_sProdLiProbingTcPos2Var
      Return Nothing
   End Function

   Friend Function sProdLiSawDiameterVar(nI As Integer) As String
      If nI = 1 Then Return m_sProdLiSawDiameterVar
      If nI = 2 Then Return m_sProdLiSawDiameter2Var
      Return Nothing
   End Function

   Friend ReadOnly Property sProdLiProbingState2Var As String
      Get
         Return m_sProdLiProbingState2Var
      End Get
   End Property

   Friend ReadOnly Property sProdLiProbingTcPos2Var As String
      Get
         Return m_sProdLiProbingTcPos2Var
      End Get
   End Property

   Friend ReadOnly Property sProdLiSawDiameter2Var As String
      Get
         Return m_sProdLiSawDiameter2Var
      End Get
   End Property

   Friend ReadOnly Property MountedToolConfig As MountedToolConfigs
      Get
         Return m_MountedToolConfig
      End Get
   End Property

   Friend ReadOnly Property dMaxSawDiamForVac As Double
      Get
         Return m_dMaxSawDiamForVac
      End Get
   End Property

   Friend ReadOnly Property dMaxSawDiamForProbe As Double
      Get
         Return m_dMaxSawDiamForProbe
      End Get
   End Property

   Friend ReadOnly Property bRawSplMovRotate As Boolean
      Get
         Return m_bRawSplMovRotate
      End Get
   End Property

   Friend ReadOnly Property bRawFinalMove As Boolean
      Get
         Return m_bRawFinalMove
      End Get
   End Property

   Friend Property dSafeZ As Double
      Get
         If EgtMdbGetGeneralParam(MCH_GP.SAFEZ, m_dSafeZ) Then
            Return m_dSafeZ
         Else
            Return Nothing
         End If
      End Get
      Set(value As Double)
         If EgtMdbSetGeneralParam(MCH_GP.SAFEZ, m_dSafeZ) And
            EgtMdbSave() Then
            m_dSafeZ = value
         End If
      End Set
   End Property

   Friend ReadOnly Property bSaw As Boolean
      Get
         Return m_bSaw
      End Get
   End Property

   Friend ReadOnly Property bDrill As Boolean
      Get
         Return m_bDrill
      End Get
   End Property

   Friend ReadOnly Property bMill As Boolean
      Get
         Return m_bMill
      End Get
   End Property

   Friend ReadOnly Property bCupWheel As Boolean
      Get
         Return m_bCupWheel
      End Get
   End Property

   Friend ReadOnly Property bPolishingWheel As Boolean
      Get
         Return m_bPolishingWheel
      End Get
   End Property

   Friend ReadOnly Property bWaterJet As Boolean
      Get
         Return m_bWaterJet
      End Get
   End Property

   Friend ReadOnly Property bFromDBWaterJet As Boolean
      Get
         Return m_bFromDBWaterJet
      End Get
   End Property

   Friend ReadOnly Property ShowToolChanger As Integer
      Get
         Return m_nShowToolChanger
      End Get
   End Property

   Friend ReadOnly Property bShowHeadExit As Boolean
      Get
         Return m_bShowHeadExit
      End Get
   End Property

   Friend ReadOnly Property ShowUserNotes As Integer
      Get
         Return m_nShowUserNotes
      End Get
   End Property

   Friend ReadOnly Property ShowSpecials As Integer
      Get
         Return m_nShowSpecials
      End Get
   End Property

   Friend ReadOnly Property bSawing As Boolean
      Get
         Return m_bSawing
      End Get
   End Property

   Friend ReadOnly Property bDrilling As Boolean
      Get
         Return m_bDrilling
      End Get
   End Property

   Friend ReadOnly Property bMilling As Boolean
      Get
         Return m_bMilling
      End Get
   End Property

   Friend ReadOnly Property bPocketing As Boolean
      Get
         Return m_bPocketing
      End Get
   End Property

   Friend ReadOnly Property bPolishing As Boolean
      Get
         Return m_bPolishing
      End Get
   End Property

   Friend ReadOnly Property bSawRoughing As Boolean
      Get
         Return m_bSawRoughing
      End Get
   End Property

   Friend ReadOnly Property bSawFinishing As Boolean
      Get
         Return m_bSawFinishing
      End Get
   End Property

   Friend ReadOnly Property bWaterJetting As Boolean
      Get
         Return m_bWaterJetting
      End Get
   End Property

   Friend ReadOnly Property bSawingSpecialLeadIn As Boolean
      Get
         Return m_bSawingSpecialLeadIn
      End Get
   End Property

   Friend Property sCurrSaw As String
      Get
         Return m_sCurrSaw
      End Get
      Set(value As String)
         If WritePrivateProfileString(S_MACH_MACH, K_CURRSAW, value, sMachIniFile) Then
            m_sCurrSaw = value
            m_MainWindow.m_CurrentProjectPageUC.ToolTxBx.Text = value
         End If
      End Set
   End Property

   Friend Property sCurrDrill As String
      Get
         Return m_sCurrDrill
      End Get
      Set(value As String)
         If WritePrivateProfileString(S_MACH_MACH, K_CURRDRILL, value, sMachIniFile) Then
            m_sCurrDrill = value
         End If
      End Set
   End Property

   Friend Property sCurrMill As String
      Get
         Return m_sCurrMill
      End Get
      Set(value As String)
         If WritePrivateProfileString(S_MACH_MACH, K_CURRMILL, value, sMachIniFile) Then
            m_sCurrMill = value
         End If
      End Set
   End Property

   Friend Property sCurrMillNoTip As String
      Get
         Return m_sCurrMillNoTip
      End Get
      Set(value As String)
         If WritePrivateProfileString(S_MACH_MACH, K_CURRMILLNOTIP, value, sMachIniFile) Then
            m_sCurrMillNoTip = value
         End If
      End Set
   End Property

   Friend Property sCurrDripSaw As String
      Get
         Return m_sCurrDripSaw
      End Get
      Set(value As String)
         If WritePrivateProfileString(S_MACH_MACH, K_CURRDRIPSAW, value, sMachIniFile) Then
            m_sCurrDripSaw = value
         End If
      End Set
   End Property

   Friend Property sCurrDripDrill As String
      Get
         Return m_sCurrDripDrill
      End Get
      Set(value As String)
         If WritePrivateProfileString(S_MACH_MACH, K_CURRDRIPDRILL, value, sMachIniFile) Then
            m_sCurrDripDrill = value
         End If
      End Set
   End Property

   Friend Property sCurrWaterJet As String
      Get
         Return m_sCurrWaterJet
      End Get
      Set(value As String)
         If WritePrivateProfileString(S_MACH_MACH, K_CURRWATERJET, value, sMachIniFile) Then
            m_sCurrWaterJet = value
         End If
      End Set
   End Property

   Friend Property sCurrSawing As String
      Get
         Return m_sCurrSawing
      End Get
      Set(value As String)
         If WritePrivateProfileString(S_MACH_MACH, K_CURRSAWING, value, sMachIniFile) Then
            m_sCurrSawing = value
            m_MainWindow.m_CurrentProjectPageUC.MachiningTxBx.Text = value
         End If
      End Set
   End Property

   Friend Property sCurrDrilling As String
      Get
         Return m_sCurrDrilling
      End Get
      Set(value As String)
         If WritePrivateProfileString(S_MACH_MACH, K_CURRDRILLING, value, sMachIniFile) Then
            m_sCurrDrilling = value
         End If
      End Set
   End Property

   Friend Property sCurrMilling As String
      Get
         Return m_sCurrMilling
      End Get
      Set(value As String)
         If WritePrivateProfileString(S_MACH_MACH, K_CURRMILLING, value, sMachIniFile) Then
            m_sCurrMilling = value
         End If
      End Set
   End Property

   Friend Property sCurrPocketing As String
      Get
         Return m_sCurrPocketing
      End Get
      Set(value As String)
         If WritePrivateProfileString(S_MACH_MACH, K_CURRPOCKETING, value, sMachIniFile) Then
            m_sCurrPocketing = value
         End If
      End Set
   End Property

   Friend Property sCurrSawRoughing As String
      Get
         Return m_sCurrSawRoughing
      End Get
      Set(value As String)
         If WritePrivateProfileString(S_MACH_MACH, K_CURRSAWROUGHING, value, sMachIniFile) Then
            m_sCurrSawRoughing = value
         End If
      End Set
   End Property

   Friend Property sCurrSawFinishing As String
      Get
         Return m_sCurrSawFinishing
      End Get
      Set(value As String)
         If WritePrivateProfileString(S_MACH_MACH, K_CURRSAWFINISHING, value, sMachIniFile) Then
            m_sCurrSawFinishing = value
         End If
      End Set
   End Property

   Friend Property sCurrSawSideFinishing As String
      Get
         Return m_sCurrSawSideFinishing
      End Get
      Set(value As String)
         If WritePrivateProfileString(S_MACH_MACH, K_CURRSAWSIDEFINISHING, value, sMachIniFile) Then
            m_sCurrSawSideFinishing = value
         End If
      End Set
   End Property

   Friend Property sCurrSawSingle As String
      Get
         Return m_sCurrSawSingle
      End Get
      Set(value As String)
         If WritePrivateProfileString(S_MACH_MACH, K_CURRSAWSINGLE, value, sMachIniFile) Then
            m_sCurrSawSingle = value
         End If
      End Set
   End Property

   Friend Property sCurrDripSawing As String
      Get
         Return m_sCurrDripSawing
      End Get
      Set(value As String)
         If WritePrivateProfileString(S_MACH_MACH, K_CURRDRIPSAWING, value, sMachIniFile) Then
            m_sCurrDripSawing = value
         End If
      End Set
   End Property

   Friend Property sCurrDripDrilling As String
      Get
         Return m_sCurrDripDrilling
      End Get
      Set(value As String)
         If WritePrivateProfileString(S_MACH_MACH, K_CURRDRIPDRILLING, value, sMachIniFile) Then
            m_sCurrDripDrilling = value
         End If
      End Set
   End Property

   Friend Property sCurrWaterJetting As String
      Get
         Return m_sCurrWaterJetting
      End Get
      Set(value As String)
         If WritePrivateProfileString(S_MACH_MACH, K_CURRWATERJETTING, value, sMachIniFile) Then
            m_sCurrWaterJetting = value
         End If
      End Set
   End Property

   Friend Property sCurrWaterJettingQuality As String
      Get
         Return m_sCurrWaterJettingQuality
      End Get
      Set(value As String)
         If WritePrivateProfileString(S_MACH_MACH, K_CURRWATERJETTINGQUALITY, value, sMachIniFile) Then
            m_sCurrWaterJettingQuality = value
         End If
      End Set
   End Property

   Friend ReadOnly Property WaterJettingActive As Double
      Get
         Return m_bWaterJetting AndAlso Not String.IsNullOrWhiteSpace(m_sCurrWaterJetting)
      End Get
   End Property

   Friend Property dAdditionalTable As Double
      Get
         Select Case GetCurrentTable()
            Case 3
               Return m_dTab3AdditionalTable
            Case 2
               Return m_dTab2AdditionalTable
            Case Else
               Return m_dAdditionalTable
         End Select
      End Get
      Set(value As Double)
         Select Case GetCurrentTable()
            Case 3
               If Math.Abs(value - m_dTab3AdditionalTable) > EPS_SMALL And
               WritePrivateProfileString(S_TABLE, K_TAB3_ADDITIONALTABLE, DoubleToString(value, 3), sMachIniFile) Then
                  ' Aggiorno il valore corrente
                  m_dTab3AdditionalTable = value
                  ' Aggiorno il progetto corrente
                  m_MainWindow.m_CurrentProjectPageUC.AdjustAdditionalTable()
               End If
            Case 2
               If Math.Abs(value - m_dTab2AdditionalTable) > EPS_SMALL And
               WritePrivateProfileString(S_TABLE, K_TAB2_ADDITIONALTABLE, DoubleToString(value, 3), sMachIniFile) Then
                  ' Aggiorno il valore corrente
                  m_dTab2AdditionalTable = value
                  ' Aggiorno il progetto corrente
                  m_MainWindow.m_CurrentProjectPageUC.AdjustAdditionalTable()
               End If
            Case Else
               If Math.Abs(value - m_dAdditionalTable) > EPS_SMALL And
               WritePrivateProfileString(S_TABLE, K_ADDITIONALTABLE, DoubleToString(value, 3), sMachIniFile) Then
                  ' Aggiorno il valore corrente
                  m_dAdditionalTable = value
                  ' Aggiorno il progetto corrente
                  m_MainWindow.m_CurrentProjectPageUC.AdjustAdditionalTable()
               End If
         End Select
      End Set
   End Property

   Friend ReadOnly Property CenterRawOnX As Boolean
      Get
         Return m_bCenterRawOnX
      End Get
   End Property

   Friend ReadOnly Property PhotoOffset As Vector3d
      Get
         Select Case GetCurrentTable()
            Case 2
               Return New Vector3d(m_dTab2PhotoOffsetX, m_dTab2PhotoOffsetY, 0)
            Case Else
               Return New Vector3d(m_dPhotoOffsetX, m_dPhotoOffsetY, 0)
         End Select
      End Get
   End Property

   Friend ReadOnly Property bEnableRestart As Boolean
      Get
         Return m_bEnableRestart
      End Get
   End Property

   Friend ReadOnly Property bEnablePause As Boolean
      Get
         Return m_bEnablePause
      End Get
   End Property

   Friend Property bDirectCutsFinalHome As Boolean
      Get
         Return m_bDirectCutsFinalHome
      End Get
      Set(value As Boolean)
         If WritePrivateProfileString(S_MACH_DIRECTCUTS, K_FINALHOME, If(value, "1", "0"), sMachIniFile) Then
            m_bDirectCutsFinalHome = value
         End If
      End Set
   End Property

   Friend Property bAddSawThTo5AxMove As Boolean
      Get
         Return m_bAddSawThTo5AxMove
      End Get
      Set(value As Boolean)
         If WritePrivateProfileString(S_MACH_DIRECTCUTS, K_ADDSAWTHTO5AXMOVE, If(value, "1", "0"), sMachIniFile) Then
            m_bAddSawThTo5AxMove = value
         End If
      End Set
   End Property

   Friend ReadOnly Property bFsevEnable As Boolean
      Get
         Return m_bFsevEnable
      End Get
   End Property

   Friend Property dFsevLength As Double
      Get
         Return m_dFsevLength
      End Get
      Set(value As Double)
         If WritePrivateProfileString(S_NEST, K_MACH_CUTFSEVLEN, DoubleToString(value, 3), sMachIniFile) Then
            m_dFsevLength = value
         End If
      End Set
   End Property

   Friend Property dFsevPerc As Double
      Get
         Return m_dFsevPerc
      End Get
      Set(value As Double)
         If WritePrivateProfileString(S_NEST, K_MACH_CUTFSEVPERC, DoubleToString(value, 3), sMachIniFile) Then
            m_dFsevPerc = value
         End If
      End Set
   End Property

   Friend ReadOnly Property ToolChangerNbr As Integer
      Get
         Return m_ToolChangerNbr
      End Get
   End Property

   Friend ReadOnly Property ToolChanger As List(Of ToolChangerPos)
      Get
         Return m_ToolChanger
      End Get
   End Property

   Friend ReadOnly Property ToolChangerName(Index As Integer) As String
      Get
         If Index >= 0 And Index < m_ToolChanger.Count Then
            Return m_ToolChanger(Index).sName
         Else
            Return String.Empty
         End If
      End Get
   End Property

   Friend WriteOnly Property ToolPos(Index As Integer) As String
      Set(value As String)
         If value <> m_ToolChanger(Index).sTool Then
            m_ToolChanger(Index).sTool = value
            WritePrivateProfileString(S_TOOLCHANGER, K_POS & Index + 1, value, sMachIniFile)
         End If
      End Set
   End Property

   Friend ReadOnly Property ManualToolChangerNbr As Integer
      Get
         Return m_ManualToolChangerNbr
      End Get
   End Property

   Friend ReadOnly Property ManualToolChanger As List(Of ToolChangerPos)
      Get
         Return m_ManualToolChanger
      End Get
   End Property

   Friend ReadOnly Property ManualToolChangerName(Index As Integer) As String
      Get
         If Index >= 0 And Index < m_ManualToolChanger.Count Then
            Return m_ManualToolChanger(Index).sName
         Else
            Return String.Empty
         End If
      End Get
   End Property

   Friend WriteOnly Property ManualToolPos(Index As Integer) As String
      Set(value As String)
         If value <> m_ManualToolChanger(Index).sTool Then
            m_ManualToolChanger(Index).sTool = value
            WritePrivateProfileString(S_TOOLCHANGER, K_MANUALPOS & Index + 1, value, sMachIniFile)
         End If
      End Set
   End Property

   Friend ReadOnly Property Materials As ObservableCollection(Of Material)
      Get
         Return m_Materials
      End Get
   End Property

   Friend ReadOnly Property Qualities As ObservableCollection(Of String)
      Get
         Return m_Qualities
      End Get
   End Property

   Friend Sub AddMaterial(value As Material)
      For Each Material As Material In Materials
         If Material.nId = value.nId Then
            Exit Sub
         End If
      Next
      WritePrivateProfileMaterial(S_MATERIALS, K_MATERIAL & m_MaxIdMat + 1, value, sMachIniFile)
      m_MaxIdMat += 1
      Materials.Add(value)
   End Sub

   Friend Sub RemoveMaterial(value As Material)
      ' Se è il materiale corrente lo tolgo
      If Not IsNothing(CurrMat) AndAlso CurrMat.nId = value.nId Then
         CurrMat = Nothing
      End If
      ' Rimuovo il materiale dall'elenco
      Dim Index As Integer
      For Index = 0 To Materials.Count - 1
         If Materials(Index).nId = value.nId Then
            m_Materials.Remove(Materials(Index))
            Exit For
         End If
      Next
      Dim TempMat As Material = Nothing
      For I As Integer = Index To m_MaxIdMat - 1
         GetPrivateProfileMaterial(S_MATERIALS, K_MATERIAL & I + 2, TempMat, sMachIniFile)
         WritePrivateProfileMaterial(S_MATERIALS, K_MATERIAL & I + 1, TempMat, sMachIniFile)
      Next
      WritePrivateProfileMaterial(S_MATERIALS, K_MATERIAL & m_MaxIdMat, Nothing, sMachIniFile)
      m_MaxIdMat -= 1
      ' Rimuovo il materiale dall'elenco di tutte le lavorazioni
      Dim MachiningName As String = String.Empty
      Dim bFound As Boolean = EgtMdbGetFirstMachining(MCH_MY.SAWING, MachiningName)
      While bFound
         EgtMdbSetCurrMachining(MachiningName)
         SearchAndRemoveMat(value.nId)
         bFound = EgtMdbGetNextMachining(MCH_MY.SAWING, MachiningName)
      End While
      bFound = EgtMdbGetFirstMachining(MCH_MY.DRILLING, MachiningName)
      While bFound
         EgtMdbSetCurrMachining(MachiningName)
         SearchAndRemoveMat(value.nId)
         bFound = EgtMdbGetNextMachining(MCH_MY.DRILLING, MachiningName)
      End While
      bFound = EgtMdbGetFirstMachining(MCH_MY.MILLING, MachiningName)
      While bFound
         EgtMdbSetCurrMachining(MachiningName)
         SearchAndRemoveMat(value.nId)
         bFound = EgtMdbGetNextMachining(MCH_MY.DRILLING, MachiningName)
      End While

   End Sub

   ' Funzione che dato l'Id di un materiale lo rimuove dalla lavorazione corrente
   Friend Sub SearchAndRemoveMat(valueId As Integer)
      Dim SysNotes As String = String.Empty
      EgtMdbGetCurrMachiningParam(MCH_MP.SYSNOTES, SysNotes)
      If SysNotes <> String.Empty Then
         Dim MachiningMaterials() = SysNotes.Split(";".ToCharArray)
         SysNotes = String.Empty
         For Each Material As Object In MachiningMaterials
            Dim Param() As String = Material.Split(",".ToCharArray)
            If Param(0) <> valueId.ToString Then
               SysNotes &= Material & ";"
            End If
         Next
         If SysNotes(SysNotes.Length - 1) = ";" Then
            SysNotes = SysNotes.Substring(0, SysNotes.Length - 1)
         End If
         EgtMdbSetCurrMachiningParam(MCH_MP.SYSNOTES, SysNotes)
      End If
      EgtMdbSaveCurrMachining()
   End Sub

   Friend Property CurrMat As Material
      Get
         Return m_CurrMat
      End Get
      Set(value As Material)
         Dim CurrMatId As String = If(Not IsNothing(value), value.nId.ToString, "")
         If bWaterJet And bFromDBWaterJet Then
            WritePrivateProfileString(S_MATERIALS, K_CURRMATERIAL, CurrMatId & If(Not IsNothing(value), "." & value.SubId, ""), sMachIniFile)
         Else
            WritePrivateProfileString(S_MATERIALS, K_CURRMATERIAL, CurrMatId, sMachIniFile)
         End If
         m_CurrMat = value
      End Set
   End Property

   Public Function SetCurrMatByName(sMatName As String) As Boolean
      ' Se stringa vuota
      If String.IsNullOrWhiteSpace(sMatName) Then Return False
      ' Cerco il nome nella lista dei materiali
      For Index As Integer = 0 To Materials.Count - 1
         If String.Compare(Materials(Index).sName, sMatName, True) = 0 Then
            CurrMat = Materials(Index)
         End If
      Next
      Return False
   End Function

   Friend ReadOnly Property bHasRawProbe As Boolean
      Get
         Return m_HasRawProbe
      End Get
   End Property

   Friend ReadOnly Property bHasRawBarCodeReader As Boolean
      Get
         Return (m_nEnableBarCodeReader = 1)
      End Get
   End Property

   Friend ReadOnly Property bHasOpenBarCodeReader As Boolean
      Get
         Return (m_nEnableBarCodeReader = 2)
      End Get
   End Property

#End Region

   Sub New()
      ' Leggo da file ini nome macchina corrente
      GetPrivateProfileString(S_MACH, K_CURRMACH, "", m_sMachineName, m_MainWindow.GetIniFile())
      ' Impostazione direttorio macchina
      m_sMachDir = m_MainWindow.GetMachinesRootDir & "\" & sMachineName
      ' Impostazione path MachIni file
      m_sMachIniFile = m_MainWindow.GetMachinesRootDir & "\" & sMachineName & "\" & sMachineName & ".ini"
      ' Leggo estensione del file programma pezzo
      GetPrivateProfileString(S_PARTPROGRAM, K_EXTENSION, "xpi", m_sIsoFileExt, sMachIniFile)
      If m_sIsoFileExt(0) <> "." Then m_sIsoFileExt = "." & m_sIsoFileExt
      ' Leggo flag incremento asse C
      m_dDeltaC = GetPrivateProfileDouble(S_AXES, K_DELTA_C, 0.0, sMachIniFile)
      ' Leggo ritardo tra reset e send per NUM
      m_nResetSendDelay = GetPrivateProfileInt(S_NCNUM, K_RESETSENDDELAY, 1000, sMachIniFile)
      ' Leggo ritardo tra reset e send per NUM
      m_nThreadSleep = GetPrivateProfileInt(S_NCNUM, K_THREADSLEEP, 150, sMachIniFile)
      ' Leggo se linea di produzione e parametri relativi
      m_bProdLine = (GetPrivateProfileInt(S_PRODUCTIONLINE, K_ACTIVE, 0, sMachIniFile) <> 0)
      If m_bProdLine And Not m_MainWindow.GetKeyOption(MainWindow.KEY_OPT.PRODUCTION_LINE) Then
         m_bProdLine = False
         EgtOutLog("Error - Production line requested but not key enabled")
      End If
      m_bDemo = (GetPrivateProfileInt(S_PRODUCTIONLINE, "Demo", 0, sMachIniFile) <> 0)
      GetPrivateProfileString(S_PRODUCTIONLINE, K_VARPROG1, "", m_sVarProg1, sMachIniFile)
      m_nProg1 = GetPrivateProfileInt(S_PRODUCTIONLINE, K_NAMEPROG1, 0, m_MainWindow.GetMachIniFile())
      GetPrivateProfileString(S_PRODUCTIONLINE, K_VARPROG2, "", m_sVarProg2, sMachIniFile)
      m_nProg2 = GetPrivateProfileInt(S_PRODUCTIONLINE, K_NAMEPROG2, 0, m_MainWindow.GetMachIniFile())
      GetPrivateProfileString(S_PRODUCTIONLINE, K_LINEDATADIR, "", m_sLineDataDir, sMachIniFile)
      GetPrivateProfileString(S_PRODUCTIONLINE, K_VARPROJFINISHED, "", m_sVarProjFinished, sMachIniFile)
      GetPrivateProfileString(S_PRODUCTIONLINE, K_VARPROJCOPY, "", m_sVarProjCopy, sMachIniFile)
      m_bSpecialUnload = (GetPrivateProfileInt(S_PRODUCTIONLINE, K_SPECIALUNLOAD, 0, sMachIniFile) <> 0)
      m_nSaveProduced = GetPrivateProfileInt(S_PRODUCTIONLINE, K_SAVEPRODUCED, 0, sMachIniFile)
      GetPrivateProfileString(S_PRODUCTIONLINE, K_VARPROGTWINS, "", m_sVarProgTwins, sMachIniFile)
      m_bAskNewOnResend = (GetPrivateProfileInt(S_PRODUCTIONLINE, K_ASKNEWONRESEND, 0, sMachIniFile) <> 0)
      m_nProdLiSawProbe = GetPrivateProfileInt(S_PRODUCTIONLINE, K_PRODLIENABLESAWPROBE, 0, sMachIniFile)
      GetPrivateProfileString(S_PRODUCTIONLINE, K_PRODLIPROBINGSTATEVAR, "", m_sProdLiProbingStateVar, sMachIniFile)
      GetPrivateProfileString(S_PRODUCTIONLINE, K_PRODLIPROBINGTCPOSVAR, "0", m_sProdLiProbingTcPosVar, sMachIniFile)
      GetPrivateProfileString(S_PRODUCTIONLINE, K_PRODLISAWDIAMETERVAR, "", m_sProdLiSawDiameterVar, sMachIniFile)
      GetPrivateProfileString(S_PRODUCTIONLINE, K_PRODLIPROBINGSTATE2VAR, "", m_sProdLiProbingState2Var, sMachIniFile)
      GetPrivateProfileString(S_PRODUCTIONLINE, K_PRODLIPROBINGTCPOS2VAR, "0", m_sProdLiProbingTcPos2Var, sMachIniFile)
      GetPrivateProfileString(S_PRODUCTIONLINE, K_PRODLISAWDIAMETER2VAR, "", m_sProdLiSawDiameter2Var, sMachIniFile)

      ' Leggo configurazione degli utensili in macchina
      m_MountedToolConfig = GetPrivateProfileInt(S_TOOLS, K_MOUNTEDTOOLCONFIG, 0, sMachIniFile)
      ' Leggo flag presenza tipologie utensili
      ' lama
      m_bSaw = (GetPrivateProfileInt(S_TOOLS, K_SAWBLADE, 0, sMachIniFile) > 0)
      ' foretto
      m_bDrill = (GetPrivateProfileInt(S_TOOLS, K_DRILLBIT, 0, sMachIniFile) > 0)
      ' fresa
      m_bMill = (GetPrivateProfileInt(S_TOOLS, K_MILL, 0, sMachIniFile) > 0) And
                m_MainWindow.GetKeyOption(MainWindow.KEY_OPT.ENABLE_MILL)
      ' mola da scasso
      m_bCupWheel = (GetPrivateProfileInt(S_TOOLS, K_CUPWHEEL, 0, sMachIniFile) > 0) And
                    m_MainWindow.GetKeyOption(MainWindow.KEY_OPT.ENABLE_MILL)
      ' lucidante
      m_bPolishingWheel = (GetPrivateProfileInt(S_TOOLS, K_POLISHINGWHEEL, 0, sMachIniFile) > 0) And
                          m_MainWindow.GetKeyOption(MainWindow.KEY_OPT.ENABLE_POLISHING)
      ' waterjet
      m_bWaterJet = (GetPrivateProfileInt(S_TOOLS, K_WATERJET, 0, sMachIniFile) > 0) And
                    m_MainWindow.GetKeyOption(MainWindow.KEY_OPT.ENABLE_WJ)
      ' Leggo abilitazione DB WaterJet
      m_bFromDBWaterJet = (GetPrivateProfileInt(S_MATERIALS, K_FROMDBWATERJET, 0, sMachIniFile) > 0)
      ' Leggo abilitazione visualizzazione TcPos, Head/exit e Note utente
      m_nShowToolChanger = GetPrivateProfileInt(S_TOOLS, K_SHOWTOOLCHANGER, 0, sMachIniFile)
      m_bShowHeadExit = (GetPrivateProfileInt(S_TOOLS, K_SHOWHEADEXIT, 0, sMachIniFile) > 0)
      m_nShowUserNotes = GetPrivateProfileInt(S_TOOLS, K_SHOWUSERNOTES, 0, sMachIniFile)
      m_nShowSpecials = GetPrivateProfileInt(S_TOOLS, K_SHOWSPECIALS, 0, sMachIniFile)
      ' Leggo limiti diametro lama per altre operazioni
      m_dMaxSawDiamForVac = GetPrivateProfileDouble(S_TOOLS, K_MAXSAWDIAMFORVAC, 630, sMachIniFile)
      m_dMaxSawDiamForProbe = GetPrivateProfileDouble(S_TOOLS, K_MAXSAWDIAMFORPROBE, 630, sMachIniFile)
      ' Leggo flag abilitazione rotazione grezzi durante spezza e muovi
      m_bRawSplMovRotate = (GetPrivateProfileInt(S_MACH_RAWMOVE, K_MACH_RM_ROTATE, 0, sMachIniFile) <> 0)
      ' Leggo flag abilitazione spostamento pezzi alla fine delle lavorazioni
      m_bRawFinalMove = (GetPrivateProfileInt(S_MACH_RAWMOVE, K_MACH_RM_FINALMOVE, 0, sMachIniFile) <> 0)
      ' Leggo flag presenza tipologie lavorazioni
      ' taglio con lama
      m_bSawing = (GetPrivateProfileInt(S_MACHININGS, K_SAWING, 0, sMachIniFile) > 0)
      ' foratura
      m_bDrilling = (GetPrivateProfileInt(S_MACHININGS, K_DRILLING, 0, sMachIniFile) > 0)
      ' fresatura
      m_bMilling = (GetPrivateProfileInt(S_MACHININGS, K_MILLING, 0, sMachIniFile) > 0) And
                   m_MainWindow.GetKeyOption(MainWindow.KEY_OPT.ENABLE_MILL)
      ' svuotatura
      m_bPocketing = (GetPrivateProfileInt(S_MACHININGS, K_POCKETING, 0, sMachIniFile) > 0) And
                     m_MainWindow.GetKeyOption(MainWindow.KEY_OPT.ENABLE_MILL)
      ' lucidatura
      m_bPolishing = (GetPrivateProfileInt(S_MACHININGS, K_POLISHING, 0, sMachIniFile) > 0) And
                     m_MainWindow.GetKeyOption(MainWindow.KEY_OPT.ENABLE_POLISHING)
      ' sgrossatura con lama
      m_bSawRoughing = (GetPrivateProfileInt(S_MACHININGS, K_SAWROUGHING, 0, sMachIniFile) > 0)
      ' finitura con lama
      m_bSawFinishing = (GetPrivateProfileInt(S_MACHININGS, K_SAWFINISHING, 0, sMachIniFile) > 0)
      ' taglio waterjet
      m_bWaterJetting = (GetPrivateProfileInt(S_MACHININGS, K_WATERJETTING, 0, sMachIniFile) > 0) And
                        m_MainWindow.GetKeyOption(MainWindow.KEY_OPT.ENABLE_WJ)
      ' possibilità di definire attacco speciale per materiali ceramici
      m_bSawingSpecialLeadIn = (GetPrivateProfileInt(S_MACHININGS, K_SAWINGSPECIALLEADIN, 0, sMachIniFile) > 0)

      ' Leggo utensili correnti
      ' lama
      GetPrivateProfileString(S_MACH_MACH, K_CURRSAW, Nothing, m_sCurrSaw, sMachIniFile)
      ' foretto
      GetPrivateProfileString(S_MACH_MACH, K_CURRDRILL, Nothing, m_sCurrDrill, sMachIniFile)
      ' fresa
      GetPrivateProfileString(S_MACH_MACH, K_CURRMILL, Nothing, m_sCurrMill, sMachIniFile)
      ' mola
      GetPrivateProfileString(S_MACH_MACH, K_CURRMILLNOTIP, Nothing, m_sCurrMillNoTip, sMachIniFile)
      ' lama da sotto
      GetPrivateProfileString(S_MACH_MACH, K_CURRDRIPSAW, Nothing, m_sCurrDripSaw, sMachIniFile)
      ' foretto da sotto
      GetPrivateProfileString(S_MACH_MACH, K_CURRDRIPDRILL, Nothing, m_sCurrDripDrill, sMachIniFile)
      ' waterjet
      GetPrivateProfileString(S_MACH_MACH, K_CURRWATERJET, Nothing, m_sCurrWaterJet, sMachIniFile)

      ' Leggo lavorazioni correnti
      ' lama
      GetPrivateProfileString(S_MACH_MACH, K_CURRSAWING, Nothing, m_sCurrSawing, sMachIniFile)
      ' foretto
      GetPrivateProfileString(S_MACH_MACH, K_CURRDRILLING, Nothing, m_sCurrDrilling, sMachIniFile)
      ' fresa
      GetPrivateProfileString(S_MACH_MACH, K_CURRMILLING, Nothing, m_sCurrMilling, sMachIniFile)
      ' svuotatura
      GetPrivateProfileString(S_MACH_MACH, K_CURRPOCKETING, Nothing, m_sCurrPocketing, sMachIniFile)
      ' sgrossatura con lama
      GetPrivateProfileString(S_MACH_MACH, K_CURRSAWROUGHING, Nothing, m_sCurrSawRoughing, sMachIniFile)
      ' finitura con lama
      GetPrivateProfileString(S_MACH_MACH, K_CURRSAWFINISHING, Nothing, m_sCurrSawFinishing, sMachIniFile)
      ' spatolatura con lama
      GetPrivateProfileString(S_MACH_MACH, K_CURRSAWSIDEFINISHING, Nothing, m_sCurrSawSideFinishing, sMachIniFile)
      ' taglio singolo
      GetPrivateProfileString(S_MACH_MACH, K_CURRSAWSINGLE, Nothing, m_sCurrSawSingle, sMachIniFile)
      ' lama da sotto
      GetPrivateProfileString(S_MACH_MACH, K_CURRDRIPSAWING, Nothing, m_sCurrDripSawing, sMachIniFile)
      ' foretto da sotto
      GetPrivateProfileString(S_MACH_MACH, K_CURRDRIPDRILLING, Nothing, m_sCurrDripDrilling, sMachIniFile)
      ' waterjetting
      GetPrivateProfileString(S_MACH_MACH, K_CURRWATERJETTING, Nothing, m_sCurrWaterJetting, sMachIniFile)
      ' waterjettingquality
      GetPrivateProfileString(S_MACH_MACH, K_CURRWATERJETTINGQUALITY, Nothing, m_sCurrWaterJettingQuality, sMachIniFile)

      ' Leggo numero di portautensili
      m_ToolChangerNbr = GetPrivateProfileInt(S_TOOLCHANGER, K_NUMBER, 0, sMachIniFile)
      m_ToolChangerNbr = Math.Min(m_ToolChangerNbr, MAX_TC_TOOLS)

      ' Leggo da file ini nomi e utensili presenti in portautensili
      For Index As Integer = 1 To m_ToolChangerNbr
         Dim sName As String = String.Empty
         Dim sTool As String = String.Empty
         GetPrivateProfileString(S_TOOLCHANGER, K_NAME & Index, Nothing, sName, sMachIniFile)
         GetPrivateProfileString(S_TOOLCHANGER, K_POS & Index, Nothing, sTool, sMachIniFile)
         m_ToolChanger.Add(New ToolChangerPos(Index, sName, sTool))
      Next

      ' Leggo numero portautensili manuali
      m_ManualToolChangerNbr = GetPrivateProfileInt(S_TOOLCHANGER, K_MANUALNUMBER, 0, sMachIniFile)
      m_ManualToolChangerNbr = Math.Min(m_ManualToolChangerNbr, MAX_TCMAN_TOOLS)

      ' Leggo da file ini nomi e utensili manuali presenti
      For Index As Integer = 1 To m_ManualToolChangerNbr
         Dim sName As String = String.Empty
         Dim sTool As String = String.Empty
         GetPrivateProfileString(S_TOOLCHANGER, K_MANUALNAME & Index, Nothing, sName, sMachIniFile)
         GetPrivateProfileString(S_TOOLCHANGER, K_MANUALPOS & Index, Nothing, sTool, sMachIniFile)
         m_ManualToolChanger.Add(New ToolChangerPos(Index, sName, sTool))
      Next

      ' Leggo altezza sopratavola
      m_dAdditionalTable = GetPrivateProfileDouble(S_TABLE, K_ADDITIONALTABLE, 0, sMachIniFile)
      m_dTab2AdditionalTable = GetPrivateProfileDouble(S_TABLE, K_TAB2_ADDITIONALTABLE, 0, sMachIniFile)
      m_dTab3AdditionalTable = GetPrivateProfileDouble(S_TABLE, K_TAB3_ADDITIONALTABLE, 0, sMachIniFile)

      ' Leggo centratura del pezzo in X sulla tavola (per macchine con nastro)
      m_bCenterRawOnX = (GetPrivateProfileInt(S_TABLE, K_CENTER_RAW_ONX, 0, sMachIniFile) <> 0)

      ' Leggo offset aggiuntivo a fotografia
      m_dPhotoOffsetX = GetPrivateProfileDouble(S_PHOTO, K_PHOTO_OFFSETX, 0, sMachIniFile)
      m_dPhotoOffsetY = GetPrivateProfileDouble(S_PHOTO, K_PHOTO_OFFSETY, 0, sMachIniFile)
      m_dTab2PhotoOffsetX = GetPrivateProfileDouble(S_PHOTO, K_PHOTO_TAB2_OFFSETX, 0, sMachIniFile)
      m_dTab2PhotoOffsetY = GetPrivateProfileDouble(S_PHOTO, K_PHOTO_TAB2_OFFSETY, 0, sMachIniFile)

      ' Leggo abilitazione per restart
      m_bEnableRestart = (GetPrivateProfileInt(S_MACH_NEST, K_MACH_ENABLERESTART, 0, sMachIniFile) <> 0)
      ' Leggo abilitazione scelta pausa a fine lavorazione
      m_bEnablePause = (GetPrivateProfileInt(S_MACH_NEST, K_MACH_ENABLEPAUSE, 0, sMachIniFile) <> 0)

      ' Leggo flag per posizione home alla fine dei tagli diretti
      m_bDirectCutsFinalHome = (GetPrivateProfileInt(S_MACH_DIRECTCUTS, K_FINALHOME, 1, sMachIniFile) <> 0)
      ' Leggo flag per aggiunta spessore lama a movimento manuale 5assi
      m_bAddSawThTo5AxMove = (GetPrivateProfileInt(S_MACH_DIRECTCUTS, K_ADDSAWTHTO5AXMOVE, 0, sMachIniFile) <> 0)

      ' Leggo dati per feed ridotta all'inizio/fine dei tagli
      m_bFsevEnable = (GetPrivateProfileInt(S_NEST, K_MACH_CUTFSEVENABLE, 0, sMachIniFile) <> 0)
      m_dFsevLength = GetPrivateProfileDouble(S_NEST, K_MACH_CUTFSEVLEN, 0, sMachIniFile)
      m_dFsevPerc = GetPrivateProfileDouble(S_NEST, K_MACH_CUTFSEVPERC, 0, sMachIniFile)

      ' Creo materiale di default e lo aggiungo alla lista
      ' m_Materials.Add(New Material(0, "Generico"))
      ' Leggo la lista dei materiali
      Dim Material As Material = Nothing
      Dim nIndex As Integer = 1

      If m_bWaterJet And m_bFromDBWaterJet Then
         LoadWJMaterial(True)
      Else
         While (GetPrivateProfileMaterial(S_MATERIALS, K_MATERIAL & nIndex, Material, sMachIniFile))
            m_Materials.Add(Material)
            nIndex += 1
         End While
      End If

      ' Salvo massimo indice a cui sono arrivato per usarlo quando devo aggiungere nuovi elementi alla lista
      m_MaxIdMat = nIndex - 1

      ' Leggo materiale correntemente attivo
      Dim sCurrMatId As String = String.Empty
      EgtUILib.GetPrivateProfileString(S_MATERIALS, K_CURRMATERIAL, "0", sCurrMatId, sMachIniFile)
      If bWaterJet And bFromDBWaterJet Then
         Dim sCurrMatIds As String()
         sCurrMatIds = sCurrMatId.Split("."c)
         If sCurrMatIds.Length > 1 Then
            Dim nCurrMatId As Integer
            Dim nCurrSubMatId As Integer
            If Not Integer.TryParse(sCurrMatIds(0), nCurrMatId) Then nCurrMatId = 0
            If Not Integer.TryParse(sCurrMatIds(1), nCurrSubMatId) Then nCurrSubMatId = 0
            For Each Material In Materials
               If Material.nId = nCurrMatId AndAlso Material.SubId = nCurrSubMatId Then
                  m_CurrMat = Material
                  Exit For
               End If
            Next
         End If
      Else
         Dim nCurrMatId As Integer
         If Not Integer.TryParse(sCurrMatId, nCurrMatId) Then nCurrMatId = 0
         For Each Material In Materials
            If Material.nId = nCurrMatId Then
               m_CurrMat = Material
               Exit For
            End If
         Next
      End If
      ' leggo se la macchina ha il tastatore dello spessore tavola
      m_HasRawProbe = (GetPrivateProfileInt(S_MACH_PROBING, K_ENABLERAWPROBE, 1, sMachIniFile) <> 0)
      ' leggo se la macchina ha il lettore di codice a barre della lastra
      m_nEnableBarCodeReader = GetPrivateProfileInt(S_MACH_BARCODEREADER, K_ENABLEBCR, 0, sMachIniFile)
   End Sub

   Public Sub LoadWJMaterial(Optional bIsStart As Boolean = False)
      Dim TempCurrMat As Material = CurrMat
      ' Svuoto l'attuale lista di materiali
      m_Materials.Clear()
      ' Leggo valori da file Data e li carico nelle proprietà
      Dim sFilePath As String = sMachDir & "\" &  MACHININGS_DIR & "\" & WATERJETDB_FILE
      Dim Local_MaterialList = New ObservableCollection(Of EgtWPFLib5.WjMaterial)
      Local_MaterialList = EgtWPFLib5.WaterjetDbWindowVM.LoadWjMaterials(sFilePath)
      ' definisco la lista delle qualità (solo se è vuota)
      If m_Qualities.Count < 1 Then
         m_Qualities.Add("Q1")
         m_Qualities.Add("Q2")
         m_Qualities.Add("Q3")
         m_Qualities.Add("Q4")
         m_Qualities.Add("Q5")
         m_Qualities.Add("QExtra")
      End If
      ' inserisco il materiale generico di default -- IN SOSPESO
      'm_Materials.Add(New Material(0, "***", 0))
      ' costruiscoi la lista 
      Dim i As Integer = 0
      Dim sName As String = String.Empty
      For i = 0 To Local_MaterialList.Count - 1
         sName = Local_MaterialList(i).Name
         Dim j As Integer = 0
         For j = 0 To Local_MaterialList(i).SubMaterialList.Count - 1
            m_Materials.Add(New Material(i + 1, sName & "." & Local_MaterialList(i).SubMaterialList(j).Name, j + 1))
         Next
      Next
      If Not bIsStart Then CurrMat = TempCurrMat
   End Sub

   Public Function GetMaxThicknessCurrMaterial(sCurMat As String) As Double
      Dim bFound As Boolean = False
      Dim dMaxThick As Double = 0
      ' Leggo valori da file Data e li carico nelle proprietà
      Dim sFilePath As String = sMachDir & "\" & MACHININGS_DIR & "\" & WATERJETDB_FILE
      Dim Local_MaterialList = New ObservableCollection(Of EgtWPFLib5.WjMaterial)
      Local_MaterialList = EgtWPFLib5.WaterjetDbWindowVM.LoadWjMaterials(sFilePath)
      Dim sItems() As String = sCurMat.Split("."c)
      If sItems.Count > 1 Then
         Dim sMat As String = sItems(0)
         Dim sSubMat As String = sItems(1)
         For Each ItemMat As EgtWPFLib5.WjMaterial In Local_MaterialList
            If ItemMat.Name = sMat Then
               For Each ItemSubMat As EgtWPFLib5.WjSubMaterial In ItemMat.SubMaterialList
                  If ItemSubMat.Name = sSubMat Then
                     For Each ItemParam As EgtWPFLib5.WjParam In ItemSubMat.ParamList
                        Dim dParamThick As Double
                        StringToDouble(ItemParam.Thickness, dParamThick)
                        If dParamThick > dMaxThick Then
                           dMaxThick = dParamThick
                        End If
                     Next
                     bFound = True
                     Exit For
                  End If
                  If bFound Then Exit For
               Next
            End If
            If bFound Then Exit For
         Next
      End If
      Return dMaxThick
   End Function

   Friend Function IsVacuumMovePossible() As Boolean
      ' Recupero diametro lama corrente
      EgtTdbSetCurrTool(sCurrSaw)
      Dim dToolDiam As Double = 0
      EgtTdbGetCurrToolParam(MCH_TP.DIAM, dToolDiam)
      ' Confronto con massimo ammesso
      Return (dToolDiam <= dMaxSawDiamForVac)
   End Function

   Friend Function IsRawProbingPossible() As Boolean
      ' Recupero diametro lama corrente
      EgtTdbSetCurrTool(sCurrSaw)
      Dim dToolDiam As Double = 0
      EgtTdbGetCurrToolParam(MCH_TP.DIAM, dToolDiam)
      ' Confronto con massimo ammesso
      Return (dToolDiam <= dMaxSawDiamForProbe)
   End Function

   Friend Function GetPrivateProfileMaterial(
                         ByVal lpAppName As String,
                         ByVal lpKeyName As String,
                         ByRef Material As Material,
                         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() = 2 Then
         Dim nId As Integer = 0
         StringToInt(sItems(0), nId)
         Material = New Material(nId, sItems(1))
         Return True
      End If
      Return False

   End Function

   Friend Function WritePrivateProfileMaterial(
                      ByVal lpAppName As String,
                      ByVal lpKeyName As String,
                      ByVal Material As Material,
                      ByVal lpFileName As String) As Boolean

      Dim MatStr As String = Nothing
      If Not IsNothing(Material) Then
         MatStr = Material.nId.ToString & "," & Material.sName
      End If
      If WritePrivateProfileString(lpAppName, lpKeyName, MatStr, sMachIniFile) Then
         Return True
      End If
      Return False
   End Function

End Class

Class ToolChangerPos

   Private m_nPosition As Integer
   Private m_sName As String = String.Empty
   Private m_sTool As String = String.Empty

   Public ReadOnly Property nPosition As Integer
      Get
         Return m_nPosition
      End Get
   End Property

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

   Public Property sTool As String
      Get
         Return m_sTool
      End Get
      Set(value As String)
         m_sTool = value
      End Set
   End Property

   Sub New(sPosition As Integer, sName As String, sTool As String)
      m_nPosition = sPosition
      m_sName = sName
      m_sTool = sTool
   End Sub

End Class

Friend Class Material

   Private m_nId As Integer
   Private m_sName As String
   Private m_SubId As Integer = 0

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

   Public Property sName As String
      Get
         Return m_sName
      End Get
      Set(value As String)
         m_sName = value
      End Set
   End Property

   Public Property SubId As Integer
      Get
         Return m_SubId
      End Get
      Set(value As Integer)
         m_SubId = value
      End Set
   End Property

   Sub New(sName As String, MaterialList As ObservableCollection(Of Material))
      Dim nMaxId As Integer = 0
      For Each Material As Material In MaterialList
         If Material.nId > nMaxId Then
            nMaxId = Material.nId
         End If
      Next
      m_nId = nMaxId + 1
      m_sName = sName
   End Sub

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

   Sub New(nId As Integer, sName As String, nSubId As Integer)
      m_nId = nId
      m_sName = sName
      m_SubId = nSubId
   End Sub

End Class

