SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Joints: an as-built joint at an origin on one of its parts follows it (#2600)
NOWN-DEVf68a3dd5d542
diff --git a/crates/engine/src/cmd/joints.rs b/crates/engine/src/cmd/joints.rsindex 856f0454eb..d6d1fc5598 100644--- a/crates/engine/src/cmd/joints.rs+++ b/crates/engine/src/cmd/joints.rs@@ -31,7 +31,7 @@ pub static COMMANDS: &[CommandSpec] = &[expression no longer works out (a parameter deleted) is left out of the solve with the reason in its errors",),CommandSpec::new("joint.as_built", "As-Built Joint", as_built).at("SOLID", "ASSEMBLE").icon("as_built").params(- "type, a: occurrence, b: occurrence (stay where they are); at?: joint origin (default: b's origin) — the joint's frame; \+ "type, a: occurrence, b: occurrence (stay where they are); at?: joint origin (default: b's origin) — the joint's frame; one on a or b is kept on its face or hole there, so the joint follows it as the design changes; \name?; limits?: [[min, max] | null…]; quiet?: bool",),CommandSpec::new("joint.rigid_group", "Rigid Group", rigid_group).at("SOLID", "ASSEMBLE").icon("rigid_group").params("occurrences: [ids or names] (move together, as they are)"),@@ -669,15 +669,21 @@ fn as_built(s: &mut Session, p: &Value) -> Result<Value> {let kind = kind_param(p, cmd)?;let (oa, ob) = (occurrence_id(s, p.get("a"), cmd)?, occurrence_id(s, p.get("b"), cmd)?);// The joint's frame in the world: the given origin, or B's component origin.- let w = match p.get("at") {- Some(v) => {- let o = origin_param(s, Some(v), cmd)?;- s.doc.origin_world(&s.model.state(), &o).map_err(|e| bad(cmd, e.to_string()))?- }+ let at = p.get("at").map(|v| origin_param(s, Some(v), cmd)).transpose()?;+ let w = match &at {+ Some(o) => s.doc.origin_world(&s.model.state(), o).map_err(|e| bad(cmd, e.to_string()))?,None => joints::occurrence_world(&s.doc, ob),};- let fa = s.doc.frame_in_occurrence(oa, &w).ok_or_else(|| bad(cmd, "occurrence transform"))?;- let fb = s.doc.frame_in_occurrence(ob, &w).ok_or_else(|| bad(cmd, "occurrence transform"))?;+ // An origin on A or B is kept as it is on that side, so the joint follows its face or hole+ // as the design changes; the other side holds the frame where it is now.+ let side = |occ: u64| -> Result<Snap> {+ match &at {+ Some(o) if o.occurrence == occ => Ok(o.snap.clone()),+ _ => s.doc.frame_in_occurrence(occ, &w).ok_or_else(|| bad(cmd, "occurrence transform")),+ }+ };+ let fa = side(oa)?;+ let fb = side(ob)?;let id = next_joint_id(s, cmd)?;let name = str_(p, "name").map(str::to_string).unwrap_or_else(|| format!("Joint{id}"));let n = kind.dofs().len();@@ -1370,6 +1376,9 @@ pub(crate) fn overlapping_pairs(boxes: &[Aabb3], limit: usize) -> (Vec<(usize, u(pairs, skipped)}+#[cfg(test)]+#[path = "joints_as_built_tests.rs"]+mod as_built_tests;#[cfg(test)]#[path = "joint_edge_tests.rs"]mod edge_tests;diff --git a/crates/engine/src/cmd/joints_as_built_tests.rs b/crates/engine/src/cmd/joints_as_built_tests.rsnew file mode 100644index 0000000000..9acd97e54b--- /dev/null+++ b/crates/engine/src/cmd/joints_as_built_tests.rs@@ -0,0 +1,62 @@+//! An as-built joint made at an origin on one of its parts follows that origin's face or hole as+//! the design changes, instead of holding the frame where it was made.++use serde_json::json;+use solidcraft_geom::Vec3;++use super::tests::{occ, place, run};+use crate::Session;++/// A 200 x 60 x 10 plate with a 10 mm hole at x = `X` (X = 50), and a 20 x 20 x 20 latch beside+/// it, off the plate.+fn plate() -> Session {+ let mut s = Session::default();+ run(&mut s, "parameters.add", json!({"name": "X", "expression": "50 mm"}));+ run(&mut s, "component.create", json!({"name": "Plate"}));+ run(&mut s, "solid.box", json!({"length": 200, "width": 60, "height": 10, "corner": [0, 0, 0]}));+ run(&mut s, "solid.cylinder", json!({"base": ["X", 30, 0], "axis": [0, 0, 1], "radius": 5, "height": 10, "operation": "cut"}));+ run(&mut s, "component.activate", json!({"component": "root"}));+ run(&mut s, "component.create", json!({"name": "Latch"}));+ run(&mut s, "solid.box", json!({"length": 20, "width": 20, "height": 20, "corner": [40, 100, 0]}));+ run(&mut s, "component.activate", json!({"component": "root"}));+ let plate = occ(&s, "Plate");+ run(&mut s, "occurrence.ground", json!({"occurrence": plate, "grounded": true}));+ s+}++#[test]+fn an_as_built_joint_at_a_hole_follows_the_hole() {+ let mut s = plate();+ run(&mut s, "joint.origin", json!({"name": "Hole", "occurrence": "Plate", "circle": [55, 30, 8]}));+ let r = run(&mut s, "joint.as_built", json!({"type": "rigid", "a": "Plate", "b": "Latch", "at": "Hole", "name": "Latch"}));+ assert_eq!(r["conflicts"], json!([]), "{r}");+ let latch = |s: &Session| place(s, "Latch", Vec3::new(40.0, 100.0, 0.0));+ // Made where it is.+ assert!(latch(&s).dist(Vec3::new(40.0, 100.0, 0.0)) < 1e-6, "{:?}", latch(&s));+ for x in [80.0, 150.0, 30.0] {+ run(&mut s, "parameters.change", json!({"name": "X", "expression": format!("{x} mm")}));+ let at = latch(&s);+ assert!(at.dist(Vec3::new(40.0 + x - 50.0, 100.0, 0.0)) < 1e-6, "X {x}: {at:?}");+ }+ // The plate side keeps the hole (found by its face's name); the latch side holds its frame.+ let j = s.doc.assembly.joints.first().cloned();+ assert!(matches!(j.as_ref().map(|j| &j.a.snap), Some(solidcraft_doc::joints::Snap::CircleCenter { face: Some(_), .. })), "{j:?}");+ assert!(matches!(j.as_ref().map(|j| &j.b.snap), Some(solidcraft_doc::joints::Snap::Frame { .. })), "{j:?}");+}++#[test]+fn an_as_built_joint_at_no_origin_or_one_elsewhere_holds_its_frame() {+ let mut s = plate();+ run(&mut s, "joint.as_built", json!({"type": "rigid", "a": "Plate", "b": "Latch", "name": "Here"}));+ let j = s.doc.assembly.joints.first().cloned();+ assert!(+ matches!(+ j.as_ref().map(|j| (&j.a.snap, &j.b.snap)),+ Some((solidcraft_doc::joints::Snap::Frame { .. }, solidcraft_doc::joints::Snap::Frame { .. }))+ ),+ "{j:?}"+ );+ run(&mut s, "parameters.change", json!({"name": "X", "expression": "120 mm"}));+ let at = place(&s, "Latch", Vec3::new(40.0, 100.0, 0.0));+ assert!(at.dist(Vec3::new(40.0, 100.0, 0.0)) < 1e-6, "{at:?}");+}