Imports System.Windows.Forms.Integration Imports System.Collections.ObjectModel Imports System.IO Imports EgtUILib Public Class MachiningDbWindowVM Inherits VMBase Friend Shared m_bActive As Boolean = False Friend Shared m_ProjectSceneContext As Integer = 0 Friend Shared m_sMachineIniPath As String = String.Empty Friend Shared m_MatType As MaterialType = Nothing Public Event m_CloseWindow(bDialogResult As Boolean) Public Property MatType As MaterialType Get Return m_MatType End Get Set(value As MaterialType) m_MatType = value End Set End Property Private m_Title As String Public ReadOnly Property Title As String Get Return EgtMsg(MSG_MAINWINDOW + 4) End Get End Property ' Lista delle lavorazioni Private m_MachiningsList As New ObservableCollection(Of FamilyMachiningTreeViewItem) Public Property MachiningsList As ObservableCollection(Of FamilyMachiningTreeViewItem) Get Return m_MachiningsList End Get Set(value As ObservableCollection(Of FamilyMachiningTreeViewItem)) m_MachiningsList = value End Set End Property Private m_IsEnabledNewBtn As Boolean Public ReadOnly Property IsEnabledNewBtn As Boolean Get Return m_IsEnabledNewBtn End Get End Property Private m_IsEnabledSaveBtn As Boolean Public ReadOnly Property IsEnabledSaveBtn As Boolean Get Return m_IsEnabledSaveBtn End Get End Property Private m_IsEnabledRemoveBtn As Boolean Public ReadOnly Property IsEnabledRemoveBtn As Boolean Get Return m_IsEnabledRemoveBtn End Get End Property Dim WjParamPageV As WaterjettingParamPageV Dim StoneSawParamPageV As StoneSawingParamPageV Dim StoneDrillParamPageV As StoneDrillingParamPageV Dim StoneMillParamPageV As StoneMillingParamPageV Dim StoneSawRoughParamPageV As StoneSawRoughingParamPageV Dim StoneSawFinishParamPageV As StoneSawFinishingParamPageV Dim WoodSawParamPageV As WoodSawingParamPageV Dim WoodDrillParamPageV As WoodDrillingParamPageV Dim WoodMillParamPageV As WoodMillingParamPageV Dim WoodSawRoughParamPageV As WoodSawRoughingParamPageV Dim WoodSawFinishParamPageV As WoodSawFinishingParamPageV Dim PocketParamPageV As PocketingParamPageV Dim MortiseParamPageV As MortisingParamPageV Dim ChiselParamPageV As ChiselingParamPageV Public ReadOnly Property ParamPageV As ContentControl Get Dim selMachiningFamily As FamilyMachiningTreeViewItem = Nothing Dim selMachining As MachiningTreeViewItem = Nothing For Each MachiningFamily In MachiningsList For Each Machining In MachiningFamily.Items If Machining.IsSelected Then selMachining = DirectCast(Machining, MachiningTreeViewItem) Exit For End If Next If Not IsNothing(selMachining) Then Exit For Next Select Case selMachining.Type Case MCH_MY.WATERJETTING WjParamPageV.DataContext = selMachining Return WjParamPageV Case MCH_MY.SAWING If m_MatType = MaterialType.STONE Then StoneSawParamPageV.DataContext = selMachining Return StoneSawParamPageV ElseIf m_MatType = MaterialType.WOOD Then WoodSawParamPageV.DataContext = selMachining Return WoodSawParamPageV End If Case MCH_MY.DRILLING If m_MatType = MaterialType.STONE Then StoneDrillParamPageV.DataContext = selMachining Return StoneDrillParamPageV ElseIf m_MatType = MaterialType.WOOD Then WoodDrillParamPageV.DataContext = selMachining Return WoodDrillParamPageV End If Case MCH_MY.MILLING If m_MatType = MaterialType.STONE Then StoneMillParamPageV.DataContext = selMachining Return StoneMillParamPageV ElseIf m_MatType = MaterialType.WOOD Then WoodMillParamPageV.DataContext = selMachining Return WoodMillParamPageV End If Case MCH_MY.SAWROUGHING 'If m_MatType = MaterialType.STONE Then StoneSawRoughParamPageV.DataContext = selMachining Return StoneSawRoughParamPageV 'ElseIf m_MatType = MaterialType.WOOD Then ' WoodSawRoughParamPageV.DataContext = selMachining ' Return WoodSawRoughParamPageV 'End If Case MCH_MY.SAWFINISHING 'If m_MatType = MaterialType.STONE Then StoneSawFinishParamPageV.DataContext = selMachining Return StoneSawFinishParamPageV 'ElseIf m_MatType = MaterialType.WOOD Then ' WoodSawFinishParamPageV.DataContext = selMachining ' Return WoodSawFinishParamPageV 'End If Case MCH_MY.POCKETING PocketParamPageV.DataContext = selMachining Return PocketParamPageV Case MCH_MY.MORTISING MortiseParamPageV.DataContext = selMachining Return MortiseParamPageV Case MCH_MY.CHISELING ChiselParamPageV.DataContext = selMachining Return ChiselParamPageV End Select Return Nothing End Get End Property ' Definizione comandi Private m_cmdNew As ICommand Private m_cmdSave As ICommand Private m_cmdRemove As ICommand Private m_cmdReloadMachining As ICommand Private m_cmdCloseMachiningsDb As ICommand #Region "CONSTRUCTOR" Sub New(sMachineIniPath As String, ProjectSceneContext As Integer) m_bActive = True m_ProjectSceneContext = ProjectSceneContext m_sMachineIniPath = sMachineIniPath Dim sMatType As String = String.Empty If EgtUILib.GetPrivateProfileString(S_GENERAL, K_MATERIAL, "Wood", sMatType, sMachineIniPath) <> 0 Then Select Case sMatType Case "Wood" m_MatType = MaterialType.WOOD Case "Stone" m_MatType = MaterialType.STONE Case Else ' Se il materiale spcificato nel file INI della macchina non corrisponde ai 2 sopracitati viene emesso un messaggio di errore m_MatType = Nothing MsgBox(EgtMsg(31409), MsgBoxStyle.Exclamation, EgtMsg(31551)) Exit Sub End Select End If ' Passo all'item della lista il delegato alla funzuione che permette di cancellare una lavorazione MachiningTreeViewItem.m_delRemoveMachining = AddressOf RemoveMachining ' Passo all'item della lista il delegato alla funzuione che permette di disattivare la lista delle lavorazioni MachiningTreeViewItem.m_delErrorOnMachining = AddressOf ErrorOnMachining ' Passo all'item della lista il delegato alla funzuione che permette di disattivare la lista delle lavorazioni MachiningTreeViewItem.m_delIsEnabledBtns = AddressOf IsEnabledBtns FamilyMachiningTreeViewItem.m_delIsEnabledBtns = AddressOf IsEnabledBtns MachiningTreeViewItem.m_delNotifyPropertyChanged = AddressOf ccc FamilyMachiningTreeViewItem.m_delNotifyPropertyChanged = AddressOf ccc ' Carico la lista delle lavorazioni della macchina selezionata LoadSelectedMachineMachinings() WjParamPageV = New WaterjettingParamPageV StoneSawParamPageV = New StoneSawingParamPageV StoneDrillParamPageV = New StoneDrillingParamPageV StoneMillParamPageV = New StoneMillingParamPageV StoneSawRoughParamPageV = New StoneSawRoughingParamPageV StoneSawFinishParamPageV = New StoneSawFinishingParamPageV WoodSawParamPageV = New WoodSawingParamPageV WoodDrillParamPageV = New WoodDrillingParamPageV WoodMillParamPageV = New WoodMillingParamPageV WoodSawRoughParamPageV = New WoodSawRoughingParamPageV WoodSawFinishParamPageV = New WoodSawFinishingParamPageV PocketParamPageV = New PocketingParamPageV MortiseParamPageV = New MortisingParamPageV ChiselParamPageV = New ChiselingParamPageV NotifyPropertyChanged("ParamPageV") End Sub #End Region ' Constructor #Region "METHODS" Private Sub ccc() NotifyPropertyChanged("ParamPageV") End Sub Private Sub IsEnabledBtns(bIsEnabledNewBtn As Boolean, bIsEnabledSaveBtn As Boolean, bIsEnabledDeleteBtn As Boolean) m_IsEnabledNewBtn = bIsEnabledNewBtn m_IsEnabledSaveBtn = bIsEnabledSaveBtn m_IsEnabledRemoveBtn = bIsEnabledDeleteBtn NotifyPropertyChanged("IsEnabledNewBtn") NotifyPropertyChanged("IsEnabledSaveBtn") NotifyPropertyChanged("IsEnabledRemoveBtn") End Sub ' Quando chiamato, disattiva tutti gli item dell'albero o li riattiva a seconda che ci sia o meno un errore Private Sub ErrorOnMachining(bIsEnabled As Boolean) For Each FamilyMachiningItem In MachiningsList FamilyMachiningItem.IsEnabled = Not bIsEnabled Next End Sub ''' ''' Method that search the machines in the correct folder and add to the MachinesList those valid. ''' Public Overridable Sub LoadSelectedMachineMachinings() EgtSetCurrentContext(m_ProjectSceneContext) Dim ActiveMachiningsTypes() As MachiningsType = MachineUtility.ReadMachiningFamilies(m_sMachineIniPath) For Each MachiningsType In ActiveMachiningsTypes Dim FamilyTreeView As New FamilyMachiningTreeViewItem(MachiningsType.Name, MachiningsType.Id, Nothing) MachiningsList.Add(FamilyTreeView) Dim MachiningName As String = String.Empty EgtMdbGetFirstMachining(MachiningsType.Id, MachiningName) While Not String.IsNullOrWhiteSpace(MachiningName) ' recupero tuuid EgtMdbSetCurrMachining(MachiningName) Dim sCurrMachTUUID As String = String.Empty EgtMdbGetCurrMachiningParam(MCH_MP.TUUID, sCurrMachTUUID) FamilyTreeView.Items.Add(New MachiningTreeViewItem(MachiningName, MachiningsType.Id, ToolDbUtility.IsToolInDb(sCurrMachTUUID), FamilyTreeView)) EgtMdbGetNextMachining(MachiningsType.Id, MachiningName) End While FamilyTreeView.VerifyMachiningValidity() Next ' Se esiste almeno una famiglia di lavorazioni, la seleziono If MachiningsList.Count > 0 Then MachiningsList(0).IsSelected = True MachiningsList(0).NotifyPropertyChanged("IsSelected") End If End Sub #End Region ' Methods #Region "COMMANDS" #Region "NewCommand" ''' ''' Returns a command that create a new machining. ''' Public ReadOnly Property NewCommand As ICommand Get If m_cmdNew Is Nothing Then m_cmdNew = New Command(AddressOf NewPar) End If Return m_cmdNew End Get End Property ''' ''' Creata the new machining. This method is invoked by the NewCommand. ''' Public Overridable Sub NewPar(ByVal param As Object) ' Verifico se sia selezionata una famiglia Dim NewMachiningItem As MachiningTreeViewItem If TypeOf param Is FamilyMachiningTreeViewItem Then Dim MachiningFamily As FamilyMachiningTreeViewItem = DirectCast(param, FamilyMachiningTreeViewItem) Dim NewName As String = MachiningFamily.Name EgtMdbGetMachiningNewName(NewName) If EgtMdbAddMachining(NewName, MachiningFamily.MachiningType) Then NewMachiningItem = New MachiningTreeViewItem(NewName, MachiningFamily.MachiningType, False, MachiningFamily) MachiningFamily.Items.Add(NewMachiningItem) EgtMdbSaveCurrMachining() NewMachiningItem.NewMachining = True If Not MachiningFamily.IsExpanded Then MachiningFamily.IsExpanded = True NewMachiningItem.IsSelected = True NewMachiningItem.NotifyPropertyChanged("IsSelected") End If ' Verifico se sia selezionata una lavorazione ElseIf TypeOf param Is MachiningTreeViewItem Then Dim MachiningCopied As MachiningTreeViewItem = DirectCast(param, MachiningTreeViewItem) Dim NewName As String = MachiningCopied.Name EgtMdbGetMachiningNewName(NewName) If EgtMdbCopyMachining(MachiningCopied.Name, NewName) Then Dim CurrType As Integer EgtMdbGetCurrMachiningParam(MCH_MP.TYPE, CurrType) For Each MachiningFamily In MachiningsList If (MachiningFamily.MachiningType And CurrType) <> 0 Then NewMachiningItem = New MachiningTreeViewItem(NewName, MachiningFamily.MachiningType, False, MachiningFamily) MachiningFamily.Items.Add(NewMachiningItem) EgtMdbSaveCurrMachining() NewMachiningItem.NewMachining = True ' Reimposto la lavorazione vecchia nel Db per deselezionarla e fare le verifiche correttamente EgtMdbSetCurrMachining(MachiningCopied.Name) MachiningCopied.IsSelected = False MachiningCopied.NotifyPropertyChanged("IsSelected") NewMachiningItem.IsSelected = True Exit For End If Next End If End If End Sub #End Region ' NewCommand #Region "SaveCommand" ''' ''' Returns a command that save the current selected machining. ''' Public ReadOnly Property SaveCommand() As ICommand Get If m_cmdSave Is Nothing Then m_cmdSave = New Command(AddressOf Save) End If Return m_cmdSave End Get End Property ''' ''' Saves the current machining. This method is invoked by the SaveCommand. ''' Public Sub Save(ByVal param As Object) Dim CurrMachining As MachiningTreeViewItem = DirectCast(param, MachiningTreeViewItem) CurrMachining.WriteMachiningParam() CurrMachining.m_Name = CurrMachining.m_Name.Trim() CurrMachining.WriteMachiningName() EgtMdbSaveCurrMachining() CurrMachining.NewMachining = False CurrMachining.IsModifiedReset() IsEnabledBtns(CurrMachining.IsValid And Not CurrMachining.IsModified, CurrMachining.IsValid, True) End Sub #End Region ' SaveCommand #Region "RemoveCommand" ''' ''' Returns a command that remove the current selected machining. ''' Public ReadOnly Property RemoveCommand() As ICommand Get If m_cmdRemove Is Nothing Then m_cmdRemove = New Command(AddressOf Remove) End If Return m_cmdRemove End Get End Property ''' ''' Remove the current selected machining from Db and TreeView. This method is invoked by the RemoveCommand. ''' Public Sub Remove(ByVal param As Object) EgtSetCurrentContext(m_ProjectSceneContext) Dim MachiningToRemove As MachiningTreeViewItem = DirectCast(param, MachiningTreeViewItem) ' Chiedo conferma cancellazione lavorazione If MessageBox.Show(EgtMsg(MSG_MACHININGDBERRORS + 5) & " " & MachiningToRemove.Name & EgtMsg(MSG_MACHININGDBERRORS + 6), EgtMsg(MSG_MACHININGDBERRORS + 7), MessageBoxButton.YesNo, MessageBoxImage.Question) <> MessageBoxResult.Yes Then Return End If RemoveMachining(MachiningToRemove) End Sub Public Sub RemoveMachining(MachiningToRemove As MachiningTreeViewItem) ' Salvo il tipo di lavorazione per poterla cancellare Dim MachiningType As Integer = MachiningToRemove.Type ' Cancello la lavorazione Dim CurrMachiningOriginalName As String = String.Empty EgtMdbGetMachiningFromUUID(MachiningToRemove.Uuid, CurrMachiningOriginalName) EgtMdbRemoveMachining(CurrMachiningOriginalName) ' Rimuovo il nome dall'albero For Each MachiningFamily In MachiningsList If (MachiningFamily.MachiningType And MachiningType) <> 0 Then MachiningFamily.Items.Remove(MachiningToRemove) End If Next ErrorOnMachining(False) End Sub #End Region ' RemoveCommand #Region "ReloadMachiningCommand" ''' ''' Returns a command that reload the current selected tool. ''' Public ReadOnly Property ReloadMachiningCommand() As ICommand Get If m_cmdReloadMachining Is Nothing Then m_cmdReloadMachining = New Command(AddressOf ReloadMachining) End If Return m_cmdReloadMachining End Get End Property ''' ''' Reload the current selected tools. This method is invoked by the RemoveCommand. ''' Public Sub ReloadMachining(ByVal param As Object) If TypeOf param Is FamilyMachiningTreeViewItem Then Return ' Verifico se sia selezionato un utensile ElseIf TypeOf param Is MachiningTreeViewItem Then Dim MachiningToReload As MachiningTreeViewItem = DirectCast(param, MachiningTreeViewItem) EgtSetCurrentContext(m_ProjectSceneContext) MachiningToReload.ReadMachiningParam() MachiningToReload.ReadMachiningName() MachiningToReload.IsModifiedReset() End If End Sub #End Region ' ReloadMachiningCommand #Region "CloseMachiningsDbCommand" ''' ''' Returns a command that remove the current selected machining. ''' Public ReadOnly Property CloseMachiningsDbCommand() As ICommand Get If m_cmdCloseMachiningsDb Is Nothing Then m_cmdCloseMachiningsDb = New Command(AddressOf CloseMachiningsDb) End If Return m_cmdCloseMachiningsDb End Get End Property ''' ''' Manage the MachiningDb closing. This method is invoked by the CloseMachiningDbCommand. ''' Public Sub CloseMachiningsDb(param As Object) m_bActive = False ' Se selezionata una lavorazione, verifico salvataggio If TypeOf param Is MachiningTreeViewItem Then Dim CurrMachining As MachiningTreeViewItem = DirectCast(param, MachiningTreeViewItem) If CurrMachining.IsValid Then CurrMachining.WriteMachiningParam() If EgtMdbIsCurrMachiningModified() Or CurrMachining.m_IsModifiedName Or CurrMachining.NewMachining Then Select Case MsgBox(EgtMsg(MSG_MACHININGDBERRORS + 1), MsgBoxStyle.YesNo, EgtMsg(MSG_MACHININGDBERRORS + 2)) Case MsgBoxResult.Yes CurrMachining.NewMachining = False CurrMachining.m_Name = CurrMachining.m_Name.Trim() CurrMachining.WriteMachiningName() EgtMdbSaveCurrMachining() Case MsgBoxResult.No If CurrMachining.NewMachining Then RemoveMachining(CurrMachining) ElseIf CurrMachining.m_IsModifiedName Then Dim DbName As String = String.Empty EgtMdbGetCurrMachiningParam(MCH_MP.NAME, DbName) CurrMachining.NamePar = DbName End If End Select End If Else MessageBox.Show("Utensile non valido, correggerlo o cancellarlo prima di uscire.", "ERRORE") Return End If End If ' Salvataggio DB lavorazioni EgtMdbSave() '' ''Application.Msn.NotifyColleagues(Application.UPDATEOPERATIONMACHININGLIST) ??? DA SISTEMARE PER EGTCAM5 ' Verifico se ci sono lavorazioni senza utensile o con utensile cancellato ' Chiusura finestra MachiningTreeViewItem.m_delRemoveMachining = Nothing MachiningTreeViewItem.m_delErrorOnMachining = Nothing MachiningTreeViewItem.m_delIsEnabledBtns = Nothing FamilyMachiningTreeViewItem.m_delIsEnabledBtns = Nothing RaiseEvent m_CloseWindow(True) End Sub #End Region ' CloseMachiningDbCommand #End Region ' Commands End Class