vcad 0.1.0

Parametric CAD in Rust — CSG modeling with multi-format export
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
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
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
//! DXF export for 2D laser cutting profiles.
//!
//! Exports flat profiles as DXF R12 format for SendCutSend and other
//! laser cutting services. Supports:
//! - Cut lines (layer "0" - default)
//! - Bend lines (layer "BEND" - for forming services)

use std::fs::File;
use std::io::{BufWriter, Write};
use std::path::Path;

/// A 2D point for DXF export.
#[derive(Debug, Clone, Copy)]
pub struct Point2D {
    /// X coordinate.
    pub x: f64,
    /// Y coordinate.
    pub y: f64,
}

impl Point2D {
    /// Create a new 2D point.
    pub fn new(x: f64, y: f64) -> Self {
        Self { x, y }
    }
}

/// A 2D shape for DXF export.
#[derive(Debug, Clone)]
#[allow(missing_docs)]
pub enum Shape2D {
    /// Rectangular outline.
    Rectangle {
        /// Width in drawing units.
        width: f64,
        /// Height in drawing units.
        height: f64,
        /// Center position.
        center: Point2D,
    },
    /// Circle (for holes).
    Circle {
        /// Center position.
        center: Point2D,
        /// Circle radius.
        radius: f64,
    },
    /// Rounded rectangle (for slots with corner radii).
    RoundedRectangle {
        /// Overall width.
        width: f64,
        /// Overall height.
        height: f64,
        /// Center position.
        center: Point2D,
        /// Fillet radius for corners.
        corner_radius: f64,
    },
    /// Line segment (for bend lines, etc.).
    Line {
        /// Start point.
        start: Point2D,
        /// End point.
        end: Point2D,
        /// DXF layer name (e.g. `"0"` or `"BEND"`).
        layer: String,
    },
    /// Slot (stadium shape — rectangle with semicircular ends).
    Slot {
        /// Overall width.
        width: f64,
        /// Overall height.
        height: f64,
        /// Center position.
        center: Point2D,
    },
    /// Arbitrary closed polyline (for complex profiles).
    Polyline {
        /// Ordered list of vertices.
        points: Vec<Point2D>,
        /// Whether the polyline forms a closed loop.
        closed: bool,
    },
    /// Circular arc.
    Arc {
        /// Arc center.
        center: Point2D,
        /// Arc radius.
        radius: f64,
        /// Start angle in degrees.
        start_angle: f64,
        /// End angle in degrees.
        end_angle: f64,
    },
}

/// DXF document builder.
///
/// Accumulates 2D shapes and exports them as DXF R12 for laser cutting services.
pub struct DxfDocument {
    shapes: Vec<Shape2D>,
}

impl DxfDocument {
    /// Create a new empty DXF document.
    pub fn new() -> Self {
        Self { shapes: Vec::new() }
    }

    /// Add an arbitrary [`Shape2D`] to the document.
    pub fn add_shape(&mut self, shape: Shape2D) {
        self.shapes.push(shape);
    }

    /// Add a rectangular outline
    pub fn add_rectangle(&mut self, width: f64, height: f64, cx: f64, cy: f64) {
        self.shapes.push(Shape2D::Rectangle {
            width,
            height,
            center: Point2D::new(cx, cy),
        });
    }

    /// Add a circle (for holes)
    pub fn add_circle(&mut self, cx: f64, cy: f64, radius: f64) {
        self.shapes.push(Shape2D::Circle {
            center: Point2D::new(cx, cy),
            radius,
        });
    }

    /// Add a rounded rectangle (for slots)
    pub fn add_rounded_rectangle(
        &mut self,
        width: f64,
        height: f64,
        cx: f64,
        cy: f64,
        corner_radius: f64,
    ) {
        self.shapes.push(Shape2D::RoundedRectangle {
            width,
            height,
            center: Point2D::new(cx, cy),
            corner_radius,
        });
    }

