acadrust 0.6.7

A pure Rust library for reading and writing CAD files in DXF format (ASCII and Binary) and DWG format (Binary).
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
//! Semantic round-trip validation of the annotative
//! `AcDb*ObjectContextData` encoder.
//!
//! Strategy: the golden R2018 drawing `0718-mbmdmc.dwg` carries 134 real
//! `AcDbBlkRefObjectContextData` leaves (class 533). The reader models each
//! into an `ObjectContextData`.
//!
//! - `baseline_roundtrip_preserves_all_contexts` proves modeling did not
//!   regress the round-trip: a plain save/reload preserves every modeled leaf.
//! - `encoder_preserves_context_fields_and_scale_link` verifies that the field
//!   encoder preserves both the modeled payload and its annotation-scale link.
//!
//! The golden drawing is not in the repository, so those two tests are
//! ignored. To run them, point `ACADRUST_GOLDEN_0718` at the file:
//!
//! ```text
//! ACADRUST_GOLDEN_0718=/path/to/0718-mbmdmc.dwg cargo test --test annotative_context_encoder -- --ignored
//! ```

use std::io::Cursor;
use std::path::PathBuf;

use acadrust::entities::{
    BoundaryEdge, BoundaryPath, BoundaryPathFlags, HatchPatternLine, LineEdge,
};
use acadrust::objects::{
    DimContext, DimSubtype, HatchLoopContext, HatchScaleContext, HatchViewContext, MTextColumns,
    MTextContext, ObjectContextData, ObjectContextKind, ObjectType, Scale,
};
use acadrust::types::{DxfVersion, Vector2, Vector3};
use acadrust::{CadDocument, DwgReader, DwgWriter, Handle};

/// Environment variable holding the path of the golden drawing.
const GOLDEN_ENV: &str = "ACADRUST_GOLDEN_0718";

fn load_golden() -> CadDocument {
    let path = std::env::var_os(GOLDEN_ENV)
        .map(PathBuf::from)
        .unwrap_or_else(|| panic!("set {GOLDEN_ENV} to the path of 0718-mbmdmc.dwg"));
    let mut reader = DwgReader::from_file(&path)
        .unwrap_or_else(|e| panic!("{GOLDEN_ENV}={}: {e}", path.display()));
    reader.read().expect("read golden")
}

fn roundtrip(doc: &CadDocument) -> CadDocument {
    let bytes = DwgWriter::write_to_vec(doc).expect("dwg write");
    let mut r = DwgReader::from_stream(Cursor::new(bytes));
    r.read().expect("dwg re-read")
}

#[test]
#[ignore = "needs 0718-mbmdmc.dwg; set ACADRUST_GOLDEN_0718 to its path"]
fn baseline_roundtrip_preserves_all_contexts() {
    let doc = load_golden();

    let ctx0 = doc.context_scales.len();
    let blk0 = blkref_fields(&doc);
    eprintln!(
        "loaded: context_scales={ctx0} modeled_blkref={}",
        blk0.len()
    );
    assert!(blk0.len() >= 134, "expected the 134 BLKREF contexts");

    let rt = roundtrip(&doc);
    let ctx1 = rt.context_scales.len();
    let blk1 = blkref_fields(&rt);

    assert_eq!(ctx0, ctx1, "context_scales count changed on round-trip");
    assert_eq!(blk0, blk1, "modeled BLKREF fields changed on round-trip");
}

/// A BLKREF leaf's decoded fields + the annotation scale it resolves to (the
/// interop-critical link), keyed by handle.
#[derive(Debug, Clone, PartialEq)]
struct BlkFields {
    class_version: i16,
    is_default: bool,
    scale_target: Handle,
    rotation: f64,
    insertion: [f64; 3],
    scale_factor: [f64; 3],
}

fn blkref_fields(doc: &CadDocument) -> std::collections::BTreeMap<Handle, BlkFields> {
    doc.objects
        .iter()
        .filter_map(|(h, o)| match o {
            ObjectType::ObjectContextData(c) => match &c.kind {
                ObjectContextKind::BlkRef {
                    rotation,
                    insertion,
                    scale_factor,
                } => Some((
                    *h,
                    BlkFields {
                        class_version: c.class_version,
                        is_default: c.is_default,
                        scale_target: c.scale,
                        rotation: *rotation,
                        insertion: [insertion.x, insertion.y, insertion.z],
                        scale_factor: [scale_factor.x, scale_factor.y, scale_factor.z],
                    },
                )),
                _ => None,
            },
            _ => None,
        })
        .collect()
}

