dxf 0.6.1

A rust crate for reading and writing DXF and DXB CAD files.
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
use crate::entities::*;
use crate::enums::*;
use crate::helper_functions::tests::*;
use crate::*;

use std::io::{BufReader, Cursor, Seek, SeekFrom};
use std::str::from_utf8;

use image::{DynamicImage, GenericImageView};

#[test]
fn read_string_with_control_characters() {
    let drawing = parse_drawing(
        [
            "0",
            "SECTION",
            "2",
            "HEADER",
            "9",
            "$LASTSAVEDBY",
            "1",
            "a^G^ ^^ b",
            "0",
            "ENDSEC",
            "0",
            "EOF",
        ]
        .join("\n")
        .as_str(),
    );
    assert_eq!("a\u{7}^\u{1E} b", drawing.header.last_saved_by);
}

#[test]
fn write_string_with_control_characters() {
    let mut drawing = Drawing::new();
    drawing.header.version = AcadVersion::R2004;
    drawing.header.last_saved_by = String::from("a\u{7}^\u{1E} b");
    assert_contains(&drawing, String::from("a^G^ ^^ b"));
}

#[test]
fn totally_empty_file() {
    let _file = parse_drawing("");
}

#[test]
fn empty_file_trailing_newline() {
    let _file = parse_drawing("0\nEOF\n");
}

#[test]
fn empty_file_no_trailing_newline() {
    let _file = parse_drawing("0\nEOF");
}

#[test]
fn unsupported_section() {
    let _file = from_section(
        "UNSUPPORTED_SECTION",
        vec![
            CodePair::new_str(1, "garbage value 1"),
            CodePair::new_str(1, "garbage value 2"),
        ],
    );
}

#[test]
fn read_lf_and_crlf() {
    let code_pairs = [
        "0", "SECTION", "2", "HEADER", "9", "$ACADVER", "1", "AC1027", "0", "ENDSEC", "0", "EOF",
    ];

    let lf_file = parse_drawing(code_pairs.join("\n").as_str());
    assert_eq!(AcadVersion::R2013, lf_file.header.version);

    let crlf_file = parse_drawing(code_pairs.join("\r\n").as_str());
    assert_eq!(AcadVersion::R2013, crlf_file.header.version);
}

#[test]
fn read_file_with_comments() {
    let drawing = drawing_from_pairs(vec![
        CodePair::new_str(999, "comment"),
        CodePair::new_str(0, "SECTION"),
        CodePair::new_str(999, ""), // empty comment
        CodePair::new_str(2, "ENTITIES"),
        CodePair::new_str(0, "LINE"),
        CodePair::new_str(999, "comment"),
        CodePair::new_f64(10, 1.1),
        CodePair::new_str(999, "comment"),
        CodePair::new_f64(20, 2.2),
        CodePair::new_str(999, "comment"),
        CodePair::new_str(0, "ENDSEC"),
        CodePair::new_str(0, "EOF"),
        CodePair::new_str(9, "comment"),
    ]);
    let entities = drawing.entities().collect::<Vec<_>>();
    assert_eq!(1, entities.len());
    match entities[0].specific {
        EntityType::Line(ref line) => {
            assert_eq!(Point::new(1.1, 2.2, 0.0), line.p1);
        }
        _ => panic!("expected a LINE"),
    }
}

#[test]
fn enum_out_of_bounds() {
    let file = from_section(
        "HEADER",
        vec![CodePair::new_str(9, "$DIMZIN"), CodePair::new_i16(70, 8)],
    );
    assert_eq!(
        UnitZeroSuppression::SuppressZeroFeetAndZeroInches,
        file.header.dimension_unit_zero_suppression
    );
}

#[test]
fn round_trip() {
    // drawing with one entity and one auto-added layer
    let mut drawing = Drawing::new();
    drawing.clear();
    drawing.add_entity(Entity {
        common: Default::default(),
        specific: EntityType::Line(Default::default()),
    });
    assert_eq!(1, drawing.entities().count());
    assert_eq!(1, drawing.layers().count());

    // ensure they're still there
    let drawing = drawing_from_pairs(drawing.code_pairs().unwrap());
    assert_eq!(1, drawing.entities().count());
    assert_eq!(1, drawing.layers().count());
}

#[test]
fn parse_with_leading_bom() {
    let buf = vec![
        0xEFu8, 0xBB, 0xBF, // UTF-8 byte representation of BOM
        b'0', b'\n', b'E', b'O', b'F',
    ];
    let _drawing = Drawing::load(&mut buf.as_slice());
}

