From a147a7c083ba7b7af1a0fb03c39675eb281a70a1 Mon Sep 17 00:00:00 2001 From: dudedagger42 <76926918+dudedagger42@users.noreply.github.com> Date: Fri, 23 Oct 2026 13:44:42 +0000 Subject: [PATCH] Sketch Point: a point put on a midpoint or a curve snap is held there (#4691) (#4745) * Sketch Point: a point put on a midpoint or a curve snap is held there (#4691) The Point tool sent only a snapped point's coordinates and sketch.point took only [x, y], so a point clicked on a line's midpoint (or on a curve, or a model vertex) was free to move off it. sketch.point now takes the snap forms the other draw commands take ("mid:", "on::x,y", {vertex}, {on_edge}); the snap makes the point with its constraint. The tool passes those snaps as themselves; a snap to an existing point still goes as coordinates, where `connect` joins them. Co-Authored-By: Claude Opus 5.5 * Sketch Point: refuse a snap it can't read instead of answering as if it made a point sketch_project::snap_points leaves a snap it can't parse ("on:c1:abc", "on:c1", "on:c1:1,2,3") as it was given, and sketch.point then read that string back as the point it had made: no error, no point, and an id a script would go on to dimension. It now takes the snap only when it names a point of the sketch, and otherwise refuses, listing the snap forms (found by the test agent on #4745). Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 --- crates/engine/src/cmd/sketch.rs | 30 ++++++++++++++++++- .../src/cmd/sketch_constraints_tests.rs | 26 ++++++++++++++++ crates/ui-egui/src/sketch_corner_ui_tests.rs | 17 +++++++++++ crates/ui-egui/src/tools.rs | 7 ++++- 4 files changed, 78 insertions(+), 2 deletions(-) diff --git a/crates/engine/src/cmd/sketch.rs b/crates/engine/src/cmd/sketch.rs index f78b78a5e..01d9dac21 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:\" (held at the line's midpoint), \"on::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 { fn draw_point(s: &mut Session, p: &Value) -> Result { let cmd = "sketch.point"; let id = str_(p, "id").map(str::to_string); + // A point snapped while drawing (`"mid:"`, `"on::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:\", \"on::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.rs index c4e0d8a96..f346b2f37 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.rs index bc9bb338f..f54df7951 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::>()); +} diff --git a/crates/ui-egui/src/tools.rs b/crates/ui-egui/src/tools.rs index 4ea5ef17f..53a66a83b 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, 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)?;