acadrust 0.6.7

A pure Rust library for reading and writing CAD files in DXF format (ASCII and Binary) and DWG format (Binary).
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
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
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
//! Semantic data model for dynamic-block parameters, grips, actions and
//! evaluation/history records.
//!
//! The types in this module mirror the inheritance layers used by the DWG
//! object stream. They intentionally contain no reader/writer policy.

use crate::entities::AcisData;
use crate::types::{Color, Handle, Vector2, Vector3};

use super::BlockEvalValue;
use super::BlockVisibilityParameter;

#[derive(Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct DynamicBlockObject {
    pub handle: Handle,
    pub owner: Handle,
    pub reactors: Vec<Handle>,
    pub xdictionary_handle: Option<Handle>,
    pub dxf_name: String,
    pub cpp_class_name: String,
    pub data: DynamicBlockData,
}

impl DynamicBlockObject {
    pub fn new(dxf_name: impl Into<String>, cpp_class_name: impl Into<String>) -> Self {
        Self {
            dxf_name: dxf_name.into(),
            cpp_class_name: cpp_class_name.into(),
            ..Self::default()
        }
    }

    /// Visit every handle the object refers to except its own.
    pub fn visit_handles_mut(&mut self, visit: &mut impl FnMut(&mut Handle)) {
        visit(&mut self.owner);
        for handle in &mut self.reactors {
            visit(handle);
        }
        if let Some(handle) = self.xdictionary_handle.as_mut() {
            visit(handle);
        }
        self.data.visit_handles_mut(visit);
    }
}

#[derive(Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum DynamicBlockData {
    #[default]
    Unknown,
    Representation(BlockRepresentationData),
    ProxyNode(BlockEvalExpression),
    GripLocationComponent(BlockGripExpression),
    AlignmentGrip(BlockOrientedGrip),
    FlipGrip(BlockFlipGrip),
    LinearGrip(BlockOrientedGrip),
    LookupGrip(BlockGrip),
    PolarGrip(BlockGrip),
    RotationGrip(BlockGrip),
    VisibilityGrip(BlockGrip),
    XYGrip(BlockGrip),
    PropertiesTableGrip(BlockGrip),
    AlignmentParameter(BlockAlignmentParameter),
    BasePointParameter(BlockBasePointParameter),
    FlipParameter(BlockFlipParameter),
    LinearParameter(BlockLinearParameter),
    LookupParameter(BlockLookupParameter),
    PointParameter(BlockPointParameter),
    PolarParameter(BlockPolarParameter),
    RotationParameter(BlockRotationParameter),
    UserParameter(BlockUserParameter),
    VisibilityParameter(BlockVisibilityParameter),
    XYParameter(BlockXYParameter),
    AngularConstraintParameter(BlockAngularConstraintParameter),
    DiametricConstraintParameter(BlockDistanceConstraintParameter),
    RadialConstraintParameter(BlockDistanceConstraintParameter),
    AlignedConstraintParameter(BlockLinearConstraintParameter),
    LinearConstraintParameter(BlockLinearConstraintParameter),
    HorizontalConstraintParameter(BlockLinearConstraintParameter),
    VerticalConstraintParameter(BlockLinearConstraintParameter),
    ParameterDependencyBody(BlockParameterDependencyBody),
    MoveAction(BlockMoveAction),
    FlipAction(BlockFlipAction),
    RotateAction(BlockBasePointAction),
    ScaleAction(BlockBasePointAction),
    ArrayAction(BlockArrayAction),
    LookupAction(BlockLookupAction),
    PropertiesTable(BlockPropertiesTable),
    StretchAction(BlockStretchAction),
    PolarStretchAction(BlockPolarStretchAction),
    AlignmentParameterEntity,
    BasePointParameterEntity,
    FlipParameterEntity,
    LinearParameterEntity,
    PointParameterEntity,
    RotationParameterEntity,
    VisibilityParameterEntity,
    XYParameterEntity,
    AngularConstraintParameterEntity(BlockAngularConstraintParameterEntity),
    FlipGripEntity,
    LinearGripEntity,
    PolarGripEntity,
    RotationGripEntity,
    VisibilityGripEntity,
    XYGripEntity,
    EvaluationGraph(BlockEvaluationGraph),
    SolidHistory(SolidHistory),
    SolidHistoryNode(SolidHistoryOperation),
}

impl DynamicBlockData {
    pub fn empty_entity_from_dxf_name(name: &str) -> Option<Self> {
        match name.to_ascii_uppercase().as_str() {
            "ALIGNMENTPARAMETERENTITY" => Some(Self::AlignmentParameterEntity),
            "BASEPOINTPARAMETERENTITY" => Some(Self::BasePointParameterEntity),
            "FLIPPARAMETERENTITY" => Some(Self::FlipParameterEntity),
            "LINEARPARAMETERENTITY" => Some(Self::LinearParameterEntity),
            "POINTPARAMETERENTITY" => Some(Self::PointParameterEntity),
            "ROTATIONPARAMETERENTITY" => Some(Self::RotationParameterEntity),
            "VISIBILITYPARAMETERENTITY" => Some(Self::VisibilityParameterEntity),
            "XYPARAMETERENTITY" => Some(Self::XYParameterEntity),
            "FLIPGRIPENTITY" => Some(Self::FlipGripEntity),
            "LINEARGRIPENTITY" => Some(Self::LinearGripEntity),
            "POLARGRIPENTITY" => Some(Self::PolarGripEntity),
            "ROTATIONGRIPENTITY" => Some(Self::RotationGripEntity),
            "VISIBILITYGRIPENTITY" => Some(Self::VisibilityGripEntity),
            "XYGRIPENTITY" => Some(Self::XYGripEntity),
            _ => None,
        }
    }

