Imports System.Text.RegularExpressions Imports System.Collections.ObjectModel Imports System.IO Imports EgtUILib 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 MACHORD As String = "MachOrd" Private Const MACHJOIN As String = "MachJoin" Private Const MACH As String = "Mach" Private Const MACHUPORD As String = "MachUpOrd" Private Const MACHUPJOIN As String = "MachUpJoin" Private Const MACHUP As String = "MachUp" Private Const MACHDWORD As String = "MachDwOrd" Private Const MACHDWJOIN As String = "MachDwJoin" Private Const MACHDW As String = "MachDw" Private Const MACHID As String = "MachId" Private Const SHIFT As String = "Shift" Private Const PROPERTYTABLE As String = "PropertyTable" Private Const OPER As String = "Oper" Private Const GROUP As String = "Group" Private Const PROPERTYPAR As String = "Property" Friend Function ReadDoorsTable(Table As MTableListBoxItem) As Boolean Dim FileContent As String() = File.ReadAllLines(Table.m_TableNamePath) Dim bMMachineData As Boolean = False Dim bMTable As Boolean = False Dim bPropertyTable As Boolean = False Dim bOrderedMachining 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 ElseIf FileContent(LineIndex).Contains("PropertyTable") Then bPropertyTable = 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 ElseIf bPropertyTable Then bPropertyTable = 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 Try Table.SharedMachIndex += 1 Table.ActiveMachinesList.Add(New MTableMachineListBoxItem(sMachName, bNcGenerate, bMakeraw, Table.SharedMachIndex)) Table.m_MachIdList.Add(Table.SharedMachIndex) Catch ex As Exception EgtOutLog(ex.ToString) Return False End Try 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 ' Creo elementi per lista lavorazioni da ordinare Dim MachItem As MTableMachiningGridBoxItem = Nothing Dim MachUpItem As MTableMachiningGridBoxItem = Nothing Dim MachDwItem As MTableMachiningGridBoxItem = Nothing For MachIndex = 0 To Table.ActiveMachinesList.Count - 1 If Table.ActiveMachinesList(MachIndex).MachId = nMachId Then If Not String.IsNullOrWhiteSpace(sMach) Then ' Recupero indice della lavorazione Dim sMachOrd As String = SearchKey(FileContent(LineIndex), MACHORD) Dim sMachJoin As String = SearchKey(FileContent(LineIndex), MACHJOIN) AddOrdMachItem(sMachOrd, MachIndex, sName, sMach, sMachJoin, MachItem, Table) End If If Not String.IsNullOrWhiteSpace(sMachUp) Then If sMachUp <> sMach Then ' Recupero indice della lavorazione Dim sMachOrd As String = SearchKey(FileContent(LineIndex), MACHUPORD) Dim sMachJoin As String = SearchKey(FileContent(LineIndex), MACHUPJOIN) AddOrdMachItem(sMachOrd, MachIndex, sName, sMachUp, sMachJoin, MachUpItem, Table) Else MachUpItem = MachItem End If End If If Not String.IsNullOrWhiteSpace(sMachDw) Then If sMachDw <> sMach And sMachDw <> sMachUp Then ' Recupero indice della lavorazione Dim sMachOrd As String = SearchKey(FileContent(LineIndex), MACHDWORD) Dim sMachJoin As String = SearchKey(FileContent(LineIndex), MACHDWJOIN) AddOrdMachItem(sMachOrd, MachIndex, sName, sMachDw, sMachJoin, MachDwItem, Table) ElseIf sMachDw = sMach Then MachDwItem = MachItem ElseIf sMachDw = sMachUp Then MachDwItem = MachUpItem Else MachDwItem = MachItem End If End If End If Next Table.AssociationList.Add(New MTableAssociationGridBoxItem(bOn, sName, nMachId, nShift, sOper, MachItem, sMach, MachUpItem, sMachUp, MachDwItem, sMachDw, Table.ActiveMachinesList, Table.AssociationList)) End If End If If bPropertyTable Then ' se lavorazioni ancora disordinate If Not bOrderedMachining Then ' Riordino tabelle per ordine lavorazioni For MachIndex = 0 To Table.ActiveMachinesList.Count - 1 ' Verifico se c'è almeno una lavorazione ordinata Dim bFound As Boolean = False For MachiningIndex = 0 To Table.ActiveMachinesList(MachIndex).MachiningList.Count - 1 Dim CurrMachining As MTableMachiningGridBoxItem = Table.ActiveMachinesList(MachIndex).MachiningList(MachiningIndex) If CurrMachining.RWGroupId <> 0 Then bFound = True Exit For End If Next If Not bFound And Table.ActiveMachinesList(MachIndex).MachiningList.Count > 0 Then Table.ActiveMachinesList(MachIndex).MachiningList(0).GroupId = 1 Table.ActiveMachinesList(MachIndex).MachiningList(0).RWGroupId = 100 End If Table.ActiveMachinesList(MachIndex).MachiningList = New ObservableCollection(Of MTableMachiningGridBoxItem)(Table.ActiveMachinesList(MachIndex).MachiningList.OrderBy(Function(x) x.RWGroupId)) Next bOrderedMachining = True End If ' recupero valori da tabella proprietà Dim sMachId As String = SearchKey(FileContent(LineIndex), MACHID) Dim nMachId As Integer = -1 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 sGroup As String = SearchKey(FileContent(LineIndex), GROUP) Dim nGroup As Integer = -1 Integer.TryParse(sGroup, nGroup) Dim sProperty As String = SearchKey(FileContent(LineIndex), PROPERTYPAR) For MachIndex = 0 To Table.ActiveMachinesList.Count - 1 Dim bFound As Boolean = False If Table.ActiveMachinesList(MachIndex).MachId = nMachId Then For MachiningIndex = 0 To Table.ActiveMachinesList(MachIndex).MachiningList.Count - 1 Dim CurrMachining As MTableMachiningGridBoxItem = Table.ActiveMachinesList(MachIndex).MachiningList(MachiningIndex) If CurrMachining.GroupId = nGroup \ 100 Then CurrMachining.PropertyPar = sProperty bFound = True Exit For End If Next End If If bFound Then Exit for End If Next End If Next ' verifico se le liste ordinate contengono un indice diverso da zero For MachIndex = 0 To Table.ActiveMachinesList.Count - 1 Dim CurrMachine As MTableMachineListBoxItem = Table.ActiveMachinesList(MachIndex) If CurrMachine.MachiningList.Count > 0 AndAlso CurrMachine.MachiningList(CurrMachine.MachiningList.Count - 1).GroupId = 0 Then CurrMachine.MachiningList(CurrMachine.MachiningList.Count - 1).GroupId = 1 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, Nothing, String.Empty, Nothing, String.Empty, Nothing, String.Empty, Table.ActiveMachinesList, Table.AssociationList)) End If Return True End Function Private Sub AddOrdMachItem(sMachOrd As String, MachIndex As Integer, sName As String, sMach As String, sJoin As String, ByRef MachItem As MTableMachiningGridBoxItem, Table As MTableListBoxItem) Dim nMachOrd As Integer = 0 Dim bJoin As Boolean = False If Not String.IsNullOrWhiteSpace(sMachOrd) Then Integer.TryParse(sMachOrd, nMachOrd) End If If Not String.IsNullOrEmpty(sJoin) Then Dim nValue As Integer Integer.TryParse(sJoin, nValue) bJoin = nValue <> 0 End If ' verifico se esiste già un item della tabella lavorazioni ordinate con gli stessi dati Dim bExist As Boolean = False For OrdMachIndex = 0 To Table.ActiveMachinesList(MachIndex).MachiningList.Count - 1 Dim CurrMachining As MTableMachiningGridBoxItem = Table.ActiveMachinesList(MachIndex).MachiningList(OrdMachIndex) If CurrMachining.Geometry = sName AndAlso CurrMachining.Machining = sMach Then bExist = True MachItem = CurrMachining Exit For End If Next If Not bExist Then MachItem = New MTableMachiningGridBoxItem(nMachOrd, sName, sMach, bJoin, Table.ActiveMachinesList(MachIndex)) Table.ActiveMachinesList(MachIndex).MachiningList.Add(MachItem) 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 String() = File.ReadAllLines(IniFile.m_sTablesRoot & "\" & MTABLETEMPLATE_FILE) Dim NewTableFileContent As New List(Of String) Dim bMMachineData As Boolean = False Dim bMTable As Boolean = False Dim bPropertyTable As Boolean = False ReorderMachiningIndex(SelectedTable) 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) ElseIf sCurrLine.Contains(PROPERTYTABLE) Then bPropertyTable = True NewTableFileContent.Add(sCurrLine) PrintPropertyList(SelectedTable, NewTableFileContent) End If If Not bMMachineData AndAlso Not bMTable AndAlso Not bPropertyTable Then NewTableFileContent.Add(sCurrLine) Else bMMachineData = False bMTable = False bPropertyTable = False End If Next File.WriteAllLines(sTableNamePath, NewTableFileContent, Text.Encoding.UTF8) ' scrivo tabelle lavorazioni ordinate For MachineIndex = 0 To SelectedTable.ActiveMachinesList.Count - 1 WriteMachiningOrderedTable(SelectedTable.ActiveMachinesList(MachineIndex), sTableNamePath) Next End Sub Friend Sub WriteMachiningOrderedTable(SelectedMachine As MTableMachineListBoxItem, sTableNamePath As String) Dim FileContent As String() = File.ReadAllLines(IniFile.m_sTablesRoot & "\" & MTABLETEMPLATE_FILE) Dim NewTableFileContent As New List(Of String) Dim bMMachineData As Boolean = False Dim bWaitClosure As Boolean = False Dim bMTable As Boolean = False Dim bPropertyTable 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 bWaitClosure = True ElseIf sCurrLine.Contains(MTABLE) Then bMTable = True NewTableFileContent.Add(sCurrLine) PrintOrderedMachiningList(SelectedMachine, NewTableFileContent) ElseIf sCurrLine.Contains(PROPERTYTABLE) Then bPropertyTable = True bWaitClosure = True End If If Not bMMachineData AndAlso Not bMTable AndAlso Not bPropertyTable Then NewTableFileContent.Add(sCurrLine) ElseIf bWaitClosure Then bWaitClosure = False Else bMMachineData = False bMTable = False bPropertyTable = False End If Next ' creo nuovo nome Dim sOrderedTableName As String = Path.GetDirectoryName(sTableNamePath) & "\" & Path.GetFileNameWithoutExtension(sTableNamePath) & "_" & SelectedMachine.MachId.ToString & ".otl" File.WriteAllLines(sOrderedTableName, NewTableFileContent, Text.Encoding.UTF8) End Sub Private Sub PrintOrderedMachiningList(CurrMachine As MTableMachineListBoxItem, NewTableFileContent As List(Of String)) For MachiningIndex = 0 To CurrMachine.MachiningList.Count - 1 Dim CurrMachining As MTableMachiningGridBoxItem = CurrMachine.MachiningList(MachiningIndex) Dim sCurrTool As String = String.Empty EgtMdbSetCurrMachining(CurrMachining.Machining) Dim sTUUID As String = String.Empty EgtMdbGetCurrMachiningParam(MCH_MP.TUUID, sTUUID) EgtTdbGetToolFromUUID(sTUUID, sCurrTool) Dim CurrentLine As String = " { Group = " & CurrMachining.GroupId & ", Property = '" & CurrMachining.PropertyPar & "', Name = '" & CurrMachining.Geometry & "', Mach = '" & CurrMachining.Machining & "', Join = " & If(CurrMachining.Join, 1, 0) & "}" If MachiningIndex < CurrMachine.MachiningList.Count - 1 Then CurrentLine &= "," End If CurrentLine &= " -- " & sCurrTool NewTableFileContent.Add(CurrentLine) Next 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 PrintPropertyList(SelectedTable As MTableListBoxItem, NewTableFileContent As List(Of String)) Dim CurrGroupId As Integer = -1 Dim LastGroupId As Integer = -1 For MachineIndex As Integer = 0 To SelectedTable.ActiveMachinesList.Count - 1 If SelectedTable.ActiveMachinesList(MachineIndex).MachiningList.Count > 0 Then LastGroupId = SelectedTable.ActiveMachinesList(MachineIndex).MachiningList(SelectedTable.ActiveMachinesList(MachineIndex).MachiningList.Count - 1).GroupId For MachiningIndex = 0 To SelectedTable.ActiveMachinesList(MachineIndex).MachiningList.Count - 1 Dim CurrMachining As MTableMachiningGridBoxItem = SelectedTable.ActiveMachinesList(MachineIndex).MachiningList(MachiningIndex) If CurrMachining.GroupId <> CurrGroupId And Not String.IsNullOrWhiteSpace(CurrMachining.PropertyPar) Then Dim CurrentLine As String = " { MachId = '" & SelectedTable.ActiveMachinesList(MachineIndex).MachId & "', Group = " & (CurrMachining.GroupId * 100).ToString & ", Property = " & CurrMachining.PropertyPar & " }" If CurrMachining.GroupId <> LastGroupId Then CurrentLine &= " ," End If NewTableFileContent.Add(CurrentLine) CurrGroupId = CurrMachining.GroupId ElseIf CurrMachining.GroupId <> CurrGroupId And CurrMachining.GroupId = LastGroupId Then NewTableFileContent(NewTableFileContent.Count - 1) = NewTableFileContent(NewTableFileContent.Count - 1).Trim(","c) End If Next End If Next End Sub Private Sub ReorderMachiningIndex(SelectedTable As MTableListBoxItem) For MachineIndex = 0 To SelectedTable.ActiveMachinesList.Count - 1 Dim nCurrGroupIndex As Integer = 0 Dim nCurrGroupOrd As Integer = 0 For MachiningIndex = 0 To SelectedTable.ActiveMachinesList(MachineIndex).MachiningList.Count - 1 Dim CurrMachining As MTableMachiningGridBoxItem = SelectedTable.ActiveMachinesList(MachineIndex).MachiningList(MachiningIndex) If CurrMachining.GroupId = 0 Then Continue For If CurrMachining.GroupId <> nCurrGroupIndex Then nCurrGroupOrd = 0 nCurrGroupIndex = CurrMachining.GroupId Else nCurrGroupOrd += 1 End If CurrMachining.RWGroupId = CurrMachining.GroupId * 100 + nCurrGroupOrd Next 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), ", MachOrd = '" & SelectedTable.AssociationList(Index).RefMachItem.RWGroupId & "'" & ", MachJoin = '" & If(SelectedTable.AssociationList(Index).RefMachItem.Join, 1, 0) & "'" & ", Mach = '" & SelectedTable.AssociationList(Index).Mach & "'", String.Empty) & If(Not String.IsNullOrEmpty(SelectedTable.AssociationList(Index).MachUp), ", MachUpOrd = '" & SelectedTable.AssociationList(Index).RefMachUpItem.RWGroupId & "'" & ", MachUpJoin = '" & If(SelectedTable.AssociationList(Index).RefMachUpItem.Join, 1, 0) & "'" & ", MachUp = '" & SelectedTable.AssociationList(Index).MachUp & "'", String.Empty) & If(Not String.IsNullOrEmpty(SelectedTable.AssociationList(Index).MachDw), ", MachDwOrd = '" & SelectedTable.AssociationList(Index).RefMachDwItem.RWGroupId & "'" & ", MachDwJoin = '" & If(SelectedTable.AssociationList(Index).RefMachDwItem.Join, 1, 0) & "'" & ", MachDw = '" & SelectedTable.AssociationList(Index).MachDw & "'", String.Empty) & " }" If Index < SelectedTable.AssociationList.Count - 1 Then CurrentLine &= " ," End If NewTableFileContent.Add(CurrentLine) Next End Sub Friend Function VerifyDoorsDir() As Boolean ' verifico l'esistenza della cartella Machines If String.IsNullOrWhiteSpace(IniFile.m_sDoorsDirPath) Then Return False End If Return RecursiveVerifyDoorsDir() End Function Private Function RecursiveVerifyDoorsDir() As Boolean If Directory.Exists(IniFile.m_sDoorsDirPath) Then Return True Else If MessageBox.Show(String.Format(EgtMsg(MSG_DOORSERRORS + 15) & Environment.NewLine & EgtMsg(MSG_DOORSERRORS + 16), IniFile.m_sDoorsDirPath), EgtMsg(MSG_MESSAGEBOX + 1), MessageBoxButton.YesNo, MessageBoxImage.Error) = MessageBoxResult.Yes Then Return RecursiveVerifyDoorsDir() End If Return False End If End Function End Module