def __init__(self, module_manager): SimpleVTKClassModuleBase.__init__( self, module_manager, vtk.vtkXMLStructuredGridReader(), 'Reading vtkXMLStructuredGrid.', (), ('vtkXMLStructuredGrid',), replaceDoc=True, inputFunctions=None, outputFunctions=None)
def extract_data(self): """ Extract the data from the VTK file using the python-vtk library The data is passed to numpy arrays for better manipulation """ # Check the type of file to load it according to its grid structure # which was specified when calling the class These dictionary entries # return the corresponding VTK Reader functions, so they can be easily # called according to the class argument reader_type = { 'XMLUnstructuredGrid': lambda: vtk.vtkXMLUnstructuredGridReader(), 'XMLStructuredGrid': lambda: vtk.vtkXMLStructuredGridReader(), 'UnstructuredGrid': lambda: vtk.vtkUnstructuredGridReader(), 'StructuredGrid': lambda: vtk.vtkStructuredGridReader(), } if self.vtkfiletype.startswith('XML'): # Load the vtk file from the input file self.reader = reader_type[self.vtkfiletype]() self.reader.SetFileName(self.vtk_file) else: # Load the vtk file from the input file self.reader = reader_type[self.vtkfiletype]() self.reader.SetFileName(self.vtk_file) # For Non XML vtk files: self.reader.ReadAllVectorsOn() self.reader.ReadAllScalarsOn() self.reader.Update() # Get the coordinates of the nodes in the mesh nodes_vtk_array = self.reader.GetOutput().GetPoints().GetData() # Get The vector field (data of every node) vf_vtk_array = self.reader.GetOutput().GetPointData().GetArray(0) # Transform the coordinates of the nodes to a Numpy array and # save them to the corresponding class objects nodes_numpy_array = vtk_to_numpy(nodes_vtk_array) self.x, self.y, self.z = (nodes_numpy_array[:, 0], nodes_numpy_array[:, 1], nodes_numpy_array[:, 2] ) # Transform the magnetisation data to a Numpy array and save vf_numpy_array = vtk_to_numpy(vf_vtk_array) self.vf = vf_numpy_array
#line1.SetColor(150, 100, 0, 255) if __name__ == "__main__": dirname, filename = os.path.split(sys.argv[1]) _, _, k = filename.partition("-") l,_,_ = k.partition(".") view = vtk.vtkContextView() view.GetRenderer().SetBackground(1.0, 1.0, 1.0) view.GetRenderWindow().SetSize(800, 600) chart = vtk.vtkChartXY() view.GetScene().AddItem(chart) chart.SetTitle("time = {}".format(int(l))) chart.GetAxis(0).SetTitle("Pressure") chart.GetAxis(1).SetTitle("Coordinate") reader = vtk.vtkXMLStructuredGridReader() reader.SetFileName(sys.argv[1]) reader.Update() addPlot(chart, reader, "time = {}".format(int(l))) #chart.SetShowLegend(True) view.GetRenderWindow().SetMultiSamples(0) view.GetRenderWindow().Render() captureImage(view.GetRenderWindow(), l)
def readVTSFile(fileName): '''Function to read vtk structured grid (vts) and return a grid object.''' Reader = vtk.vtkXMLStructuredGridReader() Reader.SetFileName(fileName) Reader.Update() return Reader.GetOutput()
def extract_data(self, rotate=None): """ Extract the data from the VTK file using the python-vtk library The data is passed to numpy arrays for better manipulation """ # Check the type of file to load it according to its grid structure # which was specified when calling the class These dictionary entries # return the corresponding VTK Reader functions, so they can be easily # called according to the class argument # # Rotate argument is a rotation in the x-y plane. To use, set rotate # equal to an angle in radians. reader_type = { 'XMLUnstructuredGrid': lambda: vtk.vtkXMLUnstructuredGridReader(), 'XMLStructuredGrid': lambda: vtk.vtkXMLStructuredGridReader(), 'UnstructuredGrid': lambda: vtk.vtkUnstructuredGridReader(), 'StructuredGrid': lambda: vtk.vtkStructuredGridReader(), } if self.vtkfiletype.startswith('XML'): # Load the vtk file from the input file self.reader = reader_type[self.vtkfiletype]() self.reader.SetFileName(self.vtk_file) else: # Load the vtk file from the input file self.reader = reader_type[self.vtkfiletype]() self.reader.SetFileName(self.vtk_file) # For Non XML vtk files: self.reader.ReadAllVectorsOn() self.reader.ReadAllScalarsOn() self.reader.Update() # Get the coordinates of the nodes in the mesh nodes_vtk_array = self.reader.GetOutput().GetPoints().GetData() # Get The vector field (data of every node) vf_vtk_array = self.reader.GetOutput().GetPointData().GetArray(0) # Transform the coordinates of the nodes to a Numpy array and # save them to the corresponding class objects nodes = vtk_to_numpy(nodes_vtk_array) if rotate: self.x = nodes[:, 0]*np.cos(rotate) - nodes[:, 1]*np.sin(rotate) self.y = nodes[:, 0]*np.sin(rotate) + nodes[:, 1]*np.cos(rotate) self.z = nodes[:, 2] else: self.x, self.y, self.z = (nodes[:, 0], nodes[:, 1], nodes[:, 2] ) # Transform the magnetisation data to a Numpy array and save if rotate: vf = vtk_to_numpy(vf_vtk_array) vfx = vf[:, 0]*np.cos(rotate) - vf[:, 1]*np.sin(rotate) vfy = vf[:, 0]*np.sin(rotate) + vf[:, 1]*np.cos(rotate) vfz = vf[:, 2] self.vf = np.zeros_like(vf) self.vf[:, 0] = vfx self.vf[:, 1] = vfy self.vf[:, 2] = vfz else: self.vf = vtk_to_numpy(vf_vtk_array)