Skip to content

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

Box, Sphere and Torus: clicking a point of a sketch places them there (#9403) (#9405)

austin62jakebf625dff0509
diff --git a/assets/locales/pending/primitive-at-sketch-point.json b/assets/locales/pending/primitive-at-sketch-point.json
new file mode 100644
index 0000000000..522d0b5ee1
--- /dev/null
+++ b/assets/locales/pending/primitive-at-sketch-point.json
@@ -0,0 +1,8 @@
+{
+ "en": { "dialog.selection_external_point": "{point} · {sketch}" },
+ "ar": { "dialog.selection_external_point": "{point} · {sketch}" },
+ "he": { "dialog.selection_external_point": "{point} · {sketch}" },
+ "ja": { "dialog.selection_external_point": "{point} · {sketch}" },
+ "vi": { "dialog.selection_external_point": "{point} · {sketch}" },
+ "zh": { "dialog.selection_external_point": "{point} · {sketch}" }
+}
diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs
index b766acac05..02ff70fe24 100644
--- a/crates/ui-egui/src/dialogs.rs
+++ b/crates/ui-egui/src/dialogs.rs
@@ -1229,11 +1229,16 @@ impl Dialog {
}
}
Self::take_component_bodies(app, &mut d);
- // A primitive goes where a face or vertex is picked, standing on a planar face.
- if let Kind::Primitive { at, .. } = &mut d.kind {
+ // A primitive goes where a face or vertex is picked, standing on a planar face. The Box,
+ // Sphere and Torus also go to a sketch point (#9403).
+ if let Kind::Primitive { cmd, at, .. } = &mut d.kind {
+ let sketch_points = !crate::primitive_placement::inputs(cmd).is_empty();
*at = s.selection.iter().find_map(|sel| match sel {
Sel::Face { body, index, point } => Some((*point, planar_face(s, body, *index).map(|(_, n)| n))),
Sel::Vertex { point, .. } => Some((*point, None)),
+ Sel::ExternalSketchPoint { .. } | Sel::SketchPoint { .. } if sketch_points => {
+ crate::primitive_placement::world_point(s, sel).map(|p| (p, None))
+ }
_ => None,
});
}
@@ -2852,6 +2857,12 @@ pub(crate) fn selection_label(s: &Session, names: crate::prefs::OriginPlaneNames
Sel::ExternalSketchCurve { sketch, id } => {
crate::i18n::format("dialog.selection_external_curve", &[("sketch", &sketch_name(*sketch)), ("id", id)])
}
+ // Another sketch's point (a Sphere's centre, #9403), named as Select Other names it.
+ Sel::ExternalSketchPoint { sketch, id } => {
+ let state = crate::open_meshing::session_world(s);
+ let point = state.sketch(*sketch).map_or_else(|| id.clone(), |ss| crate::select_other::point_label(&ss.sketch, id));
+ crate::i18n::format("dialog.selection_external_point", &[("point", &point), ("sketch", &sketch_name(*sketch))])
+ }
Sel::SketchConstraint { id } => crate::i18n::format("dialog.selection_constraint", &[("id", id)]),
Sel::Profile { sketch, index } => {
crate::i18n::format("dialog.selection_profile", &[("sketch", &sketch_name(*sketch)), ("number", &index.saturating_add(1).to_string())])
@@ -2876,7 +2887,6 @@ pub(crate) fn selection_label(s: &Session, names: crate::prefs::OriginPlaneNames
format!("({:.2}, {:.2}) mm", p.x, p.y)
}
Sel::Vertex { body, .. } => format!("{body} · vertex"),
- _ => format!("{item:?}"),
}
}
diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs
index 8ab93ba2ae..07d562a9f2 100644
--- a/crates/ui-egui/src/lib.rs
+++ b/crates/ui-egui/src/lib.rs
@@ -1954,6 +1954,15 @@ impl SolveApp {
// it was clicked in, so take that one before the turn starts (#10571).
self.pre_command_cam = finished_sketch.is_some().then(|| self.settled_cam());
if finished_sketch.is_some() {
+ // A point picked in the sketch being finished stays a pick of that sketch: its
+ // placement reads where the sketch puts it once the sketch is no longer the open one.
+ if let (Some(sketch), "solid.box" | "solid.sphere" | "solid.torus") = (finished_sketch, id) {
+ for sel in self.session.selection.iter_mut() {
+ if let solidcraft_engine::Sel::SketchPoint { id } = sel {
+ *sel = solidcraft_engine::Sel::ExternalSketchPoint { sketch, id: id.clone() };
+ }
+ }
+ }
self.finish_sketch();
// A feature committed before the marker is back would go in where the sketch was.
if self.deferred.is_some() {
diff --git a/crates/ui-egui/src/primitive_placement.rs b/crates/ui-egui/src/primitive_placement.rs
index 1583ef72a4..c09fbf603c 100644
--- a/crates/ui-egui/src/primitive_placement.rs
+++ b/crates/ui-egui/src/primitive_placement.rs
@@ -1,8 +1,9 @@
//! 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.
+//! picked in the view (a vertex, a point on an edge or face, the origin, a point of a shown
+//! sketch) fills the fields of the Box, Sphere and Torus; one selected before the window opens
+//! places them there (#9403).
//!
//! 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
@@ -13,9 +14,10 @@ use serde_json::{Map, Value, json};
use solidcore_record::Vec3;
use solidcraft_engine::Session;
use solidcraft_engine::doc::expr::Kind as ValueKind;
+use solidcraft_engine::geom::PlaneGeom as _;
use crate::i18n::tr;
-use crate::selection::{EDGES, FACES, ORIGIN, SelInput, VERTICES};
+use crate::selection::{EDGES, EXTERNAL_POINTS, FACES, ORIGIN, POINTS, SelInput, VERTICES};
use crate::ui_kit::{self, choice_row, expr_row};
use solidcraft_engine::Sel;
@@ -33,10 +35,13 @@ fn keys(cmd: &str) -> &'static [&'static str] {
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 Sphere and Torus for their centre.
+/// the Sphere and Torus for their centre. It takes the points of every shown sketch, each with
+/// its sketch (#9403).
pub(crate) fn inputs(cmd: &str) -> Vec<SelInput> {
match cmd {
- "solid.box" | "solid.sphere" | "solid.torus" => vec![SelInput::new("Point", VERTICES | EDGES | FACES | ORIGIN, false)],
+ "solid.box" | "solid.sphere" | "solid.torus" => {
+ vec![SelInput::new("Point", VERTICES | EDGES | FACES | ORIGIN | POINTS | EXTERNAL_POINTS, false)]
+ }
_ => Vec::new(),
}
}
@@ -104,13 +109,25 @@ fn mm(v: f64) -> String {
format!("{v} mm")
}
+/// Where a pick is in the world: a vertex, or a point on an edge or face, where it was picked; a
+/// sketch point where its sketch puts it (one without a sketch is the open sketch's).
+pub(crate) fn world_point(s: &Session, sel: &Sel) -> Option<Vec3> {
+ let (sketch, id) = match sel {
+ Sel::Vertex { point, .. } | Sel::Face { point, .. } | Sel::Edge { point, .. } => return Some(*point),
+ Sel::ExternalSketchPoint { sketch, id } => (*sketch, id),
+ Sel::SketchPoint { id } => (s.active_sketch?, id),
+ _ => return None,
+ };
+ let st = s.world_state().ok()?;
+ let ss = st.sketch(sketch)?;
+ let at = ss.sketch.resolve_point(id).and_then(|i| ss.sketch.point(i))?;
+ Some(ss.plane.to_world(at))
+}
+
/// A point picked into the Box's input: it goes into the X, Y and Z of the current placement
/// (the corner or the centre), replacing a pre-selected face or vertex.
pub(crate) fn picked(s: &Session, cmd: &str, at: &mut Option<(Vec3, Option<Vec3>)>, extra: &mut Map<String, Value>, sel: Option<&Sel>) {
- let point = match sel {
- Some(Sel::Vertex { point, .. } | Sel::Face { point, .. } | Sel::Edge { point, .. }) => *point,
- _ => return,
- };
+ let Some(point) = sel.and_then(|sel| world_point(s, sel)) else { return };
if !point.is_finite() || inputs(cmd).is_empty() {
return;
}
diff --git a/crates/ui-egui/src/primitive_units_tests.rs b/crates/ui-egui/src/primitive_units_tests.rs
index ea52f69463..01bec23d64 100644
--- a/crates/ui-egui/src/primitive_units_tests.rs
+++ b/crates/ui-egui/src/primitive_units_tests.rs
@@ -720,3 +720,53 @@ fn a_point_picked_in_the_sphere_or_torus_dialog_becomes_its_centre() {
h.app.dialog = None;
}
}
+
+#[test]
+fn a_sketch_point_places_the_box_sphere_and_torus() {
+ // #9403: a point of a sketch, clicked in the window or selected before it opens, is where the
+ // primitive goes, where the sketch puts it in the world (here 20 mm up, on an offset plane).
+ use solidcraft_engine::Sel;
+ let mut h = design("mm");
+ h.app.run("sketch.create", json!({"plane": "XY", "offset": 20, "name": "Marks"})).unwrap();
+ h.app.run("sketch.point", json!({"point": [12, 7], "id": "m"})).unwrap();
+ h.app.run("sketch.finish", json!({})).unwrap();
+ h.frames(3);
+ for (cmd, key) in [("solid.sphere", "center"), ("solid.torus", "center"), ("solid.box", "corner")] {
+ h.app.start(cmd);
+ h.frames(3);
+ h.step(&json!({"click": {"world": [12, 7, 20]}})).unwrap();
+ h.frames(2);
+ assert_shown(&h, &[("X", "12 mm".into()), ("Y", "7 mm".into()), ("Z", "20 mm".into())]);
+ assert_eq!(sent(&h)[key], json!(["12 mm", "7 mm", "20 mm"]), "{cmd}");
+ let d = h.app.dialog.as_ref().unwrap();
+ let chip = crate::dialogs::selection_label(&h.app.session, crate::prefs::OriginPlaneNames::Xyz, d, &d.inputs[0], &d.inputs[0].items[0]);
+ // Numbered as Select Other numbers it: the sketch's origin is point 1.
+ assert_eq!(chip, "Point 2 · Marks", "the chip names the point and its sketch");
+ h.app.dialog = None;
+ }
+ let sketch = h.app.session.doc.features.iter().find(|f| f.name == "Marks").map(|f| f.id).unwrap();
+ h.app.session.selection = vec![Sel::ExternalSketchPoint { sketch, id: "m".into() }];
+ h.app.start("solid.sphere");
+ h.frames(3);
+ assert_eq!(sent(&h)["center"], json!([12.0, 7.0, 20.0]));
+ ok(&mut h);
+ let made = bounds(&mut h.app.session);
+ assert_within(made[0].0, [2.0, -3.0, 10.0], 0.05);
+ assert_within(made[0].1, [22.0, 17.0, 30.0], 0.05);
+}
+
+#[test]
+fn a_point_selected_in_the_open_sketch_still_places_the_sphere_when_the_sketch_ends() {
+ // Starting the sphere finishes the sketch; the point picked in it is then the finished
+ // sketch's, and still where the sphere goes.
+ use solidcraft_engine::Sel;
+ let mut h = design("mm");
+ h.app.run("sketch.create", json!({"plane": "XY", "offset": 20, "name": "Marks"})).unwrap();
+ h.app.run("sketch.point", json!({"point": [12, 7], "id": "m"})).unwrap();
+ assert!(h.app.session.active_sketch.is_some());
+ h.app.session.selection = vec![Sel::SketchPoint { id: "m".into() }];
+ h.app.start("solid.sphere");
+ h.frames(3);
+ assert!(h.app.session.active_sketch.is_none(), "the sphere ends the sketch");
+ assert_eq!(sent(&h)["center"], json!([12.0, 7.0, 20.0]));
+}
diff --git a/crates/ui-egui/src/select_other.rs b/crates/ui-egui/src/select_other.rs
index 4909aaf778..61aface993 100644
--- a/crates/ui-egui/src/select_other.rs
+++ b/crates/ui-egui/src/select_other.rs
@@ -293,7 +293,7 @@ pub(crate) fn curve_label(sk: &solidcraft_engine::sketch::Sketch, id: &str) -> S
}
/// "Point (arc 2 centre)": the role it plays in the first curve that uses it.
-fn point_label(sk: &solidcraft_engine::sketch::Sketch, id: &str) -> String {
+pub(crate) fn point_label(sk: &solidcraft_engine::sketch::Sketch, id: &str) -> String {
let Some(pi) = sk.points.iter().position(|p| p.id == id) else { return id.to_string() };
let role = sk.curves.iter().find_map(|c| {
let name = format!("{} {}", curve_kind_name(&c.kind).to_lowercase(), id_number(&c.id).unwrap_or(0));