cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - You can change your system assigned username to something more personal in your community settings. X

Tutorial on driving Creo through a PoSh shell

awei3
13-Aquamarine

Tutorial on driving Creo through a PoSh shell

Thought I'd give back a bit to the community, one secret weapon that I've been leveraging for quick and dirty automation is to access the asynchronous VB API through PowerShell. VB API is a .NET construct, so it's accessible through VB, C#, as well as PowerShell. API wise, it is almost 1:1 with the base J-Link package, so you are not missing out on much. You will not have Toolkit functionality, but you can in theory create Toolkit DLLs to load in via config.pro that you can invoke via the VB API.

 

The convenience of using PowerShell is that it is deployed by default on all machines Win7+. You may run into some compatibility issues with Powershell V2, since that was a rather primitive version compared to what we have today. Win10 deploys with Powershell 5 and above.

It is also a shell environment. You do not need to compile anything or open any special program, you can literally link up in a new shell window and immediately start piloting Creo.

So far I can pretty much create a quick one off script on a network drive, point a user to it, and immediately have them running my automation.

 

 

To link up, you must ensure you have the VB API installed with Creo.

 

I'll refer to the loadpoint of Creo Parametric from here on out as PRO_DIRECTORY. This is, for example, 

C:\Program Files\PTC\Creo 3.0\M120
 
In your script, you must assign the environment variable PRO_COMM_MSG_EXE. This will likely point to PRO_DIRECTORY\Common Files\x86e_win64\obj\pro_comm_msg.exe
 
 
On to actual scripting:
You must first initiate a connection to Creo. This should sound familiar to any J-Link programmers.
You then obtain a session object from the connection. From here, you can open models, access the active model, and the sky is the limit from there.
After you are done, you must disconnect from the session. It seems that the VB API async server only handles a certain amount of handles at a time, and if you don't close out, you will hit a point soon where you cannot connect unless you restart Creo.
 
Figuring out the API calls is confusing. You may throw PRO_DIRECTORY\Common Files\vbapi\vbapidoc\index.html in a browser to figure out the base VB API calls. Translating them to Powershell is a bit more confusing, you may need to create parent objects to reference their calls. These will all be COM objects. I use a script like the following to dump out all COM Objects on the machine and I just Ctrl-F through there to figure them out.
 
gci HKLM:\Software\Classes -ea 0| ? {$_.PSChildName -match '^\w+\.\w+$' -and
(gp "$($_.PSPath)\CLSID" -ea 0)} | ft PSChildName
 
The following is an example script that expects Creo to be open. It links in, gets the current model, runs a mapkey, and disconnects. In theory you can have the VB API script start Creo totally on its own, you can in theory script out workers in this manner.
 
# Get path of running Creo executable
$proc = Get-Process | Where-Object {$_.ProcessName -eq "xtop"}
if ($proc -eq $null) {
    Write-Output "Running Creo process (xtop) not found"
    Write-Output "Press any key to continue..."
    Read-Host     exit } $pc_path = $proc.Path -replace "xtop.exe", "pro_comm_msg.exe" $Env:PRO_DIRECTORY = $proc.Path.TrimEnd("xtop.exe") $Env:PRO_COMM_MSG_EXE = $pc_path # Check if VB API is registered. try {     $testasync = New-Object -ComObject pfcls.pfcAsyncConnection } catch {     Write-Output "VB API not yet registered on this machine, performing first time setup..."     $vb_path = $proc.Path -replace "Common Files(.*)$", "Parametric\bin\vb_api_register.bat"     Start-Process -Wait -FilePath $vb_path } # Initiate connection to Creo. try {     $async = New-Object -ComObject pfcls.pfcAsyncConnection     $connection = $async.Connect($null, $null, $null, $null)     $session = $connection.Session } catch {     $_     Write-Output "Could not connect to Creo session."     Write-Output "Press any key to continue..."     Read-Host     exit } $session.GetActiveModel() $session.RunMacro('~ Command `ProCmdRibbonOptionsDlg` ') $connection.Disconnect($null)
3 REPLIES 3
MartinHanak
24-Ruby III
(To:awei3)

Hi,

thanks for sharing your knowledge 🙂

Note: To prevent displaying emoticons inside code, use Insert Code button ... </>.


Martin Hanák
awei3
13-Aquamarine
(To:MartinHanak)

Noted, corrected above. Thanks for the tip!

ZW_9572490
4-Participant
(To:awei3)

For what it's worth to you and any other users, I've been trying to automate Creo for a few years now, and your post finally explained how it worked.

 

Using your code to show all the COM Objects to determine which proper Powershell classes correspond to the classes used in the vbapidoc and vbug.pdf is the only way to get anything to work.

 

Also, your Powershell code to connect and disconnect really helped me get off the ground, too. I couldn't even make use of any of the information I was reading in the documentation since I could not even get it to connect first! There was no way for me to know if my VB API installation was wrong, or if I messed up changing the PATH variable, or if something else was going on. It turns out that I was simply not using the Powershell versions of the classes.

 

Thank you for posting this information. Thanks to your post, I have already implemented updating tables and parameters via Powershell.

 

PTC really needs to put this info in the vbug.pdf or something.

Announcements


Top Tags