acadrust 0.3.3

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
//! Polyline entities (2D and 3D polylines)

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

/// Polyline flags (matches DXF group code 70)
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct PolylineFlags {
    bits: u16,
}

impl PolylineFlags {
    pub const CLOSED: Self = Self { bits: 1 };
    pub const CURVE_FIT: Self = Self { bits: 2 };
    pub const SPLINE_FIT: Self = Self { bits: 4 };
    pub const POLYLINE_3D: Self = Self { bits: 8 };
    pub const POLYGON_MESH: Self = Self { bits: 16 };
    pub const CLOSED_N: Self = Self { bits: 32 };
    pub const POLYFACE_MESH: Self = Self { bits: 64 };
    pub const LINETYPE_CONTINUOUS: Self = Self { bits: 128 };

    pub fn new() -> Self {
        Self { bits: 0 }
    }
    
    pub fn from_bits(bits: u16) -> Self {
        Self { bits }
    }
    
    pub fn bits(&self) -> u16 {
        self.bits
    }

    pub fn is_closed(&self) -> bool {
        self.bits & 1 != 0
    }

    pub fn is_3d(&self) -> bool {
        self.bits & 8 != 0
    }
    
    pub fn is_spline_fit(&self) -> bool {
        self.bits & 4 != 0
    }
    
    pub fn set_closed(&mut self, value: bool) {
        if value {
            self.bits |= 1;
        } else {
            self.bits &= !1;
        }
    }
    
    pub fn set_3d(&mut self, value: bool) {
        if value {
            self.bits |= 8;
        } else {
            self.bits &= !8;
        }
    }
}

impl std::ops::BitOr for PolylineFlags {
    type Output = Self;
    fn bitor(self, rhs: Self) -> Self::Output {
        Self { bits: self.bits | rhs.bits }
    }
}

impl std::ops::BitOrAssign for PolylineFlags {
    fn bitor_assign(&mut self, rhs: Self) {
        self.bits |= rhs.bits;
    }
}

/// Vertex flags (matches DXF group code 70)
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct VertexFlags {
    bits: u8,
}

impl VertexFlags {
    pub const EXTRA_VERTEX: Self = Self { bits: 1 };
    pub const CURVE_FIT_TANGENT: Self = Self { bits: 2 };
    pub const SPLINE_VERTEX: Self = Self { bits: 8 };
    pub const SPLINE_CONTROL: Self = Self { bits: 16 };
    pub const POLYLINE_3D: Self = Self { bits: 32 };
    pub const POLYGON_MESH: Self = Self { bits: 64 };
    pub const POLYFACE_FACE: Self = Self { bits: 128 };

    pub fn new() -> Self {
        Self { bits: 0 }
    }
    
    pub fn from_bits(bits: u8) -> Self {
        Self { bits }
    }
    
    pub fn bits(&self) -> u8 {
        self.bits
    }
}

/// Smooth surface type (matches DXF group code 75)
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum SmoothSurfaceType {
    #[default]
    None = 0,
    QuadraticBSpline = 5,
    CubicBSpline = 6,
    Bezier = 8,
}

impl From<i16> for SmoothSurfaceType {
    fn from(value: i16) -> Self {
        match value {
            5 => SmoothSurfaceType::QuadraticBSpline,
            6 => SmoothSurfaceType::CubicBSpline,
            8 => SmoothSurfaceType::Bezier,
            _ => SmoothSurfaceType::None,
        }
    }
}

/// A vertex in a 2D polyline
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Vertex2D {
    /// Location of the vertex (X, Y in OCS, Z is elevation)
    pub location: Vector3,
    /// Vertex flags
    pub flags: VertexFlags,
    /// Start width (0 = use default)
    pub start_width: f64,
    /// End width (0 = use default)
    pub end_width: f64,
    /// Bulge (0 = straight segment, <0 = clockwise arc, >0 = counter-clockwise arc)
    pub bulge: f64,
    /// Curve fit tangent direction
    pub curve_tangent: f64,
    /// Vertex ID (R2010+)
    pub id: i32,
}

impl Vertex2D {
    pub fn new(location: Vector3) -> Self {
        Self {
            location,
            flags: VertexFlags::new(),
            start_width: 0.0,
            end_width: 0.0,
            bulge: 0.0,
            curve_tangent: 0.0,
            id: 0,
        }
    }
    
    pub fn from_point(point: Vector2) -> Self {
        Self::new(Vector3::new(point.x, point.y, 0.0))
    }
    
    pub fn with_bulge(mut self, bulge: f64) -> Self {
        self.bulge = bulge;
        self
    }
    
    pub fn with_width(mut self, start_width: f64, end_width: f64) -> Self {
        self.start_width = start_width;
        self.end_width = end_width;
        self
    }
}

