cadkit-core 0.5.0

Format-neutral CAD drawing model, bounded binary readers and SVG export for cadkit.
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
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
//! Format-neutral drawing model.
//!
//! Every reader (DWG, DGN, DXF) produces a [`Document`]. The model keeps the
//! common CAD semantics typed and pushes format-specific details into
//! [`Props`] under a format prefix (`dgn.`, `dwg.`, `dxf.`), so nothing a
//! reader understood is thrown away.
//!
//! Conventions:
//! - Coordinates are in drawing units (see [`Units`]); DGN UORs are already
//!   converted to master units.
//! - **Every point and vector is in world coordinates (WCS).** Readers convert
//!   DWG/DXF object coordinates (OCS) to WCS; there is no OCS in this model.
//! - Planar entities carry a `normal` that defines their plane. Angles
//!   (arc start/end, text/insert rotation, hatch pattern angle) are radians,
//!   counter-clockwise about that normal, measured from the plane's X axis as
//!   given by the DXF arbitrary-axis algorithm (`geom_ops::arbitrary_axes`).
//!   For `normal = +Z` this is the ordinary world X axis.
//! - Ordered point lists (polyline vertices, face corners, hatch edges) are in
//!   outline order. Readers fix format quirks such as DXF `SOLID` storing
//!   corners 3 and 4 swapped.
//! - Entities reference layers, blocks, linetypes and text styles by name.

use std::collections::BTreeMap;

use serde::{Deserialize, Serialize};

use crate::geom::{Point3, Vec3};

/// Format-specific extra data, keyed `"<format>.<name>"` (e.g. `"dgn.level_id"`).
pub type Props = BTreeMap<String, Value>;

/// A complete drawing.
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[non_exhaustive]
pub struct Document {
    /// Version of this JSON layout; see [`SchemaVersion`].
    #[serde(default)]
    pub schema_version: SchemaVersion,
    /// Where the data came from.
    pub source: SourceInfo,
    /// Unit of all coordinates in this document.
    pub units: Units,
    /// Layers (DWG/DXF layers, DGN levels).
    pub layers: Vec<Layer>,
    /// Linetype definitions.
    pub linetypes: Vec<Linetype>,
    /// Text style definitions.
    pub text_styles: Vec<TextStyle>,
    /// Block definitions (DWG/DXF blocks, DGN shared cell definitions).
    pub blocks: Vec<Block>,
    /// Model spaces, layouts and sheets that hold the drawn entities.
    pub models: Vec<Model>,
    /// Document-level extras (header variables, file properties).
    pub props: Props,
    /// Recoverable problems met while reading. A non-empty list does not mean failure.
    pub warnings: Vec<Warning>,
}

impl Document {
    /// Creates an empty drawing document.
    pub fn new() -> Self {
        Self::default()
    }

    /// Returns the primary design model (`ModelKind::Model`), falling back to the first model.
    pub fn model_space(&self) -> Option<&Model> {
        self.models
            .iter()
            .find(|m| m.kind == ModelKind::Model)
            .or_else(|| self.models.first())
    }

    /// Returns a mutable reference to the primary design model, creating `"Model"` if `models` is empty.
    pub fn model_space_mut(&mut self) -> &mut Model {
        if self.models.is_empty() {
            self.models.push(Model {
                name: "Model".to_owned(),
                kind: ModelKind::Model,
                ..Model::default()
            });
        }
        let idx = self
            .models
            .iter()
            .position(|m| m.kind == ModelKind::Model)
            .unwrap_or(0);
        #[allow(clippy::expect_used)]
        self.models
            .get_mut(idx)
            .expect("idx is in bounds of non-empty models")
    }

    /// Iterates over paper-space layout and sheet models (`kind != ModelKind::Model`).
    pub fn layouts(&self) -> impl Iterator<Item = &Model> {
        self.models.iter().filter(|m| m.kind != ModelKind::Model)
    }

    /// Iterates over top-level entities across all [`Document::models`].
    pub fn all_entities(&self) -> impl Iterator<Item = &Entity> {
        self.models.iter().flat_map(|m| m.entities.iter())
    }

