/
importIFC.py
1386 lines (1205 loc) · 60.6 KB
/
importIFC.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) 2014 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 Lesser General Public License (LGPL) *
# * as published by the Free Software Foundation; either version 2 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 Library 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 *
# * *
# ***************************************************************************
"""Provide the importer for IFC files used above all in Arch and BIM.
Internally it uses IfcOpenShell, which must be installed before using.
"""
## @package importIFC
# \ingroup ARCH
# \brief IFC file format importer
#
# This module provides tools to import IFC files.
from __future__ import print_function
import six
import os
import math
import time
import FreeCAD
import Part
import Draft
import Arch
import DraftVecUtils
import ArchIFCSchema
import importIFCHelper
import importIFCmulticore
from draftutils.messages import _msg, _err
if FreeCAD.GuiUp:
import FreeCADGui as Gui
__title__ = "FreeCAD IFC importer - Enhanced IfcOpenShell-only version"
__author__ = ("Yorik van Havre", "Jonathan Wiedemann", "Bernd Hahnebach")
__url__ = "https://www.freecadweb.org"
DEBUG = False # Set to True to see debug messages. Otherwise, totally silent
ZOOMOUT = True # Set to False to not zoom extents after import
# Save the Python open function because it will be redefined
if open.__module__ in ['__builtin__', 'io']:
pyopen = open
# Templates and other definitions ****
# which IFC type must create which FreeCAD type
typesmap = {
"Site": [
"IfcSite"
],
"Building": [
"IfcBuilding"
],
"Floor": [
"IfcBuildingStorey"
],
"Structure": [
"IfcBeam",
"IfcBeamStandardCase",
"IfcColumn",
"IfcColumnStandardCase",
"IfcSlab",
"IfcFooting",
"IfcPile",
"IfcTendon"
],
"Wall": [
"IfcWall",
"IfcWallStandardCase",
"IfcCurtainWall"
],
"Window": [
"IfcWindow",
"IfcWindowStandardCase",
"IfcDoor",
"IfcDoorStandardCase"
],
"Roof": [
"IfcRoof"
],
"Stairs": [
"IfcStair",
"IfcStairFlight",
"IfcRamp",
"IfcRampFlight"
],
"Space": [
"IfcSpace"
],
"Rebar": [
"IfcReinforcingBar"
],
"Panel": [
"IfcPlate"
],
"Equipment": [
"IfcFurnishingElement",
"IfcSanitaryTerminal",
"IfcFlowTerminal",
"IfcElectricAppliance"
],
"Pipe": [
"IfcPipeSegment"
],
"PipeConnector": [
"IfcPipeFitting"
],
"BuildingPart": [
"IfcElementAssembly"
]
}
# which IFC entity (product) is a structural object
structuralifcobjects = (
"IfcStructuralCurveMember",
"IfcStructuralSurfaceMember",
"IfcStructuralPointConnection",
"IfcStructuralCurveConnection",
"IfcStructuralSurfaceConnection",
"IfcStructuralAction",
"IfcStructuralPointAction",
"IfcStructuralLinearAction",
"IfcStructuralLinearActionVarying",
"IfcStructuralPlanarAction"
)
def getPreferences():
"""Retrieve the IFC preferences available in import and export.
MERGE_MODE_ARCH:
0 = parametric arch objects
1 = non-parametric arch objects
2 = Part shapes
3 = One compound per storey
"""
p = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/Arch")
if FreeCAD.GuiUp and p.GetBool("ifcShowDialog", False):
Gui.showPreferences("Import-Export", 0)
preferences = {
'DEBUG': p.GetBool("ifcDebug", False),
'PREFIX_NUMBERS': p.GetBool("ifcPrefixNumbers", False),
'SKIP': p.GetString("ifcSkip", "").split(","),
'SEPARATE_OPENINGS': p.GetBool("ifcSeparateOpenings", False),
'ROOT_ELEMENT': p.GetString("ifcRootElement", "IfcProduct"),
'GET_EXTRUSIONS': p.GetBool("ifcGetExtrusions", False),
'MERGE_MATERIALS': p.GetBool("ifcMergeMaterials", False),
'MERGE_MODE_ARCH': p.GetInt("ifcImportModeArch", 0),
'MERGE_MODE_STRUCT': p.GetInt("ifcImportModeStruct", 1),
'CREATE_CLONES': p.GetBool("ifcCreateClones", True),
'IMPORT_PROPERTIES': p.GetBool("ifcImportProperties", False),
'SPLIT_LAYERS': p.GetBool("ifcSplitLayers", False), # wall layer, not layer for visual props
'FITVIEW_ONIMPORT': p.GetBool("ifcFitViewOnImport", False),
'ALLOW_INVALID': p.GetBool("ifcAllowInvalid", False),
'REPLACE_PROJECT': p.GetBool("ifcReplaceProject", False),
'MULTICORE': p.GetInt("ifcMulticore", 0),
'IMPORT_LAYER': p.GetBool("ifcImportLayer", True)
}
if preferences['MERGE_MODE_ARCH'] > 0:
preferences['SEPARATE_OPENINGS'] = False
preferences['GET_EXTRUSIONS'] = False
if not preferences['SEPARATE_OPENINGS']:
preferences['SKIP'].append("IfcOpeningElement")
return preferences
def export(exportList, filename, colors=None, preferences=None):
"""Export the selected objects to IFC format.
The export code is now in a separate module; this call is retained
in this module for compatibility purposes with older scripts.
"""
import exportIFC
exportIFC.export(exportList, filename, colors, preferences)
def open(filename, skip=[], only=[], root=None):
"""Open an IFC file inside a new document.
TODO: change the default argument to `None`, instead of `[]`.
This is better because lists are mutable.
Most of the work is done in the `insert` function.
"""
docname = os.path.splitext(os.path.basename(filename))[0]
docname = importIFCHelper.decode(docname, utf=True)
doc = FreeCAD.newDocument(docname)
doc.Label = docname
doc = insert(filename, doc.Name, skip, only, root)
return doc
def insert(srcfile, docname, skip=[], only=[], root=None, preferences=None):
"""Import the contents of an IFC file in the current active document.
TODO: change the default argument to `None`, instead of `[]`.
This is better because lists are mutable.
Parameters
----------
skip: list
By default empty list `[]`.
Can contain a list of ids of objects to be skipped.
only: list
By default, empty list `[]`.
Restrict the import to certain object ids; it will also
get their children.
root: object
It is used to import only the derivates of a certain element type,
for example, `'ifcProduct'
"""
try:
import ifcopenshell
from ifcopenshell import geom
# Sometimes there is an error importing `geom` in this way
# import ifcopenshell.geom
#
# therefore we must use the `from x import y` way.
#
# For some reason this works; see the bug report
# https://github.com/IfcOpenShell/IfcOpenShell/issues/689
except ModuleNotFoundError:
_err("IfcOpenShell was not found on this system. "
"IFC support is disabled.\n"
"Visit https://wiki.freecadweb.org/IfcOpenShell "
"to learn about installing it.")
return
starttime = time.time() # in seconds
if preferences is None:
preferences = getPreferences()
if preferences["MULTICORE"] and not hasattr(srcfile, "by_guid"):
return importIFCmulticore.insert(srcfile, docname, preferences)
try:
doc = FreeCAD.getDocument(docname)
except NameError:
doc = FreeCAD.newDocument(docname)
FreeCAD.ActiveDocument = doc
global parametrics
# allow to override the root element
if root:
preferences['ROOT_ELEMENT'] = root
# keeping global variable for debugging purposes
# global ifcfile
# If the `by_guid` attribute exists, this is already a loaded ifcfile,
# otherwise, it's just a string, and we have to open it with ifcopenshell
if hasattr(srcfile, "by_guid"):
ifcfile = srcfile
filesize = None
filename = None
else:
if preferences['DEBUG']:
_msg("Opening '{}'... ".format(srcfile), end="")
filename = importIFCHelper.decode(srcfile, utf=True)
filesize = os.path.getsize(filename) * 1E-6 # in megabytes
ifcfile = ifcopenshell.open(filename)
if preferences['DEBUG']:
_msg("done.")
# Get file scale
ifcscale = importIFCHelper.getScaling(ifcfile)
# IfcOpenShell multiplies the precision value of the file by 100
# So we raise the precision by 100 too to compensate.
# ctxs = ifcfile.by_type("IfcGeometricRepresentationContext")
# for ctx in ctxs:
# if not ctx.is_a("IfcGeometricRepresentationSubContext"):
# ctx.Precision = ctx.Precision/100
# Set default ifcopenshell options to work in brep mode
settings = geom.settings()
settings.set(settings.USE_BREP_DATA, True)
settings.set(settings.SEW_SHELLS, True)
settings.set(settings.USE_WORLD_COORDS, True)
if preferences['SEPARATE_OPENINGS']:
settings.set(settings.DISABLE_OPENING_SUBTRACTIONS, True)
if preferences['SPLIT_LAYERS'] and hasattr(settings, "APPLY_LAYERSETS"):
settings.set(settings.APPLY_LAYERSETS, True)
# build all needed tables
if preferences['DEBUG']:
_msg("Building types and relationships table...")
# type tables
sites = ifcfile.by_type("IfcSite")
buildings = ifcfile.by_type("IfcBuilding")
floors = ifcfile.by_type("IfcBuildingStorey")
openings = ifcfile.by_type("IfcOpeningElement")
materials = ifcfile.by_type("IfcMaterial")
(products,
annotations) = importIFCHelper.buildRelProductsAnnotations(ifcfile,
preferences['ROOT_ELEMENT'])
# empty relation tables
objects = {} # { id:object, ... }
shapes = {} # { id:shaoe } only used for merge mode
structshapes = {} # { id:shaoe } only used for merge mode
sharedobjects = {} # { representationmapid:object }
# a list of imported objects whose parametric relationships
# need processing after all objects have been created
parametrics = []
profiles = {} # to store reused extrusion profiles {ifcid:fcobj, ...}
layers = {} # { layer_name, [ids] }
# filled relation tables
# TODO: investigate using inverse attributes.
# For the following tables it might be better to use inverse attributes
# to find the properties, otherwise a lot of loops
# and if testing is needed.
# See https://forum.freecadweb.org/viewtopic.php?f=39&t=37892
prodrepr = importIFCHelper.buildRelProductRepresentation(ifcfile)
additions = importIFCHelper.buildRelAdditions(ifcfile)
groups = importIFCHelper.buildRelGroups(ifcfile)
subtractions = importIFCHelper.buildRelSubtractions(ifcfile)
mattable = importIFCHelper.buildRelMattable(ifcfile)
colors = importIFCHelper.buildRelProductColors(ifcfile, prodrepr)
colordict = {} # { objname:color tuple } for non-GUI use
if preferences['DEBUG']:
_msg("done.")
# only import a list of IDs and their children, if defined
if only:
ids = []
while only:
currentid = only.pop()
ids.append(currentid)
if currentid in additions.keys():
only.extend(additions[currentid])
products = [ifcfile[currentid] for currentid in ids]
# start the actual import, set FreeCAD UI
count = 0
from FreeCAD import Base
progressbar = Base.ProgressIndicator()
progressbar.start("Importing IFC objects...", len(products))
if preferences['DEBUG']:
_msg("Parsing {} BIM objects...".format(len(products)))
# Prepare the 3D view if applicable
if preferences['FITVIEW_ONIMPORT'] and FreeCAD.GuiUp:
overallboundbox = None
Gui.ActiveDocument.activeView().viewAxonometric()
# Create the base project object
if not preferences['REPLACE_PROJECT']:
if len(ifcfile.by_type("IfcProject")) > 0:
projectImporter = importIFCHelper.ProjectImporter(ifcfile, objects)
projectImporter.execute()
else:
# https://forum.freecadweb.org/viewtopic.php?f=39&t=40624
print("No IfcProject found in the ifc file. Nothing imported")
return doc
# handle IFC products
for product in products:
count += 1
pid = product.id()
guid = product.GlobalId
ptype = product.is_a()
if preferences['DEBUG']: print(count,"/",len(products),"object #"+str(pid),":",ptype,end="")
# build list of related property sets
psets = importIFCHelper.getIfcPropertySets(ifcfile, pid)
# add layer names to layers
if hasattr(product, "Representation") and hasattr(product.Representation, "Representations"):
if len(product.Representation.Representations) > 0:
lays = product.Representation.Representations[0].LayerAssignments
if len(lays) > 0:
layer_name = lays[0].Name
if layer_name not in list(layers.keys()):
layers[layer_name] = [pid]
else:
layers[layer_name].append(pid)
if preferences['DEBUG']: print(" layer ", layer_name, " found", ptype,end="")
else:
if preferences['DEBUG']: print(" no layer found", ptype,end="")
# checking for full FreeCAD parametric definition, overriding everything else
if psets and FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/Arch").GetBool("IfcImportFreeCADProperties",False):
if "FreeCADPropertySet" in [ifcfile[pset].Name for pset in psets.keys()]:
if preferences['DEBUG']: print(" restoring from parametric definition...",end="")
obj,parametrics = importIFCHelper.createFromProperties(psets,ifcfile,parametrics)
if obj:
objects[pid] = obj
if preferences['DEBUG']: print("done")
continue
else:
if preferences['DEBUG']: print("failed")
# no parametric data, we go the good old way
name = str(ptype[3:])
if product.Name:
name = product.Name
if six.PY2:
name = name.encode("utf8")
if preferences['PREFIX_NUMBERS']:
name = "ID" + str(pid) + " " + name
obj = None
baseobj = None
brep = None
shape = None
# classify object and verify if we must skip it
archobj = True # assume all objects not in structuralifcobjects are architecture
structobj = False
if ptype in structuralifcobjects:
archobj = False
structobj = True
if preferences['DEBUG']: print(" (struct)",end="")
else:
if preferences['DEBUG']: print(" (arch)",end="")
if preferences['MERGE_MODE_ARCH'] == 4 and archobj:
if preferences['DEBUG']: print(" skipped.")
continue
if preferences['MERGE_MODE_STRUCT'] == 3 and not archobj:
if preferences['DEBUG']: print(" skipped.")
continue
if pid in skip: # user given id skip list
if preferences['DEBUG']: print(" skipped.")
continue
if ptype in skip: # user given type skip list
if preferences['DEBUG']: print(" skipped.")
continue
if ptype in preferences['SKIP']: # preferences-set type skip list
if preferences['DEBUG']: print(" skipped.")
continue
if preferences['REPLACE_PROJECT']: # options-enabled project/site/building skip
if ptype in ['IfcProject','IfcSite']:
if preferences['DEBUG']: print(" skipped.")
continue
elif ptype in ['IfcBuilding']:
if len(ifcfile.by_type("IfcBuilding")) == 1:
# let multiple buildings through...
if preferences['DEBUG']: print(" skipped.")
continue
elif ptype in ['IfcBuildingStorey']:
if len(ifcfile.by_type("IfcBuildingStorey")) == 1:
# let multiple storeys through...
if preferences['DEBUG']: print(" skipped.")
continue
# check if this object is sharing its shape (mapped representation)
clone = None
store = None
prepr = None
try:
prepr = product.Representation
except Exception:
if preferences['DEBUG']: print(" ERROR unable to get object representation",end="")
if prepr and (preferences['MERGE_MODE_ARCH'] == 0) and archobj and preferences['CREATE_CLONES']:
for r in prepr.Representations:
if r.RepresentationIdentifier.upper() == "BODY":
if r.Items[0].is_a("IfcMappedItem"):
originalid = r.Items[0].MappingSource.id()
if originalid in sharedobjects:
clone = sharedobjects[originalid]
else:
sharedobjects[originalid] = None
store = originalid # flag this object to be stored later
# set additional setting for structural entities
if hasattr(settings,"INCLUDE_CURVES"):
if structobj:
settings.set(settings.INCLUDE_CURVES,True)
else:
settings.set(settings.INCLUDE_CURVES,False)
try:
cr = geom.create_shape(settings, product)
brep = cr.geometry.brep_data
except Exception:
pass # IfcOpenShell will yield an error if a given product has no shape, but we don't care, we're brave enough
# from now on we have a brep string
if brep:
if preferences['DEBUG']: print(" "+str(int(len(brep)/1000))+"k ",end="")
# create a Part shape
shape = Part.Shape()
shape.importBrepFromString(brep,False)
shape.scale(1000.0) # IfcOpenShell always outputs in meters, we convert to mm, the freecad internal unit
if shape.isNull() and (not preferences['ALLOW_INVALID']):
if preferences['DEBUG']: print("null shape ",end="")
elif not shape.isValid() and (not preferences['ALLOW_INVALID']):
if preferences['DEBUG']: print("invalid shape ",end="")
else:
# add to the global boundbox if applicable
if preferences['FITVIEW_ONIMPORT'] and FreeCAD.GuiUp:
try:
bb = shape.BoundBox
# if preferences['DEBUG']: print(' ' + str(bb),end="")
except Exception:
bb = None
if preferences['DEBUG']: print(' BB could not be computed',end="")
if bb and bb.isValid():
if not overallboundbox:
overallboundbox = bb
if not overallboundbox.isInside(bb):
Gui.SendMsgToActiveView("ViewFit")
overallboundbox.add(bb)
if (preferences['MERGE_MODE_ARCH'] > 0 and archobj) or structobj:
# we are not using Arch objects
# additional tweaks to set when not using Arch objects
if ptype == "IfcSpace": # do not add spaces to compounds
if preferences['DEBUG']: print("skipping space ",pid,end="")
elif structobj:
structshapes[pid] = shape
if preferences['DEBUG']: print(len(shape.Solids),"solids ",end="")
baseobj = shape
else:
shapes[pid] = shape
if preferences['DEBUG']: print(len(shape.Solids),"solids ",end="")
baseobj = shape
else:
# create base shape object
if clone:
if preferences['DEBUG']: print("clone ",end="")
else:
if preferences['GET_EXTRUSIONS'] and (preferences['MERGE_MODE_ARCH'] != 1):
# get IFC profile
profileid = None
sortmethod = None
if product.Representation:
if product.Representation.Representations:
if product.Representation.Representations[0].is_a("IfcShapeRepresentation"):
if product.Representation.Representations[0].Items:
if product.Representation.Representations[0].Items[0].is_a("IfcExtrudedAreaSolid"):
profileid = product.Representation.Representations[0].Items[0].SweptArea.id()
sortmethod = importIFCHelper.getProfileCenterPoint(product.Representation.Representations[0].Items[0])
# recompose extrusions from a shape
if not sortmethod:
if ptype in ["IfcWall","IfcWallStandardCase","IfcSpace"]:
sortmethod = "z"
else:
sortmethod = "area"
ex = Arch.getExtrusionData(shape,sortmethod) # is this an extrusion?
if ex:
# check for extrusion profile
baseface = None
addplacement = None
if profileid and (profileid in profiles):
# reuse existing profile if existing
print("shared extrusion ",end="")
baseface = profiles[profileid]
# calculate delta placement between stored profile and this one
addplacement = FreeCAD.Placement()
r = FreeCAD.Rotation(baseface.Shape.Faces[0].normalAt(0,0),ex[0].Faces[0].normalAt(0,0))
if r.Angle > 0.000001:
# use shape methods to easily obtain a correct placement
ts = Part.Shape()
ts.rotate(DraftVecUtils.tup(baseface.Shape.CenterOfMass), DraftVecUtils.tup(r.Axis), math.degrees(r.Angle))
addplacement = ts.Placement
d = ex[0].CenterOfMass.sub(baseface.Shape.CenterOfMass)
if d.Length > 0.000001:
addplacement.move(d)
if not baseface:
# this is an extrusion but we haven't built the profile yet
print("extrusion ",end="")
import DraftGeomUtils
if DraftGeomUtils.hasCurves(ex[0]) or len(ex[0].Wires) != 1:
# is this a circle?
if (len(ex[0].Edges) == 1) and isinstance(ex[0].Edges[0].Curve,Part.Circle):
baseface = Draft.makeCircle(ex[0].Edges[0])
else:
# curves or holes? We just make a Part face
baseface = FreeCAD.ActiveDocument.addObject("Part::Feature",name+"_footprint")
# bug/feature in ifcopenshell? Some faces of a shell may have non-null placement
# workaround to remove the bad placement: exporting/reimporting as step
if not ex[0].Placement.isNull():
import tempfile
fd, tf = tempfile.mkstemp(suffix=".stp")
ex[0].exportStep(tf)
f = Part.read(tf)
os.close(fd)
os.remove(tf)
else:
f = ex[0]
baseface.Shape = f
else:
# no curve and no hole, we can make a draft object
verts = [v.Point for v in ex[0].Wires[0].OrderedVertexes]
# TODO verts are different if shape is made of RectangleProfileDef or not
# is this a rectangle?
if importIFCHelper.isRectangle(verts):
baseface = Draft.makeRectangle(verts,face=True)
else:
# no hole and no curves, we make a Draft Wire instead
baseface = Draft.makeWire(verts,closed=True)
if profileid:
# store for possible shared use
profiles[profileid] = baseface
baseobj = FreeCAD.ActiveDocument.addObject("Part::Extrusion",name+"_body")
baseobj.Base = baseface
if addplacement:
# apply delta placement (stored profile)
baseobj.Placement = addplacement
baseobj.Dir = addplacement.Rotation.inverted().multVec(ex[1])
else:
baseobj.Dir = ex[1]
if FreeCAD.GuiUp:
baseface.ViewObject.hide()
if (not baseobj):
baseobj = FreeCAD.ActiveDocument.addObject("Part::Feature",name+"_body")
baseobj.Shape = shape
else:
# this object has no shape (storeys, etc...)
if preferences['DEBUG']: print(" no brep ",end="")
# we now have the shape, we create the final object
if preferences['MERGE_MODE_ARCH'] == 0 and archobj:
# full Arch objects
for freecadtype,ifctypes in typesmap.items():
if ptype not in ifctypes:
continue
if clone:
obj = getattr(Arch,"make"+freecadtype)(name=name)
obj.CloneOf = clone
# calculate the correct distance from the cloned object
if shape:
if shape.Solids:
s1 = shape.Solids[0]
else:
s1 = shape
if clone.Shape.Solids:
s2 = clone.Shape.Solids[0]
else:
s1 = clone.Shape
if hasattr(s1,"CenterOfMass") and hasattr(s2,"CenterOfMass"):
v = s1.CenterOfMass.sub(s2.CenterOfMass)
if product.Representation:
r = importIFCHelper.getRotation(product.Representation.Representations[0].Items[0].MappingTarget)
if not r.isNull():
v = v.add(s2.CenterOfMass)
v = v.add(r.multVec(s2.CenterOfMass.negative()))
obj.Placement.Rotation = r
obj.Placement.move(v)
else:
print("failed to compute placement ",)
else:
# no clone
obj = getattr(Arch,"make"+freecadtype)(baseobj=baseobj,name=name)
if freecadtype in ["Wall","Structure"] and baseobj and baseobj.isDerivedFrom("Part::Extrusion"):
# remove intermediary extrusion for types that can extrude themselves
# TODO do this check earlier so we do not calculate it, to save time
obj.Base = baseobj.Base
obj.Placement = obj.Placement.multiply(baseobj.Placement)
obj.Height = baseobj.Dir.Length
obj.Normal = FreeCAD.Vector(baseobj.Dir).normalize()
bn = baseobj.Name
FreeCAD.ActiveDocument.removeObject(bn)
if (freecadtype in ["Structure","Wall"]) and not baseobj:
# remove sizes to prevent auto shape creation for types that don't require a base object
obj.Height = 0
obj.Width = 0
obj.Length = 0
if store:
sharedobjects[store] = obj
# set the placement from the storey's elevation property
if ptype == "IfcBuildingStorey":
if product.Elevation:
obj.Placement.Base.z = product.Elevation * ifcscale
break
if not obj:
# we couldn't make an object of a specific type, use default arch component
obj = Arch.makeComponent(baseobj,name=name)
# set additional properties
obj.Label = name
if preferences['DEBUG']: print(": "+obj.Label+" ",end="")
if hasattr(obj,"Description") and hasattr(product,"Description"):
if product.Description:
obj.Description = product.Description
if FreeCAD.GuiUp and baseobj:
try:
if hasattr(baseobj,"ViewObject"):
baseobj.ViewObject.hide()
except ReferenceError:
pass
# setting IFC type
try:
if hasattr(obj,"IfcType"):
obj.IfcType = ''.join(map(lambda x: x if x.islower() else " "+x, ptype[3:]))[1:]
except Exception:
print("Unable to give IFC type ",ptype," to object ",obj.Label)
# setting uid
if hasattr(obj,"IfcData"):
a = obj.IfcData
a["IfcUID"] = str(guid)
obj.IfcData = a
# setting IFC attributes
for attribute in ArchIFCSchema.IfcProducts[product.is_a()]["attributes"]:
if attribute["name"] == "Name":
continue
# print("attribute:",attribute["name"])
if hasattr(product, attribute["name"]) and getattr(product, attribute["name"]) and hasattr(obj,attribute["name"]):
# print("Setting attribute",attribute["name"],"to",getattr(product, attribute["name"]))
setattr(obj, attribute["name"], getattr(product, attribute["name"]))
# TODO: ArchIFCSchema.IfcProducts uses the IFC version from the FreeCAD prefs.
# This might not coincide with the file being opened, hence some attributes are not properly read.
if obj:
# print number of solids
if preferences['DEBUG']:
s = ""
if hasattr(obj,"Shape"):
if obj.Shape.Solids:
s = str(len(obj.Shape.Solids))+" solids"
print(s,end="")
objects[pid] = obj
elif (preferences['MERGE_MODE_ARCH'] == 1 and archobj) or (preferences['MERGE_MODE_STRUCT'] == 0 and not archobj):
# non-parametric Arch objects (just Arch components with a shape)
if ptype in ["IfcSite","IfcBuilding","IfcBuildingStorey"]:
for freecadtype,ifctypes in typesmap.items():
if ptype in ifctypes:
obj = getattr(Arch,"make"+freecadtype)(baseobj=baseobj,name=name)
if preferences['DEBUG']: print(": "+obj.Label+" ",end="")
if ptype == "IfcBuildingStorey":
if product.Elevation:
obj.Placement.Base.z = product.Elevation * ifcscale
elif baseobj:
obj = Arch.makeComponent(baseobj,name=name,delete=True)
obj.Label = name
if preferences['DEBUG']: print(": "+obj.Label+" ",end="")
if hasattr(obj,"Description") and hasattr(product,"Description"):
if product.Description:
obj.Description = product.Description
try:
if hasattr(obj,"IfcType"):
obj.IfcType = ''.join(map(lambda x: x if x.islower() else " "+x, ptype[3:]))[1:]
except Exception:
print("Unable to give IFC type ",ptype," to object ",obj.Label)
if hasattr(obj,"IfcData"):
a = obj.IfcData
a["IfcUID"] = str(guid)
obj.IfcData = a
elif pid in additions:
# no baseobj but in additions, thus we make a BuildingPart container
obj = getattr(Arch,"makeBuildingPart")(name=name)
if preferences['DEBUG']: print(": "+obj.Label+" ",end="")
else:
if preferences['DEBUG']: print(": skipped.")
continue
if obj and hasattr(obj, "GlobalId"):
obj.GlobalId = guid
elif (preferences['MERGE_MODE_ARCH'] == 2 and archobj) or (preferences['MERGE_MODE_STRUCT'] == 1 and not archobj):
# Part shapes
if ptype in ["IfcSite","IfcBuilding","IfcBuildingStorey"]:
for freecadtype,ifctypes in typesmap.items():
if ptype in ifctypes:
obj = getattr(Arch,"make"+freecadtype)(baseobj=baseobj,name=name)
if preferences['DEBUG']: print(": "+obj.Label+" ",end="")
if ptype == "IfcBuildingStorey":
if product.Elevation:
obj.Placement.Base.z = product.Elevation * ifcscale
elif baseobj:
obj = FreeCAD.ActiveDocument.addObject("Part::Feature",name)
obj.Shape = shape
elif pid in additions:
# no baseobj but in additions, thus we make a BuildingPart container
obj = getattr(Arch,"makeBuildingPart")(name=name)
if preferences['DEBUG']: print(": "+obj.Label+" ",end="")
else:
if preferences['DEBUG']: print(": skipped.")
continue
if preferences['DEBUG']: print("") # newline for debug prints, print for a new object should be on a new line
if obj:
obj.Label = name
objects[pid] = obj
# handle properties
if psets:
if preferences['IMPORT_PROPERTIES'] and hasattr(obj,"IfcProperties"):
# treat as spreadsheet (pref option)
if isinstance(obj.IfcProperties,dict):
# fix property type if needed
obj.removeProperty("IfcProperties")
obj.addProperty("App::PropertyLink","IfcProperties","Component","Stores IFC properties as a spreadsheet")
ifc_spreadsheet = Arch.makeIfcSpreadsheet()
n = 2
for c in psets.keys():
o = ifcfile[c]
if preferences['DEBUG']: print("propertyset Name",o.Name,type(o.Name))
catname = o.Name
for p in psets[c]:
l = ifcfile[p]
lname = l.Name
if l.is_a("IfcPropertySingleValue"):
if preferences['DEBUG']:
print("property name",l.Name,type(l.Name))
if six.PY2:
catname = catname.encode("utf8")
lname = lname.encode("utf8")
ifc_spreadsheet.set(str('A'+str(n)), catname)
ifc_spreadsheet.set(str('B'+str(n)), lname)
if l.NominalValue:
if preferences['DEBUG']:
print("property NominalValue",l.NominalValue.is_a(),type(l.NominalValue.is_a()))
print("property NominalValue.wrappedValue",l.NominalValue.wrappedValue,type(l.NominalValue.wrappedValue))
# print("l.NominalValue.Unit",l.NominalValue.Unit,type(l.NominalValue.Unit))
ifc_spreadsheet.set(str('C'+str(n)), l.NominalValue.is_a())
if l.NominalValue.is_a() in ['IfcLabel','IfcText','IfcIdentifier','IfcDescriptiveMeasure']:
if six.PY2:
ifc_spreadsheet.set(str('D'+str(n)), "'" + str(l.NominalValue.wrappedValue.encode("utf8")))
else:
ifc_spreadsheet.set(str('D'+str(n)), "'" + str(l.NominalValue.wrappedValue))
else:
ifc_spreadsheet.set(str('D'+str(n)), str(l.NominalValue.wrappedValue))
if hasattr(l.NominalValue,'Unit'):
ifc_spreadsheet.set(str('E'+str(n)), str(l.NominalValue.Unit))
n += 1
obj.IfcProperties = ifc_spreadsheet
elif hasattr(obj,"IfcProperties") and isinstance(obj.IfcProperties,dict):
# 0.18 behaviour: properties are saved as pset;;type;;value in IfcProperties
d = obj.IfcProperties
obj.IfcProperties = importIFCHelper.getIfcProperties(ifcfile, pid, psets, d)
elif hasattr(obj,"IfcData"):
# 0.17: properties are saved as type(value) in IfcData
a = obj.IfcData
for c in psets.keys():
for p in psets[c]:
l = ifcfile[p]
if l.is_a("IfcPropertySingleValue"):
a[l.Name.encode("utf8")] = str(l.NominalValue) # no py3 support here
obj.IfcData = a
# color
if (pid in colors) and colors[pid]:
colordict[obj.Name] = colors[pid]
if FreeCAD.GuiUp:
# if preferences['DEBUG']: print(" setting color: ",int(colors[pid][0]*255),"/",int(colors[pid][1]*255),"/",int(colors[pid][2]*255))
if hasattr(obj.ViewObject,"ShapeColor"):
obj.ViewObject.ShapeColor = tuple(colors[pid][0:3])
if hasattr(obj.ViewObject,"Transparency"):
obj.ViewObject.Transparency = colors[pid][3]
# if preferences['DEBUG'] is on, recompute after each shape
if preferences['DEBUG']: FreeCAD.ActiveDocument.recompute()
# attached 2D elements
if product.Representation:
for r in product.Representation.Representations:
if r.RepresentationIdentifier == "FootPrint":
annotations.append(product)
break
# additional properties for specific types
if product.is_a("IfcSite"):
if product.RefElevation:
obj.Elevation = product.RefElevation * ifcscale
if product.RefLatitude:
obj.Latitude = importIFCHelper.dms2dd(*product.RefLatitude)
if product.RefLongitude:
obj.Longitude = importIFCHelper.dms2dd(*product.RefLongitude)
if product.SiteAddress:
if product.SiteAddress.AddressLines:
obj.Address = product.SiteAddress.AddressLines[0]
if product.SiteAddress.Town:
obj.City = product.SiteAddress.Town
if product.SiteAddress.Region:
obj.Region = product.SiteAddress.Region
if product.SiteAddress.Country:
obj.Country = product.SiteAddress.Country
if product.SiteAddress.PostalCode:
obj.PostalCode = product.SiteAddress.PostalCode
project = product.Decomposes[0].RelatingObject
modelRC = next((rc for rc in project.RepresentationContexts if rc.ContextType == "Model"), None)
if modelRC and modelRC.TrueNorth:
# If the y-part of TrueNorth is 0, then the x-part should be checked.
# Declination would be -90° if x >0 and +90° if x < 0
# Only if x==0 then we can not determine TrueNorth.
# But that would actually be an invalid IFC file, because the magnitude
# of the (twodimensional) direction vector for TrueNorth shall be greater than zero.
(x, y) = modelRC.TrueNorth.DirectionRatios[:2]
obj.Declination = ((math.degrees(math.atan2(y,x))-90+180)%360)-180
if (FreeCAD.GuiUp):
obj.ViewObject.CompassRotation.Value = obj.Declination
try:
progressbar.next(True)
except(RuntimeError):
print("Aborted.")
progressbar.stop()
FreeCAD.ActiveDocument.recompute()
return
progressbar.stop()
FreeCAD.ActiveDocument.recompute()
if preferences['MERGE_MODE_STRUCT'] == 2:
if preferences['DEBUG']: print("Joining Structural shapes...",end="")
for host,children in groups.items(): # Structural
if ifcfile[host].is_a("IfcStructuralAnalysisModel"):
compound = []
for c in children:
if c in structshapes.keys():
compound.append(structshapes[c])
del structshapes[c]
if compound:
name = ifcfile[host].Name or "AnalysisModel"
if preferences['PREFIX_NUMBERS']: name = "ID" + str(host) + " " + name
obj = FreeCAD.ActiveDocument.addObject("Part::Feature",name)
obj.Label = name
obj.Shape = Part.makeCompound(compound)
if structshapes: # remaining Structural shapes
obj = FreeCAD.ActiveDocument.addObject("Part::Feature","UnclaimedStruct")
obj.Shape = Part.makeCompound(structshapes.values())