[vtkusers] Assign a color to each cell of an unstructured grid and save it in a vtu file

Pascal Graignic pascal.graignic at utc.fr
Thu Feb 14 04:18:09 EST 2013


Hello,

since my last mail, I have managed the parsing of a nastran file and its visualization in vtk, in Python.
For that, I created different cells corresponding to the element contained in a nastran file.
Each cell are set in a unique unstructured grid.
However, the grid is in a unique color. I want to assign different color for each cell for this unique unstructuredgrid.
And save it to a .vtu file.

My opinion for doing this is to: 
	- create a lookup table with the color
	- assign an array to each cell
	- Map the desired color from the look up table to the cell through the vtkFloatArray

However, I don't know how to use it or to make it...

I looked to the Cxx example for PolyData ColorCells in the wiki vtk, but I don't understand how it works.
Could someone can help me for this issue?


To help you in the understanding of my issue, this is my python code to parse and visualize a nastran file: 
Many thanks in advance...

import os
import vtk
from pyNastran.bdf.bdf import (BDF, CTRIA3, CBUSH, CBEAM, CONM2)

file0 = 'nastran_file.vtu'
#Set the nastran model and read it with the pyNastran library
model = BDF()
model.readBulkBDF('nastran_file.bdf', includeDir=None, xref=False)

# Creation of point and unstructured grid for visualization
points = vtk.vtkPoints()
grid = vtk.vtkUnstructuredGrid()

#Parse the Node list of a Nastran file, convert coordinate in cartesian, create vtkpoints of the grid
for (nid, node) in model.nodes.iteritems():
    cp = node.Cp()
    coord = model.Coord(cp)
    pos = coord.transformToGlobal(node.xyz)
    Coord = []
    gpos = pos[0]
    x = float(gpos[0])
    y = float(gpos[1])
    z = float(gpos[2])
    Coord.append(x)
    Coord.append(y)
    Coord.append(z)
    points.InsertNextPoint(*Coord)
grid.SetPoints(points)

#Create the vertex to visualize the GRID of Nastran file
for nid in model.nodes.iteritems():
    vertex = vtk.vtkVertex()
    nodeID = nid[0]
    vertex.GetPointIds().SetId(0, nidMap[nodeID])    
    grid.InsertNextCell(vertex.GetCellType(), vertex.GetPointIds())

#Parse the element in the nastran file, and create the desired representation in vtk


for (eid, element) in model.elements.iteritems():   
    if isinstance(element, CONM2):                
        exmcon = model.Element(eid)
        val = str(exmcon)
        val.strip() 
        mcon = val.strip().split()
        g = (int(mcon[2]))
        mconvert = vtk.vtkVertex()        
        mconvert.GetPointIds().SetId(0, nidMap[g])    
        grid.InsertNextCell(mconvert.GetCellType(), mconvert.GetPointIds())   
    
    if isinstance(element, CBUSH) or isinstance(element, CBEAM):
        exbar = model.Element(eid)                        
        val = str(exbar)
        val.strip() 
        bar = val.strip().split()        
        ga = int(bar[3])
        gb = int(bar[4])      
        bar = vtk.vtkLine()  
        bar.GetPointIds().SetId(0, nidMap[ga])
        bar.GetPointIds().SetId(1, nidMap[gb])
        grid.InsertNextCell(bar.GetCellType(), bar.GetPointIds())    
    
    if isinstance(element, CTRIA3) or isinstance(element, CTRIAR):
        #print "ctria3"
        elem = vtk.vtkTriangle()
        nodeIDs = element.nodeIDs()
        elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]])
        elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]])
        elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]])
        grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds())


rbes = model.rigidElements
rbelist = rbes.values()
rbenidmap = {} 
Nilist = []
rbevizdic = {}
rbeconnec = {}
rbeeltmap = {}
p = 0
nrbe = len(rbelist)
for k in range (0, nrbe):
    exrbe = rbelist[k]          
    val = str(exrbe)
    val.strip() 
    rbe = val.strip().split()
    l = len(rbe)
    rbeid = int(rbe[1])        
    for q in range(4,l):                       
        Nilist.append(int(r
        be[2]))  
        Nilist.append(int(rbe[q])) 
        rbevizdic[p] = Nilist
        Nilist = []
    rbeeltmap[rbeid] = p
    p = 0

for elt in rbevizdic:
    rbeline = vtk.vtkLine()
    rbenode = rbevizdic[elt]
    Masternode = rbenode[0]
    Slavenode = rbenode[1]
    rbeline.GetPointIds().SetId(0, nidMap[Masternode])
    rbeline.GetPointIds().SetId(1, nidMap[Slavenode])
    grid.InsertNextCell(rbeline.GetCellType(), rbeline.GetPointIds())

#Map the unstructured grid for visualization
Gridmapper = vtk.vtkDataSetMapper()
Gridmapper.SetInput(grid)
Gridactor = vtk.vtkActor()
Gridactor.SetMapper(Gridmapper)

#Write the grid in a vtu file
modelwriter = vtk.vtkUnstructuredGridWriter()
modelwriter.SetFileName(file0)
modelwriter.SetInput(grid)
modelwriter.Write()

# Create the Renderer
renderer = vtk.vtkRenderer()
renderer.AddActor(Gridactor)
renderer.SetBackground(0.1, 0.2, 0.3) # Set background to white

# Create the RendererWindow
renderer_window = vtk.vtkRenderWindow()
renderer_window.AddRenderer(renderer)

# Create the RendererWindowInteractor and display the vtk_file
interactor = vtk.vtkRenderWindowInteractor()
interactor.SetRenderWindow(renderer_window)
style = vtk.vtkInteractorStyleTrackballCamera()
interactor.SetInteractorStyle(style)
interactor.Initialize()
interactor.Start()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20130214/24f4eb1f/attachment.htm>


More information about the vtkusers mailing list