Skip to content

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

Sketch: a constraint that would shrink a curve to a point is refused (#8988)

semmelrinde5cf18ba8c31b
diff --git a/crates/engine/src/cmd/sketch.rs b/crates/engine/src/cmd/sketch.rs
index 2be3a477e3..23e1951dfb 100644
--- a/crates/engine/src/cmd/sketch.rs
+++ b/crates/engine/src/cmd/sketch.rs
@@ -342,12 +342,32 @@ fn settle(strict: bool, rep: SolveReport, sk: &mut Sketch, unsolved: Sketch) ->
"that would throw points of the sketch far away: its constraints have no solution near the sketch as it is".into(),
));
}
- if !rep.keeps_orientation() || far {
+ // A solve that shrinks a line or circle to a point met its constraints only by squeezing
+ // that curve away (a corner made the midpoint of the opposite side of its rectangle).
+ let gone = shrunk(&unsolved, sk);
+ if strict && let Some(curve) = &gone {
+ return Err(EngineError::Other(format!("that would shrink {curve} to a point: its constraints can only be met without it")));
+ }
+ if !rep.keeps_orientation() || far || gone.is_some() {
*sk = unsolved;
}
Ok(rep)
}
+/// The first line or circle that solving `before` into `after` shrank to a point: longer (or
+/// wider) than 1e-3 before and shorter than 1e-6 after. A length a dimension asks for is never
+/// that short.
+fn shrunk(before: &Sketch, after: &Sketch) -> Option<String> {
+ let size = |sk: &Sketch, i: usize| match sk.curves.get(i).map(|c| &c.kind) {
+ Some(CurveKind::Line { .. }) => line_pts(sk, i).map(|(a, b)| a.dist(b)),
+ Some(CurveKind::Arc { .. } | CurveKind::Circle { .. }) => sk.radius(i),
+ _ => None,
+ };
+ (0..before.curves.len().min(after.curves.len()))
+ .find(|&i| matches!((size(before, i), size(after, i)), (Some(b), Some(a)) if b > 1e-3 && a < 1e-6))
+ .and_then(|i| after.curves.get(i).map(|c| c.id.clone()))
+}
+
/// A solve moving some point further than this many times the sketch's size (and its largest
/// dimension value) has thrown the geometry to a far solution.
const FLUNG: f64 = 1000.0;
@@ -1781,6 +1801,9 @@ pub(super) fn apply_in_order(sk: &mut Sketch, rep: &SolveReport, first: &[Ent],
if flung(sk, &trial) {
return Err(bad(cmd, "that would throw points of the sketch far away: its constraints have no solution near the sketch as it is"));
}
+ if let Some(curve) = shrunk(sk, &trial) {
+ return Err(bad(cmd, format!("that would shrink {curve} to a point: its constraints can only be met without it")));
+ }
*sk = trial;
return Ok(());
}
diff --git a/crates/engine/src/cmd/sketch_constraints_tests.rs b/crates/engine/src/cmd/sketch_constraints_tests.rs
index 9007390687..720cc21d47 100644
--- a/crates/engine/src/cmd/sketch_constraints_tests.rs
+++ b/crates/engine/src/cmd/sketch_constraints_tests.rs
@@ -1281,6 +1281,24 @@ fn a_constraint_never_throws_the_sketch_far_away() {
assert!(far < 1e4, "a point went {far} mm away");
}
+/// A constraint that can only be met by shrinking curves to a point is refused, as one that
+/// throws the sketch far away is: making a rectangle's corner the midpoint of the opposite side,
+/// or two opposite corners coincident, squeezed the whole rectangle to a point while the sketch
+/// said solved (found by a local sketch fuzzer).
+#[test]
+fn a_constraint_that_would_shrink_a_rectangle_to_a_point_is_refused() {
+ for (id, p) in
+ [("sketch.constraint.midpoint", json!({"line": "l4", "point": "p2"})), ("sketch.constraint.coincident", json!({"a": "p1", "b": "p3"}))]
+ {
+ let mut s = new_sketch();
+ run(&mut s, "sketch.rectangle.two_point", json!({"p0": [-30, 4], "p1": [11, 27]}));
+ let before = sketch(&s);
+ let e = s.execute(id, &p).unwrap_err().to_string();
+ assert!(e.contains("shrink") && e.contains("to a point"), "{id} {p}: {e}");
+ assert_eq!(sketch(&s), before, "{id} {p}");
+ }
+}
+
#[test]
fn coincident_circles_lie_on_each_other() {
let mut s = new_sketch();