Creo Mapkey Os Script Example

Below is a ready-to-import mapkey that you can paste into your mapkey file or create via the Mapkeys dialog.

OS script (paste as the Mapkey's command text):

# Start mapkey
!MK_CREATE_RECT_EXTRUDE;Create centered rectangle and extrude
!OS=1
! Activate model (assume part)
ACTION(occt:Model)
! Create datum plane selection: TOP
SELECTION(component,type=model)
SELECTION(plane,name,TOP)
! Create sketch on Top
COMMAND(Planar Sketch)
! Wait for sketch environment
PAUSE(0.2)
! Create rectangle centered at origin:
# Draw first line (center to right)
RECTANGLE_CREATION_MODE=2
COMMAND(SketchRectangle)
# Set rectangle width and height using relations to parameters
! Assume parameters WIDTH and HEIGHT exist; otherwise create them
PARAMETER_CREATE(Width,DIMENSION,20.0)
PARAMETER_CREATE(Height,DIMENSION,10.0)
# Place rectangle centered: use corner points at +/- Width/2, +/- Height/2
SKETCH_POINT(-Width/2, -Height/2)
SKETCH_POINT(Width/2, -Height/2)
SKETCH_POINT(Width/2, Height/2)
SKETCH_POINT(-Width/2, Height/2)
SKETCH_CONNECT_POINTS_RECT
! Exit sketch
COMMAND(Exit Sketch)
PAUSE(0.1)
! Extrude the sketch:
COMMAND(Extrude)
EXTRUDE_DEPTH=5.0
EXTRUDE_DEPTH_TYPE=distance
EXTRUDE_DIRECTION=normal
COMMAND(OK)
! Save model
COMMAND(Save)
! End mapkey

Notes:

Create a mapkey that:

This demonstrates basic OS commands: model selection, feature creation, parameter setting, and save.

Creo provides two primary functions to call OS commands within Mapkeys:

| Function | Behavior | Use Case | | :--- | :--- | :--- | | SYSTEM(command) | Executes command and immediately returns to Creo. | Launching a logging script or batch file. | | PROTECT(command) | Executes command and waits for it to complete before Creo proceeds. | Converting a file, then importing the result. |

Syntax inside a Mapkey:

mapkey my_script @MAPKEY_NAMERun OS Script;\
~ Command `ProCmdSessionCustomRibbon` ;\
~ Command `ProCmdUtilSystem` `system("C:\scripts\export_and_rename.bat")`;

When combining Mapkeys and OS scripts, 90% of failures are due to these three issues:

This article shows how to create and use Creo Mapkeys and Object Script (OS) to automate repetitive tasks in Creo Parametric. It includes a working example, explanation of key elements, and tips for debugging and portability.

Below is a complete, copy-paste workflow for a manufacturing engineer who needs to export a STEP, create a PDF drawing, rename both with a part number and revision, and copy to a network drive.

Instead of putting OS commands directly in the Mapkey, create a Launcher Mapkey that writes a temporary script on the fly.

Example: Dynamic Folder Creation This Mapkey creates a timestamped folder for the current assembly.

Mapkey content:

OS_Script cmd.exe /c mkdir C:\Projects\Assy_%date:~-4,4%%date:~-10,2%%date:~-7,2%_%time:~0,2%%time:~3,2%

(Note: This is fragile. Better to call a dedicated script.)