SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Sheet metal: a pattern of a cut made in the flat is kept like the cut (#6841) (#6853)
NOWN-DEVfa961eaa31a5
diff --git a/crates/doc/src/eval.rs b/crates/doc/src/eval.rsindex ee5a46b5f0..cac9943ade 100644--- a/crates/doc/src/eval.rs+++ b/crates/doc/src/eval.rs@@ -6421,13 +6421,38 @@ fn replay(}}if op == Operation::Cut {- for instance in instances {- if let Some(tools) = copies.get(instance) {+ for instance in &instances {+ if let Some(tools) = copies.get(instance.clone()) {total += 1;missed += usize::from(copy_misses(st, f, &targets, tools));}}}+ // Copies of a cut through sheet metal are cut-outs of its flat pattern, as the cut is+ // (#6841): each machined into the sheets in turn, then cut from the other targets.+ if op == Operation::Cut && matches!(src.kind, FeatureKind::Extrude { .. }) && !st.sheets.is_empty() && instances.len() == mats.len() {+ let mut handled = Vec::with_capacity(mats.len());+ for (m, instance) in mats.iter().zip(&instances) {+ let bodies: Vec<Body> = copies.get(instance.clone()).unwrap_or_default().iter().map(|t| t.body.clone()).collect();+ handled.push(more::sheet_cut_copy(f, src, m, st, &bodies, &targets, warning)?);+ }+ if handled.iter().any(|h| !h.is_empty()) {+ // Copies the same sheets took are cut from the rest at once.+ let mut groups: Vec<(Vec<String>, Vec<Traced>)> = Vec::new();+ let mut rest = copies.into_iter();+ for (done, instance) in handled.into_iter().zip(&instances) {+ let tools: Vec<Traced> = rest.by_ref().take(instance.len()).collect();+ match groups.iter_mut().find(|(d, _)| *d == done) {+ Some((_, all)) => all.extend(tools),+ None => groups.push((done, tools)),+ }+ }+ for (done, tools) in groups {+ apply_beside_sheet_cut(doc, st, f, tools, op, &targets, &done)?;+ }+ continue;+ }+ }// New bodies from a pattern join their source when they touch it (as one feature would).if f.body_names.is_empty() {apply_traced(doc, st, f, copies, op, &targets)?;diff --git a/crates/doc/src/eval_more.rs b/crates/doc/src/eval_more.rsindex 12b84ca81a..6e3748490b 100644--- a/crates/doc/src/eval_more.rs+++ b/crates/doc/src/eval_more.rs@@ -933,13 +933,61 @@ pub(super) fn sheet_cut(constructor: Option<&ExtrudeConstructor>,warning: &mut Option<String>,) -> Result<Vec<String>> {- let FeatureKind::Extrude { sketch, profiles, operation, extent, .. } = &f.kind else { return Ok(Vec::new()) };- if extent.taper.is_some() || !any_eligible_sheet(f, st, targets) {+ if !any_eligible_sheet(f, st, targets) {return Ok(Vec::new());}- let Some(ss) = st.sketch(sketch.0).cloned() else { return Ok(Vec::new()) };- let regions = solidcraft_sketch::merge_regions(&select_profiles(&ss, profiles)?);- // The outlines of the sketch's text: the sheet remembers where its letters went.+ let Some(cut) = extrude_cut(st, f)? else { return Ok(Vec::new()) };+ sheet_prism_cut(f, st, &cut.plane, &cut.regions, cut.operation, tools, targets, constructor, cut.bound, &cut.text, warning)+}++/// A copy of the extrude cut `src` that the pattern `f` makes, moved by `m`: machined into the+/// sheets as the cut itself is, in the moved sketch plane (#6841). `tools` are the copy's. Only+/// for a cut moved without turning over: a mirrored copy, and any other feature, comes back+/// unhandled (no sheets) for the caller to apply as before.+pub(super) fn sheet_cut_copy(+ f: &Feature,+ src: &Feature,+ m: &Mat,+ st: &mut ModelState,+ tools: &[Body],+ targets: &[String],+ warning: &mut Option<String>,+) -> Result<Vec<String>> {+ if !any_eligible_sheet(f, st, targets) {+ return Ok(Vec::new());+ }+ let Some(cut) = extrude_cut(st, src)? else { return Ok(Vec::new()) };+ if cut.operation != Operation::Cut {+ return Ok(Vec::new());+ }+ let (x, y) = (crate::apply_vector(m, cut.plane.x), crate::apply_vector(m, cut.plane.y));+ if x.cross(y).dot(crate::apply_vector(m, cut.plane.normal())) <= 0.0 {+ return Ok(Vec::new());+ }+ let Some(plane) = Plane::new(apply_point(m, cut.plane.origin), x, y) else { return Ok(Vec::new()) };+ sheet_prism_cut(f, st, &plane, &cut.regions, cut.operation, tools, targets, None, cut.bound, &cut.text, warning)+}++/// What an untapered extrude cut (or intersect) machines into a sheet.+struct ExtrudeCut {+ plane: Plane,+ regions: Vec<Region2>,+ /// The outlines of the sketch's text: the sheet remembers where its letters went.+ text: Vec<Seg2>,+ operation: Operation,+ /// Up to a face: the tools' own footprints bound it.+ bound: bool,+}++/// The sketch plane, regions and text of `f`'s extrude; None for any other feature, a tapered+/// extrude or a missing sketch.+fn extrude_cut(st: &ModelState, f: &Feature) -> Result<Option<ExtrudeCut>> {+ let FeatureKind::Extrude { sketch, profiles, operation, extent, .. } = &f.kind else { return Ok(None) };+ if extent.taper.is_some() {+ return Ok(None);+ }+ let Some(ss) = st.sketch(sketch.0) else { return Ok(None) };+ let regions = solidcraft_sketch::merge_regions(&select_profiles(ss, profiles)?);let text: Vec<Seg2> = ss.sketch.links@@ -948,7 +996,7 @@ pub(super) fn sheet_cut(.flat_map(|l| ss.sketch.link_curves(&l.id)).flat_map(|ci| ss.sketch.exact_segs(ci).unwrap_or_else(|| ss.sketch.segs(ci))).collect();- sheet_prism_cut(f, st, &ss.plane, ®ions, *operation, tools, targets, constructor, extent.to.is_some(), &text, warning)+ Ok(Some(ExtrudeCut { plane: ss.plane, regions, text, operation: *operation, bound: extent.to.is_some() }))}/// Whether a cut of `f` with these targets (else its participants, else its component'sdiff --git a/crates/engine/src/cmd/sheet_tests.rs b/crates/engine/src/cmd/sheet_tests.rsindex 295d5040c9..b7c2ce3f76 100644--- a/crates/engine/src/cmd/sheet_tests.rs+++ b/crates/engine/src/cmd/sheet_tests.rs@@ -787,6 +787,58 @@ fn unfold_cut_refold_and_convert() {assert!(t.execute("sheet.convert", &json!({"body": "Block", "face": [50, 30, 2.5]})).is_err());}+/// A pattern of a cut made in the flat is a set of cuts in the flat, kept like the cut itself: in+/// the flat pattern and refolded, its count following a parameter. A circular pattern too, and a+/// pattern of a cut through a folded sheet. They were refused ("this sheet machining cannot be+/// preserved in its flat material").+#[test]+fn a_pattern_of_a_cut_in_the_flat_is_kept_like_the_cut() {+ let cutouts = |s: &mut Session| flat(s)["cutouts"].as_array().map(Vec::len);+ let slot = |s: &mut Session| {+ rectangle_tool(s, "Slot", [10.0, 10.0], [14.0, 30.0]);+ run(s, "solid.extrude", json!({"sketch": "Slot", "through_all": true, "operation": "cut", "direction": "symmetric", "name": "Slot_Cut"}));+ };+ // A 4 x 20 slot through the 2.5 mm sheet.+ let each = 4.0 * 20.0 * 2.5;+ let mut s = Session::default();+ run(&mut s, "parameters.add", json!({"name": "N", "expression": "3", "unit": ""}));+ 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!({}));+ slot(&mut s);+ run(&mut s, "solid.pattern.rectangular", json!({"features": ["Slot_Cut"], "dir1": [1, 0, 0], "count1": "N", "spacing1": 15}));+ assert_eq!(cutouts(&mut s), Some(3));+ run(&mut s, "sheet.refold", json!({}));+ assert!(rel(volume(&mut s), folded - 3.0 * each) < 1e-9, "{} vs {}", volume(&mut s), folded - 3.0 * each);+ assert_eq!(cutouts(&mut s), Some(3));+ run(&mut s, "parameters.change", json!({"name": "N", "expression": "5"}));+ assert_eq!(cutouts(&mut s), Some(5));+ assert!(rel(volume(&mut s), folded - 5.0 * each) < 1e-9, "{} vs {}", volume(&mut s), folded - 5.0 * each);++ // Half a turn about the middle of the base: the copy lands at (66, 20) to (70, 40).+ 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!({}));+ slot(&mut s);+ run(&mut s, "solid.pattern.circular", json!({"features": ["Slot_Cut"], "axis": {"origin": [40, 25, 0], "dir": [0, 0, 1]}, "count": 2}));+ run(&mut s, "sheet.refold", json!({}));+ assert_eq!(cutouts(&mut s), Some(2));+ assert!(rel(volume(&mut s), folded - 2.0 * each) < 1e-9, "{} vs {}", volume(&mut s), folded - 2.0 * each);++ // Through the base of the folded sheet, never unfolded.+ 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);+ slot(&mut s);+ run(&mut s, "solid.pattern.rectangular", json!({"features": ["Slot_Cut"], "dir1": [1, 0, 0], "count1": 4, "spacing1": 15}));+ assert_eq!(cutouts(&mut s), Some(4));+ assert!(rel(volume(&mut s), folded - 4.0 * each) < 1e-9, "{} vs {}", volume(&mut s), folded - 4.0 * each);+}+/// 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