    /// Add a line segment on the default layer
    pub fn add_line(&mut self, x1: f64, y1: f64, x2: f64, y2: f64) {
        self.shapes.push(Shape2D::Line {
            start: Point2D::new(x1, y1),
            end: Point2D::new(x2, y2),
            layer: "0".to_string(),
        });
    }

    /// Add a bend line (on BEND layer for forming services)
    pub fn add_bend_line(&mut self, x1: f64, y1: f64, x2: f64, y2: f64) {
        self.shapes.push(Shape2D::Line {
            start: Point2D::new(x1, y1),
            end: Point2D::new(x2, y2),
            layer: "BEND".to_string(),
        });
    }

    /// Add a slot (stadium shape - rectangle with semicircular ends)
    /// Used for louver vents and elongated cutouts
    pub fn add_slot(&mut self, width: f64, height: f64, cx: f64, cy: f64) {
        self.shapes.push(Shape2D::Slot {
            width,
            height,
            center: Point2D::new(cx, cy),
        });
    }

    /// Add an arbitrary closed polyline (for complex profiles)
    pub fn add_polyline(&mut self, points: Vec<(f64, f64)>, closed: bool) {
        self.shapes.push(Shape2D::Polyline {
            points: points
                .into_iter()
                .map(|(x, y)| Point2D::new(x, y))
                .collect(),
            closed,
        });
    }

    /// Add an arc (for rounded corners)
    pub fn add_arc(&mut self, cx: f64, cy: f64, radius: f64, start_angle: f64, end_angle: f64) {
        self.shapes.push(Shape2D::Arc {
            center: Point2D::new(cx, cy),
            radius,
            start_angle,
            end_angle,
        });
    }

    /// Generate knuckle hinge tab profile points along an edge
    /// Returns points for a profile with tabs extending in the +Y direction
    /// `edge_y` - Y coordinate of the edge
    /// `edge_x_start` - X coordinate of edge start
    /// `edge_x_end` - X coordinate of edge end
    /// `tab_width` - Width of each tab
    /// `tab_height` - Height of each tab (how far they extend)
    /// `num_tabs` - Number of tabs
    /// `offset` - If true, offset tabs (for mating piece)
    pub fn knuckle_tab_edge_points(
        edge_y: f64,
        edge_x_start: f64,
        edge_x_end: f64,
        _tab_width: f64,
        tab_height: f64,
        num_tabs: usize,
        offset: bool,
    ) -> Vec<(f64, f64)> {
        let mut points = Vec::new();
        let edge_length = edge_x_end - edge_x_start;
        let total_tabs = num_tabs * 2 - 1; // tabs + gaps
        let segment_width = edge_length / total_tabs as f64;

        // Start from left edge
        let mut x = edge_x_start;
        let start_with_tab = !offset;

        for i in 0..total_tabs {
            let is_tab = if start_with_tab {
                i % 2 == 0
            } else {
                i % 2 == 1
            };

            if is_tab {
                // Tab: go up, across, down
                points.push((x, edge_y));
                points.push((x, edge_y + tab_height));
                points.push((x + segment_width, edge_y + tab_height));
                points.push((x + segment_width, edge_y));
            } else {
                // Gap: just the edge (will connect to next segment)
                points.push((x, edge_y));
                points.push((x + segment_width, edge_y));
            }
            x += segment_width;
        }

        // Remove duplicate points where segments meet
        let mut deduped: Vec<(f64, f64)> = Vec::new();
        for p in points {
            if deduped.is_empty() || {
                let last = deduped.last().unwrap();
                (last.0 - p.0).abs() > 0.001 || (last.1 - p.1).abs() > 0.001
            } {
                deduped.push(p);
            }
        }

        deduped
    }

