Creo Mapkey Os Script Example | UHD |

You can chain DOS commands to perform file operations.

Goal: Create a timestamped ZIP backup of the current folder using 7-Zip (assuming 7-Zip is installed). Warning: This requires the folder to have write permissions.

mapkey $F6 @SYSTEM powershell -Command "Compress-Archive -Path '.\*' -DestinationPath 'Backup.zip'";

(This uses PowerShell, which is built into modern Windows, to zip the current folder contents.)


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:

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

First, record a Mapkey that exports a BOM (Table > Save As > CSV). Then modify it:

mapkey bom_export @MAPKEY_NAMEExport BOM to DB;\
~ Command `ProCmdTblSaveAs` ;\
~ Select `file_saveas` `type_option` 1 `csv`;\
~ Path `file_saveas` `FileName_Input_Browse` `C:\temp\bom_temp.csv`;\
~ Command `ProCmdFileSave` ;\
~ Command `ProCmdUtilSystem` `system("python C:\creo_scripts\export_bom_to_db.py C:\temp\bom_temp.csv")`;\
~ Command `ProCmdFileEraseNotDisp` ;

Now pressing your Mapkey exports the BOM, processes it with Python, and sends it to your database.

Save this as C:\creo_scripts\export_step_processor.bat:

@echo off
REM This script expects one argument: the full path of the exported STEP file
set STEP_FILE=%1

REM Create dated folder (YYYY-MM-DD) set TODAY=%DATE:~10,4%-%DATE:~4,2%-%DATE:~7,2% set TARGET_DIR=C:\STEP_ARCHIVE%TODAY% creo mapkey os script example

if not exist "%TARGET_DIR%" mkdir "%TARGET_DIR%"

REM Move the STEP file move "%STEP_FILE%" "%TARGET_DIR%" > nul

REM Log the action echo %DATE% %TIME% - %STEP_FILE% moved to %TARGET_DIR% >> C:\creo_scripts\export_log.csv

echo Done. exit /b 0

In a Creo configuration file (config.pro), an OS script mapkey looks different than a standard UI mapkey.

Standard Mapkey syntax:

mapkey $F1 ~ Close `main_dlg_cur` `main_dlg_cur`;

OS Script Mapkey syntax:

mapkey $F2 @SYSTEM command_to_run;

Key Components: