SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Wave 402/509: Sheet face names: a triangle takes a flat face's name only where that face faces its way (and 3 more)
jyviko27e5a5ea8d29
diff --git a/crates/doc/src/eval_more.rs b/crates/doc/src/eval_more.rsindex 7368c05da9..536a9f8bba 100644--- a/crates/doc/src/eval_more.rs+++ b/crates/doc/src/eval_more.rs@@ -1422,6 +1422,13 @@ fn sheet_prism_cut(}let (a, b) = nominal_depth.unwrap_or((a, b));let (z0, z1) = (a.min(b).max(0.0), a.max(b).min(sh.t));+ // A depth within a few units in the last place of the numbers it is worked out from+ // is on the face: a flange panel's frame, turned by the cosine and sine of its angle,+ // puts its faces an ulp (2e-16 mm) off the tool's cap they meet. A thinner layer is+ // the tool's own and stays (refused below when under the resolution).+ let ulps =+ 4.0 * f64::EPSILON * (apply_point(&w, Vec3::ZERO).len() + plane.origin.len() + span.0.abs() + span.1.abs() + sh.t) / sign.abs();+ let (z0, z1) = (if z0 <= ulps { 0.0 } else { z0 }, if sh.t - z1 <= ulps { sh.t } else { z1 });// Sub-resolution layers must not be silently erased by endpoint snapping.let depth_resolved = ![z0, sh.t - z1, z1 - z0].into_iter().any(|d| d > 0.0 && d < 1e-6);labels.extend(letters.iter().filter_map(|r| sh.region_to_flat(pi, plane, r).ok()));diff --git a/crates/doc/src/sheet.rs b/crates/doc/src/sheet.rsindex 00c8edff2c..1677c187c7 100644--- a/crates/doc/src/sheet.rs+++ b/crates/doc/src/sheet.rs@@ -1420,11 +1420,22 @@ impl SheetBody {let mut v = Vec::new();for e in segs {let Seg2::Line { a, b } = *e else {- split_edges += 1;- if split_edges > MAX_FLAT_POINTS {- return Err(flat_budget_error());+ // A closed curve (a whole circle is one arc, `Loop2::circle`) in its two+ // exact halves: a sewn edge runs between two vertices, and an arc's wall is+ // the cylinder through its ends and middle.+ let halves = if e.start().dist(e.end()) < LINEAR_TOL {+ let (h0, h1) = e.split_half();+ vec![h0, h1]+ } else {+ vec![*e]+ };+ for h in halves {+ split_edges += 1;+ if split_edges > MAX_FLAT_POINTS {+ return Err(flat_budget_error());+ }+ v.push(h);}- v.push(*e);continue;};let d = b - a;diff --git a/crates/doc/src/sheet_face_names.rs b/crates/doc/src/sheet_face_names.rsindex 3e37736f5f..8d42e849c4 100644--- a/crates/doc/src/sheet_face_names.rs+++ b/crates/doc/src/sheet_face_names.rs@@ -5,7 +5,7 @@//! neighbouring flange), the rim along the bend there, `<flange>:rim.ext0` and `rim.ext1`. A panel is `base` or a flange's tag (`F<feature>.<k>`), so the names//! say what each face is and stay with it when sizes change or other flanges come and go.-use solidcraft_geom::{Vec2, Vec3};+use solidcraft_geom::{Plane, Vec2, Vec3};use super::SheetBody;use crate::{ModelBody, ModelState, apply_point, mat_inverse};@@ -13,6 +13,9 @@ use crate::{ModelBody, ModelState, apply_point, mat_inverse};/// The share of a face's sample points that must agree on a name for the face to take it.const AGREE: f64 = 0.8;+/// The sine of the angle within which a flat face's plane faces along a triangle's normal.+const FACING: f64 = 1e-3;+impl SheetBody {/// The readable name of the face of this sheet a point lies on (sheet coordinates, within/// `tol`, kept below a quarter of the thickness): a flat face's (see@@ -21,6 +24,24 @@ impl SheetBody {self.panel_face_at(p, tol).map(|(name, _)| name).or_else(|| self.bend_face_at(p, tol)).or_else(|| self.ext_rim_at(p, tol))}+ /// [`face_name_at`](Self::face_name_at) for a point on a face whose normal is `normal` (a+ /// mesh triangle's, either way round): a flat face's name is taken only where its plane faces+ /// along `normal`. A rim is as narrow as the sheet is thick, so the centres of its triangles+ /// can lie within `tol` of the panel's top or bottom face beside it.+ fn face_name_facing(&self, p: Vec3, normal: Vec3, tol: f64) -> Option<String> {+ let facing = |plane: &Plane| plane.normal().cross(normal).len() < FACING;+ match self.panel_face_at(p, tol) {+ Some((name, plane)) if facing(&plane) => Some(name),+ Some(_) => self+ .rim_face_at(p, tol)+ .filter(|(_, plane)| facing(plane))+ .map(|(name, _)| name)+ .or_else(|| self.bend_face_at(p, tol))+ .or_else(|| self.ext_rim_at(p, tol)),+ None => self.bend_face_at(p, tol).or_else(|| self.ext_rim_at(p, tol)),+ }+ }+/// The rim of a flange's panel along its bend line, where the panel runs on past the/// bend's start (`<flange>:rim.ext0`) or end (`rim.ext1`).fn ext_rim_at(&self, p: Vec3, tol: f64) -> Option<String> {@@ -106,12 +127,13 @@ pub fn body_sheet_face_names(st: &ModelState, b: &ModelBody) -> Vec<Option<Strinlet mut total = vec![0usize; nf];for (t, f) in m.triangles.iter().zip(&m.tri_face) {let (Some(v), Some(n), Some([a, b2, c])) = (votes.get_mut(*f as usize), total.get_mut(*f as usize), m.tri(t)) else { continue };+ let Some(normal) = (b2 - a).cross(c - a).normalized() else { continue };// At most a few dozen points a face: enough to tell its name, cheap on a fine mesh.if *n >= 48 {continue;}*n += 1;- if let Some(name) = sheet.face_name_at((a + b2 + c) * (1.0 / 3.0), tol) {+ if let Some(name) = sheet.face_name_facing((a + b2 + c) * (1.0 / 3.0), normal, tol) {match v.iter_mut().find(|(x, _)| *x == name) {Some(x) => x.1 += 1,None => v.push((name, 1)),diff --git a/crates/engine/src/cmd/sheet_tests.rs b/crates/engine/src/cmd/sheet_tests.rsindex 8fe8430ba6..330d2a40f0 100644--- a/crates/engine/src/cmd/sheet_tests.rs+++ b/crates/engine/src/cmd/sheet_tests.rs@@ -1844,7 +1844,9 @@ fn a_flange_bent_inside_keeps_the_base_sketchs_arcs_in_the_flat_pattern_and_the_// bottom edge (2 mm, R 1, K 0.5: setback 3 inside, half of it in the middle; BA π, panel// 37). With the bend inside or in the middle the base is cut back through the two lower// arcs: the DXF's outline was about 150 LINEs then, every arc as chords a quarter- // millimetre long, where the bend outside gives 8 LINEs and 4 ARCs.+ // millimetre long, where the bend outside gives 6 LINEs and 4 ARCs: each of the flange's+ // sides is one line through its bend and its panel (their union has no vertex where the+ // two sides meet in line).let h = 8.0 * (1.0 - 0.5f64.sqrt());// ∫ √(64 − z²): the area under an arc of radius 8, from its centre's height on.let under = |z: f64| z / 2.0 * (64.0 - z * z).sqrt() + 32.0 * (z / 8.0).asin();@@ -1869,8 +1871,9 @@ fn a_flange_bent_inside_keeps_the_base_sketchs_arcs_in_the_flat_pattern_and_the_let lines = shapes.iter().filter(|g| matches!(g, solidcraft_io::Geom2::Line(..))).count();let radii: Vec<f64> =shapes.iter().filter_map(|g| if let solidcraft_io::Geom2::Arc { c, a, .. } = g { Some(c.dist(*a)) } else { None }).collect();- // Eight lines of outline and the bend's centre line; the four corners.- assert_eq!((lines, radii.len(), shapes.len()), (9, 4, 13), "{position}");+ // Six lines of outline (the plate's top, left and right; the flange's two sides and its+ // end) and the bend's centre line; the four corners.+ assert_eq!((lines, radii.len(), shapes.len()), (7, 4, 11), "{position}");assert!(radii.iter().all(|r| (r - 8.0).abs() < 1e-9), "{position}: {radii:?}");// The plate less what lies below the bend line, ∫ 184 + 2·√(64 − (8 − y)²) from 0 to the// setback, and the flange along the trimmed edge, which runs from arc to arc.@@ -3152,7 +3155,13 @@ fn hole_beside_two_adjacent_edge_flanges_cuts_and_bends_stay_guarded() {let mut s = Session::default();build(&mut s);let before = volume(&mut s);- assert!((before - 14057.2).abs() < 0.1, "{before}");+ // Folded, the sheet (2.5 mm, R 2.5, K 0.44) holds its flat area times the thickness, less+ // each bend's flat strip (its length times the allowance (R + K·t)·π/2) and plus its sector+ // (R + t/2)·π/2: t·A + t²·(½ − K)·(π/2)·L for the bends' length L.+ let flat_area: f64 = s.model.state().sheets[0].flat_regions().unwrap().iter().map(|r| r.area()).sum();+ let bends: f64 = flat(&mut s)["bends"].as_array().unwrap().iter().map(|b| b["length"].as_f64().unwrap()).sum();+ let want = 2.5 * flat_area + 2.5 * 2.5 * (0.5 - 0.44) * FRAC_PI_2 * bends;+ assert!(rel(before, want) < 1e-9, "{before} vs {want}");if let Err(e) = hole(&mut s, "Hole", [25.0, 20.0]) {panic!("hole beside two flanges: {e}");}