Skip to content

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

Sketch: a feature on a failed sketch names that sketch (#9075) (#9276)

jeromehzl966b10e73136
diff --git a/crates/doc/src/eval.rs b/crates/doc/src/eval.rs
index 389e36a58a..678d3e2f35 100644
--- a/crates/doc/src/eval.rs
+++ b/crates/doc/src/eval.rs
@@ -2153,7 +2153,14 @@ impl Model {
feature_component.entry(f.id).or_insert(f.component);
}
let watched = crate::progress::begin(doc.features.len());
+ // The features that failed so far in this pass, by id: their names and errors (#9075).
+ let mut failed: std::collections::HashMap<u64, (String, String)> = std::collections::HashMap::new();
for (i, f) in doc.features.iter().enumerate() {
+ if let Some(r) = out.last()
+ && let Some(e) = &r.error
+ {
+ failed.insert(r.id, (r.name.clone(), e.clone()));
+ }
self.placement_local = self.placement_local && placement_reuse::local_geometry(f, &feature_component);
let rolled_back = i >= marker;
// Retain at most one active result per rolled-back feature, even across several
@@ -2213,6 +2220,33 @@ impl Model {
if state.reserved_body_names != reserved {
Arc::make_mut(&mut state).reserved_body_names.clone_from(&reserved);
}
+ // A sketch the feature is built from failed earlier in this pass: the feature fails naming
+ // that sketch, with its error, as one using a failed parameter does (#9075), not as a
+ // sketch that must come earlier in the timeline. Never cached: the cache key holds
+ // neither the sketch's name nor its error (a rename, another failing value).
+ let failed_sketch = if failed.is_empty() {
+ None
+ } else {
+ f.kind.input_sketches().into_iter().find_map(|id| failed.get(&id)).map(|(name, e)| format!("sketch `{name}` failed: {e}"))
+ };
+ if let Some(e) = failed_sketch {
+ recomputed += 1;
+ reuse = false;
+ out.push(FeatureResult {
+ id: f.id,
+ name: f.name.clone(),
+ error: Some(e),
+ warning: None,
+ boolean_diagnostics: Default::default(),
+ ms: 0.0,
+ skipped: false,
+ stale: false,
+ fingerprint: fp,
+ unkept: false,
+ state: state.clone(),
+ });
+ continue;
+ }
// The same feature on the same inputs: its earlier result.
let key =
cache_key(f, fp, fingerprint(doc, 0, f, &vals, false, &self.step_digests) ^ placement_digest(doc, f, Some(&state)), ctx, &state);
diff --git a/crates/engine/src/cmd/parameters_tests.rs b/crates/engine/src/cmd/parameters_tests.rs
index 4fcb624260..3c1fe01e16 100644
--- a/crates/engine/src/cmd/parameters_tests.rs
+++ b/crates/engine/src/cmd/parameters_tests.rs
@@ -1060,3 +1060,51 @@ fn a_definition_of_the_parameter_a_value_names_changes_it() {
let r = run(&mut s, "expr.evaluate", json!({"expression": "q = w * 2", "kind": "length"}));
assert_eq!((r["ok"].as_bool(), r["value"].as_f64(), r["defines"].as_str()), (Some(true), Some(60.0), Some("q")), "{r}");
}
+
+/// The error of the feature named `name` in the last evaluation.
+fn feature_error(s: &Session, name: &str) -> Option<String> {
+ s.model.results.iter().find(|r| r.name == name).and_then(|r| r.error.clone())
+}
+
+/// A sketch dimension set negative fails its sketch; the features built from it fail naming that
+/// sketch, not as a sketch that must come earlier in the timeline (#9075). Their errors follow
+/// the sketch's own (never an earlier one from the cache) and clear when the value is set back.
+#[test]
+fn a_feature_on_a_failed_sketch_names_the_sketch() {
+ let mut s = Session::default();
+ run(&mut s, "sketch.create", json!({"plane": "XY"}));
+ let out = run(
+ &mut s,
+ "sketch.draw.dimensioned",
+ json!({"command": "sketch.rectangle.center", "params": {"center": [0, 0], "corner": [20, 10]}, "dimensions": {"width": 40, "height": 20}}),
+ );
+ let width = out["dimensions"]["width"]["param"].as_str().unwrap_or_default().to_string();
+ run(&mut s, "sketch.finish", json!({}));
+ run(&mut s, "solid.extrude", json!({"distance": 5}));
+ // A healthy sketch and its extrude after them.
+ run(&mut s, "sketch.create", json!({"plane": "XY"}));
+ run(&mut s, "sketch.rectangle.two_point", json!({"p0": [50, 0], "p1": [60, 10]}));
+ run(&mut s, "sketch.finish", json!({}));
+ run(&mut s, "solid.extrude", json!({"sketch": "Sketch2", "distance": 5, "operation": "new"}));
+ assert!(rel(volume(&mut s), 40.0 * 20.0 * 5.0 + 500.0) < 1e-9);
+
+ let r = run(&mut s, "parameters.change", json!({"name": width, "expression": "-10"}));
+ let sketch = feature_error(&s, "Sketch1").unwrap_or_default();
+ assert!(sketch.contains("dimension value -10"), "{sketch}");
+ let extrude = feature_error(&s, "Extrude1").unwrap_or_default();
+ assert!(extrude.contains("`Sketch1`") && extrude.contains("dimension value -10"), "{extrude}");
+ assert!(!extrude.contains("earlier in the timeline"), "{extrude}");
+ assert_eq!((feature_error(&s, "Sketch2"), feature_error(&s, "Extrude2")), (None, None), "{r}");
+ // Another failing value: its own error, not the earlier one from the cache.
+ run(&mut s, "parameters.change", json!({"name": width, "expression": "-5"}));
+ let extrude = feature_error(&s, "Extrude1").unwrap_or_default();
+ assert!(extrude.contains("dimension value -5"), "{extrude}");
+ // The failed sketch renamed: its new name.
+ run(&mut s, "timeline.rename", json!({"feature": "Sketch1", "name": "Outline"}));
+ let extrude = feature_error(&s, "Extrude1").unwrap_or_default();
+ assert!(extrude.contains("`Outline`") && !extrude.contains("Sketch1"), "{extrude}");
+ // Set back: both build again.
+ let r = run(&mut s, "parameters.change", json!({"name": width, "expression": "40"}));
+ assert_eq!(r["errors"], json!([]), "{r}");
+ assert!(rel(volume(&mut s), 40.0 * 20.0 * 5.0 + 500.0) < 1e-9);
+}