    /// Iterates over top-level entities across all models whose effective layer matches `layer`
    /// (treating `None` on an entity as layer `"0"`).
    pub fn entities_on_layer<'a>(
        &'a self,
        layer: &'a str,
    ) -> impl Iterator<Item = &'a Entity> + 'a {
        self.all_entities()
            .filter(move |e| e.layer.as_deref().unwrap_or("0") == layer)
    }

    /// Looks up a layer by exact name first, then ASCII case-insensitively.
    pub fn layer(&self, name: &str) -> Option<&Layer> {
        self.layers.iter().find(|l| l.name == name).or_else(|| {
            self.layers
                .iter()
                .find(|l| l.name.eq_ignore_ascii_case(name))
        })
    }

    /// Looks up a block definition by exact name first, then ASCII case-insensitively.
    pub fn block(&self, name: &str) -> Option<&Block> {
        self.blocks.iter().find(|b| b.name == name).or_else(|| {
            self.blocks
                .iter()
                .find(|b| b.name.eq_ignore_ascii_case(name))
        })
    }

    /// Total number of entities in [`Document::models`], counting nested [`EntityKind::Group`] children.
    pub fn entity_count(&self) -> usize {
        fn count_slice(slice: &[Entity]) -> usize {
            let mut total = 0usize;
            let mut stack: Vec<&[Entity]> = vec![slice];
            while let Some(items) = stack.pop() {
                total = total.saturating_add(items.len());
                for e in items {
                    if let EntityKind::Group { children, .. } = &e.kind
                        && !children.is_empty()
                    {
                        stack.push(children);
                    }
                }
            }
            total
        }
        self.models.iter().fold(0usize, |acc, m| {
            acc.saturating_add(count_slice(&m.entities))
        })
    }
}

/// Version of the [`Document`] JSON layout, serialized as the integer `schema_version`.
///
/// Every document this crate writes carries [`SchemaVersion::CURRENT`]. JSON without the
/// field predates versioning and reads as version 1. [`crate::export::document_from_json`]
/// rejects other versions with [`crate::Error::SchemaVersion`]. The version increases only
/// when an existing field changes meaning or shape; new optional fields do not change it.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[serde(transparent)]
pub struct SchemaVersion(u32);

impl SchemaVersion {
    /// The version this crate reads and writes.
    pub const CURRENT: Self = Self(1);

    /// The version number.
    pub const fn get(self) -> u32 {
        self.0
    }
}

impl Default for SchemaVersion {
    fn default() -> Self {
        Self::CURRENT
    }
}

/// Origin of a [`Document`].
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[non_exhaustive]
pub struct SourceInfo {
    /// File format family.
    pub format: Format,
    /// Format version as written in the file, e.g. `"AC1032"`, `"V8"`, `"V7"`.
    pub version: String,
    /// Producing application if recorded, e.g. `"MicroStation v8.11.9.578"`.
    pub application: Option<String>,
    /// Code page used for 8-bit strings, e.g. `"windows-1254"`.
    pub codepage: Option<String>,
}

/// Lightweight header summary returned by header-only `inspect` routines without decoding entity streams.
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[non_exhaustive]
pub struct Summary {
    /// File format family.
    pub format: Format,
    /// Format version as written in the file, e.g. `"AC1032"`, `"V8"`, `"V7"`, `"2.0"`.
    pub version: String,
    /// Whether the primary model is 3D, when recorded in the header.
    pub is_3d: Option<bool>,
    /// Drawing coordinate units, when recorded in the header.
    pub units: Option<Units>,
    /// Code page or encoding name recorded or implied by the header.
    pub codepage: Option<String>,
    /// Producing application if recorded in the header.
    pub application: Option<String>,
}

impl Summary {
    /// Creates a new header summary for `format` and `version`.
    #[must_use]
    pub fn new(format: Format, version: impl Into<String>) -> Self {
        Self {
            format,
            version: version.into(),
            is_3d: None,
            units: None,
            codepage: None,
            application: None,
        }
    }

    /// Sets [`Self::is_3d`].
    #[must_use]
    pub fn with_is_3d(mut self, is_3d: Option<bool>) -> Self {
        self.is_3d = is_3d;
        self
    }

    /// Sets [`Self::units`].
    #[must_use]
    pub fn with_units(mut self, units: Option<Units>) -> Self {
        self.units = units;
        self
    }

    /// Sets [`Self::codepage`].
    #[must_use]
    pub fn with_codepage(mut self, codepage: Option<String>) -> Self {
        self.codepage = codepage;
        self
    }

    /// Sets [`Self::application`].
    #[must_use]
    pub fn with_application(mut self, application: Option<String>) -> Self {
        self.application = application;
        self
    }
}

/// File format family.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum Format {
    /// Not known (e.g. a document built in memory).
    #[default]
    Unknown,
    /// AutoCAD DWG.
    Dwg,
    /// AutoCAD DXF (ASCII or binary).
    Dxf,
    /// MicroStation DGN V7 (ISFF).
    DgnV7,
    /// MicroStation DGN V8 (OLE compound file).
    DgnV8,
    /// CityGML 2.0 (XML application schema based on GML 3.1.1).
    CityGml,
}

