Skip to content

SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.

Sheet metal convert: a bend split by cut-outs across it is one bend (#7530) (#7581)

NOWN-DEV9c93a6c3400a
diff --git a/crates/doc/src/sheet_convert.rs b/crates/doc/src/sheet_convert.rs
index e5eaa83f54..75d3e57353 100644
--- a/crates/doc/src/sheet_convert.rs
+++ b/crates/doc/src/sheet_convert.rs
@@ -419,6 +419,21 @@ fn bend_cylinder(b: &Body, mesh: &Mesh, f: usize, tol: f64) -> Option<Chamfer> {
na.cross(nb).normalized().map(|_| Chamfer { strip: f, a, b: bb })
}
+/// Every piece of bend cylinder `c`'s cylinder (the same axis line and radius) that is a bend
+/// cylinder between the same two faces: the pieces cut-outs across a bend leave of it. `c` too.
+fn bend_pieces(b: &Body, mesh: &Mesh, c: Chamfer, tol: f64) -> Vec<Chamfer> {
+ let Some(FaceSurface::Cylinder { origin, axis, radius }) = face_surface(b, c.strip) else { return vec![c] };
+ let coaxial = |g: usize| match face_surface(b, g) {
+ Some(FaceSurface::Cylinder { origin: o, axis: a, radius: r }) => {
+ (r - radius).abs() <= tol && a.cross(axis).len() <= 1e-6 && ((o - origin) - axis * (o - origin).dot(axis)).len() <= tol
+ }
+ _ => false,
+ };
+ let same = |o: &Chamfer| (o.a, o.b) == (c.a, c.b) || (o.a, o.b) == (c.b, c.a);
+ let pieces = (0..b.face_count()).filter(|g| *g != c.strip && coaxial(*g)).filter_map(|g| bend_cylinder(b, mesh, g, tol)).filter(same);
+ std::iter::once(c).chain(pieces).collect()
+}
+
/// The cylinder of bend `c` on a shell's other skin: the bend cylinder between the faces
/// straight behind `c`'s two sides, so that a bend picked on either skin is that bend on both.
fn other_skin(b: &Body, mesh: &Mesh, c: &Chamfer, tol: f64) -> Option<Chamfer> {
@@ -703,33 +718,91 @@ fn drawn(trimmed: &[Vec2], from: &[Option<usize>], pts: &[Vec2], round: &[Option
if round.iter().all(Option::is_none) {
return Ok(None);
}
- let (m, n) = (trimmed.len(), pts.len());
- let mut segs: Vec<Seg2> = Vec::with_capacity(m);
- for j in 0..m {
- let (Some(&a), Some(&b)) = (trimmed.get(j), trimmed.get((j + 1) % m)) else { continue };
- let arc = from.get(j).copied().flatten().and_then(|i| Some((i, round.get(i).copied().flatten()?)));
- let Some((i, (c, mid))) = arc else {
- segs.push(Seg2::Line { a: snap(a), b: snap(b) });
- continue;
- };
- let (Some(&p), Some(&q)) = (pts.get(i), pts.get((i + 1) % n)) else { continue };
- if a.dist(p) > 1e-6 || b.dist(q) > 1e-6 {
- return err("not supported yet: a round edge of a face that ends at a bend or a cut edge of the sheet");
- }
- let (start, r) = ((p - c).angle(), c.dist(p));
- let half = ((mid - c).angle() - start + PI).rem_euclid(TAU) - PI;
- match segs.last_mut() {
- Some(Seg2::Arc { center, radius, sweep, .. })
- if center.dist(c) <= 1e-9 && (*radius - r).abs() <= 1e-9 && *sweep * half > 0.0 && (*sweep + 2.0 * half).abs() <= PI + 1e-9 =>
+ let mut segs: Vec<Seg2> = Vec::with_capacity(trimmed.len());
+ for j in 0..trimmed.len() {
+ let Some(seg) = drawn_edge(trimmed, from, pts, round, j)? else { continue };
+ match (segs.last_mut(), seg) {
+ (Some(Seg2::Arc { center, radius, sweep, .. }), Seg2::Arc { center: c, radius: r, sweep: s, .. })
+ if center.dist(c) <= 1e-9 && (*radius - r).abs() <= 1e-9 && *sweep * s > 0.0 && (*sweep + s).abs() <= PI + 1e-9 =>
{
- *sweep += 2.0 * half;
+ *sweep += s;
}
- _ => segs.push(Seg2::Arc { center: c, radius: r, start, sweep: 2.0 * half }),
+ _ => segs.push(seg),
}
}
Ok(Some(Loop2 { segs }))
}
+/// A panel's trimmed outline where it meets the panel across bend `g` along more than one edge
+/// (cut-outs across the bend notch it): the edges must lie in one line, running the same way;
+/// they become one edge, from the first one's start to the last one's end, and each notch
+/// between two of them a hole of the sheet (the notch's edges, closed along the line). Returns
+/// the holes, each with the corners where it leaves the line and comes back to it. Nothing
+/// changes where the panel meets `g` along one edge or none.
+fn join_runs(
+ trimmed: &mut Vec<Vec2>,
+ from: &mut Vec<Option<usize>>,
+ across: &[Option<usize>],
+ pts: &[Vec2],
+ round: &[Option<(Vec2, Vec2)>],
+ g: usize,
+ tol: f64,
+) -> Result<Vec<(Region2, Vec2, Vec2)>> {
+ let m = trimmed.len();
+ let along = |j: usize| from.get(j).copied().flatten().and_then(|i| across.get(i).copied().flatten()) == Some(g);
+ let runs: Vec<usize> = (0..m).filter(|j| along(*j)).collect();
+ let (Some(&first), true) = (runs.first(), runs.len() > 1) else { return Ok(Vec::new()) };
+ let corner = |j: usize| trimmed.get(j % m).copied().unwrap_or_default();
+ let (o, Some(d)) = (corner(first), (corner(first + 1) - corner(first)).normalized()) else { return err("a bend edge has no length") };
+ let off = |p: Vec2| (p - o).cross(d).abs();
+ if runs.iter().any(|j| off(corner(*j)) > tol || off(corner(j + 1)) > tol || (corner(j + 1) - corner(*j)).dot(d) <= 0.0) {
+ return err("not supported yet: a face of the sheet that meets a bend along edges not in one line");
+ }
+ let mut order = runs.clone();
+ order.sort_by(|x, y| (corner(*x) - o).dot(d).total_cmp(&(corner(*y) - o).dot(d)));
+ let mut gone = vec![false; m];
+ let mut holes = Vec::new();
+ for pair in order.windows(2) {
+ let &[ja, jb] = pair else { continue };
+ // The notch: forward round the outline from the end of one edge to the start of the next.
+ let mut segs = Vec::new();
+ let mut j = (ja + 1) % m;
+ while j != jb {
+ if along(j) || segs.len() > m {
+ return err("not supported yet: a face of the sheet that meets a bend along edges out of order");
+ }
+ segs.extend(drawn_edge(trimmed, from, pts, round, j)?);
+ gone[j] = true;
+ j = (j + 1) % m;
+ }
+ gone[jb] = true;
+ let (e, s) = (corner(ja + 1), corner(jb));
+ segs.push(Seg2::Line { a: snap(s), b: snap(e) });
+ let lp = Loop2 { segs };
+ holes.push((Region2 { outer: if lp.signed_area() < 0.0 { lp.reversed() } else { lp }, holes: Vec::new() }, e, s));
+ }
+ let keep: Vec<usize> = (0..m).filter(|j| !gone.get(*j).copied().unwrap_or(false)).collect();
+ let (kept, kept_from): (Vec<Vec2>, Vec<Option<usize>>) = keep.iter().map(|j| (corner(*j), from.get(*j).copied().flatten())).unzip();
+ (*trimmed, *from) = (kept, kept_from);
+ Ok(holes)
+}
+
+/// Edge `j` of a panel's trimmed outline as [`drawn`] draws it: a line, or an arc where it was a
+/// round edge of the face.
+fn drawn_edge(trimmed: &[Vec2], from: &[Option<usize>], pts: &[Vec2], round: &[Option<(Vec2, Vec2)>], j: usize) -> Result<Option<Seg2>> {
+ let (m, n) = (trimmed.len(), pts.len());
+ let (Some(&a), Some(&b)) = (trimmed.get(j % m), trimmed.get((j + 1) % m)) else { return Ok(None) };
+ let arc = from.get(j % m).copied().flatten().and_then(|i| Some((i, round.get(i).copied().flatten()?)));
+ let Some((i, (c, mid))) = arc else { return Ok(Some(Seg2::Line { a: snap(a), b: snap(b) })) };
+ let (Some(&p), Some(&q)) = (pts.get(i), pts.get((i + 1) % n)) else { return Ok(None) };
+ if a.dist(p) > 1e-6 || b.dist(q) > 1e-6 {
+ return err("not supported yet: a round edge of a face that ends at a bend or a cut edge of the sheet");
+ }
+ let (start, r) = ((p - c).angle(), c.dist(p));
+ let half = ((mid - c).angle() - start + PI).rem_euclid(TAU) - PI;
+ Ok(Some(Seg2::Arc { center: c, radius: r, start, sweep: 2.0 * half }))
+}
+
/// Names of the edges of `body` at the bend points, to find them again after the body changes.
/// A point on no edge gets none: the conversion then says so, instead of bending the edge that
/// happens to be nearest. A point on a chamfer or a bend cylinder gets none either: it stays that
@@ -779,7 +852,8 @@ pub fn convert_body(
for p in bends {
if let Some(c) = chamfer_at(mb, &mesh, *p, tol, near) {
picks.push((*p, (c.a, c.b)));
- for c in std::iter::once(c).chain(other_skin(b, &mesh, &c, tol)) {
+ // Every piece of the bend, on either skin: cut-outs across it split its cylinder.
+ for c in std::iter::once(c).chain(other_skin(b, &mesh, &c, tol)).flat_map(|c| bend_pieces(b, &mesh, c, tol)) {
if !chamfers.iter().any(|x| x.strip == c.strip) {
chamfers.push(c);
}
@@ -956,7 +1030,20 @@ pub fn convert_body(
}
let first = panel.face.pts.first().copied().unwrap_or_default();
let face_at = |e: DocError| DocError::Invalid(format!("{e} (the face at {})", at(first)));
- let (trimmed, from) = inset(&pts, &by, tol).map_err(face_at)?;
+ let (mut trimmed, mut from) = inset(&pts, &by, tol).map_err(face_at)?;
+ // Where cut-outs across a bend notch the panel, its edges along the bend are one bend
+ // edge and the notches holes of the sheet, as is the bend zone between them (below).
+ let mut gaps: BTreeMap<usize, Vec<(Vec2, Vec2)>> = BTreeMap::new();
+ let mut bent: Vec<usize> =
+ across.iter().flatten().copied().filter(|g| panel.parent == Some(*g) || panels.get(g).is_some_and(|c| c.parent == Some(pf))).collect();
+ bent.sort_unstable();
+ bent.dedup();
+ for g in bent {
+ for (hole, e, s) in join_runs(&mut trimmed, &mut from, &across, &pts, &round, g, tol).map_err(face_at)? {
+ sh.holes.push(hole);
+ gaps.entry(g).or_default().push((e, s));
+ }
+ }
let exact = drawn(&trimmed, &from, &pts, &round).map_err(face_at)?;
match fl {
None => sh.base.outer = exact.unwrap_or_else(|| Loop2::polygon(&trimmed.iter().map(|q| snap(*q)).collect::<Vec<_>>())),
@@ -1072,7 +1159,13 @@ pub fn convert_body(
tag: String::new(),
tear: (false, false),
});
- queue.push((g, Some(sh.flanges.len() - 1)));
+ let i = sh.flanges.len() - 1;
+ let (ba, n) = sh.flanges.get(i).map_or((0.0, Vec2::new(d.y, -d.x)), |f| (sh.allowance(f), f.n));
+ for (e, s) in gaps.get(&g).into_iter().flatten() {
+ let zone = Loop2::polygon(&[snap(*e), snap(*s), snap(*s + n * ba), snap(*e + n * ba)]);
+ sh.holes.push(Region2 { outer: zone.ccw(), holes: Vec::new() });
+ }
+ queue.push((g, Some(i)));
}
}
// The bends on the fixed face finished as an edge flange feature finishes its own
diff --git a/crates/engine/src/cmd/sheet_convert_tests.rs b/crates/engine/src/cmd/sheet_convert_tests.rs
index 7735324aeb..e2b890ee73 100644
--- a/crates/engine/src/cmd/sheet_convert_tests.rs
+++ b/crates/engine/src/cmd/sheet_convert_tests.rs
@@ -1807,6 +1807,12 @@ fn a_wall_on_part_of_an_edge_meets_a_wall_at_a_relieved_corner_and_is_relieved_a
/// axes 45° round from the base. Returns the new design (with the rule), the picks and the
/// design the tray was made in.
fn tray_round_trip(shape: &str, two: bool) -> (Session, Vec<[f64; 3]>, Session) {
+ slotted_tray_round_trip(shape, two, &[])
+}
+
+/// [`tray_round_trip`] with `slots` (x0, y0, x1, y1) cut through the unfolded tray before it is
+/// folded again and written out.
+fn slotted_tray_round_trip(shape: &str, two: bool, slots: &[[f64; 4]]) -> (Session, Vec<[f64; 3]>, Session) {
let (t, r) = (1.0, 1.0);
let rule = json!({"name": "T", "thickness": "1 mm", "k_factor": "0.5", "bend_radius": "1 mm", "gap": "0.6 mm", "corner_relief_shape": shape, "corner_relief": "3 mm", "active": true});
let mut a = Session::default();
@@ -1824,7 +1830,18 @@ fn tray_round_trip(shape: &str, two: bool) -> (Session, Vec<[f64; 3]>, Session)
all.extend(short.as_array().cloned().unwrap_or_default());
run(&mut a, "sheet.flange", json!({"edges": all, "height": 30}));
}
- let path = std::env::temp_dir().join(format!("solidcraft-tray-round-trip-{shape}-{two}-{}.step", std::process::id()));
+ if !slots.is_empty() {
+ run(&mut a, "sheet.unfold", json!({}));
+ for (i, [x0, y0, x1, y1]) in slots.iter().enumerate() {
+ let name = format!("Slot{i}");
+ run(&mut a, "sketch.create", json!({"plane": "XY", "name": name}));
+ run(&mut a, "sketch.rectangle.two_point", json!({"p0": [x0, y0], "p1": [x1, y1]}));
+ run(&mut a, "sketch.finish", json!({}));
+ run(&mut a, "solid.extrude", json!({"sketch": name, "through_all": true, "operation": "cut", "direction": "symmetric"}));
+ }
+ run(&mut a, "sheet.refold", json!({}));
+ }
+ let path = std::env::temp_dir().join(format!("solidcraft-tray-round-trip-{shape}-{two}-{}-{}.step", slots.len(), std::process::id()));
run(&mut a, "file.export", json!({"path": path.to_string_lossy()}));
let mut s = Session::default();
run(&mut s, "file.insert_step", json!({"path": path.to_string_lossy()}));
@@ -1876,3 +1893,37 @@ fn trays_of_its_own_sheet_metal_convert_back() {
}
}
}
+
+/// A bend that slots cross is one bend, however many of its pieces are picked: the pieces of
+/// its cylinder between the slots are all bent, and the slots come back as cut-outs across it.
+#[test]
+fn a_bend_split_by_slots_across_it_converts_as_one_bend() {
+ // Two slots across the long bend at y = 0, one across the short bend at x = 120, from the
+ // base into the walls of the unfolded tray.
+ let slots = [[30.0, -6.0, 40.0, 8.0], [70.0, -6.0, 80.0, 8.0], [112.0, 55.0, 126.0, 65.0]];
+ for shape in ["notch", "tear"] {
+ for every in [false, true] {
+ let what = format!("{shape}, {}", if every { "every piece picked" } else { "one piece picked" });
+ let (mut s, mut picks, mut source) = slotted_tray_round_trip(shape, false, &slots);
+ if every {
+ let (o, d) = (2.0, 2.0 * std::f64::consts::FRAC_1_SQRT_2);
+ picks.extend([[15.0, o - d, o - d], [100.0, o - d, o - d], [120.0 - o + d, 20.0, o - d], [120.0 - o + d, 75.0, o - d]]);
+ }
+ let body = s.model.state().bodies.first().map(|b| b.name.clone()).unwrap_or_default();
+ let out = s.execute("sheet.convert", &json!({"body": body, "face": [60, 45, 0], "bends": picks, "rule": "T"}));
+ assert!(out.is_ok(), "{what}: {}", out.err().map(|e| e.to_string()).unwrap_or_default());
+ let (v, size, (area, bends)) = (volume(&mut source), flat_size(&mut source), flat_area_and_bends(&mut source));
+ let cutouts = flat(&mut source)["cutouts"].as_array().map(Vec::len);
+ assert_eq!(cutouts, Some(3), "{what}");
+ assert!((volume(&mut s) - v).abs() < 1e-6 * v, "{what}: {} vs {v}", volume(&mut s));
+ assert!(same_size(flat_size(&mut s), size, 1e-6), "{what}: {:?} vs {size:?}", flat_size(&mut s));
+ let (got_area, got_bends) = flat_area_and_bends(&mut s);
+ assert!((got_area - area).abs() < 1e-6 * area, "{what}: {got_area} vs {area}");
+ assert!(
+ got_bends.len() == bends.len() && got_bends.iter().zip(&bends).all(|(x, y)| (x - y).abs() < 1e-6),
+ "{what}: {got_bends:?} vs {bends:?}"
+ );
+ assert_eq!(flat(&mut s)["cutouts"].as_array().map(Vec::len), cutouts, "{what}");
+ }
+ }
+}