    /// Export to DXF file
    pub fn export(&self, path: impl AsRef<Path>) -> std::io::Result<()> {
        let file = File::create(path)?;
        let mut writer = BufWriter::new(file);

        // DXF Header
        writeln!(writer, "0")?;
        writeln!(writer, "SECTION")?;
        writeln!(writer, "2")?;
        writeln!(writer, "HEADER")?;
        writeln!(writer, "9")?;
        writeln!(writer, "$ACADVER")?;
        writeln!(writer, "1")?;
        writeln!(writer, "AC1009")?; // DXF R12
        writeln!(writer, "9")?;
        writeln!(writer, "$INSUNITS")?;
        writeln!(writer, "70")?;
        writeln!(writer, "4")?; // Millimeters
        writeln!(writer, "0")?;
        writeln!(writer, "ENDSEC")?;

        // Tables section (minimal)
        writeln!(writer, "0")?;
        writeln!(writer, "SECTION")?;
        writeln!(writer, "2")?;
        writeln!(writer, "TABLES")?;
        writeln!(writer, "0")?;
        writeln!(writer, "ENDSEC")?;

        // Entities section
        writeln!(writer, "0")?;
        writeln!(writer, "SECTION")?;
        writeln!(writer, "2")?;
        writeln!(writer, "ENTITIES")?;

        for shape in &self.shapes {
            match shape {
                Shape2D::Rectangle {
                    width,
                    height,
                    center,
                } => {
                    self.write_rectangle(&mut writer, *width, *height, center)?;
                }
                Shape2D::Circle { center, radius } => {
                    self.write_circle(&mut writer, center, *radius)?;
                }
                Shape2D::RoundedRectangle {
                    width,
                    height,
                    center,
                    corner_radius,
                } => {
                    self.write_rounded_rectangle(
                        &mut writer,
                        *width,
                        *height,
                        center,
                        *corner_radius,
                    )?;
                }
                Shape2D::Line { start, end, layer } => {
                    self.write_line(&mut writer, start, end, layer)?;
                }
                Shape2D::Slot {
                    width,
                    height,
                    center,
                } => {
                    self.write_slot(&mut writer, *width, *height, center)?;
                }
                Shape2D::Polyline { points, closed } => {
                    self.write_polyline(&mut writer, points, *closed)?;
                }
                Shape2D::Arc {
                    center,
                    radius,
                    start_angle,
                    end_angle,
                } => {
                    self.write_arc(&mut writer, center, *radius, *start_angle, *end_angle)?;
                }
            }
        }

        writeln!(writer, "0")?;
        writeln!(writer, "ENDSEC")?;

        // End of file
        writeln!(writer, "0")?;
        writeln!(writer, "EOF")?;

        Ok(())
    }

    fn write_rectangle(
        &self,
        writer: &mut impl Write,
        width: f64,
        height: f64,
        center: &Point2D,
    ) -> std::io::Result<()> {
        let x1 = center.x - width / 2.0;
        let y1 = center.y - height / 2.0;
        let x2 = center.x + width / 2.0;
        let y2 = center.y + height / 2.0;

        // LWPOLYLINE (lightweight polyline)
        writeln!(writer, "0")?;
        writeln!(writer, "LWPOLYLINE")?;
        writeln!(writer, "8")?;
        writeln!(writer, "0")?; // Layer 0
        writeln!(writer, "90")?;
        writeln!(writer, "4")?; // 4 vertices
        writeln!(writer, "70")?;
        writeln!(writer, "1")?; // Closed polyline

        // Vertex 1 (bottom-left)
        writeln!(writer, "10")?;
        writeln!(writer, "{:.6}", x1)?;
        writeln!(writer, "20")?;
        writeln!(writer, "{:.6}", y1)?;

        // Vertex 2 (bottom-right)
        writeln!(writer, "10")?;
        writeln!(writer, "{:.6}", x2)?;
        writeln!(writer, "20")?;
        writeln!(writer, "{:.6}", y1)?;

        // Vertex 3 (top-right)
        writeln!(writer, "10")?;
        writeln!(writer, "{:.6}", x2)?;
        writeln!(writer, "20")?;
        writeln!(writer, "{:.6}", y2)?;

        // Vertex 4 (top-left)
        writeln!(writer, "10")?;
        writeln!(writer, "{:.6}", x1)?;
        writeln!(writer, "20")?;
        writeln!(writer, "{:.6}", y2)?;

        Ok(())
    }