    pub fn entity_dxf_name(&self) -> Option<&'static str> {
        match self {
            Self::AlignmentParameterEntity => Some("ALIGNMENTPARAMETERENTITY"),
            Self::BasePointParameterEntity => Some("BASEPOINTPARAMETERENTITY"),
            Self::FlipParameterEntity => Some("FLIPPARAMETERENTITY"),
            Self::LinearParameterEntity => Some("LINEARPARAMETERENTITY"),
            Self::PointParameterEntity => Some("POINTPARAMETERENTITY"),
            Self::RotationParameterEntity => Some("ROTATIONPARAMETERENTITY"),
            Self::VisibilityParameterEntity => Some("VISIBILITYPARAMETERENTITY"),
            Self::XYParameterEntity => Some("XYPARAMETERENTITY"),
            Self::AngularConstraintParameterEntity(_) => {
                Some("BLOCKANGULARCONSTRAINTPARAMETERENTITY")
            }
            Self::FlipGripEntity => Some("FLIPGRIPENTITY"),
            Self::LinearGripEntity => Some("LINEARGRIPENTITY"),
            Self::PolarGripEntity => Some("POLARGRIPENTITY"),
            Self::RotationGripEntity => Some("ROTATIONGRIPENTITY"),
            Self::VisibilityGripEntity => Some("VISIBILITYGRIPENTITY"),
            Self::XYGripEntity => Some("XYGRIPENTITY"),
            _ => None,
        }
    }

    pub fn entity_cpp_name(&self) -> Option<&'static str> {
        match self {
            Self::AlignmentParameterEntity => Some("AcDbBlockAlignmentParameterEntity"),
            Self::BasePointParameterEntity => Some("AcDbBlockBasepointParameterEntity"),
            Self::FlipParameterEntity => Some("AcDbBlockFlipParameterEntity"),
            Self::LinearParameterEntity => Some("AcDbBlockLinearParameterEntity"),
            Self::PointParameterEntity => Some("AcDbBlockPointParameterEntity"),
            Self::RotationParameterEntity => Some("AcDbBlockRotationParameterEntity"),
            Self::VisibilityParameterEntity => Some("AcDbBlockVisibilityParameterEntity"),
            Self::XYParameterEntity => Some("AcDbBlockXYParameterEntity"),
            Self::AngularConstraintParameterEntity(_) => {
                Some("AcDbBlockAngularConstraintParameterEntity")
            }
            Self::FlipGripEntity => Some("AcDbBlockFlipGripEntity"),
            Self::LinearGripEntity => Some("AcDbBlockLinearGripEntity"),
            Self::PolarGripEntity => Some("AcDbBlockPolarGripEntity"),
            Self::RotationGripEntity => Some("AcDbBlockRotationGripEntity"),
            Self::VisibilityGripEntity => Some("AcDbBlockVisibilityGripEntity"),
            Self::XYGripEntity => Some("AcDbBlockXYGripEntity"),
            _ => None,
        }
    }

    pub(crate) fn visit_handles_mut(&mut self, visit: &mut impl FnMut(&mut Handle)) {
        match self {
            Self::Unknown
            | Self::AlignmentParameterEntity
            | Self::BasePointParameterEntity
            | Self::FlipParameterEntity
            | Self::LinearParameterEntity
            | Self::PointParameterEntity
            | Self::RotationParameterEntity
            | Self::VisibilityParameterEntity
            | Self::XYParameterEntity
            | Self::FlipGripEntity
            | Self::LinearGripEntity
            | Self::PolarGripEntity
            | Self::RotationGripEntity
            | Self::VisibilityGripEntity
            | Self::XYGripEntity
            | Self::ParameterDependencyBody(_) => {}
            Self::Representation(value) => visit(&mut value.block),
            Self::PropertiesTable(value) => {
                visit_one_point_parameter(&mut value.parameter, visit);
                for field in &mut value.fields {
                    if let BlockPropertiesTableValue::Handle(handle) = &mut field.value {
                        visit(handle);
                    }
                }
            }
            Self::ProxyNode(value) => visit_block_eval(value, visit),
            Self::GripLocationComponent(value) => {
                visit_block_eval(&mut value.eval, visit);
            }
            Self::AlignmentGrip(value) | Self::LinearGrip(value) => {
                visit_block_grip(&mut value.grip, visit);
            }
            Self::FlipGrip(value) => {
                visit_block_grip(&mut value.grip, visit);
            }
            Self::LookupGrip(value)
            | Self::PolarGrip(value)
            | Self::RotationGrip(value)
            | Self::VisibilityGrip(value)
            | Self::XYGrip(value)
            | Self::PropertiesTableGrip(value) => {
                visit_block_grip(value, visit);
            }
            Self::AlignmentParameter(value) => {
                visit_two_point_parameter(&mut value.parameter, visit);
            }
            Self::BasePointParameter(value) => {
                visit_one_point_parameter(&mut value.parameter, visit);
            }
            Self::FlipParameter(value) => {
                visit_two_point_parameter(&mut value.parameter, visit);
            }
            Self::LinearParameter(value) => {
                visit_two_point_parameter(&mut value.parameter, visit);
            }
            Self::LookupParameter(value) => {
                visit_one_point_parameter(&mut value.parameter, visit);
            }
            Self::PointParameter(value) => {
                visit_one_point_parameter(&mut value.parameter, visit);
            }
            Self::PolarParameter(value) => {
                visit_two_point_parameter(&mut value.parameter, visit);
            }
            Self::RotationParameter(value) => {
                visit_two_point_parameter(&mut value.parameter, visit);
            }
            Self::UserParameter(value) => {
                visit_one_point_parameter(&mut value.parameter, visit);
                visit(&mut value.associated_variable);
                visit_block_value(&mut value.value, visit);
            }
            Self::VisibilityParameter(value) => {
                value.visit_handles_mut(visit);
            }
            Self::XYParameter(value) => {
                visit_two_point_parameter(&mut value.parameter, visit);
            }
            Self::AngularConstraintParameter(value) => {
                visit_constraint(&mut value.constraint, visit);
            }
            Self::DiametricConstraintParameter(value) | Self::RadialConstraintParameter(value) => {
                visit_constraint(&mut value.constraint, visit);
            }
            Self::AlignedConstraintParameter(value)
            | Self::LinearConstraintParameter(value)
            | Self::HorizontalConstraintParameter(value)
            | Self::VerticalConstraintParameter(value) => {
                visit_constraint(&mut value.constraint, visit);
            }
            Self::MoveAction(value) => {
                visit_block_action(&mut value.action, visit);
            }
            Self::FlipAction(value) => {
                visit_block_action(&mut value.action, visit);
            }
            Self::RotateAction(value) | Self::ScaleAction(value) => {
                visit_action_with_base_point(&mut value.action, visit);
            }
            Self::ArrayAction(value) => {
                visit_block_action(&mut value.action, visit);
            }
            Self::LookupAction(value) => {
                visit_block_action(&mut value.action, visit);
            }
            Self::StretchAction(value) => {
                visit_block_action(&mut value.action, visit);
                for item in &mut value.handles {
                    visit(&mut item.handle);
                }
            }
            Self::PolarStretchAction(value) => {
                visit_block_action(&mut value.action, visit);
                for handle in &mut value.handles {
                    visit(handle);
                }
                for item in &mut value.bindings {
                    visit(&mut item.handle);
                }
            }
            Self::AngularConstraintParameterEntity(value) => {
                visit_constraint(&mut value.constraint, visit);
            }
            Self::EvaluationGraph(value) => {
                for node in &mut value.nodes {
                    visit(&mut node.expression);
                }
            }
            Self::SolidHistory(value) => visit(&mut value.owner),
            Self::SolidHistoryNode(value) => {
                visit_solid_history_operation(value, visit);
            }
        }
    }
}

