ferrocad_occt 0.1.4

OCCT geometry backend for FerroCAD: implements the ferrocad_geom GeometryBackend
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
//! The `ShapeFix_*` fixers behind `Part.ShapeFix`.
//!
//! A sibling cxx bridge over `include/fc_shapefix.hxx` (the same pattern as
//! [`crate::bridge`]): OCCT 7.x's shape-fixing package is not bridged by
//! `opencascade-sys`, so the header wraps each fixer and this module exposes it
//! as a plain Rust object. The fixers own `Handle(ShapeFix_*)`, which is why
//! every bridge method is `const` and no `Pin` appears here.
//!
//! Input shapes are [`ferrocad_geom::Shape`] values (they must come from this
//! backend); outputs are owned `Shape` values again. A failed cast or an OCCT
//! exception becomes `false`/`None`, which the Python layer reports as
//! `Part.OCCError` rather than inventing a result.

use cxx::UniquePtr;
use ferrocad_geom::Shape;
use opencascade_sys::topo_ds::TopoDS_Shape;

use crate::shape::{downcast, OcctShape};

#[cxx::bridge]
mod ffi {
    unsafe extern "C++" {
            include!("ferrocad_occt/include/fc_shapefix.hxx");

            type TopoDS_Shape = opencascade_sys::topo_ds::TopoDS_Shape;

            type FcShapeFixRoot;
            type FcShapeFixEdge;
            type FcShapeFixWire;
            type FcShapeFixFace;
            type FcShapeFixShell;
            type FcShapeFixSolid;
            type FcShapeFixShape;
            type FcShapeList;

            // --- Root ------------------------------------------------------
            fn fc_shapefix_root_new() -> UniquePtr<FcShapeFixRoot>;
            fn set_precision(self: &FcShapeFixRoot, value: f64);
            fn precision(self: &FcShapeFixRoot) -> f64;
            fn set_min_tolerance(self: &FcShapeFixRoot, value: f64);
            fn min_tolerance(self: &FcShapeFixRoot) -> f64;
            fn set_max_tolerance(self: &FcShapeFixRoot, value: f64);
            fn max_tolerance(self: &FcShapeFixRoot) -> f64;
            fn limit_tolerance(self: &FcShapeFixRoot, value: f64) -> f64;

            // --- Edge ------------------------------------------------------
            fn fc_shapefix_edge_new() -> UniquePtr<FcShapeFixEdge>;
            fn fix_remove_pcurve_face(
                self: &FcShapeFixEdge,
                edge: &TopoDS_Shape,
                face: &TopoDS_Shape,
            ) -> bool;
            fn fix_remove_pcurve_surface(
                self: &FcShapeFixEdge,
                edge: &TopoDS_Shape,
                nx: f64,
                ny: f64,
                nz: f64,
            ) -> bool;
            fn fix_remove_curve3d(self: &FcShapeFixEdge, edge: &TopoDS_Shape) -> bool;
            fn fix_add_pcurve_face(
                self: &FcShapeFixEdge,
                edge: &TopoDS_Shape,
                face: &TopoDS_Shape,
                seam: bool,
                prec: f64,
            ) -> bool;
            fn fix_add_pcurve_surface(
                self: &FcShapeFixEdge,
                edge: &TopoDS_Shape,
                nx: f64,
                ny: f64,
                nz: f64,
                seam: bool,
                prec: f64,
            ) -> bool;
            fn fix_add_curve3d(self: &FcShapeFixEdge, edge: &TopoDS_Shape) -> bool;
            fn fix_vertex_tolerance_face(
                self: &FcShapeFixEdge,
                edge: &TopoDS_Shape,
                face: &TopoDS_Shape,
            ) -> bool;
            fn fix_vertex_tolerance(self: &FcShapeFixEdge, edge: &TopoDS_Shape) -> bool;
            fn fix_reversed2d_face(
                self: &FcShapeFixEdge,
                edge: &TopoDS_Shape,
                face: &TopoDS_Shape,
            ) -> bool;
            fn fix_reversed2d_surface(
                self: &FcShapeFixEdge,
                edge: &TopoDS_Shape,
                nx: f64,
                ny: f64,
                nz: f64,
            ) -> bool;
            fn fix_same_parameter(
                self: &FcShapeFixEdge,
                edge: &TopoDS_Shape,
                tolerance: f64,
            ) -> bool;
            fn fix_same_parameter_face(
                self: &FcShapeFixEdge,
                edge: &TopoDS_Shape,
                face: &TopoDS_Shape,
                tolerance: f64,
            ) -> bool;

            // --- Wire ------------------------------------------------------
            fn fc_shapefix_wire_new() -> UniquePtr<FcShapeFixWire>;
            fn fc_shapefix_wire_from_wire(
                wire: &TopoDS_Shape,
                face: &TopoDS_Shape,
                prec: f64,
            ) -> UniquePtr<FcShapeFixWire>;
            fn init(
                self: &FcShapeFixWire,
                wire: &TopoDS_Shape,
                face: &TopoDS_Shape,
                prec: f64,
            );
            fn load(self: &FcShapeFixWire, wire: &TopoDS_Shape) -> bool;
            fn set_face(self: &FcShapeFixWire, face: &TopoDS_Shape) -> bool;
            fn set_surface(
                self: &FcShapeFixWire,
                ox: f64,
                oy: f64,
                oz: f64,
                nx: f64,
                ny: f64,
                nz: f64,
            ) -> bool;
            fn set_surface_placed(
                self: &FcShapeFixWire,
                ox: f64,
                oy: f64,
                oz: f64,
                nx: f64,
                ny: f64,
                nz: f64,
            ) -> bool;
            fn set_max_tail_angle(self: &FcShapeFixWire, angle: f64);
            fn set_max_tail_width(self: &FcShapeFixWire, width: f64);
            fn is_loaded(self: &FcShapeFixWire) -> bool;
            fn is_ready(self: &FcShapeFixWire) -> bool;
            fn nb_edges(self: &FcShapeFixWire) -> i32;
            fn wire(self: &FcShapeFixWire) -> UniquePtr<TopoDS_Shape>;
            fn wire_api_make(self: &FcShapeFixWire) -> UniquePtr<TopoDS_Shape>;
            fn face(self: &FcShapeFixWire) -> UniquePtr<TopoDS_Shape>;
            fn perform(self: &FcShapeFixWire) -> bool;
            fn clear_modes(self: &FcShapeFixWire);
            fn clear_statuses(self: &FcShapeFixWire);
            fn fix_edge_tool(self: &FcShapeFixWire) -> UniquePtr<FcShapeFixEdge>;
            fn fix_reorder(self: &FcShapeFixWire) -> bool;
            fn fix_small_global(self: &FcShapeFixWire, lock: bool, prec: f64) -> i32;
            fn fix_small_one(
                self: &FcShapeFixWire,
                num: i32,
                lock: bool,
                prec: f64,
            ) -> bool;
            fn fix_connected(self: &FcShapeFixWire, prec: f64) -> bool;
            fn fix_connected_one(self: &FcShapeFixWire, num: i32, prec: f64) -> bool;
            fn fix_edge_curves(self: &FcShapeFixWire) -> bool;
            fn fix_degenerated(self: &FcShapeFixWire) -> bool;
            fn fix_degenerated_one(self: &FcShapeFixWire, num: i32) -> bool;
            fn fix_self_intersection(self: &FcShapeFixWire) -> bool;
            fn fix_lacking(self: &FcShapeFixWire, force: bool) -> bool;
            fn fix_lacking_one(self: &FcShapeFixWire, num: i32, force: bool) -> bool;
            fn fix_closed(self: &FcShapeFixWire, prec: f64) -> bool;
            fn fix_gaps3d(self: &FcShapeFixWire) -> bool;
            fn fix_gaps2d(self: &FcShapeFixWire) -> bool;
            fn fix_seam(self: &FcShapeFixWire, num: i32) -> bool;
            fn fix_shifted(self: &FcShapeFixWire) -> bool;
            fn fix_notched_edges(self: &FcShapeFixWire) -> bool;
            fn fix_gap3d(self: &FcShapeFixWire, num: i32, convert: bool) -> bool;
            fn fix_gap2d(self: &FcShapeFixWire, num: i32, convert: bool) -> bool;
            fn fix_tails(self: &FcShapeFixWire) -> bool;
            fn modify_topology_mode(self: &FcShapeFixWire) -> bool;
            fn set_modify_topology_mode(self: &FcShapeFixWire, value: bool);
            fn modify_geometry_mode(self: &FcShapeFixWire) -> bool;
            fn set_modify_geometry_mode(self: &FcShapeFixWire, value: bool);
            fn closed_wire_mode(self: &FcShapeFixWire) -> bool;
            fn set_closed_wire_mode(self: &FcShapeFixWire, value: bool);
            fn preference_pcurve_mode(self: &FcShapeFixWire) -> bool;
            fn set_preference_pcurve_mode(self: &FcShapeFixWire, value: bool);
            fn fix_gaps_by_ranges_mode(self: &FcShapeFixWire) -> bool;
            fn set_fix_gaps_by_ranges_mode(self: &FcShapeFixWire, value: bool);
            fn modify_remove_loop_mode(self: &FcShapeFixWire) -> i32;
            fn set_modify_remove_loop_mode(self: &FcShapeFixWire, value: i32);
            fn fix_reorder_mode(self: &FcShapeFixWire) -> i32;
            fn set_fix_reorder_mode(self: &FcShapeFixWire, value: i32);
            fn fix_small_mode(self: &FcShapeFixWire) -> i32;
            fn set_fix_small_mode(self: &FcShapeFixWire, value: i32);
            fn fix_connected_mode(self: &FcShapeFixWire) -> i32;
            fn set_fix_connected_mode(self: &FcShapeFixWire, value: i32);
            fn fix_edge_curves_mode(self: &FcShapeFixWire) -> i32;
            fn set_fix_edge_curves_mode(self: &FcShapeFixWire, value: i32);
            fn fix_degenerated_mode(self: &FcShapeFixWire) -> i32;
            fn set_fix_degenerated_mode(self: &FcShapeFixWire, value: i32);
            fn fix_self_intersection_mode(self: &FcShapeFixWire) -> i32;
            fn set_fix_self_intersection_mode(self: &FcShapeFixWire, value: i32);
            fn fix_lacking_mode(self: &FcShapeFixWire) -> i32;
            fn set_fix_lacking_mode(self: &FcShapeFixWire, value: i32);
            fn fix_gaps3d_mode(self: &FcShapeFixWire) -> i32;
            fn set_fix_gaps3d_mode(self: &FcShapeFixWire, value: i32);
            fn fix_gaps2d_mode(self: &FcShapeFixWire) -> i32;
            fn set_fix_gaps2d_mode(self: &FcShapeFixWire, value: i32);
            fn fix_reversed2d_mode(self: &FcShapeFixWire) -> i32;
            fn set_fix_reversed2d_mode(self: &FcShapeFixWire, value: i32);
            fn fix_remove_pcurve_mode(self: &FcShapeFixWire) -> i32;
            fn set_fix_remove_pcurve_mode(self: &FcShapeFixWire, value: i32);
            fn fix_add_pcurve_mode(self: &FcShapeFixWire) -> i32;
            fn set_fix_add_pcurve_mode(self: &FcShapeFixWire, value: i32);
            fn fix_remove_curve3d_mode(self: &FcShapeFixWire) -> i32;
            fn set_fix_remove_curve3d_mode(self: &FcShapeFixWire, value: i32);
            fn fix_add_curve3d_mode(self: &FcShapeFixWire) -> i32;
            fn set_fix_add_curve3d_mode(self: &FcShapeFixWire, value: i32);
            fn fix_seam_mode(self: &FcShapeFixWire) -> i32;
            fn set_fix_seam_mode(self: &FcShapeFixWire, value: i32);
            fn fix_shifted_mode(self: &FcShapeFixWire) -> i32;
            fn set_fix_shifted_mode(self: &FcShapeFixWire, value: i32);
            fn fix_same_parameter_mode(self: &FcShapeFixWire) -> i32;
            fn set_fix_same_parameter_mode(self: &FcShapeFixWire, value: i32);
            fn fix_vertex_tolerance_mode(self: &FcShapeFixWire) -> i32;
            fn set_fix_vertex_tolerance_mode(self: &FcShapeFixWire, value: i32);
            fn fix_notched_edges_mode(self: &FcShapeFixWire) -> i32;
            fn set_fix_notched_edges_mode(self: &FcShapeFixWire, value: i32);
            fn fix_self_intersecting_edge_mode(self: &FcShapeFixWire) -> i32;
            fn set_fix_self_intersecting_edge_mode(self: &FcShapeFixWire, value: i32);
            fn fix_intersecting_edges_mode(self: &FcShapeFixWire) -> i32;
            fn set_fix_intersecting_edges_mode(self: &FcShapeFixWire, value: i32);
            fn fix_non_adjacent_intersecting_edges_mode(self: &FcShapeFixWire) -> i32;
            fn set_fix_non_adjacent_intersecting_edges_mode(
                self: &FcShapeFixWire,
                value: i32,
            );
            fn fix_tail_mode(self: &FcShapeFixWire) -> i32;
            fn set_fix_tail_mode(self: &FcShapeFixWire, value: i32);

            // --- Face ------------------------------------------------------
            fn fc_shapefix_face_new() -> UniquePtr<FcShapeFixFace>;
            fn fc_shapefix_face_from_face(face: &TopoDS_Shape) -> UniquePtr<FcShapeFixFace>;
            fn fc_shapefix_face_from_plane(
                ox: f64,
                oy: f64,
                oz: f64,
                nx: f64,
                ny: f64,
                nz: f64,
                prec: f64,
                fwd: bool,
            ) -> UniquePtr<FcShapeFixFace>;
            fn init_face(self: &FcShapeFixFace, face: &TopoDS_Shape) -> bool;
            fn init_plane(
                self: &FcShapeFixFace,
                ox: f64,
                oy: f64,
                oz: f64,
                nx: f64,
                ny: f64,
                nz: f64,
                prec: f64,
                fwd: bool,
            ) -> bool;
            fn add(self: &FcShapeFixFace, wire: &TopoDS_Shape) -> bool;
            fn face(self: &FcShapeFixFace) -> UniquePtr<TopoDS_Shape>;
            fn result(self: &FcShapeFixFace) -> UniquePtr<TopoDS_Shape>;
            fn perform(self: &FcShapeFixFace) -> bool;
            fn clear_modes(self: &FcShapeFixFace);
            fn fix_orientation(self: &FcShapeFixFace) -> bool;
            fn fix_add_natural_bound(self: &FcShapeFixFace) -> bool;
            fn fix_missing_seam(self: &FcShapeFixFace) -> bool;
            fn fix_small_area_wire(self: &FcShapeFixFace, remove_small: bool) -> bool;
            fn fix_loop_wire(self: &FcShapeFixFace) -> UniquePtr<FcShapeList>;
            fn fix_intersecting_wires(self: &FcShapeFixFace) -> bool;
            fn fix_wires_two_coincident_edges(self: &FcShapeFixFace) -> bool;
            fn fix_periodic_degenerated(self: &FcShapeFixFace) -> bool;
            fn fix_wire_tool(self: &FcShapeFixFace) -> UniquePtr<FcShapeFixWire>;
            fn fix_wire_mode(self: &FcShapeFixFace) -> i32;
            fn set_fix_wire_mode(self: &FcShapeFixFace, value: i32);
            fn fix_orientation_mode(self: &FcShapeFixFace) -> i32;
            fn set_fix_orientation_mode(self: &FcShapeFixFace, value: i32);
            fn fix_add_natural_bound_mode(self: &FcShapeFixFace) -> i32;
            fn set_fix_add_natural_bound_mode(self: &FcShapeFixFace, value: i32);
            fn fix_missing_seam_mode(self: &FcShapeFixFace) -> i32;
            fn set_fix_missing_seam_mode(self: &FcShapeFixFace, value: i32);
            fn fix_small_area_wire_mode(self: &FcShapeFixFace) -> i32;
            fn set_fix_small_area_wire_mode(self: &FcShapeFixFace, value: i32);
            fn remove_small_area_face_mode(self: &FcShapeFixFace) -> i32;
            fn set_remove_small_area_face_mode(self: &FcShapeFixFace, value: i32);
            fn fix_intersecting_wires_mode(self: &FcShapeFixFace) -> i32;
            fn set_fix_intersecting_wires_mode(self: &FcShapeFixFace, value: i32);
            fn fix_loop_wires_mode(self: &FcShapeFixFace) -> i32;
            fn set_fix_loop_wires_mode(self: &FcShapeFixFace, value: i32);
            fn fix_split_face_mode(self: &FcShapeFixFace) -> i32;
            fn set_fix_split_face_mode(self: &FcShapeFixFace, value: i32);
            fn auto_correct_precision_mode(self: &FcShapeFixFace) -> i32;
            fn set_auto_correct_precision_mode(self: &FcShapeFixFace, value: i32);
            fn fix_periodic_degenerated_mode(self: &FcShapeFixFace) -> i32;
            fn set_fix_periodic_degenerated_mode(self: &FcShapeFixFace, value: i32);

            // --- Shell -----------------------------------------------------
            fn fc_shapefix_shell_new() -> UniquePtr<FcShapeFixShell>;
            fn fc_shapefix_shell_from_shell(
                shell: &TopoDS_Shape,
            ) -> UniquePtr<FcShapeFixShell>;
            fn init(self: &FcShapeFixShell, shell: &TopoDS_Shape) -> bool;
            fn perform(self: &FcShapeFixShell) -> bool;
            fn shape(self: &FcShapeFixShell) -> UniquePtr<TopoDS_Shape>;
            fn shell(self: &FcShapeFixShell) -> UniquePtr<TopoDS_Shape>;
            fn error_faces(self: &FcShapeFixShell) -> UniquePtr<TopoDS_Shape>;
            fn nb_shells(self: &FcShapeFixShell) -> i32;
            fn fix_face_tool(self: &FcShapeFixShell) -> UniquePtr<FcShapeFixFace>;
            fn fix_face_orientation(
                self: &FcShapeFixShell,
                shell: &TopoDS_Shape,
                multi_connex: bool,
                non_manifold: bool,
            ) -> bool;
            fn set_non_manifold_flag(self: &FcShapeFixShell, value: bool);
            fn fix_face_mode(self: &FcShapeFixShell) -> i32;
            fn set_fix_face_mode(self: &FcShapeFixShell, value: i32);
            fn fix_orientation_mode(self: &FcShapeFixShell) -> i32;
            fn set_fix_orientation_mode(self: &FcShapeFixShell, value: i32);

            // --- Solid -----------------------------------------------------
            fn fc_shapefix_solid_new() -> UniquePtr<FcShapeFixSolid>;
            fn fc_shapefix_solid_from_solid(
                solid: &TopoDS_Shape,
            ) -> UniquePtr<FcShapeFixSolid>;
            fn init(self: &FcShapeFixSolid, solid: &TopoDS_Shape) -> bool;
            fn perform(self: &FcShapeFixSolid) -> bool;
            fn shape(self: &FcShapeFixSolid) -> UniquePtr<TopoDS_Shape>;
            fn solid_from_shell(
                self: &FcShapeFixSolid,
                shell: &TopoDS_Shape,
            ) -> UniquePtr<TopoDS_Shape>;
            fn solid(self: &FcShapeFixSolid) -> UniquePtr<TopoDS_Shape>;
            fn fix_shell_tool(self: &FcShapeFixSolid) -> UniquePtr<FcShapeFixShell>;
            fn fix_shell_mode(self: &FcShapeFixSolid) -> i32;
            fn set_fix_shell_mode(self: &FcShapeFixSolid, value: i32);
            fn fix_shell_orientation_mode(self: &FcShapeFixSolid) -> i32;
            fn set_fix_shell_orientation_mode(self: &FcShapeFixSolid, value: i32);
            fn create_open_solid_mode(self: &FcShapeFixSolid) -> bool;
            fn set_create_open_solid_mode(self: &FcShapeFixSolid, value: bool);

            // --- Shape -----------------------------------------------------
            fn fc_shapefix_shape_new() -> UniquePtr<FcShapeFixShape>;
            fn fc_shapefix_shape_from_shape(
                shape: &TopoDS_Shape,
            ) -> UniquePtr<FcShapeFixShape>;
            fn init(self: &FcShapeFixShape, shape: &TopoDS_Shape) -> bool;
            fn perform(self: &FcShapeFixShape) -> bool;
            fn shape(self: &FcShapeFixShape) -> UniquePtr<TopoDS_Shape>;
            fn fix_solid_tool(self: &FcShapeFixShape) -> UniquePtr<FcShapeFixSolid>;
            fn fix_shell_tool(self: &FcShapeFixShape) -> UniquePtr<FcShapeFixShell>;
            fn fix_face_tool(self: &FcShapeFixShape) -> UniquePtr<FcShapeFixFace>;
            fn fix_wire_tool(self: &FcShapeFixShape) -> UniquePtr<FcShapeFixWire>;
            fn fix_edge_tool(self: &FcShapeFixShape) -> UniquePtr<FcShapeFixEdge>;
            fn fix_solid_mode(self: &FcShapeFixShape) -> i32;
            fn set_fix_solid_mode(self: &FcShapeFixShape, value: i32);
            fn fix_free_shell_mode(self: &FcShapeFixShape) -> i32;
            fn set_fix_free_shell_mode(self: &FcShapeFixShape, value: i32);
            fn fix_free_face_mode(self: &FcShapeFixShape) -> i32;
            fn set_fix_free_face_mode(self: &FcShapeFixShape, value: i32);
            fn fix_free_wire_mode(self: &FcShapeFixShape) -> i32;
            fn set_fix_free_wire_mode(self: &FcShapeFixShape, value: i32);
            fn fix_same_parameter_mode(self: &FcShapeFixShape) -> i32;
            fn set_fix_same_parameter_mode(self: &FcShapeFixShape, value: i32);
            fn fix_vertex_position_mode(self: &FcShapeFixShape) -> i32;
            fn set_fix_vertex_position_mode(self: &FcShapeFixShape, value: i32);
            fn fix_vertex_tol_mode(self: &FcShapeFixShape) -> i32;
            fn set_fix_vertex_tol_mode(self: &FcShapeFixShape, value: i32);

            // --- List ------------------------------------------------------
            fn is_ok(self: &FcShapeList) -> bool;
            fn size(self: &FcShapeList) -> usize;
            fn at(self: &FcShapeList, index: usize) -> &TopoDS_Shape;
    }
}