/// Drawing units.
#[derive(Debug, Clone, Copy, PartialEq, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[non_exhaustive]
pub struct Units {
    /// Named unit of the coordinates.
    pub unit: LengthUnit,
    /// Size of one drawing unit in meters, when known. Authoritative over `unit`
    /// for non-standard DGN master units.
    pub meters_per_unit: Option<f64>,
}

/// Length unit. Mirrors the DWG `$INSUNITS` set.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[serde(rename_all = "snake_case")]
#[allow(missing_docs)]
pub enum LengthUnit {
    #[default]
    Unitless,
    Inch,
    Foot,
    Mile,
    Millimeter,
    Centimeter,
    Meter,
    Kilometer,
    Microinch,
    Mil,
    Yard,
    Angstrom,
    Nanometer,
    Micrometer,
    Decimeter,
    Decameter,
    Hectometer,
    Gigameter,
    AstronomicalUnit,
    LightYear,
    Parsec,
    UsSurveyFoot,
    /// A unit that is not in this list; see [`Units::meters_per_unit`].
    Custom,
}

/// A layer (DGN: level).
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[non_exhaustive]
pub struct Layer {
    /// Unique name.
    pub name: String,
    /// Native identifier (DWG handle, DGN level id).
    pub id: Option<u64>,
    /// Default color of entities on this layer.
    pub color: Color,
    /// Default linetype name.
    pub linetype: Option<String>,
    /// Default lineweight.
    pub lineweight: Lineweight,
    /// Layer is on / displayed.
    pub visible: bool,
    /// Layer is frozen.
    pub frozen: bool,
    /// Layer is locked.
    pub locked: bool,
    /// Layer is plotted.
    pub plottable: bool,
    /// Free-text description.
    pub description: Option<String>,
    /// Format-specific extras.
    pub props: Props,
}

/// A linetype definition.
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[non_exhaustive]
pub struct Linetype {
    /// Unique name.
    pub name: String,
    /// Description text.
    pub description: Option<String>,
    /// Dash pattern in drawing units: positive = dash, negative = gap, zero = dot.
    pub pattern: Vec<f64>,
    /// Format-specific extras.
    pub props: Props,
}

/// A text style definition.
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[non_exhaustive]
pub struct TextStyle {
    /// Unique name.
    pub name: String,
    /// Font file or font name.
    pub font: Option<String>,
    /// Fixed text height, 0 when variable.
    pub height: f64,
    /// Width factor (1.0 = normal).
    pub width_factor: f64,
    /// Oblique angle in radians.
    pub oblique: f64,
    /// Format-specific extras.
    pub props: Props,
}

/// A block definition (DGN: shared cell definition).
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[non_exhaustive]
pub struct Block {
    /// Unique name.
    pub name: String,
    /// Native identifier.
    pub id: Option<u64>,
    /// Insertion base point.
    pub base_point: Point3,
    /// Entities in block coordinates.
    pub entities: Vec<Entity>,
    /// Block is an external reference.
    pub is_xref: bool,
    /// Path of the external reference.
    pub xref_path: Option<String>,
    /// Description text.
    pub description: Option<String>,
    /// Format-specific extras.
    pub props: Props,
}

/// A space that holds drawn entities: model space, a paper-space layout or a DGN model.
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[non_exhaustive]
pub struct Model {
    /// Display name.
    pub name: String,
    /// Native identifier.
    pub id: Option<u64>,
    /// Kind of space.
    pub kind: ModelKind,
    /// Model stores 3D geometry.
    pub is_3d: bool,
    /// Entities in drawing order.
    pub entities: Vec<Entity>,
    /// Format-specific extras.
    pub props: Props,
}

/// Kind of [`Model`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum ModelKind {
    /// Design / model space.
    #[default]
    Model,
    /// Paper-space layout (DWG/DXF).
    Layout,
    /// Sheet model (DGN).
    Sheet,
}

/// One drawn object.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[non_exhaustive]
pub struct Entity {
    /// Native identifier (DWG handle, DGN element id).
    pub id: Option<u64>,
    /// Layer name; `None` means the default layer.
    pub layer: Option<String>,
    /// Color.
    pub color: Color,
    /// Linetype name; `None` means by layer.
    pub linetype: Option<String>,
    /// Lineweight.
    pub lineweight: Lineweight,
    /// Entity is displayed.
    pub visible: bool,
    /// Geometry and type-specific data.
    pub kind: EntityKind,
    /// Attached non-graphic data: DWG attributes, DGN tags, item-type properties.
    pub attributes: Vec<Attribute>,
    /// Format-specific extras.
    pub props: Props,
    /// Original record bytes, kept only when [`crate::ReadOptions::keep_raw`] is set.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub raw: Option<Raw>,
}