fn visit_block_value(value: &mut BlockEvalValue, visit: &mut impl FnMut(&mut Handle)) {
    if let BlockEvalValue::Handle(handle) = value {
        visit(handle);
    }
}

fn visit_block_eval(value: &mut BlockEvalExpression, visit: &mut impl FnMut(&mut Handle)) {
    visit_block_value(&mut value.value, visit);
}

fn visit_block_element(value: &mut BlockElement, visit: &mut impl FnMut(&mut Handle)) {
    visit_block_eval(&mut value.eval, visit);
}

fn visit_one_point_parameter(
    value: &mut BlockOnePointParameter,
    visit: &mut impl FnMut(&mut Handle),
) {
    visit_block_element(&mut value.parameter.element, visit);
}

fn visit_two_point_parameter(
    value: &mut BlockTwoPointParameter,
    visit: &mut impl FnMut(&mut Handle),
) {
    visit_block_element(&mut value.parameter.element, visit);
}

fn visit_block_grip(value: &mut BlockGrip, visit: &mut impl FnMut(&mut Handle)) {
    visit_block_element(&mut value.element, visit);
}

fn visit_block_action(value: &mut BlockAction, visit: &mut impl FnMut(&mut Handle)) {
    visit_block_element(&mut value.element, visit);
    for handle in &mut value.dependencies {
        visit(handle);
    }
}

fn visit_action_with_base_point(
    value: &mut BlockActionWithBasePoint,
    visit: &mut impl FnMut(&mut Handle),
) {
    visit_block_action(&mut value.action, visit);
}

fn visit_constraint(value: &mut BlockConstraintParameter, visit: &mut impl FnMut(&mut Handle)) {
    visit_two_point_parameter(&mut value.parameter, visit);
    visit(&mut value.dependency);
}

fn visit_solid_history_base(value: &mut SolidHistoryNodeBase, visit: &mut impl FnMut(&mut Handle)) {
    visit_block_eval(&mut value.eval, visit);
    visit(&mut value.material);
}

fn visit_solid_history_operation(
    value: &mut SolidHistoryOperation,
    visit: &mut impl FnMut(&mut Handle),
) {
    match value {
        SolidHistoryOperation::Unknown => {}
        SolidHistoryOperation::Box(value) | SolidHistoryOperation::Wedge(value) => {
            visit_solid_history_base(&mut value.base, visit);
        }
        SolidHistoryOperation::Sphere(value) => {
            visit_solid_history_base(&mut value.base, visit);
        }
        SolidHistoryOperation::Cylinder(value) => {
            visit_solid_history_base(&mut value.base, visit);
        }
        SolidHistoryOperation::Cone(value) => {
            visit_solid_history_base(&mut value.base, visit);
        }
        SolidHistoryOperation::Pyramid(value) => {
            visit_solid_history_base(&mut value.base, visit);
        }
        SolidHistoryOperation::Torus(value) => {
            visit_solid_history_base(&mut value.base, visit);
        }
        SolidHistoryOperation::Boolean(value) => {
            visit_solid_history_base(&mut value.base, visit);
        }
        SolidHistoryOperation::Brep(value) => {
            visit_solid_history_base(&mut value.base, visit);
        }
        SolidHistoryOperation::Fillet(value) => {
            visit_solid_history_base(&mut value.base, visit);
        }
        SolidHistoryOperation::Chamfer(value) => {
            visit_solid_history_base(&mut value.base, visit);
        }
        SolidHistoryOperation::Sweep(value) | SolidHistoryOperation::Extrusion(value) => {
            visit_solid_history_base(&mut value.base, visit);
        }
        SolidHistoryOperation::Loft(value) => {
            visit_solid_history_base(&mut value.base, visit);
        }
        SolidHistoryOperation::Revolve(value) => {
            visit_solid_history_base(&mut value.base, visit);
        }
    }
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BlockEvalExpression {
    pub parent_id: i32,
    pub major: i32,
    pub minor: i32,
    pub value_code: i16,
    pub value: BlockEvalValue,
    pub node_id: i32,
}

impl BlockEvalExpression {
    pub const NO_PARENT: i32 = -1;
}

impl Default for BlockEvalExpression {
    fn default() -> Self {
        Self {
            parent_id: Self::NO_PARENT,
            major: 0,
            minor: 0,
            value_code: 0,
            value: BlockEvalValue::default(),
            node_id: 0,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BlockElement {
    pub eval: BlockEvalExpression,
    pub name: String,
    pub major: i32,
    pub minor: i32,
    pub eed_1071: i32,
}

#[derive(Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BlockConnection {
    pub code: i32,
    pub name: String,
}

#[derive(Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BlockParameterProperty {
    pub connections: Vec<BlockConnection>,
}

#[derive(Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BlockParameterValueSet {
    pub description: String,
    pub flags: i32,
    pub minimum: f64,
    pub maximum: f64,
    pub increment: f64,
    pub values: Vec<f64>,
}

#[derive(Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BlockParameter {
    pub element: BlockElement,
    pub show_properties: bool,
    pub chain_actions: bool,
}

