Imports System.Windows.Forms.Integration
Imports System.Collections.ObjectModel
Imports System.Reflection
Imports System.IO
Imports EgtUILib
Imports EgtWPFLib5
Namespace EgtCAM5
Public Class ToolsDbViewModel
Inherits TabViewModel
Private m_Title As String
Public ReadOnly Property Title As String
Get
Return EgtMsg(MSG_MAINWINDOW + 3)
End Get
End Property
' Lista degli utensili
Private m_ToolsList As New ObservableCollection(Of FamilyToolTreeViewItem)
Public Property ToolsList As ObservableCollection(Of FamilyToolTreeViewItem)
Get
Return m_ToolsList
End Get
Set(value As ObservableCollection(Of FamilyToolTreeViewItem))
m_ToolsList = value
End Set
End Property
'PROJECT PAGE'S SCENE FIELDS AND PROPERTIES
' Reference to the ProjectScene
Private WithEvents m_ToolScene As New Scene
' Reference to the ProjectSceneHost
Private SceneHost As WindowsFormsHost
' Property used to bind the scene to the WindowsFormsHost in XAML
Private m_bfirst As Boolean = True
Public ReadOnly Property ToolSceneHost As WindowsFormsHost
Get
If m_bfirst Then
SceneHost = New WindowsFormsHost() With {.Child = m_ToolScene}
If Not m_ToolScene.Init() Then
EgtOutLog("Error in Tool scene creation")
'Application.Msn.NotifyColleagues(Application.CLOSEAPPLICATION)
End If
IniFile.m_ToolsDbSceneContext = m_ToolScene.GetCtx()
' inibisco selezione diretta da Scene
m_ToolScene.SetStatusNull()
'OnPropertyChanged("SelectedMachine")
m_bfirst = False
End If
Return SceneHost
End Get
End Property
' Definizione comandi
Private m_cmdNew As ICommand
Private m_cmdSave As ICommand
Private m_cmdRemove As ICommand
#Region "MESSAGES"
'Definizione dei messaggi della pagina
Public ReadOnly Property CorrTxBl As String
Get
Return EgtMsg(MSG_TOOLSDBPAGE + 51)
End Get
End Property
Public ReadOnly Property ExitParTxBl As String
Get
Return EgtMsg(MSG_TOOLSDBPAGE + 52)
End Get
End Property
Public ReadOnly Property TypeTxBl As String
Get
Return EgtMsg(MSG_TOOLSDBPAGE + 53)
End Get
End Property
Public ReadOnly Property CoolantTxBl As String
Get
Return EgtMsg(MSG_TOOLSDBPAGE + 54)
End Get
End Property
Public ReadOnly Property CornRadTxBl As String
Get
Return EgtMsg(MSG_TOOLSDBPAGE + 55)
End Get
End Property
Public ReadOnly Property DiamTxBl As String
Get
Return EgtMsg(MSG_TOOLSDBPAGE + 56)
End Get
End Property
Public ReadOnly Property TotDiamTxBl As String
Get
Return EgtMsg(MSG_TOOLSDBPAGE + 57)
End Get
End Property
Public ReadOnly Property FeedTxBl As String
Get
Return EgtMsg(MSG_TOOLSDBPAGE + 58)
End Get
End Property
Public ReadOnly Property EndFeedTxBl As String
Get
Return EgtMsg(MSG_TOOLSDBPAGE + 59)
End Get
End Property
Public ReadOnly Property StartFeedTxBl As String
Get
Return EgtMsg(MSG_TOOLSDBPAGE + 60)
End Get
End Property
Public ReadOnly Property TipFeedTxBl As String
Get
Return EgtMsg(MSG_TOOLSDBPAGE + 61)
End Get
End Property
Public ReadOnly Property LenTxBl As String
Get
Return EgtMsg(MSG_TOOLSDBPAGE + 62)
End Get
End Property
Public ReadOnly Property TotLenTxBl As String
Get
Return EgtMsg(MSG_TOOLSDBPAGE + 63)
End Get
End Property
Public ReadOnly Property MaxMatTxBl As String
Get
Return EgtMsg(MSG_TOOLSDBPAGE + 64)
End Get
End Property
Public ReadOnly Property LonOffsetTxBl As String
Get
Return EgtMsg(MSG_TOOLSDBPAGE + 65)
End Get
End Property
Public ReadOnly Property RadOffsetTxBl As String
Get
Return EgtMsg(MSG_TOOLSDBPAGE + 66)
End Get
End Property
Public ReadOnly Property SpeedTxBl As String
Get
Return EgtMsg(MSG_TOOLSDBPAGE + 67)
End Get
End Property
Public ReadOnly Property SideAngTxBl As String
Get
Return EgtMsg(MSG_TOOLSDBPAGE + 68)
End Get
End Property
Public ReadOnly Property MaxSpeedTxBl As String
Get
Return EgtMsg(MSG_TOOLSDBPAGE + 69)
End Get
End Property
Public ReadOnly Property ThickTxBl As String
Get
Return EgtMsg(MSG_TOOLSDBPAGE + 70)
End Get
End Property
Public ReadOnly Property MaxAbsorptionTxBl As String
Get
Return EgtMsg(MSG_TOOLSDBPAGE + 71)
End Get
End Property
Public ReadOnly Property MinFeedTxBl As String
Get
Return EgtMsg(MSG_TOOLSDBPAGE + 72)
End Get
End Property
Public ReadOnly Property DrawTxBl As String
Get
Return EgtMsg(MSG_TOOLSDBPAGE + 73)
End Get
End Property
Public ReadOnly Property HeadTxBl As String
Get
Return EgtMsg(MSG_TOOLSDBPAGE + 74)
End Get
End Property
Public ReadOnly Property NameParTxBl As String
Get
Return EgtMsg(MSG_TOOLSDBPAGE + 75)
End Get
End Property
Public ReadOnly Property UserNotesTxBl As String
Get
Return EgtMsg(MSG_TOOLSDBPAGE + 76)
End Get
End Property
Public ReadOnly Property TcPosTxBl As String
Get
Return EgtMsg(MSG_TOOLSDBPAGE + 77)
End Get
End Property
#End Region
'''
''' Constructor
'''
Sub New()
' leggo direttorio toolmakers
GetPrivateProfileString(S_MACH, K_TOOLMAKERSDIR, "", IniFile.m_sToolMakersDir)
LoadSelectedMachineTools()
Application.Msn.Register(Application.REMOVETOOL, Sub(ToolToRemove As ToolTreeViewItem)
Remove(ToolToRemove)
End Sub)
' Quando chiamato, disattiva tutti gli item dell'albero o li riattiva a seconda che ci sia o meno un errore
Application.Msn.Register(Application.ERRORONTOOL, Sub(WrongTool As ToolTreeViewItem)
For Each FamilyToolItem In ToolsList
If IsNothing(WrongTool) Then
FamilyToolItem.IsEnabled = True
Else
FamilyToolItem.IsEnabled = False
End If
For Each ToolItem In FamilyToolItem.Items
If IsNothing(WrongTool) Then
ToolItem.IsEnabled = True
ElseIf ToolItem IsNot WrongTool Then
ToolItem.IsEnabled = False
Else
ToolItem.IsEnabled = True
End If
Next
Next
End Sub)
Application.Msn.Register(Application.UPDATESTATUSUNITS, Sub(bMmUnits As Boolean)
Dim ToolFounded As Boolean = False
For Each FamilyToolItem In ToolsList
If FamilyToolItem.IsSelected Then Exit For
For Each ToolItem In FamilyToolItem.Items
If ToolItem.IsSelected = True Then
ToolFounded = True
ToolItem.IsSelected = False
ToolItem.IsSelected = True
ToolItem.NotifyPropertyChanged("IsSelected")
End If
Next
If ToolFounded Then Exit For
Next
End Sub)
End Sub
'''
''' Method that search tools for the currently selected Machine and add it to the ToolsList.
'''
Private Sub LoadSelectedMachineTools()
Dim ActiveToolsFamilies() As ToolsFamily = ReadActiveToolsFamilies()
For Each ToolsFamily In ActiveToolsFamilies
Dim FamilyTreeView As New FamilyToolTreeViewItem(ToolsFamily.FamilyName, ToolsFamily.FamilyId)
ToolsList.Add(FamilyTreeView)
Dim nType As Integer = 0
Dim ToolName As String = String.Empty
EgtSetCurrentContext(IniFile.m_ProjectSceneContext)
If EgtTdbGetFirstTool(ToolsFamily.FamilyId, ToolName, nType) Then
FamilyTreeView.Items.Add(New ToolTreeViewItem(ToolName))
While EgtTdbGetNextTool(ToolsFamily.FamilyId, ToolName, nType)
FamilyTreeView.Items.Add(New ToolTreeViewItem(ToolName))
End While
End If
Next
' Se esiste almeno una famiglia di utensili, la seleziono
If ToolsList.Count > 0 Then
ToolsList(0).IsSelected = True
ToolsList(0).NotifyPropertyChanged("IsSelected")
End If
End Sub
'''
''' Method that search the machines in the correct folder and add to the MachinesList those valid.
'''
Private Function ReadActiveToolsFamilies() As ToolsFamily()
Dim ActiveToolsFamiliesList As New List(Of ToolsFamily)
If EgtUILib.GetPrivateProfileInt(S_TOOLS, K_DRILLBIT, 0, m_sDbsCurrMachIniFilePath) <> 0 Then
ActiveToolsFamiliesList.Add(New ToolsFamily With {.FamilyId = MCH_TF.DRILLBIT, .FamilyName = EgtMsg(MSG_TOOLSDBPAGE + 1)})
End If
If EgtUILib.GetPrivateProfileInt(S_TOOLS, K_SAWBLADE, 0, m_sDbsCurrMachIniFilePath) <> 0 Then
ActiveToolsFamiliesList.Add(New ToolsFamily With {.FamilyId = MCH_TF.SAWBLADE, .FamilyName = EgtMsg(MSG_TOOLSDBPAGE + 2)})
End If
If EgtUILib.GetPrivateProfileInt(S_TOOLS, K_MILL, 0, m_sDbsCurrMachIniFilePath) <> 0 Then
ActiveToolsFamiliesList.Add(New ToolsFamily With {.FamilyId = MCH_TF.MILL, .FamilyName = EgtMsg(MSG_TOOLSDBPAGE + 3)})
End If
If EgtUILib.GetPrivateProfileInt(S_TOOLS, K_MORTISE, 0, m_sDbsCurrMachIniFilePath) <> 0 Then
ActiveToolsFamiliesList.Add(New ToolsFamily With {.FamilyId = MCH_TF.MORTISE, .FamilyName = EgtMsg(MSG_TOOLSDBPAGE + 4)})
End If
If EgtUILib.GetPrivateProfileInt(S_TOOLS, K_COMPO, 0, m_sDbsCurrMachIniFilePath) <> 0 Then
ActiveToolsFamiliesList.Add(New ToolsFamily With {.FamilyId = MCH_TF.COMPO, .FamilyName = EgtMsg(MSG_TOOLSDBPAGE + 5)})
End If
Return ActiveToolsFamiliesList.ToArray
End Function
'''
''' Structure that represent a tool's family, containing family type and family name
'''
Structure ToolsFamily
Friend FamilyId As MCH_TF
Friend FamilyName As String
End Structure
#Region "NewCommand"
'''
''' Returns a command that create a new tool.
'''
Public ReadOnly Property NewCommand As ICommand
Get
If m_cmdNew Is Nothing Then
m_cmdNew = New RelayCommand(AddressOf NewPar, AddressOf CanNew)
End If
Return m_cmdNew
End Get
End Property
'''
''' Creata the new tool. This method is invoked by the NewCommand.
'''
Public Sub NewPar(ByVal param As Object)
EgtSetCurrentContext(IniFile.m_ProjectSceneContext)
' Verifico se sia selezionata una famiglia
Dim NewToolItem As ToolTreeViewItem
If TypeOf param Is FamilyToolTreeViewItem Then
Dim ToolFamily As FamilyToolTreeViewItem = DirectCast(param, FamilyToolTreeViewItem)
Dim NewName As String = ToolFamily.Name
EgtTdbGetToolNewName(NewName)
If EgtTdbAddTool(NewName, ToolFamily.ToolType) Then
NewToolItem = New ToolTreeViewItem(NewName)
ToolFamily.Items.Add(NewToolItem)
EgtTdbSaveCurrTool()
NewToolItem.NewTool = True
If Not ToolFamily.IsExpanded Then ToolFamily.IsExpanded = True
ToolFamily.IsSelected = False
ToolFamily.NotifyPropertyChanged("IsSelected")
NewToolItem.IsSelected = True
NewToolItem.NotifyPropertyChanged("IsSelected")
End If
' Verifico se sia selezionato un utensile
ElseIf TypeOf param Is ToolTreeViewItem Then
Dim ToolCopied As ToolTreeViewItem = DirectCast(param, ToolTreeViewItem)
Dim NewName As String = ToolCopied.Name
EgtTdbGetToolNewName(NewName)
If EgtTdbCopyTool(ToolCopied.Name, NewName) Then
' Elimino disegno che non deve essere copiato
EgtTdbSetCurrToolParam(MCH_TP.DRAW, String.Empty)
Dim CurrType As Integer
EgtTdbGetCurrToolParam(MCH_TP.TYPE, CurrType)
For Each ToolFamily In ToolsList
If (ToolFamily.ToolType And CurrType) <> 0 Then
NewToolItem = New ToolTreeViewItem(NewName)
ToolFamily.Items.Add(NewToolItem)
EgtTdbSaveCurrTool()
NewToolItem.NewTool = True
NewToolItem.IsSelected = True
NewToolItem.NotifyPropertyChanged("IsSelected")
Exit For
End If
Next
End If
End If
End Sub
'''
''' Returns true if the selected treeviewitem is valid for new tool creation.
'''
Private Function CanNew(ByVal param As Object) As Boolean
' Verifico se sia selezionata una famiglia
If TypeOf param Is FamilyToolTreeViewItem Then
Return True
' Verifico se sia selezionato un utensile
ElseIf TypeOf param Is ToolTreeViewItem Then
' Verifico che i parametri dell'utensile siano validi
Return DirectCast(param, ToolTreeViewItem).IsValid
Else
Return False
End If
End Function
#End Region ' NewCommand
#Region "SaveCommand"
'''
''' Returns a command that save the current selected tool.
'''
Public ReadOnly Property SaveCommand() As ICommand
Get
If m_cmdSave Is Nothing Then
m_cmdSave = New RelayCommand(AddressOf Save, AddressOf CanSave)
End If
Return m_cmdSave
End Get
End Property
'''
''' Saves the current tool. This method is invoked by the SaveCommand.
'''
Public Sub Save(ByVal param As Object)
Dim CurrTool As ToolTreeViewItem = DirectCast(param, ToolTreeViewItem)
CurrTool.m_Name = CurrTool.m_Name.Trim()
CurrTool.WriteToolParam()
If IsUUID(Path.GetFileNameWithoutExtension(CurrTool.m_Draw)) Then
CurrTool.SaveToolDraw()
End If
EgtTdbSaveCurrTool()
EgtTdbSave()
CurrTool.NewTool = False
End Sub
'''
''' Returns true if the selected tool is valid and can be saved.
'''
Private Function CanSave(ByVal param As Object) As Boolean
' Verifico che sia selezionato un utensile
If TypeOf param Is ToolTreeViewItem Then
Dim Tool As ToolTreeViewItem = DirectCast(param, ToolTreeViewItem)
' Verifico che i parametri dell'utensile siano validi
Return (Tool.IsValid And Tool.IsModified) Or Tool.NewTool
Else
Return False
End If
End Function
#End Region ' SaveCommand
#Region "RemoveCommand"
'''
''' Returns a command that remove the current selected tool.
'''
Public ReadOnly Property RemoveCommand() As ICommand
Get
If m_cmdRemove Is Nothing Then
m_cmdRemove = New RelayCommand(AddressOf Remove, AddressOf CanRemove)
End If
Return m_cmdRemove
End Get
End Property
'''
''' Remove the current selected tools from Db and TreeView. This method is invoked by the RemoveCommand.
'''
Public Sub Remove(ByVal param As Object)
Dim ToolToRemove As ToolTreeViewItem = DirectCast(param, ToolTreeViewItem)
EgtSetCurrentContext(IniFile.m_ProjectSceneContext)
' Cancello disegno dell'utensile
EraseToolDraw()
' Salvo il tipo di utensile per poterlo cancellare
Dim ToolType As Integer = ToolToRemove.Type
' Cancello l'utensile
EgtTdbRemoveTool(ToolToRemove.Name)
EgtTdbSave()
' Rimuovo il nome dell'albero
For Each ToolFamily In ToolsList
If (ToolFamily.ToolType And ToolType) <> 0 Then
ToolFamily.Items.Remove(ToolToRemove)
End If
Next
Application.Msn.NotifyColleagues(Application.ERRORONTOOL, Nothing)
End Sub
'''
''' Returns true if the selected tool can be removed.
'''
Private Function CanRemove(ByVal param As Object) As Boolean
' Verifico se sia selezionata una famiglia
If TypeOf param Is FamilyToolTreeViewItem Then
Return False
' Verifico se sia selezionato un utensile
ElseIf TypeOf param Is ToolTreeViewItem Then
' Verifico che i parametri dell'utensile siano validi
Return True
Else
Return False
End If
End Function
Private Function EraseToolDraw() As Boolean
' nome e direttorio del file da cancellare
EgtSetCurrentContext(IniFile.m_ProjectSceneContext)
Dim sDrawName As String = String.Empty
EgtTdbGetCurrToolParam(MCH_TP.DRAW, sDrawName)
Dim sPath As String = String.Empty
EgtTdbGetToolDir(sPath)
sPath = sPath & "\" & sDrawName
' se esiste lo cancello
Try
If My.Computer.FileSystem.FileExists(sPath) Then
My.Computer.FileSystem.DeleteFile(sPath)
End If
Return True
Catch ex As Exception
Return False
End Try
End Function
#End Region ' RemoveCommand
End Class
End Namespace