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
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
//! DXF writer module

mod binary_writer;
mod r12;
mod section_writer;
mod stream_writer;
mod text_writer;

pub use binary_writer::DxfBinaryWriter;
pub use section_writer::SectionWriter;
pub use stream_writer::{value_type_for_code, DxfStreamWriter, DxfStreamWriterExt};
pub use text_writer::DxfTextWriter;

use crate::document::CadDocument;
use crate::entities::EntityType;
use crate::error::Result;
use std::fs::File;
use std::io::{BufWriter, Write};
use std::path::Path;

/// An angle in degrees for a DXF angle group, chosen so the reader's
/// `to_radians` gives back exactly `radians`. A plain `to_degrees` can land
/// one representable step off that value, and the angle then reads back
/// changed by one ULP.
pub(crate) fn degrees(radians: f64) -> f64 {
    let degrees = radians.to_degrees();
    if !degrees.is_finite() || degrees.to_radians() == radians {
        return degrees;
    }
    let (mut down, mut up) = (degrees, degrees);
    for _ in 0..4 {
        up = up.next_up();
        if up.to_radians() == radians {
            return up;
        }
        down = down.next_down();
        if down.to_radians() == radians {
            return down;
        }
    }
    degrees
}

/// DXF file writer
pub struct DxfWriter<'a> {
    document: &'a CadDocument,
    /// Whether to write binary DXF format
    pub binary: bool,
    /// Explicit target version; `None` writes the document's own version.
    version: Option<crate::types::DxfVersion>,
}

impl<'a> DxfWriter<'a> {
    /// Create a new DXF writer for ASCII output
    pub fn new(document: &'a CadDocument) -> Self {
        Self {
            document,
            binary: false,
            version: None,
        }
    }

    /// Create a new DXF writer for binary output
    pub fn new_binary(document: &'a CadDocument) -> Self {
        Self {
            document,
            binary: true,
            version: None,
        }
    }

    /// Write the document as `version` instead of its own version.
    ///
    /// Requesting [`DxfVersion::AC1009`](crate::types::DxfVersion::AC1009)
    /// produces a genuine R12 file (no handles, no CLASSES/OBJECTS sections,
    /// newer entities converted to R12 entities; ASCII only). A document that
    /// merely *carries* `AC1009` without this call keeps being written as R13,
    /// because the R13 structure preserves everything the document holds.
    pub fn with_version(mut self, version: crate::types::DxfVersion) -> Self {
        self.version = Some(version);
        self
    }

    /// Set whether to write binary format
    pub fn set_binary(&mut self, binary: bool) {
        self.binary = binary;
    }

    /// Write to a file
    pub fn write_to_file<P: AsRef<Path>>(&self, path: P) -> Result<()> {
        self.validate_architecture_records()?;
        let file = File::create(path)?;
        let writer = BufWriter::new(file);
        self.write_to_writer(writer)
    }

    /// Write to any writer
    pub fn write_to_writer<W: Write>(&self, writer: W) -> Result<()> {
        self.validate_architecture_records()?;
        if self.version == Some(crate::types::DxfVersion::AC1009) {
            return self.write_r12(writer);
        }
        for (handle, object) in &self.document.objects {
            if let crate::objects::ObjectType::Unknown {
                type_name,
                raw_dxf_codes: None,
                ..
            } = object
            {
                if crate::objects::associative_canonical_name(type_name) == "ASSOC2DCONSTRAINTGROUP"
                {
                    return Err(crate::error::DxfError::NotImplemented(format!(
                        "Cannot export undecoded constraint group {type_name} at handle {handle} to DXF; save as the source DWG version to preserve its payload"
                    )));
                }
            }
        }
        // BricsCAD's binary DXF importer rejects the R2000/R2004 headers even
        // when their records are otherwise valid.  The same records are
        // accepted as R2010 binary DXF, whose strings are UTF-8.  Keep this
        // compatibility promotion local to binary output; ASCII and DWG
        // continue to preserve the requested legacy version and code page.
        let output_version = self.output_version();
        let encoding = if output_version < crate::types::DxfVersion::AC1021 {
            crate::io::dxf::code_page::encoding_from_code_page(&self.document.header.code_page)
        } else {
            None
        };
        if self.binary {
            let mut stream_writer = DxfBinaryWriter::new(writer)?;
            if let Some(encoding) = encoding {
                stream_writer.set_encoding(encoding);
            }
            self.write_dxf(&mut stream_writer, output_version)?;
            stream_writer.flush()?;
        } else {
            let mut stream_writer = DxfTextWriter::new(writer);
            if let Some(encoding) = encoding {
                stream_writer.set_encoding(encoding);
            }
            self.write_dxf(&mut stream_writer, output_version)?;
            stream_writer.flush()?;
        }
        Ok(())
    }