#[derive(Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BlockOnePointParameter {
    pub parameter: BlockParameter,
    pub definition_point: Vector3,
    pub properties: [BlockParameterProperty; 2],
    pub property_count: i32,
}

#[derive(Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BlockTwoPointParameter {
    pub parameter: BlockParameter,
    pub definition_base_point: Vector3,
    pub definition_end_point: Vector3,
    pub properties: [BlockParameterProperty; 4],
    pub property_states: [i32; 4],
    pub parameter_base_location: i16,
    pub updated_base_point: Option<Vector3>,
    pub base_point: Option<Vector3>,
    pub updated_end_point: Option<Vector3>,
    pub end_point: Option<Vector3>,
}

#[derive(Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BlockGrip {
    pub element: BlockElement,
    pub flags_91: i32,
    pub flags_92: i32,
    pub location: Vector3,
    pub insert_cycling: bool,
    pub insert_cycling_weight: i32,
}

#[derive(Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BlockGripExpression {
    pub eval: BlockEvalExpression,
    pub grip_type: i32,
    pub expression: String,
}

#[derive(Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BlockOrientedGrip {
    pub grip: BlockGrip,
    pub orientation: Vector3,
}

#[derive(Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BlockFlipGrip {
    pub grip: BlockGrip,
    pub combined_state: i32,
    pub orientation: Vector3,
}

#[derive(Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BlockAction {
    pub element: BlockElement,
    pub display_location: Vector3,
    pub dependencies: Vec<Handle>,
    pub action_ids: Vec<i32>,
}

#[derive(Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BlockActionOffsets {
    pub distance_multiplier: f64,
    pub angle_offset: f64,
    pub flags: u8,
}

#[derive(Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BlockActionWithBasePoint {
    pub action: BlockAction,
    pub offset: Vector3,
    pub connections: Vec<BlockConnection>,
    pub dependent: bool,
    pub base_point: Vector3,
}

#[derive(Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BlockConstraintParameter {
    pub parameter: BlockTwoPointParameter,
    pub dependency: Handle,
}

#[derive(Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BlockLinearConstraintParameter {
    pub constraint: BlockConstraintParameter,
    pub expression_name: String,
    pub expression_description: String,
    pub value: f64,
    pub value_set: BlockParameterValueSet,
}

#[derive(Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BlockRepresentationData {
    pub flags: i16,
    pub block: Handle,
}

#[derive(Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BlockAlignmentParameter {
    pub parameter: BlockTwoPointParameter,
    pub align_perpendicular: bool,
}

#[derive(Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BlockLinearParameter {
    pub parameter: BlockTwoPointParameter,
    pub distance_name: String,
    pub distance_description: String,
    pub distance: f64,
    pub value_set: BlockParameterValueSet,
}

#[derive(Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BlockBasePointParameter {
    pub parameter: BlockOnePointParameter,
    pub point: Vector3,
    pub base_point: Vector3,
}

#[derive(Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BlockFlipParameter {
    pub parameter: BlockTwoPointParameter,
    pub flip_label: String,
    pub flip_label_description: String,
    pub base_state_label: String,
    pub flipped_state_label: String,
    pub definition_label_point: Vector3,
    pub flags_96: i32,
    pub tooltip: String,
}

#[derive(Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BlockXYParameter {
    pub parameter: BlockTwoPointParameter,
    pub x_label: String,
    pub x_label_description: String,
    pub y_label: String,
    pub y_label_description: String,
    pub x_value: f64,
    pub y_value: f64,
    pub x_value_set: BlockParameterValueSet,
    pub y_value_set: BlockParameterValueSet,
}

#[derive(Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BlockRotationParameter {
    pub parameter: BlockTwoPointParameter,
    pub definition_base_angle_point: Vector3,
    pub angle_name: String,
    pub angle_description: String,
    pub angle: f64,
    pub value_set: BlockParameterValueSet,
}

#[derive(Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BlockPolarParameter {
    pub parameter: BlockTwoPointParameter,
    pub angle_name: String,
    pub angle_description: String,
    pub distance_name: String,
    pub distance_description: String,
    pub offset: f64,
    pub angle_value_set: BlockParameterValueSet,
    pub distance_value_set: BlockParameterValueSet,
}

#[derive(Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BlockLookupParameter {
    pub parameter: BlockOnePointParameter,
    pub index: i32,
    pub lookup_name: String,
    pub lookup_description: String,
}

#[derive(Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BlockPointParameter {
    pub parameter: BlockOnePointParameter,
    pub position_name: String,
    pub position_description: String,
    pub definition_label_point: Vector3,
}

#[derive(Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BlockUserParameter {
    pub parameter: BlockOnePointParameter,
    pub flags: i16,
    pub associated_variable: Handle,
    pub expression: String,
    pub value_code: i16,
    pub value: BlockEvalValue,
    pub value_type: i16,
}

#[derive(Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BlockAngularConstraintParameter {
    pub constraint: BlockConstraintParameter,
    pub center_point: Vector3,
    pub end_point: Vector3,
    pub expression_name: String,
    pub expression_description: String,
    pub angle: f64,
    pub orientation_on_both_grips: bool,
    pub value_set: BlockParameterValueSet,
}

#[derive(Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BlockAngularConstraintParameterEntity {
    pub constraint: BlockConstraintParameter,
    pub center_point: Vector3,
    pub label_point: Vector3,
    pub expression_name: String,
    pub expression_description: String,
    pub angle: f64,
    pub orientation_on_both_grips: bool,
    pub value_set: BlockParameterValueSet,
}

