Imports System.Collections.ObjectModel Imports System.IO Imports EgtUILib Imports EgtWPFLib5 Public Class MachiningDbVM Inherits VMBase #Region "FIELDS & PROPERTIES" Private m_MachiningList As New ObservableCollection(Of Machining) Public ReadOnly Property MachiningList As ObservableCollection(Of Machining) Get Return m_MachiningList End Get End Property Private m_SelMachining As Machining Public Property SelMachining As Machining Get Return m_SelMachining End Get Set(value As Machining) Dim IsExpandedList As New List(Of Boolean) If Not IsNothing(m_SelMachining) Then ' verifico se modificato If m_SelMachining.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_SelMachining.Save() Case MessageBoxResult.No m_SelMachining.ResetModification() Case MessageBoxResult.Cancel NotifyPropertyChanged(NameOf(SelMachining)) Return End Select End If ' recupero stato IsExpanded di tutte le categorie For Each Cathegory In m_SelMachining.CathegoryList IsExpandedList.Add(Cathegory.Cathegory_IsExpanded) Next End If m_SelMachining = value ' ripristino stato IsExpanded di tutte le categorie If Not IsNothing(m_SelMachining) Then For Index = 0 To m_SelMachining.CathegoryList.Count - 1 If IsExpandedList.Count() >= Index + 1 Then m_SelMachining.CathegoryList(Index).Cathegory_IsExpanded = IsExpandedList(Index) End If 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 Private m_Combo_Visibility As Visibility = Visibility.Visible Public ReadOnly Property Combo_Visibility As Visibility Get Return m_Combo_Visibility End Get End Property ' variabile che indica se una qualunque lavorazione e' stata modificata Private m_bIsModified As Boolean = False 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 Private m_IsEnabled As Boolean = True Public ReadOnly Property IsEnabled As Boolean Get Return m_IsEnabled End Get End Property Friend Sub SetIsEnabled(value As Boolean) m_IsEnabled = value NotifyPropertyChanged(NameOf(IsEnabled)) End Sub Private m_UserShouldEditValueNow As Boolean = False Public Property UserShouldEditValueNow As Boolean Get Return m_UserShouldEditValueNow End Get Set(value As Boolean) m_UserShouldEditValueNow = value End Set End Property Friend Sub SetUserShouldEditValueNow() m_UserShouldEditValueNow = True NotifyPropertyChanged(NameOf(UserShouldEditValueNow)) m_UserShouldEditValueNow = False NotifyPropertyChanged(NameOf(UserShouldEditValueNow)) End Sub ' Definizione comandi Private m_cmdOk As ICommand Private m_cmdNew As ICommand Private m_cmdSave As ICommand Private m_cmdDelete As ICommand Private m_cmdEditName As ICommand #End Region ' FIELDS & PROPERTIES #Region "CONSTRUCTORS" Sub New() ' Creo riferimento a questa classe in EgtCAM5Map Map.SetRefMachiningDbVM(Me) End Sub #End Region ' CONSTRUCTORS #Region "METHODS" Friend Sub Init() MachiningList.Clear() Dim nIndex As Integer = 1 Dim sGUID As String = "" Dim sName As String = "" While ReadMachiningParamString(nIndex, MAC_GUID, "", sGUID) > 0 MachiningList.Add(New Machining(nIndex)) nIndex += 1 End While Dim sCurrMachining As String = "" GetMainPrivateProfileString(S_PRINTING3D, K_CURRMACHINING, "", sCurrMachining) m_SelMachining = MachiningList.FirstOrDefault(Function(x) x.sGUID = sCurrMachining) NotifyPropertyChanged(NameOf(SelMachining)) End Sub Friend Sub SetNameVisibility(bValue As Boolean) m_Name_Visibility = If(bValue, Visibility.Visible, Visibility.Collapsed) m_Combo_Visibility = If(Not bValue, Visibility.Visible, Visibility.Collapsed) NotifyPropertyChanged(NameOf(Name_Visibility)) NotifyPropertyChanged(NameOf(Combo_Visibility)) If bValue Then SetUserShouldEditValueNow() 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_SelMachining) AndAlso m_SelMachining.bIsModified Then ' chiedo se salvare Select Case MessageBox.Show("Do you want to save modified parameters?", "Warning", MessageBoxButton.YesNoCancel, MessageBoxImage.Warning) Case MessageBoxResult.Yes m_SelMachining.Save() Case MessageBoxResult.No m_SelMachining.ResetModification() Case MessageBoxResult.Cancel Return End Select End If If m_bIsModified Then Dim sBakMacIniFilePath As String = Path.ChangeExtension(CurrentMachine.sMachiningFilePath, ".bak") If File.Exists(sBakMacIniFilePath) Then Try ' elimino vecchio file bak File.Delete(sBakMacIniFilePath) Catch ex As Exception End Try End If If File.Exists(CurrentMachine.sMachiningFilePath) Then Try ' cambio estensione in bak a file Db vecchio File.Move(CurrentMachine.sMachiningFilePath, sBakMacIniFilePath) Catch ex As Exception End Try End If ' se ancora esiste lo elimino If File.Exists(CurrentMachine.sMachiningFilePath) Then Try File.Delete(CurrentMachine.sMachiningFilePath) Catch ex As Exception End Try End If ' creo nuovo file If Not File.Exists(CurrentMachine.sMachiningFilePath) Then Try File.WriteAllLines(CurrentMachine.sMachiningFilePath, {"; 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_MachiningList.Count - 1 m_MachiningList(Index).WriteParamsOnDb(Index + 1) Next m_bIsModified = False ' aggiorno combo top Dim CurrSelTopMachining As MachiningIndex = Map.refTopPanelVM.SelMachining Map.refTopPanelVM.InitMachiningsList() Map.refTopPanelVM.SetSelMachining( CurrSelTopMachining) End If ' ripristino modalita' standard Map.refTopPanelVM.SelPage = Pages.MODIFY End Sub #End Region ' Ok #Region "New" Public ReadOnly Property New_Command As ICommand Get If m_cmdNew Is Nothing Then m_cmdNew = New Command(AddressOf NewCmd) End If Return m_cmdNew End Get End Property Public Sub NewCmd() Dim nIndex As Integer = m_MachiningList.Max(Function(x) x.nIndex) + 1 Dim NewMachining As Machining = New Machining(nIndex) MachiningList.Add(NewMachining) m_SelMachining = NewMachining NotifyPropertyChanged(NameOf(SelMachining)) SetNameVisibility(True) End Sub #End Region ' New #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() If IsNothing( m_SelMachining) Then Return m_SelMachining.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() If IsNothing(m_SelMachining) Then Return ' chiedo conferma Select Case MessageBox.Show("Sei sicuro di voler cancellare la lavorazione selezionata?", "Warning", MessageBoxButton.YesNo, MessageBoxImage.Warning) Case MessageBoxResult.Yes m_MachiningList.Remove(m_SelMachining) SetIsModified(True) Case MessageBoxResult.No Return End Select End Sub #End Region ' Delete #Region "EditName" Public ReadOnly Property EditName_Command As ICommand Get If m_cmdEditName Is Nothing Then m_cmdEditName = New Command(AddressOf EditName) End If Return m_cmdEditName End Get End Property Public Sub EditName() If IsNothing(m_SelMachining) Then Return SetNameVisibility(True) SetIsEnabled(False) End Sub #End Region ' EditName #End Region ' COMMANDS End Class