diff --git a/crates/engine/src/cmd/sketch_dimensioned.rs b/crates/engine/src/cmd/sketch_dimensioned.rs index db158803f..2de6c6356 100644 --- a/crates/engine/src/cmd/sketch_dimensioned.rs +++ b/crates/engine/src/cmd/sketch_dimensioned.rs @@ -31,6 +31,7 @@ pub fn dimension_roles(creator: &str) -> Option<&'static [&'static str]> { // A circle is dimensioned by its diameter unless a radius is given. "sketch.circle.center" | "sketch.circle.two_point" | "sketch.circle.three_point" => &["diameter", "radius"], "sketch.arc.center_point" => &["radius", "sweep"], + "sketch.ellipse" => &["major_radius", "minor_radius"], "sketch.slot.center_to_center" | "sketch.slot.overall" | "sketch.slot.center_point" => &["length", "width"], "sketch.slot.arc_center" => &["radius", "sweep", "width"], "sketch.slot.arc_three_point" => &["width"], @@ -152,6 +153,9 @@ pub(super) fn draw(s: &mut Session, p: &Value) -> Result { (c, "width" | "height") if c.starts_with("sketch.rectangle.") => (vec![curve(usize::from(role == "height"))?], "length"), (c, "radius" | "diameter") if c.starts_with("sketch.circle.") => (vec![curve(0)?], role), ("sketch.arc.center_point", "radius" | "sweep") => (vec![curve(0)?], role), + // `sketch.dimension` gives an ellipse its major radius as `major`, else its minor one. + ("sketch.ellipse", "major_radius") => (vec![curve(0)?], "major"), + ("sketch.ellipse", "minor_radius") => (vec![curve(0)?], "radius"), ("sketch.slot.center_to_center" | "sketch.slot.center_point", "length") => (vec![curve(0)?], "length"), ("sketch.slot.overall", "length") => (vec![curve(1)?, curve(3)?], "overall_length"), (c, "width") if c.starts_with("sketch.slot.") => (vec![curve(1)?], "diameter"), diff --git a/crates/engine/src/cmd/sketch_dimensioned_tests.rs b/crates/engine/src/cmd/sketch_dimensioned_tests.rs index 81e452e62..026ac7b78 100644 --- a/crates/engine/src/cmd/sketch_dimensioned_tests.rs +++ b/crates/engine/src/cmd/sketch_dimensioned_tests.rs @@ -355,6 +355,7 @@ fn sample(creator: &str) -> Option { "sketch.circle.two_point" => json!({"p0":[0,0],"p1":[10,0]}), "sketch.circle.three_point" => json!({"p0":[0,0],"p1":[10,0],"p2":[5,5]}), "sketch.arc.center_point" => json!({"center":[0,0],"start":[10,0],"end":[0,10]}), + "sketch.ellipse" => json!({"center":[0,0],"major":[10,6],"minor_radius":4}), "sketch.slot.center_to_center" | "sketch.slot.overall" => json!({"p0":[2,3],"p1":[22,3],"width":4}), "sketch.slot.center_point" => json!({"center":[12,3],"end":[22,3],"width":4}), "sketch.slot.arc_center" => json!({"center":[0,0],"start":[20,0],"end":[0,20],"width":6}), @@ -450,6 +451,19 @@ fn as_drawn_keeps_typed_values_alternatives_and_leaves_out_determined_sizes() { assert_eq!(out["dimensions"].as_object().unwrap().keys().collect::>(), ["width"]); } +/// An ellipse takes its major and minor radius (#10279): typed values drive it. +#[test] +fn an_ellipse_takes_a_typed_major_and_minor_radius() { + let mut s = session(); + let out = + draw(&mut s, "sketch.ellipse", json!({"center":[0,0],"major":[10,0],"minor_radius":4}), json!({"major_radius":"12 mm","minor_radius":5})); + let mut placed: Vec<&String> = out["dimensions"].as_object().unwrap().keys().collect(); + placed.sort(); + assert_eq!(placed, ["major_radius", "minor_radius"]); + close(measure(&s, |k| matches!(k, ConstraintKind::Distance { .. })), 12.0); + close(measure(&s, |k| matches!(k, ConstraintKind::Radius { .. })), 5.0); +} + /// Every `[x, y]` point of a sample moved by `(dx, dy)`; sizes and counts stay. fn shifted(v: &Value, dx: f64, dy: f64) -> Value { match v { diff --git a/crates/ui-egui/src/sketch_dims.rs b/crates/ui-egui/src/sketch_dims.rs index b9da03821..84d90d168 100644 --- a/crates/ui-egui/src/sketch_dims.rs +++ b/crates/ui-egui/src/sketch_dims.rs @@ -47,6 +47,8 @@ pub fn boxes_for(cmd: &str, n: usize) -> Vec { // The two points are a diameter's ends (#3278). ("sketch.circle.two_point", 1) => vec![b("Diameter", Length)], ("sketch.circle.two_tangent", 2) => vec![b("Radius", Length)], + ("sketch.ellipse", 1) => vec![b("Major radius", Length)], + ("sketch.ellipse", 2) => vec![b("Minor radius", Length)], ("sketch.arc.center_point" | "sketch.slot.arc_center", 1) => vec![b("Radius", Length)], ("sketch.arc.center_point" | "sketch.slot.arc_center", 2) => vec![b("Sweep", Angle)], ("sketch.polygon.inscribed" | "sketch.polygon.circumscribed", 1) => vec![b("Radius", Length), b("Sides", Unitless)], @@ -119,6 +121,13 @@ pub fn effective(app: &SolveApp, t: &Tool, h: Vec2) -> Vec2 { ("sketch.circle.two_point", 1) => a + dir_or_x(h - a) * lk("Diameter").unwrap_or((h - a).len()), ("sketch.slot.center_point", 1) => a + dir_or_x(h - a) * lk("Length").map_or((h - a).len(), |l| l * 0.5), ("sketch.arc.center_point" | "sketch.slot.arc_center", 1) => a + dir_or_x(h - a) * lk("Radius").unwrap_or((h - a).len()), + ("sketch.ellipse", 1) => a + dir_or_x(h - a) * lk("Major radius").unwrap_or((h - a).len()), + // A typed minor radius puts the third point on the minor axis, on the cursor's side. + ("sketch.ellipse", 2) => lk("Minor radius").map_or(h, |r| { + let c = t.pts.first().map(|p| p.0).unwrap_or(a); + let n = dir_or_x(a - c).perp(); + c + n * (r * sign(n.dot(h - c))) + }), ("sketch.arc.center_point" | "sketch.slot.arc_center", 2) => { let c = t.pts.first().map(|p| p.0).unwrap_or(a); let r = (a - c).len(); @@ -278,6 +287,17 @@ fn live(app: &SolveApp, t: &Tool, e: Vec2, px: f64) -> Vec<(f64, Vec2)> { } ("sketch.circle.center", 1) => vec![(2.0 * (e - a).len(), (a + e) * 0.5 + dir_or_x(e - a).perp() * off)], ("sketch.circle.two_point", 1) => vec![((e - a).len(), (a + e) * 0.5 + dir_or_x(e - a).perp() * off)], + ("sketch.ellipse", 1) => vec![((e - a).len(), (a + e) * 0.5 + dir_or_x(e - a).perp() * off)], + ("sketch.ellipse", 2) => { + // The minor radius of the ellipse through the cursor, as the preview draws it; the box + // sits beyond the cursor, away from the major axis (#2201). + let u = dir_or_x(a - first); + let d = e - first; + let y = u.cross(d).abs(); + let k = 1.0 - (d.dot(u) / first.dist(a).max(1e-12)).powi(2); + let r = if k > 1e-12 { y / k.sqrt() } else { y }; + vec![(r, e + u.perp() * (sign(u.perp().dot(d)) * 2.5 * off))] + } ("sketch.circle.two_tangent", 2) => { vec![(crate::sketch_tools::two_tangent_default_radius(app, t), (first + a) * 0.5 + dir_or_x(a - first).perp() * off)] } @@ -341,6 +361,7 @@ pub fn sync(t: &mut Tool) { && matches!( t.cmd, "sketch.arc.center_point" + | "sketch.ellipse" | "sketch.slot.center_to_center" | "sketch.slot.overall" | "sketch.slot.center_point" @@ -391,7 +412,7 @@ pub fn dimensions(t: &Tool) -> Value { .iter() .chain(&t.dims) .filter(|dimension| dimension.locked && is_dimension(t, dimension.label)) - .map(|dimension| (dimension.label.to_ascii_lowercase(), json!(dimension.value))) + .map(|dimension| (dimension.label.to_ascii_lowercase().replace(' ', "_"), json!(dimension.value))) .collect::>(); Value::Object(values) } diff --git a/crates/ui-egui/src/sketch_dims_tests.rs b/crates/ui-egui/src/sketch_dims_tests.rs index c11bd0eb5..128e0ded2 100644 --- a/crates/ui-egui/src/sketch_dims_tests.rs +++ b/crates/ui-egui/src/sketch_dims_tests.rs @@ -172,6 +172,39 @@ fn two_point_circle_takes_a_typed_diameter() { assert!((c - dir * 15.0).len() < 1e-6, "{c:?}"); } +/// #10279: the Ellipse tool shows a Major radius box after the centre and a Minor radius box after +/// the end of the major axis. The typed values size the ellipse and become its two dimensions, +/// the major radius one kept from the first stage. +#[test] +fn an_ellipse_takes_a_typed_major_and_minor_radius() { + use solidcraft_engine::doc::expr::Kind; + use solidcraft_engine::sketch::CurveKind; + let labels = |h: &Harness| h.app.tool.as_ref().unwrap().dims.iter().map(|b| b.label).collect::>(); + let mut h = drawing("sketch.ellipse"); + assert_eq!(labels(&h), ["Major radius"]); + type_value(&mut h, "25"); + h.step(&json!({"key": "Enter"})).unwrap(); + h.step(&json!({"move": {"world": [0, 30, 0]}})).unwrap(); + assert_eq!(labels(&h), ["Minor radius"]); + assert_focus(&h, 0); + type_value(&mut h, "8"); + h.step(&json!({"key": "Enter"})).unwrap(); + let state = h.app.session.world_state(); + let sk = &state.sketch(h.app.session.active_sketch.unwrap()).unwrap().sketch; + assert_eq!(sk.curves.len(), 1, "{:?}", h.app.status); + let CurveKind::Ellipse { c, m, r } = sk.curves[0].kind.clone() else { panic!("{:?}", sk.curves[0].kind) }; + let (c, m) = (sk.point(c).unwrap(), sk.point(m).unwrap()); + // From the centre (the origin) toward the cursor at (20, 10). + let dir = solidcraft_engine::geom::Vec2::new(20.0, 10.0).normalized().unwrap(); + assert!(c.len() < 1e-6 && (m - dir * 25.0).len() < 1e-6, "{c:?} {m:?}"); + assert!((r - 8.0).abs() < 1e-6, "{r}"); + let doc = &h.app.session.doc; + let mut sizes: Vec = doc.params.iter().filter(|p| p.model).filter_map(|p| doc.eval(&p.name, Kind::Length).ok()).collect(); + sizes.sort_by(f64::total_cmp); + assert_eq!(sizes.len(), 2, "{sizes:?}"); + assert!((sizes[0] - 8.0).abs() < 1e-6 && (sizes[1] - 25.0).abs() < 1e-6, "{sizes:?}"); +} + #[test] fn inch_live_lengths_use_design_units_and_typed_four_digits_commit_in_inches() { let mut h = Harness::new(); diff --git a/docs/book/src/guide/sketching.md b/docs/book/src/guide/sketching.md index 275fa56d6..d7bd14053 100644 --- a/docs/book/src/guide/sketching.md +++ b/docs/book/src/guide/sketching.md @@ -94,7 +94,7 @@ shortcut such as L can start a Midpoint Line; pick the variant you want from the change it ([Keyboard shortcuts](shortcuts.md)). Once a shape's first point is down, value boxes (a line's length and angle, a rectangle's width -and height, a circle's diameter…) follow the cursor. Typing goes into the first box, Tab moves to +and height, a circle's diameter, an ellipse's major and minor radius…) follow the cursor. Typing goes into the first box, Tab moves to the next and Enter places the shape; each typed value becomes a dimension. A value is an expression (`25`, `1 in`, `d1 * 2`), or `width = 20` (also `#width = 20`), which also adds the user parameter `width` and dimensions with it, as when [placing a dimension](parameters.md). A @@ -341,9 +341,9 @@ and the tool stays on for the next dimension. The dimension's text always takes lines take it only away from sketch points and curves, which still start a new dimension. To have shapes dimensioned as you draw them, turn on **Dimension new shapes** in Preferences › -Sketch (it is off by default). Each line, rectangle, circle, arc, slot or polygon you draw then -gets all its dimensions at the sizes you drew: a line its length and angle, a rectangle its width -and height, a circle its diameter, and so on. Values you type in the drawing boxes win, and a size +Sketch (it is off by default). Each line, rectangle, circle, arc, slot, polygon or ellipse you draw +then gets all its dimensions at the sizes you drew: a line its length and angle, a rectangle its +width and height, a circle its diameter, an ellipse its major and minor radius, and so on. Values you type in the drawing boxes win, and a size the sketch already sets (the angle of a line that snapped horizontal) is left out. The shape and its dimensions are one undo step. In scripts, `sketch.draw.dimensioned` takes `"as_drawn": true`.