#[derive(Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BlockDistanceConstraintParameter {
    pub constraint: BlockConstraintParameter,
    pub expression_name: String,
    pub expression_description: String,
    pub distance: f64,
    pub value_set: BlockParameterValueSet,
}

#[derive(Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BlockParameterDependencyBody {
    pub dependency_body_version: i16,
    pub dimension_base_version: i16,
    pub name: String,
    pub class_version: i16,
}

#[derive(Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BlockMoveAction {
    pub action: BlockAction,
    pub connections: [BlockConnection; 2],
    pub offsets: BlockActionOffsets,
}

#[derive(Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BlockFlipAction {
    pub action: BlockAction,
    pub connections: [BlockConnection; 4],
}

#[derive(Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BlockBasePointAction {
    pub action: BlockActionWithBasePoint,
    pub connections: Vec<BlockConnection>,
    /// DWG-only byte after a scale action's connections (zero in every
    /// sample seen; rotate actions carry none, and DXF has no group for
    /// it). Strict readers read it, so a record written without it is
    /// rejected as improperly read.
    #[cfg_attr(feature = "serde", serde(default))]
    pub dwg_tail_byte: u8,
}

#[derive(Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BlockArrayAction {
    pub action: BlockAction,
    pub connections: [BlockConnection; 4],
    pub column_offset: f64,
    pub row_offset: f64,
}

#[derive(Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BlockLookupColumn {
    pub node_id: i32,
    pub value_type: i32,
    pub property_type: i32,
    pub lookup_property: bool,
    pub unmatched_name: String,
    pub writable: bool,
    pub connection_name: String,
}

#[derive(Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BlockLookupAction {
    pub action: BlockAction,
    pub row_count: i32,
    pub column_count: i32,
    pub expressions: Vec<String>,
    pub columns: Vec<BlockLookupColumn>,
    pub flag_280: bool,
}

/// Ordered fields belonging to a native Block Properties Table subclass.
/// Retaining the original group-code sequence preserves fields that are not
/// yet interpreted by the application.
#[derive(Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BlockPropertiesTable {
    pub parameter: BlockOnePointParameter,
    pub fields: Vec<BlockPropertiesTableField>,
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BlockPropertiesTableField {
    pub code: i32,
    pub value: BlockPropertiesTableValue,
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum BlockPropertiesTableValue {
    Text(String),
    Boolean(bool),
    Integer(i64),
    Real(f64),
    Handle(Handle),
    Binary(Vec<u8>),
}

impl Default for BlockPropertiesTableValue {
    fn default() -> Self {
        Self::Text(String::new())
    }
}

#[derive(Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BlockStretchHandle {
    pub handle: Handle,
    pub indexes: Vec<i32>,
}

#[derive(Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BlockStretchCode {
    pub code: i32,
    pub indexes: Vec<i32>,
}

#[derive(Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BlockStretchAction {
    pub action: BlockAction,
    pub connections: [BlockConnection; 2],
    pub points: Vec<Vector2>,
    pub handles: Vec<BlockStretchHandle>,
    pub codes: Vec<BlockStretchCode>,
    pub offsets: BlockActionOffsets,
}

#[derive(Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BlockPolarStretchAction {
    pub action: BlockAction,
    pub connections: [BlockConnection; 6],
    pub points: Vec<Vector2>,
    pub handles: Vec<Handle>,
    pub bindings: Vec<BlockStretchHandle>,
    pub codes: Vec<BlockStretchCode>,
    pub distance_multiplier: f64,
    pub angle_offset: f64,
    pub extra: Vec<i32>,
}

#[derive(Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BlockEvaluationNode {
    pub id: i32,
    pub edge_flags: i32,
    pub next_id: i32,
    pub expression: Handle,
    pub node_data: [i32; 4],
    pub active_cycles: Option<bool>,
}

#[derive(Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BlockEvaluationEdge {
    pub id: i32,
    pub next_id: i32,
    pub incoming_edge: i32,
    pub source_node: i32,
    pub destination_node: i32,
    pub outgoing_edges: [i32; 5],
}

#[derive(Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BlockEvaluationGraph {
    pub first_node_id: i32,
    pub first_node_id_copy: i32,
    pub nodes: Vec<BlockEvaluationNode>,
    pub edges: Vec<BlockEvaluationEdge>,
}

#[derive(Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SolidHistory {
    pub major: i32,
    pub minor: i32,
    pub owner: Handle,
    pub history_node_id: i32,
    pub show_history: bool,
    pub record_history: bool,
}

#[derive(Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SolidHistoryNodeBase {
    pub eval: BlockEvalExpression,
    pub major: i32,
    pub minor: i32,
    pub transform: [f64; 16],
    pub color: Color,
    pub step_id: i32,
    pub material: Handle,
}

impl SolidHistoryNodeBase {
    pub const ROOT_PARENT: i32 = BlockEvalExpression::NO_PARENT;
    pub const NO_VALUE: i16 = -9999;

    /// The id other nodes and the history root name this node by: its
    /// evaluation node id, or its step id in histories that carry none.
    pub fn node_id(&self) -> i32 {
        if self.eval.node_id > 0 {
            self.eval.node_id
        } else {
            self.step_id
        }
    }

    pub fn new(step_id: i32) -> Self {
        Self {
            eval: BlockEvalExpression {
                parent_id: Self::ROOT_PARENT,
                major: 1,
                value_code: Self::NO_VALUE,
                node_id: step_id,
                ..BlockEvalExpression::default()
            },
            major: 1,
            transform: [
                1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0,
            ],
            step_id,
            ..Self::default()
        }
    }

    pub(crate) fn saved_eval(&self) -> BlockEvalExpression {
        let mut eval = self.eval.clone();
        if eval.parent_id == 0 {
            eval.parent_id = Self::ROOT_PARENT;
        }
        if eval.value_code == 0 && eval.value == BlockEvalValue::None {
            eval.value_code = Self::NO_VALUE;
        }
        eval
    }
}

