Skip to content

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

Parameters: unit-less numbers follow the other side's unit (#9015) (#9027)

SilentWolf75355b48659582
diff --git a/crates/doc/src/eval.rs b/crates/doc/src/eval.rs
index d38922a724..389e36a58a 100644
--- a/crates/doc/src/eval.rs
+++ b/crates/doc/src/eval.rs
@@ -2523,13 +2523,40 @@ fn point(vals: &BTreeMap<String, Value>, p: &crate::PointExpr) -> Result<Vec3> {
pub(crate) fn val(vals: &BTreeMap<String, Value>, e: &str, k: Kind) -> Result<f64> {
let scale = vals.get(DESIGN_LEN).map(|x| x.v).unwrap_or(1.0);
+ // Non-literal lengths (a parameter ± a bare number) must read that bare number in the design's
+ // units during the expression, not only when the whole result is unit-less (#9015).
if k == Kind::Length && scale != 1.0 && !expr::is_literal(e) {
- let v = expr::eval_with(e, &|n| vals.get(n).copied().ok_or_else(|| DocError::Expr(format!("unknown parameter `{n}`"))))?;
- return v.to_kind_in(k, expr::Defaults { len: scale, ..expr::Defaults::default() });
+ let defaults = expr::Defaults { len: scale, ..expr::Defaults::default() };
+ let look = |n: &str| vals.get(n).copied().ok_or_else(|| DocError::Expr(format!("unknown parameter `{n}`")));
+ return expr::eval_with_defaults(e, &look, defaults)?.to_kind_in(k, defaults);
}
Document::eval_in(vals, e, k)
}
+#[cfg(test)]
+mod design_unit_val_tests {
+ use super::*;
+ use expr::Value;
+
+ /// #9015: with inches as the design unit, `baseLength - 1` subtracts one inch (not one mm).
+ #[test]
+ fn bare_offsets_beside_a_length_follow_the_design_units() {
+ let mut vals = BTreeMap::new();
+ vals.insert(DESIGN_LEN.into(), Value::num(25.4));
+ vals.insert("baseLength".into(), Value::length(76.0 * 25.4));
+ let got = val(&vals, "baseLength - 1", Kind::Length).unwrap();
+ assert!((got - 75.0 * 25.4).abs() < 1e-9, "got {got} mm, want 75 in");
+ let got = val(&vals, "baseLength - 1.5", Kind::Length).unwrap();
+ assert!((got - 74.5 * 25.4).abs() < 1e-9, "got {got} mm, want 74.5 in");
+ // An explicit millimetre still wins.
+ let got = val(&vals, "baseLength - 1 mm", Kind::Length).unwrap();
+ assert!((got - (76.0 * 25.4 - 1.0)).abs() < 1e-9, "got {got}");
+ // A bare literal stays millimetres (feature inputs write the design unit in when typed).
+ let got = val(&vals, "2", Kind::Length).unwrap();
+ assert!((got - 2.0).abs() < 1e-12, "got {got}");
+ }
+}
+
/// Collapsing the outline and its holes onto one apex cannot make a manifold solid.
fn refuse_loft_holes(r: &Region2) -> Result<()> {
if r.holes.is_empty() {
diff --git a/crates/doc/src/params.rs b/crates/doc/src/params.rs
index 2f094f9a8e..87f601dcbe 100644
--- a/crates/doc/src/params.rs
+++ b/crates/doc/src/params.rs
@@ -268,7 +268,7 @@ impl Document {
.filter(|(_, _, k)| *k == Kind::Length)
.map(|(_, e, _)| {
let d = if expr::is_literal(&e) { expr::Defaults::default() } else { expr::Defaults { len: scale, ..expr::Defaults::default() } };
- expr::eval_with(&e, &look).and_then(|v| v.to_kind_in(Kind::Length, d)).ok()
+ expr::eval_with_defaults(&e, &look, d).and_then(|v| v.to_kind_in(Kind::Length, d)).ok()
})
.collect()
}
diff --git a/crates/engine/src/cmd/parameters.rs b/crates/engine/src/cmd/parameters.rs
index 55326d50a6..1ab65dc6df 100644
--- a/crates/engine/src/cmd/parameters.rs
+++ b/crates/engine/src/cmd/parameters.rs
@@ -467,7 +467,9 @@ fn evaluate(s: &mut Session, p: &Value) -> Result<Value> {
(None, None) => None,
};
let (vals, _) = s.doc.param_values();
- let defaults = expr::Defaults::for_unit(unit.unwrap_or(""), 1.0);
+ // Bare numbers follow the asked-for unit when given, else the design's length unit (#9015).
+ let doc_len = expr::unit_info(s.doc.units.trim()).filter(|(k, _)| *k == Kind::Length).map(|(_, s)| s).unwrap_or(1.0);
+ let defaults = expr::Defaults::for_unit(unit.unwrap_or(""), doc_len);
let refs = expr::references(&e);
let lookup = |n: &str| vals.get(n).copied().ok_or_else(|| solidcraft_doc::DocError::Expr(format!("unknown parameter `{n}`")));
let r = expr::eval_with_defaults(&e, &lookup, defaults).and_then(|v| match kind {
diff --git a/crates/engine/src/cmd/parameters_tests.rs b/crates/engine/src/cmd/parameters_tests.rs
index bec96d2dd5..4fcb624260 100644
--- a/crates/engine/src/cmd/parameters_tests.rs
+++ b/crates/engine/src/cmd/parameters_tests.rs
@@ -1033,6 +1033,18 @@ fn expr_of_now(s: &Session, name: &str) -> String {
s.doc.param(name).map(|p| p.expr.clone()).unwrap_or_default()
}
+/// #9015: `expr.evaluate` of a length with a bare offset uses the design's units.
+#[test]
+fn expr_evaluate_bare_offsets_follow_design_units() {
+ let mut s = Session::default();
+ run(&mut s, "document.units", json!({"units": "in"}));
+ run(&mut s, "parameters.add", json!({"name": "baseLength", "expression": "76"}));
+ let r = run(&mut s, "expr.evaluate", json!({"expression": "baseLength - 1", "kind": "length"}));
+ assert_eq!(r["ok"], true, "{r}");
+ // 75 in in millimetres (without the fix this was 76 in − 1 mm).
+ assert!((r["value"].as_f64().unwrap_or(0.0) - 75.0 * 25.4).abs() < 1e-9, "{r}");
+}
+
/// A dimension made as `w = 20` shows `w`; typing `w = 30` over it sets `w` rather than being
/// refused as a taken name (#4071).
#[test]
diff --git a/crates/engine/src/cmd/patterns_tests.rs b/crates/engine/src/cmd/patterns_tests.rs
index 75b9475949..2ea9aa1284 100644
--- a/crates/engine/src/cmd/patterns_tests.rs
+++ b/crates/engine/src/cmd/patterns_tests.rs
@@ -1006,6 +1006,22 @@ fn pattern_count_units_explain_how_to_use_a_parameter() -> Result<(), Box<dyn st
Ok(())
}
+/// #9015: in an inch design, pattern spacing `baseLength - 1` subtracts one inch, not one mm.
+#[test]
+fn pattern_spacing_bare_offsets_follow_design_units() {
+ let mut s = Session::default();
+ run(&mut s, "document.units", json!({"units": "in"}));
+ run(&mut s, "parameters.add", json!({"name": "baseLength", "expression": "4"}));
+ run(&mut s, "solid.box", json!({"length": 10, "width": 10, "height": 10, "body_name": "Seed"}));
+ run(&mut s, "solid.pattern.rectangular", json!({"bodies": ["Seed"], "dir1": [1, 0, 0], "count1": 2, "spacing1": "baseLength - 1"}));
+ assert_eq!(s.model.state().bodies.len(), 2);
+ // Spacing is 3 in = 76.2 mm (not 3 mm): the seed is 10 in, so the copy starts at x = 76.2.
+ let m = run(&mut s, "inspect.measure", json!({}));
+ let bodies = m["bodies"].as_array().expect("bodies");
+ let xmin = |b: &Value| b["bbox"]["min"][0].as_f64().unwrap_or(0.0);
+ assert!(bodies.iter().any(|b| (xmin(b) - 76.2).abs() < 1e-6), "expected a copy spaced 3 in apart, got {m}");
+}
+
#[test]
fn pattern_count_fractions_are_refused() -> Result<(), Box<dyn std::error::Error>> {
let mut s = Session::default();