SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Dimension values: `#name = value` defines a parameter too (#2371) (#4212)
leValdemar19f7b08ab8c494
diff --git a/crates/doc/src/expr.rs b/crates/doc/src/expr.rsindex b135c2db13..ffeda21a2c 100644--- a/crates/doc/src/expr.rs+++ b/crates/doc/src/expr.rs@@ -562,8 +562,8 @@ fn is_name_shaped(s: &str) -> bool {matches!(cs.next(), Some(c) if c.is_alphabetic() || c == '_') && cs.all(|c| c.is_alphanumeric() || c == '_')}-/// A value typed as a definition, `name = expression`: the name and the text after the first-/// `=`, both trimmed (the text may be empty). `None` for anything else, including text whose+/// A value typed as a definition, `name = expression` (or `#name = expression`, #2371): the name+/// without the `#` and the text after the first `=`, both trimmed (the text may be empty). `None` for anything else, including text whose/// left side is not shaped like a name (`2 = 3`), which stays an ordinary expression and fails/// to parse as before. The dimension input defines a user parameter this way (#2916); other/// value inputs can take the same form later.@@ -571,6 +571,7 @@ pub fn definition(s: &str) -> Option<(&str, &str)> {let (name, rhs) = s.split_once('=')?;let name = name.trim();let rhs = rhs.trim();+ let name = name.strip_prefix('#').unwrap_or(name);// `w == 3` is not a definition: a second `=` would read as a formula marker (#4681).(is_name_shaped(name) && !rhs.starts_with('=')).then_some((name, rhs))}@@ -1258,6 +1259,9 @@ mod tests {("w = 1 in - 5.4 mm", "w", "1 in - 5.4 mm"),("_w1 = 1", "_w1", "1"),("szerokość = 20", "szerokość", "20"),+ // A `#` before the name marks it as a new parameter's (#2371).+ ("#w = 20", "w", "20"),+ (" #w=20 mm", "w", "20 mm"),// Reserved names split too: the parameter check names the reason.("mm = 5", "mm", "5"),("w = a = 3", "w", "a = 3"),@@ -1268,7 +1272,27 @@ mod tests {for (text, name, rhs) in splits {assert_eq!(definition(text), Some((name, rhs)), "{text:?}");}- for text in ["20", "20 mm", "width / 2", "= 20", "=", "", "2w = 3", "a b = 3", "a-b = 3", "w.x = 3", "(w) = 3", "-w = 3", "w == 3"] {+ for text in [+ "20",+ "20 mm",+ "width / 2",+ "= 20",+ "=",+ "",+ "2w = 3",+ "a b = 3",+ "a-b = 3",+ "w.x = 3",+ "(w) = 3",+ "-w = 3",+ "# = 3",+ "#= 3",+ "##w = 3",+ "# w = 3",+ "#2w = 3",+ "#w",+ "w == 3",+ ] {assert_eq!(definition(text), None, "{text:?}");}}diff --git a/crates/engine/src/cmd/sketch.rs b/crates/engine/src/cmd/sketch.rsindex 4dc2cdeeb6..f4d339121e 100644--- a/crates/engine/src/cmd/sketch.rs+++ b/crates/engine/src/cmd/sketch.rs@@ -120,7 +120,7 @@ pub static COMMANDS: &[CommandSpec] = &[.params(concat!("p0, p1: overall ends, width", connect_doc!(), created_doc!())),CommandSpec::new("sketch.point", "Point", draw_point).at("SKETCH", "CREATE").icon("point").enabled(in_sketch).params(concat!("point: [x,y] (each a number or an expression over the design's parameters: the point is then fixed where it comes to and follows them) or a snap: \"mid:<line>\" (held at the line's midpoint), \"on:<curve>:x,y\" (on the curve near x,y), {vertex: [x,y,z]} or {on_edge: [x,y,z]} of the model; id?", connect_doc!(), created_doc!())),CommandSpec::new("sketch.dimension", "Sketch Dimension", dimension).at("SKETCH", "CREATE").icon("dimension").key("D").enabled(in_sketch).params(- "entities: [refs] (a line midpoint is \"mid:line_id\"), type?: auto|distance|horizontal|vertical|length|radius|diameter|angle, value?: number or expression (default: current; a bare number is in the design's units, e.g. 2 means 2 in in an inch design; give a unit such as \"50 mm\" to override), or `name = expression` (e.g. \"width = 40\"), which adds the user parameter `name` with that expression and dimensions with `name` (refused when the name is taken or reserved; the reply's `defined` names it); text_at?: [x, y]. Two points or a line off the axes: with auto, text_at across from them is the distance between them, above or below them horizontal, beside them vertical. Two midpoints: placing text above or below their bounding box measures horizontally, beside it vertically. Two lines: the angle between them (acute, or 180° − it when the value is nearer that) unless text_at picks a sector. A line and a circle or arc: the distance from the line to its edge (the gap, or how far the edge reaches past a line that cuts it; 0 is tangent); dimension the centre point for the distance to the centre. A dimension the sketch already determines is refused (naming what determines it); if_redundant?: \"driven\" places it as a driven (reference) dimension instead. scale_sketch?: bool (the dimension tool sends true): when this is the sketch's first length dimension and nothing in the sketch fixes a size (no other driving length dimension, nothing fixed or projected), the whole sketch scales about its origin to the value instead of distorting. Returns measured in unit (mm or deg), and scaled: the factor when the sketch was scaled",+ "entities: [refs] (a line midpoint is \"mid:line_id\"), type?: auto|distance|horizontal|vertical|length|radius|diameter|angle, value?: number or expression (default: current; a bare number is in the design's units, e.g. 2 means 2 in in an inch design; give a unit such as \"50 mm\" to override), or `name = expression` (e.g. \"width = 40\"; also `#name = expression`), which adds the user parameter `name` with that expression and dimensions with `name` (refused when the name is taken or reserved; the reply's `defined` names it); text_at?: [x, y]. Two points or a line off the axes: with auto, text_at across from them is the distance between them, above or below them horizontal, beside them vertical. Two midpoints: placing text above or below their bounding box measures horizontally, beside it vertically. Two lines: the angle between them (acute, or 180° − it when the value is nearer that) unless text_at picks a sector. A line and a circle or arc: the distance from the line to its edge (the gap, or how far the edge reaches past a line that cuts it; 0 is tangent); dimension the centre point for the distance to the centre. A dimension the sketch already determines is refused (naming what determines it); if_redundant?: \"driven\" places it as a driven (reference) dimension instead. scale_sketch?: bool (the dimension tool sends true): when this is the sketch's first length dimension and nothing in the sketch fixes a size (no other driving length dimension, nothing fixed or projected), the whole sketch scales about its origin to the value instead of distorting. Returns measured in unit (mm or deg), and scaled: the factor when the sketch was scaled",),CommandSpec::new("sketch.constraint.horizontal_vertical", "Horizontal/Vertical", c_horizontal_vertical).at("SKETCH", "CONSTRAINTS")diff --git a/crates/engine/src/cmd/sketch_constraints_tests.rs b/crates/engine/src/cmd/sketch_constraints_tests.rsindex 9238f63121..1b2306ff93 100644--- a/crates/engine/src/cmd/sketch_constraints_tests.rs+++ b/crates/engine/src/cmd/sketch_constraints_tests.rs@@ -815,6 +815,8 @@ fn a_dimension_value_can_define_a_user_parameter() {("w = 1 in - 5.4 mm", "1 in - 5.4 mm"),("w = len / 3", "len / 3"),("w = sqrt(400)", "sqrt(400)"),+ ("#w = 20", "20"),+ ("#w = len / 3", "len / 3"),("w = 2 * (len / 6)", "2 * (len / 6)"),("w = min(len, 20 mm)", "min(len, 20 mm)"),];diff --git a/crates/ui-egui/tests/scenarios/sketch_dimension_defines_parameter_hash.json b/crates/ui-egui/tests/scenarios/sketch_dimension_defines_parameter_hash.jsonnew file mode 100644index 0000000000..f5c50ee6b2--- /dev/null+++ b/crates/ui-egui/tests/scenarios/sketch_dimension_defines_parameter_hash.json@@ -0,0 +1,19 @@+[+ {"note": "#2371: typing `#width = 40` (the `#` marks a new parameter) into a new dimension's value creates the user parameter `width` and drives the dimension by it, in one step; the parameter then drives the sketch."},+ {"call": "engine.execute", "params": {"command": "sketch.create", "params": {"plane": "XY"}}},+ {"call": "engine.execute", "params": {"command": "sketch.line", "params": {"points": [[0, 0], [30, 0]], "ids": ["l1"]}}},+ {"call": "ui.view", "params": {"view": "top"}},+ {"call": "ui.view", "params": {"view": "fit"}},+ {"start": "sketch.dimension"},+ {"click": {"sketch": [15, 0]}},+ {"click": {"sketch": [15, 6]}},+ {"text": "#width = 40"},+ {"key": "Enter"},+ {"key": "Escape"},+ {"expect": {"errors": 0}},+ {"expect": {"param": {"width": 40}}},+ {"expect": {"query": {"command": "document.inspect", "path": ["params", 1, "expression"], "value": "width"}}},+ {"expect": {"query": {"command": "sketch.inspect", "path": ["curves", 0, "end_at", 0], "value": 40}}},+ {"call": "engine.execute", "params": {"command": "parameters.change", "params": {"name": "width", "expression": "25"}}},+ {"expect": {"query": {"command": "sketch.inspect", "path": ["curves", 0, "end_at", 0], "value": 25}}}+]diff --git a/docs/book/src/guide/parameters.md b/docs/book/src/guide/parameters.mdindex 23f6b39717..7584e8feb2 100644--- a/docs/book/src/guide/parameters.md+++ b/docs/book/src/guide/parameters.md@@ -10,7 +10,7 @@ name (`d1`, `d2`, …). Click another driving dimension to use its name: when thjust a number (such as `10` or `10 mm`) the name replaces it; otherwise it goes in at thecursor, replacing selected text and keeping what you typed, so typing `10 + ` and clicking`d1` gives `10 + d1`. Add arithmetic such as `/ 2` to build a dependent dimension. Press **Enter** to apply-the expression or **Escape** to cancel the edit. When placing a new dimension, `width = 40` adds+the expression or **Escape** to cancel the edit. When placing a new dimension, `width = 40` (or `#width = 40`) addsthe user parameter `width` with that value (a bare number is in the design's units) anddimensions with it; a name that is already taken is refused.Add and edit user parameters through the existing **Change Parameters** command.diff --git a/docs/book/src/guide/sketching.md b/docs/book/src/guide/sketching.mdindex 953d425a18..d632cdd210 100644--- a/docs/book/src/guide/sketching.md+++ b/docs/book/src/guide/sketching.md@@ -204,7 +204,7 @@ Press D, click what you want to measure and type the value: as soon as the clickdimension (one line, circle or arc, or the second point or line of a distance or angle), itsvalue box takes the keyboard with the measured value selected, so typing replaces it. Numbers,units (`1 in`), expressions (`2*width`) and parameter names all work; letters go into the box,-not to tool shortcuts. `width = 40` defines the user parameter `width` and dimensions with it,+not to tool shortcuts. `width = 40` (or `#width = 40`) defines the user parameter `width` and dimensions with it,in one step. While you type, the dimension text follows the cursor, and Enter placesit there (or in its default place if you haven't moved the cursor) as one undo step. Esccancels the dimension.