From 493addc2dd810103deae05a9d19dbd3f5340b072 Mon Sep 17 00:00:00 2001 From: Marco Arnulfo <103679419+marcoarnulfo@users.noreply.github.com> Date: Mon, 19 Oct 2026 08:31:01 +0200 Subject: [PATCH] DXF files that declare no units stay unitless when saved again (fixes #1209) (#1210) --- crates/io/src/dxf_read.rs | 2 ++ crates/io/src/tests.rs | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/crates/io/src/dxf_read.rs b/crates/io/src/dxf_read.rs index 1bd7cd9c..c043842e 100644 --- a/crates/io/src/dxf_read.rs +++ b/crates/io/src/dxf_read.rs @@ -1655,6 +1655,8 @@ pub fn read(bytes: &[u8]) -> Result { } let mut d = Drawing::new_imperial(); d.layouts.clear(); + // A file without $INSUNITS declares no units (#1209); only new drawings take the template's. + d.header.set_i64("INSUNITS", 0); let mut rx = Rx { pstyles: plot_style_names(&secs), ..Rx::default() }; let mut blocks: Vec<(String, Vec3, String, Vec<(Option, bool, Entity)>, i64)> = Vec::new(); let mut layout_objs: Vec<(String, u32, String, PageSetup)> = Vec::new(); diff --git a/crates/io/src/tests.rs b/crates/io/src/tests.rs index 20a18fe5..8b7f5647 100644 --- a/crates/io/src/tests.rs +++ b/crates/io/src/tests.rs @@ -2502,3 +2502,23 @@ fn foreign_table_reads_cell_text_height_and_keeps_style_scale() { assert_eq!(t2.cells[0][0].text_height, Some(150.0)); assert_eq!(t2.cells[0][1].text_height, None); } + +/// Issue #1209: a file that declares no units ($INSUNITS absent or 0) stays unitless when written +/// again; only a new drawing takes its template's units. +#[test] +fn drawing_without_insunits_stays_unitless() { + let r12 = |extra: &str| { + format!( + "0\nSECTION\n2\nHEADER\n9\n$ACADVER\n1\nAC1009\n{extra}0\nENDSEC\n0\nSECTION\n2\nENTITIES\n0\nLINE\n8\n0\n10\n0\n20\n0\n11\n1\n21\n1\n0\nENDSEC\n0\nEOF\n" + ) + }; + let insunits = |text: &str| { + text.lines().map(str::trim).collect::>().windows(3).find(|w| w[0] == "$INSUNITS" && w[1] == "70").map(|w| w[2].to_string()) + }; + let rewritten = |extra: &str| insunits(&write_dxf(&read_dxf(r12(extra).as_bytes()).unwrap())); + assert_eq!(rewritten(""), Some("0".into()), "absent"); + assert_eq!(rewritten("9\n$INSUNITS\n70\n0\n"), Some("0".into()), "declared unitless"); + assert_eq!(rewritten("9\n$INSUNITS\n70\n6\n"), Some("6".into()), "declared metres"); + assert_eq!(insunits(&write_dxf(&Drawing::new_metric())), Some("4".into())); + assert_eq!(insunits(&write_dxf(&Drawing::new_imperial())), Some("1".into())); +}