Skip to content

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

Sketch: an angle dimension between two lines from one point measures the angle between them (#2724) (#3439)

Jakobonly2f5f65d6e32d
diff --git a/crates/engine/src/cmd/sketch.rs b/crates/engine/src/cmd/sketch.rs
index 96d9bc5b00..5d9170243d 100644
--- a/crates/engine/src/cmd/sketch.rs
+++ b/crates/engine/src/cmd/sketch.rs
@@ -2075,9 +2075,15 @@ fn dimension(s: &mut Session, p: &Value) -> Result<Value> {
(ConstraintKind::PointLineDistance { p: bq, l: a, value: d }, d)
}
} else {
- match text_at {
- Some(t) => angle_by_sector(a, b, (a0, a1), (b0, b1), t).ok_or_else(|| bad(cmd, "cannot place that angle"))?,
- None => {
+ match (text_at, angle_from_shared_end(a, b, (a0, a1), (b0, b1))) {
+ (Some(t), _) => angle_by_sector(a, b, (a0, a1), (b0, b1), t).ok_or_else(|| bad(cmd, "cannot place that angle"))?,
+ // Two lines from one point (two radii, a corner) measure the angle
+ // between them, from that point towards their other ends (#2724).
+ (None, Some((between, supplement))) => {
+ alt = Some(supplement);
+ between
+ }
+ (None, None) => {
let mut ang = da.cross(db).atan2(da.dot(db));
let (mut ka, mut kb) = (a, b);
if ang < 0.0 {
@@ -2295,6 +2301,39 @@ fn angle_by_sector(a: usize, b: usize, (a0, a1): (Vec2, Vec2), (b0, b1): (Vec2,
None
}
+/// The angle dimension between two lines that run from one shared end, measured from that end
+/// towards their other ends (#2724), and its supplement; None when they share no end or are
+/// collinear.
+fn angle_from_shared_end(
+ a: usize,
+ b: usize,
+ (a0, a1): (Vec2, Vec2),
+ (b0, b1): (Vec2, Vec2),
+) -> Option<((ConstraintKind, f64), (ConstraintKind, f64))> {
+ let tol = 1e-9 * a0.dist(a1).max(b0.dist(b1)).max(1.0);
+ // The sign that turns each line's direction into its ray away from the shared end.
+ let (sa, sb) = [(a0, b0, 1.0, 1.0), (a0, b1, 1.0, -1.0), (a1, b0, -1.0, 1.0), (a1, b1, -1.0, -1.0)]
+ .into_iter()
+ .find(|(p, q, ..)| p.dist(*q) <= tol)
+ .map(|(_, _, sa, sb)| (sa, sb))?;
+ let (ra, rb) = ((a1 - a0) * sa, (b1 - b0) * sb);
+ let mut ang = ra.cross(rb).atan2(ra.dot(rb));
+ let (mut first, mut second) = ((a, sa), (b, sb));
+ if ang < 0.0 {
+ ang = -ang;
+ std::mem::swap(&mut first, &mut second);
+ }
+ if !(ang > 1e-9 && ang < std::f64::consts::PI - 1e-9) {
+ return None;
+ }
+ // The constraint turns the first line's direction onto the second's, negated when the
+ // two rays run against their lines' directions differently.
+ let flip = first.1 * second.1 < 0.0;
+ let between = (ConstraintKind::Angle { a: first.0, b: second.0, value: ang, flip }, ang);
+ let supplement = (ConstraintKind::Angle { a: second.0, b: first.0, value: std::f64::consts::PI - ang, flip: !flip }, std::f64::consts::PI - ang);
+ Some((between, supplement))
+}
+
pub(super) fn round6(x: f64) -> f64 {
(x * 1e6).round() / 1e6
}
diff --git a/crates/engine/src/cmd/sketch_constraints_tests.rs b/crates/engine/src/cmd/sketch_constraints_tests.rs
index 56063ccdcc..de5e2e114b 100644
--- a/crates/engine/src/cmd/sketch_constraints_tests.rs
+++ b/crates/engine/src/cmd/sketch_constraints_tests.rs
@@ -313,6 +313,47 @@ fn an_angle_between_lines_takes_the_reading_nearest_its_value_and_reports_degree
assert_eq!(d["unit"], "mm", "{d}");
}
+#[test]
+fn an_angle_between_two_lines_from_one_point_measures_the_angle_between_them() {
+ // #2724: two radii of a circle 120° apart measured 60°, and 150° apart 30°.
+ for deg in [30.0_f64, 60.0, 90.0, 120.0, 150.0] {
+ let far = [20.0 * deg.to_radians().cos(), 20.0 * deg.to_radians().sin()];
+ // Both drawn out from the centre, the second drawn in towards it, or a corner.
+ let shapes = [
+ vec![json!([[0, 0], [20, 0]]), json!([[0, 0], far])],
+ vec![json!([[0, 0], [20, 0]]), json!([far, [0, 0]])],
+ vec![json!([[20, 0], [0, 0], far])],
+ ];
+ for (i, lines) in shapes.iter().enumerate() {
+ let mut s = new_sketch();
+ for pts in lines {
+ run(&mut s, "sketch.line", json!({"points": pts, "connect": false}));
+ }
+ let d = run(&mut s, "sketch.dimension", json!({"entities": ["l1", "l2"]}));
+ assert!((d["measured"].as_f64().unwrap() - deg).abs() < 1e-4, "{deg}° shape {i}: {d}");
+ }
+ // A value turns the corner's sides to it, keeping them on the same side of each other.
+ let mut s = new_sketch();
+ run(&mut s, "sketch.line", json!({"points": [[20, 0], [0, 0], far], "connect": false}));
+ let want = deg + 10.0;
+ let d = run(&mut s, "sketch.dimension", json!({"entities": ["l1", "l2"], "value": format!("{want} deg")}));
+ assert!((d["measured"].as_f64().unwrap() - want).abs() < 1e-6, "{deg}°: {d}");
+ let sk = sketch(&s);
+ let line = |l: &str| crate::cmd::sketch::line_pts(&sk, sk.curve_index(l).unwrap()).unwrap();
+ let ((a0, a1), (b0, b1)) = (line("l1"), line("l2"));
+ assert!(a1.dist(b0) < 1e-9, "the corner stays joined");
+ let (ra, rb) = (a0 - a1, b1 - b0);
+ let between = ra.cross(rb).abs().atan2(ra.dot(rb)).to_degrees();
+ assert!((between - want).abs() < 1e-6, "{deg}°: the sides are {between}° apart");
+ }
+ // Lines that share no end keep the acute reading.
+ let mut s = new_sketch();
+ run(&mut s, "sketch.line", json!({"points": [[0, 0], [20, 0]], "connect": false}));
+ run(&mut s, "sketch.line", json!({"points": [[30, 10], [40, 27.3205]], "connect": false}));
+ let d = run(&mut s, "sketch.dimension", json!({"entities": ["l1", "l2"]}));
+ assert!((d["measured"].as_f64().unwrap() - 60.0).abs() < 1e-3, "{d}");
+}
+
#[test]
fn symmetry_moves_a_free_line_between_two_fixed_lines() {
// #1796: with both mirrored lines fixed, the symmetry line between them (a free construction