From 9b8016342163d04eae4da78bba80085bbfd6782a Mon Sep 17 00:00:00 2001 From: raykholo Date: Wed, 21 Oct 2026 00:54:16 +0000 Subject: [PATCH] Extrudes from a nested STEP part's face join and cut it with the imported root active (#3223, #2872) (#3852) * claim: #3223 * Extrudes from a nested STEP part's face join and cut it with the imported root active (#3223, #2872) Opening a STEP assembly leaves its root component active, and the root owns no body: its parts are components inside it. A profile sketched on such a part's face was therefore treated as outside the active scope. Join made a separate body instead of joining the part, and the automatic operation saw no material and offered New Body in either direction; an automatic extrude that came out as a cut found nothing to cut. A component with no body of its own now works on the bodies of the components inside it whose geometry lies in its own frame (as an import places them): Join targets the body whose face the sketch is on, the automatic operation looks at those bodies, and an extrude whose own operation is Cut or Intersect takes part in every body as an explicit cut already did. --- crates/engine/src/cmd/features.rs | 50 ++++++++++++++++----- crates/engine/src/cmd/occurrence_tests.rs | 55 +++++++++++++++++++++++ 2 files changed, 95 insertions(+), 10 deletions(-) diff --git a/crates/engine/src/cmd/features.rs b/crates/engine/src/cmd/features.rs index 8f9d762c6..51c5ad8fc 100644 --- a/crates/engine/src/cmd/features.rs +++ b/crates/engine/src/cmd/features.rs @@ -205,13 +205,16 @@ fn auto_operation_in(s: &Session, p: &Value, world_faces: bool) -> Option<&'stat // Match the evaluator's implicit participants: a grouped cut from an empty // assembly component considers every body; otherwise keep the active scope. // Explicit participants override either default. + // A component with no body of its own works on those of the components inside it (#3223). let parts = string_list(p, "participants"); let implicit_all = grouped.is_some() && !st.bodies.iter().any(|body| s.doc.model_body_component(body) == s.active_component); - let bodies: Vec<_> = st - .bodies - .iter() - .filter(|b| if parts.is_empty() { implicit_all || s.doc.model_body_component(b) == s.active_component } else { parts.contains(&b.name) }) - .collect(); + let bodies: Vec<_> = if !parts.is_empty() { + st.bodies.iter().filter(|b| parts.contains(&b.name)).collect() + } else if implicit_all { + st.bodies.iter().collect() + } else { + active_scope(s, &st) + }; if bodies.is_empty() { return Some("new"); } @@ -494,11 +497,12 @@ fn add_feature_in(s: &mut Session, p: &Value, kind: FeatureKind, world: bool) -> } // A cut or intersect made where the active component has no body of its own (a sketch in // the assembly's root, cutting through the parts, #1234) would have nothing to work on: it - // takes part in every body instead, which it changes only where it meets them. + // takes part in every body instead, which it changes only where it meets them. An extrude's + // own operation counts too: an automatic one that came out as a cut (#3223). let participants = if participants.is_empty() && !primitive && (matches!(str_(p, "operation").and_then(Operation::parse), Some(Operation::Cut | Operation::Intersect)) - || p.get("faces").is_some() && matches!(&kind, FeatureKind::Extrude { operation: Operation::Cut | Operation::Intersect, .. })) + || matches!(&kind, FeatureKind::Extrude { operation: Operation::Cut | Operation::Intersect, .. })) && { let st = s.model.state(); !st.bodies.is_empty() && !st.bodies.iter().any(|b| s.doc.model_body_component(b) == s.active_component) @@ -652,14 +656,40 @@ fn extrude(s: &mut Session, p: &Value) -> Result { } /// The body whose face `sketch` is attached to (on the face itself, not offset from it), when -/// it is a body of the active component. +/// it is one the active component works on ([`active_scope`]). fn sketch_face_body(s: &Session, sketch: u64) -> Option { let FeatureKind::Sketch { plane: solidcraft_doc::PlaneRef::Face { anchor: Some(anchor), .. }, .. } = &s.doc.feature(sketch)?.kind else { return None; }; let st = s.model.state(); - let body = st.body(&anchor.target.body)?; - (s.doc.model_body_component(body) == s.active_component).then(|| body.name.clone()) + active_scope(s, &st).into_iter().find(|b| b.name == anchor.target.body).map(|b| b.name.clone()) +} + +/// The bodies a feature of the active component works on when it names none: the component's +/// own. A component with no body of its own (the root of an opened STEP assembly, whose parts +/// are components inside it) works on the bodies of the components inside it whose geometry is +/// in its own frame, as an import places it: a profile sketched on such a part's face meets the +/// part where it is shown (#3223). +fn active_scope<'a>(s: &Session, st: &'a solidcraft_doc::ModelState) -> Vec<&'a solidcraft_doc::ModelBody> { + let active = s.active_component; + let own: Vec<_> = st.bodies.iter().filter(|b| s.doc.model_body_component(b) == active).collect(); + if !own.is_empty() { + return own; + } + let Some(into_active) = solidcraft_doc::mat_inverse(&s.doc.component_transform(active)) else { return own }; + st.bodies + .iter() + .filter(|b| { + let comp = s.doc.model_body_component(b); + if !s.doc.component_within(comp, active) { + return false; + } + let frame = s.doc.component_transform(comp); + let world = s.doc.model_body_offset(b).map_or(frame, |offset| solidcraft_doc::mat_mul(&frame, offset)); + let rel = solidcraft_doc::mat_mul(&into_active, &world); + rel.iter().flatten().zip(solidcraft_doc::IDENTITY.iter().flatten()).all(|(a, b)| (a - b).abs() < 1e-9) + }) + .collect() } /// A thin extrude's wall from its parameters: the sketch's curves it follows (every drawn diff --git a/crates/engine/src/cmd/occurrence_tests.rs b/crates/engine/src/cmd/occurrence_tests.rs index 365710557..d5bdfd9de 100644 --- a/crates/engine/src/cmd/occurrence_tests.rs +++ b/crates/engine/src/cmd/occurrence_tests.rs @@ -616,6 +616,61 @@ fn matrix_move_turns_a_quarter_and_moves() { } } +/// #3223, #2872: a STEP assembly whose part sits two levels down, each level placed, opened and +/// stood up. With the imported root active, as Open leaves it, a circle sketched on the part's +/// top face cuts it through, reads as a Cut dragged in and a Join dragged out, and joins it in +/// one piece instead of starting a body of the root's own. +#[test] +fn a_stood_up_nested_step_part_is_cut_and_joined_from_its_face() { + let dir = std::env::temp_dir().join(format!("solidcraft-nested-stood-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let path = dir.join("nested.step"); + let mut src = Session::default(); + let (_, carrier) = created(&mut src, "Carrier"); + let (part, nested) = created(&mut src, "Nested Part"); + run(&mut src, "solid.box", json!({"length": 40, "width": 24, "height": 10, "body_name": "NestedBlock"})); + run(&mut src, "occurrence.move", json!({"occurrence": carrier, "translate": [12, 7, 5], "axis": [0, 0, 1], "angle": "30 deg"})); + run(&mut src, "occurrence.move", json!({"occurrence": nested, "translate": [3, 11, 2], "axis": [0, 1, 0], "angle": "20 deg"})); + // The middle of the top face, in the file's frame: the imported root's. + let top = solidcraft_doc::apply_point(&src.doc.component_transform(part), solidcraft_geom::Vec3::new(20.0, 12.0, 10.0)); + run(&mut src, "file.export", json!({"path": path, "format": "step"})); + + let mut s = Session::default(); + run(&mut s, "doc.open", json!({"path": path, "up": "y"})); + let _ = std::fs::remove_dir_all(&dir); + let at = solidcraft_doc::apply_point(&s.doc.component_transform(s.active_component), top); + run(&mut s, "sketch.create", json!({"plane": {"face": [at.x, at.y, at.z]}, "project_edges": false})); + let centre = s + .doc + .features + .iter() + .rev() + .find_map(|f| match &f.kind { + solidcraft_doc::FeatureKind::Sketch { plane: solidcraft_doc::PlaneRef::Face { plane, .. }, .. } => Some(plane.to_local(top)), + _ => None, + }) + .unwrap(); + run(&mut s, "sketch.circle.center", json!({"center": [centre.x, centre.y], "radius": 3})); + run(&mut s, "sketch.finish", json!({})); + assert_eq!(crate::auto_operation(&s, &json!({"distance": -5})), Some("cut")); + assert_eq!(crate::auto_operation(&s, &json!({"distance": 5})), Some("join")); + let volume = |s: &mut Session| run(s, "inspect.measure", json!({"bodies": ["NestedBlock"]}))["total"]["volume_mm3"].as_f64().unwrap(); + let block = 40.0 * 24.0 * 10.0; + let hole = std::f64::consts::PI * 3.0 * 3.0; + + // The circle is measured as faceted: within a thousandth of what it removes or adds. + for operation in ["cut", "auto"] { + run(&mut s, "solid.extrude", json!({"distance": -20, "operation": operation})); + assert!((block - volume(&mut s) - hole * 10.0).abs() < 1e-3 * hole * 10.0, "{operation}: {}", volume(&mut s)); + run(&mut s, "edit.undo", json!({})); + } + run(&mut s, "solid.extrude", json!({"distance": 5, "operation": "join"})); + let st = s.model.state(); + assert_eq!(st.bodies.iter().map(|b| b.name.as_str()).collect::>(), ["NestedBlock"]); + assert_eq!(st.body("NestedBlock").map(|b| b.body.shell_count()), Some(1)); + assert!((volume(&mut s) - block - hole * 5.0).abs() < 1e-3 * hole * 5.0, "join: {}", volume(&mut s)); +} + /// A rotation that is one only up to rounding is orthonormalised; a scale, a shear or a mirror /// is refused with the reason, and the occurrence stays where it was. #[test]