/// Run `body` with the kernel shape behind `shape`; `false`/`None` when the
/// shape is null or came from another backend.
macro_rules! with_occt {
    ($shape:expr, |$inner:ident| $body:expr) => {{
        match downcast($shape) {
            Ok(occt) => {
                let guard = occt.borrow();
                match guard.as_ref() {
                    Some($inner) => $body,
                    None => Default::default(),
                }
            }
            Err(_) => Default::default(),
        }
    }};
}

/// Take an owned output shape, or `None` if OCCT returned a null handle.
fn output(shape: UniquePtr<TopoDS_Shape>) -> Option<Shape> {
    if shape.is_null() {
        None
    } else {
        Some(Shape::new(OcctShape(shape)))
    }
}

/// Call `f` with the kernel shape behind `shape`; `None` when the shape is null
/// or came from another backend.
fn with_shape<R>(shape: &Shape, f: impl FnOnce(&TopoDS_Shape) -> R) -> Option<R> {
    let occt = downcast(shape).ok()?;
    let guard = occt.borrow();
    guard.as_ref().map(f)
}

/// `Part.ShapeFix.Root` — precision and tolerance limits shared by all fixers.
pub struct Root {
    inner: UniquePtr<ffi::FcShapeFixRoot>,
}

impl Root {
    pub fn new() -> Self {
        Root {
            inner: ffi::fc_shapefix_root_new(),
        }
    }

