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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
// SPDX-License-Identifier: ISC
use crate::point::Point3;
use crate::value::GroupValue;
use alloc::vec::Vec;
/// A HATCH entity — a filled region with boundary paths.
#[derive(Debug, Clone, Default)]
pub struct Hatch<'a> {
/// Elevation point.
pub elevation: Point3,
/// Pattern name.
///
/// `b"SOLID"` for solid fill, or a named pattern like `b"ANSI31"`.
pub pattern_name: &'a [u8],
/// Solid fill flag.
///
/// | Value | Meaning |
/// |------:|--------------|
/// | 0 | Pattern fill |
/// | 1 | Solid fill |
pub solid_fill: i16,
/// Associativity flag.
pub associative: i16,
/// Hatch style.
///
/// | Value | Meaning |
/// |------:|---------------------|
/// | 0 | Normal (odd parity) |
/// | 1 | Outer |
/// | 2 | Ignore |
pub style: i16,
/// Pattern type.
///
/// | Value | Meaning |
/// |------:|--------------|
/// | 0 | User-defined |
/// | 1 | Predefined |
/// | 2 | Custom |
pub pattern_type: i16,
/// Pattern angle in degrees.
pub pattern_angle: f64,
/// Pattern scale.
pub pattern_scale: f64,
/// Boundary paths defining the hatch fill region.
///
/// Each path is a closed loop. Multiple paths interact according
/// to the hatch `style`: with normal style, alternating paths
/// toggle between filled and unfilled (even-odd rule).
pub boundary_paths: Vec<HatchBoundaryPath>,
/// Pattern definition lines for non-solid hatches.
pub pattern_def_lines: Vec<HatchPatternDefLine>,
/// Seed points.
pub seed_points: Vec<(f64, f64)>,
}
/// A single boundary path within a [`Hatch`].
#[derive(Debug, Clone)]
pub enum HatchBoundaryPath {
/// Polyline boundary.
Polyline(HatchPolylineBoundary),
/// Edge boundary composed of line, arc, ellipse, and/or spline edges.
Edges(Vec<HatchEdge>),
}
/// A polyline boundary path.
#[derive(Debug, Clone, Default)]
pub struct HatchPolylineBoundary {
/// Whether vertices have bulge values.
pub has_bulge: bool,
/// Whether the polyline is closed.
pub is_closed: bool,
/// Vertices as `(x, y, bulge)`.
pub vertices: Vec<(f64, f64, f64)>,
}
/// A single edge within a non-polyline boundary path.
#[derive(Debug, Clone)]
pub enum HatchEdge {
/// Line edge.
Line {
/// Start point.
start: (f64, f64),
/// End point.
end: (f64, f64),
},
/// Circular arc edge.
Arc {
/// Center point.
center: (f64, f64),
/// Radius.
radius: f64,
/// Start angle in degrees.
///
/// When `is_ccw` is false, the DXF stores negated angles.
start_angle: f64,
/// End angle in degrees.
///
/// When `is_ccw` is false, the DXF stores negated angles.
end_angle: f64,
/// Counter-clockwise flag.
///
/// Determines the arc's winding direction in the boundary
/// path. The stored angles may be negated when this is false.
is_ccw: bool,
},
/// Elliptic arc edge.
Ellipse {
/// Center point.
center: (f64, f64),
/// Major axis endpoint relative to center.
major_axis: (f64, f64),
/// Ratio of minor axis to major axis.
minor_axis_ratio: f64,
/// Start angle in degrees.
start_angle: f64,
/// End angle in degrees.
end_angle: f64,
/// Counter-clockwise flag.
is_ccw: bool,
},
/// Spline edge.
Spline {
/// Degree.
degree: i16,
/// Rational flag.
rational: bool,
/// Periodic flag.
periodic: bool,
/// Knot values.
knots: Vec<f64>,
/// Control points as `(x, y, weight)`.
///
/// Weight is 1.0 for non-rational splines.
control_points: Vec<(f64, f64, f64)>,
/// Fit points as `(x, y)`.
fit_points: Vec<(f64, f64)>,
},
}
/// A pattern definition line within a [`Hatch`].
#[derive(Debug, Clone, Default)]
pub struct HatchPatternDefLine {
/// Line angle in degrees.
pub angle: f64,
/// Base point.
pub base: (f64, f64),
/// Offset.
pub offset: (f64, f64),
/// Dash lengths.
///
/// Positive values are dashes, negative values are gaps, and zero
/// is a dot.
pub dashes: Vec<f64>,
}
// --- Parsing ---
/// Internal state for parsing the complex HATCH structure.
#[derive(Default)]
struct ParseState {
// Counts read from the entity to drive the sub-parsers.
num_paths: i32,
paths_read: i32,
// Current boundary path state.
boundary_type: i32,
num_edges: i32,
edges_read: i32,
num_vertices: i32,
// Current edge state.
edge_type: i16,
// Polyline boundary accumulator.
poly_has_bulge: bool,
poly_is_closed: bool,
poly_vertices: Vec<(f64, f64, f64)>,
// Edge boundary accumulator.
edges: Vec<HatchEdge>,
// Current edge field accumulators.
x1: f64,
y1: f64,
x2: f64,
y2: f64,
radius: f64,
start_angle: f64,
end_angle: f64,
minor_ratio: f64,
is_ccw: bool,
// Spline edge accumulators.
spline_degree: i16,
spline_rational: bool,
spline_periodic: bool,
num_knots: i32,
num_control_points: i32,
num_fit_points: i32,
spline_knots: Vec<f64>,
spline_control_points: Vec<(f64, f64, f64)>,
spline_fit_points: Vec<(f64, f64)>,
// Pattern definition lines.
num_def_lines: i32,
def_lines_read: i32,
current_def_line: HatchPatternDefLine,
num_dashes: i32,
// Seed points.
num_seed_points: i32,
// Which sub-parser phase we're in.
phase: ParsePhase,
}
#[derive(Default, Clone, Copy, PartialEq)]
enum ParsePhase {
#[default]
Header,
BoundaryPaths,
AfterPaths,
PatternDefLines,
SeedPoints,
Done,
}
/// Builder that drives the stateful HATCH parser.
pub(crate) struct HatchBuilder<'a> {
pub hatch: Hatch<'a>,
state: ParseState,
}
impl<'a> HatchBuilder<'a> {
pub fn new() -> Self {
Self {
hatch: Hatch::default(),
state: ParseState::default(),
}
}
pub fn feed(&mut self, code: u16, val: &GroupValue<'a>) {
match self.state.phase {
ParsePhase::Header => self.feed_header(code, val),
ParsePhase::BoundaryPaths => self.feed_boundary(code, val),
ParsePhase::AfterPaths => self.feed_after_paths(code, val),
ParsePhase::PatternDefLines => self.feed_pattern_def(code, val),
ParsePhase::SeedPoints => self.feed_seed_points(code, val),
ParsePhase::Done => {}
}
}
fn feed_header(&mut self, code: u16, val: &GroupValue<'a>) {
let s = &mut self.state;
let h = &mut self.hatch;
match code {
2 => {
if let Some(v) = val.as_str_bytes() {
h.pattern_name = v;
}
}
70 => {
if let Some(v) = val.as_i16() {
h.solid_fill = v;
}
}
71 => {
if let Some(v) = val.as_i16() {
h.associative = v;
}
}
91 => {
if let Some(v) = val.as_i32() {
s.num_paths = v;
s.paths_read = 0;
if v > 0 {
s.phase = ParsePhase::BoundaryPaths;
}
}
}
_ => {
if let Some(f) = val.as_f64() {
match code {
10 => h.elevation.x = f,
20 => h.elevation.y = f,
30 => h.elevation.z = f,
52 => h.pattern_angle = f,
41 => h.pattern_scale = f,
_ => {}
}
}
}
}
}
fn feed_boundary(&mut self, code: u16, val: &GroupValue<'a>) {
match code {
92 => {
// New boundary path. Flush any previous one.
self.flush_boundary();
if let Some(v) = val.as_i32() {
self.state.boundary_type = v;
self.state.edges_read = 0;
self.state.num_edges = 0;
self.state.num_vertices = 0;
self.state.poly_has_bulge = false;
self.state.poly_is_closed = false;
self.state.poly_vertices.clear();
self.state.edges.clear();
}
}
// Polyline boundary fields.
72 if self.state.boundary_type & 2 != 0 => {
if let Some(v) = val.as_i16() {
self.state.poly_has_bulge = v != 0;
}
}
73 if self.state.boundary_type & 2 != 0 => {
if let Some(v) = val.as_i16() {
self.state.poly_is_closed = v != 0;
}
}
93 if self.state.boundary_type & 2 != 0 => {
if let Some(v) = val.as_i32() {
self.state.num_vertices = v;
}
}
10 if self.state.boundary_type & 2 != 0 => {
if let Some(f) = val.as_f64() {
self.state.poly_vertices.push((f, 0.0, 0.0));
}
}
20 if self.state.boundary_type & 2 != 0 => {
if let Some(f) = val.as_f64() {
if let Some(last) = self.state.poly_vertices.last_mut() {
last.1 = f;
}
}
}
42 if self.state.boundary_type & 2 != 0 => {
if let Some(f) = val.as_f64() {
if let Some(last) = self.state.poly_vertices.last_mut() {
last.2 = f;
}
}
}
// Edge boundary fields.
93 if self.state.boundary_type & 2 == 0 => {
if let Some(v) = val.as_i32() {
self.state.num_edges = v;
self.state.edges_read = 0;
}
}
72 if self.state.boundary_type & 2 == 0 => {
// New edge type — flush previous edge.
self.flush_edge();
if let Some(v) = val.as_i16() {
self.state.edge_type = v;
self.state.x1 = 0.0;
self.state.y1 = 0.0;
self.state.x2 = 0.0;
self.state.y2 = 0.0;
self.state.radius = 0.0;
self.state.start_angle = 0.0;
self.state.end_angle = 0.0;
self.state.minor_ratio = 1.0;
self.state.is_ccw = false;
self.state.spline_degree = 0;
self.state.spline_rational = false;
self.state.spline_periodic = false;
self.state.num_knots = 0;
self.state.num_control_points = 0;
self.state.num_fit_points = 0;
self.state.spline_knots.clear();
self.state.spline_control_points.clear();
self.state.spline_fit_points.clear();
}
}
// Edge data codes — dispatched by edge_type.
10 if self.state.boundary_type & 2 == 0 => {
if let Some(f) = val.as_f64() {
match self.state.edge_type {
1..=3 => self.state.x1 = f,
4 => {
self.state.spline_control_points.push((f, 0.0, 1.0));
}
_ => {}
}
}
}
20 if self.state.boundary_type & 2 == 0 => {
if let Some(f) = val.as_f64() {
match self.state.edge_type {
1..=3 => self.state.y1 = f,
4 => {
if let Some(last) = self.state.spline_control_points.last_mut() {
last.1 = f;
}
}
_ => {}
}
}
}
11 if self.state.boundary_type & 2 == 0 => {
if let Some(f) = val.as_f64() {
match self.state.edge_type {
1 | 3 => self.state.x2 = f,
4 => self.state.spline_fit_points.push((f, 0.0)),
_ => {}
}
}
}
21 if self.state.boundary_type & 2 == 0 => {
if let Some(f) = val.as_f64() {
match self.state.edge_type {
1 | 3 => self.state.y2 = f,
4 => {
if let Some(last) = self.state.spline_fit_points.last_mut() {
last.1 = f;
}
}
_ => {}
}
}
}
40 if self.state.boundary_type & 2 == 0 => {
if let Some(f) = val.as_f64() {
match self.state.edge_type {
2 => self.state.radius = f,
3 => self.state.minor_ratio = f,
4 => self.state.spline_knots.push(f),
_ => {}
}
}
}
42 if self.state.boundary_type & 2 == 0 => {
if let Some(f) = val.as_f64() {
if self.state.edge_type == 4 {
if let Some(last) = self.state.spline_control_points.last_mut() {
last.2 = f;
}
}
}
}
50 if self.state.boundary_type & 2 == 0 => {
if let Some(f) = val.as_f64() {
self.state.start_angle = f;
}
}
51 if self.state.boundary_type & 2 == 0 => {
if let Some(f) = val.as_f64() {
self.state.end_angle = f;
}
}
73 if self.state.boundary_type & 2 == 0 => {
if let Some(v) = val.as_i16() {
match self.state.edge_type {
2 | 3 => self.state.is_ccw = v != 0,
4 => self.state.spline_periodic = v != 0,
_ => {}
}
}
}
74 if self.state.boundary_type & 2 == 0 => {
if let Some(v) = val.as_i16() {
if self.state.edge_type == 4 {
self.state.num_knots = v as i32;
}
}
}
94 if self.state.boundary_type & 2 == 0 => {
if let Some(v) = val.as_i32() {
if self.state.edge_type == 4 {
self.state.num_control_points = v;
}
}
}
95 if self.state.boundary_type & 2 == 0 => {
if let Some(v) = val.as_i32() {
if self.state.edge_type == 4 {
self.state.num_fit_points = v;
}
}
}
96 if self.state.boundary_type & 2 == 0 => {
// Spline degree is in group 94 for spline edges — but
// group 96 can hold num_seed_points when we're past boundaries.
// This is tricky; 96 is only used for seed point count in the
// after-paths phase, not during boundary parsing. Ignore here.
}
// Source boundary objects (group 330) — skip.
97 => {
// Number of source boundary objects. Flush current boundary
// and transition to next path or to after-paths.
self.flush_edge();
self.flush_boundary();
self.state.paths_read += 1;
if self.state.paths_read >= self.state.num_paths {
self.state.phase = ParsePhase::AfterPaths;
}
}
// Spline edge degree and rational flag.
94 if self.state.boundary_type & 2 == 0 && self.state.edge_type == 4 => {
// Already handled above.
}
// Post-boundary group codes: flush remaining boundary and transition.
75 | 76 | 47 | 78 | 98 | 450 | 451 | 452 | 453 | 460 | 461 | 462 | 463 | 470 => {
self.flush_edge();
self.flush_boundary();
self.state.phase = ParsePhase::AfterPaths;
self.feed_after_paths(code, val);
}
_ => {
// Spline degree (94) already handled.
// Rational flag for spline edges comes in group 73 (handled above).
}
}
}
fn flush_edge(&mut self) {
let s = &mut self.state;
let edge = match s.edge_type {
1 => Some(HatchEdge::Line {
start: (s.x1, s.y1),
end: (s.x2, s.y2),
}),
2 => Some(HatchEdge::Arc {
center: (s.x1, s.y1),
radius: s.radius,
start_angle: s.start_angle,
end_angle: s.end_angle,
is_ccw: s.is_ccw,
}),
3 => Some(HatchEdge::Ellipse {
center: (s.x1, s.y1),
major_axis: (s.x2, s.y2),
minor_axis_ratio: s.minor_ratio,
start_angle: s.start_angle,
end_angle: s.end_angle,
is_ccw: s.is_ccw,
}),
4 => Some(HatchEdge::Spline {
degree: s.spline_degree,
rational: s.spline_rational,
periodic: s.spline_periodic,
knots: core::mem::take(&mut s.spline_knots),
control_points: core::mem::take(&mut s.spline_control_points),
fit_points: core::mem::take(&mut s.spline_fit_points),
}),
_ => None,
};
if let Some(e) = edge {
s.edges.push(e);
s.edges_read += 1;
}
s.edge_type = 0;
}
fn flush_boundary(&mut self) {
let s = &mut self.state;
let h = &mut self.hatch;
if s.boundary_type & 2 != 0 {
if !s.poly_vertices.is_empty() {
h.boundary_paths
.push(HatchBoundaryPath::Polyline(HatchPolylineBoundary {
has_bulge: s.poly_has_bulge,
is_closed: s.poly_is_closed,
vertices: core::mem::take(&mut s.poly_vertices),
}));
}
} else if !s.edges.is_empty() {
h.boundary_paths
.push(HatchBoundaryPath::Edges(core::mem::take(&mut s.edges)));
}
s.boundary_type = 0;
}
fn feed_after_paths(&mut self, code: u16, val: &GroupValue<'a>) {
let s = &mut self.state;
let h = &mut self.hatch;
match code {
75 => {
if let Some(v) = val.as_i16() {
h.style = v;
}
}
76 => {
if let Some(v) = val.as_i16() {
h.pattern_type = v;
}
}
52 => {
if let Some(f) = val.as_f64() {
h.pattern_angle = f;
}
}
41 => {
if let Some(f) = val.as_f64() {
h.pattern_scale = f;
}
}
78 => {
if let Some(v) = val.as_i32() {
s.num_def_lines = v;
s.def_lines_read = 0;
if v > 0 {
s.phase = ParsePhase::PatternDefLines;
}
}
}
47 => {} // pixel size, ignore
98 => {
if let Some(v) = val.as_i32() {
s.num_seed_points = v;
if v > 0 {
s.phase = ParsePhase::SeedPoints;
} else {
s.phase = ParsePhase::Done;
}
}
}
_ => {}
}
}
fn feed_pattern_def(&mut self, code: u16, val: &GroupValue<'a>) {
let s = &mut self.state;
let h = &mut self.hatch;
match code {
53 => {
// New pattern def line angle — flush previous.
if s.def_lines_read > 0 {
h.pattern_def_lines
.push(core::mem::take(&mut s.current_def_line));
}
s.def_lines_read += 1;
s.current_def_line = HatchPatternDefLine::default();
if let Some(f) = val.as_f64() {
s.current_def_line.angle = f;
}
}
43 => {
if let Some(f) = val.as_f64() {
s.current_def_line.base.0 = f;
}
}
44 => {
if let Some(f) = val.as_f64() {
s.current_def_line.base.1 = f;
}
}
45 => {
if let Some(f) = val.as_f64() {
s.current_def_line.offset.0 = f;
}
}
46 => {
if let Some(f) = val.as_f64() {
s.current_def_line.offset.1 = f;
}
}
79 => {
if let Some(v) = val.as_i16() {
s.num_dashes = v as i32;
}
}
49 => {
if let Some(f) = val.as_f64() {
s.current_def_line.dashes.push(f);
}
}
98 => {
// Seed point count — flush last def line and transition.
if s.def_lines_read > 0 {
h.pattern_def_lines
.push(core::mem::take(&mut s.current_def_line));
}
if let Some(v) = val.as_i32() {
s.num_seed_points = v;
if v > 0 {
s.phase = ParsePhase::SeedPoints;
} else {
s.phase = ParsePhase::Done;
}
}
}
_ => {}
}
}
fn feed_seed_points(&mut self, code: u16, val: &GroupValue<'a>) {
match code {
10 => {
if let Some(f) = val.as_f64() {
self.hatch.seed_points.push((f, 0.0));
}
}
20 => {
if let Some(f) = val.as_f64() {
if let Some(last) = self.hatch.seed_points.last_mut() {
last.1 = f;
}
}
}
_ => {}
}
}
}