Sheet metal rules: a design parameter named Thickness no longer overrides it (#9623) (#9633)

* claim: #9623

Signed-off-by: Maurizio Monticelli <maurizio@whatwedo.ch>

* Sheet metal and plastic rules: Thickness in a rule's expressions is the rule's thickness

A rule's expressions read `Thickness` as the rule's thickness only while the
design had no parameter of that name; with one, the default gap, reliefs and
a `Thickness` bend radius took the parameter instead (a 2 mm rule beside
Thickness = 10 mm closed corners to a 10 mm gap). Now `Thickness` is always
the rule's, as documented; the rule's own thickness still reads the design's
parameters, so `thickness: Thickness` keeps naming the design parameter.

Renaming a parameter called Thickness, its users and what it drives follow
the same reading: only a rule's thickness field refers to it. Renaming a
parameter to Thickness is refused while another rule field uses it, since
that field would then read the rule's thickness instead.

Fixes #9623

Signed-off-by: Maurizio Monticelli <maurizio@whatwedo.ch>

---------

Signed-off-by: Maurizio Monticelli <maurizio@whatwedo.ch>
This commit is contained in:
rugbymauri
2026-11-05 01:07:30 +00:00
committed by GitHub
parent 7c89f7445f
commit 67c58cbd28
6 changed files with 136 additions and 11 deletions
+26 -8
View File
@@ -9,6 +9,7 @@ use std::collections::{BTreeMap, BTreeSet};
use serde::{Deserialize, Serialize};
use crate::expr::{self, Expr, Kind, ParamDef, Value};
use crate::sheet::rule_references;
use crate::walk::Walk;
use crate::{DocError, Document, FeatureKind, MAX_PARAMS, Result};
@@ -797,13 +798,14 @@ impl Document {
}
}
let uses = |e: &str| expr::references(e).iter().any(|r| r == name);
let rule_uses = |field: &str, e: &str| rule_references(field, e).iter().any(|r| r == name);
for r in &self.sheet.rules {
for (field, _, _) in r.fields().into_iter().filter(|(_, e, _)| uses(e)) {
for (field, _, _) in r.fields().into_iter().filter(|(f, e, _)| rule_uses(f, e)) {
out.push(ParamUser::Rule { kind: "sheet metal".into(), name: r.name.clone(), field: field.into() });
}
}
for r in &self.plastic.rules {
for (field, _, _) in r.fields().into_iter().filter(|(_, e, _)| uses(e)) {
for (field, _, _) in r.fields().into_iter().filter(|(f, e, _)| rule_uses(f, e)) {
out.push(ParamUser::Rule { kind: "plastic".into(), name: r.name.clone(), field: field.into() });
}
}
@@ -855,6 +857,21 @@ impl Document {
if self.name_taken(new) {
return Err(DocError::Invalid(format!("there is already a parameter `{new}`")));
}
// Outside a rule's own thickness, `Thickness` is the rule's thickness (#9623): renaming to
// it would turn those references into the rule's thickness and change their values.
if new == "Thickness" {
let uses_old = |field: &str, e: &str| field != "thickness" && expr::references(e).iter().any(|r| r == old);
let sheet = self.sheet.rules.iter().flat_map(|r| r.fields().into_iter().map(move |(f, e, _)| ("sheet metal", &r.name, f, e)));
let plastic = self.plastic.rules.iter().flat_map(|r| r.fields().into_iter().map(move |(f, e, _)| ("plastic", &r.name, f, e)));
let list: Vec<String> =
sheet.chain(plastic).filter(|(_, _, f, e)| uses_old(f, e)).map(|(kind, name, f, _)| format!("{kind} rule {name} ({f})")).collect();
if !list.is_empty() {
return Err(DocError::Invalid(format!(
"`{old}` can't be renamed to `Thickness`: {} would read the rule's thickness instead",
list.join(", ")
)));
}
}
let mut n = 0;
let text = self.text_param(old).is_some();
for p in &mut self.text_params {
@@ -964,7 +981,8 @@ impl Document {
}
// Sheet metal and plastic rules and canvas planes hold expressions too (#4420).
let rules = self.sheet.rules.iter_mut().flat_map(|r| r.fields_mut()).chain(self.plastic.rules.iter_mut().flat_map(|r| r.fields_mut()));
for (_, e) in rules {
// `Thickness` outside a rule's own thickness is the rule's, not the parameter (#9623).
for (_, e) in rules.filter(|(field, _)| old != "Thickness" || *field == "thickness") {
let r = expr::rename_reference(e, old, new);
if r != *e {
*e = r;
@@ -1025,10 +1043,10 @@ impl Document {
}
// Sheet metal and plastic features rebuild when their rules' parameters change (#4420).
if f.kind.is_sheet() {
names.extend(self.sheet.rules.iter().flat_map(|r| r.fields()).flat_map(|(_, e, _)| expr::references(e)));
names.extend(self.sheet.rules.iter().flat_map(|r| r.fields()).flat_map(|(f, e, _)| rule_references(f, e)));
}
if f.kind.is_plastic() {
names.extend(self.plastic.rules.iter().flat_map(|r| r.fields()).flat_map(|(_, e, _)| expr::references(e)));
names.extend(self.plastic.rules.iter().flat_map(|r| r.fields()).flat_map(|(f, e, _)| rule_references(f, e)));
}
out.extend(names.into_iter().map(|n| (format!("Feature:{}", f.name), n)));
}
@@ -1052,9 +1070,9 @@ impl Document {
}
// A rule that uses it changes every sheet metal (or plastic) feature: the evaluation
// hashes the rules into their fingerprint.
let uses = |e: &String| expr::references(e).iter().any(|r| affected.contains(r));
let sheet = self.sheet.rules.iter().any(|r| r.fields().iter().any(|(_, e, _)| uses(e)));
let plastic = self.plastic.rules.iter().any(|r| r.fields().iter().any(|(_, e, _)| uses(e)));
let uses = |field: &str, e: &str| rule_references(field, e).iter().any(|r| affected.contains(r));
let sheet = self.sheet.rules.iter().any(|r| r.fields().iter().any(|(f, e, _)| uses(f, e)));
let plastic = self.plastic.rules.iter().any(|r| r.fields().iter().any(|(f, e, _)| uses(f, e)));
let mut out = Vec::new();
for f in &self.features {
let mut hit = f.param_names.iter().any(|n| affected.contains(n));
+1 -1
View File
@@ -167,7 +167,7 @@ impl Document {
return Err(DocError::Invalid("the plastic thickness must be positive".into()));
}
let with_t = |e: &str, k: Kind| -> Result<f64> {
let f = |n: &str| if n == "Thickness" && !vals.contains_key("Thickness") { Ok(Value::length(t)) } else { look(n) };
let f = |n: &str| if n == "Thickness" { Ok(Value::length(t)) } else { look(n) };
let v = expr::eval_with(e, &f)?.to_kind(k)?;
if !v.is_finite() || v < 0.0 {
return Err(DocError::Invalid(format!("plastic rule `{}`: `{e}` must not be negative", r.name)));
+13 -2
View File
@@ -284,6 +284,16 @@ impl SheetRule {
}
}
/// The design parameters a rule's `field` expression `e` reads: outside the rule's own
/// `thickness`, `Thickness` is the rule's thickness (sheet metal and plastic rules alike).
pub fn rule_references(field: &str, e: &str) -> Vec<String> {
let mut names = expr::references(e);
if field != "thickness" {
names.retain(|n| n != "Thickness");
}
names
}
fn rule_gap() -> String {
"Thickness".into()
}
@@ -464,7 +474,8 @@ impl Document {
.unwrap_or_else(SheetRule::steel)
}
/// Evaluate a rule: `Thickness` in its expressions is the rule's thickness.
/// Evaluate a rule: `Thickness` in its expressions is the rule's thickness, even where the
/// design has a parameter of that name (#9623); only the rule's `thickness` itself reads it.
pub fn rule_values(&self, vals: &std::collections::BTreeMap<String, Value>, r: &SheetRule) -> Result<RuleValues> {
self.rule_values_at(vals, r, None)
}
@@ -481,7 +492,7 @@ impl Document {
return Err(DocError::Invalid("the sheet thickness must be more than 0 and less than 10000 mm".into()));
}
let with_t = |e: &str, k: Kind| -> Result<f64> {
let f = |n: &str| if n == "Thickness" && !vals.contains_key("Thickness") { Ok(Value::length(t)) } else { look(n) };
let f = |n: &str| if n == "Thickness" { Ok(Value::length(t)) } else { look(n) };
expr::eval_with(e, &f)?.to_kind(k)
};
let v = RuleValues {
+35
View File
@@ -257,6 +257,41 @@ fn rules_and_canvases_follow_a_renamed_parameter() {
assert_eq!(s.doc.sheet.rules[0].thickness, "gauge");
}
/// #9623: outside a rule's own `thickness`, `Thickness` is the rule's thickness, not a design
/// parameter of that name: renaming that parameter leaves it, and it doesn't count as a user.
#[test]
fn a_rules_own_thickness_is_not_a_parameter_named_thickness() {
let mut s = Session::default();
run(&mut s, "parameters.add", json!({"name": "Thickness", "expression": "2 mm"}));
run(&mut s, "sheet.manage_rules", json!({"name": "R", "thickness": "Thickness", "bend_radius": "Thickness", "active": true}));
run(&mut s, "plastic.manage_rules", json!({"name": "Housing", "thickness": "3 mm", "clearance": "Thickness / 10"}));
let u = run(&mut s, "parameters.users", json!({"name": "Thickness"}));
let fields: Vec<String> = u["users"].as_array().unwrap().iter().map(|x| x.to_string()).collect();
assert_eq!(fields.len(), 1, "only the sheet metal rule's thickness: {u}");
run(&mut s, "parameters.rename", json!({"name": "Thickness", "new_name": "Gauge"}));
let r = &s.doc.sheet.rules[0];
assert_eq!((r.thickness.as_str(), r.bend_radius.as_str(), r.gap.as_str()), ("Gauge", "Thickness", "Thickness"));
let housing = s.doc.plastic.rules.iter().find(|r| r.name == "Housing").unwrap();
assert_eq!(housing.clearance, "Thickness / 10");
}
/// #9623: renaming a parameter to `Thickness` is refused while a rule field other than its own
/// thickness uses it (that field would then read the rule's thickness), and nothing changes.
#[test]
fn renaming_to_thickness_is_refused_where_a_rule_would_read_its_own() {
let mut s = Session::default();
run(&mut s, "parameters.add", json!({"name": "Factor", "expression": "0.3", "unit": ""}));
run(&mut s, "parameters.add", json!({"name": "Gauge", "expression": "2 mm"}));
run(&mut s, "sheet.manage_rules", json!({"name": "R", "thickness": "Gauge", "k_factor": "Factor", "active": true}));
let before = s.doc.clone();
let e = err(&mut s, "parameters.rename", json!({"name": "Factor", "new_name": "Thickness"}));
assert!(e.contains("sheet metal rule R (k_factor)"), "{e}");
assert!(s.doc == before, "a refused rename changes nothing");
// Used only by a rule's own thickness, the rename goes through.
run(&mut s, "parameters.rename", json!({"name": "Gauge", "new_name": "Thickness"}));
assert_eq!((s.doc.sheet.rules[0].thickness.as_str(), s.doc.sheet.rules[0].k_factor.as_str()), ("Thickness", "Factor"));
}
#[test]
fn favorites_comments_and_completion() {
let mut s = Session::default();
+20
View File
@@ -594,6 +594,26 @@ mod tests {
assert!(s.execute("plastic.assign_rule", &json!({"bodies": ["Nope"], "rule": "Thick ABS"})).is_err());
}
/// #9623: `Thickness` in a rule's expressions is the rule's thickness, also when the design
/// has a parameter of that name; the rule's own `thickness` may still use that parameter.
#[test]
fn thickness_in_a_plastic_rule_is_the_rules_even_beside_a_parameter_of_that_name() {
let values = |param: Option<&str>, thickness: &str| -> Value {
let mut s = Session::default();
if let Some(e) = param {
run(&mut s, "parameters.add", json!({"name": "Thickness", "expression": e}));
}
run(&mut s, "plastic.manage_rules", json!({"name": "Thick ABS", "thickness": thickness, "clearance": "Thickness / 10"}))["rule"]["values"]
.clone()
};
let want = values(None, "3 mm");
assert_eq!((want["thickness"].as_f64(), want["nominal_radius"].as_f64()), (Some(3.0), Some(1.5)), "{want}");
assert!(want["clearance"].as_f64().is_some_and(|c| (c - 0.3).abs() < 1e-12), "{want}");
assert!(want["thickness_variation"].as_f64().is_some_and(|v| (v - 0.45).abs() < 1e-12), "{want}");
assert_eq!(values(Some("10 mm"), "3 mm"), want);
assert_eq!(values(Some("3 mm"), "Thickness"), want);
}
#[test]
fn groove_takes_the_rule_clearance() {
let mut g = Session::default();
+41
View File
@@ -546,6 +546,47 @@ fn a_refused_rule_names_the_value_at_fault() {
run(&mut s, "sheet.manage_rules", json!({"name": "R", "thickness": 2, "bend_radius": 0}));
}
/// #9623: `Thickness` in a rule's expressions is the rule's thickness, also when the design has a
/// parameter of that name; the rule's own `thickness` may still use that parameter.
#[test]
fn thickness_in_a_rule_is_the_rules_even_beside_a_parameter_of_that_name() {
fn build(param: Option<&str>, rule: Value) -> (Value, f64) {
let mut s = Session::default();
if let Some(e) = param {
run(&mut s, "parameters.add", json!({"name": "Thickness", "expression": e}));
}
run(&mut s, "sheet.manage_rules", rule);
base(&mut s, 100.0, 50.0);
let t = s.model.state().sheets[0].t;
run(&mut s, "sheet.flange", json!({"edges": [[50, 0, t], [100, 25, t]], "height": 20}));
let failing: Vec<String> = s.model.results.iter().filter_map(|r| r.error.clone()).collect();
assert!(failing.is_empty(), "{failing:?}");
let l = run(&mut s, "sheet.rules", json!({}));
let values = l["rules"].as_array().and_then(|r| r.iter().find(|r| r["rule"]["name"] == "R")).map(|r| r["values"].clone()).unwrap_or_default();
(values, volume(&mut s))
}
let rule = json!({"name": "R", "thickness": 2, "k_factor": 0.5, "bend_radius": 2, "corner_relief_shape": "round", "active": true});
let (want, v) = build(None, rule.clone());
for (k, x) in [("thickness", 2.0), ("gap", 2.0), ("relief_width", 2.0), ("relief_depth", 1.0), ("corner_relief", 8.0), ("bend_radius", 2.0)] {
assert_eq!(want[k].as_f64(), Some(x), "{k}: {want}");
}
assert!(rel(v, 14651.157) < 1e-4, "{v}");
let (got, w) = build(Some("10 mm"), rule);
assert_eq!(got, want);
assert!(rel(w, v) < 1e-9, "{w} against {v}");
// A bend radius of `Thickness` is the rule's 2 mm, not the parameter's 10.
let rule = json!({"name": "R", "thickness": 2, "k_factor": 0.5, "bend_radius": "Thickness", "corner_relief_shape": "round", "active": true});
let (got, w) = build(Some("10 mm"), rule);
assert_eq!(got, want);
assert!(rel(w, v) < 1e-9, "{w} against {v}");
// The rule's thickness itself reads the design's `Thickness`, and the rest follow it.
let rule =
json!({"name": "R", "thickness": "Thickness", "k_factor": 0.5, "bend_radius": "Thickness", "corner_relief_shape": "round", "active": true});
let (got, w) = build(Some("2 mm"), rule);
assert_eq!(got, want);
assert!(rel(w, v) < 1e-9, "{w} against {v}");
}
#[test]
fn contour_flange_hem_and_tray() {
// Oracle 72.