impl Entity {
    /// An entity with default symbology.
    pub fn new(kind: EntityKind) -> Self {
        Self {
            id: None,
            layer: None,
            color: Color::ByLayer,
            linetype: None,
            lineweight: Lineweight::ByLayer,
            visible: true,
            kind,
            attributes: Vec::new(),
            props: Props::new(),
            raw: None,
        }
    }

    /// Constructs a [`EntityKind::Point`] entity.
    pub fn point(position: Point3) -> Self {
        Self::new(EntityKind::Point { position })
    }

    /// Constructs a [`EntityKind::Line`] entity.
    pub fn line(start: Point3, end: Point3) -> Self {
        Self::new(EntityKind::Line { start, end })
    }

    /// Constructs a straight-segment [`EntityKind::Polyline`] in the world XY plane (`normal = +Z`).
    pub fn polyline(points: impl IntoIterator<Item = Point3>, closed: bool) -> Self {
        Self::new(EntityKind::Polyline {
            vertices: points.into_iter().map(Vertex::at).collect(),
            closed,
            normal: Vec3::Z,
        })
    }

    /// Constructs a [`EntityKind::Circle`] in the world XY plane (`normal = +Z`).
    pub fn circle(center: Point3, radius: f64) -> Self {
        Self::new(EntityKind::Circle {
            center,
            radius,
            normal: Vec3::Z,
        })
    }

    /// Constructs a counter-clockwise [`EntityKind::Arc`] in the world XY plane (`normal = +Z`).
    pub fn arc(center: Point3, radius: f64, start_angle: f64, end_angle: f64) -> Self {
        Self::new(EntityKind::Arc {
            center,
            radius,
            start_angle,
            end_angle,
            normal: Vec3::Z,
        })
    }

    /// Constructs a left-baseline [`EntityKind::Text`] entity in the world XY plane (`normal = +Z`).
    pub fn text(position: Point3, height: f64, value: impl Into<String>) -> Self {
        Self::new(EntityKind::Text {
            position,
            end_point: None,
            height,
            rotation: 0.0,
            width_factor: 1.0,
            oblique: 0.0,
            value: value.into(),
            style: None,
            halign: HAlign::Left,
            valign: VAlign::Baseline,
            normal: Vec3::Z,
        })
    }

    /// Constructs a top-left [`EntityKind::MText`] entity from plain UTF-8 text.
    pub fn mtext(position: Point3, height: f64, plain: impl Into<String>) -> Self {
        let plain = plain.into();
        let value = plain.replace('\n', "\\P");
        Self::new(EntityKind::MText {
            position,
            height,
            width: 0.0,
            rotation: 0.0,
            value,
            plain,
            style: None,
            attachment: Attachment::TopLeft,
            line_spacing: 1.0,
            normal: Vec3::Z,
        })
    }

    /// Constructs a unit-scale [`EntityKind::Insert`] block reference at `position`.
    pub fn insert(block: impl Into<String>, position: Point3) -> Self {
        Self::new(EntityKind::Insert {
            block: block.into(),
            position,
            scale: Vec3::ONE,
            rotation: 0.0,
            normal: Vec3::Z,
            columns: 1,
            rows: 1,
            column_spacing: 0.0,
            row_spacing: 0.0,
        })
    }

    /// Assigns the entity's layer name.
    pub fn on_layer(mut self, layer: impl Into<String>) -> Self {
        self.layer = Some(layer.into());
        self
    }

    /// Assigns the entity's [`Color`].
    pub fn with_color(mut self, color: Color) -> Self {
        self.color = color;
        self
    }

    /// Assigns the entity's linetype name.
    pub fn with_linetype(mut self, linetype: impl Into<String>) -> Self {
        self.linetype = Some(linetype.into());
        self
    }

    /// Assigns the entity's [`Lineweight`].
    pub fn with_lineweight(mut self, lineweight: Lineweight) -> Self {
        self.lineweight = lineweight;
        self
    }

    /// Sets [`Entity::visible`].
    pub fn with_visible(mut self, visible: bool) -> Self {
        self.visible = visible;
        self
    }

    /// Inserts a key-value entry into [`Entity::props`].
    pub fn with_prop(mut self, key: impl Into<String>, value: impl Into<Value>) -> Self {
        self.props.insert(key.into(), value.into());
        self
    }

