<!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>
- create a lookup table with the color<BR>
- assign an array to each cell<BR>
- 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>
cp = node.Cp()<BR>
coord = model.Coord(cp)<BR>
pos = coord.transformToGlobal(node.xyz)<BR>
Coord = []<BR>
gpos = pos[0]<BR>
x = float(gpos[0])<BR>
y = float(gpos[1])<BR>
z = float(gpos[2])<BR>
Coord.append(x)<BR>
Coord.append(y)<BR>
Coord.append(z)<BR>
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>
vertex = vtk.vtkVertex()<BR>
nodeID = nid[0]<BR>
vertex.GetPointIds().SetId(0, nidMap[nodeID]) <BR>
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(): <BR>
if isinstance(element, CONM2): <BR>
exmcon = model.Element(eid)<BR>
val = str(exmcon)<BR>
val.strip()<BR>
mcon = val.strip().split()<BR>
g = (int(mcon[2]))<BR>
mconvert = vtk.vtkVertex() <BR>
mconvert.GetPointIds().SetId(0, nidMap[g]) <BR>
grid.InsertNextCell(mconvert.GetCellType(), mconvert.GetPointIds()) <BR>
<BR>
if isinstance(element, CBUSH) or isinstance(element, CBEAM):<BR>
exbar = model.Element(eid) <BR>
val = str(exbar)<BR>
val.strip()<BR>
bar = val.strip().split() <BR>
ga = int(bar[3])<BR>
gb = int(bar[4]) <BR>
bar = vtk.vtkLine() <BR>
bar.GetPointIds().SetId(0, nidMap[ga])<BR>
bar.GetPointIds().SetId(1, nidMap[gb])<BR>
grid.InsertNextCell(bar.GetCellType(), bar.GetPointIds()) <BR>
<BR>
if isinstance(element, CTRIA3) or isinstance(element, CTRIAR):<BR>
#print "ctria3"<BR>
elem = vtk.vtkTriangle()<BR>
nodeIDs = element.nodeIDs()<BR>
elem.GetPointIds().SetId(0, nidMap[nodeIDs[0]])<BR>
elem.GetPointIds().SetId(1, nidMap[nodeIDs[1]])<BR>
elem.GetPointIds().SetId(2, nidMap[nodeIDs[2]])<BR>
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>
exrbe = rbelist[k] <BR>
val = str(exrbe)<BR>
val.strip()<BR>
rbe = val.strip().split()<BR>
l = len(rbe)<BR>
rbeid = int(rbe[1]) <BR>
for q in range(4,l): <BR>
Nilist.append(int(r<BR>
be[2])) <BR>
Nilist.append(int(rbe[q]))<BR>
rbevizdic[p] = Nilist<BR>
Nilist = []<BR>
rbeeltmap[rbeid] = p<BR>
p = 0<BR>
<BR>
for elt in rbevizdic:<BR>
rbeline = vtk.vtkLine()<BR>
rbenode = rbevizdic[elt]<BR>
Masternode = rbenode[0]<BR>
Slavenode = rbenode[1]<BR>
rbeline.GetPointIds().SetId(0, nidMap[Masternode])<BR>
rbeline.GetPointIds().SetId(1, nidMap[Slavenode])<BR>
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>