SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Sketch: accept curve snaps for Midpoint Line (#5981) (#6041)
Colin Faulkinghamf93913b3d39c
diff --git a/crates/engine/src/cmd/sketch_create.rs b/crates/engine/src/cmd/sketch_create.rsindex 6cbc9fd3c5..babba325fa 100644--- a/crates/engine/src/cmd/sketch_create.rs+++ b/crates/engine/src/cmd/sketch_create.rs@@ -30,7 +30,7 @@ pub static COMMANDS: &[CommandSpec] = &[.at("SKETCH", "CREATE").icon("line_mid").enabled(in_sketch)- .params(concat!("mid: [x,y] or point ref, end: [x,y]; construction?", created_doc!())),+ .params(concat!("mid: [x,y], point ref or snap (\"mid:<line>\", \"on:<curve>:x,y\", model vertex/edge); end: [x,y]; construction?", created_doc!())),CommandSpec::new("sketch.arc.tangent", "Tangent Arc", arc_tangent).at("SKETCH", "CREATE").icon("arc_tangent").enabled(in_sketch).params(concat!("start: end point of a line or arc (\"l1.end\"), end: [x,y]; curve?: the curve to be tangent to (default: the one ending at start)",created_doc!()@@ -698,6 +698,7 @@ fn conic(s: &mut Session, p: &Value) -> Result<Value> {fn midpoint_line(s: &mut Session, p: &Value) -> Result<Value> {let cmd = "sketch.line.midpoint";let end = req_vec2(cmd, p, "end")?;+ let p = &super::sketch_project::snap_points(s, p, cmd)?;let (out, info) = edit(s, p, cmd, false, |sk, doc| {let m = req_parg(sk, doc, p, "mid", cmd)?;let start = m.pos() * 2.0 - end;diff --git a/crates/engine/src/cmd/sketch_modify_tests.rs b/crates/engine/src/cmd/sketch_modify_tests.rsindex e841d3489c..89529ba26c 100644--- a/crates/engine/src/cmd/sketch_modify_tests.rs+++ b/crates/engine/src/cmd/sketch_modify_tests.rs@@ -597,6 +597,71 @@ fn three_point_arc_coordinates_stay_free_and_invalid_references_are_atomic() {}}+#[test]+fn midpoint_line_keeps_curve_and_midpoint_snaps() {+ for (mid, at) in [("on:c1:20,0", Vec2::new(20.0, 0.0)), ("mid:base", Vec2::new(20.0, -10.0))] {+ let mut s = new_sketch();+ run(&mut s, "sketch.circle.center", json!({"center":[0,0],"radius":20}));+ run(&mut s, "sketch.line", json!({"points":[[10,-10],[30,-10]],"ids":["base"]}));+ let before = s.doc.to_json();+ let undo = s.undo.len();+ let out = run(&mut s, "sketch.line.midpoint", json!({"mid":mid,"end":[at.x,at.y + 15.0]}));+ let line = ids(&out["curves"])[0].clone();+ let check = |s: &Session| {+ let sk = sketch(s);+ let l = sk.curve_index(&line).unwrap();+ let a = sk.resolve_point(&format!("{line}.start")).and_then(|i| sk.point(i)).unwrap();+ let b = sk.resolve_point(&format!("{line}.end")).and_then(|i| sk.point(i)).unwrap();+ assert!(a.dist(at - Vec2::new(0.0, 15.0)) < 1e-6);+ assert!(b.dist(at + Vec2::new(0.0, 15.0)) < 1e-6);+ let point = sk+ .constraints+ .iter()+ .find_map(|c| match c.kind {+ ConstraintKind::Midpoint { p, l: target } if target == l => Some(p),+ _ => None,+ })+ .unwrap();+ assert!(sk.point(point).unwrap().dist(at) < 1e-6);+ let reference = sk.curve_index(if mid.starts_with("on:") { "c1" } else { "base" }).unwrap();+ assert!(+ sk.constraints.iter().any(|c| match c.kind {+ ConstraintKind::PointOnCurve { p, c } => mid.starts_with("on:") && p == point && c == reference,+ ConstraintKind::Midpoint { p, l } => mid.starts_with("mid:") && p == point && l == reference,+ _ => false,+ }),+ "the line's middle must remain attached to the snapped geometry"+ );+ };+ check(&s);+ assert_eq!(s.undo.len(), undo + 1, "the snap and line are one edit");+ let after = s.doc.to_json();+ run(&mut s, "edit.undo", json!({}));+ assert_eq!(s.doc.to_json(), before);+ run(&mut s, "edit.redo", json!({}));+ check(&s);+ let mut reopened = Session::new(solidcraft_doc::Document::from_json(&after).unwrap());+ reopened.active_sketch = s.active_sketch;+ check(&reopened);+ }+}++#[test]+fn midpoint_line_rejects_bad_snaps_and_degenerate_lines_atomically() {+ let mut s = new_sketch();+ run(&mut s, "sketch.circle.center", json!({"center":[0,0],"radius":20}));+ s.mark_saved();+ let before = s.doc.to_json();+ let history = (s.undo.len(), s.redo.len());+ for mid in ["on:missing:20,0", "on:c1:NaN,0", "mid:c1", "on:c1:20,0"] {+ // The last snap creates a point before the zero-length line is refused.+ assert!(s.execute("sketch.line.midpoint", &json!({"mid":mid,"end":[20,0]})).is_err(), "{mid}");+ assert_eq!(s.doc.to_json(), before, "failed midpoint {mid} changed the sketch");+ assert_eq!((s.undo.len(), s.redo.len()), history);+ assert!(!s.is_dirty());+ }+}+#[test]fn new_creation_tools() {let mut s = new_sketch();