    pub fn precision(&self) -> f64 {
        self.inner.precision()
    }
    pub fn set_precision(&self, value: f64) {
        self.inner.set_precision(value)
    }
    pub fn min_tolerance(&self) -> f64 {
        self.inner.min_tolerance()
    }
    pub fn set_min_tolerance(&self, value: f64) {
        self.inner.set_min_tolerance(value)
    }
    pub fn max_tolerance(&self) -> f64 {
        self.inner.max_tolerance()
    }
    pub fn set_max_tolerance(&self, value: f64) {
        self.inner.set_max_tolerance(value)
    }
    pub fn limit_tolerance(&self, value: f64) -> f64 {
        self.inner.limit_tolerance(value)
    }
}

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

/// `Part.ShapeFix.Edge` — the edge-level fixes.
pub struct Edge {
    inner: UniquePtr<ffi::FcShapeFixEdge>,
}

impl Edge {
    pub fn new() -> Option<Self> {
        let inner = ffi::fc_shapefix_edge_new();
        if inner.is_null() {
            None
        } else {
            Some(Edge { inner })
        }
    }

    fn from_inner(inner: UniquePtr<ffi::FcShapeFixEdge>) -> Option<Self> {
        if inner.is_null() {
            None
        } else {
            Some(Edge { inner })
        }
    }

