[vtkusers] programmable source multiple hexahedrons in unstructured grid

Olivier Rodenberg orodenberg at gmail.com
Mon Feb 22 13:18:38 EST 2016

Hi all,

I am trying to create a programmable filter where multiple(>1000) cubes(hexahedrons) are stored in an unstructured grid. 
Each of the cubes has an own attribute value which will be used for colouring. 

The centre points, the edge lenth and the attribute value of the cubes are extracted from a PostgreSQL database via the python extension psycopg2. I am able to create a single hexahedron. However I am not able to add more geometries to an unstructured grid. The code that I am using is described bellow.  When running  the code I get the error “TypeError: 'vtkCommonDataModelPython.vtkUnstructuredGrid' object is not iterable”.  I am not sure how to proceed. Is it even possible to create a unstructured grid? And does anyone know how I could store multiple geometries in an unstructured grid?

import psycopg2

def connectDBMS():
    conn = psycopg2.connect("host='localhost' dbname='thesis' user='postgres' password='Supermen1'")
    cur = conn.cursor() 
    cur.execute('SELECT * FROM emptyspace limit 10;')                                                    
    return  cur.fetchall()

def DBMSToGeom(octree):
    numberOfVertices = 8

    # create the unstructured grid
    octree = vtk.vtkUnstructuredGrid()

    # Create the points
    for  materialpath, x , y , z , leafsize in octree:

        # create the coordinates for the points
        l = .5*leafsize
        xL = int(x)-l
        xR = int(x)+l        
        yL = int(y)-l
        yR = int(y)+l        
        zL = int(z)-l
        zR = int(z)+l

        # create the points
        points = vtk.vtkPoints()
        points.InsertNextPoint(xL, yL, zL)
        points.InsertNextPoint(xR, yL, zL)
        points.InsertNextPoint(xR, yR, zL)
        points.InsertNextPoint(xL, yR, zL)
        points.InsertNextPoint(xL, yL, zR)
        points.InsertNextPoint(xR, yL, zR)
        points.InsertNextPoint(xR, yR, zR)
        points.InsertNextPoint(xL, yR, zR)

        # Create a hexahedron from the points
        hex_ = vtk.vtkHexahedron()
        for i in range(0, numberOfVertices):
            hex_.GetPointIds().SetId(i, i)

        #Create the attribute
        NodeLength = vtk.vtkDoubleArray()
        NodeLength.SetName('Node lenght')

        # Add the geometry to the unstructured grid
        octree.InsertNextCell(hex_.GetCellType(), hex_.GetPointIds())

    # Add the points and hexahedron to an unstructured grid
    output.Points = points

if (__name__ == "__main__"):

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20160222/6f7ea78c/attachment.html>

More information about the vtkusers mailing list