    fn validate_architecture_records(&self) -> Result<()> {
        let r12 = self.version == Some(crate::types::DxfVersion::AC1009);
        let reason = if r12 {
            "R12 does not support Arch classes"
        } else {
            "a native DXF class body is required"
        };
        for entity in self.document.entities() {
            if let EntityType::Extended(entity) = entity {
                if let crate::entities::ExtendedEntityData::Arch(data) = &entity.data {
                    if r12 || data.raw_dxf_codes.is_none() {
                        return Err(crate::error::DxfError::NotImplemented(format!(
                            "Cannot export Arch entity {} at handle {} to DXF: {reason}; save as the source DWG version to preserve its payload",
                            data.dxf_name, entity.common.handle,
                        )));
                    }
                }
            }
        }
        for object in self.document.objects.values() {
            if let crate::objects::ObjectType::Arch(object) = object {
                if r12 || object.raw_dxf_codes.is_none() {
                    return Err(crate::error::DxfError::NotImplemented(format!(
                        "Cannot export Arch object {} at handle {} to DXF: {reason}; save as the source DWG version to preserve its payload",
                        object.dxf_name, object.handle,
                    )));
                }
            }
        }
        Ok(())
    }

    /// Write to a byte vector (useful for testing)
    pub fn write_to_vec(&self) -> Result<Vec<u8>> {
        // Pre-allocate based on entity count: ~512 bytes per entity is a reasonable estimate
        let entity_count = self.document.entities().count();
        let estimated = (entity_count + 64) * 512;
        let mut buffer = Vec::with_capacity(estimated);
        self.write_to_writer(&mut buffer)?;
        Ok(buffer)
    }

    /// Write DXF content to a stream writer
    fn write_dxf<W: DxfStreamWriter>(
        &self,
        writer: &mut W,
        output_version: crate::types::DxfVersion,
    ) -> Result<()> {
        let mut prepared = crate::io::loft_parameters::prepared(self.document);
        // DXF and DWG share the same ownership graph. Repair table-backed
        // entities before either writer snapshots handles, otherwise an
        // ACAD_TABLE can emit a null/nonexistent block-record pointer.
        crate::io::dwg::dwg_writer::prepare_database_references(&mut prepared);
        // An in-place table rename leaves the entry keyed under its old name,
        // so entity layer/linetype lookups miss and reference a name the
        // written table no longer defines (issue #80).
        crate::io::dwg::dwg_writer::prepare_table_keys(&mut prepared);
        // Table entries added after the document was built or read
        // (`Layer::new` + `layers.add`, `AppId::new` + `app_ids.add`) still
        // carry NULL handles. Written as they are, each becomes a `5 / 0`
        // record, a duplicate of the others, and each also claims the root
        // dictionary (owned by NULL) as its extension dictionary. Assign them
        // on the output copy, as the DWG writer does.
        if prepared.has_null_table_entries() {
            prepared.to_mut().assign_table_entry_handles();
        }
        crate::io::dwg::dwg_writer::prepare_active_viewport_elevation(&mut prepared);
        self.write_prepared_dxf(writer, prepared.as_ref(), output_version)
    }

    fn write_prepared_dxf<W: DxfStreamWriter>(
        &self,
        writer: &mut W,
        document: &CadDocument,
        output_version: crate::types::DxfVersion,
    ) -> Result<()> {
        let handle_start = compute_max_handle(document);
        // Count against the version actually emitted, not the one the document
        // was loaded as, so the handle budget matches what the writer produces.
        let extra_handles = count_extra_handles(
            document,
            SectionWriter::<W>::writable_version(output_version),
        );
        let handle_seed = handle_start + extra_handles + 1;
        let mut section_writer = SectionWriter::new(writer, handle_start, handle_seed);
        section_writer.set_version(output_version);
        section_writer.build_valid_handles(document);

        // Write all sections
        section_writer.write_header(document)?;
        section_writer.write_classes(document)?;
        section_writer.write_tables(document)?;
        section_writer.write_blocks(document)?;
        section_writer.write_entities(document)?;
        section_writer.write_objects(document)?;
        section_writer.write_acdsdata()?;

        // Write EOF
        writer.write_string(0, "EOF")?;

        Ok(())
    }

    fn output_version(&self) -> crate::types::DxfVersion {
        let version = self.version.unwrap_or(self.document.version);
        if self.binary && version < crate::types::DxfVersion::AC1021 {
            crate::types::DxfVersion::AC1024
        } else {
            version
        }
    }

    /// Genuine R12 output (see [`DxfWriter::with_version`]).
    fn write_r12<W: Write>(&self, writer: W) -> Result<()> {
        if self.binary {
            // R12 binary DXF uses one-byte group codes, which the binary writer does not emit
            return Err(crate::error::DxfError::UnsupportedVersion(
                "binary DXF R12 (AC1009); write R12 as ASCII DXF".into(),
            ));
        }
        let mut stream_writer = DxfTextWriter::new(writer);
        if let Some(encoding) =
            crate::io::dxf::code_page::encoding_from_code_page(&self.document.header.code_page)
        {
            stream_writer.set_encoding(encoding);
        }
        r12::write_r12(&mut stream_writer, self.document)?;
        stream_writer.flush()?;
        Ok(())
    }

    /// Get a reference to the document
    pub fn document(&self) -> &CadDocument {
        &self.document
    }
}

