Skip to content

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

Sketch MidPoint: refuse a line's own end as its midpoint (#8562)

semmelrinde17807ab6dcaa
diff --git a/crates/engine/src/cmd/sketch.rs b/crates/engine/src/cmd/sketch.rs
index 6643d3514a..fe626d4c7d 100644
--- a/crates/engine/src/cmd/sketch.rs
+++ b/crates/engine/src/cmd/sketch.rs
@@ -2106,7 +2106,7 @@ fn c_midpoint(s: &mut Session, p: &Value) -> Result<Value> {
let cmd = "sketch.constraint.midpoint";
let p = &super::sketch_project::project_operands(s, p, cmd, &["a", "b", "point", "line"])?;
let p = &line_stays(s, p, cmd);
- constrain(s, p, cmd, |sk| {
+ let pick = |sk: &mut Sketch| -> Result<ConstraintKind> {
if p.get("a").is_none() && p.get("b").is_none() {
return Ok(ConstraintKind::Midpoint { p: point_ref(sk, p.get("point"), cmd, "point")?, l: curve_ref(sk, p.get("line"), cmd, "line")? });
}
@@ -2139,6 +2139,18 @@ fn c_midpoint(s: &mut Session, p: &Value) -> Result<Value> {
}
_ => Err(bad(cmd, "MidPoint needs a point and a line, or two lines")),
}
+ };
+ constrain(s, p, cmd, |sk| {
+ let k = pick(sk)?;
+ // A line's own end at its middle shrinks the line to a point; an angle, tangent or
+ // parallel on it afterwards had no direction to hold and threw points millions of
+ // millimetres away.
+ if let ConstraintKind::Midpoint { p: q, l } = k
+ && matches!(sk.curves.get(l).map(|c| &c.kind), Some(CurveKind::Line { a, b }) if *a == q || *b == q)
+ {
+ return Err(bad(cmd, "a line's own end can't be its midpoint: 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 029a24af6a..98f89ab3a3 100644
--- a/crates/engine/src/cmd/sketch_constraints_tests.rs
+++ b/crates/engine/src/cmd/sketch_constraints_tests.rs
@@ -979,6 +979,10 @@ fn midpoint_rejects_wrong_picks_without_changing_the_sketch() {
json!({"a":"l1","b":"missing"}),
json!({"a":"l1"}),
json!({"a":12,"b":"l1"}),
+ // The line's own end: it would shrink the line to a point.
+ json!({"a":"l1.end","b":"l1"}),
+ json!({"a":"l1","b":"l1.start"}),
+ json!({"point":"l1.end","line":"l1"}),
] {
let before = s.doc.to_json();
let undo = s.undo.len();