#[test]
fn parse_with_bom_like_in_the_middle() {
    let head = "
  0
SECTION
  2
HEADER
  9
$PROJECTNAME
  1"
    .trim();
    let tail = "
  0
ENDSEC
  0
EOF"
    .trim();
    let mut bytes = head.as_bytes().to_vec();
    bytes.push(b'\r');
    bytes.push(b'\n');
    bytes.push(0xEF); // these three bytes represent the character `ア` in UTF8
    bytes.push(0xBD);
    bytes.push(0xB1);
    bytes.push(b'\r');
    bytes.push(b'\n');
    for b in tail.as_bytes() {
        bytes.push(*b);
    }
    let mut bytes = bytes.as_slice();
    let drawing = unwrap_drawing(Drawing::load_with_encoding(&mut bytes, encoding_rs::UTF_8));
    assert_eq!("", drawing.header.project_name);
}

#[test]
fn parse_as_ascii_text() {
    // if version <= R2004 (AC1018) stream is ASCII
    let drawing = parse_drawing(
        r"
  0
SECTION
  2
HEADER
  9
$ACADVER
  1
AC1018
  9
$PROJECTNAME
  1
\U+00E8
  0
ENDSEC
  0
EOF
"
        .trim(),
    );
    assert_eq!("è", drawing.header.project_name);
}

#[test]
fn parse_as_utf8_text() {
    // if version >= R2007 (AC1021) stream is UTF-8
    let mut bytes = vec![];
    bytes.extend_from_slice(
        r"
  0
SECTION
  2
HEADER
  9
$ACADVER
  1
AC1018
  9
$PROJECTNAME
  1"
        .trim()
        .as_bytes(),
    );
    bytes.push(b'\n');
    bytes.push(232); // è
    bytes.push(b'\n');
    bytes.extend_from_slice(
        r"
  0
ENDSEC
  0
EOF"
        .trim()
        .as_bytes(),
    );
    let drawing = Drawing::load(&mut bytes.as_slice()).ok().unwrap();
    assert_eq!("è", drawing.header.project_name);
}

#[test]
fn read_with_alternate_encoding() {
    let head = "
  0
SECTION
  2
HEADER
  9
$PROJECTNAME
  1"
    .trim();
    let tail = "
  0
ENDSEC
  0
EOF"
    .trim();
    let mut bytes = head.as_bytes().to_vec();
    bytes.push(b'\r');
    bytes.push(b'\n');
    bytes.push(0xB2); // these two bytes represent the character `不` in GB18030 encoding
    bytes.push(0xBB);
    bytes.push(b'\r');
    bytes.push(b'\n');
    for b in tail.as_bytes() {
        bytes.push(*b);
    }
    let mut bytes = bytes.as_slice();
    let drawing = unwrap_drawing(Drawing::load_with_encoding(
        &mut bytes,
        encoding_rs::GB18030,
    ));
    assert_eq!("", drawing.header.project_name);
}

#[test]
fn read_binary_file() {
    // `diamond-bin.dxf` is a pre-R13 binary file
    let drawing = unwrap_drawing(Drawing::load_file("./src/misc_tests/diamond-bin.dxf"));
    let entities = drawing.entities().collect::<Vec<_>>();
    assert_eq!(12, entities.len());
    match entities[0].specific {
        EntityType::Line(ref line) => {
            assert_eq!(Point::new(45.0, 45.0, 0.0), line.p1);
            assert_eq!(Point::new(45.0, -45.0, 0.0), line.p2);
        }
        _ => panic!("expected a line"),
    }
}

#[test]
fn read_binary_file_post_r13() {
    // post R13 binary files have 2 byte codes and single byte booleans
    let data = vec![
        // binary header
        b'A', b'u', b't', b'o', b'C', b'A', b'D', b' ', b'B', b'i', b'n', b'a', b'r', b'y', b' ',
        b'D', b'X', b'F', b'\r', b'\n', 0x1A, 0x00, // 0/SECTION
        0x00, 0x00, b'S', b'E', b'C', b'T', b'I', b'O', b'N', 0x00, // 2/HEADER
        0x02, 0x00, b'H', b'E', b'A', b'D', b'E', b'R', 0x00, // 9/$LWDISPLAY
        0x09, 0x00, b'$', b'L', b'W', b'D', b'I', b'S', b'P', b'L', b'A', b'Y', 0x00, 0x22, 0x01,
        0x01, // 290/true
        0x00, 0x00, b'E', b'N', b'D', b'S', b'E', b'C', 0x00, // 0/ENDSEC
        0x00, 0x00, b'E', b'O', b'F', 0x00, // 0/EOF
    ];
    let drawing = Drawing::load(&mut data.as_slice()).unwrap();
    assert!(drawing.header.display_linewieght_in_model_and_layout_tab);
}

