SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Sketch corners: a round that would use up an arc, or where the curves meet smoothly, is refused (#8813)
semmelrinde7b24a1dc1e87
diff --git a/crates/engine/src/cmd/sketch_corner_tests.rs b/crates/engine/src/cmd/sketch_corner_tests.rsindex 60294befb4..f49402bd5a 100644--- a/crates/engine/src/cmd/sketch_corner_tests.rs+++ b/crates/engine/src/cmd/sketch_corner_tests.rs@@ -74,9 +74,10 @@ fn fillet_and_chamfer_corners() {let e = s.execute("sketch.chamfer.equal_distance", &json!({"a": c[0], "b": c[1], "distance": 25})).unwrap_err().to_string();assert!(e.contains("too large") || e.contains("does not fit") || e.contains("longer"), "{e}");assert_eq!(sk(&s).curves.len(), n);- // A line meeting an arc, and two arcs (a slot's end rounded into a line).+ // A line meeting an arc at a corner (down onto its top), and two arcs (a slot's end rounded+ // into a line).let mut s = new_sketch();- let l = curves(&run(&mut s, "sketch.line", json!({"points": [[-20, 10], [0, 10]]})))[0].clone();+ let l = curves(&run(&mut s, "sketch.line", json!({"points": [[0, 20], [0, 10]]})))[0].clone();let a = curves(&run(&mut s, "sketch.arc.three_point", json!({"start": format!("{l}.end"), "end": [10, 0], "through": [7.0710678, 7.0710678]})))[0].clone();@@ -117,6 +118,38 @@ fn a_chamfer_where_two_lines_lie_in_line_is_refused() {}}+/// A round that would use up a whole arc is refused, as one too large for two lines is: a+/// quarter arc of radius 5 running into a line along it, rounded with radius 5, was left as an+/// arc of no length (found by a local trim fuzzer).+#[test]+fn a_fillet_that_would_use_up_an_arc_is_refused() {+ let mut s = new_sketch();+ // Centre (15, 5): from (10, 5) down to (15, 0), where the line along y = 0 leaves it.+ let through = [15.0 - 2.5 * 2f64.sqrt(), 5.0 - 2.5 * 2f64.sqrt()];+ let a = curves(&run(&mut s, "sketch.arc.three_point", json!({"start": [10, 5], "end": [15, 0], "through": through})))[0].clone();+ let l = curves(&run(&mut s, "sketch.line", json!({"points": [format!("{a}.end"), [-35, 0]], "infer": false})))[0].clone();+ let n = sk(&s).curves.len();+ let e = s.execute("sketch.fillet", &json!({"a": a, "b": l, "radius": 5})).unwrap_err().to_string();+ assert!(e.contains("does not fit"), "{e}");+ assert_eq!(sk(&s).curves.len(), n);+}++/// A round where a line runs on along an arc's end, off only by rounding, is refused: there is no+/// corner, and the round came out as an arc about 1e-7 mm long (found by a local trim fuzzer,+/// after earlier rounds left the line a hair off the arc's tangent).+#[test]+fn a_fillet_where_a_line_runs_on_smoothly_from_an_arc_is_refused() {+ let mut s = new_sketch();+ let h = 0.5 * 2f64.sqrt();+ let a = curves(&run(&mut s, "sketch.arc.three_point", json!({"start": [1, 0], "end": [0, 1], "through": [h, h]})))[0].clone();+ // Off the arc's tangent at (0, 1) by 1e-7 rad.+ let l = curves(&run(&mut s, "sketch.line", json!({"points": [format!("{a}.end"), [-10, 1.000001]], "infer": false})))[0].clone();+ let n = sk(&s).curves.len();+ let e = s.execute("sketch.fillet", &json!({"a": a, "b": l, "radius": 5})).unwrap_err().to_string();+ assert!(e.contains("smoothly") || e.contains("does not fit"), "{e}");+ assert_eq!(sk(&s).curves.len(), n);+}+/// The ids of the constraints driven by the model parameter `name`.fn driven_by(s: &Session, name: &str) -> Vec<String> {sk(s).constraints.iter().filter(|c| c.param.as_deref() == Some(name)).map(|c| c.id.clone()).collect()diff --git a/crates/engine/src/cmd/sketch_modify.rs b/crates/engine/src/cmd/sketch_modify.rsindex 7c18a9b956..29b4ac42c0 100644--- a/crates/engine/src/cmd/sketch_modify.rs+++ b/crates/engine/src/cmd/sketch_modify.rs@@ -1755,12 +1755,29 @@ fn round_corner(sk: &mut Sketch, p: &Value, r: f64, cmd: &str) -> Result<Option<}}};+ // A tangent point at a curve's other end would leave nothing of it (a quarter arc rounded+ // with its own radius was left as an arc of no length): that radius does not fit, as one too+ // large for two lines doesn't.+ let far = |c: usize| ends(c).map(|(a, b)| if a == q { b } else { a }).and_then(|f| sk.point(f));+ let (far_a, far_b) = (far(ca), far(cb));+ let used_up = |t: Vec2, end: Option<Vec2>| end.is_some_and(|e| t.dist(e) < TOL * 10.0);+ // Where the curves meet smoothly (a line along an arc's end, off by rounding), the round's+ // tangent points fall on the corner within rounding: an arc of no length, refused as such.+ let (mut smooth, mut short) = (false, false);let mut best: Option<(f64, Vec2, Vec2, Vec2)> = None;for oa in offsets(&sa) {for ob in offsets(&sb) {for cen in intersections(&oa, &ob) {let (ta, tb) = (sa.project(cen), sb.project(cen));- if on_bounded(&sa, ta).is_none() || on_bounded(&sb, tb).is_none() || ta.dist(pc) < 1e-9 || tb.dist(pc) < 1e-9 {+ if on_bounded(&sa, ta).is_none() || on_bounded(&sb, tb).is_none() {+ continue;+ }+ if ta.dist(pc) < TOL * 10.0 || tb.dist(pc) < TOL * 10.0 {+ smooth = true;+ continue;+ }+ if used_up(ta, far_a) || used_up(tb, far_b) {+ short = true;continue;}let score = ta.dist(pc) + tb.dist(pc);@@ -1770,7 +1787,9 @@ fn round_corner(sk: &mut Sketch, p: &Value, r: f64, cmd: &str) -> Result<Option<}}}- let (_, cen, ta, tb) = best.ok_or_else(|| bad(cmd, "that radius does not fit the corner"))?;+ let (_, cen, ta, tb) = best.ok_or_else(|| {+ bad(cmd, if smooth && !short { "the curves meet smoothly there: there is no corner to round" } else { "that radius does not fit the corner" })+ })?;let (na, nb) = (sk.add_point(ta, None)?, sk.add_point(tb, None)?);for (c, n) in [(ca, na), (cb, nb)] {if let Some(cu) = sk.curves.get_mut(c) {