SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
3-Point Circle keeps snapped clicks as coincident constraints (#9858) (#9922)
Dahak Builds8986a670e501
diff --git a/crates/engine/src/cmd/sketch.rs b/crates/engine/src/cmd/sketch.rsindex 1191b6f738..1516589638 100644--- a/crates/engine/src/cmd/sketch.rs+++ b/crates/engine/src/cmd/sketch.rs@@ -94,7 +94,7 @@ pub static COMMANDS: &[CommandSpec] = &[.at("SKETCH", "CREATE").icon("circle3").enabled(in_sketch)- .params(concat!("p0, p1, p2; construction?: bool", connect_doc!(), created_doc!())),+ .params(concat!("p0, p1, p2: [x,y] or point ref (a referenced point stays on the circle); construction?: bool", connect_doc!(), created_doc!())),CommandSpec::new("sketch.arc.three_point", "3-Point Arc", arc_three).at("SKETCH", "CREATE").icon("arc3").enabled(in_sketch).params(concat!("start, end, through: [x,y] or point ref (a referenced through point stays on the arc); construction?: bool", connect_doc!(), created_doc!())),CommandSpec::new("sketch.arc.center_point", "Center Point Arc", arc_center).at("SKETCH", "CREATE")@@ -1409,12 +1409,21 @@ pub(super) fn circumcircle(a: Vec2, b: Vec2, c: Vec2) -> Option<(Vec2, f64)> {fn circle_three(s: &mut Session, p: &Value) -> Result<Value> {let cmd = "sketch.circle.three_point";- let (a, b, c) = (req_vec2(cmd, p, "p0")?, req_vec2(cmd, p, "p1")?, req_vec2(cmd, p, "p2")?);- let (o, r) = circumcircle(a, b, c).ok_or_else(|| bad(cmd, "the points are collinear"))?;- let (out, info) = draw(s, p, cmd, |sk, _| {+ let p = &super::sketch_project::snap_points(s, p, cmd)?;+ let (out, info) = draw(s, p, cmd, |sk, doc| {+ let (a, b, c) = (req_parg(sk, doc, p, "p0", cmd)?, req_parg(sk, doc, p, "p1", cmd)?, req_parg(sk, doc, p, "p2", cmd)?);+ let (o, r) = circumcircle(a.pos(), b.pos(), c.pos()).ok_or_else(|| bad(cmd, "the points are collinear"))?;let ci = sk.add_circle(o, r, None, None)?;mark_construction(sk, &[ci], p);- Ok((ids_of(sk, &[ci]), Vec::new()))+ // A click snapped to a point keeps the circle on it (#9858), as the centre circle's+ // `through` click does (#3099) and the 3-Point Arc's clicks do.+ let mut cons = Vec::new();+ for q in [a, b, c] {+ if let Some(point) = q.idx() {+ cons.push(add_c(sk, ConstraintKind::PointOnCurve { p: point, c: ci })?);+ }+ }+ Ok((ids_of(sk, &[ci]), cons))})?;Ok(result(out, info))}diff --git a/crates/engine/src/cmd/sketch_modify_tests.rs b/crates/engine/src/cmd/sketch_modify_tests.rsindex 072456841a..5537cf25ae 100644--- a/crates/engine/src/cmd/sketch_modify_tests.rs+++ b/crates/engine/src/cmd/sketch_modify_tests.rs@@ -1150,6 +1150,53 @@ fn centre_circle_through_a_point_keeps_it_on_the_circle() {assert!(!sketch(&s).constraints.iter().any(|k| matches!(k.kind, ConstraintKind::PointOnCurve { c, .. } if c == ci)));}+/// #9858: a 3-point circle drawn on snapped clicks keeps them. Drawn through a line's start,+/// its midpoint and another line's end, the circle stays on those points when the line is+/// dimensioned.+#[test]+fn three_point_circle_keeps_snapped_points() {+ let mut s = new_sketch();+ let l = ids(&run(&mut s, "sketch.line", json!({"points": [[0, 0], [40, 0]]}))["curves"])[0].clone();+ let v = ids(&run(&mut s, "sketch.line", json!({"points": [[20, 5], [20, 30]]}))["curves"])[0].clone();+ let r = run(&mut s, "sketch.circle.three_point", json!({"p0": format!("{l}.start"), "p1": format!("mid:{l}"), "p2": format!("{v}.end")}));+ let c = ids(&r["curves"])[0].clone();+ assert_eq!(ids(&r["constraints"]).len(), 3, "{r}");+ let sk = sketch(&s);+ let ci = sk.curve_index(&c).unwrap();+ let on_circle =+ |sk: &solidcraft_sketch::Sketch, p: usize, c: usize| sk.constraints.iter().any(|k| k.kind == ConstraintKind::PointOnCurve { p, c });+ let (start, tip) = (sk.resolve_point(&format!("{l}.start")).unwrap(), sk.resolve_point(&format!("{v}.end")).unwrap());+ // The midpoint snap holds its point at the line's midpoint, and the circle keeps it too.+ let mid = sk+ .constraints+ .iter()+ .find_map(|k| match k.kind {+ ConstraintKind::Midpoint { p, .. } => Some(p),+ _ => None,+ })+ .expect("midpoint snap point");+ for p in [start, mid, tip] {+ assert!(on_circle(&sk, p, ci), "{:?} is not on the circle: {:?}", sk.points[p].id, sk.constraints);+ }+ // Seeded as the circumcircle of the three clicks.+ let (o, rad) = (sk.center(ci).unwrap(), sk.radius(ci).unwrap());+ for p in [start, mid, tip] {+ assert!(close(o.dist(sk.points[p].pos), rad), "the circle does not pass its click: {o:?} {rad}");+ }+ // Dimensioning the line: the circle stays on the start, midpoint and end.+ run(&mut s, "sketch.dimension", json!({"entities": [l], "value": 60}));+ let sk = sketch(&s);+ let (o, rad) = (sk.center(ci).unwrap(), sk.radius(ci).unwrap());+ for p in [start, mid, tip] {+ assert!(close(o.dist(sk.points[p].pos), rad), "the circle left a snapped point: {:?}", sk.points);+ }+ // Plain positions: nothing to keep.+ let r = run(&mut s, "sketch.circle.three_point", json!({"p0": [100, 0], "p1": [110, 0], "p2": [105, 8]}));+ let ci = sketch(&s).curve_index(&ids(&r["curves"])[0]).unwrap();+ assert!(ids(&r["constraints"]).is_empty());+ assert!(!sketch(&s).constraints.iter().any(|k| matches!(k.kind, ConstraintKind::PointOnCurve { c, .. } if c == ci)));+}+#[test]fn ellipse_splines_conic() {let pi = std::f64::consts::PI;diff --git a/crates/ui-egui/src/tools.rs b/crates/ui-egui/src/tools.rsindex 26c698eae9..8ef1480385 100644--- a/crates/ui-egui/src/tools.rs+++ b/crates/ui-egui/src/tools.rs@@ -626,7 +626,9 @@ fn run_shape(app: &mut SolveApp, t: &Tool) -> Result<Vec<String>, String> {v}"sketch.circle.two_point" => json!({"p0": xy(&p[0]), "p1": xy(&p[1])}),- "sketch.circle.three_point" => json!({"p0": xy(&p[0]), "p1": xy(&p[1]), "p2": xy(&p[2])}),+ // Each click goes as its snap, as the 3-Point Arc's do: a click snapped to a point or+ // curve keeps the circle on it (#9858).+ "sketch.circle.three_point" => json!({"p0": arg(&p[0]), "p1": arg(&p[1]), "p2": arg(&p[2])}),"sketch.arc.three_point" => json!({"start": arg(&p[0]), "end": arg(&p[1]), "through": arg(&p[2])}),"sketch.arc.center_point" => {// A centre snapped to an existing point goes as its place, where `connect` ties