SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Sketch Trim: keep endpoint tangency on the surviving arc (#10851) (#10853)
Colin Faulkingham2339510e4107
diff --git a/crates/engine/src/cmd/sketch_modify.rs b/crates/engine/src/cmd/sketch_modify.rsindex 48199cc887..63efedce77 100644--- a/crates/engine/src/cmd/sketch_modify.rs+++ b/crates/engine/src/cmd/sketch_modify.rs@@ -326,6 +326,54 @@ fn release_extent(sk: &mut Sketch, ci: usize) {});}+/// Tangencies tied to one of the curve's ends, through a shared point or Coincident+/// constraints. Record them before splitting: keeping the curve ID alone would leave an+/// end tangent on the wrong piece and lose the solver's endpoint tangency equation.+fn endpoint_tangencies(sk: &Sketch, ci: usize) -> Vec<(usize, usize)> {+ let Some((start, end)) = sk.curves.get(ci).and_then(|c| c.kind.ends()) else { return Vec::new() };+ let groups = coincident_points(sk);+ let root = |p: usize| groups.get(p).copied().unwrap_or(p);+ sk.constraints+ .iter()+ .enumerate()+ .filter_map(|(i, k)| {+ let ConstraintKind::Tangent { a, b } = k.kind else { return None };+ let other = if a == ci {+ b+ } else if b == ci {+ a+ } else {+ return None;+ };+ let (a, b) = sk.curves.get(other)?.kind.ends()?;+ let joined = |p| root(p) == root(a) || root(p) == root(b);+ match (joined(start), joined(end)) {+ (true, false) => Some((i, start)),+ (false, true) => Some((i, end)),+ _ => None,+ }+ })+ .collect()+}++/// Follow the original endpoints after the pieces have exchanged geometry to preserve the+/// original curve ID. A tangent on the removed piece goes with it in `remove_curves`.+fn rebind_endpoint_tangencies(sk: &mut Sketch, ci: usize, pieces: &[usize], tangencies: &[(usize, usize)]) {+ for &(constraint, point) in tangencies {+ let owner = pieces.iter().copied().find(|i| sk.curves.get(*i).and_then(|c| c.kind.ends()).is_some_and(|(a, b)| a == point || b == point));+ if let Some(owner) = owner+ && let Some(k) = sk.constraints.get_mut(constraint)+ && let ConstraintKind::Tangent { a, b } = &mut k.kind+ {+ if *a == ci {+ *a = owner;+ } else if *b == ci {+ *b = owner;+ }+ }+ }+}+/// The cut parameters bracketing `tq` on curve `ci`: (before, after) with their cutter curves.fn bracket(sk: &Sketch, ci: usize, tq: f64) -> (Vec<(f64, Vec2, usize)>, bool) {let Some(sh) = sk.shape(ci) else { return (Vec::new(), false) };@@ -549,6 +597,7 @@ fn remove_piece(sk: &mut Sketch, ci: usize, at: Vec2, cmd: &str) -> Result<(Valu}).collect();release_extent(sk, ci);+ let tangencies = endpoint_tangencies(sk, ci);let ts: Vec<f64> = cuts.iter().map(|c| c.0).collect();let (pieces, pts) = split_at(sk, ci, &ts)?;// The piece containing tq.@@ -580,6 +629,7 @@ fn remove_piece(sk: &mut Sketch, ci: usize, at: Vec2, cmd: &str) -> Result<(Valu}_ => gone,};+ rebind_endpoint_tangencies(sk, ci, &pieces, &tangencies);let keep: Vec<usize> = pieces.iter().copied().filter(|x| *x != gone).collect();tie(sk, &keep)?;if let Some((a, _)) = sk.curves.get(ci).and_then(|c| c.kind.ends()) {diff --git a/crates/engine/src/cmd/sketch_modify_tests.rs b/crates/engine/src/cmd/sketch_modify_tests.rsindex c28786d8fa..c0216ffdae 100644--- a/crates/engine/src/cmd/sketch_modify_tests.rs+++ b/crates/engine/src/cmd/sketch_modify_tests.rs@@ -196,6 +196,87 @@ fn analytic_trim_and_break_use_freeform_cutters() {assert!(sk.shape(ci).unwrap().dist(Vec2::new(-5.0, 0.0)) < 1e-6);}+/// #10851: an endpoint's tangent must stay with the arc piece that owns that endpoint,+/// including when the endpoints are separate points joined by a Coincident constraint.+#[test]+fn trim_keeps_endpoint_tangency_on_its_arc_piece() {+ for path in [false, true] {+ for separate in [false, true] {+ for at_start in [false, true] {+ for reverse in [false, true] {+ let mut s = new_sketch();+ run(&mut s, "sketch.arc.center_point", json!({"center": [0,0], "start": [50,0], "end": [-50,0]}));+ let endpoint = if at_start { "a1.start" } else { "a1.end" };+ let x = if at_start { 50 } else { -50 };+ let start = if separate { json!([x, 0]) } else { json!(endpoint) };+ run(&mut s, "sketch.line", json!({"points": [start, [x,-30]], "connect": false, "infer": false}));+ if separate {+ run(&mut s, "sketch.constraint.coincident", json!({"a": "l1.start", "b": endpoint}));+ }+ let (a, b) = if reverse { ("a1", "l1") } else { ("l1", "a1") };+ let tangent = run(&mut s, "sketch.constraint.tangent", json!({"a": a, "b": b}))["constraint"].as_str().unwrap().to_string();+ run(&mut s, "sketch.dimension", json!({"entities": ["a1"], "type": "radius", "value": "50"}));+ run(&mut s, "sketch.dimension", json!({"entities": ["l1"], "value": "30"}));+ run(&mut s, "sketch.constraint.horizontal_vertical", json!({"points": ["origin", "a1.start"], "mode": "horizontal"}));+ for x in [-10, 10] {+ run(&mut s, "sketch.line", json!({"points": [[x,40],[x,60]]}));+ }+ run(&mut s, "sketch.constraint.fix", json!({"entities": ["l2", "l3"]}));+ assert_eq!(run(&mut s, "sketch.solve", json!({}))["dof"], 1);+ let before = sketch(&s);+ let undo = s.undo.len();+ let result = if path {+ run(&mut s, "sketch.trim.path", json!({"points": [[0,45],[0,55]]}))+ } else {+ run(&mut s, "sketch.trim", json!({"curve": "a1", "at": [0,50]}))+ };+ assert_eq!(result["sketch"]["dof"], 1, "path={path}, separate={separate}, at_start={at_start}, reverse={reverse}: {result}");+ assert_eq!(s.undo.len(), undo + 1);+ let after = sketch(&s);+ let ci = after.curve_index(if at_start { "a1" } else { "a3" }).unwrap();+ let li = after.curve_index("l1").unwrap();+ assert!(after.constraints.iter().any(+ |k| k.id == tangent && matches!(k.kind, ConstraintKind::Tangent { a, b } if (a == ci && b == li) || (a == li && b == ci))+ ));+ let at = |id: &str| after.point(after.resolve_point(id).unwrap()).unwrap();+ assert!(at(endpoint).dist(at("l1.start")) < 1e-8);+ let radius = at("l1.start") - at("a1.center");+ let line = at("l1.end") - at("l1.start");+ assert!(radius.dot(line).abs() < 1e-6, "the line stays tangent to the circle");+ run(&mut s, "edit.undo", json!({}));+ assert_eq!(sketch(&s), before);+ run(&mut s, "edit.redo", json!({}));+ assert_eq!(sketch(&s), after);+ }+ }+ }+ }+}++#[test]+fn trim_keeps_or_removes_tangency_with_the_original_end() {+ for remove_tangent_end in [false, true] {+ let mut s = new_sketch();+ run(&mut s, "sketch.arc.center_point", json!({"center": [0,0], "start": [50,0], "end": [-50,0]}));+ run(&mut s, "sketch.line", json!({"points": ["a1.end", [-50,-30]]}));+ let tangent = run(&mut s, "sketch.constraint.tangent", json!({"a": "l1", "b": "a1"}))["constraint"].as_str().unwrap().to_string();+ run(&mut s, "sketch.line", json!({"points": [[0,40],[0,60]]}));+ let before = sketch(&s);+ let x = if remove_tangent_end { -40 } else { 40 };+ run(&mut s, "sketch.trim", json!({"curve": "a1", "at": [x,30]}));+ let after = sketch(&s);+ assert!(after.curve_index("a1").is_some(), "the original curve ID survives either end trim");+ assert_eq!(after.constraints.iter().any(|k| k.id == tangent), !remove_tangent_end);+ if !remove_tangent_end {+ let arc = after.curve_index("a1").unwrap();+ let line = after.curve_index("l1").unwrap();+ assert!(after.constraints.iter().any(|k| k.id == tangent && matches!(k.kind, ConstraintKind::Tangent { a, b } if a == line && b == arc)));+ }+ run(&mut s, "edit.undo", json!({}));+ assert_eq!(sketch(&s), before);+ }+}+#[test]fn trim_extend_break() {let mut s = new_sketch();