Skip to content

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

Parameters: the parameter list names the sketch that reads a parameter in a point coordinate (#12293) (#12350)

Ahmad Khan89191a1278fe
diff --git a/crates/doc/src/params.rs b/crates/doc/src/params.rs
index 48d452f817..15ea16ca2f 100644
--- a/crates/doc/src/params.rs
+++ b/crates/doc/src/params.rs
@@ -371,6 +371,14 @@ impl Document {
users.entry(x.as_str()).or_default().push(k.clone());
}
}
+ // A feature that reads a parameter itself (a sketch point at `a`) is one of its users
+ // too, named as `parameters.graph` names it (#12293).
+ let reads: Vec<(String, BTreeSet<String>)> = self.features.iter().map(|f| (format!("Feature:{}", f.name), self.feature_reads(f))).collect();
+ for (feature, names) in &reads {
+ for n in names {
+ users.entry(n.as_str()).or_default().push(feature.clone());
+ }
+ }
let used_by = |n: &str| -> Vec<String> { users.get(n).cloned().unwrap_or_default() };
let value = |n: &str, k: Kind| {
vals.get(n).and_then(|v| v.to_kind(k).ok()).map(|x| if k == Kind::Angle { x.to_degrees() } else { x }).map(expr::round_reported)
@@ -1036,23 +1044,34 @@ impl Document {
let mut names: BTreeSet<String> = f.param_names.iter().filter(|n| !n.is_empty()).cloned().collect();
if let FeatureKind::Sketch { sketch, .. } = &f.kind {
names.extend(sketch.constraints.iter().filter_map(|c| c.param.clone()));
- names.extend(sketch.patterns.iter().flat_map(|p| p.expressions.values()).flat_map(|e| expr::references(e)));
- names.extend(sketch.text_exprs().into_iter().flat_map(|(_, e)| expr::references(e)));
- names.extend(sketch.patterns.iter().filter_map(|p| p.count_expression.as_deref()).flat_map(expr::references));
- names.extend(sketch.text_word_exprs().into_iter().flat_map(|e| crate::text_params::references(e)));
- }
- // 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(|(f, e, _)| rule_references(f, e)));
- }
- if f.kind.is_plastic() {
- names.extend(self.plastic.rules.iter().flat_map(|r| r.fields()).flat_map(|(f, e, _)| rule_references(f, e)));
}
+ names.extend(self.feature_reads(f));
out.extend(names.into_iter().map(|n| (format!("Feature:{}", f.name), n)));
}
out
}
+ /// The parameters a feature reads through expressions it holds itself, not through a named
+ /// input or dimension of its own: a sketch's point coordinates, texts, equations and pattern
+ /// expressions, and the rules a sheet metal or plastic feature is built with.
+ fn feature_reads(&self, f: &crate::Feature) -> BTreeSet<String> {
+ let mut names = BTreeSet::new();
+ if let FeatureKind::Sketch { sketch, .. } = &f.kind {
+ names.extend(sketch.patterns.iter().flat_map(|p| p.expressions.values()).flat_map(|e| expr::references(e)));
+ names.extend(sketch.text_exprs().into_iter().flat_map(|(_, e)| expr::references(e)));
+ names.extend(sketch.patterns.iter().filter_map(|p| p.count_expression.as_deref()).flat_map(expr::references));
+ names.extend(sketch.text_word_exprs().into_iter().flat_map(|e| crate::text_params::references(e)));
+ }
+ // 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(|(f, e, _)| rule_references(f, e)));
+ }
+ if f.kind.is_plastic() {
+ names.extend(self.plastic.rules.iter().flat_map(|r| r.fields()).flat_map(|(f, e, _)| rule_references(f, e)));
+ }
+ names
+ }
+
/// Features whose result depends on a parameter (directly or through other parameters).
pub fn features_using_param(&self, name: &str) -> Vec<u64> {
let defs = self.param_defs();
diff --git a/crates/engine/src/cmd/parameters_tests.rs b/crates/engine/src/cmd/parameters_tests.rs
index c711ef5c5b..d277c696e1 100644
--- a/crates/engine/src/cmd/parameters_tests.rs
+++ b/crates/engine/src/cmd/parameters_tests.rs
@@ -198,6 +198,31 @@ fn deleting_a_used_parameter_lists_its_users() {
assert!(g["edges"].as_array().unwrap().iter().any(|e| e[0] == "Feature:Base" && e[1] == dim.as_str()), "{g}");
}
+/// #12293: a parameter that only a sketch point's coordinate reads lists that sketch in the
+/// parameter list's `used_by`, as `parameters.graph` and `parameters.users` already show it.
+#[test]
+fn a_parameter_read_by_a_sketch_point_lists_the_sketch_as_its_user() {
+ let mut s = Session::default();
+ run(&mut s, "parameters.add", json!({"name": "a", "expression": "20 mm"}));
+ run(&mut s, "parameters.add", json!({"name": "h", "expression": "5 mm"}));
+ run(&mut s, "sketch.create", json!({"plane": "XY", "name": "S"}));
+ run(&mut s, "sketch.line", json!({"points": [[0, 0], ["a", 0], ["a", 10], [0, 10]], "closed": true}));
+ run(&mut s, "sketch.finish", json!({}));
+ run(&mut s, "solid.extrude", json!({"sketch": "S", "distance": "h", "name": "E"}));
+ let g = run(&mut s, "parameters.graph", json!({}));
+ assert!(g["edges"].as_array().unwrap().iter().any(|e| e[0] == "Feature:S" && e[1] == "a"), "{g}");
+ let list = run(&mut s, "parameters.list", json!({"filter": "user"}));
+ assert_eq!(row(&list, "a")["used_by"], json!(["Feature:S"]), "{list}");
+ // `h` is read through the extrude's named input, not by the extrude itself.
+ let h = row(&list, "h")["used_by"].clone();
+ assert_eq!(h.as_array().map(Vec::len), Some(1), "{list}");
+ assert!(h[0].as_str().is_some_and(|n| n.starts_with('d')), "{list}");
+ // The sketch still reads `a` after `a` changes.
+ run(&mut s, "parameters.change", json!({"name": "a", "expression": "30 mm"}));
+ let list = run(&mut s, "parameters.list", json!({"filter": "user"}));
+ assert_eq!(row(&list, "a")["used_by"], json!(["Feature:S"]), "{list}");
+}
+
/// #4420: sheet metal and plastic rules and the plane of a canvas hold expressions too. Renaming a
/// parameter they use renames it there, and they count as its users (so it can't be deleted).
#[test]