Does anyone know what code would be used to find out if a program is currently running in PC-Dmis? I wrote some code to automate the execution process and if I use it while a program is running, it closes the program and opens the new program while the original program continues to run. (Not sure for how long though. I stopped it after I tested it.)
VB code/ If a program is running
Collapse
X
-
I'm not sure if there is a specific command to get what you're looking for, but I use "ApplicationObjectEvents.OnStartExecution" and set a bool variable = true at the beginning of the program and then set it to false using "ApplicationObjectEvents.OnEndExecution" at the end.
Here's a link to object library: https://docs.hexagonmi.com/pcdmis/20...s_members.html
HTH
- Likes 1
-
I apologize. I'm fairly new to visual basic. Basically, you use those 2 objects to set variables inside of the VB script?
Could I, for example, write a line in VB that pulls the active program and set a variable to this program, then use that variable for these 2 object events so it knows which program to look for automatically? For this to happen, the script would also have to continuously run, correct? Do you have an example code I could use for reference on this? (Still learning.) I appreciate the help on this.
Comment
-
This is all tied to a form I created in Visual Studio. Right now, I am focusing on making is so this code will not work if a program is currently running. I have something I planned on trying today, which will require some googling, but I could have it done before tomorrow.
1) I figure I will need this form to always be open for the start and end execution event to trigger. I plan on changing the end of each button click so it doesn't close, but instead minimizes to the system tray.
2) I will assign a variable with a value of "Stopped" at the beginning of the class. Then I'll create a start and end event trigger sub that will change the value of this variable to "Started" or "Stopped" depending on the trigger.
3) The buttons would use the previous variable to only execute if the value is set to "Stopped" Which I'm assuming would render the buttons useless until the trigger changes the variable.
4) Lastly, I would like the .exe of the form to maximize the form from the system tray instead of opening another instance. I found the setting inside of Visual Studio that locks it to only one instance, but I haven't testing if double clicking the .exe will automatically maximize from the system tray or if that is something I need to program in.
I think if I do the above 4 things, this will be almost bug free. The only other bug I found is if the open dialog box is showing in PC-Dmis when I use this, the dialog box stays open when the script opens the program. (Which the operator can just hit cancel.) But would be nice to automatically hide that window if it is open.
You guys are awesome for helping with this.
Code:Public Class File_Explorer Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim PCDApp, PCDPartProgram, PCDPartPrograms PCDApp = CreateObject("PCDLRN.Application") PCDPartPrograms = PCDApp.PartPrograms 'If PC-DMIS is open, close current program If PCDApp.Visible = True Then PCDPartProgram = PCDApp.ActivePartProgram On Error Resume Next PCDPartProgram.Close() End If 'If PC-DMIS is not open, open operator mode If PCDApp.Visible = False Then PCDApp.OperatorMode = True PCDApp.Visible = True End If OpenFileDialog1.InitialDirectory = "ORIGINAL FILE DIRECTORY HERE" OpenFileDialog1.Title = "Find your Program" OpenFileDialog1.Filter = ".PRG files|*.prg" OpenFileDialog1.FileName = "" OpenFileDialog1.Multiselect = False If OpenFileDialog1.ShowDialog() = DialogResult.OK Then Dim sourceDir As String = OpenFileDialog1.FileName 'Sets 'sourceDir' to the location of the selected PRG file Dim sourceFileName As String = Dir(sourceDir) 'Extracts PRG file name from path Dim cadDir As String = IO.Path.ChangeExtension(sourceDir, ".CAD") 'Sets 'cadDir' to the location of the selected CAD file Dim cadFileName As String = Dir(cadDir) 'Extracts CAD file name from path Dim destDir As String = IO.Path.Combine("C:\CMM\", sourceFileName) 'Get PRG Destination Path Dim destCADDir As String = IO.Path.Combine("C:\CMM\", cadFileName) 'Get CAD Destination Path 'If the CAD file doesn't already exist in new directory, then copy the file If Not My.Computer.FileSystem.FileExists(destCADDir) Then 'Copy file IO.File.Copy(cadDir, destCADDir) End If 'If the PRG file doesn't already exist in new directory, then copy the file If Not My.Computer.FileSystem.FileExists(destDir) Then 'Copy file IO.File.Copy(sourceDir, destDir) End If 'If PC-DMIS is not open, open operator mode If PCDApp.Visible = False Then PCDApp.OperatorMode = True PCDApp.Visible = True End If 'Check if both PRG and CAD files exists. If they do, open the PRG. If not, wait 1 sec and loop Do If My.Computer.FileSystem.FileExists(destDir) And My.Computer.FileSystem.FileExists(destCADDir) Then PCDPartPrograms.Open(destDir, "CMM1") Exit Do Else Threading.Thread.Sleep(1000) End If Loop 'Closes the form Me.Close() End If End Sub Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click Dim PCDApp, PCDPartProgram, PCDPartPrograms PCDApp = CreateObject("PCDLRN.Application") PCDPartPrograms = PCDApp.PartPrograms 'If PC-DMIS is open, close current program If PCDApp.Visible = True Then PCDPartProgram = PCDApp.ActivePartProgram On Error Resume Next PCDPartProgram.Close() End If 'If PC-DMIS is not open, open operator mode If PCDApp.Visible = False Then PCDApp.OperatorMode = True PCDApp.Visible = True End If OpenFileDialog1.InitialDirectory = "ORIGINAL FILE DIRECTORY HERE" OpenFileDialog1.Title = "Find your Program" OpenFileDialog1.Filter = ".PRG files|*.prg" OpenFileDialog1.FileName = "" OpenFileDialog1.Multiselect = False If OpenFileDialog1.ShowDialog() = DialogResult.OK Then Dim sourceDir As String = OpenFileDialog1.FileName 'Sets 'sourceDir' to the location of the selected PRG file Dim sourceFileName As String = Dir(sourceDir) 'Extracts PRG file name from path Dim cadDir As String = IO.Path.ChangeExtension(sourceDir, ".CAD") 'Sets 'cadDir' to the location of the selected CAD file Dim cadFileName As String = Dir(cadDir) 'Extracts CAD file name from path Dim destDir As String = IO.Path.Combine("C:\CMM\", sourceFileName) 'Get PRG Destination Path Dim destCADDir As String = IO.Path.Combine("C:\CMM\", cadFileName) 'Get CAD Destination Path 'If the CAD file doesn't already exist in new directory, then copy the file If Not My.Computer.FileSystem.FileExists(destCADDir) Then 'Copy file IO.File.Copy(cadDir, destCADDir) End If 'If the PRG file doesn't already exist in new directory, then copy the file If Not My.Computer.FileSystem.FileExists(destDir) Then 'Copy file IO.File.Copy(sourceDir, destDir) End If 'If PC-DMIS is not open, open operator mode If PCDApp.Visible = False Then PCDApp.OperatorMode = True PCDApp.Visible = True End If 'Check if both PRG and CAD files exists. If they do, open the PRG. If not, wait 1 sec and loop Do If My.Computer.FileSystem.FileExists(destDir) And My.Computer.FileSystem.FileExists(destCADDir) Then PCDPartPrograms.Open(destDir, "CMM1") Exit Do Else Threading.Thread.Sleep(1000) End If Loop 'Closes the form Me.Close() End If End Sub End Class
Comment
-
Played with it a little but have to get back to something else... hope it's enough to get you unstuck.
Code:' Globals Private busy As Boolean = False Private complete As Boolean = True Private PCDApp As PCDLRN.Application = Nothing ' take advantage of intellisence by using this method of instantiating. Private PCDPartPrograms As PCDLRN.PartPrograms Private PCDPartProgram As PCDLRN.PartProgram = Nothing Private destDir As String = "" Dim destCADDir As String = "" Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 'Dim PCDApp, PCDPartProgram, PCDPartPrograms PCDApp = CreateObject("PCDLRN.Application") '<-- if Pcdmis is not running this will start it, if open then it'll only attach to it. PCDPartPrograms = PCDApp.PartPrograms 'If PC-DMIS is open, close current program If PCDApp.Visible = True Then ' <-- if Pcdmis is NOT hidden then PCDPartProgram = PCDApp.ActivePartProgram ' <-- attach exe to currently opened part program 'On Error Resume Next ' <-- it'll make you a better dev if you avoid these. 'PCDPartProgram.Close() ' you're closing current part program...why? End If 'If PC-DMIS is not open, open operator mode If PCDApp.Visible = False Then ' <-- if Pcdmis IS hidden then PCDApp.OperatorMode = True ' <-- switching modes (I've never tested this, so no input to give) PCDApp.Visible = True ' <-- making Pcdmis visible if hidden End If Dim OpenFileDialog1 As New OpenFileDialog() OpenFileDialog1.InitialDirectory = "ORIGINAL FILE DIRECTORY HERE" OpenFileDialog1.Title = "Find your Program" OpenFileDialog1.Filter = ".PRG files|*.prg" OpenFileDialog1.FileName = "" OpenFileDialog1.Multiselect = False If OpenFileDialog1.ShowDialog() = DialogResult.OK Then Dim sourceDir As String = OpenFileDialog1.FileName 'Sets 'sourceDir' to the location of the selected PRG file Dim sourceFileName As String = Dir(sourceDir) 'Extracts PRG file name from path Dim cadDir As String = IO.Path.ChangeExtension(sourceDir, ".CAD") 'Sets 'cadDir' to the location of the selected CAD file Dim cadFileName As String = Dir(cadDir) 'Extracts CAD file name from path destCADDir = IO.Path.Combine("C:\CMM\", cadFileName) 'Get CAD Destination Path destDir = IO.Path.Combine("C:\CMM\", sourceFileName) 'Get PRG Destination Path Dim t As Threading.Thread If Not busy Then t = New Threading.Thread(AddressOf MyThread) End If 'If the CAD file doesn't already exist in new directory, then copy the file If Not My.Computer.FileSystem.FileExists(destCADDir) Then 'Copy file 'IO.File.Copy(cadDir, destCADDir) End If 'If the PRG file doesn't already exist in new directory, then copy the file If Not My.Computer.FileSystem.FileExists(destDir) Then 'Copy file 'IO.File.Copy(sourceDir, destDir) End If 'If PC-DMIS is not open, open operator mode If PCDApp.Visible = False Then PCDApp.OperatorMode = True PCDApp.Visible = True End If ' Start FileIO thread If Not busy Then busy = True t.Start() End If 'Closes the form 'Me.Close() ' you want to hide maybe? 'Me.Hide() End If End Sub Private Sub MyThread() If busy And complete Then complete = False 'Check if both PRG and CAD files exists. If they do, open the PRG. If not, wait 1 sec and loop While busy If My.Computer.FileSystem.FileExists(destDir) And My.Computer.FileSystem.FileExists(destCADDir) Then PCDPartPrograms.Open(destDir, "CMM1") busy = False complete = True 'Me.Show() ' causes a crss thread error FYI Exit While Else Threading.Thread.Sleep(1000) End If End While End If End Sub
PS why not extract directory paths from selected files? That might, also, be what's causing your app to open the 'wrong' file, maybe?Last edited by Kp61dude!; 02-18-2020, 05:54 PM.PcDmis 2015.1 SP10 CAD++
Global 7-10-7 DC800S
- Likes 1
Comment
-
I'll keep that in mind! I'm reading up on passing arguements now. Honestly, I started with PC-DMIS and just started messing with VB so variables are all I really know. I'll have to take some online courses and practice more on the weekend. I'm surprised I got as far as j did to be honest
-
I actually signed up for some online courses. (Udemy) And I did some research on passing arguements. I have some ideas on what I can do to use the same code for both buttons while just changing the initial directory in a sub. I may change a few more things also and I'll repost the code so you can see progress. May not have the time until the weekend tho.
You guys are awesome for helping!
-
https://docs.hexagonmi.com/pcdmis/20...broutines.html
This is the link i used to manage application events. Thought this would be some help
- Likes 1
Comment
-
CodeWarrior Kp61dude!
I updated my code to remove Global variables and pass arguments instead. I also added some extra functionality so that it can totally replace all shortcuts for the operator and they can use only my program and I used some code suggested here. I still hit the same road block as before though. The event handler is giving me trouble. I found some code in a repository that does exactly what I need. It works perfect in excel but when I transfer it to visual studio, it only opens PC-Dmis. This is the excel code...
Code:Dim PCDApp As PCDLRN.Application Dim WithEvents AppEvents As PCDLRN.ApplicationObjectEvents Sub Start() HideExcel End Sub Private Sub HideExcel() Dim intAnswer As Integer intAnswer = MsgBox("Do you want to make Excel invisible? For this test, you should click Yes. It will become visible when you open a part program.", vbYesNo, "Hide Excel?") If intAnswer = vbYes Then Application.Visible = False Else Application.Visible = True End If LaunchPCDMIS End Sub Sub LaunchPCDMIS() Set PCDApp = CreateObject("PCDLRN.Application") Set AppEvents = PCDApp.ApplicationEvents PCDApp.Visible = True End Sub Private Sub AppEvents_OnOpenPartProgram(ByVal PartProg As PCDLRN.IPartProgram) ' Event subroutine. This activates when you OPEN a part program. Set PartProg = PCDApp.ActivePartProgram Application.Visible = True MsgBox "Part Program " & PartProg.Name & " opened. Excel should also be visible." End Sub Private Sub AppEvents_OnStartExecution(ByVal PartProg As PCDLRN.IPartProgram) ' Event subroutine. This activates when you START EXECUTION of the part program. MsgBox "STARTING EXECUTION OF " & PartProg.Name & ". Click OK to continue." End Sub Private Sub AppEvents_OnEndExecution(ByVal PartProg As PCDLRN.IPartProgram, ByVal TerminationType As Long) ' Event subroutine. This activates when you END EXECUTION of the part program. MsgBox "ENDING EXECUTION OF " & PartProg.Name & ". Click OK to continue." End Sub
My end goal here is when PC-Dmis starts running a program it will disable the buttons on my form so someone can't open something over top the running program and when the execution ends it enables the buttons. If I can only get the event handler to work I could get the rest done.
Thanks again for your help guysLast edited by SingularitY; 02-22-2020, 07:28 PM.
Comment
-
You should be able to use the Enabled property of the buttons.
For example, in the OnStartExecution section you set the button.Enabled property to False and in the OnEndExecution you set it back to True again. You might want to have some sort of error handling too, if something goes wrong it should enable the buttons again, otherwise they might be disabled until you restart your app (if your app crashes during PC-DMIS program execution or PC-DMIS closes during program execution).PC-DMIS CAD++ 2o23.1 SP1
- Likes 1
Comment
-
That's what I was thinking of doing. I just can't get the onstart and onendexecution to work in testing unless I use it in excel. (Code above your comment.)
-
I added this to my program and I'm turning off 2 buttons. Both start and end event handlers only read the first line and ignores the rest. Basically, only disabling and enabling the first button. Only reason why I want to disable them is so someone doesn't open another program when one is running.
-
I just tested the code by itself without anything else and it worked perfect. I will go back to the drawing board and updated everyone when it is finished.
-
Originally posted by SingularitY View PostCodeWarrior Kp61dude!
I updated my code to remove Global variables and pass arguments instead. I also added some extra functionality so that it can totally replace all shortcuts for the operator and they can use only my program and I used some code suggested here. I still hit the same road block as before though. The event handler is giving me trouble. I found some code in a repository that does exactly what I need. It works perfect in excel but when I transfer it to visual studio, it only opens PC-Dmis. This is the excel code...
Code:Dim PCDApp As PCDLRN.Application Dim WithEvents AppEvents As PCDLRN.ApplicationObjectEvents Sub Start() HideExcel End Sub Private Sub HideExcel() Dim intAnswer As Integer intAnswer = MsgBox("Do you want to make Excel invisible? For this test, you should click Yes. It will become visible when you open a part program.", vbYesNo, "Hide Excel?") If intAnswer = vbYes Then Application.Visible = False Else Application.Visible = True End If LaunchPCDMIS End Sub Sub LaunchPCDMIS() Set PCDApp = CreateObject("PCDLRN.Application") Set AppEvents = PCDApp.ApplicationEvents PCDApp.Visible = True End Sub Private Sub AppEvents_OnOpenPartProgram(ByVal PartProg As PCDLRN.IPartProgram) [COLOR=#e74c3c]Hangles AppEvents.OnOpenPartProgram[/COLOR] ' Event subroutine. This activates when you OPEN a part program. Set PartProg = PCDApp.ActivePartProgram Application.Visible = True MsgBox "Part Program " & PartProg.Name & " opened. Excel should also be visible." End Sub Private Sub AppEvents_OnStartExecution(ByVal PartProg As PCDLRN.IPartProgram) [COLOR=#e74c3c]Handles AppEvents.OnStartExecution[/COLOR] ' Event subroutine. This activates when you START EXECUTION of the part program. MsgBox "STARTING EXECUTION OF " & PartProg.Name & ". Click OK to continue." End Sub Private Sub AppEvents_OnEndExecution(ByVal PartProg As PCDLRN.IPartProgram, ByVal TerminationType As Long) [COLOR=#e74c3c]Handles AppEvents.OnEndExecution[/COLOR] ' Event subroutine. This activates when you END EXECUTION of the part program. MsgBox "ENDING EXECUTION OF " & PartProg.Name & ". Click OK to continue." End Sub
My end goal here is when PC-Dmis starts running a program it will disable the buttons on my form so someone can't open something over top the running program and when the execution ends it enables the buttons. If I can only get the event handler to work I could get the rest done.
Thanks again for your help guys
Here's my test...
Code:Imports PCDLRN Public Class Form1 ' Globals Dim oPcd As Application = New Application() Dim WithEvents AppEvent As ApplicationObjectEvents Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim oPart As PartProgram = oPcd.ActivePartProgram AppEvent = oPcd.ApplicationEvents End Sub Private Sub AppEvents_OnOpenPartProgram(ByVal partProg As PartProgram) [COLOR=#e74c3c]Handles AppEvent.OnOpenPartProgram[/COLOR] partProg = oPcd.ActivePartProgram MsgBox("Part: " & partProg.Name & " opened.") End Sub End Class
Last edited by Kp61dude!; 02-24-2020, 09:58 AM.PcDmis 2015.1 SP10 CAD++
Global 7-10-7 DC800S
- Likes 3
Comment
-
That makes complete sense! Going off of the code needed to activate button clicks...
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
All of the eventargs have it, so I think you fixed my problem! I will test this tonight but I'm confident this fixes it. If I could like on your post 5x I would right now.
-
Awesome guys! Thanks for the help. Once the code is completely finished, I will post it for anyone to edit or use to their liking. After testing what I have, I noticed that opening Operator Mode only halfway works. It looks like operator but you can still access the edit window if you go through the menu View-Edit Window. IF PC-Dmis is already open, this doesn't happen. If I can't figure out a solid solution for this, I will just change my open code to open the .lnk shortcut used to open operator mode and watch task manager for when it starts. Not ideal but in theory it should work.
- Likes 3
Comment
-
Originally posted by SingularitY View PostIt looks like operator but you can still access the edit window if you go through the menu View-Edit Window. IF PC-Dmis is already open, this doesn't happen.
- "Select Probe File" (when pcd is launched)
-"Program is using a probe that is not defined" (when program is opened)
-"Program is using a tip that is not calibrated! (when program opened)
You'll also find that when pc-dmis is exited, it will still be running in the task manager process list, and it won't respond to Application.Quit in your code. Kill it with task manager, and any settings changes made during the pcd session are lost. It's a mess.
The key is to wait until pcd is ready. The question is, "when is that?"
Application.WaitUntilReady() is a joke. Doesn't wait for anything far as I can tell. I beat my head against the wall trying to figure this out. Different machine interfaces and computers are ready at differing intervals. Finally decided to try waiting for a pc-dmis window to appear. Initially tried waiting for the "Select Probe File" window, which worked on all machines but two that use DME interface. Eventually settled on waiting for the pc-dmis Open window. Then I execute the CreateObject() command. We implement standard settings across all machines, one of which is to always show the pcd Open dialog at startup. We do this with batch files/registry import files.
In our start pcd batch files, the event handler .exe is started before pc-dmis, and sits waiting for the pc-dmis Open dialog to appear.
I use FindWindowEx from the Windows API (user32.dll) in a loop with Thread.Sleep() to wait for the window to appear.
I use a Timer where the Timer.Elapsed event handler looks for pcd to become invisible(every 10 seconds), which is my cue that the user is trying to close pcd, and I need to close the event handler. I've found that the vb "End" command closes pcd gracefully, solving the problem above.
Wish I could post my code...
- Likes 3
Comment
-
You had your chance dude. Lol.
I don't like complicating things like this. Is indeed fun.i enjoy like making it work. Been doing this a long time and have come to realize that simple is better. Force into it though. Only responding to the onopen event. To make a little edit to the programs before they run. Ill be adding cmm error logs when I have time, and that'll be it. Maybe.
Related Topics
Collapse
-
by GumleezyHi everyone,
So I've created a front end program that I would like to have start up as soon as PC-DMIS is opened. Is there a way to get...-
Channel: PC-DMIS Code Samples
07-14-2014, 09:33 AM -
-
by Mik95Hi to everyone!
I'm looking for a method that allows me to choose if to run a program entirely or partially. The program takes several minutes,...-
Channel: PC-DMIS for CMMs
05-30-2022, 09:45 AM -
-
by dlewisI would like to know if anyone knows how to open up & run multiple pc-dmis prgms from either another software or from within pcdmis by possibly creating...
-
Channel: PC-DMIS for CMMs
08-15-2007, 01:42 PM -
-
I am currently attempting to write a program (VB.NET) that will have a PC-DMIS program's file name passed to it. This program should then start PC-DMIS...
-
Channel: PC-DMIS Code Samples
10-11-2012, 03:15 PM -
-
by kaikaiHello there,
I am trying to run a program from vb .exe application, but don't know how to leave PC-DMIS open, so that I can see which line...-
Channel: PC-DMIS Code Samples
02-15-2018, 09:19 AM -
Comment