From b94692497eddfc94331bd02bf2355f6e053cb310 Mon Sep 17 00:00:00 2001 From: leValdemar19 Date: Sun, 25 Oct 2026 22:17:01 +0000 Subject: [PATCH] Sketch Dimension: arc length from an arc and its end point (#754) (#4245) * claim: #754 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01SBN24y2TcqRcm2zn8Gkj27 * Sketch Dimension: arc length from an arc and its end point (#754) The arc-length dimension existed (sketch.dimension type arc_length) but the Dimension tool could only give an arc its radius. An arc and one of its own end points, in either order, now dimension the arc's length: click the arc, then its end. The pair was refused before ("cannot dimension that combination"), so no existing result changes. A point where the arc's end is (a coincident line end) counts as that end. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01SBN24y2TcqRcm2zn8Gkj27 --------- Co-authored-by: Claude Opus 5.5 --- crates/engine/src/cmd/sketch.rs | 14 +++++++++- .../src/cmd/sketch_constraints_tests.rs | 26 +++++++++++++++++++ .../sketch_dimension_arc_length.json | 16 ++++++++++++ docs/book/src/guide/sketching.md | 2 +- 4 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 crates/ui-egui/tests/scenarios/sketch_dimension_arc_length.json diff --git a/crates/engine/src/cmd/sketch.rs b/crates/engine/src/cmd/sketch.rs index 55417b4c6..7afd70afb 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:\" (held at the line's midpoint), \"on::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|distance_x|vertical|distance_y|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 point and a circle or arc: the distance from the point to its edge (or how far within it a point inside lies). Two circles or arcs: the gap between their edges (or how far they overlap when they cross, or the narrowest gap between them when one lies inside the other); dimension their centre points for the distance between centres. 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|distance_x|vertical|distance_y|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 point and a circle or arc: the distance from the point to its edge (or how far within it a point inside lies). Two circles or arcs: the gap between their edges (or how far they overlap when they cross, or the narrowest gap between them when one lies inside the other); dimension their centre points for the distance between centres. An arc and one of its end points: the arc's length (type arc_length does it from the arc alone). 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") @@ -2125,6 +2125,13 @@ fn symmetric_about_point(sk: &mut Sketch, a: usize, b: usize, c: usize, cmd: &st add_c(sk, ConstraintKind::Midpoint { p: c, l }) } +/// Is point `q` an end of arc `c`: one of its end points, or a point where one of them is? +fn arc_end(sk: &Sketch, c: usize, q: usize) -> bool { + let Some(CurveKind::Arc { a, b, .. }) = sk.curves.get(c).map(|x| &x.kind) else { return false }; + let at = |i: usize| sk.point(i); + *a == q || *b == q || at(q).is_some_and(|pq| [*a, *b].iter().any(|e| at(*e).is_some_and(|pe| pe.dist(pq) < 1e-9))) +} + fn c_symmetry(s: &mut Session, p: &Value) -> Result { let cmd = "sketch.constraint.symmetry"; let (ids, info) = edit(s, p, cmd, true, |sk, _| { @@ -2439,6 +2446,11 @@ fn dimension(s: &mut Session, p: &Value) -> Result { (ConstraintKind::PointLineDistance { p: q, l, value: d }, d) } } + // An arc and one of its own end points (or a point on that end): its length (#754). + ((Some(q), None), Some((None, Some(c)))) | ((None, Some(c)), Some((Some(q), None))) if arc_end(sk, c, q) => { + let len = arc_len(sk, c).ok_or_else(|| bad(cmd, "arc length needs an arc"))?; + (ConstraintKind::ArcLength { c, value: len }, len) + } ((None, Some(a)), Some((None, Some(b)))) if ty == "overall_length" => { let current = sk .center(a) diff --git a/crates/engine/src/cmd/sketch_constraints_tests.rs b/crates/engine/src/cmd/sketch_constraints_tests.rs index ac46d722a..ccbf55927 100644 --- a/crates/engine/src/cmd/sketch_constraints_tests.rs +++ b/crates/engine/src/cmd/sketch_constraints_tests.rs @@ -1339,3 +1339,29 @@ fn conflicting_component_dimension_is_still_refused() { assert!(error.contains("would over-constrain the sketch (conflicts with"), "{error}"); assert_eq!(serde_json::to_value(sketch(&s)).unwrap(), before); } + +/// #754: an arc and one of its end points, in either order, dimension the arc's length (the +/// pair was refused). A line's end that meets the arc's end counts too; another point does not. +#[test] +fn an_arc_and_its_end_point_dimension_the_arc_length() { + let arc_length = |s: &Session, a: &str| { + let sk = sketch(s); + match sk.segs(sk.curve_index(a).unwrap())[0] { + solidcraft_geom::Seg2::Arc { radius, sweep, .. } => radius * sweep.abs(), + _ => f64::NAN, + } + }; + for (entities, value) in [(json!(["a1", "a1.end"]), 20.0), (json!(["a1.start", "a1"]), 18.0), (json!(["a1", "l1.start"]), 25.0)] { + let mut s = new_sketch(); + run(&mut s, "sketch.arc.center_point", json!({"center": [0, 0], "start": [10, 0], "end": [0, 10], "id": "a1"})); + run(&mut s, "sketch.line", json!({"points": [[0, 10], [-15, 10]], "ids": ["l1"]})); + let r = run(&mut s, "sketch.dimension", json!({"entities": entities, "value": value})); + assert_eq!(r["type"], "ArcLengthDimension", "{entities}: {r}"); + assert!((arc_length(&s, "a1") - value).abs() < 1e-6, "{entities}: {}", arc_length(&s, "a1")); + } + let mut s = new_sketch(); + run(&mut s, "sketch.arc.center_point", json!({"center": [0, 0], "start": [10, 0], "end": [0, 10], "id": "a1"})); + run(&mut s, "sketch.point", json!({"point": [20, 20], "id": "p9"})); + let e = s.execute("sketch.dimension", &json!({"entities": ["a1", "p9"]})).map(|v| v["type"].clone()); + assert!(!matches!(e, Ok(ref t) if t == "ArcLengthDimension"), "a point off the arc's ends is not an arc length: {e:?}"); +} diff --git a/crates/ui-egui/tests/scenarios/sketch_dimension_arc_length.json b/crates/ui-egui/tests/scenarios/sketch_dimension_arc_length.json new file mode 100644 index 000000000..7c4b9ac81 --- /dev/null +++ b/crates/ui-egui/tests/scenarios/sketch_dimension_arc_length.json @@ -0,0 +1,16 @@ +[ + {"note": "#754: with the Dimension tool, an arc and then one of its end points dimension the arc's length."}, + {"call": "engine.execute", "params": {"command": "sketch.create", "params": {"plane": "XY"}}}, + {"call": "engine.execute", "params": {"command": "sketch.arc.center_point", "params": {"center": [0, 0], "start": [10, 0], "end": [0, 10], "id": "a1"}}}, + {"call": "ui.view", "params": {"view": "top"}}, + {"call": "ui.view", "params": {"view": "fit"}}, + {"start": "sketch.dimension"}, + {"click": {"sketch": [7.0710678, 7.0710678]}}, + {"click": {"sketch": [0, 10]}}, + {"text": "20"}, + {"key": "Enter"}, + {"key": "Escape"}, + {"expect": {"errors": 0}}, + {"expect": {"query": {"command": "sketch.inspect", "path": ["constraints", 1, "type"], "value": "arc_length"}}}, + {"expect": {"query": {"command": "sketch.inspect", "path": ["constraints", 1, "value"], "value": 20}}} +] diff --git a/docs/book/src/guide/sketching.md b/docs/book/src/guide/sketching.md index 8cd0c3a93..2b74623fa 100644 --- a/docs/book/src/guide/sketching.md +++ b/docs/book/src/guide/sketching.md @@ -204,7 +204,7 @@ The solver is SolidCraft's own (damped Gauss–Newton with degree-of-freedom ana coincident, horizontal/vertical, parallel, perpendicular, tangent, equal, concentric, collinear, midpoint, symmetry, fix, curvature (G2) and polygon. **Sketch Dimension** (D) adds linear, aligned, horizontal, vertical, point-to-line, line-to-circle, point-to-circle, circle-to-circle, -radius, diameter and angle dimensions. Each is a parameter +radius, diameter, arc length and angle dimensions. For an arc's length, click the arc and then one of its end points. Each is a parameter (`d1`, `d2`, …), so it can hold an expression. Press D, click what you want to measure and type the value: as soon as the click settles the