diff --git a/crates/doc/src/eval.rs b/crates/doc/src/eval.rs index 85d7aa1c2..85b1448ae 100644 --- a/crates/doc/src/eval.rs +++ b/crates/doc/src/eval.rs @@ -1055,6 +1055,15 @@ impl EvalCache { EvalCache { budget: bytes, ..EvalCache::default() } } + /// Results stored. + pub fn len(&self) -> usize { + self.map.len() + } + + pub fn is_empty(&self) -> bool { + self.map.is_empty() + } + fn get(&mut self, key: u64) -> Option { let r = self.map.get(&key).cloned(); if r.is_some() { diff --git a/crates/engine/src/cmd/delete.rs b/crates/engine/src/cmd/delete.rs index ffa949f39..d7479ae80 100644 --- a/crates/engine/src/cmd/delete.rs +++ b/crates/engine/src/cmd/delete.rs @@ -274,6 +274,43 @@ mod tests { assert_eq!(done["would_fail"], json!(["Hole"]), "{done}"); } + /// Deleting a dimension of a sketch with a rolled-back history after it (#7253): the sketch + /// keeps its geometry, so the check before the delete does not build the features after the + /// marker again (that took minutes on a long history). A deleted line still breaks them, and + /// the check still says so. + #[test] + fn deleting_a_dimension_in_a_rolled_back_history_builds_nothing_after_the_marker() { + let mut s = Session::default(); + let sk = run(&mut s, "sketch.create", json!({"plane": "XY"}))["sketch"].as_u64().unwrap(); + run(&mut s, "sketch.line", json!({"points": [[0, 0], [40, 0], [30, 20], [10, 20]], "closed": true})); + let lines: Vec = s.model.state().sketch(sk).unwrap().sketch.curves.iter().map(|c| c.id.clone()).collect(); + let param = run(&mut s, "sketch.dimension", json!({"entities": [lines[0], lines[1]], "type": "angle"}))["param"].as_str().map(str::to_string); + let angle = s.model.state().sketch(sk).unwrap().sketch.constraints.iter().find(|c| param.is_some() && c.param == param).unwrap().id.clone(); + run(&mut s, "sketch.finish", json!({})); + run(&mut s, "solid.extrude", json!({"distance": 5, "name": "Plate"})); + let later = 6; + for i in 0..later { + run(&mut s, "solid.cylinder", json!({"base": [12 + 3 * i, 8, -1], "radius": 1, "height": 10, "operation": "cut"})); + } + run(&mut s, "timeline.roll_to", json!({"position": 1})); + run(&mut s, "sketch.edit", json!({"sketch": sk})); + let cached = |s: &Session| s.model.cache.lock().map(|c| c.len()).unwrap_or(0); + let before = cached(&s); + let doc = s.doc.clone(); + let items = json!([{"type": "sketch_constraint", "id": angle}]); + let dry = run(&mut s, "selection.delete", json!({"items": items, "dry_run": true})); + assert_eq!((&dry["deleted"], &dry["would_fail"]), (&json!([]), &json!([])), "{dry}"); + assert_eq!(*s.doc, *doc, "the check changes nothing"); + // The sketch itself is solved again; Plate and the cuts are not built. + let built = cached(&s).saturating_sub(before); + assert!(built < later, "{built} results built for the check"); + let line = json!([{"type": "sketch_curve", "id": lines[1]}]); + let dry = run(&mut s, "selection.delete", json!({"items": line, "dry_run": true})); + assert!(dry["would_fail"].as_array().unwrap().contains(&json!("Plate")), "{dry}"); + run(&mut s, "selection.delete", json!({"items": items})); + assert!(s.model.state().sketch(sk).unwrap().sketch.constraints.iter().all(|c| c.id != angle)); + } + #[test] fn mixed_selection_is_one_undo_step() { let (mut s, _) = part(); diff --git a/crates/engine/src/cmd/edit.rs b/crates/engine/src/cmd/edit.rs index 518e6944f..89772ca25 100644 --- a/crates/engine/src/cmd/edit.rs +++ b/crates/engine/src/cmd/edit.rs @@ -1222,14 +1222,15 @@ fn newly_broken(before: &solidcraft_doc::Model, after: &solidcraft_doc::Model) - /// (the model of `after_doc`). With the history rolled back, the features after the marker are /// evaluated too, on copies of the models (which share their cache): a delete made in the /// rolled-back history that breaks them says so before it is made, rather than leaving error marks -/// that only show at the end of the timeline (#3471). +/// that only show at the end of the timeline (#3471). A change that leaves them the same inputs +/// needs no such evaluation ([`only_constraints_changed`]). pub(super) fn newly_broken_in_history( before_doc: &solidcraft_doc::Document, before: &solidcraft_doc::Model, after_doc: &solidcraft_doc::Document, after: &solidcraft_doc::Model, ) -> Vec<(String, String)> { - if before_doc.marker.is_none() && after_doc.marker.is_none() { + if (before_doc.marker.is_none() && after_doc.marker.is_none()) || only_constraints_changed(before_doc, before, after_doc, after) { return newly_broken(before, after); } let whole = |doc: &solidcraft_doc::Document, model: &solidcraft_doc::Model| { @@ -1242,6 +1243,50 @@ pub(super) fn newly_broken_in_history( newly_broken(&whole(before_doc, before), &whole(after_doc, after)) } +/// Whether `after_doc` differs from `before_doc` only in sketch constraints and dimensions (and +/// model parameters nothing uses any more), every sketch whose constraints changed solving to +/// the same geometry, profiles and ends as before. The features after the marker are then built +/// from the same inputs, so evaluating the whole history twice would find nothing new (#7253): +/// deleting one dimension of a sketch with a long rolled-back history after it took minutes. +fn only_constraints_changed( + before_doc: &solidcraft_doc::Document, + before: &solidcraft_doc::Model, + after_doc: &solidcraft_doc::Document, + after: &solidcraft_doc::Model, +) -> bool { + let without_constraints = |doc: &solidcraft_doc::Document| { + let mut doc = doc.clone(); + for f in &mut doc.features { + if let FeatureKind::Sketch { sketch, .. } = &mut f.kind { + sketch.constraints.clear(); + } + } + doc + }; + let (mut b, a) = (without_constraints(before_doc), without_constraints(after_doc)); + if b.params.iter().any(|p| !a.params.contains(p) && (!p.model || !after_doc.param_users(&p.name).is_empty())) { + return false; + } + b.params.retain(|p| a.params.contains(p)); + if b != a { + return false; + } + // What a later feature reads of a sketch: the solved geometry, its plane, profiles and ends. + let solved = |model: &solidcraft_doc::Model, id: u64| { + let r = model.result(id).filter(|r| !r.skipped && r.error.is_none())?; + let s = r.state.sketch(id)?; + let mut sketch = s.sketch.clone(); + sketch.constraints.clear(); + Some((s.plane, sketch, serde_json::to_value(&s.profiles).ok()?, s.ends.clone(), s.report.ok())) + }; + before_doc.features.iter().zip(&after_doc.features).all(|(fb, fa)| match (&fb.kind, &fa.kind) { + (FeatureKind::Sketch { sketch: sb, .. }, FeatureKind::Sketch { sketch: sa, .. }) if sb.constraints != sa.constraints => { + solved(before, fb.id).is_some_and(|x| solved(after, fa.id).is_some_and(|y| x == y)) + } + _ => true, + }) +} + fn reorder(s: &mut Session, p: &Value) -> Result { let cmd = "timeline.reorder"; let id = feature_id(s, p.get("feature"), cmd)?;