File size: 51,148 Bytes
985c397
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# SPDX-License-Identifier: LGPL-2.1-or-later

# ***************************************************************************
# *                                                                         *
# *   Copyright (c) 2018 Yorik van Havre <yorik@uncreated.net>              *
# *                                                                         *
# *   This file is part of FreeCAD.                                         *
# *                                                                         *
# *   FreeCAD is free software: you can redistribute it and/or modify it    *
# *   under the terms of the GNU Lesser General Public License as           *
# *   published by the Free Software Foundation, either version 2.1 of the  *
# *   License, or (at your option) any later version.                       *
# *                                                                         *
# *   FreeCAD is distributed in the hope that it will be useful, but        *
# *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
# *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      *
# *   Lesser General Public License for more details.                       *
# *                                                                         *
# *   You should have received a copy of the GNU Lesser General Public      *
# *   License along with FreeCAD. If not, see                               *
# *   <https://www.gnu.org/licenses/>.                                      *
# *                                                                         *
# ***************************************************************************

__title__ = "FreeCAD Arch BuildingPart"
__author__ = "Yorik van Havre"
__url__ = "https://www.freecad.org"

## @package ArchBuildingPart
#  \ingroup ARCH
#  \brief The BuildingPart object and tools
#
#  This module provides tools to build BuildingPart objects.
#  BuildingParts are used to group different Arch objects

import os
import tempfile

import FreeCAD
import Arch
import ArchCommands
import ArchIFC
import Draft
import DraftVecUtils

from draftutils import params

if FreeCAD.GuiUp:
    from PySide.QtCore import QT_TRANSLATE_NOOP
    import FreeCADGui
    from draftutils.translate import translate
    import draftutils.units as units
else:
    # \cond
    def translate(ctxt, txt):
        return txt

    def QT_TRANSLATE_NOOP(ctxt, txt):
        return txt

    # \endcond
unicode = str


# fmt: off
BuildingTypes = ['Undefined',
'Agricultural - Barn',
'Agricultural - Chicken coop or chickenhouse',
'Agricultural - Cow-shed',
'Agricultural - Farmhouse',
'Agricultural - Granary',
'Agricultural - Greenhouse',
'Agricultural - Hayloft',
'Agricultural - Pigpen or sty',
'Agricultural - Root cellar',
'Agricultural - Shed',
'Agricultural - Silo',
'Agricultural - Stable',
'Agricultural - Storm cellar',
'Agricultural - Well house',
'Agricultural - Underground pit',

'Commercial - Automobile repair shop',
'Commercial - Bank',
'Commercial - Car wash',
'Commercial - Convention center',
'Commercial - Forum',
'Commercial - Gas station',
'Commercial - Hotel',
'Commercial - Market',
'Commercial - Market house',
'Commercial - Skyscraper',
'Commercial - Shop',
'Commercial - Shopping mall',
'Commercial - Supermarket',
'Commercial - Warehouse',
'Commercial - Restaurant',

'Residential - Apartment block',
'Residential - Asylum',
'Residential - Condominium',
'Residential - Dormitory',
'Residential - Duplex',
'Residential - House',
'Residential - Nursing home',
'Residential - Townhouse',
'Residential - Villa',
'Residential - Bungalow',

'Educational - Archive',
'Educational - College classroom building',
'Educational - College gymnasium',
'Educational - College students union',
'Educational - School',
'Educational - Library',
'Educational - Museum',
'Educational - Art gallery',
'Educational - Theater',
'Educational - Amphitheater',
'Educational - Concert hall',
'Educational - Cinema',
'Educational - Opera house',
'Educational - Boarding school',

'Government - Capitol',
'Government - City hall',
'Government - Consulate',
'Government - Courthouse',
'Government - Embassy',
'Government - Fire station',
'Government - Meeting house',
'Government - Moot hall',
'Government - Palace',
'Government - Parliament',
'Government - Police station',
'Government - Post office',
'Government - Prison',

'Industrial - Brewery',
'Industrial - Factory',
'Industrial - Foundry',
'Industrial - Power plant',
'Industrial - Mill',

'Military - Arsenal',
'Military -Barracks',

'Parking - Boathouse',
'Parking - Garage',
'Parking - Hangar',

'Storage - Silo',
'Storage - Hangar',

'Religious - Church',
'Religious - Basilica',
'Religious - Cathedral',
'Religious - Chapel',
'Religious - Oratory',
'Religious - Martyrium',
'Religious - Mosque',
'Religious - Mihrab',
'Religious - Surau',
'Religious - Imambargah',
'Religious - Monastery',
'Religious - Mithraeum',
'Religious - Fire temple',
'Religious - Shrine',
'Religious - Synagogue',
'Religious - Temple',
'Religious - Pagoda',
'Religious - Gurdwara',
'Religious - Hindu temple',

'Transport - Airport terminal',
'Transport - Bus station',
'Transport - Metro station',
'Transport - Taxi station',
'Transport - Railway station',
'Transport - Signal box',
'Transport - Lighthouse',

'Infrastructure - Data centre',

'Power station - Fossil-fuel power station',
'Power station - Nuclear power plant',
'Power station - Geothermal power',
'Power station - Biomass-fuelled power plant',
'Power station - Waste heat power plant',
'Power station - Renewable energy power station',
'Power station - Atomic energy plant',

'Other - Apartment',
'Other - Clinic',
'Other - Community hall',
'Other - Eatery',
'Other - Folly',
'Other - Food court',
'Other - Hospice',
'Other - Hospital',
'Other - Hut',
'Other - Bathhouse',
'Other - Workshop',
'Other - World trade centre'
]
# fmt: on


