Skip to content

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

Sketch: a circle drawn at the origin can be moved (#5034) (#6462)

Homero Thompsond9c5e4c3251d
diff --git a/crates/engine/src/cmd/sketch.rs b/crates/engine/src/cmd/sketch.rs
index 87ce88759d..f3a3b4ba1b 100644
--- a/crates/engine/src/cmd/sketch.rs
+++ b/crates/engine/src/cmd/sketch.rs
@@ -1252,7 +1252,12 @@ fn circle_center(s: &mut Session, p: &Value) -> Result<Value> {
let id = str_(p, "id").map(str::to_string);
let (out, info) = draw(s, p, cmd, |sk, doc| {
let c = req_parg(sk, doc, p, "center", cmd)?;
- let ci = sk.add_circle(c.pos(), r, c.idx(), id.as_deref())?;
+ // The origin is the sketch's frame, not a hold on the geometry: a circle centred there
+ // keeps a centre of its own, tied to the origin as any other drawn geometry is, so it
+ // is not fixed geometry the sketch cannot move (#5034). A centre held by a parameter or
+ // by projected geometry keeps that hold: the circle follows it.
+ let own = c.idx().is_some_and(|i| sk.point_index("origin") == Some(i));
+ let ci = sk.add_circle(c.pos(), r, if own { None } else { c.idx() }, id.as_deref())?;
mark_construction(sk, &[ci], p);
// A point the circle was drawn through stays on it (#3099): with a placed centre, that
// sets the radius.
diff --git a/crates/engine/src/cmd/sketch_modify.rs b/crates/engine/src/cmd/sketch_modify.rs
index fa00e53d5f..964f019819 100644
--- a/crates/engine/src/cmd/sketch_modify.rs
+++ b/crates/engine/src/cmd/sketch_modify.rs
@@ -2378,6 +2378,17 @@ fn transform_in_place(sk: &mut Sketch, cs: &[usize], ps: &[usize], f: &dyn Fn(Ve
if pts.iter().any(|q| sk.points.get(*q).is_some_and(|x| x.fixed || x.link.is_some())) {
return Err(bad(cmd, "fixed or projected geometry cannot move"));
}
+ // A point the origin holds goes where the move takes it: the coincidence that held it
+ // there is dropped (#5034).
+ let moved: Vec<usize> = pts
+ .iter()
+ .copied()
+ .filter(|q| {
+ let p = sk.point(*q).unwrap_or_default();
+ (f(p) - p).len() > TOL
+ })
+ .collect();
+ release_origin_holds(sk, &moved);
for q in pts {
if let Some(x) = sk.points.get_mut(q) {
x.pos = f(x.pos);
@@ -2458,6 +2469,15 @@ fn rotate_axis_constraints(sk: &mut Sketch, doc: &mut solidcraft_doc::Document,
Ok(())
}
+/// Drop the coincidences that held the moved points to the sketch's origin: a move translates
+/// the geometry it is given, and the origin does not travel with it (#5034).
+fn release_origin_holds(sk: &mut Sketch, pts: &[usize]) {
+ let Some(origin) = sk.point_index("origin") else { return };
+ sk.constraints.retain(
+ |c| !matches!(&c.kind, ConstraintKind::Coincident { p, q } if (pts.contains(p) && *q == origin) || (pts.contains(q) && *p == origin)),
+ );
+}
+
fn move_copy(s: &mut Session, p: &Value) -> Result<Value> {
let cmd = "sketch.move";
let t = if p.get("translate").is_some() { req_vec2(cmd, p, "translate")? } else { Vec2::ZERO };
diff --git a/crates/engine/src/cmd/sketch_modify_tests.rs b/crates/engine/src/cmd/sketch_modify_tests.rs
index aa5be825c9..bd0034ad89 100644
--- a/crates/engine/src/cmd/sketch_modify_tests.rs
+++ b/crates/engine/src/cmd/sketch_modify_tests.rs
@@ -730,6 +730,22 @@ fn move_rotation_turns_line_angle_dimension() {
assert!(close(d.len(), length) && close(d.angle(), 120f64.to_radians()), "{d:?}");
}
+/// #5034: a circle drawn with its centre on the fixed origin is ordinary geometry: it keeps a
+/// centre of its own, and `sketch.move` takes it to the place asked for.
+#[test]
+fn a_circle_drawn_at_the_origin_can_be_moved() {
+ let mut s = new_sketch();
+ let drawn = run(&mut s, "sketch.circle.center", json!({"center": "origin", "radius": 10}));
+ let c = ids(&drawn["curves"])[0].clone();
+ // The circle's centre is a point of the sketch, and the sketch can name it.
+ assert!(sketch(&s).resolve_point(&format!("{c}.center")).is_some(), "the circle's centre is a point of the sketch");
+ let moved = run(&mut s, "sketch.move", json!({"entities": [c], "translate": [10, 5]}));
+ let sk = sketch(&s);
+ let centre = sk.center(sk.curve_index(&c).unwrap()).unwrap();
+ assert!(centre.dist(Vec2::new(10.0, 5.0)) < 1e-7, "the circle moved where it was asked to: {centre:?}");
+ assert!(moved["sketch"]["solved"].as_bool().unwrap_or(false), "the sketch stayed solved: {moved}");
+}
+
/// #1485: after a circle is trimmed between two construction lines, clicking the trimmed end
/// picks the construction line's end lying there, not the arc's own; the tangent arc still starts
/// from the trimmed arc, tangent to it.