1#!/usr/bin/env python 2 3# This example shows how to extract portions of an unstructured grid 4# using vtkExtractUnstructuredGrid. vtkConnectivityFilter is also used 5# to extract connected components. 6# 7# The data found here represents a blow molding process. Blow molding 8# requires a mold and parison (hot, viscous plastic) which is shaped 9# by the mold into the final form. The data file contains several steps 10# in time for the analysis. 11 12import vtk 13from vtk.util.misc import vtkGetDataRoot 14VTK_DATA_ROOT = vtkGetDataRoot() 15 16# Create a reader to read the unstructured grid data. We use a 17# vtkDataSetReader which means the type of the output is unknown until 18# the data file is read. So we follow the reader with a 19# vtkCastToConcrete and cast the output to vtkUnstructuredGrid. 20reader = vtk.vtkDataSetReader() 21reader.SetFileName(VTK_DATA_ROOT + "/Data/blow.vtk") 22reader.SetScalarsName("thickness9") 23reader.SetVectorsName("displacement9") 24castToUnstructuredGrid = vtk.vtkCastToConcrete() 25castToUnstructuredGrid.SetInputConnection(reader.GetOutputPort()) 26warp = vtk.vtkWarpVector() 27warp.SetInputConnection(castToUnstructuredGrid.GetOutputPort()) 28 29# The connectivity filter extracts the first two regions. These are 30# know to represent the mold. 31connect = vtk.vtkConnectivityFilter() 32 33connect.SetInputConnection(warp.GetOutputPort()) 34connect.SetExtractionModeToSpecifiedRegions() 35connect.AddSpecifiedRegion(0) 36connect.AddSpecifiedRegion(1) 37moldMapper = vtk.vtkDataSetMapper() 38moldMapper.SetInputConnection(reader.GetOutputPort()) 39moldMapper.ScalarVisibilityOff() 40moldActor = vtk.vtkActor() 41moldActor.SetMapper(moldMapper) 42moldActor.GetProperty().SetColor(.2, .2, .2) 43moldActor.GetProperty().SetRepresentationToWireframe() 44 45# Another connectivity filter is used to extract the parison. 46connect2 = vtk.vtkConnectivityFilter() 47connect2.SetInputConnection(warp.GetOutputPort()) 48connect2.SetExtractionModeToSpecifiedRegions() 49connect2.AddSpecifiedRegion(2) 50 51# We use vtkExtractUnstructuredGrid because we are interested in 52# looking at just a few cells. We use cell clipping via cell id to 53# extract the portion of the grid we are interested in. 54extractGrid = vtk.vtkExtractUnstructuredGrid() 55extractGrid.SetInputConnection(connect2.GetOutputPort()) 56extractGrid.CellClippingOn() 57extractGrid.SetCellMinimum(0) 58extractGrid.SetCellMaximum(23) 59parison = vtk.vtkGeometryFilter() 60parison.SetInputConnection(extractGrid.GetOutputPort()) 61normals2 = vtk.vtkPolyDataNormals() 62normals2.SetInputConnection(parison.GetOutputPort()) 63normals2.SetFeatureAngle(60) 64lut = vtk.vtkLookupTable() 65lut.SetHueRange(0.0, 0.66667) 66parisonMapper = vtk.vtkPolyDataMapper() 67parisonMapper.SetInputConnection(normals2.GetOutputPort()) 68parisonMapper.SetLookupTable(lut) 69parisonMapper.SetScalarRange(0.12, 1.0) 70parisonActor = vtk.vtkActor() 71parisonActor.SetMapper(parisonMapper) 72 73# graphics stuff 74ren = vtk.vtkRenderer() 75renWin = vtk.vtkRenderWindow() 76renWin.AddRenderer(ren) 77iren = vtk.vtkRenderWindowInteractor() 78iren.SetRenderWindow(renWin) 79 80# Add the actors to the renderer, set the background and size 81ren.AddActor(parisonActor) 82ren.AddActor(moldActor) 83ren.SetBackground(1, 1, 1) 84ren.ResetCamera() 85ren.GetActiveCamera().Azimuth(60) 86ren.GetActiveCamera().Roll(-90) 87ren.GetActiveCamera().Dolly(2) 88ren.ResetCameraClippingRange() 89renWin.SetSize(500, 375) 90 91iren.Initialize() 92renWin.Render() 93iren.Start() 94