class BuildingPart(ArchIFC.IfcProduct):
    "The BuildingPart object"

    def __init__(self, obj):

        obj.Proxy = self
        self.Type = "BuildingPart"
        obj.addExtension("App::GroupExtensionPython")
        # obj.addExtension('App::OriginGroupExtensionPython')
        self.setProperties(obj)

    def setProperties(self, obj):
        ArchIFC.IfcProduct.setProperties(self, obj)

        pl = obj.PropertiesList
        if not "Height" in pl:
            obj.addProperty(
                "App::PropertyLength",
                "Height",
                "BuildingPart",
                QT_TRANSLATE_NOOP("App::Property", "The height of this object"),
                locked=True,
            )
        if not "HeightPropagate" in pl:
            obj.addProperty(
                "App::PropertyBool",
                "HeightPropagate",
                "Children",
                QT_TRANSLATE_NOOP(
                    "App::Property",
                    "If true, the height value propagates to contained objects if the height of those objects is set to 0",
                ),
                locked=True,
            )
            obj.HeightPropagate = True
        if not "LevelOffset" in pl:
            obj.addProperty(
                "App::PropertyDistance",
                "LevelOffset",
                "BuildingPart",
                QT_TRANSLATE_NOOP("App::Property", "The level of the (0,0,0) point of this level"),
                locked=True,
            )
        if not "Area" in pl:
            obj.addProperty(
                "App::PropertyArea",
                "Area",
                "BuildingPart",
                QT_TRANSLATE_NOOP("App::Property", "The computed floor area of this floor"),
                locked=True,
            )
        if not "Description" in pl:
            obj.addProperty(
                "App::PropertyString",
                "Description",
                "Component",
                QT_TRANSLATE_NOOP("App::Property", "An optional description for this component"),
                locked=True,
            )
        if not "Tag" in pl:
            obj.addProperty(
                "App::PropertyString",
                "Tag",
                "Component",
                QT_TRANSLATE_NOOP("App::Property", "An optional tag for this component"),
                locked=True,
            )
        if not "Shape" in pl:
            obj.addProperty(
                "Part::PropertyPartShape",
                "Shape",
                "BuildingPart",
                QT_TRANSLATE_NOOP("App::Property", "The shape of this object"),
                locked=True,
            )
        if not "SavedInventor" in pl:
            obj.addProperty(
                "App::PropertyFileIncluded",
                "SavedInventor",
                "BuildingPart",
                QT_TRANSLATE_NOOP(
                    "App::Property",
                    "This property stores an OpenInventor representation for this object",
                ),
                locked=True,
            )
            obj.setEditorMode("SavedInventor", 2)
        if not "OnlySolids" in pl:
            obj.addProperty(
                "App::PropertyBool",
                "OnlySolids",
                "BuildingPart",
                QT_TRANSLATE_NOOP(
                    "App::Property",
                    "If true, only solids will be collected by this object when referenced from other files",
                ),
                locked=True,
            )
            obj.OnlySolids = True
        if not "MaterialsTable" in pl:
            obj.addProperty(
                "App::PropertyMap",
                "MaterialsTable",
                "BuildingPart",
                QT_TRANSLATE_NOOP(
                    "App::Property",
                    "A MaterialName:SolidIndexesList map that relates material names with solid indexes to be used when referencing this object from other files",
                ),
                locked=True,
            )

    def onDocumentRestored(self, obj):

        self.setProperties(obj)

    def dumps(self):

        return None

    def loads(self, state):

        self.Type = "BuildingPart"

    def onBeforeChange(self, obj, prop):

        if prop == "Placement":
            self.oldPlacement = FreeCAD.Placement(obj.Placement)

    def onChanged(self, obj, prop):

        import math

        ArchIFC.IfcProduct.onChanged(self, obj, prop)

        # clean svg cache if needed
        if prop in ["Placement", "Group"]:
            self.svgcache = None
            self.shapecache = None

        if (prop == "Height" or prop == "HeightPropagate") and obj.Height.Value:
            self.touchChildren(obj)

        elif prop == "Placement":
            if hasattr(self, "oldPlacement") and self.oldPlacement != obj.Placement:
                deltap = obj.Placement.Base.sub(self.oldPlacement.Base)
                if deltap.Length == 0:
                    deltap = None
                deltar = obj.Placement.Rotation * self.oldPlacement.Rotation.inverted()
                if deltar.Angle < 0.0001:
                    deltar = None
                for child in self.getMovableChildren(obj):
                    if deltar:
                        child.Placement.rotate(
                            self.oldPlacement.Base,
                            deltar.Axis,
                            math.degrees(deltar.Angle),
                            comp=True,
                        )
                    if deltap:
                        child.Placement.move(deltap)

    def execute(self, obj):
        "gather all the child shapes into a compound"

        pl = obj.Placement
        shapes, materialstable = self.getShapes(obj)
        if shapes:
            import Part

            if obj.OnlySolids:
                f = []
                for s in shapes:
                    f.extend(s.Solids)
                # print("faces before compound:",len(f))
                obj.Shape = Part.makeCompound(f)
                # print("faces after compound:",len(obj.Shape.Faces))
                # print("recomputing ",obj.Label)
            else:
                obj.Shape = Part.makeCompound(shapes)
            obj.Placement = pl
        obj.Area = self.getArea(obj)
        obj.MaterialsTable = materialstable
        if obj.ViewObject:
            # update the autogroup box if needed
            obj.ViewObject.Proxy.onChanged(obj.ViewObject, "AutoGroupBox")

    def getMovableChildren(self, obj):
        "recursively get movable children"

        result = []
        for child in obj.Group:
            if child.isDerivedFrom("App::DocumentObjectGroup"):
                result.extend(self.getMovableChildren(child))
            if not hasattr(child, "MoveWithHost") or child.MoveWithHost:
                if hasattr(child, "Placement"):
                    result.append(child)
        return result

    def getArea(self, obj):
        "computes the area of this floor by adding its inner spaces"

        area = 0
        if hasattr(obj, "Group"):
            for child in obj.Group:
                if (Draft.get_type(child) in ["Space", "BuildingPart"]) and hasattr(
                    child, "IfcType"
                ):
                    area += child.Area.Value
        return area

    def getShapes(self, obj):
        "recursively get the shapes of objects inside this BuildingPart"

        shapes = []
        solidindex = 0
        materialstable = {}
        for child in Draft.get_group_contents(obj, walls=True):
            if not Draft.get_type(child) in ["Space"]:
                if hasattr(child, "Shape") and child.Shape:
                    shapes.append(child.Shape)
                    for solid in child.Shape.Solids:
                        matname = "Undefined"
                        if hasattr(child, "Material") and child.Material:
                            matname = child.Material.Name
                        if matname in materialstable:
                            materialstable[matname] = (
                                materialstable[matname] + "," + str(solidindex)
                            )
                        else:
                            materialstable[matname] = str(solidindex)
                        solidindex += 1
        return shapes, materialstable

    def getSpaces(self, obj):
        "gets the list of Spaces that have this object as their Zone property"

        g = []
        for o in obj.OutList:
            if hasattr(o, "Zone"):
                if o.Zone == obj:
                    g.append(o)
        return g

    def touchChildren(self, obj):
        "Touches all descendents where applicable"

        g = []
        if hasattr(obj, "Group"):
            g = obj.Group
        elif Draft.getType(obj) in ["Wall", "Structure"]:
            g = obj.Additions
        for child in g:
            if Draft.getType(child) in ["Wall", "Structure"]:
                if not child.Height.Value:
                    FreeCAD.Console.PrintLog("Auto-updating Height of " + child.Name + "\n")
                    self.touchChildren(child)
                    child.Proxy.execute(child)
            elif Draft.getType(child) in ["App::DocumentObjectGroup", "Group", "BuildingPart"]:
                self.touchChildren(child)

    def addObject(self, obj, child):
        "Adds an object to the group of this BuildingPart"

        if not child in obj.Group:
            g = obj.Group
            g.append(child)
            obj.Group = g

    def autogroup(self, obj, child):
        "Adds an object to the group of this BuildingPart automatically"

        if obj.ViewObject:
            if hasattr(obj.ViewObject.Proxy, "autobbox") and obj.ViewObject.Proxy.autobbox:
                if hasattr(child, "Shape") and child.Shape:
                    abb = obj.ViewObject.Proxy.autobbox
                    cbb = child.Shape.BoundBox
                    if abb.isValid():
                        if not cbb.isValid():
                            FreeCAD.ActiveDocument.recompute()
                        if not cbb.isValid():
                            cbb = FreeCAD.BoundBox()
                            for v in child.Shape.Vertexes:
                                print(v.Point)
                                cbb.add(v.Point)
                        if cbb.isValid() and abb.isInside(cbb):
                            self.addObject(obj, child)
                            return True
        return False