    /// Appends a non-graphic [`Attribute`] tag/value pair to [`Entity::attributes`].
    pub fn with_attribute(mut self, tag: impl Into<String>, value: impl Into<Value>) -> Self {
        self.attributes.push(Attribute {
            tag: tag.into(),
            value: value.into(),
            invisible: true,
            ..Attribute::default()
        });
        self
    }
}

/// Geometry and type-specific data of an [`Entity`].
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[serde(tag = "type", rename_all = "snake_case")]
#[non_exhaustive]
pub enum EntityKind {
    /// A point.
    Point {
        /// Location.
        position: Point3,
    },
    /// A straight segment.
    Line {
        /// Start point.
        start: Point3,
        /// End point.
        end: Point3,
    },
    /// Connected segments, optionally with arc segments (bulge) and widths.
    /// Covers DWG LWPOLYLINE/POLYLINE and DGN line string / shape.
    Polyline {
        /// Vertices in order.
        vertices: Vec<Vertex>,
        /// Last vertex connects back to the first.
        closed: bool,
        /// Plane normal; bulge arcs turn counter-clockwise about it. `+Z` for plain 2D data
        /// and for non-planar 3D polylines (which have no bulges).
        normal: Vec3,
    },
    /// A full circle.
    Circle {
        /// Center.
        center: Point3,
        /// Radius.
        radius: f64,
        /// Plane normal.
        normal: Vec3,
    },
    /// A circular arc from `start_angle` counter-clockwise to `end_angle`.
    Arc {
        /// Center.
        center: Point3,
        /// Radius.
        radius: f64,
        /// Start angle in radians.
        start_angle: f64,
        /// End angle in radians.
        end_angle: f64,
        /// Plane normal.
        normal: Vec3,
    },
    /// An ellipse or elliptical arc.
    Ellipse {
        /// Center.
        center: Point3,
        /// Vector from the center to the end of the major axis (its length is the semi-major axis).
        major_axis: Vec3,
        /// Minor / major axis ratio, in (0, 1].
        ratio: f64,
        /// Start parameter in radians (0 for a full ellipse).
        start_param: f64,
        /// End parameter in radians (2Ï€ for a full ellipse).
        end_param: f64,
        /// Plane normal.
        normal: Vec3,
    },
    /// A B-spline / NURBS curve.
    Spline {
        /// Degree.
        degree: u32,
        /// Knot vector (may be empty when only fit points are known).
        knots: Vec<f64>,
        /// Control points.
        control_points: Vec<Point3>,
        /// Weights; empty for non-rational curves.
        weights: Vec<f64>,
        /// Fit points, if stored.
        fit_points: Vec<Point3>,
        /// Curve is closed / periodic.
        closed: bool,
    },
    /// Single-line text.
    Text {
        /// Anchor point implied by `halign`/`valign`: the baseline start for left/baseline
        /// text, otherwise the justification point (DXF/DWG second alignment point, group 11).
        /// For `Aligned`/`Fit` text: the baseline start.
        position: Point3,
        /// Baseline end for `Aligned`/`Fit` text; `None` for every other alignment.
        end_point: Option<Point3>,
        /// Character height.
        height: f64,
        /// Rotation in radians.
        rotation: f64,
        /// Width factor (1.0 = normal).
        width_factor: f64,
        /// Oblique angle in radians.
        oblique: f64,
        /// Decoded text.
        value: String,
        /// Text style name.
        style: Option<String>,
        /// Horizontal alignment.
        halign: HAlign,
        /// Vertical alignment.
        valign: VAlign,
        /// Plane normal.
        normal: Vec3,
    },
    /// Multi-line, formatted text. DGN text nodes map to this when they only hold text.
    MText {
        /// Insertion point.
        position: Point3,
        /// Character height.
        height: f64,
        /// Reference rectangle width, 0 when unconstrained.
        width: f64,
        /// Rotation in radians.
        rotation: f64,
        /// Text including format codes as stored.
        value: String,
        /// Text with format codes removed, lines separated by `\n`.
        plain: String,
        /// Text style name.
        style: Option<String>,
        /// Attachment point.
        attachment: Attachment,
        /// Line spacing factor.
        line_spacing: f64,
        /// Plane normal.
        normal: Vec3,
    },
    /// A placed block (DWG INSERT, DGN shared cell instance).
    /// Attribute values live in [`Entity::attributes`].
    Insert {
        /// Block name.
        block: String,
        /// Insertion point.
        position: Point3,
        /// Scale factors.
        scale: Vec3,
        /// Rotation in radians.
        rotation: f64,
        /// Plane normal.
        normal: Vec3,
        /// Array columns (1 = no array).
        columns: u32,
        /// Array rows (1 = no array).
        rows: u32,
        /// Column spacing.
        column_spacing: f64,
        /// Row spacing.
        row_spacing: f64,
    },
    /// A filled or patterned area.
    Hatch {
        /// Boundary loops; the first is usually the outer one.
        loops: Vec<HatchLoop>,
        /// Solid fill.
        solid: bool,
        /// Pattern name, if patterned.
        pattern: Option<String>,
        /// Pattern scale.
        pattern_scale: f64,
        /// Pattern angle in radians.
        pattern_angle: f64,
        /// Plane normal.
        normal: Vec3,
    },
    /// A dimension annotation.
    Dimension {
        /// Kind of dimension.
        dimension_type: DimensionType,
        /// Definition points in format order (see `props` for their roles).
        points: Vec<Point3>,
        /// Text middle point.
        text_position: Option<Point3>,
        /// Measured value in drawing units (or radians for angular).
        measurement: Option<f64>,
        /// Text override.
        text: Option<String>,
        /// Anonymous block holding the rendered dimension, if any.
        block: Option<String>,
        /// Dimension style name.
        style: Option<String>,
    },
    /// A planar 3- or 4-sided face (DWG SOLID/TRACE/3DFACE).
    Face {
        /// Corner points (3 or 4).
        points: Vec<Point3>,
        /// The face is filled (SOLID/TRACE) rather than a wire face.
        filled: bool,
    },
    /// A planar surface with optional holes, in world coordinates (WCS) and drawing units.
    /// Each ring is explicitly closed by repeating its first point as its last point.
    Polygon {
        /// Closed outer boundary ring containing at least four positions.
        exterior: Vec<Point3>,
        /// Closed hole rings inside the exterior boundary.
        interiors: Vec<Vec<Point3>>,
    },
    /// A leader line.
    Leader {
        /// Vertices from arrow tip to the end.
        vertices: Vec<Point3>,
        /// Draws an arrowhead at the first vertex.
        arrowhead: bool,
    },
    /// A raster image reference (DWG IMAGE, DGN raster attachment).
    Image {
        /// Image file path as stored.
        path: Option<String>,
        /// Lower-left corner.
        position: Point3,
        /// Vector along the image bottom edge, covering the full width.
        u_vector: Vec3,
        /// Vector along the image left edge, covering the full height.
        v_vector: Vec3,
        /// Pixel size, when known.
        size_px: Option<[u32; 2]>,
    },
    /// A paper-space viewport.
    Viewport {
        /// Center on the sheet.
        center: Point3,
        /// Width on the sheet.
        width: f64,
        /// Height on the sheet.
        height: f64,
        /// Model-space point shown at the center.
        view_center: Point3,
        /// Model-space height shown.
        view_height: f64,
    },
    /// A polygon mesh or polyface.
    Mesh {
        /// Vertex positions.
        vertices: Vec<Point3>,
        /// Faces as vertex indices (0-based).
        faces: Vec<Vec<u32>>,
    },
    /// Entities that belong together: DGN complex chains/shapes, cells, text nodes, groups.
    /// Children are in the same coordinate space as the group.
    Group {
        /// What kind of grouping this is.
        group_kind: GroupKind,
        /// Name (cell name, group name).
        name: Option<String>,
        /// Origin (cell origin, text node origin).
        origin: Option<Point3>,
        /// Member entities.
        children: Vec<Entity>,
    },
    /// A record the reader recognized but does not model; see `props` / `raw`.
    Unknown {
        /// Native type name or code, e.g. `"dgn.type_94"` or `"dwg.ACAD_TABLE"`.
        type_name: String,
    },
}

