Imports System.Collections.ObjectModel
Imports System.Runtime.InteropServices
Imports System.IO
Imports EgtUILib
Imports System.Threading
Namespace EgtCAM5
Public Class StatusBarViewModel
Inherits ViewModelBase
#Region "FIELDS & PROPERTIES"
' Funzioni di callback per output in interfaccia da LUA
Private m_ProcEventsCallback As New ProcessEventsCallback(AddressOf ProcessEvents)
Private m_OutTextCallback As New OutTextCallback(AddressOf OutText)
' GRAPHICAL ELEMENTS
Private m_StatusOutput As String
Public Property StatusOutput As String
Get
Return m_StatusOutput
End Get
Set(value As String)
m_StatusOutput = value
OnPropertyChanged("StatusOutput")
End Set
End Property
Private m_StatusCurrPos As String
Public Property StatusCurrPos As String
Get
Return m_StatusCurrPos
End Get
Set(value As String)
m_StatusCurrPos = value
OnPropertyChanged("StatusCurrPos")
End Set
End Property
Private m_StatusProgress As Integer
Public Property StatusProgress As Integer
Get
Return m_StatusProgress
End Get
Set(value As Integer)
m_StatusProgress = value
OnPropertyChanged("StatusProgress")
End Set
End Property
Private m_StatusStopIsEnabled As Boolean
Public Property StatusStopIsEnabled As Boolean
Get
Return m_StatusStopIsEnabled
End Get
Set(value As Boolean)
m_StatusStopIsEnabled = value
OnPropertyChanged("StatusStopIsEnabled")
End Set
End Property
Private m_GridDimensionText As String
Public Property GridDimensionText As String
Get
Return m_GridDimensionText
End Get
Set(value As String)
m_GridDimensionText = value
OnPropertyChanged("GridDimensionText")
End Set
End Property
Private m_StatusGridText As String
Public Property StatusGridText As String
Get
Return m_StatusGridText
End Get
Set(value As String)
m_StatusGridText = value
OnPropertyChanged("StatusGridText")
End Set
End Property
Private m_CurrPosTypeText As String
Public Property CurrPosTypeText As String
Get
Return m_CurrPosTypeText
End Get
Set(value As String)
m_CurrPosTypeText = value
OnPropertyChanged("CurrPosTypeText")
End Set
End Property
Private m_StatusUnitsText As String
Public Property StatusUnitsText As String
Get
Return m_StatusUnitsText
End Get
Set(value As String)
m_StatusUnitsText = value
OnPropertyChanged("StatusUnitsText")
End Set
End Property
Private m_SnapPointTypeText As String
Public Property SnapPointTypeText As String
Get
Return m_SnapPointTypeText
End Get
Set(value As String)
m_SnapPointTypeText = value
OnPropertyChanged("SnapPointTypeText")
End Set
End Property
Private m_SnapPointTypeBackground As SolidColorBrush
Public Property SnapPointTypeBackground As SolidColorBrush
Get
Return m_SnapPointTypeBackground
End Get
Set(value As SolidColorBrush)
m_SnapPointTypeBackground = value
OnPropertyChanged("SnapPointTypeBackground")
End Set
End Property
' Lista delle macchine disponibili nel programma
Private m_MachinesList As New ObservableCollection(Of Machine)
Public Property MachinesList As ObservableCollection(Of Machine)
Get
Return m_MachinesList
End Get
Set(value As ObservableCollection(Of Machine))
m_MachinesList = value
End Set
End Property
' Macchina correntemente selezionata e quindi attiva
Private m_SelectedMachine As Machine
Public Property SelectedMachine As Machine
Get
Return m_SelectedMachine
End Get
Set(value As Machine)
If value IsNot m_SelectedMachine Then
EgtSetCurrentContext(IniFile.m_ProjectSceneContext)
If EgtSetCurrMachine(value.Name) Then
m_SelectedMachine = value
IniFile.m_sMachineName = m_SelectedMachine.Name
IniFile.m_sCurrMachIniFilePath = m_SelectedMachine.MachineDirPath & "\" & m_SelectedMachine.Name & ".ini"
IniFile.m_sCurrMachToolsDirPath = m_SelectedMachine.MachineDirPath & "\Tools"
IniFile.m_sCurrMachSetUpDirPath = m_SelectedMachine.MachineDirPath & "\SetUp"
IniFile.m_sCurrMachScriptsDirPath = m_SelectedMachine.MachineDirPath & "\Scripts"
UpdateToolAndMachDbParamVisibility()
OnPropertyChanged("SelectedMachine")
End If
End If
End Set
End Property
Private m_MachineListIsEnabled As Boolean
Public Property MachineListIsEnabled As Boolean
Get
Return m_MachineListIsEnabled
End Get
Set(value As Boolean)
m_MachineListIsEnabled = value
OnPropertyChanged("MachineListIsEnabled")
End Set
End Property
' Commands definition
Private m_cmdGridDimension As ICommand
Private m_cmdGridDimensionLostFocus As ICommand
Private m_cmdStatusGrid As ICommand
Private m_cmdCurrPosType As ICommand
Private m_cmdStatusUnits As ICommand
Private m_cmdStatusStop As ICommand
Private m_cmdClearStatusOutput As ICommand
#End Region ' Fields & Properties
#Region "CONSTRUCTOR"
Sub New()
' Creo riferimento a questa classe in EgtCAM5Map
EgtCAM5Map.SetRefStatusBarVM(Me)
Application.Msn.Register(Application.NOTIFYCURRPOS, Sub(sCursorPos As String)
StatusCurrPos = sCursorPos
End Sub)
Application.Msn.Register(Application.NOTIFYSTATUSOUTPUT, Sub(sStatusOutput As String)
StatusOutput = sStatusOutput
End Sub)
Application.Msn.Register(Application.NOTIFYSTATUSPROGRESS, Sub(sStatusProgress As Integer)
StatusProgress = sStatusProgress
End Sub)
Application.Msn.Register(Application.NOTIFYSTATUSSTOP, Sub(sStatusStop As Boolean)
StatusStopIsEnabled = sStatusStop
End Sub)
Application.Msn.Register(Application.UPDATESTATUSGRID, Sub(UpdateStatusGridParam As UpdateStatusGridParam)
UpdateStatusGrid(UpdateStatusGridParam.m_bShowGrid, UpdateStatusGridParam.m_bShowGridFrame)
End Sub)
Application.Msn.Register(Application.STATUSCURRPOSTYPETEXT, Sub(sString As String)
CurrPosTypeText = sString
End Sub)
Application.Msn.Register(Application.UPDATESTATUSUNITS, Sub(bMmUnits As Boolean)
StatusUnitsText = If(bMmUnits, "mm", "in")
EgtSetUiUnits(bMmUnits)
End Sub)
Application.Msn.Register(Application.STATUSSNAPPOINTTYPETEXT, Sub(sString As String)
SnapPointTypeText = sString
End Sub)
Application.Msn.Register(Application.STATUSSNAPPOINTTYPEBACKGROUND, Sub(Color As SolidColorBrush)
SnapPointTypeBackground = Color
End Sub)
SearchMachine()
Application.Msn.Register(Application.LOADCURRENTMACHINE, Sub()
If m_MachinesList.Count = 0 Then Return
Dim CurrMach As String = String.Empty
GetPrivateProfileString(S_MACH, K_CURRMACH, String.Empty, CurrMach)
Dim bFound As Boolean = False
If Not String.IsNullOrEmpty(CurrMach) Then
For Each Mach In MachinesList
If Mach.Name = CurrMach Then
bFound = True
SelectedMachine = Mach
Exit For
End If
Next
End If
If Not bFound And MachinesList.Count > 0 Then
SelectedMachine = MachinesList(0)
End If
End Sub)
Application.Msn.Register(Application.SAVECURRENTMACHINE, Sub()
If IsNothing(m_SelectedMachine) Then Return
WritePrivateProfileString(S_MACH, K_CURRMACH, SelectedMachine.Name)
End Sub)
Application.Msn.Register(Application.UPDATECURRENTMACHINE, Sub()
EgtSetCurrentContext(IniFile.m_ProjectSceneContext)
Dim sMachName As String = String.Empty
If EgtGetCurrMachineName(sMachName) Then
For Each Mach In MachinesList
If Mach.Name = sMachName Then
SelectedMachine = Mach
Exit For
End If
Next
End If
End Sub)
Application.Msn.Register(Application.MAINWINDOW_CONTENTRENDERED, Sub()
If IniFile.m_bMmUnits Then
GridDimensionText = LenToString(IniFile.dSnapStepMm, 4)
Else
GridDimensionText = LenToString(IniFile.dSnapStepInch, 4)
End If
If IniFile.m_ProjectMode = ProjectModeOpt.ONLYDRAW Then
MachineListIsEnabled = False
End If
End Sub)
Application.Msn.Register(Application.MACHININGMODE_ISCHECKED, Sub()
StatusOutput = String.Empty
MachineListIsEnabled = False
End Sub)
Application.Msn.Register(Application.DRAWMODE_ISCHECKED, Sub()
StatusOutput = String.Empty
MachineListIsEnabled = True
End Sub)
' Installo funzione gestione eventi per lua
EgtSetProcessEvents(m_ProcEventsCallback)
' Installo funzione output testo su status per lua
EgtSetOutText(m_OutTextCallback)
End Sub
#End Region ' Constructor
#Region "COMMANDS"
#Region "GridDimensionCommand"
'''
''' Returns a command that set grid status.
'''
Public ReadOnly Property GridDimensionCommand As ICommand
Get
If m_cmdGridDimension Is Nothing Then
m_cmdGridDimension = New RelayCommand(AddressOf GridDimension)
End If
Return m_cmdGridDimension
End Get
End Property
'''
''' Notify the function to set the status. This method is invoked by the StatusGridCommand.
'''
Public Sub GridDimension(ByVal param As Object)
If IniFile.m_bMmUnits Then
StringToLen(GridDimensionText, IniFile.dSnapStepMm)
EgtSetGridGeo(IniFile.dSnapStepMm, IniFile.nMinLineSStep, IniFile.nMajLineSStep, IniFile.nExtSStep)
Else
StringToLen(GridDimensionText, IniFile.dSnapStepInch)
EgtSetGridGeo(IniFile.dSnapStepInch, IniFile.nMinLineSStep, IniFile.nMajLineSStep, IniFile.nExtSStep)
End If
EgtSetGridColor(IniFile.MinLnColor, IniFile.MajLnColor)
Keyboard.ClearFocus()
EgtDraw()
End Sub
#End Region ' GridDimensionCommand
#Region "GridDimensionLostFocusCommand"
'''
''' Returns a command that set grid status.
'''
Public ReadOnly Property GridDimensionLostFocusCommand As ICommand
Get
If m_cmdGridDimensionLostFocus Is Nothing Then
m_cmdGridDimensionLostFocus = New RelayCommand(AddressOf GridDimensionLostFocus)
End If
Return m_cmdGridDimensionLostFocus
End Get
End Property
'''
''' Notify the function to set the status. This method is invoked by the StatusGridCommand.
'''
Public Sub GridDimensionLostFocus(ByVal param As Object)
If IniFile.m_bMmUnits Then
GridDimensionText = LenToString(IniFile.dSnapStepMm, 4)
Else
GridDimensionText = LenToString(IniFile.dSnapStepInch, 4)
End If
End Sub
#End Region ' GridDimensionLostFocusCommand
#Region "StatusGridCommand"
'''
''' Returns a command that set grid status.
'''
Public ReadOnly Property StatusGridCommand As ICommand
Get
If m_cmdStatusGrid Is Nothing Then
m_cmdStatusGrid = New RelayCommand(AddressOf StatusGrid)
End If
Return m_cmdStatusGrid
End Get
End Property
'''
''' Notify the function to set the status. This method is invoked by the StatusGridCommand.
'''
Public Sub StatusGrid(ByVal param As Object)
Dim bGridState As Boolean
If IniFile.m_ProjectMode = ProjectModeOpt.DRAW OrElse IniFile.m_ProjectMode = ProjectModeOpt.ONLYDRAW Then
IniFile.m_bDrawShowGrid = Not IniFile.m_bDrawShowGrid
bGridState = m_bDrawShowGrid
ElseIf IniFile.m_ProjectMode = ProjectModeOpt.MACHINING Then
IniFile.m_bMachiningShowGrid = Not IniFile.m_bMachiningShowGrid
bGridState = m_bMachiningShowGrid
End If
Application.Msn.NotifyColleagues(Application.UPDATESTATUSGRID, New UpdateStatusGridParam(bGridState, IniFile.m_bShowGridFrame))
EgtDraw()
End Sub
#End Region ' StatusGridCommand
#Region "CurrPosTypeCommand"
'''
''' Returns a command that choose the current reference.
'''
Public ReadOnly Property CurrPosTypeCommand As ICommand
Get
If m_cmdCurrPosType Is Nothing Then
m_cmdCurrPosType = New RelayCommand(AddressOf CurrPosType)
End If
Return m_cmdCurrPosType
End Get
End Property
'''
''' Execute the Point. This method is invoked by the CurrPosTypeCommand.
'''
Public Sub CurrPosType(ByVal param As Object)
Application.Msn.NotifyColleagues(Application.STATUSCURRPOSTYPECOMMAND)
End Sub
#End Region ' CurrPosTypeCommand
#Region "StatusUnitsCommand"
'''
''' Returns a command that choose the current reference.
'''
Public ReadOnly Property StatusUnitsCommand As ICommand
Get
If m_cmdStatusUnits Is Nothing Then
m_cmdStatusUnits = New RelayCommand(AddressOf StatusUnits)
End If
Return m_cmdStatusUnits
End Get
End Property
'''
''' Execute the Point. This method is invoked by the CurrPosTypeCommand.
'''
Public Sub StatusUnits(ByVal param As Object)
Application.Msn.NotifyColleagues(Application.STATUSUNITSCOMMAND)
If IniFile.m_bMmUnits Then
GridDimensionText = LenToString(IniFile.dSnapStepMm, 4)
Else
GridDimensionText = LenToString(IniFile.dSnapStepInch, 4)
End If
End Sub
#End Region ' StatusUnitsCommand
#Region "StatusStopCommand"
'''
''' Returns a command that choose the current reference.
'''
Public ReadOnly Property StatusStopCommand As ICommand
Get
If m_cmdStatusStop Is Nothing Then
m_cmdStatusStop = New RelayCommand(AddressOf StatusStop)
End If
Return m_cmdStatusStop
End Get
End Property
'''
''' Execute the Point. This method is invoked by the CurrPosTypeCommand.
'''
Public Sub StatusStop(ByVal param As Object)
m_bStopScript = True
End Sub
#End Region ' StatusStopCommand
#Region "ClearStatusOutputCommand"
'''
''' Returns a command that choose the current reference.
'''
Public ReadOnly Property ClearStatusOutputCommand As ICommand
Get
If m_cmdClearStatusOutput Is Nothing Then
m_cmdClearStatusOutput = New RelayCommand(AddressOf ClearStatusOutput)
End If
Return m_cmdClearStatusOutput
End Get
End Property
'''
''' Execute the Point. This method is invoked by the CurrPosTypeCommand.
'''
Public Sub ClearStatusOutput(ByVal param As Object)
StatusOutput = String.Empty
End Sub
#End Region ' ClearStatusOutputCommand
#End Region ' Commands
#Region "METHODS"
Private Sub UpdateStatusGrid(bShowGrid As Boolean, bShowGridFrame As Boolean)
'tsStatusGrid.ForeColor = If(m_bShowGrid, Color.Black, Color.Gray)
StatusGridText = If(bShowGrid, "GRID ON ", "GRID OFF")
EgtSetGridShow(bShowGrid, bShowGrid And bShowGridFrame)
End Sub
'''
''' Method that search the machines in the correct folder and add to the MachinesList those valid.
'''
Private Sub SearchMachine()
' Se direttorio base macchine non definito o non esiste, ritorno
If String.IsNullOrWhiteSpace(IniFile.m_sMachinesRoot) OrElse
Not Directory.Exists(IniFile.m_sMachinesRoot) OrElse Directory.GetDirectories(IniFile.m_sMachinesRoot).Count = 0 Then
IniFile.m_ProjectMode = ProjectModeOpt.ONLYDRAW
EgtOutLog(EgtMsg(MSG_STATUSBAR + 1))
Return
End If
' Cerco le macchine
Dim TempArray As String() = Directory.GetDirectories(IniFile.m_sMachinesRoot)
For i As Integer = 0 To TempArray.Count - 1
Dim MachinePathIni As String = TempArray(i) & "\" & Path.GetFileName(TempArray(i)) & ".ini"
If File.Exists(MachinePathIni) Then
m_MachinesList.Add(New Machine With {.Name = Path.GetFileName(TempArray(i)), .MachineDirPath = TempArray(i)})
End If
Next
If m_MachinesList.Count = 0 Then
IniFile.m_ProjectMode = ProjectModeOpt.ONLYDRAW
EgtOutLog(EgtMsg(MSG_STATUSBAR + 1))
Return
End If
End Sub
'''
''' Class that create the association Name/IniPath for the machine's
'''
Class Machine
Private m_sName As String
Public Property Name As String
Get
Return m_sName
End Get
Set(value As String)
m_sName = value
End Set
End Property
Friend MachineDirPath As String
End Class
Private Sub UpdateToolAndMachDbParamVisibility()
If EgtUILib.GetPrivateProfileInt(S_MACHININGS, K_SAWINGONARCS, 0, IniFile.m_sCurrMachIniFilePath) <> 0 Then
Sawing(39) = Visibility.Visible ' StepExtArc
Sawing(40) = Visibility.Visible ' StepIntArc
Else
Sawing(39) = Visibility.Collapsed ' StepExtArc
Sawing(40) = Visibility.Collapsed ' StepIntArc
End If
End Sub
Public Function OutText(ByRef psText As IntPtr) As Boolean
' Assegno stringa
StatusOutput = (Marshal.PtrToStringUni(psText))
' Costringo ad aggiornare
UpdateUI()
Return True
End Function
Private Function ProcessEvents(ByVal nProg As Integer, ByVal nPause As Integer) As Integer
' Se previsto, imposto progress
If nProg > 0 Then StatusProgress = Math.Min(nProg, 100)
' Notifico a simulazione aggiornamento dati
Application.Msn.NotifyColleagues(Application.SIMULATIONEXPANDER_UPDATE_CNCDATA)
' Costringo ad aggiornare
UpdateUI()
' Eventuale attesa
Thread.Sleep(nPause)
' Ritorno eventuale stop
If m_bStopScript Then
m_bStopScript = False
Return 0
Else
Return 1
End If
End Function
#End Region ' Methods
End Class
End Namespace