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();