Skip to content

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

Redefine: the features after the edited one are computed once (#7076)

Kai Evans17339cf65cef
diff --git a/crates/doc/src/eval.rs b/crates/doc/src/eval.rs
index 988876a117..5b410c68fe 100644
--- a/crates/doc/src/eval.rs
+++ b/crates/doc/src/eval.rs
@@ -1047,6 +1047,12 @@ const CACHE_ENTRIES: usize = 4096;
const CACHE_BYTES: usize = 64 << 20;
impl EvalCache {
+ /// A cache that keeps `bytes` of results (always at least the last one): a small budget
+ /// stands in for a design whose states are large.
+ pub fn with_budget(bytes: usize) -> Self {
+ EvalCache { budget: bytes, ..EvalCache::default() }
+ }
+
fn get(&mut self, key: u64) -> Option<CachedEval> {
let r = self.map.get(&key).cloned();
if r.is_some() {
diff --git a/crates/doc/src/lib.rs b/crates/doc/src/lib.rs
index ac4fefdc08..327918ef23 100644
--- a/crates/doc/src/lib.rs
+++ b/crates/doc/src/lib.rs
@@ -73,7 +73,6 @@ pub use assembly::{
mat_mul, rigid,
};
pub use document::*;
-pub use eval::KeptResults;
pub use eval::{
EdgeRef, FeatureResult, Model, ModelBody, ModelState, PlacedBodies, RevolveExtent, SavedModel, SolvedSketch, ThreadInfo, curve_pick_error,
edge_names_for, edge_refs, face_point_names, face_point_state, hole_direction_at, import_body_names, map_wire, metric_thread_sizes,
@@ -81,6 +80,7 @@ pub use eval::{
sketch_line_plane, state_in_frame, state_in_frame_at, suggested_thread, tangent_face_chain, through_all_distance, world_state,
world_state_reusing,
};
+pub use eval::{EvalCache, KeptResults};
pub use expr::Expr;
pub use names::{BodyName, FeatureId, FeatureName, SketchId, copy_name};
pub use params::{ParamRow, ParamUser, cycles};
diff --git a/crates/engine/src/cmd/edit.rs b/crates/engine/src/cmd/edit.rs
index 2a6e00e98e..9c677d1928 100644
--- a/crates/engine/src/cmd/edit.rs
+++ b/crates/engine/src/cmd/edit.rs
@@ -1440,6 +1440,11 @@ fn redefine(s: &mut Session, p: &Value) -> Result<Value> {
// Run the constructor on a scratch session: failed edits must not disturb sketch mode,
// selections, component authoring state or the original feature's definition.
let mut trial = s.scratch();
+ // What the trial computes is kept for the session's own evaluation at the end: the cache may
+ // hold only the last few states of a large design, which would be computed twice.
+ let kept = s.model.kept();
+ let shared = kept.clone().unwrap_or_default();
+ trial.model.keep(Some(shared.clone()));
trial.active_component = old.component;
if let FeatureKind::Emboss { face: Some(face), .. } = &old.kind {
trial.active_occurrence_path = (!face.author_path.is_empty()).then(|| face.author_path.clone());
@@ -1549,7 +1554,9 @@ fn redefine(s: &mut Session, p: &Value) -> Result<Value> {
}
s.pending_moves = trial.pending_moves;
s.doc = trial.doc;
+ s.model.keep(Some(shared));
s.refresh();
+ s.model.keep(kept);
if let Some(e) = s.model.result(id).and_then(|r| r.error.clone()) {
return Err(EngineError::Other(e));
}
diff --git a/crates/engine/src/cmd/edit_tests.rs b/crates/engine/src/cmd/edit_tests.rs
index 01d8317eb3..ad75da6378 100644
--- a/crates/engine/src/cmd/edit_tests.rs
+++ b/crates/engine/src/cmd/edit_tests.rs
@@ -144,6 +144,24 @@ fn redefining_a_blend_replaces_persistent_edge_selection_and_survives_resize() {
assert!(s.model.result(old.id).unwrap().warning.is_none());
}
+/// A redefine computes the features after the edited one once: its trial's results are kept
+/// for the session's own evaluation, though the cache holds only the last state (a large
+/// design's).
+#[test]
+fn redefining_computes_the_later_features_once() {
+ let mut s = Session::default();
+ run(&mut s, "solid.box", json!({"length": 20, "width": 10, "height": 5, "name": "Base"}));
+ for x in [30, 40, 50] {
+ run(&mut s, "solid.box", json!({"length": 5, "width": 5, "height": 5, "corner": [x, 0, 0]}));
+ }
+ *s.model.cache.lock().unwrap() = solidcraft_doc::EvalCache::with_budget(1);
+ let reply =
+ run(&mut s, "timeline.redefine", json!({"feature": "Base", "command": "solid.box", "params": {"length": 25, "width": 10, "height": 5}}));
+ assert_eq!(reply["recomputed"], 0, "{reply}");
+ let volume = run(&mut s, "inspect.measure", json!({}))["total"]["volume_mm3"].as_f64().unwrap();
+ assert!((volume - (25.0 * 10.0 * 5.0 + 3.0 * 125.0)).abs() < 1e-6, "{volume}");
+}
+
#[test]
fn redefining_preserves_body_names_participants_and_marker_without_temporary_features() {
let mut s = Session::default();