DXF files that declare no units stay unitless when saved again (fixes #1209) (#1210)

This commit is contained in:
Marco Arnulfo
2026-10-19 02:31:01 -04:00
committed by GitHub
parent a2bee8b49e
commit 493addc2dd
2 changed files with 22 additions and 0 deletions
+2
View File
@@ -1655,6 +1655,8 @@ pub fn read(bytes: &[u8]) -> Result<Drawing> {
} }
let mut d = Drawing::new_imperial(); let mut d = Drawing::new_imperial();
d.layouts.clear(); 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 rx = Rx { pstyles: plot_style_names(&secs), ..Rx::default() };
let mut blocks: Vec<(String, Vec3, String, Vec<(Option<String>, bool, Entity)>, i64)> = Vec::new(); let mut blocks: Vec<(String, Vec3, String, Vec<(Option<String>, bool, Entity)>, i64)> = Vec::new();
let mut layout_objs: Vec<(String, u32, String, PageSetup)> = Vec::new(); let mut layout_objs: Vec<(String, u32, String, PageSetup)> = Vec::new();
+20
View File
@@ -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][0].text_height, Some(150.0));
assert_eq!(t2.cells[0][1].text_height, None); 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::<Vec<_>>().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()));
}