diff --git a/crates/doc/src/eval.rs b/crates/doc/src/eval.rs index 27a2022a7..988876a11 100644 --- a/crates/doc/src/eval.rs +++ b/crates/doc/src/eval.rs @@ -7487,14 +7487,37 @@ fn eval_feature(doc: &Document, vals: &BTreeMap, f: &Feature, st: return replay(doc, vals, f, st, features, &mats, warning); } // Bodies: each copy is a new body named after its source. + let here = doc.component_transform(f.component); let mut output = 0; for n in bodies { let mb = st.body(n).cloned().ok_or_else(|| DocError::Unknown(format!("body `{n}`")))?; + // A body of another component is held in that component's frame, the pattern's + // transforms in this feature's: each copy is moved as the pattern moves it in the + // world and kept beside its source, in its component (#4763). Taken as they were, + // the copies landed where the source sits before its component is placed. + let owner = (mb.component != f.component).then(|| { + let frame = doc.component_transform(mb.component); + let singular = || DocError::Invalid(format!("the component holding `{n}` has a singular placement")); + Ok::<_, DocError>((frame, crate::mat_inverse(&frame).ok_or_else(singular)?, crate::mat_inverse(&here).ok_or_else(singular)?)) + }); + let owner = owner.transpose()?; for (k, m) in mats.iter().enumerate() { - let copy = moved_copy(&mb, kernel::transform_matrix(&mb.body, *m)?, k + 1); + let local = match &owner { + Some((frame, frame_inv, here_inv)) => { + crate::mat_mul(&crate::mat_mul(frame_inv, &here), &crate::mat_mul(&crate::mat_mul(m, here_inv), frame)) + } + None => *m, + }; + let copy = moved_copy(&mb, kernel::transform_matrix(&mb.body, local)?, k + 1); let name = pattern_copy_name(st, f, output, &mb.name); - copy_sheet(st, &mb.name, &name, m); - st.bodies.push(ModelBody::traced_new(name, copy, f.id)); + copy_sheet(st, &mb.name, &name, &local); + let mut body = ModelBody::traced_new(name, copy, f.id); + if owner.is_some() { + // Kept in that component as a moved body is (and through later features). + body.component = mb.component; + body.transferred = true; + } + st.bodies.push(body); output += 1; } } diff --git a/crates/engine/src/cmd/patterns_tests.rs b/crates/engine/src/cmd/patterns_tests.rs index 294de17b7..2fe7a32c5 100644 --- a/crates/engine/src/cmd/patterns_tests.rs +++ b/crates/engine/src/cmd/patterns_tests.rs @@ -384,6 +384,39 @@ fn patterns_of_components_place_occurrences() { assert!(s.execute("solid.pattern.circular", &json!({"components": ["Nope"], "axis": "Z", "count": 2})).is_err()); } +/// #4763: a body of a component moved 60 mm along X, patterned from the root. The copy goes +/// where the pattern puts the body as it is shown (beside it), not where the body sits before +/// its component is placed (x 10, near the origin). +#[test] +fn patterns_of_a_body_in_a_moved_component_follow_its_placement() { + for (kind, params, want) in [ + ("solid.pattern.path", json!({"bodies": ["Pin"], "path_sketch": "Path", "path": ["l1"], "count": 2, "spacing": 10}), [70.0, 0.0]), + ("solid.pattern.rectangular", json!({"bodies": ["Pin"], "dir1": [1, 0, 0], "count1": 2, "spacing1": 10}), [70.0, 0.0]), + ("solid.pattern.circular", json!({"bodies": ["Pin"], "axis": "Z", "count": 2, "angle": "90 deg"}), [-4.0, 60.0]), + ] { + let mut s = Session::default(); + run(&mut s, "component.create", json!({"name": "Screw"})); + run(&mut s, "solid.box", json!({"length": 4, "width": 4, "height": 4, "body_name": "Pin"})); + run(&mut s, "sketch.create", json!({"plane": "XY", "name": "Path"})); + run(&mut s, "sketch.line", json!({"points": [[0, 0], [20, 0]]})); + run(&mut s, "sketch.finish", json!({})); + run(&mut s, "component.activate", json!({"component": "root"})); + // The first part is grounded when it is made; this one is moved. + run(&mut s, "occurrence.ground", json!({"occurrence": 2, "grounded": false})); + run(&mut s, "occurrence.move", json!({"occurrence": 2, "translate": [60, 0, 0]})); + run(&mut s, kind, params); + let m = run(&mut s, "inspect.measure", json!({})); + let bodies = m["bodies"].as_array().unwrap(); + let at = |name: &str| { + let b = bodies.iter().find(|b| b["name"] == name).unwrap_or_else(|| panic!("{kind}: no `{name}` in {m}")); + [b["bbox"]["min"][0].as_f64().unwrap(), b["bbox"]["min"][1].as_f64().unwrap()] + }; + assert!((at("Pin")[0] - 60.0).abs() < 1e-6, "{kind}: the source stays at x 60"); + let copy = at("Pin (2)"); + assert!((copy[0] - want[0]).abs() < 1e-6 && (copy[1] - want[1]).abs() < 1e-6, "{kind}: the copy is at {copy:?}, want {want:?}"); + } +} + #[test] fn path_pattern_orientation_follows_the_path() { let width_of_copy = |orient: &str| {