diff --git a/crates/doc/src/sheet.rs b/crates/doc/src/sheet.rs index 6b4b4e4e3..f46b0e9a1 100644 --- a/crates/doc/src/sheet.rs +++ b/crates/doc/src/sheet.rs @@ -3661,7 +3661,11 @@ impl SheetBody { return Err("the gap would cut a panel away".into()); } } - // The flat pattern must stay one sheet without overlaps. + // The flat pattern must stay one sheet without overlaps: closing must not make the two + // overlap, nor add to what either already overlaps of another flange. A flange can lie + // over another one in the flat away from this corner (the walls on either side of a + // notch's inner corner both unfold into the notch, and a return on one of them lies over + // the other), and the corner is still closed then (#8823). let [new_a, new_b] = new; let pieces = |s: &SheetBody, f: &SheetFlange| { let mut v = vec![corners_of(&s.zone(f))]; @@ -3671,13 +3675,19 @@ impl SheetBody { v }; let mine = [pieces(self, &new_a), pieces(self, &new_b)]; + let was = [pieces(self, &fa), pieces(self, &fb)]; let overlap = |p: &[Vec], q: &[Vec]| p.iter().any(|a| q.iter().any(|b| pieces_overlap(a, b))); + let shared = |p: &[Vec], q: &[Vec]| p.iter().map(|a| q.iter().map(|b| convex_overlap(a, b)).sum::()).sum::(); + let perimeter = + |p: &[Vec]| p.iter().flat_map(|c| (0..c.len()).filter_map(move |i| Some(c.get(i)?.dist(*c.get((i + 1) % c.len())?)))).sum::(); let [mine_a, mine_b] = &mine; let clash = overlap(mine_a, mine_b) || self.flanges.iter().enumerate().any(|(j, g)| { j != k.a && j != k.b && g.shape.is_none() && { let other = pieces(self, g); - mine.iter().any(|p| overlap(p, &other)) + mine.iter().zip(&was).any(|(p, q)| { + overlap(p, &other) && shared(p, &other) > shared(q, &other) + FLAT_SLIVER * perimeter(p).max(perimeter(&other)).max(1.0) + }) } }); if clash { diff --git a/crates/engine/src/cmd/sheet_tests.rs b/crates/engine/src/cmd/sheet_tests.rs index 468fbca09..2c7612ba0 100644 --- a/crates/engine/src/cmd/sheet_tests.rs +++ b/crates/engine/src/cmd/sheet_tests.rs @@ -4932,6 +4932,48 @@ fn returns_follow_the_rules_gap_and_stay_open_with_a_warning_when_it_is_zero() { assert!((lengths[4] - lengths[0]).abs() < 1e-9 && (lengths[0] - (120.0 - 6.0 * 55f64.to_radians().tan())).abs() < 1e-6, "{lengths:?}"); } +#[test] +fn returns_on_a_notched_tray_close_at_the_corner_away_from_the_notch() { + // A 500 x 400 tray (#8823) with a 100 x 100 notch out of one corner: walls 20 high on its six + // edges, then returns 15 high turned in on all but the notch's side on x = 400. In the flat, + // the walls on either side of the notch's inner corner both unfold into the notch, so the + // return on y = 300 lies over the wall on x = 400 there whatever happens at its other end. + // That overlap used to keep the return's corner at (500, 300) open ("closed, the flanges + // would overlap in the flat pattern"), and the two returns ran through each other there. + let mut s = Session::default(); + run( + &mut s, + "sheet.manage_rules", + json!({"name": "AL2", "thickness": "2 mm", "k_factor": 0.5, "bend_radius": "1 mm", "gap": "0.5 mm", "active": true}), + ); + let pts: [(f64, f64); 6] = [(0.0, 0.0), (500.0, 0.0), (500.0, 300.0), (400.0, 300.0), (400.0, 400.0), (0.0, 400.0)]; + run(&mut s, "sketch.create", json!({"plane": "XY", "name": "Base"})); + run(&mut s, "sketch.line", json!({"points": pts.map(|p| [p.0, p.1]), "closed": true})); + run(&mut s, "sketch.finish", json!({})); + run(&mut s, "sheet.flange", json!({"sketch": "Base"})); + // The middle of edge k, `into` in from it (toward the base) and `z` up. + let pick = |k: usize, into: f64, z: f64| { + let (a, b) = (pts[k], pts[(k + 1) % pts.len()]); + let l = (b.0 - a.0).hypot(b.1 - a.1); + json!([(a.0 + b.0) / 2.0 - (b.1 - a.1) / l * into, (a.1 + b.1) / 2.0 + (b.0 - a.0) / l * into, z]) + }; + let walls: Vec = (0..6).map(|k| pick(k, 0.0, 2.0)).collect(); + run(&mut s, "sheet.flange", json!({"edges": walls, "height": 20, "angle": "90 deg"})); + let returns: Vec = [0, 1, 2, 4, 5].iter().map(|&k| pick(k, 2.0, 20.0)).collect(); + run(&mut s, "sheet.flange", json!({"edges": returns, "height": 15, "angle": "90 deg"})); + let warnings: Vec = s.model.results.iter().filter_map(|r| r.warning.clone()).collect(); + assert!(s.model.results.iter().all(|r| r.error.is_none()) && warnings.is_empty(), "{warnings:?}"); + // The returns on x = 500 and y = 300 (z 18 … 20) are mitred on the diagonal through their + // corner, the rule's gap apart: open on it, material on either side of it. + assert!(!contains(&s, [490.0, 290.0, 19.0]), "the returns run through each other at (500, 300)"); + assert!(contains(&s, [490.0, 285.0, 19.0]) && contains(&s, [485.0, 290.0, 19.0])); + // The other corners close as before, and the part is one valid solid. + assert!(!contains(&s, [10.0, 10.0, 19.0]) && !contains(&s, [490.0, 10.0, 19.0]) && !contains(&s, [10.0, 390.0, 19.0])); + let state = s.model.state(); + let body = &state.bodies[0].body; + assert!(body.validity().is_empty() && body.shell_count() == 1); +} + #[test] fn bad_per_edge_input_is_refused_and_plain_edges_stay_plain() { let mut s = Session::default();