From ca9ebb44db4f6a6add1dab943a3b6cefe573ebbe Mon Sep 17 00:00:00 2001 From: raykholo Date: Tue, 20 Oct 2026 12:07:24 +0000 Subject: [PATCH] Sheet metal: refuse a flat pattern that no longer matches the body (#3324) (#3325) * claim: #3324 * Sheet metal: refuse a flat pattern that no longer matches the body A feature that is not sheet metal can change a sheet body's material without its sheet model knowing: a mirror combined into it, an extrude joined onto it, a fillet or chamfer on a corner. The flat pattern, the DXF and the bend table then wrote the sheet as it was before, a part other than the one in the design, with no warning. The evaluator now notes, per sheet, the first such feature: one that changes the body's shape while its sheet model stays the same. That feature warns, and sheet.flat_pattern, sheet.export_dxf (also with per_body) and sheet.export_bend_table refuse with "not supported yet", naming it. A sheet metal feature, or machining the sheet keeps as cut-outs, rebuilds the body from the model: it clears the note and warns that the change is not kept. Moving the body, splitting its faces and cosmetic threads leave the flat pattern as it is. * Sheet metal: a modelled thread is refused in the flat pattern too Only a cosmetic thread leaves a sheet body's material as it is. A modelled thread cuts the thread profile into the hole, which the flat pattern does not have, and a later sheet metal feature dropped it without a word. It now marks the sheet like a fillet does: it warns, the flat pattern, DXF and bend table refuse naming it, and the feature that rebuilds the body says the thread is not kept. --- crates/doc/src/eval.rs | 47 ++++++++++++ crates/doc/src/sheet.rs | 23 ++++++ crates/engine/src/cmd/sheet.rs | 11 ++- crates/engine/src/cmd/sheet_tests.rs | 104 +++++++++++++++++++++++++++ crates/kernel/src/body.rs | 15 ++-- docs/book/src/guide/sheet-metal.md | 8 +++ 6 files changed, 202 insertions(+), 6 deletions(-) diff --git a/crates/doc/src/eval.rs b/crates/doc/src/eval.rs index 82ccad82d..e9832fdb7 100644 --- a/crates/doc/src/eval.rs +++ b/crates/doc/src/eval.rs @@ -1416,6 +1416,7 @@ impl Model { if let Some(note) = boolean_diagnostics.warning() { append_warning(&mut warning, note); } + track_altered_sheets(&state, &mut next, f, &mut warning); } else { boolean_diagnostics = Default::default(); } @@ -1622,6 +1623,52 @@ fn copy_body_name(state: &ModelState, f: &Feature, k: usize, source: &str) -> St unique_body_name(state, base) } +/// Keeps each sheet metal body's flat pattern true to the body (see `SheetBody::altered_by`). +/// A feature that changes a sheet body's material but not its sheet model (a fillet, a join, a +/// mirror combined into it) marks the sheet with its name, and says so. A feature that rebuilds +/// the body from the model (a sheet metal feature, or machining the model keeps as cut-outs, +/// which changes the model) clears the mark, and says that the change it drops is not kept. +/// Features that move whole bodies or only mark their faces (a cosmetic thread, not a modelled +/// one, which cuts material) leave the flat pattern as it was. +fn track_altered_sheets(before: &ModelState, after: &mut ModelState, f: &Feature, warning: &mut Option) { + if matches!( + f.kind, + FeatureKind::Move { .. } + | FeatureKind::Align { .. } + | FeatureKind::Rest { .. } + | FeatureKind::BodyTransfer { .. } + | FeatureKind::SplitFace { .. } + | FeatureKind::Thread { modeled: false, .. } + ) { + return; + } + let ModelState { sheets, bodies, .. } = after; + for sh in sheets.iter_mut() { + let Some(old) = before.sheets.iter().find(|s| s.body == sh.body) else { continue }; + let (Some(was), Some(now)) = (before.body(&sh.body), bodies.iter().find(|b| b.name == sh.body)) else { continue }; + if was.body.same_shape(&now.body) { + continue; + } + if f.kind.is_sheet() || sh != old { + if let Some(lost) = sh.altered_by.take() { + append_warning( + warning, + format!("`{}` is rebuilt from its sheet metal features: what {lost} changed is not kept (not supported yet)", sh.body), + ); + } + } else if sh.altered_by.is_none() { + sh.altered_by = Some(f.name.clone()); + append_warning( + warning, + format!( + "`{}` is sheet metal: its flat pattern, DXF and bend table can't include this change yet and are refused; a later sheet metal feature rebuilds the body without it", + sh.body + ), + ); + } + } +} + /// 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/doc/src/sheet.rs b/crates/doc/src/sheet.rs index af1653bf0..15ba85342 100644 --- a/crates/doc/src/sheet.rs +++ b/crates/doc/src/sheet.rs @@ -513,6 +513,13 @@ pub struct SheetBody { /// The base sketch's lines (id, ends in flat coordinates), naming the base panel's edges. #[serde(default, skip_serializing_if = "Vec::is_empty")] pub base_curves: Vec<(String, Vec2, Vec2)>, + /// The feature (its name) that changed this body's material without changing its sheet + /// model: a fillet or chamfer, a join, a mirror combined into it, any feature that is not + /// sheet metal machining. The flat pattern, its DXF and the bend table no longer describe the + /// body while it is set, so they are refused. Cleared when a sheet metal feature rebuilds the + /// body from the model. Derived from feature history, like `pockets`. + #[serde(skip_serializing_if = "Option::is_none")] + pub altered_by: Option, } /// A straight round bore of one panel (None = the base), in flat coordinates: a cylinder from @@ -849,6 +856,20 @@ struct ReturnCorner { } impl SheetBody { + /// Whether the flat pattern still describes the body: refused, naming the feature, once a + /// feature that is not sheet metal changed the body's material (see `altered_by`). The flat + /// pattern, its DXF and the bend table all check it, so none of them makes a part other than + /// the one in the design. + pub fn flat_matches_body(&self) -> Result<()> { + match &self.altered_by { + None => Ok(()), + Some(feature) => Err(DocError::Invalid(format!( + "not supported yet: the flat pattern of `{}` can't include {feature}, which changed the body after its sheet metal features, so the flat pattern, DXF and bend table would not match the part; remove or suppress {feature}, or make that shape with sheet metal features", + self.body + ))), + } + } + /// Bend allowance of a flange. pub fn allowance(&self, f: &SheetFlange) -> f64 { f.angle.abs() * (f.radius + self.k * self.t) @@ -4343,6 +4364,7 @@ pub fn base_flange(body: &str, rule: &str, rv: &RuleValues, plane: Plane, region relief_size: rv.corner_relief, bend_relief: (rv.relief_width, rv.relief_depth), base_curves: Vec::new(), + altered_by: None, } } @@ -4401,6 +4423,7 @@ pub fn contour_flange(body: &str, rule: &str, rv: &RuleValues, plane: &Plane, pt relief_size: rv.corner_relief, bend_relief: (rv.relief_width, rv.relief_depth), base_curves: Vec::new(), + altered_by: None, }; let (y0, y1) = if ysign > 0.0 { (0.0, width) } else { (-width, 0.0) }; // First segment: the base panel. diff --git a/crates/engine/src/cmd/sheet.rs b/crates/engine/src/cmd/sheet.rs index 58db634ab..9d693d3de 100644 --- a/crates/engine/src/cmd/sheet.rs +++ b/crates/engine/src/cmd/sheet.rs @@ -558,6 +558,7 @@ fn flat_json(sh: &SheetBody) -> Result { fn flat_pattern(s: &mut Session, p: &Value) -> Result { let sh = sheet_of(s, p, "sheet.flat_pattern")?; + sh.flat_matches_body()?; flat_json(&sh) } @@ -818,7 +819,13 @@ fn export_dxf(s: &mut Session, p: &Value) -> Result { return Err(bad(cmd, "there is no sheet metal body")); } // Every file is made before the first is written. - let texts = sheets.iter().map(|sh| flat_dxf(sh, &options)).collect::>>()?; + let texts = sheets + .iter() + .map(|sh| { + sh.flat_matches_body()?; + flat_dxf(sh, &options) + }) + .collect::>>()?; let paths = path.map(|path| body_paths(path, &sheets.iter().map(|sh| sh.body.clone()).collect::>())); let mut files = Vec::new(); for (i, (sh, text)) in sheets.iter().zip(&texts).enumerate() { @@ -833,6 +840,7 @@ fn export_dxf(s: &mut Session, p: &Value) -> Result { return Ok(json!({"files": files})); } let sh = sheet_of(s, p, cmd)?; + sh.flat_matches_body()?; let text = flat_dxf(&sh, &options)?; if let Some(path) = path { solidcraft_io::vfs::write(path, text.as_bytes()).map_err(|e| EngineError::Other(format!("{path}: {e}")))?; @@ -848,6 +856,7 @@ fn export_bend_table(s: &mut Session, p: &Value) -> Result { return Err(bad(cmd, "`format` must be csv or json")); } let sh = sheet_of(s, p, cmd)?; + sh.flat_matches_body()?; let table = flat_json(&sh)?; let text = if format == "json" { serde_json::to_string_pretty(&table).map_err(|e| EngineError::Other(e.to_string()))? diff --git a/crates/engine/src/cmd/sheet_tests.rs b/crates/engine/src/cmd/sheet_tests.rs index 0efb477b5..334e3fd2e 100644 --- a/crates/engine/src/cmd/sheet_tests.rs +++ b/crates/engine/src/cmd/sheet_tests.rs @@ -4755,3 +4755,107 @@ fn a_hole_that_parts_a_flange_from_the_base_names_it() { assert_eq!(out["warning"], json!(last_warning(&s))); assert_eq!(flat_pieces(&s).len(), 2); } + +/// The three exports of a sheet's flat pattern: its JSON, the DXF (of this body and of every +/// body) and the bend table, each as its error if it is refused. +fn flat_exports(s: &mut Session) -> Vec> { + let calls = [ + ("sheet.flat_pattern", json!({})), + ("sheet.export_dxf", json!({})), + ("sheet.export_dxf", json!({"per_body": true})), + ("sheet.export_bend_table", json!({})), + ]; + 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. +#[test] +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); + run(&mut s, "sheet.flange", json!({"edges": [[50, 0, 2.5]], "height": 20})); + let body = s.model.state().sheets[0].body.clone(); + let out = run(&mut s, "solid.mirror", json!({"bodies": [body], "plane": "YZ", "combine": true})); + assert!(out["warning"].as_str().unwrap_or_default().contains("is sheet metal: its flat pattern, DXF and bend table"), "{out}"); + for result in flat_exports(&mut s) { + let error = result.expect_err("a mirrored half the flat pattern lacks"); + assert!(error.contains("not supported yet: the flat pattern of") && error.contains("can't include Mirror1"), "{error}"); + } + + 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"})); + for result in flat_exports(&mut s) { + let error = result.expect_err("a joined tab the flat pattern lacks"); + assert!(error.contains("can't include Extrude1"), "{error}"); + } +} + +/// A fillet on a sheet's corner is refused in the flat pattern until a sheet metal feature +/// rebuilds the body from its sheet features; that feature says the round is not kept, and the +/// flat pattern describes the body again. +#[test] +fn a_fillet_on_a_sheet_corner_is_refused_until_a_sheet_feature_rebuilds_the_body() { + let mut s = Session::default(); + base(&mut s, 100.0, 60.0); + let out = run(&mut s, "solid.fillet", json!({"edges": [[100, 60, 1.25]], "radius": 10})); + assert!(out["warning"].as_str().is_some_and(|w| w.contains("is sheet metal")), "{out}"); + let error = s.execute("sheet.export_dxf", &json!({})).unwrap_err().to_string(); + assert!(error.contains("can't include Fillet1"), "{error}"); + let out = run(&mut s, "sheet.flange", json!({"edges": [[50, 0, 2.5]], "height": 20})); + assert!(out["warning"].as_str().is_some_and(|w| w.contains("what Fillet1 changed is not kept")), "{out}"); + assert!(flat_exports(&mut s).into_iter().all(|r| r.is_ok())); + assert_eq!(s.model.state().sheets[0].altered_by, None); +} + +/// A cosmetic thread in a sheet's hole leaves its flat pattern as it is; a modelled thread cuts +/// material the flat pattern lacks, so it is refused like a fillet until a sheet feature rebuilds +/// the body and says the thread is not kept. +#[test] +fn a_modelled_thread_in_a_sheet_hole_is_refused_but_a_cosmetic_one_is_not() { + let mut s = Session::default(); + base(&mut s, 100.0, 60.0); + run(&mut s, "solid.hole", json!({"position": [30, 30, 2.5], "diameter": 4.2})); + let drilled = volume(&mut s); + run(&mut s, "solid.thread", json!({"face": [32.1, 30, 1.25], "designation": "M5"})); + assert_eq!(last_warning(&s), ""); + assert!(flat_exports(&mut s).into_iter().all(|r| r.is_ok())); + run(&mut s, "solid.thread", json!({"face": [32.1, 30, 1.25], "designation": "M5", "modeled": true})); + assert!(volume(&mut s) < drilled - 0.05, "the modelled thread cuts material"); + assert!(last_warning(&s).contains("is sheet metal"), "{}", last_warning(&s)); + for result in flat_exports(&mut s) { + let error = result.expect_err("a modelled thread the flat pattern lacks"); + assert!(error.contains("can't include Thread2"), "{error}"); + } + let out = run(&mut s, "sheet.flange", json!({"edges": [[50, 0, 2.5]], "height": 20})); + assert!(out["warning"].as_str().is_some_and(|w| w.contains("what Thread2 changed is not kept")), "{out}"); + assert!(flat_exports(&mut s).into_iter().all(|r| r.is_ok())); +} + +/// A sheet built and machined with sheet features (flanges, a hem, a hole and a cut across a +/// bend while unfolded, refolded, then moved) keeps its flat pattern and exports. +#[test] +fn a_sheet_made_of_sheet_features_and_flat_cuts_still_exports_its_flat_pattern() { + let mut s = Session::default(); + base(&mut s, 100.0, 60.0); + run(&mut s, "sheet.flange", json!({"edges": [[50, 0, 2.5], [100, 30, 2.5]], "height": 20})); + run(&mut s, "sheet.hem", json!({"edges": [[0, 30, 2.5]], "length": 8})); + run(&mut s, "solid.hole", json!({"position": [30, 30, 2.5], "diameter": 6})); + run(&mut s, "sheet.unfold", json!({})); + rectangle_tool(&mut s, "Slot", [45.0, -10.0], [55.0, 10.0]); + run(&mut s, "solid.extrude", json!({"sketch": "Slot", "through_all": true, "direction": "symmetric", "operation": "cut"})); + run(&mut s, "sheet.refold", json!({})); + let body = s.model.state().sheets[0].body.clone(); + run(&mut s, "solid.move", json!({"bodies": [body], "translate": [10, 0, 0]})); + let flagged = |w: &str| w.contains("flat pattern, DXF and bend table") || w.contains("is not kept"); + assert!(s.model.results.iter().all(|r| r.error.is_none() && !r.warning.as_deref().is_some_and(flagged)), "{:?}", s.model.results); + assert_eq!(s.model.state().sheets[0].altered_by, None); + for result in flat_exports(&mut s) { + let value = result.expect("a sheet the flat pattern describes"); + assert!(value.is_object(), "{value}"); + } + assert_eq!(flat(&mut s)["cutouts"].as_array().map(Vec::len), Some(2)); +} diff --git a/crates/kernel/src/body.rs b/crates/kernel/src/body.rs index be11a1199..33db86eeb 100644 --- a/crates/kernel/src/body.rs +++ b/crates/kernel/src/body.rs @@ -568,17 +568,22 @@ impl Body { /// operation made it) is not part of it: an operation that leaves a body as it is (a cut by /// a tool clear of it) gives the same body, with that operation's history. pub fn same(&self, other: &Body) -> bool { + self.same_shape(other) + && self.color == other.color + && same_shared(&self.paint, &other.paint) + && same_shared(&self.splits, &other.splits) + && same_shared(&self.face_sources, &other.face_sources) + && self.regularization_protected == other.regularization_protected + } + + /// The very same shape (shared solid or mesh), whatever its colour, paint or face splits. + pub fn same_shape(&self, other: &Body) -> bool { std::sync::Arc::ptr_eq(&self.solid, &other.solid) && match (&self.mesh, &other.mesh) { (Some(a), Some(b)) => std::sync::Arc::ptr_eq(a, b), (None, None) => true, _ => false, } - && self.color == other.color - && same_shared(&self.paint, &other.paint) - && same_shared(&self.splits, &other.splits) - && same_shared(&self.face_sources, &other.face_sources) - && self.regularization_protected == other.regularization_protected } pub(crate) fn new(solid: Solid) -> Result { diff --git a/docs/book/src/guide/sheet-metal.md b/docs/book/src/guide/sheet-metal.md index 813927f0b..8e8f3a4b3 100644 --- a/docs/book/src/guide/sheet-metal.md +++ b/docs/book/src/guide/sheet-metal.md @@ -314,6 +314,14 @@ but a pocket crossing a bend zone cannot yet be refolded. Machining folded bend tapered sheet cuts and other unsupported 3D sheet Cut/Intersect operations report an error before changing the design; they cannot silently restore removed material during a later sheet rebuild. +Other features that change a sheet body's material without the sheet knowing it (a fillet or +chamfer on a corner, an extrude joined onto it, a Mirror or Combine joined into it) are not in +the flat pattern yet. Such a feature warns, and while the body carries its change the flat +pattern, the DXF and the bend table are refused with an error that names it, rather than writing +a part that does not match the body. A later sheet metal feature rebuilds the body from its sheet +features and says that the change is not kept. Moving the body, splitting its faces and cosmetic +threads leave the flat pattern as it is. + A sketch can be drawn on a flat face of the sheet: the base or a flange, on either side. Pick the face (or give `sketch.create` the point `plane: {face: [x, y, z]}`) and the sketch attaches to that panel instead of to a certified source face. It keeps the face's name,