Skip to content

SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.

Sketch: dragging a constrained centre point of a centre rectangle moves it, not resizes it (#10510) (#10535)

rugbymauri7d86a8cfd3e8
diff --git a/crates/ui-egui/src/sketch_drag_ui_tests.rs b/crates/ui-egui/src/sketch_drag_ui_tests.rs
index a641ad0be3..6b5f25b3ff 100644
--- a/crates/ui-egui/src/sketch_drag_ui_tests.rs
+++ b/crates/ui-egui/src/sketch_drag_ui_tests.rs
@@ -461,3 +461,34 @@ fn a_freed_circle_centre_on_the_origin_drags_instead_of_the_origin() {
assert!(point(&h, "c1.center").dist(Vec2::new(15.0, 10.0)) < 0.05, "{:?}", point(&h, "c1.center"));
assert!(point(&h, "origin").dist(Vec2::new(0.0, 0.0)) < 1e-9);
}
+
+/// A centre rectangle's centre held on the origin stays there when dragged, and the rectangle
+/// keeps its size. The press lands on the middle of the rectangle's diagonal as well as on the
+/// centre; the drag took the diagonal by its middle and turned and stretched it about the held
+/// centre, resizing the rectangle (#10510).
+#[test]
+fn a_centre_rectangles_centre_held_on_the_origin_keeps_the_rectangle_when_dragged() {
+ let mut h = Harness::new();
+ run(&mut h, "sketch.create", json!({"plane": "XY"}));
+ run(&mut h, "sketch.rectangle.center", json!({"center": [3, 2], "corner": [13, 9]}));
+ run(&mut h, "sketch.constraint.coincident", json!({"a": "p5", "b": "origin"}));
+ run(&mut h, "sketch.options", json!({"snap": false, "show_dimensions": false, "show_constraints": false}));
+ h.call("ui.view", json!({"view": "top"}));
+ h.app.cam.distance = 150.0;
+ h.frames(3);
+ let corners = ["p1", "p2", "p3", "p4"].map(|p| point(&h, p));
+ assert!(point(&h, "p5").len() < 1e-9, "{:?}", point(&h, "p5"));
+ // A press a little off the centre, as a hand presses.
+ let a = screen(&mut h, 0.3, 0.2);
+ let b = screen(&mut h, 12.0, 8.0);
+ let mut path = vec![a];
+ path.extend(steps(a, b, 6));
+ press_and_move(&mut h, &path);
+ assert_eq!(h.app.viewport.point_drag.as_ref().map(|(id, _, _)| id.as_str()), Some("p5"), "grabbed the centre, not the diagonal");
+ h.frames(4);
+ release(&mut h, b);
+ assert!(point(&h, "p5").len() < 1e-6, "the centre left the origin: {:?}", point(&h, "p5"));
+ for (p, was) in ["p1", "p2", "p3", "p4"].into_iter().zip(corners) {
+ assert!(point(&h, p).dist(was) < 1e-6, "{p} moved from {was:?} to {:?}", point(&h, p));
+ }
+}
diff --git a/crates/ui-egui/src/viewport.rs b/crates/ui-egui/src/viewport.rs
index e4451ba67e..250f7f8045 100644
--- a/crates/ui-egui/src/viewport.rs
+++ b/crates/ui-egui/src/viewport.rs
@@ -4817,6 +4817,7 @@ pub fn show(app: &mut SolveApp, ui: &mut egui::Ui) {
// the text, also from the space between its letters (#8835).
let own = matches!(&hit, Some(Hit::SketchPoint { sketch, .. } | Hit::SketchMidpoint { sketch, .. } | Hit::SketchCurve { sketch, .. }) if Some(*sketch) == active);
let hit = if own { hit } else { crate::sketch_text_handles::grab_inside(app, &proj, a).or(hit) };
+ let hit = hit.map(|hit| point_at_midpoint(app, hit));
match hit {
Some(Hit::SketchPoint { sketch, id, .. }) if Some(sketch) == active => {
// A point dragged with the rest of a selection slides with it, from where it
@@ -5816,6 +5817,27 @@ pub(crate) fn select(app: &mut SolveApp, sel: Option<Sel>, add: bool, toggle: bo
/// The rest of the selected sketch geometry when the point or curve `id` is part of it (#2621):
/// a drag that starts on a selected entity moves the whole selection with it.
+/// A drag that starts on a line's middle where a point is held (a centre rectangle's centre on
+/// its diagonal) drags that point, as a drag that starts on the point does. Dragging the line by
+/// its middle instead moved its ends: with the point constrained (on the origin, on a line) the
+/// line turned and stretched about it, and the rectangle round it changed size (#10510).
+fn point_at_midpoint(app: &SolveApp, hit: Hit) -> Hit {
+ let Hit::SketchMidpoint { sketch, id, .. } = &hit else { return hit };
+ let st = app.session.world_state();
+ let held = st.sketch(*sketch).and_then(|ss| {
+ let sk = &ss.sketch;
+ let line = sk.curve_index(id)?;
+ sk.constraints.iter().filter(|c| !c.driven).find_map(|c| match c.kind {
+ solidcraft_engine::sketch::ConstraintKind::Midpoint { p, l } if l == line => sk.points.get(p).map(|q| (q.id.clone(), q.pos)),
+ _ => None,
+ })
+ });
+ match held {
+ Some((id, at)) => Hit::SketchPoint { sketch: *sketch, id, at },
+ None => hit,
+ }
+}
+
fn dragged_with(app: &SolveApp, id: &str) -> Vec<String> {
fn geometry(s: &Sel) -> Option<&str> {
match s {