acadrust 0.6.0

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
//! Less-common but fully structured DWG entity classes.
//!
//! Keeping these class-based entities behind one wrapper avoids treating
//! supported records as opaque proxy payloads while keeping the main entity
//! enum compact.

use crate::entities::{Entity, EntityCommon, MText};
use crate::types::{BoundingBox3D, Color, Handle, LineWeight, Transparency, Vector2, Vector3};

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ExtendedEntity {
    pub common: EntityCommon,
    pub data: ExtendedEntityData,
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum ExtendedEntityData {
    Camera { view_handle: Handle },
    SectionObject(SectionObjectData),
    ArcAlignedText(ArcAlignedTextData),
    RemoteText(RemoteTextData),
    GeoPositionMarker(GeoPositionMarkerData),
    CoordinationModel(CoordinationModelData),
    PointCloud(PointCloudData),
    PointCloudEx(PointCloudExData),
    Proxy(ProxyEntityData),
    OleFrame(OleFrameData),
    LayoutPrintConfig(LayoutPrintConfigData),
    Format(FormatData),
    Legacy(LegacyEntityData),
    DynamicBlock(crate::objects::DynamicBlockData),
    RegisteredClass(RegisteredClassEntityData),
}

impl ExtendedEntityData {
    pub(crate) fn preserve_storage_data_from(&mut self, source: &Self) {
        match (self, source) {
            (Self::LayoutPrintConfig(value), Self::LayoutPrintConfig(source)) => {
                value.raw_dwg_data = source.raw_dwg_data.clone();
                value.raw_dwg_version = source.raw_dwg_version;
            }
            (Self::Format(value), Self::Format(source)) => {
                value.raw_dwg_data = source.raw_dwg_data.clone();
                value.raw_dwg_version = source.raw_dwg_version;
                value.raw_dxf_codes = source.raw_dxf_codes.clone();
            }
            _ => {}
        }
    }
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SectionObjectData {
    pub state: i32,
    pub flags: i32,
    pub name: String,
    pub vertical_direction: Vector3,
    pub top_height: f64,
    pub bottom_height: f64,
    pub indicator_alpha: i16,
    pub indicator_color: Color,
    pub vertices: Vec<Vector3>,
    pub back_line_vertices: Vec<Vector3>,
    pub settings_handle: Handle,
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ArcAlignedTextData {
    pub text: String,
    pub font_name: String,
    pub big_font_name: String,
    pub style_name: String,
    pub center: Vector3,
    pub radius: f64,
    pub x_scale: f64,
    pub text_size: f64,
    pub character_spacing: f64,
    pub offset_from_arc: f64,
    pub right_offset: f64,
    pub left_offset: f64,
    pub start_angle: f64,
    pub end_angle: f64,
    pub reverse: bool,
    pub text_direction: i16,
    pub alignment: i16,
    pub text_position: i16,
    pub bold: bool,
    pub italic: bool,
    pub underlined: bool,
    pub character_set: i16,
    pub pitch_and_family: i16,
    pub is_shx: bool,
    pub text_color: i32,
    pub normal: Vector3,
    pub wizard_flag: bool,
    pub arc_handle: Handle,
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct RemoteTextData {
    pub position: Vector3,
    pub normal: Vector3,
    pub rotation: f64,
    pub height: f64,
    pub style_handle: Handle,
    pub style_name: String,
    pub flags: i16,
    pub text: String,
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct GeoPositionMarkerData {
    pub class_version: i32,
    pub position: Vector3,
    pub radius: f64,
    pub notes: String,
    pub landing_gap: f64,
    pub mtext_visible: bool,
    pub text_alignment: u8,
    pub enable_frame_text: bool,
    pub embedded_mtext: Option<MText>,
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CoordinationModelData {
    pub flags: i16,
    pub definition_handle: Handle,
    pub transform: [f64; 16],
    pub unit_factor: f64,
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct PointCloudClip {
    pub inverted: bool,
    pub clip_type: i16,
    pub vertices: Vec<Vector2>,
    pub z_min: f64,
    pub z_max: f64,
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct PointCloudData {
    pub class_version: i16,
    pub origin: Vector3,
    pub saved_filename: String,
    pub source_files: Vec<String>,
    pub extents_min: Vector3,
    pub extents_max: Vector3,
    pub point_count: i64,
    pub ucs_name: String,
    pub ucs_origin: Vector3,
    pub ucs_x_direction: Vector3,
    pub ucs_y_direction: Vector3,
    pub ucs_z_direction: Vector3,
    pub definition_handle: Handle,
    pub reactor_handle: Handle,
    pub show_intensity: bool,
    pub intensity_scheme: i16,
    pub minimum_intensity: f64,
    pub maximum_intensity: f64,
    pub low_intensity_threshold: f64,
    pub high_intensity_threshold: f64,
    pub show_clipping: bool,
    pub clippings: Vec<PointCloudClip>,
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct PointCloudExCrop {
    pub crop_type: i16,
    pub inside: bool,
    pub inverted: bool,
    pub plane: Vector3,
    pub x_direction: Vector3,
    pub y_direction: Vector3,
    pub points: Vec<Vector3>,
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct PointCloudExData {
    pub class_version: i16,
    pub extents_min: Vector3,
    pub extents_max: Vector3,
    pub ucs_origin: Vector3,
    pub ucs_x_direction: Vector3,
    pub ucs_y_direction: Vector3,
    pub ucs_z_direction: Vector3,
    pub locked: bool,
    pub definition_handle: Handle,
    pub reactor_handle: Handle,
    pub name: String,
    pub show_intensity: bool,
    pub show_cropping: bool,
    pub unknown_bl0: i32,
    pub unknown_bl1: i32,
    pub stylization_type: i16,
    pub intensity_color_scheme: String,
    pub current_color_scheme: String,
    pub classification_color_scheme: String,
    pub elevation_min: f64,
    pub elevation_max: f64,
    pub intensity_min: i32,
    pub intensity_max: i32,
    pub intensity_out_of_range_behavior: i16,
    pub elevation_out_of_range_behavior: i16,
    pub elevation_apply_to_fixed_range: bool,
    pub intensity_as_gradient: bool,
    pub elevation_as_gradient: bool,
    pub croppings: Vec<PointCloudExCrop>,
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ProxyEntityData {
    pub proxy_id: i32,
    pub class_id: i32,
    /// R2004+ binary text-stream name of the proxied DXF class.
    pub dxf_subclass: String,
    pub version: i32,
    pub dwg_version: i32,
    pub maintenance_version: i32,
    pub from_dxf: bool,
    pub graphics: crate::objects::ProxyPayload,
    pub payload: crate::objects::ProxyPayload,
    /// Opaque custom strings following `dxf_subclass` in the R2007+ text
    /// stream. Kept separate because proxy payloads may address both streams.
    #[cfg_attr(feature = "serde", serde(default))]
    pub text_payload: crate::objects::ProxyPayload,
    pub object_ids: Vec<crate::objects::ProxyObjectReference>,
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct OleFrameData {
    pub flag: i16,
    pub mode: i16,
    pub storage: crate::compound_file::StructuredStoragePayload,
}

#[derive(Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct LayoutPrintConfigData {
    pub class_version: i16,
    pub flag: i16,
    #[cfg_attr(feature = "serde", serde(skip))]
    pub raw_dwg_data: Option<Vec<u8>>,
    #[cfg_attr(feature = "serde", serde(default))]
    pub raw_dwg_handle_bits: i64,
    #[cfg_attr(feature = "serde", serde(skip))]
    pub raw_dwg_version: Option<crate::types::DxfVersion>,
}

#[derive(Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct FormatData {
    /// Opaque SPDSGraphiCS payload. The vendor schema is not public.
    #[cfg_attr(feature = "serde", serde(skip))]
    pub raw_dwg_data: Option<Vec<u8>>,
    pub raw_dwg_handle_bits: i64,
    #[cfg_attr(feature = "serde", serde(skip))]
    pub raw_dwg_version: Option<crate::types::DxfVersion>,
    #[cfg_attr(feature = "serde", serde(skip))]
    pub raw_dxf_codes: Option<Vec<(i32, String)>>,
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum LegacyEntityData {
    Repeat,
    EndRepeat {
        columns: i16,
        rows: i16,
        column_spacing: f64,
        row_spacing: f64,
    },
    Load {
        filename: String,
    },
    Jump {
        address: u32,
    },
}

#[derive(Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct RegisteredClassEntityData {
    pub dxf_name: String,
    pub cpp_class_name: String,
    pub properties: Vec<crate::objects::SemanticProperty>,
    pub payload: crate::objects::ProxyPayload,
    pub object_ids: Vec<crate::objects::ProxyObjectReference>,
}

impl ExtendedEntity {
    pub fn class_name(&self) -> &str {
        match &self.data {
            ExtendedEntityData::Camera { .. } => "CAMERA",
            ExtendedEntityData::SectionObject(_) => "SECTIONOBJECT",
            ExtendedEntityData::ArcAlignedText(_) => "ARCALIGNEDTEXT",
            ExtendedEntityData::RemoteText(_) => "RTEXT",
            ExtendedEntityData::GeoPositionMarker(_) => "POSITIONMARKER",
            ExtendedEntityData::CoordinationModel(_) => "COORDINATION_MODEL",
            ExtendedEntityData::PointCloud(_) => "ACDBPOINTCLOUD",
            ExtendedEntityData::PointCloudEx(_) => "ACDBPOINTCLOUDEX",
            ExtendedEntityData::Proxy(_) => "ACAD_PROXY_ENTITY",
            ExtendedEntityData::OleFrame(_) => "OLEFRAME",
            ExtendedEntityData::LayoutPrintConfig(_) => "LAYOUTPRINTCONFIG",
            ExtendedEntityData::Format(_) => "Format",
            ExtendedEntityData::Legacy(LegacyEntityData::Repeat) => "REPEAT",
            ExtendedEntityData::Legacy(LegacyEntityData::EndRepeat { .. }) => "ENDREP",
            ExtendedEntityData::Legacy(LegacyEntityData::Load { .. }) => "LOAD",
            ExtendedEntityData::Legacy(LegacyEntityData::Jump { .. }) => "JUMP",
            ExtendedEntityData::DynamicBlock(data) => {
                data.entity_dxf_name().unwrap_or("DYNAMICBLOCKENTITY")
            }
            ExtendedEntityData::RegisteredClass(data) => &data.dxf_name,
        }
    }
}

impl Entity for ExtendedEntity {
    fn handle(&self) -> Handle {
        self.common.handle
    }

    fn set_handle(&mut self, handle: Handle) {
        self.common.handle = handle;
    }

    fn layer(&self) -> &str {
        &self.common.layer
    }

    fn set_layer(&mut self, layer: String) {
        self.common.layer = layer;
    }

    fn color(&self) -> Color {
        self.common.color
    }

    fn set_color(&mut self, color: Color) {
        self.common.color = color;
    }

    fn line_weight(&self) -> LineWeight {
        self.common.line_weight
    }

    fn set_line_weight(&mut self, weight: LineWeight) {
        self.common.line_weight = weight;
    }

    fn transparency(&self) -> Transparency {
        self.common.transparency
    }

    fn set_transparency(&mut self, transparency: Transparency) {
        self.common.transparency = transparency;
    }

    fn is_invisible(&self) -> bool {
        self.common.invisible
    }

    fn set_invisible(&mut self, invisible: bool) {
        self.common.invisible = invisible;
    }

    fn bounding_box(&self) -> BoundingBox3D {
        let points: Vec<Vector3> = match &self.data {
            ExtendedEntityData::SectionObject(data) => data
                .vertices
                .iter()
                .chain(data.back_line_vertices.iter())
                .copied()
                .collect(),
            ExtendedEntityData::ArcAlignedText(data) => vec![data.center],
            ExtendedEntityData::RemoteText(data) => vec![data.position],
            ExtendedEntityData::GeoPositionMarker(data) => vec![data.position],
            ExtendedEntityData::PointCloud(data) => {
                vec![data.extents_min, data.extents_max]
            }
            ExtendedEntityData::PointCloudEx(data) => {
                vec![data.extents_min, data.extents_max]
            }
            _ => Vec::new(),
        };
        BoundingBox3D::from_points(&points).unwrap_or_default()
    }

    fn translate(&mut self, offset: Vector3) {
        match &mut self.data {
            ExtendedEntityData::SectionObject(data) => {
                for point in &mut data.vertices {
                    *point = *point + offset;
                }
                for point in &mut data.back_line_vertices {
                    *point = *point + offset;
                }
            }
            ExtendedEntityData::ArcAlignedText(data) => data.center = data.center + offset,
            ExtendedEntityData::RemoteText(data) => data.position = data.position + offset,
            ExtendedEntityData::GeoPositionMarker(data) => data.position = data.position + offset,
            ExtendedEntityData::PointCloud(data) => {
                data.origin = data.origin + offset;
                data.extents_min = data.extents_min + offset;
                data.extents_max = data.extents_max + offset;
                data.ucs_origin = data.ucs_origin + offset;
            }
            ExtendedEntityData::PointCloudEx(data) => {
                data.extents_min = data.extents_min + offset;
                data.extents_max = data.extents_max + offset;
                data.ucs_origin = data.ucs_origin + offset;
            }
            _ => {}
        }
    }

    fn entity_type(&self) -> &'static str {
        "EXTENDED_ENTITY"
    }
}