    pub fn remove_pcurve_face(&self, edge: &Shape, face: &Shape) -> bool {
        with_occt!(edge, |edge| with_occt!(face, |face| self
            .inner
            .fix_remove_pcurve_face(edge, face)))
    }

    /// The surface overload: `Face.Surface` carries a normal but no location,
    /// so the plane is anchored at a vertex of the edge (which lies on it).
    pub fn remove_pcurve_surface(&self, edge: &Shape, normal: [f64; 3]) -> bool {
        with_occt!(edge, |edge| self.inner.fix_remove_pcurve_surface(
            edge, normal[0], normal[1], normal[2]
        ))
    }

    pub fn remove_curve3d(&self, edge: &Shape) -> bool {
        with_occt!(edge, |edge| self.inner.fix_remove_curve3d(edge))
    }

    pub fn add_pcurve_face(&self, edge: &Shape, face: &Shape, seam: bool, prec: f64) -> bool {
        with_occt!(edge, |edge| with_occt!(face, |face| self
            .inner
            .fix_add_pcurve_face(edge, face, seam, prec)))
    }

    pub fn add_pcurve_surface(
        &self,
        edge: &Shape,
        normal: [f64; 3],
        seam: bool,
        prec: f64,
    ) -> bool {
        with_occt!(edge, |edge| self.inner.fix_add_pcurve_surface(
            edge, normal[0], normal[1], normal[2], seam, prec
        ))
    }

    pub fn add_curve3d(&self, edge: &Shape) -> bool {
        with_occt!(edge, |edge| self.inner.fix_add_curve3d(edge))
    }

    pub fn vertex_tolerance_face(&self, edge: &Shape, face: &Shape) -> bool {
        with_occt!(edge, |edge| with_occt!(face, |face| self
            .inner
            .fix_vertex_tolerance_face(edge, face)))
    }

    pub fn vertex_tolerance(&self, edge: &Shape) -> bool {
        with_occt!(edge, |edge| self.inner.fix_vertex_tolerance(edge))
    }

    pub fn reversed2d_face(&self, edge: &Shape, face: &Shape) -> bool {
        with_occt!(edge, |edge| with_occt!(face, |face| self
            .inner
            .fix_reversed2d_face(edge, face)))
    }

    pub fn reversed2d_surface(&self, edge: &Shape, normal: [f64; 3]) -> bool {
        with_occt!(edge, |edge| self.inner.fix_reversed2d_surface(
            edge, normal[0], normal[1], normal[2]
        ))
    }

    pub fn same_parameter(&self, edge: &Shape, tolerance: f64) -> bool {
        with_occt!(edge, |edge| self
            .inner
            .fix_same_parameter(edge, tolerance))
    }

