SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Snap fit: refuse sizes too small to build (#5022) (#5027)
raykholo567bc5072395
diff --git a/crates/doc/src/eval_more.rs b/crates/doc/src/eval_more.rsindex b60947b1dc..5f450368aa 100644--- a/crates/doc/src/eval_more.rs+++ b/crates/doc/src/eval_more.rs@@ -1714,6 +1714,21 @@ fn offset_left(p: &[Vec2], d: f64) -> Vec<Vec2> {.collect()}+/// The smallest size a plastic feature's lengths may have, in millimetres. Thinner walls and+/// shorter arms than this are below what the solid booleans can resolve.+const PLASTIC_MIN_SIZE: f64 = 0.01;++/// `v`, the feature's `what`, when it is at least [`PLASTIC_MIN_SIZE`].+fn plastic_size(feature: &str, what: &str, v: f64) -> Result<f64> {+ if v.is_finite() && v >= PLASTIC_MIN_SIZE {+ Ok(v)+ } else {+ // Tiny values in exponent form: 1e-320 written out is hundreds of zeros.+ let got = if v != 0.0 && v.abs() < 1e-3 { format!("{v:e}") } else { format!("{v}") };+ Err(DocError::Invalid(format!("the {feature}'s {what} must be at least {PLASTIC_MIN_SIZE} mm (got {got} mm)")))+ }+}+pub(super) fn plastic_eval(doc: &Document, vals: &BTreeMap<String, Value>, f: &Feature, st: &mut ModelState) -> Result<()> {let len = |e: &str| val(vals, e, Kind::Length);// The plastic rule of the body a feature goes on gives its defaults (draft, clearance).@@ -1738,10 +1753,8 @@ pub(super) fn plastic_eval(doc: &Document, vals: &BTreeMap<String, Value>, f: &Fbody,} => {let target = body_under(st, body, position.0)?;- let (r0, h) = (len(diameter)? / 2.0, len(height)?);- if !(r0 > 0.0 && h > 0.0) {- return Err(DocError::Invalid("the boss diameter and height must be positive".into()));- }+ let r0 = plastic_size("boss", "diameter", len(diameter)?)? / 2.0;+ let h = plastic_size("boss", "height", len(height)?)?;let dr = match draft {Some(e) => val(vals, e, Kind::Angle)?,None => rule(&target)?.map_or(0.0, |r| r.draft),@@ -1784,8 +1797,10 @@ pub(super) fn plastic_eval(doc: &Document, vals: &BTreeMap<String, Value>, f: &FNone => 0.0,};let hr = h - off;- if n > 0.0 && !(t > 0.0 && l > 0.0 && hr > 0.0) {- return Err(DocError::Invalid("ribs need a positive thickness, length and height".into()));+ if n > 0.0 {+ plastic_size("boss", "rib_thickness", t)?;+ plastic_size("boss", "rib_length", l)?;+ plastic_size("boss", "rib height (height less rib_offset)", hr)?;}for k in 0..n as usize {let a = std::f64::consts::TAU * k as f64 / n;@@ -1816,9 +1831,9 @@ pub(super) fn plastic_eval(doc: &Document, vals: &BTreeMap<String, Value>, f: &F}}if let Some(hd) = hole_diameter {- let rh = len(hd)? / 2.0;+ let rh = plastic_size("boss", "hole_diameter", len(hd)?)? / 2.0;let depth = match hole_depth {- Some(e) => len(e)?,+ Some(e) => plastic_size("boss", "hole_depth", len(e)?)?,None => h,};if !(rh > 0.0 && rh < r_top && depth > 0.0) {@@ -1835,14 +1850,15 @@ pub(super) fn plastic_eval(doc: &Document, vals: &BTreeMap<String, Value>, f: &Flet target = body_under(st, body, face.0)?;let mb = st.body(&target).cloned().ok_or_else(|| DocError::Invalid("body".into()))?;let n = planar_normal_at(&mb.body, face.0).ok_or_else(|| DocError::Invalid("pick the planar rim face".into()))?;- let (w, h) = (len(width)?, len(height)?);+ let what = if *groove { "groove" } else { "lip" };+ let (w, h) = (plastic_size(what, "width", len(width)?)?, plastic_size(what, "height", len(height)?)?);let g = match gap {Some(e) => len(e)?,None if *groove => rule(&target)?.map_or(0.0, |r| r.clearance),None => 0.0,};- if !(w > 0.0 && h > 0.0 && g >= 0.0) {- return Err(DocError::Invalid("the lip width and height must be positive".into()));+ if !(g >= 0.0) {+ return Err(DocError::Invalid("the groove's gap must not be negative".into()));}let plane = Plane::from_normal(face.0, n).ok_or_else(|| DocError::Invalid("rim plane".into()))?;let tol = (mb.body.size() * 1e-3).max(1e-3);@@ -1920,9 +1936,11 @@ pub(super) fn plastic_eval(doc: &Document, vals: &BTreeMap<String, Value>, f: &F}FeatureKind::SnapFit { position, direction, hook, length, thickness, width, catch_depth, catch_length, body } => {let target = body_under(st, body, position.0)?;- let (l, t, w, cd, cl) = (len(length)?, len(thickness)?, len(width)?, len(catch_depth)?, len(catch_length)?);- if !(l > 0.0 && t > 0.0 && w > 0.0 && cd > 0.0 && cl > 0.0 && cl < l) {- return Err(DocError::Invalid("the snap fit's sizes must be positive and the catch shorter than the arm".into()));+ let size = |what: &str, e: &str| plastic_size("snap fit", what, len(e)?);+ let (l, t, w) = (size("length", length)?, size("thickness", thickness)?, size("width", width)?);+ let (cd, cl) = (size("catch_depth", catch_depth)?, size("catch_length", catch_length)?);+ if !(cl < l) {+ return Err(DocError::Invalid("the snap fit's catch must be shorter than its arm".into()));}let (x, y, z) = local_frame(direction.0, Some(hook.0))?;let delta = (l * 0.05).clamp(1e-3, 0.5);@@ -1943,15 +1961,16 @@ pub(super) fn plastic_eval(doc: &Document, vals: &BTreeMap<String, Value>, f: &F}FeatureKind::Rest { position, direction, along, width, length, height, draft, thickness, body } => {let target = body_under(st, body, position.0)?;- let (w, h) = (len(width)?, len(height)?);- let l = length.as_deref().map(len).transpose()?;+ let size = |what: &str, e: &str| plastic_size("rest", what, len(e)?);+ let (w, h) = (size("width", width)?, size("height", height)?);+ let l = length.as_deref().map(|e| size("length", e)).transpose()?;let dr = match draft {Some(e) => val(vals, e, Kind::Angle)?,None => rule(&target)?.map_or(0.0, |r| r.draft),};- let t = thickness.as_deref().map(len).transpose()?;- if !(w > 0.0 && h > 0.0 && l.is_none_or(|l| l > 0.0) && t.is_none_or(|t| t > 0.0 && 2.0 * t < w.min(l.unwrap_or(w)))) {- return Err(DocError::Invalid("the rest's sizes must be positive and its wall thinner than half its width".into()));+ let t = thickness.as_deref().map(|e| size("thickness", e)).transpose()?;+ if t.is_some_and(|t| 2.0 * t >= w.min(l.unwrap_or(w))) {+ return Err(DocError::Invalid("the rest's wall must be thinner than half its width".into()));}if !(dr.abs() < 1.0) {return Err(DocError::Invalid("the rest's draft must be under 57°".into()));diff --git a/crates/engine/src/cmd/plastic.rs b/crates/engine/src/cmd/plastic.rsindex 6675dcdc0f..5a4e138b4c 100644--- a/crates/engine/src/cmd/plastic.rs+++ b/crates/engine/src/cmd/plastic.rs@@ -425,6 +425,40 @@ mod tests {assert!(s.execute("plastic.snap_fit", &json!({"position": [5, 5, 4], "direction": [0, 0, 1], "hook": [0, 0, 1], "length": 15, "thickness": 1.5, "width": 6, "catch_depth": 1, "catch_length": 3})).is_err());}+ #[test]+ fn plastic_sizes_below_the_minimum_are_refused_by_name() {+ let mut s = Session::default();+ run(&mut s, "solid.box", json!({"length": 40, "width": 40, "height": 4}));+ let v0 = volume(&mut s);+ let snap =+ json!({"position": [20, 20, 4], "hook": [1, 0, 0], "length": 15, "thickness": 1.5, "width": 6, "catch_depth": 1, "catch_length": 3});+ for (key, tiny) in+ [("thickness", 1e-9), ("thickness", 1e-320), ("length", 1e-5), ("width", 0.005), ("catch_depth", 1e-9), ("catch_length", 1e-9)]+ {+ let mut p = snap.clone();+ p[key] = json!(tiny);+ let e = s.execute("plastic.snap_fit", &p).unwrap_err().to_string();+ assert!(e.contains(&format!("snap fit's {key} must be at least 0.01 mm")), "{key}={tiny}: {e}");+ }+ let boss = json!({"position": [20, 20, 4], "diameter": 6, "height": 5});+ for (key, tiny) in [("diameter", 1e-9), ("height", 1e-9), ("hole_diameter", 1e-9), ("rib_thickness", 1e-9)] {+ let mut p = boss.clone();+ p[key] = json!(tiny);+ if key == "rib_thickness" {+ p["ribs"] = json!(4);+ }+ let e = s.execute("plastic.boss", &p).unwrap_err().to_string();+ assert!(e.contains(&format!("boss's {key} must be at least 0.01 mm")), "{key}={tiny}: {e}");+ }+ let e = s.execute("plastic.rest", &json!({"position": [20, 20, 4], "width": 6, "height": 1e-9})).unwrap_err().to_string();+ assert!(e.contains("rest's height must be at least 0.01 mm"), "{e}");+ assert!(rel(volume(&mut s), v0) < 1e-9, "the refused features changed nothing");+ // The minimum itself still builds.+ let mut p = snap.clone();+ p["thickness"] = json!(0.01);+ run(&mut s, "plastic.snap_fit", p);+ }+#[test]fn enclosure_snap_fit_follows_resized_front_wall() {let mut s = Session::default();