From 69434ba3700bb247bd0f1bf36a9db669078d914f Mon Sep 17 00:00:00 2001 From: Francesco BV Date: Tue, 27 Oct 2026 17:57:54 +0100 Subject: [PATCH] dxf: mirrored entities keep extrusion -Z; unchanged dimension text survives a save (FF1) Entities read with extrusion (0,0,-1) (LINE, CIRCLE, ARC, ELLIPSE, LWPOLYLINE, INSERT, DIMENSION, LEADER, SOLID, TRACE) are written back in that OCS with their extrusion, the reader's inverse. A dimension still linked to the block it was read with keeps its text position and measurement; a dimension read without a measurement (group 42, kept as 0 by the reader) gets the one of its geometry. The attribute justification reader that was part of this change is now its own pull request (#2313). Co-Authored-By: Claude Opus 5.5 (1M context) --- crates/io/src/dxf_write/ctx.rs | 3 + crates/io/src/dxf_write/entity.rs | 102 +++- crates/io/src/dxf_write/sections.rs | 13 + crates/io/tests/it/dxf_entity_fidelity.rs | 680 ++++++++++++++++++++++ crates/io/tests/it/main.rs | 1 + docs/gaps.md | 5 +- 6 files changed, 784 insertions(+), 20 deletions(-) create mode 100644 crates/io/tests/it/dxf_entity_fidelity.rs diff --git a/crates/io/src/dxf_write/ctx.rs b/crates/io/src/dxf_write/ctx.rs index 11c679a955..3c72d0e9a2 100644 --- a/crates/io/src/dxf_write/ctx.rs +++ b/crates/io/src/dxf_write/ctx.rs @@ -32,6 +32,9 @@ pub(super) struct Ctx { pub(super) paper_views: HashMap, /// Dimension → anonymous `*D` block name. pub(super) dim_blocks: HashMap, + /// Dimensions whose block is written as read (unchanged since read): their text position + /// and measurement are written as read too, not recomputed. + pub(super) dims_as_read: std::collections::HashSet, /// Upper-case text style name → STYLE record handle. pub(super) styles: HashMap, /// Upper-case arrow name (as stored in styles/overrides) → BLOCK_RECORD handle. diff --git a/crates/io/src/dxf_write/entity.rs b/crates/io/src/dxf_write/entity.rs index 6c58cdfacc..d19fdec803 100644 --- a/crates/io/src/dxf_write/entity.rs +++ b/crates/io/src/dxf_write/entity.rs @@ -38,6 +38,7 @@ pub(super) fn entity(w: &mut W, d: &Drawing, e: &Entity, owner: &str, paper: boo thickness(w, e); w.p(10, l.a); w.p(11, l.b); + extrusion(w, mirrored(e)); } EntityKind::Point(p) => { w.s(0, "POINT"); @@ -52,27 +53,42 @@ pub(super) fn entity(w: &mut W, d: &Drawing, e: &Entity, owner: &str, paper: boo w.s(0, "CIRCLE"); common(w, e, owner, paper, "AcDbCircle"); thickness(w, e); - w.p(10, c.center); + let n = mirrored(e); + w.p(10, n.map_or(c.center, |n| to_ocs(n, c.center))); w.f(40, c.radius); + extrusion(w, n); } EntityKind::Arc(a) => { w.s(0, "ARC"); common(w, e, owner, paper, "AcDbCircle"); thickness(w, e); - w.p(10, a.center); + let n = mirrored(e); + w.p(10, n.map_or(a.center, |n| to_ocs(n, a.center))); w.f(40, a.radius); + extrusion(w, n); w.s(100, "AcDbArc"); - w.f(50, a.start.to_degrees()); - w.f(51, a.end.to_degrees()); + // In the mirrored OCS the angles run the other way (the reader's inverse). + let pi = std::f64::consts::PI; + let (start, end) = + if n.is_some() { (cadcraft_geom::norm_angle(pi - a.end), cadcraft_geom::norm_angle(pi - a.start)) } else { (a.start, a.end) }; + w.f(50, start.to_degrees()); + w.f(51, end.to_degrees()); } EntityKind::Ellipse(el) => { w.s(0, "ELLIPSE"); common(w, e, owner, paper, "AcDbEllipse"); w.p(10, el.center); w.p(11, el.major); + let n = mirrored(e); + extrusion(w, n); w.f(40, el.ratio); - w.f(41, el.start); - w.f(42, el.end); + // The minor axis is extrusion × major: mirrored, a parameter t lands where -t does + // in the plan view (the reader's inverse; a full ellipse is the same either way). + let full = (cadcraft_geom::ccw_sweep(el.start, el.end) - std::f64::consts::TAU).abs() <= 1e-9; + let (start, end) = + if n.is_some() && !full { (cadcraft_geom::norm_angle(-el.end), cadcraft_geom::norm_angle(-el.start)) } else { (el.start, el.end) }; + w.f(41, start); + w.f(42, end); } EntityKind::LwPolyline(p) => { w.s(0, "LWPOLYLINE"); @@ -86,16 +102,20 @@ pub(super) fn entity(w: &mut W, d: &Drawing, e: &Entity, owner: &str, paper: boo w.f(38, p.elevation); } thickness(w, e); + // Mirrored, the vertices are in the OCS of (0,0,-1): x and the bulges turn round. + let n = mirrored(e); + let m = if n.is_some() { -1.0 } else { 1.0 }; for v in &p.vertices { - w.p2(10, v.p); + w.p2(10, Vec2::new(m * v.p.x, v.p.y)); if v.start_width != 0.0 || v.end_width != 0.0 { w.f(40, v.start_width); w.f(41, v.end_width); } if v.bulge != 0.0 { - w.f(42, v.bulge); + w.f(42, m * v.bulge); } } + extrusion(w, n); } EntityKind::MLine(m) => { w.s(0, "MLINE"); @@ -262,17 +282,23 @@ pub(super) fn entity(w: &mut W, d: &Drawing, e: &Entity, owner: &str, paper: boo w.i(66, 1); } w.s(2, &i.block); - w.p(10, i.insert); - w.f(41, i.scale.x); + // Read mirrored (extrusion (0,0,-1)), the reference goes back to that OCS: the + // reader's inverse (insertion point in the OCS, x scale, rotation and column spacing + // turned round). + let n = mirrored(e); + let m = if n.is_some() { -1.0 } else { 1.0 }; + w.p(10, n.map_or(i.insert, |n| to_ocs(n, i.insert))); + w.f(41, m * i.scale.x); w.f(42, i.scale.y); w.f(43, i.scale.z); - w.f(50, i.rotation.to_degrees()); + w.f(50, (m * i.rotation).to_degrees()); if i.cols > 1 || i.rows > 1 { w.i(70, i64::from(i.cols)); w.i(71, i64::from(i.rows)); - w.f(44, i.col_spacing); + w.f(44, m * i.col_spacing); w.f(45, i.row_spacing); } + extrusion(w, n); if !i.attribs.is_empty() { for a in &i.attribs { let ah = w.h(); @@ -308,7 +334,13 @@ pub(super) fn entity(w: &mut W, d: &Drawing, e: &Entity, owner: &str, paper: boo w.p(10, dm.defpt); let style = d.dim_style(&dm.style).cloned().unwrap_or_default(); let g = cadcraft_render::dimension_geometry(dm, &style, d.header.f64("DIMSCALE", 1.0)); - w.p(11, if dm.user_text_pos { dm.text_mid } else { g.text_pos.to3(0.0) }); + // Unchanged since read, the text stays where the file had it (CADCraft's own layout + // of the text differs a little from other programs'). + let as_read = cx.dims_as_read.contains(&e.handle); + let mid = if dm.user_text_pos || as_read { dm.text_mid } else { g.text_pos.to3(0.0) }; + // The text midpoint is the one OCS point of a DIMENSION. + let n = mirrored(e); + w.p(11, n.map_or(mid, |n| to_ocs(n, mid))); let ty: i64 = match dm.kind { DimKind::Linear { .. } => 0, DimKind::Aligned => 1, @@ -322,7 +354,11 @@ pub(super) fn entity(w: &mut W, d: &Drawing, e: &Entity, owner: &str, paper: boo }; w.i(70, ty | 32 | if dm.user_text_pos { 128 } else { 0 }); // The actual measurement (read-only in DXF): AutoLISP's entget shows it. - w.f(42, g.measurement); + // The measurement as read: the reader stores a missing group 42 as 0, so 0 is + // written from the geometry instead (the same for an unchanged dimension that did + // measure 0, which the geometry still gives). + let read = Some(dm.measurement).filter(|m| as_read && m.is_finite() && *m != 0.0); + w.f(42, read.unwrap_or(g.measurement)); if !dm.text.is_empty() { w.s(1, &dm.text); } @@ -330,6 +366,7 @@ pub(super) fn entity(w: &mut W, d: &Drawing, e: &Entity, owner: &str, paper: boo if dm.text_rotation != 0.0 { w.f(53, dm.text_rotation.to_degrees()); } + extrusion(w, n); match dm.kind { DimKind::Linear { rotation } => { w.s(100, "AcDbAlignedDimension"); @@ -466,6 +503,7 @@ pub(super) fn entity(w: &mut W, d: &Drawing, e: &Entity, owner: &str, paper: boo for v in &l.vertices { w.p(10, *v); } + extrusion(w, mirrored(e)); } EntityKind::Image(i) => { // Every image has its IMAGEDEF (planned in `write`); the fallback never happens. @@ -478,11 +516,14 @@ pub(super) fn entity(w: &mut W, d: &Drawing, e: &Entity, owner: &str, paper: boo let solid = matches!(e.kind, EntityKind::Solid(_)); w.s(0, if solid { "SOLID" } else { "TRACE" }); common(w, e, owner, paper, "AcDbTrace"); - w.p(10, s.corners[0]); - w.p(11, s.corners[1]); - w.p(12, s.corners[2]); - w.p(13, s.corners[3]); + let n = mirrored(e); + let at = |p: Vec3| n.map_or(p, |n| to_ocs(n, p)); + w.p(10, at(s.corners[0])); + w.p(11, at(s.corners[1])); + w.p(12, at(s.corners[2])); + w.p(13, at(s.corners[3])); thickness(w, e); + extrusion(w, n); } EntityKind::Face3d(f) => { w.s(0, "3DFACE"); @@ -698,6 +739,31 @@ fn gradient(w: &mut W, g: &Gradient) { w.s(1000, g.color2.name()); } +/// The extrusion of an entity read mirrored, (0,0,-1) give or take noise (gap FF1): the reader +/// drew it in the plan view, and the writer puts it back in that OCS with this extrusion (group +/// 210), as it was read. Other extrusions are not written (the geometry is in world coordinates). +fn mirrored(e: &Entity) -> Option { + let n = e.common.extrusion; + (n.is_finite() && n.z < 0.0 && n.x.hypot(n.y) <= 1e-3 * n.z.abs()).then_some(n) +} + +/// A world point in the OCS of extrusion `n` (the inverse of the reader's OCS → WCS). +fn to_ocs(n: Vec3, p: Vec3) -> Vec3 { + let m = cadcraft_geom::Mat4::ocs(n).m; + Vec3::new( + m[0][0] * p.x + m[1][0] * p.y + m[2][0] * p.z, + m[0][1] * p.x + m[1][1] * p.y + m[2][1] * p.z, + m[0][2] * p.x + m[1][2] * p.y + m[2][2] * p.z, + ) +} + +/// Group 210, the extrusion of a mirrored entity (see [`mirrored`]). +fn extrusion(w: &mut W, n: Option) { + if let Some(n) = n { + w.p(210, n); + } +} + /// Group 39 (thickness) of the entities whose DXF Reference record has one; only when set. fn thickness(w: &mut W, e: &Entity) { if e.common.thickness != 0.0 { diff --git a/crates/io/src/dxf_write/sections.rs b/crates/io/src/dxf_write/sections.rs index 7198eeee84..fb043a7fe9 100644 --- a/crates/io/src/dxf_write/sections.rs +++ b/crates/io/src/dxf_write/sections.rs @@ -60,6 +60,7 @@ pub(super) fn tags_for(d: &Drawing, version: Version, target: Version) -> Vec { + cx.dims_as_read.insert(e.handle); ents = b.entities.iter().map(|x| x.as_ref().clone()).collect(); reused_handles.extend(ents.iter().map(|x| x.handle).filter(|h| h.0 != 0)); b.name.clone() @@ -87,6 +88,18 @@ pub(super) fn tags_for(d: &Drawing, version: Version, target: Version) -> Vec = all_spaces .iter() diff --git a/crates/io/tests/it/dxf_entity_fidelity.rs b/crates/io/tests/it/dxf_entity_fidelity.rs new file mode 100644 index 0000000000..476948d18a --- /dev/null +++ b/crates/io/tests/it/dxf_entity_fidelity.rs @@ -0,0 +1,680 @@ +//! What another program's DXF entities keep through open → save → reopen in +//! CADCraft (gap FF1): +//! +//! - the extrusion (0,0,-1) of mirrored entities; +//! - the text position and measurement of dimensions saved unchanged. +//! +//! The fixture is synthetic, written from the public DXF Reference: a drawing as another program +//! might save it. + +use cadcraft_doc::{Drawing, EntityKind, Handle}; +use cadcraft_dxf::{Tag, records, sections}; +use cadcraft_io::read_dxf; + +/// DXF text from lines of `code value` (the value may be empty). +fn dxf(src: &str) -> String { + let mut out = String::new(); + for line in src.lines().map(str::trim).filter(|l| !l.is_empty()) { + let (code, value) = line.split_once(' ').unwrap_or((line, "")); + out.push_str(&format!("{code}\n{value}\n")); + } + out +} + +const FIXTURE: &str = " +0 SECTION +2 HEADER +9 $ACADVER +1 AC1032 +9 $DWGCODEPAGE +3 ANSI_1252 +9 $MEASUREMENT +70 1 +9 $DIMSTYLE +2 Standard +9 $HANDSEED +5 400 +0 ENDSEC +0 SECTION +2 TABLES +0 TABLE +2 LTYPE +5 5 +330 0 +100 AcDbSymbolTable +70 1 +0 LTYPE +5 16 +330 5 +100 AcDbSymbolTableRecord +100 AcDbLinetypeTableRecord +2 Continuous +70 0 +3 Solid line +72 65 +73 0 +40 0 +0 ENDTAB +0 TABLE +2 LAYER +5 2 +330 0 +100 AcDbSymbolTable +70 2 +0 LAYER +5 10 +330 2 +100 AcDbSymbolTableRecord +100 AcDbLayerTableRecord +2 0 +70 0 +62 7 +6 Continuous +0 LAYER +5 1A0 +330 2 +100 AcDbSymbolTableRecord +100 AcDbLayerTableRecord +2 WALLS +70 0 +62 1 +6 Continuous +0 ENDTAB +0 TABLE +2 STYLE +5 3 +330 0 +100 AcDbSymbolTable +70 1 +0 STYLE +5 11 +330 3 +100 AcDbSymbolTableRecord +100 AcDbTextStyleTableRecord +2 Standard +70 0 +40 0 +41 1 +50 0 +71 0 +42 0.2 +3 txt +4 +0 ENDTAB +0 TABLE +2 DIMSTYLE +5 A +330 0 +100 AcDbSymbolTable +70 1 +100 AcDbDimStyleTable +71 0 +0 DIMSTYLE +105 27 +330 A +100 AcDbSymbolTableRecord +100 AcDbDimStyleTableRecord +2 Standard +70 0 +140 0.18 +0 ENDTAB +0 TABLE +2 BLOCK_RECORD +5 1 +330 0 +100 AcDbSymbolTable +70 4 +0 BLOCK_RECORD +5 1F +330 1 +100 AcDbSymbolTableRecord +100 AcDbBlockTableRecord +2 *Model_Space +340 22 +0 BLOCK_RECORD +5 1B +330 1 +100 AcDbSymbolTableRecord +100 AcDbBlockTableRecord +2 *Paper_Space +340 1E +0 BLOCK_RECORD +5 1A3 +330 1 +100 AcDbSymbolTableRecord +100 AcDbBlockTableRecord +2 DOOR +340 0 +70 0 +280 1 +0 BLOCK_RECORD +5 1A4 +330 1 +100 AcDbSymbolTableRecord +100 AcDbBlockTableRecord +2 *D1 +340 0 +0 ENDTAB +0 ENDSEC +0 SECTION +2 BLOCKS +0 BLOCK +5 20 +330 1F +100 AcDbEntity +8 0 +100 AcDbBlockBegin +2 *Model_Space +70 0 +10 0 +20 0 +30 0 +3 *Model_Space +1 +0 ENDBLK +5 21 +330 1F +100 AcDbEntity +8 0 +100 AcDbBlockEnd +0 BLOCK +5 1C +330 1B +100 AcDbEntity +67 1 +8 0 +100 AcDbBlockBegin +2 *Paper_Space +70 0 +10 0 +20 0 +30 0 +3 *Paper_Space +1 +0 ENDBLK +5 1D +330 1B +100 AcDbEntity +67 1 +8 0 +100 AcDbBlockEnd +0 BLOCK +5 1A5 +330 1A3 +100 AcDbEntity +8 0 +100 AcDbBlockBegin +2 DOOR +70 0 +10 0 +20 0 +30 0 +3 DOOR +1 +0 LINE +5 1AA +330 1A3 +100 AcDbEntity +8 0 +100 AcDbLine +10 0 +20 0 +30 0 +11 1 +21 0 +31 0 +0 ENDBLK +5 1A6 +330 1A3 +100 AcDbEntity +8 0 +100 AcDbBlockEnd +0 BLOCK +5 1A7 +330 1A4 +100 AcDbEntity +8 0 +100 AcDbBlockBegin +2 *D1 +70 1 +10 0 +20 0 +30 0 +3 *D1 +1 +0 LINE +5 1A9 +330 1A4 +100 AcDbEntity +8 0 +100 AcDbLine +10 0 +20 5 +30 0 +11 10 +21 5 +31 0 +0 ENDBLK +5 1A8 +330 1A4 +100 AcDbEntity +8 0 +100 AcDbBlockEnd +0 ENDSEC +0 SECTION +2 ENTITIES +0 CIRCLE +5 300 +330 1F +100 AcDbEntity +8 0 +100 AcDbCircle +10 5 +20 0 +30 0 +40 1 +210 0 +220 0 +230 -1 +0 ARC +5 301 +330 1F +100 AcDbEntity +8 0 +100 AcDbCircle +10 5 +20 1 +30 0 +40 2 +210 0 +220 0 +230 -1 +100 AcDbArc +50 10 +51 80 +0 ELLIPSE +5 302 +330 1F +100 AcDbEntity +8 0 +100 AcDbEllipse +10 0 +20 10 +30 0 +11 3 +21 0 +31 0 +210 0 +220 0 +230 -1 +40 0.5 +41 0.5 +42 2 +0 LWPOLYLINE +5 303 +330 1F +100 AcDbEntity +8 0 +100 AcDbPolyline +90 2 +70 0 +10 1 +20 1 +42 0.5 +10 4 +20 1 +210 0 +220 0 +230 -1 +0 LEADER +5 304 +330 1F +100 AcDbEntity +8 0 +100 AcDbLeader +3 Standard +71 1 +72 0 +76 2 +10 0 +20 0 +30 0 +10 2 +20 2 +30 0 +210 0 +220 0 +230 -1 +0 SOLID +5 305 +330 1F +100 AcDbEntity +8 0 +100 AcDbTrace +10 1 +20 1 +30 0 +11 2 +21 1 +31 0 +12 1 +22 2 +32 0 +13 2 +23 2 +33 0 +210 0 +220 0 +230 -1 +0 INSERT +5 306 +330 1F +100 AcDbEntity +8 0 +100 AcDbBlockReference +2 DOOR +10 3 +20 4 +30 0 +41 1 +42 2 +43 1 +50 30 +210 0 +220 0 +230 -1 +0 INSERT +5 307 +330 1F +100 AcDbEntity +8 WALLS +100 AcDbBlockReference +66 1 +2 DOOR +10 20 +20 20 +30 0 +0 ATTRIB +5 308 +330 307 +100 AcDbEntity +8 WALLS +100 AcDbText +10 20 +20 21 +30 0 +40 0.25 +1 first +7 Standard +100 AcDbAttribute +280 0 +2 TAG1 +70 0 +280 0 +71 2 +72 1 +11 20 +21 21 +31 0 +101 Embedded Object +10 20 +20 21 +30 0 +40 0.25 +41 0 +46 0 +71 1 +72 5 +1 first +7 Standard +73 1 +44 1 +0 ATTRIB +5 309 +330 307 +100 AcDbEntity +8 WALLS +100 AcDbText +10 20 +20 22.375 +30 0 +40 0.25 +1 second +7 Standard +72 0 +11 20 +21 22.5 +31 0 +100 AcDbAttribute +2 TAG2 +70 0 +73 0 +74 2 +0 SEQEND +5 30A +330 307 +100 AcDbEntity +8 WALLS +0 DIMENSION +5 30B +330 1F +100 AcDbEntity +8 0 +100 AcDbDimension +2 *D1 +10 10 +20 5 +30 0 +11 5 +21 7.3 +31 0 +70 32 +1 +3 Standard +42 10 +100 AcDbAlignedDimension +13 0 +23 0 +33 0 +14 10 +24 0 +34 0 +50 0 +100 AcDbRotatedDimension +0 ENDSEC +0 SECTION +2 OBJECTS +0 DICTIONARY +5 C +330 0 +100 AcDbDictionary +281 1 +3 ACAD_LAYOUT +350 1A +0 DICTIONARY +5 1A +330 C +100 AcDbDictionary +281 1 +3 Layout1 +350 1E +3 Model +350 22 +0 LAYOUT +5 22 +330 1A +100 AcDbPlotSettings +1 +2 none_device +4 ISO_A4_(210.00_x_297.00_MM) +6 +40 0 +41 0 +42 0 +43 0 +44 210 +45 297 +70 688 +72 1 +73 0 +74 1 +100 AcDbLayout +1 Model +70 1 +71 0 +330 1F +0 LAYOUT +5 1E +330 1A +100 AcDbPlotSettings +1 +2 none_device +4 ISO_A4_(210.00_x_297.00_MM) +6 +40 0 +41 0 +42 0 +43 0 +44 210 +45 297 +70 688 +72 0 +73 0 +74 5 +100 AcDbLayout +1 Layout1 +70 1 +71 1 +330 1B +0 ENDSEC +0 EOF +"; + +fn fixture() -> String { + dxf(FIXTURE) +} + +/// Saved as a DXF of the drawing's own version (2018 for the fixture). +fn save(d: &Drawing) -> String { + String::from_utf8(cadcraft_io::write(d, "x.dxf").expect("save")).expect("text") +} + +/// Every record of a DXF text with its section: (section, type, groups). +fn recs(text: &str) -> Vec<(String, String, Vec)> { + let tags = cadcraft_dxf::parse(text.as_bytes()).expect("parse"); + sections(&tags).into_iter().flat_map(|s| records(&s.tags).into_iter().map(move |(k, t)| (s.name.clone(), k, t))).collect() +} + +fn val(t: &[Tag], code: i32) -> Option { + t.iter().find(|x| x.code == code).map(|x| x.str().trim().to_string()) +} + +fn handle_of(t: &[Tag]) -> Option { + t.iter().find(|x| x.code == 5 || x.code == 105).map(|x| x.str().trim().to_ascii_uppercase()) +} + +/// The groups of the record with handle `h` (entities keep their handles on save). +fn record(text: &str, h: &str) -> Vec { + recs(text).into_iter().find(|(s, _, t)| s != "HEADER" && handle_of(t).as_deref() == Some(h)).map(|(_, _, t)| t).unwrap_or_default() +} + +/// The entities of model space by handle, with their geometry as JSON (the reopened drawing is +/// compared with the one read). +fn geometry(d: &Drawing) -> Vec<(Handle, serde_json::Value, cadcraft_geom::Vec3)> { + d.model.iter().map(|e| (e.handle, serde_json::to_value(&e.kind).unwrap_or_default(), e.common.extrusion)).collect() +} + +/// Two geometry lists agree to 1e-9 in every number. +fn same(a: &serde_json::Value, b: &serde_json::Value) -> bool { + use serde_json::Value as V; + match (a, b) { + (V::Number(x), V::Number(y)) => (x.as_f64().unwrap_or(f64::NAN) - y.as_f64().unwrap_or(f64::NAN)).abs() < 1e-9, + (V::Array(x), V::Array(y)) => x.len() == y.len() && x.iter().zip(y).all(|(x, y)| same(x, y)), + (V::Object(x), V::Object(y)) => x.len() == y.len() && x.iter().all(|(k, v)| y.get(k).is_some_and(|w| same(v, w))), + _ => a == b, + } +} + +/// Entities read with extrusion (0,0,-1) are written back in that OCS with their extrusion: the +/// reader turned them into the plan view, the writer turns them back (ellipses in blocks and a +/// LEADER came back flipped to +Z). +#[test] +fn mirrored_entities_keep_their_extrusion() { + let d = read_dxf(fixture().as_bytes()).expect("read"); + // Read into the plan view: the circle's centre is the OCS point mirrored. + let circle = d.model.get(Handle(0x300)).expect("circle"); + let EntityKind::Circle(c) = &circle.kind else { panic!("not a circle") }; + assert!((c.center.x + 5.0).abs() < 1e-9 && c.center.y.abs() < 1e-9); + let out = save(&d); + for h in ["300", "301", "302", "303", "304", "305", "306"] { + let r = record(&out, h); + assert_eq!(val(&r, 230).and_then(|z| z.parse::().ok()), Some(-1.0), "{h}"); + } + // As read: the circle's OCS centre, the arc's angles, the ellipse's parameters, the + // polyline's vertices and bulge, the solid's corner, the reference's point, scale and + // rotation. + let num = |h: &str, code: i32| val(&record(&out, h), code).and_then(|v| v.parse::().ok()).unwrap_or(f64::NAN); + let near = |a: f64, b: f64| (a - b).abs() < 1e-9; + assert!(near(num("300", 10), 5.0)); + assert!(near(num("301", 50), 10.0) && near(num("301", 51), 80.0)); + assert!(near(num("302", 41), 0.5) && near(num("302", 42), 2.0)); + assert!(near(num("303", 10), 1.0) && near(num("303", 42), 0.5)); + assert!(near(num("305", 10), 1.0)); + assert!(near(num("306", 10), 3.0) && near(num("306", 41), 1.0) && near(num("306", 50), 30.0)); + // Reopened, the geometry and the extrusions of the mirrored entities are as read (the block + // reference after them carries attributes, whose justification is read in #2313). + let back = read_dxf(out.as_bytes()).expect("reopen"); + let (before, after) = (geometry(&d), geometry(&back)); + for (h, g, n) in before.iter().filter(|(h, _, _)| (0x300..=0x306).contains(&h.0)) { + let Some((_, g2, n2)) = after.iter().find(|(h2, _, _)| h2 == h) else { panic!("{h:?} lost") }; + assert!(same(g, g2), "{h:?}: {g} vs {g2}"); + assert_eq!(n, n2, "{h:?}"); + } +} + +/// An entity drawn in CADCraft (extrusion +Z) writes no extrusion, as before. +#[test] +fn plan_view_entities_write_no_extrusion() { + let d = read_dxf(fixture().as_bytes()).expect("read"); + let out = save(&d); + assert_eq!(val(&record(&out, "307"), 230), None); +} + +/// A dimension saved unchanged keeps its text position and measurement as read; once edited +/// (the link to the block it was read with dropped) they are CADCraft's again (dimension text +/// midpoints moved on every save). +#[test] +fn unchanged_dimensions_keep_their_text_position() { + let mut d = read_dxf(fixture().as_bytes()).expect("read"); + let out = save(&d); + let r = record(&out, "30B"); + let num = |r: &[Tag], code: i32| val(r, code).and_then(|v| v.parse::().ok()).unwrap_or(f64::NAN); + assert_eq!((num(&r, 11), num(&r, 21)), (5.0, 7.3)); + assert_eq!(num(&r, 42), 10.0); + let back = read_dxf(out.as_bytes()).expect("reopen"); + let EntityKind::Dimension(dm) = &back.model.get(Handle(0x30B)).expect("dimension").kind else { panic!("not a dimension") }; + assert!((dm.text_mid.x - 5.0).abs() < 1e-9 && (dm.text_mid.y - 7.3).abs() < 1e-9); + // Edited: the dimension is drawn (and saved) by CADCraft. + d.modify_entity(Handle(0x30B), |e| { + if let EntityKind::Dimension(dm) = &mut e.kind { + dm.block = None; + } + }) + .expect("edit"); + let r = record(&save(&d), "30B"); + assert_ne!((num(&r, 11), num(&r, 21)), (5.0, 7.3)); +} + +/// A dimension read without a measurement (no group 42) and saved unchanged gets the measurement +/// of its geometry, not 0: the reader keeps a missing 42 as 0. +#[test] +fn dimension_without_group_42_writes_the_measured_value() { + let with = "42\n10\n100\nAcDbAlignedDimension\n"; + assert!(fixture().contains(with)); + let src = fixture().replacen(with, "100\nAcDbAlignedDimension\n", 1); + let d = read_dxf(src.as_bytes()).expect("read"); + let r = record(&save(&d), "30B"); + let num = |code: i32| val(&r, code).and_then(|v| v.parse::().ok()).unwrap_or(f64::NAN); + assert_eq!(num(42), 10.0, "the aligned dimension measures 10"); + assert_eq!((num(11), num(21)), (5.0, 7.3), "still saved unchanged"); +} diff --git a/crates/io/tests/it/main.rs b/crates/io/tests/it/main.rs index 51e7f1965e..2a467ace73 100644 --- a/crates/io/tests/it/main.rs +++ b/crates/io/tests/it/main.rs @@ -21,6 +21,7 @@ mod dwg_layout_owner; #[cfg(not(target_arch = "wasm32"))] mod dwg_roundtrip_losses; mod dwt_template; +mod dxf_entity_fidelity; mod dxf_versions; mod dynblock_read; mod entity_props_roundtrip; diff --git a/docs/gaps.md b/docs/gaps.md index a4a12cff8b..4c751ce796 100644 --- a/docs/gaps.md +++ b/docs/gaps.md @@ -1,6 +1,6 @@ # Where CADCraft falls short of AutoCAD -> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-25 · **Change:** minor (DIMTFILL limits in the `*D` block, EXPLODE and DXF: F7 updated) · **Target:** Autodesk AutoCAD 2027 +> **Last reviewed:** 2026-10-10 · **Last updated:** 2026-10-27 · **Change:** minor (DXF: mirrored extrusions and unchanged dimension text kept: FF1 narrowed) · **Target:** Autodesk AutoCAD 2027 Every known shortfall, one row each, ranked within each section by user impact. This is the work list: agents pick from here (top of a section first), and remove or update a row in the same PR @@ -73,7 +73,7 @@ Detail: [file-format-parity.md](file-format-parity.md). | # | Gap | User impact | Est. | |---|---|---|---| -| FF1 | **[alpha blocker]** Round-trip what we don't model. DXF done (#528): unknown entities and objects (with their CLASS records), foreign xdata (hyperlinks included), extension dictionaries, persistent reactors, DICTIONARYVAR and XRECORDs, layer states, colour book colours, references remapped on save; IMAGE, VIEW/UCS, groups, block descriptions, true colours, transparency (#201, #204, #208, #209, #213, #223); WIPEOUT and gradients (#147, #162). Left: through DWG, acadrust drops custom-class entities and objects, xdata on table records and blocks, entity colour book names and reactors; UCS elevation; MTEXT background colours; unknown entities follow only layer edits, and renamed layers and blocks lose their foreign data | High | 6–10 | +| FF1 | **[alpha blocker]** Round-trip what we don't model. DXF done (#528): unknown entities and objects (with their CLASS records), foreign xdata (hyperlinks included), extension dictionaries, persistent reactors, DICTIONARYVAR and XRECORDs, layer states, colour book colours, references remapped on save; IMAGE, VIEW/UCS, groups, block descriptions, true colours, transparency (#201, #204, #208, #209, #213, #223); WIPEOUT and gradients (#147, #162); entities read mirrored (extrusion 0,0,-1) keep their OCS and extrusion, and unchanged dimensions their text position and measurement (2026-10-27). Left: TEXT, MTEXT and HATCH read under extrusion -Z are written back in the plan view; through DWG, acadrust drops custom-class entities and objects, xdata on table records and blocks, entity colour book names and reactors; UCS elevation; MTEXT background colours; unknown entities follow only layer edits, and renamed layers and blocks lose their foreign data | High | 6–10 | | FF2 | Multileader leftovers (native MULTILEADER read/write landed, #459): several landings per multileader (extra leader roots are folded into the lines), per-object overrides (leader colour and linetype, arrowhead symbol, text frame), annotative scale contexts; check the text placement of our MULTILEADER output in AutoCAD | Medium | 3–5 | | FF3 | Save-version leftovers (DXF/DWG 2000–2018, binary DXF, SAVEAS `version`/`binary` and the default save version landed, #421): no R14 DWG or R12 DXF output; the Save dialog can't pick the version per file (the file picker doesn't report the chosen type; File > Default Save Format sets it); no command-line SAVEAS format prompt | Low | 2–4 | | FF4 | Code-page leftovers (pre-2007 DXF read in their `$DWGCODEPAGE` landed, #505): Johab (ANSI_1361) and DOS 855/857/860–865/869 read as ANSI_1252; saving declares `ANSI_1252` instead of the file's code page (the text is pure ASCII, so still correct); acadrust approximates DOS code pages in pre-2007 DWG (DOS437 as 866, DOS850 as 1252) | Low | 1–2 | @@ -150,6 +150,7 @@ Detail: [localization-parity.md](localization-parity.md). | Date | Change | Summary | |---|---|---| +| 2026-10-27 | minor | DXF entities: LINE, CIRCLE, ARC, ELLIPSE, LWPOLYLINE, INSERT, DIMENSION, LEADER, SOLID and TRACE read with extrusion (0,0,-1) are written back in that OCS with their extrusion; dimensions saved unchanged keep their text position and measurement: FF1 narrowed | | 2026-10-25 | minor | DIMTFILL limits: with DIMTFILL 1 (the drawing's background color) the written `*D` block and EXPLODE keep no background mask (a WIPEOUT over the text box would), and a true-color DIMTFILLCLR is written to DXF as the nearest ACI: F7 updated | | 2026-10-24 | minor | COMPAREEXPORT (typed, or Export on the comparison bar) opens the comparison as a new untitled drawing, `compare_ vs .dwg`: this drawing's objects in one group and the other drawing's differing objects in a second group, in the comparison colors (green, red, gray for objects in both), block contents and style colors included: F22 narrowed | | 2026-10-24 | minor | DIMTFILL and DIMTFILLCLR: a dimension's text background (none, the drawing's background color, or a color) hides what is drawn under the text; SETVAR, DIMOVERRIDE and style fields, DIMSTYLE groups 69 and 70 in DXF from R2007 (CADCraft xdata before), and a SOLID in the `*D` block and from EXPLODE for DIMTFILL 2: F7 narrowed |