forked from FreeCAD/FreeCAD
/
importInpMesh.py
322 lines (289 loc) · 12.7 KB
/
importInpMesh.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
# ***************************************************************************
# * *
# * Copyright (c) 2016 - Frantisek Loeffelmann <LoffF@email.cz> *
# * *
# * 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 *
# * *
# ***************************************************************************
__title__ = "FreeCAD .inp file reader"
__author__ = "Frantisek Loeffelmann, Bernd Hahnebach"
__url__ = "http://www.freecadweb.org"
__date__ = "04/08/2016"
## @package importInpMesh
# \ingroup FEM
# \brief FreeCAD INP file reader for FEM workbench
import FreeCAD
import os
import string
########## generic FreeCAD import and export methods ##########
if open.__module__ == '__builtin__':
# because we'll redefine open below (Python2)
pyopen = open
elif open.__module__ == 'io':
# because we'll redefine open below (Python3)
pyopen = open
def open(filename):
"called when freecad opens a file"
docname = os.path.splitext(os.path.basename(filename))[0]
insert(filename, docname)
def insert(filename, docname):
"called when freecad wants to import a file"
try:
doc = FreeCAD.getDocument(docname)
except NameError:
doc = FreeCAD.newDocument(docname)
FreeCAD.ActiveDocument = doc
import_inp(filename)
########## module specific methods ##########
def import_inp(filename):
"create imported objects in FreeCAD, currently only FemMesh"
m = read_inp(filename)
import importToolsFem
mesh = importToolsFem.make_femmesh(m)
mesh_name = os.path.splitext(os.path.basename(filename))[0]
mesh_object = FreeCAD.ActiveDocument.addObject('Fem::FemMeshObject', mesh_name)
mesh_object.FemMesh = mesh
def read_inp(file_name):
"read .inp file, currently only the mesh"
class elements():
tria3 = {}
tria6 = {}
quad4 = {}
quad8 = {}
tetra4 = {}
tetra10 = {}
hexa8 = {}
hexa20 = {}
penta6 = {}
penta15 = {}
seg2 = {}
seg3 = {}
error_seg3 = False # to print "not supported"
nodes = {}
model_definition = True
f = pyopen(file_name, "r")
line = "\n"
include = ""
f_include = None
while line != "":
if include:
line = f_include.readline()
if line == "":
f_include.close()
include = ""
line = f.readline()
else:
line = f.readline()
if line.strip() == '':
continue
elif line[0] == '*': # start/end of a reading set
if line[0:2] == '**': # comments
continue
if line[:8].upper() == "*INCLUDE":
start = 1 + line.index("=")
include = line[start:].strip().strip('"')
include_path = os.path.normpath(include)
if os.path.isfile(include_path) is True:
f_include = pyopen(include_path, "r")
else:
path_start = os.path.split(file_name)[0]
include_full_path = os.path.join(path_start, include_path)
f_include = pyopen(include_full_path, "r")
continue
read_node = False
elm_category = []
elm_2nd_line = False
# reading nodes
if (line[:5].upper() == "*NODE") and (model_definition is True):
read_node = True
elif read_node is True:
line_list = string.split(line, ',')
number = int(line_list[0])
x = float(line_list[1])
y = float(line_list[2])
z = float(line_list[3])
nodes[number] = [x, y, z]
# reading elements
elif line[:8].upper() == "*ELEMENT":
line_list = line[8:].upper().split(',')
for line_part in line_list:
if line_part.lstrip()[:4] == "TYPE":
elm_type = line_part.split('=')[1].strip()
if elm_type in ["S3", "CPS3", "CPE3", "CAX3"]:
elm_category = elements.tria3
number_of_nodes = 3
elif elm_type in ["S6", "CPS6", "CPE6", "CAX6"]:
elm_category = elements.tria6
number_of_nodes = 6
elif elm_type in ["S4", "S4R", "CPS4", "CPS4R", "CPE4", "CPE4R",
"CAX4", "CAX4R"]:
elm_category = elements.quad4
number_of_nodes = 4
elif elm_type in ["S8", "S8R", "CPS8", "CPS8R", "CPE8", "CPE8R",
"CAX8", "CAX8R"]:
elm_category = elements.quad8
number_of_nodes = 8
elif elm_type == "C3D4":
elm_category = elements.tetra4
number_of_nodes = 4
elif elm_type == "C3D10":
elm_category = elements.tetra10
number_of_nodes = 10
elif elm_type in ["C3D8", "C3D8R", "C3D8I"]:
elm_category = elements.hexa8
number_of_nodes = 8
elif elm_type in ["C3D20", "C3D20R", "C3D20RI"]:
elm_category = elements.hexa20
number_of_nodes = 20
elif elm_type == "C3D6":
elm_category = elements.penta6
number_of_nodes = 6
elif elm_type == "C3D15":
elm_category = elements.penta15
number_of_nodes = 15
elif elm_type in ["B31", "B31R", "T3D2"]:
elm_category = elements.seg2
number_of_nodes = 2
elif elm_type in ["B32", "B32R", "T3D3"]:
elm_category = elements.seg3
number_of_nodes = 3
error_seg3 = True # to print "not supported"
elif elm_category != []:
line_list = string.split(line, ',')
if elm_2nd_line is False:
number = int(line_list[0])
elm_category[number] = []
pos = 1
else:
pos = 0
elm_2nd_line = False
for en in range(pos, pos + number_of_nodes - len(elm_category[number])):
try:
enode = int(line_list[en])
elm_category[number].append(enode)
except:
elm_2nd_line = True
break
elif line[:5].upper() == "*STEP":
model_definition = False
if error_seg3 is True: # to print "not supported"
FreeCAD.Console.PrintError("Error: seg3 (3-node beam element type) not supported, yet.\n")
f.close()
# switch from the CalculiX node numbering to the FreeCAD node numbering
# numbering do not change: tria3, tria6, quad4, quad8, seg2
for en in elements.tetra4:
n = elements.tetra4[en]
elements.tetra4[en] = [n[1], n[0], n[2], n[3]]
for en in elements.tetra10:
n = elements.tetra10[en]
elements.tetra10[en] = [n[1], n[0], n[2], n[3], n[4], n[6], n[5],
n[8], n[7], n[9]]
for en in elements.hexa8:
n = elements.hexa8[en]
elements.hexa8[en] = [n[5], n[6], n[7], n[4], n[1], n[2], n[3], n[0]]
for en in elements.hexa20:
n = elements.hexa20[en]
elements.hexa20[en] = [n[5], n[6], n[7], n[4], n[1], n[2], n[3], n[0],
n[13], n[14], n[15], n[12], n[9], n[10], n[11],
n[8], n[17], n[18], n[19], n[16]]
for en in elements.penta6:
n = elements.penta6[en]
elements.penta6[en] = [n[4], n[5], n[3], n[1], n[2], n[0]]
for en in elements.penta15:
n = elements.penta15[en]
elements.penta15[en] = [n[4], n[5], n[3], n[1], n[2], n[0],
n[10], n[11], n[9], n[7], n[8], n[6], n[13],
n[14], n[12]]
for en in elements.seg3:
n = elements.seg3[en]
elements.seg3[en] = [n[0], n[2], n[1]]
return {'Nodes': nodes,
'Hexa8Elem': elements.hexa8, 'Penta6Elem': elements.penta6, 'Tetra4Elem': elements.tetra4,
'Tetra10Elem': elements.tetra10, 'Penta15Elem': elements.penta15, 'Hexa20Elem': elements.hexa20,
'Tria3Elem': elements.tria3, 'Tria6Elem': elements.tria6, 'Quad4Elem': elements.quad4,
'Quad8Elem': elements.quad8, 'Seg2Elem': elements.seg2} # , 'Seg3Elem': elements.seg3}
def write_inp(femmesh, file_name):
f = file_name # use file_name in def just to know imediately what f stands for ...
f.write('** written by the FreeCAD Python inp file writer, we gone try mixed meshes of edge and face elements :-)\n\n')
f.write('** Nodes\n')
f.write('*Node, NSET=Nall\n')
# nodes
meshnodes = femmesh.Nodes
for node in meshnodes:
vec = meshnodes[node]
f.write("{0}, {1:.12f}, {2:.12f}, {3:.12f}\n".format(node, vec.x, vec.y, vec.z, node)) # TODO do not writes all the '0'
f.write('\n\n')
# elements
f.write('** Elements\n')
f.write('** we can not just write all edges because there are edges in the mesh which belongs to the faces\n')
f.write('** we need one elementset fo the elements of each mesh dimension\n')
# faces, at the moment I test with beams and faces, thus I can just write all faces and need only to find the edges
# only S6 elements
f.write('** faces element set for the face mesh\n')
f.write('*Element, TYPE=S6, ELSET=Efaces\n')
meshfaces = femmesh.Faces
for mf in meshfaces:
n = femmesh.getElementNodes(mf) # nodes of the mesh element face mf
f.write("{0}, {1}, {2}, {3}, {4}, {5}, {6}\n".format(
mf, n[0], n[1], n[2], n[3], n[4], n[5]))
f.write('\n')
# edges
f.write('** edges element set for the edge mesh\n')
f.write('*Element, TYPE=B32, ELSET=Eedges\n')
# get edges
meshedges = get_fem_mesh_edges_not_belong_to_faces_or_volumes(femmesh)
for me in meshedges:
n = femmesh.getElementNodes(me) # nodes of the mesh element edge me
f.write("{0}, {1}, {2}, {3}\n".format(
me, n[0], n[1], n[2]))
f.write('\n')
'''
f.write('** for debugging we gone writing them all ... \n')
meshedges = femmesh.Edges
for me in meshedges:
n = femmesh.getElementNodes(me) # nodes of the mesh element edge me
f.write("{0}, {1}, {2}, {3}\n".format(
me, n[0], n[1], n[2]))
f.write('\n')
'''
# kommt ins FemMeshtools spaeter
def get_fem_mesh_edges_not_belong_to_faces_or_volumes(femmesh):
'''vorerst nur faces, mhh die def mach ich fuer umsonst, die kann ulrich wohl viiiiieeeel smarter ... egal
- zuerst finde alle edges die zu faces gehoeren
-- nimm die punkte einer edge und schaue in allen faces ob eine face alle drei punkte hat
--- wenn ja gehoert die edge zu einer face
--- wenn nein ist sie eigenstaendig --> die brauchen wir
returens a list of EdgeIDs not belonging to a face or volume (at the moment face only)
[EdgeID, EdgeID, ...]
'''
meshfaces = femmesh.Faces
meshedges = femmesh.Edges
edges = []
for me in meshedges:
nme = femmesh.getElementNodes(me)
# print(nme)
edge_belongs_to_a_face = False
for mf in meshfaces:
nmf = femmesh.getElementNodes(mf)
# print(nmf)
if (nme[0] in nmf) and (nme[1] in nmf) and (nme[2] in nmf): # B32 has 3 nodes
edge_belongs_to_a_face = True
break
if edge_belongs_to_a_face is False:
edges.append(me)
# print(edges)
return edges