Files
EgtDOORProbe/ProbingPanel/ProbingPanelVM.vb
T
Emmanuele Sassi 7cb763affa EgtDOORProbe 1.9i4 :
- primo rilascio.
2018-09-28 18:15:37 +00:00

612 lines
21 KiB
VB.net

Imports System.Windows.Threading
Imports EgtWPFLib5
Imports System.IO
Public Class ProbingPanelVM
Inherits VMBase
Public Enum Properties As Integer
NULL = 0
SKINNED = 1
EXTRUDED = 2
NOTHING_ = 3
End Enum
' Path di EgtCAM5
Private m_EgtCAM5Path As String
Private m_CncFilePath As String
Private m_DataFilePath As String
Private m_CheckFileName As String
Private m_DataFileName As String
Private m_PartProgFileName As String
' Timer d'attesa
Private m_Timer As New DispatcherTimer
Private m_DDFPath As String
Public ReadOnly Property DDFPath As String
Get
Return m_DDFPath
End Get
End Property
Private m_DoorProperty As Properties
Public ReadOnly Property DoorProperty As Properties
Get
Return m_DoorProperty
End Get
End Property
Public ReadOnly Property DoorType As String
Get
Select Case m_DoorProperty
Case Properties.SKINNED
Return "Skinned"
Case Properties.EXTRUDED
Return "Extruded"
Case Properties.NOTHING_
Return "Nothing"
Case Else
Return ""
End Select
End Get
End Property
Private m_Width As String
Public Property Width As String
Get
Return m_Width
End Get
Set(value As String)
m_Width = value
End Set
End Property
Private m_Height As String
Public Property Height As String
Get
Return m_Height
End Get
Set(value As String)
m_Height = value
End Set
End Property
Private m_Thickness As String
Public Property Thickness As String
Get
Return m_Thickness
End Get
Set(value As String)
m_Thickness = value
End Set
End Property
Private m_Status As String
Public Property Status As String
Get
Return m_Status
End Get
Set(value As String)
m_Status = value
End Set
End Property
Public Property Progress_Value As Double
Get
Return m_Timer_Time / m_Timer_TimeOut * 100
End Get
Set(value As Double)
End Set
End Property
Private m_OpenDDF_IsEnabled As Boolean
Public Property OpenDDF_IsEnabled As Boolean
Get
Return m_OpenDDF_IsEnabled
End Get
Set(value As Boolean)
m_OpenDDF_IsEnabled = value
End Set
End Property
Private m_Generate_IsEnabled As Boolean
Public Property Generate_IsEnabled As Boolean
Get
Return m_Generate_IsEnabled
End Get
Set(value As Boolean)
m_Generate_IsEnabled = value
End Set
End Property
Private m_Timer_IsEnabled As Boolean = False
Public Property Timer_IsEnabled As Boolean
Get
Return m_Timer_IsEnabled
End Get
Set(value As Boolean)
m_Timer_IsEnabled = value
End Set
End Property
' Tempo per cui il timer resta in attesa, espresso in secondi
Private m_Timer_TimeOut As Integer
Private m_Timer_Time As Integer
' definizione comandi
Private m_cmdOpenDDF As ICommand
Private m_cmdCancel As ICommand
Private m_cmdGenerate As ICommand
#Region "CONSTRUCTOR"
Sub New()
' Leggo path EgtCAM5
Dim sTemp As String = ""
GetMainPrivateProfileString(S_GENERAL, K_EGTCAM5PATH, "", sTemp)
' verifico che esista il file d'esecuzione
If File.Exists(sTemp) Then
m_EgtCAM5Path = sTemp
Else
MessageBox.Show("Can't find EgtCAM5 in the configured path." & Environment.NewLine &
"Try to check in the Config.ini file." & Environment.NewLine &
"The program will close", "Error!", MessageBoxButton.OK)
End
End If
' Leggo path CNC File
GetMainPrivateProfileString(S_GENERAL, K_CNCFILEPATH, "", sTemp)
' verifico che esista il file d'esecuzione
If Directory.Exists(sTemp) Then
m_CncFilePath = sTemp
Else
MessageBox.Show("Can't find CNC file path." & Environment.NewLine &
"Try to check in the Config.ini file." & Environment.NewLine &
"The program will close", "Error!", MessageBoxButton.OK)
End
End If
' Leggo path Data File
GetMainPrivateProfileString(S_GENERAL, K_DATAFILEPATH, "", sTemp)
' verifico che esista il file d'esecuzione
If Directory.Exists(sTemp) Then
m_DataFilePath = sTemp
Else
MessageBox.Show("Can't find Data path." & Environment.NewLine &
"Try to check in the Config.ini file." & Environment.NewLine &
"The program will close", "Error!", MessageBoxButton.OK)
End
End If
' Leggo nome Part Program File
GetMainPrivateProfileString(S_GENERAL, K_PARTPROGRAMFILENAME, "", sTemp)
' verifico che non sia vuoto
If Not String.IsNullOrWhiteSpace(sTemp) Then
m_PartProgFileName = sTemp
Else
m_PartProgFileName = "Door.cnc"
End If
' Leggo nome Data File
GetMainPrivateProfileString(S_GENERAL, K_DATAFILENAME, "", sTemp)
' verifico che non sia vuoto
If Not String.IsNullOrWhiteSpace(sTemp) Then
m_DataFileName = sTemp
Else
m_DataFileName = "Data.dat"
End If
' Leggo nome Check File
GetMainPrivateProfileString(S_GENERAL, K_CHECKFILENAME, "", sTemp)
' verifico che non sia vuoto
If Not String.IsNullOrWhiteSpace(sTemp) Then
m_CheckFileName = sTemp
Else
m_CheckFileName = "PCHK.txt"
End If
' settaggi timer
AddHandler m_Timer.Tick, AddressOf WaitingTimer_Tick
m_Timer.Interval = TimeSpan.FromMilliseconds(1000)
' Leggo timeout
Dim nTemp As Integer = 0
GetMainPrivateProfileString(S_GENERAL, K_PROBINGTIMEOUT, "60", sTemp)
If Not String.IsNullOrWhiteSpace(sTemp) And Integer.TryParse(sTemp, nTemp) Then
m_Timer_TimeOut = nTemp
Else
m_Timer_TimeOut = 60
End If
' Attivo bottone apertura DDF
EnableDDFButton(True)
End Sub
#End Region ' CONSTRUCTOR
#Region "METHODS"
Private Sub SetStatus(sValue As String)
m_Status = sValue
NotifyPropertyChanged("Status")
End Sub
Private Sub ResetStatus()
m_Status = ""
NotifyPropertyChanged("Status")
End Sub
Private Sub WaitingTimer_Tick(sender As Object, e As System.EventArgs)
' incremento il contatore
m_Timer_Time += 1
NotifyPropertyChanged("Progress_Value")
' se trova il file di fine tastatura
Dim EndProbeFilePath As String = m_DataFilePath & m_CheckFileName
If File.Exists(EndProbeFilePath) Then
' fermo il timer
m_Timer.Stop()
m_Timer_Time = 0
m_Timer_IsEnabled = False
NotifyPropertyChanged("Progress_Value")
NotifyPropertyChanged("Timer_IsEnabled")
SetStatus("Probe completed")
' lancio calcolo porta da file tastatura
GenAfterProbe()
EnableDDFButton(True)
End If
' verifico che non sia finito il tempo di attesa
If m_Timer_Time >= m_Timer_TimeOut Then
m_Timer.Stop()
m_Timer_Time = 0
m_Timer_IsEnabled = False
NotifyPropertyChanged("Progress_Value")
NotifyPropertyChanged("Timer_IsEnabled")
SetStatus("Error in probing, can't find the probe file")
EnableDDFButton(True)
End If
End Sub
Private Sub GenAfterProbe()
' verifico se esiste il file dati del probe
Dim DataFile As String = m_DataFilePath & m_DataFileName
If Not File.Exists(DataFile) Then
SetStatus("Can't find the Probe Data File")
Return
End If
SetStatus("Start recalculating door from probe dimensions")
' copio file probe data in cartella ddf
Try
File.Copy(DataFile, Path.GetDirectoryName(m_DDFPath) & "\" & "Data.dat")
Catch ex As Exception
SetStatus("Error in copying the Probe Data File to the PC")
Return
End Try
' cancello file probe data sul CN
Try
File.Delete(DataFile)
Catch ex As Exception
SetStatus("Error in deleting the Probe Data File on the CNC" &
Environment.NewLine & "Please, delete it manually")
System.Threading.Thread.Sleep(2000)
End Try
' cancello file txt e cnc del ddf
Try
File.Delete(Path.ChangeExtension(m_DDFPath, ".txt"))
File.Delete(Path.ChangeExtension(m_DDFPath, ".cnc"))
Catch ex As Exception
End Try
' Lancio il programma in cieco per generazione porta ricalcolata
Dim GenFilePath As String = Path.ChangeExtension(m_DDFPath, ".cnc")
SetStatus("Generating the Part Program")
Try
Dim CommandLine As String = """" & m_DDFPath & """ " & "4"
Process.Start(m_EgtCAM5Path, CommandLine)
Dim bTxtOk As Boolean = False
For I = 0 To 150
' aspetto che venga generato il txt del ddf
If Not bTxtOk AndAlso File.Exists(Path.ChangeExtension(m_DDFPath, ".txt")) Then
bTxtOk = True
' pausa necessaria, altrimenti copia un file vuoto!
System.Threading.Thread.Sleep(1000)
End If
If bTxtOk AndAlso File.Exists(GenFilePath) Then
SetStatus("Part program correctly generated")
Exit Try
End If
' Aspetto e costringo ad aggiornare UI
System.Threading.Thread.Sleep(100)
UpdateUI()
Next
SetStatus("Error in Part Program generation!")
Return
Catch ex As Exception
SetStatus("Error in Part Program generation!")
Return
End Try
SetStatus("Copying the Part Program to the CNC")
' copio il programma sul CN
Try
Dim NcFilePath As String = m_CncFilePath & "\" &
m_PartProgFileName &
".cnc"
File.Delete(NcFilePath)
File.Copy(Path.ChangeExtension(m_DDFPath, ".cnc"), NcFilePath)
SetStatus("Part program copied to the CNC, activate it and start the machine")
Catch ex As Exception
SetStatus("Error in copying the Part Program to the CNC")
Return
End Try
End Sub
#End Region ' METHODS
#Region "COMMANDS"
#Region "OpenDDF"
' Returns a command that manage the MainWindow_Unloaded command
Public ReadOnly Property OpenDDF_Command() As ICommand
Get
If m_cmdOpenDDF Is Nothing Then
m_cmdOpenDDF = New Command(AddressOf OpenDDF)
End If
Return m_cmdOpenDDF
End Get
End Property
' Manage the MainWindow_Unloaded event. This method is invoked by the cmdMainWindow_Unloaded.
Public Sub OpenDDF(ByVal param As Object)
' verifico che non sia in corso una tastatura
If m_Timer.IsEnabled Then Return
Dim Path_IsValid As Boolean = False
' Direttorio corrente per file DDF
Dim sDir As String = String.Empty
GetMainPrivateProfileString(S_DOORS, K_DDFDIR, "", sDir)
' apro finestra di scelta file
Dim OpenFileDialogWnd As New Microsoft.Win32.OpenFileDialog With {
.Title = "Open DDF",
.Filter = "DDF File(*.ddf)|*.ddf",
.FilterIndex = 1,
.InitialDirectory = sDir}
If Not OpenFileDialogWnd.ShowDialog Then Return
' segno path ottenuta
Dim TempPath As String = OpenFileDialogWnd.FileName
' verifico di aver ricevuto una path valida
If File.Exists(TempPath) And Path.GetExtension(TempPath) = ".ddf" Then
m_DDFPath = TempPath
Path_IsValid = True
' segno path in ini file
WriteMainPrivateProfileString(S_DOORS, K_DDFDIR, System.IO.Path.GetDirectoryName(TempPath))
Else
m_DDFPath = String.Empty
Path_IsValid = False
End If
' se path valida
If Path_IsValid Then
' provo a leggere il file
Dim FileContent As String() = File.ReadAllLines(m_DDFPath)
If FileContent.Length = 0 Then
Map.refStatusBarVM.SetOutputMessage("Impossible to read this DDF file", MSG_TYPE.ERROR_)
Return
End If
' scansiono il contenuto del file
Dim bPropertiesFounded As Boolean = False
Dim bSizeFounded As Boolean = False
Dim bWidthFounded As Boolean = False
Dim bHeightFounded As Boolean = False
Dim bThicknessFounded As Boolean = False
For LineIndex As Integer = 0 To FileContent.Count - 1
Dim sFileLine As String = FileContent(LineIndex)
' se riga vuota, salto alla prossima
If String.IsNullOrWhiteSpace(sFileLine) Then Continue For
' se riga di commento, salto alla prossima
If sFileLine(0) = "#" Then Continue For
' elimino commento all'interno della riga
If sFileLine.Contains("#") Then
Strings.Left(sFileLine, InStr(sFileLine, "#"))
End If
' cerco la proprietà
If sFileLine.Contains("properties:") Then
If sFileLine.Contains("skinned") Then
m_DoorProperty = Properties.SKINNED
ElseIf sFileLine.Contains("extruded") Then
m_DoorProperty = Properties.EXTRUDED
Else
m_DoorProperty = Properties.NOTHING_
End If
bPropertiesFounded = True
End If
' cerco dimensioni porta
If Not bSizeFounded And sFileLine.Contains("size:") Then
bSizeFounded = True
Continue For
End If
If Not bThicknessFounded And bHeightFounded Then
bThicknessFounded = CheckDoorDimension(sFileLine, "thickness:", m_Thickness)
End If
If Not bHeightFounded And bWidthFounded Then
bHeightFounded = CheckDoorDimension(sFileLine, "height:", m_Height)
End If
If Not bWidthFounded And bSizeFounded Then
bWidthFounded = CheckDoorDimension(sFileLine, "width:", m_Width)
End If
Next
' se non ho trovato dimensioni, errore e disattivo interfaccia
If Not bSizeFounded Or Not bThicknessFounded Then
Map.refStatusBarVM.SetOutputMessage("Impossible to read this DDF file", MSG_TYPE.ERROR_)
m_Generate_IsEnabled = False
Return
Else
Map.refStatusBarVM.SetOutputMessage("DDF correctly opened", 5, MSG_TYPE.INFO)
m_Generate_IsEnabled = True
End If
' se non ho trovato opzioni, setto a NULL
If Not bPropertiesFounded Then m_DoorProperty = Properties.NOTHING_
Else
Map.refStatusBarVM.SetOutputMessage("Impossible to read this DDF file", MSG_TYPE.ERROR_)
' disabilito interfaccia
m_Generate_IsEnabled = False
End If
' resetto stato
ResetStatus()
' aggiorno valori interfaccia
NotifyPropertyChanged("Generate_IsEnabled")
NotifyPropertyChanged("DDFPath")
NotifyPropertyChanged("DoorType")
NotifyPropertyChanged("Width")
NotifyPropertyChanged("Height")
NotifyPropertyChanged("Thickness")
End Sub
Private Function CheckDoorDimension(sFileLine As String, sKey As String, ByRef sDimension As String) As Boolean
If sFileLine.Contains(sKey) Then
CleanValueString(sFileLine, sKey, sDimension)
Return True
Else
Map.refStatusBarVM.SetOutputMessage("Impossible to read this DDF file", MSG_TYPE.ERROR_)
Return False
End If
End Function
Private Sub CleanValueString(sFileLine As String, sKey As String, ByRef sResult As String)
Dim sTemp As String = sFileLine
sTemp = sTemp.Replace(sKey, "")
sTemp = sTemp.Trim()
sResult = sTemp
End Sub
#End Region ' OpenDDF
#Region "Generate"
' Returns a command that manage the MainWindow_Unloaded command
Public ReadOnly Property Generate_Command() As ICommand
Get
If m_cmdGenerate Is Nothing Then
m_cmdGenerate = New Command(AddressOf Generate)
End If
Return m_cmdGenerate
End Get
End Property
' Manage the MainWindow_Unloaded event. This method is invoked by the cmdMainWindow_Unloaded.
Public Sub Generate(ByVal param As Object)
' se nessuna proprietà impostata do errore ed esco
If m_DoorProperty = Properties.NULL Then
Map.refStatusBarVM.SetOutputMessage("Error, proprerty not readed!", MSG_TYPE.ERROR_)
Return
End If
' disabilito bottone apri DDF
EnableDDFButton(False)
' cancello file txt e cnc del ddf
Try
File.Delete(Path.ChangeExtension(m_DDFPath, ".txt"))
File.Delete(Path.ChangeExtension(m_DDFPath, ".cnc"))
Catch ex As Exception
End Try
' Inizio generazione primo PartProgram per entrambe le modalità
Dim bIsSkinned As Boolean = m_DoorProperty = Properties.SKINNED
Dim sProgramMsg As String = If(bIsSkinned, "Probing Part Program", "Part Program")
Dim GenFileExtension As String = If(bIsSkinned, ".prb", ".cnc")
Dim GenFilePath As String = Path.ChangeExtension(m_DDFPath, GenFileExtension)
SetStatus("Generating the " & sProgramMsg)
' Lancio il programma in cieco per generazione normale
Try
Dim CommandLine As String = """" & m_DDFPath & """ " & If(bIsSkinned, "3", "2")
Process.Start(m_EgtCAM5Path, CommandLine)
Dim bTxtOk As Boolean = False
For I = 0 To 150
' aspetto che venga generato il txt del ddf
If Not bTxtOk AndAlso File.Exists(Path.ChangeExtension(m_DDFPath, ".txt")) Then
bTxtOk = True
' pausa necessaria, altrimenti copia un file vuoto!
System.Threading.Thread.Sleep(1000)
End If
If bTxtOk AndAlso File.Exists(GenFilePath) Then
SetStatus(sProgramMsg & " correctly generated")
Exit Try
End If
' Aspetto e costringo ad aggiornare UI
System.Threading.Thread.Sleep(100)
UpdateUI()
Next
SetStatus("Error in " & sProgramMsg & " generation!")
EnableDDFButton(True)
Return
Catch ex As Exception
SetStatus("Error in " & sProgramMsg & " generation!")
EnableDDFButton(True)
Return
End Try
SetStatus("Copying the " & sProgramMsg & " to the CNC")
' copio il programma sul CN
Try
Dim NcFilePath As String = m_CncFilePath & "\" &
m_PartProgFileName &
GenFileExtension
File.Delete(NcFilePath)
File.Copy(Path.ChangeExtension(m_DDFPath, GenFileExtension), NcFilePath)
SetStatus(sProgramMsg & " copied to the CNC, activate it and start the machine")
Catch ex As Exception
SetStatus("Error in copying the " & sProgramMsg & " to the CNC")
EnableDDFButton(True)
Return
End Try
' se skinned
If m_DoorProperty = Properties.SKINNED Then
' cancello file di fine tastatura precedente
Dim EndProbeFilePath As String = m_DataFilePath & m_CheckFileName
Try
File.Delete(EndProbeFilePath)
Catch ex As Exception
SetStatus("Error in deleting the probe end file on the CNC" &
Environment.NewLine & "Please, delete it manually")
End Try
' avvio timer d'attesa
m_Timer.Start()
m_Timer_Time = 0
m_Timer_IsEnabled = True
NotifyPropertyChanged("Progress_Value")
NotifyPropertyChanged("Timer_IsEnabled")
Else
EnableDDFButton(True)
End If
End Sub
Private Sub EnableDDFButton(value As Boolean)
' abilito bottone apri DDF
m_OpenDDF_IsEnabled = value
NotifyPropertyChanged("OpenDDF_IsEnabled")
End Sub
#End Region ' Generate
#Region "Cancel"
' Returns a command that manage the MainWindow_Unloaded command
Public ReadOnly Property Cancel_Command() As ICommand
Get
If m_cmdCancel Is Nothing Then
m_cmdCancel = New Command(AddressOf Cancel)
End If
Return m_cmdCancel
End Get
End Property
' Manage the MainWindow_Unloaded event. This method is invoked by the cmdMainWindow_Unloaded.
Public Sub Cancel(ByVal param As Object)
' se timer non attivo esco
If Not m_Timer.IsEnabled Then Return
' ferma il timer di attesa
m_Timer.Stop()
m_Timer_Time = 0
m_Timer_IsEnabled = False
NotifyPropertyChanged("Progress_Value")
NotifyPropertyChanged("Timer_IsEnabled")
SetStatus("Probing waiting cycle manually interrupted")
' abilito bottone apri DDF
m_OpenDDF_IsEnabled = True
NotifyPropertyChanged("OpenDDF_IsEnabled")
End Sub
#End Region ' Cancel
#End Region ' COMMANDS
End Class