SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Extrude: a face shared by two bodies is resolved in the active occurrence (#419) (#6469)
Homero Thompson7e8f87dc575b
diff --git a/crates/engine/src/cmd/face.rs b/crates/engine/src/cmd/face.rsindex 0903743629..5571f43cad 100644--- a/crates/engine/src/cmd/face.rs+++ b/crates/engine/src/cmd/face.rs@@ -181,9 +181,24 @@ type FacePick = (std::sync::Arc<Mesh>, u32, f64, String);/// The face under a point of `only` (any body when `None`). Where faces of several bodies are/// equally near (bodies touching flush), the pick is ambiguous: `Err` names those bodies.+/// A pick of the running command was resolved against the placed model, so the body under it+/// wins: a body that only shares its place in the model is not in the way (#419). A named+/// body still wins over that.fn face_at_in(s: &Session, p: Vec3, only: Option<&str>) -> std::result::Result<Option<FacePick>, Vec<String>> {let st = s.model.state();- face_at_state(&st, p, only)+ match (crate::frames::picked(p), only) {+ (Some(names), None) if names.len() > 1 => Err(model_names(&st, &names)),+ (Some(names), None) => {+ let under = names.first().and_then(|n| st.bodies.iter().find(|b| b.definition_name() == n.as_str()).map(|b| b.name.clone()));+ face_at_state(&st, p, under.as_deref())+ }+ (_, only) => face_at_state(&st, p, only),+ }+}++/// The names the model gives the bodies a pick met.+fn model_names(st: &solidcraft_doc::ModelState, names: &[String]) -> Vec<String> {+ names.iter().map(|n| st.bodies.iter().find(|b| b.definition_name() == n.as_str()).map(|b| b.name.clone()).unwrap_or_else(|| n.clone())).collect()}pub(super) fn face_at_state(st: &solidcraft_doc::ModelState, p: Vec3, only: Option<&str>) -> std::result::Result<Option<FacePick>, Vec<String>> {diff --git a/crates/engine/src/cmd/face_join_tests.rs b/crates/engine/src/cmd/face_join_tests.rsindex 7ebd9a7a14..ca5f61430c 100644--- a/crates/engine/src/cmd/face_join_tests.rs+++ b/crates/engine/src/cmd/face_join_tests.rs@@ -126,6 +126,44 @@ fn face_join_of_a_step_import_keeps_all_32_neighbours_unchanged() {assert_eq!(targets, &["Wall"]);}+/// #419: a face pick is resolved against the placed state: a leaf of an assembly that its+/// joint swung away does not make the pick ambiguous, even though both leaves are built at+/// the same local place. The pick lands on the leaf that is shown there.+#[test]+fn a_face_pick_on_an_assembly_leaf_is_resolved_where_the_leaf_is_shown() {+ let mut s = Session::default();+ for leaf in ["Leaf A", "Leaf B"] {+ run(&mut s, "component.activate", json!({"component": "root"}));+ run(&mut s, "component.create", json!({"name": leaf}));+ run(&mut s, "solid.box", json!({"length": 40, "width": 30, "height": 3, "corner": [0, 0, 0]}));+ run(&mut s, "solid.cylinder", json!({"base": [0, 33, 4], "axis": [1, 0, 0], "radius": 4, "height": 40, "operation": "new"}));+ }+ run(&mut s, "component.activate", json!({"component": "root"}));+ run(&mut s, "occurrence.ground", json!({"occurrence": "Leaf A:1", "grounded": true}));+ run(+ &mut s,+ "joint.create",+ json!({+ "type": "revolute",+ "name": "Hinge",+ "a": {"occurrence": "Leaf A:1", "circle": [1, 37, 4]},+ "b": {"occurrence": "Leaf B:1", "circle": [1, 37, 4]},+ "limits": [["0 deg", "180 deg"]]+ }),+ );+ run(&mut s, "joint.drive", json!({"joint": "Hinge", "value": 60}));+ // Leaf B is swung 60° away, so the pick is on Leaf A's top face, where it is shown.+ let result = run(+ &mut s,+ "solid.extrude",+ json!({"face": [13.002403831300967, 9.849781594495369, 3.0], "distance": "10 mm", "direction": "positive", "operation": "join"}),+ );+ assert_eq!(result["changed_bodies"], json!(["Body1"]));+ let state = s.model.state();+ let volume = solidcraft_kernel::measure(&state.body("Body1").unwrap().body).unwrap().volume;+ assert!((volume - 15_600.0).abs() < 1e-6, "{volume}");+}+/// #1349: a profile sketched on a body's face joins that body, and leaves alone a neighbour/// whose face it lies on too: a rib on the side of a bracket, drawn down over the motor the/// bracket sits on, welded the motor in (a join that ran for minutes on the imported motor anddiff --git a/crates/engine/src/frames.rs b/crates/engine/src/frames.rsindex e55b6eaca8..64fb163bd3 100644--- a/crates/engine/src/frames.rs+++ b/crates/engine/src/frames.rs@@ -142,17 +142,19 @@ fn pt(p: Vec3) -> Value {serde_json::json!([p.x, p.y, p.z])}-/// The frame of the body a world point lies on (the nearest), or of the named body.-fn frame_at(s: &Session, f: &Frames, p: Vec3, body: Option<&str>) -> Option<Mat> {+/// The frame of the body a world point lies on (the nearest), or of the named body, with the+/// bodies whose faces meet there: the first is the body under the point, and more than one+/// means the pick is ambiguous.+fn frame_at(s: &Session, f: &Frames, p: Vec3, body: Option<&str>) -> Option<(Mat, Vec<String>)> {if let Some(b) = body- && let Some((_, _, m)) = f.bodies(s).iter().find(|(w, _, _)| w == b)+ && let Some((_, own, m)) = f.bodies(s).iter().find(|(w, _, _)| w == b){- return Some(*m);+ return Some((*m, vec![own.clone()]));}let world = s.world_state();- let mut best: Option<(f64, Mat)> = None;+ let mut best: Option<(f64, Mat, Vec<String>)> = None;for wb in &world.bodies {- let Some((_, _, m)) = f.bodies(s).iter().find(|(w, _, _)| *w == wb.name) else { continue };+ let Some((_, own, m)) = f.bodies(s).iter().find(|(w, _, _)| *w == wb.name) else { continue };let mesh = wb.mesh();let bb = mesh.bounds();let tol = (bb.diagonal() * 1e-3).max(1e-3);@@ -166,11 +168,23 @@ fn frame_at(s: &Session, f: &Frames, p: Vec3, body: Option<&str>) -> Option<Mat>continue;}let d = mesh.triangles.iter().filter_map(|t| mesh.tri(t)).map(|[a, b, c]| tri_dist(p, a, b, c)).fold(f64::INFINITY, f64::min);- if d <= tol && best.as_ref().is_none_or(|(bd, _)| d < *bd) {- best = Some((d, *m));+ if d > tol {+ continue;+ }+ // Bodies whose faces are as near the point as the nearest one meet there: two of them+ // make the pick ambiguous. The same body placed twice is one body, not a tie.+ let as_near = best.as_ref().is_some_and(|(bd, _, _)| (d - *bd).abs() <= 1e-8);+ if as_near {+ if let Some((_, _, names)) = &mut best+ && !names.contains(own)+ {+ names.push(own.clone());+ }+ } else if best.as_ref().is_none_or(|(bd, _, _)| d < *bd) {+ best = Some((d, *m, vec![own.clone()]));}}- best.map(|(_, m)| m)+ best.map(|(_, m, names)| (m, names))}fn tri_dist(p: Vec3, a: Vec3, b: Vec3, c: Vec3) -> f64 {@@ -195,9 +209,17 @@ struct Mapper<'a> {impl Mapper<'_> {fn pick(&mut self, v: &Value, body: Option<&str>) -> Option<Value> {let p = vec3(v)?;- let m = if self.here { self.f.sketch } else { frame_at(self.s, &self.f, p, body).unwrap_or(self.f.active) };+ let under = if self.here { None } else { frame_at(self.s, &self.f, p, body) };+ let m = if self.here { self.f.sketch } else { under.as_ref().map(|(m, _)| *m).unwrap_or(self.f.active) };self.first.get_or_insert(m);- self.local_point(&m, p)+ let out = self.local_point(&m, p);+ // The command sees the pick in the frame of the body under it. Remember the bodies+ // whose faces meet there, so a face of that body is found and a body that only+ // shares its place in the model does not make the pick ambiguous (#419).+ if let Some((_, names)) = &under {+ remember(out.as_ref().and_then(vec3).unwrap_or(p), names.clone());+ }+ out}fn local_point(&mut self, m: &Mat, p: Vec3) -> Option<Value> {@@ -315,6 +337,36 @@ thread_local! {static DEPTH: std::cell::Cell<u32> = const { std::cell::Cell::new(0) };/// The outermost command's coordinates were mapped here (its table lists them).static MAPPED: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };+ /// The picks of the running command: the point it sees and the bodies whose faces meet+ /// there (the first is the body under it; more than one means the pick is ambiguous).+ static PICKS: std::cell::RefCell<Vec<(Vec3, Vec<String>)>> = const { std::cell::RefCell::new(Vec::new()) };+}++/// How many picks of one command are remembered (a command takes few).+const MAX_PICKS: usize = 4096;+/// How many bodies may meet at one pick (an ambiguous pick names them).+const MAX_PICK_BODIES: usize = 64;++/// Remember the bodies whose faces meet at a pick, against the point the command sees.+fn remember(point: Vec3, names: Vec<String>) {+ let names: Vec<String> = names.into_iter().take(MAX_PICK_BODIES).collect();+ PICKS.with(|picks| {+ let mut picks = picks.borrow_mut();+ if picks.len() < MAX_PICKS {+ picks.push((point, names));+ }+ });+}++/// The bodies whose faces meet at `point`, when the running command picked it: the first is+/// the body under the pick, and more than one means the pick is ambiguous.+pub(crate) fn picked(point: Vec3) -> Option<Vec<String>> {+ PICKS.with(|picks| picks.borrow().iter().find(|(p, _)| *p == point).map(|(_, names)| names.clone()))+}++/// Forget the picks of the last command: a new one starts with none.+pub(crate) fn forget_picks() {+ PICKS.with(|picks| picks.borrow_mut().clear());}/// Did this module map the running command's coordinates? (Otherwise a new feature's geometrydiff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rsindex 4f7207088f..e48da67560 100644--- a/crates/engine/src/lib.rs+++ b/crates/engine/src/lib.rs@@ -1160,7 +1160,12 @@ impl Session {// Picks in the world go into their component's frame (once: commands run by commands// already have them there).let (_nested, outermost) = frames::Nested::enter(spec.id);- let mapped = if outermost { frames::to_local(self, spec.id, params) } else { None };+ let mapped = if outermost {+ frames::forget_picks();+ frames::to_local(self, spec.id, params)+ } else {+ None+ };let params = mapped.as_ref().unwrap_or(params);if outermost {crash::command_begin(self, spec.id, given);