From 2e6e05bc9ae74853ac68005f994243b56c4bf5b3 Mon Sep 17 00:00:00 2001 From: raykholo Date: Sun, 25 Oct 2026 17:56:08 +0000 Subject: [PATCH] Sketch pattern edit shows its preview while dragging (#4412) (#4426) --- crates/ui-egui/src/preview.rs | 75 ++++++++++++++++++-- crates/ui-egui/src/sketch_corner_ui_tests.rs | 10 +-- 2 files changed, 76 insertions(+), 9 deletions(-) diff --git a/crates/ui-egui/src/preview.rs b/crates/ui-egui/src/preview.rs index 7d133b013..0638ac1de 100644 --- a/crates/ui-egui/src/preview.rs +++ b/crates/ui-egui/src/preview.rs @@ -667,15 +667,20 @@ pub fn build(before: &ModelState, after: &ModelState, c: Colors) -> Built { .iter() .filter_map(|sketch| { let old = before.sketch(sketch.feature); - let old_curves = old.into_iter().flat_map(|s| s.sketch.curves.iter().map(|c| c.id.as_str())).collect::>(); - let old_points = old.into_iter().flat_map(|s| s.sketch.points.iter().map(|p| p.id.as_str())).collect::>(); + let old_curves = + old.into_iter().flat_map(|s| s.sketch.curves.iter().map(|c| (c.id.as_str(), c))).collect::>(); + let old_points = + old.into_iter().flat_map(|s| s.sketch.points.iter().map(|p| (p.id.as_str(), p.pos))).collect::>(); let indices = sketch .sketch .curves .iter() .enumerate() .filter_map(|(index, curve)| { - let unchanged = old_curves.contains(curve.id.as_str()); + let unchanged = old_curves.get(curve.id.as_str()).is_some_and(|previous| { + previous.kind == curve.kind + && curve.kind.point_ids().iter().all(|i| old.and_then(|s| s.sketch.point(*i)) == sketch.sketch.point(*i)) + }); if unchanged { None } else { Some(index) } }) .collect::>(); @@ -684,7 +689,7 @@ pub fn build(before: &ModelState, after: &ModelState, c: Colors) -> Built { .points .iter() .enumerate() - .filter_map(|(i, p)| if old_points.contains(p.id.as_str()) { None } else { Some(i) }) + .filter_map(|(i, p)| if old_points.get(p.id.as_str()) == Some(&p.pos) { None } else { Some(i) }) .collect::>(); if indices.is_empty() && points.is_empty() { None } else { Some(SketchPreview { sketch: sketch.clone(), curves: indices, points }) } }) @@ -1019,6 +1024,68 @@ mod tests { assert_eq!(motion_key(&cam("0 deg"), 7, colors()), motion_key(&cam("90 deg"), 7, colors())); } + #[test] + fn edited_pattern_preview_includes_moved_instances_only() -> Result<(), Box> { + for (command, initial, edits) in [ + ( + "sketch.pattern.rectangular", + json!({"entities":["seed"],"count":3,"spacing":10,"count2":2,"spacing2":15}), + vec![(json!({"spacing":14}), 4), (json!({"spacing2":19}), 3)], + ), + ( + "sketch.pattern.circular", + json!({"entities":["seed"],"center":"origin","count":5}), + vec![(json!({"count":20}), 19), (json!({"count":2}), 1)], + ), + ] { + let mut s = Session::default(); + s.execute("sketch.create", &json!({"plane":"XY"}))?; + s.execute("sketch.circle.center", &json!({"center":[-50,0],"radius":2,"id":"seed"}))?; + let pattern = s.execute(command, &initial)?; + // An identical result must not draw the seed or any existing copies. + let unchanged = compute(&s, &[(command.into(), json!({"pattern":pattern["pattern"]}))], colors())?; + assert!(unchanged.sketches.is_empty()); + for (mut edit, expected) in edits { + edit["pattern"] = pattern["pattern"].clone(); + let built = compute(&s, &[(command.into(), edit)], colors())?; + assert_eq!(built.sketches.len(), 1, "{command}"); + for preview in &built.sketches { + assert_eq!(preview.curves.len(), expected, "{command}"); + assert_eq!(preview.points.len(), expected, "{command}"); + assert!(preview.curves.iter().all(|i| preview.sketch.sketch.curves.get(*i).is_some_and(|c| c.id != "seed"))); + } + } + } + Ok(()) + } + + #[test] + fn sketch_preview_includes_changed_curve_shapes() -> Result<(), Box> { + let mut s = Session::default(); + s.execute("sketch.create", &json!({"plane":"XY"}))?; + s.execute("sketch.circle.center", &json!({"center":[0,0],"radius":2,"id":"circle"}))?; + s.execute("sketch.line", &json!({"points":[[10,0],[20,0]],"ids":["line"]}))?; + let before = s.model.state(); + let mut after = before.as_ref().clone(); + for sketch in &mut after.sketches { + let sketch = &mut std::sync::Arc::make_mut(sketch).sketch; + for curve in &mut sketch.curves { + match &mut curve.kind { + solidcraft_engine::sketch::CurveKind::Circle { r, .. } => *r = 3.0, + solidcraft_engine::sketch::CurveKind::Line { a, b } => std::mem::swap(a, b), + _ => {} + } + } + } + let built = build(&before, &after, colors()); + assert_eq!(built.sketches.len(), 1); + for preview in &built.sketches { + assert_eq!(preview.curves, vec![0, 1]); + assert!(preview.points.is_empty()); + } + Ok(()) + } + /// Combine previews its result (cut, join or intersect between existing bodies) without an /// error, so its dialog's OK is enabled. Before, the preview also tried the command as a new /// body, which a combine has no form of, and always reported "operation must be join, cut or diff --git a/crates/ui-egui/src/sketch_corner_ui_tests.rs b/crates/ui-egui/src/sketch_corner_ui_tests.rs index a212044e7..2654c1436 100644 --- a/crates/ui-egui/src/sketch_corner_ui_tests.rs +++ b/crates/ui-egui/src/sketch_corner_ui_tests.rs @@ -585,7 +585,7 @@ fn chamfer_window_cuts_the_corner_between_two_picked_lines() { assert!(sk.curve_index("left").is_some(), "the third line stays"); } -/// Corner previews draw new curves when a corner is picked, just like Mirror and Offset do. +/// Corner previews draw the new corner curve and the two shortened sides when a corner is picked. /// This tests that `previews()` for Corner produces sketch preview curves visible in the viewport. #[test] fn fillet_chamfer_corner_preview_draws_curves() { @@ -602,8 +602,8 @@ fn fillet_chamfer_corner_preview_draws_curves() { h.step(&json!({"click":{"world":[0.0,10.0,0.0]}})).unwrap(); h.frames(3); - // The preview should have drawn new curves (fillet arcs at picked corners). - h.step(&json!({"until":{"count":{"name":"sketch_preview_curves","value":1}}})).unwrap(); + // Show the fillet arc and both shortened sides, whose ids stay the same. + h.step(&json!({"until":{"count":{"name":"sketch_preview_curves","value":3}}})).unwrap(); // Verify the sketch itself hasn't been modified yet. let sid = h.app.session.active_sketch.unwrap(); @@ -618,8 +618,8 @@ fn fillet_chamfer_corner_preview_draws_curves() { h.step(&json!({"click":{"world":[0.0,10.0,0.0]}})).unwrap(); h.frames(3); - // Chamfer preview should also produce curves. - h.step(&json!({"until":{"count":{"name":"sketch_preview_curves","value":1}}})).unwrap(); + // Show the chamfer line and both shortened sides too. + h.step(&json!({"until":{"count":{"name":"sketch_preview_curves","value":3}}})).unwrap(); let sk = h.app.session.world_state().sketch(sid).unwrap().sketch.clone(); assert_eq!(sk.curves.len(), 2, "chamfer preview must not modify the sketch");