diff --git a/crates/doc/src/eval.rs b/crates/doc/src/eval.rs index 6f5c84e94..3694e234b 100644 --- a/crates/doc/src/eval.rs +++ b/crates/doc/src/eval.rs @@ -1965,6 +1965,7 @@ impl Model { append_warning(&mut warning, note); } track_altered_sheets(&state, &mut next, f, &mut warning); + note_join_apart(&state, &next, f, &mut warning); } else { boolean_diagnostics = Default::default(); } @@ -2343,6 +2344,59 @@ fn track_altered_sheets(before: &ModelState, after: &mut ModelState, f: &Feature } } +/// A Join whose material meets none of its targets still joins it, to the first target as a +/// separate piece (see `apply_traced`): a valid body in several pieces, but rarely what was meant +/// when Join was left on by mistake. Says so when a feature's Join leaves the bodies it changed +/// in more pieces than they had (#4434). Combine joins the bodies it is given, apart or not. +fn note_join_apart(before: &ModelState, after: &ModelState, f: &Feature, warning: &mut Option) { + let operation = match &f.kind { + FeatureKind::Extrude { operation, .. } + | FeatureKind::Revolve { operation, .. } + | FeatureKind::Box { operation, .. } + | FeatureKind::Cylinder { operation, .. } + | FeatureKind::Sphere { operation, .. } + | FeatureKind::Torus { operation, .. } + | FeatureKind::Loft { operation, .. } + | FeatureKind::Sweep { operation, .. } + | FeatureKind::Pipe { operation, .. } + | FeatureKind::Coil { operation, .. } + | FeatureKind::BoundaryFill { operation, .. } + | FeatureKind::Thicken { operation, .. } + | FeatureKind::SurfaceNetwork { operation, .. } => *operation, + _ => return, + }; + if operation != Operation::Join { + return; + } + let pieces = |b: &Body| b.lumps().map_or(1, |l| l.len()); + // Pieces of the bodies the join changed or absorbed before it, and of those it kept after. + let (mut had, mut have) = (0usize, 0usize); + let mut grown: Vec<(&str, usize)> = Vec::new(); + for was in &before.bodies { + match after.body(&was.name) { + Some(now) if now.body.same_shape(&was.body) => {} + Some(now) => { + let (old, new) = (pieces(&was.body), pieces(&now.body)); + had += old; + have += new; + if new > old { + grown.push((now.name.as_str(), new)); + } + } + None => had += pieces(&was.body), + } + } + if have <= had { + return; + } + for (name, n) in grown { + append_warning( + warning, + format!("the joined piece doesn't touch `{name}`, so `{name}` is now in {n} separate pieces (New Body makes it a body of its own)"), + ); + } +} + /// Add tool bodies to the model according to the operation. fn sheet_boolean_unchanged(body: &Body, tools: &[Body], op: Operation) -> Result<()> { if sheet_boolean_changes(body, tools, op)? { diff --git a/crates/engine/src/boolean_regression_tests.rs b/crates/engine/src/boolean_regression_tests.rs index f69fb1dd0..af7a91df3 100644 --- a/crates/engine/src/boolean_regression_tests.rs +++ b/crates/engine/src/boolean_regression_tests.rs @@ -1346,3 +1346,64 @@ fn spherical_shell_cuts_and_joins_a_block_crossing_its_hollow() { assert!(state.bodies.iter().all(|b| b.body.validity().is_empty())); } } + +/// #4434: a Join whose profile stands apart from the body still joins it, as a second piece of +/// `Body1`, and says so; scripts read it from the reply as History shows it. A join that meets +/// the body, one that closes a cavity (a void is not a piece) and a Combine of bodies chosen +/// apart say nothing. +#[test] +fn join_that_meets_no_body_keeps_the_piece_and_says_so() { + let apart = "`Body1` is now in 2 separate pieces"; + let two_squares = || { + let mut s = Session::default(); + s.execute("sketch.create", &json!({"plane": "XY", "name": "Sketch1"})).unwrap(); + s.execute("sketch.rectangle.two_point", &json!({"p0": [0, 0], "p1": [20, 20]})).unwrap(); + s.execute("sketch.rectangle.two_point", &json!({"p0": [40, 0], "p1": [60, 20]})).unwrap(); + s.execute( + "solid.extrude", + &json!({"sketch": "Sketch1", "profiles": [{"point": [10, 10]}], "distance": 10, "operation": "new", "name": "Extrude1"}), + ) + .unwrap(); + s + }; + let mut s = two_squares(); + let join = s + .execute( + "solid.extrude", + &json!({"sketch": "Sketch1", "profiles": [{"point": [50, 10]}], "distance": 10, "operation": "join", "name": "Extrude2"}), + ) + .unwrap(); + assert!(join["warning"].as_str().is_some_and(|w| w.contains(apart)), "{join}"); + assert_eq!(s.model.results.last().and_then(|r| r.warning.as_deref()), join["warning"].as_str()); + assert_eq!(s.model.state().bodies.iter().map(|b| b.name.as_str()).collect::>(), ["Body1"]); + assert!((volume(&s, "Body1") - 8000.0).abs() < 1e-6, "{}", volume(&s, "Body1")); + + // Both profiles at once, one on the body and one apart: joined as one tool, still told. + let mut s = two_squares(); + let both = s + .execute("solid.extrude", &json!({"sketch": "Sketch1", "profiles": [{"point": [10, 10]}, {"point": [50, 10]}], "distance": 5, "start_offset": 10, "operation": "join"})) + .unwrap(); + assert!(both["warning"].as_str().is_some_and(|w| w.contains(apart)), "{both}"); + + // A box joined apart from the body, then one bridging both pieces: only the first is told. + let mut s = two_squares(); + let box_apart = s.execute("solid.box", &json!({"length": 10, "width": 10, "height": 10, "center": [10, 40, 5], "operation": "join"})).unwrap(); + assert!(box_apart["warning"].as_str().is_some_and(|w| w.contains(apart)), "{box_apart}"); + let bridge = s.execute("solid.box", &json!({"length": 10, "width": 30, "height": 10, "center": [10, 25, 5], "operation": "join"})).unwrap(); + assert!(bridge["warning"].is_null(), "{bridge}"); + + // A lid joined on a cup closes a cavity: one piece with a void, nothing to say. + let mut s = Session::default(); + s.execute("solid.box", &json!({"length": 20, "width": 20, "height": 10, "center": [0, 0, 5], "operation": "new"})).unwrap(); + s.execute("solid.box", &json!({"length": 16, "width": 16, "height": 10, "center": [0, 0, 6], "operation": "cut"})).unwrap(); + let lid = s.execute("solid.box", &json!({"length": 20, "width": 20, "height": 2, "center": [0, 0, 11], "operation": "join"})).unwrap(); + assert_eq!(s.model.state().body("Body1").map(|b| b.body.shell_count()), Some(2)); + assert!(lid["warning"].is_null(), "{lid}"); + + // Combine joins the bodies it is given, apart or not. + let mut s = Session::default(); + s.execute("solid.box", &json!({"length": 10, "width": 10, "height": 10, "center": [0, 0, 5], "operation": "new", "body_name": "A"})).unwrap(); + s.execute("solid.box", &json!({"length": 10, "width": 10, "height": 10, "center": [30, 0, 5], "operation": "new", "body_name": "B"})).unwrap(); + let combine = s.execute("solid.combine", &json!({"target": "A", "tools": ["B"], "operation": "join"})).unwrap(); + assert!(combine["warning"].is_null(), "{combine}"); +} diff --git a/docs/book/src/guide/features.md b/docs/book/src/guide/features.md index 2ce1718a6..66f825179 100644 --- a/docs/book/src/guide/features.md +++ b/docs/book/src/guide/features.md @@ -16,7 +16,10 @@ Extrude automatically selects **Join** for additions when the active component a solid, including sideways elbow connections and screw bosses that meet only part of a face. Extruding into existing material selects **Cut**. In an empty component it selects **New Body**; choose **New Body** explicitly to keep an addition separate. Changing the default does not alter -operations already saved in a design. +operations already saved in a design. A Join (of an extrude, revolve, sweep, loft, primitive…) +whose material touches none of its target bodies still joins it, to the first one as a separate +piece, and the feature's warning says so: "`Body1` is now in 2 separate pieces". Combine joins +the bodies it is given without that warning. **Extrude to a picked target** (Preferences › Design, off by default) lets a pick choose the extent. With it on and a profile selected in the solid Extrude dialog, picking a face away from