Talk:VTK/Examples/Python

From KitwarePublic
< Talk:VTK‎ | Examples
Revision as of 17:45, 23 April 2015 by Sxg125 (talk | contribs) (Dump a Graph in VTK format to visualize it in ParaView/VisIt)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search
  1. !/usr/bin/env python
  2. Sanjaya Gajurel, Computational Scientist, Case Western Reserve University, April 2015

import sys import vtk import os

  1. ------------------------------------------------------------------------------
  2. Script Entry Point
  3. ------------------------------------------------------------------------------

if __name__ == "__main__":

   print "vtkGraph: Building a graph using Unstructured Grid, dump it in a vtk file, vertex.vtu, to be visualized using ParaView"
   pointSource = vtk.vtkPointSource()
   pointSource.Update()
   # Create an integer array to store vertex id data link it with its degree value as a scalar.
   degree  = vtk.vtkIntArray()
   degree.SetNumberOfComponents(1)
   degree.SetName("degree")
   degree.SetNumberOfTuples(7)
   degree.SetValue(0,2)
   degree.SetValue(1,1)
   degree.SetValue(2,3)
   degree.SetValue(3,3)
   degree.SetValue(4,4)
   degree.SetValue(5,2)
   degree.SetValue(6,1)
   pointSource.GetOutput().GetPointData().AddArray(degree)
   # Assaign co-ordinates for vertices
   Points = vtk.vtkPoints()
   Points.InsertNextPoint(0,1,0)
   Points.InsertNextPoint(0,0,0)
   Points.InsertNextPoint(1,1,0)
   Points.InsertNextPoint(1,0,0)
   Points.InsertNextPoint(2,1,0)
   Points.InsertNextPoint(2,0,0)
   Points.InsertNextPoint(3,0,0)
 
   # Establish the specified edges using CellArray
   line = vtk.vtkCellArray()
   line.Allocate(8)
   line.InsertNextCell(2)
   line.InsertCellPoint(0)
   line.InsertCellPoint(1)
   line.InsertNextCell(2)
   line.InsertCellPoint(0)
   line.InsertCellPoint(2)
   line.InsertNextCell(2)
   line.InsertCellPoint(2)
   line.InsertCellPoint(3)
   line.InsertNextCell(2)
   line.InsertCellPoint(2)
   line.InsertCellPoint(4)
   line.InsertNextCell(2)
   line.InsertCellPoint(3)
   line.InsertCellPoint(4)
   line.InsertNextCell(2)
   line.InsertCellPoint(3)
   line.InsertCellPoint(5)
   line.InsertNextCell(2)
   line.InsertCellPoint(4)
   line.InsertCellPoint(5)
   line.InsertNextCell(2)
   line.InsertCellPoint(4)
   line.InsertCellPoint(6)
   # Add the vertices and edges to unstructured Grid
   G = vtk.vtkUnstructuredGrid()
   G.GetPointData().SetScalars(degree)
   G.SetPoints(Points)
   G.SetCells(vtk.VTK_LINE, line)
   # Dump the graph in VTK unstructured format (.vtu)
   gw = vtk.vtkXMLUnstructuredGridWriter()
   gw.SetFileName("vertex.vtu")
   gw.SetInput(G)
   gw.Write()
   print '---> ',
   print "Feed the vertex.vtu file in ParaView/VisIt."