Imports System.Collections.ObjectModel Imports System.IO Imports EgtUILib Imports EgtWPFLib5 Public Class MaterialDbVM Inherits VMBase #Region "FIELDS & PROPERTIES" Private m_MaterialList As New ObservableCollection(Of Material) Public ReadOnly Property MaterialList As ObservableCollection(Of Material) Get Return m_MaterialList End Get End Property Private m_SelMaterial As Material Public Property SelMaterial As Material Get Return m_SelMaterial End Get Set(value As Material) ' verifico se modificato If Not IsNothing(m_SelMaterial) AndAlso m_SelMaterial.bIsModified Then ' chiedo se salvare Select Case MessageBox.Show("Salvare le modifiche apportate alla lavorazione selezionata?", "Info", MessageBoxButton.YesNoCancel, MessageBoxImage.Question) Case MessageBoxResult.Yes m_SelMaterial.Save() Case MessageBoxResult.No m_SelMaterial.ResetModification() Case MessageBoxResult.Cancel NotifyPropertyChanged(NameOf(SelMaterial)) Return End Select End If ' recupero stato IsExpanded di tutte le categorie Dim IsExpandedList As New List(Of Boolean) For Each Cathegory In m_SelMaterial.CathegoryList IsExpandedList.Add(Cathegory.Cathegory_IsExpanded) Next m_SelMaterial = value ' ripristino stato IsExpanded di tutte le categorie For Index = 0 To m_SelMaterial.CathegoryList.Count - 1 m_SelMaterial.CathegoryList(Index).Cathegory_IsExpanded = IsExpandedList(Index) Next End Set End Property Private m_Name_Visibility As Visibility = Visibility.Collapsed Public ReadOnly Property Name_Visibility As Visibility Get Return m_Name_Visibility End Get End Property Friend Sub SetNameVisibility(bValue As Boolean) m_Name_Visibility = If(bValue, Visibility.Visible, Visibility.Collapsed) NotifyPropertyChanged(NameOf(Name_Visibility)) End Sub ' variabile che indica se una qualunque lavorazione e' stata modificata Private m_bIsModified As Boolean Public ReadOnly Property bIsModified As Boolean Get Return m_bIsModified End Get End Property Friend Sub SetIsModified(value As Boolean) m_bIsModified = value End Sub ' Definizione comandi Private m_cmdOk As ICommand Private m_cmdCopy As ICommand Private m_cmdSave As ICommand Private m_cmdDelete As ICommand #End Region ' FIELDS & PROPERTIES #Region "CONSTRUCTORS" Sub New() ' Creo riferimento a questa classe in EgtCAM5Map Map.SetRefMaterialDbVM(Me) ' inizializzo lista lavorazioni Init() End Sub #End Region ' CONSTRUCTORS #Region "METHODS" Private Sub Init() Dim sMachiningIniFilePath As String = Map.refMainWindowVM.MainWindowM.sMaterialsDir & "\Materials.ini" MaterialList.Clear() Dim nIndex As Integer = 1 Dim sGUID As String = "" Dim sName As String = "" While GetPrivateProfileString(nIndex, MAT_GUID, "", sGUID, sMachiningIniFilePath) > 0 MaterialList.Add(New Material(nIndex)) nIndex += 1 End While Dim sCurrMaterial As String = "" GetMainPrivateProfileString(S_PRINTING3D, K_CURRMATERIAL, "", sCurrMaterial) m_SelMaterial = MaterialList.FirstOrDefault(Function(x) x.sGUID = sCurrMaterial) End Sub #End Region ' METHODS #Region "COMMANDS" #Region "Ok" Public ReadOnly Property Ok_Command As ICommand Get If m_cmdOk Is Nothing Then m_cmdOk = New Command(AddressOf Ok) End If Return m_cmdOk End Get End Property Public Sub Ok() If m_SelMaterial.bIsModified Then ' chiedo se salvare Select Case MessageBox.Show("Salvare le modifiche apportate al materiale selezionato?", "Warning", MessageBoxButton.YesNoCancel, MessageBoxImage.Warning) Case MessageBoxResult.Yes m_SelMaterial.Save() Case MessageBoxResult.No m_SelMaterial.ResetModification() Case MessageBoxResult.Cancel Return End Select End If If m_bIsModified Then Dim sMatIniFilePath As String = Map.refMainWindowVM.MainWindowM.sMaterialsDir & "/Materials.ini" Dim sBakMatIniFilePath As String = Path.ChangeExtension(sMatIniFilePath, ".bak") If File.Exists(sBakMatIniFilePath) Then Try ' cambio estensione in bak a file Db vecchio File.Delete(sBakMatIniFilePath) Catch ex As Exception End Try End If If File.Exists(sMatIniFilePath) Then Try ' cambio estensione in bak a file Db vecchio File.Move(sMatIniFilePath, sBakMatIniFilePath) Catch ex As Exception End Try End If ' se ancora esiste lo elimino If File.Exists(sMatIniFilePath) Then Try File.Delete(sMatIniFilePath) Catch ex As Exception End Try End If ' creo nuovo file If Not File.Exists(sMatIniFilePath) Then Try File.WriteAllLines(sMatIniFilePath, {"; Commento per evitare BOM con UTF-8"}) Catch ex As Exception End Try End If ' salvo tutte le lavorazioni sul Db For Index = 0 To m_MaterialList.Count - 1 m_MaterialList(Index).WriteParamsOnDb(Index + 1) Next m_bIsModified = False End If ' ripristino modalita' standard Map.refTopPanelVM.SelPage = Pages.MODIFY End Sub #End Region ' Ok #Region "Copy" Public ReadOnly Property Copy_Command As ICommand Get If m_cmdCopy Is Nothing Then m_cmdCopy = New Command(AddressOf Copy) End If Return m_cmdCopy End Get End Property Public Sub Copy() If IsNothing(m_SelMaterial) Then Return ' recupero stato IsExpanded di tutte le categorie Dim IsExpandedList As New List(Of Boolean) For Each Cathegory In m_SelMaterial.CathegoryList IsExpandedList.Add(Cathegory.Cathegory_IsExpanded) Next Dim NewMaterial As Material = New Material() MaterialList.Add(NewMaterial) m_SelMaterial = NewMaterial NotifyPropertyChanged(NameOf(SelMaterial)) ' ripristino stato IsExpanded di tutte le categorie For Index = 0 To m_SelMaterial.CathegoryList.Count - 1 m_SelMaterial.CathegoryList(Index).Cathegory_IsExpanded = IsExpandedList(Index) Next SetNameVisibility(True) End Sub #End Region ' Copy #Region "Save" Public ReadOnly Property Save_Command As ICommand Get If m_cmdSave Is Nothing Then m_cmdSave = New Command(AddressOf Save) End If Return m_cmdSave End Get End Property Public Sub Save() m_SelMaterial.Save() End Sub #End Region ' Save #Region "Delete" Public ReadOnly Property Delete_Command As ICommand Get If m_cmdDelete Is Nothing Then m_cmdDelete = New Command(AddressOf Delete) End If Return m_cmdDelete End Get End Property Public Sub Delete() ' chiedo conferma Select Case MessageBox.Show("Sei sicuro di voler cancellare la lavorazione selezionata?", "Warning", MessageBoxButton.YesNo, MessageBoxImage.Warning) Case MessageBoxResult.Yes m_MaterialList.Remove(m_SelMaterial) Case MessageBoxResult.No Return End Select End Sub #End Region ' Delete #End Region ' COMMANDS End Class