    pub fn same_parameter_face(&self, edge: &Shape, face: &Shape, tolerance: f64) -> bool {
        with_occt!(edge, |edge| with_occt!(face, |face| self
            .inner
            .fix_same_parameter_face(edge, face, tolerance)))
    }
}

impl Default for Edge {
    fn default() -> Self {
        Self::new().expect("a ShapeFix_Edge cannot fail to allocate")
    }
}

/// `Part.ShapeFix.Wire` — the wire-level fixes and their modes.
pub struct Wire {
    inner: UniquePtr<ffi::FcShapeFixWire>,
}

impl Wire {
    pub fn new() -> Option<Self> {
        Self::from_inner(ffi::fc_shapefix_wire_new())
    }

    pub fn from_wire_face(wire: &Shape, face: &Shape, prec: f64) -> Option<Self> {
        let (Ok(wire), Ok(face)) = (downcast(wire), downcast(face)) else {
            return None;
        };
        let wire_guard = wire.borrow();
        let face_guard = face.borrow();
        let (Some(wire), Some(face)) = (wire_guard.as_ref(), face_guard.as_ref()) else {
            return None;
        };
        Self::from_inner(ffi::fc_shapefix_wire_from_wire(wire, face, prec))
    }

    fn from_inner(inner: UniquePtr<ffi::FcShapeFixWire>) -> Option<Self> {
        if inner.is_null() {
            None
        } else {
            Some(Wire { inner })
        }
    }

    pub fn init(&self, wire: &Shape, face: &Shape, prec: f64) -> bool {
        with_occt!(wire, |wire| with_occt!(face, |face| {
            self.inner.init(wire, face, prec);
            true
        }))
    }

    pub fn load(&self, wire: &Shape) -> bool {
        with_occt!(wire, |wire| self.inner.load(wire))
    }

    pub fn set_face(&self, face: &Shape) -> bool {
        with_occt!(face, |face| self.inner.set_face(face))
    }

    pub fn set_surface(&self, origin: [f64; 3], normal: [f64; 3], placed: bool) -> bool {
        if placed {
            self.inner.set_surface_placed(
                origin[0], origin[1], origin[2], normal[0], normal[1], normal[2],
            )
        } else {
            self.inner
                .set_surface(origin[0], origin[1], origin[2], normal[0], normal[1], normal[2])
        }
    }

    pub fn set_max_tail_angle(&self, angle: f64) {
        self.inner.set_max_tail_angle(angle)
    }
    pub fn set_max_tail_width(&self, width: f64) {
        self.inner.set_max_tail_width(width)
    }
    pub fn is_loaded(&self) -> bool {
        self.inner.is_loaded()
    }
    pub fn is_ready(&self) -> bool {
        self.inner.is_ready()
    }
    pub fn number_of_edges(&self) -> i32 {
        self.inner.nb_edges()
    }
    pub fn wire(&self) -> Option<Shape> {
        output(self.inner.wire())
    }
    pub fn wire_api_make(&self) -> Option<Shape> {
        output(self.inner.wire_api_make())
    }
    pub fn face(&self) -> Option<Shape> {
        output(self.inner.face())
    }
    pub fn perform(&self) -> bool {
        self.inner.perform()
    }
    pub fn clear_modes(&self) {
        self.inner.clear_modes()
    }
    pub fn clear_statuses(&self) {
        self.inner.clear_statuses()
    }
    pub fn fix_edge_tool(&self) -> Option<Edge> {
        Edge::from_inner(self.inner.fix_edge_tool())
    }

    pub fn fix_reorder(&self) -> bool {
        self.inner.fix_reorder()
    }
    pub fn fix_small_global(&self, lock: bool, prec: f64) -> i32 {
        self.inner.fix_small_global(lock, prec)
    }
    pub fn fix_small_one(&self, num: i32, lock: bool, prec: f64) -> bool {
        self.inner.fix_small_one(num, lock, prec)
    }
    pub fn fix_connected(&self, prec: f64) -> bool {
        self.inner.fix_connected(prec)
    }
    pub fn fix_connected_one(&self, num: i32, prec: f64) -> bool {
        self.inner.fix_connected_one(num, prec)
    }
    pub fn fix_edge_curves(&self) -> bool {
        self.inner.fix_edge_curves()
    }
    pub fn fix_degenerated(&self) -> bool {
        self.inner.fix_degenerated()
    }
    pub fn fix_degenerated_one(&self, num: i32) -> bool {
        self.inner.fix_degenerated_one(num)
    }
    pub fn fix_self_intersection(&self) -> bool {
        self.inner.fix_self_intersection()
    }
    pub fn fix_lacking(&self, force: bool) -> bool {
        self.inner.fix_lacking(force)
    }
    pub fn fix_lacking_one(&self, num: i32, force: bool) -> bool {
        self.inner.fix_lacking_one(num, force)
    }
    pub fn fix_closed(&self, prec: f64) -> bool {
        self.inner.fix_closed(prec)
    }
    pub fn fix_gaps3d(&self) -> bool {
        self.inner.fix_gaps3d()
    }
    pub fn fix_gaps2d(&self) -> bool {
        self.inner.fix_gaps2d()
    }
    pub fn fix_seam(&self, num: i32) -> bool {
        self.inner.fix_seam(num)
    }
    pub fn fix_shifted(&self) -> bool {
        self.inner.fix_shifted()
    }
    pub fn fix_notched_edges(&self) -> bool {
        self.inner.fix_notched_edges()
    }
    pub fn fix_gap3d(&self, num: i32, convert: bool) -> bool {
        self.inner.fix_gap3d(num, convert)
    }
    pub fn fix_gap2d(&self, num: i32, convert: bool) -> bool {
        self.inner.fix_gap2d(num, convert)
    }
    pub fn fix_tails(&self) -> bool {
        self.inner.fix_tails()
    }

