diff --git a/crates/engine/src/cmd/sketch_corner_tests.rs b/crates/engine/src/cmd/sketch_corner_tests.rs index 9c58a85f9..60294befb 100644 --- a/crates/engine/src/cmd/sketch_corner_tests.rs +++ b/crates/engine/src/cmd/sketch_corner_tests.rs @@ -97,6 +97,26 @@ fn fillet_and_chamfer_corners() { assert!((area(&s) - (a0 - 8.0)).abs() < 1e-6, "{}", area(&s)); } +/// A chamfer where the two lines have no corner is refused, as a fillet there is: two lines +/// lying on each other made a chamfer line of no length between their cut-back ends (found by a +/// local trim fuzzer), and two lines in line with each other have no corner either. +#[test] +fn a_chamfer_where_two_lines_lie_in_line_is_refused() { + for far in [[0, 10], [0, 30]] { + let mut s = new_sketch(); + let a = curves(&run(&mut s, "sketch.line", json!({"points": [[0, 0], [0, 20]], "infer": false})))[0].clone(); + let b = curves(&run(&mut s, "sketch.line", json!({"points": [far, [0, 20]], "infer": false, "connect": false})))[0].clone(); + run(&mut s, "sketch.constraint.coincident", json!({"a": format!("{a}.end"), "b": format!("{b}.end")})); + let n = sk(&s).curves.len(); + for cmd in ["sketch.chamfer.equal_distance", "sketch.fillet"] { + let p = if cmd == "sketch.fillet" { json!({"a": a, "b": b, "radius": 2}) } else { json!({"a": a, "b": b, "distance": 2}) }; + let e = s.execute(cmd, &p).unwrap_err().to_string(); + assert!(e.contains("parallel"), "{cmd} at {far:?}: {e}"); + assert_eq!(sk(&s).curves.len(), n, "{cmd} at {far:?}"); + } + } +} + /// The ids of the constraints driven by the model parameter `name`. fn driven_by(s: &Session, name: &str) -> Vec { 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.rs index 3ed3cc107..124f68c29 100644 --- a/crates/engine/src/cmd/sketch_modify.rs +++ b/crates/engine/src/cmd/sketch_modify.rs @@ -1789,6 +1789,12 @@ fn chamfer(s: &mut Session, p: &Value, cmd: &str, dists: impl FnOnce(f64) -> Res let (pc, pa, pb) = (sk.point(c).unwrap_or_default(), sk.point(fa).unwrap_or_default(), sk.point(fb).unwrap_or_default()); let (u, v) = ((pa - pc).normalized().unwrap_or(Vec2::X), (pb - pc).normalized().unwrap_or(Vec2::Y)); let corner_angle = u.cross(v).atan2(u.dot(v)).abs(); + // Lines in line with each other, or lying on each other, have no corner to cut, as a + // fillet refuses them: on two lines lying on each other the chamfer was a line of no + // length between their cut-back ends. + if !(2e-6..=std::f64::consts::PI - 2e-9).contains(&corner_angle) { + return Err(bad(cmd, "the lines are parallel")); + } let (da, db) = dists(corner_angle)?; let (ta, tb, _, kept) = cut_corner(sk, la, lb, c, fa, fb, da, db, cmd)?; let l = sk.add_line_pts(ta, tb, None)?;