1#!/usr/bin/env python 2 3import os 4import vtk 5from vtk.util.misc import vtkGetTempDir 6 7VTK_TEMP_DIR = vtkGetTempDir() 8filename = VTK_TEMP_DIR + '/TestCellArray.vtu' 9 10 11ugrid = vtk.vtkUnstructuredGrid() 12pts = vtk.vtkPoints() 13ugrid.SetPoints(pts) 14ugrid.Allocate(10) 15 16for i in range(10): 17 pts.InsertNextPoint(i, i, i) 18 ids = [i] 19 ugrid.InsertNextCell(1, 1, ids) # a vertex 20 21writer = vtk.vtkXMLUnstructuredGridWriter() 22writer.SetInputDataObject(ugrid) 23writer.SetFileName(filename) 24writer.Write() 25 26reader = vtk.vtkXMLUnstructuredGridReader() 27reader.SetFileName(filename) 28reader.Update() 29 30readergrid = reader.GetOutput() 31copygrid = readergrid.NewInstance() 32 33copygrid.DeepCopy(readergrid) 34 35pts = copygrid.GetPoints() 36#o.SetPoints(pts) 37 38pt = [1, 2, 3] 39 40idlist = vtk.vtkIdList() 41idlist.SetNumberOfIds(1) 42 43for i in range(5): 44 newpt = [10+i, 10+i, 10+i] 45 pts.InsertNextPoint(newpt) 46 iii = [copygrid.GetNumberOfPoints()-1] 47 copygrid.InsertNextCell(1, 1, iii) 48 copygrid.GetCellPoints(copygrid.GetNumberOfCells()-1, idlist) 49 50if copygrid.GetNumberOfPoints() != 15 or copygrid.GetNumberOfCells() != 15: 51 print ("ERROR: incorrect number of points and or cells") 52 import sys 53 sys.exit(1) 54 55count = 0 56ci = copygrid.NewCellIterator() 57# Now we verify that each vertex has the proper point (the cell id should be the point id) 58while ci.IsDoneWithTraversal() == False: 59 id = ci.GetCellId() 60 ids = ci.GetPointIds() 61 copygrid.GetCellPoints(id, idlist) 62 if ids.GetId(0) != idlist.GetId(0) or idlist.GetId(0) != count: 63 print ("ERROR: incorrect cell points ", count, id, ids.GetId(0), idlist.GetId(0)) 64 import sys 65 sys.exit(1) 66 67 count = count+1 68 ci.GoToNextCell() 69 70ci.UnRegister(None) 71ci = None 72 73os.remove(filename) 74