forked from 541435721/myVTKPythonLibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadDynaMesh.py
82 lines (63 loc) · 2.84 KB
/
readDynaMesh.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
#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 readDynaMesh(lsdyna_mesh_filename,
cell_type='hexahedron',
verbose=1):
myVTK.myPrint(verbose, "*** readDynaMesh: "+lsdyna_mesh_filename+" ***")
points = vtk.vtkPoints()
if (cell_type == 'vertex'):
cell_vtk_type = vtk.VTK_VERTEX
cell = vtk.vtkVertex()
cell_array = vtk.vtkCellArray()
elif (cell_type == 'hexahedron'):
cell_vtk_type = vtk.VTK_HEXAHEDRON
cell = vtk.vtkHexahedron()
cell_array = vtk.vtkCellArray()
else:
print 'Wrong cell type. Aborting.'
exit()
myVTK.myPrint(verbose-1, "Reading Dyna mesh file...")
mesh_file = open(lsdyna_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 (cell_type == 'vertex'):
cell.GetPointIds().SetId(0, points.GetNumberOfPoints()-1)
cell_array.InsertNextCell(cell)
if (context == 'reading elems'):
if line.startswith('*'):
context = ''
else:
splitted_line = line.split(',')
if (len(splitted_line) == 3): continue
if (cell_type == 'hexahedron'):
for k_node in xrange(8):
cell.GetPointIds().SetId(k_node, int(splitted_line[2+k_node])-1)
cell_array.InsertNextCell(cell)
if line.startswith('*NODE'): context = 'reading nodes'
if line.startswith('*ELEMENT_SOLID'): context = 'reading elems'
mesh_file.close()
myVTK.myPrint(verbose-1, "Creating VTK mesh...")
ugrid = vtk.vtkUnstructuredGrid()
ugrid.SetPoints(points)
ugrid.SetCells(cell_vtk_type, cell_array)
return ugrid