<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Generator" CONTENT="MS Exchange Server version 6.5.7654.12">
<TITLE>Assign a color to each cell of an unstructured grid and save it in a vtu file</TITLE>
</HEAD>
<BODY>
<!-- Converted from text/plain format -->

<P><FONT SIZE=2>Hello,<BR>
<BR>
since my last mail, I have managed the parsing of a nastran file and its visualization in vtk, in Python.<BR>
For that, I created different cells corresponding to the element contained in a nastran file.<BR>
Each cell are set in a unique unstructured grid.<BR>
However, the grid is in a unique color. I want to assign different color for each cell for this unique unstructuredgrid.<BR>
And save it to a .vtu file.<BR>
<BR>
My opinion for doing this is to:<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; - create a lookup table with the color<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; - assign an array to each cell<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; - Map the desired color from the look up table to the cell through the vtkFloatArray<BR>
<BR>
However, I don't know how to use it or to make it...<BR>
<BR>
I looked to the Cxx example for PolyData ColorCells in the wiki vtk, but I don't understand how it works.<BR>
Could someone can help me for this issue?<BR>
<BR>
<BR>
To help you in the understanding of my issue, this is my python code to parse and visualize a nastran file:<BR>
Many thanks in advance...<BR>
<BR>
import os<BR>
import vtk<BR>
from pyNastran.bdf.bdf import (BDF, CTRIA3, CBUSH, CBEAM, CONM2)<BR>
<BR>
file0 = 'nastran_file.vtu'<BR>
#Set the nastran model and read it with the pyNastran library<BR>
model = BDF()<BR>
model.readBulkBDF('nastran_file.bdf', includeDir=None, xref=False)<BR>
<BR>
# Creation of point and unstructured grid for visualization<BR>
points = vtk.vtkPoints()<BR>
grid = vtk.vtkUnstructuredGrid()<BR>
<BR>
#Parse the Node list of a Nastran file, convert coordinate in cartesian, create vtkpoints of the grid<BR>
for (nid, node) in model.nodes.iteritems():<BR>
&nbsp;&nbsp;&nbsp; cp = node.Cp()<BR>
&nbsp;&nbsp;&nbsp; coord = model.Coord(cp)<BR>
&nbsp;&nbsp;&nbsp; pos = coord.transformToGlobal(node.xyz)<BR>
&nbsp;&nbsp;&nbsp; Coord = []<BR>
&nbsp;&nbsp;&nbsp; gpos = pos[0]<BR>
&nbsp;&nbsp;&nbsp; x = float(gpos[0])<BR>
&nbsp;&nbsp;&nbsp; y = float(gpos[1])<BR>
&nbsp;&nbsp;&nbsp; z = float(gpos[2])<BR>
&nbsp;&nbsp;&nbsp; Coord.append(x)<BR>
&nbsp;&nbsp;&nbsp; Coord.append(y)<BR>
&nbsp;&nbsp;&nbsp; Coord.append(z)<BR>
&nbsp;&nbsp;&nbsp; points.InsertNextPoint(*Coord)<BR>
grid.SetPoints(points)<BR>
<BR>
#Create the vertex to visualize the GRID of Nastran file<BR>
for nid in model.nodes.iteritems():<BR>
&nbsp;&nbsp;&nbsp; vertex = vtk.vtkVertex()<BR>
&nbsp;&nbsp;&nbsp; nodeID = nid[0]<BR>
&nbsp;&nbsp;&nbsp; vertex.GetPointIds().SetId(0, nidMap[nodeID])&nbsp;&nbsp;&nbsp;<BR>
&nbsp;&nbsp;&nbsp; grid.InsertNextCell(vertex.GetCellType(), vertex.GetPointIds())<BR>
<BR>
#Parse the element in the nastran file, and create the desired representation in vtk<BR>
<BR>
<BR>
for (eid, element) in model.elements.iteritems():&nbsp;&nbsp;<BR>
&nbsp;&nbsp;&nbsp; if isinstance(element, CONM2):&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exmcon = model.Element(eid)<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; val = str(exmcon)<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; val.strip()<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mcon = val.strip().split()<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; g = (int(mcon[2]))<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mconvert = vtk.vtkVertex()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mconvert.GetPointIds().SetId(0, nidMap[g])&nbsp;&nbsp;&nbsp;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; grid.InsertNextCell(mconvert.GetCellType(), mconvert.GetPointIds())&nbsp;&nbsp;<BR>
&nbsp;&nbsp;&nbsp;<BR>
&nbsp;&nbsp;&nbsp; if isinstance(element, CBUSH) or isinstance(element, CBEAM):<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exbar = model.Element(eid)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; val = str(exbar)<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; val.strip()<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bar = val.strip().split()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ga = int(bar[3])<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; gb = int(bar[4])&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bar = vtk.vtkLine()&nbsp;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bar.GetPointIds().SetId(0, nidMap[ga])<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bar.GetPointIds().SetId(1, nidMap[gb])<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; grid.InsertNextCell(bar.GetCellType(), bar.GetPointIds())&nbsp;&nbsp;&nbsp;<BR>
&nbsp;&nbsp;&nbsp;<BR>
&nbsp;&nbsp;&nbsp; if isinstance(element, CTRIA3) or isinstance(element, CTRIAR):<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #print &quot;ctria3&quot;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; elem = vtk.vtkTriangle()<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; nodeIDs = element.nodeIDs()<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]])<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]])<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]])<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; grid.InsertNextCell(elem.GetCellType(), elem.GetPointIds())<BR>
<BR>
<BR>
rbes = model.rigidElements<BR>
rbelist = rbes.values()<BR>
rbenidmap = {}<BR>
Nilist = []<BR>
rbevizdic = {}<BR>
rbeconnec = {}<BR>
rbeeltmap = {}<BR>
p = 0<BR>
nrbe = len(rbelist)<BR>
for k in range (0, nrbe):<BR>
&nbsp;&nbsp;&nbsp; exrbe = rbelist[k]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>
&nbsp;&nbsp;&nbsp; val = str(exrbe)<BR>
&nbsp;&nbsp;&nbsp; val.strip()<BR>
&nbsp;&nbsp;&nbsp; rbe = val.strip().split()<BR>
&nbsp;&nbsp;&nbsp; l = len(rbe)<BR>
&nbsp;&nbsp;&nbsp; rbeid = int(rbe[1])&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>
&nbsp;&nbsp;&nbsp; for q in range(4,l):&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Nilist.append(int(r<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; be[2]))&nbsp;<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Nilist.append(int(rbe[q]))<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rbevizdic[p] = Nilist<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Nilist = []<BR>
&nbsp;&nbsp;&nbsp; rbeeltmap[rbeid] = p<BR>
&nbsp;&nbsp;&nbsp; p = 0<BR>
<BR>
for elt in rbevizdic:<BR>
&nbsp;&nbsp;&nbsp; rbeline = vtk.vtkLine()<BR>
&nbsp;&nbsp;&nbsp; rbenode = rbevizdic[elt]<BR>
&nbsp;&nbsp;&nbsp; Masternode = rbenode[0]<BR>
&nbsp;&nbsp;&nbsp; Slavenode = rbenode[1]<BR>
&nbsp;&nbsp;&nbsp; rbeline.GetPointIds().SetId(0, nidMap[Masternode])<BR>
&nbsp;&nbsp;&nbsp; rbeline.GetPointIds().SetId(1, nidMap[Slavenode])<BR>
&nbsp;&nbsp;&nbsp; grid.InsertNextCell(rbeline.GetCellType(), rbeline.GetPointIds())<BR>
<BR>
#Map the unstructured grid for visualization<BR>
Gridmapper = vtk.vtkDataSetMapper()<BR>
Gridmapper.SetInput(grid)<BR>
Gridactor = vtk.vtkActor()<BR>
Gridactor.SetMapper(Gridmapper)<BR>
<BR>
#Write the grid in a vtu file<BR>
modelwriter = vtk.vtkUnstructuredGridWriter()<BR>
modelwriter.SetFileName(file0)<BR>
modelwriter.SetInput(grid)<BR>
modelwriter.Write()<BR>
<BR>
# Create the Renderer<BR>
renderer = vtk.vtkRenderer()<BR>
renderer.AddActor(Gridactor)<BR>
renderer.SetBackground(0.1, 0.2, 0.3) # Set background to white<BR>
<BR>
# Create the RendererWindow<BR>
renderer_window = vtk.vtkRenderWindow()<BR>
renderer_window.AddRenderer(renderer)<BR>
<BR>
# Create the RendererWindowInteractor and display the vtk_file<BR>
interactor = vtk.vtkRenderWindowInteractor()<BR>
interactor.SetRenderWindow(renderer_window)<BR>
style = vtk.vtkInteractorStyleTrackballCamera()<BR>
interactor.SetInteractorStyle(style)<BR>
interactor.Initialize()<BR>
interactor.Start()<BR>
</FONT>
</P>

</BODY>
</HTML>