Files
cadcraft/crates/engine/tests/insert_file_field.rs
T
SlopapaloozaandClaude Opus 5.5 c6016720ee TEXT's own Backwards and Upside down (DXF 71 generation flags)
TEXT, ATTDEF and ATTRIB have their own Backwards and Upside down, as AutoCAD's do (DXF group
71: 2 backwards, 4 upside down). New TEXT and attribute definitions take them from their style;
changing the style afterwards leaves existing text as it is. They were the style's only, so
text read from a file with its own flags was drawn as its style said.

- Model: `Text::backwards` and `Text::upside_down` (serde default false).
- DXF: read from 71 (the object's own groups, before an embedded MTEXT) and written when set.
- Render: drawn from the text's flags; the extents box mirrors with them, so zooming to
  extents frames mirrored text. Linetype text keeps its style's.
- Properties: a Misc group with Upside down and Backwards (`properties.describe`, and
  `properties.set` takes `backwards` and `upsideDown`), for several texts at once with *VARIES*.
- ATTDEF builds its text as TEXT does, so it also takes the style's width factor and obliquing.
- Round trip: #2237's matrices compare both flags, with DXF and DWG cases for them. Its
  mirrored-style cases mirror the text by the text's own flags, and each mirrored style
  also has text whose flags differ, so a style's flags can't leak into its text's.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 09:19:23 +01:00

110 lines
4.8 KiB
Rust

//! `INSERT name=file` keeps the field of an attribute definition: the definition's extension
//! dictionary and the objects in it come along under new handles, so the attributes the block's
//! references get can carry the field when the drawing is saved.
use std::sync::Arc;
use cadcraft_engine::Session;
use cadcraft_engine::cmd::file::{IoHooks, set_io};
use cadcraft_engine::doc::foreign::{ForeignExtra, ForeignObject};
use cadcraft_engine::doc::{Attrib, Drawing, Entity, EntityKind, Handle, RawTag, Text};
use cadcraft_engine::geom::Vec3;
use serde_json::json;
#[path = "support/temp_dir.rs"]
mod temp_dir;
use temp_dir::TempDir;
fn tag(code: i32, value: &str) -> RawTag {
RawTag { code, value: value.into() }
}
fn object(handle: u64, kind: &str, tags: Vec<RawTag>) -> ForeignObject {
ForeignObject { handle: Handle(handle), kind: kind.into(), tags }
}
/// A drawing file in a test format: model space holds a definition NUMBER (handle 0x50) whose
/// extension dictionary 0x60 owns the ACAD_FIELD dictionary 0x61, which owns the field 0x62 with
/// a child field 0x63. Handles 0x61..0x63 stand for the file's own; a second, unrelated object
/// 0x70 is not part of that tree. The bytes `plain` give the same definition without any of it.
fn read(bytes: &[u8], _name: &str) -> Result<Drawing, String> {
let mut d = Drawing::new_imperial();
let text = Text {
insert: Vec3::ZERO,
align_pt: None,
height: 2.0,
value: "TEMPLATE".into(),
rotation: 0.0,
width_factor: 1.0,
oblique: 0.0,
style: "Standard".into(),
halign: Default::default(),
valign: Default::default(),
backwards: false,
upside_down: false,
};
let def = Attrib {
tag: "NUMBER".into(),
text,
invisible: false,
constant: false,
prompt: String::new(),
props: Default::default(),
multiline: None,
modes: Default::default(),
};
d.model.push(Entity::new(Handle(0x50), EntityKind::AttDef(def)));
if bytes == b"plain" {
return Ok(d);
}
let f = Arc::make_mut(&mut d.foreign);
f.entities.insert(Handle(0x50), ForeignExtra { xdict: Some(Handle(0x60)), ..Default::default() });
f.objects.push(object(0x60, "DICTIONARY", vec![tag(5, "60"), tag(330, "50"), tag(3, "ACAD_FIELD"), tag(360, "61")]));
f.objects.push(object(0x61, "DICTIONARY", vec![tag(5, "61"), tag(330, "60"), tag(3, "TEXT"), tag(360, "62")]));
f.objects.push(object(0x62, "FIELD", vec![tag(5, "62"), tag(330, "61"), tag(1, "_text"), tag(360, "63")]));
f.objects.push(object(0x63, "FIELD", vec![tag(5, "63"), tag(330, "62"), tag(1, "AcVar")]));
f.objects.push(object(0x70, "XRECORD", vec![tag(5, "70"), tag(330, "0")]));
f.classes.push(vec![tag(1, "FIELD"), tag(2, "AcDbField")]);
Ok(d)
}
fn hooks() {
set_io(IoHooks { read, write: |_, _| Err("no writer in this test".into()), plot: None });
}
#[test]
fn the_definition_of_an_inserted_file_keeps_its_field() {
hooks();
let dir = TempDir::new("insert-file-field");
let path = dir.file("border.dxf", "");
let mut s = Session::new();
s.execute("insert", &json!({ "name": "BORDER", "file": path, "at": [0, 0], "attribs": { "NUMBER": "A-1" } })).unwrap();
let d = s.doc().unwrap();
let def = d.block("BORDER").and_then(|b| b.entities.iter().find(|e| matches!(e.kind, EntityKind::AttDef(_)))).expect("the definition");
assert_ne!(def.handle, Handle(0x50), "the definition has a handle of this drawing");
let root = d.foreign.entities.get(&def.handle).and_then(|x| x.xdict).expect("the definition has an extension dictionary");
let tree = d.foreign.tree(root);
assert_eq!(tree.len(), 4, "the dictionaries and both fields, not the unrelated object");
assert!(tree.iter().all(|h| ![0x60, 0x61, 0x62, 0x63, 0x70].contains(&h.0)), "the copies have new handles");
// The root's owner is the definition, the others point at each other.
let owner =
|h: Handle| d.foreign.objects.iter().find(|o| o.handle == h).and_then(|o| o.tags.iter().find(|t| t.code == 330)).map(|t| t.value.clone());
assert_eq!(owner(tree[0]), Some(def.handle.hex()));
assert_eq!(owner(tree[1]), Some(tree[0].hex()));
assert_eq!(owner(tree[3]), Some(tree[2].hex()));
assert!(d.foreign.classes.iter().any(|c| c.iter().any(|t| t.code == 1 && t.value == "FIELD")), "the field class comes along");
}
#[test]
fn a_definition_without_a_field_gets_none() {
hooks();
let dir = TempDir::new("insert-file-nofield");
let path = dir.file("plain.dxf", "plain");
let mut s = Session::new();
s.execute("insert", &json!({ "name": "PLAIN", "file": path, "at": [0, 0] })).unwrap();
let d = s.doc().unwrap();
assert!(d.block("PLAIN").is_some());
assert!(d.foreign.is_empty(), "nothing is kept for a drawing without fields");
}