Difference between revisions of "VTK/Examples/Python/Graphs/SelectedVerticesAndEdges"

From KitwarePublic
< VTK‎ | Examples‎ | Python
Jump to navigationJump to search
m (Code cleanup (indentation) + added picture)
 
Line 6: Line 6:
 
<source lang="python">
 
<source lang="python">
 
#!/usr/bin/env python
 
#!/usr/bin/env python
 +
from __future__ import print_function
  
 
import vtk
 
import vtk
+
 
 
source = vtk.vtkRandomGraphSource()
 
source = vtk.vtkRandomGraphSource()
 
source.Update()
 
source.Update()
+
 
 
view = vtk.vtkGraphLayoutView()
 
view = vtk.vtkGraphLayoutView()
 
view.AddRepresentationFromInputConnection(source.GetOutputPort())
 
view.AddRepresentationFromInputConnection(source.GetOutputPort())
+
 
 
def selectionCallback(caller, event):
 
def selectionCallback(caller, event):
   
+
 
 
     # In C++ there is some extra data passed to the callback, but in Python
 
     # In C++ there is some extra data passed to the callback, but in Python
 
     # the callback data is lost...
 
     # the callback data is lost...
  
     # There should be two selection nodes, but which one is vertices and which  
+
     # There should be two selection nodes, but which one is vertices and which
 
     # is edges does not seem to be guaranteed...
 
     # is edges does not seem to be guaranteed...
 
     sel = caller.GetCurrentSelection()
 
     sel = caller.GetCurrentSelection()
Line 33: Line 34:
 
         printFieldType(node0_field_type)
 
         printFieldType(node0_field_type)
 
         for ii in range(sel_list0.GetNumberOfTuples()):
 
         for ii in range(sel_list0.GetNumberOfTuples()):
             print "\t", sel_list0.GetValue(ii)
+
             print("\t", sel_list0.GetValue(ii))
  
 
     if (sel_list1.GetNumberOfTuples() > 0):
 
     if (sel_list1.GetNumberOfTuples() > 0):
 
         printFieldType(node1_field_type)
 
         printFieldType(node1_field_type)
 
         for ii in range(sel_list1.GetNumberOfTuples()):
 
         for ii in range(sel_list1.GetNumberOfTuples()):
             print "\t", sel_list1.GetValue(ii)
+
             print("\t", sel_list1.GetValue(ii))
+
 
         print "- - -"
+
         print("- - -")
+
 
+
 
 
def printFieldType(field_type):
 
def printFieldType(field_type):
   
+
 
 
     if field_type == 3:
 
     if field_type == 3:
           print "Vertices Selected:"
+
           print("Vertices Selected:")
 
     elif field_type == 4:
 
     elif field_type == 4:
           print "Edges Selected:"
+
           print("Edges Selected:")
 
     else:
 
     else:
           print "Unknown type:"
+
           print("Unknown type:")
+
 
 
rep = view.GetRepresentation(0)
 
rep = view.GetRepresentation(0)
+
 
 
# The vtkRenderedGraphRepresentation should already have a vtkAnnotationLink,
 
# The vtkRenderedGraphRepresentation should already have a vtkAnnotationLink,
# so we just want to grab it and add an observer with our callback function  
+
# so we just want to grab it and add an observer with our callback function
 
# attached
 
# attached
 
link = rep.GetAnnotationLink()
 
link = rep.GetAnnotationLink()
 
link.AddObserver("AnnotationChangedEvent", selectionCallback)
 
link.AddObserver("AnnotationChangedEvent", selectionCallback)
+
 
 
view.GetRenderWindow().SetSize(600, 600)
 
view.GetRenderWindow().SetSize(600, 600)
 
view.ResetCamera()
 
view.ResetCamera()

Latest revision as of 01:23, 23 August 2015

VTK Examples Python Graphs SelectedVerticesAndEdges.png

  • Contributed by Eric Monson

SelectedVerticesAndEdges.py

#!/usr/bin/env python
from __future__ import print_function

import vtk

source = vtk.vtkRandomGraphSource()
source.Update()

view = vtk.vtkGraphLayoutView()
view.AddRepresentationFromInputConnection(source.GetOutputPort())

def selectionCallback(caller, event):

    # In C++ there is some extra data passed to the callback, but in Python
    # the callback data is lost...

    # There should be two selection nodes, but which one is vertices and which
    # is edges does not seem to be guaranteed...
    sel = caller.GetCurrentSelection()
    node0 = sel.GetNode(0)
    node0_field_type = node0.GetFieldType()
    sel_list0 = caller.GetCurrentSelection().GetNode(0).GetSelectionList()
    node1 = sel.GetNode(1)
    node1_field_type = node1.GetFieldType()
    sel_list1 = caller.GetCurrentSelection().GetNode(1).GetSelectionList()

    if (sel_list0.GetNumberOfTuples() > 0):
        printFieldType(node0_field_type)
        for ii in range(sel_list0.GetNumberOfTuples()):
            print("\t", sel_list0.GetValue(ii))

    if (sel_list1.GetNumberOfTuples() > 0):
        printFieldType(node1_field_type)
        for ii in range(sel_list1.GetNumberOfTuples()):
            print("\t", sel_list1.GetValue(ii))

        print("- - -")


def printFieldType(field_type):

    if field_type == 3:
           print("Vertices Selected:")
    elif field_type == 4:
           print("Edges Selected:")
    else:
           print("Unknown type:")

rep = view.GetRepresentation(0)

# The vtkRenderedGraphRepresentation should already have a vtkAnnotationLink,
# so we just want to grab it and add an observer with our callback function
# attached
link = rep.GetAnnotationLink()
link.AddObserver("AnnotationChangedEvent", selectionCallback)

view.GetRenderWindow().SetSize(600, 600)
view.ResetCamera()
view.Render()
view.GetInteractor().Start()