-
-
Notifications
You must be signed in to change notification settings - Fork 903
Expand file tree
/
Copy pathca2ifc.py
More file actions
450 lines (393 loc) · 18.7 KB
/
ca2ifc.py
File metadata and controls
450 lines (393 loc) · 18.7 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
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
# Ifc2CA - IFC Code_Aster utility
# Copyright (C) 2020, 2021, 2023, 2024 Ioannis P. Christovasilis <ipc@aethereng.com>
#
# This file is part of Ifc2CA.
#
# Ifc2CA is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ifc2CA 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Ifc2CA. If not, see <http://www.gnu.org/licenses/>.
import itertools
import ifcopenshell as ios
import meshio
import numpy as np
flatten = itertools.chain.from_iterable
def get_element_data(model, name, element):
if element["geometry_type"] == "Edge":
for i, cell_block in enumerate(model.cells):
if cell_block.type == "line":
cell_tags = model.cell_data["cell_tags"][i]
break
rows = []
for i_row, i in enumerate(cell_tags):
if i == 0:
continue
tags = model.cell_tags[i]
for tag in tags:
if tag == name:
# print(i_row, i)
rows.append(i_row)
break
points = list(set(flatten([cell_block.data[c] for c in rows])))
points.sort(key=lambda p: np.linalg.norm(model.points[p] - np.array(element["origin"])))
coords = [np.round(model.points[p], 4).tolist() for p in points]
local_coords = [
[float(round(np.linalg.norm(model.points[p] - np.array(element["origin"])), 4))] for p in points
]
return {
"name": name,
"points": points,
"coords": coords,
"local_coords": local_coords,
}
elif element["geometry_type"] == "Face":
triangle_cell_tags = None
quad_cell_tags = None
for i, cell_block in enumerate(model.cells):
if cell_block.type == "triangle":
triangle_cell_tags = model.cell_data["cell_tags"][i]
break
if triangle_cell_tags is not None:
rows = []
for i_row, i in enumerate(triangle_cell_tags):
if i == 0:
continue
tags = model.cell_tags[i]
for tag in tags:
if tag == name:
# print(i_row, i)
rows.append(i_row)
break
if not len(rows):
points = []
else:
points = list(flatten([cell_block.data[c] for c in rows]))
for i, cell_block in enumerate(model.cells):
if cell_block.type == "quad":
quad_cell_tags = model.cell_data["cell_tags"][i]
break
if quad_cell_tags is not None:
rows = []
for i_row, i in enumerate(quad_cell_tags):
if i == 0:
continue
tags = model.cell_tags[i]
for tag in tags:
if tag == name:
# print(i_row, i)
rows.append(i_row)
break
if len(rows):
points.extend(list(flatten([cell_block.data[c] for c in rows])))
points = list(set(points))
points.sort()
coords = [model.points[p].tolist() for p in points]
local_coords = [
np.round(np.array(element["orientation"]).dot(model.points[p] - np.array(element["origin"])), 4).tolist()[
:2
]
for p in points
]
return {
"name": name,
"points": points,
"coords": coords,
"local_coords": local_coords,
}
def get_element_result_data(model, field_label, name, element, field_type):
points = get_element_data(model, name, element)["points"]
if field_type == "InternalForces":
if element["geometry_type"] == "Edge":
return {
"N": [round(model.point_data[field_label][p][0], 4) for p in points],
"VY": [round(model.point_data[field_label][p][1], 4) for p in points],
"VZ": [round(model.point_data[field_label][p][2], 4) for p in points],
"MT": [round(model.point_data[field_label][p][3], 4) for p in points],
"MFY": [round(model.point_data[field_label][p][4], 4) for p in points],
"MFZ": [round(model.point_data[field_label][p][5], 4) for p in points],
}
elif element["geometry_type"] == "Face":
if len(model.point_data[field_label][points[0]]) == 8:
offset = 0
elif len(model.point_data[field_label][points[0]]) == 14:
offset = 6
else:
assert (
False
), f"Internal force field with {len(model.point_data[field_label][points[0]])} field values for {field_label} and {element['Name']} "
return {
"NXX": [round(model.point_data[field_label][p][offset + 0], 4) for p in points],
"NYY": [round(model.point_data[field_label][p][offset + 1], 4) for p in points],
"NXY": [round(model.point_data[field_label][p][offset + 2], 4) for p in points],
"MXX": [round(model.point_data[field_label][p][offset + 3], 4) for p in points],
"MYY": [round(model.point_data[field_label][p][offset + 4], 4) for p in points],
"MXY": [round(model.point_data[field_label][p][offset + 5], 4) for p in points],
"QX": [round(model.point_data[field_label][p][offset + 6], 4) for p in points],
"QY": [round(model.point_data[field_label][p][offset + 7], 4) for p in points],
}
if field_type == "Displacements":
return {
"DX": [round(model.point_data[field_label][p][0], 4) for p in points],
"DY": [round(model.point_data[field_label][p][1], 4) for p in points],
"DZ": [round(model.point_data[field_label][p][2], 4) for p in points],
"DRX": [round(model.point_data[field_label][p][3], 4) for p in points],
"DRY": [round(model.point_data[field_label][p][4], 4) for p in points],
"DRZ": [round(model.point_data[field_label][p][5], 4) for p in points],
}
def results_to_ifc(ifc_file, ifc_model, rmed_path, global_case, field_types, data):
if not rmed_path.exists():
print(f"Med file with results not found for case_instant: {global_case}")
return
result = meshio.read(rmed_path, "med")
if global_case == "LC":
model_cases = data["load_cases"]
elif global_case == "COMB":
model_cases = data["load_combinations"]
for field in field_types:
if field == "InternalForces":
_parsed_data = internal_forces_to_ifc(ifc_file, ifc_model, result, model_cases, data["elements"])
elif field == "Displacements":
_parsed_data = displacements_to_ifc(ifc_file, ifc_model, result, model_cases, data["elements"])
def internal_forces_to_ifc(ifc_file, ifc_model, result, model_cases, elements):
result_cases = [dict() for _ in model_cases]
field_cases = [f"ELEMENT_FORCE[{i}] - {i + 1}" for i in range(len(result_cases))]
# Create Result Groups for load case_instance combinations
for iCase, case_instance in enumerate(model_cases):
result_cases[iCase]["case_instance"] = ifc_file.create_entity(
"IfcStructuralResultGroup",
**{
"GlobalId": ios.guid.new(),
"Name": "Internal Forces for " + case_instance["Name"],
"TheoryType": "FIRST_ORDER_THEORY",
"ResultForLoadGroup": ifc_file.by_id(case_instance["id"]),
"IsLinear": True,
},
)
result_cases[iCase]["assignment"] = ifc_file.create_entity(
"IfcRelAssignsToGroup",
**{
"GlobalId": ios.guid.new(),
"RelatedObjects": [],
"RelatingGroup": result_cases[iCase]["case_instance"],
},
)
if ifc_model.HasResults:
ifc_model.HasResults += tuple([result["case_instance"] for result in result_cases])
else:
ifc_model.HasResults = tuple([result["case_instance"] for result in result_cases])
data = []
for _, element in enumerate(elements):
group_name = getGroupName(element["ref_id"])
name = element["Name"]
info = get_element_data(result, group_name, element)
assert len(info["coords"]) >= 2
for iCase, field_case in enumerate(field_cases):
forces = get_element_result_data(result, field_case, group_name, element, field_type="InternalForces")
reaction = ifc_file.create_entity(
"IfcStructuralCurveReaction" if element["geometry_type"] == "Edge" else "IfcStructuralSurfaceReaction",
**{
"GlobalId": ios.guid.new(),
"Name": "Internal Forces for " + model_cases[iCase]["Name"] + f" on {name}",
# "AppliedLoad": load["ifcLoad"],
"GlobalOrLocal": "LOCAL_COORDS",
"PredefinedType": "DISCRETE",
},
)
result_cases[iCase]["assignment"].RelatedObjects += (reaction,)
ifc_file.create_entity(
"IfcRelConnectsStructuralActivity",
**{
"GlobalId": ios.guid.new(),
"RelatingElement": ifc_file.by_id(element["id"]),
"RelatedStructuralActivity": reaction,
},
)
reaction.AppliedLoad = ifc_file.create_entity(
"IfcStructuralLoadConfiguration",
**{
"Name": "Internal Forces for " + model_cases[iCase]["Name"] + f" on {name}",
"Values": [],
"Locations": tuple([tuple(node) for node in info["local_coords"]]),
},
)
if element["geometry_type"] == "Edge":
for iNode, node in enumerate(info["coords"]):
location = f"({node[0]}, {node[1]}, {node[2]})"
distance = info["local_coords"][iNode][0]
N = forces["N"][iNode]
VY = forces["VY"][iNode]
VZ = forces["VZ"][iNode]
MT = forces["MT"][iNode]
MFY = forces["MFY"][iNode]
MFZ = forces["MFZ"][iNode]
data.append([name, f"LCC-{iCase + 1} @ {distance}", location, N, VY, VZ, MT, MFY, MFZ])
pointValue = ifc_file.create_entity(
"IfcStructuralLoadSingleForce",
**{
"Name": "Internal Forces for " + model_cases[iCase]["Name"] + f" @ {distance} on {name}",
"ForceX": N,
"ForceY": VY,
"ForceZ": VZ,
"MomentX": MT,
"MomentY": MFY,
"MomentZ": MFZ,
},
)
reaction.AppliedLoad.Values += (pointValue,)
elif element["geometry_type"] == "Face":
for iNode, node in enumerate(info["coords"]):
location = f"({node[0]}, {node[1]}, {node[2]})"
distance = tuple(info["local_coords"][iNode])
NXX = forces["NXX"][iNode]
NYY = forces["NYY"][iNode]
NXY = forces["NXY"][iNode]
MXX = forces["MXX"][iNode]
MYY = forces["MYY"][iNode]
MXY = forces["MXY"][iNode]
data.append([name, f"LCC-{iCase + 1} @ {distance}", location, NXX, NYY, NXY, MXX, MYY, MXY])
pointValue = ifc_file.create_entity(
"IfcStructuralLoadSingleForce",
**{
"Name": "Internal Forces for " + model_cases[iCase]["Name"] + f" @ {distance} on {name}",
"ForceX": NXX,
"ForceY": NYY,
"ForceZ": NXY,
"MomentX": MXX,
"MomentY": MYY,
"MomentZ": MXY,
},
)
reaction.AppliedLoad.Values += (pointValue,)
return data
def displacements_to_ifc(ifc_file, ifc_model, result, model_cases, elements):
result_cases = [dict() for _ in model_cases]
field_cases = [f"MODEL_DISP[{i}] - {i + 1}" for i in range(len(result_cases))]
# Create Result Groups for load case_instance combinations
for iCase, case_instance in enumerate(model_cases):
result_cases[iCase]["case_instance"] = ifc_file.create_entity(
"IfcStructuralResultGroup",
**{
"GlobalId": ios.guid.new(),
"Name": "Global Displacements for " + case_instance["Name"],
"TheoryType": "FIRST_ORDER_THEORY",
"ResultForLoadGroup": ifc_file.by_id(case_instance["id"]),
"IsLinear": True,
},
)
result_cases[iCase]["assignment"] = ifc_file.create_entity(
"IfcRelAssignsToGroup",
**{
"GlobalId": ios.guid.new(),
"RelatedObjects": [],
"RelatingGroup": result_cases[iCase]["case_instance"],
},
)
if ifc_model.HasResults:
ifc_model.HasResults += tuple([result["case_instance"] for result in result_cases])
else:
ifc_model.HasResults = tuple([result["case_instance"] for result in result_cases])
data = []
for _, element in enumerate(elements):
group_name = getGroupName(element["ref_id"])
name = element["Name"]
info = get_element_data(result, group_name, element)
assert len(info["coords"]) >= 2
for iCase, case_instance in enumerate(field_cases):
displacements = get_element_result_data(
result, case_instance, group_name, element, field_type="Displacements"
)
reaction = ifc_file.create_entity(
"IfcStructuralCurveReaction" if element["geometry_type"] == "Edge" else "IfcStructuralSurfaceReaction",
**{
"GlobalId": ios.guid.new(),
"Name": "Global Displacements for " + model_cases[iCase]["Name"] + f" on {name}",
# "AppliedLoad": load["ifcLoad"],
"GlobalOrLocal": "LOCAL_COORDS",
"PredefinedType": "DISCRETE",
},
)
result_cases[iCase]["assignment"].RelatedObjects += (reaction,)
ifc_file.create_entity(
"IfcRelConnectsStructuralActivity",
**{
"GlobalId": ios.guid.new(),
"RelatingElement": ifc_file.by_id(element["id"]),
"RelatedStructuralActivity": reaction,
},
)
reaction.AppliedLoad = ifc_file.create_entity(
"IfcStructuralLoadConfiguration",
**{
"Name": "Global Displacements for " + model_cases[iCase]["Name"] + f" on {name}",
"Values": [],
"Locations": tuple([tuple(node) for node in info["local_coords"]]),
},
)
if element["geometry_type"] == "Edge":
for iNode, node in enumerate(info["coords"]):
location = f"({node[0]}, {node[1]}, {node[2]})"
distance = info["local_coords"][iNode][0]
DX = displacements["DX"][iNode]
DY = displacements["DY"][iNode]
DZ = displacements["DZ"][iNode]
DRX = displacements["DRX"][iNode]
DRY = displacements["DRY"][iNode]
DRZ = displacements["DRZ"][iNode]
data.append([name, f"LCC-{iCase + 1} @ {distance}", location, DX, DY, DZ, DRX, DRY, DRZ])
pointValue = ifc_file.create_entity(
"IfcStructuralLoadSingleDisplacement",
**{
"Name": "Global Displacements for "
+ model_cases[iCase]["Name"]
+ f" @ {distance} on {name}",
"DisplacementX": DX,
"DisplacementY": DY,
"DisplacementZ": DZ,
"RotationalDisplacementRX": DRX,
"RotationalDisplacementRY": DRY,
"RotationalDisplacementRZ": DRZ,
},
)
reaction.AppliedLoad.Values += (pointValue,)
elif element["geometry_type"] == "Face":
for iNode, node in enumerate(info["coords"]):
location = f"({node[0]}, {node[1]}, {node[2]})"
distance = tuple(info["local_coords"][iNode])
DX = displacements["DX"][iNode]
DY = displacements["DY"][iNode]
DZ = displacements["DZ"][iNode]
DRX = displacements["DRX"][iNode]
DRY = displacements["DRY"][iNode]
DRZ = displacements["DRZ"][iNode]
data.append([name, f"LCC-{iCase + 1} @ {distance}", location, DX, DY, DZ, DRX, DRY, DRZ])
pointValue = ifc_file.create_entity(
"IfcStructuralLoadSingleDisplacement",
**{
"Name": "Global Displacements for "
+ model_cases[iCase]["Name"]
+ f" @ {distance} on {name}",
"DisplacementX": DX,
"DisplacementY": DY,
"DisplacementZ": DZ,
"RotationalDisplacementRX": DRX,
"RotationalDisplacementRY": DRY,
"RotationalDisplacementRZ": DRZ,
},
)
reaction.AppliedLoad.Values += (pointValue,)
return data
def getGroupName(name):
if "|" in name:
info = name.split("|")
sortName = "".join(c for c in info[0] if c.isupper())
return f"{sortName[2:]}_{info[1]}"
else:
return name