-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
BimIfcQuantities.py
374 lines (341 loc) · 15.6 KB
/
BimIfcQuantities.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
# -*- coding: utf-8 -*-
# ***************************************************************************
# * *
# * Copyright (c) 2018 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 *
# * *
# ***************************************************************************
"""This module contains FreeCAD commands for the BIM workbench"""
import os
import FreeCAD
from BimTranslateUtils import *
qprops = [
"Length",
"Width",
"Height",
"Area",
"HorizontalArea",
"VerticalArea",
"Volume",
] # quantities columns
trqprops = [
translate("BIM", "Length"),
translate("BIM", "Width"),
translate("BIM", "Height"),
translate("BIM", "Area"),
translate("BIM", "Horizontal Area"),
translate("BIM", "Vertical Area"),
translate("BIM", "Volume"),
]
class BIM_IfcQuantities:
def GetResources(self):
return {
"Pixmap": os.path.join(
os.path.dirname(__file__), "icons", "BIM_IfcQuantities.svg"
),
"MenuText": QT_TRANSLATE_NOOP(
"BIM_IfcQuantities", "Manage IFC quantities..."
),
"ToolTip": QT_TRANSLATE_NOOP(
"BIM_IfcQuantities",
"Manage how the quantities of different elements of of your BIM project will be exported to IFC",
),
}
def IsActive(self):
if FreeCAD.ActiveDocument:
# disable for pre-v0.18
if float(FreeCAD.Version()[0] + "." + FreeCAD.Version()[1]) < 0.18:
return False
return True
else:
return False
def Activated(self):
import FreeCADGui
from PySide import QtCore, QtGui
# build objects list
self.objectslist = {}
for obj in FreeCAD.ActiveDocument.Objects:
role = self.getRole(obj)
if role:
self.objectslist[obj.Name] = role
# support for arrays
array = self.getArray(obj)
for i in range(array):
if i > 0: # the first item already went above
self.objectslist[obj.Name+"+array"+str(i)] = role
try:
import ArchIFC
self.ifcroles = ArchIFC.IfcTypes
except (ImportError, AttributeError):
import ArchComponent
self.ifcroles = ArchComponent.IfcRoles
# load the form and set the tree model up
self.form = FreeCADGui.PySideUic.loadUi(
os.path.join(os.path.dirname(__file__), "dialogIfcQuantities.ui")
)
self.form.setWindowIcon(
QtGui.QIcon(
os.path.join(
os.path.dirname(__file__), "icons", "BIM_IfcQuantities.svg"
)
)
)
# quantities tab
self.qmodel = QtGui.QStandardItemModel()
self.form.quantities.setModel(self.qmodel)
self.form.quantities.setUniformRowHeights(True)
self.form.quantities.setItemDelegate(QtGui.QStyledItemDelegate())
self.quantitiesDrawn = False
QtCore.QObject.connect(
self.qmodel,
QtCore.SIGNAL("dataChanged(QModelIndex,QModelIndex)"),
self.setChecked,
)
QtCore.QObject.connect(
self.form.buttonBox, QtCore.SIGNAL("accepted()"), self.accept
)
QtCore.QObject.connect(
self.form.quantities,
QtCore.SIGNAL("clicked(QModelIndex)"),
self.onClickTree,
)
QtCore.QObject.connect(
self.form.onlyVisible, QtCore.SIGNAL("stateChanged(int)"), self.update
)
# center the dialog over FreeCAD window
mw = FreeCADGui.getMainWindow()
self.form.move(
mw.frameGeometry().topLeft()
+ mw.rect().center()
- self.form.rect().center()
)
self.update()
self.form.show()
def getArray(self, obj):
"returns a count number if this object needs to be duplicated"
import Draft
if len(obj.InList) == 1:
parent = obj.InList[0]
if Draft.getType(parent) == "Array":
return parent.Count
return 0
def decamelize(self, s):
return "".join([" " + c if c.isupper() else c for c in s]).strip(" ")
def update(self, index=None):
"updates the tree widgets in all tabs"
import FreeCADGui
from PySide import QtCore, QtGui
import Draft
# quantities tab - only fill once
if not self.quantitiesDrawn:
self.qmodel.setHorizontalHeaderLabels(
[translate("BIM", "Label")] + trqprops
)
quantheaders = self.form.quantities.header() # QHeaderView instance
if hasattr(quantheaders, "setClickable"): # qt4
quantheaders.setClickable(True)
else: # qt5
quantheaders.setSectionsClickable(True)
quantheaders.sectionClicked.connect(self.quantHeaderClicked)
# sort by type
groups = {}
for name, role in self.objectslist.items():
groups.setdefault(role, []).append(name)
for names in groups.values():
suffix = ""
for name in names:
if "+array" in name:
name = name.split("+array")[0]
suffix = " (duplicate)"
obj = FreeCAD.ActiveDocument.getObject(name)
if obj:
if (
not self.form.onlyVisible.isChecked()
) or obj.ViewObject.isVisible():
if obj.isDerivedFrom("Part::Feature") and not (
Draft.getType(obj) == "Site"
):
it1 = QtGui.QStandardItem(obj.Label + suffix)
it1.setToolTip(name + suffix)
it1.setEditable(False)
if QtCore.QFileInfo(
":/icons/Arch_" + obj.Proxy.Type + "_Tree.svg"
).exists():
icon = QtGui.QIcon(
":/icons/Arch_" + obj.Proxy.Type + "_Tree.svg"
)
else:
icon = QtGui.QIcon(":/icons/Arch_Component.svg")
it1.setIcon(icon)
props = []
for prop in qprops:
it = QtGui.QStandardItem()
val = None
if prop == "Volume":
if obj.Shape and hasattr(obj.Shape, "Volume"):
val = FreeCAD.Units.Quantity(
obj.Shape.Volume, FreeCAD.Units.Volume
)
it.setText(
val.getUserPreferred()[0].replace(
"^3", "³"
)
)
it.setCheckable(True)
else:
if hasattr(obj, prop) and (
not "Hidden" in obj.getEditorMode(prop)
):
val = getattr(obj, prop)
it.setText(
val.getUserPreferred()[0].replace(
"^2", "²"
)
)
it.setCheckable(True)
if val != None:
d = None
if hasattr(obj, "IfcAttributes"):
d = obj.IfcAttributes
elif hasattr(obj, "IfcData"):
d = obj.IfcData
if d:
if ("Export" + prop in d) and (
d["Export" + prop] == "True"
):
it.setCheckState(QtCore.Qt.Checked)
if val == 0:
it.setIcon(
QtGui.QIcon(
os.path.join(
os.path.dirname(__file__),
"icons",
"warning.svg",
)
)
)
if prop in [
"Area",
"HorizontalArea",
"VerticalArea",
"Volume",
]:
it.setEditable(False)
props.append(it)
self.qmodel.appendRow([it1] + props)
self.quantitiesDrawn = True
def getRole(self, obj):
if hasattr(obj, "IfcType"):
return obj.IfcType
elif hasattr(obj, "IfcRole"):
return obj.IfcRole
else:
return None
def accept(self):
self.form.hide()
changed = False
for row in range(self.qmodel.rowCount()):
name = self.qmodel.item(row, 0).toolTip()
obj = FreeCAD.ActiveDocument.getObject(name)
if obj:
for i in range(len(qprops)):
item = self.qmodel.item(row, i + 1)
val = item.text()
sav = bool(item.checkState())
if i < 3: # Length, Width, Height, value can be changed
if hasattr(obj, qprops[i]):
if getattr(obj, qprops[i]).getUserPreferred()[0] != val:
setattr(obj, qprops[i], val)
if not changed:
FreeCAD.ActiveDocument.openTransaction(
"Change quantities"
)
changed = True
d = None
if hasattr(obj, "IfcAttributes"):
d = obj.IfcAttributes
att = "IfcAttributes"
elif hasattr(obj, "IfcData"):
d = obj.IfcData
att = "IfcData"
if d:
if sav:
if (not "Export" + qprops[i] in d) or (
d["Export" + qprops[i]] == "False"
):
d["Export" + qprops[i]] = "True"
setattr(obj, att, d)
if not changed:
FreeCAD.ActiveDocument.openTransaction(
"Change quantities"
)
changed = True
else:
if "Export" + qprops[i] in d:
if d["Export" + qprops[i]] == "True":
d["Export" + qprops[i]] = "False"
setattr(obj, att, d)
if not changed:
FreeCAD.ActiveDocument.openTransaction(
"Change quantities"
)
changed = True
else:
FreeCAD.Console.PrintError(
translate(
"BIM", "Cannot save quantities settings for object %1"
).replace("%1", obj.Label)
+ "\n"
)
if changed:
FreeCAD.ActiveDocument.commitTransaction()
FreeCAD.ActiveDocument.recompute()
def setChecked(self, id1, id2):
sel = self.form.quantities.selectedIndexes()
state = self.qmodel.itemFromIndex(id1).checkState()
if len(sel) > 7:
for idx in sel:
if idx.column() == id1.column():
item = self.qmodel.itemFromIndex(idx)
if item.checkState() != state:
item.setCheckState(state)
def quantHeaderClicked(self, col):
from PySide import QtCore
sel = self.form.quantities.selectedIndexes()
state = None
if len(sel) > 7:
for idx in sel:
if idx.column() == col:
item = self.qmodel.itemFromIndex(idx)
if state is None:
# take the state to apply from the first selected item
state = QtCore.Qt.Checked
if item.checkState():
state = QtCore.Qt.Unchecked
item.setCheckState(state)
def onClickTree(self, index=None):
import FreeCADGui
FreeCADGui.Selection.clearSelection()
sel = self.form.quantities.selectedIndexes()
for index in sel:
if index.column() == 0:
obj = FreeCAD.ActiveDocument.getObject(
self.qmodel.itemFromIndex(index).toolTip()
)
if obj:
FreeCADGui.Selection.addSelection(obj)