diff --git a/crates/engine/src/cmd/features.rs b/crates/engine/src/cmd/features.rs index 4da50e1bd..78c9a6233 100644 --- a/crates/engine/src/cmd/features.rs +++ b/crates/engine/src/cmd/features.rs @@ -828,6 +828,9 @@ fn extrude(s: &mut Session, p: &Value) -> Result { } else if let Some(parts) = cut_scope(s, &p, operation, sketch, face_body)? { p.to_mut()["participants"] = json!(parts); } + if matches!(operation, Operation::Cut | Operation::Intersect) && targets.is_empty() && string_list(&p, "participants").is_empty() { + targets = shown_scope(s); + } let kind = FeatureKind::Extrude { sketch: SketchId(sketch), profiles: match face_profile { @@ -972,6 +975,17 @@ fn face_body_reached(s: &Session, sketch: u64) -> Result> { Ok(Some(body.name.clone())) } +/// A cut or intersect that names no bodies leaves hidden bodies alone, as hiding a body to keep +/// it out of a cut is the usual way to (#3961): while some of the active component's bodies +/// are hidden, its shown ones, stored as the feature's targets so showing the others again +/// later changes nothing. Empty (every body, as before) when none is hidden, or all are. +fn shown_scope(s: &Session) -> Vec { + let st = s.model.state(); + let scope = active_scope(s, &st); + let shown: Vec = scope.iter().filter(|b| s.body_visible(&b.name)).map(|b| b.name.clone()).collect(); + if shown.len() == scope.len() { Vec::new() } else { shown } +} + /// The body whose face `sketch` is attached to (on the face itself, not offset from it), when /// it is one the active component works on ([`active_scope`]). fn sketch_face_body(s: &Session, sketch: u64) -> Option { diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 8ac9740c4..dea0aaeba 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -4464,3 +4464,32 @@ fn parallel_pipe_large_nominals_support_shallow_and_thin_cylindrical_holes() { } } } + +/// #3961: a cut made while a body is hidden leaves that body alone, and keeps doing so once it +/// is shown again (the shown bodies are stored as the cut's targets); with nothing hidden, a +/// cut still goes through every body it meets. +#[test] +fn a_cut_leaves_hidden_bodies_alone() { + let setup = |hide: bool| { + let mut s = Session::default(); + run(&mut s, "solid.box", json!({"length": 20, "width": 20, "height": 10, "body_name": "Below"})); + run(&mut s, "solid.box", json!({"length": 20, "width": 20, "height": 10, "corner": [0, 0, 10], "body_name": "Above", "operation": "new"})); + if hide { + run(&mut s, "browser.visibility", json!({"items": ["Below"], "visible": false})); + } + run(&mut s, "sketch.create", json!({"plane": {"face": [10, 10, 20]}})); + run(&mut s, "sketch.circle.center", json!({"center": [10, 10], "radius": 3})); + run(&mut s, "sketch.finish", json!({})); + run(&mut s, "solid.extrude", json!({"distance": 30, "direction": "negative", "operation": "cut"})); + s + }; + let vol = |s: &Session, name: &str| solidcraft_kernel::measure(&s.world_state().body(name).unwrap().body).unwrap().volume; + let full = 20.0 * 20.0 * 10.0; + let mut hidden = setup(true); + assert!((vol(&hidden, "Below") - full).abs() < 1e-6, "the hidden body is not cut"); + assert!(vol(&hidden, "Above") < full - 1.0, "the shown one is"); + run(&mut hidden, "browser.visibility", json!({"items": ["Below"], "visible": true})); + assert!((vol(&hidden, "Below") - full).abs() < 1e-6, "shown again, it stays as it was"); + let shown = setup(false); + assert!(vol(&shown, "Below") < full - 1.0 && vol(&shown, "Above") < full - 1.0, "with nothing hidden, both are cut"); +}