diff --git a/assets/locales/pending/sketch-move-points.json b/assets/locales/pending/sketch-move-points.json new file mode 100644 index 000000000..525b02aca --- /dev/null +++ b/assets/locales/pending/sketch-move-points.json @@ -0,0 +1,8 @@ +{ + "en": { "cmd.sketch_move_points": "Move Sketch Points" }, + "ar": { "cmd.sketch_move_points": "سحب نقاط الرسم" }, + "he": { "cmd.sketch_move_points": "גרירת נקודות סקיצה" }, + "ja": { "cmd.sketch_move_points": "スケッチ点を移動" }, + "vi": { "cmd.sketch_move_points": "Kéo các điểm phác thảo" }, + "zh": { "cmd.sketch_move_points": "移动草图点" } +} diff --git a/crates/engine/src/cmd/sketch.rs b/crates/engine/src/cmd/sketch.rs index e14110f47..630683777 100644 --- a/crates/engine/src/cmd/sketch.rs +++ b/crates/engine/src/cmd/sketch.rs @@ -208,6 +208,11 @@ pub static COMMANDS: &[CommandSpec] = &[ .enabled(in_sketch) .params("entities: [curve, point or constraint ids]"), CommandSpec::new("sketch.move_point", "Drag Sketch Point", move_point).icon("sketch_move").enabled(in_sketch).params("point: ref, to: [x,y]"), + CommandSpec::new("sketch.move_points", "Move Sketch Points", move_points).icon("sketch_move").enabled(in_sketch).params( + "moves: [{point: ref | near: [x,y], to: [x,y]}] (each point to its own target; `near`: the sketch point closest to it, \ + refused when none lies within `tolerance` or two are as close), tolerance?: mm (default 0.1); sketch? → moved: \ + [{point, to, at, reached}] (`at`: where it ended up after the one solve; `reached`: whether that is `to`)", + ), CommandSpec::new("sketch.drag", "Drag Sketch Geometry", drag).icon("sketch_move").enabled(in_sketch).live(drag_live).params( "entity: point or curve, from: [x,y] where it was grabbed, to: [x,y], live?: bool: points and the material point grabbed on a line move toward `to` within the design constraints. When exact placement is impossible, bounded feasible steps minimize cursor distance. Splines slide by to - from; a circle or arc grabbed on its edge takes the radius to `to`. `with?`: [point or curve ids] moves the rest of a selection with the entity as one translation (circles keep their radii). Long moves retain the solution branch; arcs cannot sweep through their starts. Returns `reached`: progress projected onto the requested move (0..1), `at`: the grabbed position for the next frame, and `retry`: budget-limited progress can continue in another frame. `live: true` shares a work budget across held-solve decomposition and cursor correction; ordinary recovery and the final model evaluation retain their existing limits, not a wall-clock deadline. Later features stay stale until `sketch.drag_end`; `dof` stays from before the drag", ), @@ -3725,6 +3730,67 @@ fn move_point(s: &mut Session, p: &Value) -> Result { Ok(json!({"sketch": rep.as_ref().map(report_json)})) } +/// `sketch.move_points`: several points, each dragged to its own target, in one edit (one undo +/// step), then solved; each move's point by reference or by `near` (#5961). +fn move_points(s: &mut Session, p: &Value) -> Result { + let cmd = "sketch.move_points"; + let moves = + p.get("moves").and_then(Value::as_array).filter(|m| !m.is_empty()).ok_or_else(|| bad(cmd, "`moves` must be a list of {point | near, to}"))?; + let tol = match p.get("tolerance") { + None | Some(Value::Null) => 0.1, + Some(_) => num(p, "tolerance").filter(|t| *t > 0.0).ok_or_else(|| bad(cmd, "`tolerance` must be a positive length (mm)"))?, + }; + let id = sketch_store::target(s, p, cmd)?; + let state = s.model.result(id).map(|r| r.state.clone()); + let last = state.as_ref().and_then(|state| state.sketch(id)); + let (picked, rep) = edit_solved(s, id, false, Analysis::Freedom, |sk, _| { + if moves.len() > sk.points.len().max(1) { + return Err(bad(cmd, "`moves` lists more moves than the sketch has points")); + } + // Every point is found before any moves, so `near` reads the sketch as it was. + let mut picked = Vec::with_capacity(moves.len()); + for (k, m) in moves.iter().enumerate() { + let to = req_vec2(cmd, m, "to").map_err(|_| bad(cmd, format!("moves[{k}]: `to` must be a point [x, y]")))?; + let q = match m.get("near") { + Some(v) => { + let at = vec2(v).ok_or_else(|| bad(cmd, format!("moves[{k}]: `near` must be a point [x, y]")))?; + let mut d: Vec<(f64, usize)> = (0..sk.points.len()).filter_map(|i| Some((sk.point(i)?.dist(at), i))).collect(); + d.sort_by(|a, b| a.0.total_cmp(&b.0)); + match d.as_slice() { + [(best, q), rest @ ..] if *best <= tol => { + if rest.first().is_some_and(|(next, _)| *next - *best <= 1e-9) { + return Err(bad( + cmd, + format!("moves[{k}]: two sketch points are as close to `near` [{}, {}]; name the point", at.x, at.y), + )); + } + *q + } + _ => return Err(bad(cmd, format!("moves[{k}]: no sketch point within {tol} mm of `near` [{}, {}]", at.x, at.y))), + } + } + None => point_ref(sk, m.get("point"), cmd, "point")?, + }; + picked.push((q, to)); + } + let mut left = Left::Unsolved; + for (q, to) in &picked { + left = drag_entity(sk, Ent::Point(*q), &[], *to, *to, false, last).1.solve_again(); + } + Ok((picked, left)) + })?; + let solved = sketch_store::read(&s.doc, id).ok(); + let moved: Vec = picked + .iter() + .map(|(q, to)| { + let point = solved.and_then(|sk| sk.points.get(*q)).map(|pt| pt.id.clone()); + let at = solved.and_then(|sk| sk.point(*q)); + json!({"point": point, "to": [to.x, to.y], "at": at.map(|a| [a.x, a.y]), "reached": at.is_some_and(|a| a.dist(*to) <= 1e-6)}) + }) + .collect(); + Ok(json!({"moved": moved, "sketch": rep.as_ref().map(report_json)})) +} + fn drag(s: &mut Session, p: &Value) -> Result { drag_frame(s, p, false).map(|(v, _)| v) } diff --git a/crates/engine/src/cmd/sketch_ux_tests.rs b/crates/engine/src/cmd/sketch_ux_tests.rs index 8986834e6..8b6bb1b10 100644 --- a/crates/engine/src/cmd/sketch_ux_tests.rs +++ b/crates/engine/src/cmd/sketch_ux_tests.rs @@ -2481,3 +2481,55 @@ fn a_dimension_refuses_a_long_list_of_entities() { // The refused request leaves the design as it was. assert_eq!(serde_json::to_value(&s.doc).unwrap(), before); } + +/// #5961: `sketch.move_points` moves several points of a polyline, each to its own target, by id +/// or by `near`, in one undo step; it says where each ended up, refuses a `near` that finds +/// nothing or two points as close, and reports a fixed point that can't move. +#[test] +fn move_points_moves_each_point_to_its_own_target() { + let mut s = new_sketch(); + run(&mut s, "sketch.line", json!({"points": [[0, 0], [10, 0], [20, 0], [30, 0]]})); + let undo_before = run(&mut s, "document.inspect", json!({}))["undo"].clone(); + let r = run(&mut s, "sketch.move_points", json!({"moves": [{"point": "l1.end", "to": [10, 5]}, {"near": [20.05, 0], "to": [20, -5]}]})); + let moved = r["moved"].as_array().unwrap(); + assert_eq!(moved.len(), 2, "{r}"); + assert!(moved.iter().all(|m| m["reached"] == true), "{r}"); + assert_eq!(moved[1]["to"], json!([20.0, -5.0])); + let sk = sketch(&s); + let at = |id: &str| sk.point(sk.resolve_point(id).unwrap()).unwrap(); + assert!(at("l1.end").dist(solidcore_record::Vec2::new(10.0, 5.0)) < 1e-9); + assert!(at("l2.end").dist(solidcore_record::Vec2::new(20.0, -5.0)) < 1e-9); + assert!(at("l3.end").dist(solidcore_record::Vec2::new(30.0, 0.0)) < 1e-9, "the other points stay"); + // One undo step takes both back. + let undo_after = run(&mut s, "document.inspect", json!({}))["undo"].as_u64().unwrap(); + assert_eq!(undo_after, undo_before.as_u64().unwrap() + 1); + run(&mut s, "edit.undo", json!({})); + assert!(pt(&s, "l1.end").dist(solidcore_record::Vec2::new(10.0, 0.0)) < 1e-9); + // `near` that finds nothing, or a malformed move, is refused and moves nothing. + for bad in [ + json!({"moves": [{"near": [100, 100], "to": [0, 0]}]}), + json!({"moves": [{"near": [10, 0]}]}), + json!({"moves": []}), + json!({"moves": [{"point": "l1.end", "to": [1, 1]}], "tolerance": -1}), + ] { + let before = sketch(&s); + assert!(s.execute("sketch.move_points", &bad).is_err(), "{bad}"); + assert_eq!(sketch(&s), before, "{bad}"); + } + // A fixed point is reported, not silently left where it was. + run(&mut s, "sketch.constraint.fix", json!({"entities": ["l3.end"]})); + let r = run(&mut s, "sketch.move_points", json!({"moves": [{"point": "l3.end", "to": [40, 0]}]})); + assert_eq!(r["moved"][0]["reached"], false, "{r}"); + assert_eq!(r["moved"][0]["at"], json!([30.0, 0.0]), "{r}"); +} + +/// Two sketch points as close to `near` (two points at one place) can't be told apart: the +/// move is refused. +#[test] +fn move_points_refuses_a_near_that_two_points_answer() { + let mut s = new_sketch(); + run(&mut s, "sketch.point", json!({"point": [10, 0]})); + run(&mut s, "sketch.point", json!({"point": [10, 0]})); + let e = s.execute("sketch.move_points", &json!({"moves": [{"near": [10, 0], "to": [12, 0]}]})).unwrap_err().to_string(); + assert!(e.contains("as close"), "{e}"); +}