fix: skip impossible drags of determined sketch geometry (#2006) (#2199)

Co-authored-by: Krzysztof Heim <krzysztof.heim@b4bridge.com>
This commit is contained in:
Krzysztof Heim
2026-10-20 20:56:50 +00:00
committed by GitHub
parent 687caad24c
commit 1b84ce6330
3 changed files with 80 additions and 5 deletions
+7 -4
View File
@@ -2513,9 +2513,11 @@ fn move_point(s: &mut Session, p: &Value) -> Result<Value> {
let cmd = "sketch.move_point";
let to = req_vec2(cmd, p, "to")?;
let id = target_sketch(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 (_, rep) = edit_solved(s, id, false, Analysis::Freedom, |sk, _| {
let q = point_ref(sk, p.get("point"), cmd, "point")?;
Ok(((), drag_entity(sk, Ent::Point(q), &[], to, to, false).1.solve_again()))
Ok(((), drag_entity(sk, Ent::Point(q), &[], to, to, false, last).1.solve_again()))
})?;
Ok(json!({"sketch": rep.as_ref().map(report_json)}))
}
@@ -2538,7 +2540,8 @@ fn drag_frame(s: &mut Session, p: &Value, live: bool) -> Result<(Value, Option<c
};
let id = target_sketch(s, p, cmd)?;
// The sketch's report as last evaluated: a live frame keeps its degrees of freedom.
let last = s.model.result(id).and_then(|r| r.state.sketch(id)).map(|ss| ss.report.clone());
let state = s.model.result(id).map(|r| r.state.clone());
let last = state.as_ref().and_then(|state| state.sketch(id));
let analysis = if live { Analysis::Full } else { Analysis::Freedom };
let (d, rep) = edit_solved(s, id, false, analysis, |sk, doc| {
let r = p.get("entity").and_then(Value::as_str).ok_or_else(|| bad(cmd, "`entity` must be a sketch point or curve"))?;
@@ -2565,7 +2568,7 @@ fn drag_frame(s: &mut Session, p: &Value, live: bool) -> Result<(Value, Option<c
}
// The held solves work on the dimensions' current values (a solved step applies them).
doc.apply_dimension_values(&doc.param_values().0, sk)?;
let (d, left) = drag_entity(sk, e, &with, from, to, live);
let (d, left) = drag_entity(sk, e, &with, from, to, live, last);
// A live frame's held solve is its only one; otherwise the sketch's own solve reports
// its degrees of freedom.
Ok((d, if live && last.is_some() { left } else { left.solve_again() }))
@@ -2574,7 +2577,7 @@ fn drag_frame(s: &mut Session, p: &Value, live: bool) -> Result<(Value, Option<c
// average displacement of their moving points, as before.
let sk = s.doc.sketch(id)?;
let at = d.at(sk, from);
let rep = rep.or_else(|| last.map(|last| SolveReport { status: solidcraft_sketch::SolveStatus::Solved, failing: Vec::new(), ..last }));
let rep = rep.or_else(|| last.map(|ss| SolveReport { status: solidcraft_sketch::SolveStatus::Solved, failing: Vec::new(), ..ss.report.clone() }));
let out = json!({"sketch": rep.as_ref().map(report_json), "reached": d.reached, "at": [at.x, at.y], "retry": d.retry});
Ok((out, rep.filter(|_| live).map(|report| crate::LiveEdit { sketch: id, report })))
}
+27 -1
View File
@@ -125,7 +125,33 @@ impl Path {
///
/// `live`: one frame of an interactive drag, whose held solves and cursor correction share
/// one frame's work budget ([`Budget::frame`]); otherwise each has a solve's own.
pub(super) fn drag_entity(sk: &mut Sketch, e: Ent, with: &[Ent], from: Vec2, to: Vec2, live: bool) -> (Dragged, Left) {
///
/// `solved`: the sketch as last evaluated. When it is unchanged and solved, a grabbed entity it
/// determined is not dragged.
pub(super) fn drag_entity(
sk: &mut Sketch,
e: Ent,
with: &[Ent],
from: Vec2,
to: Vec2,
live: bool,
solved: Option<&solidcraft_doc::SolvedSketch>,
) -> (Dragged, Left) {
// An unchanged, solved entity with no freedom cannot follow a drag. Holding it at a
// different position only makes its constraints conflict, and a large pattern would spend
// minutes trying dense solves before returning to the same geometry (#2006).
if solved.is_some_and(|ss| {
ss.report.ok()
&& ss.sketch == *sk
&& match e {
Ent::Point(q) => ss.report.point_determined.get(q),
Ent::Curve(c) => ss.report.curve_determined.get(c),
}
.copied()
.unwrap_or(false)
}) {
return (Dragged::default(), Left::Solved);
}
let Some((path, hold, n, short)) = prepare(sk, e, with, from, to) else {
return (Dragged::default(), Left::Unsolved);
};
@@ -7,6 +7,52 @@ use solidcraft_geom::Vec3;
use super::solve_counts;
use crate::Session;
#[test]
fn determined_pattern_geometry_does_not_attempt_conflicting_held_solves() {
let mut s = Session::default();
run(&mut s, "sketch.create", json!({"plane": "XY"}));
run(&mut s, "sketch.rectangle.two_point", json!({"p0": [5, 5], "p1": [6.5, 6.5]}));
run(&mut s, "sketch.dimension", json!({"entities": ["l1"], "value": 1.5}));
run(&mut s, "sketch.dimension", json!({"entities": ["l2"], "value": 1.5}));
run(&mut s, "sketch.constraint.fix", json!({"entities": ["l1.start"]}));
let pattern = run(
&mut s,
"sketch.pattern.rectangular",
json!({"entities": ["l1", "l2", "l3", "l4"], "count": 3, "count2": 3, "spacing": 2.2, "spacing2": -2.2}),
);
let line = pattern["curves"][0].as_str().expect("a patterned line");
let endpoint = format!("{line}.start");
let before = sketch_of(&s);
assert!(!before.curve_locked(before.curve_index(line).expect("patterned line index")));
for entity in [line, endpoint.as_str()] {
for live in [true, false] {
reset(&mut s);
let result = run(&mut s, "sketch.drag", json!({"entity": entity, "from": [5, 5], "to": [9, 12], "live": live}));
assert_eq!(result["reached"], json!(0.0));
assert_eq!(result["at"], json!([5.0, 5.0]));
assert_eq!(result["sketch"]["solved"], json!(true));
let full = usize::from(!live);
assert_eq!(solve_counts::take(), (full, full), "no held solve for determined geometry");
assert_eq!(sketch_of(&s), before);
}
}
reset(&mut s);
run(&mut s, "sketch.move_point", json!({"point": endpoint, "to": [9, 12]}));
assert_eq!(solve_counts::take(), (1, 1), "move_point also skips held solves");
assert_eq!(sketch_of(&s), before);
// A cached determined report must not suppress an outstanding dimension edit.
let dimension = before
.constraints
.iter()
.find(|c| matches!(c.kind, solidcraft_sketch::ConstraintKind::Length { l: 0, .. }))
.and_then(|c| c.param.as_ref())
.expect("the first line's dimension");
s.doc_mut().params.iter_mut().find(|p| &p.name == dimension).expect("dimension parameter").expr = "3".into();
run(&mut s, "sketch.drag", json!({"entity": line, "from": [5, 5], "to": [9, 12], "live": true}));
assert!((pt(&s, "l1.start").dist(pt(&s, "l1.end")) - 3.0).abs() < 1e-6);
}
fn run(s: &mut Session, id: &str, p: Value) -> Value {
match s.execute(id, &p) {
Ok(v) => v,