#[test]
fn read_binary_file_after_writing() {
    for version in &[AcadVersion::R12, AcadVersion::R13] {
        let mut drawing = Drawing::new();
        drawing.header.version = *version;
        let line = Line {
            p1: Point::new(1.1, 2.2, 3.3),
            p2: Point::new(4.4, 5.5, 6.6),
            ..Default::default()
        };
        drawing.add_entity(Entity::new(EntityType::Line(line)));
        let mut buf = Cursor::new(vec![]);
        drawing.save_binary(&mut buf).ok().unwrap();
        buf.seek(SeekFrom::Start(0)).ok().unwrap();
        let mut reader = BufReader::new(&mut buf);
        let drawing = unwrap_drawing(Drawing::load(&mut reader));
        let entities = drawing.entities().collect::<Vec<_>>();
        assert_eq!(1, entities.len());
        match entities[0].specific {
            EntityType::Line(ref line) => {
                assert_eq!(Point::new(1.1, 2.2, 3.3), line.p1);
                assert_eq!(Point::new(4.4, 5.5, 6.6), line.p2);
            }
            _ => panic!("expected a line"),
        }
    }
}

#[test]
fn read_dxb_file() {
    let data = vec![
        // DXB sentinel "AutoCAD DXB 1.0\r\n"
        b'A', b'u', b't', b'o', b'C', b'A', b'D', b' ', b'D', b'X', b'B', b' ', b'1', b'.', b'0',
        b'\r', b'\n', 0x1A, 0x0, // color
        136, // type specifier for new color
        0x01, 0x00, // color index 1
        // line
        0x01, // type specifier
        0x01, 0x00, // p1.x = 0x0001
        0x02, 0x00, // p1.y = 0x0002
        0x03, 0x00, // p1.z = 0x0003
        0x04, 0x00, // p2.x = 0x0004
        0x05, 0x00, // p2.y = 0x0005
        0x06, 0x00, // p2.z = 0x0006
        0x0,  // null terminator
    ];
    let drawing = Drawing::load(&mut data.as_slice()).unwrap();
    let entities = drawing.entities().collect::<Vec<_>>();
    assert_eq!(1, entities.len());
    assert_eq!(Some(1), entities[0].common.color.index());
    match entities[0].specific {
        EntityType::Line(ref line) => {
            assert_eq!(Point::new(1.0, 2.0, 3.0), line.p1);
            assert_eq!(Point::new(4.0, 5.0, 6.0), line.p2);
        }
        _ => panic!("expected a line"),
    }
}

#[test]
fn read_dxb_file_with_polyline() {
    let data = vec![
        // DXB sentinel "AutoCAD DXB 1.0\r\n"
        b'A', b'u', b't', b'o', b'C', b'A', b'D', b' ', b'D', b'X', b'B', b' ', b'1', b'.', b'0',
        b'\r', b'\n', 0x1A, 0x0, 19, // polyline
        0x00, 0x00, // is closed = false
        20,   // vertex
        0x01, 0x00, // x
        0x02, 0x00, // y
        20,   // vertex
        0x03, 0x00, // x
        0x04, 0x00, // y
        17,   // seqend
        0x0,  // null terminator
    ];
    let drawing = Drawing::load(&mut data.as_slice()).unwrap();
    let entities = drawing.entities().collect::<Vec<_>>();
    assert_eq!(1, entities.len());
    match entities[0].specific {
        EntityType::Polyline(ref poly) => {
            let vertices = poly.vertices().collect::<Vec<_>>();
            assert_eq!(2, vertices.len());
            assert_eq!(Point::new(1.0, 2.0, 0.0), vertices[0].location);
            assert_eq!(Point::new(3.0, 4.0, 0.0), vertices[1].location);
        }
        _ => panic!("expected a polyline"),
    }
}