    fn write_circle(
        &self,
        writer: &mut impl Write,
        center: &Point2D,
        radius: f64,
    ) -> std::io::Result<()> {
        writeln!(writer, "0")?;
        writeln!(writer, "CIRCLE")?;
        writeln!(writer, "8")?;
        writeln!(writer, "0")?; // Layer 0
        writeln!(writer, "10")?;
        writeln!(writer, "{:.6}", center.x)?;
        writeln!(writer, "20")?;
        writeln!(writer, "{:.6}", center.y)?;
        writeln!(writer, "40")?;
        writeln!(writer, "{:.6}", radius)?;

        Ok(())
    }

    fn write_rounded_rectangle(
        &self,
        writer: &mut impl Write,
        width: f64,
        height: f64,
        center: &Point2D,
        corner_radius: f64,
    ) -> std::io::Result<()> {
        // For rounded rectangles, we approximate with a polyline
        // For simplicity, use arcs at corners

        let r = corner_radius.min(width / 2.0).min(height / 2.0);
        let x1 = center.x - width / 2.0;
        let y1 = center.y - height / 2.0;
        let x2 = center.x + width / 2.0;
        let y2 = center.y + height / 2.0;

        // Use a polyline with bulge values for rounded corners
        writeln!(writer, "0")?;
        writeln!(writer, "LWPOLYLINE")?;
        writeln!(writer, "8")?;
        writeln!(writer, "0")?;
        writeln!(writer, "90")?;
        writeln!(writer, "8")?; // 8 vertices (2 per corner)
        writeln!(writer, "70")?;
        writeln!(writer, "1")?; // Closed

        // Bulge value for 90-degree arc: tan(45°/2) = 0.414
        let bulge = 0.414213562; // tan(π/8)

        // Bottom edge
        writeln!(writer, "10")?;
        writeln!(writer, "{:.6}", x1 + r)?;
        writeln!(writer, "20")?;
        writeln!(writer, "{:.6}", y1)?;

        writeln!(writer, "10")?;
        writeln!(writer, "{:.6}", x2 - r)?;
        writeln!(writer, "20")?;
        writeln!(writer, "{:.6}", y1)?;
        writeln!(writer, "42")?;
        writeln!(writer, "{:.6}", bulge)?; // Bulge for corner arc

        // Right edge
        writeln!(writer, "10")?;
        writeln!(writer, "{:.6}", x2)?;
        writeln!(writer, "20")?;
        writeln!(writer, "{:.6}", y1 + r)?;

        writeln!(writer, "10")?;
        writeln!(writer, "{:.6}", x2)?;
        writeln!(writer, "20")?;
        writeln!(writer, "{:.6}", y2 - r)?;
        writeln!(writer, "42")?;
        writeln!(writer, "{:.6}", bulge)?;

        // Top edge
        writeln!(writer, "10")?;
        writeln!(writer, "{:.6}", x2 - r)?;
        writeln!(writer, "20")?;
        writeln!(writer, "{:.6}", y2)?;

        writeln!(writer, "10")?;
        writeln!(writer, "{:.6}", x1 + r)?;
        writeln!(writer, "20")?;
        writeln!(writer, "{:.6}", y2)?;
        writeln!(writer, "42")?;
        writeln!(writer, "{:.6}", bulge)?;

        // Left edge
        writeln!(writer, "10")?;
        writeln!(writer, "{:.6}", x1)?;
        writeln!(writer, "20")?;
        writeln!(writer, "{:.6}", y2 - r)?;

        writeln!(writer, "10")?;
        writeln!(writer, "{:.6}", x1)?;
        writeln!(writer, "20")?;
        writeln!(writer, "{:.6}", y1 + r)?;
        writeln!(writer, "42")?;
        writeln!(writer, "{:.6}", bulge)?;

        Ok(())
    }

