Files
icarus/Icarus/MaterialDb/MaterialDbVM.vb
T
2023-05-26 10:21:46 +02:00

475 lines
17 KiB
VB.net

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)
Select Case EgtMessageBoxV.Show(Application.Current.MainWindow, "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
' verifico abilitazione delete
SetDeleteIsEnabled(Not SelMaterialIsOriginal())
NotifyPropertyChanged(NameOf(Delete_IsEnabled))
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
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))
NotifyPropertyChanged(NameOf(Delete_IsEnabled))
End Sub
Private m_Delete_IsEnabled As Boolean = True
Public ReadOnly Property Delete_IsEnabled As Boolean
Get
Return m_IsEnabled AndAlso m_Delete_IsEnabled
End Get
End Property
Friend Sub SetDeleteIsEnabled(value As Boolean)
If Map.refMainWindowVM.MainWindowM.nUserLevel >= 5 Then
m_Delete_IsEnabled = True
Else
m_Delete_IsEnabled = value
End If
NotifyPropertyChanged(NameOf(Delete_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
Public ReadOnly Property ImpExp_IsEnabled As Boolean
Get
Return m_IsEnabled AndAlso (IsNothing(m_SelMaterial) OrElse Not m_SelMaterial.bIsModified)
End Get
End Property
#Region "Tooltip"
Public ReadOnly Property Import_ToolTip As String
Get
Return "Import"
End Get
End Property
Public ReadOnly Property Export_ToolTip As String
Get
Return "Export"
End Get
End Property
Public ReadOnly Property EditName_ToolTip As String
Get
Return "Edit Name"
End Get
End Property
#End Region ' Tooltip
' Definizione comandi
Private m_cmdOk As ICommand
Private m_cmdCopy As ICommand
Private m_cmdSave As ICommand
Private m_cmdDelete As ICommand
Private m_cmdEditName As ICommand
Private m_cmdImport As ICommand
Private m_cmdExport 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)
If IsNothing(m_SelMaterial) AndAlso m_MaterialList.Count > 0 Then
m_SelMaterial = m_MaterialList(0)
End If
NotifyPropertyChanged(NameOf(SelMaterial))
' verifico abilitazione delete
If Not IsNothing(m_SelMaterial) Then
SetDeleteIsEnabled(Not SelMaterialIsOriginal())
NotifyPropertyChanged(NameOf(Delete_IsEnabled))
End If
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
Private Function SelMaterialIsOriginal() As Boolean
If IsNothing(m_SelMaterial) Then Return False
' verifico abilitazione delete
Dim General As MaterialCathegory = m_SelMaterial.CathegoryList.FirstOrDefault(Function(x) x.Type = MaterialCathegory.Cathegories.GENERAL)
If Not IsNothing(General) Then
Dim Original As MaterialParam = General.MaterialParamList.FirstOrDefault(Function(x) x.Type = MaterialParam.Params.ORIG)
If Not IsNothing(Original) Then
If DirectCast(Original, StringMaterialParam).sValue = ORIG_MATERIAL Then
Return True
End If
End If
End If
Return False
End Function
#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)
Select Case EgtMessageBoxV.Show(Application.Current.MainWindow, "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.sMaterialFilePath, ".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.sMaterialFilePath) Then
Try
' cambio estensione in bak a file Db vecchio
File.Move(CurrentMachine.sMaterialFilePath, sBakMatIniFilePath)
Catch ex As Exception
End Try
End If
' se ancora esiste lo elimino
If File.Exists(CurrentMachine.sMaterialFilePath) Then
Try
File.Delete(CurrentMachine.sMaterialFilePath)
Catch ex As Exception
End Try
End If
' creo nuovo file
If Not File.Exists(CurrentMachine.sMaterialFilePath) Then
Try
File.WriteAllLines(CurrentMachine.sMaterialFilePath, {"; 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
' ricarico lavorazioni per aggiorno liste materiali all'interno
Map.refMachiningDbVM.Init()
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()
Dim sCheckMessage As String = "Are you sure you want to delete selected material?"
' se materiale originale, esco
If SelMaterialIsOriginal() Then
If Map.refMainWindowVM.MainWindowM.nUserLevel >= 5 Then
'If MessageBox.Show("Trying to delete an Original Material! Are you sure you want to delete it?", "", MessageBoxButton.YesNo, MessageBoxImage.Warning) <> MessageBoxResult.Yes Then
If EgtMessageBoxV.Show(Application.Current.MainWindow, "Trying to delete an Original Material! Are you sure you want to delete it?", "", MessageBoxButton.YesNo, MessageBoxImage.Warning) <> MessageBoxResult.Yes Then
Return
End If
sCheckMessage = "Trying to delete an Original Material! Are you ABSOLUTELY sure you want to delete it?"
Else
'MessageBox.Show("Original material impossible to delete!")
EgtMessageBoxV.Show(Application.Current.MainWindow, "Original material impossible to delete!")
Return
End If
End If
' chiedo conferma
Select Case EgtMessageBoxV.Show(Application.Current.MainWindow, sCheckMessage, "Warning", MessageBoxButton.YesNo, MessageBoxImage.Warning)
'MessageBox.Show(sCheckMessage, "Warning", MessageBoxButton.YesNo, MessageBoxImage.Warning)
Case MessageBoxResult.Yes
m_MaterialList.Remove(m_SelMaterial)
SetIsModified(True)
If m_MaterialList.Count > 0 Then
SelMaterial = m_MaterialList(0)
NotifyPropertyChanged(NameOf(SelMaterial))
End If
' segno Db come modificato
Map.refMaterialDbVM.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_SelMaterial) Then Return
SetNameVisibility(True)
SetIsEnabled(False)
End Sub
#End Region ' EditName
#Region "Import"
Public ReadOnly Property Import_Command As ICommand
Get
If m_cmdImport Is Nothing Then
m_cmdImport = New Command(AddressOf Import)
End If
Return m_cmdImport
End Get
End Property
Public Sub Import()
' chiedo il nome del file .data da aprire
Dim OpenFileDlg As New System.Windows.Forms.OpenFileDialog() With {.Title = EgtMsg(31451) & " " & EgtMsg(31452),
.Filter = "Material file data (*" & ImportExportMachiningPanelVM.MaterialDataExtension & ")|*" & ImportExportMachiningPanelVM.MaterialDataExtension &
"|Original Material file data (*" & ImportExportMachiningPanelVM.OriginalMaterialDataExtension & ")|*" & ImportExportMachiningPanelVM.OriginalMaterialDataExtension &
"|All Material file data (*" & ImportExportMachiningPanelVM.MaterialDataExtension & "," & ImportExportMachiningPanelVM.OriginalMaterialDataExtension & ")|*" &
ImportExportMachiningPanelVM.MaterialDataExtension & ";*" & ImportExportMachiningPanelVM.OriginalMaterialDataExtension,
.FilterIndex = 3,
.FileName = String.Empty}
If OpenFileDlg.ShowDialog() <> System.Windows.Forms.DialogResult.OK Then Return
Dim ImportWindowVM As New ImportExportMachiningPanelVM(ImportExportMachiningPanelVM.WindowTypeEnum.MATERIAL, ImportExportMachiningPanelVM.WindowModeEnum.IMPORT, OpenFileDlg.FileName)
If ImportWindowVM.WindowMode <> ImportExportMachiningPanelVM.WindowModeEnum.IMPORT_ORIG Then
Dim ImportWindowV As New ImportExportMachiningPanelV(Application.Current.MainWindow, ImportWindowVM)
ImportWindowV.ShowDialog()
End If
End Sub
#End Region ' Import
#Region "Export"
Public ReadOnly Property Export_Command As ICommand
Get
If m_cmdExport Is Nothing Then
m_cmdExport = New Command(AddressOf Export)
End If
Return m_cmdExport
End Get
End Property
Public Sub Export()
Dim ExportMode As ImportExportMachiningPanelVM.WindowModeEnum = ImportExportMachiningPanelVM.WindowModeEnum.EXPORT
If Map.refMainWindowVM.MainWindowM.nUserLevel >= 5 AndAlso (Keyboard.Modifiers And ModifierKeys.Shift) = ModifierKeys.Shift Then
ExportMode = ImportExportMachiningPanelVM.WindowModeEnum.EXPORT_ORIG
End If
Dim ExportWindowVM As New ImportExportMachiningPanelVM(ImportExportMachiningPanelVM.WindowTypeEnum.MATERIAL, ExportMode)
Dim ExportWindowV As New ImportExportMachiningPanelV(Application.Current.MainWindow, ExportWindowVM)
ExportWindowV.ShowDialog()
End Sub
#End Region ' Export
#End Region ' COMMANDS
End Class