#[test]
fn read_dxb_after_writing() {
    let mut drawing = Drawing::new();
    let line = Line::new(Point::new(1.0, 2.0, 3.0), Point::new(4.0, 5.0, 6.0));
    drawing.add_entity(Entity::new(EntityType::Line(line)));
    let mut buf = Cursor::new(vec![]);
    drawing.save_dxb(&mut buf).ok().unwrap();
    buf.seek(SeekFrom::Start(0)).ok().unwrap();
    let mut reader = BufReader::new(&mut buf);
    let drawing = unwrap_drawing(Drawing::load(&mut reader));
    let entities = drawing.entities().collect::<Vec<_>>();
    assert_eq!(1, entities.len());
    match entities[0].specific {
        EntityType::Line(ref line) => {
            assert_eq!(Point::new(1.0, 2.0, 3.0), line.p1);
            assert_eq!(Point::new(4.0, 5.0, 6.0), line.p2);
        }
        _ => panic!("expected a line"),
    }
}

#[test]
fn dont_write_utf8_bom() {
    let drawing = Drawing::new();
    let mut buf = Cursor::new(vec![]);
    drawing.save(&mut buf).ok().unwrap();
    buf.seek(SeekFrom::Start(0)).ok().unwrap();
    let vec = buf.into_inner();

    // file should start directly with a code, not a UTF-8 BOM
    assert_eq!(b' ', vec[0]);
    assert_eq!(b' ', vec[1]);
    assert_eq!(b'0', vec[2]);
}

#[test]
fn write_unicode_as_ascii() {
    let mut drawing = Drawing::new();
    drawing.header.version = AcadVersion::R2004;
    drawing.header.project_name = String::from("è");
    assert_contains(
        &drawing,
        ["  9", "$PROJECTNAME", "  1", "\\U+00E8"].join("\r\n"),
    );
}

#[test]
fn write_unicode_as_utf8() {
    let mut drawing = Drawing::new();
    drawing.header.version = AcadVersion::R2007;
    drawing.header.project_name = String::from("è");
    assert_contains(&drawing, ["  9", "$PROJECTNAME", "  1", "è"].join("\r\n"));
}

#[test]
fn write_binary_file() {
    for version in &[AcadVersion::R12, AcadVersion::R13] {
        println!("checking version {version:?}");
        let mut drawing = Drawing::new();
        drawing.header.version = *version;
        let buf = to_binary(&drawing);

        // check binary sentinel
        let sentinel = from_utf8(&buf[0..20]).ok().unwrap();
        assert_eq!("AutoCAD Binary DXF\r\n", sentinel);

        // check "SECTION" text at expected offset
        let sec_offset = if *version <= AcadVersion::R12 { 23 } else { 24 };
        let sec_end = sec_offset + 7;
        let sec_text = from_utf8(&buf[sec_offset..sec_end]).ok().unwrap();
        assert_eq!("SECTION", sec_text);
    }
}

#[test]
fn thumbnail_round_trip_rgb8() {
    // 1x1 px image, red pixel
    let mut imgbuf = image::ImageBuffer::new(1, 1);
    imgbuf.put_pixel(0, 0, image::Rgb([255u8, 0, 0]));
    let thumbnail = DynamicImage::ImageRgb8(imgbuf);

    let round_tripped = round_trip_thumbnail(thumbnail);
    assert_eq!((1, 1), round_tripped.dimensions());
    assert_eq!(
        image::Rgba([255u8, 0, 0, 255]),
        round_tripped.get_pixel(0, 0)
    );
}

#[test]
fn thumbnail_round_trip_grayscale() {
    // 1x1 px grayscale image, white
    let mut imgbuf = image::ImageBuffer::new(1, 1);
    imgbuf.put_pixel(0, 0, image::Luma([255]));
    let thumbnail = DynamicImage::ImageLuma8(imgbuf);

    let round_tripped = round_trip_thumbnail(thumbnail);
    assert_eq!((1, 1), round_tripped.dimensions());
    assert_eq!(
        image::Rgba([255u8, 255, 255, 255]),
        round_tripped.get_pixel(0, 0)
    ); // it comes back as RGB
}

fn round_trip_thumbnail(thumbnail: image::DynamicImage) -> image::DynamicImage {
    // write drawing with thumbnail
    let mut drawing = Drawing::new();
    drawing.header.version = AcadVersion::R2000; // thumbnails are only written >= R2000
    drawing.thumbnail = Some(thumbnail);

    let drawing_pairs = drawing.code_pairs().unwrap();
    assert_vec_contains(
        &drawing_pairs,
        &[
            CodePair::new_str(0, "SECTION"),
            CodePair::new_str(2, "THUMBNAILIMAGE"),
        ],
    );

    let drawing = drawing_from_pairs(drawing_pairs);
    drawing.thumbnail.unwrap()
}