    // Modes reported as booleans where OCCT stores a `Standard_Boolean`, and as
    // integers where it stores a three-state `Standard_Integer`.
    pub fn modify_topology_mode(&self) -> bool {
        self.inner.modify_topology_mode()
    }
    pub fn set_modify_topology_mode(&self, value: bool) {
        self.inner.set_modify_topology_mode(value)
    }
    pub fn modify_geometry_mode(&self) -> bool {
        self.inner.modify_geometry_mode()
    }
    pub fn set_modify_geometry_mode(&self, value: bool) {
        self.inner.set_modify_geometry_mode(value)
    }
    pub fn closed_wire_mode(&self) -> bool {
        self.inner.closed_wire_mode()
    }
    pub fn set_closed_wire_mode(&self, value: bool) {
        self.inner.set_closed_wire_mode(value)
    }
    pub fn preference_pcurve_mode(&self) -> bool {
        self.inner.preference_pcurve_mode()
    }
    pub fn set_preference_pcurve_mode(&self, value: bool) {
        self.inner.set_preference_pcurve_mode(value)
    }
    pub fn fix_gaps_by_ranges_mode(&self) -> bool {
        self.inner.fix_gaps_by_ranges_mode()
    }
    pub fn set_fix_gaps_by_ranges_mode(&self, value: bool) {
        self.inner.set_fix_gaps_by_ranges_mode(value)
    }
    pub fn modify_remove_loop_mode(&self) -> i32 {
        self.inner.modify_remove_loop_mode()
    }
    pub fn set_modify_remove_loop_mode(&self, value: i32) {
        self.inner.set_modify_remove_loop_mode(value)
    }
    pub fn fix_reorder_mode(&self) -> i32 {
        self.inner.fix_reorder_mode()
    }
    pub fn set_fix_reorder_mode(&self, value: i32) {
        self.inner.set_fix_reorder_mode(value)
    }
    pub fn fix_small_mode(&self) -> i32 {
        self.inner.fix_small_mode()
    }
    pub fn set_fix_small_mode(&self, value: i32) {
        self.inner.set_fix_small_mode(value)
    }
    pub fn fix_connected_mode(&self) -> i32 {
        self.inner.fix_connected_mode()
    }
    pub fn set_fix_connected_mode(&self, value: i32) {
        self.inner.set_fix_connected_mode(value)
    }
    pub fn fix_edge_curves_mode(&self) -> i32 {
        self.inner.fix_edge_curves_mode()
    }
    pub fn set_fix_edge_curves_mode(&self, value: i32) {
        self.inner.set_fix_edge_curves_mode(value)
    }
    pub fn fix_degenerated_mode(&self) -> i32 {
        self.inner.fix_degenerated_mode()
    }
    pub fn set_fix_degenerated_mode(&self, value: i32) {
        self.inner.set_fix_degenerated_mode(value)
    }
    pub fn fix_self_intersection_mode(&self) -> i32 {
        self.inner.fix_self_intersection_mode()
    }
    pub fn set_fix_self_intersection_mode(&self, value: i32) {
        self.inner.set_fix_self_intersection_mode(value)
    }
    pub fn fix_lacking_mode(&self) -> i32 {
        self.inner.fix_lacking_mode()
    }
    pub fn set_fix_lacking_mode(&self, value: i32) {
        self.inner.set_fix_lacking_mode(value)
    }
    pub fn fix_gaps3d_mode(&self) -> i32 {
        self.inner.fix_gaps3d_mode()
    }
    pub fn set_fix_gaps3d_mode(&self, value: i32) {
        self.inner.set_fix_gaps3d_mode(value)
    }
    pub fn fix_gaps2d_mode(&self) -> i32 {
        self.inner.fix_gaps2d_mode()
    }
    pub fn set_fix_gaps2d_mode(&self, value: i32) {
        self.inner.set_fix_gaps2d_mode(value)
    }
    pub fn fix_reversed2d_mode(&self) -> i32 {
        self.inner.fix_reversed2d_mode()
    }
    pub fn set_fix_reversed2d_mode(&self, value: i32) {
        self.inner.set_fix_reversed2d_mode(value)
    }
    pub fn fix_remove_pcurve_mode(&self) -> i32 {
        self.inner.fix_remove_pcurve_mode()
    }
    pub fn set_fix_remove_pcurve_mode(&self, value: i32) {
        self.inner.set_fix_remove_pcurve_mode(value)
    }
    pub fn fix_add_pcurve_mode(&self) -> i32 {
        self.inner.fix_add_pcurve_mode()
    }
    pub fn set_fix_add_pcurve_mode(&self, value: i32) {
        self.inner.set_fix_add_pcurve_mode(value)
    }
    pub fn fix_remove_curve3d_mode(&self) -> i32 {
        self.inner.fix_remove_curve3d_mode()
    }
    pub fn set_fix_remove_curve3d_mode(&self, value: i32) {
        self.inner.set_fix_remove_curve3d_mode(value)
    }
    pub fn fix_add_curve3d_mode(&self) -> i32 {
        self.inner.fix_add_curve3d_mode()
    }
    pub fn set_fix_add_curve3d_mode(&self, value: i32) {
        self.inner.set_fix_add_curve3d_mode(value)
    }
    pub fn fix_seam_mode(&self) -> i32 {
        self.inner.fix_seam_mode()
    }
    pub fn set_fix_seam_mode(&self, value: i32) {
        self.inner.set_fix_seam_mode(value)
    }
    pub fn fix_shifted_mode(&self) -> i32 {
        self.inner.fix_shifted_mode()
    }
    pub fn set_fix_shifted_mode(&self, value: i32) {
        self.inner.set_fix_shifted_mode(value)
    }
    pub fn fix_same_parameter_mode(&self) -> i32 {
        self.inner.fix_same_parameter_mode()
    }
    pub fn set_fix_same_parameter_mode(&self, value: i32) {
        self.inner.set_fix_same_parameter_mode(value)
    }
    pub fn fix_vertex_tolerance_mode(&self) -> i32 {
        self.inner.fix_vertex_tolerance_mode()
    }
    pub fn set_fix_vertex_tolerance_mode(&self, value: i32) {
        self.inner.set_fix_vertex_tolerance_mode(value)
    }
    pub fn fix_notched_edges_mode(&self) -> i32 {
        self.inner.fix_notched_edges_mode()
    }
    pub fn set_fix_notched_edges_mode(&self, value: i32) {
        self.inner.set_fix_notched_edges_mode(value)
    }
    pub fn fix_self_intersecting_edge_mode(&self) -> i32 {
        self.inner.fix_self_intersecting_edge_mode()
    }
    pub fn set_fix_self_intersecting_edge_mode(&self, value: i32) {
        self.inner.set_fix_self_intersecting_edge_mode(value)
    }
    pub fn fix_intersecting_edges_mode(&self) -> i32 {
        self.inner.fix_intersecting_edges_mode()
    }
    pub fn set_fix_intersecting_edges_mode(&self, value: i32) {
        self.inner.set_fix_intersecting_edges_mode(value)
    }
    pub fn fix_non_adjacent_intersecting_edges_mode(&self) -> i32 {
        self.inner.fix_non_adjacent_intersecting_edges_mode()
    }
    pub fn set_fix_non_adjacent_intersecting_edges_mode(&self, value: i32) {
        self.inner.set_fix_non_adjacent_intersecting_edges_mode(value)
    }
    pub fn fix_tail_mode(&self) -> i32 {
        self.inner.fix_tail_mode()
    }
    pub fn set_fix_tail_mode(&self, value: i32) {
        self.inner.set_fix_tail_mode(value)
    }
}

impl Default for Wire {
    fn default() -> Self {
        Self::new().expect("a ShapeFix_Wire cannot fail to allocate")
    }
}

/// `Part.ShapeFix.Face` — the face-level fixes.
pub struct Face {
    inner: UniquePtr<ffi::FcShapeFixFace>,
}

impl Face {
    pub fn new() -> Option<Self> {
        Self::from_inner(ffi::fc_shapefix_face_new())
    }

    pub fn from_face(face: &Shape) -> Option<Self> {
        Self::from_inner(with_shape(face, ffi::fc_shapefix_face_from_face)?)
    }

