Files
EgtCAM5/MTableDb/TableUtility.vb
T
Emmanuele Sassi e3a00c0933 EgtCAM5 :
- Aggiunta selezione MTable correntemente attiva.
2016-10-24 09:52:38 +00:00

187 lines
9.7 KiB
VB.net

Imports System.Text.RegularExpressions
Imports System.IO
Module TableUtility
Friend Const DATETIME As String = "%DATE_TIME%"
Friend Const TABLENAME As String = "%TABLE_NAME%"
Private Const MMACHINEDATA As String = ".MMachineData"
Private Const MACHNAME As String = "MachName"
Private Const NCGENERATE As String = "NcGenerate"
Private Const MAKERAW As String = "MakeRaw"
Private Const MTABLE As String = ".MTable"
Private Const NAME As String = "Name"
Private Const ONCONST As String = "On"
Private Const MACH As String = "Mach"
Private Const MACHUP As String = "MachUp"
Private Const MACHDW As String = "MachDw"
Private Const MACHID As String = "MachId"
Private Const SHIFT As String = "Shift"
Private Const OPER As String = "Oper"
Friend Sub ReadDoorsTable(Table As MTableListBoxItem)
Dim FileContent As IEnumerable(Of String) = File.ReadLines(Table.m_TableNamePath)
Dim bMMachineData As Boolean = False
Dim bMTable As Boolean = False
For LineIndex As Integer = 0 To FileContent.Count - 1
If FileContent(LineIndex).Contains(MMACHINEDATA) Then
bMMachineData = True
ElseIf FileContent(LineIndex).Contains(MTABLE) Then
bMTable = True
End If
Dim Open As Integer = CountCharacter(FileContent(LineIndex), "{"c)
Dim Close As Integer = CountCharacter(FileContent(LineIndex), "}"c)
If Close > Open Then
If bMMachineData Then
bMMachineData = False
ElseIf bMTable Then
bMTable = False
End If
End If
If bMMachineData Then
Dim sMachName As String = SearchKey(FileContent(LineIndex), MACHNAME)
Dim bNcGenerate As Boolean = False
Dim bMakeraw As Boolean = False
If Not String.IsNullOrEmpty(sMachName) Then
Dim sNcGenerate As String = SearchKey(FileContent(LineIndex), NCGENERATE)
If Not String.IsNullOrEmpty(sNcGenerate) Then
bNcGenerate = Convert.ToBoolean(sNcGenerate)
End If
Dim sMakeraw As String = SearchKey(FileContent(LineIndex), MAKERAW)
If Not String.IsNullOrEmpty(sMakeraw) Then
bMakeraw = Convert.ToBoolean(sMakeraw)
End If
Table.SharedMachIndex += 1
Table.ActiveMachinesList.Add(New MTableMachineListBoxItem(sMachName, bNcGenerate, bMakeraw, Table.SharedMachIndex))
Table.m_MachIdList.Add(Table.SharedMachIndex)
End If
End If
If bMTable Then
Dim sName As String = SearchKey(FileContent(LineIndex), NAME)
Dim bOn As Boolean = False
Dim sMach As String = String.Empty
Dim sMachUp As String = String.Empty
Dim sMachDw As String = String.Empty
Dim nMachId As Integer = 1
Dim nShift As Integer = 0
Dim sOper As String = String.Empty
If Not String.IsNullOrEmpty(sName) Then
Dim sOn As String = SearchKey(FileContent(LineIndex), ONCONST)
If Not String.IsNullOrEmpty(sOn) Then
Dim nValue As Integer
Integer.TryParse(sOn, nValue)
bOn = nValue <> 0
End If
sMach = SearchKey(FileContent(LineIndex), MACH)
sMachUp = SearchKey(FileContent(LineIndex), MACHUP)
sMachDw = SearchKey(FileContent(LineIndex), MACHDW)
Dim sMachId = SearchKey(FileContent(LineIndex), MACHID)
If String.IsNullOrEmpty(sMachId) Then
' il suo valore di default è 1 quindi lo imposto a questo valore
nMachId = 1
Else
Integer.TryParse(sMachId, nMachId)
End If
Dim sShift = SearchKey(FileContent(LineIndex), SHIFT)
If String.IsNullOrEmpty(sShift) Then
' il suo valore di default è 1 quindi lo imposto a questo valore
nShift = 0
Else
Integer.TryParse(sShift, nShift)
End If
sOper = SearchKey(FileContent(LineIndex), OPER)
' verifico che il MachId letto sia valido
If nMachId > Table.m_MachIdList.Count Then
nMachId = 0
End If
Table.AssociationList.Add(New MTableAssociationGridBoxItem(bOn, sName, nMachId, nShift, sOper, sMach, sMachUp, sMachDw, Table.ActiveMachinesList))
End If
End If
Next
' se nessuna macchina, ne aggiungo una vuota
If Table.ActiveMachinesList.Count = 0 Then
Table.SharedMachIndex += 1
Table.ActiveMachinesList.Add(New MTableMachineListBoxItem(String.Empty, False, False, Table.SharedMachIndex))
Table.m_MachIdList.Add(Table.SharedMachIndex)
End If
' se nessuna lavorazione, ne aggiungo una vuota
If Table.AssociationList.Count = 0 Then
Table.AssociationList.Add(New MTableAssociationGridBoxItem(False, String.Empty, 0, 0, String.Empty, String.Empty, String.Empty, String.Empty, Table.ActiveMachinesList))
End If
End Sub
Friend Function SearchKey(sLine As String, sKey As String) As String
Return Regex.Match(sLine, "[,|{|\s]" & sKey & "\s*=\s*['|\""]?(.*?)\s*?[,|}|'|\""]").Groups(1).Value
End Function
Public Function CountCharacter(ByVal sLine As String, ByVal cValue As Char) As Integer
Dim nCount As Integer = 0
Dim cArray() As Char = sLine.ToCharArray
For Index As Integer = 0 To cArray.Count - 1
If cArray(Index) = cValue Then nCount += 1
Next
Return nCount
End Function
Friend Sub WriteDoorTable(SelectedTable As MTableListBoxItem, sTableNamePath As String)
Dim FileContent As IEnumerable(Of String) = File.ReadLines(IniFile.m_sTablesRoot & "\" & MTABLETEMPLATE_FILE)
Dim NewTableFileContent As New List(Of String)
Dim bMMachineData As Boolean = False
Dim bMTable As Boolean = False
For LineIndex As Integer = 0 To FileContent.Count - 1
Dim sCurrLine As String = FileContent(LineIndex)
sCurrLine = sCurrLine.Replace(DATETIME, System.DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"))
sCurrLine = sCurrLine.Replace(TABLENAME, Path.GetFileNameWithoutExtension(sTableNamePath))
If sCurrLine.Contains(MMACHINEDATA) Then
bMMachineData = True
NewTableFileContent.Add(sCurrLine)
PrintActiveMachineList(SelectedTable, NewTableFileContent)
ElseIf sCurrLine.Contains(MTABLE) Then
bMTable = True
NewTableFileContent.Add(sCurrLine)
PrintActiveMachiningList(SelectedTable, NewTableFileContent)
End If
If Not bMMachineData AndAlso Not bMTable Then
NewTableFileContent.Add(sCurrLine)
Else
bMMachineData = False
bMTable = False
End If
Next
File.WriteAllLines(sTableNamePath, NewTableFileContent, Text.Encoding.UTF8)
End Sub
Private Sub PrintActiveMachineList(SelectedTable As MTableListBoxItem, NewTableFileContent As List(Of String))
For Index As Integer = 0 To SelectedTable.ActiveMachinesList.Count - 1
Dim CurrentLine As String = " { MachName = '" & SelectedTable.ActiveMachinesList(Index).SelectedMachine & "', NcGenerate = " & SelectedTable.ActiveMachinesList(Index).NcGenerate.ToString.ToLower _
& ", MakeRaw = " & SelectedTable.ActiveMachinesList(Index).Makeraw.ToString.ToLower & " }"
If Index < SelectedTable.ActiveMachinesList.Count - 1 Then
CurrentLine &= " ,"
End If
NewTableFileContent.Add(CurrentLine)
Next
End Sub
Private Sub PrintActiveMachiningList(SelectedTable As MTableListBoxItem, NewTableFileContent As List(Of String))
For Index As Integer = 0 To SelectedTable.AssociationList.Count - 1
If String.IsNullOrEmpty(SelectedTable.AssociationList(Index).Name) Then Continue For
Dim CurrentLine As String = " { On = " & If(SelectedTable.AssociationList(Index).OnPar, 1, 0) & ", Name = '" & SelectedTable.AssociationList(Index).Name & "'" _
& If(Not String.IsNullOrEmpty(SelectedTable.AssociationList(Index).Oper), ", Oper = '" & SelectedTable.AssociationList(Index).Oper & "'", String.Empty) _
& If(Not IsNothing(SelectedTable.AssociationList(Index).MachId) AndAlso SelectedTable.AssociationList(Index).MachId > 1, ", MachId = " & SelectedTable.AssociationList(Index).MachId.ToString, String.Empty) _
& If(SelectedTable.AssociationList(Index).Shift <> 0, ", Shift = " & SelectedTable.AssociationList(Index).Shift.ToString, String.Empty) _
& If(Not String.IsNullOrEmpty(SelectedTable.AssociationList(Index).Mach), ", Mach = '" & SelectedTable.AssociationList(Index).Mach & "'", String.Empty) _
& If(Not String.IsNullOrEmpty(SelectedTable.AssociationList(Index).MachUp), ", MachUp = '" & SelectedTable.AssociationList(Index).MachUp & "'", String.Empty) _
& If(Not String.IsNullOrEmpty(SelectedTable.AssociationList(Index).MachDw), ", MachDw = '" & SelectedTable.AssociationList(Index).MachDw & "'", String.Empty) & " }"
If Index < SelectedTable.AssociationList.Count - 1 Then
CurrentLine &= " ,"
End If
NewTableFileContent.Add(CurrentLine)
Next
End Sub
End Module