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
// SPDX-License-Identifier: ISC
mod arc;
mod attdef;
mod attrib;
mod circle;
mod dimension;
mod ellipse;
mod face3d;
pub(crate) mod hatch;
mod insert;
mod leader;
mod line;
mod lwpolyline;
mod mtext;
mod point;
mod polyline;
mod solid;
mod spline;
mod text;
mod trace;
mod wipeout;
pub use arc::Arc;
pub use attdef::AttDef;
pub use attrib::Attrib;
pub use circle::Circle;
pub use dimension::Dimension;
pub use ellipse::Ellipse;
pub use face3d::Face3d;
pub use hatch::{Hatch, HatchBoundaryPath, HatchEdge, HatchPatternDefLine, HatchPolylineBoundary};
pub use insert::Insert;
pub use leader::Leader;
pub use line::Line;
pub use lwpolyline::{LwPolyline, LwPolylineVertex};
pub use mtext::MText;
pub use point::DxfPoint;
pub use polyline::{Polyline, Vertex};
pub use solid::Solid;
pub use spline::Spline;
pub use text::Text;
pub use trace::Trace;
pub use wipeout::Wipeout;
use crate::point::Point3;
use crate::value::GroupValue;
/// Common properties shared by all DXF entities.
#[derive(Debug, Clone, Default)]
pub struct EntityCommon<'a> {
/// Entity handle as a hex string.
///
/// Handles are unique identifiers assigned by AutoCAD. They are
/// persistent across save/reload and are used for cross-references
/// between entities (e.g. DIMENSION → anonymous block).
pub handle: &'a [u8],
/// Layer name.
pub layer: &'a [u8],
/// Linetype name.
///
/// Empty means not explicitly set. `b"BYLAYER"` inherits from the
/// layer, `b"BYBLOCK"` inherits from the containing block reference.
pub linetype_name: &'a [u8],
/// Color number.
///
/// | Value | Meaning |
/// |------:|-----------------|
/// | 0 | BYBLOCK |
/// | 1–255 | ACI color index |
/// | 256 | BYLAYER |
///
/// When `color_24_bit` is also set, the relationship between the
/// two depends on the application. Some writers set both; consumers
/// typically prefer `color` when it is in the 1–255 range.
pub color: i16,
/// Lineweight enum value.
///
/// | Value | Meaning |
/// |------:|-------------------------------------|
/// | -2 | BYLAYER |
/// | -1 | BYBLOCK |
/// | 0+ | Explicit weight in hundredths of mm |
///
/// A value of 0 does not mean invisible — lineweights are intended
/// to be clamped to the application's minimum and maximum plotting
/// widths (typically 1 pixel minimum on screen).
pub lineweight: i16,
/// Visibility flag.
///
/// `true` when the entity is visible. Invisible entities are still
/// part of the drawing but should not be rendered.
pub is_visible: bool,
/// 24-bit true color.
///
/// RGB packed as `0x00RRGGBB`. When nonzero, this overrides `color`
/// for rendering unless `color` is in the ACI 1–255 range.
/// Zero means the true color is not set.
pub color_24_bit: i32,
/// Transparency value.
///
/// Encoded as `0x020000TT` where `TT` is the transparency
/// percentage (0 = fully opaque, 100 = fully transparent).
/// Zero means transparency is not set (fully opaque).
pub transparency: i32,
/// Extrusion direction (OCS normal).
///
/// Defines the object coordinate system (OCS) for this entity.
/// Most entities use the default `(0, 0, 1)` which means their
/// coordinates are in the world XY plane. Non-default values
/// require an OCS-to-WCS transformation to render correctly.
pub extrusion: Point3,
}
impl<'a> EntityCommon<'a> {
/// Creates a new `EntityCommon` with default values.
pub fn new() -> Self {
Self {
color: 256, // BYLAYER
lineweight: -2, // BYLAYER
is_visible: true,
extrusion: Point3 {
x: 0.0,
y: 0.0,
z: 1.0,
},
..Default::default()
}
}
/// Feeds a single group code/value pair. Returns `true` if consumed.
pub(crate) fn feed(&mut self, code: u16, val: &GroupValue<'a>) -> bool {
match code {
5 => {
if let Some(s) = val.as_str_bytes() {
self.handle = s;
}
true
}
6 => {
if let Some(s) = val.as_str_bytes() {
self.linetype_name = s;
}
true
}
8 => {
if let Some(s) = val.as_str_bytes() {
self.layer = s;
}
true
}
62 => {
if let Some(v) = val.as_i16() {
self.color = v;
}
true
}
370 => {
if let Some(v) = val.as_i16() {
self.lineweight = v;
}
true
}
60 => {
if let Some(v) = val.as_i16() {
self.is_visible = v == 0;
}
true
}
420 => {
if let Some(v) = val.as_i32() {
self.color_24_bit = v;
}
true
}
440 => {
if let Some(v) = val.as_i32() {
self.transparency = v;
}
true
}
210 => {
if let Some(v) = val.as_f64() {
self.extrusion.x = v;
}
true
}
220 => {
if let Some(v) = val.as_f64() {
self.extrusion.y = v;
}
true
}
230 => {
if let Some(v) = val.as_f64() {
self.extrusion.z = v;
}
true
}
// Common metadata codes that aren't entity fields. Consumed to
// avoid passing to entity-specific feed. Note: group 100 (subclass
// markers) is NOT consumed here — some entity types (e.g. Attrib)
// need to track subclass boundaries.
330 | 102 | 360 => true,
_ => false,
}
}
}
/// A parsed DXF entity.
#[derive(Debug, Clone)]
pub enum Entity<'a> {
/// A LINE entity.
Line(EntityCommon<'a>, Line),
/// A CIRCLE entity.
Circle(EntityCommon<'a>, Circle),
/// An ARC entity.
Arc(EntityCommon<'a>, Arc),
/// An ELLIPSE entity.
Ellipse(EntityCommon<'a>, Ellipse),
/// An LWPOLYLINE entity.
LwPolyline(EntityCommon<'a>, LwPolyline),
/// A POLYLINE entity.
Polyline(EntityCommon<'a>, Polyline),
/// A SPLINE entity.
Spline(EntityCommon<'a>, Spline),
/// A SOLID entity.
Solid(EntityCommon<'a>, Solid),
/// A TRACE entity.
Trace(EntityCommon<'a>, Trace),
/// A 3DFACE entity.
Face3d(EntityCommon<'a>, Face3d),
/// A POINT entity.
Point(EntityCommon<'a>, DxfPoint),
/// A TEXT entity.
Text(EntityCommon<'a>, Text<'a>),
/// An MTEXT entity.
MText(EntityCommon<'a>, MText<'a>),
/// An INSERT entity.
Insert(EntityCommon<'a>, Insert<'a>),
/// A DIMENSION entity.
Dimension(EntityCommon<'a>, Dimension<'a>),
/// A LEADER entity.
Leader(EntityCommon<'a>, Leader<'a>),
/// A HATCH entity.
Hatch(EntityCommon<'a>, Hatch<'a>),
/// A WIPEOUT entity.
Wipeout(EntityCommon<'a>, Wipeout),
/// An ATTRIB entity.
Attrib(EntityCommon<'a>, Attrib<'a>),
/// An ATTDEF entity.
AttDef(EntityCommon<'a>, AttDef<'a>),
/// An unrecognized entity type.
///
/// The second field is the DXF type name.
Unknown(EntityCommon<'a>, &'a [u8]),
}
impl<'a> Entity<'a> {
/// Returns a reference to the common properties.
pub fn common(&self) -> &EntityCommon<'a> {
match self {
Entity::Line(c, _)
| Entity::Circle(c, _)
| Entity::Arc(c, _)
| Entity::Ellipse(c, _)
| Entity::LwPolyline(c, _)
| Entity::Polyline(c, _)
| Entity::Spline(c, _)
| Entity::Solid(c, _)
| Entity::Trace(c, _)
| Entity::Face3d(c, _)
| Entity::Point(c, _)
| Entity::Text(c, _)
| Entity::MText(c, _)
| Entity::Insert(c, _)
| Entity::Dimension(c, _)
| Entity::Leader(c, _)
| Entity::Hatch(c, _)
| Entity::Wipeout(c, _)
| Entity::Attrib(c, _)
| Entity::AttDef(c, _)
| Entity::Unknown(c, _) => c,
}
}
}