Files
EgtCAM5/MainWindow/MainWindowViewModel.vb
T
Emmanuele Sassi 0f32dde7db EgtCAM5 :
- Aggiunta voce SplitArcs nelle MachOptions.
- Aggiunto modulo con parametri utili della finestra Options.
- Piccola miglioria progetto selezione avanzata(scalo le linee sul piano di vista corrente).
2016-11-29 18:46:51 +00:00

510 lines
21 KiB
VB.net

Imports System.Collections.ObjectModel
Imports System.Threading
Imports System.Windows.Threading
Imports System.Runtime.InteropServices
Imports System.Math
Imports EgtUILib
Namespace EgtCAM5
Public Class MainWindowViewModel
Inherits ViewModelBase
#Region "FIELDS"
' EGALTECH ENVIRONMENT FIELDS
Private m_objMutex As New Mutex
'' '' '' '' '' ''Private m_nInstance As Integer = 0
Private m_sDataRoot As String = String.Empty
Private m_sConfigDir As String = String.Empty
Private m_nDebug As Integer = 0
Private m_ProcEventsCallback As New ProcessEventsCallback(AddressOf ProcessEvents)
Private m_OutTextCallback As New OutTextCallback(AddressOf OutText)
' EGALTECH ENVIRONMENT FIELDS WITH PROPERTY
' GRAPHICAL FIELDS
' Event commands
' MainWindow ContentRendered Event
Private m_Title As String = String.Empty
Public Property Title As String
Get
Return m_Title
End Get
Set(value As String)
If value <> m_Title Then
m_Title = value
OnPropertyChanged("Title")
End If
End Set
End Property
Private m_AboutBox As Window
Private bFirstAboutBox As Boolean = True
Public ReadOnly Property AboutBox As Window
Get
If bFirstAboutBox Then
m_AboutBox = New AboutBoxView
End If
Return New AboutBoxView
End Get
End Property
Private m_cmdMainWindow_ContentRendered As ICommand
' MainWindow Activated Event
Private m_cmdMainWindow_Activated As ICommand
' MainWindow Deactivated Event
Private m_cmdMainWindow_Deactivated As ICommand
' MainWindow Closing Event
Private m_cmdMainWindow_Closing As ICommand
Private m_cmdCloseApplication As ICommand
' GRAPHICAL ELEMENTS
Private m_TopCommandBar As TopCommandBarView
Public ReadOnly Property TopCommandBar As TopCommandBarView
Get
If IsNothing(m_TopCommandBar) Then
m_TopCommandBar = New TopCommandBarView
m_TopCommandBar.DataContext = New TopCommandBarViewModel
End If
Return m_TopCommandBar
End Get
End Property
' GRAPHICAL FIELDS WITH PROPERTY
Private m_StatusBar As StatusBarView
Private m_bfirst As Boolean = True
Public ReadOnly Property StatusBar As StatusBarView
Get
If m_bfirst Then
m_bfirst = False
m_StatusBar = New StatusBarView
m_StatusBar.DataContext = New StatusBarViewModel
End If
Return m_StatusBar
End Get
End Property
Private m_ProjectPage As ProjectView
Private m_bFirstProjectPage As Boolean = True
Public ReadOnly Property ProjectPage As ProjectView
Get
If m_bFirstProjectPage Then
m_bFirstProjectPage = False
m_ProjectPage = New ProjectView
m_ProjectPage.DataContext = New ProjectViewModel
End If
Return m_ProjectPage
End Get
End Property
#End Region
#Region "CONSTRUCTOR"
Sub New()
' INITIALIZE EGALTECH ENVIRONMENT
InitializeEgtEnvironment()
RegisterMyMessages()
AddHandler Application.Current.MainWindow.KeyDown, AddressOf MainWindow_KeyDown
' INITIALIZE GRAPHICS
'TabList.Add(New ProjectViewModel)
'TabList.Add(New ToolsDbViewModel)
'TabList.Add(New MachiningsDbViewModel)
'SelectedTab = TabList(0)
'OnPropertyChanged("SelectedTab")
End Sub
#End Region ' Constructor
#Region "COMMANDS"
#Region "CloseApplicationCommand"
''' <summary>
''' Returns a command that manage the MainWindow_Unloaded command
''' </summary>
Public ReadOnly Property CloseApplicationCommand() As ICommand
Get
If m_cmdCloseApplication Is Nothing Then
m_cmdCloseApplication = New RelayCommand(AddressOf CloseApplication, AddressOf CanCloseApplication)
End If
Return m_cmdCloseApplication
End Get
End Property
''' <summary>
''' Manage the MainWindow_Unloaded event. This method is invoked by the cmdMainWindow_Unloaded.
''' </summary>
Public Sub CloseApplication(ByVal param As Object)
'' Gestisco eventuale file corrente modificato
Application.Msn.NotifyColleagues(Application.MANAGEMODIFIED)
If m_allowWindowToClose Then
Application.Msn.NotifyColleagues(Application.CLOSEAPPLICATION)
' Salvo posizione Form (se non minimizzato)
If Application.Current.MainWindow.WindowState <> WindowState.Minimized Then
Dim WinPos As New WinPos
WindowToWinPos(Application.Current.MainWindow, WinPos)
WritePrivateProfileWinPos(S_GENERAL, K_WINPLACE, WinPos.nFlag, WinPos.nLeft, WinPos.nTop, WinPos.nWidth, WinPos.nHeight)
End If
' Terminazione generale di EgtInterface
EgtExit()
' Rilascio mutex
m_objMutex.Close()
' Aggiorno istanze usate
Dim nTmp As Integer = GetPrivateProfileInt(S_GENERAL, K_INSTANCES, 0)
nTmp -= (1 << (m_nInstance - 1))
WritePrivateProfileString(S_GENERAL, K_INSTANCES, nTmp.ToString())
' Salvo impostazione macchina corrente
Application.Msn.NotifyColleagues(Application.SAVECURRENTMACHINE)
'' Disabilito gestore Idle
'RemoveHandler Application.Idle, AddressOf Application_Idle
Application.Current.MainWindow.Close()
End If
End Sub
''' <summary>
''' Returns true if the application can be closed
''' </summary>
Private Function CanCloseApplication(ByVal param As Object) As Boolean
'' Impedisco uscita se script in esecuzione
'If m_bScriptRunning Then
' m_bStopScript = True
' E.Cancel = True
' Return
'End If
Return True
End Function
#End Region ' CloseApplicationCommand
#Region "cmdMainWindow_ContentRendered"
''' <summary>
''' Returns a command that manage the MainWindow_ContentRendered command
''' </summary>
Public ReadOnly Property cmdMainWindow_ContentRendered() As ICommand
Get
If m_cmdMainWindow_ContentRendered Is Nothing Then
m_cmdMainWindow_ContentRendered = New RelayCommand(AddressOf MainWindow_ContentRendered)
End If
Return m_cmdMainWindow_ContentRendered
End Get
End Property
''' <summary>
''' Manage the MainWindow_ContentRendered event. This method is invoked by the cmdMainWindow_ContentRendered.
''' </summary>
Public Sub MainWindow_ContentRendered(ByVal param As Object)
' Notify the ContentRendered event
Application.Msn.NotifyColleagues(Application.MAINWINDOW_CONTENTRENDERED)
End Sub
#End Region ' ContentRendered Command
#Region "cmdMainWindow_Activated"
''' <summary>
''' Returns a command that manage the MainWindow_ContentRendered command
''' </summary>
Public ReadOnly Property cmdMainWindow_Activated() As ICommand
Get
If m_cmdMainWindow_Activated Is Nothing Then
m_cmdMainWindow_Activated = New RelayCommand(AddressOf MainWindow_Activated)
End If
Return m_cmdMainWindow_Activated
End Get
End Property
''' <summary>
''' Manage the MainWindow_ContentRendered event. This method is invoked by the cmdMainWindow_ContentRendered.
''' </summary>
Public Sub MainWindow_Activated(ByVal param As Object)
' Notify the ContentRendered event
Application.Msn.NotifyColleagues(Application.MAINWINDOW_ACTIVATED)
End Sub
#End Region ' Activated Command
#Region "cmdMainWindow_Deactivated"
''' <summary>
''' Returns a command that manage the MainWindow_ContentRendered command
''' </summary>
Public ReadOnly Property cmdMainWindow_Deactivated() As ICommand
Get
If m_cmdMainWindow_Deactivated Is Nothing Then
m_cmdMainWindow_Deactivated = New RelayCommand(AddressOf MainWindow_Deactivated)
End If
Return m_cmdMainWindow_Deactivated
End Get
End Property
''' <summary>
''' Manage the MainWindow_ContentRendered event. This method is invoked by the cmdMainWindow_ContentRendered.
''' </summary>
Public Sub MainWindow_Deactivated(ByVal param As Object)
' Notify the ContentRendered event
Application.Msn.NotifyColleagues(Application.MAINWINDOW_DEACTIVATED)
End Sub
#End Region ' Deactivated Command
#End Region
#Region "METHODS"
Private Sub RegisterMyMessages()
Application.Msn.Register(Application.CLOSEAPPLICATIONCOMMAND, Sub()
Application.Msn.NotifyColleagues(Application.CLOSEAPPLICATION)
' Terminazione generale di EgtInterface
EgtExit()
' Rilascio mutex
m_objMutex.Close()
' Aggiorno istanze usate
Dim nTmp As Integer = GetPrivateProfileInt(S_GENERAL, K_INSTANCES, 0)
nTmp -= (1 << (m_nInstance - 1))
WritePrivateProfileString(S_GENERAL, K_INSTANCES, nTmp.ToString())
Application.Current.Shutdown()
End Sub)
Application.Msn.Register(Application.ALLOWWINDOWTOCLOSE, Sub(bBoolean As Boolean)
m_allowWindowToClose = bBoolean
End Sub)
Application.Msn.Register(Application.UPDATEMAINWINDOWTITLE, Sub(sString As String)
Title = sString
End Sub)
End Sub
''' <summary>
''' Method that initialize EgalTech environment
''' </summary>
Private Sub InitializeEgtEnvironment()
'' Abilito drag and drop
'Me.AllowDrop = True
'' Title
Application.Msn.NotifyColleagues(Application.EMITTITLE)
' Impostazione path radice per i dati
m_sDataRoot = System.AppDomain.CurrentDomain.BaseDirectory
If EgtUILib.GetPrivateProfileString(S_DATA, K_DATAROOT, "", m_sDataRoot, m_sDataRoot & "\" & DAT_FILE_NAME) = 0 Then
m_sDataRoot = System.AppDomain.CurrentDomain.BaseDirectory
End If
' Impostazione direttorio di configurazione
m_sConfigDir = m_sDataRoot & "\" & CONF_DIR
' Impostazione direttorio per file temporanei
m_sTempDir = m_sDataRoot & "\" & TEMP_DIR
' Impostazione path Ini file
m_sIniFile = m_sConfigDir & "\" & INI_FILE_NAME
' Impostazione direttorio per le macchine
If GetPrivateProfileString(S_MACH, K_MACHINESDIR, "", m_sMachinesRoot) = 0 Then
m_sMachinesRoot = m_sDataRoot & "\" & MACHINES_DFL_DIR
End If
IniFile.m_sMachinesRoot = m_sMachinesRoot
' Verifico indice di istanza
ManageIstance()
' Imposto tipo di chiave
EgtSetLockType(KEY_TYPE.HW)
' Leggo e imposto chiave di protezione
Dim sLicFileName As String = String.Empty
GetPrivateProfileString(S_GENERAL, K_LICENCE, LIC_FILE_NAME, sLicFileName)
Dim sLicFile As String = m_sConfigDir & "\" & sLicFileName
Dim sKey As String = String.Empty
EgtUILib.GetPrivateProfileString(S_LICENCE, K_KEY, "", sKey, sLicFile)
EgtSetKey(sKey)
' Recupero livello e opzioni della chiave
Dim bKey As Boolean = EgtGetKeyLevel(3279, 16, 1, IniFile.m_nKeyLevel) And
EgtGetKeyOptions(3279, 16, 1, IniFile.m_nKeyOptions)
' Inizializzazione generale di EgtInterface
m_nDebug = GetPrivateProfileInt(S_GENERAL, K_DEBUG, 0)
Dim sLogFile As String = m_sTempDir & "\" & GENLOG_FILE_NAME.Replace("#", m_nInstance.ToString())
Dim sLogMsg As String = "User " & Environment.UserName & "\" & Environment.MachineName & " (" & m_nInstance.ToString() & ")" & vbLf &
My.Application.Info.Description.ToString() & " ver. " &
My.Application.Info.Version.Major.ToString() &
"." & My.Application.Info.Version.Minor.ToString() &
(ChrW(97 - 1 + My.Application.Info.Version.Build)).ToString() &
My.Application.Info.Version.Revision.ToString()
EgtInit(m_nDebug, sLogFile, sLogMsg)
' Leggo direttorio dei messaggi (se manca uso direttorio di configurazione)
Dim sMsgDir As String = String.Empty
If GetPrivateProfileString(S_GENERAL, K_MESSAGESDIR, "", sMsgDir) = 0 Then
sMsgDir = m_sConfigDir
End If
' Leggo elenco lingue disponibili da file ini
Dim nIndex As Integer = 1
Dim ReadLanguage As Language = GetPrivateProfileLanguage(S_LANGUAGES, K_LANGUAGE & nIndex)
While Not IsNothing(ReadLanguage)
OptionModule.m_LanguageList.Add(ReadLanguage)
nIndex += 1
ReadLanguage = GetPrivateProfileLanguage(S_LANGUAGES, K_LANGUAGE & nIndex)
End While
' Inizializzo OptionModule
OptionModule.InitOptionModule()
' Leggo file messaggi
Dim sMsgName As String = String.Empty
GetPrivateProfileString(S_GENERAL, K_MESSAGES, "", sMsgName)
Dim sMsgFilePath As String = sMsgDir & "\EgalTechIta.txt"
For Each Language In OptionModule.m_LanguageList
If Language.Name = sMsgName Then
OptionModule.m_SelectedLanguage = Language
sMsgFilePath = sMsgDir & "\" & Language.FilePath
End If
Next
If Not EgtLoadMessages(sMsgFilePath) Then
EgtOutLog("Error in EgtLoadMessages")
End If
' Leggo e imposto livello utilizzatore
m_nUserLevel = Math.Min(m_nKeyLevel, GetPrivateProfileInt(S_GENERAL, K_USERLEVEL, 1))
' imposto dir font Nfe e font default
Dim sNfeDir As String = String.Empty
GetPrivateProfileString(S_GEOMDB, K_NFEFONTDIR, "", sNfeDir)
Dim sDefFont As String = String.Empty
GetPrivateProfileString(S_GEOMDB, K_DEFAULTFONT, "", sDefFont)
EgtSetFont(sNfeDir, sDefFont)
' imposto dir di default per libreria Lua e lancio libreria di base
Dim sLuaLibsDir As String = String.Empty
GetPrivateProfileString(S_LUA, K_LIBSDIR, "", sLuaLibsDir)
EgtSetLuaLibs(sLuaLibsDir)
Dim sLuaBaseLib As String = String.Empty
GetPrivateProfileString(S_LUA, K_BASELIB, "EgtBase", sLuaBaseLib)
EgtLuaRequire(sLuaBaseLib)
'' Impostazioni MruLists
m_MruFiles.Init(S_MRUFILES, 8)
m_MruScripts.Init(S_MRUSCRIPTS, 8)
If IniFile.IsActiveDoors() Then
m_MruDoors.Init(S_MRUDOORS, 8)
End If
If IniFile.IsActiveGunStock() Then
m_MruNewGunStock.Init(S_MRUGUNSTOCK, 8)
m_MruModifyGunStock.Init(S_MRUGUNSTOCK, 8)
End If
' Installo funzione gestione eventi per lua
EgtSetProcessEvents(m_ProcEventsCallback)
' Installo funzione output testo su status per lua
EgtSetOutText(m_OutTextCallback)
' Info su opzioni chiave
EgtOutLog("KeyOptions : " & bKey.ToString() & " " & IniFile.m_nKeyOptions.ToString())
End Sub
''' <summary>
''' Funzione che permette di gestire il numero di istanze del programma attive contemporaneamente
''' </summary>
Private Sub ManageIstance()
Dim bCreated As Boolean
Try
m_objMutex = New Mutex(False, "Global\EgtCAM5", bCreated)
Catch
bCreated = False
End Try
If bCreated Then
' Prima istanza
m_nInstance = 1
' Aggiorno stato istanze attive
WritePrivateProfileString(S_GENERAL, K_INSTANCES, m_nInstance.ToString)
Else
' Leggo il massimo numero di istanze ammesse
Dim nMaxInst As Integer = GetPrivateProfileInt(S_GENERAL, K_MAXINST, 1)
nMaxInst = Max(1, Min(nMaxInst, 32))
' Cerco il primo indice di istanza libero (max 32)
Dim nTmp As Integer = GetPrivateProfileInt(S_GENERAL, K_INSTANCES, 0)
m_nInstance = 1
Dim nMask As Integer = 1
While (nTmp And nMask) <> 0 And m_nInstance <= m_nInstance
m_nInstance += 1
nMask *= 2
End While
' Se l'indice supera il massimo
If m_nInstance > nMaxInst Then
' porto in primo piano la prima istanza
Dim bFound As Boolean = False
' processi del programma a 32 bit
Dim localProc As Process() = Process.GetProcessesByName("EgtCAM5R32")
For Each p As Process In localProc
If p.Id <> Process.GetCurrentProcess().Id Then
bFound = True
ShowWindow(p.MainWindowHandle, SW.SHOWMAXIMIZED)
Exit For
End If
Next
' se non trovati processi a 32 bit provo a 64 bit
If Not bFound Then
localProc = Process.GetProcessesByName("EgtCAM5R64")
For Each p As Process In localProc
If p.Id <> Process.GetCurrentProcess().Id Then
bFound = True
ShowWindow(p.MainWindowHandle, SW.SHOWMAXIMIZED)
Exit For
End If
Next
End If
' esco dal programma
End
End If
' Aggiorno stato istanze attive
nTmp += (1 << (m_nInstance - 1))
WritePrivateProfileString(S_GENERAL, K_INSTANCES, nTmp.ToString())
End If
End Sub
Public Function ProcessEvents(ByVal nProg As Integer, ByVal nPause As Integer) As Integer
' Se previsto, imposto progress
If nProg > 0 Then
Application.Msn.NotifyColleagues(Application.NOTIFYSTATUSPROGRESS, nProg)
End If
' Costringo ad aggiornare
Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, New Action(Sub()
End Sub))
' Eventuale attesa
Thread.Sleep(nPause)
' Ritorno eventuale stop
If m_bStopScript Then
m_bStopScript = False
Return 0
Else
Return 1
End If
End Function
Public Function OutText(ByRef psText As IntPtr) As Boolean
Application.Msn.NotifyColleagues(Application.NOTIFYSTATUSOUTPUT, Marshal.PtrToStringUni(psText))
Return True
End Function
#End Region
#Region "Events"
Private Sub MainWindow_KeyDown(ByVal sender As System.Object, ByVal e As KeyEventArgs)
' Con ESC esco dall'azione corrente
If e.Key = Key.Escape Then
' reset Azione corrente
Application.Msn.NotifyColleagues(Application.RESETSTATUS)
' reset Analisi e Distanza
Application.Msn.NotifyColleagues(Application.ANALYZE_ISCHECKED, False)
Application.Msn.NotifyColleagues(Application.GETDISTANCE_ISCHECKED, False)
' pulisco output
Application.Msn.NotifyColleagues(Application.NOTIFYSTATUSOUTPUT, "")
Application.Msn.NotifyColleagues(Application.RESETINPUTBOX)
End If
End Sub
#End Region
#Region "Verify closing"
''' <summary>
''' Should we let our window close?
''' </summary>
Private m_allowWindowToClose As Boolean = False
#End Region
End Class
End Namespace