SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Sketch: symmetry about a line pairs line ends by mirror image (#2961, #2940) (#3029)
rlartota595805fc81f
diff --git a/crates/engine/src/cmd/sketch.rs b/crates/engine/src/cmd/sketch.rsindex d8d3f247e9..04505396a4 100644--- a/crates/engine/src/cmd/sketch.rs+++ b/crates/engine/src/cmd/sketch.rs@@ -1864,8 +1864,11 @@ fn c_symmetry(s: &mut Session, p: &Value) -> Result<Value> {let cb = sk.curve_index(rb).ok_or_else(|| bad(cmd, format!("unknown entity `{rb}`")))?;match (sk.curves.get(ca).map(|c| c.kind.clone()), sk.curves.get(cb).map(|c| c.kind.clone())) {(Some(CurveKind::Line { a: a0, b: a1 }), Some(CurveKind::Line { a: b0, b: b1 })) => {- out.push(add_c(sk, ConstraintKind::Symmetric { p: a0, q: b0, l })?);- out.push(add_c(sk, ConstraintKind::Symmetric { p: a1, q: b1, l })?);+ // Opposite sides of a rectangle run in opposite directions: pair each end+ // with the end of the other line nearest its mirror image (#2961, #2940).+ let (p0, p1) = if mirror_pairing_is_crossed(sk, l, [a0, a1], [b0, b1]) { (b1, b0) } else { (b0, b1) };+ out.push(add_c(sk, ConstraintKind::Symmetric { p: a0, q: p0, l })?);+ out.push(add_c(sk, ConstraintKind::Symmetric { p: a1, q: p1, l })?);}(Some(_), Some(_)) => {let pa = round_center(sk, ca).ok_or_else(|| bad(cmd, "symmetry needs two lines or two circles/arcs"))?;@@ -1886,6 +1889,25 @@ fn c_symmetry(s: &mut Session, p: &Value) -> Result<Value> {Ok(json!({"constraints": ids, "sketch": info}))}+/// True when the ends of line `a` mirror (about the line `l`) closer onto the ends of `b` crossed+/// (`a0` with `b1`, `a1` with `b0`) than in order. A degenerate symmetry line keeps the order.+fn mirror_pairing_is_crossed(sk: &Sketch, l: usize, a: [usize; 2], b: [usize; 2]) -> bool {+ let Some((o, e)) = line_pts(sk, l) else { return false };+ let d = e - o;+ let len2 = d.x * d.x + d.y * d.y;+ if len2 < 1e-18 {+ return false;+ }+ let pt = |i: usize| sk.point(i).unwrap_or_default();+ let near = |i: usize, j: usize| {+ let v = pt(i) - o;+ let t = (v.x * d.x + v.y * d.y) / len2;+ let foot = o + d * t;+ (foot * 2.0 - pt(i)).dist(pt(j))+ };+ near(a[0], b[1]) + near(a[1], b[0]) < near(a[0], b[0]) + near(a[1], b[1])+}+/// Symmetry about point `c`: two points, two circles/arcs (their centres, and equal radii) or two/// lines (each end with the end of the other line nearest its mirror image). The constraints/// (the construction lines show in the response's `created`).diff --git a/crates/engine/src/cmd/sketch_constraints_tests.rs b/crates/engine/src/cmd/sketch_constraints_tests.rsindex f835ded7e2..795ce0fad4 100644--- a/crates/engine/src/cmd/sketch_constraints_tests.rs+++ b/crates/engine/src/cmd/sketch_constraints_tests.rs@@ -374,6 +374,66 @@ fn symmetry_moves_a_free_line_between_two_fixed_lines() {}}+/// A 20 x 10 rectangle (sides l1..l4; left = l4 and right = l2 run in opposite directions) and a+/// free vertical line at x = -20 as the symmetry line (#2940, #2961).+fn rectangle_and_axis(dimension_left: bool) -> (Session, Vec<String>) {+ let mut s = new_sketch();+ let r = run(&mut s, "sketch.rectangle.two_point", json!({"p0": [-12, 0], "p1": [8, 10]}));+ let sides = ids(&r["curves"]);+ run(&mut s, "sketch.line", json!({"points": [[-20, -5], [-20, 15]], "construction": true, "ids": ["axis"]}));+ if dimension_left {+ run(&mut s, "sketch.dimension", json!({"entities": [sides[3].clone()], "value": 10}));+ }+ (s, sides)+}++/// Both sides keep their 10 mm height and their ends mirror in x about `axis_x`.+fn assert_sides_mirror_with_height(s: &Session, left: &str, right: &str, axis_x: f64) {+ let sk = sketch(s);+ let at = |r: String| sk.point(sk.resolve_point(&r).unwrap()).unwrap();+ for side in [left, right] {+ let (a, b) = (at(format!("{side}.start")), at(format!("{side}.end")));+ assert!((a.y - b.y).abs() > 9.999 && (a.y - b.y).abs() < 10.001, "{side} height {:?} {:?}", a, b);+ assert!((a.x - b.x).abs() < 1e-7, "{side} not vertical: {:?} {:?}", a, b);+ }+ let (l, r) = (at(format!("{left}.start")), at(format!("{right}.start")));+ assert!((l.x + r.x - 2.0 * axis_x).abs() < 1e-6, "sides not mirrored: {l:?} {r:?}");+}++#[test]+fn symmetry_of_rectangle_sides_keeps_their_height() {+ // #2940: the opposite sides of a rectangle run in opposite directions, so pairing their ends+ // by index forced both heights to 0.+ let (mut s, sides) = rectangle_and_axis(false);+ run(&mut s, "sketch.constraint.symmetry", json!({"a": sides[3], "b": sides[1], "line": "axis"}));+ assert_sides_mirror_with_height(&s, &sides[3], &sides[1], -20.0);+}++#[test]+fn symmetry_of_a_dimensioned_rectangle_side_is_accepted() {+ // #2961: with a length on a side, the same symmetry was refused as "neither entity can move".+ let (mut s, sides) = rectangle_and_axis(true);+ run(&mut s, "sketch.constraint.symmetry", json!({"a": sides[3], "b": sides[1], "line": "axis"}));+ assert_sides_mirror_with_height(&s, &sides[3], &sides[1], -20.0);+}++#[test]+fn symmetry_of_rectangle_sides_with_swapped_arguments() {+ let (mut s, sides) = rectangle_and_axis(true);+ run(&mut s, "sketch.constraint.symmetry", json!({"a": sides[1], "b": sides[3], "line": "axis"}));+ assert_sides_mirror_with_height(&s, &sides[3], &sides[1], -20.0);+}++#[test]+fn symmetry_of_rectangle_sides_about_the_y_axis() {+ let mut s = new_sketch();+ let r = run(&mut s, "sketch.rectangle.two_point", json!({"p0": [-12, 0], "p1": [8, 10]}));+ let sides = ids(&r["curves"]);+ run(&mut s, "sketch.dimension", json!({"entities": [sides[3].clone()], "value": 10}));+ run(&mut s, "sketch.constraint.symmetry", json!({"a": sides[3], "b": sides[1], "line": "y"}));+ assert_sides_mirror_with_height(&s, &sides[3], &sides[1], 0.0);+}+#[test]fn midpoint_alignment_persists_undoes_and_survives_endpoint_edits() {use solidcraft_sketch::ConstraintKind;