    fn write_line(
        &self,
        writer: &mut impl Write,
        start: &Point2D,
        end: &Point2D,
        layer: &str,
    ) -> std::io::Result<()> {
        writeln!(writer, "0")?;
        writeln!(writer, "LINE")?;
        writeln!(writer, "8")?;
        writeln!(writer, "{}", layer)?; // Layer name
        writeln!(writer, "10")?;
        writeln!(writer, "{:.6}", start.x)?;
        writeln!(writer, "20")?;
        writeln!(writer, "{:.6}", start.y)?;
        writeln!(writer, "11")?;
        writeln!(writer, "{:.6}", end.x)?;
        writeln!(writer, "21")?;
        writeln!(writer, "{:.6}", end.y)?;

        Ok(())
    }

    fn write_slot(
        &self,
        writer: &mut impl Write,
        width: f64,
        height: f64,
        center: &Point2D,
    ) -> std::io::Result<()> {
        // Stadium shape: rectangle with semicircular ends
        // The semicircles are on the shorter dimension
        let (long, short) = if width >= height {
            (width, height)
        } else {
            (height, width)
        };

        let r = short / 2.0;
        let straight = long - short; // Length of straight portion

        if width >= height {
            // Horizontal slot
            let x1 = center.x - straight / 2.0;
            let x2 = center.x + straight / 2.0;

            // LWPOLYLINE with 4 vertices and bulges for semicircles
            writeln!(writer, "0")?;
            writeln!(writer, "LWPOLYLINE")?;
            writeln!(writer, "8")?;
            writeln!(writer, "0")?;
            writeln!(writer, "90")?;
            writeln!(writer, "4")?; // 4 vertices
            writeln!(writer, "70")?;
            writeln!(writer, "1")?; // Closed

            // Bottom-left (start of left semicircle)
            writeln!(writer, "10")?;
            writeln!(writer, "{:.6}", x1)?;
            writeln!(writer, "20")?;
            writeln!(writer, "{:.6}", center.y - r)?;
            writeln!(writer, "42")?;
            writeln!(writer, "1.0")?; // Bulge for 180° arc (semicircle)

            // Top-left (end of left semicircle)
            writeln!(writer, "10")?;
            writeln!(writer, "{:.6}", x1)?;
            writeln!(writer, "20")?;
            writeln!(writer, "{:.6}", center.y + r)?;

            // Top-right (start of right semicircle)
            writeln!(writer, "10")?;
            writeln!(writer, "{:.6}", x2)?;
            writeln!(writer, "20")?;
            writeln!(writer, "{:.6}", center.y + r)?;
            writeln!(writer, "42")?;
            writeln!(writer, "1.0")?; // Bulge for 180° arc

            // Bottom-right (end of right semicircle)
            writeln!(writer, "10")?;
            writeln!(writer, "{:.6}", x2)?;
            writeln!(writer, "20")?;
            writeln!(writer, "{:.6}", center.y - r)?;
        } else {
            // Vertical slot
            let y1 = center.y - straight / 2.0;
            let y2 = center.y + straight / 2.0;

            writeln!(writer, "0")?;
            writeln!(writer, "LWPOLYLINE")?;
            writeln!(writer, "8")?;
            writeln!(writer, "0")?;
            writeln!(writer, "90")?;
            writeln!(writer, "4")?;
            writeln!(writer, "70")?;
            writeln!(writer, "1")?;

            // Left-bottom
            writeln!(writer, "10")?;
            writeln!(writer, "{:.6}", center.x - r)?;
            writeln!(writer, "20")?;
            writeln!(writer, "{:.6}", y1)?;
            writeln!(writer, "42")?;
            writeln!(writer, "1.0")?;

            // Right-bottom
            writeln!(writer, "10")?;
            writeln!(writer, "{:.6}", center.x + r)?;
            writeln!(writer, "20")?;
            writeln!(writer, "{:.6}", y1)?;

            // Right-top
            writeln!(writer, "10")?;
            writeln!(writer, "{:.6}", center.x + r)?;
            writeln!(writer, "20")?;
            writeln!(writer, "{:.6}", y2)?;
            writeln!(writer, "42")?;
            writeln!(writer, "1.0")?;

            // Left-top
            writeln!(writer, "10")?;
            writeln!(writer, "{:.6}", center.x - r)?;
            writeln!(writer, "20")?;
            writeln!(writer, "{:.6}", y2)?;
        }

        Ok(())
    }