/// A vertex in a 3D polyline
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Vertex3D {
    /// Location of the vertex
    pub location: Vector3,
    /// Vertex flags
    pub flags: VertexFlags,
}

impl Vertex3D {
    /// Create a new vertex
    pub fn new(location: Vector3) -> Self {
        Self {
            location,
            flags: VertexFlags::new(),
        }
    }

    /// Create a vertex from coordinates
    pub fn from_coords(x: f64, y: f64, z: f64) -> Self {
        Vertex3D::new(Vector3::new(x, y, z))
    }
}

/// A 2D polyline entity (heavy polyline with vertices)
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Polyline2D {
    /// Common entity data
    pub common: EntityCommon,
    /// Polyline flags
    pub flags: PolylineFlags,
    /// Smooth surface type
    pub smooth_surface: SmoothSurfaceType,
    /// Default start width
    pub start_width: f64,
    /// Default end width
    pub end_width: f64,
    /// Thickness (extrusion height)
    pub thickness: f64,
    /// Elevation (Z coordinate in OCS)
    pub elevation: f64,
    /// Normal vector (extrusion direction)
    pub normal: Vector3,
    /// Vertices
    pub vertices: Vec<Vertex2D>,
}

impl Polyline2D {
    pub fn new() -> Self {
        Self {
            common: EntityCommon::new(),
            flags: PolylineFlags::new(),
            smooth_surface: SmoothSurfaceType::None,
            start_width: 0.0,
            end_width: 0.0,
            thickness: 0.0,
            elevation: 0.0,
            normal: Vector3::new(0.0, 0.0, 1.0),
            vertices: Vec::new(),
        }
    }
    
    pub fn add_vertex(&mut self, vertex: Vertex2D) {
        self.vertices.push(vertex);
    }
    
    pub fn is_closed(&self) -> bool {
        self.flags.is_closed()
    }
    
    pub fn close(&mut self) {
        self.flags.set_closed(true);
    }
}

impl Default for Polyline2D {
    fn default() -> Self {
        Self::new()
    }
}

/// A 3D polyline entity
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Polyline {
    /// Common entity data
    pub common: EntityCommon,
    /// Polyline flags
    pub flags: PolylineFlags,
    /// Vertices of the polyline
    pub vertices: Vec<Vertex3D>,
}

impl Polyline {
    /// Create a new empty polyline
    pub fn new() -> Self {
        let mut flags = PolylineFlags::new();
        flags.set_3d(true);
        Polyline {
            common: EntityCommon::new(),
            flags,
            vertices: Vec::new(),
        }
    }

    /// Create a polyline from a list of points
    pub fn from_points(points: Vec<Vector3>) -> Self {
        Polyline {
            vertices: points.into_iter().map(Vertex3D::new).collect(),
            ..Self::new()
        }
    }

    /// Add a vertex to the polyline
    pub fn add_vertex(&mut self, vertex: Vertex3D) {
        self.vertices.push(vertex);
    }

    /// Add a point to the polyline
    pub fn add_point(&mut self, point: Vector3) {
        self.vertices.push(Vertex3D::new(point));
    }

    /// Get the number of vertices
    pub fn vertex_count(&self) -> usize {
        self.vertices.len()
    }

    /// Check if closed
    pub fn is_closed(&self) -> bool {
        self.flags.is_closed()
    }

    /// Close the polyline
    pub fn close(&mut self) {
        self.flags.set_closed(true);
    }
}

impl Default for Polyline {
    fn default() -> Self {
        Self::new()
    }
}

impl Entity for Polyline2D {
    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 {
        if self.vertices.is_empty() {
            return BoundingBox3D::from_point(Vector3::ZERO);
        }

        let points: Vec<Vector3> = self.vertices.iter().map(|v| v.location).collect();
        BoundingBox3D::from_points(&points).unwrap()
    }

    fn translate(&mut self, offset: Vector3) {
        super::translate::translate_polyline2d(self, offset);
    }

    fn entity_type(&self) -> &'static str {
        "POLYLINE"
    }
    
    fn apply_transform(&mut self, transform: &crate::types::Transform) {
        super::transform::transform_polyline2d(self, transform);
    }
    
    fn apply_mirror(&mut self, transform: &crate::types::Transform) {
        super::mirror::mirror_polyline2d(self, transform);
    }
}

impl Entity for Polyline {
    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 {
        if self.vertices.is_empty() {
            return BoundingBox3D::from_point(Vector3::ZERO);
        }

        let points: Vec<Vector3> = self.vertices.iter().map(|v| v.location).collect();
        BoundingBox3D::from_points(&points).unwrap()
    }

    fn translate(&mut self, offset: Vector3) {
        super::translate::translate_polyline(self, offset);
    }

    fn entity_type(&self) -> &'static str {
        "POLYLINE"
    }
    
    fn apply_transform(&mut self, transform: &crate::types::Transform) {
        super::transform::transform_polyline(self, transform);
    }
}