/// Polyline vertex.
#[derive(Debug, Clone, Copy, PartialEq, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
pub struct Vertex {
    /// Position.
    pub position: Point3,
    /// Bulge of the segment that starts here: tan(included angle / 4), 0 = straight.
    pub bulge: f64,
    /// Start width of the segment that starts here.
    pub start_width: f64,
    /// End width of the segment that starts here.
    pub end_width: f64,
}

impl Vertex {
    /// A straight, zero-width vertex.
    pub fn at(position: Point3) -> Self {
        Self {
            position,
            ..Self::default()
        }
    }
}

/// Horizontal text alignment.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[serde(rename_all = "snake_case")]
#[allow(missing_docs)]
pub enum HAlign {
    #[default]
    Left,
    Center,
    Right,
    Aligned,
    Middle,
    Fit,
}

/// Vertical text alignment.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[serde(rename_all = "snake_case")]
#[allow(missing_docs)]
pub enum VAlign {
    #[default]
    Baseline,
    Bottom,
    Middle,
    Top,
}

/// Multi-line text attachment point.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[serde(rename_all = "snake_case")]
#[allow(missing_docs)]
pub enum Attachment {
    #[default]
    TopLeft,
    TopCenter,
    TopRight,
    MiddleLeft,
    MiddleCenter,
    MiddleRight,
    BottomLeft,
    BottomCenter,
    BottomRight,
}

