diff --git a/crates/doc/src/eval.rs b/crates/doc/src/eval.rs index 48983133e..ab63bea77 100644 --- a/crates/doc/src/eval.rs +++ b/crates/doc/src/eval.rs @@ -5392,6 +5392,23 @@ fn path_at(segs: &[kernel::PathSeg], d: f64) -> (Vec3, Vec3) { (Vec3::ZERO, Vec3::X) } +/// Whether a copy has no material to cut in any of its eligible targets. +fn copy_misses(st: &ModelState, f: &Feature, targets: &[String], tools: &[Traced]) -> bool { + let targets = if targets.is_empty() { &f.participants } else { targets }; + let all = targets.len() == 1 && targets.first().is_some_and(|t| t == "*"); + st.bodies + .iter() + .filter(|b| all || if targets.is_empty() { b.component == f.component || b.feature == f.id } else { targets.contains(&b.name) }) + .all(|b| { + tools.iter().all(|t| { + // A diagnostic must neither reject an otherwise valid cut nor add its own + // retries to the feature's report. An inconclusive probe is not a miss. + let (intersection, _) = kernel::capture_boolean_diagnostics(|| kernel::boolean(&b.body, &t.body, BoolOp::Intersect)); + matches!(intersection, Ok(None)) + }) + }) +} + /// Apply transformed copies of the tools of `features` (patterns, mirrors). fn replay( doc: &Document, @@ -5415,6 +5432,7 @@ fn replay( format!("{list} suppressed and not copied") }); } + let (mut missed, mut total) = (0usize, 0usize); for name in features.iter().filter(|n| !suppressed.contains(n)) { // A sketch and its extrude often share a name; the feature that makes geometry wins. let src = doc @@ -5462,6 +5480,7 @@ fn replay( let present: Vec = targets.iter().filter(|t| st.bodies.iter().any(|b| &b.name == *t)).cloned().collect(); let targets = if present.is_empty() { targets } else { present }; let mut copies = Vec::new(); + let mut instances = Vec::with_capacity(mats.len()); if let FeatureKind::Hole { diameter, depth, hole, to, .. } = &src.kind { // A hole is drilled again at each copy of its spot (found on the surface there), so // a blind hole's depth counts from the face at the copy, not from the bottom of the @@ -5469,6 +5488,7 @@ fn replay( let (spots, dir) = hole_spots_raw(vals, st, src)?; for (k, m) in mats.iter().enumerate() { let d = crate::apply_vector(m, dir).normalized().ok_or_else(|| DocError::Invalid("hole direction".into()))?; + let start = copies.len(); for p in &spots { let at = hole_on_surface(st, crate::apply_point(m, *p), d, hole_scope(src)); let to = to.map(|t| crate::apply_point(m, t.0)); @@ -5480,6 +5500,7 @@ fn replay( .map(|t| Traced { lineage: t.lineage.map(|l| l.copied(src.id, k + 1)), body: t.body }), ); } + instances.push(start..copies.len()); } } else if let FeatureKind::Rib { .. } = &src.kind { // A rib is built again at each copy of its sketch, against the bodies there (#4593): @@ -5495,9 +5516,19 @@ fn replay( // Each copy's faces are named as the source feature's, with the instance. let tools = feature_tools_with_constructor(doc, vals, src, st, &mut None)?; for (k, m) in mats.iter().enumerate() { + let start = copies.len(); for t in &tools { copies.push(t.copied(kernel::transform_matrix(&t.body, *m)?, src.id, k + 1)); } + instances.push(start..copies.len()); + } + } + if op == Operation::Cut { + for instance in instances { + if let Some(tools) = copies.get(instance) { + total += 1; + missed += usize::from(copy_misses(st, f, &targets, tools)); + } } } // New bodies from a pattern join their source when they touch it (as one feature would). @@ -5510,6 +5541,9 @@ fn replay( apply_traced(st, &named, copies, op, &targets)?; } } + if missed > 0 { + append_warning(warning, format!("{missed} of {total} cut copies remove no material")); + } Ok(()) } diff --git a/crates/engine/src/cmd/patterns_tests.rs b/crates/engine/src/cmd/patterns_tests.rs index e12284342..12802150d 100644 --- a/crates/engine/src/cmd/patterns_tests.rs +++ b/crates/engine/src/cmd/patterns_tests.rs @@ -41,6 +41,65 @@ fn feature_pattern_plate() -> Session { s } +#[test] +fn rectangular_hole_pattern_warns_for_copies_that_miss_and_clears_after_edit() { + let mut s = Session::default(); + run(&mut s, "solid.box", json!({"length":40,"width":30,"height":10,"corner":[0,0,0],"body_name":"Plate"})); + run(&mut s, "solid.hole", json!({"position":[10,15,10],"diameter":5,"depth":10,"name":"Hole1"})); + let before = volume(&mut s); + let reply = run( + &mut s, + "solid.pattern.rectangular", + json!({"features":["Hole1"],"dir1":[1,0,0],"count1":2,"spacing1":45, + "dir2":[0,0,1],"count2":2,"spacing2":5,"name":"Pattern"}), + ); + let id = reply["feature"].as_u64().unwrap(); + let check = |session: &Session| { + let result = session.model.result(id).unwrap(); + assert!(result.error.is_none(), "{result:?}"); + assert_eq!(result.warning.as_deref(), Some("3 of 3 cut copies remove no material")); + }; + check(&s); + assert_eq!(reply["warning"], "3 of 3 cut copies remove no material"); + // The remaining copy lands in the existing through hole, so the plate stays intact. + assert!(rel(volume(&mut s), before) < 1e-8); + let reopened = Session::new(solidcraft_doc::Document::from_json(&s.doc.to_json()).unwrap()); + check(&reopened); + run( + &mut s, + "timeline.redefine", + json!({"feature":"Pattern","command":"solid.pattern.rectangular","params":{ + "features":["Hole1"],"dir1":[1,0,0],"count1":2,"spacing1":45, + "dir2":[0,1,0],"count2":2,"spacing2":8}}), + ); + assert_eq!(s.model.result(id).unwrap().warning.as_deref(), Some("2 of 3 cut copies remove no material")); + assert!(rel(volume(&mut s), 12000.0 - 2.0 * PI * 2.5_f64.powi(2) * 10.0) < 1e-3); + run( + &mut s, + "timeline.redefine", + json!({"feature":"Pattern","command":"solid.pattern.rectangular","params":{ + "features":["Hole1"],"dir1":[1,0,0],"count1":2,"spacing1":15, + "dir2":[0,1,0],"count2":2,"spacing2":8}}), + ); + assert!(s.model.result(id).unwrap().warning.is_none()); + assert!(rel(volume(&mut s), 12000.0 - 4.0 * PI * 2.5_f64.powi(2) * 10.0) < 1e-3); +} + +#[test] +fn rectangular_cut_pattern_counts_only_target_contacts_and_keeps_landing_copies() { + let mut s = Session::default(); + run(&mut s, "solid.box", json!({"length":40,"width":30,"height":10,"corner":[0,0,0],"body_name":"Plate"})); + run(&mut s, "solid.box", json!({"length":40,"width":30,"height":10,"corner":[45,0,0],"operation":"new","body_name":"Neighbor"})); + run(&mut s, "solid.cylinder", json!({"radius":2,"height":20,"base":[10,15,-5],"operation":"cut","targets":["Plate"],"name":"Cut"})); + let reply = run(&mut s, "solid.pattern.rectangular", json!({"features":["Cut"],"dir1":[1,0,0],"count1":4,"spacing1":20})); + assert_eq!(reply["warning"], "2 of 3 cut copies remove no material"); + let state = s.world_state(); + let plate = &state.body("Plate").unwrap().body; + assert!(!plate.contains(solidcraft_geom::Vec3::new(30., 15., 5.))); + assert!(state.body("Neighbor").unwrap().body.contains(solidcraft_geom::Vec3::new(50., 15., 5.))); + assert!(rel(volume(&mut s), 24000.0 - 2.0 * PI * 4.0 * 10.0) < 1e-3); +} + #[test] fn feature_rectangular_center_flip_and_oblique_direction_keep_source_and_recompute() { use solidcraft_geom::Vec3;