From 91aefd5abcc49689cc765fd36a83623440c1b258 Mon Sep 17 00:00:00 2001 From: Dahak Builds Date: Sat, 31 Oct 2026 04:08:20 +0000 Subject: [PATCH] Refuse sketch values that collapse a curve in settle_value_edit (#9729) (#9837) * claim: #9729 * Refuse value edits that collapse a sketch curve: settle_value_edit runs settle's shrunk/flung checks (#9729) * rustfmt --------- Co-authored-by: Dahak Builds --- crates/engine/src/cmd/mod.rs | 1 + crates/engine/src/cmd/parameters_tests.rs | 50 +++++++++++++++++++++++ crates/engine/src/cmd/sketch.rs | 15 +++++++ crates/engine/src/lib.rs | 17 +++++++- 4 files changed, 82 insertions(+), 1 deletion(-) diff --git a/crates/engine/src/cmd/mod.rs b/crates/engine/src/cmd/mod.rs index 10f999046..4ec05544d 100644 --- a/crates/engine/src/cmd/mod.rs +++ b/crates/engine/src/cmd/mod.rs @@ -92,6 +92,7 @@ pub(crate) use sheet::{ mod silhouette; mod sketch; pub(crate) use sketch::author_path_for_rebind; +pub(crate) use sketch::collapse_reason; pub(crate) use sketch::created as sketch_created; mod canvas_sketch; mod sketch_3d; diff --git a/crates/engine/src/cmd/parameters_tests.rs b/crates/engine/src/cmd/parameters_tests.rs index 70027a330..04642c6bc 100644 --- a/crates/engine/src/cmd/parameters_tests.rs +++ b/crates/engine/src/cmd/parameters_tests.rs @@ -1145,3 +1145,53 @@ fn a_feature_on_a_failed_sketch_names_the_sketch() { assert_eq!(r["errors"], json!([]), "{r}"); assert!(rel(volume(&mut s), 40.0 * 20.0 * 5.0 + 500.0) < 1e-9); } + +/// A finished sketch with one line (0,0)-(30,0) whose length is dimensioned to parameter `L`. +fn line_dimensioned_by_a_parameter() -> (Session, u64) { + let mut s = Session::default(); + run(&mut s, "parameters.add", json!({"name": "L", "expression": "30"})); + run(&mut s, "sketch.create", json!({"plane": "XY"})); + run(&mut s, "sketch.line", json!({"points": [[0, 0], [30, 0]]})); + run(&mut s, "sketch.dimension", json!({"entities": ["l1"], "value": "L"})); + run(&mut s, "sketch.finish", json!({})); + let feature = s.doc.find_feature("Sketch1").map(|f| f.id).unwrap_or_default(); + (s, feature) +} + +/// #9729: `sketch.dimension` refuses 0 ("that would shrink l1 to a point") but a value edit +/// let it through: the line collapsed to a point and every later value counted as a conflict, +/// so only Undo recovered — and after a save and reopen there is no Undo. A value edit now +/// runs the same shrink/fling checks `settle` runs, so the zero is refused where it happens. +#[test] +fn a_parameter_that_shrinks_a_dimensioned_line_to_a_point_is_refused() { + let (mut s, feature) = line_dimensioned_by_a_parameter(); + let e = err(&mut s, "parameters.change", json!({"name": "L", "expression": "0"})); + assert!(e.contains("shrink l1 to a point") && e.contains("the values were not changed"), "{e}"); + // The line is where it was and the sketch still solves. + assert!((far_x(&s, feature) - 30.0).abs() < 1e-6); + assert!(s.model.state().sketch(feature).unwrap().report.keeps_orientation()); + // Other values still work, in both directions: the sketch was never stored collapsed. + run(&mut s, "parameters.change", json!({"name": "L", "expression": "20"})); + assert!((far_x(&s, feature) - 20.0).abs() < 1e-6); + run(&mut s, "parameters.change", json!({"name": "L", "expression": "30"})); + assert!((far_x(&s, feature) - 30.0).abs() < 1e-6); +} + +/// The largest x among the stored sketch's points: the line's far end. +fn far_x(s: &Session, feature: u64) -> f64 { + stored_points(s, feature).iter().map(|p| p.x).fold(f64::MIN, f64::max) +} + +/// The same through a configuration row: activating a row whose value collapses the line is +/// refused, and the Default row can still be activated afterwards. +#[test] +fn a_configuration_row_that_shrinks_a_dimensioned_line_to_a_point_is_refused() { + let (mut s, feature) = line_dimensioned_by_a_parameter(); + run(&mut s, "config.configure", json!({"params": ["L"]})); + run(&mut s, "config.row", json!({"name": "Zero", "values": {"param:L": "0"}})); + let e = err(&mut s, "config.activate", json!({"row": "Zero"})); + assert!(e.contains("shrink l1 to a point"), "{e}"); + // The Default row still activates and the line is untouched. + run(&mut s, "config.activate", json!({"row": "Default"})); + assert!((far_x(&s, feature) - 30.0).abs() < 1e-6); +} diff --git a/crates/engine/src/cmd/sketch.rs b/crates/engine/src/cmd/sketch.rs index 5668c7144..19ae602bd 100644 --- a/crates/engine/src/cmd/sketch.rs +++ b/crates/engine/src/cmd/sketch.rs @@ -373,6 +373,21 @@ fn shrunk(before: &Sketch, after: &Sketch) -> Option { /// dimension value) has thrown the geometry to a far solution. const FLUNG: f64 = 1000.0; +/// Why a solve from `before` into `after` collapses the sketch, in the words `settle` uses to +/// refuse an edit's solve: a line or circle shrunk to a point, or points thrown far away. +/// `None` when the solved shape is one the edit can mean. Value edits (`Session::settle_value_edit`) +/// run the same checks, so a parameter, a configuration row or the dimension editor cannot +/// store a sketch whose curve has been squeezed away and which can never be solved back. +pub(crate) fn collapse_reason(before: &Sketch, after: &Sketch) -> Option { + if let Some(curve) = shrunk(before, after) { + return Some(format!("that would shrink {curve} to a point: its constraints can only be met without it")); + } + if flung(before, after) { + return Some("that would throw points of the sketch far away: its constraints have no solution near the sketch as it is".into()); + } + None +} + /// Did solving `before` into `after` throw some point further than [`FLUNG`] times the sketch's /// size, or to no number at all? fn flung(before: &Sketch, after: &Sketch) -> bool { diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index 47d76e057..e17c2857d 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -1231,6 +1231,10 @@ impl Session { /// as before. Otherwise store each sketch whose values changed as solved, so the next edit /// continues from it (see `Model::adopt_solved_sketches`). pub(crate) fn settle_value_edit(&mut self, solved_before: &SketchValues) -> Result<()> { + // The solved geometry each sketch had before the edit, for the shrink/fling checks + // after the refresh below (the same checks `settle` runs on an edit's own solve). + let solved_geometry: std::collections::BTreeMap = + self.model.results.iter().filter_map(|r| r.state.sketch(r.id).map(|k| (r.id, k.sketch.clone()))).collect(); // Quantity changes alter topology, so store the regenerated instances in the same // transaction as the parameter edit. Later picks and undo must see those same ids. let mut updated: Vec<_> = self @@ -1259,10 +1263,21 @@ impl Session { self.refresh(); for r in &self.model.results { let (Some(before), Some(k)) = (solved_before.get(&r.id), r.state.sketch(r.id)) else { continue }; - if !k.report.keeps_orientation() && own_values(&k.sketch) != *before { + if own_values(&k.sketch) == *before { + continue; + } + if !k.report.keeps_orientation() { let why = k.report.failure(|f| format!("its constraints would conflict ({f})")); return Err(EngineError::Other(format!("sketch `{}`: {why}; the values were not changed", r.name))); } + // The value changed this sketch and it still solves: refuse it the way `settle` + // refuses an edit's solve when the curve was squeezed away or points thrown far, + // so a zero (or wild) value is never stored as a sketch that can't be solved back. + if let Some(before_sketch) = solved_geometry.get(&r.id) + && let Some(why) = cmd::collapse_reason(before_sketch, &k.sketch) + { + return Err(EngineError::Other(format!("sketch `{}`: {why}; the values were not changed", r.name))); + } } // Checked first: storing makes the document unique (a copy when the undo step shares it). if self.model.edited_sketches(&self.doc).next().is_some() {