Skip to content

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

Timeline: deleting a sketch entity reuses the results it replaced (#3923) (#11283)

Bartosz Błachutdb20c8be232e
diff --git a/crates/doc/src/eval.rs b/crates/doc/src/eval.rs
index c386a7639f..2403b45830 100644
--- a/crates/doc/src/eval.rs
+++ b/crates/doc/src/eval.rs
@@ -784,7 +784,7 @@ impl ModelState {
let mut h = std::collections::hash_map::DefaultHasher::new();
serde_json::to_string(&s.plane).unwrap_or_default().hash(&mut h);
let mut sketch = s.sketch.clone();
- quantize(&mut sketch);
+ fingerprint_sketch(&mut sketch);
serde_json::to_string(&sketch).unwrap_or_default().hash(&mut h);
s.report.ok().hash(&mut h);
self.sketch_keys.insert(feature, h.finish());
@@ -1253,6 +1253,13 @@ fn quantize(sk: &mut solidcraft_sketch::Sketch) {
}
}
+/// The stored solve as a cache key: on the [`QUANTUM`] grid, without the editor state
+/// (see [`solidcraft_sketch::Sketch::clear_id_counters`]).
+fn fingerprint_sketch(sk: &mut solidcraft_sketch::Sketch) {
+ quantize(sk);
+ sk.clear_id_counters();
+}
+
/// A feature as fingerprinted: its definition, a sketch's stored solve quantized, an imported
/// file's text or mesh payload by its content digest.
/// Hash [`feature_json`] of `f` into `h`. Every evaluation fingerprints every feature, and in a
@@ -1319,7 +1326,7 @@ fn feature_json(f: &Feature, steps: &ImportDigests) -> String {
if let FeatureKind::Sketch { .. } = &f.kind {
let mut g = f.clone();
if let FeatureKind::Sketch { sketch, .. } = &mut g.kind {
- quantize(sketch);
+ fingerprint_sketch(sketch);
}
return serde_json::to_string(&g).unwrap_or_default();
}
diff --git a/crates/doc/src/eval_cost_tests.rs b/crates/doc/src/eval_cost_tests.rs
index 0ae6fa90a1..3ac7b436e0 100644
--- a/crates/doc/src/eval_cost_tests.rs
+++ b/crates/doc/src/eval_cost_tests.rs
@@ -1759,3 +1759,30 @@ fn mesh_metadata_and_order_remain_part_of_the_fingerprint() {
same_as_fresh(&doc, &model);
}
}
+
+#[test]
+fn an_added_and_deleted_sketch_entity_keeps_its_fingerprint() {
+ let mut doc = Document::new("sketch fingerprint");
+ let id = doc.add_feature(line_sketch(PlaneRef::Origin { name: "XY".into() }, Vec2::new(10.0, 0.0)), None).unwrap();
+ let mut model = Model::new();
+ model.evaluate(&doc);
+ let key = model.state().sketch_keys.get(&id).copied();
+ let json = super::feature_json(doc.feature(id).unwrap(), &super::ImportDigests::default());
+
+ // Make and delete an id: the counters advance, but the geometry is the same.
+ {
+ let sk = doc.sketch_mut(id).unwrap();
+ let a = sk.add_point(Vec2::new(2.0, 2.0), None).unwrap();
+ let b = sk.add_point(Vec2::new(3.0, 2.0), None).unwrap();
+ let line = sk.add_line_pts(a, b, None).unwrap();
+ sk.remove_curves(&[line]);
+ sk.remove_points(&[b]);
+ sk.remove_points(&[a]);
+ }
+
+ let json_after = super::feature_json(doc.feature(id).unwrap(), &super::ImportDigests::default());
+ assert_eq!(json, json_after, "deleted ids are not geometry");
+ model.evaluate(&doc);
+ assert_eq!(model.state().sketch_keys.get(&id).copied(), key);
+ assert_eq!((model.last_recomputed, model.last_restored), (0, 0));
+}
diff --git a/crates/doc/src/tests.rs b/crates/doc/src/tests.rs
index dca6b1fa84..091bd24683 100644
--- a/crates/doc/src/tests.rs
+++ b/crates/doc/src/tests.rs
@@ -105,6 +105,35 @@ fn set_back_reports_restored_features() {
assert_eq!((m.last_recomputed, m.last_restored), (0, 0));
}
+/// Adding a line and deleting it again leaves the solved geometry as it was: the extrude and
+/// everything after it take their earlier results from the cache.
+#[test]
+fn a_sketch_line_added_and_deleted_restores_the_extrude() {
+ let (mut doc, s) = plate_doc();
+ let mut m = Model::new();
+ m.evaluate(&doc);
+ assert_eq!((m.last_recomputed, m.last_restored), (2, 0));
+
+ let (a, b, line) = {
+ let sk = doc.sketch_mut(s).unwrap();
+ let a = sk.add_point(Vec2::new(5.0, 5.0), None).unwrap();
+ let b = sk.add_point(Vec2::new(6.0, 5.0), None).unwrap();
+ (a, b, sk.add_line_pts(a, b, None).unwrap())
+ };
+ m.evaluate(&doc);
+ assert_eq!((m.last_recomputed, m.last_restored), (2, 0));
+
+ {
+ let sk = doc.sketch_mut(s).unwrap();
+ sk.remove_curves(&[line]);
+ sk.remove_points(&[b]);
+ sk.remove_points(&[a]);
+ }
+ m.evaluate(&doc);
+ assert_eq!((m.last_recomputed, m.last_restored), (0, 2));
+ assert!(rel(measure(&m.state().bodies[0].body).unwrap().volume, 24000.0) < 1e-9);
+}
+
/// Setting a parameter back restores every feature on a design whose states are large next to
/// the cache's budget: each cached state shares the bodies it did not change with the states
/// before it, so it is charged only for what it made, not for the whole model again.
diff --git a/crates/sketch/src/model.rs b/crates/sketch/src/model.rs
index 2503482d77..938db93617 100644
--- a/crates/sketch/src/model.rs
+++ b/crates/sketch/src/model.rs
@@ -802,6 +802,14 @@ impl Sketch {
}
}
+ /// Forget the id counters a fingerprint must not see: they advance with every id made,
+ /// including ones since deleted. The document keeps them; only a copy about to be hashed
+ /// calls this. The display options stay, because `three_d` and the `hide_*` flags change
+ /// what the evaluated sketch does.
+ pub fn clear_id_counters(&mut self) {
+ self.counters.clear();
+ }
+
/// A new unused id with the given prefix (`l` → `l3`).
pub fn fresh(&mut self, prefix: &str) -> Result<String> {
let mut n = self.counters.get(prefix).copied().unwrap_or(0);