Skip to content

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

Fix fastener placement on transformed washers (#9648) (#10583)

Colin Faulkingham78c5b8de427c
diff --git a/crates/engine/src/cmd/parts.rs b/crates/engine/src/cmd/parts.rs
index 0a651c013a..1a9c9a907d 100644
--- a/crates/engine/src/cmd/parts.rs
+++ b/crates/engine/src/cmd/parts.rs
@@ -624,7 +624,8 @@ fn hole_picks(p: &Value, cmd: &str) -> Result<Vec<Vec3>> {
/// A point on the cylindrical wall of the hole at `a`, a little in from the picked rim, and the
/// point on the hole's axis level with it (the same for every pick on one rim).
fn hole_wall(s: &Session, a: Vec3) -> Option<(Vec3, Vec3)> {
- let st = s.model.state();
+ // Picks are in world coordinates, including rims on already placed standard parts.
+ let st = s.world_state();
let dirs = [Vec3::Z * -1.0, Vec3::Z, Vec3::X, Vec3::X * -1.0, Vec3::Y, Vec3::Y * -1.0];
for step in [0.2, 0.5, 1.0, 2.0] {
for b in &st.bodies {
@@ -859,6 +860,42 @@ mod tests {
assert!(s.model.state().threads.iter().any(|t| t.designation == "M8" && (t.pitch - 1.25).abs() < 1e-9));
}
+ #[test]
+ fn a_screw_seats_on_a_translated_washer() {
+ for z in [11.6, 11.2] {
+ let mut s = Session::default();
+ run(&mut s, "solid.box", json!({"length":60,"width":60,"height":10,"body_name":"Plate"}));
+ run(&mut s, "solid.hole", json!({"position":[19.6,33.948,10],"diameter":8.4}));
+ let washer = run(&mut s, "parts.insert", json!({"family":"washer","size":"M8","at":[23.8,33.948,10]}));
+ let before = s.doc.clone();
+ let undo = s.undo.len();
+ // Both the top bore rim and the wall are world picks on the seated washer (#9648).
+ let screw = run(&mut s, "parts.fastener", json!({"family":"socket_head_cap_screw","size":"M8","length":20,"at":[23.8,33.948,z]}));
+ let joint = s.doc.assembly.joints.iter().find(|j| Some(j.id) == screw["joint"].as_u64()).unwrap();
+ assert_eq!(Some(joint.a.occurrence), washer["occurrence"].as_u64());
+ let (lo, hi) = bbox(&s, "SHCS M8x20");
+ assert!((lo.z + 8.4).abs() < 1e-6 && (hi.z - 19.6).abs() < 1e-6, "{lo:?} {hi:?}");
+ assert!(((lo.x + hi.x) / 2.0 - 19.6).abs() < 1e-6 && ((lo.y + hi.y) / 2.0 - 33.948).abs() < 1e-6, "{lo:?} {hi:?}");
+ assert_eq!(s.undo.len(), undo + 1);
+ run(&mut s, "edit.undo", json!({}));
+ assert_eq!(s.doc, before);
+ }
+ }
+
+ #[test]
+ fn a_screw_seats_on_a_rotated_washer() {
+ let mut s = Session::default();
+ let washer = run(&mut s, "parts.insert", json!({"family":"washer","size":"M8","point":[40,50,60],"direction":[1,0,0]}));
+ run(&mut s, "occurrence.ground", json!({"occurrence":washer["occurrence"],"grounded":true}));
+ let screw = run(&mut s, "parts.insert", json!({"family":"socket_head_cap_screw","size":"M8","length":20,"at":[41.6,54.2,60]}));
+ let joint = s.doc.assembly.joints.iter().find(|j| Some(j.id) == screw["joint"].as_u64()).unwrap();
+ assert_eq!(Some(joint.a.occurrence), washer["occurrence"].as_u64());
+ // The seating face is X = 41.6: the 20 mm shank runs along -X and the 8 mm head along +X.
+ let (lo, hi) = bbox(&s, "SHCS M8x20");
+ assert!(lo.dist(solidcraft_geom::Vec3::new(21.6, 43.5, 53.5)) < 1e-6, "{lo:?}");
+ assert!(hi.dist(solidcraft_geom::Vec3::new(49.6, 56.5, 66.5)) < 1e-6, "{hi:?}");
+ }
+
/// Several holes in one insert: one component placed and seated once per hole, in one undo
/// step (#9341); two picks of one rim are one hole; a pick off every body fails the whole
/// insert.
diff --git a/crates/engine/src/frames.rs b/crates/engine/src/frames.rs
index 45eb016c9d..afe7de3867 100644
--- a/crates/engine/src/frames.rs
+++ b/crates/engine/src/frames.rs
@@ -90,7 +90,9 @@ fn table(id: &str) -> &'static [(&'static str, K)] {
"sketch.project_to_surface" | "sketch.isoparametric_curve" => &[("face", Here)],
"sketch.intersection_curve" => &[("a", Here), ("b", Here)],
"inspect.curvature_comb" | "inspect.minimum_radius" | "inspect.isocurve" => &[("edges", Picks), ("faces", Picks)],
- "parts.insert" | "parts.fastener" => &[("at", Picks), ("point", Make), ("direction", Dir)],
+ // A fastener's `at` picks locate host occurrences and feed joint.create in world
+ // coordinates. Only free placement belongs in the active component's frame.
+ "parts.insert" | "parts.fastener" => &[("point", Make), ("direction", Dir)],
"appearance.assign" => &[("faces", Picks), ("bodies", Body), ("body", Body)],
"solid.move" => &[("bodies", Body), ("translate", Delta), ("axis", Dir), ("origin", Moved)],
"solid.combine" => &[("target", Body), ("tools", Body)],