class ViewProviderBuildingPart:
    "A View Provider for the BuildingPart object"

    def __init__(self, vobj):

        if vobj:
            vobj.addExtension("Gui::ViewProviderGroupExtensionPython")
            vobj.Proxy = self
            self.setProperties(vobj)
            vobj.ShapeColor = ArchCommands.getDefaultColor("Helpers")
            self.Object = vobj.Object

    def setProperties(self, vobj):

        pl = vobj.PropertiesList
        if not "LineWidth" in pl:
            vobj.addProperty(
                "App::PropertyFloat",
                "LineWidth",
                "BuildingPart",
                QT_TRANSLATE_NOOP("App::Property", "The line width of this object"),
                locked=True,
            )
            vobj.LineWidth = 1
        if not "OverrideUnit" in pl:
            vobj.addProperty(
                "App::PropertyString",
                "OverrideUnit",
                "BuildingPart",
                QT_TRANSLATE_NOOP("App::Property", "An optional unit to express levels"),
                locked=True,
            )
        if not "DisplayOffset" in pl:
            vobj.addProperty(
                "App::PropertyPlacement",
                "DisplayOffset",
                "BuildingPart",
                QT_TRANSLATE_NOOP("App::Property", "A transformation to apply to the level mark"),
                locked=True,
            )
            vobj.DisplayOffset = FreeCAD.Placement(
                FreeCAD.Vector(0, 0, 0), FreeCAD.Rotation(FreeCAD.Vector(1, 0, 0), 90)
            )
        if not "ShowLevel" in pl:
            vobj.addProperty(
                "App::PropertyBool",
                "ShowLevel",
                "BuildingPart",
                QT_TRANSLATE_NOOP("App::Property", "If true, show the level"),
                locked=True,
            )
            vobj.ShowLevel = True
        if not "ShowUnit" in pl:
            vobj.addProperty(
                "App::PropertyBool",
                "ShowUnit",
                "BuildingPart",
                QT_TRANSLATE_NOOP("App::Property", "If true, show the unit on the level tag"),
                locked=True,
            )
        if not "OriginOffset" in pl:
            vobj.addProperty(
                "App::PropertyBool",
                "OriginOffset",
                "BuildingPart",
                QT_TRANSLATE_NOOP(
                    "App::Property", "If true, display offset will affect the origin mark too"
                ),
                locked=True,
            )
        if not "ShowLabel" in pl:
            vobj.addProperty(
                "App::PropertyBool",
                "ShowLabel",
                "BuildingPart",
                QT_TRANSLATE_NOOP("App::Property", "If true, the object's label is displayed"),
                locked=True,
            )
            vobj.ShowLabel = True
        if not "FontName" in pl:
            vobj.addProperty(
                "App::PropertyFont",
                "FontName",
                "BuildingPart",
                QT_TRANSLATE_NOOP("App::Property", "The font to be used for texts"),
                locked=True,
            )
            vobj.FontName = params.get_param("textfont")
        if not "FontSize" in pl:
            vobj.addProperty(
                "App::PropertyLength",
                "FontSize",
                "BuildingPart",
                QT_TRANSLATE_NOOP("App::Property", "The font size of texts"),
                locked=True,
            )
            vobj.FontSize = params.get_param("textheight") * params.get_param(
                "DefaultAnnoScaleMultiplier"
            )
        if not "DiffuseColor" in pl:
            vobj.addProperty(
                "App::PropertyColorList",
                "DiffuseColor",
                "BuildingPart",
                QT_TRANSLATE_NOOP("App::Property", "The individual face colors"),
                locked=True,
            )

        # Interaction properties
        if not "SetWorkingPlane" in pl:
            vobj.addProperty(
                "App::PropertyBool",
                "SetWorkingPlane",
                "Interaction",
                QT_TRANSLATE_NOOP(
                    "App::Property",
                    "If true, when activated, the working plane will automatically adapt to this level",
                ),
                locked=True,
            )
            vobj.SetWorkingPlane = True
        if not "AutoWorkingPlane" in pl:
            vobj.addProperty(
                "App::PropertyBool",
                "AutoWorkingPlane",
                "Interaction",
                QT_TRANSLATE_NOOP(
                    "App::Property", "If set to True, the working plane will be kept on Auto mode"
                ),
                locked=True,
            )
        if not "ViewData" in pl:
            vobj.addProperty(
                "App::PropertyFloatList",
                "ViewData",
                "Interaction",
                QT_TRANSLATE_NOOP(
                    "App::Property", "Camera position data associated with this object"
                ),
                locked=True,
            )
            vobj.setEditorMode("ViewData", 2)
        if not "RestoreView" in pl:
            vobj.addProperty(
                "App::PropertyBool",
                "RestoreView",
                "Interaction",
                QT_TRANSLATE_NOOP(
                    "App::Property",
                    "If set, the view stored in this object will be restored on double-click",
                ),
                locked=True,
            )
        if not "DoubleClickActivates" in pl:
            vobj.addProperty(
                "App::PropertyBool",
                "DoubleClickActivates",
                "Interaction",
                QT_TRANSLATE_NOOP(
                    "App::Property", "If True, double-clicking this object in the tree activates it"
                ),
                locked=True,
            )
            vobj.DoubleClickActivates = True

        # inventor saving
        if not "SaveInventor" in pl:
            vobj.addProperty(
                "App::PropertyBool",
                "SaveInventor",
                "Interaction",
                QT_TRANSLATE_NOOP(
                    "App::Property",
                    "If this is enabled, the OpenInventor representation of this object will be saved in the FreeCAD file, allowing to reference it in other files in lightweight mode.",
                ),
                locked=True,
            )
        if not "SavedInventor" in pl:
            vobj.addProperty(
                "App::PropertyFileIncluded",
                "SavedInventor",
                "Interaction",
                QT_TRANSLATE_NOOP(
                    "App::Property",
                    "A slot to save the OpenInventor representation of this object, if enabled",
                ),
                locked=True,
            )
            vobj.setEditorMode("SavedInventor", 2)

        # children properties
        if not "ChildrenOverride" in pl:
            vobj.addProperty(
                "App::PropertyBool",
                "ChildrenOverride",
                "Children",
                QT_TRANSLATE_NOOP(
                    "App::Property",
                    "If true, show the objects contained in this Building Part will adopt these line, color and transparency settings",
                ),
                locked=True,
            )
        if not "ChildrenLineWidth" in pl:
            vobj.addProperty(
                "App::PropertyFloat",
                "ChildrenLineWidth",
                "Children",
                QT_TRANSLATE_NOOP("App::Property", "The line width of child objects"),
                locked=True,
            )
            vobj.ChildrenLineWidth = params.get_param_view("DefaultShapeLineWidth")
        if not "ChildrenLineColor" in pl:
            vobj.addProperty(
                "App::PropertyColor",
                "ChildrenLineColor",
                "Children",
                QT_TRANSLATE_NOOP("App::Property", "The line color of child objects"),
                locked=True,
            )
            vobj.ChildrenLineColor = params.get_param_view("DefaultShapeLineColor") & 0xFFFFFF00
        if not "ChildrenShapeColor" in pl:
            vobj.addProperty(
                "App::PropertyMaterial",
                "ChildrenShapeColor",
                "Children",
                QT_TRANSLATE_NOOP("App::Property", "The shape appearance of child objects"),
                locked=True,
            )
            vobj.ChildrenShapeColor = params.get_param_view("DefaultShapeColor") & 0xFFFFFF00
        if not "ChildrenTransparency" in pl:
            vobj.addProperty(
                "App::PropertyPercent",
                "ChildrenTransparency",
                "Children",
                QT_TRANSLATE_NOOP("App::Property", "The transparency of child objects"),
                locked=True,
            )
            vobj.ChildrenTransparency = params.get_param_view("DefaultShapeTransparency")

        # clip properties
        if not "CutView" in pl:
            vobj.addProperty(
                "App::PropertyBool",
                "CutView",
                "Clip",
                QT_TRANSLATE_NOOP("App::Property", "Cut the view above this level"),
                locked=True,
            )
        if not "CutMargin" in pl:
            vobj.addProperty(
                "App::PropertyLength",
                "CutMargin",
                "Clip",
                QT_TRANSLATE_NOOP(
                    "App::Property", "The distance between the level plane and the cut line"
                ),
                locked=True,
            )
            vobj.CutMargin = 1600
        if not "AutoCutView" in pl:
            vobj.addProperty(
                "App::PropertyBool",
                "AutoCutView",
                "Clip",
                QT_TRANSLATE_NOOP("App::Property", "Turn cutting on when activating this level"),
                locked=True,
            )

        # autogroup properties
        if not "AutogroupSize" in pl:
            vobj.addProperty(
                "App::PropertyIntegerList",
                "AutogroupSize",
                "AutoGroup",
                QT_TRANSLATE_NOOP(
                    "App::Property",
                    "The capture box for newly created objects expressed as [XMin,YMin,ZMin,XMax,YMax,ZMax]",
                ),
                locked=True,
            )
        if not "AutogroupBox" in pl:
            vobj.addProperty(
                "App::PropertyBool",
                "AutogroupBox",
                "AutoGroup",
                QT_TRANSLATE_NOOP("App::Property", "Turns auto group box on/off"),
                locked=True,
            )
        if not "AutogroupAutosize" in pl:
            vobj.addProperty(
                "App::PropertyBool",
                "AutogroupAutosize",
                "AutoGroup",
                QT_TRANSLATE_NOOP("App::Property", "Automatically set size from contents"),
                locked=True,
            )
        if not "AutogroupMargin" in pl:
            vobj.addProperty(
                "App::PropertyLength",
                "AutogroupMargin",
                "AutoGroup",
                QT_TRANSLATE_NOOP("App::Property", "A margin to use when autosize is turned on"),
                locked=True,
            )

    def onDocumentRestored(self, vobj):

        self.setProperties(vobj)

    def getIcon(self):

        import Arch_rc

        if hasattr(self, "Object"):
            if self.Object.IfcType == "Building Storey":
                return ":/icons/Arch_Floor_Tree.svg"
            elif self.Object.IfcType == "Building":
                return ":/icons/Arch_Building_Tree.svg"
            elif self.Object.IfcType == "Annotation":
                return ":/icons/BIM_ArchView.svg"
            elif hasattr(self.Object, "IfcClass"):
                from nativeifc import ifc_viewproviders

                return ifc_viewproviders.get_icon(self)
        return ":/icons/Arch_BuildingPart_Tree.svg"

    def attach(self, vobj):

        self.Object = vobj.Object
        self.clip = None
        from pivy import coin

        self.sep = coin.SoGroup()
        self.mat = coin.SoMaterial()
        self.sep.addChild(self.mat)
        self.dst = coin.SoDrawStyle()
        self.sep.addChild(self.dst)
        self.lco = coin.SoCoordinate3()
        self.sep.addChild(self.lco)
        import PartGui  # Required for "SoBrepEdgeSet" (because a BuildingPart is not a Part::FeaturePython object).

        lin = coin.SoType.fromName("SoBrepEdgeSet").createInstance()
        if lin:
            lin.coordIndex.setValues([0, 1, -1, 2, 3, -1, 4, 5, -1])
        self.sep.addChild(lin)
        self.bbox = coin.SoSwitch()
        self.bbox.whichChild = -1
        bboxsep = coin.SoSeparator()
        self.bbox.addChild(bboxsep)
        drawstyle = coin.SoDrawStyle()
        drawstyle.style = coin.SoDrawStyle.LINES
        drawstyle.lineWidth = 3
        drawstyle.linePattern = 0x0F0F  # 0xaa
        bboxsep.addChild(drawstyle)
        self.bbco = coin.SoCoordinate3()
        bboxsep.addChild(self.bbco)
        lin = coin.SoIndexedLineSet()
        lin.coordIndex.setValues(
            [0, 1, 2, 3, 0, -1, 4, 5, 6, 7, 4, -1, 0, 4, -1, 1, 5, -1, 2, 6, -1, 3, 7, -1]
        )
        bboxsep.addChild(lin)
        self.sep.addChild(self.bbox)
        self.tra = coin.SoTransform()
        self.tra.rotation.setValue(FreeCAD.Rotation(0, 0, 90).Q)
        self.sep.addChild(self.tra)
        self.fon = coin.SoFont()
        self.sep.addChild(self.fon)
        self.txt = coin.SoAsciiText()
        self.txt.justification = coin.SoText2.LEFT
        self.txt.string.setValue("level")
        self.sep.addChild(self.txt)
        vobj.addDisplayMode(self.sep, "Default")
        self.onChanged(vobj, "ShapeColor")
        self.onChanged(vobj, "FontName")
        self.onChanged(vobj, "ShowLevel")
        self.onChanged(vobj, "FontSize")
        self.onChanged(vobj, "AutogroupBox")
        self.setProperties(vobj)
        return

    def getDisplayModes(self, vobj):

        return ["Default"]

    def getDefaultDisplayMode(self):

        return "Default"

    def setDisplayMode(self, mode):

        return mode

    def updateData(self, obj, prop):

        if prop in ["Placement", "LevelOffset"]:
            self.onChanged(obj.ViewObject, "OverrideUnit")
        elif prop == "Shape":
            # gather all the child shapes
            colors = self.getColors(obj)
            if colors and hasattr(obj.ViewObject, "DiffuseColor"):
                if len(colors) == len(obj.Shape.Faces):
                    if colors != obj.ViewObject.DiffuseColor:
                        obj.ViewObject.DiffuseColor = colors
                        self.writeInventor(obj)
                # else:
                # print("color mismatch:",len(colors),"colors,",len(obj.Shape.Faces),"faces")
        elif prop == "Group":
            self.onChanged(obj.ViewObject, "ChildrenOverride")
        elif prop == "Label":
            self.onChanged(obj.ViewObject, "ShowLabel")

    def getColors(self, obj):
        "recursively get the colors of objects inside this BuildingPart"

        colors = []
        for child in Draft.get_group_contents(obj, walls=True):
            if not Draft.get_type(child) in ["Space"]:
                if hasattr(child, "Shape") and (
                    hasattr(child.ViewObject, "DiffuseColor")
                    or hasattr(child.ViewObject, "ShapeColor")
                ):
                    if hasattr(child.ViewObject, "DiffuseColor") and len(
                        child.ViewObject.DiffuseColor
                    ) == len(child.Shape.Faces):
                        colors.extend(child.ViewObject.DiffuseColor)
                    else:
                        c = child.ViewObject.ShapeColor[:3] + (
                            1.0 - child.ViewObject.Transparency / 100.0,
                        )
                        for i in range(len(child.Shape.Faces)):
                            colors.append(c)
        return colors

    def onChanged(self, vobj, prop):

        # print(vobj.Object.Label," - ",prop)

        if prop == "ShapeColor":
            if hasattr(vobj, "ShapeColor"):
                l = vobj.ShapeColor
                self.mat.diffuseColor.setValue([l[0], l[1], l[2]])
        elif prop == "LineWidth":
            if hasattr(vobj, "LineWidth"):
                self.dst.lineWidth = vobj.LineWidth
        elif prop == "FontName":
            if hasattr(vobj, "FontName") and hasattr(self, "fon"):
                if vobj.FontName:
                    self.fon.name = vobj.FontName
        elif prop in ["FontSize", "DisplayOffset", "OriginOffset"]:
            if (
                hasattr(vobj, "FontSize")
                and hasattr(vobj, "DisplayOffset")
                and hasattr(vobj, "OriginOffset")
                and hasattr(self, "fon")
            ):
                fs = vobj.FontSize.Value
                if fs:
                    self.fon.size = fs
                    b = vobj.DisplayOffset.Base
                    self.tra.translation.setValue([b.x + fs / 8, b.y, b.z + fs / 8])
                    r = vobj.DisplayOffset.Rotation
                    self.tra.rotation.setValue(r.Q)
                    if vobj.OriginOffset:
                        self.lco.point.setValues(
                            [
                                [b.x - fs, b.y, b.z],
                                [b.x + fs, b.y, b.z],
                                [b.x, b.y - fs, b.z],
                                [b.x, b.y + fs, b.z],
                                [b.x, b.y, b.z - fs],
                                [b.x, b.y, b.z + fs],
                            ]
                        )
                    else:
                        self.lco.point.setValues(
                            [
                                [-fs, 0, 0],
                                [fs, 0, 0],
                                [0, -fs, 0],
                                [0, fs, 0],
                                [0, 0, -fs],
                                [0, 0, fs],
                            ]
                        )
        elif prop in ["OverrideUnit", "ShowUnit", "ShowLevel", "ShowLabel"]:
            if (
                hasattr(vobj, "OverrideUnit")
                and hasattr(vobj, "ShowUnit")
                and hasattr(vobj, "ShowLevel")
                and hasattr(vobj, "ShowLabel")
                and hasattr(self, "txt")
            ):
                offset = getattr(vobj.Object, "LevelOffset", 0)
                if hasattr(offset, "Value"):
                    offset = offset.Value
                z = vobj.Object.Placement.Base.z + offset
                q = FreeCAD.Units.Quantity(z, FreeCAD.Units.Length)
                txt = ""
                if vobj.ShowLabel:
                    txt += vobj.Object.Label
                if vobj.ShowLevel:
                    if txt:
                        txt += " "
                    if z >= 0:
                        txt += "+"
                    if vobj.OverrideUnit:
                        u = vobj.OverrideUnit
                    else:
                        u = q.getUserPreferred()[2]
                    try:
                        txt += units.display_external(float(q), None, "Length", vobj.ShowUnit, u)
                    except Exception:
                        q = q.getValueAs(q.getUserPreferred()[2])
                        d = params.get_param("Decimals", path="Units")
                        fmt = "{0:." + str(d) + "f}"
                        if not vobj.ShowUnit:
                            u = ""
                        txt += fmt.format(float(q)) + str(u)
                if not txt:
                    txt = " "  # empty texts make coin crash...
                if isinstance(txt, unicode):
                    txt = txt.encode("utf8")
                self.txt.string.setValue(txt)
        elif prop in [
            "ChildrenOverride",
            "ChildenLineWidth",
            "ChildrenLineColor",
            "ChildrenShapeColor",
            "ChildrenTransparency",
        ]:
            if hasattr(vobj, "ChildrenOverride") and vobj.ChildrenOverride:
                props = [
                    "ChildenLineWidth",
                    "ChildrenLineColor",
                    "ChildrenShapeColor",
                    "ChildrenTransparency",
                ]
                for child in vobj.Object.Group:
                    for prop in props:
                        if (
                            hasattr(vobj, prop)
                            and hasattr(child.ViewObject, prop[8:])
                            and not hasattr(child, "ChildrenOverride")
                        ):
                            setattr(child.ViewObject, prop[8:], getattr(vobj, prop))
        elif prop in ["CutView", "CutMargin"]:
            if (
                hasattr(vobj, "CutView")
                and FreeCADGui.ActiveDocument.ActiveView
                and hasattr(FreeCADGui.ActiveDocument.ActiveView, "getSceneGraph")
            ):
                sg = FreeCADGui.ActiveDocument.ActiveView.getSceneGraph()
                if vobj.CutView:
                    from pivy import coin

                    if self.clip:
                        sg.removeChild(self.clip)
                        self.clip = None
                    for o in Draft.get_group_contents(vobj.Object.Group, walls=True):
                        if hasattr(o.ViewObject, "Lighting"):
                            o.ViewObject.Lighting = "One side"
                    self.clip = coin.SoClipPlane()
                    self.clip.on.setValue(True)
                    norm = vobj.Object.Placement.multVec(FreeCAD.Vector(0, 0, 1))
                    mp = vobj.Object.Placement.Base
                    mp = DraftVecUtils.project(mp, norm)
                    dist = mp.Length  # - 0.1 # to not clip exactly on the section object
                    norm = norm.negative()
                    marg = 1
                    if hasattr(vobj, "CutMargin"):
                        marg = vobj.CutMargin.Value
                    if mp.getAngle(norm) > 1:
                        dist += marg
                        dist = -dist
                    else:
                        dist -= marg
                    plane = coin.SbPlane(coin.SbVec3f(norm.x, norm.y, norm.z), dist)
                    self.clip.plane.setValue(plane)
                    sg.insertChild(self.clip, 0)
                else:
                    if getattr(self, "clip", None):
                        sg.removeChild(self.clip)
                        self.clip = None
                    for o in Draft.get_group_contents(vobj.Object.Group, walls=True):
                        if hasattr(o.ViewObject, "Lighting"):
                            o.ViewObject.Lighting = "Two side"
        elif prop == "Visibility":
            # turn clipping off when turning the object off
            if hasattr(vobj, "Visibility") and not (vobj.Visibility) and hasattr(vobj, "CutView"):
                vobj.CutView = False
        elif prop == "SaveInventor":
            self.writeInventor(vobj.Object)
        elif prop in ["AutogroupBox", "AutogroupSize"]:
            if hasattr(vobj, "AutogroupBox") and hasattr(vobj, "AutogroupSize"):
                if vobj.AutogroupBox:
                    if len(vobj.AutogroupSize) >= 6:
                        self.autobbox = FreeCAD.BoundBox(*vobj.AutogroupSize[0:6])
                        self.autobbox.move(vobj.Object.Placement.Base)
                        pts = [list(self.autobbox.getPoint(i)) for i in range(8)]
                        self.bbco.point.setValues(pts)
                        self.bbox.whichChild = 0
                else:
                    self.autobbox = None
                    self.bbox.whichChild = -1
        elif prop in ["AutogroupAutosize", "AutogroupMargin"]:
            if hasattr(vobj, "AutogroupAutosize") and vobj.AutogroupAutosize:
                bbox = vobj.Object.Shape.BoundBox
                bbox.enlarge(vobj.AutogroupMargin.Value)
                vobj.AutogroupSize = [
                    int(i)
                    for i in [bbox.XMin, bbox.YMin, bbox.ZMin, bbox.XMax, bbox.YMax, bbox.ZMax]
                ]

    def onDelete(self, vobj, subelements):

        if self.clip:
            sg = FreeCADGui.ActiveDocument.ActiveView.getSceneGraph()
            sg.removeChild(self.clip)
            self.clip = None
        for o in Draft.get_group_contents(vobj.Object.Group, walls=True):
            if hasattr(o.ViewObject, "Lighting"):
                o.ViewObject.Lighting = "Two side"
        return True

    def setEdit(self, vobj, mode):
        # mode == 1 if Transform is selected in the Tree view context menu.
        # mode == 2 has been added for consistency.
        if mode == 1 or mode == 2:
            return None
        # For some reason mode is always 0 if the object is double-clicked in
        # the Tree view. Using FreeCADGui.getUserEditMode() as a workaround.
        if FreeCADGui.getUserEditMode() in ("Transform", "Cutting"):
            return None

        self.activate()
        return False  # Return `False` as we don't want to enter edit mode.

    def unsetEdit(self, vobj, mode):
        if mode == 1 or mode == 2:
            return None
        if FreeCADGui.getUserEditMode() in ("Transform", "Cutting"):
            return None

        return True

    def setupContextMenu(self, vobj, menu):
        from PySide import QtCore, QtGui
        import Draft_rc

        if FreeCADGui.activeWorkbench().name() != "BIMWorkbench":
            return
        if (not hasattr(vobj, "DoubleClickActivates")) or vobj.DoubleClickActivates:
            menuTxt = translate("Arch", "Active")
            actionActivate = QtGui.QAction(menuTxt, menu)
            actionActivate.setCheckable(True)
            if FreeCADGui.ActiveDocument.ActiveView.getActiveObject("Arch") == self.Object:
                actionActivate.setChecked(True)
            else:
                actionActivate.setChecked(False)
            actionActivate.triggered.connect(lambda _: self.activate(actionActivate))
            menu.addAction(actionActivate)

        actionSetWorkingPlane = QtGui.QAction(
            QtGui.QIcon(":/icons/Draft_SelectPlane.svg"),
            translate("Arch", "Set Working Plane"),
            menu,
        )
        QtCore.QObject.connect(
            actionSetWorkingPlane, QtCore.SIGNAL("triggered()"), self.setWorkingPlane
        )
        menu.addAction(actionSetWorkingPlane)

        actionWriteCamera = QtGui.QAction(
            QtGui.QIcon(":/icons/Draft_SelectPlane.svg"),
            translate("Arch", "Write Camera Position"),
            menu,
        )
        QtCore.QObject.connect(actionWriteCamera, QtCore.SIGNAL("triggered()"), self.writeCamera)
        menu.addAction(actionWriteCamera)

        actionCreateGroup = QtGui.QAction(translate("Arch", "New Group"), menu)
        QtCore.QObject.connect(actionCreateGroup, QtCore.SIGNAL("triggered()"), self.createGroup)
        menu.addAction(actionCreateGroup)

        actionReorder = QtGui.QAction(translate("Arch", "Reorder Children Alphabetically"), menu)
        QtCore.QObject.connect(actionReorder, QtCore.SIGNAL("triggered()"), self.reorder)
        menu.addAction(actionReorder)

        actionCloneUp = QtGui.QAction(translate("Arch", "Clone Level Up"), menu)
        QtCore.QObject.connect(actionCloneUp, QtCore.SIGNAL("triggered()"), self.cloneUp)
        menu.addAction(actionCloneUp)

    def activate(self, action=None):
        from draftutils.gui_utils import toggle_working_plane

        vobj = self.Object.ViewObject

        if (not hasattr(vobj, "DoubleClickActivates")) or vobj.DoubleClickActivates:
            if toggle_working_plane(self.Object, action, restore=True):
                print("Setting active working plane to: ", self.Object.Label)
            else:
                print("Deactivating working plane from: ", self.Object.Label)

        FreeCADGui.Selection.clearSelection()

    def setWorkingPlane(self, restore=False):
        vobj = self.Object.ViewObject

        import WorkingPlane

        wp = WorkingPlane.get_working_plane(update=False)
        autoclip = False
        if hasattr(vobj, "AutoCutView"):
            autoclip = vobj.AutoCutView
        if restore:
            if wp.label.rstrip("*") == self.Object.Label:
                prev_data = wp._previous()
                if prev_data:
                    prev_label = prev_data.get("label", "").rstrip("*")
                    prev_obj = None
                    for obj in FreeCAD.ActiveDocument.Objects:
                        if hasattr(obj, "Label") and obj.Label == prev_label:
                            prev_obj = obj
                            break

                    if prev_obj:
                        # check in which context we need to set the active object
                        context = "Arch"
                        obj_type = Draft.getType(prev_obj)
                        if obj_type == "IfcBuildingStorey":
                            context = "NativeIFC"
                        FreeCADGui.ActiveDocument.ActiveView.setActiveObject(context, prev_obj)
                        print(f"Set active object to: {prev_obj.Label} (context: {context})")

            if autoclip:
                vobj.CutView = False
        else:
            wp.align_to_selection()
            if autoclip:
                vobj.CutView = True

    def writeCamera(self):

        if hasattr(self, "Object"):
            from pivy import coin

            n = FreeCADGui.ActiveDocument.ActiveView.getCameraNode()
            FreeCAD.Console.PrintMessage(
                QT_TRANSLATE_NOOP("Draft", "Writing camera position") + "\n"
            )
            cdata = list(n.position.getValue().getValue())
            cdata.extend(list(n.orientation.getValue().getValue()))
            cdata.append(n.nearDistance.getValue())
            cdata.append(n.farDistance.getValue())
            cdata.append(n.aspectRatio.getValue())
            cdata.append(n.focalDistance.getValue())
            if isinstance(n, coin.SoOrthographicCamera):
                cdata.append(n.height.getValue())
                cdata.append(0.0)  # orthograhic camera
            elif isinstance(n, coin.SoPerspectiveCamera):
                cdata.append(n.heightAngle.getValue())
                cdata.append(1.0)  # perspective camera
            self.Object.ViewObject.ViewData = cdata

    def createGroup(self):

        if hasattr(self, "Object"):
            s = (
                'FreeCAD.ActiveDocument.getObject("%s").newObject("App::DocumentObjectGroup","Group")'
                % self.Object.Name
            )
            FreeCADGui.doCommand(s)

    def reorder(self):

        if hasattr(self, "Object"):
            if hasattr(self.Object, "Group") and self.Object.Group:
                g = self.Object.Group
                g.sort(key=lambda obj: obj.Label)
                self.Object.Group = g
                FreeCAD.ActiveDocument.recompute()

    def cloneUp(self):

        if hasattr(self, "Object"):
            if not self.Object.Height.Value:
                FreeCAD.Console.PrintError(
                    "This level has no height value. Define a height before using this function.\n"
                )
                return
            height = self.Object.Height.Value
            ng = []
            if hasattr(self.Object, "Group") and self.Object.Group:
                for o in self.Object.Group:
                    no = Draft.clone(o)
                    Draft.move(no, FreeCAD.Vector(0, 0, height))
                    ng.append(no)
            nobj = Arch.makeBuildingPart()
            Draft.formatObject(nobj, self.Object)
            nobj.Placement = self.Object.Placement
            nobj.Placement.move(FreeCAD.Vector(0, 0, height))
            nobj.IfcType = self.Object.IfcType
            nobj.Height = height
            nobj.Label = self.Object.Label
            nobj.Group = ng
            for parent in self.Object.InList:
                if (
                    hasattr(parent, "Group")
                    and hasattr(parent, "addObject")
                    and (self.Object in parent.Group)
                ):
                    parent.addObject(nobj)
            FreeCAD.ActiveDocument.recompute()
            # fix for missing IFC attributes
            for no in ng:
                if (
                    hasattr(no, "LongName")
                    and hasattr(no, "CloneOf")
                    and no.CloneOf
                    and hasattr(no.CloneOf, "LongName")
                ):
                    no.LongName = no.CloneOf.LongName
            FreeCAD.ActiveDocument.recompute()

    def dumps(self):
        return None

    def loads(self, state):
        return None

    def writeInventor(self, obj):

        def callback(match):
            return next(callback.v)

        if hasattr(obj.ViewObject, "SaveInventor") and obj.ViewObject.SaveInventor:
            if obj.Shape and obj.Shape.Faces and hasattr(obj, "SavedInventor"):
                colors = obj.ViewObject.DiffuseColor
                if len(colors) != len(obj.Shape.Faces):
                    print("Debug: Colors mismatch in", obj.Label)
                    colors = None
                iv = self.Object.Shape.writeInventor()
                import re

                if colors:
                    if len(re.findall(r"IndexedFaceSet", iv)) == len(obj.Shape.Faces):
                        # convert colors to iv representations
                        colors = [
                            "Material { diffuseColor "
                            + str(color[0])
                            + " "
                            + str(color[1])
                            + " "
                            + str(color[2])
                            + "}\n    IndexedFaceSet"
                            for color in colors
                        ]
                        # replace
                        callback.v = iter(colors)
                        iv = re.sub(r"IndexedFaceSet", callback, iv)
                    else:
                        print("Debug: IndexedFaceSet mismatch in", obj.Label)
                # save embedded file
                tf = tempfile.mkstemp(prefix=obj.Name, suffix=".iv")[1]
                f = open(tf, "w")
                f.write(iv)
                f.close()
                obj.SavedInventor = tf
                os.remove(tf)