diff --git a/crates/engine/src/cmd/sketch.rs b/crates/engine/src/cmd/sketch.rs index 78e742e44..4dc2cdeeb 100644 --- a/crates/engine/src/cmd/sketch.rs +++ b/crates/engine/src/cmd/sketch.rs @@ -141,7 +141,7 @@ pub static COMMANDS: &[CommandSpec] = &[ .at("SKETCH", "CONSTRAINTS") .icon("c_equal") .enabled(in_sketch) - .params("a, b: lines or circles/arcs"), + .params("a, b: lines or circles/arcs; or curves: [2 to 200 lines or circles/arcs], each made equal to the first, in one step"), CommandSpec::new("sketch.constraint.parallel", "Parallel", c_parallel) .at("SKETCH", "CONSTRAINTS") .icon("c_parallel") @@ -1925,8 +1925,43 @@ fn c_tangent(s: &mut Session, p: &Value) -> Result { }) } fn c_equal(s: &mut Session, p: &Value) -> Result { + if p.get("curves").is_some() { + return equal_curves(s, p); + } constrain(s, p, "sketch.constraint.equal", |sk| two_curves(sk, p, "sketch.constraint.equal").map(|(a, b)| ConstraintKind::Equal { a, b })) } + +/// Equal over a list of curves (#4985): each one is made equal to the first, in one edit (one +/// undo step). A pair already made equal is not constrained twice. The solve moves as little as it +/// can, so a dimensioned curve keeps its size and the others take it, whatever the order. +fn equal_curves(s: &mut Session, p: &Value) -> Result { + let cmd = "sketch.constraint.equal"; + let ids = string_list(p, "curves"); + if !(2..=200).contains(&ids.len()) { + return Err(bad(cmd, "`curves` must list 2 to 200 lines or circles/arcs")); + } + let (out, info) = edit(s, p, cmd, true, |sk, _| { + let mut curves: Vec = Vec::with_capacity(ids.len()); + for id in &ids { + let c = sk.curve_index(id).ok_or_else(|| bad(cmd, format!("unknown curve `{id}`")))?; + if curves.contains(&c) { + return Err(bad(cmd, format!("`{id}` is listed twice"))); + } + curves.push(c); + } + let Some((&first, rest)) = curves.split_first() else { return Err(bad(cmd, "`curves` is empty")) }; + let mut out = Vec::new(); + for &c in rest { + let already = + sk.constraints.iter().any(|k| matches!(k.kind, ConstraintKind::Equal { a, b } if (a == first && b == c) || (a == c && b == first))); + if !already { + out.push(add_c(sk, ConstraintKind::Equal { a: first, b: c })?); + } + } + Ok(out) + })?; + Ok(json!({"constraints": out, "sketch": info})) +} fn c_parallel(s: &mut Session, p: &Value) -> Result { constrain(s, p, "sketch.constraint.parallel", |sk| { two_curves_or_axes(sk, p, "sketch.constraint.parallel").map(|(a, b)| ConstraintKind::Parallel { a, b }) diff --git a/crates/engine/src/cmd/sketch_constraints_tests.rs b/crates/engine/src/cmd/sketch_constraints_tests.rs index bf6e30db8..9238f6312 100644 --- a/crates/engine/src/cmd/sketch_constraints_tests.rs +++ b/crates/engine/src/cmd/sketch_constraints_tests.rs @@ -1058,3 +1058,52 @@ fn midpoint_pair_orientation_uses_the_text_box_and_respects_explicit_types() { assert!((solidcraft_sketch::measure_dimension(&sk, &c.kind).unwrap() - expected).abs() < 1e-6); } } + +/// Four circles, `c1` dimensioned to radius 5 (diameter 10). +fn four_circles() -> Session { + let mut s = new_sketch(); + for (x, r) in [(0, 5), (20, 3), (40, 7), (60, 4)] { + run(&mut s, "sketch.circle.center", json!({"center": [x, 0], "radius": r})); + } + run(&mut s, "sketch.dimension", json!({"entities": ["c1"], "value": 10})); + s +} + +fn radius_of(s: &Session, id: &str) -> f64 { + let sk = sketch(s); + sk.radius(sk.curve_index(id).unwrap()).unwrap() +} + +#[test] +fn equal_over_a_list_of_curves_makes_them_all_equal_in_one_step() { + // #4985: whatever the order, the dimensioned circle keeps its size and the others take it. + for order in [["c1", "c2", "c3", "c4"], ["c4", "c3", "c2", "c1"]] { + let mut s = four_circles(); + let r1 = radius_of(&s, "c1"); + let undo = s.undo.len(); + let out = run(&mut s, "sketch.constraint.equal", json!({"curves": order})); + assert_eq!(out["constraints"].as_array().unwrap().len(), 3, "{order:?}"); + assert_eq!(s.undo.len(), undo + 1, "one undo step"); + for id in ["c1", "c2", "c3", "c4"] { + assert!((radius_of(&s, id) - r1).abs() < 1e-6, "{order:?} {id}: {}", radius_of(&s, id)); + } + // Applying it again adds nothing: those pairs are already equal. + let again = run(&mut s, "sketch.constraint.equal", json!({"curves": order})); + assert!(again["constraints"].as_array().unwrap().is_empty()); + } +} + +#[test] +fn equal_over_a_list_refuses_bad_lists_and_conflicts_without_changing_the_sketch() { + let mut s = four_circles(); + let before = s.doc.to_json(); + for bad in [json!({"curves": ["c1"]}), json!({"curves": ["c1", "c1"]}), json!({"curves": ["c1", "nope"]}), json!({"curves": vec!["c1"; 201]})] { + assert!(s.execute("sketch.constraint.equal", &bad).is_err(), "{bad}"); + assert_eq!(s.doc.to_json(), before, "{bad}"); + } + // Two circles dimensioned to different sizes can't be made equal: nothing changes. + run(&mut s, "sketch.dimension", json!({"entities": ["c2"], "value": 6})); + let before = s.doc.to_json(); + assert!(s.execute("sketch.constraint.equal", &json!({"curves": ["c1", "c2", "c3"]})).is_err()); + assert_eq!(s.doc.to_json(), before); +} diff --git a/crates/ui-egui/src/tools.rs b/crates/ui-egui/src/tools.rs index 3bf1a155d..10dabc934 100644 --- a/crates/ui-egui/src/tools.rs +++ b/crates/ui-egui/src/tools.rs @@ -541,6 +541,8 @@ pub(crate) fn constraint_preselection(command: &str, selection: &[solidcraft_eng match (command, selection) { ("sketch.constraint.fix", _) => selected_ids(false, 1).map(|ids| json!({"entities":ids})), ("sketch.constraint.polygon", _) => selected_ids(true, 3).map(|ids| json!({"lines":ids})), + // Three or more curves: all of them equal, in pick order (#4985). Two keep the `a, b` form. + ("sketch.constraint.equal", _) if selection.len() >= 3 => selected_ids(true, 3).map(|ids| json!({"curves":ids})), ("sketch.constraint.curvature", [SketchCurve { id: a }, SketchCurve { id: b }]) if a != b => Some(json!({"a":a,"b":b})), ("sketch.constraint.horizontal_vertical", [SketchCurve { id }]) => Some(json!({"line":id})), ("sketch.constraint.horizontal_vertical", [SketchPoint { id: a }, SketchPoint { id: b }]) if a != b => Some(json!({"points":[a,b]})), diff --git a/crates/ui-egui/tests/constraint_preselection.rs b/crates/ui-egui/tests/constraint_preselection.rs index 5baca2f6a..d32d85f2e 100644 --- a/crates/ui-egui/tests/constraint_preselection.rs +++ b/crates/ui-egui/tests/constraint_preselection.rs @@ -102,6 +102,39 @@ fn selected_circles_apply_constraints() { } } +/// #4985: Equal with three or more curves selected makes them all equal, in one undo step. With +/// one circle dimensioned, the others take its size. +#[test] +fn selected_circles_all_become_equal() { + let mut h = Harness::new(); + h.run(&json!([ + {"call":"engine.execute","params":{"command":"sketch.create","params":{"plane":"XY"}}}, + {"call":"engine.execute","params":{"command":"sketch.circle.center","params":{"center":[0,0],"radius":5}}}, + {"call":"engine.execute","params":{"command":"sketch.circle.center","params":{"center":[20,0],"radius":3}}}, + {"call":"engine.execute","params":{"command":"sketch.circle.center","params":{"center":[40,0],"radius":7}}}, + {"call":"engine.execute","params":{"command":"sketch.circle.center","params":{"center":[60,0],"radius":4}}}, + {"call":"engine.execute","params":{"command":"sketch.dimension","params":{"entities":["c1"]}}} + ])) + .unwrap(); + let radius = |h: &Harness, id: &str| { + let sk = &h.app.session.model.state().sketches[0].sketch; + sk.radius(sk.curve_index(id).unwrap()).unwrap() + }; + let r1 = radius(&h, "c1"); + h.app.session.selection = ["c4", "c3", "c2", "c1"].iter().map(|id| Sel::SketchCurve { id: (*id).into() }).collect(); + let undo = h.app.session.undo.len(); + h.run(&json!([{"start":"sketch.constraint.equal"}])).unwrap(); + assert!(!h.app.status.as_ref().is_some_and(|(_, _, error)| *error), "{:?}", h.app.status); + assert_eq!(h.app.session.undo.len(), undo + 1, "one undo step"); + assert!(h.app.tool.is_none(), "applied at once, no pick tool"); + assert!(h.app.session.selection.is_empty()); + let sk = &h.app.session.model.state().sketches[0].sketch; + assert_eq!(sk.constraints.iter().filter(|c| matches!(c.kind, ConstraintKind::Equal { .. })).count(), 3); + for id in ["c1", "c2", "c3", "c4"] { + assert!((radius(&h, id) - r1).abs() < 1e-6, "{id}: {} vs the dimensioned {r1}", radius(&h, id)); + } +} + #[test] fn ambiguous_selection_preserves_tool_first_workflow() { for selection in [