Imports System.Collections.ObjectModel Imports System.IO Imports EgtUILib Imports EgtWPFLib5 Public Class ImportExportMachiningPanelVM Inherits VMBase ' Modalita' di apertura della finestra Public Enum WindowModeEnum As Integer IMPORT EXPORT End Enum Private m_WindowMode As WindowModeEnum Public ReadOnly Property WindowMode As WindowModeEnum Get Return m_WindowMode End Get End Property ' Lista delle lavorazioni Private m_MachiningList As New ObservableCollection(Of ImpExpMachiningItem) Public Property MachiningList As ObservableCollection(Of ImpExpMachiningItem) Get Return m_MachiningList End Get Set(value As ObservableCollection(Of ImpExpMachiningItem)) m_MachiningList = value End Set End Property ' Percorso del file da cui importare le lavorazioni Private m_ImportFilePath As String Public ReadOnly Property ImportFilePath As String Get Return m_ImportFilePath End Get End Property ' Lista delle lavorazioni presenti nel file da cui importare Private m_ImportFileMachiningNameList As String() Public ReadOnly Property ImportFileMachiningNameList As String() Get Return m_ImportFileMachiningNameList End Get End Property ' Lista delle lavorazioni importate con successo Private m_vsImported As String() Public ReadOnly Property vsImported As String() Get Return m_vsImported End Get End Property Public ReadOnly Property IsEnabledOkBtn As Boolean Get For Each Machining In MachiningList If Machining.Active Then Return True Next Return False End Get End Property Friend Event m_CloseWindow(bDialogResult As Boolean) ' Definizione comandi Private m_cmdOk As ICommand #Region "MESSAGES" Public ReadOnly Property OkMsg As String Get If WindowMode = WindowModeEnum.IMPORT Then Return EgtMsg(31450) Else Return EgtMsg(31451) End If End Get End Property #End Region ' Messages #Region "CONSTRUCTOR" ' export Sub New() m_WindowMode = WindowModeEnum.EXPORT ' carico lista lavorazioni MachiningList.Clear() ' leggo ed aggiungo le altre Dim nIndex As Integer = 1 Dim sGUID As String = "" Dim sName As String = "" Dim sMaterials As String = "" While ReadMachiningParamString(nIndex, MAC_GUID, "", sGUID) > 0 Dim Guid As Guid = Guid.Empty Guid.TryParse(sGUID, Guid) ReadMachiningParamString(nIndex, MAC_NAME, "", sName) MachiningList.Add(New ImpExpMachiningItem(nIndex, Guid, sName, False)) nIndex += 1 End While ImpExpMachiningItem.m_delEnableOkBtn = AddressOf EnableOkBtn End Sub ' import Sub New(sImportFilePath As String) m_WindowMode = WindowModeEnum.IMPORT m_ImportFilePath = sImportFilePath ' carico lista lavorazioni MachiningList.Clear() ' leggo ed aggiungo le altre Dim nIndex As Integer = 1 Dim sGUID As String = "" Dim sName As String = "" Dim sMaterials As String = "" While ReadMachiningParamString(nIndex, MAC_GUID, "", sGUID, sImportFilePath) > 0 Dim Guid As Guid = Guid.Empty Guid.TryParse(sGUID, Guid) ReadMachiningParamString(nIndex, MAC_NAME, "", sName, sImportFilePath) MachiningList.Add(New ImpExpMachiningItem(nIndex, Guid, sName, AlreadyExist(sName))) nIndex += 1 End While ImpExpMachiningItem.m_delEnableOkBtn = AddressOf EnableOkBtn End Sub #End Region ' Constructor #Region "METHODS" Private Sub EnableOkBtn() NotifyPropertyChanged(NameOf(IsEnabledOkBtn)) End Sub Private Function AlreadyExist(MachiningName As String) As Boolean Return Map.refMachiningDbVM.MachiningList.Any(Function(x) x.sName = MachiningName) End Function #End Region ' METHODS #Region "COMMANDS" #Region "OkCommand" ''' ''' Returns a command that remove the current selected machining. ''' Public ReadOnly Property OkCommand() As ICommand Get If m_cmdOk Is Nothing Then m_cmdOk = New Command(AddressOf ConfirmImpExpMachinings) End If Return m_cmdOk End Get End Property ''' ''' Manage the MachiningDb closing. This method is invoked by the CloseMachiningDbCommand. ''' Public Sub ConfirmImpExpMachinings(param As Object) Select Case WindowMode Case WindowModeEnum.IMPORT Dim OrigFilePath As String = CurrentMachine.sMachiningFilePath Dim NewMachining As Machining = Nothing For Each Machining In m_MachiningList Dim nIndex As Integer = Map.refMachiningDbVM.MachiningList.Max(Function(x) x.nIndex) + 1 If Machining.Active Then CurrentMachine.SetMachiningFilePath(ImportFilePath) ' leggo lavorazione da file di import NewMachining = New Machining(Machining.nIndex) ' cambio guid, indice e verifico nome NewMachining.sGUID = Guid.NewGuid.ToString() NewMachining.SetIndex(nIndex) If Machining.AlreadyExist Then If Machining.ChangeName Then NewMachining.sName = Machining.sName End If End If Map.refMachiningDbVM.MachiningList.Add(NewMachining) ' ripristino path corretta del Db CurrentMachine.SetMachiningFilePath(OrigFilePath) ' salvo la lavorazione NewMachining.Save() End If Next ' ripristino path corretta del Db CurrentMachine.SetMachiningFilePath(OrigFilePath) ' seleziono ultima importata If Not IsNothing(NewMachining) Then Map.refMachiningDbVM.SelMachining = NewMachining Map.refMachiningDbVM.NotifyPropertyChanged(NameOf(Map.refMachiningDbVM.SelMachining)) End If Case WindowModeEnum.EXPORT ' recupero le lavorazioni checkate Dim FinalNameList As New List(Of String) For Each Machining In MachiningList If Machining.Active Then FinalNameList.Add(Machining.sName) End If Next If FinalNameList.Count() = 0 Then Return Dim FinalNameArray = FinalNameList.ToArray() ' chiedo il nome con cui salvare il file .data Dim SaveFileDlg As New System.Windows.Forms.SaveFileDialog() With { .Title = EgtMsg(31451) & " " & EgtMsg(31452), .Filter = "File data (*.data)|*.data|Tutti i file (*.*)|*.*", .FileName = String.Empty } If SaveFileDlg.ShowDialog() <> System.Windows.Forms.DialogResult.OK Then Return Dim sFilePath As String = String.Empty sFilePath = SaveFileDlg.FileName ' se esiste già lo elimino If File.Exists(sFilePath) Then Try File.Delete(sFilePath) Catch ex As Exception End Try End If ' creo nuovo file If Not File.Exists(sFilePath) Then Try File.WriteAllLines(sFilePath, {"; Commento per evitare BOM con UTF-8"}) Catch ex As Exception End Try End If Dim ActiveMachiningList As List(Of Machining) = (From Machining In Map.refMachiningDbVM.MachiningList Select Machining Where m_MachiningList.Any(Function(x) x.sGUID.ToString() = Machining.sGUID AndAlso x.Active)).ToList() ' salvo tutte le lavorazioni sul Db For Index = 0 To ActiveMachiningList.Count - 1 ActiveMachiningList(Index).WriteParamsOnDb(Index + 1, sFilePath) Next End Select ' Chiusura finestra RaiseEvent m_CloseWindow(True) End Sub #End Region ' OkCommand #End Region ' Commands End Class Public Class ImpExpMachiningItem Inherits VMBase Public Const IMPEXTNAME As String = "_imp" ' Actions Friend Shared m_delEnableOkBtn As Action Private Shared m_Empty As New MachiningIndex(-1, Guid.Empty, "None") Private m_nIndex As Integer Public ReadOnly Property nIndex As Integer Get Return m_nIndex End Get End Property Private m_sGUID As Guid Public ReadOnly Property sGUID As Guid Get Return m_sGUID End Get End Property Private m_sOrigName As String Private m_sName As String Public ReadOnly Property sName As String Get Return m_sName End Get End Property Private m_Active As Boolean Public Property Active As Boolean Get Return m_Active End Get Set(value As Boolean) ' se esiste gia' chiedo se sovrascriverla If value Then If m_AlreadyExist Then Select Case System.Windows.MessageBox.Show(EgtMsg(31453), "", MessageBoxButton.YesNoCancel) Case MessageBoxResult.Yes m_ChangeName = False m_Active = True Case MessageBoxResult.No Dim bImpNameAlreadyInList = False Dim nImpNameIndex As Integer = 0 If Map.refMachiningDbVM.MachiningList.Any(Function(x) x.sName = m_sOrigName & IMPEXTNAME) Then nImpNameIndex += 1 While Map.refMachiningDbVM.MachiningList.Any(Function(x) x.sName = m_sOrigName & IMPEXTNAME & "_" & nImpNameIndex) nImpNameIndex += 1 End While End If m_sName = m_sOrigName & IMPEXTNAME & If(nImpNameIndex > 0, "_" & nImpNameIndex, "") System.Windows.MessageBox.Show("Machining will be imported with the name: " & m_sName, "", MessageBoxButton.OK) NotifyPropertyChanged(NameOf(sName)) m_ChangeName = True m_Active = True Case Else m_Active = False End Select NotifyPropertyChanged(NameOf(Active)) Else m_Active = True End If Else m_ChangeName = False m_Active = False m_sName = m_sOrigName NotifyPropertyChanged(NameOf(sName)) End If NotifyPropertyChanged(NameOf(Overwrite_Visibility)) If Not IsNothing(m_delEnableOkBtn) Then m_delEnableOkBtn() End Set End Property ' Parametro che indica se questo item da importare esiste gia' Private m_AlreadyExist As Boolean Friend ReadOnly Property AlreadyExist As Boolean Get Return m_AlreadyExist End Get End Property Private m_Overwrite_Visibility As Visibility = Visibility.Collapsed Public ReadOnly Property Overwrite_Visibility As Visibility Get Return If(m_Active AndAlso m_AlreadyExist AndAlso Not m_ChangeName, Visibility.Visible, Visibility.Collapsed) End Get End Property ' Parametro che indica se cambiare il nome di un item che esiste gia' o sovrascriverlo Private m_ChangeName As Boolean Friend Property ChangeName As Boolean Get Return m_ChangeName End Get Set(value As Boolean) m_ChangeName = value End Set End Property Sub New(nIndex As Integer, GUID As Guid, sName As String, AlreadyExist As Boolean) m_nIndex = nIndex m_sGUID = GUID m_sOrigName = sName m_sName = sName m_AlreadyExist = AlreadyExist m_Active = False End Sub Friend Shared Function Empty() As MachiningIndex Return m_Empty End Function End Class Public Class MachiningItem Inherits VMBase ' Actions Friend Shared m_delEnableOkBtn As Action Private m_Name As String Public Property Name As String Get If m_ChangeName Then Return m_Name & "_imp" Else Return m_Name End If End Get Set(value As String) m_Name = value End Set End Property Private m_Active As Boolean Public Property Active As Boolean Get Return m_Active End Get Set(value As Boolean) ' se esiste gia' chiedo se sovrascriverla If value Then If m_AlreadyExist Then Select Case System.Windows.MessageBox.Show(EgtMsg(31453), "", MessageBoxButton.YesNoCancel) Case MessageBoxResult.Yes m_ChangeName = False m_Active = True Case MessageBoxResult.No System.Windows.MessageBox.Show(EgtMsg(31454), "", MessageBoxButton.OK) m_ChangeName = True m_Active = True Case Else m_Active = False End Select NotifyPropertyChanged("Active") Else m_Active = True End If Else m_ChangeName = False m_Active = False End If If Not IsNothing(m_delEnableOkBtn) Then m_delEnableOkBtn() End Set End Property ' Parametro che indica se questo item da importare esiste gia' Private m_AlreadyExist As Boolean Friend ReadOnly Property AlreadyExist As Boolean Get Return m_AlreadyExist End Get End Property ' Parametro che indica se cambiare il nome di un item che esiste gia' o sovrascriverlo Private m_ChangeName As Boolean Friend Property ChangeName As Boolean Get Return m_ChangeName End Get Set(value As Boolean) m_ChangeName = value NotifyPropertyChanged("Name") End Set End Property Sub New(Name As String, AlreadyExist As Boolean) m_Name = Name m_AlreadyExist = AlreadyExist End Sub End Class