Skip to content

SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.

Center circle through a snapped point stays on it (#3099) (#3277)

phanoobac1768e08b83
diff --git a/crates/engine/src/cmd/sketch.rs b/crates/engine/src/cmd/sketch.rs
index 04505396a4..1981068071 100644
--- a/crates/engine/src/cmd/sketch.rs
+++ b/crates/engine/src/cmd/sketch.rs
@@ -67,7 +67,11 @@ pub static COMMANDS: &[CommandSpec] = &[
.icon("circle")
.key("C")
.enabled(in_sketch)
- .params(concat!("center: [x,y] or point ref, radius | diameter: number", connect_doc!(), created_doc!())),
+ .params(concat!(
+ "center: [x,y] or point ref, radius | diameter: number; through?: point ref (a point the circle passes through, kept on it: where the tool's second click snapped)",
+ connect_doc!(),
+ created_doc!()
+ )),
CommandSpec::new("sketch.circle.two_point", "2-Point Circle", circle_two)
.at("SKETCH", "CREATE")
.icon("circle2")
@@ -1143,7 +1147,15 @@ fn circle_center(s: &mut Session, p: &Value) -> Result<Value> {
let c = req_parg(sk, doc, p, "center", cmd)?;
let ci = sk.add_circle(c.pos(), r, c.idx(), id.as_deref())?;
mark_construction(sk, &[ci], p);
- Ok((ids_of(sk, &[ci]), Vec::new()))
+ // A point the circle was drawn through stays on it (#3099): with a placed centre, that
+ // sets the radius.
+ let mut cons = Vec::new();
+ if let Some(v) = p.get("through")
+ && let PArg::Ref(q, _) = parg(sk, doc, v, cmd)?
+ {
+ cons.push(add_c(sk, ConstraintKind::PointOnCurve { p: q, 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.rs
index 63de01fec0..f5dafa37a5 100644
--- a/crates/engine/src/cmd/sketch_modify_tests.rs
+++ b/crates/engine/src/cmd/sketch_modify_tests.rs
@@ -483,6 +483,30 @@ fn two_tangent_circle_from_a_click_on_a_curve_leaves_the_curves_as_drawn() {
}
}
+/// #3099: a centre circle drawn through a point stays on it. Centred on a line's midpoint and
+/// through its end, the circle's radius follows the line when the line is dimensioned.
+#[test]
+fn centre_circle_through_a_point_keeps_it_on_the_circle() {
+ let mut s = new_sketch();
+ let l = ids(&run(&mut s, "sketch.line", json!({"points": [[0, 30], [40, 30]]}))["curves"])[0].clone();
+ let r = run(&mut s, "sketch.circle.center", json!({"center": format!("mid:{l}"), "radius": 20, "through": format!("{l}.end")}));
+ let c = ids(&r["curves"])[0].clone();
+ let sk = sketch(&s);
+ let (li, ci) = (sk.curve_index(&l).unwrap(), sk.curve_index(&c).unwrap());
+ let end = sk.resolve_point(&format!("{l}.end")).unwrap();
+ assert!(sk.constraints.iter().any(|k| k.kind == ConstraintKind::PointOnCurve { p: end, c: ci }), "{:?}", sk.constraints);
+ run(&mut s, "sketch.dimension", json!({"entities": [l], "value": 60}));
+ let sk = sketch(&s);
+ let line = sk.shape(li).unwrap();
+ let solidcraft_sketch::Shape::Line { a, b } = line else { panic!("{line:?}") };
+ assert!(close(a.dist(b), 60.0));
+ assert!(close(sk.radius(ci).unwrap(), 30.0), "the circle followed the line's end: {:?}", sk.radius(ci));
+ // Through a plain position: nothing to keep it on.
+ let r = run(&mut s, "sketch.circle.center", json!({"center": [100, 0], "radius": 5, "through": [105, 0]}));
+ let ci = sketch(&s).curve_index(&ids(&r["curves"])[0]).unwrap();
+ 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/sketch_corner_ui_tests.rs b/crates/ui-egui/src/sketch_corner_ui_tests.rs
index d0b602123e..bc9bb338f2 100644
--- a/crates/ui-egui/src/sketch_corner_ui_tests.rs
+++ b/crates/ui-egui/src/sketch_corner_ui_tests.rs
@@ -679,3 +679,33 @@ fn two_tangent_circle_clicked_on_two_sides_fits_the_corner_and_moves_nothing() {
assert!(before.iter().zip(&after).all(|(a, b)| a.dist(*b) < 1e-9), "fixed {fixed}: the rectangle moved: {after:?}");
}
}
+
+/// #3099: the centre circle tool, its centre snapped to a line's midpoint and its second click
+/// to the line's end, keeps the circle through that end: nothing is left free.
+#[test]
+fn centre_circle_clicked_through_a_line_end_stays_on_it() {
+ let mut h = crate::scenario::Harness::new();
+ h.app.run("sketch.create", json!({"plane":"XY"})).unwrap();
+ h.app.run("sketch.line", json!({"points":[[0,30],[40,30]]})).unwrap();
+ h.step(&json!({"call":"ui.view","params":{"view":"fit"}})).unwrap();
+ h.settle();
+ let sid = h.app.session.active_sketch.unwrap();
+ h.step(&json!({"start":"sketch.circle.center"})).unwrap();
+ for at in [[20, 30, 0], [40, 30, 0]] {
+ h.step(&json!({"move":{"world":at}})).unwrap();
+ h.step(&json!({"click":{"world":at}})).unwrap();
+ }
+ let sk = h.app.session.model.state().sketch(sid).unwrap().sketch.clone();
+ let circle = sk.curves.iter().position(|c| matches!(c.kind, CurveKind::Circle { .. })).unwrap();
+ let end = sk.resolve_point("l1.end").unwrap();
+ assert!(
+ sk.constraints.iter().any(|k| matches!(k.kind, solidcraft_engine::sketch::ConstraintKind::Midpoint { l: 0, .. })),
+ "{:?}",
+ sk.constraints
+ );
+ assert!(
+ sk.constraints.iter().any(|k| k.kind == solidcraft_engine::sketch::ConstraintKind::PointOnCurve { p: end, c: circle }),
+ "the circle stays on the line's end: {:?}",
+ sk.constraints
+ );
+}
diff --git a/crates/ui-egui/src/tools.rs b/crates/ui-egui/src/tools.rs
index e205dced0e..bbe7bbcd64 100644
--- a/crates/ui-egui/src/tools.rs
+++ b/crates/ui-egui/src/tools.rs
@@ -424,7 +424,14 @@ fn run_shape(app: &mut SolveApp, t: &Tool) -> Result<Vec<String>, String> {
json!({"p0": xy(&p[0]), "p1": xy(&p[1]), "radius": crate::sketch_dims::rounded_radius_value(app, t, p[0].0, p[1].0)})
}
"sketch.rectangle.three_point" => json!({"p0": xy(&p[0]), "p1": xy(&p[1]), "p2": xy(&p[2])}),
- "sketch.circle.center" => json!({"center": arg(&p[0]), "radius": d(0, 1)}),
+ "sketch.circle.center" => {
+ // A second click snapped to a point keeps the circle on that point (#3099).
+ let mut v = json!({"center": arg(&p[0]), "radius": d(0, 1)});
+ if p[1].1.is_some() {
+ v["through"] = arg(&p[1]);
+ }
+ 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])}),
"sketch.arc.three_point" => json!({"start": arg(&p[0]), "end": arg(&p[1]), "through": arg(&p[2])}),