[vtkusers] vtkUnstructuredGrid and vtkCellArray

Helvin Lui helvinlui at gmail.com
Sun Aug 23 06:55:48 EDT 2009


Hi,
I have figured out how to do it now. This post was very useful:
http://www.vtk.org/pipermail/vtkusers/2005-October/082317.html

However, when I run my code, another window pops up saying* "vtkpython.exe
has stopped working"*.
Does anyone have any ideas why vtkpython might stop running???

The txt file that I read from has a single line, saying the no. of points,
then a list of point info, then single line saying the no. of elements, then
a list of elements info.

My code in its entirety:

import vtk
from vtk import vtkTriangle
VTK_TRIANGLE = vtkTriangle().GetCellType()

with open('C:\\Qt\\SimLCM\\Default\\Data_Input_Geometry.txt', 'r') as f:

    aMeshGrid = vtk.vtkUnstructuredGrid()
    aMeshGrid.Allocate(2, 180)

    # Get number of mesh points
    no_points = int(f.readline())

    # Set number of points
    meshPoints = vtk.vtkPoints()
    meshPoints.SetNumberOfPoints(no_points)

    # Iterate through point data
    for i in range(no_points):
        # Get coord info for each point
        point_info = f.readline().split() # I need to split, before I assign
to point_coord
                                          # else the whole thing is split
into single numbers
        #print point_info # Check reading
        point_ID = (int(point_info[0])-1) # -1 because the IDs need to start
with 0.
        point_x = float(point_info[1])
        point_y = float(point_info[2])
        point_z = float(point_info[3])
        #print point_ID, point_x, point_y, point_z
        # Set coord info in mesh
        meshPoints.InsertPoint(point_ID, point_x, point_y, point_z)

    # Get number of elements
    no_elements = int(f.readline())
    #print 'the no_elements', no_elements

    # Set number of elements
    for i in range(no_elements):
        element_info = f.readline().split()
        element_ID = (int(element_info[0])-1)
        element_no_nodes = int(element_info[1])

        element_ID_list = vtk.vtkIdList()
        for j in range(element_no_nodes):
            node_no = int(element_info[j+2])
            element_ID_list.InsertNextId(node_no)
            print j, node_no
        if element_no_nodes == 3: # a triangle
            cell_type = VTK_TRIANGLE

        aMeshGrid.InsertNextCell(cell_type, element_ID_list)

aMeshGrid.SetPoints(meshPoints)

aMeshMapper = vtk.vtkDataSetMapper()
aMeshMapper.SetInput(aMeshGrid)

aMeshActor = vtk.vtkActor()
aMeshActor.SetMapper(aMeshMapper)
aMeshActor.GetProperty().SetDiffuseColor(1, 0, 0)

# Create the usual rendering stuff.
ren = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren)
renWin.SetSize(300, 300)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)

ren.AddActor(aMeshActor)

# Zoom, to see actor. (in case it was too small to see)
ren.ResetCamera() #
http://www.vtk.org/doc/nightly/html/classvtkRenderer.html#b14d1aeb74a4990f2da819e09d028d65
cam1 = ren.GetActiveCamera()
cam1.Zoom(10)

## Render the scene and start interaction.
iren.Initialize()
renWin.Render()
iren.Start()


Help appreciated!

Regards,
Helvin


On Sun, Aug 23, 2009 at 6:51 PM, Helvin Lui <helvinlui at gmail.com> wrote:

> I'm gonna cry soon if I don't get this done. > <
> I have a .txt file of points and connectivity data. How do I read this in
> as an unstructuredGrid?
> How do I use vtkCellArray?
>
> At the moment, I have this:
>
> with open('C:\\Qt\\SimLCM\\Default\\Data_Input_Geometry.txt', 'r') as f:
> #with open('C:\\Qt\\SimLCM\\Default\\Data_Input_Geometry_trial2.txt', 'r')
> as f:
>
>     meshPoints = vtk.vtkPoints()
>     # Can't use voxels, because they specify corners that are perpendicular
>
>     # Get number of points
>     no_points = f.readline()
>     no_points = int(no_points)
>     print no_points   # Check reading
>
>     # Set number of points in mesh
>     meshPoints.SetNumberOfPoints(no_points)
>
>     for i in range(no_points):
>         # Get coord info for each point
>         point_info = f.readline().split() # I need to split, before I
> assign to point_coord
>                                           # else the whole thing is split
> into single numbers
>         #print point_info # Check reading
>
>         point_ID = (int(point_info[0])-1) # -1 because the IDs need to
> start with 0.
>         point_x = float(point_info[1])
>         point_y = float(point_info[2])
>         point_z = float(point_info[3])
>         print point_ID, point_x, point_y, point_z
>         # Set coord info in mesh
>         meshPoints.InsertPoint(point_ID, point_x, point_y, point_z)
>
> # Don't need the input file anymore. Get on with the display
> aMeshGrid = vtk.vtkUnstructuredGrid()
> aMeshGrid.Allocate(180,1)
>
> cellTypes = vtk.vtkCellTypes()
> aMeshGrid.InsertNextCell(cellTypes,[1,2,13])
> aMeshGrid.SetPoints(meshPoints)
>
>
>           etc...
>
> But this second last line gives an error saying: TypeError: An Integer is
> required.
>
> PLEASE help!  Please...
> --
> Helvin
>
> "Though the world may promise me more, I'm just made to be filled with the
> Lord."
>



-- 
Helvin

"Though the world may promise me more, I'm just made to be filled with the
Lord."
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20090823/bc8f84d1/attachment.htm>


More information about the vtkusers mailing list