run_python
Runs Python on the server's live SolidWorks COM connection to handle modeling work no dedicated tool covers, reusing persisted variables across calls and returning the result or a traceback.
Instructions
Run Python on the server's own SolidWorks connection, for work no tool covers.
Predefined: sw (ISldWorks), model (current/active document, IModelDoc2),
mod (interfaces: wrap(obj, mod.IAssemblyDoc), mod.IComponent2, mod.IFeature,
mod.IMate2, ...), wrap, session, math, np. Variables persist between calls:
build a component map or face cache once and reuse it (cleared when
SolidWorks was restarted). COM runs with CommandInProgress set, ~25x faster
than a separate script. Returns the final expression's value (or result)
and printed output; a failure returns the traceback. Nothing is rolled
back. A modal dialog fails the call within seconds naming it: see
answer_dialog.
The script stops with an error naming its line when it runs longer than time_limit_s, grows the server's memory by more than memory_limit_mb, or on stop_python. Bound every loop over the feature tree or sub-features: a loop that never ends holds every other tool.
Tool call template
Replace each placeholder with a value matching the input schema. Only required parameters are included; add optional parameters as needed.
{
"id": 1,
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"arguments": {
"code": "<code>"
},
"name": "run_python"
}
}Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| code | Yes | ||
| time_limit_s | No | ||
| memory_limit_mb | No |