    fn write_polyline(
        &self,
        writer: &mut impl Write,
        points: &[Point2D],
        closed: bool,
    ) -> std::io::Result<()> {
        if points.is_empty() {
            return Ok(());
        }

        writeln!(writer, "0")?;
        writeln!(writer, "LWPOLYLINE")?;
        writeln!(writer, "8")?;
        writeln!(writer, "0")?; // Layer 0
        writeln!(writer, "90")?;
        writeln!(writer, "{}", points.len())?;
        writeln!(writer, "70")?;
        writeln!(writer, "{}", if closed { 1 } else { 0 })?;

        for point in points {
            writeln!(writer, "10")?;
            writeln!(writer, "{:.6}", point.x)?;
            writeln!(writer, "20")?;
            writeln!(writer, "{:.6}", point.y)?;
        }

        Ok(())
    }

    fn write_arc(
        &self,
        writer: &mut impl Write,
        center: &Point2D,
        radius: f64,
        start_angle: f64,
        end_angle: f64,
    ) -> std::io::Result<()> {
        writeln!(writer, "0")?;
        writeln!(writer, "ARC")?;
        writeln!(writer, "8")?;
        writeln!(writer, "0")?; // Layer 0
        writeln!(writer, "10")?;
        writeln!(writer, "{:.6}", center.x)?;
        writeln!(writer, "20")?;
        writeln!(writer, "{:.6}", center.y)?;
        writeln!(writer, "40")?;
        writeln!(writer, "{:.6}", radius)?;
        writeln!(writer, "50")?;
        writeln!(writer, "{:.6}", start_angle)?;
        writeln!(writer, "51")?;
        writeln!(writer, "{:.6}", end_angle)?;

        Ok(())
    }
}

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

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;

    #[test]
    fn test_dxf_rectangle() {
        let mut doc = DxfDocument::new();
        doc.add_rectangle(100.0, 50.0, 0.0, 0.0);

        let path = "/tmp/test_rect.dxf";
        doc.export(path).unwrap();

        let content = fs::read_to_string(path).unwrap();
        assert!(content.contains("LWPOLYLINE"));
        assert!(content.contains("EOF"));
    }

    #[test]
    fn test_dxf_circle() {
        let mut doc = DxfDocument::new();
        doc.add_circle(10.0, 20.0, 5.0);

        let path = "/tmp/test_circle.dxf";
        doc.export(path).unwrap();

        let content = fs::read_to_string(path).unwrap();
        assert!(content.contains("CIRCLE"));
    }

    #[test]
    fn test_dxf_rounded_rectangle() {
        let mut doc = DxfDocument::new();
        doc.add_rounded_rectangle(100.0, 50.0, 0.0, 0.0, 10.0);

        let path = "/tmp/test_rounded.dxf";
        doc.export(path).unwrap();

        let content = fs::read_to_string(path).unwrap();
        assert!(content.contains("LWPOLYLINE"));
        assert!(content.contains("42")); // Bulge code
    }
}