fn count_extra_handles(document: &CadDocument, version: crate::types::DxfVersion) -> u64 {
    use crate::objects::ObjectType;

    let mut count = 0u64;

    for entity in document.entities() {
        match entity {
            EntityType::Polyline(polyline) => {
                // Vertices + SEQEND all use allocate_handle()
                count += polyline.vertices.len() as u64 + 1;
            }
            EntityType::Polyline2D(polyline) => {
                // Plain 2D polylines down-save as LWPOLYLINE for R2000+,
                // which emits no child records (see SectionWriter).
                if version >= crate::types::DxfVersion::AC1015
                    && section_writer::polyline2d_downsaves_to_lwpolyline(polyline)
                {
                    continue;
                }
                // Vertices + SEQEND all use allocate_handle()
                count += polyline.vertices.len() as u64 + 1;
            }
            EntityType::Polyline3D(polyline) => {
                for vertex in &polyline.vertices {
                    if vertex.handle.is_null() {
                        count += 1;
                    }
                }
                // SEQEND always written
                count += 1;
            }
            EntityType::PolyfaceMesh(mesh) => {
                for vertex in &mesh.vertices {
                    if vertex.common.handle.is_null() {
                        count += 1;
                    }
                }
                for face in &mesh.faces {
                    if face.common.handle.is_null() {
                        count += 1;
                    }
                }
                if mesh.seqend_handle.is_none() {
                    count += 1;
                }
            }
            EntityType::PolygonMesh(mesh) => {
                for vertex in &mesh.vertices {
                    if vertex.common.handle.is_null() {
                        count += 1;
                    }
                }
                // SEQEND always needs a handle
                count += 1;
            }
            EntityType::Insert(insert) => {
                if insert.has_attributes() {
                    count += insert
                        .attributes
                        .iter()
                        .filter(|attribute| attribute.common.handle.is_null())
                        .count() as u64;
                    // SEQEND for attribute sequence
                    count += 1;
                }
            }
            _ => {}
        }
    }

    // Count SortEntitiesTable sort entries (allocate_handle used for each)
    for obj in document.objects.values() {
        if let ObjectType::SortEntitiesTable(table) = obj {
            count += table.len() as u64;
        }
    }

    // Null layer handles are allocated while writing the LAYER table.
    count += document
        .layers
        .iter()
        .filter(|layer| layer.handle.is_null())
        .count() as u64;

    count
}

/// Compute the true maximum handle across all document objects.
/// Returns max_handle + 1 (the first safe handle to allocate).
/// This is needed because DWG-loaded documents may have next_handle
/// below some object handles (DWG reader doesn't call resolve_references).
fn compute_max_handle(document: &CadDocument) -> u64 {
    let mut max = document.next_handle();

    for entity in document.entities() {
        let h = entity.common().handle.value();
        if h >= max {
            max = h + 1;
        }
        if let EntityType::Insert(insert) = entity {
            for attribute in &insert.attributes {
                max = max.max(attribute.common.handle.value().saturating_add(1));
            }
        }
    }
    for (handle, _) in &document.objects {
        let h = handle.value();
        if h >= max {
            max = h + 1;
        }
    }
    for br in document.block_records.iter() {
        let h = br.handle.value();
        if h >= max {
            max = h + 1;
        }
        for eh in &br.entity_handles {
            let h = eh.value();
            if h >= max {
                max = h + 1;
            }
        }
        // BLOCK/ENDBLK markers are excluded from document.entities(), so scan
        // their handles here explicitly to avoid re-issuing them.
        let h = br.block_entity_handle.value();
        if h >= max {
            max = h + 1;
        }
        let h = br.block_end_handle.value();
        if h >= max {
            max = h + 1;
        }
    }
    for r in document.layers.iter() {
        let h = r.handle.value();
        if h >= max {
            max = h + 1;
        }
    }
    for r in document.line_types.iter() {
        let h = r.handle.value();
        if h >= max {
            max = h + 1;
        }
    }
    for r in document.text_styles.iter() {
        let h = r.handle.value();
        if h >= max {
            max = h + 1;
        }
    }
    for r in document.dim_styles.iter() {
        let h = r.handle.value();
        if h >= max {
            max = h + 1;
        }
    }
    for r in document.app_ids.iter() {
        let h = r.handle.value();
        if h >= max {
            max = h + 1;
        }
    }
    for r in document.views.iter() {
        let h = r.handle.value();
        if h >= max {
            max = h + 1;
        }
    }
    for r in document.vports.iter() {
        let h = r.handle.value();
        if h >= max {
            max = h + 1;
        }
    }
    for r in document.ucss.iter() {
        let h = r.handle.value();
        if h >= max {
            max = h + 1;
        }
    }

    max
}

/// Convenience function to write a document to a file
pub fn write_dxf<P: AsRef<Path>>(document: &CadDocument, path: P) -> Result<()> {
    let writer = DxfWriter::new(document);
    writer.write_to_file(path)
}

/// Convenience function to write a document to a binary DXF file
pub fn write_binary_dxf<P: AsRef<Path>>(document: &CadDocument, path: P) -> Result<()> {
    let writer = DxfWriter::new_binary(document);
    writer.write_to_file(path)
}