Skip to content

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

Sketch Dimension: choose horizontal or vertical from the two points' box (#7295) (#7355)

semmelrinde2841210fb28f
diff --git a/crates/engine/src/cmd/sketch.rs b/crates/engine/src/cmd/sketch.rs
index 79ece6966c..3cccaf3593 100644
--- a/crates/engine/src/cmd/sketch.rs
+++ b/crates/engine/src/cmd/sketch.rs
@@ -127,7 +127,7 @@ pub static COMMANDS: &[CommandSpec] = &[
.params(concat!("p0, p1: overall ends, width", connect_doc!(), created_doc!())),
CommandSpec::new("sketch.point", "Point", draw_point).at("SKETCH", "CREATE").icon("point").enabled(in_sketch).params(concat!("point: [x,y] (each a number or an expression over the design's parameters: the point is then fixed where it comes to and follows them) or a snap: \"mid:<line>\" (held at the line's midpoint), \"on:<curve>:x,y\" (on the curve near x,y), {vertex: [x,y,z]} or {on_edge: [x,y,z]} of the model; id?", connect_doc!(), created_doc!())),
CommandSpec::new("sketch.dimension", "Sketch Dimension", dimension).at("SKETCH", "CREATE").icon("dimension").key("D").enabled(in_sketch).params(
- "entities: [refs] (a line midpoint is \"mid:line_id\"), type?: auto|distance|horizontal|distance_x|vertical|distance_y|length|radius|diameter|angle, value?: number or expression (default: current; a bare number is in the design's units, e.g. 2 means 2 in in an inch design; give a unit such as \"50 mm\" to override), or `name = expression` (e.g. \"width = 40\"; also `#name = expression`), which adds the user parameter `name` with that expression and dimensions with `name` (refused when the name is taken or reserved; the reply's `defined` names it); text_at?: [x, y]. Two points or a line off the axes: with auto, text_at across from them is the distance between them, above or below them horizontal, beside them vertical. Two midpoints: placing text above or below their bounding box measures horizontally, beside it vertically. Two lines: the angle between them (acute, or 180° − it when the value is nearer that) unless text_at picks a sector. A line and a circle or arc: the distance from the line to its edge (the gap, or how far the edge reaches past a line that cuts it; 0 is tangent); dimension the centre point for the distance to the centre. A point and a circle or arc: the distance from the point to its edge (or how far within it a point inside lies). Two circles or arcs: the gap between their edges (or how far they overlap when they cross, or the narrowest gap between them when one lies inside the other), or the centre-to-centre distance when they are the two caps of a slot. An arc and one of its end points: the arc's length (type arc_length does it from the arc alone). A dimension the sketch already determines is refused (naming what determines it); if_redundant?: \"driven\" places it as a driven (reference) dimension instead. scale_sketch?: bool (the dimension tool sends true): when this is the sketch's first length dimension and nothing in the sketch fixes a size (no other driving length dimension, nothing fixed or projected), the whole sketch scales about its origin to the value instead of distorting. Returns measured in unit (mm or deg), and scaled: the factor when the sketch was scaled",
+ "entities: [refs] (a line midpoint is \"mid:line_id\"), type?: auto|distance|horizontal|distance_x|vertical|distance_y|length|radius|diameter|angle, value?: number or expression (default: current; a bare number is in the design's units, e.g. 2 means 2 in in an inch design; give a unit such as \"50 mm\" to override), or `name = expression` (e.g. \"width = 40\"; also `#name = expression`), which adds the user parameter `name` with that expression and dimensions with `name` (refused when the name is taken or reserved; the reply's `defined` names it); text_at?: [x, y]. Two points, two midpoints or a line off the axes: with auto, text_at inside the box they span (as opposite corners) or in its outer corners is the distance between them, above or below the box horizontal, beside it vertical. Two lines: the angle between them (acute, or 180° − it when the value is nearer that) unless text_at picks a sector. A line and a circle or arc: the distance from the line to its edge (the gap, or how far the edge reaches past a line that cuts it; 0 is tangent); dimension the centre point for the distance to the centre. A point and a circle or arc: the distance from the point to its edge (or how far within it a point inside lies). Two circles or arcs: the gap between their edges (or how far they overlap when they cross, or the narrowest gap between them when one lies inside the other), or the centre-to-centre distance when they are the two caps of a slot. An arc and one of its end points: the arc's length (type arc_length does it from the arc alone). A dimension the sketch already determines is refused (naming what determines it); if_redundant?: \"driven\" places it as a driven (reference) dimension instead. scale_sketch?: bool (the dimension tool sends true): when this is the sketch's first length dimension and nothing in the sketch fixes a size (no other driving length dimension, nothing fixed or projected), the whole sketch scales about its origin to the value instead of distorting. Returns measured in unit (mm or deg), and scaled: the factor when the sketch was scaled",
),
CommandSpec::new("sketch.constraint.horizontal_vertical", "Horizontal/Vertical", c_horizontal_vertical)
.at("SKETCH", "CONSTRAINTS")
@@ -2505,12 +2505,7 @@ fn dimension(s: &mut Session, p: &Value) -> Result<Value> {
}
((Some(a), None), Some((Some(b), None))) => {
let (pa, pb) = (sk.point(a).unwrap_or_default(), sk.point(b).unwrap_or_default());
- let orientation = if ents.iter().all(|id| id.starts_with("mid:")) {
- midpoint_orientation(&ty, pa, pb, text_at)
- } else {
- auto_orientation(&ty, pa, pb, text_at)
- };
- match orientation {
+ match auto_orientation(&ty, pa, pb, text_at) {
"horizontal" => (ConstraintKind::DistanceX { p: a, q: b, value: (pb.x - pa.x).abs() }, (pb.x - pa.x).abs()),
"vertical" => (ConstraintKind::DistanceY { p: a, q: b, value: (pb.y - pa.y).abs() }, (pb.y - pa.y).abs()),
_ => (ConstraintKind::Distance { p: a, q: b, value: pa.dist(pb) }, pa.dist(pb)),
@@ -2745,47 +2740,26 @@ fn solved_measure(s: &Session, p: &Value, cmd: &str) -> Option<(f64, &'static st
Some(measured(k.is_angle(), solidcraft_sketch::measure_dimension(sk, k)?))
}
-/// The type of a dimension between `a` and `b` (two points or a line's ends). `auto` takes it
-/// from where the text is placed: across from the two (between the perpendiculars through
-/// them) the distance between them; past that, above or below them the horizontal distance,
-/// beside them the vertical one (#257). With no text, or when the two already line up with
-/// an axis, `auto` stays the distance between them; other types are returned unchanged.
+/// The type of a dimension between `a` and `b` (two points, a line's ends or two line
+/// midpoints). `auto` takes it from where the text is placed relative to the box the two span
+/// as opposite corners: inside the box the distance between them; above or below it the
+/// horizontal distance, beside it the vertical one; in the corners outside it the distance
+/// again (#7295). Every type keeps a zone the size of the box's side whatever the slope, and
+/// the box's edges are the thresholds. With no text, or when the two already line up with an
+/// axis, `auto` stays the distance between them; other types are returned unchanged.
fn auto_orientation(ty: &str, a: Vec2, b: Vec2, text_at: Option<Vec2>) -> &str {
let Some(t) = text_at.filter(|_| ty == "auto") else { return ty };
let d = b - a;
let len = d.len();
- let along = (t - a).dot(d) / len;
- let between = |v: f64, p: f64, q: f64| p.min(q) <= v && v <= p.max(q);
- if d.x.abs() <= 1e-9 * len || d.y.abs() <= 1e-9 * len || !along.is_finite() || (0.0..=len).contains(&along) {
- ty
- } else if between(t.x, a.x, b.x) {
- "horizontal"
- } else if between(t.y, a.y, b.y) {
- "vertical"
- } else {
- ty
+ if !t.x.is_finite() || !t.y.is_finite() || d.x.abs() <= 1e-9 * len || d.y.abs() <= 1e-9 * len {
+ return ty;
}
-}
-
-/// Midpoint pairs choose an axis as soon as the text passes their bounding box.
-/// Keeping the aligned sector inside that box avoids requiring a long pointer move
-/// for shallow diagonals; explicit types and already aligned points retain their behavior.
-fn midpoint_orientation(ty: &str, a: Vec2, b: Vec2, text_at: Option<Vec2>) -> &str {
- let d = b - a;
- if ty == "auto"
- && d.x.abs() > 1e-9 * d.len()
- && d.y.abs() > 1e-9 * d.len()
- && let Some(t) = text_at
- {
- let between = |v: f64, p: f64, q: f64| p.min(q) <= v && v <= p.max(q);
- if between(t.x, a.x, b.x) && !between(t.y, a.y, b.y) {
- return "horizontal";
- }
- if between(t.y, a.y, b.y) && !between(t.x, a.x, b.x) {
- return "vertical";
- }
+ let between = |v: f64, p: f64, q: f64| p.min(q) <= v && v <= p.max(q);
+ match (between(t.x, a.x, b.x), between(t.y, a.y, b.y)) {
+ (true, false) => "horizontal",
+ (false, true) => "vertical",
+ _ => ty,
}
- auto_orientation(ty, a, b, text_at)
}
/// Keep a dimension's text where it was placed (`at`, sketch coordinates), relative to the
diff --git a/crates/engine/src/cmd/sketch_ux_tests.rs b/crates/engine/src/cmd/sketch_ux_tests.rs
index 341b115e1e..589dd348f3 100644
--- a/crates/engine/src/cmd/sketch_ux_tests.rs
+++ b/crates/engine/src/cmd/sketch_ux_tests.rs
@@ -1420,15 +1420,15 @@ fn pipe_and_sweep_follow_3d_sketch_curves() {
}
/// Between two points, or on a line, that don't line up with an axis, an `auto` dimension takes
-/// its orientation from where its text goes: across from the two the distance between them,
-/// past that above or below them the horizontal distance, beside them the vertical one (#257).
+/// its orientation from where its text goes relative to the box the two span: inside it the
+/// distance between them, above or below it the horizontal distance, beside it the vertical
+/// one, in its outer corners the distance again (#257, #7295).
#[test]
fn auto_dimension_orientation_follows_where_its_text_is_placed() {
use solidcraft_sketch::ConstraintKind;
- // The ends of a 30 × 40 line: 50 long, and the slope keeps "across" and "past" distinct.
- let dimension = |end: [f64; 2], entities: Value, extra: Value| {
+ let dimension = |start: [f64; 2], end: [f64; 2], entities: Value, extra: Value| {
let mut s = new_sketch();
- run(&mut s, "sketch.line", json!({"points": [[0, 0], end], "connect": false}));
+ run(&mut s, "sketch.line", json!({"points": [start, end], "connect": false}));
let mut p = json!({"entities": entities});
if let (Some(p), Some(extra)) = (p.as_object_mut(), extra.as_object()) {
p.extend(extra.clone());
@@ -1439,38 +1439,74 @@ fn auto_dimension_orientation_follows_where_its_text_is_placed() {
(kind, r["measured"].as_f64().unwrap_or(f64::NAN), sk)
};
let points = json!(["l1.start", "l1.end"]);
- for entities in [points.clone(), json!(["l1"])] {
- for (at, expected) in [
- ([15.0, 60.0], "horizontal"),
- ([15.0, -20.0], "horizontal"),
- ([-30.0, 5.0], "vertical"),
- ([55.0, 38.0], "vertical"),
- ([-20.0, 25.0], "aligned"),
- ([45.0, 10.0], "aligned"),
- ([-20.0, -20.0], "aligned"),
- ] {
- let (kind, measured, _) = dimension([30.0, 40.0], entities.clone(), json!({"text_at": at}));
- let (got, value) = match kind {
- ConstraintKind::DistanceX { .. } => ("horizontal", 30.0),
- ConstraintKind::DistanceY { .. } => ("vertical", 40.0),
- ConstraintKind::Distance { .. } | ConstraintKind::Length { .. } => ("aligned", 50.0),
- other => panic!("{entities} at {at:?}: {other:?}"),
- };
- assert_eq!(got, expected, "{entities} with its text at {at:?}");
- assert!((measured - value).abs() < 1e-9, "{entities} at {at:?} measured {measured}");
+ // A 30 × 40 line (50 long), and a shallow one, 50 × 10, whose box is a thin strip: above
+ // the middle of it is horizontal, not the distance between its ends.
+ let shallow = 2600_f64.sqrt();
+ let cases: [([f64; 2], [f64; 2], [f64; 3], &[([f64; 2], &str)]); 2] = [
+ (
+ [0.0, 0.0],
+ [30.0, 40.0],
+ [30.0, 40.0, 50.0],
+ &[
+ ([15.0, 60.0], "horizontal"),
+ ([15.0, 41.0], "horizontal"),
+ ([15.0, -20.0], "horizontal"),
+ ([-30.0, 5.0], "vertical"),
+ ([55.0, 38.0], "vertical"),
+ ([-20.0, 25.0], "vertical"),
+ ([45.0, 10.0], "vertical"),
+ ([20.0, 10.0], "aligned"),
+ ([10.0, 30.0], "aligned"),
+ ([-20.0, -20.0], "aligned"),
+ ([40.0, 50.0], "aligned"),
+ ],
+ ),
+ (
+ [10.0, 10.0],
+ [60.0, 0.0],
+ [50.0, 10.0, shallow],
+ &[
+ ([35.0, 30.0], "horizontal"),
+ ([35.0, -20.0], "horizontal"),
+ ([12.0, 40.0], "horizontal"),
+ ([80.0, 5.0], "vertical"),
+ ([0.0, 3.0], "vertical"),
+ ([35.0, 6.0], "aligned"),
+ ([80.0, 40.0], "aligned"),
+ ([0.0, -10.0], "aligned"),
+ ],
+ ),
+ ];
+ for (start, end, [dx, dy, len], placements) in cases {
+ for entities in [points.clone(), json!(["l1"])] {
+ for &(at, expected) in placements {
+ let (kind, measured, _) = dimension(start, end, entities.clone(), json!({"text_at": at}));
+ let (got, value) = match kind {
+ ConstraintKind::DistanceX { .. } => ("horizontal", dx),
+ ConstraintKind::DistanceY { .. } => ("vertical", dy),
+ ConstraintKind::Distance { .. } | ConstraintKind::Length { .. } => ("aligned", len),
+ other => panic!("{entities} at {at:?}: {other:?}"),
+ };
+ assert_eq!(got, expected, "{start:?}–{end:?}: {entities} with its text at {at:?}");
+ assert!((measured - value).abs() < 1e-6, "{entities} at {at:?} measured {measured}");
+ }
}
+ }
+ for entities in [points.clone(), json!(["l1"])] {
// No text, an explicit type, or two ends already on an axis keep today's dimension.
- let (kind, ..) = dimension([30.0, 40.0], entities.clone(), json!({}));
+ let (kind, ..) = dimension([0.0, 0.0], [30.0, 40.0], entities.clone(), json!({}));
assert!(matches!(kind, ConstraintKind::Distance { .. } | ConstraintKind::Length { .. }), "{kind:?}");
- let (kind, ..) = dimension([30.0, 40.0], entities.clone(), json!({"type": "distance", "text_at": [15, 60]}));
+ let (kind, ..) = dimension([0.0, 0.0], [30.0, 40.0], entities.clone(), json!({"type": "distance", "text_at": [15, 60]}));
assert!(matches!(kind, ConstraintKind::Distance { .. } | ConstraintKind::Length { .. }), "{kind:?}");
- let (kind, ..) = dimension([30.0, 40.0], entities.clone(), json!({"type": "vertical", "text_at": [15, 60]}));
+ let (kind, ..) = dimension([0.0, 0.0], [30.0, 40.0], entities.clone(), json!({"type": "vertical", "text_at": [15, 60]}));
assert!(matches!(kind, ConstraintKind::DistanceY { .. }), "{kind:?}");
- let (kind, ..) = dimension([40.0, 0.0], json!(["l1.start", "l1.end"]), json!({"text_at": [60, 5]}));
+ let (kind, ..) = dimension([0.0, 0.0], [40.0, 0.0], json!(["l1.start", "l1.end"]), json!({"text_at": [60, 5]}));
+ assert!(matches!(kind, ConstraintKind::Distance { .. }), "{kind:?}");
+ let (kind, ..) = dimension([0.0, 0.0], [40.0, 0.0], json!(["l1.start", "l1.end"]), json!({"text_at": [20, 15]}));
assert!(matches!(kind, ConstraintKind::Distance { .. }), "{kind:?}");
}
// The chosen dimension drives that coordinate: 24 across X, the 40 in Y kept.
- let (_, _, sk) = dimension([30.0, 40.0], points, json!({"text_at": [15, 60], "value": 24}));
+ let (_, _, sk) = dimension([0.0, 0.0], [30.0, 40.0], points, json!({"text_at": [15, 60], "value": 24}));
let end = sk.point(sk.resolve_point("l1.end").expect("end")).expect("end point");
let start = sk.point(sk.resolve_point("l1.start").expect("start")).expect("start point");
assert!(((end.x - start.x).abs() - 24.0).abs() < 1e-6 && ((end.y - start.y).abs() - 40.0).abs() < 1e-6, "{start:?} {end:?}");
diff --git a/crates/ui-egui/src/dim_view.rs b/crates/ui-egui/src/dim_view.rs
index 1f7cf530ef..78873c772e 100644
--- a/crates/ui-egui/src/dim_view.rs
+++ b/crates/ui-egui/src/dim_view.rs
@@ -202,8 +202,8 @@ pub fn pin(app: &SolveApp, at: Vec2) {
}
/// Move a draft's text to `at`. Where the text goes can change what the dimension measures
-/// (two points or a slanted line: across from them the distance between them, above or below
-/// the horizontal one, beside them the vertical one), so a value nothing was typed into yet
+/// (two points or a slanted line: inside the box they span the distance between them, above or
+/// below it the horizontal one, beside it the vertical one), so a value nothing was typed into yet
/// becomes what the new dimension measures; carrying the old one over would preview, and on
/// Enter apply, the aligned length as a horizontal or vertical span, moving the picked points.
fn move_text(app: &SolveApp, e: &mut Edit, at: Vec2) {
diff --git a/crates/ui-egui/src/dimension_create_ui_tests.rs b/crates/ui-egui/src/dimension_create_ui_tests.rs
index fa7bf3dfba..d47072ee44 100644
--- a/crates/ui-egui/src/dimension_create_ui_tests.rs
+++ b/crates/ui-egui/src/dimension_create_ui_tests.rs
@@ -224,7 +224,7 @@ fn edge_and_endpoint_creation_commit_once_and_undo_restores_exact_design() {
([20., 0.], vec![[10., 0.]], [10., 5.]),
([20., 0.], vec![[0., 0.], [20., 0.]], [10., 5.]),
([0., 20.], vec![[0., 0.], [0., 20.]], [-5., 10.]),
- ([12., 16.], vec![[0., 0.], [12., 16.]], [-5., 12.]),
+ ([12., 16.], vec![[0., 0.], [12., 16.]], [10., 4.]),
] {
let mut h = line(end);
let original = h.app.session.doc.to_json();
@@ -515,13 +515,15 @@ fn inch_dimension_entry_survives_unit_changes_undo_and_reopen() {
crate::dim_view::cancel();
}
-/// Placing the dimension of two unaligned points above them gives the horizontal distance,
-/// beside them the vertical one, and across from them the distance between them (#257).
+/// Placing the dimension of two unaligned points above or below the box they span gives the
+/// horizontal distance, beside it the vertical one, and inside it or in its outer corners the
+/// distance between them (#257). Just above the middle of the box, and just beside it, are
+/// horizontal and vertical too, not only past the ends (#7295).
#[test]
fn placing_a_two_point_dimension_picks_horizontal_vertical_or_aligned() {
// What each placement fixes at 10 mm: the X span, the Y span, or the length.
let spans = |a: solidcraft_engine::geom::Vec2, b: solidcraft_engine::geom::Vec2| [(b.x - a.x).abs(), (b.y - a.y).abs(), a.dist(b)];
- for (at, measured) in [([6., 25.], 0), ([-10., 1.], 1), ([-5., 12.], 2)] {
+ for (at, measured) in [([6., 25.], 0), ([2., 20.], 0), ([-10., 1.], 1), ([15., 4.], 1), ([10., 4.], 2), ([-6., 22.], 2)] {
let mut h = line([12., 16.]);
place(&mut h, &[[0., 0.], [12., 16.]], at);
type_value(&mut h, "10 mm");
@@ -786,8 +788,8 @@ fn type_first_keeps_the_place_first_flow() {
crate::dim_view::cancel();
}
-/// Type-first, the text follows the pointer: moving it from across the picks (the distance
-/// between them) to above or beside them switches to the horizontal or vertical distance and
+/// Type-first, the text follows the pointer: moving it from inside the box the picks span (the
+/// distance between them) to above or beside it switches to the horizontal or vertical distance and
/// back. Until something is typed the value follows the dimension, so the preview measures the
/// picked points where they are (it neither moves nor scales them) and Enter keeps them there.
#[test]
@@ -795,8 +797,8 @@ fn type_first_switching_between_aligned_horizontal_and_vertical_keeps_the_picked
use solidcraft_engine::sketch::ConstraintKind as K;
let end = [12., 16.];
let mid = [6., 8.];
- // Across from the picks, above them, beside them.
- let (across, above, beside) = ([-5., 12.], [6., 25.], [-10., 1.]);
+ // Inside the picks' box, above it (over its middle), beside it.
+ let (across, above, beside) = ([10., 4.], [3., 20.], [-10., 1.]);
type Kind = fn(&K) -> bool;
let aligned: Kind = |k| matches!(k, K::Distance { .. } | K::Length { .. });
let horizontal: Kind = |k| matches!(k, K::DistanceX { .. });