Skip to content

SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.

Trim cuts a line at its tangent point on a circle (#5074) (#5163)

DuBeartec0da9fe86108
diff --git a/crates/engine/src/cmd/sketch_modify.rs b/crates/engine/src/cmd/sketch_modify.rs
index cead75217c..662631325b 100644
--- a/crates/engine/src/cmd/sketch_modify.rs
+++ b/crates/engine/src/cmd/sketch_modify.rs
@@ -6,7 +6,7 @@ use std::f64::consts::TAU;
use serde_json::{Value, json};
use solidcraft_doc::ProfileSel;
use solidcraft_geom::Vec2;
-use solidcraft_sketch::{ConstraintKind, Curve, CurveKind, Shape, Sketch, intersections};
+use solidcraft_sketch::{ConstraintKind, Curve, CurveKind, Shape, Sketch, intersections, intersections_within};
use super::sketch::{add_c, edit, ids_of, solve_current};
use super::{CommandSpec, in_sketch};
@@ -144,7 +144,10 @@ fn cut_points(sk: &Sketch, ci: usize) -> Vec<(f64, Vec2, usize)> {
continue;
}
let Some(other) = sk.shape(oi) else { continue };
- let mut pts = intersections(&sh, &other);
+ // A line or circle the solver made tangent to a circle touches it within rounding noise,
+ // and the exact roots miss it whenever the noise falls outside: the touch counts as a
+ // cut, as profile detection counts it (#588), or trim deletes the whole curve (#5074).
+ let mut pts = intersections_within(&sh, &other, TOL * 10.0);
// Overlapping or touching ends that the analytic intersection misses (parallel lines).
if let Shape::Line { a, b } = other {
pts.extend([a, b]);
diff --git a/crates/engine/src/cmd/sketch_modify_tests.rs b/crates/engine/src/cmd/sketch_modify_tests.rs
index f6d7326ce4..e9e153305d 100644
--- a/crates/engine/src/cmd/sketch_modify_tests.rs
+++ b/crates/engine/src/cmd/sketch_modify_tests.rs
@@ -1972,3 +1972,34 @@ fn fillet_radius_accepts_a_leading_equals_sign() {
let plain = run(&mut s, "sketch.fillet", json!({"point": "c.end", "radius": "= 2 mm"}));
assert!(plain["curves"].is_array());
}
+
+/// #5074: lines made tangent to a circle by the solver touch it within rounding noise, and the
+/// noise often falls outside, so trim found no cut at the touch and deleted the whole line. Each
+/// line is trimmed back to its tangent point instead: the belt shape (two lines from one point)
+/// and two parallel lines.
+#[test]
+fn trim_cuts_a_line_at_its_tangent_point_on_a_circle() {
+ // (start, end, where the trim picks: past the tangent point)
+ let belt = [([30.0, 0.0], [-8.0, 12.0], [-7.0, 12.5]), ([30.0, 0.0], [-8.0, -12.0], [-7.0, -12.5])];
+ let parallel = [([-20.0, 11.0], [20.0, 11.0], [-15.0, 10.0]), ([-20.0, -11.0], [20.0, -11.0], [-15.0, -10.0])];
+ for (name, lines) in [("belt", belt), ("parallel", parallel)] {
+ let mut s = new_sketch();
+ run(&mut s, "sketch.circle.center", json!({"center": [0, 0], "radius": 10}));
+ for (a, b, _) in lines {
+ run(&mut s, "sketch.line", json!({"points": [a, b], "connect": false, "infer": false}));
+ }
+ run(&mut s, "sketch.constraint.tangent", json!({"a": "l1", "b": "c1"}));
+ run(&mut s, "sketch.constraint.tangent", json!({"a": "l2", "b": "c1"}));
+ for (line, (_, _, at)) in ["l1", "l2"].into_iter().zip(lines) {
+ let r = run(&mut s, "sketch.trim", json!({"curve": line, "at": at}));
+ assert_eq!(r["result"]["trimmed"], line, "{name}: {line} was {}", r["result"]);
+ let sk = sketch(&s);
+ let at = |r: &str| sk.point(sk.resolve_point(r).unwrap()).unwrap();
+ // The solve may size the circle to the lines (its radius isn't dimensioned).
+ let radius = sk.radius(sk.curve_index("c1").unwrap()).unwrap();
+ let touches = [format!("{line}.start"), format!("{line}.end")].iter().any(|e| (at(e).len() - radius).abs() < 1e-6);
+ assert!(touches, "{name}: {line} now ends at its tangent point");
+ }
+ assert_eq!(sketch(&s).curves.len(), 3, "{name}: the circle and both lines stay");
+ }
+}