-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
ifc_tree.py
229 lines (206 loc) · 9.16 KB
/
ifc_tree.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
# ***************************************************************************
# * *
# * Copyright (c) 2023 Yorik van Havre <[email protected]> *
# * *
# * 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 NativeIFC module handles the retrieval and display of geometry compositions of objects"""
import os
import FreeCAD
TAB = 2
def get_geometry_tree(element, prefix=""):
"""Returns a list of elements representing an object's representation"""
result = [prefix + str(element)]
prefix += TAB * " "
if getattr(element, "Representation", None):
reps = element.Representation
result.append(prefix + str(reps))
prefix += prefix
for rep in reps.Representations:
result.extend(get_geometry_tree(rep, prefix))
elif getattr(element, "Items", None):
for it in element.Items:
result.extend(get_geometry_tree(it, prefix))
elif element.is_a("IfcPolyline"):
for p in element.Points:
result.append(prefix + str(p))
elif element.is_a("IfcExtrudedAreaSolid"):
result.append(prefix + str(element.ExtrudedDirection))
result.extend(get_geometry_tree(element.SweptArea, prefix))
elif element.is_a("IfcArbitraryClosedProfileDef"):
result.extend(get_geometry_tree(element.OuterCurve, prefix))
elif element.is_a("IfcArbitraryProfileDefWithVoids"):
result.extend(get_geometry_tree(element.OuterCurve, prefix))
for inn in element.InnerCurves:
result.extend(get_geometry_tree(inn, prefix))
elif element.is_a("IfcMappedItem"):
result.extend(get_geometry_tree(element.MappingSource[1], prefix))
elif element.is_a("IfcBooleanClippingResult"):
result.extend(get_geometry_tree(element.FirstOperand, prefix))
result.extend(get_geometry_tree(element.SecondOperand, prefix))
elif element.is_a("IfcBooleanResult"):
result.extend(get_geometry_tree(element.FirstOperand, prefix))
result.extend(get_geometry_tree(element.SecondOperand, prefix))
elif element.is_a("IfcHalfSpaceSolid"):
result.extend(get_geometry_tree(element.BaseSurface, prefix))
return result
def print_geometry_tree(element):
"""Same as get_geometry_tree but printed"""
for line in get_geometry_tree(element):
print(line)
def show_geometry_tree(element):
"""Same as get_geometry_tree but in a Qt dialog"""
import FreeCADGui # lazy import
from PySide2 import QtGui, QtWidgets
import ifc_tools
if isinstance(element, FreeCAD.DocumentObject):
element = ifc_tools.get_ifc_element(element)
if not element:
return
path = os.path.dirname(__file__)
dlg = FreeCADGui.PySideUic.loadUi(os.path.join(path, "ui", "dialogTree.ui"))
tops = {}
for line in get_geometry_tree(element):
psize = (len(line) - len(line.lstrip())) / TAB
wline = QtWidgets.QTreeWidgetItem([line.lstrip()])
if not psize:
dlg.geomtree.addTopLevelItem(wline)
else:
parent = tops[psize - 1]
parent.addChild(wline)
tops[psize] = wline
dlg.geomtree.expandAll()
dlg.setWindowTitle(dlg.windowTitle() + " " + element.Name)
dlg.geomtree.currentItemChanged.connect(show_properties)
dlg.proptree.itemChanged.connect(edit_property)
result = dlg.exec_()
if result:
props = [r.split("::") for r in dlg.proptree.toolTip().split(";;;;") if r]
if not props:
return
obj = ifc_tools.get_object(element)
ifcfile = ifc_tools.get_ifcfile(obj)
modified = False
for prop in props:
elt = ifcfile[int(prop[0])]
attrib = prop[1]
if attrib not in dir(elt):
print("DEBUG: Unknown attribute:", attrib)
continue
value = prop[3]
if isfloat(getattr(elt, attrib)):
try:
value = float(value)
except:
print("DEBUG: wrong value for", attrib, ":", value)
continue
ifc_tools.set_attribute(ifcfile, elt, attrib, value)
modified = True
if modified:
obj.touch()
proj = ifc_tools.get_project(obj)
proj.Modified = True
obj.Document.recompute()
def isfloat(s):
"""Tells if the given string is a number"""
try:
float(s)
except ValueError:
return False
print(s)
return True
def show_properties(current, previous):
"""Displays object properties"""
import ifc_tools # lazy loading
import FreeCADGui
from PySide2 import QtCore, QtGui, QtWidgets
ifcid = int(current.text(0).split("=", 1)[0].strip(" ").strip("#"))
sel = FreeCADGui.Selection.getSelection()
if len(sel) != 1:
return
obj = sel[0]
ifcfile = ifc_tools.get_ifcfile(obj)
if not ifcfile:
return
elt = ifcfile[ifcid]
box = current.treeWidget().parentWidget().widget(1)
box.setTitle("#" + str(ifcid) + ": " + elt.is_a())
props = [p for p in dir(elt) if p[0].isupper()]
# allow only numeric values?
# props = [p for p in props if isfloat(str(getattr(elt,p)))]
props = [p for p in props if not str(getattr(elt, p)).startswith("#")]
props = [p for p in props if not str(getattr(elt, p)).startswith("(")]
props = [
p for p in props if p not in ["Position", "LayerAssignments", "StyledByItem"]
]
proptree = box.children()[0].itemAt(0).widget()
proptree.clear()
proptree.setHorizontalHeaderLabels(["Property", "Value"])
proptree.setRowCount(len(props))
for i, prop in enumerate(props):
value = str(getattr(elt, prop))
if value == "None":
value = ""
r1 = QtWidgets.QTableWidgetItem(prop)
r1.setFlags(QtCore.Qt.ItemIsSelectable)
proptree.setItem(i, 0, r1)
r2 = QtWidgets.QTableWidgetItem(value)
r2.setToolTip(value)
if value.startswith("#") or value.startswith("(") or prop == "GlobalId":
r2.setFlags(QtCore.Qt.ItemIsSelectable)
proptree.setItem(i, 1, r2)
if "Position" in dir(elt):
i = proptree.rowCount()
proptree.setRowCount(i + 6)
position = FreeCAD.Vector(elt.Position.Location.Coordinates)
axis = FreeCAD.Vector(elt.Position.Axis.DirectionRatios)
xref = FreeCAD.Vector(elt.Position.RefDirection.DirectionRatios)
rotation = FreeCAD.Rotation(axis, xref, FreeCAD.Vector(), "ZXY").toEulerAngles(
"XYZ"
)
rotation = FreeCAD.Vector(rotation)
for c in ["x", "y", "z"]:
r1 = QtWidgets.QTableWidgetItem("Position " + c.upper())
r1.setFlags(QtCore.Qt.ItemIsSelectable)
proptree.setItem(i, 0, r1)
v = str(getattr(position, c))
r2 = QtWidgets.QTableWidgetItem(v)
r2.setToolTip(v)
proptree.setItem(i, 1, r2)
i += 1
for c in ["x", "y", "z"]:
r1 = QtWidgets.QTableWidgetItem("Rotation " + c.upper())
r1.setFlags(QtCore.Qt.ItemIsSelectable)
proptree.setItem(i, 0, r1)
v = str(getattr(rotation, c))
r2 = QtWidgets.QTableWidgetItem(v)
r2.setToolTip(v)
proptree.setItem(i, 1, r2)
i += 1
def edit_property(item):
"""Edits the value of a property"""
if item.toolTip() and (item.text() != item.toolTip()):
old = item.toolTip()
new = item.text()
table = item.tableWidget()
prop = table.item(item.row(), 0).text()
dlg = table.parent().parent().parent()
line = dlg.geomtree.currentItem().text(0)
ifcid = line.split("=", 1)[0].strip(" ").strip("#")
strid = ";;;;" + "::".join([str(i) for i in [ifcid, prop, old, new]])
dlg.proptree.setToolTip(dlg.proptree.toolTip() + strid)