diff --git a/crates/engine/src/cmd/component_reparent.rs b/crates/engine/src/cmd/component_reparent.rs index 35edd2d0b..c2563d197 100644 --- a/crates/engine/src/cmd/component_reparent.rs +++ b/crates/engine/src/cmd/component_reparent.rs @@ -3,8 +3,8 @@ //! once is one definition: what goes in it goes in every instance, placed by the one instance //! named (`parent_path`, else the active placement) and in the same place in the others. Paths //! through the placement change: the session's paths (selection, active and hidden placements, -//! hidden folders) follow it, and a move that would leave a feature failing that worked before -//! (a face attachment found through the old path) is refused. +//! hidden folders) and the design's face attachments found through it follow it, and a move that +//! would still leave a feature failing that worked before is refused. use super::placing::{parent_path, placement_of}; use super::*; @@ -71,6 +71,37 @@ pub(super) fn reparent(s: &mut Session, p: &Value) -> Result { { c.parent = to; } + // Face attachments found through it follow it too (#11063): a sketch on a face of a body + // inside it kept the old path, which no longer starts at the design, and the move was refused. + // A path through it by way of any placement of its old parent (a parent placed more than once + // is one definition) now runs through its one new place. A path that would grow past the 64 + // placements a design keeps is refused here rather than saved where it can't be read back. + let mut too_deep = false; + let mut rebase = |path: &mut Vec| { + if let Some(at) = path.iter().position(|x| *x == id) { + let moved: Vec = new_path.iter().chain(&path[at + 1..]).copied().collect(); + too_deep |= moved.len() > 64; + *path = moved; + } + }; + for f in &mut scratch.doc_mut().features { + match &mut f.kind { + FeatureKind::Emboss { face: Some(face), .. } => { + rebase(&mut face.path); + rebase(&mut face.author_path); + } + FeatureKind::Sketch { plane, .. } | FeatureKind::ConstructionPlane { plane } => { + if let Some(anchor) = crate::cmd::sketch::face::anchor_mut(plane) { + rebase(&mut anchor.path); + rebase(&mut anchor.author_path); + } + } + _ => {} + } + } + if too_deep { + return Err(bad(cmd, format!("moving {} would nest a face attachment more than 64 placements deep", o.name))); + } // The paths through it follow it before the model is worked out again: that drops a selected // or active placement whose path no longer leads anywhere. for sel in &mut scratch.selection { diff --git a/crates/engine/src/cmd/occurrence_tests.rs b/crates/engine/src/cmd/occurrence_tests.rs index 18dc2bff4..48182507b 100644 --- a/crates/engine/src/cmd/occurrence_tests.rs +++ b/crates/engine/src/cmd/occurrence_tests.rs @@ -1303,6 +1303,54 @@ fn reparent_keeps_the_world_position_and_follows_paths() { /// A component can't go inside itself or what it holds. Out of a parent placed more than once it /// moves from the placement named, keeping that one's place in the world. +/// #11063: a part with a hole cut from a sketch on its disc's top face moves into another +/// component. The sketch's face attachment was found through the part's old placement path, +/// which no longer starts at the design once the part is inside the other component, and the +/// move was refused ("would break Sketch2"). The attachment follows the move, and the hole stays. +#[test] +fn reparent_carries_a_sketch_on_a_face_inside_the_moved_part() { + let mut s = Session::default(); + run(&mut s, "component.create", json!({"name": "Component1", "activate": false})); + run(&mut s, "component.create", json!({"name": "Component2"})); + run(&mut s, "sketch.create", json!({"plane": "XY", "name": "Sketch1"})); + run(&mut s, "sketch.circle.center", json!({"center": [0, 0], "radius": 10})); + run(&mut s, "sketch.finish", json!({})); + run(&mut s, "solid.extrude", json!({"sketch": "Sketch1", "distance": 3, "operation": "new"})); + run(&mut s, "sketch.create", json!({"plane": {"face": [0, 5, 3]}, "name": "Sketch2"})); + run(&mut s, "sketch.circle.center", json!({"center": [0, 0], "radius": 3})); + run(&mut s, "sketch.finish", json!({})); + run(&mut s, "solid.extrude", json!({"sketch": "Sketch2", "distance": -3, "operation": "cut"})); + let volume = |s: &Session| s.world_state().bodies.iter().map(|b| b.mesh().measure().volume.abs()).sum::(); + let before = volume(&s); + let r = run(&mut s, "occurrence.reparent", json!({"occurrence": "Component2:1", "parent": "Component1"})); + assert_eq!(r["path"].as_array().map(Vec::len), Some(2), "{r}"); + assert!(s.model.results.iter().all(|r| r.error.is_none()), "every feature still builds"); + assert!((volume(&s) - before).abs() < 1e-6, "the disc keeps its hole: {} vs {before}", volume(&s)); + let reopened = Session::new(crate::doc::Document::from_json(&s.doc.to_json()).unwrap()); + assert!(reopened.model.results.iter().all(|r| r.error.is_none()), "and after reopening"); +} + +/// A parent placed twice is one definition: a sketch on the child's face made through the +/// second placement moves along when the child is moved out through the first one, to the +/// child's one new place. +#[test] +fn reparent_carries_a_face_sketch_made_through_another_placement_of_the_parent() { + let mut s = Session::default(); + let (parent, first_parent) = created(&mut s, "Assembly"); + let (_, child) = created(&mut s, "Knob"); + run(&mut s, "solid.box", json!({"length": 10, "width": 10, "height": 10, "corner": [0, 0, 0], "body_name": "Knob"})); + let second_parent = run(&mut s, "occurrence.copy", json!({"component": parent, "translate": [20, 0, 0]}))["occurrence"].as_u64().unwrap(); + run(&mut s, "sketch.create", json!({"plane": {"face": [25, 5, 10], "path": [second_parent, child]}, "name": "OnTop"})); + run(&mut s, "sketch.finish", json!({})); + let out = run(&mut s, "occurrence.reparent", json!({"occurrence": child, "parent": "root", "path": [first_parent, child]})); + assert_eq!(out["path"], json!([child])); + assert!(s.model.results.iter().all(|r| r.error.is_none()), "the sketch still finds its face"); + let sketch = s.doc.features.iter().find(|f| f.name == "OnTop").unwrap(); + let solidcraft_doc::FeatureKind::Sketch { plane, .. } = &sketch.kind else { panic!("OnTop is no sketch") }; + let anchor = crate::cmd::sketch::face::anchor(plane).unwrap(); + assert_eq!(anchor.path, vec![child], "{anchor:?}"); +} + #[test] fn reparent_refuses_cycles_and_moves_out_of_a_named_instance() { let mut s = Session::default();