SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Sketch: keep nested circles ordered when diameters change (#6764) (#6789)
Colin Faulkingham484f0cf8aed2
diff --git a/crates/engine/src/cmd/sketch_circle_edge_tests.rs b/crates/engine/src/cmd/sketch_circle_edge_tests.rsindex fd4b5d150a..b59961c243 100644--- a/crates/engine/src/cmd/sketch_circle_edge_tests.rs+++ b/crates/engine/src/cmd/sketch_circle_edge_tests.rs@@ -132,6 +132,55 @@ fn crossing_and_nested_circles_dimension_their_overlap_and_their_narrowest_gap()near(&s, "c2.center", [16.0, 10.0]);}+/// #6764: enlarging the bore beyond the old outside radius must enlarge the outside too,+/// keeping the selected annulus through recompute, undo/redo and reopening.+#[test]+fn a_tube_keeps_its_bore_when_its_inner_diameter_changes() {+ fn check(s: &mut Session, inner: f64) {+ {+ let state = s.model.state();+ let ss = state.sketch(1).unwrap();+ let radius = |id| ss.sketch.radius(ss.sketch.curve_index(id).unwrap()).unwrap();+ assert!((radius("outside") - inner - 10.0).abs() < 1e-6);+ assert!((radius("bore") - inner).abs() < 1e-6);+ let annulus = ss.profiles.iter().find(|p| p.outer_curves == ["outside"]).unwrap();+ assert_eq!(annulus.hole_curves, [vec!["bore"]]);+ }+ let area = std::f64::consts::PI * ((inner + 10.0).powi(2) - inner.powi(2));+ let m = run(s, "inspect.measure", json!({}));+ assert_eq!(m["body_count"], 1, "{m}");+ let volume = m["total"]["volume_mm3"].as_f64().unwrap();+ assert!((volume / (100.0 * area) - 1.0).abs() < 0.001, "{m}");+ let body = &s.model.state().bodies[0].body;+ assert!(!body.contains(solidcraft_geom::Vec3::new(0.0, 0.0, 50.0)), "the bore stays empty");+ assert!(body.contains(solidcraft_geom::Vec3::new(inner + 5.0, 0.0, 50.0)), "the wall stays solid");+ }+ for dimension_first in [false, true] {+ let mut s = Session::default();+ run(&mut s, "sketch.create", json!({"plane": "XY"}));+ run(&mut s, "sketch.circle.center", json!({"center": "origin", "radius": 20, "id": "outside"}));+ run(&mut s, "sketch.circle.center", json!({"center": "origin", "radius": 10, "id": "bore"}));+ let diameter = json!({"entities": ["bore"], "type": "diameter", "value": 20});+ let wall = json!({"entities": ["bore", "outside"], "value": 10});+ let params = if dimension_first { [diameter, wall] } else { [wall, diameter] };+ let dimensions = params.map(|p| run(&mut s, "sketch.dimension", p));+ let name = dimensions[usize::from(!dimension_first)]["param"].as_str().unwrap();+ run(&mut s, "sketch.finish", json!({}));+ run(&mut s, "solid.extrude", json!({"distance": 100, "profiles": [{"curves": ["outside"]}]}));+ check(&mut s, 10.0);+ run(&mut s, "parameters.change", json!({"name": name, "expression": "50 mm"}));+ check(&mut s, 25.0);+ run(&mut s, "edit.undo", json!({}));+ check(&mut s, 10.0);+ run(&mut s, "edit.redo", json!({}));+ check(&mut s, 25.0);+ let mut reopened = Session::new(solidcraft_doc::Document::from_json(&s.doc.to_json()).unwrap());+ check(&mut reopened, 25.0);+ run(&mut reopened, "parameters.change", json!({"name": name, "expression": "6 mm"}));+ check(&mut reopened, 3.0);+ }+}+#[test]fn arcs_dimension_to_their_circles() {let mut s = Session::default();diff --git a/crates/sketch/src/circle_edge_tests.rs b/crates/sketch/src/circle_edge_tests.rsindex 8850785798..506c57f933 100644--- a/crates/sketch/src/circle_edge_tests.rs+++ b/crates/sketch/src/circle_edge_tests.rs@@ -142,6 +142,40 @@ fn how_two_circles_lie_and_their_gap() {}}+/// A diameter edit must keep the same circle outside: the unsigned wall gap also has a+/// solution with the radii reversed, which would turn a tube's selected outer loop into a disc.+#[test]+fn nested_circles_keep_their_radius_order_after_diameter_edits() {+ for swap in [false, true] {+ for dimension_first in [false, true] {+ for drive_inner in [false, true] {+ let mut sk = Sketch::new();+ let outer = sk.add_circle(Vec2::ZERO, 20.0, Some(0), None).unwrap();+ let inner = sk.add_circle(Vec2::ZERO, 10.0, Some(0), None).unwrap();+ let (a, b) = if swap { (inner, outer) } else { (outer, inner) };+ let wall = ConstraintKind::CircleCircleDistance { a, b, value: 10.0, circles: TwoCircles::Nested };+ let diameter = ConstraintKind::Diameter { c: if drive_inner { inner } else { outer }, value: if drive_inner { 20.0 } else { 40.0 } };+ let kinds = if dimension_first { [diameter, wall] } else { [wall, diameter] };+ for kind in kinds {+ sk.add_constraint(kind, None).unwrap();+ }+ assert!(solve(&mut sk).keeps_orientation());+ let di = usize::from(!dimension_first);+ let diameters = if drive_inner { [50.0, 6.0, 100.0, 20.0] } else { [24.0, 200.0, 40.0, 30.0] };+ for diameter in diameters {+ let old = sk.driving_values();+ sk.constraints[di].kind.set_value(diameter);+ let rep = solve_after_value_edit(&mut sk, &old);+ assert!(rep.keeps_orientation(), "{swap} {dimension_first} {drive_inner} {diameter}: {rep:?}");+ let expected_inner = if drive_inner { diameter / 2.0 } else { diameter / 2.0 - 10.0 };+ assert!((sk.radius(inner).unwrap() - expected_inner).abs() < 1e-6, "{sk:?}");+ assert!((sk.radius(outer).unwrap() - expected_inner - 10.0).abs() < 1e-6, "{sk:?}");+ }+ }+ }+ }+}+#[test]fn arcs_are_measured_to_their_circles() {let mut sk = Sketch::new();diff --git a/crates/sketch/src/solver.rs b/crates/sketch/src/solver.rsindex 6521bbb382..40ea39a1e4 100644--- a/crates/sketch/src/solver.rs+++ b/crates/sketch/src/solver.rs@@ -1988,6 +1988,8 @@ enum Orient {/// Which side of line `l` the point `p` is on (`PointLineDistance`, `LinearDiameter`, a/// circle's centre for `LineCircleDistance`).OfLine(usize, usize, f64),+ /// Which of two nested circles or arcs encloses the other.+ RadiusOrder(usize, usize),}/// An orientation's signed offset in a state (a corner's distance from the opposite side, an@@ -2014,6 +2016,13 @@ fn offset(s: &State, o: Orient) -> Option<(f64, f64, f64)> {let pp = s.p(p);((b - a).normalized()?.cross(pp - a), size, coords(&[pp, a, b]))}+ Orient::RadiusOrder(a, b) => {+ let (ca, ra) = s.round(a);+ let (cb, rb) = s.round(b);+ // Even a thin wall has an inside and an outside. Use the radius difference as+ // its size; rounding still accounts for the radii and the arc coordinates.+ (ra - rb, (ra - rb).abs(), ra.max(rb).max(coords(&[ca, cb])))+ }})}@@ -2155,6 +2164,11 @@ fn orients(s: &State, sub: &[(Block, Vec<usize>)]) -> Vec<(Orient, Vec<usize>)>ConstraintKind::DistanceY { p, q, value } if free(p) || free(q) => out.push((Orient::SideY(p, q, value), vec![*ci])),ConstraintKind::PointLineDistance { p, l, value } => out.push((Orient::OfLine(p, l, value), vec![*ci])),ConstraintKind::LinearDiameter { p, l, value } => out.push((Orient::OfLine(p, l, value / 2.0), vec![*ci])),+ // The unsigned gap has two solutions: a large diameter edit can otherwise+ // exchange the inner and outer circle and turn an extruded tube into a disc.+ ConstraintKind::CircleCircleDistance { a, b, circles: TwoCircles::Nested, .. } => {+ out.push((Orient::RadiusOrder(a, b), vec![*ci]));+ }// The circle stays on its side of the line: the distance is unsigned.ConstraintKind::LineCircleDistance { l, c, value, .. } => {if let Some(CurveKind::Circle { c: centre, .. } | CurveKind::Arc { c: centre, .. }) = s.sk.curves.get(c).map(|x| &x.kind) {