Difference between revisions of "VTK/Examples/Python/SolidColoredTriangle"

From KitwarePublic
< VTK‎ | Examples‎ | Python
Jump to navigationJump to search
 
Line 3: Line 3:
 
import vtk
 
import vtk
 
from vtk import *
 
from vtk import *
 
+
 
#setup points and vertices
 
#setup points and vertices
 
Points = vtk.vtkPoints()
 
Points = vtk.vtkPoints()
 
Triangles = vtk.vtkCellArray()
 
Triangles = vtk.vtkCellArray()
 
+
 
Points.InsertNextPoint(1.0, 0.0, 0.0)
 
Points.InsertNextPoint(1.0, 0.0, 0.0)
 
Points.InsertNextPoint(0.0, 0.0, 0.0)
 
Points.InsertNextPoint(0.0, 0.0, 0.0)
 
Points.InsertNextPoint(0.0, 1.0, 0.0)
 
Points.InsertNextPoint(0.0, 1.0, 0.0)
 
+
 
Triangle = vtk.vtkTriangle();
 
Triangle = vtk.vtkTriangle();
 
Triangle.GetPointIds().SetId(0, 0);
 
Triangle.GetPointIds().SetId(0, 0);
Line 17: Line 17:
 
Triangle.GetPointIds().SetId(2, 2);
 
Triangle.GetPointIds().SetId(2, 2);
 
Triangles.InsertNextCell(Triangle);
 
Triangles.InsertNextCell(Triangle);
 
+
 
#setup colors (setting the name to "Colors" is nice but not necessary)
 
#setup colors (setting the name to "Colors" is nice but not necessary)
 
Colors = vtk.vtkUnsignedCharArray();
 
Colors = vtk.vtkUnsignedCharArray();
Line 23: Line 23:
 
Colors.SetName("Colors");
 
Colors.SetName("Colors");
 
Colors.InsertNextTuple3(255,0,0);
 
Colors.InsertNextTuple3(255,0,0);
 
+
 
polydata = vtk.vtkPolyData()
 
polydata = vtk.vtkPolyData()
 
polydata.SetPoints(Points)
 
polydata.SetPoints(Points)
 
polydata.SetPolys(Triangles)
 
polydata.SetPolys(Triangles)
 
+
 
polydata.GetCellData().SetScalars(Colors);
 
polydata.GetCellData().SetScalars(Colors);
 
polydata.Modified()
 
polydata.Modified()
polydata.Update()
+
if vtk.VTK_MAJOR_VERSION <= 5:
 
+
    polydata.Update()
 +
 
writer = vtk.vtkXMLPolyDataWriter();
 
writer = vtk.vtkXMLPolyDataWriter();
 
writer.SetFileName("TriangleSolidColor.vtp");
 
writer.SetFileName("TriangleSolidColor.vtp");
writer.SetInput(polydata);
+
if vtk.VTK_MAJOR_VERSION <= 5:
writer.Write();
+
    writer.SetInput(polydata)
 +
else:
 +
    writer.SetInputData(polydata)
 +
writer.Write()
 
</source>
 
</source>

Latest revision as of 19:28, 28 May 2012

SolidColoredTriangle.py

import vtk
from vtk import *
 
#setup points and vertices
Points = vtk.vtkPoints()
Triangles = vtk.vtkCellArray()
 
Points.InsertNextPoint(1.0, 0.0, 0.0)
Points.InsertNextPoint(0.0, 0.0, 0.0)
Points.InsertNextPoint(0.0, 1.0, 0.0)
 
Triangle = vtk.vtkTriangle();
Triangle.GetPointIds().SetId(0, 0);
Triangle.GetPointIds().SetId(1, 1);
Triangle.GetPointIds().SetId(2, 2);
Triangles.InsertNextCell(Triangle);
 
#setup colors (setting the name to "Colors" is nice but not necessary)
Colors = vtk.vtkUnsignedCharArray();
Colors.SetNumberOfComponents(3);
Colors.SetName("Colors");
Colors.InsertNextTuple3(255,0,0);
 
polydata = vtk.vtkPolyData()
polydata.SetPoints(Points)
polydata.SetPolys(Triangles)
 
polydata.GetCellData().SetScalars(Colors);
polydata.Modified()
if vtk.VTK_MAJOR_VERSION <= 5:
    polydata.Update()
 
writer = vtk.vtkXMLPolyDataWriter();
writer.SetFileName("TriangleSolidColor.vtp");
if vtk.VTK_MAJOR_VERSION <= 5:
    writer.SetInput(polydata)
else:
    writer.SetInputData(polydata)
writer.Write()