Files
egtbeamwall/EgtBEAMWALL.Optimizer/Utility/MyMachine.vb
T
Emmanuele Sassi b6a68f06a3 - spostamento parametri macchina in MyMachine
- divisione tra macchina del progetto e di configurazione
2025-09-08 10:48:51 +02:00

420 lines
17 KiB
VB.net

Imports System.Collections.ObjectModel
Imports EgtBEAMWALL.Core
Imports EgtWPFLib5
Imports EgtUILib
Imports EgtBEAMWALL.Optimizer.MachParam
Imports System.IO
Public Class MyMachine
Inherits Core.MyMachine
' File ini dei parametri macchina
Private m_sMachParamIniFile As String = String.Empty
' File lettura Ts3Data
Private m_sGetTs3DataFile As String = String.Empty
' File Ts3Data
Private m_sTs3DataFile As String = String.Empty
Private m_MachTableList As New ObservableCollection(Of MachTable)
Public Property MachTableList As ObservableCollection(Of MachTable)
Get
Return m_MachTableList
End Get
Set(value As ObservableCollection(Of MachTable))
m_MachTableList = value
End Set
End Property
Sub New(sDirPath As String, sIniPath As String)
MyBase.New(sDirPath, sIniPath)
End Sub
Public Shared Shadows Function MachineListInit(sMachinesRootDir As String, MachineList As IList(Of Machine)) As Boolean
' Se direttorio base macchine non definito o non esiste, ritorno
If String.IsNullOrWhiteSpace(sMachinesRootDir) OrElse
Not Directory.Exists(sMachinesRootDir) Then
MachineList = Nothing
Return False
End If
' Cerco le macchine
Dim Machines As String() = Directory.GetDirectories(sMachinesRootDir)
For i As Integer = 0 To Machines.Count - 1
Dim PathIni As String = Machines(i) & "\" & Path.GetFileName(Machines(i)) & ".ini"
Dim PathBeam As String = Machines(i) & "\" & "Beam"
Dim PathWall As String = Machines(i) & "\" & "Wall"
If File.Exists(PathIni) AndAlso (Directory.Exists(PathBeam) OrElse Directory.Exists(PathWall)) Then
MachineList.Add(New MyMachine(Machines(i), PathIni))
End If
Next
Return True
End Function
' funzione che scrive i parametri modificati sul file INI
Public Sub WriteMachParams()
If String.IsNullOrWhiteSpace(CurrentMachine.sMachParamIniFile) OrElse Not File.Exists(CurrentMachine.sMachParamIniFile) Then
EgtOutLog("Impossible loading Mach parameters: MachData.ini file not found or not readable")
Return
End If
For Each MachTableItem In MachTableList
For Each MachParamItem In MachTableItem.MachParamList
If MachParamItem.IsModified Then
Dim sMachParamType As String = String.Empty
Select Case MachParamItem.nType
Case MachParamType.DOUBLE_
sMachParamType = "d"
Case MachParamType.STRING_
sMachParamType = "s"
Case MachParamType.LENGTH
sMachParamType = "l"
End Select
Dim MachParamString As String = sMachParamType & "," & MachParamItem.sName & "," & MachParamItem.sValue & "," & MachParamItem.sDescription
Dim bOk = WriteMachPrivateProfileString(MachParamItem.nParentTable, MachParamItem.nParamIndex, MachParamString)
If Not bOk Then
MessageBox.Show(EgtMsg(61859), EgtMsg(30007))
Return
End If
MachParamItem.IsModifiedReset()
End If
Next
Next
' se macchina di tipo BOTH copio il file appena scritto nella cartella Beam nella cartella Wall o viceversa
If CurrentMachine.nType = MachineType.BOTH Then
If CurrentMachine.sMachParamIniFile.Contains("\" & K_BEAM & "\") Then
File.Copy(CurrentMachine.sMachParamIniFile, CurrentMachine.sMachParamIniFile.Replace("\" & K_BEAM & "\", "\" & K_WALL & "\"), True)
Else
File.Copy(CurrentMachine.sMachParamIniFile, CurrentMachine.sMachParamIniFile.Replace("\" & K_WALL & "\", "\" & K_BEAM & "\"), True)
End If
End If
End Sub
' funzione che scrive i parametri modificati sul file LUA
Private Function WriteMachParamsLua() As Boolean
If String.IsNullOrWhiteSpace(CurrentMachine.sMachParamIniFile) OrElse Not File.Exists(CurrentMachine.sMachParamIniFile) Then
EgtOutLog("Impossible loading Mach parameters: MachData.ini file not found or not readable")
Return False
End If
' inizio routine di scrittura
Dim NewTs3DataFileContent = New List(Of String)
Dim sMachineStr As String = String.Empty
For Each MachTableItem In MachTableList
NewTs3DataFileContent.Add("local " & MachTableItem.sName & " = {")
sMachineStr &= " " & MachTableItem.sName & "=" & MachTableItem.sName & ","
For Each MachParamItem In MachTableItem.MachParamList
NewTs3DataFileContent.Add(" " & MachParamItem.sName & "=" & MachParamItem.sValue & ",")
Next
NewTs3DataFileContent.Last().TrimEnd(",")
NewTs3DataFileContent.Add("}")
NewTs3DataFileContent.Add("")
Next
NewTs3DataFileContent.Add("local Machine = {" & sMachineStr.TrimEnd(",") & "}")
NewTs3DataFileContent.Add("return Machine")
Dim FilePath As String
If Map.refMainWindowVM.MainWindowM.bMachConfig Then
FilePath = CurrentMachine.sMachDir & "\" & If(CurrentMachine.nType = MachineType.BEAM, BEAM_DIR, WALL_DIR) & "\" & EBWDATA_FILE
Else
FilePath = CurrentMachine.sMachDir & "\" & If(CurrentMachine.nType = MachineType.BEAM, BEAM_DIR, WALL_DIR) & "\" & TS3DATA_FILE
End If
Dim sBakFilePath As String = Path.ChangeExtension(FilePath, ".bak")
If File.Exists(sBakFilePath) Then File.Delete(sBakFilePath)
If File.Exists(FilePath) Then File.Move(FilePath, sBakFilePath)
File.WriteAllLines(FilePath, NewTs3DataFileContent, Text.Encoding.UTF8)
' se macchina di tipo BOTH copio il file appena scritto nella cartella Wall nella cartella Beam
If CurrentMachine.nType = MachineType.BOTH Then
Dim DestinationFilePath As String
If Map.refMainWindowVM.MainWindowM.bMachConfig Then
DestinationFilePath = CurrentMachine.sMachDir & "\" & BEAM_DIR & "\" & EBWDATA_FILE
Else
DestinationFilePath = CurrentMachine.sMachDir & "\" & BEAM_DIR & "\" & TS3DATA_FILE
End If
sBakFilePath = Path.ChangeExtension(DestinationFilePath, ".bak")
If File.Exists(sBakFilePath) Then File.Delete(sBakFilePath)
File.Move(DestinationFilePath, Path.ChangeExtension(DestinationFilePath, ".bak"))
File.Copy(FilePath, DestinationFilePath, True)
End If
Return True
End Function
' funzione che verifica la modifica dei valori in Configurazione e ne chiede il salvataggio
Friend Sub VerifyConfigPageModification()
Dim bExitFor = False
' ciclo sui parametri Macchina
For Each MachTableItem In MachTableList
For Each MachParamItem In MachTableItem.MachParamList
If MachParamItem.IsModified Then
If MessageBox.Show(EgtMsg(61860), "", MessageBoxButton.YesNo, MessageBoxImage.Question) = MessageBoxResult.Yes Then
' scrivo i parametri Macchina
WriteMachParams()
WriteMachParamsLua()
Else
' se da non salvare li resetto ed esco dai For
CreateMachParams()
bExitFor = True
Exit For
End If
End If
Next
If bExitFor Then Exit For
Next
End Sub
' funzione che crea l'elenco dei parametri Macchina
Friend Sub CreateMachParams()
' Impostazione path MachParamIni file
If nType = MachineType.BEAM Then
m_sMachParamIniFile = Map.refMainWindowVM.MainWindowM.sMachinesRoot & "\" & Me.Name & "\" & K_BEAM & "\" & MACH_INI_FILE_NAME
m_sGetTs3DataFile = Map.refMainWindowVM.MainWindowM.sMachinesRoot & "\" & Me.Name & "\" & K_BEAM & "\" & GETTS3DATA_FILE_NAME
m_sTs3DataFile = Map.refMainWindowVM.MainWindowM.sMachinesRoot & "\" & Me.Name & "\" & K_BEAM & "\" & TS3DATA_FILE
ElseIf nType = MachineType.WALL Then
m_sMachParamIniFile = Map.refMainWindowVM.MainWindowM.sMachinesRoot & "\" & Me.Name & "\" & K_WALL & "\" & MACH_INI_FILE_NAME
m_sGetTs3DataFile = Map.refMainWindowVM.MainWindowM.sMachinesRoot & "\" & Me.Name & "\" & K_WALL & "\" & GETTS3DATA_FILE_NAME
m_sTs3DataFile = Map.refMainWindowVM.MainWindowM.sMachinesRoot & "\" & Me.Name & "\" & K_WALL & "\" & TS3DATA_FILE
Else
' Se macchina di tipo BOTH prendo quello presente in Beam, se esiste, altrimenti prendo quello in Wall
Dim sBeamMachParamIniFile = Map.refMainWindowVM.MainWindowM.sMachinesRoot & "\" & Me.Name & "\" & K_BEAM & "\" & MACH_INI_FILE_NAME
Dim sWallMachParamIniFile = Map.refMainWindowVM.MainWindowM.sMachinesRoot & "\" & Me.Name & "\" & K_WALL & "\" & MACH_INI_FILE_NAME
Dim sBeamGetTs3File = Map.refMainWindowVM.MainWindowM.sMachinesRoot & "\" & Me.Name & "\" & K_BEAM & "\" & GETTS3DATA_FILE_NAME
Dim sWallGetTs3File = Map.refMainWindowVM.MainWindowM.sMachinesRoot & "\" & Me.Name & "\" & K_WALL & "\" & GETTS3DATA_FILE_NAME
Dim sBeamTs3DataFile = Map.refMainWindowVM.MainWindowM.sMachinesRoot & "\" & Me.Name & "\" & K_BEAM & "\" & TS3DATA_FILE
Dim sWallTs3DataFile = Map.refMainWindowVM.MainWindowM.sMachinesRoot & "\" & Me.Name & "\" & K_WALL & "\" & TS3DATA_FILE
If nType = MachineType.BOTH Then
m_sMachParamIniFile = If(File.Exists(sBeamMachParamIniFile), sBeamMachParamIniFile, sWallMachParamIniFile)
m_sGetTs3DataFile = If(File.Exists(sBeamGetTs3File), sBeamGetTs3File, sWallGetTs3File)
m_sTs3DataFile = If(File.Exists(sBeamTs3DataFile), sBeamTs3DataFile, sWallTs3DataFile)
End If
End If
MachTableList.Clear()
If String.IsNullOrWhiteSpace(m_sMachParamIniFile) OrElse Not File.Exists(m_sMachParamIniFile) Then
EgtOutLog("Impossible loading Mach parameters: MachData.ini file not found or not readable")
Return
End If
Dim NewMachParam As MachParam = Nothing
Dim MachParamList As New ObservableCollection(Of MachParam)
Dim TableIndex = 1
Dim ParamIndex As Integer = 1
Dim TableName As String = String.Empty
' carico lua ts3data
Dim bTs3DataLoaded As Boolean = False
EgtLuaCreateGlobTable("GTSD")
EgtLuaSetGlobStringVar("GTSD.TS3PATH", m_sTs3DataFile)
Dim sExecPath As String = m_sGetTs3DataFile
bTs3DataLoaded = File.Exists(m_sTs3DataFile) AndAlso EgtLuaExecFile(sExecPath)
If Not bTs3DataLoaded Then
EgtOutLog("Error executing Ts3Data file " & m_sTs3DataFile)
End If
' verifico che ci sia una table con l'indice designato e ne leggo il nome
While GetMachPrivateProfileString(TableIndex, K_NAME, "", TableName)
' leggo tutti i parametri della table
While MachParamIniFile.GetMachPrivateProfileParam(TableIndex, ParamIndex, NewMachParam, TableName, bTs3DataLoaded)
MachParamList.Add(NewMachParam)
ParamIndex += 1
End While
MachTableList.Add(New MachTable(TableName, MachParamList))
' aggiorno indici e resetto lista per lettura dell'eventuale table successiva
TableIndex += 1
ParamIndex = 1
MachParamList = New ObservableCollection(Of MachParam)
End While
' Cancello tavola globale
EgtLuaResetGlobVar("GTSD")
End Sub
End Class
Public Class MachParam
Inherits VMBase
' Tipo parametro nel file di configurazione Macchina
Public Enum MachParamType As Integer
DOUBLE_ = 1
STRING_ = 2
COMBO = 3
LENGTH = 4
CHECKBOX = 5
End Enum
' table a cui appartiene il parametro
Private m_nParentTable As Integer
Friend ReadOnly Property nParentTable As Integer
Get
Return m_nParentTable
End Get
End Property
' indice del parametro
Private m_nParamIndex As Integer
Friend ReadOnly Property nParamIndex As Integer
Get
Return m_nParamIndex
End Get
End Property
' tipo della variabile
Private m_nType As MachParamType
Friend ReadOnly Property nType As MachParamType
Get
Return m_nType
End Get
End Property
' parametri da struttura
Private m_sName As String
Public Property sName As String
Get
Return m_sName
End Get
Set(value As String)
m_sName = value
End Set
End Property
' parametri da geometria
Private m_IsModifiedValue As Boolean = False
Private m_dValue As Double
Private m_sValue As String
Public Property sValue As String
Get
Select Case nType
Case MachParamType.DOUBLE_
Return DoubleToString(m_dValue, 3)
Case MachParamType.LENGTH
Return LenToString(m_dValue, 3)
Case Else ' stringhe
Return m_sValue
End Select
Return If(nType = MachParamType.LENGTH, LenToString(m_dValue, 3), DoubleToString(m_dValue, 3))
End Get
Set(value As String)
Dim dNewValue As Double
' verifico se valore immesso è diverso dall'originale
If nParentTable > 0 Then
' trasformo valori
Select Case nType
Case MachParamType.DOUBLE_
StringToDoubleAdv(value, dNewValue, True)
m_IsModifiedValue = Math.Abs(dNewValue - m_dValue) > EPS_SMALL
Case MachParamType.LENGTH
StringToLenAdv(value, dNewValue, True)
m_IsModifiedValue = Math.Abs(dNewValue - m_dValue) > EPS_SMALL
Case Else
m_IsModifiedValue = String.Compare(value, m_sValue) <> 0
End Select
End If
' se valore immesso è diverso e password non inserita
If m_IsModifiedValue AndAlso Not Map.refConfigurationPageVM.bModifyMachParam Then
Map.refConfigurationPageVM.bModifyMachParam = Map.refConfigurationPageVM.VerifyConfigPagePassword()
If Not Map.refConfigurationPageVM.bModifyMachParam Then
NotifyPropertyChanged(NameOf(sValue))
m_IsModifiedValue = False
Return
End If
End If
Select Case nType
Case MachParamType.DOUBLE_, MachParamType.LENGTH
UpdateParamValue(dNewValue, "")
Case Else
UpdateParamValue(0, value)
End Select
If Map.refConfigurationPageVM.bModifyMachParam Then Map.refConfigurationPageVM.VerifyConfigPageModification()
End Set
End Property
Public Property dValue As Double
Get
Return m_dValue
End Get
Set(value As Double)
m_dValue = value
NotifyPropertyChanged(NameOf(sValue))
End Set
End Property
' descrizione del parametro
Private m_sDescription As String
Public Property sDescription As String
Get
Return m_sDescription
End Get
Set(value As String)
m_sDescription = value
End Set
End Property
Private Sub StdInit(nParentTable As Integer, nParamIndex As Integer, Type As MachParamType, sName As String, sDescription As String)
m_nParentTable = nParentTable
m_nParamIndex = nParamIndex
m_nType = Type
m_sName = sName
m_sDescription = MsgToString(sDescription)
End Sub
' new per double e length
Sub New(nParentTable As Integer, nParamIndex As Integer, nType As MachParamType, sParamName As String, dValue As Double, sDescription As String)
StdInit(nParentTable, nParamIndex, nType, sParamName, sDescription)
m_dValue = dValue 'sValue = DoubleToString(dValue, 3)
End Sub
' new per stringhe
Sub New(nParentTable As Integer, nParamIndex As Integer, nType As MachParamType, sParamName As String, sDescription As String)
StdInit(nParentTable, nParamIndex, nType, sParamName, sDescription)
End Sub
' new per parametro vuoto
Sub New(nType As MachParamType, nParamIndex As Integer, sParamName As String)
StdInit(Nothing, nParamIndex, nType, sParamName, "")
End Sub
Friend Sub UpdateParamValue(dNewValue As Double, sNewValue As String, Optional bDraw As Boolean = True)
Select Case nType
Case MachParamType.DOUBLE_, MachParamType.LENGTH
m_dValue = dNewValue
Case MachParamType.STRING_
m_sValue = sNewValue
End Select
End Sub
Public ReadOnly Property IsModified() As Boolean
Get
Return m_IsModifiedValue
End Get
End Property
Public Sub IsModifiedReset()
m_IsModifiedValue = False
End Sub
End Class
Public Class MachTable
Inherits VMBase
' nome della table
Private m_sName As String
Public Property sName As String
Get
Return m_sName
End Get
Set(value As String)
m_sName = value
End Set
End Property
Private m_MachParamList As New ObservableCollection(Of MachParam)
Public Property MachParamList As ObservableCollection(Of MachParam)
Get
Return m_MachParamList
End Get
Set(value As ObservableCollection(Of MachParam))
m_MachParamList = value
End Set
End Property
Sub New(Name As String, ParamList As ObservableCollection(Of MachParam))
sName = Name
MachParamList = ParamList
End Sub
End Class