SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Sketch Dimension: a slanted line gets an aligned dimension by default (#8582)
semmelrinde278ab40a0f9e
diff --git a/crates/engine/src/cmd/sketch.rs b/crates/engine/src/cmd/sketch.rsindex 4de7bb9e7f..fa63f36fa2 100644--- a/crates/engine/src/cmd/sketch.rs+++ b/crates/engine/src/cmd/sketch.rs@@ -141,7 +141,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\"; a model edge or vertex, {edge: [x,y,z], body?} or {vertex: [x,y,z], body?}, is projected into the sketch first and measured there: a point against the projected edge measures the distance to it), type?: auto|distance|horizontal|distance_x|vertical|distance_y|length|radius|diameter|major|minor|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 ellipse: a half-axis, the distance from the centre to an axis end (major or minor; with auto, text_at pulled across the major axis gives the major half-axis, pulled out past an end of it the minor one, the minor without text_at; previous_axis?: major|minor, the half-axis an earlier placement chose, kept while text_at is near the boundary between them; the reply's axis names the one chosen; with auto, when a driving dimension already holds one half-axis (or its whole axis), the other one, wherever text_at is; without its major axis line the major half-axis goes to the ellipse's own major end, without its minor axis line the minor half-axis is the minor radius); any other type, radius among them, the minor radius; an ellipse's axis line with auto: its length, the whole axis, wherever text_at is; half_axes?: bool (default true; false: with auto, the minor radius wherever text_at is, as before half-axes, which the Dimension tool sends with Draw ellipses with their axes off). 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\"; a model edge or vertex, {edge: [x,y,z], body?} or {vertex: [x,y,z], body?}, is projected into the sketch first and measured there: a point against the projected edge measures the distance to it), type?: auto|distance|horizontal|distance_x|vertical|distance_y|length|radius|diameter|major|minor|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 (between the perpendiculars through them, or a tenth of their distance past) is the distance between them; pulled further past an end, up or down from it horizontal, sideways vertical. Two midpoints: placing text above or below their bounding box measures horizontally, beside it vertically, anywhere else (inside it or past its corners) the distance between them. 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 ellipse: a half-axis, the distance from the centre to an axis end (major or minor; with auto, text_at pulled across the major axis gives the major half-axis, pulled out past an end of it the minor one, the minor without text_at; previous_axis?: major|minor, the half-axis an earlier placement chose, kept while text_at is near the boundary between them; the reply's axis names the one chosen; with auto, when a driving dimension already holds one half-axis (or its whole axis), the other one, wherever text_at is; without its major axis line the major half-axis goes to the ellipse's own major end, without its minor axis line the minor half-axis is the minor radius); any other type, radius among them, the minor radius; an ellipse's axis line with auto: its length, the whole axis, wherever text_at is; half_axes?: bool (default true; false: with auto, the minor radius wherever text_at is, as before half-axes, which the Dimension tool sends with Draw ellipses with their axes off). 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")@@ -3034,7 +3034,12 @@ 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());- match auto_orientation(&ty, pa, pb, text_at) {+ 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 {"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)),@@ -3276,14 +3281,33 @@ fn solved_measure(s: &Session, p: &Value, cmd: &str) -> Option<(f64, &'static stSome(measured(k.is_angle(), solidcraft_sketch::measure_dimension(sk, k)?))}-/// 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.+/// 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+/// on either side, it is the distance between them, and a little past those too (a tenth of+/// that distance), so it takes a clear pull past an end to change it. Pulled past an end, the+/// pull as seen from that end decides: up or down the horizontal distance, sideways the+/// vertical one (#257, #7295). The two meet on the diagonals through the end, so each has a+/// wide zone however shallow or steep the line is. 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 margin = len / 10.0;+ if d.x.abs() <= 1e-9 * len || d.y.abs() <= 1e-9 * len || !along.is_finite() || (-margin..=len + margin).contains(&along) {+ return ty;+ }+ let pull = t - if along < 0.0 { a } else { b };+ if pull.y.abs() >= pull.x.abs() { "horizontal" } else { "vertical" }+}++/// Midpoint pairs choose an axis as soon as the text passes their bounding box: above or below+/// it the horizontal distance, beside it the vertical one. Inside it and in its outer corners+/// they keep the distance between them, as before #7355. 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 Some(t) = text_at.filter(|_| ty == "auto") else { return ty };let d = b - a;let len = d.len();diff --git a/crates/engine/src/cmd/sketch_constraints_tests.rs b/crates/engine/src/cmd/sketch_constraints_tests.rsindex a60e08d68e..d2c224a03a 100644--- a/crates/engine/src/cmd/sketch_constraints_tests.rs+++ b/crates/engine/src/cmd/sketch_constraints_tests.rs@@ -1548,6 +1548,11 @@ fn midpoint_pair_orientation_uses_the_text_box_and_respects_explicit_types() {("auto", [25., 33.], 'x'),("auto", [43., 20.], 'y'),("auto", [25., 20.], 'd'),+ // The box's outer corners keep the distance between them, as before #7355.+ ("auto", [50., 45.], 'd'),+ ("auto", [55., 35.], 'd'),+ ("auto", [0., 0.], 'd'),+ ("auto", [-5., 5.], 'd'),("distance", [25., 33.], 'd'),("horizontal", [43., 20.], 'x'),("vertical", [25., 33.], 'y'),diff --git a/crates/engine/src/cmd/sketch_ux_tests.rs b/crates/engine/src/cmd/sketch_ux_tests.rsindex 9a5a075641..378687353c 100644--- a/crates/engine/src/cmd/sketch_ux_tests.rs+++ b/crates/engine/src/cmd/sketch_ux_tests.rs@@ -1422,9 +1422,9 @@ 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 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).+/// its orientation from where its text goes: across from the two (and a tenth of their distance+/// past) the distance between them; pulled further past an end, up or down from it the+/// horizontal distance, sideways the vertical one (#257, #7295).#[test]fn auto_dimension_orientation_follows_where_its_text_is_placed() {use solidcraft_sketch::ConstraintKind;@@ -1441,8 +1441,8 @@ 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"]);- // 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.+ // A 30 × 40 line (50 long), and a shallow one, 50 × 10: above or below the middle of it is+ // the distance between its ends, and past its ends both other types have wide zones.let shallow = 2600_f64.sqrt();let cases: [([f64; 2], [f64; 2], [f64; 3], &[([f64; 2], &str)]); 2] = [(@@ -1451,16 +1451,18 @@ fn auto_dimension_orientation_follows_where_its_text_is_placed() {[30.0, 40.0, 50.0],&[([15.0, 60.0], "horizontal"),- ([15.0, 41.0], "horizontal"),([15.0, -20.0], "horizontal"),+ ([-20.0, -25.0], "horizontal"),+ ([40.0, 55.0], "horizontal"),([-30.0, 5.0], "vertical"),([55.0, 38.0], "vertical"),- ([-20.0, 25.0], "vertical"),- ([45.0, 10.0], "vertical"),+ ([-25.0, -20.0], "vertical"),([20.0, 10.0], "aligned"),([10.0, 30.0], "aligned"),- ([-20.0, -20.0], "aligned"),- ([40.0, 50.0], "aligned"),+ ([15.0, 41.0], "aligned"),+ ([-20.0, 25.0], "aligned"),+ ([45.0, 10.0], "aligned"),+ ([32.0, 43.0], "aligned"),],),(@@ -1468,14 +1470,18 @@ fn auto_dimension_orientation_follows_where_its_text_is_placed() {[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, 40.0], "horizontal"),+ ([65.0, -30.0], "horizontal"),+ ([0.0, -10.0], "horizontal"),+ ([2.0, 30.0], "horizontal"),([80.0, 5.0], "vertical"),+ ([90.0, -2.0], "vertical"),([0.0, 3.0], "vertical"),+ ([35.0, 30.0], "aligned"),+ ([35.0, -20.0], "aligned"),([35.0, 6.0], "aligned"),- ([80.0, 40.0], "aligned"),- ([0.0, -10.0], "aligned"),+ ([12.0, 40.0], "aligned"),+ ([63.0, 5.0], "aligned"),],),];diff --git a/crates/ui-egui/src/dim_view.rs b/crates/ui-egui/src/dim_view.rsindex f9b4889e09..a1b068032c 100644--- a/crates/ui-egui/src/dim_view.rs+++ b/crates/ui-egui/src/dim_view.rs@@ -307,8 +307,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: 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+/// (two points or a slanted line: across from them the distance between them, pulled past an+/// end up or down the horizontal one, sideways 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.rsindex a7bda9a1a1..502dd2748a 100644--- a/crates/ui-egui/src/dimension_create_ui_tests.rs+++ b/crates/ui-egui/src/dimension_create_ui_tests.rs@@ -434,7 +434,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.]], [10., 4.]),+ ([12., 16.], vec![[0., 0.], [12., 16.]], [-5., 12.]),] {let mut h = line(end);let original = h.app.session.doc.to_json();@@ -733,15 +733,16 @@ fn inch_dimension_entry_survives_unit_changes_undo_and_reopen() {crate::dim_view::cancel();}-/// 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).+/// Placing the dimension of two unaligned points across from them, on either side, gives the+/// distance between them; pulled past an end, up or down from it gives the horizontal distance+/// and sideways the vertical one (#257, #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: solidcore_record::Vec2, b: solidcore_record::Vec2| [(b.x - a.x).abs(), (b.y - a.y).abs(), a.dist(b)];- for (at, measured) in [([6., 25.], 0), ([2., 20.], 0), ([-10., 1.], 1), ([15., 4.], 1), ([10., 4.], 2), ([-6., 22.], 2)] {+ for (at, measured) in+ [([6., 25.], 0), ([8., 26.], 0), ([-10., 1.], 1), ([22., 14.], 1), ([10., 4.], 2), ([-6., 22.], 2), ([2., 20.], 2), ([15., 4.], 2)]+ {let mut h = line([12., 16.]);place(&mut h, &[[0., 0.], [12., 16.]], at);type_value(&mut h, "10 mm");@@ -1424,8 +1425,8 @@ fn a_caret_the_person_moved_stays_where_they_put_it() {crate::dim_view::cancel();}-/// 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+/// 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/// 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]@@ -1433,8 +1434,8 @@ fn type_first_switching_between_aligned_horizontal_and_vertical_keeps_the_pickeduse solidcraft_engine::sketch::ConstraintKind as K;let end = [12., 16.];let mid = [6., 8.];- // Inside the picks' box, above it (over its middle), beside it.- let (across, above, beside) = ([10., 4.], [3., 20.], [-10., 1.]);+ // Across from the picks, above them, beside them.+ let (across, above, beside) = ([-5., 12.], [6., 25.], [-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 { .. });