Skip to content

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

Sketch drag: a line is never shrunk to a point to follow the cursor (#9062) (#9728)

PaddleStroke9d18034e1334
diff --git a/crates/engine/src/cmd/sketch_drag.rs b/crates/engine/src/cmd/sketch_drag.rs
index 0337546f11..e039c330ca 100644
--- a/crates/engine/src/cmd/sketch_drag.rs
+++ b/crates/engine/src/cmd/sketch_drag.rs
@@ -40,6 +40,13 @@ const HALVINGS: usize = 16;
const AHEAD: usize = 8;
/// An arc whose sweep gets this close (radians) to 0 or a full turn has run into its start.
const SWEEP_EPS: f64 = 1e-6;
+/// A line or circle longer or wider than this before a step and shorter or narrower than
+/// `SHRINK_TO` after it was shrunk to a point by that step. An edit's `shrunk` stops at 1e-5,
+/// but a drag's first step shrank a horizontal 20 mm line to 1.05e-5 (#9062): its direction
+/// rows (`b.y - a.y`) scale with its length, so the solve stops wherever the length times the
+/// tilt is under the tolerance, and the next frames took it the rest of the way.
+const SHRINK_FROM: f64 = 1e-3;
+const SHRINK_TO: f64 = 1e-4;
/// What a drag moves, from where it is (`t = 0`) to where the cursor puts it (`t = 1`).
enum Path {
@@ -179,7 +186,9 @@ pub(super) fn drag_entity(
let held = |sk: &mut Sketch, last: &Geometry, t: f64, budget: &mut Budget| {
path.apply(sk, t);
let kept = keep.as_ref().is_some_and(|keep| {
- let ok = counted_solve(sk, keep, Some(&last.pos), Analysis::StatusOnly, Some(&mut *budget)).ok() && !last.sweep_wrapped(sk);
+ let ok = counted_solve(sk, keep, Some(&last.pos), Analysis::StatusOnly, Some(&mut *budget)).ok()
+ && !last.sweep_wrapped(sk)
+ && !last.shrank(sk);
if !ok {
last.restore(sk);
path.apply(sk, t);
@@ -197,7 +206,8 @@ pub(super) fn drag_entity(
solves.work += budget.spent() - spent;
solves.cut |= budget.exhausted();
solves.numeric |= budget.numeric_exhausted();
- let outcome = if !ok {
+ // A step that shrank a line to a point only met its constraints without that line.
+ let outcome = if !ok || last.shrank(sk) {
Step::Failed
} else if last.sweep_wrapped(sk) {
Step::Wrapped
@@ -332,7 +342,7 @@ pub(super) fn drag_entity(
let valid = |before: &Sketch, after: &Sketch| {
let mut geometry = Geometry::default();
geometry.take(before);
- !geometry.sweep_wrapped(after) && solidcraft_sketch::fillets_kept(before, after)
+ !geometry.sweep_wrapped(after) && !geometry.shrank(after) && solidcraft_sketch::fillets_kept(before, after)
};
let followed = match (e, line_grab) {
(Ent::Point(q), _) => solidcraft_sketch::move_point_towards(sk, q, to, max_step, frame.as_mut(), valid),
@@ -585,6 +595,22 @@ impl Geometry {
(s1 - s0).abs() > PI || (clear(s0) && !clear(s1))
})
}
+
+ /// Did the step shrink a line or circle to a point (from longer or wider than `SHRINK_FROM`
+ /// to under `SHRINK_TO`)? A line with no length has no direction left, so its horizontal,
+ /// parallel or point-on-line constraints hold anything: a solve took that to put a point it
+ /// couldn't reach on the line, and the construction line it was on vanished (#9062).
+ fn shrank(&self, sk: &Sketch) -> bool {
+ let was = |q: usize| self.pos.get(q).copied();
+ self.curves.iter().zip(&sk.curves).any(|((kind, _), now)| match (kind, &now.kind) {
+ (CurveKind::Line { a, b }, _) => match (was(*a).zip(was(*b)), sk.point(*a).zip(sk.point(*b))) {
+ (Some((a0, b0)), Some((a1, b1))) => a0.dist(b0) > SHRINK_FROM && !(a1.dist(b1) >= SHRINK_TO),
+ _ => false,
+ },
+ (CurveKind::Circle { r: r0, .. }, CurveKind::Circle { r: r1, .. }) => *r0 > SHRINK_FROM && !(*r1 >= SHRINK_TO),
+ _ => false,
+ })
+ }
}
/// The held solves dragging `e` from `from` to `to` takes (0 when it cannot move): the plan
diff --git a/crates/engine/tests/sketch_drag_target.rs b/crates/engine/tests/sketch_drag_target.rs
index 883907e93b..d425c03c4b 100644
--- a/crates/engine/tests/sketch_drag_target.rs
+++ b/crates/engine/tests/sketch_drag_target.rs
@@ -29,6 +29,52 @@ fn angular_drag_keeps_the_horizontal_construction_line() {
}
}
+/// A line's end on a horizontal construction line through the origin, dragged up off it in
+/// small frames (#9062): the end can only slide along the construction line, so it stays on it
+/// and the construction line keeps its length. A line's horizontal row is `b.y - a.y`, which a
+/// line a hair long meets at any tilt: the drag's solves shrank the construction line to 1e-5 mm
+/// on the first frame and to 1e-7 mm by the tenth, so it vanished and the end went up.
+#[test]
+fn an_end_dragged_off_a_horizontal_construction_line_leaves_it_whole() {
+ let through_origin = [
+ // Drawn from the origin (inferred horizontal).
+ vec![("sketch.line", json!({"points":["origin",[20,0]],"construction":true,"infer":true,"ids":["c"]}))],
+ // Drawn across the origin, then the origin made coincident with it.
+ vec![
+ ("sketch.line", json!({"points":[[-10,0.2],[20,0.2]],"construction":true,"infer":true,"ids":["c"]})),
+ ("sketch.constraint.coincident", json!({"a":"origin","b":"c"})),
+ ],
+ ];
+ for construction in through_origin {
+ for live in [false, true] {
+ let mut s = solidcraft_engine::Session::default();
+ s.execute("sketch.create", &json!({"plane":"XY"})).unwrap();
+ for (id, params) in construction.iter().cloned().chain([
+ ("sketch.line", json!({"points":[[10,0],[10,10]],"ids":["l"],"infer":false})),
+ ("sketch.constraint.coincident", json!({"a":"l.start","b":"c"})),
+ ]) {
+ s.execute(id, &params).unwrap();
+ }
+ let point = |s: &solidcraft_engine::Session, id| four_bar::point(s, id).unwrap();
+ let length = |s: &solidcraft_engine::Session| point(s, "c.start").dist(point(s, "c.end"));
+ let before = length(&s);
+ let mut at = point(&s, "l.start");
+ for _ in 0..10 {
+ let to = Vec2::new(at.x, at.y + 0.1);
+ s.execute("sketch.drag", &json!({"entity":"l.start","from":[at.x,at.y],"to":[to.x,to.y],"live":live})).unwrap();
+ assert!((length(&s) - before).abs() < 1e-6, "(live {live}) the construction line went from {before} mm to {} mm", length(&s));
+ at = to;
+ }
+ if live {
+ s.execute("sketch.drag_end", &json!({})).unwrap();
+ }
+ let end = point(&s, "l.start");
+ assert!(end.y.abs() < 1e-6, "(live {live}) l.start left the construction line: {end:?}");
+ assert!((length(&s) - before).abs() < 1e-6, "(live {live}) the construction line went to {} mm", length(&s));
+ }
+ }
+}
+
/// A line held at its midpoint, with an angle to a horizontal line from that point (#8944):
/// dragging one of its ends off that angle slides the end along the line as far as it can go,
/// and the horizontal line keeps its length. That line's far end is free only along it, which