Files
EgtCAM5/MainWindow/MainWindowVM.vb
T
Emmanuele Sassi a4b5cd4834 EgtCAM5 :
- Cambiati nomi classi e file.
2018-04-10 17:08:35 +00:00

493 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 MainWindowVM
Inherits ViewModelBase
#Region "FIELDS"
' EGALTECH ENVIRONMENT FIELDS
Private m_objMutex As Mutex
Private m_sDataRoot As String = String.Empty
Private m_sConfigDir As String = String.Empty
Private m_nDebug As Integer = 0
' 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_cmdMainWindow_ContentRendered As ICommand
Private m_cmdAboutBox 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 TopCommandBarV
Public ReadOnly Property TopCommandBar As TopCommandBarV
Get
If IsNothing(m_TopCommandBar) Then
m_TopCommandBar = New TopCommandBarV
m_TopCommandBar.DataContext = New TopCommandBarVM
End If
Return m_TopCommandBar
End Get
End Property
' GRAPHICAL FIELDS WITH PROPERTY
Private m_StatusBar As StatusBarV
Private m_bfirst As Boolean = True
Public ReadOnly Property StatusBar As StatusBarV
Get
If m_bfirst Then
m_bfirst = False
m_StatusBar = New StatusBarV
m_StatusBar.DataContext = New StatusBarVM
End If
Return m_StatusBar
End Get
End Property
Private m_ProjectPage As ProjectV
Private m_bFirstProjectPage As Boolean = True
Public ReadOnly Property ProjectPage As ProjectV
Get
If m_bFirstProjectPage Then
m_bFirstProjectPage = False
m_ProjectPage = New ProjectV
m_ProjectPage.DataContext = New ProjectVM
End If
Return m_ProjectPage
End Get
End Property
#End Region
#Region "CONSTRUCTOR"
Sub New()
' Inizializzo EgtCAM5Map
Map.BeginInit(Me)
' INITIALIZE EGALTECH ENVIRONMENT
InitializeEgtEnvironment()
RegisterMyMessages()
AddHandler Application.Current.MainWindow.KeyDown, AddressOf MainWindow_KeyDown
End Sub
#End Region ' Constructor
#Region "COMMANDS"
#Region "AboutBoxCommand"
' Returns a command that manage the MainWindow_Unloaded command
Public ReadOnly Property AboutBoxCommand() As ICommand
Get
If m_cmdAboutBox Is Nothing Then
m_cmdAboutBox = New Command(AddressOf AboutBox)
End If
Return m_cmdAboutBox
End Get
End Property
' Manage the MainWindow_Unloaded event. This method is invoked by the cmdMainWindow_Unloaded.
Public Sub AboutBox(ByVal param As Object)
Dim AboutBoxWindow As New AboutBoxWndV
AboutBoxWindow.Owner = Application.Current.MainWindow
AboutBoxWindow.ShowDialog()
End Sub
#End Region ' AboutBoxCommand
#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)
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)
If IniFile.m_bScriptRunning Then
If Not IniFile.m_bFailedRun Then
'MessageBox.Show("Can't exit now. Wait until the end of the script execution", "", MessageBoxButton.OK, MessageBoxImage.Stop)
Return
End If
End If
If IniFile.m_bSimulExecuting Then
' Impossibile uscire ora. Prima fermare la simulazione o aspettarne la fine!
MessageBox.Show(EgtMsg(MSG_SIMULATION + 13), "", MessageBoxButton.OK, MessageBoxImage.Exclamation)
Return
End If
' 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
If Not IsNothing(m_objMutex) Then m_objMutex.Close()
' Aggiorno istanze usate
Dim nTmp As Integer = GetPrivateProfileInt(S_GENERAL, K_INSTANCES, 0)
nTmp -= (1 << (IniFile.m_nInstance - 1))
WritePrivateProfileString(S_GENERAL, K_INSTANCES, nTmp.ToString())
' Salvo impostazione macchina corrente
Application.Msn.NotifyColleagues(Application.SAVECURRENTMACHINE)
' Chiudo la finestra principale del programma
Application.Current.MainWindow.Close()
End If
End Sub
#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)
' se sono in modalità solo cad
If IniFile.m_ProjectMode = ProjectModeOpt.ONLYDRAW Then
Application.Msn.NotifyColleagues(Application.NOTIFYSTATUSOUTPUT, "Impossible finding Machines dir. EgtCAM5 will run in CAD-ONLY mode.")
End If
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
If Not IsNothing(m_objMutex) Then m_objMutex.Close()
' Aggiorno istanze usate
Dim nTmp As Integer = GetPrivateProfileInt(S_GENERAL, K_INSTANCES, 0)
nTmp -= (1 << (IniFile.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()
' 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
IniFile.m_sDataRoot = m_sDataRoot
' Impostazione direttorio di configurazione
m_sConfigDir = m_sDataRoot & "\" & CONF_DIR
IniFile.m_sConfigDir = m_sConfigDir
' Impostazione direttorio per file temporanei
IniFile.m_sTempDir = m_sDataRoot & "\" & TEMP_DIR
' Impostazione path Ini file
IniFile.m_sIniFile = m_sConfigDir & "\" & INI_FILE_NAME
EgtWPFLib5.IniFile.m_sIniFile = m_sIniFile
' Impostazione path resources dir
IniFile.m_sResourcesRoot = m_sDataRoot & "\" & RES_DIR
' 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
ManageInstance()
' 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, 18, 1, IniFile.m_nKeyLevel) And
EgtGetKeyOptions(3279, 18, 1, IniFile.m_nKeyOptions)
' Inizializzazione generale di EgtInterface
m_nDebug = GetPrivateProfileInt(S_GENERAL, K_DEBUG, 0)
IniFile.m_sLogFile = m_sTempDir & "\" & GENLOG_FILE_NAME.Replace("#", IniFile.m_nInstance.ToString())
Dim sLogMsg As String = "User " & Environment.MachineName & "\" & Environment.UserName & " (" & IniFile.m_nInstance.ToString() & ")" & vbLf &
My.Application.Info.Title.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, IniFile.m_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)
' imposto IniFile a EgtInterface
EgtSetIniFile(m_sIniFile)
' verifico se avviare programma in modalità CAD-ONLY
IniFile.m_ProjectMode = If(GetPrivateProfileInt(S_GENERAL, K_ONLYDRAW, 0) = 0, ProjectModeOpt.DRAW, ProjectModeOpt.ONLYDRAW)
' 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_MRUGUNSTOCKMOD, 8)
m_MruModifyGunStock.Init(S_MRUGUNSTOCKPEZ, 8)
End If
' 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 ManageInstance()
Dim bCreated As Boolean
Try
m_objMutex = New Mutex(False, "Global\EgtCAM5", bCreated)
Catch
bCreated = False
End Try
If bCreated Then
' Prima istanza
IniFile.m_nInstance = 1
' Aggiorno stato istanze attive
WritePrivateProfileString(S_GENERAL, K_INSTANCES, IniFile.m_nInstance.ToString)
Else
' Leggo il massimo numero di istanze ammesse
Const MAX_INST As Integer = 32
Dim nMaxInst As Integer = GetPrivateProfileInt(S_GENERAL, K_MAXINST, 1)
nMaxInst = Max(1, Min(nMaxInst, MAX_INST))
' Cerco il primo indice di istanza libero (max 32)
Dim nTmp As Integer = GetPrivateProfileInt(S_GENERAL, K_INSTANCES, 0)
IniFile.m_nInstance = 1
Dim nMask As Integer = 1
While (nTmp And nMask) <> 0 And IniFile.m_nInstance < MAX_INST
IniFile.m_nInstance += 1
nMask *= 2
End While
' Se l'indice supera il massimo
If IniFile.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 << (IniFile.m_nInstance - 1))
WritePrivateProfileString(S_GENERAL, K_INSTANCES, nTmp.ToString())
End If
End Sub
#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