1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
#!/usr/bin/env python
from vtkmodules.vtkCommonDataModel import vtkSelectionNode
from vtkmodules.vtkFiltersExtraction import vtkExtractSelection
from vtkmodules.vtkFiltersSources import vtkSelectionSource
from vtkmodules.vtkIOExodus import vtkExodusIIReader
from vtkmodules.util.misc import vtkGetDataRoot
VTK_DATA_ROOT = vtkGetDataRoot()
def GetSelection(ids, inverse=False):
selectionSource = vtkSelectionSource()
selectionSource.SetInverse(inverse)
selectionSource.SetContentType(vtkSelectionNode.BLOCKS)
for i in range(len(ids)):
selectionSource.AddBlock(ids[i])
return selectionSource
def GetNumberOfNonNullLeaves(cd):
count = 0
it = cd.NewIterator()
while not it.IsDoneWithTraversal():
if it.GetCurrentDataObject():
count += 1
it.GoToNextItem()
return count
reader = vtkExodusIIReader()
reader.SetFileName(VTK_DATA_ROOT + "/Data/can.ex2")
reader.UpdateInformation()
extractor = vtkExtractSelection()
extractor.SetInputConnection(0, reader.GetOutputPort())
# extract 0
selectionSource = GetSelection([0])
extractor.SetInputConnection(1, selectionSource.GetOutputPort())
extractor.Update()
assert GetNumberOfNonNullLeaves(extractor.GetOutputDataObject(0)) == 2
# extract inverse of root
selectionSource = GetSelection([0], True)
extractor.SetInputConnection(1, selectionSource.GetOutputPort())
extractor.Update()
assert GetNumberOfNonNullLeaves(extractor.GetOutputDataObject(0)) == 0
# extract a non-leaf block
selectionSource = GetSelection([1])
extractor.SetInputConnection(1, selectionSource.GetOutputPort())
extractor.Update()
assert GetNumberOfNonNullLeaves(extractor.GetOutputDataObject(0)) == 2
# extract a leaf block
selectionSource = GetSelection([2])
extractor.SetInputConnection(1, selectionSource.GetOutputPort())
extractor.Update()
assert GetNumberOfNonNullLeaves(extractor.GetOutputDataObject(0)) == 1
# extract a non-leaf and leaf block
selectionSource = GetSelection([1, 3])
extractor.SetInputConnection(1, selectionSource.GetOutputPort())
extractor.Update()
assert GetNumberOfNonNullLeaves(extractor.GetOutputDataObject(0)) == 2
|