#[derive(Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum SolidHistoryOperation {
    #[default]
    Unknown,
    Box(SolidHistoryBox),
    Wedge(SolidHistoryBox),
    Sphere(SolidHistorySphere),
    Cylinder(SolidHistoryCylinder),
    Cone(SolidHistoryCone),
    Pyramid(SolidHistoryPyramid),
    Torus(SolidHistoryTorus),
    Boolean(SolidHistoryBoolean),
    Brep(SolidHistoryBrep),
    Fillet(SolidHistoryFillet),
    Chamfer(SolidHistoryChamfer),
    Sweep(SolidHistorySweep),
    Extrusion(SolidHistorySweep),
    Loft(SolidHistoryLoft),
    Revolve(SolidHistoryRevolve),
}

impl SolidHistoryOperation {
    pub fn base(&self) -> Option<&SolidHistoryNodeBase> {
        match self {
            Self::Unknown => None,
            Self::Box(value) | Self::Wedge(value) => Some(&value.base),
            Self::Sphere(value) => Some(&value.base),
            Self::Cylinder(value) => Some(&value.base),
            Self::Cone(value) => Some(&value.base),
            Self::Pyramid(value) => Some(&value.base),
            Self::Torus(value) => Some(&value.base),
            Self::Boolean(value) => Some(&value.base),
            Self::Brep(value) => Some(&value.base),
            Self::Fillet(value) => Some(&value.base),
            Self::Chamfer(value) => Some(&value.base),
            Self::Sweep(value) | Self::Extrusion(value) => Some(&value.base),
            Self::Loft(value) => Some(&value.base),
            Self::Revolve(value) => Some(&value.base),
        }
    }

    pub fn base_mut(&mut self) -> Option<&mut SolidHistoryNodeBase> {
        match self {
            Self::Unknown => None,
            Self::Box(value) | Self::Wedge(value) => Some(&mut value.base),
            Self::Sphere(value) => Some(&mut value.base),
            Self::Cylinder(value) => Some(&mut value.base),
            Self::Cone(value) => Some(&mut value.base),
            Self::Pyramid(value) => Some(&mut value.base),
            Self::Torus(value) => Some(&mut value.base),
            Self::Boolean(value) => Some(&mut value.base),
            Self::Brep(value) => Some(&mut value.base),
            Self::Fillet(value) => Some(&mut value.base),
            Self::Chamfer(value) => Some(&mut value.base),
            Self::Sweep(value) | Self::Extrusion(value) => Some(&mut value.base),
            Self::Loft(value) => Some(&mut value.base),
            Self::Revolve(value) => Some(&mut value.base),
        }
    }

    pub fn class_names(&self) -> Option<(&'static str, &'static str)> {
        Some(match self {
            Self::Unknown => return None,
            Self::Box(_) => ("ACSH_BOX_CLASS", "AcDbShBox"),
            Self::Wedge(_) => ("ACSH_WEDGE_CLASS", "AcDbShWedge"),
            Self::Sphere(_) => ("ACSH_SPHERE_CLASS", "AcDbShSphere"),
            Self::Cylinder(_) => ("ACSH_CYLINDER_CLASS", "AcDbShCylinder"),
            Self::Cone(_) => ("ACSH_CONE_CLASS", "AcDbShCone"),
            Self::Pyramid(_) => ("ACSH_PYRAMID_CLASS", "AcDbShPyramid"),
            Self::Torus(_) => ("ACSH_TORUS_CLASS", "AcDbShTorus"),
            Self::Boolean(_) => ("ACSH_BOOLEAN_CLASS", "AcDbShBoolean"),
            Self::Brep(_) => ("ACSH_BREP_CLASS", "AcDbShBrep"),
            Self::Fillet(_) => ("ACSH_FILLET_CLASS", "AcDbShFillet"),
            Self::Chamfer(_) => ("ACSH_CHAMFER_CLASS", "AcDbShChamfer"),
            Self::Sweep(_) => ("ACSH_SWEEP_CLASS", "AcDbShSweep"),
            Self::Extrusion(_) => ("ACSH_EXTRUSION_CLASS", "AcDbShExtrusion"),
            Self::Loft(_) => ("ACSH_LOFT_CLASS", "AcDbShLoft"),
            Self::Revolve(_) => ("ACSH_REVOLVE_CLASS", "AcDbShRevolve"),
        })
    }
}

#[derive(Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SolidHistoryBox {
    pub base: SolidHistoryNodeBase,
    pub operation_major: i32,
    pub operation_minor: i32,
    pub length: f64,
    pub width: f64,
    pub height: f64,
}

#[derive(Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SolidHistorySphere {
    pub base: SolidHistoryNodeBase,
    pub operation_major: i32,
    pub operation_minor: i32,
    pub radius: f64,
}

#[derive(Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SolidHistoryCylinder {
    pub base: SolidHistoryNodeBase,
    pub operation_major: i32,
    pub operation_minor: i32,
    pub height: f64,
    pub major_radius: f64,
    pub minor_radius: f64,
    pub x_radius: f64,
}

/// Parametric cone or frustum history.
///
/// The three stored radii are the two base semi-axes and the top major
/// radius. Keeping this separate from cylinder history prevents the top
/// radius from being mistaken for a third copy of the base radius.
#[derive(Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SolidHistoryCone {
    pub base: SolidHistoryNodeBase,
    pub operation_major: i32,
    pub operation_minor: i32,
    pub height: f64,
    pub base_x_radius: f64,
    pub base_y_radius: f64,
    pub top_radius: f64,
}

#[derive(Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SolidHistoryPyramid {
    pub base: SolidHistoryNodeBase,
    pub operation_major: i32,
    pub operation_minor: i32,
    pub height: f64,
    pub sides: i32,
    pub radius: f64,
    pub top_radius: f64,
}

#[derive(Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SolidHistoryTorus {
    pub base: SolidHistoryNodeBase,
    pub operation_major: i32,
    pub operation_minor: i32,
    pub major_radius: f64,
    pub minor_radius: f64,
}