/// Dimension kind.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[serde(rename_all = "snake_case")]
#[allow(missing_docs)]
pub enum DimensionType {
    #[default]
    Linear,
    Aligned,
    Angular,
    Angular3Point,
    Diameter,
    Radius,
    Ordinate,
    ArcLength,
    Other,
}

/// What a [`EntityKind::Group`] represents.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum GroupKind {
    /// DGN complex chain (open).
    ComplexChain,
    /// DGN complex shape (closed).
    ComplexShape,
    /// DGN text node.
    TextNode,
    /// DGN cell (children inline, already placed).
    Cell,
    /// Any other grouping.
    #[default]
    Other,
}

/// One hatch boundary loop.
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
pub struct HatchLoop {
    /// Edges in order.
    pub edges: Vec<HatchEdge>,
    /// Loop is the outermost boundary.
    pub external: bool,
}

/// A hatch boundary edge.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[serde(tag = "type", rename_all = "snake_case")]
#[allow(missing_docs)]
pub enum HatchEdge {
    Line {
        start: Point3,
        end: Point3,
    },
    Arc {
        center: Point3,
        radius: f64,
        start_angle: f64,
        end_angle: f64,
        ccw: bool,
    },
    Ellipse {
        center: Point3,
        major_axis: Vec3,
        ratio: f64,
        start_param: f64,
        end_param: f64,
        ccw: bool,
    },
    Spline {
        degree: u32,
        knots: Vec<f64>,
        control_points: Vec<Point3>,
        weights: Vec<f64>,
    },
    Polyline {
        vertices: Vec<Vertex>,
        closed: bool,
    },
}

/// Color.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum Color {
    /// Use the layer color.
    #[default]
    ByLayer,
    /// Use the color of the inserting block reference.
    ByBlock,
    /// AutoCAD Color Index 1..=255. DGN palette indices are resolved to [`Color::Rgb`]
    /// by the DGN reader; the original index goes to `props["dgn.color_index"]`.
    Aci {
        /// Index.
        index: u8,
    },
    /// True color.
    Rgb {
        /// Red.
        r: u8,
        /// Green.
        g: u8,
        /// Blue.
        b: u8,
    },
}

impl Color {
    /// Shorthand for `Color::Aci { index }`.
    pub const fn aci(index: u8) -> Self {
        Self::Aci { index }
    }

    /// Shorthand for `Color::Rgb { r, g, b }`.
    pub const fn rgb(r: u8, g: u8, b: u8) -> Self {
        Self::Rgb { r, g, b }
    }
}

/// Line weight.
#[derive(Debug, Clone, Copy, PartialEq, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[serde(tag = "type", content = "value", rename_all = "snake_case")]
pub enum Lineweight {
    /// Use the layer lineweight.
    #[default]
    ByLayer,
    /// Use the block reference lineweight.
    ByBlock,
    /// Application default.
    Default,
    /// Explicit width in millimeters. DGN weight steps are converted; the original goes to props.
    Millimeters(f64),
}

/// Non-graphic data attached to an entity (DWG ATTRIB, DGN tag, item-type property).
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[non_exhaustive]
pub struct Attribute {
    /// Tag / property name.
    pub tag: String,
    /// Value.
    pub value: Value,
    /// Owning tag set / item type / definition name.
    pub set: Option<String>,
    /// Where the attribute text is displayed, if it is displayed.
    pub position: Option<Point3>,
    /// The attribute is not displayed.
    pub invisible: bool,
    /// Layer of the attribute's own record when the format stores one (a DGN tag
    /// element has its own level). `None` uses the owning entity's layer.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub layer: Option<String>,
    /// How the value text is drawn at `position`; `None` when unknown or not drawn.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub display: Option<AttributeDisplay>,
    /// Format-specific data with no neutral field (e.g. `dgn.color_index`, `dgn.weight`).
    #[serde(default, skip_serializing_if = "Props::is_empty")]
    pub props: Props,
}

