From f4e52c4d3e99483d736d8ae634c218a232507986 Mon Sep 17 00:00:00 2001 From: semmelrinde <124386289+semmelrinde@users.noreply.github.com> Date: Thu, 29 Oct 2026 04:14:22 +0000 Subject: [PATCH] Sketch: points tied only through a removed curve's end stay tied (#8640) * Sketch: points tied only through a removed curve's end stay tied Removing a curve removed its orphaned end points with their constraints, so two points made coincident with such an end lost their tie although they still met: two rectangles' corners on one arc end came apart when the arc was trimmed. They are now tied to each other before the point goes. Co-Authored-By: Claude Opus 5.5 * Sketch: keep the tie test with the other trim tests Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 --- crates/engine/src/cmd/sketch_modify_tests.rs | 24 ++++++++++++ crates/sketch/src/model.rs | 41 ++++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/crates/engine/src/cmd/sketch_modify_tests.rs b/crates/engine/src/cmd/sketch_modify_tests.rs index 08286c7f0..347d73b22 100644 --- a/crates/engine/src/cmd/sketch_modify_tests.rs +++ b/crates/engine/src/cmd/sketch_modify_tests.rs @@ -2468,6 +2468,30 @@ fn several_texts_edit_in_one_rebuild() { assert!((v1 / v0 - 2.0).abs() < 0.05, "two bars per label now: {v0} -> {v1}"); } +/// Two ends tied to each other only through a third curve's end stay tied when a trim takes +/// that curve away: they still meet there (found by a local trim fuzzer: two rectangles' +/// corners made coincident with one arc end came apart when the arc was trimmed). +#[test] +fn ends_tied_through_a_trimmed_away_end_stay_tied() { + let mut s = new_sketch(); + for (a, b) in [([0, 0], [10, 0]), ([10, 0], [10, 10]), ([10, 0], [20, 5])] { + run(&mut s, "sketch.line", json!({"points": [a, b], "infer": false, "connect": false})); + } + run(&mut s, "sketch.constraint.coincident", json!({"a": "l1.end", "b": "l3.start"})); + run(&mut s, "sketch.constraint.coincident", json!({"a": "l2.start", "b": "l3.start"})); + assert!(solidcraft_sketch::loose_ends(&sketch(&s)).untied.is_empty()); + let r = run(&mut s, "sketch.trim", json!({"curve": "l3", "at": [15, 2.5]})); + assert_eq!(r["result"]["deleted"], "l3"); + let sk = sketch(&s); + let ends = solidcraft_sketch::loose_ends(&sk); + assert!(ends.untied.is_empty(), "l1 and l2 still meet at (10, 0): {ends:?}"); + // Tied, not merely touching: dragging one end takes the other along. + run(&mut s, "sketch.drag", json!({"entity": "l1.end", "from": [10, 0], "to": [12, 1]})); + let sk = sketch(&s); + let at = |r: &str| sk.point(sk.resolve_point(r).unwrap()).unwrap(); + assert!(at("l1.end").dist(at("l2.start")) < 1e-9); +} + #[test] fn trim_keeps_the_rest_of_a_dimensioned_line_where_it_was() { // A 40 x 40 square, its right side dimensioned with the Dimension tool (a line length). diff --git a/crates/sketch/src/model.rs b/crates/sketch/src/model.rs index 492789c4c..30f77c65d 100644 --- a/crates/sketch/src/model.rs +++ b/crates/sketch/src/model.rs @@ -1603,11 +1603,52 @@ impl Sketch { let mut orphans: Vec = owned.into_iter().filter(|p| *p != 0 && !self.point_used_by_curve(*p)).collect(); orphans.sort_unstable(); orphans.dedup(); + self.keep_ties_through(&orphans); self.remove_points(&orphans); self.prune_links(); self.prune_patterns(); } + /// Points held together only through points about to go stay together: two rectangles' + /// corners made coincident with one arc end are tied to each other when a trim takes that + /// end away, instead of coming apart where they still meet. + fn keep_ties_through(&mut self, gone: &[usize]) { + if gone.is_empty() { + return; + } + let gone: std::collections::BTreeSet = gone.iter().copied().collect(); + let n = self.points.len(); + // Coincident groups with every point, and with the points that stay only. + let (mut all, mut kept): (Vec, Vec) = ((0..n).collect(), (0..n).collect()); + for c in &self.constraints { + if let ConstraintKind::Coincident { p, q } = c.kind { + union(&mut all, p, q); + if !gone.contains(&p) && !gone.contains(&q) { + union(&mut kept, p, q); + } + } + } + let mut first = std::collections::BTreeMap::::new(); + let mut ties = Vec::new(); + for x in (0..n).filter(|x| !gone.contains(x)) { + let group = find(&mut all, x); + match first.get(&group).copied() { + None => { + first.insert(group, x); + } + Some(f) if find(&mut kept, f) != find(&mut kept, x) => { + union(&mut kept, f, x); + ties.push((f, x)); + } + Some(_) => {} + } + } + for (p, q) in ties { + // Only a full sketch refuses one more; the points then come apart as before. + let _ = self.add_constraint(ConstraintKind::Coincident { p, q }, None); + } + } + fn point_used_by_curve(&self, p: usize) -> bool { self.curves.iter().any(|c| c.kind.uses(p)) }