mirror of
https://github.com/storytold/cadcraft.git
synced 2026-12-01 18:25:32 -06:00
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 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
e0fa2af3b0
commit
95f7f6687a
@@ -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}."));
|
||||
|
||||
@@ -141,9 +141,26 @@ pub fn reference(d: &Drawing, insert: Handle) -> Option<Reference> {
|
||||
}
|
||||
|
||||
/// 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).
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user