Skip to content

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

Sketch Trim: an end at a place several curves cut is tied to the one with an end there (#8612) (#8628)

semmelrindef071f001551b
diff --git a/crates/engine/src/cmd/sketch_modify.rs b/crates/engine/src/cmd/sketch_modify.rs
index 9cecede6f3..490f8f0dcb 100644
--- a/crates/engine/src/cmd/sketch_modify.rs
+++ b/crates/engine/src/cmd/sketch_modify.rs
@@ -173,22 +173,37 @@ fn cut_points(sk: &Sketch, ci: usize) -> Vec<(f64, Vec2, usize)> {
for q in pts {
if (other_infinite || on_bounded(&other, q).is_some())
&& let Some(t) = on_bounded(&sh, q)
- && !out.iter().any(|(_, x, _)| x.dist(q) < TOL * 10.0)
{
- out.push((t, q, oi));
+ add_cut(sk, &mut out, (t, q, oi));
}
}
}
for (q, oi) in super::sketch_freeform::analytic_cut_points(sk, ci) {
- if let Some(t) = on_bounded(&sh, q)
- && !out.iter().any(|(_, x, _)| x.dist(q) < TOL * 10.0)
- {
- out.push((t, q, oi));
+ if let Some(t) = on_bounded(&sh, q) {
+ add_cut(sk, &mut out, (t, q, oi));
}
}
out
}
+/// One cut per place. Where several curves cut at one place, the one with an end there is kept:
+/// [`pin`] makes the new end coincident with it. Pinned onto a curve passing through instead,
+/// the end came loose when a later trim removed that curve: two circles touching where a
+/// rectangle's side crossed them came apart once the side was trimmed away (#8612).
+fn add_cut(sk: &Sketch, out: &mut Vec<(f64, Vec2, usize)>, cut: (f64, Vec2, usize)) {
+ let ends_at = |oi: usize, q: Vec2| {
+ sk.curves
+ .get(oi)
+ .and_then(|c| c.kind.ends())
+ .is_some_and(|(a, b)| [a, b].iter().any(|e| sk.point(*e).is_some_and(|p| p.dist(q) < TOL * 10.0)))
+ };
+ match out.iter_mut().find(|(_, x, _)| x.dist(cut.1) < TOL * 10.0) {
+ Some(known) if !ends_at(known.2, known.1) && ends_at(cut.2, cut.1) => known.2 = cut.2,
+ Some(_) => {}
+ None => out.push(cut),
+ }
+}
+
/// Constrain a new end point onto the curve that cut there.
fn pin(sk: &mut Sketch, p: usize, cutter: usize) -> Result<()> {
let at = sk.point(p).unwrap_or_default();
diff --git a/crates/engine/src/cmd/sketch_modify_tests.rs b/crates/engine/src/cmd/sketch_modify_tests.rs
index 6158375b24..08286c7f0b 100644
--- a/crates/engine/src/cmd/sketch_modify_tests.rs
+++ b/crates/engine/src/cmd/sketch_modify_tests.rs
@@ -2655,6 +2655,36 @@ fn trim_cuts_a_line_at_its_tangent_point_on_a_circle() {
}
}
+/// A dog bone: a 100 x 50 rectangle with a 50 mm circle on each corner, the sides and the
+/// circles' insides trimmed away. Neighbouring circles touch where a side crossed them; the arc
+/// ends there are tied to each other whatever the trim order, as the drawing shows them, and not
+/// to a side a later trim removes (#8612).
+#[test]
+fn trimmed_touching_circles_stay_joined_where_they_touch() {
+ let lines = [("l4", [0.0, 10.0]), ("l4", [0.0, 40.0]), ("l2", [100.0, 10.0]), ("l2", [100.0, 40.0])];
+ let rest = [("l1", [10.0, 0.0]), ("l1", [90.0, 0.0]), ("l3", [10.0, 50.0]), ("l3", [90.0, 50.0])];
+ let circles = [("c1", [17.68, 17.68]), ("c2", [82.32, 17.68]), ("c3", [82.32, 32.32]), ("c4", [17.68, 32.32])];
+ let sides_first: Vec<_> = lines.iter().chain(&rest).chain(&circles).collect();
+ let circles_first: Vec<_> = circles.iter().chain(&lines).chain(&rest).collect();
+ for (name, trims) in [("sides first", sides_first), ("circles first", circles_first)] {
+ let mut s = new_sketch();
+ run(&mut s, "sketch.rectangle.two_point", json!({"p0": [0, 0], "p1": [100, 50]}));
+ for c in [[0, 0], [100, 0], [100, 50], [0, 50]] {
+ run(&mut s, "sketch.circle.center", json!({"center": c, "radius": 25}));
+ }
+ for (curve, at) in trims {
+ run(&mut s, "sketch.trim", json!({"curve": curve, "at": at}));
+ }
+ let sk = sketch(&s);
+ let ends = solidcraft_sketch::loose_ends(&sk);
+ assert!(ends.is_empty(), "{name}: loose ends {ends:?}");
+ assert_eq!(profiles(&s).len(), 1, "{name}");
+ // Dragging one circle's centre keeps the outline closed.
+ run(&mut s, "sketch.drag", json!({"entity": "c4.center", "from": [0, 50], "to": [-3, 52]}));
+ assert!(solidcraft_sketch::loose_ends(&sketch(&s)).is_empty(), "{name}: the touch point held after a drag");
+ }
+}
+
/// A point midway between an offset output and its source, inside the wall they bound.
fn inside_offset_wall(s: &Session, output: &str) -> Vec2 {
let sk = sketch(s);