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

Community Tip - Want the oppurtunity to discuss enhancements to PTC products? Join a working group! X

Connecting into Creo using Python

PO_9389460
6-Contributor

Connecting into Creo using Python

Hello all ,

 

I am trying to connect into Creo using a python code to extract the parameters into BOMs..

First off has anyone successful done this?? 

I believe it is possible , However I maybe missing a file or module or not using the correct connection command.

 

Here is  the current setup.

-Creo 6.0.5.1

-Python Latest version

-All modules update to date.

-I have all the toolkit API installed.

-pro_comm_msg in environment variables

- I have preferred to used win32com.client module to make connection but it has failed to make the connection.

- I have also tried Creoson third party software to  make the connection. I was able to connect  to the server and Creo but failed to extract the parameters .it just stated no attributes to get...As there no offical help on this software it is hard to get help on this.

 

So If anyone can provide code of how they establish a connection and was able to send commands/extracted data would be helpful.

 

Thanks.

11 REPLIES 11

Creoson relies on J-Link so the first thing is to run ptcsetup and confirm you have the J-Link packages installed. Also, if I remember correctly the Creoson tool only allows one way communication (it's just using JSON requests) so you can get the data out of Creo but you will have to do something like a mapkey or playing a trail file to put the data back in to Creo.

PO_9389460
6-Contributor
(To:TractorGuy)

Hello TractorGuy,

 

Thanks for quick reply. I am only look to extract the parameters of the parts. listed the code below. nad the result from command prompt using  creopyson.

Perhaps someone see when I am wrong.

 

import creopyson

def is_creo_running():
"""
Check if Creo Parametric is running.

Returns:
bool: True if Creo Parametric is running, False otherwise.
"""
try:
c = creopyson.Client()
c.connect()
c.disconnect()
return True
except:
return False

try:
print("Connecting to Pro/ENGINEER (Creo) Parametric...")
if not is_creo_running():
print("Creo Parametric is not running.")
exit()

# Create a Creo session using creopyson.Client()
c = creopyson.Client()
c.connect()
c.creo_set_creo_version(6)

print("Connection to Pro/ENGINEER (Creo) Parametric established successfully!")

# Get the sessionId from the client object
sessionId = c.sessionId
print("Session ID: " + str(sessionId))

# Execute Creo CLI command to retrieve parameter list from the model
result = c.creo_execute_command("parm_list")

if result["success"]:
param_list = result["message"]
print("Parameter list: ")
for param in param_list:
param_name = param["name"]
param_file = param["file"]
print(f"Name: {param_name}, File: {param_file}")
else:
print("Failed to retrieve parameter list.")

# Disconnect from Pro/ENGINEER (Creo) Parametric
c.disconnect()

print("Pro/ENGINEER (Creo) Parametric session closed successfully!")
except Exception as e:
print("Error: ", e)

 

 

command prompt printouts....

 

C:\python_code>python 202.py
Connecting to Pro/ENGINEER (Creo) Parametric...
Connection to Pro/ENGINEER (Creo) Parametric established successfully!
Session ID: -2295100368324320170
Error: 'Client' object has no attribute 'creo_execute_command'

 

The model does have attributes and it can seem to get them or carry out the command?

 

anyone seen this error before?

 

Morning Martin,

 

Thanks for the information. I have been able to connect into Creo use Cresoson and creopython. I have been able to export single part or assembly paraMeters. Do you or anyone know what is the command for a  user to export the full parameters of a assembly.? The BOM commands seem to failed for some reason.

 

Thanks

Hi martin, 

 

I created a simple test script listed below. 

 

import creopyson

# Create a Creo connection
c = creopyson.Client()
c.connect()

# Get the children of the assembly
children = c.bom.get_paths(
client=c,
paths=True,
top_level=True,
exclude_inactive=True
)['children']

# Print the names of the children
for child in children:
print(child['name'])

 

I get the following error  AttributeError: 'Client' object has no attribute 'bom'

 

it like the creopyson is not not working correctly which I find highly unlikely..

I keep fighting with it.. unless you see anything i am doing incorrect?

 

 

 

I still haven't seen that you have confirmed that J-link is installed. Creoson can make sessions all day long but will not be able to retrieve any data without J-Link.

PO_9389460
6-Contributor
(To:TractorGuy)

Hello TractorGuy,

 

Sorry that is correct. To proof that J-link is working I was able to get a part file to be read and exported. See attached code and CMD print out below.. It is the proof that the connect to creo is working on this command . However I want to grow this to export the full BOM of an assembly.

 

import creopyson

try:
# Create a Creo connection
c = creopyson.Client()
c.connect()

# Define parameter names to retrieve
param_names = ["PARTNO","REVISION", "DESCRIPTION","MANUFACTURING_INFORMATION", "MATERIAL", "FINISH", "MASS"]

# Call the parameter_list() function to get the list of parameters
param_result = c.parameter_list(name=param_names, encoded=False)

# Check if the request was successful
if param_result:
# Print the retrieved parameters
for param in param_result:
print("{}: {}".format(param["name"], param["value"]))
else:
print("Failed to get parameters. No results returned.")

except Exception as e:
print("Failed to start Creo session. Creo is not running. Error: {}".format(str(e)))   

 

 

CMD results   :

 

C:\python_code>PYTHON PART.PY
PARTNO: T21-008-000-006
REVISION: 0
DESCRIPTION: SIDE GUARD
MANUFACTURING_INFORMATION: FOLD,LASER
MATERIAL: 3_MM_PLATE_S275
FINISH: PAINTED
MASS: 4.501156381832352


@PO_9389460 wrote:

Hi martin, 

 

I created a simple test script listed below. 

 

import creopyson

# Create a Creo connection
c = creopyson.Client()
c.connect()

# Get the children of the assembly
children = c.bom.get_paths(
client=c,
paths=True,
top_level=True,
exclude_inactive=True
)['children']

# Print the names of the children
for child in children:
print(child['name'])

 

I get the following error  AttributeError: 'Client' object has no attribute 'bom'

 

it like the creopyson is not not working correctly which I find highly unlikely..

I keep fighting with it.. unless you see anything i am doing incorrect?


Hi,

what about to use creopyson.bom.get_paths instead of c.bom.get_paths ?

See https://creopyson.readthedocs.io/en/latest/creopyson.html page.


Martin Hanák

Hello Martin,

 Nice find.. Yes that was needed to fix the simple test. Thanks. It was able to print the part numbers and proof it does work. However there doesn't seem to have a function of getting specific params out of the assembly by looping around each part in that assembly or is there?

 

Thanks

 

.

 

 


@PO_9389460 wrote:

Hello Martin,

 Nice find.. Yes that was needed to fix the simple test. Thanks. It was able to print the part numbers and proof it does work. However there doesn't seem to have a function of getting specific params out of the assembly by looping around each part in that assembly or is there?

 

Thanks


Hi,

this is my last reply. I'm sorry I won't be quite polite in my reply. It seems to me that your programming knowledge is insufficient because you are asking as a beginner. Unfortunatelly I do not have enough time to investigate your problems.

 

Note:

If you were able to get list of component names, then surely you can cycle through this list and (1) open component, (2) get parameter value, (3) close component.

 

Wish you good luck


Martin Hanák
Top Tags