-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
ifc_geometry.py
289 lines (264 loc) · 14.1 KB
/
ifc_geometry.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
# ***************************************************************************
# * *
# * 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 module contains geometry editing and geometry properties-related tools"""
import FreeCAD
import ifcopenshell
from ifcopenshell.util import unit
import ifc_tools
def add_geom_properties(obj):
"""Adds geometry properties to a FreeCAD object"""
element = ifc_tools.get_ifc_element(obj)
if not ifc_tools.has_representation(element):
return
ifcfile = ifc_tools.get_ifcfile(obj)
scaling = ifcopenshell.util.unit.calculate_unit_scale(ifcfile)
scaling = scaling * 1000 # given scale is for m, we work in mm
for rep in element.Representation.Representations:
if rep.RepresentationIdentifier == "Body":
if len(rep.Items) == 1:
# Extrusions
if rep.Items[0].is_a("IfcExtrudedAreaSolid"):
ext = rep.Items[0]
if "ExtrusionDepth" not in obj.PropertiesList:
obj.addProperty(
"App::PropertyLength", "ExtrusionDepth", "Geometry"
)
obj.ExtrusionDepth = ext.Depth * scaling
if "ExtrusionDirection" not in obj.PropertiesList:
obj.addProperty(
"App::PropertyVector", "ExtrusionDirection", "Geometry"
)
obj.ExtrusionDirection = FreeCAD.Vector(
ext.ExtrudedDirection.DirectionRatios
)
# Extrusion of a rectangle
if ext.SweptArea.is_a("IfcRectangleProfileDef"):
if "RectangleLength" not in obj.PropertiesList:
obj.addProperty(
"App::PropertyLength", "RectangleLength", "Geometry"
)
obj.RectangleLength = ext.SweptArea.XDim * scaling
if "RectangleWidth" not in obj.PropertiesList:
obj.addProperty(
"App::PropertyLength", "RectangleWidth", "Geometry"
)
obj.RectangleWidth = ext.SweptArea.YDim * scaling
# Extrusion of a polyline
elif ext.SweptArea.is_a("IfcArbitraryClosedProfileDef"):
if ext.SweptArea.OuterCurve.is_a("IfcPolyline"):
if "PolylinePoints" not in obj.PropertiesList:
obj.addProperty(
"App::PropertyVectorList",
"PolylinePoints",
"Geometry",
)
points = [
p.Coordinates for p in ext.SweptArea.OuterCurve.Points
]
points = [p + (0,) for p in points if len(p) < 3]
points = [
FreeCAD.Vector(p).multiply(scaling) for p in points
]
obj.PolylinePoints = points
# I profile
elif ext.SweptArea.is_a("IfcIShapeProfileDef"):
for p in [
"FilletRadius",
"FlangeEdgeRadius",
"FlangeSlope",
"FlangeThickness",
"OverallDepth",
"OverallWidth",
"WebThickness",
]:
if hasattr(ext.SweptArea, p):
obj.addProperty("App::PropertyLength", p, "Geometry")
value = getattr(ext.SweptArea, p)
if not value:
value = 0
value = value * scaling
setattr(obj, p, value)
obj.addProperty(
"App::PropertyString", "ProfileName", "Geometry"
)
obj.ProfileName = ext.SweptArea.ProfileName
# below is disabled for now... Don't know if it's useful to expose to the user
elif False: # rep.RepresentationIdentifier == "Axis":
# Wall axis consisting on a single line
if len(rep.Items) == 1:
if rep.Items[0].is_a("IfcCompositeCurve"):
if len(rep.Items[0].Segments) == 1:
if rep.Items[0].Segments[0].is_a("IfcCompositeCurveSegment"):
if rep.Items[0].Segments[0].ParentCurve.is_a("IfcPolyline"):
pol = rep.Items[0].Segments[0].ParentCurve
if len(pol.Points) == 2:
if "AxisStart" not in obj.PropertiesList:
obj.addProperty(
"App::PropertyPosition",
"AxisStart",
"Geometry",
)
obj.AxisStart = FreeCAD.Vector(
pol.Points[0].Coordinates
).multiply(scaling)
if "AxisEnd" not in obj.PropertiesList:
obj.addProperty(
"App::PropertyPosition",
"AxisEnd",
"Geometry",
)
obj.AxisEnd = FreeCAD.Vector(
pol.Points[1].Coordinates
).multiply(scaling)
def set_attribute(ifcfile, element, prop, value):
"""Sets an attribute. Returns True if the attribute was changed"""
if value != getattr(element, prop):
ifc_tools.api_run(
"attribute.edit_attributes",
ifcfile,
product=element,
attributes={prop: value},
)
return True
return False
def set_geom_property(obj, prop):
"""Updates the internal IFC file with the given value"""
# TODO verify if values are different than the stored value before changing!
element = ifc_tools.get_ifc_element(obj)
if not ifc_tools.has_representation(element):
return False
ifcfile = ifc_tools.get_ifcfile(obj)
scaling = ifcopenshell.util.unit.calculate_unit_scale(ifcfile)
scaling = 0.001 / scaling
changed = False
if prop == "ExtrusionDepth":
for rep in element.Representation.Representations:
if rep.RepresentationIdentifier == "Body":
if len(rep.Items) == 1:
if rep.Items[0].is_a("IfcExtrudedAreaSolid"):
elem = rep.Items[0]
value = getattr(obj, prop).Value * scaling
changed = set_attribute(ifcfile, elem, "Depth", value)
elif prop == "ExtrusionDirection":
for rep in element.Representation.Representations:
if rep.RepresentationIdentifier == "Body":
if len(rep.Items) == 1:
if rep.Items[0].is_a("IfcExtrudedAreaSolid"):
elem = rep.Items[0].ExtrudedDirection
value = tuple(getattr(obj, prop))
changed = set_attribute(ifcfile, elem, "DirectionRatios", value)
elif prop == "RectangleLength":
for rep in element.Representation.Representations:
if rep.RepresentationIdentifier == "Body":
if len(rep.Items) == 1:
if rep.Items[0].is_a("IfcExtrudedAreaSolid"):
elem = rep.Items[0].SweptArea
if elem.is_a("IfcRectangleProfileDef"):
value = getattr(obj, prop).Value * scaling
changed = set_attribute(ifcfile, elem, "XDim", value)
elif prop == "RectangleWidth":
for rep in element.Representation.Representations:
if rep.RepresentationIdentifier == "Body":
if len(rep.Items) == 1:
if rep.Items[0].is_a("IfcExtrudedAreaSolid"):
elem = rep.Items[0].SweptArea
if elem.is_a("IfcRectangleProfileDef"):
value = getattr(obj, prop).Value * scaling
changed = set_attribute(ifcfile, elem, "YDim", value)
elif prop == "PolylinePoints":
# TODO check values against existing
for rep in element.Representation.Representations:
if rep.RepresentationIdentifier == "Body":
if len(rep.Items) == 1:
if rep.Items[0].is_a("IfcExtrudedAreaSolid"):
if rep.Items[0].SweptArea.is_a("IfcArbitraryClosedProfileDef"):
if rep.Items[0].SweptArea.OuterCurve.is_a("IfcPolyline"):
elem = rep.Items[0].SweptArea.OuterCurve
elem_points = elem.Points
psize = elem_points[0].Dim
points = getattr(obj, prop)
if len(points) > len(elem_points):
for i in range(len(points) - len(elem_points)):
p = ifc_tools.api_run(
"root.create_entity",
ifcfile,
ifc_class="IfcCartesianPoint",
)
elem_points.append(p)
elem.Points = elem_points
elif len(points) < len(elem_points):
rest = []
for i in range(len(elem_points) - len(points)):
rest.append(elem_points.pop())
elem.Points = elem_points
for r in rest:
ifc_tools.api_run(
"root.remove_product", ifcfile, product=r
)
if len(points) == len(elem_points):
for i in range(len(points)):
v = FreeCAD.Vector(points[i]).multiply(scaling)
coord = tuple(v)[:psize]
ifc_tools.api_run(
"attribute.edit_attributes",
ifcfile,
product=elem_points[i],
attributes={"Coordinates": coord},
)
changed = True
elif prop in [
"FilletRadius",
"FlangeEdgeRadius",
"FlangeSlope",
"FlangeThickness",
"OverallDepth",
"OverallWidth",
"WebThickness",
]:
for rep in element.Representation.Representations:
if rep.RepresentationIdentifier == "Body":
if len(rep.Items) == 1:
if rep.Items[0].SweptArea.is_a("IfcIShapeProfileDef"):
elem = rep.Items[0].SweptArea
value = getattr(obj, prop).Value * scaling
if value == 0 and getattr(elem, prop) is None:
value = None
changed = set_attribute(ifcfile, elem, prop, value)
elif prop in ["ProfileName"]:
for rep in element.Representation.Representations:
if rep.RepresentationIdentifier == "Body":
if len(rep.Items) == 1:
if rep.Items[0].SweptArea.is_a("IfcIShapeProfileDef"):
elem = rep.Items[0].SweptArea
value = obj.ProfileName
changed = set_attribute(ifcfile, elem, prop, value)
if changed:
FreeCAD.Console.PrintLog(
"DEBUG: Changing prop"
+ obj.Label
+ ":"
+ str(prop)
+ "to"
+ str(getattr(obj, prop))
+ "\n"
)
return changed