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 Public Event m_CloseWindow(bDialogResult As Boolean) Private m_Title As String Public ReadOnly Property Title As String Get Return EgtMsg(MSG_MAINWINDOW + 4) End Get End Property Public ReadOnly Property NewMsg As String Get Return EgtMsg(31701) End Get End Property Public ReadOnly Property SaveMsg As String Get Return EgtMsg(31702) End Get End Property Public ReadOnly Property RemoveMsg As String Get Return EgtMsg(31703) 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 StoneDrillParamPageV As StoneDrillingParamPageV Dim WoodDrillParamPageV As WoodDrillingParamPageV Dim StoneSawParamPageV As StoneSawingParamPageV Dim WoodSawParamPageV As WoodSawingParamPageV Dim StoneMillParamPageV As StoneMillingParamPageV Dim WoodMillParamPageV As WoodMillingParamPageV Dim BeamMillParamPageV As BeamMillingParamPageV Dim PocketParamPageV As PocketingParamPageV Dim MortiseParamPageV As MortisingParamPageV Dim StoneSawRoughParamPageV As StoneSawRoughingParamPageV Dim StoneSawFinishParamPageV As StoneSawFinishingParamPageV Dim ChiselParamPageV As ChiselingParamPageV Dim WjParamPageV As WaterjettingParamPageV 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 If IsNothing(selMachining) Then Return Nothing StoneDrillParamPageV.DataContext = Nothing WoodDrillParamPageV.DataContext = Nothing StoneSawParamPageV.DataContext = Nothing WoodSawParamPageV.DataContext = Nothing StoneMillParamPageV.DataContext = Nothing WoodMillParamPageV.DataContext = Nothing BeamMillParamPageV.DataContext = Nothing PocketParamPageV.DataContext = Nothing MortiseParamPageV.DataContext = Nothing StoneSawRoughParamPageV.DataContext = Nothing StoneSawFinishParamPageV.DataContext = Nothing ChiselParamPageV.DataContext = Nothing WjParamPageV.DataContext = Nothing Select Case selMachining.Type Case MCH_MY.DRILLING If MachiningTreeViewItem.m_MatType = MaterialType.STONE Then StoneDrillParamPageV.DataContext = selMachining Return StoneDrillParamPageV ElseIf MachiningTreeViewItem.m_MatType = MaterialType.WOOD Or MachiningTreeViewItem.m_MatType = MaterialType.BEAM Then WoodDrillParamPageV.DataContext = selMachining Return WoodDrillParamPageV End If Case MCH_MY.SAWING If MachiningTreeViewItem.m_MatType = MaterialType.STONE Then StoneSawParamPageV.DataContext = selMachining Return StoneSawParamPageV ElseIf MachiningTreeViewItem.m_MatType = MaterialType.WOOD Or MachiningTreeViewItem.m_MatType = MaterialType.BEAM Then WoodSawParamPageV.DataContext = selMachining Return WoodSawParamPageV End If Case MCH_MY.MILLING If MachiningTreeViewItem.m_MatType = MaterialType.STONE Then StoneMillParamPageV.DataContext = selMachining Return StoneMillParamPageV ElseIf MachiningTreeViewItem.m_MatType = MaterialType.WOOD Then WoodMillParamPageV.DataContext = selMachining Return WoodMillParamPageV ElseIf MachiningTreeViewItem.m_MatType = MaterialType.BEAM Then BeamMillParamPageV.DataContext = selMachining Return BeamMillParamPageV End If Case MCH_MY.POCKETING PocketParamPageV.DataContext = selMachining Return PocketParamPageV Case MCH_MY.MORTISING MortiseParamPageV.DataContext = selMachining Return MortiseParamPageV Case MCH_MY.SAWROUGHING StoneSawRoughParamPageV.DataContext = selMachining Return StoneSawRoughParamPageV Case MCH_MY.SAWFINISHING StoneSawFinishParamPageV.DataContext = selMachining Return StoneSawFinishParamPageV Case MCH_MY.CHISELING ChiselParamPageV.DataContext = selMachining Return ChiselParamPageV Case MCH_MY.WATERJETTING WjParamPageV.DataContext = selMachining Return WjParamPageV 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, Optional sMatType As String = "Wood") m_bActive = True m_ProjectSceneContext = ProjectSceneContext m_sMachineIniPath = sMachineIniPath EgtUILib.GetPrivateProfileString(S_GENERAL, K_MATERIAL, sMatType, sMatType, sMachineIniPath) Select Case sMatType Case "Stone" MachiningTreeViewItem.m_MatType = MaterialType.STONE Case "Wood" MachiningTreeViewItem.m_MatType = MaterialType.WOOD Case "Beam" MachiningTreeViewItem.m_MatType = MaterialType.BEAM Case Else ' Se il materiale spcificato nel file INI della macchina non corrisponde ai 2 sopracitati viene emesso un messaggio di errore MachiningTreeViewItem.m_MatType = Nothing MsgBox(EgtMsg(31409), MsgBoxStyle.Exclamation, EgtMsg(31551)) Exit Sub End Select ' 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() StoneDrillParamPageV = New StoneDrillingParamPageV WoodDrillParamPageV = New WoodDrillingParamPageV StoneSawParamPageV = New StoneSawingParamPageV WoodSawParamPageV = New WoodSawingParamPageV StoneMillParamPageV = New StoneMillingParamPageV WoodMillParamPageV = New WoodMillingParamPageV BeamMillParamPageV = New BeamMillingParamPageV PocketParamPageV = New PocketingParamPageV MortiseParamPageV = New MortisingParamPageV StoneSawRoughParamPageV = New StoneSawRoughingParamPageV StoneSawFinishParamPageV = New StoneSawFinishingParamPageV ChiselParamPageV = New ChiselingParamPageV WjParamPageV = New WaterjettingParamPageV 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") ' Se il Materiale della nuova Lav. è Stone setto Lato Testa e Lato Lavoro a Destra di default If NewMachiningItem.MatType = MaterialType.STONE Then NewMachiningItem.SelectedHeadSide = IdNameStruct.IndFromId(MCH_SAW_HS.RIGHT, NewMachiningItem.HeadSideList) NewMachiningItem.SelectedWorkSide = IdNameStruct.IndFromId(MCH_SAW_WS.RIGHT, NewMachiningItem.WorkSideList) End If 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 'Lavorazione non valida, correggerla o cancellarla prima di uscire. - Errore MessageBox.Show( EgtMsg( 31410), EgtMsg( 31126)) 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