From 4a7a8f31f86144505186ca25095e7d269fa9768e Mon Sep 17 00:00:00 2001 From: Tyler Gregorcyk Date: Wed, 28 Oct 2026 17:39:31 +0000 Subject: [PATCH] Sphere, Torus and Cone dialogs: place them by their centre or base (#5638) (#8328) * claim: #5638 * Sphere, Torus and Cone dialogs: place them by their centre or base (#5638) The Box and Cylinder dialogs already had typed X, Y and Z placement fields; the Sphere, Torus and Cone dialogs now do too. Sphere and Torus show Placement: Center and take a picked point like the Box; Cone shows Placement: Base Center like the Cylinder. The fields take expressions, and Edit Feature shows and keeps the stored centre or base. Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 --- crates/ui-egui/src/dialogs.rs | 4 +- crates/ui-egui/src/primitive_placement.rs | 20 ++-- crates/ui-egui/src/primitive_units_tests.rs | 104 +++++++++++++++++++- 3 files changed, 117 insertions(+), 11 deletions(-) diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs index ea16cd7d6..782b0df31 100644 --- a/crates/ui-egui/src/dialogs.rs +++ b/crates/ui-egui/src/dialogs.rs @@ -958,7 +958,7 @@ impl Dialog { at: None, refs: Default::default(), }, - vec![], + crate::primitive_placement::inputs("solid.sphere"), ), "solid.torus" => Dialog::new( Kind::Primitive { @@ -968,7 +968,7 @@ impl Dialog { at: None, refs: Default::default(), }, - vec![], + crate::primitive_placement::inputs("solid.torus"), ), "solid.cone" => Dialog::new( Kind::Primitive { diff --git a/crates/ui-egui/src/primitive_placement.rs b/crates/ui-egui/src/primitive_placement.rs index 053d399a8..400d43ce8 100644 --- a/crates/ui-egui/src/primitive_placement.rs +++ b/crates/ui-egui/src/primitive_placement.rs @@ -1,7 +1,8 @@ -//! Where a Box or Cylinder goes, typed in its dialog (#5638). The Box is placed by its corner -//! or its centre, the Cylinder by the centre of its base, as X, Y and Z fields that take -//! expressions and units like the size fields. A point picked in the view (a vertex, a point on -//! an edge or face, the origin) fills the Box's fields. +//! Where a primitive goes, typed in its dialog (#5638). The Box is placed by its corner or its +//! centre, the Cylinder and Cone by the centre of their base, the Sphere and Torus by their +//! centre, as X, Y and Z fields that take expressions and units like the size fields. A point +//! picked in the view (a vertex, a point on an edge or face, the origin) fills the fields of the +//! Box, Sphere and Torus. //! //! The placement lives in the dialog's extra parameters under the command's own key (`corner`, //! `center` or `base`). It is written there only once a field or the choice changes, so a new @@ -22,7 +23,8 @@ use solidcraft_engine::Sel; fn keys(cmd: &str) -> &'static [&'static str] { match cmd { "solid.box" => &["corner", "center"], - "solid.cylinder" => &["base"], + "solid.cylinder" | "solid.cone" => &["base"], + "solid.sphere" | "solid.torus" => &["center"], _ => &[], } } @@ -30,10 +32,11 @@ fn keys(cmd: &str) -> &'static [&'static str] { /// The Box's choice of the point its fields place. const BOX_PLACEMENTS: [&str; 2] = ["Corner", "Center"]; -/// The selection inputs of a primitive's dialog: the Box takes a point for its corner or centre. +/// The selection inputs of a primitive's dialog: the Box takes a point for its corner or centre, +/// the Sphere and Torus for their centre. pub(crate) fn inputs(cmd: &str) -> Vec { match cmd { - "solid.box" => vec![SelInput::new("Point", VERTICES | EDGES | FACES | ORIGIN, false)], + "solid.box" | "solid.sphere" | "solid.torus" => vec![SelInput::new("Point", VERTICES | EDGES | FACES | ORIGIN, false)], _ => Vec::new(), } } @@ -77,8 +80,9 @@ pub(crate) fn rows(ui: &mut egui::Ui, s: &Session, cmd: &str, extra: &mut Map 1 { choice_row(ui, "Placement", "pr_place", &mut choice, &BOX_PLACEMENTS); } else { + let point = if keys(cmd) == ["center"] { tr("ui.center") } else { tr("Base Center") }; ui_kit::row(ui, "Placement", |ui| { - ui_kit::text(ui, tr("Base Center"), ui_kit::Text::Cell); + ui_kit::text(ui, point, ui_kit::Text::Cell); }); } let before = coords.clone(); diff --git a/crates/ui-egui/src/primitive_units_tests.rs b/crates/ui-egui/src/primitive_units_tests.rs index a939b74b1..438c58bc7 100644 --- a/crates/ui-egui/src/primitive_units_tests.rs +++ b/crates/ui-egui/src/primitive_units_tests.rs @@ -1,7 +1,8 @@ //! #630: the Box, Cylinder, Sphere, Torus and Coil dialogs show the unit of their sizes, as the //! other dialogs do ("10 mm"), in the design's units. OK makes what the bare numbers made before, //! and a bare number typed into a field still means the design's units. #5638: the Box dialog -//! places the box by its corner or centre and the Cylinder dialog by its base, typed or picked. +//! places the box by its corner or centre, the Cylinder and Cone dialogs by their base and the +//! Sphere and Torus dialogs by their centre, typed or picked. use serde_json::{Value, json}; use solidcraft_engine::Session; @@ -615,3 +616,104 @@ fn a_point_picked_in_the_box_dialog_becomes_its_corner_or_centre() { let made = bounds(&mut h.app.session); assert_near(made[1].0, [-10.0, -10.0, -10.0]); } + +#[test] +fn the_sphere_torus_and_cone_dialogs_take_their_placement() { + let mut h = design("mm"); + // The sphere by its centre: left alone at the origin, as before. + h.app.start("solid.sphere"); + h.frames(3); + assert!(sent(&h).get("center").is_none(), "left alone, at the origin as before"); + type_into(&mut h, "X", "30"); + type_into(&mut h, "Z", "-4"); + h.frames(2); + assert_eq!(sent(&h)["center"], json!(["30", "0 mm", "-4"])); + ok(&mut h); + // The torus by its centre. + h.app.start("solid.torus"); + h.frames(3); + type_into(&mut h, "Y", "60"); + h.frames(2); + assert_eq!(sent(&h)["center"], json!(["0 mm", "60", "0 mm"])); + ok(&mut h); + // The cone by the centre of its base. + h.app.start("solid.cone"); + h.frames(3); + assert!(sent(&h).get("base").is_none(), "left alone, at the origin as before"); + type_into(&mut h, "X", "-50"); + h.frames(2); + assert_eq!(sent(&h)["base"], json!(["-50", "0 mm", "0 mm"])); + ok(&mut h); + let made = bounds(&mut h.app.session); + // The round faces are measured on their facets: within a hundredth of a millimetre. + assert_within(made[0].0, [20.0, -10.0, -14.0], 1e-2); + assert_within(made[0].1, [40.0, 10.0, 6.0], 1e-2); + assert_within(made[1].0, [-25.0, 35.0, -5.0], 1e-2); + assert_within(made[1].1, [25.0, 85.0, 5.0], 1e-2); + assert_within(made[2].0, [-60.0, -10.0, 0.0], 1e-2); + assert_within(made[2].1, [-40.0, 10.0, 20.0], 1e-2); +} + +#[test] +fn editing_a_sphere_torus_or_cone_shows_its_placement_and_keeps_it() { + use solidcraft_engine::doc::FeatureKind; + let mut h = design("mm"); + h.app.run("parameters.add", json!({"name": "off", "expression": "15 mm"})).unwrap(); + h.app.run("solid.sphere", json!({"radius": 4, "center": ["-off", 0, 5], "operation": "new"})).unwrap(); + h.app.run("solid.torus", json!({"major": 10, "minor": 2, "center": [60, 60, 0], "operation": "new"})).unwrap(); + h.app.run("solid.cone", json!({"radius": 5, "height": 10, "base": [0, "off", 0], "operation": "new"})).unwrap(); + h.frames(2); + let ids: Vec = h.app.session.doc.features.iter().map(|f| f.id).collect(); + let stored = |h: &Harness, id: u64| match h.app.session.doc.feature(id).map(|f| &f.kind) { + Some(FeatureKind::Sphere { center, .. } | FeatureKind::Torus { center, .. }) => center.iter().map(ToString::to_string).collect::>(), + Some(FeatureKind::Cone { base, .. }) => base.iter().map(ToString::to_string).collect(), + other => panic!("not a sphere, torus or cone: {other:?}"), + }; + let (sphere_id, torus_id, cone_id) = (ids[0], ids[1], ids[2]); + // The sphere's centre, an expression, shown and kept. + let centre = stored(&h, sphere_id); + h.app.edit_feature(sphere_id); + h.frames(3); + assert_shown(&h, &[("X", "-off".into()), ("Y", "0".into()), ("Z", "5".into())]); + ok(&mut h); + assert_eq!(stored(&h, sphere_id), centre, "OK without a change keeps the centre"); + // A changed coordinate moves it. + h.app.edit_feature(sphere_id); + h.frames(3); + type_into(&mut h, "Z", "8"); + h.frames(2); + ok(&mut h); + assert_eq!(stored(&h, sphere_id), ["-off", "0", "8"]); + assert_within(bounds(&mut h.app.session)[0].0, [-19.0, -4.0, 4.0], 1e-2); + // The torus's centre and the cone's base. + for (id, shown) in [(torus_id, ["60", "60", "0"]), (cone_id, ["0", "off", "0"])] { + let before = stored(&h, id); + h.app.edit_feature(id); + h.frames(3); + assert_shown(&h, &[("X", shown[0].into()), ("Y", shown[1].into()), ("Z", shown[2].into())]); + ok(&mut h); + assert_eq!(stored(&h, id), before); + } +} + +#[test] +fn a_point_picked_in_the_sphere_or_torus_dialog_becomes_its_centre() { + use solidcraft_engine::Sel; + use solidcraft_engine::geom::Vec3; + let mut h = design("mm"); + h.app.run("solid.box", json!({"length": 40, "width": 30, "height": 20, "body_name": "Base"})).unwrap(); + h.frames(2); + for cmd in ["solid.sphere", "solid.torus"] { + h.app.start(cmd); + h.frames(3); + let mut d = h.app.dialog.take().unwrap(); + let hit = crate::viewport::Hit::Vertex { body: "Base".into(), point: Vec3::ZERO }; + assert!(d.active_input().is_some_and(|i| i.accepts(&hit, None, |_, _| true).is_some()), "the {cmd} dialog takes a vertex"); + d.pick(&h.app.session, Sel::Vertex { body: "Base".into(), point: Vec3::new(40.0, 30.0, 20.0) }); + h.app.dialog = Some(d); + h.frames(2); + assert_shown(&h, &[("X", "40 mm".into()), ("Y", "30 mm".into()), ("Z", "20 mm".into())]); + assert_eq!(sent(&h)["center"], json!(["40 mm", "30 mm", "20 mm"]), "{cmd}"); + h.app.dialog = None; + } +}