DXF: $ANGBASE is in degrees (#1234)

AutoCAD writes $ANGBASE in degrees (a drawing whose angles run clockwise from
north has 90.0); CADCraft read and wrote it as radians, so such a drawing's
angle base came in as 90 radians (about 116.6 degrees), turning typed angles,
polar tracking and angles shown in Properties. The reader now converts to
radians (the variable's unit), the writer back, and the DWG reader's hand-off
of the value in DXF form gives degrees.

Co-authored-by: Cody Vogt <18035288+codyvogt@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Coast
2026-10-19 02:31:12 -04:00
committed by GitHub
co-authored by Cody Vogt Claude Opus 5.5
parent aba903dbb3
commit 3fa7275e4f
6 changed files with 26 additions and 5 deletions
+2 -1
View File
@@ -330,7 +330,8 @@ mod native {
/// the HEADER section: (name, group code, value).
fn unwritten_header_vars(h: &acadrust::document::HeaderVariables) -> [(&'static str, i32, String); 7] {
[
("$ANGBASE", 50, h.angle_base.to_string()),
// DWG holds radians; `$ANGBASE` is in degrees.
("$ANGBASE", 50, h.angle_base.to_degrees().to_string()),
("$ANGDIR", 70, h.angle_direction.to_string()),
("$FILLETRAD", 40, h.fillet_radius.to_string()),
("$CHAMFERA", 40, h.chamfer_distance_a.to_string()),
+1 -1
View File
@@ -130,7 +130,7 @@ pub fn get(s: &Session, name: &str) -> Option<Value> {
"MAXARRAY" => json!(st.maxarray),
"ZOOMFACTOR" => json!(st.zoomfactor),
"LASTPOINT" => json!([s.last_point.x, s.last_point.y, 0.0]),
// Held in radians (as `$ANGBASE` in DXF); reported in degrees, like POLARANG.
// Held in radians (`$ANGBASE` in DXF is in degrees); reported in degrees, like POLARANG.
"ANGBASE" => json!(s.doc().ok()?.header.f64("ANGBASE", 0.0).to_degrees()),
"CONSTRAINTINFER" => json!(i32::from(s.doc().ok()?.parametric.settings.infer)),
// Process-wide font substitution (profile settings, not saved in the drawing).
+1 -1
View File
@@ -157,7 +157,7 @@ fn parse_mixed(s: &str) -> Option<f64> {
pub struct AngleSettings {
/// AUNITS: 0 decimal degrees, 1 degrees/minutes/seconds, 2 grads, 3 radians, 4 surveyor's.
pub aunits: i64,
/// ANGBASE: the direction of angle 0, in radians counterclockwise from +X (as `$ANGBASE` in DXF).
/// ANGBASE: the direction of angle 0, in radians counterclockwise from +X (`$ANGBASE` in DXF is in degrees).
pub angbase: f64,
/// ANGDIR 1: positive angles turn clockwise.
pub clockwise: bool,
+3
View File
@@ -1341,6 +1341,9 @@ fn header(tags: &[Tag], d: &mut Drawing) {
},
// $DIMJOGANG is in radians; the variable (like the style) holds degrees.
HVal::Real(r) if name == "DIMJOGANG" => HVal::Real((r.to_degrees() * 1e9).round() / 1e9),
// $ANGBASE is in degrees (a drawing measured from north has 90.0); the variable
// holds radians.
HVal::Real(r) if name == "ANGBASE" => HVal::Real(r.to_radians()),
hv => hv,
};
d.header.set(&name, hv);
+2
View File
@@ -358,6 +358,8 @@ fn header_vars(w: &mut W, d: &Drawing) {
(-2 | -3, _) => {}
(c @ (40 | 50), v) => {
if let Some(f) = v.as_f64() {
// ANGBASE is held in radians; `$ANGBASE` is in degrees.
let f = if *name == "ANGBASE" { f.to_degrees() } else { f };
w.s(9, format!("${name}"));
w.f(c, f);
}
+17 -2
View File
@@ -1,5 +1,6 @@
//! The angle settings round-trip through DXF: `$ANGBASE` (group 50) in radians, `$ANGDIR` and
//! `$AUNITS` (group 70) as integers.
//! The angle settings round-trip through DXF: `$ANGBASE` (group 50) in degrees, as AutoCAD writes
//! it (a drawing measured from north has 90.0), held in radians; `$ANGDIR` and `$AUNITS`
//! (group 70) as integers.
use cadcraft_doc::{Drawing, HVal};
@@ -10,9 +11,23 @@ fn angbase_angdir_aunits_round_trip_through_dxf() {
d.header.set_i64("ANGDIR", 1);
d.header.set_i64("AUNITS", 3);
let text = cadcraft_io::write_dxf(&d);
// Written in degrees.
let lines: Vec<&str> = text.lines().map(str::trim).collect();
let at = lines.iter().position(|l| *l == "$ANGBASE").expect("$ANGBASE is written");
let written: f64 = lines.get(at + 2).and_then(|v| v.parse().ok()).expect("a number");
assert!((written - 90.0).abs() < 1e-9, "$ANGBASE written as {written}");
let r = cadcraft_io::read_dxf(text.as_bytes()).unwrap();
let base = r.header.f64("ANGBASE", 0.0);
assert!((base - std::f64::consts::FRAC_PI_2).abs() < 1e-12, "$ANGBASE read back as {base}");
assert_eq!(r.header.get("ANGDIR"), Some(&HVal::Int(1)));
assert_eq!(r.header.get("AUNITS"), Some(&HVal::Int(3)));
}
#[test]
fn angbase_in_degrees_is_read_as_radians() {
// What AutoCAD writes for a drawing whose angles run clockwise from north.
let text = "0\nSECTION\n2\nHEADER\n9\n$ANGBASE\n50\n90.0\n9\n$ANGDIR\n70\n1\n0\nENDSEC\n0\nEOF\n";
let d = cadcraft_io::read_dxf(text.as_bytes()).unwrap();
let base = d.header.f64("ANGBASE", 0.0);
assert!((base - std::f64::consts::FRAC_PI_2).abs() < 1e-12, "$ANGBASE 90.0 read as {base} radians");
}