#[test]
#[ignore = "needs 0718-mbmdmc.dwg; set ACADRUST_GOLDEN_0718 to its path"]
fn encoder_preserves_context_fields_and_scale_link() {
    let doc = load_golden();

    // Ground truth: the fields AutoCAD stored + which SCALE each leaf points at.
    let original = blkref_fields(&doc);
    assert!(original.len() >= 134, "expected the 134 BLKREF contexts");

    // Recognised context objects are always encoded from their modeled fields.
    let rt = roundtrip(&doc);
    let produced = blkref_fields(&rt);

    assert_eq!(
        original.len(),
        produced.len(),
        "leaf count changed after encode"
    );

    let mut mism = 0usize;
    for (h, orig) in &original {
        match produced.get(h) {
            None => {
                eprintln!("leaf {h:?} vanished");
                mism += 1;
            }
            Some(got) if got != orig => {
                if mism < 3 {
                    eprintln!("leaf {h:?} FIELD MISMATCH:\n  orig {orig:?}\n  got  {got:?}");
                }
                mism += 1;
            }
            _ => {}
        }
    }
    assert_eq!(
        mism, 0,
        "{mism} BLKREF leaves lost fields/scale-link after field-encoding"
    );

    // The re-encode must be a stable semantic fixed point.
    let twice = blkref_fields(&roundtrip(&rt));
    assert_eq!(produced, twice, "field encoder is not semantically stable");
}

// ── Synthesis path (what OCS uses to CREATE contexts from scratch) ──────────

/// Build a fresh R2018 document carrying one annotative context leaf of `kind`
/// that references a synthesized SCALE, round-trip it, and return the reloaded
/// leaf's decoded fields (scale target + kind). Exercises the whole create
/// path: class registration → object insertion → DWG write → read-back.
fn synth_roundtrip_leaf(
    class_name: &str,
    kind: ObjectContextKind,
    has_file_to_extension_dictionary: bool,
) -> (Handle, ObjectContextKind, i16, bool, bool) {
    let mut doc = CadDocument::with_version(DxfVersion::AC1032);
    doc.register_object_context_class(class_name);

    let scale_h = doc.allocate_handle();
    let mut scale = Scale::new("1:50", 1.0, 50.0);
    scale.handle = scale_h;
    doc.objects.insert(scale_h, ObjectType::Scale(scale));

    let owner_h = doc.allocate_handle();
    let leaf_h = doc.allocate_handle();
    let ctx = ObjectContextData {
        handle: leaf_h,
        owner_handle: owner_h,
        reactors: vec![owner_h],
        xdictionary_handle: None,
        class_version: 3,
        has_file_to_extension_dictionary,
        uses_legacy_mleader_context_base: false,
        is_default: true,
        scale: scale_h,
        kind,
    };
    doc.objects
        .insert(leaf_h, ObjectType::ObjectContextData(ctx));

    let rt = roundtrip(&doc);
    let (h, c) = rt
        .objects
        .iter()
        .find_map(|(h, o)| match o {
            ObjectType::ObjectContextData(c) => Some((*h, c.clone())),
            _ => None,
        })
        .expect("context leaf survived round-trip");
    // The scale link must resolve back to a SCALE object in the reloaded doc.
    assert!(
        matches!(rt.objects.get(&c.scale), Some(ObjectType::Scale(_))),
        "leaf scale handle {:?} must resolve to a SCALE",
        c.scale
    );
    assert!(
        rt.context_scales.contains_key(&h),
        "leaf must be registered in context_scales"
    );
    (
        h,
        c.kind,
        c.class_version,
        c.has_file_to_extension_dictionary,
        c.is_default,
    )
}

#[test]
fn synth_blkref_context_roundtrips() {
    let kind = ObjectContextKind::BlkRef {
        rotation: 0.75,
        insertion: Vector3::new(123.5, -42.25, 7.0),
        scale_factor: Vector3::new(2.0, 3.0, 4.0),
    };
    let (_h, got, cv, has_file_dictionary, def) =
        synth_roundtrip_leaf("ACDB_BLKREFOBJECTCONTEXTDATA_CLASS", kind.clone(), true);
    assert_eq!(cv, 3);
    assert!(has_file_dictionary);
    assert!(def);
    assert_eq!(
        got, kind,
        "BLKREF fields did not survive synthesis round-trip"
    );
}

#[test]
fn r2010_context_file_dictionary_flag_survives_roundtrip() {
    let kind = ObjectContextKind::AnnotScale;
    let (_h, got, _cv, has_file_dictionary, _def) = synth_roundtrip_leaf(
        "ACDB_ANNOTSCALEOBJECTCONTEXTDATA_CLASS",
        kind.clone(),
        false,
    );
    assert_eq!(got, kind);
    assert!(!has_file_dictionary);
}

#[test]
fn synth_text_context_roundtrips() {
    let kind = ObjectContextKind::Text {
        horizontal_mode: 1,
        rotation: 0.5,
        insertion: Vector2::new(10.0, 20.0),
        alignment: Vector2::new(-3.0, 4.0),
    };
    let (_h, got, cv, has_file_dictionary, def) =
        synth_roundtrip_leaf("ACDB_TEXTOBJECTCONTEXTDATA_CLASS", kind.clone(), true);
    assert_eq!(cv, 3);
    assert!(has_file_dictionary);
    assert!(def);
    assert_eq!(
        got, kind,
        "TEXT fields did not survive synthesis round-trip"
    );
}

