SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Sketch Dimension: pick an origin axis as a reference (#2364) (#2373)
Martin Pålsson1939adba6be6
diff --git a/crates/engine/src/cmd/sketch.rs b/crates/engine/src/cmd/sketch.rsindex a06fb01b26..b83fd20f5e 100644--- a/crates/engine/src/cmd/sketch.rs+++ b/crates/engine/src/cmd/sketch.rs@@ -2954,6 +2954,18 @@ fn dimension(s: &mut Session, p: &Value) -> Result<Value> {let start = s.doc.clone();let ((param, kind_name, current), info) = edit(s, p, cmd, true, |sk, doc| {before = Some(dof_now(sk));+ let ents: Vec<String> = ents+ .iter()+ .map(|r| -> Result<String> {+ if sk.curve_index(r).is_none()+ && matches!(r.as_str(), "x" | "y")+ && let Some(id) = sk.sketch_axis_line(r)?.and_then(|c| sk.curves.get(c)).map(|cu| cu.id.clone())+ {+ return Ok(id);+ }+ Ok(r.clone())+ })+ .collect::<Result<_>>()?;let mut ent = |r: &str| -> Result<(Option<usize>, Option<usize>)> {if let Some(c) = sk.curve_index(r) {return Ok((None, Some(c)));diff --git a/crates/engine/src/cmd/sketch_axis_ref_tests.rs b/crates/engine/src/cmd/sketch_axis_ref_tests.rsindex 41329fe10a..82e20192a2 100644--- a/crates/engine/src/cmd/sketch_axis_ref_tests.rs+++ b/crates/engine/src/cmd/sketch_axis_ref_tests.rs@@ -238,3 +238,65 @@ fn bad_line_references_are_errors_and_change_nothing() {assert_eq!(s.doc.to_json(), before_redundant);assert!(axis_lines(&sketch(&s)).is_empty());}++/// #2364: a dimension takes `x` / `y` too. A line parallel to the axis gets its distance to it,+/// a point its distance to it, and both share the one axis line.+#[test]+fn issue_2364_dimension_to_a_sketch_axis() {+ let mut s = on(json!("XY"));+ run(&mut s, "sketch.line", json!({"points": [[5, 0], [5, 10]], "ids": ["a"]}));+ run(&mut s, "sketch.constraint.horizontal_vertical", json!({"line": "a"}));+ let r = run(&mut s, "sketch.dimension", json!({"entities": ["a", "y"], "value": 12}));+ assert!(r["sketch"]["solved"].as_bool().unwrap(), "{r}");+ let sk = sketch(&s);+ for end in ["a.start", "a.end"] {+ let p = pt(&sk, end);+ assert!((p.x.abs() - 12.0).abs() < 1e-7, "{end} is 12 from the y axis: {p:?}");+ }+ let axes = axis_lines(&sk);+ assert_eq!(axes.len(), 1, "{axes:?}");+ assert_eq!(axes[0].0, "y");+ assert_axis_line(&sk, &axes[0].1, Vec2::Y);++ // A point to the other axis; the y axis line is not made again.+ let r = run(&mut s, "sketch.dimension", json!({"entities": ["a.end", "x"], "value": 7}));+ assert!(r["sketch"]["solved"].as_bool().unwrap(), "{r}");+ let r = run(&mut s, "sketch.dimension", json!({"entities": ["y", "a.start"], "value": 12, "driven": true}));+ assert!(r["sketch"]["solved"].as_bool().unwrap(), "{r}");+ let sk = sketch(&s);+ assert!((pt(&sk, "a.end").y.abs() - 7.0).abs() < 1e-7, "{:?}", pt(&sk, "a.end"));+ let mut names: Vec<String> = axis_lines(&sk).into_iter().map(|(n, _)| n).collect();+ names.sort();+ assert_eq!(names, ["x", "y"], "one line per axis");+}++/// #2364: a real curve named like an axis is still the curve, not the axis.+#[test]+fn issue_2364_a_curve_named_like_an_axis_wins_in_a_dimension() {+ let mut s = on(json!("XY"));+ run(&mut s, "sketch.line", json!({"points": [[0, 0], [8, 0]], "ids": ["y"]}));+ run(&mut s, "sketch.dimension", json!({"entities": ["y"], "value": 20}));+ let sk = sketch(&s);+ let (a, b) = (pt(&sk, "y.start"), pt(&sk, "y.end"));+ assert!(((b - a).len() - 20.0).abs() < 1e-7, "the line `y` is 20 long: {a:?} {b:?}");+ assert!(axis_lines(&sk).is_empty(), "no axis line was made");+}++/// #3137: a dimension takes `x` / `y` for the sketch's axes too, from a circle's centre point,+/// and both dimensions share the axis line.+#[test]+fn issue_3137_dimensions_from_a_circle_centre_to_the_sketch_axes() {+ let mut s = on(json!("XY"));+ run(&mut s, "sketch.circle.center", json!({"center": [30, 20], "radius": 5, "id": "c"}));+ let r = run(&mut s, "sketch.dimension", json!({"entities": ["c.center", "y"], "value": 40}));+ let line = strs(&r["created"]["curves"]);+ assert_eq!(line.len(), 1, "the y axis line is created: {r}");+ run(&mut s, "sketch.dimension", json!({"entities": ["c.center", "x"], "value": 15}));+ let sk = sketch(&s);+ let o = pt(&sk, "c.center");+ assert!((o.x - 40.0).abs() < 1e-7 && (o.y - 15.0).abs() < 1e-7, "the centre is 40 from y and 15 from x: {o:?}");+ let axes = axis_lines(&sk);+ assert_eq!(axes.len(), 2, "{axes:?}");+ assert_axis_line(&sk, &line[0], Vec2::Y);+ assert!(sk.radius(sk.curve_index("c").unwrap()).is_some_and(|r| (r - 5.0).abs() < 1e-7), "the circle keeps its radius");+}diff --git a/crates/ui-egui/src/dimension_create_ui_tests.rs b/crates/ui-egui/src/dimension_create_ui_tests.rsindex 502dd2748a..0b48012ee6 100644--- a/crates/ui-egui/src/dimension_create_ui_tests.rs+++ b/crates/ui-egui/src/dimension_create_ui_tests.rs@@ -2047,3 +2047,67 @@ fn a_typed_value_hides_the_curvature_combs() {assert!(!comb_drawn(&h), "no comb of the design while the value is typed");crate::dim_view::cancel();}++/// #3137: the Dimension tool takes the origin axis along the sketch's x or y as its second+/// pick: a circle's centre, or the circle itself, is then dimensioned from that axis.+#[test]+fn a_circle_centre_is_dimensioned_from_a_sketch_axis() {+ for (picks, typed, centre) in [(vec![[30., 20.], [0., 3.]], "40 mm", [40., 20.]), (vec![[30., 20.], [3., 0.]], "12 mm", [30., 12.])] {+ crate::dim_view::cancel();+ let mut h = Harness::new();+ h.app.run("sketch.create", json!({"plane":"XY"})).unwrap();+ h.app.run("sketch.circle.center", json!({"center":[30,20],"radius":5})).unwrap();+ // Sized already, so the first dimension below measures instead of scaling the sketch (#1365).+ h.app.run("sketch.dimension", json!({"entities":["c1"],"type":"radius","value":"5 mm"})).unwrap();+ h.call("ui.view", json!({"view":"top"}));+ h.app.cam.distance = 80.0;+ h.app.cam.target = solidcore_record::Vec3::new(15., 10., 0.);+ h.settle();+ h.step(&json!({"start":"sketch.dimension"})).unwrap();+ for p in &picks {+ h.step(&json!({"move":{"world":[p[0],p[1],0]}})).unwrap();+ h.step(&json!({"click":{"world":[p[0],p[1],0]}})).unwrap();+ }+ type_value(&mut h, typed);+ h.step(&json!({"key":"Enter"})).unwrap();+ let state = crate::open_meshing::session_world(&h.app.session);+ let sk = &state.sketch(h.app.session.active_sketch.unwrap()).unwrap().sketch;+ let o = sk.point(sk.resolve_point("c1.center").unwrap()).unwrap();+ assert!((o.x - centre[0]).abs() < 1e-6 && (o.y - centre[1]).abs() < 1e-6, "{picks:?}: {o:?} {:?}", h.app.status);+ }+}++/// #3320: after a line or a line's end, the X axis is the second pick: an angle from the axis,+/// and a distance from it. The line is picked off its midpoint, which is a point pick (#4872).+#[test]+fn a_line_is_dimensioned_against_the_sketch_x_axis() {+ for (picks, typed) in [(vec![[5., 5.], [3., 0.]], "30 deg"), (vec![[20., 20.], [3., 0.]], "12 mm")] {+ crate::dim_view::cancel();+ let mut h = Harness::new();+ h.app.run("sketch.create", json!({"plane":"XY"})).unwrap();+ h.app.run("sketch.line", json!({"points":[[0,0],[20,20]]})).unwrap();+ // Sized already, so the first dimension below measures instead of scaling the sketch (#1365).+ h.app.run("sketch.dimension", json!({"entities":["l1"],"type":"length","value":"28.284271247461902 mm"})).unwrap();+ h.call("ui.view", json!({"view":"top"}));+ h.app.cam.distance = 80.0;+ h.app.cam.target = solidcore_record::Vec3::new(10., 10., 0.);+ h.settle();+ h.step(&json!({"start":"sketch.dimension"})).unwrap();+ for p in &picks {+ h.step(&json!({"move":{"world":[p[0],p[1],0]}})).unwrap();+ h.step(&json!({"click":{"world":[p[0],p[1],0]}})).unwrap();+ }+ assert_eq!(h.app.tool.as_ref().unwrap().picks.last().map(String::as_str), Some("x"), "the axis is the second pick");+ type_value(&mut h, typed);+ h.step(&json!({"key":"Enter"})).unwrap();+ let state = crate::open_meshing::session_world(&h.app.session);+ let sk = &state.sketch(h.app.session.active_sketch.unwrap()).unwrap().sketch;+ let (a, b) = (sk.point(sk.resolve_point("l1.start").unwrap()).unwrap(), sk.point(sk.resolve_point("l1.end").unwrap()).unwrap());+ if typed.ends_with("deg") {+ let angle = (b - a).angle().to_degrees();+ assert!((angle - 30.0).abs() < 1e-6, "the line is at 30° to x: {angle}");+ } else {+ assert!((b.y - 12.0).abs() < 1e-6, "the end is 12 from x: {b:?}");+ }+ }+}diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rsindex 363a7f4657..b779acf095 100644--- a/crates/ui-egui/src/lib.rs+++ b/crates/ui-egui/src/lib.rs@@ -2172,6 +2172,7 @@ impl SolveApp {.iter().filter_map(|s| match s {solidcraft_engine::Sel::SketchCurve { id, .. } | solidcraft_engine::Sel::SketchPoint { id } => Some(id.clone()),+ solidcraft_engine::Sel::Axis { name } => tools::sketch_axis_of(self, name).map(str::to_string),_ => None,}).take(2)diff --git a/crates/ui-egui/src/tools.rs b/crates/ui-egui/src/tools.rsindex 8471634c23..4fe9b1bb0b 100644--- a/crates/ui-egui/src/tools.rs+++ b/crates/ui-egui/src/tools.rs@@ -408,6 +408,7 @@ pub fn on_click(app: &mut SolveApp, proj: &Proj, pos: Pos2, shift: bool) {Hit::SketchCurve { sketch, id, straight, .. } if Some(*sketch) == app.session.active_sketch => {Some((id.clone(), true, *straight))}+ Hit::Axis { name } => sketch_axis_of(app, name).map(|a| (a.to_string(), true, true)),_ => None,})})