Skip to content

Equations and global variables drive a part's dimensions - #250

Merged
andrewbartels1 merged 22 commits into
mainfrom
feat/equations
Nov 19, 2026
Merged

andrewbartels1 merged 22 commits into
mainfrom
feat/equations

Conversation

@andrewbartels1

@andrewbartels1 andrewbartels1 commented Nov 18, 2026 •

Copy link
Copy Markdown
Owner

Equations and global variables (queue item 29)

Equations and global variables are how a SolidWorks part becomes parametric: one value drives several dimensions, so changing it resizes the part consistently. The server had nothing for them. This adds three tools for equations and two for sketch text (145 in all):

Tool What it does
list_equations Every equation and global variable, in order, with name, expression, kind (global_variable, dimension, other) and its solved value
set_equation Adds an equation for name, or changes the one there is; solves and rebuilds; replies with the value and whether it was added or changed
delete_equation Removes one by name; a dimension it drove keeps its last value
add_sketch_text Writes text in the open sketch at a height; center_at centres it on a point. The text can show a custom property ($PRP:"Name")
set_sketch_text Rewrites the text in a named sketch and rebuilds, giving the text before and after
  • Name and expression, not raw text. The agent gives name (Width, or a dimension such as D1@Sketch1) and expression (60mm, "Width" / 2); the server writes "Width" = 60mm, so the quoting is always right.
  • Configurations. all by default, as SolidWorks' Equations dialog does; this or a list of names, checked like set_dimension's. Named configurations are set one at a time, and the reply's values gives each one's value.
  • Refusals change nothing. A dimension the model doesn't have is refused before anything is written (invalid_input). An expression SolidWorks can't use fails as the new equation_refused: an added equation is deleted again, and a changed one gets its old text back.
  • The COM calls that work (live probes, 2026-11-17/18; COM pitfall 36): every IEquationMgr call given an array of configuration names returns -1 from Python, so Add2 adds, writing the Equation(i) property changes an equation everywhere, and the "this configuration" call sets one configuration at a time.
  • Sketch text. SolidWorks' own centre alignment doesn't centre text, so center_at measures the text's edges and moves it onto the point. SolidWorks doesn't redraw sketch text when an equation changes the part; set_sketch_text writes it again with the sketch open, which does.

The demo: one variable sizes a combination wrench

A steel combination wrench driven by one global variable and lettered with its size: 13 mm, then 19 mm, then 8 mm in its own configuration

A 13 mm combination wrench modelled from photos of a real one: an open end at 15° with a curved jaw bottom, a raised 12-point box end, fillets where both heads meet the handle, and 1.8159 (51CrV4) chrome-vanadium steel. Both faces of the handle are lettered "solidworksmcp-python 13 mm", centred, with the size read from Size. One variable drives 27 equations:

  • Size = 19mm regrows it, and the lettering is rewritten to "19 mm".
  • An 8 mm size lives in its own configuration and reads "8 mm".
  • A bad expression is refused and changes nothing.

verify_model checks each size against the hand calculation:

Size Expected (mm) Measured (mm)
19 mm 276.37 × 16.8 × 38 276.366 × 16.7985 × 38.0
8 mm 116.36 × 7.07 × 16 116.3646 × 7.0731 × 16.0

The GIF's caption changes as each step starts. It's on the demos page and in the README's demo table.

Building it needed these tool changes, in this PR:

  • create_cut_extrude: a Blind cut with both_directions is now mid-plane (it cut one way only).
  • Polygons: add_polygon takes an angle, and a diameter dimension on a polygon sizes it across flats.
  • Ellipses: add_ellipse takes an angle, and add_sketch_dimension sizes an ellipse's major_axis or minor_axis.
  • CI: a test no longer imports pythoncom, which the Linux runners don't have.

Checks

  • Unit tests: 4,428 pass at 100% coverage; ruff and mypy are clean; the docs build in strict mode; every link checks out.

  • Live, all passed 2026-11-18:

    • test_a_global_variable_drives_the_depth: 30 then 45 mm deep (72,000 mm³); the bad expression refused; delete keeps 45 mm.
    • test_an_equation_in_one_configuration: Long 50 mm, Default 30 mm.
    • test_a_combination_wrench_sized_by_one_variable: the demo above, recorded.
    powershell -ExecutionPolicy Bypass -File tests\scripts\live_demo.ps1 -Test tests/live/test_live_sw_equations_wrench.py

list_equations, set_equation and delete_equation (queue item 29). One
value ("Width" = 60mm) can drive several dimensions ("D1@Sketch1" =
"Width" / 2). set_equation adds or changes one, in every configuration
by default, solves and rebuilds, and gives the new value; an expression
SolidWorks can't use fails as equation_refused and changes nothing.
143 tools.
set_equation used Add3, which returns -1 from Python for every input. Live
probes found what works on SolidWorks 2026: Add2 adds; writing the
Equation(i) property changes an equation in every configuration; the
"this configuration" call (option 1, no names) sets one configuration, so
named configurations are set one at a time and the active one is shown
again. Every call given an array of configuration names fails. The reply
now gives each named configuration's value.

The demo: one global variable, Size, drives a 13 mm combination wrench
(open end at 15 degrees, 12-point box end, flat handle) through 19
equations; Size = 19mm regrows it, an 8 mm size lives in its own
configuration, and a bad expression is refused. Passed live and recorded
for the demos page and the README.

Building it needed:
- create_cut_extrude: a Blind cut with both_directions is now mid-plane
  (it cut one way only).
- add_polygon takes an angle; a diameter dimension on a polygon sizes it
  across flats.

COM pitfalls 36-38 record the findings.
… a curved jaw, neck fillets and 51CrV4 steel (local, under review)
…axis, and refuse one that isn't aligned (local, under review)
…rV4 from the DIN library (local, under review)
…d or not; the wrench's handle reads its size (local, under review)
…o along or mirror (they didn't work); the wrench letters its handle's middle at every size (local, under review)
…Works doesn't redraw sketch text on rebuild); the wrench re-letters after each size change (local, under review)
…s working direction first (local, under review)
… whole-mm lettering, sketches hidden one by one before framing (local, under review)
…s at the sketch origin first (local, under review)
…estored after), frames before lettering, ends on 'Equations demo done' (local, under review)
…atches what SolidWorks is doing (local, under review)
…LOG (add_sketch_text, set_sketch_text, ellipse angle and axes); recorded GIF
# Conflicts:
#	docs/assets/progress/progress.json
#	docs/assets/progress/progress.svg
#	docs/assets/progress/tool-list-size.md
#	openspec/queue.md
@andrewbartels1
andrewbartels1 merged commit bab5b7e into main Nov 19, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant