diff --git a/crates/doc/src/eval.rs b/crates/doc/src/eval.rs index b27409225..f0159c737 100644 --- a/crates/doc/src/eval.rs +++ b/crates/doc/src/eval.rs @@ -5741,7 +5741,9 @@ fn feature_tools_with_constructor( let start = if hi > 0.0 { lo + off } else { hi + off }; tools = trim_to_body(tools, tb, |q: Vec3| ((q - ss.plane.origin).dot(n) - start).abs())?; } - if untrimmed { + // A join's profile grows into the material it joins; its sketch plane and depth as drawn + // still serve a join into sheet metal (`sheet_join`), which takes the sketch's regions. + if untrimmed || (*operation == Operation::Join && curved.is_none()) { *constructor = Some(ExtrudeConstructor { plane: ss.plane, interval: (lo + off, hi + off) }); } // A Join tool's faces outlive the boolean where they stay on the result, so they are @@ -7653,6 +7655,14 @@ fn eval_feature(doc: &Document, vals: &BTreeMap, f: &Feature, st: return apply_beside_sheet_cut(doc, st, f, tools, *operation, &crate::plain(targets), &handled); } } + // A join one sheet thick in the plane of a panel becomes part of that panel. A Join to an + // object (#6424) is left to the generic path below, which joins the object as well. + if *operation == Operation::Join && to_body.is_none() && matches!(f.kind, FeatureKind::Extrude { .. }) && !st.sheets.is_empty() { + let bodies: Vec = tools.iter().map(|t| t.body.clone()).collect(); + if !more::sheet_join(f, st, &bodies, &crate::plain(targets), constructor.as_ref())?.is_empty() { + return Ok(()); + } + } // A Join extrude to an object joins that object too (#6424): the tool and the body // it runs to are one body, not only the body the profile was sketched on. let extra: Vec = if *operation == Operation::Join { to_body.into_iter().collect() } else { Vec::new() }; diff --git a/crates/doc/src/eval_more.rs b/crates/doc/src/eval_more.rs index 29c9fbbb9..788a03f02 100644 --- a/crates/doc/src/eval_more.rs +++ b/crates/doc/src/eval_more.rs @@ -1034,6 +1034,164 @@ fn extrude_cut(st: &ModelState, f: &Feature) -> Result> { Ok(Some(ExtrudeCut { plane: ss.plane, regions, text, operation: *operation, bound: extent.to.is_some() })) } +/// An untapered extrude join one sheet thick in the plane of a panel of sheet metal (the base or +/// a flange's panel, unfolded or folded) that touches that panel and nothing else of the sheet: +/// the material becomes part of the panel's outline, so the flat pattern, its DXF and a refold +/// keep it, and it follows its sketch's parameters as a cut made there does. Cut-outs it covers +/// are filled. Returns the sheets it took; none (and the generic path's warning) for any other +/// join: another depth, out of every panel's plane, reaching into a bend zone or another panel, +/// apart from the sheet, or over a pocket, label, bore or perforation. +pub(super) fn sheet_join( + f: &Feature, + st: &mut ModelState, + tools: &[Body], + targets: &[String], + constructor: Option<&ExtrudeConstructor>, +) -> Result> { + let FeatureKind::Extrude { sketch, profiles, operation: Operation::Join, extent, .. } = &f.kind else { return Ok(Vec::new()) }; + if extent.taper.is_some() || tools.is_empty() || !any_eligible_sheet(f, st, targets) { + return Ok(Vec::new()); + } + let Some(ss) = st.sketch(sketch.0).cloned() else { return Ok(Vec::new()) }; + // The depth the extrude was given, along its sketch's normal: only an untrimmed extrude has one. + let Some(c) = constructor.filter(|c| c.plane == ss.plane) else { return Ok(Vec::new()) }; + let regions = solidcraft_sketch::merge_regions(&select_profiles(&ss, profiles)?); + let plane = ss.plane; + let n = plane.normal(); + let (lo, hi) = (c.interval.0.min(c.interval.1), c.interval.0.max(c.interval.1)); + let roundoff = 128.0 * f64::EPSILON * (plane.origin.len() + lo.abs() + hi.abs()).max(1.0); + let mut work = sheet::FlatWork::new(); + let mut plans = Vec::new(); + for original in &st.sheets { + if !sheet_eligible(f, st, targets, original) { + continue; + } + let Some(sh) = sheet_join_one(original, &plane, n, ®ions, (lo, hi, roundoff), &mut work)? else { return Ok(Vec::new()) }; + let body = sh.solid()?; + plans.push((sh, body)); + } + let handled = plans.iter().map(|(sh, _)| sh.body.clone()).collect(); + for (sh, body) in plans { + if let Some(mb) = st.bodies.iter_mut().find(|b| b.name == sh.body) { + replace_sheet_body(mb, body); + } + if let Some(target) = st.sheets.iter_mut().find(|s| s.body == sh.body) { + *target = sh; + } + } + Ok(handled) +} + +/// [`sheet_join`] on one sheet: the sheet with each of the join's `regions` (in `plane`, the +/// depth `span` along its normal `n`) taken into the one panel it fits, or None where one of them +/// fits none. +fn sheet_join_one( + original: &sheet::SheetBody, + plane: &Plane, + n: Vec3, + regions: &[Region2], + span: (f64, f64, f64), + work: &mut sheet::FlatWork, +) -> Result> { + let (lo, hi, roundoff) = span; + let sh = original; + let region_of = |pi: Option| match pi { + None => Some(sh.base.clone()), + Some(i) => sh.flanges.get(i).map(|f| sh.panel(f)), + }; + let all: Vec> = std::iter::once(None).chain((0..sh.flanges.len()).map(Some)).collect(); + // The panels whose faces are the sheet's thickness apart along the sketch's normal, the join + // filling exactly that slab. + let pieces: Vec> = all + .iter() + .copied() + .filter(|&pi| { + let w = sh.world(pi); + let sign = crate::apply_vector(&w, Vec3::Z).dot(n); + if sign.abs() < 1.0 - 1e-9 { + return false; + } + let zorigin = (apply_point(&w, Vec3::ZERO) - plane.origin).dot(n); + let (a, b) = ((lo - zorigin) / sign, (hi - zorigin) / sign); + let tol = 1e-6 + roundoff; + a.min(b).abs() <= tol && (a.max(b) - sh.t).abs() <= tol + }) + .collect(); + let zones: Vec = sh.flanges.iter().map(|f| sh.zone(f)).collect(); + let touches = |a: &Region2, b: &Region2, work: &mut sheet::FlatWork| -> Result { + Ok(sheet::material_boolean(std::slice::from_ref(a), std::slice::from_ref(b), kernel::BoolOp::Union, work)?.len() == 1) + }; + // Each region joins the one panel it is one piece with, apart from every bend zone and the + // other panels (touching one along an edge, the flat pattern would lose the cut between them). + let mut fits: Vec<(Option, Region2)> = Vec::new(); + for r in regions { + let mut found = None; + for &pi in &pieces { + let Some(piece) = region_of(pi) else { continue }; + let flat = sh.region_to_flat(pi, plane, r)?; + if !touches(&piece, &flat, work)? { + continue; + } + let mut apart = true; + for other in zones.iter().cloned().chain(all.iter().filter(|&&q| q != pi).filter_map(|&q| region_of(q))) { + if touches(&other, &flat, work)? { + apart = false; + break; + } + } + if !apart { + continue; + } + if found.is_some() { + // Two panels in the plane both take it. + return Ok(None); + } + found = Some((pi, flat)); + } + let Some(fit) = found else { return Ok(None) }; + fits.push(fit); + } + // Pockets, labels, bores and perforations keep their own outlines: not under a join yet. + if !sh.bores.is_empty() || !sh.perforations.is_empty() { + return Ok(None); + } + let flats: Vec = fits.iter().map(|(_, r)| r.clone()).collect(); + let covered: f64 = { + let kept: Vec = sh.pockets.iter().map(|p| p.region.clone()).chain(sh.labels.iter().cloned()).collect(); + sheet::material_boolean(&kept, &flats, kernel::BoolOp::Intersect, work)?.iter().map(Region2::area).sum() + }; + if covered > 1e-9 { + return Ok(None); + } + let mut sh = original.clone(); + for &pi in &all { + let mine: Vec = fits.iter().filter(|(q, _)| *q == pi).map(|(_, r)| r.clone()).collect(); + if mine.is_empty() { + continue; + } + let Some(piece) = region_of(pi) else { return Ok(None) }; + let joined = sheet::material_boolean(std::slice::from_ref(&piece), &mine, kernel::BoolOp::Union, work)?; + let [one] = joined.as_slice() else { return Ok(None) }; + match pi { + None => sh.base = one.clone(), + Some(i) => { + if !one.holes.is_empty() { + return Ok(None); + } + let Some(f) = sh.flanges.get_mut(i) else { return Ok(None) }; + f.exact = Some(one.outer.clone()); + } + } + } + // Cut-outs it covers are filled. + let mut holes = Vec::with_capacity(sh.holes.len()); + for h in &sh.holes { + holes.extend(sheet::material_boolean(std::slice::from_ref(h), &flats, kernel::BoolOp::Cut, work)?); + } + sh.holes = holes; + Ok(Some(sh)) +} + /// Whether a cut of `f` with these targets (else its participants, else its component's /// bodies) machines the sheet `original`. fn sheet_eligible(f: &Feature, st: &ModelState, targets: &[String], original: &crate::sheet::SheetBody) -> bool { diff --git a/crates/engine/src/cmd/sheet_tests.rs b/crates/engine/src/cmd/sheet_tests.rs index 45fa6a332..38aa771f1 100644 --- a/crates/engine/src/cmd/sheet_tests.rs +++ b/crates/engine/src/cmd/sheet_tests.rs @@ -839,6 +839,99 @@ fn a_pattern_of_a_cut_in_the_flat_is_kept_like_the_cut() { assert!(rel(volume(&mut s), folded - 4.0 * each) < 1e-9, "{} vs {}", volume(&mut s), folded - 4.0 * each); } +/// Material joined in the flat, one sheet thick in the plane of a panel, becomes part of that +/// panel as a cut made there does: kept in the flat pattern and refolded, its size following a +/// parameter. A tab on the base's free edge and one beside the flange's panel. A join that +/// reaches into the bend zone keeps the warning that the flat pattern can't include it. +#[test] +fn a_join_in_the_flat_is_kept_like_a_cut() { + // The rule's 2.5 mm sheet, R 2.5, K 0.44: the flange on y = 50 sets the base back to y = 45 and + // bends across BA = π/2·(2.5 + 0.44·2.5); its panel runs on from y = 45 + BA, x 0 to 80. + let (t, ba) = (2.5, FRAC_PI_2 * (2.5 + 0.44 * 2.5)); + let tab = |s: &mut Session, name: &str, points: Value| { + run(s, "sketch.create", json!({"plane": "XY", "name": name})); + run(s, "sketch.line", json!({"points": points, "closed": true})); + run(s, "sketch.finish", json!({})); + run(s, "solid.extrude", json!({"sketch": name, "distance": t, "operation": "join", "name": format!("{name}_Join")})) + }; + let mut s = Session::default(); + run(&mut s, "parameters.add", json!({"name": "TabW", "expression": "10 mm"})); + base(&mut s, 80.0, 50.0); + run(&mut s, "sheet.flange", json!({"edges": [[40, 50, 2.5]], "height": 20})); + let folded = volume(&mut s); + run(&mut s, "sheet.unfold", json!({})); + let unfolded = volume(&mut s); + // 15 x TabW on the base's free edge at x = 80. + let out = tab(&mut s, "Base_Tab", json!([["80 mm", "10 mm"], ["95 mm", "10 mm"], ["95 mm", "10 mm + TabW"], ["80 mm", "10 mm + TabW"]])); + assert!(out["warning"].is_null(), "{out}"); + assert!(rel(volume(&mut s), unfolded + 15.0 * 10.0 * t) < 1e-9, "{}", volume(&mut s)); + // 10 x 5 beside the flange's panel, 3 mm past its bend. + let y = 45.0 + ba + 3.0; + let out = tab(&mut s, "Panel_Tab", json!([[80, y], [90, y], [90, y + 5.0], [80, y + 5.0]])); + assert!(out["warning"].is_null(), "{out}"); + let added = |w: f64| 15.0 * w * t + 10.0 * 5.0 * t; + assert!(rel(volume(&mut s), unfolded + added(10.0)) < 1e-9, "{}", volume(&mut s)); + assert!((flat(&mut s)["flat_size_mm"][0].as_f64().unwrap_or(0.0) - 95.0).abs() < 1e-9); + run(&mut s, "sheet.refold", json!({})); + assert!(rel(volume(&mut s), folded + added(10.0)) < 1e-9, "{} vs {}", volume(&mut s), folded + added(10.0)); + run(&mut s, "parameters.change", json!({"name": "TabW", "expression": "20 mm"})); + assert!(rel(volume(&mut s), folded + added(20.0)) < 1e-9, "{} vs {}", volume(&mut s), folded + added(20.0)); + // One join with a tab on each: each joins the panel it is beside. + let mut s = Session::default(); + base(&mut s, 80.0, 50.0); + run(&mut s, "sheet.flange", json!({"edges": [[40, 50, 2.5]], "height": 20})); + let folded = volume(&mut s); + run(&mut s, "sheet.unfold", json!({})); + run(&mut s, "sketch.create", json!({"plane": "XY", "name": "Tabs"})); + run(&mut s, "sketch.line", json!({"points": [[80, 10], [95, 10], [95, 20], [80, 20]], "closed": true})); + run(&mut s, "sketch.line", json!({"points": [[80, y], [90, y], [90, y + 5.0], [80, y + 5.0]], "closed": true})); + run(&mut s, "sketch.finish", json!({})); + let out = run(&mut s, "solid.extrude", json!({"sketch": "Tabs", "distance": t, "operation": "join"})); + assert!(out["warning"].is_null(), "{out}"); + run(&mut s, "sheet.refold", json!({})); + assert!(rel(volume(&mut s), folded + added(10.0)) < 1e-9, "{} vs {}", volume(&mut s), folded + added(10.0)); + // Into the bend zone (it starts at y = 45): the generic path, with its warning. + let mut s = Session::default(); + base(&mut s, 80.0, 50.0); + run(&mut s, "sheet.flange", json!({"edges": [[40, 50, 2.5]], "height": 20})); + run(&mut s, "sheet.unfold", json!({})); + let out = tab(&mut s, "Zone_Tab", json!([[80, 40], [90, 40], [90, 48], [80, 48]])); + assert!(out["warning"].as_str().is_some_and(|w| w.contains("can't include this change")), "{out}"); + // The same two tabs in a 1 mm sheet (R 1.18) and a 3 mm one (R 3), each joined one sheet + // thick: kept, refolded and following the parameter alike. + for (t, r) in [(1.0, 1.18), (3.0, 3.0)] { + let what = format!("T {t} R {r}"); + let ba = FRAC_PI_2 * (r + 0.44 * t); + let mut s = Session::default(); + run(&mut s, "parameters.add", json!({"name": "TabW", "expression": "10 mm"})); + let rule = json!({"name": "Rule", "active": true, "thickness": format!("{t} mm"), "k_factor": "0.44", "bend_radius": format!("{r} mm")}); + run(&mut s, "sheet.manage_rules", rule); + base(&mut s, 80.0, 50.0); + run(&mut s, "sheet.flange", json!({"edges": [[40, 50, t]], "height": 20})); + let folded = volume(&mut s); + run(&mut s, "sheet.unfold", json!({})); + let unfolded = volume(&mut s); + let join = |s: &mut Session, name: &str, points: Value| { + run(s, "sketch.create", json!({"plane": "XY", "name": name})); + run(s, "sketch.line", json!({"points": points, "closed": true})); + run(s, "sketch.finish", json!({})); + run(s, "solid.extrude", json!({"sketch": name, "distance": t, "operation": "join"})) + }; + let out = join(&mut s, "Base_Tab", json!([["80 mm", "10 mm"], ["95 mm", "10 mm"], ["95 mm", "10 mm + TabW"], ["80 mm", "10 mm + TabW"]])); + assert!(out["warning"].is_null(), "{what}: {out}"); + // The panel starts BA past the base, set back R + T from y = 50. + let y = 50.0 - (r + t) + ba + 3.0; + let out = join(&mut s, "Panel_Tab", json!([[80, y], [90, y], [90, y + 5.0], [80, y + 5.0]])); + assert!(out["warning"].is_null(), "{what}: {out}"); + let added = |w: f64| 15.0 * w * t + 10.0 * 5.0 * t; + assert!(rel(volume(&mut s), unfolded + added(10.0)) < 1e-9, "{what}: {}", volume(&mut s)); + run(&mut s, "sheet.refold", json!({})); + assert!(rel(volume(&mut s), folded + added(10.0)) < 1e-9, "{what}: {} vs {}", volume(&mut s), folded + added(10.0)); + run(&mut s, "parameters.change", json!({"name": "TabW", "expression": "20 mm"})); + assert!(rel(volume(&mut s), folded + added(20.0)) < 1e-9, "{what}: {} vs {}", volume(&mut s), folded + added(20.0)); + } +} + /// A plate (15, -20) (0, 0) (-100, 0) (-100, -20) with a 20 mm inside flange on its slanted edge /// (2 mm sheet, R 2, K 0.5: the folded sheet holds its flat area times the thickness), unfolded, /// and a triangle cut out with a corner on the bend line: where the line ends on the plate's @@ -5860,9 +5953,10 @@ fn flat_exports(s: &mut Session) -> Vec> { calls.iter().map(|(id, p)| s.execute(id, p).map_err(|e| e.to_string())).collect() } -/// A mirror combined into a sheet, or a tab extruded onto it and joined, makes a part its -/// flat pattern does not have (the mirrored half, the tab): the flat pattern, the DXF and the -/// bend table are refused with the feature's name instead of writing the sheet without it. +/// A mirror combined into a sheet, or a block extruded onto it and joined, makes a part its +/// flat pattern does not have (the mirrored half, the block): the flat pattern, the DXF and the +/// bend table are refused with the feature's name instead of writing the sheet without it. (A +/// tab joined in the sheet's own plane becomes part of it: `a_join_in_the_flat_is_kept_like_a_cut`.) #[test] fn a_mirror_or_a_join_into_a_sheet_refuses_its_flat_pattern_naming_the_feature() { let mut s = Session::default(); @@ -5878,12 +5972,21 @@ fn a_mirror_or_a_join_into_a_sheet_refuses_its_flat_pattern_naming_the_feature() let mut s = Session::default(); base(&mut s, 100.0, 60.0); - rectangle_tool(&mut s, "Tab", [40.0, 60.0], [60.0, 75.0]); - run(&mut s, "solid.extrude", json!({"sketch": "Tab", "distance": 2.5, "operation": "join"})); + run(&mut s, "sketch.create", json!({"plane": "XY", "offset": 2.5, "name": "Block"})); + run(&mut s, "sketch.rectangle.two_point", json!({"p0": [40, 20], "p1": [60, 35]})); + run(&mut s, "sketch.finish", json!({})); + run(&mut s, "solid.extrude", json!({"sketch": "Block", "distance": 10, "operation": "join"})); for result in flat_exports(&mut s) { - let error = result.expect_err("a joined tab the flat pattern lacks"); + let error = result.expect_err("a joined block the flat pattern lacks"); assert!(error.contains("can't include Extrude1"), "{error}"); } + // The tab beside the base's edge, joined in its plane, is in the flat pattern. + let mut s = Session::default(); + base(&mut s, 100.0, 60.0); + rectangle_tool(&mut s, "Tab", [40.0, 60.0], [60.0, 75.0]); + run(&mut s, "solid.extrude", json!({"sketch": "Tab", "distance": 2.5, "operation": "join"})); + assert!(flat_exports(&mut s).into_iter().all(|r| r.is_ok())); + assert!((flat(&mut s)["flat_size_mm"][1].as_f64().unwrap_or(0.0) - 75.0).abs() < 1e-9); } /// A fillet on a sheet's corner is refused in the flat pattern until a sheet metal feature