#[test]
fn synth_mtext_context_roundtrips() {
    // Non-columned MTEXT (the common single-representation case).
    let kind = ObjectContextKind::MText(MTextContext {
        attachment: 2,
        x_axis_dir: Vector3::new(1.0, 0.0, 0.0),
        insertion: Vector3::new(7306.97, 12427.8, 0.0),
        rect_width: 39.26,
        rect_height: 17.6,
        extents_width: 1.146,
        extents_height: 0.0975,
        column_type: 0,
        columns: None,
    });
    let (_h, got, _cv, _has_file_dictionary, _def) =
        synth_roundtrip_leaf("ACDB_MTEXTOBJECTCONTEXTDATA_CLASS", kind.clone(), true);
    assert_eq!(
        got, kind,
        "MTEXT (no columns) fields did not survive round-trip"
    );
}

#[test]
fn synth_mtext_columns_roundtrips() {
    // Dynamic columns with explicit heights — exercises the column branch.
    let kind = ObjectContextKind::MText(MTextContext {
        attachment: 1,
        x_axis_dir: Vector3::new(0.7071, 0.7071, 0.0),
        insertion: Vector3::new(1.0, 2.0, 3.0),
        rect_width: 100.0,
        rect_height: 50.0,
        extents_width: 90.0,
        extents_height: 45.0,
        column_type: 2,
        columns: Some(MTextColumns {
            num_heights: 3,
            width: 30.0,
            gutter: 5.0,
            auto_height: false,
            flow_reversed: true,
            heights: vec![10.0, 20.0, 15.0],
        }),
    });
    let (_h, got, _cv, _has_file_dictionary, _def) =
        synth_roundtrip_leaf("ACDB_MTEXTOBJECTCONTEXTDATA_CLASS", kind.clone(), true);
    assert_eq!(
        got, kind,
        "MTEXT (columns) fields did not survive round-trip"
    );
}

#[test]
fn synth_hatch_view_context_roundtrips_inline_boundaries() {
    let mut boundary = BoundaryPath::with_flags(BoundaryPathFlags::from_bits(1560));
    boundary.add_edge(BoundaryEdge::Line(LineEdge {
        start: Vector2::new(1.0, 2.0),
        end: Vector2::new(3.0, 4.0),
    }));
    let kind = ObjectContextKind::HatchView(HatchViewContext {
        hatch: HatchScaleContext {
            pattern_lines: vec![HatchPatternLine {
                angle: 0.5,
                base_point: Vector2::new(5.0, 6.0),
                offset: Vector2::new(0.25, 0.5),
                dash_lengths: vec![1.0, -0.5],
            }],
            pattern_scale: 2.0,
            pattern_base: Vector3::new(7.0, 8.0, 9.0),
            loops: vec![
                HatchLoopContext {
                    loop_type: 7,
                    supports_context: true,
                    boundary: None,
                },
                HatchLoopContext {
                    loop_type: 1560,
                    supports_context: false,
                    boundary: Some(boundary),
                },
            ],
        },
        view: Handle::from(0x45u64),
        view_normal: Vector3::new(0.0, 0.0, 12.5),
        view_rotation: 0.75,
        evaluate_hatch: true,
    });
    let (_h, got, _cv, _has_file_dictionary, _def) =
        synth_roundtrip_leaf("ACDB_HATCHVIEWCONTEXTDATA_CLASS", kind.clone(), true);
    assert_eq!(got, kind, "hatch-view fields did not survive round-trip");
}

fn dim_ctx(subtype: DimSubtype) -> DimContext {
    DimContext {
        def_pt: Vector2::new(11.5, 22.5),
        is_def_textloc: true,
        text_rotation: 0.25,
        block: Handle::from(0x2Au64),
        b293: false,
        dimtofl: true,
        dimosxd: false,
        dimatfit: true,
        dimtix: false,
        dimtmove: true,
        override_code: 3,
        has_arrow2: true,
        flip_arrow2: false,
        flip_arrow1: true,
        subtype,
    }
}

#[test]
fn synth_aligned_dim_context_roundtrips() {
    let kind = ObjectContextKind::Dim(dim_ctx(DimSubtype::Aligned {
        dimline_pt: Vector3::new(1.0, 2.0, 3.0),
    }));
    let (_h, got, _cv, _has_file_dictionary, _def) =
        synth_roundtrip_leaf("ACDB_ALDIMOBJECTCONTEXTDATA_CLASS", kind.clone(), true);
    assert_eq!(got, kind, "aligned-dim fields did not survive round-trip");
    // The dimension's block hard-pointer must survive too.
    if let ObjectContextKind::Dim(d) = &got {
        assert_eq!(d.block, Handle::from(0x2Au64), "dim block handle lost");
    }
}

#[test]
fn synth_ordinate_dim_context_roundtrips() {
    // Two-point subtype — exercises the multi-3BD path.
    let kind = ObjectContextKind::Dim(dim_ctx(DimSubtype::Ordinate {
        feature_location_pt: Vector3::new(4.0, 5.0, 6.0),
        leader_endpt: Vector3::new(7.0, 8.0, 9.0),
    }));
    let (_h, got, _cv, _has_file_dictionary, _def) =
        synth_roundtrip_leaf("ACDB_ORDDIMOBJECTCONTEXTDATA_CLASS", kind.clone(), true);
    assert_eq!(got, kind, "ordinate-dim fields did not survive round-trip");
}