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("Do you want to save material modification?", "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) If Not IsNothing(m_SelMaterial) Then For Each Cathegory In m_SelMaterial.CathegoryList IsExpandedList.Add(Cathegory.Cathegory_IsExpanded) Next End If m_SelMaterial = value ' ripristino stato IsExpanded di tutte le categorie If IsExpandedList.Count > 0 AndAlso Not IsNothing(value) Then For Index = 0 To m_SelMaterial.CathegoryList.Count - 1 m_SelMaterial.CathegoryList(Index).Cathegory_IsExpanded = IsExpandedList(Index) Next End If 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) End Sub #End Region ' CONSTRUCTORS #Region "METHODS" Friend Sub Init() MaterialList.Clear() Dim nIndex As Integer = 1 Dim sGUID As String = "" Dim sName As String = "" While ReadMaterialParamString(nIndex, MAT_GUID, "", sGUID) > 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) NotifyPropertyChanged(NameOf(SelMaterial)) 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 Not IsNothing(m_SelMaterial) AndAlso m_SelMaterial.bIsModified Then ' chiedo se salvare Select Case MessageBox.Show("Do you want to save material modification?", "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 sBakMatIniFilePath As String = Path.ChangeExtension(CurrentMachine.sMaterialsFilePath, ".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(CurrentMachine.sMaterialsFilePath) Then Try ' cambio estensione in bak a file Db vecchio File.Move(CurrentMachine.sMaterialsFilePath, sBakMatIniFilePath) Catch ex As Exception End Try End If ' se ancora esiste lo elimino If File.Exists(CurrentMachine.sMaterialsFilePath) Then Try File.Delete(CurrentMachine.sMaterialsFilePath) Catch ex As Exception End Try End If ' creo nuovo file If Not File.Exists(CurrentMachine.sMaterialsFilePath) Then Try File.WriteAllLines(CurrentMachine.sMaterialsFilePath, {"; 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 ' aggiorno lista materiali Db Init() ' aggiorno lista materiali TopBar Dim PrevMaterialGuid As String = Map.refTopPanelVM.SelMaterial.sGUID Map.refTopPanelVM.InitMaterialList() Dim PrevMaterial As MaterialIndex = Map.refTopPanelVM.MaterialList.FirstOrDefault(Function(x) x.sGUID = PrevMaterialGuid) If Not IsNothing(PrevMaterial) Then Map.refTopPanelVM.SetSelMaterial(PrevMaterial) Else Map.refTopPanelVM.SelMaterial = Nothing Map.refTopPanelVM.NotifyPropertyChanged(NameOf(Map.refTopPanelVM.SelMaterial)) End If 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) ' segno Db come modificato Map.refMaterialDbVM.SetIsModified(True) Case MessageBoxResult.No Return End Select End Sub #End Region ' Delete #End Region ' COMMANDS End Class