forked from 541435721/myVTKPythonLibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadAbaqusMeshFromINP.py
83 lines (66 loc) · 3.1 KB
/
readAbaqusMeshFromINP.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
#coding=utf8
########################################################################
### ###
### Created by Martin Genet, 2012-2016 ###
### ###
### University of California at San Francisco (UCSF), USA ###
### Swiss Federal Institute of Technology (ETH), Zurich, Switzerland ###
### École Polytechnique, Palaiseau, France ###
### ###
########################################################################
import vtk
import myVTKPythonLibrary as myVTK
########################################################################
def readAbaqusMeshFromINP(
mesh_filename,
elem_types="all",
verbose=1):
myVTK.myPrint(verbose, "*** readAbaqusMeshFromINP: "+mesh_filename+" ***")
points = vtk.vtkPoints()
cell_array = vtk.vtkCellArray()
mesh_file = open(mesh_filename, "r")
context = ""
for line in mesh_file:
if (line[-1:] == "\n"): line = line[:-1]
#myVTK.myPrint(verbose-1, "line =", line)
if line.startswith("**"): continue
if (context == "reading nodes"):
if line.startswith("*"):
context = ""
else:
splitted_line = line.split(",")
points.InsertNextPoint([float(coord) for coord in splitted_line[1:4]])
if (context == "reading elems"):
if line.startswith("*"):
context = ""
else:
splitted_line = line.split(",")
assert (len(splitted_line) == 1+cell_n_points), "Wrong number of elements in line. Aborting."
for k_point in xrange(cell_n_points): cell.GetPointIds().SetId(k_point, int(splitted_line[1+k_point])-1)
cell_array.InsertNextCell(cell)
if line.startswith("*NODE"):
context = "reading nodes"
if line.startswith("*ELEMENT"):
if ("TYPE=F3D4" in line) and (("quad" in elem_types) or ("all" in elem_types)):
context = "reading elems"
cell_vtk_type = vtk.VTK_QUAD
cell_n_points = 4
cell = vtk.vtkQuad()
elif ("TYPE=C3D4" in line) and (("tet" in elem_types) or ("all" in elem_types)):
context = "reading elems"
cell_vtk_type = vtk.VTK_TETRA
cell_n_points = 4
cell = vtk.vtkTetra()
elif ("TYPE=C3D8" in line) and (("hex" in elem_types) or ("all" in elem_types)):
context = "reading elems"
cell_vtk_type = vtk.VTK_HEXAHEDRON
cell_n_points = 8
cell = vtk.vtkHexahedron()
else:
print "Warning: elements not read: "+line+"."
mesh_file.close()
ugrid = vtk.vtkUnstructuredGrid()
ugrid.SetPoints(points)
ugrid.SetCells(cell_vtk_type, cell_array)
myVTK.myPrint(verbose-1, "n_cells = "+str(ugrid.GetNumberOfCells()))
return ugrid