SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Sheet metal: an edge flange on part of a stepped outline spans that part (#11034) (#11046)
Christian Glombek39163148d9d7
diff --git a/crates/doc/src/sheet.rs b/crates/doc/src/sheet.rsindex 4d27268e69..074a2425d5 100644--- a/crates/doc/src/sheet.rs+++ b/crates/doc/src/sheet.rs@@ -2625,6 +2625,31 @@ impl SheetBody {best}+ /// Where the base's outline already runs along the bend line (through `o`, along `d`) beside a+ /// picked edge that spans [ua, ub] along d: the end of the nearest such run before it and the+ /// start of the nearest after it (none: unbounded). Trimmed back to that line, the picked edge+ /// merges with those runs, as beside the steps of a stepped outline; the flange stays off them.+ fn bend_line_runs(&self, o: Vec2, d: Vec2, ua: f64, ub: f64) -> (f64, f64) {+ let n = Vec2::new(-d.y, d.x);+ let pts = self.base.outer.ccw().polyline(1e-3);+ let m = pts.len();+ let (mut lo, mut hi) = (f64::MIN, f64::MAX);+ for i in 0..m {+ let (Some(&p), Some(&q)) = (pts.get(i), pts.get((i + 1) % m)) else { continue };+ if (p - o).dot(n).abs() > 1e-6 || (q - o).dot(n).abs() > 1e-6 {+ continue;+ }+ let (u0, u1) = (p.dot(d).min(q.dot(d)), p.dot(d).max(q.dot(d)));+ if u1 <= ua + 1e-9 {+ lo = lo.max(u1);+ }+ if u0 >= ub - 1e-9 {+ hi = hi.min(u0);+ }+ }+ (lo, hi)+ }+/// What `cuts` (the sheet's cut-outs) leave of a panel's edge a→b: the stretch nearest `pick`/// (a world point), as distances from `a` along the edge; of two as near, the first. `None`/// where they have taken the edge whole.@@ -2968,17 +2993,23 @@ impl SheetBody {let (s0, s1) = self.edge_left(panel, a, b, e.pick, &cuts).ok_or_else(|| DocError::Invalid(format!("{}a cut-out has taken that edge away; pick an edge the sheet still has", on(k))))?;+ // Where the base already runs along the bend line beside this edge (a stepped outline):+ // the flange stays off it, though trimming merges the two.+ let run = match panel {+ None => self.bend_line_runs(a - outward * setback, d, a.dot(d), b.dot(d)),+ Some(_) => (f64::MIN, f64::MAX),+ };let cut = (s0 > 1e-9, s1 < a.dist(b) - 1e-9);let (a, b) = (if cut.0 { a + d * s0 } else { a }, if cut.1 { a + d * s1 } else { b });- found.push((panel, a, b, d, top, setback, leg, e.angle, cut));+ found.push((panel, a, b, d, top, setback, leg, e.angle, cut, run));}- for (panel, a, b, _, _, setback, _, _, cut) in &found {+ for (panel, a, b, _, _, setback, _, _, cut, _) in &found {self.trim(*panel, *a, *b, *setback, *cut)?;}let mut made = Vec::new();// Whether a cut-out has freed each new bend's ends (start, end) of the base beside them.let mut freed = Vec::new();- for (panel, a, b, d, top, setback, leg, angle, cut) in found {+ for (panel, a, b, d, top, setback, leg, angle, cut, run) in found {let outward = Vec2::new(d.y, -d.x);let line_o = a - outward * setback;// The trimmed edge: along d, on the moved line.@@ -3027,6 +3058,16 @@ impl SheetBody {.ok_or_else(|| DocError::Invalid("the flange's edge was trimmed away".into()))?}};+ // Not over the base's own edge where it already ran along the bend line (beyond a+ // step of the outline): the flange stops where the picked edge did.+ let (start, len) = {+ let u = start.dot(d);+ let (u0, u1) = (u.max(run.0), (u + len).min(run.1));+ if !(u1 - u0 > 1e-9) {+ return Err(DocError::Invalid("the flange's edge was trimmed away".into()));+ }+ (start + d * (u0 - u), u1 - u0)+ };// Where a cut-out ends the edge, the flange ends with it.let (start, len) = if cut.0 || cut.1 {let u = (start - a).dot(d);diff --git a/crates/engine/src/cmd/sheet_tests.rs b/crates/engine/src/cmd/sheet_tests.rsindex 7980e7df71..45fa6a3324 100644--- a/crates/engine/src/cmd/sheet_tests.rs+++ b/crates/engine/src/cmd/sheet_tests.rs@@ -6105,3 +6105,50 @@ fn an_unfolded_sheets_flat_face_is_named_after_its_base_at_every_size() {}}}++/// A base flange 1 thick (R 1, K 0.44) from the closed outline `pts` on XY.+fn sheet_from_outline(s: &mut Session, pts: Value) {+ run(s, "sheet.manage_rules", json!({"name": "S1", "thickness": "1", "bend_radius": "1", "k_factor": 0.44, "active": true}));+ run(s, "sketch.create", json!({"plane": "XY", "name": "B"}));+ run(s, "sketch.line", json!({"points": pts, "closed": true}));+ run(s, "sketch.finish", json!({}));+ run(s, "sheet.flange", json!({"sketch": "B"}));+}++/// The bends of the flat pattern as (length, lower y, upper y), sorted by the lower y.+fn bend_spans(s: &mut Session) -> Vec<(f64, f64, f64)> {+ let f = flat(s);+ let mut v: Vec<(f64, f64, f64)> = f["bends"]+ .as_array()+ .unwrap()+ .iter()+ .map(|b| {+ let (y0, y1) = (b["start"][1].as_f64().unwrap(), b["end"][1].as_f64().unwrap());+ (b["length"].as_f64().unwrap(), y0.min(y1), y0.max(y1))+ })+ .collect();+ v.sort_by(|a, b| a.1.total_cmp(&b.1));+ v+}++const STEPPED: [[f64; 2]; 8] =+ [[-106.9, 5.4], [108.9, 5.4], [108.9, 408.9], [-106.9, 408.9], [-106.9, 388.3], [-108.9, 388.3], [-108.9, 10.5], [-106.9, 10.5]];++/// A flange on part of a stepped outline spans that part: on the X −108.9 segment (Y 10.5…388.3)+/// of a base whose edge beyond it is already at the bend line (X −106.9), the bend is 377.8 long+/// and no relief is cut. Before, it ran the face's whole length (403.5).+#[test]+fn a_flange_on_part_of_a_stepped_outline_spans_that_part() {+ let mut s = Session::default();+ sheet_from_outline(&mut s, json!(STEPPED));+ run(&mut s, "sheet.flange", json!({"edges": [[-108.9, 200, 1]], "height": 20}));+ let spans = bend_spans(&mut s);+ assert_eq!(spans.len(), 1);+ let (len, y0, y1) = spans[0];+ assert!((len - 377.8).abs() < 1e-9 && (y0 - 10.5).abs() < 1e-9 && (y1 - 388.3).abs() < 1e-9, "{spans:?}");+ // The base trimmed back to X −106.9 (215.8 × 403.5), and the bend (allowance (π/2)(R + K·t)) and+ // the 18 leg along the 377.8: no relief.+ let allowance = PI / 2.0 * (1.0 + 0.44 * 1.0);+ let expected = 215.8 * 403.5 + 377.8 * (allowance + 18.0);+ assert!(rel(flat_area(&flat(&mut s)), expected) < 1e-9, "{} vs {expected}", flat_area(&flat(&mut s)));+}