Imports System.Collections.ObjectModel Imports System.IO Imports EgtUILib Imports System.Text.RegularExpressions Namespace EgtCAM5 Public Class MTableDbViewModel Inherits TabViewModel Private m_sPreviousActiveMachine As String Private m_TablesList As New ObservableCollection(Of MTableListBoxItem) Public Property TablesList As ObservableCollection(Of MTableListBoxItem) Get Return m_TablesList End Get Set(value As ObservableCollection(Of MTableListBoxItem)) m_TablesList = value End Set End Property #Region "Messages" Public ReadOnly Property Title As String Get Return EgtMsg(MSG_MAINWINDOW + 20) End Get End Property Public ReadOnly Property MachListHeader As String Get Return EgtMsg(MSG_DOORS + 8) End Get End Property Public ReadOnly Property MachineNameTxBl As String Get Return EgtMsg(MSG_DOORS + 9) End Get End Property Public ReadOnly Property NcGenerateTxBl As String Get Return EgtMsg(MSG_DOORS + 10) End Get End Property Public ReadOnly Property MakeRawTxBl As String Get Return EgtMsg(MSG_DOORS + 11) End Get End Property Public ReadOnly Property OnHdr As String Get Return EgtMsg(MSG_DOORS + 12) End Get End Property Public ReadOnly Property GeomNameHdr As String Get Return EgtMsg(MSG_DOORS + 13) End Get End Property Public ReadOnly Property OperationHdr As String Get Return EgtMsg(MSG_DOORS + 14) End Get End Property Public ReadOnly Property MIdHdr As String Get Return EgtMsg(MSG_DOORS + 15) End Get End Property Public ReadOnly Property ShiftHdr As String Get Return EgtMsg(MSG_DOORS + 16) End Get End Property Public ReadOnly Property MachTypeHdr As String Get Return EgtMsg(MSG_DOORS + 17) End Get End Property Public ReadOnly Property MachHdr As String Get Return EgtMsg(MSG_DOORS + 18) End Get End Property Public ReadOnly Property MachUpHdr As String Get Return EgtMsg(MSG_DOORS + 19) End Get End Property Public ReadOnly Property MachDownHdr As String Get Return EgtMsg(MSG_DOORS + 20) End Get End Property Public ReadOnly Property AddMachBtn As String Get Return EgtMsg(MSG_DOORS + 21) End Get End Property Public ReadOnly Property RemoveMachBtn As String Get Return EgtMsg(MSG_DOORS + 22) End Get End Property Public ReadOnly Property AddMachiningBtn As String Get Return EgtMsg(MSG_DOORS + 23) End Get End Property Public ReadOnly Property RemoveMachiningBtn As String Get Return EgtMsg(MSG_DOORS + 24) End Get End Property Public ReadOnly Property CurrMsg As String Get Return EgtMsg(MSG_DOORS + 25) End Get End Property #End Region ' Messages #Region "ToolTip" 'Proprietà ToolTip Public ReadOnly Property NewTableToolTip As String Get Return EgtMsg(MSG_DOORS + 1) End Get End Property Public ReadOnly Property SaveTableToolTip As String Get Return EgtMsg(MSG_DOORS + 2) End Get End Property Public ReadOnly Property SaveAsTableToolTip As String Get Return EgtMsg(MSG_DOORS + 3) End Get End Property Public ReadOnly Property RemoveTableToolTip As String Get Return EgtMsg(MSG_DOORS + 4) End Get End Property Public ReadOnly Property SetActiveTableToolTip As String Get Return EgtMsg(MSG_DOORS + 26) End Get End Property #End Region ' ToolTip ' Definizione comandi Private m_cmdNewTable As ICommand Private m_cmdSaveTable As ICommand Private m_cmdSaveTableAs As ICommand Private m_cmdRemoveTable As ICommand Private m_cmdSetActiveTable As ICommand Private m_cmdAddMach As ICommand Private m_cmdRemoveMach As ICommand Private m_cmdAddRow As ICommand Private m_cmdRemoveRow As ICommand Private m_cmdMoveRowUp As ICommand Private m_cmdMoveRowDown As ICommand Private m_cmdCloseMTableWindow As ICommand #Region "CONSTRUCTOR" Sub New() EgtGetCurrMachineName(m_sPreviousActiveMachine) MTableListBoxItem.NewMTableIndex = 0 UpdateTables() Dim bActiveMTableFound As Boolean = False For Index = 0 To m_TablesList.Count - 1 If m_TablesList(Index).ActiveTable Then m_TablesList(Index).IsSelected = True bActiveMTableFound = True Exit For End If Next If Not bActiveMTableFound AndAlso m_TablesList.Count > 0 Then m_TablesList(0).IsSelected = True End If Application.Msn.Register(Application.SAVEDOORTABLE, Sub(MTable As MTableListBoxItem) SaveTable(MTable) End Sub) Application.Msn.Register(Application.REMOVEDOORTABLE, Sub(MTable As MTableListBoxItem) TablesList.Remove(MTable) End Sub) End Sub #End Region #Region "METHODS" Private Sub UpdateTables() m_TablesList.Clear() ' se trovo la cartella carico la lista di tabelle Dim FileTableList As ReadOnlyCollection(Of String) = My.Computer.FileSystem.GetFiles(IniFile.m_sTablesRoot) For Index As Integer = 0 To FileTableList.Count - 1 If Path.GetExtension(FileTableList(Index)).ToLower = ".mtl" Then m_TablesList.Add(New MTableListBoxItem(Path.GetFileNameWithoutExtension(FileTableList(Index)), FileTableList(Index))) VerifyNewName(Path.GetFileNameWithoutExtension(FileTableList(Index))) End If Next SetActiveTable() End Sub Private Sub VerifyNewName(sNewName As String) Dim sNameIndex As String = Regex.Match(sNewName, "MTable_(\d+)").Groups(1).Value Dim nNameIndex As Integer = 0 If Not String.IsNullOrEmpty(sNameIndex) Then Integer.TryParse(sNameIndex, nNameIndex) End If If nNameIndex > MTableListBoxItem.NewMTableIndex Then MTableListBoxItem.NewMTableIndex = nNameIndex End If End Sub Private Const MTABLENAME As String = "MTableName" Private Const MTNAME As String = "MTName" Private Sub SetActiveTable() Dim ActiveMTableName As String = String.Empty Dim ActiveMTableFilePath As String = Path.GetDirectoryName(IniFile.m_sTablesRoot) & "\CurrMTable.lua" If File.Exists(ActiveMTableFilePath) Then Dim ReadFile As String() = File.ReadAllLines(ActiveMTableFilePath) For LineIndex As Integer = 0 To ReadFile.Count - 1 If ReadFile(LineIndex).Contains(MTABLENAME) Then ActiveMTableName = TableUtility.SearchKey(ReadFile(LineIndex), MTNAME) Exit For End If Next For Index As Integer = 0 To m_TablesList.Count - 1 If m_TablesList(Index).TableName = ActiveMTableName Then m_TablesList(Index).ActiveTable = True Exit For End If Next End If End Sub #End Region #Region "COMMANDS" #Region "NewTableCommand" ''' ''' Returns a command that do Exec. ''' Public ReadOnly Property NewTableCommand As ICommand Get If m_cmdNewTable Is Nothing Then m_cmdNewTable = New RelayCommand(AddressOf NewTable) End If Return m_cmdNewTable End Get End Property ''' ''' Execute the Exec. This method is invoked by the ExecCommand. ''' Public Sub NewTable(param As Object) Dim NewMTable As MTableListBoxItem = New MTableListBoxItem("MTable_" & (MTableListBoxItem.NewMTableIndex + 1).ToString, String.Empty) m_TablesList.Add(NewMTable) MTableListBoxItem.NewMTableIndex += 1 If Not IsNothing(param) Then Dim SelectedMTable As MTableListBoxItem = DirectCast(param, MTableListBoxItem) SelectedMTable.IsSelected = False End If NewMTable.IsSelected = True End Sub #End Region ' NewTableCommand #Region "SaveTableCommand" ''' ''' Returns a command that do Exec. ''' Public ReadOnly Property SaveTableCommand As ICommand Get If m_cmdSaveTable Is Nothing Then m_cmdSaveTable = New RelayCommand(AddressOf SaveTable) End If Return m_cmdSaveTable End Get End Property ''' ''' Execute the Exec. This method is invoked by the ExecCommand. ''' Public Sub SaveTable(param As Object) Dim SelectedMTable As MTableListBoxItem = DirectCast(param, MTableListBoxItem) If IsNothing(SelectedMTable) Then Return 'Dim sFile As String = String.Empty If String.IsNullOrEmpty(SelectedMTable.TableNamePath) Then SaveTableAs(SelectedMTable) Else TableUtility.WriteDoorTable(SelectedMTable, SelectedMTable.TableNamePath) SelectedMTable.IsModifiedReset() End If End Sub #End Region ' SaveTableCommand #Region "SaveTableAsCommand" ''' ''' Returns a command that do Exec. ''' Public ReadOnly Property SaveTableAsCommand As ICommand Get If m_cmdSaveTableAs Is Nothing Then m_cmdSaveTableAs = New RelayCommand(AddressOf SaveTableAs) End If Return m_cmdSaveTableAs End Get End Property ''' ''' Execute the Exec. This method is invoked by the ExecCommand. ''' Public Sub SaveTableAs(param As Object) Dim SelectedMTable As MTableListBoxItem = DirectCast(param, MTableListBoxItem) If IsNothing(SelectedMTable) Then Return Dim sFile As String = String.Empty If SaveTableAsDialog(sFile, SelectedMTable.TableName) Then SelectedMTable.IsModifiedReset() VerifyNewName(Path.GetFileNameWithoutExtension(sFile)) TableUtility.WriteDoorTable(SelectedMTable, sFile) UpdateTables() ' seleziono la tabella appena salvata For Index As Integer = 0 To m_TablesList.Count - 1 If m_TablesList(Index).TableNamePath = sFile Then m_TablesList(Index).IsSelected = True End If Next End If End Sub Public Function SaveTableAsDialog(ByRef sPath As String, sTableName As String) As Boolean ' Direttorio corrente per MTable Dim sDir As String = IniFile.m_sTablesRoot ' Apertura dialogo di scelta file DDF Dim SaveFileDialogView As New EgtWPFLib5.EgtSaveFileDialog SaveFileDialogView.Title = EgtMsg(MSG_DOORS + 5) SaveFileDialogView.Extension = ".mtl" SaveFileDialogView.Directory = sDir SaveFileDialogView.FileName = sTableName If Not SaveFileDialogView.ShowDialog Then Return False End If sPath = SaveFileDialogView.FileName Return True End Function #End Region ' SaveTableAsCommand #Region "RemoveTableCommand" ''' ''' Returns a command that do Exec. ''' Public ReadOnly Property RemoveTableCommand As ICommand Get If m_cmdRemoveTable Is Nothing Then m_cmdRemoveTable = New RelayCommand(AddressOf RemoveTable) End If Return m_cmdRemoveTable End Get End Property ''' ''' Execute the Exec. This method is invoked by the ExecCommand. ''' Public Sub RemoveTable(param As Object) Dim SelectedMTable As MTableListBoxItem = DirectCast(param, MTableListBoxItem) If IsNothing(SelectedMTable) Then Return ' cancello tutte le tavole nella lista impostate su non visibili (quelle rimaste quando viene creata una nuova tabella e passando ad un altra non la si salva) Dim Index As Integer = m_TablesList.Count - 1 While Index >= 0 If m_TablesList(Index).Visibility <> Visibility.Visible Then m_TablesList.RemoveAt(Index) End If Index -= 1 End While ' verifico se la tabella selezionata è quella attiva If SelectedMTable.ActiveTable Then MessageBox.Show(EgtMsg(MSG_DOORSERRORS + 8) & vbCrLf & EgtMsg(MSG_DOORSERRORS + 9), "", MessageBoxButton.OK, MessageBoxImage.Stop) Exit Sub End If ' chiedo conferma prima di cancellare la tabella If MessageBox.Show(EgtMsg(MSG_DOORS + 6), "", MessageBoxButton.YesNo, MessageBoxImage.Question) = MessageBoxResult.Yes Then If Not String.IsNullOrEmpty(SelectedMTable.TableNamePath) Then File.Delete(SelectedMTable.TableNamePath) End If Dim SelectedTableIndex As Integer = m_TablesList.IndexOf(SelectedMTable) m_TablesList.RemoveAt(SelectedTableIndex) If SelectedTableIndex > 0 Then TablesList(SelectedTableIndex - 1).IsSelected = True End If End If End Sub #End Region ' RemoveTableCommand #Region "SetActiveTableCommand" ''' ''' Returns a command that do Exec. ''' Public ReadOnly Property SetActiveTableCommand As ICommand Get If m_cmdSetActiveTable Is Nothing Then m_cmdSetActiveTable = New RelayCommand(AddressOf SetActiveTable) End If Return m_cmdSetActiveTable End Get End Property ''' ''' Execute the Exec. This method is invoked by the ExecCommand. ''' Public Sub SetActiveTable(param As Object) Dim SelectedMTable As MTableListBoxItem = DirectCast(param, MTableListBoxItem) If IsNothing(SelectedMTable) Then Return For Index As Integer = 0 To m_TablesList.Count - 1 If m_TablesList(Index).TableName = SelectedMTable.TableName Then m_TablesList(Index).ActiveTable = True Else m_TablesList(Index).ActiveTable = False End If Next ' Scrivo il file di configurazione con il nome di questa tavola Dim FileContent As String() = File.ReadAllLines(Path.GetDirectoryName(IniFile.m_sTablesRoot) & "\" & CURRMTABLE_FILE) Dim NewTableFileContent As New List(Of String) For LineIndex As Integer = 0 To FileContent.Count - 1 Dim sCurrLine As String = FileContent(LineIndex) sCurrLine = sCurrLine.Replace(TableUtility.DATETIME, System.DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss")) sCurrLine = sCurrLine.Replace(TableUtility.TABLENAME, SelectedMTable.TableName) NewTableFileContent.Add(sCurrLine) Next File.WriteAllLines(Path.ChangeExtension(Path.GetDirectoryName(IniFile.m_sTablesRoot) & "\" & CURRMTABLE_FILE, ".lua"), NewTableFileContent, Text.Encoding.UTF8) End Sub #End Region ' SetActiveTableCommand #Region "AddMachCommand" ''' ''' Returns a command that do Exec. ''' Public ReadOnly Property AddMachCommand As ICommand Get If m_cmdAddMach Is Nothing Then m_cmdAddMach = New RelayCommand(AddressOf AddMach) End If Return m_cmdAddMach End Get End Property ''' ''' Execute the Exec. This method is invoked by the ExecCommand. ''' Public Sub AddMach(param As Object) Dim SelectedMTable As MTableListBoxItem = DirectCast(param, MTableListBoxItem) If IsNothing(SelectedMTable) Then Return SelectedMTable.SharedMachIndex += 1 SelectedMTable.ActiveMachinesList.Add(New MTableMachineListBoxItem(String.Empty, False, False, SelectedMTable.SharedMachIndex)) SelectedMTable.MachIdList.Add(SelectedMTable.SharedMachIndex) End Sub #End Region ' AddMachCommand #Region "RemoveMachCommand" ''' ''' Returns a command that do Exec. ''' Public ReadOnly Property RemoveMachCommand As ICommand Get If m_cmdRemoveMach Is Nothing Then m_cmdRemoveMach = New RelayCommand(AddressOf RemoveMach) End If Return m_cmdRemoveMach End Get End Property ''' ''' Execute the Exec. This method is invoked by the ExecCommand. ''' Public Sub RemoveMach(param As Object) Dim SelectedMTable As MTableListBoxItem = DirectCast(param, MTableListBoxItem) If Not IsNothing(SelectedMTable) AndAlso SelectedMTable.ActiveMachinesList.Count > 1 Then ' Chiedo conferma cancellazione associazione macchina If MessageBox.Show(EgtMsg(MSG_DOORSERRORS + 12), EgtMsg(MSG_DOORSERRORS + 11), MessageBoxButton.YesNo, MessageBoxImage.Question) <> MessageBoxResult.Yes Then Return End If SelectedMTable.ActiveMachinesList.RemoveAt(SelectedMTable.SharedMachIndex - 1) SelectedMTable.MachIdList.RemoveAt(SelectedMTable.SharedMachIndex - 1) SelectedMTable.SharedMachIndex -= 1 End If End Sub #End Region ' RemoveMachCommand #Region "AddRowCommand" ''' ''' Returns a command that do Exec. ''' Public ReadOnly Property AddRowCommand As ICommand Get If m_cmdAddRow Is Nothing Then m_cmdAddRow = New RelayCommand(AddressOf AddRow) End If Return m_cmdAddRow End Get End Property ''' ''' Execute the Exec. This method is invoked by the ExecCommand. ''' Public Sub AddRow(param As Object) Dim SelectedMTable As MTableListBoxItem = DirectCast(param, MTableListBoxItem) If Not IsNothing(SelectedMTable) AndAlso Not IsNothing(SelectedMTable.SelectedAssociation) Then Dim SelectedIndex As Integer = SelectedMTable.AssociationList.IndexOf(SelectedMTable.SelectedAssociation) Dim NewEmptyRow As MTableAssociationGridBoxItem = New MTableAssociationGridBoxItem(False, String.Empty, 1, 0, String.Empty, String.Empty, String.Empty, String.Empty, SelectedMTable.ActiveMachinesList) SelectedMTable.AssociationList.Insert(SelectedIndex + 1, NewEmptyRow) SelectedMTable.SelectedAssociation = NewEmptyRow SelectedMTable.NotifyPropertyChanged("SelectedAssociation") End If End Sub #End Region ' AddRowCommand #Region "RemoveRowCommand" ''' ''' Returns a command that do Exec. ''' Public ReadOnly Property RemoveRowCommand As ICommand Get If m_cmdRemoveRow Is Nothing Then m_cmdRemoveRow = New RelayCommand(AddressOf RemoveRow) End If Return m_cmdRemoveRow End Get End Property ''' ''' Execute the Exec. This method is invoked by the ExecCommand. ''' Public Sub RemoveRow(param As Object) Dim SelectedMTable As MTableListBoxItem = DirectCast(param, MTableListBoxItem) If Not IsNothing(SelectedMTable) AndAlso SelectedMTable.AssociationList.Count > 1 Then ' Chiedo conferma cancellazione riga di associazione If MessageBox.Show(EgtMsg(MSG_DOORSERRORS + 10), EgtMsg(MSG_DOORSERRORS + 11), MessageBoxButton.YesNo, MessageBoxImage.Question) <> MessageBoxResult.Yes Then Return End If Dim SelectedIndex As Integer = SelectedMTable.AssociationList.IndexOf(SelectedMTable.SelectedAssociation) SelectedMTable.AssociationList.RemoveAt(SelectedIndex) If Not SelectedIndex < SelectedMTable.AssociationList.Count Then SelectedIndex = SelectedMTable.AssociationList.Count - 1 End If SelectedMTable.SelectedAssociation = SelectedMTable.AssociationList(SelectedIndex) SelectedMTable.NotifyPropertyChanged("SelectedAssociation") End If End Sub #End Region ' RemoveRowCommand #Region "MoveRowUpCommand" ''' ''' Returns a command that do Exec. ''' Public ReadOnly Property MoveRowUpCommand As ICommand Get If m_cmdMoveRowUp Is Nothing Then m_cmdMoveRowUp = New RelayCommand(AddressOf MoveRowUp) End If Return m_cmdMoveRowUp End Get End Property ''' ''' Execute the Exec. This method is invoked by the ExecCommand. ''' Public Sub MoveRowUp(param As Object) Dim SelectedMTable As MTableListBoxItem = DirectCast(param, MTableListBoxItem) If IsNothing(SelectedMTable) Then Return Dim SelectedIndex As Integer = SelectedMTable.AssociationList.IndexOf(SelectedMTable.SelectedAssociation) If SelectedIndex >= 1 Then SelectedMTable.AssociationList.Move(SelectedIndex, SelectedIndex - 1) End If End Sub #End Region ' MoveRowUpCommand #Region "MoveRowDownCommand" ''' ''' Returns a command that do Exec. ''' Public ReadOnly Property MoveRowDownCommand As ICommand Get If m_cmdMoveRowDown Is Nothing Then m_cmdMoveRowDown = New RelayCommand(AddressOf MoveRowDown) End If Return m_cmdMoveRowDown End Get End Property ''' ''' Execute the Exec. This method is invoked by the ExecCommand. ''' Public Sub MoveRowDown(param As Object) Dim SelectedMTable As MTableListBoxItem = DirectCast(param, MTableListBoxItem) If IsNothing(SelectedMTable) Then Return Dim SelectedIndex As Integer = SelectedMTable.AssociationList.IndexOf(SelectedMTable.SelectedAssociation) If SelectedIndex < SelectedMTable.AssociationList.Count - 1 Then SelectedMTable.AssociationList.Move(SelectedIndex, SelectedIndex + 1) End If End Sub #End Region ' MoveRowDownCommand #Region "CloseMTableWindowCommand" ''' ''' Returns a command that remove the current selected machining. ''' Public ReadOnly Property CloseMTableWindowCommand() As ICommand Get If m_cmdCloseMTableWindow Is Nothing Then m_cmdCloseMTableWindow = New RelayCommand(AddressOf CloseMTableWindow) End If Return m_cmdCloseMTableWindow End Get End Property ''' ''' Manage the MachiningDb closing. This method is invoked by the CloseMachiningDbCommand. ''' Public Sub CloseMTableWindow(param As Object) Dim SelectedMTable As MTableListBoxItem = DirectCast(param, MTableListBoxItem) If Not IsNothing(SelectedMTable) AndAlso (SelectedMTable.IsModified() OrElse String.IsNullOrEmpty(SelectedMTable.m_TableNamePath)) Then ' chiedo se salvare la tabella corrente prima di passare alla successiva If MessageBox.Show(EgtMsg(MSG_DOORS + 7), "", MessageBoxButton.YesNo, MessageBoxImage.Question) = MessageBoxResult.Yes Then SaveTable(SelectedMTable) End If End If ' ripristino la macchina selezionata all'apertura della finestra EgtSetCurrMachine(m_sPreviousActiveMachine) ' Chiusura finestra For Each Window In Application.Current.Windows If TypeOf Window Is MTableDbView Then Dim MTableDbView As MTableDbView = DirectCast(Window, MTableDbView) MTableDbView.Close() End If Next End Sub #End Region ' CloseMTableWindowCommand #End Region End Class End Namespace