diff --git a/crates/engine/src/cmd/sketch_constraints_tests.rs b/crates/engine/src/cmd/sketch_constraints_tests.rs index 87f1eb451..0746182af 100644 --- a/crates/engine/src/cmd/sketch_constraints_tests.rs +++ b/crates/engine/src/cmd/sketch_constraints_tests.rs @@ -477,6 +477,49 @@ fn symmetry_slides_a_rectangle_whose_width_is_dimensioned() { assert!((r.x - l.x - 42.0).abs() < 1e-6 && (l.x + 21.0).abs() < 1e-6, "sides at ±21: {l:?} {r:?}"); } +#[test] +fn symmetry_pairs_the_ends_of_fixed_lines_drawn_the_opposite_way() { + // #1796: the sides of a face outline run in opposite directions (up one side, down the + // other). Pairing start with start asked for a reflection no line gives, and centring a + // construction line between them was refused as "neither entity can move". + let mut s = new_sketch(); + run(&mut s, "sketch.line", json!({"points": [[0, 0], [0, 20]], "ids": ["left"]})); + run(&mut s, "sketch.line", json!({"points": [[10, 20], [10, 0]], "ids": ["right"]})); + run(&mut s, "sketch.line", json!({"points": [[3, 2], [3, 18]], "construction": true, "ids": ["mid"]})); + run(&mut s, "sketch.constraint.fix", json!({"entities": ["left", "right"]})); + run(&mut s, "sketch.constraint.symmetry", json!({"a": "left", "b": "right", "line": "mid"})); + let sk = sketch(&s); + let at = |r: &str| sk.point(sk.resolve_point(r).unwrap()).unwrap(); + for end in ["mid.start", "mid.end"] { + assert!((at(end).x - 5.0).abs() < 1e-7, "{end} at {:?}", at(end)); + } + assert!((at("mid.end").y - at("mid.start").y - 16.0).abs() < 1e-7, "the line keeps its length"); +} + +#[test] +fn symmetry_pairs_ends_by_their_mirror_images() { + // Free lines: each `b` end goes to the mirror image of the `a` end nearest it, so a line + // drawn either way round mirrors without flipping, and lines meeting at the axis (a V) or + // end to end along it keep their pairs. + for (a, b, axis, want) in [ + ([[0.0, 0.0], [0.0, 20.0]], [[10.0, 21.0], [10.0, 1.0]], [[5.0, -5.0], [5.0, 30.0]], [[10.0, 20.0], [10.0, 0.0]]), + ([[0.0, 0.0], [4.0, 10.0]], [[10.0, 1.0], [6.0, 9.0]], [[5.0, -5.0], [5.0, 30.0]], [[10.0, 0.0], [6.0, 10.0]]), + ([[0.0, 0.0], [10.0, 0.0]], [[29.0, 1.0], [21.0, -1.0]], [[15.0, -5.0], [15.0, 5.0]], [[30.0, 0.0], [20.0, 0.0]]), + ] { + let mut s = new_sketch(); + run(&mut s, "sketch.line", json!({"points": a, "ids": ["a"]})); + run(&mut s, "sketch.line", json!({"points": b, "ids": ["b"]})); + run(&mut s, "sketch.line", json!({"points": axis, "construction": true, "ids": ["m"]})); + run(&mut s, "sketch.constraint.fix", json!({"entities": ["a", "m"]})); + run(&mut s, "sketch.constraint.symmetry", json!({"a": "a", "b": "b", "line": "m"})); + let sk = sketch(&s); + let at = |r: &str| sk.point(sk.resolve_point(r).unwrap()).unwrap(); + for (end, [x, y]) in ["b.start", "b.end"].into_iter().zip(want) { + assert!(at(end).dist(Vec2::new(x, y)) < 1e-6, "{a:?} {b:?}: {end} at {:?}, want [{x}, {y}]", at(end)); + } + } +} + #[test] fn midpoint_alignment_persists_undoes_and_survives_endpoint_edits() { use solidcraft_sketch::ConstraintKind; diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index e53a3626c..3e245c2e4 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -1132,7 +1132,9 @@ impl SolveApp { self.last_command = Some((spec.id.to_string(), spec.label.to_string())); } if let Some(mut t) = tools::Tool::for_command(id) { - if let Some(params) = tools::constraint_preselection(id, &self.session.selection) { + let state = self.session.world_state(); + let sketch = self.session.active_sketch.and_then(|s| state.sketch(s)).map(|ss| &ss.sketch); + if let Some(params) = tools::constraint_preselection(id, &self.session.selection, sketch) { if self.run(id, params).is_ok() { let _ = self.session.execute("select.clear", &json!({})); self.set_status("Constraint applied", false); diff --git a/crates/ui-egui/src/symmetry_pick_tests.rs b/crates/ui-egui/src/symmetry_pick_tests.rs index 1dcb705bc..d24f7b1c0 100644 --- a/crates/ui-egui/src/symmetry_pick_tests.rs +++ b/crates/ui-egui/src/symmetry_pick_tests.rs @@ -1,4 +1,5 @@ -//! The Symmetry tool's third pick may be the origin point or an origin axis (#1075). +//! The Symmetry tool's third pick may be the origin point or an origin axis (#1075); with three +//! lines, the construction line between the other two is the symmetry line in any pick order (#1796). use serde_json::json; use solidcraft_engine::geom::Vec2; @@ -44,3 +45,54 @@ fn symmetry_about_an_origin_axis() { let (p, q) = (point(&h, "p"), point(&h, "q")); assert!((p.x + q.x).abs() < 1e-6 && (p.y - q.y).abs() < 1e-6, "mirrored in the Y axis: {p:?} {q:?}"); } + +/// Two fixed lines and a construction line between them, off centre (#1796). +fn between_two_lines() -> Harness { + let mut h = Harness::new(); + h.app.run("sketch.create", json!({"plane":"XY"})).unwrap(); + h.app.run("sketch.line", json!({"points":[[0,0],[0,20]],"ids":["left"]})).unwrap(); + h.app.run("sketch.line", json!({"points":[[10,20],[10,0]],"ids":["right"]})).unwrap(); + h.app.run("sketch.line", json!({"points":[[3,2],[3,18]],"ids":["mid"],"construction":true})).unwrap(); + h.app.run("sketch.constraint.fix", json!({"entities":["left","right"]})).unwrap(); + h.app.cam.distance = 80.0; + h.settle(); + h +} + +fn centred(h: &Harness) -> bool { + [point(h, "mid.start"), point(h, "mid.end")].iter().all(|p| (p.x - 5.0).abs() < 1e-6) +} + +#[test] +fn the_construction_line_is_the_symmetry_line_whatever_the_pick_order() { + for order in [["mid", "left", "right"], ["left", "mid", "right"], ["left", "right", "mid"]] { + let mut h = between_two_lines(); + h.app.start("sketch.constraint.symmetry"); + for id in order { + let xy = match id { + "left" => [0, 10], + "right" => [10, 10], + _ => [3, 10], + }; + let at = h.call("ui.at", json!({ "sketch": xy }))["result"].clone(); + h.step(&json!({ "click": at })).unwrap(); + } + assert!(centred(&h), "{order:?}: mid at {:?}", point(&h, "mid.start")); + } +} + +#[test] +fn three_selected_lines_take_the_construction_line_as_the_symmetry_line() { + use solidcraft_engine::Sel::SketchCurve; + let h = between_two_lines(); + let state = h.app.session.world_state(); + let sk = &state.sketch(h.app.session.active_sketch.unwrap()).unwrap().sketch; + let sel = |ids: [&str; 3]| ids.map(|id| SketchCurve { id: id.into() }); + for ids in [["mid", "left", "right"], ["right", "mid", "left"]] { + let params = crate::tools::constraint_preselection("sketch.constraint.symmetry", &sel(ids), Some(sk)).unwrap(); + assert_eq!(params["line"], "mid", "{ids:?}"); + } + // Without a construction line among them, the last one stays the symmetry line. + let params = crate::tools::constraint_preselection("sketch.constraint.symmetry", &sel(["left", "right", "mid"]), None).unwrap(); + assert_eq!(params["line"], "mid"); +} diff --git a/crates/ui-egui/src/tools.rs b/crates/ui-egui/src/tools.rs index d31f9f3c0..b814e3226 100644 --- a/crates/ui-egui/src/tools.rs +++ b/crates/ui-egui/src/tools.rs @@ -533,7 +533,11 @@ fn run_shape(app: &mut SolveApp, t: &Tool) -> Result, String> { } /// Only consume a complete, unambiguous selection; command validation owns geometry. -pub(crate) fn constraint_preselection(command: &str, selection: &[solidcraft_engine::Sel]) -> Option { +pub(crate) fn constraint_preselection( + command: &str, + selection: &[solidcraft_engine::Sel], + sketch: Option<&solidcraft_engine::sketch::Sketch>, +) -> Option { use solidcraft_engine::Sel::{SketchCurve, SketchPoint}; let selected_ids = |curves_only: bool, minimum: usize| -> Option> { if !(minimum..=200).contains(&selection.len()) { @@ -572,19 +576,14 @@ pub(crate) fn constraint_preselection(command: &str, selection: &[solidcraft_eng ) => Some(json!({"point":point,"line":line})), ("sketch.constraint.midpoint", [SketchCurve { id: a }, SketchCurve { id: b }]) if a != b => Some(json!({"a":a,"b":b})), ("sketch.constraint.symmetry", [a, b, c]) => { - let mut points = Vec::new(); - let mut lines = Vec::new(); - for entity in [a, b, c] { - match entity { - SketchPoint { id } => points.push(id), - SketchCurve { id } => lines.push(id), - _ => return None, - } - } - match (points.as_slice(), lines.as_slice()) { - ([a, b], [line]) if a != b => Some(json!({"a":a,"b":b,"line":line})), - _ => None, - } + let picks = [a, b, c] + .into_iter() + .map(|entity| match entity { + SketchPoint { id } | SketchCurve { id } => Some(json!(id)), + _ => None, + }) + .collect::>>()?; + symmetry_args(&picks, sketch) } _ => None, } @@ -639,6 +638,29 @@ fn sketch_axis_of(app: &SolveApp, name: &str) -> Option<&'static str> { } } +/// Symmetry's arguments from three picked sketch ids in any order (#1796): with two points and +/// a line, the line; with three lines, the only construction line or centerline among them (the +/// line drawn between the two it mirrors); otherwise the last pick, as before. +fn symmetry_args(picks: &[Value], sketch: Option<&solidcraft_engine::sketch::Sketch>) -> Option { + let curve = |v: &Value| match v.as_str() { + Some(id) => sketch.and_then(|sk| sk.curve_index(id).and_then(|i| sk.curves.get(i))), + None => None, + }; + let is_line = |v: &Value| matches!(v.as_str(), Some("x" | "y")) || curve(v).is_some(); + let is_axis = |v: &Value| matches!(v.as_str(), Some("x" | "y")) || curve(v).is_some_and(|c| c.construction || c.centerline); + let only = |f: &dyn Fn(&Value) -> bool| { + let hits: Vec = (0..picks.len()).filter(|i| picks.get(*i).is_some_and(f)).collect(); + hits.first().copied().filter(|_| hits.len() == 1) + }; + if picks.len() != 3 { + return None; + } + let line = only(&is_line).or_else(|| only(&is_axis)).unwrap_or(2); + let mut rest = (0..3).filter(|i| *i != line).filter_map(|i| picks.get(i)); + let (a, b, line) = (rest.next()?, rest.next()?, picks.get(line)?); + (a != b).then(|| json!({"a": a, "b": b, "line": line})) +} + fn run_pick(app: &mut SolveApp, t: &Tool) { let k = &t.picks; let params = match t.cmd { @@ -646,7 +668,18 @@ fn run_pick(app: &mut SolveApp, t: &Tool) { "sketch.constraint.coincident" => json!({"a": k[0], "b": k[1]}), // In pick order: a point and a line either way round, or two lines to centre. "sketch.constraint.midpoint" => json!({"a": k[0], "b": k[1]}), - "sketch.constraint.symmetry" => json!({"a": k[0], "b": k[1], "line": k[2]}), + "sketch.constraint.symmetry" => { + let picks: Vec = k.iter().map(|pick| json!(pick)).collect(); + let params = { + let state = app.session.world_state(); + symmetry_args(&picks, app.session.active_sketch.and_then(|id| state.sketch(id)).map(|ss| &ss.sketch)) + }; + let Some(params) = params else { + app.set_status("Symmetry: pick two points or two curves and the line between them", true); + return; + }; + params + } _ => json!({"a": k[0], "b": k[1]}), }; let _ = app.run(t.cmd, params); diff --git a/crates/ui-egui/tests/scenarios/sketch_symmetry_face_edges.json b/crates/ui-egui/tests/scenarios/sketch_symmetry_face_edges.json new file mode 100644 index 000000000..049c27988 --- /dev/null +++ b/crates/ui-egui/tests/scenarios/sketch_symmetry_face_edges.json @@ -0,0 +1,29 @@ +[ + {"note": "#1796: on a sketch on a face, Symmetry centres a construction line between two of the face's edges (its projected outline, whose sides run in opposite directions). Before, that was refused: 'neither entity can move'. The picks may come in any order: the construction line is the symmetry line."}, + {"call": "engine.execute", "params": {"command": "sketch.create", "params": {"plane": "XY"}}}, + {"call": "engine.execute", "params": {"command": "sketch.rectangle.two_point", "params": {"p0": [0, 0], "p1": [20, 30]}}}, + {"call": "engine.execute", "params": {"command": "sketch.finish", "params": {}}}, + {"call": "engine.execute", "params": {"command": "solid.extrude", "params": {"distance": 5}}}, + {"call": "engine.execute", "params": {"command": "sketch.create", "params": {"plane": {"face": [10, 15, 5]}}}}, + {"call": "engine.execute", "params": {"command": "sketch.line", "params": {"points": [[7, 4], [7, 26]], "ids": ["lc"], "construction": true}}}, + {"call": "ui.view", "params": {"view": "top"}}, + {"call": "ui.view", "params": {"view": "fit"}}, + {"start": "sketch.constraint.symmetry"}, + {"click": {"world": [0, 15, 5]}}, + {"click": {"world": [20, 15, 5]}}, + {"click": {"sketch": [7, 15]}}, + {"key": "Escape"}, + {"shot": "centred"}, + {"expect": {"errors": 0}}, + {"expect": {"query": {"command": "sketch.inspect", "path": ["curves", 4, "start_at", 0], "value": 10}}}, + {"expect": {"query": {"command": "sketch.inspect", "path": ["curves", 4, "end_at", 0], "value": 10}}}, + {"call": "engine.execute", "params": {"command": "edit.undo", "params": {}}}, + {"expect": {"query": {"command": "sketch.inspect", "path": ["curves", 4, "start_at", 0], "value": 7}}}, + {"start": "sketch.constraint.symmetry"}, + {"click": {"sketch": [7, 15]}}, + {"click": {"world": [0, 15, 5]}}, + {"click": {"world": [20, 15, 5]}}, + {"key": "Escape"}, + {"expect": {"errors": 0}}, + {"expect": {"query": {"command": "sketch.inspect", "path": ["curves", 4, "start_at", 0], "value": 10}}} +]