From 2e981d66e89d6047f54f13c3bae7cb3a3f57dacb Mon Sep 17 00:00:00 2001 From: Colin Faulkingham Date: Sat, 31 Oct 2026 16:04:29 +0000 Subject: [PATCH] Sketch: allow dimension edits with tangent caps (#4823) (#9494) * claim: #4823 * fix: resize sketches with tangent offset caps (#4823) --- crates/doc/src/document.rs | 8 + crates/engine/src/cmd/parameters.rs | 13 +- crates/engine/src/cmd/sketch_modify_tests.rs | 161 +++++++++++++++++++ crates/sketch/src/offset.rs | 74 +++++++++ 4 files changed, 255 insertions(+), 1 deletion(-) diff --git a/crates/doc/src/document.rs b/crates/doc/src/document.rs index 1e0a63399..e41ceaeb1 100644 --- a/crates/doc/src/document.rs +++ b/crates/doc/src/document.rs @@ -4211,6 +4211,14 @@ impl Document { } else { solidcraft_sketch::solve_after_edit_with_analysis(sk, stored, was(sk).as_deref(), analysis) }; + // An attachment to the old offset can prevent the source from moving at all. + // Retry from a source-only seed, adopting it only if every constraint still holds. + if !report.ok() + && report.oversized.is_none() + && let Some(retried) = sk.solve_after_offset_edit(stored, was(sk).as_deref(), analysis) + { + report = retried; + } } Ok(report) } diff --git a/crates/engine/src/cmd/parameters.rs b/crates/engine/src/cmd/parameters.rs index 3f22f176b..d8b89d0e5 100644 --- a/crates/engine/src/cmd/parameters.rs +++ b/crates/engine/src/cmd/parameters.rs @@ -230,7 +230,18 @@ fn keep_dimension_start(s: &mut Session, name: &str) { } let reference: Vec<_> = stored.points.iter().map(|p| p.pos).collect(); let hold = Hold { points: vec![start], radii: Vec::new() }; - if !solve_with(&mut trial, &hold, Some(&reference), Analysis::StatusOnly, None).keeps_orientation() { + let solved = if trial.offsets.is_empty() && trial.freeform_offsets.is_empty() { + solve_with(&mut trial, &hold, Some(&reference), Analysis::StatusOnly, None).keeps_orientation() + } else { + // A solve against stale offset points can stretch an attached cap into a different + // tangency branch. Keep the start through the complete solve/refresh sequence. + let Some(p) = trial.points.get_mut(start) else { continue }; + p.fixed = true; + s.doc + .solve_sketch_with_analysis(&vals, &mut trial, &stored.driving_values(), Some(stored), Analysis::StatusOnly) + .is_ok_and(|report| report.keeps_orientation()) + }; + if !solved { continue; } if stored.points.len() == trial.points.len() { diff --git a/crates/engine/src/cmd/sketch_modify_tests.rs b/crates/engine/src/cmd/sketch_modify_tests.rs index e5c8e6ee6..83f3c18f4 100644 --- a/crates/engine/src/cmd/sketch_modify_tests.rs +++ b/crates/engine/src/cmd/sketch_modify_tests.rs @@ -2420,6 +2420,167 @@ fn an_offset_slot_still_moves_with_the_dimension_that_places_it() { } } +fn capped_line_offset(fixed_cap: bool) -> Session { + let mut s = Session::default(); + run( + &mut s, + "timeline.add", + json!({ + "feature": {"id":1,"name":"Capped offset","type":"sketch","plane":{"type":"origin","name":"XY"},"sketch":{ + "points":[ + {"id":"origin","pos":[0,0],"fixed":true}, + {"id":"end","pos":[0,50],"fixed":false}, + {"id":"oa","pos":[-2,0],"fixed":true}, + {"id":"ob","pos":[-2,50],"fixed":true}, + {"id":"center","pos":[-1,50],"fixed":false}, + {"id":"cap_end","pos":[-2,50],"fixed":fixed_cap} + ], + "curves":[ + {"id":"source","type":"line","a":0,"b":1}, + {"id":"offset","type":"line","a":2,"b":3}, + {"id":"cap","type":"arc","c":4,"a":1,"b":5} + ], + "constraints":[ + {"id":"vertical","type":"vertical","l":0}, + {"id":"length","type":"length","l":0,"value":50,"param":"height"}, + {"id":"source_tangent","type":"tangent","a":0,"b":2}, + {"id":"offset_tangent","type":"tangent","a":1,"b":2}, + {"id":"end_join","type":"coincident","p":3,"q":5} + ], + "offsets":[{"sources":[["source",true]],"outputs":["offset"],"distance":2,"closed":false}] + }}, + "params":[{"name":"height","expression":"50","unit":"mm"}] + }), + ); + run(&mut s, "sketch.edit", json!({"sketch":1})); + s +} + +#[test] +fn a_tangent_cap_follows_its_offset_when_the_source_length_changes() { + // #4823: the cap joins the source to the offset's held endpoint. Solving against the old + // endpoint first makes every length change look contradictory, before the offset can move. + let mut s = capped_line_offset(false); + let check = |s: &Session, height: f64| { + let world = s.world_state(); + let solved = world.sketch(1).unwrap(); + assert!(solved.report.keeps_orientation(), "{:?}", solved.report); + assert!(solved.report.fully_constrained()); + let sk = &solved.sketch; + for (id, expected) in [ + ("origin", Vec2::ZERO), + ("end", Vec2::new(0.0, height)), + ("oa", Vec2::new(-2.0, 0.0)), + ("ob", Vec2::new(-2.0, height)), + ("center", Vec2::new(-1.0, height)), + ("cap_end", Vec2::new(-2.0, height)), + ] { + let point = sk.points.iter().find(|p| p.id == id).unwrap(); + assert!(point.pos.dist(expected) < 1e-6, "{id}: {:?}, expected {expected:?}", point.pos); + } + assert_eq!(sk.constraints.len(), 5); + assert_eq!(sk.offsets.len(), 1); + }; + check(&s, 50.0); + let mut previous = 50.0; + for height in [60.0, 40.0, 70.0] { + run(&mut s, "parameters.change", json!({"name":"height","expression":height.to_string()})); + check(&s, height); + run(&mut s, "edit.undo", json!({})); + check(&s, previous); + run(&mut s, "edit.redo", json!({})); + check(&s, height); + previous = height; + } + let reopened = Session::new(solidcraft_doc::Document::from_json(&s.doc.to_json()).unwrap()); + check(&reopened, 70.0); +} + +#[test] +fn an_offset_cap_fixed_at_the_old_height_still_refuses_a_length_change() { + let mut s = capped_line_offset(true); + let before = s.doc.to_json(); + let undo = s.undo.len(); + assert!(s.execute("parameters.change", &json!({"name":"height","expression":"60"})).is_err()); + assert_eq!(s.doc.to_json(), before); + assert_eq!(s.undo.len(), undo); +} + +#[test] +fn a_cap_on_an_offset_arc_keeps_its_radius_through_large_length_edits() { + // Two externally tangent source arcs: radii 3 and 6, centres separated by (4, -sqrt(65)). + // Their inward/outward offsets share those centres; a radius-1 cap closes the 2 mm gap. + // The preliminary keep-start solve used to stretch that cap onto another tangency branch. + let dy = 65.0_f64.sqrt(); + let end_dy = 11.0_f64.sqrt(); + for batch in [false, true] { + let mut s = Session::default(); + run( + &mut s, + "timeline.add", + json!({ + "feature":{"id":1,"name":"Curved cap","type":"sketch","plane":{"type":"origin","name":"XY"},"sketch":{ + "points":[ + {"id":"origin","pos":[0,0],"fixed":true}, + {"id":"end","pos":[0,50]}, + {"id":"bend_center","pos":[3,50]}, + {"id":"bend_join","pos":[3.0+4.0/3.0,50.0-dy/3.0]}, + {"id":"tip_center","pos":[7,50.0-dy]}, + {"id":"tip_end","pos":[2,50.0-dy-end_dy]}, + {"id":"oa","pos":[-2,0],"fixed":true}, + {"id":"ob","pos":[-2,50],"fixed":true}, + {"id":"offset_join","pos":[3.0+20.0/9.0,50.0-dy*5.0/9.0],"fixed":true}, + {"id":"offset_end","pos":[7.0-10.0/3.0,50.0-dy-end_dy*2.0/3.0],"fixed":true}, + {"id":"cap_center","pos":[7.0-25.0/6.0,50.0-dy-end_dy*5.0/6.0]}, + {"id":"cap_end","pos":[7.0-10.0/3.0,50.0-dy-end_dy*2.0/3.0]} + ], + "curves":[ + {"id":"source","type":"line","a":0,"b":1}, + {"id":"bend","type":"arc","c":2,"a":3,"b":1}, + {"id":"tip","type":"arc","c":4,"a":3,"b":5}, + {"id":"offset","type":"line","a":6,"b":7}, + {"id":"bend_offset","type":"arc","c":2,"a":8,"b":7}, + {"id":"tip_offset","type":"arc","c":4,"a":8,"b":9}, + {"id":"cap","type":"arc","c":10,"a":5,"b":11} + ], + "constraints":[ + {"id":"vertical","type":"vertical","l":0}, + {"id":"length","type":"length","l":0,"value":50,"param":"height"}, + {"id":"bend_tangent","type":"tangent","a":0,"b":1}, + {"id":"tip_tangent","type":"tangent","a":1,"b":2}, + {"id":"tip_distance","type":"line_circle_distance","l":0,"c":2,"value":1}, + {"id":"bend_radius","type":"radius","c":1,"value":3}, + {"id":"tip_radius","type":"radius","c":2,"value":6}, + {"id":"end_distance","type":"point_line_distance","p":5,"l":0,"value":2}, + {"id":"cap_tangent","type":"tangent","a":2,"b":6}, + {"id":"end_join","type":"coincident","p":9,"q":11} + ], + "offsets":[{"sources":[["source",true],["bend",false],["tip",true]], + "outputs":["offset","bend_offset","tip_offset"],"distance":2,"closed":false}] + }}, + "params":[{"name":"height","expression":"50","unit":"mm"}] + }), + ); + run(&mut s, "sketch.edit", json!({"sketch":1})); + let initial = sketch(&s); + for height in [55.0, 60.0, 70.0, 40.0] { + let params = + if batch { json!({"changes":{"height":height.to_string()}}) } else { json!({"name":"height","expression":height.to_string()}) }; + run(&mut s, "parameters.change", params); + let world = s.world_state(); + let solved = world.sketch(1).unwrap(); + assert!(solved.report.keeps_orientation() && solved.report.fully_constrained(), "{:?}", solved.report); + for (p, before) in solved.sketch.points.iter().zip(&initial.points) { + let shift = if matches!(p.id.as_str(), "origin" | "oa") { 0.0 } else { height - 50.0 }; + let expected = before.pos + Vec2::new(0.0, shift); + assert!(p.pos.dist(expected) < 1e-6, "batch={batch}, height={height}, {}: {:?}, expected {expected:?}", p.id, p.pos); + assert_eq!(p.fixed, before.fixed, "temporary source holds must not be saved"); + } + assert_eq!(solved.sketch.constraints.len(), 10); + } + } +} + #[test] fn offset_driver_covers_disconnected_selections_and_refuses_bad_values_atomically() { let mut s = new_sketch(); diff --git a/crates/sketch/src/offset.rs b/crates/sketch/src/offset.rs index 23b013dab..5378f3472 100644 --- a/crates/sketch/src/offset.rs +++ b/crates/sketch/src/offset.rs @@ -115,6 +115,80 @@ impl Sketch { } } + /// Retry a failed solve whose source edit was blocked by attachments to an offset's old + /// fixed points. Seed the sources without those attachments, rebuild the offsets, then + /// solve every attachment with the sources held. Nothing is adopted unless the complete + /// sketch solves with its original constraints and orientation. + pub fn solve_after_offset_edit( + &mut self, + old: &crate::DrivingValues, + reference: Option<&[Vec2]>, + analysis: crate::Analysis, + ) -> Option { + let source = + |id: &str| self.offsets.iter().any(|o| o.sources.iter().any(|(s, _)| s == id)) || self.freeform_offsets.iter().any(|o| o.source == id); + let output = |id: &str| self.offsets.iter().any(|o| o.owns_output(id)) || self.freeform_offsets.iter().any(|o| o.output == id); + let source_points: std::collections::BTreeSet = + self.curves.iter().filter(|c| source(&c.id)).flat_map(|c| c.kind.point_ids()).collect(); + let outputs: std::collections::BTreeSet = self.curves.iter().enumerate().filter(|(_, c)| output(&c.id)).map(|(i, _)| i).collect(); + let output_points: std::collections::BTreeSet = + outputs.iter().filter_map(|i| self.curves.get(*i)).flat_map(|c| c.kind.point_ids()).filter(|p| !source_points.contains(p)).collect(); + let mut candidate = self.clone(); + candidate.constraints.retain(|c| { + !c.kind.indices().1.iter().any(|i| outputs.contains(i)) && !self.constraint_points(&c.kind).iter().any(|p| output_points.contains(p)) + }); + if candidate.constraints.len() == self.constraints.len() { + return None; + } + let before: Vec = reference.map_or_else(|| self.points.iter().map(|p| p.pos).collect(), <[Vec2]>::to_vec); + if !crate::solve_after_edit_with_analysis(&mut candidate, old, Some(&before), crate::Analysis::StatusOnly).keeps_orientation() { + return None; + } + candidate.constraints.clone_from(&self.constraints); + candidate.refresh_offsets().ok()?; + // Keep the freshly rebuilt offset exact while its attached geometry catches up. + // Refresh may retire output curves or refine a dependent spline: resolve holds again. + let hold = crate::Hold { + points: candidate.curves.iter().filter(|c| source(&c.id)).flat_map(|c| c.kind.point_ids()).collect(), + radii: candidate.curves.iter().enumerate().filter(|(_, c)| source(&c.id) || output(&c.id)).map(|(i, _)| i).collect(), + }; + if !crate::solve_with(&mut candidate, &hold, Some(&before), crate::Analysis::StatusOnly, None).keeps_orientation() { + // A cap tangent to an arc can land on the other tangency branch after a large + // jump. Walk the held source and output points from their stored positions. + // Their own dimensions are restored after the walk, not imposed at each step. + let mut walking = candidate.clone(); + walking.constraints.retain(|c| candidate.constraint_points(&c.kind).iter().any(|p| !hold.points.contains(p))); + for i in &hold.points { + if let Some(p) = walking.points.get_mut(*i) { + p.fixed = true; + } + } + for i in &hold.radii { + if let Some(c) = walking.curves.get_mut(*i) { + c.fixed = true; + } + } + let mut from = walking.clone(); + for (p, pos) in from.points.iter_mut().zip(&before) { + p.pos = *pos; + } + crate::solve_after_link_move(&mut walking, &from, old, crate::Analysis::StatusOnly)?; + for (p, q) in candidate.points.iter_mut().zip(&walking.points) { + p.pos = q.pos; + } + for (c, d) in candidate.curves.iter_mut().zip(&walking.curves) { + c.kind.clone_from(&d.kind); + } + } + // Publish ordinary freedom/redundancy diagnostics, without the temporary holds. + let report = crate::solve_after_edit_with_analysis(&mut candidate, old, Some(&before), analysis); + if !report.keeps_orientation() { + return None; + } + *self = candidate; + Some(report) + } + pub fn refresh_offsets(&mut self) -> Result<(), SketchError> { if self.offsets.is_empty() && self.freeform_offsets.is_empty() { return Ok(());