Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

fix(cad): round small holes and grooves

Sidwebworks0e42e173bdc9
diff --git a/apps/assembler/src/actions/optimize.rs b/apps/assembler/src/actions/optimize.rs
index c50592a9f7..70d8b51c86 100644
--- a/apps/assembler/src/actions/optimize.rs
+++ b/apps/assembler/src/actions/optimize.rs
@@ -212,7 +212,11 @@ impl Src {
/// `LARGE`. Always error-bounded and per-mesh adaptive — never unbounded ratio
/// targeting.
fn derive_auto_error(input_bytes: usize) -> Option<f32> {
- const SMALL: f64 = 2.0 * 1024.0 * 1024.0; // 2 MiB — below this, lossless
+ // 3 MiB — below this, lossless. Sized against the 0.15 rad angular default:
+ // the same geometry tessellates ~1.6x heavier than it did at 0.25 rad, so a
+ // 2 MiB cut would push parts that used to stay lossless back into the
+ // simplifier and undo the roundness the tighter angle just bought.
+ const SMALL: f64 = 3.0 * 1024.0 * 1024.0;
const LARGE: f64 = 32.0 * 1024.0 * 1024.0; // 32 MiB
const MAX_ERR: f64 = 0.02; // 2% of extent — heavier but still reasonable
if (input_bytes as f64) <= SMALL {
@@ -466,7 +470,7 @@ mod tests {
let base = optimize::DEFAULT_AUTO_ERROR;
// Small models get NO simplification — lossless encode only.
assert_eq!(derive_auto_error(64 * 1024), None);
- assert_eq!(derive_auto_error(2 * 1024 * 1024), None);
+ assert_eq!(derive_auto_error(3 * 1024 * 1024), None);
// Large models decimate harder, capped at 2%.
assert!((derive_auto_error(64 * 1024 * 1024).unwrap() - 0.02).abs() < 1e-6);
// Monotonic through the ramp, within bounds.
diff --git a/apps/assembler/src/main.rs b/apps/assembler/src/main.rs
index 5b5c34bf33..afebaff0ac 100644
--- a/apps/assembler/src/main.rs
+++ b/apps/assembler/src/main.rs
@@ -472,10 +472,13 @@ async fn create_convert(
glb_path: req["outputs"]["glb"]["path"].as_str().map(str::to_string),
graph_path: req["outputs"]["graph"]["path"].as_str().map(str::to_string),
lin: req["options"]["linearDeflection"].as_f64().unwrap_or(0.1),
- // Matches the optimize default: 0.25 rad keeps circles smooth
- // (≥ ~25 segments; 0.5 gave a blocky ~13). Also part of the
- // convert result-cache key, so older coarse results aren't reused.
- ang: req["options"]["angularDeflection"].as_f64().unwrap_or(0.25),
+ // Matches the optimize default: 0.15 rad ≈ 42 segments per
+ // circle (0.25 gave 25, 0.5 a blocky 13). Angular deflection
+ // is the ONLY scale-invariant constraint, so this is what a
+ // small hole/groove's roundness actually comes from — sag
+ // never binds on a small radius. Also part of the convert
+ // result-cache key, so older coarse results aren't reused.
+ ang: req["options"]["angularDeflection"].as_f64().unwrap_or(0.15),
optimize: req["options"]["optimize"].as_bool().unwrap_or(true),
},
);
@@ -582,10 +585,13 @@ pub fn optimize_opts(out: &Value, q: &Value) -> actions::optimize::Opts {
.as_u64()
.unwrap_or(DEFAULT_MAX_OUTPUT_BYTES) as usize,
lin: q["linear_deflection"].as_f64().unwrap_or(0.1),
- // 0.25 rad → a full circle tessellates with ≥ ~25 segments (0.5 gave a
- // visibly blocky ~13). The bridge also scales linear deflection to each
- // shape, so small parts keep smooth curvature too.
- ang: q["angular_deflection"].as_f64().unwrap_or(0.25),
+ // 0.15 rad → ≈ 42 segments per circle (0.25 gave 25, 0.5 a blocky 13).
+ // Angular deflection is the only SCALE-INVARIANT constraint: a small
+ // hole's sag budget is satisfied after a handful of chords, so segment
+ // count on holes/grooves/fillets comes from this value alone. The bridge
+ // also scales linear deflection to each shape, which covers the large
+ // curved faces where sag does bind.
+ ang: q["angular_deflection"].as_f64().unwrap_or(0.15),
// Per-request budget wins; else the env default (set on the Lambda path).
budget: q["time_budget_secs"]
.as_u64()
diff --git a/crates/converter/tests/tessellation_smoothness.rs b/crates/converter/tests/tessellation_smoothness.rs
index 1757d15469..a58d2744f1 100644
--- a/crates/converter/tests/tessellation_smoothness.rs
+++ b/crates/converter/tests/tessellation_smoothness.rs
@@ -6,6 +6,35 @@
use converter::convert::convert_step;
+/// Triangles contributed by ONE drilled hole: tessellate the same plate with and
+/// without holes and take the per-hole delta. The delta is proportional to the
+/// circle's segment count (side wall + the retriangulated top/bottom rims), so
+/// it is the metric for "how round is a hole".
+fn tris_per_hole(size: f64, hole_radius: f64, lin: f64, ang: f64) -> f64 {
+ const HOLES: u32 = 8;
+ let plain = plate_tris(size, hole_radius, 0, lin, ang);
+ let drilled = plate_tris(size, hole_radius, HOLES, lin, ang);
+ (drilled as f64 - plain as f64) / HOLES as f64
+}
+
+fn plate_tris(size: f64, hole_radius: f64, holes: u32, lin: f64, ang: f64) -> u64 {
+ let dir = std::env::temp_dir().join(format!(
+ "carbon-plate-{size}-{}-{holes}-{}-{}",
+ (hole_radius * 1000.0) as u64,
+ (lin * 1000.0) as u64,
+ (ang * 1000.0) as u64
+ ));
+ std::fs::create_dir_all(&dir).unwrap();
+ let path = dir.join("plate.step");
+ let path_str = path.to_str().unwrap();
+ assert!(
+ occt_bridge::write_test_plate_with_holes(path_str, size, 10.0, hole_radius, holes),
+ "failed to write test plate STEP"
+ );
+ let text = std::fs::read_to_string(path_str).unwrap();
+ convert_step(path_str, &text, lin, ang).expect("convert").triangles as u64
+}
+
fn cylinder_tris(radius: f64, height: f64, lin: f64, ang: f64) -> u64 {
let dir = std::env::temp_dir().join(format!(
"carbon-cyl-{radius}-{height}-{}-{}",
@@ -62,3 +91,55 @@ fn small_parts_keep_proportional_smoothness() {
"small cylinder should still have >= ~25 circle segments (got {small} tris)"
);
}
+
+/// A hole's roundness must not depend on how big the part around it is, nor on
+/// the hole's own radius. Linear deflection is scaled to the SHAPE's bbox, so a
+/// 1.5 mm hole in a 200 mm plate gets a sag budget it satisfies after a handful
+/// of chords — only angular deflection keeps it round. This pins that: a tiny
+/// hole in a big plate tessellates the same as a big hole, and the same as a
+/// hole in a small plate.
+#[test]
+fn holes_are_round_regardless_of_hole_or_part_size() {
+ let tiny_hole_big_plate = tris_per_hole(200.0, 1.5, 0.1, 0.15);
+ let big_hole_big_plate = tris_per_hole(200.0, 5.0, 0.1, 0.15);
+ let hole_small_plate = tris_per_hole(20.0, 1.0, 0.1, 0.15);
+
+ for (label, got) in [
+ ("tiny hole / big plate", tiny_hole_big_plate),
+ ("big hole / big plate", big_hole_big_plate),
+ ("hole / small plate", hole_small_plate),
+ ] {
+ // 0.15 rad => ~42 segments; the per-hole delta runs ~8x the segment
+ // count, so ~340 tris. Floor well under that to stay robust to OCCT
+ // retriangulation details while still catching a drop to 25 (~208) or
+ // the old 13 (~108) segments.
+ assert!(
+ got >= 280.0,
+ "{label}: hole tessellated too coarsely ({got:.0} tris/hole)"
+ );
+ }
+ // Scale invariance: all three within 10% of each other.
+ let min = tiny_hole_big_plate
+ .min(big_hole_big_plate)
+ .min(hole_small_plate);
+ let max = tiny_hole_big_plate
+ .max(big_hole_big_plate)
+ .max(hole_small_plate);
+ assert!(
+ max <= min * 1.1,
+ "hole roundness varies with size ({min:.0}..{max:.0} tris/hole)"
+ );
+}
+
+/// Tightening angular deflection is the lever that makes small features rounder
+/// — each step must actually buy segments, or the default is doing nothing.
+#[test]
+fn tighter_angular_deflection_buys_hole_segments() {
+ let coarse = tris_per_hole(200.0, 1.5, 0.1, 0.5);
+ let previous = tris_per_hole(200.0, 1.5, 0.1, 0.25);
+ let current = tris_per_hole(200.0, 1.5, 0.1, 0.15);
+ assert!(
+ current > previous && previous > coarse,
+ "expected monotonic segment gain (0.5 -> {coarse:.0}, 0.25 -> {previous:.0}, 0.15 -> {current:.0})"
+ );
+}
diff --git a/crates/occt-bridge/src/lib.rs b/crates/occt-bridge/src/lib.rs
index dcef5ba17a..eaddafae6d 100644
--- a/crates/occt-bridge/src/lib.rs
+++ b/crates/occt-bridge/src/lib.rs
@@ -67,10 +67,21 @@ pub mod ffi {
/// Test fixture generator: a single cylinder — the canonical curved
/// surface for asserting tessellation smoothness (circle segments).
fn write_test_cylinder(path: &str, radius: f64, height: f64) -> bool;
+
+ /// Test fixture generator: a square plate with `holes` small
+ /// through-holes drilled in it — a small curved feature inside a large
+ /// bounding box, the case where sag-based deflection alone goes blocky.
+ fn write_test_plate_with_holes(
+ path: &str,
+ size: f64,
+ thickness: f64,
+ hole_radius: f64,
+ holes: u32,
+ ) -> bool;
}
}
pub use ffi::{
- read_brep, read_iges, read_step, read_xbf, step_to_xbf, write_test_cylinder, write_test_step,
- RawNode, Tree,
+ read_brep, read_iges, read_step, read_xbf, step_to_xbf, write_test_cylinder,
+ write_test_plate_with_holes, write_test_step, RawNode, Tree,
};
diff --git a/crates/occt-bridge/src/occt.cc b/crates/occt-bridge/src/occt.cc
index 31323b882d..7bf81c4b22 100644
--- a/crates/occt-bridge/src/occt.cc
+++ b/crates/occt-bridge/src/occt.cc
@@ -11,6 +11,7 @@
#include <cstdio>
#include <cstdlib>
#include <BinXCAFDrivers.hxx>
+#include <BRepAlgoAPI_Cut.hxx>
#include <BRepMesh_IncrementalMesh.hxx>
#include <BRepPrimAPI_MakeBox.hxx>
#include <BRepPrimAPI_MakeCylinder.hxx>
@@ -661,6 +662,40 @@ bool write_test_cylinder(rust::Str path, double radius, double height) {
}
}
+// Test fixture generator: a `size` x `size` x `thickness` plate with `holes`
+// through-holes of `hole_radius` drilled along X. The small-feature-in-a-big-
+// part case: the shape bbox is large (so the bbox-scaled linear deflection
+// stays coarse) while the drilled circles are tiny, which is exactly where the
+// segment count has to come from angular deflection rather than sag.
+bool write_test_plate_with_holes(rust::Str path, double size, double thickness,
+ double hole_radius, uint32_t holes) {
+ try {
+ ensure_step_init();
+ TopoDS_Shape shape =
+ BRepPrimAPI_MakeBox(gp_Pnt(0.0, 0.0, 0.0), size, size, thickness)
+ .Shape();
+ // Evenly spaced along the plate's mid-line, clear of the edges.
+ for (uint32_t i = 0; i < holes; ++i) {
+ const double x = size * (i + 1) / (holes + 1);
+ // Start below and run past the far face so the cut is a clean through-hole
+ // (a coincident face would leave a degenerate lid).
+ const gp_Ax2 axis(gp_Pnt(x, size / 2.0, -thickness),
+ gp_Dir(0.0, 0.0, 1.0));
+ TopoDS_Shape drill =
+ BRepPrimAPI_MakeCylinder(axis, hole_radius, thickness * 3.0).Shape();
+ shape = BRepAlgoAPI_Cut(shape, drill).Shape();
+ }
+ STEPControl_Writer writer;
+ if (writer.Transfer(shape, STEPControl_AsIs) != IFSelect_RetDone) {
+ return false;
+ }
+ std::string p(path);
+ return writer.Write(p.c_str()) == IFSelect_RetDone;
+ } catch (...) {
+ return false;
+ }
+}
+
bool write_test_step(rust::Str path, uint32_t boxes) {
try {
ensure_step_init();
diff --git a/crates/occt-bridge/src/occt.h b/crates/occt-bridge/src/occt.h
index edfdf91ab9..1dde2ee87b 100644
--- a/crates/occt-bridge/src/occt.h
+++ b/crates/occt-bridge/src/occt.h
@@ -18,4 +18,7 @@ Tree read_xbf(rust::Str path, double linear_deflection, double angular_deflectio
bool write_test_step(rust::Str path, uint32_t boxes);
bool write_test_cylinder(rust::Str path, double radius, double height);
+
+bool write_test_plate_with_holes(rust::Str path, double size, double thickness,
+ double hole_radius, uint32_t holes);
} // namespace carbon_occt