* claim: #5961 * Sketch: sketch.move_points moves several points, each to its own target (#5961) A script editing a traced outline could move one sketch point per call, and only by its id: an agent that had to move two points of a 490-segment polyline redrew the whole sketch instead, breaking every dimension and feature built on it. sketch.move_points {moves: [{point | near, to}], tolerance?} drags each point to its own target in one edit (one undo step), as sketch.move_point drags one, and the sketch is solved. `near` picks the sketch point closest to a coordinate, refused when none lies within `tolerance` (default 0.1 mm) or two are as close. Every point is found before any moves. The reply gives each point's id, its target, where it ended up and whether it got there, so a fixed or constrained point is reported rather than silently left in place. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Move the new command label to a pending locale file Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> * Fix the new command for the sketch store and geometry split Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> * Carry the new command label in every complete catalog Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> * sketch.move_points reads the sketch through sketch_store Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: bherbruck <brennen.herbruck@gmail.com>
This commit is contained in:
3 files changed
+126
No files matched your search
@@ -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": "移动草图点" }
|
||||
}
|
||||
@@ -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<Value> {
|
||||
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<Value> {
|
||||
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<Value> = 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<Value> {
|
||||
drag_frame(s, p, false).map(|(v, _)| v)
|
||||
}
|
||||
|
||||
@@ -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}");
|
||||
}
|
||||
Reference in new issue
Block a user