Skip to content

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

Sketch constraints: refuse one curve picked twice for a two-curve constraint (#8577)

semmelrinde1937dea04b02
diff --git a/crates/engine/src/cmd/sketch.rs b/crates/engine/src/cmd/sketch.rs
index fe626d4c7d..205363ab8f 100644
--- a/crates/engine/src/cmd/sketch.rs
+++ b/crates/engine/src/cmd/sketch.rs
@@ -2028,6 +2028,17 @@ fn two_curves(sk: &Sketch, p: &Value, cmd: &str) -> Result<(usize, usize)> {
Ok((curve_ref(sk, p.get("a"), cmd, "a")?, curve_ref(sk, p.get("b"), cmd, "b")?))
}
+/// The two picks of a two-curve constraint, refused when they are one curve: parallel,
+/// collinear, equal or concentric with itself always holds (it was added as a constraint that
+/// does nothing), and perpendicular or tangent to itself can't, which the solve reported as
+/// geometry that can't move. Coincident already refuses a point with itself.
+fn two_different<T: PartialEq>(cmd: &str, (a, b): (T, T)) -> Result<(T, T)> {
+ if a == b {
+ return Err(bad(cmd, "`a` and `b` are the same curve: pick two different ones"));
+ }
+ Ok((a, b))
+}
+
/// Two curves where either may be a sketch axis (`x` / `y`, see [`line_or_axis_ref`]).
fn two_curves_or_axes(sk: &mut Sketch, p: &Value, cmd: &str) -> Result<(usize, usize)> {
Ok((line_or_axis_ref(sk, p.get("a"), cmd, "a")?, line_or_axis_ref(sk, p.get("b"), cmd, "b")?))
@@ -2036,7 +2047,9 @@ fn two_curves_or_axes(sk: &mut Sketch, p: &Value, cmd: &str) -> Result<(usize, u
fn c_tangent(s: &mut Session, p: &Value) -> Result<Value> {
let p = &super::sketch_project::project_operands(s, p, "sketch.constraint.tangent", &["a", "b"])?;
constrain(s, p, "sketch.constraint.tangent", |sk| {
- two_curves_or_axes(sk, p, "sketch.constraint.tangent").map(|(a, b)| ConstraintKind::Tangent { a, b })
+ two_curves_or_axes(sk, p, "sketch.constraint.tangent")
+ .and_then(|ab| two_different("sketch.constraint.tangent", ab))
+ .map(|(a, b)| ConstraintKind::Tangent { a, b })
})
}
fn c_equal(s: &mut Session, p: &Value) -> Result<Value> {
@@ -2044,7 +2057,11 @@ fn c_equal(s: &mut Session, p: &Value) -> Result<Value> {
return equal_curves(s, p);
}
let p = &super::sketch_project::project_operands(s, p, "sketch.constraint.equal", &["a", "b"])?;
- constrain(s, p, "sketch.constraint.equal", |sk| two_curves(sk, p, "sketch.constraint.equal").map(|(a, b)| ConstraintKind::Equal { a, b }))
+ constrain(s, p, "sketch.constraint.equal", |sk| {
+ two_curves(sk, p, "sketch.constraint.equal")
+ .and_then(|ab| two_different("sketch.constraint.equal", ab))
+ .map(|(a, b)| ConstraintKind::Equal { a, b })
+ })
}
/// Equal over a list of curves (#4985): each one is made equal to the first, in one edit (one
@@ -2081,25 +2098,33 @@ fn equal_curves(s: &mut Session, p: &Value) -> Result<Value> {
fn c_parallel(s: &mut Session, p: &Value) -> Result<Value> {
let p = &super::sketch_project::project_operands(s, p, "sketch.constraint.parallel", &["a", "b"])?;
constrain(s, p, "sketch.constraint.parallel", |sk| {
- two_curves_or_axes(sk, p, "sketch.constraint.parallel").map(|(a, b)| ConstraintKind::Parallel { a, b })
+ two_curves_or_axes(sk, p, "sketch.constraint.parallel")
+ .and_then(|ab| two_different("sketch.constraint.parallel", ab))
+ .map(|(a, b)| ConstraintKind::Parallel { a, b })
})
}
fn c_perpendicular(s: &mut Session, p: &Value) -> Result<Value> {
let p = &super::sketch_project::project_operands(s, p, "sketch.constraint.perpendicular", &["a", "b"])?;
constrain(s, p, "sketch.constraint.perpendicular", |sk| {
- two_curves_or_axes(sk, p, "sketch.constraint.perpendicular").map(|(a, b)| ConstraintKind::Perpendicular { a, b })
+ two_curves_or_axes(sk, p, "sketch.constraint.perpendicular")
+ .and_then(|ab| two_different("sketch.constraint.perpendicular", ab))
+ .map(|(a, b)| ConstraintKind::Perpendicular { a, b })
})
}
fn c_concentric(s: &mut Session, p: &Value) -> Result<Value> {
let p = &super::sketch_project::project_operands(s, p, "sketch.constraint.concentric", &["a", "b"])?;
constrain(s, p, "sketch.constraint.concentric", |sk| {
- two_curves(sk, p, "sketch.constraint.concentric").map(|(a, b)| ConstraintKind::Concentric { a, b })
+ two_curves(sk, p, "sketch.constraint.concentric")
+ .and_then(|ab| two_different("sketch.constraint.concentric", ab))
+ .map(|(a, b)| ConstraintKind::Concentric { a, b })
})
}
fn c_collinear(s: &mut Session, p: &Value) -> Result<Value> {
let p = &super::sketch_project::project_operands(s, p, "sketch.constraint.collinear", &["a", "b"])?;
constrain(s, p, "sketch.constraint.collinear", |sk| {
- two_curves_or_axes(sk, p, "sketch.constraint.collinear").map(|(a, b)| ConstraintKind::Collinear { a, b })
+ two_curves_or_axes(sk, p, "sketch.constraint.collinear")
+ .and_then(|ab| two_different("sketch.constraint.collinear", ab))
+ .map(|(a, b)| ConstraintKind::Collinear { a, b })
})
}
fn c_midpoint(s: &mut Session, p: &Value) -> Result<Value> {
diff --git a/crates/engine/src/cmd/sketch_constraints_tests.rs b/crates/engine/src/cmd/sketch_constraints_tests.rs
index 98f89ab3a3..6c12f90dd0 100644
--- a/crates/engine/src/cmd/sketch_constraints_tests.rs
+++ b/crates/engine/src/cmd/sketch_constraints_tests.rs
@@ -992,6 +992,38 @@ fn midpoint_rejects_wrong_picks_without_changing_the_sketch() {
}
}
+/// A two-curve constraint picked on one curve twice is refused, leaving the sketch and the undo
+/// history as they were. Parallel, collinear, equal or concentric with itself always held (a
+/// constraint that did nothing); perpendicular or tangent to itself never could, and the refusal
+/// blamed geometry that can't move.
+#[test]
+fn a_two_curve_constraint_on_one_curve_twice_is_refused() {
+ let mut s = new_sketch();
+ run(&mut s, "sketch.line", json!({"points":[[0,0],[60,10]],"infer":false,"connect":false}));
+ run(&mut s, "sketch.circle.center", json!({"center":[40,50],"radius":3}));
+ for (cmd, curve) in [
+ ("parallel", "l1"),
+ ("perpendicular", "l1"),
+ ("collinear", "l1"),
+ ("tangent", "c1"),
+ ("equal", "l1"),
+ ("equal", "c1"),
+ ("concentric", "c1"),
+ ("parallel", "x"),
+ ] {
+ let before = s.doc.to_json();
+ let undo = s.undo.len();
+ let err = match s.execute(&format!("sketch.constraint.{cmd}"), &json!({"a":curve,"b":curve})) {
+ Ok(v) => panic!("{cmd} of {curve} with itself was accepted: {v}"),
+ Err(e) => e.to_string(),
+ };
+ assert!(err.contains("same curve"), "{cmd} {curve}: {err}");
+ assert_eq!(s.doc.to_json(), before, "{cmd} {curve}");
+ assert_eq!(s.undo.len(), undo, "{cmd} {curve}");
+ }
+ run(&mut s, "sketch.constraint.parallel", json!({"a":"l1","b":"x"}));
+}
+
/// A parameter's row in `parameters.list` (Null when there is none).
fn param_row(s: &mut Session, name: &str) -> Value {
let l = run(s, "parameters.list", json!({}));