    pub fn from_plane(
        origin: [f64; 3],
        normal: [f64; 3],
        prec: f64,
        forward: bool,
    ) -> Option<Self> {
        let inner = ffi::fc_shapefix_face_from_plane(
            origin[0], origin[1], origin[2], normal[0], normal[1], normal[2], prec, forward,
        );
        Self::from_inner(inner)
    }

    fn from_inner(inner: UniquePtr<ffi::FcShapeFixFace>) -> Option<Self> {
        if inner.is_null() {
            None
        } else {
            Some(Face { inner })
        }
    }

    pub fn init_face(&self, face: &Shape) -> bool {
        with_occt!(face, |face| self.inner.init_face(face))
    }

    pub fn init_plane(
        &self,
        origin: [f64; 3],
        normal: [f64; 3],
        prec: f64,
        forward: bool,
    ) -> bool {
        self.inner.init_plane(
            origin[0], origin[1], origin[2], normal[0], normal[1], normal[2], prec, forward,
        )
    }

    pub fn add(&self, wire: &Shape) -> bool {
        with_occt!(wire, |wire| self.inner.add(wire))
    }

    pub fn perform(&self) -> bool {
        self.inner.perform()
    }
    pub fn fix_orientation(&self) -> bool {
        self.inner.fix_orientation()
    }
    pub fn fix_add_natural_bound(&self) -> bool {
        self.inner.fix_add_natural_bound()
    }
    pub fn fix_missing_seam(&self) -> bool {
        self.inner.fix_missing_seam()
    }
    pub fn fix_small_area_wire(&self, remove_small: bool) -> bool {
        self.inner.fix_small_area_wire(remove_small)
    }

    /// `(fixed, replaced_wires)` — upstream's `(bool, list)` tuple.
    pub fn fix_loop_wire(&self) -> (bool, Vec<Shape>) {
        let list = self.inner.fix_loop_wire();
        if list.is_null() {
            return (false, Vec::new());
        }
        let ok = list.is_ok();
        let wires = (0..list.size())
            .map(|index| crate::shape::erase(list.at(index)))
            .collect();
        (ok, wires)
    }

    pub fn fix_intersecting_wires(&self) -> bool {
        self.inner.fix_intersecting_wires()
    }
    pub fn fix_wires_two_coincident_edges(&self) -> bool {
        self.inner.fix_wires_two_coincident_edges()
    }
    pub fn fix_periodic_degenerated(&self) -> bool {
        self.inner.fix_periodic_degenerated()
    }
    pub fn face(&self) -> Option<Shape> {
        output(self.inner.face())
    }
    pub fn result(&self) -> Option<Shape> {
        output(self.inner.result())
    }
    pub fn fix_wire_tool(&self) -> Option<Wire> {
        Wire::from_inner(self.inner.fix_wire_tool())
    }
    pub fn clear_modes(&self) {
        self.inner.clear_modes()
    }

    pub fn fix_wire_mode(&self) -> i32 {
        self.inner.fix_wire_mode()
    }
    pub fn set_fix_wire_mode(&self, value: i32) {
        self.inner.set_fix_wire_mode(value)
    }
    pub fn fix_orientation_mode(&self) -> i32 {
        self.inner.fix_orientation_mode()
    }
    pub fn set_fix_orientation_mode(&self, value: i32) {
        self.inner.set_fix_orientation_mode(value)
    }
    pub fn fix_add_natural_bound_mode(&self) -> i32 {
        self.inner.fix_add_natural_bound_mode()
    }
    pub fn set_fix_add_natural_bound_mode(&self, value: i32) {
        self.inner.set_fix_add_natural_bound_mode(value)
    }
    pub fn fix_missing_seam_mode(&self) -> i32 {
        self.inner.fix_missing_seam_mode()
    }
    pub fn set_fix_missing_seam_mode(&self, value: i32) {
        self.inner.set_fix_missing_seam_mode(value)
    }
    pub fn fix_small_area_wire_mode(&self) -> i32 {
        self.inner.fix_small_area_wire_mode()
    }
    pub fn set_fix_small_area_wire_mode(&self, value: i32) {
        self.inner.set_fix_small_area_wire_mode(value)
    }
    pub fn remove_small_area_face_mode(&self) -> i32 {
        self.inner.remove_small_area_face_mode()
    }
    pub fn set_remove_small_area_face_mode(&self, value: i32) {
        self.inner.set_remove_small_area_face_mode(value)
    }
    pub fn fix_intersecting_wires_mode(&self) -> i32 {
        self.inner.fix_intersecting_wires_mode()
    }
    pub fn set_fix_intersecting_wires_mode(&self, value: i32) {
        self.inner.set_fix_intersecting_wires_mode(value)
    }
    pub fn fix_loop_wires_mode(&self) -> i32 {
        self.inner.fix_loop_wires_mode()
    }
    pub fn set_fix_loop_wires_mode(&self, value: i32) {
        self.inner.set_fix_loop_wires_mode(value)
    }
    pub fn fix_split_face_mode(&self) -> i32 {
        self.inner.fix_split_face_mode()
    }
    pub fn set_fix_split_face_mode(&self, value: i32) {
        self.inner.set_fix_split_face_mode(value)
    }
    pub fn auto_correct_precision_mode(&self) -> i32 {
        self.inner.auto_correct_precision_mode()
    }
    pub fn set_auto_correct_precision_mode(&self, value: i32) {
        self.inner.set_auto_correct_precision_mode(value)
    }
    pub fn fix_periodic_degenerated_mode(&self) -> i32 {
        self.inner.fix_periodic_degenerated_mode()
    }
    pub fn set_fix_periodic_degenerated_mode(&self, value: i32) {
        self.inner.set_fix_periodic_degenerated_mode(value)
    }
}

impl Default for Face {
    fn default() -> Self {
        Self::new().expect("a ShapeFix_Face cannot fail to allocate")
    }
}

/// `Part.ShapeFix.Shell` — the shell-level fixes.
pub struct Shell {
    inner: UniquePtr<ffi::FcShapeFixShell>,
}

impl Shell {
    pub fn new() -> Option<Self> {
        Self::from_inner(ffi::fc_shapefix_shell_new())
    }

    pub fn from_shell(shell: &Shape) -> Option<Self> {
        Self::from_inner(with_shape(shell, ffi::fc_shapefix_shell_from_shell)?)
    }

    fn from_inner(inner: UniquePtr<ffi::FcShapeFixShell>) -> Option<Self> {
        if inner.is_null() {
            None
        } else {
            Some(Shell { inner })
        }
    }

    pub fn init(&self, shell: &Shape) -> bool {
        with_occt!(shell, |shell| self.inner.init(shell))
    }

