DXF: the current annotation scale is AutoCAD's CANNOSCALE variable

AutoCAD keeps the current annotation scale in the named object dictionary's
AcDbVariableDictionary, as a DICTIONARYVAR CANNOSCALE naming the scale.
CADCraft read only its own record, so AutoCAD's drawings opened at 1:1
whatever their scale (annotative objects then showed at their first scale),
and the variable was written back as read, so AutoCAD reopened a drawing at
its old scale after CADCraft changed it.

The variable is read when the scale is in the list (after CADCraft's record:
AutoCAD keeps that record as it was, so the variable is the newer one), and
the kept variable is written with the drawing's current scale. Annotative
objects are written at that scale.

Checked in CADCraft and AutoCAD: an AutoCAD drawing at 1:2 opens at 1:2;
switched to 1:10 and saved as DXF and DWG, AutoCAD opens both at 1:10.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Cody Vogt
2026-10-31 16:47:36 -06:00
co-authored by Claude Opus 5.5
parent 0f094da0ae
commit e89ff0832d
4 changed files with 61 additions and 0 deletions
+16
View File
@@ -390,6 +390,7 @@ pub(crate) fn resolve(secs: &[Section], d: &mut Drawing) {
objs.entry(h).or_insert((kind.to_ascii_uppercase(), tags));
}
});
current_scale(&objs, d);
if !objs.values().any(|(k, _)| k.ends_with("OBJECTCONTEXTDATA_CLASS")) {
return;
}
@@ -445,6 +446,21 @@ pub(crate) fn resolve(secs: &[Section], d: &mut Drawing) {
}
}
/// The current annotation scale as AutoCAD keeps it: in the named object dictionary's
/// `AcDbVariableDictionary`, a DICTIONARYVAR `CANNOSCALE` with the scale's name (group 1). It is
/// newer than CADCraft's own record when AutoCAD saved the file last (AutoCAD keeps that record as
/// it was), and CADCraft writes the two alike. A name the scale list lacks is passed over.
fn current_scale(objs: &HashMap<Handle, (String, &[Tag])>, d: &mut Drawing) {
let root = objs.iter().filter(|(_, (k, t))| k == "DICTIONARY" && own(t).iter().find(|t| t.code == 330).is_some_and(|o| o.str().trim() == "0"));
let Some((_, (_, root))) = root.min_by_key(|(h, _)| h.0) else { return };
let var = entry(root, "AcDbVariableDictionary").and_then(|v| objs.get(&v)).and_then(|(_, t)| entry(t, "CANNOSCALE")).and_then(|c| objs.get(&c));
let Some(name) = var.and_then(|(_, t)| own(t).iter().find(|t| t.code == 1).map(Tag::str)) else { return };
let name = name.trim();
if d.anno_scale(name).is_some_and(AnnoScale::is_valid) {
d.header.set_str("CANNOSCALE", name);
}
}
/// Take an object written at annotation scale `f` back to paper size (the inverse of
/// [`at_scale`]).
fn to_paper(e: &mut Entity, f: f64) {
+18
View File
@@ -1131,6 +1131,23 @@ fn reconcile(
out
}
/// AutoCAD's current annotation scale, kept with its variable dictionary (the named object
/// dictionary's `AcDbVariableDictionary`, a DICTIONARYVAR `CANNOSCALE` with the scale's name in
/// group 1), brought up to date with the drawing's: AutoCAD opens the drawing at that scale.
/// `None` when the file had none.
fn current_scale(f: &Foreign, d: &Drawing) -> Option<(String, ForeignObject)> {
let vars = f.dictionary.iter().find(|(k, _)| k.trim().eq_ignore_ascii_case("AcDbVariableDictionary"))?.1;
let object = |h: &str| f.objects.iter().find(|o| o.handle.hex() == norm(h));
let dict = object(&vars.hex())?;
let at = dict.tags.iter().position(|t| t.code == 3 && t.value.trim().eq_ignore_ascii_case("CANNOSCALE"))?;
let var = dict.tags.get(at + 1).filter(|t| matches!(t.code, 350 | 360))?;
let mut o = object(&var.value)?.clone();
let name = d.header.str("CANNOSCALE", "1:1");
let value = o.tags.iter_mut().find(|t| t.code == 1)?;
value.value = name;
Some((o.handle.hex(), o))
}
/// Drop an extension dictionary's modelled entries (key and handle) from its groups (`of_insert`:
/// it is a block reference's).
fn strip_entries(tags: &mut Vec<RawTag>, of_insert: bool) {
@@ -1225,6 +1242,7 @@ fn refreshed_contexts(
let mut contexts = block_contexts(f, d, owners);
let attribute = attribute_contexts(f, d, owners, &contexts);
contexts.extend(attribute);
contexts.extend(current_scale(f, d));
let rec = reconcile(f, d, owners, roles, &mut contexts, seed(tags).max(max_handle(tags)));
for o in &rec.added {
if let Some(parent) = o.tags.iter().find(|t| t.code == 330) {
@@ -775,3 +775,29 @@ fn a_multileader_keeps_its_scales_through_dwg() {
scales.sort_by(f64::total_cmp);
assert_eq!(scales, [1.0, 10.0]);
}
/// The fixture with AutoCAD's variable dictionary naming 1:10 as the current annotation scale.
fn with_current_scale() -> String {
dxf(FIXTURE)
.replace("3\nACAD_SCALELIST\n350\n10\n", "3\nACAD_SCALELIST\n350\n10\n3\nAcDbVariableDictionary\n350\n60\n")
.replace(
"0\nENDSEC\n0\nEOF\n",
"0\nDICTIONARY\n5\n60\n330\nC\n100\nAcDbDictionary\n281\n1\n3\nCANNOSCALE\n350\n61\n0\nDICTIONARYVAR\n5\n61\n330\n60\n100\nDictionaryVariables\n280\n0\n1\n1:10\n0\nENDSEC\n0\nEOF\n",
)
}
#[test]
fn the_current_scale_is_autocad_s_and_saving_keeps_it_current() {
let mut d = cadcraft_io::read_dxf(with_current_scale().as_bytes()).unwrap();
assert_eq!(d.header.str("CANNOSCALE", ""), "1:10");
// Written at the current scale: the 1:10 context is the default one.
let text = save(&d);
let got: Vec<(String, bool)> = contexts_of(&text, "30").into_iter().map(|c| (c.scale, c.default)).collect();
assert_eq!(got, [("1:2".to_string(), false), ("1:10".to_string(), true)]);
// Changed in CADCraft, AutoCAD's variable says so too.
d.header.set_str("CANNOSCALE", "1:2");
let recs = records(&save(&d));
let var = recs.iter().find(|(k, _)| k == "DICTIONARYVAR").unwrap();
assert_eq!(get(&var.1, 1), Some("1:2"));
assert_eq!(recs.iter().filter(|(k, _)| k == "DICTIONARYVAR").count(), 1);
}
+1
View File
@@ -150,6 +150,7 @@ our output in AutoCAD black-box.
| 2026-10-20 | minor | DIMENSION: the `*D` block's MTEXT, and the MTEXT EXPLODE leaves, hold the diameter sign as `%%c` instead of the decoded U+2300, which no SHX font has, so other programs plotted diameter dimensions in an SHX style without the sign (#1645) |
| 2026-10-21 | minor | Proxy objects and entities keep their class through a save: group 91 renumbered to the written CLASSES (the class it names is kept), a proxy whose number names no class dropped (#1812) |
| 2026-10-17 | minor | DWG read: DGN line style components no line style definition reaches are left out before the DXF bridge, with a read note; an 83 MB site plan with 888,483 of them (1.2 GB of DXF, over the limit) now opens (#1482) |
| 2026-10-15 | minor | The current annotation scale is read from and written to AutoCAD's variable dictionary (`AcDbVariableDictionary` CANNOSCALE), so a drawing opens at its scale in either program |
| 2026-10-15 | minor | Annotative multileaders read and write AutoCAD's annotation scale contexts (each context the multileader's `CONTEXT_DATA` at its scale), DXF and DWG |
| 2026-10-15 | minor | Annotative text, MText and dimensions read and write AutoCAD's annotation scale contexts (DXF and DWG 2007 and later): written at their default scale with a context per scale, a dimension with a `*D` block per scale; read back at paper size. Before, AutoCAD's were dropped (the objects read as not annotative) and CADCraft's kept only in its own record |
| 2026-10-12 | minor | PDF output: object transparency plotted as PDF alpha (`/ExtGState` `/ca` `/CA`) when the page setup's or the plot's "Plot transparency" is on; PNG plots blend it, SVG uses `opacity`; plots without it are unchanged byte for byte |