SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Sketch Point: a point put on a midpoint or a curve snap is held there (#4691) (#4745)
dudedagger42a147a7c083ba
diff --git a/crates/engine/src/cmd/sketch.rs b/crates/engine/src/cmd/sketch.rsindex f78b78a5ee..01d9dac218 100644--- a/crates/engine/src/cmd/sketch.rs+++ b/crates/engine/src/cmd/sketch.rs@@ -113,7 +113,7 @@ pub static COMMANDS: &[CommandSpec] = &[.icon("slot_overall").enabled(in_sketch).params(concat!("p0, p1: overall ends, width", connect_doc!(), created_doc!())),- CommandSpec::new("sketch.point", "Point", draw_point).at("SKETCH", "CREATE").icon("point").enabled(in_sketch).params(concat!("point: [x,y] (each a number or an expression over the design's parameters: the point is then fixed where it comes to and follows them), id?", connect_doc!(), created_doc!())),+ CommandSpec::new("sketch.point", "Point", draw_point).at("SKETCH", "CREATE").icon("point").enabled(in_sketch).params(concat!("point: [x,y] (each a number or an expression over the design's parameters: the point is then fixed where it comes to and follows them) or a snap: \"mid:<line>\" (held at the line's midpoint), \"on:<curve>:x,y\" (on the curve near x,y), {vertex: [x,y,z]} or {on_edge: [x,y,z]} of the model; id?", connect_doc!(), created_doc!())),CommandSpec::new("sketch.dimension", "Sketch Dimension", dimension).at("SKETCH", "CREATE").icon("dimension").key("D").enabled(in_sketch).params("entities: [refs], type?: auto|distance|horizontal|vertical|length|radius|diameter|angle, value?: number or expression (default: current; a bare number is in the design's units, e.g. 2 means 2 in in an inch design; give a unit such as \"50 mm\" to override), or `name = expression` (e.g. \"width = 40\"), which adds the user parameter `name` with that expression and dimensions with `name` (refused when the name is taken or reserved; the reply's `defined` names it); text_at?: [x, y]. Two points or a line off the axes: with auto, text_at across from them is the distance between them, above or below them horizontal, beside them vertical. Two lines: the angle between them (acute, or 180° − it when the value is nearer that) unless text_at picks a sector. A line and a circle or arc: the distance from the line to its edge (the gap, or how far the edge reaches past a line that cuts it; 0 is tangent); dimension the centre point for the distance to the centre. A dimension the sketch already determines is refused (naming what determines it); if_redundant?: \"driven\" places it as a driven (reference) dimension instead. scale_sketch?: bool (the dimension tool sends true): when this is the sketch's first length dimension and nothing in the sketch fixes a size (no other driving length dimension, nothing fixed or projected), the whole sketch scales about its origin to the value instead of distorting. Returns measured in unit (mm or deg), and scaled: the factor when the sketch was scaled",),@@ -1386,6 +1386,34 @@ fn slot_overall(s: &mut Session, p: &Value) -> Result<Value> {fn draw_point(s: &mut Session, p: &Value) -> Result<Value> {let cmd = "sketch.point";let id = str_(p, "id").map(str::to_string);+ // A point snapped while drawing (`"mid:<line>"`, `"on:<curve>:x,y"`, a model vertex or edge):+ // the snap makes it, held by its constraint, as it does for a line's end. Taken as bare+ // coordinates it was free to move off the midpoint it was put on (#4691).+ let snapped = p.get("point").is_some_and(|v| {+ v.as_str().is_some_and(|t| ["mid:", "on:", "vertex:"].iter().any(|k| t.starts_with(k)))+ || v.get("vertex").is_some()+ || v.get("on_edge").is_some()+ });+ if snapped {+ let given = p.get("point").cloned().unwrap_or(Value::Null);+ let p = &super::sketch_project::snap_points(s, p, cmd)?;+ // A snap it can't read is left as it was given, which names no point: refused, as a line's+ // end refuses it, rather than answered as if a point had been made.+ let made = str_(p, "point")+ .filter(|_| p.get("point") != Some(&given))+ .filter(|id| target_sketch(s, p, cmd).ok().and_then(|sid| s.doc.sketch(sid).ok()).is_some_and(|sk| sk.resolve_point(id).is_some()))+ .map(str::to_string);+ let Some(pid) = made else {+ return Err(bad(+ cmd,+ format!(+ "`point` {given} is not a snap it takes: \"mid:<line>\", \"on:<curve>:x,y\", \"vertex:x,y,z\", {{vertex: [x,y,z]}} or {{on_edge: [x,y,z]}}"+ ),+ ));+ };+ let ((), info) = edit(s, p, cmd, false, |_, _| Ok(()))?;+ return Ok(json!({"point": pid, "constraints": [], "sketch": info}));+ }let ((pid, cons), info) = edit(s, p, cmd, false, |sk, doc| {let i = match expr_point(doc, p.get("point").unwrap_or(&Value::Null), cmd)? {Some((at, Some(exprs))) => add_placed_point(sk, at, exprs, id.as_deref())?,diff --git a/crates/engine/src/cmd/sketch_constraints_tests.rs b/crates/engine/src/cmd/sketch_constraints_tests.rsindex c4e0d8a964..f346b2f370 100644--- a/crates/engine/src/cmd/sketch_constraints_tests.rs+++ b/crates/engine/src/cmd/sketch_constraints_tests.rs@@ -221,6 +221,32 @@ fn smart_constrain_and_driven_toggle() {assert_eq!(r["driven"], false);}+/// #4691: a point put on a line's midpoint snap is held there (a MidPoint constraint), and one+/// put on a curve stays on it, as a line's end does: they no longer move freely.+#[test]+fn a_point_on_a_midpoint_or_a_curve_snap_is_held_there() {+ let mut s = new_sketch();+ let l = ids(&run(&mut s, "sketch.line", json!({"points": [[0, 0], [20, 0]]}))["curves"])[0].clone();+ let m = run(&mut s, "sketch.point", json!({"point": format!("mid:{l}")}))["point"].as_str().unwrap().to_string();+ let on = run(&mut s, "sketch.point", json!({"point": format!("on:{l}:5,0.2")}))["point"].as_str().unwrap().to_string();+ let si = run(&mut s, "sketch.inspect", json!({}));+ let names: Vec<&str> = si["constraints"].as_array().unwrap().iter().filter_map(|c| c["name"].as_str()).collect();+ assert!(names.contains(&"MidPoint") && names.contains(&"Coincident"), "{si}");+ // Lengthen the line: the midpoint's point follows the middle, the other stays on the line.+ run(&mut s, "sketch.move_point", json!({"point": format!("{l}.end"), "to": [40, 0]}));+ let sk = sketch(&s);+ let at = |id: &str| sk.resolve_point(id).and_then(|i| sk.point(i)).unwrap();+ assert!(at(&m).dist(Vec2::new(20.0, 0.0)) < 1e-7, "{:?}", at(&m));+ assert!(at(&on).y.abs() < 1e-7, "{:?}", at(&on));+ // A snap that can't be read is refused and makes nothing (it was answered as a point).+ let points = sk.points.len();+ for bad in ["on:c1:abc", "on:c1", "on:c1:1,2,3", "on:l1:1"] {+ let e = s.execute("sketch.point", &json!({"point": bad.replace("l1", &l)})).unwrap_err().to_string();+ assert!(e.contains("is not a snap it takes"), "{bad}: {e}");+ }+ assert_eq!(sketch(&s).points.len(), points, "nothing made");+}+#[test]fn drawing_from_a_line_midpoint_keeps_it_there() {let mut s = new_sketch();diff --git a/crates/ui-egui/src/sketch_corner_ui_tests.rs b/crates/ui-egui/src/sketch_corner_ui_tests.rsindex bc9bb338f2..f54df79515 100644--- a/crates/ui-egui/src/sketch_corner_ui_tests.rs+++ b/crates/ui-egui/src/sketch_corner_ui_tests.rs@@ -709,3 +709,20 @@ fn centre_circle_clicked_through_a_line_end_stays_on_it() {sk.constraints);}++/// #4691: the Point tool clicked on a line's midpoint makes a point held there (a MidPoint+/// constraint), not a free point that happens to sit on the middle.+#[test]+fn a_point_clicked_on_a_line_midpoint_is_held_there() {+ let mut h = crate::scenario::Harness::new();+ h.app.run("sketch.create", json!({"plane":"XY"})).unwrap();+ h.app.run("sketch.line", json!({"points":[[0,0],[20,0]],"ids":["base"]})).unwrap();+ h.settle();+ h.step(&json!({"start":"sketch.point"})).unwrap();+ h.step(&json!({"click":{"world":[10, 0, 0]}})).unwrap();+ let sid = h.app.session.active_sketch.unwrap();+ let state = h.app.session.model.state();+ let sk = &state.sketch(sid).unwrap().sketch;+ let held = sk.constraints.iter().any(|c| matches!(c.kind, solidcraft_engine::sketch::ConstraintKind::Midpoint { .. }));+ assert!(held, "no MidPoint constraint: {:?}", sk.constraints.iter().map(|c| &c.kind).collect::<Vec<_>>());+}diff --git a/crates/ui-egui/src/tools.rs b/crates/ui-egui/src/tools.rsindex 4ea5ef17f6..53a66a83b7 100644--- a/crates/ui-egui/src/tools.rs+++ b/crates/ui-egui/src/tools.rs@@ -452,7 +452,12 @@ fn run_shape(app: &mut SolveApp, t: &Tool) -> Result<Vec<String>, String> {let width = crate::sketch_dims::locked_value(app, t, "Width").unwrap_or_else(|| (2.0 * dir.cross(p[2].0 - p[0].0).abs()).max(1e-3));json!({"p0": xy(&p[0]), "p1": xy(&p[1]), "width": width})}- "sketch.point" => json!({"point": xy(&p[0])}),+ // A midpoint or on-curve snap goes as itself, so the point is held there (#4691); a+ // snap to an existing point goes as its place, where `connect` joins them.+ "sketch.point" => match &p[0].1 {+ Some(id) if ["mid:", "on:", "vertex:"].iter().any(|k| id.starts_with(k)) => json!({"point": id}),+ _ => json!({"point": xy(&p[0])}),+ },_ => return Err(format!("drawing {} is not supported", t.cmd)),};let result = draw(app, t, params)?;