    pub fn perform(&self) -> bool {
        self.inner.perform()
    }
    pub fn shell(&self) -> Option<Shape> {
        output(self.inner.shell())
    }
    pub fn shape(&self) -> Option<Shape> {
        output(self.inner.shape())
    }
    pub fn error_faces(&self) -> Option<Shape> {
        output(self.inner.error_faces())
    }
    pub fn number_of_shells(&self) -> i32 {
        self.inner.nb_shells()
    }
    pub fn fix_face_tool(&self) -> Option<Face> {
        Face::from_inner(self.inner.fix_face_tool())
    }
    pub fn fix_face_orientation(
        &self,
        shell: &Shape,
        multi_connex: bool,
        non_manifold: bool,
    ) -> bool {
        with_occt!(shell, |shell| self
            .inner
            .fix_face_orientation(shell, multi_connex, non_manifold))
    }
    pub fn set_non_manifold_flag(&self, value: bool) {
        self.inner.set_non_manifold_flag(value)
    }
    pub fn fix_face_mode(&self) -> i32 {
        self.inner.fix_face_mode()
    }
    pub fn set_fix_face_mode(&self, value: i32) {
        self.inner.set_fix_face_mode(value)
    }
    pub fn fix_orientation_mode(&self) -> i32 {
        self.inner.fix_orientation_mode()
    }
    pub fn set_fix_orientation_mode(&self, value: i32) {
        self.inner.set_fix_orientation_mode(value)
    }
}

impl Default for Shell {
    fn default() -> Self {
        Self::new().expect("a ShapeFix_Shell cannot fail to allocate")
    }
}

/// `Part.ShapeFix.Solid` — the solid-level fixes.
pub struct Solid {
    inner: UniquePtr<ffi::FcShapeFixSolid>,
}

impl Solid {
    pub fn new() -> Option<Self> {
        Self::from_inner(ffi::fc_shapefix_solid_new())
    }

    pub fn from_solid(solid: &Shape) -> Option<Self> {
        Self::from_inner(with_shape(solid, ffi::fc_shapefix_solid_from_solid)?)
    }

    fn from_inner(inner: UniquePtr<ffi::FcShapeFixSolid>) -> Option<Self> {
        if inner.is_null() {
            None
        } else {
            Some(Solid { inner })
        }
    }

    pub fn init(&self, solid: &Shape) -> bool {
        with_occt!(solid, |solid| self.inner.init(solid))
    }

    pub fn perform(&self) -> bool {
        self.inner.perform()
    }
    pub fn solid_from_shell(&self, shell: &Shape) -> Option<Shape> {
        with_occt!(shell, |shell| output(self.inner.solid_from_shell(shell)))
    }
    pub fn solid(&self) -> Option<Shape> {
        output(self.inner.solid())
    }
    pub fn shape(&self) -> Option<Shape> {
        output(self.inner.shape())
    }
    pub fn fix_shell_tool(&self) -> Option<Shell> {
        Shell::from_inner(self.inner.fix_shell_tool())
    }
    pub fn fix_shell_mode(&self) -> i32 {
        self.inner.fix_shell_mode()
    }
    pub fn set_fix_shell_mode(&self, value: i32) {
        self.inner.set_fix_shell_mode(value)
    }
    pub fn fix_shell_orientation_mode(&self) -> i32 {
        self.inner.fix_shell_orientation_mode()
    }
    pub fn set_fix_shell_orientation_mode(&self, value: i32) {
        self.inner.set_fix_shell_orientation_mode(value)
    }
    pub fn create_open_solid_mode(&self) -> bool {
        self.inner.create_open_solid_mode()
    }
    pub fn set_create_open_solid_mode(&self, value: bool) {
        self.inner.set_create_open_solid_mode(value)
    }
}

impl Default for Solid {
    fn default() -> Self {
        Self::new().expect("a ShapeFix_Solid cannot fail to allocate")
    }
}

/// `Part.ShapeFix.Shape` — the whole-shape fixer.
pub struct ShapeFix {
    inner: UniquePtr<ffi::FcShapeFixShape>,
}

impl ShapeFix {
    pub fn new() -> Option<Self> {
        Self::from_inner(ffi::fc_shapefix_shape_new())
    }

    pub fn from_shape(shape: &Shape) -> Option<Self> {
        Self::from_inner(with_shape(shape, ffi::fc_shapefix_shape_from_shape)?)
    }

    fn from_inner(inner: UniquePtr<ffi::FcShapeFixShape>) -> Option<Self> {
        if inner.is_null() {
            None
        } else {
            Some(ShapeFix { inner })
        }
    }

    pub fn init(&self, shape: &Shape) -> bool {
        with_occt!(shape, |shape| self.inner.init(shape))
    }

    pub fn perform(&self) -> bool {
        self.inner.perform()
    }
    pub fn shape(&self) -> Option<Shape> {
        output(self.inner.shape())
    }
    pub fn fix_solid_tool(&self) -> Option<Solid> {
        Solid::from_inner(self.inner.fix_solid_tool())
    }
    pub fn fix_shell_tool(&self) -> Option<Shell> {
        Shell::from_inner(self.inner.fix_shell_tool())
    }
    pub fn fix_face_tool(&self) -> Option<Face> {
        Face::from_inner(self.inner.fix_face_tool())
    }
    pub fn fix_wire_tool(&self) -> Option<Wire> {
        Wire::from_inner(self.inner.fix_wire_tool())
    }
    pub fn fix_edge_tool(&self) -> Option<Edge> {
        Edge::from_inner(self.inner.fix_edge_tool())
    }
    pub fn fix_solid_mode(&self) -> i32 {
        self.inner.fix_solid_mode()
    }
    pub fn set_fix_solid_mode(&self, value: i32) {
        self.inner.set_fix_solid_mode(value)
    }
    pub fn fix_free_shell_mode(&self) -> i32 {
        self.inner.fix_free_shell_mode()
    }
    pub fn set_fix_free_shell_mode(&self, value: i32) {
        self.inner.set_fix_free_shell_mode(value)
    }
    pub fn fix_free_face_mode(&self) -> i32 {
        self.inner.fix_free_face_mode()
    }
    pub fn set_fix_free_face_mode(&self, value: i32) {
        self.inner.set_fix_free_face_mode(value)
    }
    pub fn fix_free_wire_mode(&self) -> i32 {
        self.inner.fix_free_wire_mode()
    }
    pub fn set_fix_free_wire_mode(&self, value: i32) {
        self.inner.set_fix_free_wire_mode(value)
    }
    pub fn fix_same_parameter_mode(&self) -> i32 {
        self.inner.fix_same_parameter_mode()
    }
    pub fn set_fix_same_parameter_mode(&self, value: i32) {
        self.inner.set_fix_same_parameter_mode(value)
    }
    pub fn fix_vertex_position_mode(&self) -> i32 {
        self.inner.fix_vertex_position_mode()
    }
    pub fn set_fix_vertex_position_mode(&self, value: i32) {
        self.inner.set_fix_vertex_position_mode(value)
    }
    pub fn fix_vertex_tol_mode(&self) -> i32 {
        self.inner.fix_vertex_tol_mode()
    }
    pub fn set_fix_vertex_tol_mode(&self, value: i32) {
        self.inner.set_fix_vertex_tol_mode(value)
    }
}

impl Default for ShapeFix {
    fn default() -> Self {
        Self::new().expect("a ShapeFix_Shape cannot fail to allocate")
    }
}