-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
femmesh2mesh.py
203 lines (181 loc) · 7.98 KB
/
femmesh2mesh.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
# ***************************************************************************
# * *
# * 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__ = "FemMesh to Mesh converter"
__author__ = "Frantisek Loeffelmann, Ulrich Brammer, Bernd Hahnebach"
__url__ = "http://www.freecadweb.org"
## @package FwmMesh2Mesh
# \ingroup FEM
import time
import FreeCAD
# import Mesh
"""
from femexamples.manager import *
doc = run_ccx_cantileverfaceload()
fem_mesh = doc.getObject("Mesh").FemMesh # do not remove the _
result = doc.getObject("CCX_Results")
from femmesh import femmesh2mesh
out_mesh = femmesh2mesh.femmesh_2_mesh(fem_mesh, result)
import Mesh
Mesh.show(Mesh.Mesh(out_mesh))
"""
# These dictionaries list the nodes, that define faces of an element.
# The key is the face number, used internally by FreeCAD.
# The list contains the nodes in the element for each face.
tetFaces = {
1: [0, 1, 2],
2: [0, 3, 1],
3: [1, 3, 2],
4: [2, 3, 0]}
pentaFaces = {
1: [0, 1, 2],
2: [3, 5, 4],
3: [0, 3, 4, 1],
4: [1, 4, 5, 2],
5: [0, 2, 5, 3]}
hexaFaces = { # hexa8 or hexa20 (ignoring mid-nodes)
1: [0, 1, 2, 3],
2: [4, 7, 6, 5],
3: [0, 4, 5, 1],
4: [1, 5, 6, 2],
5: [2, 6, 7, 3],
6: [3, 7, 4, 0]}
pyraFaces = { # pyra5 or pyra13 (ignoring mid-nodes)
1: [0, 1, 2, 3],
2: [0, 4, 1],
3: [1, 4, 2],
4: [2, 4, 3],
5: [3, 4, 0]}
face_dicts = {
4: tetFaces,
5: pyraFaces,
6: pentaFaces,
8: hexaFaces,
10: tetFaces,
13: pyraFaces,
15: pentaFaces,
20: hexaFaces}
def femmesh_2_mesh(myFemMesh, myResults=None):
shiftBits = 20 # allows a million nodes, needs to be higher for more nodes in a FemMesh
# This code generates a dict and a faceCode for each face of all elements
# All faceCodes are than sorted.
start_time = time.process_time()
faceCodeList = []
faceCodeDict = {}
if myFemMesh.VolumeCount > 0:
for ele in myFemMesh.Volumes:
element_nodes = myFemMesh.getElementNodes(ele)
# print("element_node: ", element_nodes)
faceDef = face_dicts[len(element_nodes)]
for key in faceDef:
nodeList = []
codeList = []
faceCode = 0
shifter = 0
for nodeIdx in faceDef[key]:
nodeList.append(element_nodes[nodeIdx])
codeList.append(element_nodes[nodeIdx])
codeList.sort()
for node in codeList:
faceCode += (node << shifter)
# x << n: x shifted left by n bits = Multiplication
shifter += shiftBits
# print("codeList: ", codeList)
faceCodeDict[faceCode] = nodeList
faceCodeList.append(faceCode)
elif myFemMesh.FaceCount > 0:
for ele in myFemMesh.Faces:
element_nodes = myFemMesh.getElementNodes(ele)
# print("element_node: ", element_nodes)
faceDef = {1: [0, 1, 2]}
for key in faceDef:
nodeList = []
codeList = []
faceCode = 0
shifter = 0
for nodeIdx in faceDef[key]:
nodeList.append(element_nodes[nodeIdx])
codeList.append(element_nodes[nodeIdx])
codeList.sort()
for node in codeList:
faceCode += (node << shifter)
# x << n: x shifted left by n bits = Multiplication
shifter += shiftBits
# print("codeList: ", codeList)
faceCodeDict[faceCode] = nodeList
faceCodeList.append(faceCode)
faceCodeList.sort()
allFaces = len(faceCodeList)
actFaceIdx = 0
singleFaces = []
# Here we search for faces, which do not have a counterpart.
# These are the faces on the surface of the mesh.
while actFaceIdx < allFaces:
if actFaceIdx < (allFaces - 1):
if faceCodeList[actFaceIdx] == faceCodeList[actFaceIdx + 1]:
actFaceIdx += 2
else:
# print("found a single Face: ", faceCodeList[actFaceIdx])
singleFaces.append(faceCodeList[actFaceIdx])
actFaceIdx += 1
else:
FreeCAD.Console.PrintMessage("Found a last Face: {}\n".format(faceCodeList[actFaceIdx]))
singleFaces.append(faceCodeList[actFaceIdx])
actFaceIdx += 1
output_mesh = []
if myResults:
FreeCAD.Console.PrintMessage("{}\n".format(myResults.Name))
for myFace in singleFaces:
face_nodes = faceCodeDict[myFace]
dispVec0 = myResults.DisplacementVectors[myResults.NodeNumbers.index(face_nodes[0])]
dispVec1 = myResults.DisplacementVectors[myResults.NodeNumbers.index(face_nodes[1])]
dispVec2 = myResults.DisplacementVectors[myResults.NodeNumbers.index(face_nodes[2])]
triangle = [myFemMesh.getNodeById(face_nodes[0]) + dispVec0,
myFemMesh.getNodeById(face_nodes[1]) + dispVec1,
myFemMesh.getNodeById(face_nodes[2]) + dispVec2]
output_mesh.extend(triangle)
# print("my triangle: ", triangle)
if len(face_nodes) == 4:
dispVec3 = myResults.DisplacementVectors[myResults.NodeNumbers.index(face_nodes[3])]
triangle = [myFemMesh.getNodeById(face_nodes[2]) + dispVec2,
myFemMesh.getNodeById(face_nodes[3]) + dispVec3,
myFemMesh.getNodeById(face_nodes[0]) + dispVec0]
output_mesh.extend(triangle)
# print("my 2. triangle: ", triangle)
else:
for myFace in singleFaces:
face_nodes = faceCodeDict[myFace]
triangle = [myFemMesh.getNodeById(face_nodes[0]),
myFemMesh.getNodeById(face_nodes[1]),
myFemMesh.getNodeById(face_nodes[2])]
output_mesh.extend(triangle)
# print("my triangle: ", triangle)
if len(face_nodes) == 4:
triangle = [myFemMesh.getNodeById(face_nodes[2]),
myFemMesh.getNodeById(face_nodes[3]),
myFemMesh.getNodeById(face_nodes[0])]
output_mesh.extend(triangle)
# print("my 2. triangle: ", triangle)
end_time = time.process_time()
FreeCAD.Console.PrintMessage(
"Mesh by surface search method: {}\n".format(end_time - start_time)
)
return output_mesh