#[derive(Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SolidHistoryBoolean {
    pub base: SolidHistoryNodeBase,
    pub operation_major: i32,
    pub operation_minor: i32,
    pub operation: u8,
    pub first_operand: i32,
    pub second_operand: i32,
}

impl SolidHistoryBoolean {
    /// `operation` codes.
    pub const UNION: u8 = 0;
    pub const INTERSECT: u8 = 1;
    pub const SUBTRACT: u8 = 2;
}

/// A solid's history as it evaluates: each operation over the results of its
/// operands, in order. A primitive has none, an edge operation one, a boolean
/// two.
#[derive(Debug, Clone, PartialEq)]
pub struct SolidHistoryTree {
    pub operation: SolidHistoryOperation,
    pub operands: Vec<SolidHistoryTree>,
}

impl SolidHistoryTree {
    /// The operation whose node id is `id`, anywhere in the tree.
    pub fn find(&self, id: i32) -> Option<&SolidHistoryTree> {
        if self
            .operation
            .base()
            .is_some_and(|base| base.node_id() == id)
        {
            return Some(self);
        }
        self.operands.iter().find_map(|operand| operand.find(id))
    }

    pub fn find_mut(&mut self, id: i32) -> Option<&mut SolidHistoryTree> {
        if self
            .operation
            .base()
            .is_some_and(|base| base.node_id() == id)
        {
            return Some(self);
        }
        self.operands
            .iter_mut()
            .find_map(|operand| operand.find_mut(id))
    }

    /// Whether any step combines two solids.
    pub fn has_boolean(&self) -> bool {
        matches!(self.operation, SolidHistoryOperation::Boolean(_))
            || self.operands.iter().any(Self::has_boolean)
    }

    /// The operations with no operands — the primitives and profiles the
    /// solid was built from — in operand order.
    pub fn leaves(&self) -> Vec<&SolidHistoryOperation> {
        if self.operands.is_empty() {
            return vec![&self.operation];
        }
        self.operands.iter().flat_map(Self::leaves).collect()
    }
}

#[derive(Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SolidHistoryBrep {
    pub base: SolidHistoryNodeBase,
    pub operation_major: i32,
    pub operation_minor: i32,
    pub acis_data: AcisData,
}

#[derive(Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SolidHistoryFillet {
    pub base: SolidHistoryNodeBase,
    pub operation_major: i32,
    pub operation_minor: i32,
    pub method: i32,
    pub edges: Vec<i32>,
    pub radii: Vec<f64>,
    pub start_setbacks: Vec<f64>,
    pub end_setbacks: Vec<f64>,
}

#[derive(Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SolidHistoryChamfer {
    pub base: SolidHistoryNodeBase,
    pub operation_major: i32,
    pub operation_minor: i32,
    pub method: i32,
    pub base_distance: f64,
    pub other_distance: f64,
    pub edges: Vec<i32>,
    pub base_face: i32,
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SolidHistorySweep {
    pub base: SolidHistoryNodeBase,
    pub operation_major: i32,
    pub operation_minor: i32,
    pub direction: Vector3,
    pub sweep_entity: Option<crate::entities::EmbeddedEntity>,
    pub path_entity: Option<crate::entities::EmbeddedEntity>,
    pub draft_angle: f64,
    pub start_draft_distance: f64,
    pub end_draft_distance: f64,
    pub scale_factor: f64,
    pub twist_angle: f64,
    pub align_angle: f64,
    pub sweep_entity_transform: [f64; 16],
    pub path_entity_transform: [f64; 16],
    pub align_option: u8,
    pub miter_option: u8,
    pub has_align_start: bool,
    pub align_start: bool,
    pub bank: bool,
    pub flags_294_296: [bool; 3],
    pub reference_point: Vector3,
    #[cfg_attr(feature = "serde", serde(default = "unit_xyz"))]
    pub dwg_vector: Vector3,
}

#[cfg(feature = "serde")]
fn unit_xyz() -> Vector3 {
    Vector3::new(1.0, 1.0, 1.0)
}

