diff --git a/crates/io/src/dxf_layer_states.rs b/crates/io/src/dxf_layer_states.rs index 062818d04..5227687dc 100644 --- a/crates/io/src/dxf_layer_states.rs +++ b/crates/io/src/dxf_layer_states.rs @@ -12,8 +12,8 @@ //! //! States AutoCAD's dictionary can't hold stay in CADCraft's own record (`CADCRAFT_LAYERSTATES`): //! the view layer snapshots (`*VIEW name`), and a name that repeats an earlier one in another -//! case (of states `S` and `s` in one dictionary AutoCAD 2024 lists only `S`, and restoring `s` -//! restores `S`, measured). +//! case: AutoCAD 2024 opens the entries `S` and `s` of one dictionary as one state, named as the +//! first entry and holding the last one's record (measured), and so does the reader here. use std::collections::{HashMap, HashSet}; diff --git a/crates/io/src/dxf_read/objects.rs b/crates/io/src/dxf_read/objects.rs index a33e6e2b7..0fbc7db40 100644 --- a/crates/io/src/dxf_read/objects.rs +++ b/crates/io/src/dxf_read/objects.rs @@ -8,6 +8,8 @@ use super::{Drawing, EntityKind, Group, Handle, HashMap, MLeaderStyle, PageSetup pub(super) struct Objects { /// Dictionary entry handle (upper case) → entry name. names: HashMap, + /// (dictionary handle, entry handle), both upper case, in the order the dictionaries list them. + entry_order: Vec<(String, String)>, /// SCALE objects (the annotation scale list), in file order. scales: Vec, /// XRECORD handle (upper case) → its groups. @@ -52,7 +54,11 @@ impl Objects { if let Some(n) = name.take() && self.names.len() < MAX_OBJECTS { - self.names.insert(t.str().trim().to_ascii_uppercase(), n); + let entry = t.str().trim().to_ascii_uppercase(); + if self.entry_order.len() < MAX_OBJECTS { + self.entry_order.push((h.clone(), entry.clone())); + } + self.names.insert(entry, n); } } _ => {} @@ -85,7 +91,10 @@ impl Objects { } /// The layer states of the LAYER table's `ACAD_LAYERSTATES` dictionary (its XRECORDs, in file - /// order), read with the drawing's layers and linetypes. + /// order), read with the drawing's layers and linetypes. Entries whose names differ only in case + /// are one state, as AutoCAD 2024 opens them (measured with `S` and `s` in both orders): named as + /// the first of them in the dictionary, holding the last one's record, where the first of their + /// records comes in the file. fn acad_layer_states(&self, d: &Drawing, rx: &Rx) -> Vec { let dicts: std::collections::HashSet<&str> = self.names.iter().filter(|(_, n)| n.eq_ignore_ascii_case(crate::dxf_layer_states::KEY)).map(|(h, _)| h.as_str()).collect(); @@ -106,14 +115,34 @@ impl Objects { pstyle: &pstyle_of, colour_dependent: d.header.i64("PSTYLEMODE", 1) != 0, }; + let ours = |tags: &[Tag]| owner(tags).is_some_and(|o| dicts.contains(o.as_str())); + // By lower-case name: the first entry's name and the last entry's record, in dictionary order. + let mut first: HashMap = HashMap::new(); + let mut last: HashMap> = HashMap::new(); + for (dict, h) in &self.entry_order { + if dicts.contains(dict.as_str()) + && let (Some(name), Some(tags)) = (self.names.get(h), self.xrecords.get(h)) + && ours(tags) + { + first.entry(name.to_ascii_lowercase()).or_insert(name); + last.insert(name.to_ascii_lowercase(), tags); + } + } let mut out = Vec::new(); + let mut taken = std::collections::HashSet::new(); let mut budget = crate::dxf_layer_states::MAX_COPIED_BYTES; for h in &self.xrecord_order { let (Some(tags), Some(name)) = (self.xrecords.get(h), self.names.get(h)) else { continue }; if out.len() >= crate::dxf_ext::MAX_LAYER_STATES { break; } - if owner(tags).is_some_and(|o| dicts.contains(o.as_str())) { + if !ours(tags) { + continue; + } + let key = name.to_ascii_lowercase(); + if taken.insert(key.clone()) { + let name = first.get(&key).copied().unwrap_or(name); + let tags = last.get(&key).copied().unwrap_or(tags); out.push(crate::dxf_layer_states::read(name, tags, &lookup, &mut budget)); } } diff --git a/crates/io/tests/it/layer_states_acad.rs b/crates/io/tests/it/layer_states_acad.rs index bf5a2ac33..80ca74ddb 100644 --- a/crates/io/tests/it/layer_states_acad.rs +++ b/crates/io/tests/it/layer_states_acad.rs @@ -274,6 +274,40 @@ fn layer_filters_next_to_the_states_are_kept() { assert!(read(text.as_bytes(), "g.dxf").unwrap().layer_states.is_empty()); } +#[test] +fn entries_whose_names_differ_only_in_case_open_as_one_state() { + // AutoCAD 2024 opens the entries `S` and `s` of one ACAD_LAYERSTATES dictionary as one state: + // named as the first entry in the dictionary and holding the last entry's record (measured with + // the records in one file order and the entries in both dictionary orders, so the first entry's + // record came first in the file once and last once). Here S2's record (mask 2047) comes first in + // the file, then state T's, then S7's (66049); the merged state stays where S2's record is. + let base = write_dxf(&drawing()); + let p = pairs(&base); + let h = |k| fresh(&p, k); + let (x, y, r2, r7, rt) = (h(1), h(2), h(3), h(4), h(5)); + let states = |d: &Drawing| d.layer_states.iter().map(|s| (s.name.clone(), s.mask)).collect::>(); + for (pair, name, mask) in [([("S", r2.as_str()), ("s", r7.as_str())], "S", 66049), ([("s", r7.as_str()), ("S", r2.as_str())], "s", 2047)] { + let entries = [pair[0], ("T", rt.as_str()), pair[1]]; + let mut objects = dictionaries(&p, &x, &y, &entries, &[]); + objects.extend(s2(&p, &r2, &y)); + objects.extend(s7(&p, &rt, &y)); + objects.extend(s7(&p, &r7, &y)); + let d = read(inject(&base, &x, objects).as_bytes(), "case.dxf").unwrap(); + let want = [(name.to_string(), mask), ("T".to_string(), 66049)]; + assert_eq!(states(&d), want, "{entries:?}"); + // Written back, they are the states AutoCAD opened. + let back = read(write_dxf(&d).as_bytes(), "back.dxf").unwrap(); + assert_eq!(states(&back), want, "{entries:?}"); + } + // A record the dictionary lists as `s` but another object owns isn't a state, and doesn't stand + // in for S's record either. + let mut objects = dictionaries(&p, &x, &y, &[("S", r2.as_str()), ("s", r7.as_str())], &[]); + objects.extend(s2(&p, &r2, &y)); + objects.extend(s7(&p, &r7, &x)); + let d = read(inject(&base, &x, objects).as_bytes(), "stray.dxf").unwrap(); + assert_eq!(states(&d), [("S".to_string(), 2047)]); +} + #[test] fn view_snapshots_and_names_twice_in_another_case_stay_in_cadcrafts_record() { let mut d = drawing();