From e4cfbbd0c3a90e20b6c3caa18455a74a0ea91fcc Mon Sep 17 00:00:00 2001 From: semmelrinde <124386289+semmelrinde@users.noreply.github.com> Date: Thu, 29 Oct 2026 02:21:05 +0000 Subject: [PATCH] Sketch Coincident: refuse a line's two ends, as MidPoint refuses its own end (#8611) * Sketch Coincident: refuse a line's two ends Making a line's two ends coincident shrinks it to a point; a Parallel or Perpendicular on it afterwards has no direction to hold and a later solve could throw points far away while reporting solved, as with a line's own end as its midpoint (#8562). Co-Authored-By: Claude Opus 5.5 * Sketch Coincident: keep the own-ends test with the other Coincident tests Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 --- crates/engine/src/cmd/sketch.rs | 13 ++++++++++++- .../src/cmd/sketch_constraints_tests.rs | 19 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/crates/engine/src/cmd/sketch.rs b/crates/engine/src/cmd/sketch.rs index 205363ab8..3b2ac12e0 100644 --- a/crates/engine/src/cmd/sketch.rs +++ b/crates/engine/src/cmd/sketch.rs @@ -1936,7 +1936,7 @@ fn c_coincident(s: &mut Session, p: &Value) -> Result { let rep = solve(&mut sk.clone()); return Ok(json!({"constraint": c.id, "existing": true, "sketch": report_json(&rep)})); } - constrain(s, p, cmd, |sk| { + let pick = |sk: &mut Sketch| -> Result { if let Some((a, b)) = line_points { return Ok(ConstraintKind::Coincident { p: a, q: b }); } @@ -1955,6 +1955,17 @@ fn c_coincident(s: &mut Session, p: &Value) -> Result { } let c = curve_or_axis(sk, bref)?.ok_or_else(|| bad(cmd, format!("unknown entity `{bref}`")))?; Ok(ConstraintKind::PointOnCurve { p: a, c }) + }; + constrain(s, p, cmd, |sk| { + let k = pick(sk)?; + // A line's two ends on each other shrink it to a point, as a line's own end at its + // middle does (see `c_midpoint`). + if let ConstraintKind::Coincident { p: a, q: b } = k + && sk.curves.iter().any(|c| matches!(c.kind, CurveKind::Line { a: x, b: y } if (x, y) == (a, b) || (x, y) == (b, a))) + { + return Err(bad(cmd, "a line's two ends can't be coincident: the line would shrink to a point")); + } + Ok(k) }) } diff --git a/crates/engine/src/cmd/sketch_constraints_tests.rs b/crates/engine/src/cmd/sketch_constraints_tests.rs index 6c12f90dd..1b48667e8 100644 --- a/crates/engine/src/cmd/sketch_constraints_tests.rs +++ b/crates/engine/src/cmd/sketch_constraints_tests.rs @@ -1182,6 +1182,25 @@ fn undoing_a_defining_dimension_removes_the_parameter_too() { /// Coincident on two circles (or arcs) lays the second on the first: one centre and one /// radius, in one step, and the first stays. It used to be refused ("two curves must both be /// straight lines"). +#[test] +fn coincident_refuses_a_lines_two_ends() { + let mut s = new_sketch(); + run(&mut s, "sketch.line", json!({"points":[[0,0],[60,0]],"infer":false,"connect":false})); + run(&mut s, "sketch.line", json!({"points":[[0,20],[60,20]],"infer":false,"connect":false})); + // The line would shrink to a point, leaving a later Parallel or Perpendicular on it no + // direction to hold. + for params in [json!({"a":"l1.start","b":"l1.end"}), json!({"a":"l1.end","b":"l1.start"})] { + let before = s.doc.to_json(); + let undo = s.undo.len(); + let err = s.execute("sketch.constraint.coincident", ¶ms).unwrap_err().to_string(); + assert!(err.contains("shrink to a point"), "{params}: {err}"); + assert_eq!(s.doc.to_json(), before, "{params}"); + assert_eq!(s.undo.len(), undo, "{params}"); + } + // Ends of two different lines still join. + run(&mut s, "sketch.constraint.coincident", json!({"a":"l1.end","b":"l2.end"})); +} + #[test] fn coincident_circles_lie_on_each_other() { let mut s = new_sketch();