SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Offset Face: split a periodic face off its seam first, as booleans already do (#6226) (#6227)
Carlos Keese7734ba23002e
diff --git a/crates/kernel/src/polyhedron.rs b/crates/kernel/src/polyhedron.rsindex 95f1885fa9..22ef37f51b 100644--- a/crates/kernel/src/polyhedron.rs+++ b/crates/kernel/src/polyhedron.rs@@ -835,16 +835,14 @@ fn solve3(m: [[f64; 3]; 3], r: [f64; 3]) -> Option<[f64; 3]> {Some(out)}-/// Move the planar faces at the given points along their outward normals by `distance`-/// (negative: into the body). The body's other faces follow (its topology is kept).-pub fn offset_faces(b: &Body, at: &[Vec3], distance: f64) -> Result<Body> {- b.require_brep("offset faces")?;- if !distance.is_finite() || distance.abs() > 1e6 {- return Err(KernelError::Invalid("offset distance".into()));- }- let healed = crate::boolean_record::healed(b)?.with_split_lines(b.splits.clone());- let size = healed.size();- let mesh = healed.tessellate((size * 1e-3).max(1e-3))?;+/// The faces of `body` the given points pick, each whole (every piece of its surface).+///+/// Taken per body rather than once, because an operation may be tried again on the same body+/// with a face split in two, and the pieces are numbered differently then. The points do not+/// move, so they pick the same surface either way.+fn faces_at(body: &Body, at: &[Vec3]) -> Result<Vec<usize>> {+ let size = body.size();+ let mesh = body.tessellate((size * 1e-3).max(1e-3))?;let mut chosen: Vec<usize> = Vec::new();for p in at {let near = mesh@@ -862,13 +860,39 @@ pub fn offset_faces(b: &Body, at: &[Vec3], distance: f64) -> Result<Body> {_ => return Err(KernelError::Invalid(format!("no face at {:?}", [p.x, p.y, p.z]))),}}- let chosen = crate::offset::with_same_surface(&healed, &chosen);- match offset_planar(&healed, |fi, _| if chosen.contains(&fi) { distance } else { 0.0 }) {- Ok(r) => Ok(record::after(r, &healed, &record::counts(&[b]))),- // A piece of a split face (its neighbour stays): the piece swept along its normal- // joins on (outward) or cuts in, with walls along the split lines.- Err(e) if !healed.split_keep().is_empty() => press_pieces(&healed, &chosen, distance).map_err(|_| e),- Err(e) => Err(e),+ Ok(crate::offset::with_same_surface(body, &chosen))+}++/// Move the planar faces at the given points along their outward normals by `distance`+/// (negative: into the body). The body's other faces follow (its topology is kept).+pub fn offset_faces(b: &Body, at: &[Vec3], distance: f64) -> Result<Body> {+ b.require_brep("offset faces")?;+ if !distance.is_finite() || distance.abs() > 1e6 {+ return Err(KernelError::Invalid("offset distance".into()));+ }+ // Healing is part of the attempt, not a step before it: a face that runs along a seam edge+ // twice fails there, and the retry below needs the whole of it to run on the split body.+ let run = |body: &Body| -> Result<Body> {+ let healed = crate::boolean_record::healed(body)?.with_split_lines(body.splits.clone());+ let chosen = faces_at(&healed, at)?;+ match offset_planar(&healed, |fi, _| if chosen.contains(&fi) { distance } else { 0.0 }) {+ Ok(r) => Ok(record::after(r, &healed, &record::counts(&[body]))),+ // A piece of a split face (its neighbour stays): the piece swept along its normal+ // joins on (outward) or cuts in, with walls along the split lines.+ Err(e) if !healed.split_keep().is_empty() => press_pieces(&healed, &chosen, distance).map_err(|_| e),+ Err(e) => Err(e),+ }+ };+ match crate::guard("offset faces", || run(b)) {+ Ok(r) => Ok(r),+ // A face that runs along a seam edge twice — a hole's wall as a STEP file writes it, one+ // whole turn bounded by one wire. Healing it, or moving its neighbour, fails where the+ // same body with that face in two halves works, so split it and ask again, which is what+ // a boolean already does (`seam_faces`).+ Err(e) => match crate::seam_faces::one_body(b, "offset faces off their seams", &run) {+ Some(Ok(r)) => Ok(r),+ _ => Err(e),+ },}}diff --git a/crates/kernel/src/seam_faces.rs b/crates/kernel/src/seam_faces.rsindex e689483810..1aa38212ec 100644--- a/crates/kernel/src/seam_faces.rs+++ b/crates/kernel/src/seam_faces.rs@@ -48,6 +48,11 @@ pub(crate) fn needed(a: &Body, b: &Body) -> bool {!a.is_mesh() && !b.is_mesh() && !SPLITTING.with(|c| c.get()) && (next_split(a).is_some() || next_split(b).is_some())}+/// Whether `body` has a face for [`one_body`] to split.+pub(crate) fn needed_one(body: &Body) -> bool {+ !body.is_mesh() && !SPLITTING.with(|c| c.get()) && next_split(body).is_some()+}+/// `body` with every face that runs along a seam edge twice split in two, and its history over/// `body`. `None` when a split fails.fn halved(body: &Body) -> Option<(Body, Option<History>)> {@@ -86,6 +91,28 @@ pub(crate) fn boolean(a: &Body, b: &Body, op: BoolOp, run: &dyn Fn(&Body, &Body,})))}+/// `run` over `body` with every face that runs along a seam edge twice split in two, for an+/// operation that takes one body. `None` when the body has no such face, a split fails, or the+/// run over the split body fails: the caller then keeps the error it had.+///+/// The same answer as [`boolean`] gives, for the same reason — an operation that has to divide+/// such a face can fail where the body with the face in two halves works — and it matters most+/// to a body read from a STEP file, which is where a whole-turn face comes from. The result's+/// history is composed back over `body` as it was given, so a caller that knows `body`'s faces+/// still recognises them: the split is no authored boundary.+pub(crate) fn one_body(body: &Body, what: &str, run: &dyn Fn(&Body) -> Result<Body>) -> Option<Result<Body>> {+ if !needed_one(body) {+ return None;+ }+ let (split, over) = halved(body)?;+ SPLITTING.with(|c| c.set(true));+ let result = crate::guard(what, || run(&split));+ SPLITTING.with(|c| c.set(false));+ let out = result.ok()?;+ let history = recorded::through(&out, 0, over.as_ref());+ Some(Ok(out.with_checked_history(history, &crate::record::counts(&[body]))))+}+#[cfg(test)]#[path = "seam_faces_tests.rs"]mod tests;diff --git a/crates/kernel/src/seam_faces_tests.rs b/crates/kernel/src/seam_faces_tests.rsindex 9bf32aee5a..e93145fb43 100644--- a/crates/kernel/src/seam_faces_tests.rs+++ b/crates/kernel/src/seam_faces_tests.rs@@ -59,6 +59,34 @@ fn a_pin_read_from_step_has_one_face_along_its_seam() {assert!(rel(volume(&halved), PI * R * R * 40.0) < 1e-3);}+/// Offset Face on a body read from STEP, where the hole's wall is one face along its seam.+///+/// Healing such a body, which Offset Face does before it moves anything, fails inside `truck`+/// ("This wire is not simple"), so the top could not be moved at all — and importing is how a+/// part with a hole usually arrives. The offset now splits the face off its seam and asks again,+/// as a boolean does.+#[test]+fn the_top_of_a_read_block_with_a_hole_moves() {+ // A 20 x 20 x 10 block with a 6 mm hole through the middle of it.+ let block = box_solid(Vec3::ZERO, Vec3::new(20.0, 20.0, 10.0)).unwrap();+ let drill = cylinder(Vec3::new(10.0, 10.0, -5.0), Vec3::Z, 3.0, 20.0).unwrap();+ let built = boolean(&block, &drill, BoolOp::Cut).unwrap().unwrap();+ let read = through_step(&built);+ // Read back, the hole's wall is one whole turn instead of the two halves it was built as.+ assert!(read.face_count() < built.face_count());+ assert!(super::needed_one(&read) && !super::needed_one(&built));++ // The top, 2 mm up: the part grows by the plate's section, the hole still through it.+ let section = 400.0 - PI * 9.0;+ let moved = offset_faces(&read, &[Vec3::new(5.0, 5.0, 10.0)], 2.0).unwrap();+ assert!(rel(volume(&moved), volume(&read) + section * 2.0) < 1e-3, "{} {}", volume(&moved), volume(&read) + section * 2.0);+ // The same answer as moving the built body's top, which never needed the split.+ let straight = offset_faces(&built, &[Vec3::new(5.0, 5.0, 10.0)], 2.0).unwrap();+ assert!(rel(volume(&moved), volume(&straight)) < 1e-5, "{} {}", volume(&moved), volume(&straight));+ // And the faces still say where they came from, so names and sketches survive the retry.+ assert!(moved.history().is_some());+}+#[test]fn a_block_pressed_into_a_read_pins_side_is_intersected_and_cut_exactly() {let (read, block) = (through_step(&pin()), block());