impl Default for SolidHistorySweep {
    fn default() -> Self {
        Self {
            base: Default::default(),
            operation_major: 0,
            operation_minor: 0,
            direction: Vector3::ZERO,
            sweep_entity: None,
            path_entity: None,
            draft_angle: 0.0,
            start_draft_distance: 0.0,
            end_draft_distance: 0.0,
            scale_factor: 0.0,
            twist_angle: 0.0,
            align_angle: 0.0,
            sweep_entity_transform: [0.0; 16],
            path_entity_transform: [0.0; 16],
            align_option: 0,
            miter_option: 0,
            has_align_start: false,
            align_start: true,
            bank: false,
            flags_294_296: [false; 3],
            reference_point: Vector3::ZERO,
            dwg_vector: Vector3::new(1.0, 1.0, 1.0),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SolidHistoryLoft {
    pub base: SolidHistoryNodeBase,
    pub operation_major: i32,
    pub operation_minor: i32,
    pub cross_sections: Vec<crate::entities::EmbeddedEntity>,
    pub guides: Vec<crate::entities::EmbeddedEntity>,
    /// Optional construction settings retained in a versioned extension record.
    /// The native history stream remains unchanged for other consumers.
    #[cfg_attr(feature = "serde", serde(default))]
    pub parameters: Option<SolidHistoryLoftParameters>,
    #[cfg_attr(feature = "serde", serde(default))]
    pub path_entity: Option<crate::entities::EmbeddedEntity>,
    #[cfg_attr(feature = "serde", serde(default))]
    pub options: SolidHistoryLoftOptions,
}

impl SolidHistoryLoft {
    pub(crate) fn native(
        &self,
    ) -> (
        Option<&crate::entities::EmbeddedEntity>,
        SolidHistoryLoftOptions,
    ) {
        let mut options = self.options.clone();
        let Some(parameters) = &self.parameters else {
            return (self.path_entity.as_ref(), options);
        };
        options.start_draft_angle = parameters.start_draft_angle;
        options.end_draft_angle = parameters.end_draft_angle;
        options.start_magnitude = parameters.start_magnitude;
        options.end_magnitude = parameters.end_magnitude;
        (
            parameters
                .path_entity
                .as_ref()
                .or(self.path_entity.as_ref()),
            options,
        )
    }
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(default))]
pub struct SolidHistoryLoftOptions {
    pub surface_option: i32,
    pub start_draft_angle: f64,
    pub end_draft_angle: f64,
    pub start_magnitude: f64,
    pub end_magnitude: f64,
    pub flags: [bool; 8],
}

impl Default for SolidHistoryLoftOptions {
    fn default() -> Self {
        Self {
            surface_option: 0,
            start_draft_angle: std::f64::consts::FRAC_PI_2,
            end_draft_angle: std::f64::consts::FRAC_PI_2,
            start_magnitude: 0.0,
            end_magnitude: 0.0,
            flags: [false, true, true, true, false, true, false, true],
        }
    }
}

/// Parametric loft settings. Angles are radians; magnitudes are nonnegative.
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(default))]
pub struct SolidHistoryLoftParameters {
    pub path_entity: Option<crate::entities::EmbeddedEntity>,
    /// 0 ruled, 1 smooth, 2 first, 3 last, 4 ends, 5 all, 6 draft angles.
    pub normals: i32,
    pub start_draft_angle: f64,
    pub end_draft_angle: f64,
    pub start_magnitude: f64,
    pub end_magnitude: f64,
    /// Continuity and dimensionless bulge at point ends only.
    pub start_continuity: i32,
    pub end_continuity: i32,
    pub start_bulge: f64,
    pub end_bulge: f64,
    pub closed: bool,
    pub periodic: bool,
    pub surface: bool,
    pub align_direction: bool,
    /// Number of embedded members in each joined cross section. Empty means
    /// one independent section per embedded entity.
    pub section_counts: Vec<usize>,
}

impl Default for SolidHistoryLoftParameters {
    fn default() -> Self {
        Self {
            path_entity: None,
            normals: 1,
            start_draft_angle: std::f64::consts::FRAC_PI_2,
            end_draft_angle: std::f64::consts::FRAC_PI_2,
            start_magnitude: 0.0,
            end_magnitude: 0.0,
            start_continuity: 1,
            end_continuity: 1,
            start_bulge: 0.5,
            end_bulge: 0.5,
            closed: false,
            periodic: true,
            surface: false,
            align_direction: true,
            section_counts: Vec::new(),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SolidHistoryRevolve {
    pub base: SolidHistoryNodeBase,
    pub operation_major: i32,
    pub operation_minor: i32,
    pub axis_point: Vector3,
    pub direction: Vector3,
    pub revolve_angle: f64,
    pub start_angle: f64,
    pub draft_angle: f64,
    pub field_44: f64,
    pub field_45: f64,
    pub twist_angle: f64,
    pub flag_290: bool,
    pub close_to_axis: bool,
    pub sweep_entity: Option<crate::entities::EmbeddedEntity>,
}

/// Whether a DXF class name is a dynamic-block object record (parameters,
/// actions, grips, the evaluation graph and its helpers) — the names the DWG
/// dynamic-block reader decodes. Entity classes are not included.
pub fn is_dynamic_block_object_name(name: &str) -> bool {
    matches!(
        name.to_ascii_uppercase().as_str(),
        "ACAD_EVALUATION_GRAPH"
            | "ACDBBLOCKPARAMDEPENDENCYBODY"
            | "ACDB_BLOCKREPRESENTATION_DATA"
            | "ACDB_DYNAMICBLOCKPROXYNODE"
            | "ACDB_DYNAMICBLOCKPURGEPREVENTER_VERSION"
            | "BLOCKALIGNEDCONSTRAINTPARAMETER"
            | "BLOCKALIGNMENTGRIP"
            | "BLOCKALIGNMENTPARAMETER"
            | "BLOCKANGULARCONSTRAINTPARAMETER"
            | "BLOCKARRAYACTION"
            | "BLOCKBASEPOINTPARAMETER"
            | "BLOCKDIAMETRICCONSTRAINTPARAMETER"
            | "BLOCKFLIPACTION"
            | "BLOCKFLIPGRIP"
            | "BLOCKFLIPPARAMETER"
            | "BLOCKGRIPLOCATIONCOMPONENT"
            | "BLOCKHORIZONTALCONSTRAINTPARAMETER"
            | "BLOCKLINEARCONSTRAINTPARAMETER"
            | "BLOCKLINEARGRIP"
            | "BLOCKLINEARPARAMETER"
            | "BLOCKLOOKUPACTION"
            | "BLOCKLOOKUPGRIP"
            | "BLOCKLOOKUPPARAMETER"
            | "BLOCKMOVEACTION"
            | "BLOCKPARAMDEPENDENCYBODY"
            | "BLOCKPOINTPARAMETER"
            | "BLOCKPOLARGRIP"
            | "BLOCKPOLARPARAMETER"
            | "BLOCKPOLARSTRETCHACTION"
            | "BLOCKPROPERTIESTABLE"
            | "BLOCKPROPERTIESTABLEGRIP"
            | "BLOCKRADIALCONSTRAINTPARAMETER"
            | "BLOCKREPRESENTATION"
            | "BLOCKROTATEACTION"
            | "BLOCKROTATIONGRIP"
            | "BLOCKROTATIONPARAMETER"
            | "BLOCKSCALEACTION"
            | "BLOCKSTRETCHACTION"
            | "BLOCKUSERPARAMETER"
            | "BLOCKVERTICALCONSTRAINTPARAMETER"
            | "BLOCKVISIBILITYGRIP"
            | "BLOCKXYGRIP"
            | "BLOCKXYPARAMETER"
            | "DYNAMICBLOCKPROXYNODE"
            | "DYNAMICBLOCKPURGEPREVENTER"
            | "EVALUATION_GRAPH"
    )
}