업무 자동화/python & CAD

Creo & Python] Get creo file name

ToolBOX01 2025. 9. 5. 12:49
반응형

Developing automation programs using Creo files. I use a Python program. I use the Creoson server program. We utilize Python for other program development languages ​and functional testing. The "creoson" server program must be started, and development is performed using Creo 9.0.

Installing and Setting Up "creoson"

 

CREOSON-Creo 9기준] 설치 및 환경 설정

CREOSON 프로그램 다운로드 : CreosonServerWithSetup-2.8.2-win64.zip64비트 옵션을 선택, Java 런타임 환경(JRE)이 포함되어 있어 별도 설치가 필요 없습니다.다운 로드 하고, zip 파일을 해제 합니다. Release Versio

tool-2020.tistory.com


▣ Check the Creo program connection

You must run the Creo program. You must run the Creo program and code it as a Python program. In order to develop a program, the Creoson server program must be running. (Run the CreosonSetup.exe file)

[ CreosonSetup.exe ]

Python code :  Example1

import creopyson
import win32api

# Create a Creoson client
client = creopyson.Client()

try:
    # Attempting to connect to Creo
    client.connect()
    print("Successfully connected to Creo. Session ID:", client.sessionId)

    # Display connection status and session ID in Windows message box
    message = f"Connection successful!\nSession ID: {client.sessionId}"
    win32api.MessageBox(0, message, "Connection status", 0x00000040)  # MB_ICONINFORMATION
except Exception as e:
    print("Creo Connection failed:", str(e))
    # Display an error in a Windows message box when the connection fails.
    win32api.MessageBox(0, f"Connection failed: {str(e)}", "error", 0x00000010)  # MB_ICONERROR

Program execution results:

If you need detailed installation and setup, please visit the site. (creoson)

Python code :  Example2

import creopyson

# Initialize the 'c' object to None
c = None

try:
    # Create a client object and connect to Creo
    c = creopyson.Client()
    c.connect()

    # Check if Creo is running after a successful connection
    if c.is_creo_running():
        print("Creo is running. Session ID:", c.sessionId)
        
        # Get the current working directory of Creo
        current_directory = c.creo_pwd()
        print(f"Current working directory: {current_directory}")

        # You can add more Creo commands here, for example:
        # c.file_open("my_part.prt") # To open a file
        # c.file_save()              # To save the current file
        
    else:
        print("Creo is not running.")

except Exception as e:
    print(f"An error occurred: {e}")
finally:
    # Disconnect if the 'c' object exists
    if c:
        c.disconnect()
        print("Disconnected from Creo.")

 


▣ Opening a Creo file

This is a Python code to open the "korea.prt" file located in the C:\PTC\WORK90 folder.

import creopyson

# Initialize the 'c' object to None
c = None

try:
    # Create a client object and connect to Creo
    c = creopyson.Client()
    c.connect()

    # Check if Creo is running after a successful connection
    if c.is_creo_running():
        print("Creo is running. Session ID:", c.sessionId)
        
        # Get the current working directory of Creo
        current_directory = c.creo_pwd()
        print(f"Current working directory: {current_directory}")

        # Change the working directory to C:\PTC\WORK90
        c.creo_cd("C:\\PTC\\WORK90")
        print("Working directory changed to C:\\PTC\\WORK90")

        # Open the korea.prt file and display it
        c.file_open("korea.prt", display=True)
        print("korea.prt file has been opened.")
        
    else:
        print("Creo is not running.")

except Exception as e:
    print(f"An error occurred: {e}")
finally:
    # Disconnect if the 'c' object exists
    if c:
        c.disconnect()
        print("Disconnected from Creo.")

by korealionkk@gmail.com


반응형