/// Text presentation of a displayed attribute value.
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[non_exhaustive]
pub struct AttributeDisplay {
    /// Text height, in drawing units.
    pub height: f64,
    /// Character width, in drawing units (DGN stores it separately from the height).
    pub width: f64,
    /// Text style (DGN font) name; `None` leaves the choice to the writer's default.
    pub style: Option<String>,
    /// Horizontal alignment of the text about `Attribute::position`.
    pub halign: HAlign,
    /// Vertical alignment of the text about `Attribute::position`.
    pub valign: VAlign,
    /// Rotation about the drawing Z axis, in radians.
    pub rotation: f64,
}

/// A loosely typed value for attributes and props.
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[serde(untagged)]
#[non_exhaustive]
pub enum Value {
    /// No value.
    #[default]
    Null,
    /// Boolean.
    Bool(bool),
    /// Integer.
    Int(i64),
    /// Floating point.
    Float(f64),
    /// Text.
    Text(String),
    /// Ordered list.
    List(Vec<Value>),
    /// Binary data.
    Bytes(Vec<u8>),
}

impl Value {
    /// True when `self` is [`Value::Null`].
    pub const fn is_null(&self) -> bool {
        matches!(self, Self::Null)
    }

    /// Returns the boolean when `self` is [`Value::Bool`].
    pub const fn as_bool(&self) -> Option<bool> {
        match self {
            Self::Bool(v) => Some(*v),
            _ => None,
        }
    }

    /// Returns the integer when `self` is [`Value::Int`].
    pub const fn as_i64(&self) -> Option<i64> {
        match self {
            Self::Int(v) => Some(*v),
            _ => None,
        }
    }

    /// Returns a floating-point number for [`Value::Float`] or lossless/cast [`Value::Int`].
    pub const fn as_f64(&self) -> Option<f64> {
        match self {
            Self::Float(v) => Some(*v),
            Self::Int(v) => Some(*v as f64),
            _ => None,
        }
    }

    /// Returns the string slice when `self` is [`Value::Text`].
    pub fn as_str(&self) -> Option<&str> {
        match self {
            Self::Text(s) => Some(s.as_str()),
            _ => None,
        }
    }

    /// Returns the slice when `self` is [`Value::List`].
    pub fn as_list(&self) -> Option<&[Value]> {
        match self {
            Self::List(items) => Some(items.as_slice()),
            _ => None,
        }
    }

    /// Returns the byte slice when `self` is [`Value::Bytes`].
    pub fn as_bytes(&self) -> Option<&[u8]> {
        match self {
            Self::Bytes(bytes) => Some(bytes.as_slice()),
            _ => None,
        }
    }
}

impl From<bool> for Value {
    fn from(v: bool) -> Self {
        Value::Bool(v)
    }
}
impl From<i64> for Value {
    fn from(v: i64) -> Self {
        Value::Int(v)
    }
}
impl From<i32> for Value {
    fn from(v: i32) -> Self {
        Value::Int(i64::from(v))
    }
}
impl From<i16> for Value {
    fn from(v: i16) -> Self {
        Value::Int(i64::from(v))
    }
}
impl From<u32> for Value {
    fn from(v: u32) -> Self {
        Value::Int(i64::from(v))
    }
}
impl From<u16> for Value {
    fn from(v: u16) -> Self {
        Value::Int(i64::from(v))
    }
}
impl From<u8> for Value {
    fn from(v: u8) -> Self {
        Value::Int(i64::from(v))
    }
}
impl From<f64> for Value {
    fn from(v: f64) -> Self {
        Value::Float(v)
    }
}
impl From<f32> for Value {
    fn from(v: f32) -> Self {
        Value::Float(f64::from(v))
    }
}
impl From<String> for Value {
    fn from(v: String) -> Self {
        Value::Text(v)
    }
}
impl From<&str> for Value {
    fn from(v: &str) -> Self {
        Value::Text(v.to_owned())
    }
}
impl From<Vec<u8>> for Value {
    fn from(v: Vec<u8>) -> Self {
        Value::Bytes(v)
    }
}
impl From<Vec<Value>> for Value {
    fn from(v: Vec<Value>) -> Self {
        Value::List(v)
    }
}

/// Original bytes of a record, for lossless inspection.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
pub struct Raw {
    /// Native type code (DGN element type, DWG object type).
    pub type_code: u32,
    /// Record bytes exactly as stored (after decompression).
    pub bytes: Vec<u8>,
}

/// A recoverable problem found while reading.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[non_exhaustive]
pub struct Warning {
    /// Stable machine-readable code, e.g. `"dgn.unknown_element"`.
    pub code: String,
    /// Human-readable explanation.
    pub message: String,
    /// Byte offset in the decoded stream, when meaningful.
    pub offset: Option<u64>,
    /// Native id of the affected object, when known.
    pub object: Option<u64>,
}