-
-
Notifications
You must be signed in to change notification settings - Fork 902
Expand file tree
/
Copy pathcontext.py
More file actions
305 lines (281 loc) · 11.4 KB
/
context.py
File metadata and controls
305 lines (281 loc) · 11.4 KB
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
import ifcopenshell
import ifcopenshell.api
import ifcopenshell.api.aggregate
import ifcopenshell.api.context
import ifcopenshell.api.feature
import ifcopenshell.api.geometry
import ifcopenshell.api.root
import ifcopenshell.api.spatial
import ifcopenshell.api.type
import ifcopenshell.api.unit
import ifcopenshell.geom
import ifcopenshell.util.representation
import ifcopenshell.util.unit
import numpy as np
import propertygroups
O = 0.0, 0.0, 0.0
X = 1.0, 0.0, 0.0
Y = 0.0, 1.0, 0.0
Z = 0.0, 0.0, 1.0
class Context:
model = None
body = None
storey = None
def __init__(self):
self._create_empty_model()
def clear(self):
self._create_empty_model()
def open(self, file_content):
self.model = ifcopenshell.file.from_string(file_content)
body = [
ctx for ctx in self.model.by_type("IfcGeometricRepresentationContext") if ctx.ContextIdentifier == "Body"
]
if body:
self.body = body[0]
else:
context = ifcopenshell.api.context.add_context(self.model, context_type="Model")
self.body = ifcopenshell.api.context.add_context(
self.model,
context_type="Model",
context_identifier="Body",
target_view="MODEL_VIEW",
parent=context,
)
axis = [
ctx for ctx in self.model.by_type("IfcGeometricRepresentationContext") if ctx.ContextIdentifier == "Axis"
]
if body:
self.axis = axis[0]
else:
context = ifcopenshell.api.context.add_context(self.model, context_type="Model")
self.axis = ifcopenshell.api.context.add_context(
self.model,
context_type="Model",
context_identifier="Axis",
target_view="GRAPH_VIEW",
parent=context,
)
self.storey = self.model.by_type("IfcBuildingStorey")[0]
def _create_empty_model(self):
# Create a blank model
self.model = ifcopenshell.file()
# All projects must have one IFC Project element
project = ifcopenshell.api.root.create_entity(self.model, ifc_class="IfcProject", name="My Project")
# Geometry is optional in IFC, but because we want to use geometry in this example, let's define units
# Assigning without arguments defaults to metric units
ifcopenshell.api.unit.assign_unit(self.model)
# Let's create a modeling geometry context, so we can store 3D geometry (note: IFC supports 2D too!)
context = ifcopenshell.api.context.add_context(self.model, context_type="Model")
# In particular, in this example we want to store the 3D "body" geometry of objects, i.e. the body shape
self.body = ifcopenshell.api.context.add_context(
self.model,
context_type="Model",
context_identifier="Body",
target_view="MODEL_VIEW",
parent=context,
)
self.axis = ifcopenshell.api.context.add_context(
self.model,
context_type="Model",
context_identifier="Axis",
target_view="GRAPH_VIEW",
parent=context,
)
# Create a site, building, and storey. Many hierarchies are possible.
site = ifcopenshell.api.root.create_entity(self.model, ifc_class="IfcSite", name="My Site")
building = ifcopenshell.api.root.create_entity(self.model, ifc_class="IfcBuilding", name="Building A")
self.storey = ifcopenshell.api.root.create_entity(
self.model,
ifc_class="IfcBuildingStorey",
name="Ground Floor",
)
# Since the site is our top level location, assign it to the project
# Then place our building on the site, and our storey in the building
ifcopenshell.api.aggregate.assign_object(
self.model,
relating_object=project,
products=[site],
)
ifcopenshell.api.aggregate.assign_object(
self.model,
relating_object=site,
products=[building],
)
ifcopenshell.api.aggregate.assign_object(
self.model,
relating_object=building,
products=[self.storey],
)
def create_2pt_wall(self, p1, p2, elevation, height, thickness, container, wall_type=None):
p1 = np.array([p1[0], p1[1]])
p2 = np.array([p2[0], p2[1]])
wall = ifcopenshell.api.root.create_entity(self.model, ifc_class="IfcWall")
length = float(np.linalg.norm(p2 - p1))
representation = ifcopenshell.api.geometry.add_wall_representation(
self.model,
context=self.body,
length=length,
height=height,
thickness=thickness,
)
ifcopenshell.api.geometry.assign_representation(
self.model,
product=wall,
representation=representation,
)
representation = ifcopenshell.api.geometry.add_axis_representation(
self.model,
context=self.axis,
axis=[(0.0, 0.0), (length, 0.0)],
)
ifcopenshell.api.geometry.assign_representation(
self.model,
product=wall,
representation=representation,
)
v = p2 - p1
v = np.divide(v, float(np.linalg.norm(v)), casting="unsafe")
matrix = np.array(
[
[v[0], -v[1], 0, p1[0]],
[v[1], v[0], 0, p1[1]],
[0, 0, 1, elevation],
[0, 0, 0, 1],
]
)
ifcopenshell.api.geometry.edit_object_placement(self.model, product=wall, matrix=matrix)
ifcopenshell.api.spatial.assign_container(
self.model,
relating_structure=container,
products=[wall],
)
if wall_type:
ifcopenshell.api.type.assign_type(
self.model,
related_object=wall,
relating_type=wall_type,
)
return wall
def get_element(self, guid):
return self.model[guid]
def get_model(self):
return self.model
def create_fill(self, ty, pt, wall):
if isinstance(wall, str):
wall = self.model[wall]
if not wall.is_a("IfcWall"):
raise ValueError("Only 'wall' hosts are supported")
if ty == "door":
props = propertygroups.BIMDoorProperties()
elif ty == "window":
props = propertygroups.BIMWindowProperties()
else:
raise ValueError("Only 'door' or 'window' fills are supported")
si_conversion = ifcopenshell.util.unit.calculate_unit_scale(self.model)
body = ifcopenshell.util.representation.get_context(self.model, "Model", "Body", "MODEL_VIEW")
representation_data = props.to_dict(si_conversion=si_conversion)
representation_data["context"] = body
if ty == "door":
door_representation = ifcopenshell.api.geometry.add_door_representation(self.model, **representation_data)
else:
door_representation = ifcopenshell.api.geometry.add_window_representation(self.model, **representation_data)
door = ifcopenshell.api.root.create_entity(self.model, ifc_class=f"ifc{ty}")
door.OverallWidth = props.overall_width / si_conversion
door.OverallHeight = props.overall_height / si_conversion
ifcopenshell.api.geometry.assign_representation(
self.model,
product=door,
representation=door_representation,
)
ifcopenshell.api.spatial.assign_container(
self.model,
relating_structure=self.storey,
products=[door],
)
r = [r for r in wall.Representation.Representations if r.RepresentationIdentifier == "Axis"]
if not r:
raise ValueError("Axis representation is needed")
r = r[0]
axis_geometry = ifcopenshell.geom.create_shape(
ifcopenshell.geom.settings(
DIMENSIONALITY=ifcopenshell.ifcopenshell_wrapper.CURVES_SURFACES_AND_SOLIDS,
USE_WORLD_COORDS=True,
),
wall,
r,
)
vs = np.array(axis_geometry.geometry.verts).reshape((-1, 3))
es = np.array(axis_geometry.geometry.edges).reshape((-1, 2))
A, B = vs[es[0]]
v = B - A
P = np.zeros(3)
P[0 : len(pt)] = pt
AP = P - A
AP_dot_v = np.dot(AP, v)
v_dot_v = np.dot(v, v)
t = AP_dot_v / v_dot_v * np.linalg.norm(v) / si_conversion
opening = ifcopenshell.api.root.create_entity(
self.model,
ifc_class="IfcOpeningElement",
predefined_type="OPENING",
name="Opening",
)
position_3d = None
if self.model.schema == "IFC2X3":
position_3d = self.model.createIfcAxis2Placement2D(self.model.createIfcCartesianPoint([0.0, 0.0, 0.0]))
position_2d = self.model.createIfcAxis2Placement2D(
self.model.createIfcCartesianPoint([door.OverallWidth / 2.0, 0.0])
)
opening.Representation = self.model.createIfcProductDefinitionShape(
Representations=[
self.model.createIfcShapeRepresentation(
body,
"Body",
"SweptSolid",
Items=[
self.model.createIfcExtrudedAreaSolid(
self.model.createIfcRectangleProfileDef(
"AREA",
None,
position_2d,
door.OverallWidth,
1.2 / si_conversion,
),
position_3d,
self.model.createIfcDirection((0.0, 0.0, 1.0)),
door.OverallHeight,
)
],
)
]
)
ifcopenshell.api.feature.add_feature(self.model, feature=opening, element=wall)
ifcopenshell.api.feature.add_filling(self.model, opening=opening, element=door)
z_offsets = {"door": 0, "window": 1}
opening.ObjectPlacement = self.model.createIfcLocalPlacement(
wall.ObjectPlacement,
self.model.createIfcAxis2Placement3D(
self.model.createIfcCartesianPoint((float(t), 0.0, z_offsets[ty] / si_conversion))
),
)
door.ObjectPlacement = self.model.createIfcLocalPlacement(
opening.ObjectPlacement,
self.model.createIfcAxis2Placement3D(self.model.createIfcCartesianPoint((0.0, 0.0, 0.0))),
)
return door
def to_obj_file(self, fn):
st = ifcopenshell.geom.settings(USE_WORLD_COORDS=True, WELD_VERTICES=False)
it = ifcopenshell.geom.iterator(st, self.model, exclude=("IfcOpeningElement",))
sr = ifcopenshell.geom.serializers.obj(fn, fn + ".mtl", st, ifcopenshell.geom.serializer_settings())
if it.initialize():
for el in ifcopenshell.geom.consume_iterator(it):
sr.write(el)
sr.finalize()
if __name__ == "__main__":
m = Context()
w1 = m.create_2pt_wall((0.0, 0.0), (4.0, 0.0), 0.0, 3.0, 0.1, m.storey)
w2 = m.create_2pt_wall((1.0, 3.0), (1.0, 0.0), 0.0, 3.0, 0.1, m.storey)
m.create_fill("door", [2, 0.0], w1)
m.create_fill("window", [1, 1.5], w2)
m.model.write("out.ifc")
m.to_obj_file("out.obj")