diff --git a/crates/engine/src/cmd/sketch.rs b/crates/engine/src/cmd/sketch.rs index ec1ff880d..f78b78a5e 100644 --- a/crates/engine/src/cmd/sketch.rs +++ b/crates/engine/src/cmd/sketch.rs @@ -126,7 +126,7 @@ pub static COMMANDS: &[CommandSpec] = &[ .at("SKETCH", "CONSTRAINTS") .icon("c_coincident") .enabled(in_sketch) - .params("a, b: points, a point and a curve or sketch axis (x / y: the sketch's own axes, unless a curve has that id; an axis becomes a linked construction line on it, reported in created), or two distinct lines (joins their closest endpoints)"), + .params("a, b: points, a point and a curve or sketch axis (x / y: the sketch's own axes, unless a curve has that id; an axis becomes a linked construction line on it, reported in created), or two distinct lines (joins their closest endpoints), or two circles/arcs (the second lies on the first: concentric and equal, `constraints` lists both)"), CommandSpec::new("sketch.constraint.tangent", "Tangent", c_tangent) .at("SKETCH", "CONSTRAINTS") .icon("c_tangent") @@ -1728,6 +1728,9 @@ fn c_coincident(s: &mut Session, p: &Value) -> Result { let cmd = "sketch.constraint.coincident"; // Already tied (e.g. connected when it was drawn there): nothing to add. let sk = s.doc.sketch(target_sketch(s, p, cmd)?)?; + if let Some((a, b)) = two_round(sk, p) { + return coincident_circles(s, p, cmd, a, b); + } let pt = |k: &str| p.get(k).and_then(Value::as_str).and_then(|r| sk.resolve_point(r)); let line_points = coincident_line_points(sk, p, cmd)?; if let Some((a, b)) = line_points.or_else(|| pt("a").zip(pt("b"))) @@ -1758,6 +1761,41 @@ fn c_coincident(s: &mut Session, p: &Value) -> Result { }) } +/// `a` and `b` both circles or arcs (two different ones). +fn two_round(sk: &Sketch, p: &Value) -> Option<(usize, usize)> { + let round = |k: &str| { + let c = sk.curve_index(p.get(k)?.as_str()?)?; + matches!(sk.curves.get(c)?.kind, CurveKind::Circle { .. } | CurveKind::Arc { .. }).then_some(c) + }; + let (a, b) = (round("a")?, round("b")?); + (a != b).then_some((a, b)) +} + +/// Two circles or arcs made coincident lie on each other: one centre and one radius (Concentric +/// and Equal), the second pick moving onto the first, in one step. A half already there is +/// not added again. +fn coincident_circles(s: &mut Session, p: &Value, cmd: &str, a: usize, b: usize) -> Result { + let (ids, info) = edit(s, p, cmd, true, |sk, _| { + let rep = solve(&mut sk.clone()); + let pair = |x: usize, y: usize| (x, y) == (a, b) || (x, y) == (b, a); + let concentric = sk.constraints.iter().any(|c| !c.driven && matches!(c.kind, ConstraintKind::Concentric { a: x, b: y } if pair(x, y))); + let equal = sk.constraints.iter().any(|c| !c.driven && matches!(c.kind, ConstraintKind::Equal { a: x, b: y } if pair(x, y))); + let mut ids = Vec::new(); + if !concentric { + ids.push(add_c(sk, ConstraintKind::Concentric { a, b })?); + } + if !equal { + ids.push(add_c(sk, ConstraintKind::Equal { a, b })?); + } + if ids.is_empty() { + return Err(bad(cmd, "these circles already lie on each other")); + } + apply_in_order(sk, &rep, &[Ent::Curve(a)], Ent::Curve(b), cmd)?; + Ok(ids) + })?; + Ok(json!({"constraint": ids[0], "constraints": ids, "sketch": info})) +} + /// Four endpoint pairs, ordered independently of pick order when distances tie. fn coincident_line_points(sk: &Sketch, p: &Value, cmd: &str) -> Result> { let curve = |key: &str| p.get(key).and_then(Value::as_str).and_then(|id| sk.curve_index(id)); diff --git a/crates/engine/src/cmd/sketch_constraints_tests.rs b/crates/engine/src/cmd/sketch_constraints_tests.rs index ace5abcaf..c4e0d8a96 100644 --- a/crates/engine/src/cmd/sketch_constraints_tests.rs +++ b/crates/engine/src/cmd/sketch_constraints_tests.rs @@ -888,3 +888,38 @@ fn undoing_a_defining_dimension_removes_the_parameter_too() { assert_eq!(param_row(&mut s, "w")["expression"], "20"); assert!((line_length(&s, "la") - 20.0).abs() < 1e-6); } + +/// Coincident on two circles (or arcs) lays the second on the first: one centre and one +/// radius, in one step, and the first stays. It used to be refused ("two curves must both be +/// straight lines"). +#[test] +fn coincident_circles_lie_on_each_other() { + let mut s = new_sketch(); + run(&mut s, "sketch.circle.center", json!({"center":[0,0],"radius":10})); + run(&mut s, "sketch.circle.center", json!({"center":[3,1],"radius":8})); + let undo = s.undo.len(); + let r = run(&mut s, "sketch.constraint.coincident", json!({"a":"c1","b":"c2"})); + assert_eq!(r["constraints"].as_array().unwrap().len(), 2, "{r}"); + assert_eq!(s.undo.len(), undo + 1); + let sk = sketch(&s); + let circle = |id: &str| { + let c = sk.curve_index(id).unwrap(); + match sk.curves[c].kind { + solidcraft_sketch::CurveKind::Circle { c, r } => (sk.point(c).unwrap(), r), + _ => panic!("{id} is a circle"), + } + }; + let ((c1, r1), (c2, r2)) = (circle("c1"), circle("c2")); + assert!(c1.dist(Vec2::new(0.0, 0.0)) < 1e-6 && (r1 - 10.0).abs() < 1e-6, "the first stays"); + assert!(c2.dist(c1) < 1e-6 && (r2 - r1).abs() < 1e-6, "the second lies on it: {c2:?} r {r2}"); + // Again: nothing to add. + assert!(s.execute("sketch.constraint.coincident", &json!({"a":"c1","b":"c2"})).is_err()); + // An arc onto a circle. + run(&mut s, "sketch.arc.three_point", json!({"start":[20,0],"through":[25,5],"end":[30,0]})); + run(&mut s, "sketch.constraint.coincident", json!({"a":"c1","b":"a1"})); + let sk = sketch(&s); + let a = sk.curve_index("a1").unwrap(); + let solidcraft_sketch::CurveKind::Arc { c, a: start, .. } = sk.curves[a].kind else { panic!("a1 is an arc") }; + assert!(sk.point(c).unwrap().dist(Vec2::new(0.0, 0.0)) < 1e-6); + assert!((sk.point(start).unwrap().dist(sk.point(c).unwrap()) - 10.0).abs() < 1e-6); +} diff --git a/crates/ui-egui/tests/coincident_circles.rs b/crates/ui-egui/tests/coincident_circles.rs new file mode 100644 index 000000000..f43da1641 --- /dev/null +++ b/crates/ui-egui/tests/coincident_circles.rs @@ -0,0 +1,25 @@ +use serde_json::json; +use solidcraft_ui_egui::scenario::Harness; + +/// The Coincident tool takes two circles clicked by their edges and lays the second on the first. +#[test] +fn the_coincident_tool_lays_one_circle_on_another() { + let mut h = Harness::new(); + h.app.run("sketch.create", json!({"plane":"XY"})).unwrap(); + h.app.run("sketch.circle.center", json!({"center":[0,0],"radius":10})).unwrap(); + h.app.run("sketch.circle.center", json!({"center":[30,0],"radius":6})).unwrap(); + h.call("ui.view", json!({"view":"top"})); + h.call("ui.view", json!({"view":"fit"})); + h.frames(3); + h.step(&json!({"start":"sketch.constraint.coincident"})).unwrap(); + h.step(&json!({"click":{"world":[0,10,0]}})).unwrap(); + h.step(&json!({"click":{"world":[30,6,0]}})).unwrap(); + let st = h.app.session.world_state(); + let sk = &st.sketch(h.app.session.active_sketch.unwrap()).unwrap().sketch; + use solidcraft_engine::sketch::ConstraintKind; + let laid = sk.constraints.iter().filter(|k| matches!(k.kind, ConstraintKind::Concentric { .. } | ConstraintKind::Equal { .. })).count(); + assert_eq!(laid, 2, "{:?} {:?}", sk.constraints, h.app.status); + let c2 = sk.curve_index("c2").unwrap(); + let solidcraft_engine::sketch::CurveKind::Circle { c, r } = sk.curves[c2].kind else { panic!("c2 is a circle") }; + assert!(sk.point(c).unwrap().len() < 1e-6 && (r - 10.0).abs() < 1e-6); +}