From 95f7f6687ab607e2fb28d04579e8fc3dec99ecd6 Mon Sep 17 00:00:00 2001 From: Cody Vogt <18035288+codyvogt@users.noreply.github.com> Date: Wed, 14 Oct 2026 08:19:15 -0600 Subject: [PATCH] Dynamic blocks: an angle parameter shows and is typed as the drawing measures angles AutoCAD shows an angle parameter's value as it shows any angle: from ANGBASE, clockwise when ANGDIR is 1. In a drawing measured clockwise from north it shows an Angle1 of 0 as 90, and 0.5 rad as 61.35 (checked in AutoCAD). Properties and typed values (blocks.reference.set, a value typed at the rotation grip) now convert the same way; the stored value is unchanged. Co-Authored-By: Claude Opus 5.5 --- crates/doc/src/dynblock/apply.rs | 3 ++- crates/doc/src/dynblock/reference.rs | 19 ++++++++++++++++++- crates/io/tests/dynblock_rotate_flip.rs | 16 ++++++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/crates/doc/src/dynblock/apply.rs b/crates/doc/src/dynblock/apply.rs index 2d07a901c..66a022a30 100644 --- a/crates/doc/src/dynblock/apply.rs +++ b/crates/doc/src/dynblock/apply.rs @@ -326,7 +326,8 @@ pub fn set_property(d: &mut Drawing, insert: Handle, name: &str, value: &str) -> } // An angle in degrees, as Properties shows it, snapped to the parameter's values. (ParameterKind::Rotation { base: hb, end: he, values, label, .. }, None) => { - let typed = number(value)?.to_radians().rem_euclid(std::f64::consts::TAU); + // Typed as the drawing shows angles (from ANGBASE, clockwise when ANGDIR is 1). + let typed = super::reference::angle_from_shown(d, number(value)?.to_radians()); let a = allowed(values, typed); if !a.is_finite() { return Err(format!("\"{value}\" is not an angle for {label}.")); diff --git a/crates/doc/src/dynblock/reference.rs b/crates/doc/src/dynblock/reference.rs index d74888ff1..2c23227a6 100644 --- a/crates/doc/src/dynblock/reference.rs +++ b/crates/doc/src/dynblock/reference.rs @@ -141,9 +141,26 @@ pub fn reference(d: &Drawing, insert: Handle) -> Option { } /// An angle (radians) as Properties shows it: degrees with AUPREC decimals. +/// An angle parameter's value `a` (radians, counterclockwise) as the drawing shows angles: from +/// ANGBASE, clockwise when ANGDIR is 1 (AutoCAD shows a 0 angle as 90 in a drawing measured +/// clockwise from north, and 0.5 rad as 61.35). +pub(super) fn angle_shown(d: &Drawing, a: f64) -> f64 { + let base = d.header.f64("ANGBASE", 0.0); + let shown = if d.header.i64("ANGDIR", 0) == 1 { base - a } else { a - base }; + shown.rem_euclid(std::f64::consts::TAU) +} + +/// The value of an angle parameter shown (or typed) as `v` (radians): the inverse of +/// [`angle_shown`]. +pub(super) fn angle_from_shown(d: &Drawing, v: f64) -> f64 { + let base = d.header.f64("ANGBASE", 0.0); + let a = if d.header.i64("ANGDIR", 0) == 1 { base - v } else { v + base }; + a.rem_euclid(std::f64::consts::TAU) +} + fn angle_text(d: &Drawing, a: f64) -> String { let prec = usize::try_from(d.header.i64("AUPREC", 0).clamp(0, 8)).unwrap_or(0); - format!("{:.prec$}", a.to_degrees()) + format!("{:.prec$}", angle_shown(d, a).to_degrees()) } /// A number as the drawing shows lengths (LUPREC decimals). diff --git a/crates/io/tests/dynblock_rotate_flip.rs b/crates/io/tests/dynblock_rotate_flip.rs index ee8a10b13..1acb72a7c 100644 --- a/crates/io/tests/dynblock_rotate_flip.rs +++ b/crates/io/tests/dynblock_rotate_flip.rs @@ -166,6 +166,22 @@ fn a_rotation_grip_off_the_radius_stays_where_its_grip_object_is() { assert!(near(at(&d), (98.0, 100.0)), "{:?}", at(&d)); } +#[test] +fn an_angle_shows_and_is_typed_as_the_drawing_measures_angles() { + // Angles measured clockwise from north (ANGBASE 90°, ANGDIR 1): AutoCAD shows Angle1 0 as + // 90 and 0.5 rad as 61.35, and a typed angle is read the same way. + let (mut d, r) = symbol(); + d.header.set_f64("ANGBASE", std::f64::consts::FRAC_PI_2); + d.header.set_i64("ANGDIR", 1); + assert!(props(&d, r).contains(&("Angle1".to_string(), "90".to_string())), "{:?}", props(&d, r)); + // 60 shown is 30° counterclockwise: the grip turns from (102, 100) to there. + set_property(&mut d, r, "Angle1", "60").unwrap(); + assert!(props(&d, r).contains(&("Angle1".to_string(), "60".to_string())), "{:?}", props(&d, r)); + let at = grips(&d, r).into_iter().find(|g| g.shape == GripShape::Rotation).map(|g| g.at).unwrap(); + let want = (100.0 + 2.0 * 30f64.to_radians().cos(), 100.0 + 2.0 * 30f64.to_radians().sin()); + assert!(near(at, want), "{at:?} vs {want:?}"); +} + #[test] fn bad_angles_and_states_change_nothing() { let (mut d, r) = symbol();