-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
ifc_tools.py
1322 lines (1156 loc) · 47.6 KB
/
ifc_tools.py
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
# ***************************************************************************
# * *
# * Copyright (c) 2022 Yorik van Havre <yorik@uncreated.net> *
# * *
# * This program is free software; you can redistribute it and/or modify *
# * it under the terms of the GNU General Public License (GPL) *
# * as published by the Free Software Foundation; either version 3 of *
# * the License, or (at your option) any later version. *
# * for detail see the LICENCE text file. *
# * *
# * This program 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 General Public License for more details. *
# * *
# * You should have received a copy of the GNU Library General Public *
# * License along with this program; if not, write to the Free Software *
# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
# * USA *
# * *
# ***************************************************************************
"""This is the main NativeIFC module"""
import os
# heavyweight libraries - ifc_tools should always be lazy loaded
import FreeCAD
import Draft
import Arch
import exportIFC
import exportIFCHelper
import ifcopenshell
from ifcopenshell import geom
from ifcopenshell import api
from ifcopenshell import template
from ifcopenshell.util import attribute
from ifcopenshell.util import schema
from ifcopenshell.util import placement
from ifcopenshell.util import unit
import ifc_objects
import ifc_viewproviders
import ifc_import
import ifc_layers
SCALE = 1000.0 # IfcOpenShell works in meters, FreeCAD works in mm
SHORT = False # If True, only Step ID attribute is created
ROUND = 8 # rounding value for placements
DEFAULT_SHAPEMODE = "Coin" # Can be Shape, Coin or None
PARAMS = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/NativeIFC")
def create_document(document, filename=None, shapemode=0, strategy=0, silent=False):
"""Creates a IFC document object in the given FreeCAD document or converts that
document into an IFC document, depending on the value of SingleDoc preference.
filename: If not given, a blank IFC document is created
shapemode: 0 = full shape
1 = coin only
2 = no representation
strategy: 0 = only root object
1 = only bbuilding structure,
2 = all children
"""
if PARAMS.GetBool("SingleDoc", False):
return convert_document(document, filename, shapemode, strategy, silent)
else:
return create_document_object(document, filename, shapemode, strategy, silent)
def create_document_object(
document, filename=None, shapemode=0, strategy=0, silent=False
):
"""Creates a IFC document object in the given FreeCAD document.
filename: If not given, a blank IFC document is created
shapemode: 0 = full shape
1 = coin only
2 = no representation
strategy: 0 = only root object
1 = only bbuilding structure,
2 = all children
"""
obj = add_object(document, otype="project")
ifcfile, project, full = setup_project(obj, filename, shapemode, silent)
# populate according to strategy
if strategy == 0:
pass
elif strategy == 1:
create_children(obj, ifcfile, recursive=True, only_structure=True)
elif strategy == 2:
create_children(obj, ifcfile, recursive=True, assemblies=False)
# create default structure
if full:
site = aggregate(Arch.makeSite(), obj)
building = aggregate(Arch.makeBuilding(), site)
storey = aggregate(Arch.makeFloor(), building)
return obj
def convert_document(document, filename=None, shapemode=0, strategy=0, silent=False):
"""Converts the given FreeCAD document to an IFC document.
filename: If not given, a blank IFC document is created
shapemode: 0 = full shape
1 = coin only
2 = no representation
strategy: 0 = only root object
1 = only bbuilding structure
2 = all children
3 = no children
"""
if not "Proxy" in document.PropertiesList:
document.addProperty("App::PropertyPythonObject", "Proxy")
document.setPropertyStatus("Proxy", "Transient")
document.Proxy = ifc_objects.document_object()
ifcfile, project, full = setup_project(document, filename, shapemode, silent)
if strategy == 0:
create_children(document, ifcfile, recursive=False)
elif strategy == 1:
create_children(document, ifcfile, recursive=True, only_structure=True)
elif strategy == 2:
create_children(document, ifcfile, recursive=True, assemblies=False)
elif strategy == 3:
pass
# create default structure
if full:
site = aggregate(Arch.makeSite(), document)
building = aggregate(Arch.makeBuilding(), site)
storey = aggregate(Arch.makeFloor(), building)
return document
def setup_project(proj, filename, shapemode, silent):
"""Setups a project (common operations between signle doc/not single doc modes)
Returns the ifcfile object, the project ifc entity, and full (True/False)"""
full = False
d = "The path to the linked IFC file"
if not "IfcFilePath" in proj.PropertiesList:
proj.addProperty("App::PropertyFile", "IfcFilePath", "Base", d)
if not "Modified" in proj.PropertiesList:
proj.addProperty("App::PropertyBool", "Modified", "Base")
proj.setPropertyStatus("Modified", "Hidden")
if filename:
# opening existing file
proj.IfcFilePath = filename
ifcfile = ifcopenshell.open(filename)
else:
# creating a new file
if not silent:
full = ifc_import.get_project_type()
ifcfile = create_ifcfile()
project = ifcfile.by_type("IfcProject")[0]
# TODO configure version history
# https://blenderbim.org/docs-python/autoapi/ifcopenshell/api/owner/create_owner_history/index.html
# In IFC4, history is optional. What should we do here?
proj.Proxy.ifcfile = ifcfile
add_properties(proj, ifcfile, project, shapemode=shapemode)
if not "Schema" in proj.PropertiesList:
proj.addProperty("App::PropertyEnumeration", "Schema", "Base")
# bug in FreeCAD - to avoid a crash, pre-populate the enum with one value
proj.Schema = [ifcfile.wrapped_data.schema_name()]
proj.Schema = ifcfile.wrapped_data.schema_name()
proj.Schema = ifcopenshell.ifcopenshell_wrapper.schema_names()
return ifcfile, project, full
def create_ifcfile():
"""Creates a new, empty IFC document"""
ifcfile = api_run("project.create_file")
project = api_run("root.create_entity", ifcfile, ifc_class="IfcProject")
param = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Document")
user = param.GetString("prefAuthor", "")
user = user.split("<")[0].strip()
if user:
person = api_run("owner.add_person", ifcfile, family_name=user)
org = param.GetString("prefCompany", "")
if org:
organisation = api_run("owner.add_organisation", ifcfile, name=org)
if user and org:
api_run(
"owner.add_person_and_organisation",
ifcfile,
person=person,
organisation=organisation,
)
application = "FreeCAD"
version = FreeCAD.Version()
version = ".".join([str(v) for v in version[0:3]])
application = api_run(
"owner.add_application",
ifcfile,
application_full_name=application,
version=version,
)
# context
model3d = api_run("context.add_context", ifcfile, context_type="Model")
plan = api_run("context.add_context", ifcfile, context_type="Plan")
body = api_run(
"context.add_context",
ifcfile,
context_type="Model",
context_identifier="Body",
target_view="MODEL_VIEW",
parent=model3d,
)
api_run(
"context.add_context",
ifcfile,
context_type="Model",
context_identifier="Axis",
target_view="GRAPH_VIEW",
parent=model3d,
)
# unit
# for now, assign a default metre unit, as per https://blenderbim.org/docs-python/autoapi/ifcopenshell/api/unit/assign_unit/index.html
# TODO allow to set this at creation, from the current FreeCAD units schema
api_run("unit.assign_unit", ifcfile)
return ifcfile
def api_run(*args, **kwargs):
"""Runs an IfcOpenShell API call and flags the ifcfile as modified"""
result = ifcopenshell.api.run(*args, **kwargs)
# *args are typically command, ifcfile
if len(args) > 1:
ifcfile = args[1]
for d in FreeCAD.listDocuments().values():
for o in d.Objects:
if hasattr(o, "Proxy") and hasattr(o.Proxy, "ifcfile"):
if o.Proxy.ifcfile == ifcfile:
o.Modified = True
return result
def create_object(ifcentity, document, ifcfile, shapemode=0):
"""Creates a FreeCAD object from an IFC entity"""
exobj = get_object(ifcentity, document)
if exobj:
return exobj
s = "IFC: Created #{}: {}, '{}'\n".format(
ifcentity.id(), ifcentity.is_a(), ifcentity.Name
)
FreeCAD.Console.PrintLog(s)
obj = add_object(document)
add_properties(obj, ifcfile, ifcentity, shapemode=shapemode)
ifc_layers.add_layers(obj, ifcentity, ifcfile)
if FreeCAD.GuiUp:
if ifcentity.is_a("IfcSpace") or ifcentity.is_a("IfcOpeningElement"):
obj.ViewObject.DisplayMode = "Wireframe"
elements = [ifcentity]
return obj
def create_children(
obj,
ifcfile=None,
recursive=False,
only_structure=False,
assemblies=True,
expand=False,
):
"""Creates a hierarchy of objects under an object"""
def get_parent_objects(parent):
proj = get_project(parent)
if hasattr(proj, "OutListRecursive"):
return proj.OutListRecursive
elif hasattr(proj, "Objects"):
return proj.Objects
def create_child(parent, element):
subresult = []
# do not create if a child with same stepid already exists
if not element.id() in [
getattr(c, "StepId", 0) for c in get_parent_objects(parent)
]:
doc = getattr(parent, "Document", parent)
mode = getattr(parent, "ShapeMode", "Coin")
child = create_object(element, doc, ifcfile, mode)
subresult.append(child)
if isinstance(parent, FreeCAD.DocumentObject):
parent.Proxy.addObject(parent, child)
if element.is_a("IfcSite"):
# force-create contained buildings too if we just created a site
buildings = [
o for o in get_children(child, ifcfile) if o.is_a("IfcBuilding")
]
for building in buildings:
subresult.extend(create_child(child, building))
elif element.is_a("IfcOpeningElement"):
# force-create contained windows too if we just created an opening
windows = [
o
for o in get_children(child, ifcfile)
if o.is_a() in ("IfcWindow", "IfcDoor")
]
for window in windows:
subresult.extend(create_child(child, window))
if recursive:
subresult.extend(
create_children(
child, ifcfile, recursive, only_structure, assemblies
)
)
return subresult
if not ifcfile:
ifcfile = get_ifcfile(obj)
result = []
children = get_children(obj, ifcfile, only_structure, assemblies, expand)
for child in children:
result.extend(create_child(obj, child))
assign_groups(children)
return result
def assign_groups(children):
"""Fill the groups inthis list"""
for child in children:
if child.is_a("IfcGroup"):
grobj = get_object(child)
for rel in child.IsGroupedBy:
for elem in rel.RelatedObjects:
elobj = get_object(elem)
if elobj:
if len(elobj.InList) == 1:
p = elobj.InList[0]
if elobj in p.Group:
g = p.Group
g.remove(elobj)
p.Group = g
g = grobj.Group
g.append(elobj)
grobj.Group = g
def get_children(
obj, ifcfile=None, only_structure=False, assemblies=True, expand=False
):
"""Returns the direct descendants of an object"""
if not ifcfile:
ifcfile = get_ifcfile(obj)
ifcentity = ifcfile[obj.StepId]
children = []
if assemblies or not ifcentity.is_a("IfcElement"):
for rel in getattr(ifcentity, "IsDecomposedBy", []):
children.extend(rel.RelatedObjects)
if not only_structure:
for rel in getattr(ifcentity, "ContainsElements", []):
children.extend(rel.RelatedElements)
for rel in getattr(ifcentity, "HasOpenings", []):
children.extend([rel.RelatedOpeningElement])
for rel in getattr(ifcentity, "HasFillings", []):
children.extend([rel.RelatedBuildingElement])
return filter_elements(
children, ifcfile, expand=expand, spaces=True, assemblies=assemblies
)
def get_object(element, document=None):
"""Returns the object that references this element, if any"""
if document:
ldocs = {"document": document}
else:
ldocs = FreeCAD.listDocuments()
for n, d in ldocs.items():
for obj in d.Objects:
if hasattr(obj, "StepId"):
if obj.StepId == element.id():
if get_ifc_element(obj) == element:
return obj
return None
def get_ifcfile(obj):
"""Returns the ifcfile that handles this object"""
project = get_project(obj)
if project:
if getattr(project, "Proxy", None):
if hasattr(project.Proxy, "ifcfile"):
return project.Proxy.ifcfile
if project.IfcFilePath:
ifcfile = ifcopenshell.open(project.IfcFilePath)
if hasattr(project, "Proxy"):
if project.Proxy is None:
if not isinstance(project, FreeCAD.DocumentObject):
project.Proxy = ifc_objects.document_object()
if getattr(project, "Proxy", None):
project.Proxy.ifcfile = ifcfile
return ifcfile
return None
def get_project(obj):
"""Returns the ifc document this object belongs to.
obj can be either a document object, an ifcfile or ifc element instance"""
proj_types = ("IfcProject", "IfcProjectLibrary")
if isinstance(obj, ifcopenshell.file):
for d in FreeCAD.listDocuments().values():
for o in d.Objects:
if hasattr(o, "Proxy") and hasattr(o.Proxy, "ifcfile"):
if o.Proxy.ifcfile == obj:
return o
return None
if isinstance(obj, ifcopenshell.entity_instance):
obj = get_object(obj)
if hasattr(obj, "IfcFilePath"):
return obj
if hasattr(getattr(obj, "Document", None), "IfcFilePath"):
return obj.Document
if getattr(obj, "Class", None) in proj_types:
return obj
if hasattr(obj, "InListRecursive"):
for parent in obj.InListRecursive:
if getattr(parent, "Class", None) in proj_types:
return parent
return None
def can_expand(obj, ifcfile=None):
"""Returns True if this object can have any more child extracted"""
if not ifcfile:
ifcfile = get_ifcfile(obj)
children = get_children(obj, ifcfile, expand=True)
group = [o.StepId for o in obj.Group if hasattr(o, "StepId")]
for child in children:
if child.id() not in group:
return True
return False
def add_object(document, otype=None, oname="IfcObject"):
"""adds a new object to a FreeCAD document.
otype can be 'project', 'group', 'material', 'layer' or None (normal object)"""
if not document:
return None
proxy = ifc_objects.ifc_object(otype)
if otype == "group":
proxy = None
ftype = "App::DocumentObjectGroupPython"
elif otype == "material":
ftype = "App::MaterialObjectPython"
elif otype == "layer":
ftype = "App::FeaturePython"
else:
ftype = "Part::FeaturePython"
if otype == "project":
vp = ifc_viewproviders.ifc_vp_document()
elif otype == "group":
vp = ifc_viewproviders.ifc_vp_group()
elif otype == "material":
vp = ifc_viewproviders.ifc_vp_material()
elif otype == "layer":
vp = None
else:
vp = ifc_viewproviders.ifc_vp_object()
obj = document.addObject(ftype, oname, proxy, vp, False)
if obj.ViewObject and otype == "layer":
from draftviewproviders import view_layer # lazy import
view_layer.ViewProviderLayer(obj.ViewObject)
obj.ViewObject.addProperty("App::PropertyBool", "HideChildren", "Layer")
obj.ViewObject.HideChildren = True
return obj
def add_properties(
obj, ifcfile=None, ifcentity=None, links=False, shapemode=0, short=SHORT
):
"""Adds the properties of the given IFC object to a FreeCAD object"""
if not ifcfile:
ifcfile = get_ifcfile(obj)
if not ifcentity:
ifcentity = get_ifc_element(obj)
if getattr(ifcentity, "Name", None):
obj.Label = ifcentity.Name
elif getattr(obj, "IfcFilePath", ""):
obj.Label = os.path.splitext(os.path.basename(obj.IfcFilePath))[0]
else:
obj.Label = "_" + ifcentity.is_a()
if isinstance(obj, FreeCAD.DocumentObject) and "Group" not in obj.PropertiesList:
obj.addProperty("App::PropertyLinkList", "Group", "Base")
if "ShapeMode" not in obj.PropertiesList:
obj.addProperty("App::PropertyEnumeration", "ShapeMode", "Base")
shapemodes = [
"Shape",
"Coin",
"None",
] # possible shape modes for all IFC objects
if isinstance(shapemode, int):
shapemode = shapemodes[shapemode]
obj.ShapeMode = shapemodes
obj.ShapeMode = shapemode
if not obj.isDerivedFrom("Part::Feature"):
obj.setPropertyStatus("ShapeMode", "Hidden")
attr_defs = ifcentity.wrapped_data.declaration().as_entity().all_attributes()
try:
info_ifcentity = ifcentity.get_info()
except:
# slower but no errors
info_ifcentity = get_elem_attribs(ifcentity)
for attr, value in info_ifcentity.items():
if attr == "type":
attr = "Class"
elif attr == "id":
attr = "StepId"
elif attr == "Name":
continue
if short and attr not in ("Class", "StepId"):
continue
attr_def = next((a for a in attr_defs if a.name() == attr), None)
data_type = (
ifcopenshell.util.attribute.get_primitive_type(attr_def)
if attr_def
else None
)
if attr == "Class":
# main enum property, not saved to file
if attr not in obj.PropertiesList:
obj.addProperty("App::PropertyEnumeration", attr, "IFC")
obj.setPropertyStatus(attr, "Transient")
# to avoid bug/crash: we populate first the property with only the
# class, then we add the sibling classes
setattr(obj, attr, [value])
setattr(obj, attr, value)
setattr(obj, attr, get_ifc_classes(obj, value))
# companion hidden propertym that gets saved to file
if "IfcClass" not in obj.PropertiesList:
obj.addProperty("App::PropertyString", "IfcClass", "IFC")
obj.setPropertyStatus("IfcClass", "Hidden")
setattr(obj, "IfcClass", value)
elif attr_def and "IfcLengthMeasure" in str(attr_def.type_of_attribute()):
obj.addProperty("App::PropertyDistance", attr, "IFC")
if value:
setattr(obj, attr, value * (1 / get_scale(ifcfile)))
elif isinstance(value, int):
if attr not in obj.PropertiesList:
obj.addProperty("App::PropertyInteger", attr, "IFC")
if attr == "StepId":
obj.setPropertyStatus(attr, "ReadOnly")
setattr(obj, attr, value)
elif isinstance(value, float):
if attr not in obj.PropertiesList:
obj.addProperty("App::PropertyFloat", attr, "IFC")
setattr(obj, attr, value)
elif data_type == "boolean":
if attr not in obj.PropertiesList:
obj.addProperty("App::PropertyBool", attr, "IFC")
if not value or value in ["UNKNOWN", "FALSE"]:
value = False
elif not isinstance(value, bool):
print("DEBUG: attempting to set boolean value:", attr, value)
value = bool(value)
setattr(obj, attr, value) # will trigger error. TODO: Fix this
elif isinstance(value, ifcopenshell.entity_instance):
if links:
if attr not in obj.PropertiesList:
# value = create_object(value, obj.Document)
obj.addProperty("App::PropertyLink", attr, "IFC")
# setattr(obj, attr, value)
elif isinstance(value, (list, tuple)) and value:
if isinstance(value[0], ifcopenshell.entity_instance):
if links:
if attr not in obj.PropertiesList:
# nvalue = []
# for elt in value:
# nvalue.append(create_object(elt, obj.Document))
obj.addProperty("App::PropertyLinkList", attr, "IFC")
# setattr(obj, attr, nvalue)
elif data_type == "enum":
if attr not in obj.PropertiesList:
obj.addProperty("App::PropertyEnumeration", attr, "IFC")
items = ifcopenshell.util.attribute.get_enum_items(attr_def)
if value not in items:
for v in ("UNDEFINED", "NOTDEFINED", "USERDEFINED"):
if v in items:
value = v
break
if value in items:
# to prevent bug/crash, we first need to populate the
# enum with the value about to be used, then
# add the alternatives
setattr(obj, attr, [value])
setattr(obj, attr, value)
setattr(obj, attr, items)
else:
if attr not in obj.PropertiesList:
obj.addProperty("App::PropertyString", attr, "IFC")
if value is not None:
setattr(obj, attr, str(value))
# link Label2 and Description
if "Description" in obj.PropertiesList and hasattr(obj, "setExpression"):
obj.setExpression("Label2", "Description")
def remove_unused_properties(obj):
"""Remove IFC properties if they are not part of the current IFC class"""
elt = get_ifc_element(obj)
props = list(elt.get_info().keys())
props[props.index("id")] = "StepId"
props[props.index("type")] = "Class"
for prop in obj.PropertiesList:
if obj.getGroupOfProperty(prop) == "IFC":
if prop not in props:
obj.removeProperty(prop)
def get_ifc_classes(obj, baseclass):
"""Returns a list of sibling classes from a given FreeCAD object"""
# this function can become pure IFC
if baseclass in ("IfcProject", "IfcProjectLibrary"):
return ("IfcProject", "IfcProjectLibrary")
ifcfile = get_ifcfile(obj)
if not ifcfile:
return [baseclass]
classes = []
schema = ifcfile.wrapped_data.schema_name()
schema = ifcopenshell.ifcopenshell_wrapper.schema_by_name(schema)
declaration = schema.declaration_by_name(baseclass)
if "StandardCase" in baseclass:
declaration = declaration.supertype()
if declaration.supertype():
# include sibling classes
classes = [sub.name() for sub in declaration.supertype().subtypes()]
# include superclass too so one can "navigate up"
classes.append(declaration.supertype().name())
# also include subtypes of the current class (ex, StandardCases)
classes.extend([sub.name() for sub in declaration.subtypes()])
if baseclass not in classes:
classes.append(baseclass)
return classes
def get_ifc_element(obj, ifcfile=None):
"""Returns the corresponding IFC element of an object"""
if not ifcfile:
ifcfile = get_ifcfile(obj)
if ifcfile and hasattr(obj, "StepId"):
return ifcfile.by_id(obj.StepId)
return None
def has_representation(element):
"""Tells if an elements has an own representation"""
# This function can become pure IFC
if hasattr(element, "Representation") and element.Representation:
return True
return False
def filter_elements(elements, ifcfile, expand=True, spaces=False, assemblies=True):
"""Filter elements list of unwanted classes"""
# This function can become pure IFC
# gather decomposition if needed
if not isinstance(elements, (list, tuple)):
elements = [elements]
openings = False
if assemblies and any([e.is_a("IfcOpeningElement") for e in elements]):
openings = True
if expand and (len(elements) == 1):
elem = elements[0]
if elem.is_a("IfcSpace"):
spaces = True
if not has_representation(elem):
if elem.is_a("IfcProject"):
elements = ifcfile.by_type("IfcElement")
elements.extend(ifcfile.by_type("IfcSite"))
else:
elements = ifcopenshell.util.element.get_decomposition(elem)
else:
if elem.Representation.Representations:
rep = elem.Representation.Representations[0]
if (
rep.Items
and rep.Items[0].is_a() == "IfcPolyline"
and elem.IsDecomposedBy
):
# only use the decomposition and not the polyline
# happens for multilayered walls exported by VectorWorks
# the Polyline is the wall axis
# see https://github.com/yorikvanhavre/FreeCAD-NativeIFC/issues/28
elements = ifcopenshell.util.element.get_decomposition(elem)
if not openings:
# Never load feature elements by default, they can be lazy loaded
elements = [e for e in elements if not e.is_a("IfcFeatureElement")]
# do load spaces when required, otherwise skip computing their shapes
if not spaces:
elements = [e for e in elements if not e.is_a("IfcSpace")]
# skip projects
elements = [e for e in elements if not e.is_a("IfcProject")]
# skip furniture for now, they can be lazy loaded probably
elements = [e for e in elements if not e.is_a("IfcFurnishingElement")]
# skip annotations for now
elements = [e for e in elements if not e.is_a("IfcAnnotation")]
return elements
def set_attribute(ifcfile, element, attribute, value):
"""Sets the value of an attribute of an IFC element"""
# This function can become pure IFC
if not ifcfile or not element:
return False
if isinstance(value, FreeCAD.Units.Quantity):
f = get_scale(ifcfile)
value = value.Value * f
if attribute == "Class":
if value != element.is_a():
if value and value.startswith("Ifc"):
cmd = "root.reassign_class"
FreeCAD.Console.PrintLog(
"Changing IFC class value: "
+ element.is_a()
+ " to "
+ str(value)
+ "\n"
)
product = api_run(cmd, ifcfile, product=element, ifc_class=value)
# TODO fix attributes
return product
cmd = "attribute.edit_attributes"
attribs = {attribute: value}
if hasattr(element, attribute):
if (
attribute == "Name"
and getattr(element, attribute) is None
and value.startswith("_")
):
# do not consider default FreeCAD names given to unnamed alements
return False
if getattr(element, attribute) != value:
FreeCAD.Console.PrintLog(
"Changing IFC attribute value of "
+ str(attribute)
+ ": "
+ str(value)
+ "\n"
)
api_run(cmd, ifcfile, product=element, attributes=attribs)
return True
return False
def set_colors(obj, colors):
"""Sets the given colors to an object"""
if FreeCAD.GuiUp and colors:
# ifcopenshell issues (-1,-1,-1) colors if not set
if isinstance(colors[0], (tuple, list)):
colors = [tuple([abs(d) for d in c]) for c in colors]
else:
colors = [abs(c) for c in colors]
if hasattr(obj.ViewObject, "ShapeColor"):
if isinstance(colors[0], (tuple, list)):
obj.ViewObject.ShapeColor = colors[0][:3]
if len(colors[0]) > 3:
obj.ViewObject.Transparency = int(colors[0][3] * 100)
else:
obj.ViewObject.ShapeColor = colors[:3]
if len(colors) > 3:
obj.ViewObject.Transparency = int(colors[3] * 100)
if hasattr(obj.ViewObject, "DiffuseColor"):
# strip out transparency value because it currently gives ugly
# results in FreeCAD when combining transparent and non-transparent objects
if all([len(c) > 3 and c[3] != 0 for c in colors]):
obj.ViewObject.DiffuseColor = colors
else:
obj.ViewObject.DiffuseColor = [c[:3] for c in colors]
def get_body_context_ids(ifcfile):
# This function can become pure IFC
# Facetation is to accommodate broken Revit files
# See https://forums.buildingsmart.org/t/suggestions-on-how-to-improve-clarity\
# -of-representation-context-usage-in-documentation/3663/6?u=moult
body_contexts = [
c.id()
for c in ifcfile.by_type("IfcGeometricRepresentationSubContext")
if c.ContextIdentifier in ["Body", "Facetation"]
]
# Ideally, all representations should be in a subcontext, but some BIM apps don't do this
# correctly, so we add main contexts too
body_contexts.extend(
[
c.id()
for c in ifcfile.by_type(
"IfcGeometricRepresentationContext", include_subtypes=False
)
if c.ContextType == "Model"
]
)
return body_contexts
def get_plan_contexts_ids(ifcfile):
# This function can become pure IFC
# Annotation is to accommodate broken Revit files
# See https://github.com/Autodesk/revit-ifc/issues/187
return [
c.id()
for c in ifcfile.by_type("IfcGeometricRepresentationContext")
if c.ContextType in ["Plan", "Annotation"]
]
def get_freecad_matrix(ios_matrix):
"""Converts an IfcOpenShell matrix tuple into a FreeCAD matrix"""
# https://github.com/IfcOpenShell/IfcOpenShell/issues/1440
# https://pythoncvc.net/?cat=203
m_l = list()
for i in range(3):
line = list(ios_matrix[i::3])
line[-1] *= SCALE
m_l.extend(line)
return FreeCAD.Matrix(*m_l)
def get_ios_matrix(m):
"""Converts a FreeCAD placement or matrix into an IfcOpenShell matrix tuple"""
if isinstance(m, FreeCAD.Placement):
m = m.Matrix
mat = [
[m.A11, m.A12, m.A13, m.A14],
[m.A21, m.A22, m.A23, m.A24],
[m.A31, m.A32, m.A33, m.A34],
[m.A41, m.A42, m.A42, m.A44],
]
# apply rounding because OCCT often changes 1.0 to 0.99999999999 or something
rmat = []
for row in mat:
rmat.append([round(e, ROUND) for e in row])
return rmat
def get_scale(ifcfile):
"""Returns the scale factor to convert any file length to mm"""
scale = ifcopenshell.util.unit.calculate_unit_scale(ifcfile)
# the above lines yields meter -> file unit scale factor. We need mm
return 0.001 / scale
def set_placement(obj):
"""Updates the internal IFC placement according to the object placement"""
# This function can become pure IFC
ifcfile = get_ifcfile(obj)
if not ifcfile:
print("DEBUG: No ifc file for object", obj.Label, "Aborting")
if obj.Class in ["IfcProject", "IfcProjectLibrary"]:
return
element = get_ifc_element(obj)
placement = FreeCAD.Placement(obj.Placement)
placement.Base = FreeCAD.Vector(placement.Base).multiply(get_scale(ifcfile))
new_matrix = get_ios_matrix(placement)
old_matrix = ifcopenshell.util.placement.get_local_placement(
element.ObjectPlacement
)
# conversion from numpy array
old_matrix = old_matrix.tolist()
old_matrix = [[round(c, ROUND) for c in r] for r in old_matrix]
if new_matrix != old_matrix:
FreeCAD.Console.PrintLog(
"IFC: placement changed for "
+ obj.Label
+ " old: "
+ str(old_matrix)
+ " new: "
+ str(new_matrix)
+ "\n"
)
api = "geometry.edit_object_placement"
api_run(api, ifcfile, product=element, matrix=new_matrix, is_si=False)
return True
return False
def save_ifc(obj, filepath=None):
"""Saves the linked IFC file of a project, but does not mark it as saved"""
if not filepath:
if getattr(obj, "IfcFilePath", None):
filepath = obj.IfcFilePath
if filepath:
ifcfile = get_ifcfile(obj)
if not ifcfile:
ifcfile = create_ifcfile()
ifcfile.write(filepath)
FreeCAD.Console.PrintMessage("Saved " + filepath + "\n")
def save(obj, filepath=None):
"""Saves the linked IFC file of a project and set its saved status"""
save_ifc(obj, filepath)
obj.Modified = False
def aggregate(obj, parent):
"""Takes any FreeCAD object and aggregates it to an existing IFC object"""
proj = get_project(parent)
if not proj:
FreeCAD.Console.PrintError("The parent object is not part of an IFC project\n")
return
ifcfile = get_ifcfile(proj)
product = None
stepid = getattr(obj, "StepId", None)
if stepid:
# obj might be dragging at this point and has no project anymore
try:
elem = ifcfile[stepid]
if obj.GlobalId == elem.GlobalId:
product = elem
except:
pass
if product:
# this object already has an associated IFC product
print("DEBUG:", obj.Label, "is already part of the IFC document")
newobj = obj
new = False
else:
product = create_product(obj, parent, ifcfile)
shapemode = getattr(parent, "ShapeMode", DEFAULT_SHAPEMODE)
newobj = create_object(product, obj.Document, ifcfile, shapemode)
new = True
create_relationship(obj, newobj, parent, product, ifcfile)
base = getattr(obj, "Base", None)
if base:
# make sure the base is used only by this object before deleting
if base.InList != [obj]:
base = None
# handle layer
if FreeCAD.GuiUp:
import FreeCADGui
autogroup = getattr(
getattr(FreeCADGui, "draftToolBar", None), "autogroup", None
)
if autogroup is not None:
layer = FreeCAD.ActiveDocument.getObject(autogroup)
if hasattr(layer, "StepId"):
ifc_layers.add_to_layer(newobj, layer)
delete = not (PARAMS.GetBool("KeepAggregated", False))
if new and delete and base:
obj.Document.removeObject(base.Name)
label = obj.Label
if new and delete:
obj.Document.removeObject(obj.Name)
if new:
newobj.Label = label # to avoid 001-ing the Label...
return newobj
def deaggregate(obj, parent):
"""Removes a FreeCAD object form its parent"""
ifcfile = get_ifcfile(obj)
element = get_ifc_element(obj)
if not element:
return
api_run("aggregate.unassign_object", ifcfile, product=element)
parent.Proxy.removeObject(parent, obj)
def create_product(obj, parent, ifcfile, ifcclass=None):
"""Creates an IFC product out of a FreeCAD object"""