Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/io/src/dxf_read/entities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ pub(super) fn entity_with_table_budget(kind: &str, tags: &[Tag], table_cells_lef
"HELIX" => EntityKind::Helix(helix(tags)),
"RAY" => EntityKind::Ray(RayLine { base: t.p(10), dir: t.p(11) }),
"XLINE" => EntityKind::XLine(RayLine { base: t.p(10), dir: t.p(11) }),
"TEXT" => EntityKind::Text(text_from(&t)),
"TEXT" => EntityKind::Text(text_from(&t, false)),
"MTEXT" => EntityKind::MText(mtext_from(tags, &c)),
"ATTDEF" => EntityKind::AttDef(attdef(tags, &c)),
"INSERT" => {
Expand Down Expand Up @@ -541,7 +541,7 @@ pub(super) fn insert_attribs(recs: &[(String, Vec<Tag>)], i: &mut usize, ins_lay
let (own, embedded) = split_embedded(t2);
let multiline = multiline_attrib(embedded, &common(&tt));
let own = T(own);
let mut text = text_from(&own);
let mut text = text_from(&own, true);
if let Some(m) = &multiline {
text.value.clone_from(&m.read_as);
}
Expand Down
12 changes: 9 additions & 3 deletions crates/io/src/dxf_read/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,13 @@ pub(super) fn mtext_from(tags: &[Tag], c: &Common) -> MText {
}
}

pub(super) fn text_from(t: &T) -> Text {
/// A TEXT, or (with `attribute`) an ATTRIB or ATTDEF. The vertical alignment is group 73 for
/// TEXT and group 74 for attributes, whose group 73 is the field length: a writer that emits
/// the field length (acadrust's DWG bridge always does, AutoCAD may) used to turn every
/// attribute into baseline text, which put centred and right-aligned ones half a height too
/// high and top-aligned ones a whole height (their alignment point is then taken for the
/// baseline); left-aligned ones were saved by their precomputed insertion point.
pub(super) fn text_from(t: &T, attribute: bool) -> Text {
let h = match t.i(72).unwrap_or(0) {
1 => HAlign::Center,
2 => HAlign::Right,
Expand All @@ -101,7 +107,7 @@ pub(super) fn text_from(t: &T) -> Text {
5 => HAlign::Fit,
_ => HAlign::Left,
};
let v = match t.i(73).or_else(|| t.i(74)).unwrap_or(0) {
let v = match t.i(if attribute { 74 } else { 73 }).unwrap_or(0) {
1 => VAlign::Bottom,
2 => VAlign::Middle,
3 => VAlign::Top,
Expand Down Expand Up @@ -163,7 +169,7 @@ pub(super) fn attdef(tags: &[Tag], c: &Common) -> Attrib {
let (own, embedded) = split_embedded(tags);
let multiline = multiline_attrib(embedded, c);
let own = T(own);
let mut text = text_from(&own);
let mut text = text_from(&own, true);
if let Some(m) = &multiline {
text.value.clone_from(&m.read_as);
}
Expand Down
122 changes: 122 additions & 0 deletions crates/io/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1015,6 +1015,128 @@ fn oversized_dwg_and_dxf_are_rejected_early() {
assert_eq!(e.to_string(), "drawing too large: 60 DXF group codes, more than the 50 CADCraft reads");
}

/// An attribute's vertical alignment is group 74; its group 73 is the field length. acadrust's
/// DWG bridge writes the field length (0) for every ATTRIB and ATTDEF, and the reader took it
/// for the alignment, so a middle-centred attribute from any DWG was drawn with its baseline on
/// the alignment point, half a height too high (a top-aligned one a whole height, a
/// bottom-aligned one a third below); left-aligned ones were saved by their precomputed
/// insertion point. Measured on an office sheet against BricsCAD's own plot: +0.52 h (MR),
/// +1.00 h (TC), -0.29 h (BC), 0.00 after this.
#[test]
fn attribute_vertical_alignment_is_group_74_not_the_field_length() {
let dxf = "0\nSECTION\n2\nBLOCKS\n0\nBLOCK\n2\nB\n10\n0\n20\n0\n0\nLINE\n8\n0\n10\n0\n20\n0\n11\n1\n21\n0\n0\nENDBLK\n0\nENDSEC\n\
0\nSECTION\n2\nENTITIES\n\
0\nINSERT\n66\n1\n2\nB\n10\n0\n20\n0\n\
0\nATTRIB\n8\n0\n10\n0\n20\n0\n40\n2\n1\nX\n11\n5\n21\n7\n2\nT\n70\n0\n73\n0\n72\n1\n74\n3\n\
0\nSEQEND\n\
0\nATTDEF\n8\n0\n10\n0\n20\n0\n40\n2\n1\nX\n3\nP\n11\n5\n21\n7\n2\nT\n70\n0\n73\n0\n72\n2\n74\n2\n\
0\nTEXT\n8\n0\n10\n0\n20\n0\n40\n2\n1\nX\n11\n5\n21\n7\n72\n1\n73\n2\n\
0\nENDSEC\n0\nEOF\n";
let d = read(dxf.as_bytes(), "x.dxf").unwrap();
let ins = d.model.iter().find_map(|e| if let EntityKind::Insert(i) = &e.kind { Some(i) } else { None }).unwrap();
let a = &ins.attribs[0].text;
assert_eq!(
(a.halign, a.valign, a.align_pt),
(HAlign::Center, VAlign::Top, Some(Vec3::new(5.0, 7.0, 0.0))),
"placed attribute: 74 = 3 is top, 73 = 0 is the field length"
);
let def = d.model.iter().find_map(|e| if let EntityKind::AttDef(a) = &e.kind { Some(&a.text) } else { None }).unwrap();
assert_eq!(
(def.halign, def.valign, def.align_pt),
(HAlign::Right, VAlign::Middle, Some(Vec3::new(5.0, 7.0, 0.0))),
"definition: 74 = 2 is middle"
);
let t = d.model.iter().find_map(|e| if let EntityKind::Text(t) = &e.kind { Some(t) } else { None }).unwrap();
assert_eq!((t.halign, t.valign), (HAlign::Center, VAlign::Middle), "TEXT keeps group 73");
}

/// The writer's side of the above: an attribute definition and an attribute in each of the 15
/// justifications come back with the same one from a DXF CADCraft saved (it writes the
/// alignment as group 74 and no field length) and from a DWG saved from that DXF, whose
/// DWG→DXF rendition writes the field length `73 0` before `74`.
#[test]
fn attributes_keep_all_fifteen_justifications_through_dxf_and_dwg() {
use HAlign as H;
use VAlign as V;
let justifications = [
(H::Left, V::Baseline),
(H::Center, V::Baseline),
(H::Right, V::Baseline),
(H::Aligned, V::Baseline),
(H::Middle, V::Baseline),
(H::Fit, V::Baseline),
(H::Left, V::Top),
(H::Center, V::Top),
(H::Right, V::Top),
(H::Left, V::Middle),
(H::Center, V::Middle),
(H::Right, V::Middle),
(H::Left, V::Bottom),
(H::Center, V::Bottom),
(H::Right, V::Bottom),
];
let text = |i: usize, (halign, valign): (HAlign, VAlign)| Text {
insert: Vec3::new(0.0, 10.0 * i as f64, 0.0),
align_pt: ((halign, valign) != (H::Left, V::Baseline)).then(|| Vec3::new(20.0, 10.0 * i as f64, 0.0)),
height: 2.5,
value: format!("V{i}"),
rotation: 0.0,
width_factor: 1.0,
oblique: 0.0,
style: "Standard".into(),
halign,
valign,
};
let attrib = |i: usize, j: (HAlign, VAlign)| Attrib {
tag: format!("T{i}"),
text: text(i, j),
invisible: false,
constant: false,
prompt: String::new(),
props: Default::default(),
multiline: None,
modes: Default::default(),
};
let mut d = Drawing::new_metric();
let mut b = Block::new("J");
for (i, &j) in justifications.iter().enumerate() {
b.entities.push(Entity::new(Handle(0x50 + i as u64), EntityKind::AttDef(attrib(i, j))));
}
d.blocks.insert("J".into(), std::sync::Arc::new(b));
let insert = Insert {
block: "J".into(),
insert: Vec3::ZERO,
scale: Vec3::new(1.0, 1.0, 1.0),
rotation: 0.0,
attribs: justifications.iter().enumerate().map(|(i, &j)| attrib(i, j)).collect(),
cols: 1,
rows: 1,
col_spacing: 0.0,
row_spacing: 0.0,
};
d.add(&Space::Model, Common::default(), EntityKind::Insert(insert)).unwrap();
let dxf = write_dxf(&d);
let dwg = cadcraft_dwg::dxf_to_dwg(dxf.as_bytes()).unwrap();
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
let from_dwg = read_dxf(&cadcraft_dwg::dwg_to_dxf(&dwg).unwrap()).unwrap();
for (back, what) in [(read_dxf(dxf.as_bytes()).unwrap(), "DXF"), (from_dwg, "DWG")] {
let ins = first(&back, |k| if let EntityKind::Insert(i) = k { Some(i.clone()) } else { None });
let defs: Vec<Attrib> = back
.block("J")
.unwrap()
.entities
.iter()
.filter_map(|e| if let EntityKind::AttDef(a) = &e.kind { Some(a.clone()) } else { None })
.collect();
for (i, &j) in justifications.iter().enumerate() {
let tag = format!("T{i}");
let att = ins.attribs.iter().find(|a| a.tag == tag).unwrap_or_else(|| panic!("{what}: attribute {tag}"));
assert_eq!((att.text.halign, att.text.valign), j, "{what}: attribute {tag}");
let def = defs.iter().find(|a| a.tag == tag).unwrap_or_else(|| panic!("{what}: definition {tag}"));
assert_eq!((def.text.halign, def.text.valign), j, "{what}: definition {tag}");
}
}
}

/// Issue #800: one unreadable object record doesn't fail the whole DWG. The reader skips it, the
/// rest opens, and the drawing's read notes say how many records were left out and of which
/// types, so a partial read never passes for a complete one. A complete read has no notes.
Expand Down