-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
BimIfcExplorer.py
640 lines (568 loc) · 25.6 KB
/
BimIfcExplorer.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
# ***************************************************************************
# * *
# * Copyright (c) 2019 Yorik van Havre <[email protected]> *
# * *
# * 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 *
# * *
# ***************************************************************************
from __future__ import print_function
import os
import FreeCAD
from BimTranslateUtils import *
class BIM_IfcExplorer:
def __init__(self):
self.tree = None
def GetResources(self):
import Arch_rc
return {
"Pixmap": os.path.join(os.path.dirname(__file__), "icons", "IFC.svg"),
"MenuText": QT_TRANSLATE_NOOP("BIM_IfcExplorer", "IFC explorer"),
"ToolTip": QT_TRANSLATE_NOOP("BIM_IfcExplorer", "IFC explorer utility"),
}
def Activated(self):
import FreeCADGui
from PySide import QtCore, QtGui
try:
import ifcopenshell
except ImportError:
FreeCAD.Console.PrintError(
translate(
"BIM",
"IfcOpenShell was not found on this system. IFC support is disabled",
)
+ "\n"
)
return
# setting up a font
self.bold = QtGui.QFont()
self.bold.setWeight(75)
self.bold.setBold(True)
# setting up a link fint
self.linkfont = QtGui.QFont()
self.linkfont.setUnderline(True)
# setup a brush to paint text in system link color
self.linkbrush = QtGui.QApplication.palette().link()
# draw the main tree widget
self.tree = QtGui.QTreeWidget()
self.tree.setColumnCount(1)
self.tree.setWordWrap(True)
self.tree.header().setDefaultSectionSize(60)
self.tree.header().resizeSection(0, 180)
self.tree.header().setStretchLastSection(True)
self.tree.headerItem().setText(0, translate("BIM", "Objects structure"))
# draw the attributes widget
self.attributes = QtGui.QTreeWidget()
self.attributes.setColumnCount(2)
self.attributes.setWordWrap(True)
self.attributes.header().setDefaultSectionSize(60)
self.attributes.header().resizeSection(0, 120)
self.attributes.header().resizeSection(1, 200)
self.attributes.header().setStretchLastSection(True)
self.attributes.headerItem().setText(0, translate("BIM", "Attribute"))
self.attributes.headerItem().setText(1, translate("BIM", "Value"))
# draw the properties widget
self.properties = QtGui.QTreeWidget()
self.properties.setColumnCount(2)
self.properties.setWordWrap(True)
self.properties.header().setDefaultSectionSize(60)
self.properties.header().resizeSection(0, 120)
self.properties.header().resizeSection(1, 200)
self.properties.header().setStretchLastSection(True)
self.properties.headerItem().setText(0, translate("BIM", "Property"))
self.properties.headerItem().setText(1, translate("BIM", "Value"))
# create the dialog
self.dialog = QtGui.QDialog()
self.dialog.setObjectName("IfcExplorer")
self.dialog.setWindowTitle(translate("BIM", "Ifc Explorer"))
self.dialog.resize(720, 540)
toolbar = QtGui.QToolBar()
layout = QtGui.QVBoxLayout(self.dialog)
layout.addWidget(toolbar)
hlayout = QtGui.QHBoxLayout(self.dialog)
hlayout.addWidget(self.tree)
layout.addLayout(hlayout)
vlayout = QtGui.QVBoxLayout(self.dialog)
hlayout.addLayout(vlayout)
vlayout.addWidget(self.attributes)
vlayout.addWidget(self.properties)
# draw the toolbar buttons
self.openAction = QtGui.QAction(translate("BIM", "Open"), None)
self.openAction.setToolTip(translate("BIM", "Open another IFC file..."))
self.openAction.triggered.connect(self.open)
self.openAction.setIcon(QtGui.QIcon(":/icons/document-open.svg"))
toolbar.addAction(self.openAction)
self.backAction = QtGui.QAction(translate("BIM", "Back"), None)
self.backAction.setToolTip(translate("BIM", "Go back to last item selected"))
self.backAction.triggered.connect(self.back)
self.backAction.setIcon(QtGui.QIcon(":/icons/edit-undo.svg"))
toolbar.addAction(self.backAction)
self.shapeAction = QtGui.QAction(translate("BIM", "Insert"), None)
self.shapeAction.setToolTip(
translate(
"BIM",
"Inserts the selected object and its children in the active document",
)
)
self.shapeAction.triggered.connect(self.insert)
self.shapeAction.setIcon(QtGui.QIcon(":icons/Tree_Part.svg"))
self.shapeAction.setEnabled(False)
toolbar.addAction(self.shapeAction)
self.meshAction = QtGui.QAction(translate("BIM", "Mesh"), None)
self.meshAction.setToolTip(translate("BIM", "Turn mesh display on/off"))
self.meshAction.triggered.connect(self.toggleMesh)
self.meshAction.setCheckable(True)
self.meshAction.setChecked(False)
self.meshAction.setIcon(QtGui.QIcon(":/icons/DrawStyleShaded.svg"))
toolbar.addAction(self.meshAction)
# connect signals/slots
self.tree.currentItemChanged.connect(self.onSelectTree)
self.attributes.itemDoubleClicked.connect(self.onDoubleClickTree)
self.properties.itemDoubleClicked.connect(self.onDoubleClickTree)
self.dialog.rejected.connect(self.close)
# center the dialog over FreeCAD window
mw = FreeCADGui.getMainWindow()
self.dialog.move(
mw.frameGeometry().topLeft()
+ mw.rect().center()
- self.dialog.rect().center()
)
# open a file and show the dialog
self.open()
self.dialog.show()
def open(self):
"opens a file"
import ifcopenshell
from PySide import QtCore, QtGui
self.filename = None
lastfolder = FreeCAD.ParamGet(
"User parameter:BaseApp/Preferences/Mod/BIM"
).GetString("lastIfcExplorerFolder", "")
filename = QtGui.QFileDialog.getOpenFileName(
None,
translate("BIM", "Select an IFC file"),
lastfolder,
translate("BIM", "IFC files (*.ifc)"),
)
if filename:
self.filename = filename[0]
FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/BIM").SetString(
"lastIfcExplorerFolder", os.path.dirname(self.filename)
)
if not os.path.exists(self.filename):
FreeCAD.Console.PrintError(translate("BIM", "File not found") + "\n")
return
# set window title
self.dialog.setWindowTitle(
translate("BIM", "IFC Explorer") + " - " + os.path.basename(self.filename)
)
# clear everything
self.tree.clear()
self.attributes.clear()
self.properties.clear()
self.done = []
self.backnav = []
self.mesh = None
self.products = []
self.omeshes = {}
self.currentmesh = None
# read file and order contents
self.ifc = ifcopenshell.open(self.filename)
root = self.getEntitiesTree()
# populate tree contents
for eid, children in root.items():
self.addEntity(eid, children, self.tree)
# self.tree.expandAll()
def close(self):
"close the dialog"
if FreeCAD.ActiveDocument:
if self.mesh:
FreeCAD.ActiveDocument.removeObject(self.mesh.Name)
if self.currentmesh:
FreeCAD.ActiveDocument.removeObject(self.currentmesh.Name)
def back(self):
"selects the previously selected item in the tree"
if self.backnav:
item = self.backnav.pop()
self.tree.setCurrentItem(item)
def insert(self):
"inserts selected objects in the active document"
import importIFC
from PySide import QtCore, QtGui
doc = FreeCAD.ActiveDocument
if doc and self.filename:
item = self.tree.currentItem()
if item:
eid = item.data(0, QtCore.Qt.UserRole)
if eid:
importIFC.ZOOMOUT = False
try:
importIFC.insert(self.ifc, doc.Name, only=[eid])
except TypeError:
importIFC.insert(self.filename, doc.Name, only=[eid])
if self.currentmesh:
self.currentmesh.ViewObject.hide()
def toggleMesh(self, checked=False):
"turns mesh display on/off"
import FreeCADGui
import Mesh
import ifcopenshell
from ifcopenshell import geom
if not FreeCAD.ActiveDocument:
doc = FreeCAD.newDocument()
FreeCAD.setActiveDocument(doc.Name)
if FreeCAD.ActiveDocument:
if checked:
if self.mesh:
self.mesh.ViewObject.show()
else:
try:
import importIFCHelper
s = importIFCHelper.getScaling(self.ifc)
except:
import importIFC
s = importIFC.getScaling(self.ifc)
s *= 1000 # ifcopenshell outputs its meshes in metres
trf = None
if s != 1:
trf = FreeCAD.Matrix()
trf.scale(s, s, s)
basemesh = Mesh.Mesh()
s = geom.settings()
s.set(s.USE_WORLD_COORDS, True)
for product in self.products:
try:
m = geom.create_shape(s, product)
g = m.geometry
v = g.verts
f = g.faces
verts = [
FreeCAD.Vector(v[i : i + 3])
for i in range(0, len(v), 3)
]
faces = [tuple(f[i : i + 3]) for i in range(0, len(f), 3)]
omesh = Mesh.Mesh((verts, faces))
if trf:
omesh.transform(trf)
self.omeshes[product.id()] = omesh
basemesh.addMesh(omesh)
except:
pass
self.mesh = FreeCAD.ActiveDocument.addObject(
"Mesh::Feature", "IFCMesh"
)
self.mesh.Mesh = basemesh
self.mesh.ViewObject.Transparency = 85
FreeCAD.ActiveDocument.recompute()
FreeCADGui.Selection.clearSelection()
FreeCADGui.Selection.addSelection(self.mesh)
FreeCADGui.SendMsgToActiveView("ViewSelection")
else:
if self.mesh:
self.mesh.ViewObject.hide()
if self.currentmesh:
self.currentmesh.ViewObject.hide()
def getEntitiesTree(self):
"stacks the entities inside containers"
root = {}
for site in self.ifc.by_type("IfcSite"):
root[site.id()] = self.getChildren(site)
return root
# entities = ifc.by_type("IfcRoot")
# entities += ifc.by_type("IfcRepresentation")
# entities += ifc.by_type("IfcRepresentationItem")
# entities += ifc.by_type("IfcRepresentationMap")
# entities += ifc.by_type("IfcPlacement")
# entities += ifc.by_type("IfcProperty")
# entities += ifc.by_type("IfcPhysicalSimpleQuantity")
# entities += ifc.by_type("IfcMaterial")
# entities += ifc.by_type("IfcProductRepresentation")
# entities = sorted(entities, key=lambda eid: eid.id())
def getChildren(self, obj, keys=False):
"returns a recursive dict of the children of this obj"
children = {}
if obj.is_a("IfcProduct"):
self.products.append(obj)
if hasattr(obj, "IsDecomposedBy"): # building structure
for rel in obj.IsDecomposedBy:
if hasattr(rel, "RelatedObjects"):
for child in rel.RelatedObjects:
children[child.id()] = self.getChildren(child)
if hasattr(obj, "ContainsElements"): # objects inside building structure
for rel in obj.ContainsElements:
if hasattr(rel, "RelatedElements"):
for child in rel.RelatedElements:
children[child.id()] = self.getChildren(child)
if hasattr(obj, "Representation"): # Shape representation
if obj.Representation:
children[obj.Representation.id()] = self.getChildren(obj.Representation)
if hasattr(obj, "Representations"):
for rep in obj.Representations:
children[rep.id()] = self.getChildren(rep)
if obj.is_a("IfcShapeRepresentation"):
for it in obj.Items:
children[it.id()] = self.getChildren(it)
if keys:
def getkeys(d):
ck = list(d.keys())
for v in d.values():
ck.extend(getkeys(v))
return ck
return getkeys(children)
return children
def addEntity(self, eid, children, parent):
"adds a given entity and its children to the given tree item"
from PySide import QtCore, QtGui
def get_name(e):
try:
return " : " + e.get_info()["Name"]
except:
return ""
if not eid in self.done:
entity = self.ifc[eid]
item = QtGui.QTreeWidgetItem(parent)
# item.setText(0,self.tostr(eid))
name = ""
if entity.is_a("IfcProduct"):
name = get_name(entity)
item.setFont(0, self.bold)
if isinstance(parent, QtGui.QTreeWidgetItem):
parent.setExpanded(True)
item.setText(
0, "#" + self.tostr(eid) + " : " + self.tostr(entity.is_a()) + name
)
if entity.is_a() in ["IfcWall", "IfcWallStandardCase"]:
item.setIcon(0, QtGui.QIcon(":icons/Arch_Wall_Tree.svg"))
elif entity.is_a() in ["IfcBuildingElementProxy"]:
item.setIcon(0, QtGui.QIcon(":icons/Arch_Component.svg"))
elif entity.is_a() in [
"IfcColumn",
"IfcColumnStandardCase",
"IfcBeam",
"IfcBeamStandardCase",
"IfcSlab",
"IfcFooting",
"IfcPile",
"IfcTendon",
]:
item.setIcon(0, QtGui.QIcon(":icons/Arch_Structure_Tree.svg"))
elif entity.is_a() in ["IfcSite"]:
item.setIcon(0, QtGui.QIcon(":icons/Arch_Site_Tree.svg"))
elif entity.is_a() in ["IfcBuilding"]:
item.setIcon(0, QtGui.QIcon(":icons/Arch_Building_Tree.svg"))
elif entity.is_a() in ["IfcBuildingStorey"]:
item.setIcon(0, QtGui.QIcon(":icons/Arch_Floor_Tree.svg"))
elif entity.is_a() in [
"IfcWindow",
"IfcWindowStandardCase",
"IfcDoor",
"IfcDoorStandardCase",
]:
item.setIcon(0, QtGui.QIcon(":icons/Arch_Window_Tree.svg"))
elif entity.is_a() in ["IfcRoof"]:
item.setIcon(0, QtGui.QIcon(":icons/Arch_Roof_Tree.svg"))
elif entity.is_a() in ["IfcExtrudedAreaSolid", "IfcClosedShell"]:
item.setIcon(0, QtGui.QIcon(":icons/Tree_Part.svg"))
elif entity.is_a() in ["IfcFace"]:
item.setIcon(0, QtGui.QIcon(":icons/Draft_SwitchMode.svg"))
elif entity.is_a() in ["IfcArbitraryClosedProfileDef", "IfcPolyloop"]:
item.setIcon(0, QtGui.QIcon(":icons/Draft_Draft.svg"))
elif entity.is_a() in [
"IfcPropertySingleValue",
"IfcQuantityArea",
"IfcQuantityVolume",
]:
item.setIcon(0, QtGui.QIcon(":icons/Tree_Annotation.svg"))
elif entity.is_a() in ["IfcMaterial"]:
item.setIcon(0, QtGui.QIcon(":icons/Arch_Material.svg"))
elif entity.is_a() in ["IfcReinforcingBar"]:
item.setIcon(0, QtGui.QIcon(":icons/Arch_Rebar.svg"))
elif entity.is_a("IfcProduct"):
item.setIcon(0, QtGui.QIcon(":icons/Arch_Component.svg"))
self.tree.setFirstItemColumnSpanned(item, True)
item.setData(0, QtCore.Qt.UserRole, eid)
for childid, grandchildren in children.items():
self.addEntity(childid, grandchildren, item)
self.done.append(eid)
def addAttributes(self, eid, parent):
"adds the attributes of the given IFC entity under the given QTreeWidgetITem"
import ifcopenshell
from PySide import QtCore, QtGui
entity = self.ifc[eid]
i = 0
while True:
try:
argname = entity.attribute_name(i)
except RuntimeError:
break
else:
try:
argvalue = getattr(entity, argname)
except AttributeError:
FreeCAD.Console.PrintError(
translate("BIM", "Error in entity")
+ " "
+ self.tostr(entity)
+ "\n"
)
break
else:
if argname not in ["Id", "GlobalId"]:
colored = False
if isinstance(argvalue, ifcopenshell.entity_instance):
if argvalue.id() == 0:
t = self.tostr(argvalue)
else:
colored = True
t = (
"#"
+ self.tostr(argvalue.id())
+ ": "
+ self.tostr(argvalue.is_a())
)
elif isinstance(argvalue, (list, tuple)):
t = ""
else:
t = self.tostr(argvalue)
item = QtGui.QTreeWidgetItem(parent)
item.setText(0, self.tostr(argname))
if t and (t != "None"):
item.setText(1, t)
if colored:
item.setForeground(1, self.linkbrush)
item.setFont(1, self.linkfont)
if argname == "Name":
item.setFont(1, self.bold)
if isinstance(argvalue, (list, tuple)):
j = 0
for argitem in argvalue:
colored = False
if isinstance(argitem, ifcopenshell.entity_instance):
if argitem.id() == 0:
t = self.tostr(argitem)
else:
colored = True
t = (
"#"
+ self.tostr(argitem.id())
+ ": "
+ self.tostr(argitem.is_a())
)
else:
t = argitem
t = self.tostr(t)
if j == 0:
item.setText(1, t)
if colored:
item.setForeground(1, self.linkbrush)
item.setFont(1, self.linkfont)
else:
subitem = QtGui.QTreeWidgetItem(item)
subitem.setText(1, t)
if colored:
subitem.setForeground(1, self.linkbrush)
subitem.setFont(1, self.linkfont)
j += 1
i += 1
def addProperties(self, eid, parent):
"adds properties of a given entity to the given QTReeWidgetItem"
from PySide import QtCore, QtGui
entity = self.ifc[eid]
if hasattr(entity, "IsDefinedBy"):
for rel in entity.IsDefinedBy:
if hasattr(rel, "RelatingPropertyDefinition"):
if rel.RelatingPropertyDefinition:
item = QtGui.QTreeWidgetItem(parent)
item.setText(
0,
"PropertySet: "
+ self.tostr(rel.RelatingPropertyDefinition.Name),
)
item.setFont(0, self.bold)
self.properties.setFirstItemColumnSpanned(item, True)
if hasattr(rel.RelatingPropertyDefinition, "HasProperties"):
for prop in rel.RelatingPropertyDefinition.HasProperties:
subitem = QtGui.QTreeWidgetItem(item)
subitem.setText(0, "Property")
self.addAttributes(prop.id(), subitem)
def tostr(self, text):
"resolves py2/py3 string representation hassles"
import six
if six.PY2:
if isinstance(text, unicode):
return text.encode("utf8")
else:
return str(text)
else:
if isinstance(text, str):
return text
else:
return str(text)
def onSelectTree(self, item, previous):
"displays attributes and properties of a tree item"
from PySide import QtCore, QtGui
self.backnav.append(previous)
eid = item.data(0, QtCore.Qt.UserRole)
self.attributes.clear()
self.addAttributes(eid, self.attributes)
self.attributes.expandAll()
self.properties.clear()
self.addProperties(eid, self.properties)
self.properties.expandAll()
entity = self.ifc[eid]
if entity.is_a("IfcProduct") and FreeCAD.ActiveDocument:
self.shapeAction.setEnabled(True)
else:
self.shapeAction.setEnabled(False)
if eid in self.omeshes:
omesh = self.omeshes[eid]
else:
omesh = None
children = self.getChildren(entity, keys=True)
for k in children:
if k in self.omeshes:
kmesh = self.omeshes[k]
if omesh:
omesh.addMesh(kmesh)
else:
omesh = kmesh
if omesh:
if not self.currentmesh:
self.currentmesh = FreeCAD.ActiveDocument.addObject(
"Mesh::Feature", "IFCObjectMesh"
)
self.currentmesh.ViewObject.ShapeColor = (0.4, 0.4, 1.0)
self.currentmesh.Mesh = omesh
self.currentmesh.ViewObject.show()
FreeCAD.ActiveDocument.recompute()
else:
if self.currentmesh:
self.currentmesh.ViewObject.hide()
def onDoubleClickTree(self, item, column):
"when a property or attribute is double-clicked"
from PySide import QtCore, QtGui
if self.tree:
txt = item.text(column)
if txt.startswith("#"):
eid = txt[1:].split(":")[0]
target = self.tree.findItems(
"#" + eid + " :",
QtCore.Qt.MatchStartsWith | QtCore.Qt.MatchRecursive,
0,
)
if target:
self.tree.scrollToItem(target[0])
self.tree.setCurrentItem(target[0])