* Sketch dimension: a slot's two caps measure the overall length (#7464) * Sketch dimension: slot caps need tangent sides, not just shared lines (#7464) * Sketch dimension: slot caps measure the centre-to-centre distance (#7464) --------- Co-authored-by: Dane Doubell <2853036+danedoubell@users.noreply.github.com>
This commit is contained in:
@@ -126,7 +126,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); dimension their centre points for the distance between centres. 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 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",
|
||||
),
|
||||
CommandSpec::new("sketch.constraint.horizontal_vertical", "Horizontal/Vertical", c_horizontal_vertical)
|
||||
.at("SKETCH", "CONSTRAINTS")
|
||||
@@ -2591,9 +2591,20 @@ fn dimension(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
let round = |c: usize| sk.center(c).zip(sk.radius(c)).ok_or_else(|| bad(cmd, "cannot dimension that curve"));
|
||||
let ((ca, ra), (cb, rb)) = (round(a)?, round(b)?);
|
||||
let d = ca.dist(cb);
|
||||
let circles = TwoCircles::of(d, ra, rb);
|
||||
let gap = circles.gap(d, ra, rb).max(0.0);
|
||||
(ConstraintKind::CircleCircleDistance { a, b, value: gap, circles }, gap)
|
||||
// A slot's two arc caps: the centre-to-centre distance (the slot's length), not
|
||||
// the gap between the inner edges (#7464).
|
||||
if (ty == "auto" || ty == "distance") && slot_caps(sk, a, b) {
|
||||
let centre = |c: usize| match sk.curves.get(c).map(|x| &x.kind) {
|
||||
Some(CurveKind::Arc { c, .. }) => Some(*c),
|
||||
_ => None,
|
||||
};
|
||||
let (pa, pb) = centre(a).zip(centre(b)).ok_or_else(|| bad(cmd, "cannot dimension that curve"))?;
|
||||
(ConstraintKind::Distance { p: pa, q: pb, value: d }, d)
|
||||
} else {
|
||||
let circles = TwoCircles::of(d, ra, rb);
|
||||
let gap = circles.gap(d, ra, rb).max(0.0);
|
||||
(ConstraintKind::CircleCircleDistance { a, b, value: gap, circles }, gap)
|
||||
}
|
||||
}
|
||||
_ => return Err(bad(cmd, "cannot dimension that combination of entities")),
|
||||
};
|
||||
@@ -2795,6 +2806,55 @@ fn arc_len(sk: &Sketch, c: usize) -> Option<f64> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether two arcs are the two caps of a straight slot: each shares its end points with the same
|
||||
/// two lines (the slot's sides), and those sides are tangent to both caps. The distance between
|
||||
/// the caps is then their centre-to-centre distance, not the gap between their inner edges (#7464).
|
||||
fn slot_caps(sk: &Sketch, a: usize, b: usize) -> bool {
|
||||
if a == b {
|
||||
return false;
|
||||
}
|
||||
let arc = |c: usize| match sk.curves.get(c).map(|x| &x.kind) {
|
||||
Some(CurveKind::Arc { c, a, b }) => sk.point(*c).map(|centre| (centre, *a, *b)),
|
||||
_ => None,
|
||||
};
|
||||
let (Some((ca, a0, a1)), Some((cb, b0, b1))) = (arc(a), arc(b)) else {
|
||||
return false;
|
||||
};
|
||||
// Count the sides that run between the two caps and are tangent to both. Tangency keeps two
|
||||
// arcs joined by two lines but bulging inward out of it: for those, centre distance plus both
|
||||
// radii is not the overall length.
|
||||
let mut sides = 0;
|
||||
for c in &sk.curves {
|
||||
let CurveKind::Line { a: la, b: lb } = &c.kind else {
|
||||
continue;
|
||||
};
|
||||
let on = |p: &usize, (x, y): (usize, usize)| *p == x || *p == y;
|
||||
let (pa, pb) = if on(la, (a0, a1)) && on(lb, (b0, b1)) {
|
||||
(*la, *lb)
|
||||
} else if on(lb, (a0, a1)) && on(la, (b0, b1)) {
|
||||
(*lb, *la)
|
||||
} else {
|
||||
continue;
|
||||
};
|
||||
if tangent_at(sk, pa, pb, ca) && tangent_at(sk, pb, pa, cb) {
|
||||
sides += 1;
|
||||
}
|
||||
}
|
||||
sides >= 2
|
||||
}
|
||||
|
||||
/// Whether the line from `p` to `q` is tangent to the circle about `centre` at `p`: its direction
|
||||
/// is perpendicular to the radius from `centre` to `p`.
|
||||
fn tangent_at(sk: &Sketch, p: usize, q: usize, centre: Vec2) -> bool {
|
||||
let (Some(p), Some(q)) = (sk.point(p), sk.point(q)) else {
|
||||
return false;
|
||||
};
|
||||
match ((q - p).normalized(), (p - centre).normalized()) {
|
||||
(Some(d), Some(r)) => d.dot(r).abs() < 1e-9,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// The angle dimension between two lines whose sector contains `t` (the text position): returns
|
||||
/// the constraint and the current value of that sector angle.
|
||||
fn angle_by_sector(a: usize, b: usize, (a0, a1): (Vec2, Vec2), (b0, b1): (Vec2, Vec2), t: Vec2) -> Option<(ConstraintKind, f64)> {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
use serde_json::{Value, json};
|
||||
use solidcraft_geom::Vec2;
|
||||
use solidcraft_sketch::ConstraintKind;
|
||||
|
||||
use crate::Session;
|
||||
|
||||
@@ -1421,3 +1422,48 @@ fn an_arc_and_its_end_point_dimension_the_arc_length() {
|
||||
let e = s.execute("sketch.dimension", &json!({"entities": ["a1", "p9"]})).map(|v| v["type"].clone());
|
||||
assert!(!matches!(e, Ok(ref t) if t == "ArcLengthDimension"), "a point off the arc's ends is not an arc length: {e:?}");
|
||||
}
|
||||
|
||||
/// #7464: the distance between a slot's two arc caps measures the centre-to-centre distance
|
||||
/// (the slot's length), not the gap between their inner edges.
|
||||
#[test]
|
||||
fn slot_caps_dimension_is_the_centre_to_centre_distance() {
|
||||
let mut s = new_sketch();
|
||||
// Centre [0,0], one arc centre [20,0], width 10: caps at ±20 of radius 5, centre-to-centre 40.
|
||||
let sl = run(&mut s, "sketch.slot.center_point", json!({"center": [0, 0], "end": [20, 0], "width": 10}));
|
||||
// The slot returns its curves as [bottom line, right cap, top line, left cap].
|
||||
let curves = ids(&sl["curves"]);
|
||||
let d = run(&mut s, "sketch.dimension", json!({"entities": [curves[1], curves[3]], "driven": true}));
|
||||
assert!((d["measured"].as_f64().unwrap() - 40.0).abs() < 1e-6, "{d}");
|
||||
let sk = sketch(&s);
|
||||
assert!(
|
||||
sk.constraints.iter().any(|c| matches!(c.kind, ConstraintKind::Distance { .. })),
|
||||
"{:?}",
|
||||
sk.constraints.iter().map(|c| &c.kind).collect::<Vec<_>>()
|
||||
);
|
||||
// Two circles drawn apart, not joined as a slot, still measure the gap between their edges (#4064).
|
||||
let mut s = new_sketch();
|
||||
let c1 = ids(&run(&mut s, "sketch.circle.center", json!({"center": [0, 0], "radius": 5}))["curves"])[0].clone();
|
||||
let c2 = ids(&run(&mut s, "sketch.circle.center", json!({"center": [30, 0], "radius": 5}))["curves"])[0].clone();
|
||||
let d = run(&mut s, "sketch.dimension", json!({"entities": [c1, c2], "driven": true}));
|
||||
assert!((d["measured"].as_f64().unwrap() - 20.0).abs() < 1e-6, "{d}");
|
||||
}
|
||||
|
||||
/// Two arcs joined by two lines but bulging inward are not a slot's caps: their distance stays
|
||||
/// the gap between the edges, not a centre-to-centre distance.
|
||||
#[test]
|
||||
fn inward_bulging_arc_pairs_keep_the_gap_dimension() {
|
||||
let mut s = new_sketch();
|
||||
run(&mut s, "sketch.line", json!({"points": [[0, 0], [40, 0]], "ids": ["l1"]}));
|
||||
run(&mut s, "sketch.line", json!({"points": [[0, 10], [40, 10]], "ids": ["l2"]}));
|
||||
run(&mut s, "sketch.arc.three_point", json!({"start": "l1.start", "through": [3, 5], "end": "l2.start", "id": "ca"}));
|
||||
run(&mut s, "sketch.arc.three_point", json!({"start": "l1.end", "through": [37, 5], "end": "l2.end", "id": "cb"}));
|
||||
let d = run(&mut s, "sketch.dimension", json!({"entities": ["ca", "cb"], "driven": true}));
|
||||
// The shape spans x 0..40; the gap between the inward edges is 34 mm, not an overall length.
|
||||
assert!((d["measured"].as_f64().unwrap() - 34.0).abs() < 1e-6, "{d}");
|
||||
let sk = sketch(&s);
|
||||
assert!(
|
||||
sk.constraints.iter().any(|c| matches!(c.kind, ConstraintKind::CircleCircleDistance { .. })),
|
||||
"{:?}",
|
||||
sk.constraints.iter().map(|c| &c.kind).collect::<Vec<_>>()
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user