Keep ATTDEF prompt when saving DXF (#81) (#137)

This commit is contained in:
MarekSpodBiedry 2026-10-10 18:14:06 +02:00 committed by GitHub
parent df1c074fff
commit 56c39f1c17
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 36 additions and 7 deletions

View File

@ -286,7 +286,7 @@ fn common_x(w: &mut W, e: &Entity, owner: &str, paper: bool, subclass: &str, ass
}
}
fn text_tags(w: &mut W, t: &Text, attrib_tag: Option<(&str, bool)>, attdef: bool) {
fn text_tags(w: &mut W, t: &Text, attrib_tag: Option<(&str, bool)>, attdef_prompt: Option<&str>) {
w.p(10, t.insert);
w.f(40, t.height);
w.s(1, &t.value);
@ -322,9 +322,9 @@ fn text_tags(w: &mut W, t: &Text, attrib_tag: Option<(&str, bool)>, attdef: bool
}
match attrib_tag {
Some((tag, invisible)) => {
w.s(100, if attdef { "AcDbAttributeDefinition" } else { "AcDbAttribute" });
if attdef {
w.s(3, "");
w.s(100, if attdef_prompt.is_some() { "AcDbAttributeDefinition" } else { "AcDbAttribute" });
if let Some(prompt) = attdef_prompt {
w.s(3, prompt);
}
w.s(2, tag);
w.i(70, i64::from(invisible));
@ -454,12 +454,12 @@ fn entity(w: &mut W, d: &Drawing, e: &Entity, owner: &str, paper: bool, cx: &Ctx
EntityKind::Text(t) => {
w.s(0, "TEXT");
common(w, e, owner, paper, "AcDbText");
text_tags(w, t, None, false);
text_tags(w, t, None, None);
}
EntityKind::AttDef(a) => {
w.s(0, "ATTDEF");
common(w, e, owner, paper, "AcDbText");
text_tags(w, &a.text, Some((&a.tag, a.invisible)), true);
text_tags(w, &a.text, Some((&a.tag, a.invisible)), Some(&a.prompt));
}
EntityKind::MText(t) => {
w.s(0, "MTEXT");
@ -512,7 +512,7 @@ fn entity(w: &mut W, d: &Drawing, e: &Entity, owner: &str, paper: bool, cx: &Ctx
w.s(100, "AcDbEntity");
w.s(8, &e.common.layer);
w.s(100, "AcDbText");
text_tags(w, &a.text, Some((&a.tag, a.invisible)), false);
text_tags(w, &a.text, Some((&a.tag, a.invisible)), None);
}
let sh = w.h();
w.s(0, "SEQEND");

View File

@ -865,3 +865,32 @@ fn mleader_is_written_as_leader_and_mtext() {
assert!(back.model.iter().any(|e| matches!(e.kind, EntityKind::Leader(_))));
assert!(back.model.iter().any(|e| matches!(&e.kind, EntityKind::MText(t) if t.contents == "Note")));
}
#[test]
fn attdef_prompt_survives_dxf_roundtrip() {
let mut d = Drawing::new_metric();
let text = Text {
insert: Vec3::ZERO,
align_pt: None,
height: 2.0,
value: "X".into(),
rotation: 0.0,
width_factor: 1.0,
oblique: 0.0,
style: "Standard".into(),
halign: HAlign::Left,
valign: VAlign::Baseline,
};
let attdef = |prompt: &str| {
EntityKind::AttDef(Attrib { tag: "TAG1".into(), text: text.clone(), invisible: false, constant: false, prompt: prompt.into() })
};
d.add(&Space::Model, Default::default(), attdef("Enter value")).unwrap();
d.add(&Space::Model, Default::default(), attdef("")).unwrap();
let back = roundtrip(&d);
let prompts: Vec<&str> = back
.model
.iter()
.filter_map(|e| if let EntityKind::AttDef(a) = &e.kind { Some(a.prompt.as_str()) } else { None })
.collect();
assert_eq!(prompts, vec!["Enter value", ""]);
}