1#!/usr/bin/env python
2import vtk
3from vtk.test import Testing
4from vtk.util.misc import vtkGetDataRoot
5VTK_DATA_ROOT = vtkGetDataRoot()
6
7# This example demonstrates the use of multiline 2D text using
8# vtkTextMappers.  It shows several justifications as well as single-line
9# and multiple-line text inputs.
10#
11# First we include the VTK Tcl packages which will make available
12# all of the vtk commands to Tcl
13#
14font_size = 14
15# Create the text mappers and the associated Actor2Ds.
16# The font and text properties (except justification) are the same for each
17# single line mapper. Let's create a common text property object
18singleLineTextProp = vtk.vtkTextProperty()
19singleLineTextProp.SetFontSize(font_size)
20singleLineTextProp.SetFontFamilyToArial()
21singleLineTextProp.BoldOff()
22singleLineTextProp.ItalicOff()
23singleLineTextProp.ShadowOff()
24# The font and text properties (except justification) are the same for each
25# multi line mapper. Let's create a common text property object
26multiLineTextProp = vtk.vtkTextProperty()
27multiLineTextProp.ShallowCopy(singleLineTextProp)
28multiLineTextProp.BoldOn()
29multiLineTextProp.ItalicOn()
30multiLineTextProp.ShadowOn()
31# The text is on a single line and bottom-justified.
32singleLineTextB = vtk.vtkTextMapper()
33singleLineTextB.SetInput("Single line (bottom)")
34tprop = singleLineTextB.GetTextProperty()
35tprop.ShallowCopy(singleLineTextProp)
36tprop.SetVerticalJustificationToBottom()
37tprop.SetColor(1,0,0)
38singleLineTextActorB = vtk.vtkActor2D()
39singleLineTextActorB.SetMapper(singleLineTextB)
40singleLineTextActorB.GetPositionCoordinate().SetCoordinateSystemToNormalizedDisplay()
41singleLineTextActorB.GetPositionCoordinate().SetValue(0.05,0.85)
42# The text is on a single line and center-justified (vertical justification).
43singleLineTextC = vtk.vtkTextMapper()
44singleLineTextC.SetInput("Single line (centered)")
45tprop = singleLineTextC.GetTextProperty()
46tprop.ShallowCopy(singleLineTextProp)
47tprop.SetVerticalJustificationToCentered()
48tprop.SetColor(0,1,0)
49singleLineTextActorC = vtk.vtkActor2D()
50singleLineTextActorC.SetMapper(singleLineTextC)
51singleLineTextActorC.GetPositionCoordinate().SetCoordinateSystemToNormalizedDisplay()
52singleLineTextActorC.GetPositionCoordinate().SetValue(0.05,0.75)
53# The text is on a single line and top-justified.
54singleLineTextT = vtk.vtkTextMapper()
55singleLineTextT.SetInput("Single line (top)")
56tprop = singleLineTextT.GetTextProperty()
57tprop.ShallowCopy(singleLineTextProp)
58tprop.SetVerticalJustificationToTop()
59tprop.SetColor(0,0,1)
60singleLineTextActorT = vtk.vtkActor2D()
61singleLineTextActorT.SetMapper(singleLineTextT)
62singleLineTextActorT.GetPositionCoordinate().SetCoordinateSystemToNormalizedDisplay()
63singleLineTextActorT.GetPositionCoordinate().SetValue(0.05,0.65)
64# The text is on multiple lines and left- and top-justified.
65textMapperL = vtk.vtkTextMapper()
66textMapperL.SetInput("This is\nmulti-line\ntext output\n(left-top)")
67tprop = textMapperL.GetTextProperty()
68tprop.ShallowCopy(multiLineTextProp)
69tprop.SetJustificationToLeft()
70tprop.SetVerticalJustificationToTop()
71tprop.SetColor(1,0,0)
72textActorL = vtk.vtkActor2D()
73textActorL.SetMapper(textMapperL)
74textActorL.GetPositionCoordinate().SetCoordinateSystemToNormalizedDisplay()
75textActorL.GetPositionCoordinate().SetValue(0.05,0.5)
76# The text is on multiple lines and center-justified (both horizontal and
77# vertical).
78textMapperC = vtk.vtkTextMapper()
79textMapperC.SetInput("This is\nmulti-line\ntext output\n(centered)")
80tprop = textMapperC.GetTextProperty()
81tprop.ShallowCopy(multiLineTextProp)
82tprop.SetJustificationToCentered()
83tprop.SetVerticalJustificationToCentered()
84tprop.SetColor(0,1,0)
85textActorC = vtk.vtkActor2D()
86textActorC.SetMapper(textMapperC)
87textActorC.GetPositionCoordinate().SetCoordinateSystemToNormalizedDisplay()
88textActorC.GetPositionCoordinate().SetValue(0.5,0.5)
89# The text is on multiple lines and right- and bottom-justified.
90textMapperR = vtk.vtkTextMapper()
91textMapperR.SetInput("This is\nmulti-line\ntext output\n(right-bottom)")
92tprop = textMapperR.GetTextProperty()
93tprop.ShallowCopy(multiLineTextProp)
94tprop.SetJustificationToRight()
95tprop.SetVerticalJustificationToBottom()
96tprop.SetColor(0,0,1)
97textActorR = vtk.vtkActor2D()
98textActorR.SetMapper(textMapperR)
99textActorR.GetPositionCoordinate().SetCoordinateSystemToNormalizedDisplay()
100textActorR.GetPositionCoordinate().SetValue(0.95,0.5)
101# Draw the grid to demonstrate the placement of the text.
102# Set up the necessary points.
103Pts = vtk.vtkPoints()
104Pts.InsertNextPoint(0.05,0.0,0.0)
105Pts.InsertNextPoint(0.05,1.0,0.0)
106Pts.InsertNextPoint(0.5,0.0,0.0)
107Pts.InsertNextPoint(0.5,1.0,0.0)
108Pts.InsertNextPoint(0.95,0.0,0.0)
109Pts.InsertNextPoint(0.95,1.0,0.0)
110Pts.InsertNextPoint(0.0,0.5,0.0)
111Pts.InsertNextPoint(1.0,0.5,0.0)
112Pts.InsertNextPoint(0.00,0.85,0.0)
113Pts.InsertNextPoint(0.50,0.85,0.0)
114Pts.InsertNextPoint(0.00,0.75,0.0)
115Pts.InsertNextPoint(0.50,0.75,0.0)
116Pts.InsertNextPoint(0.00,0.65,0.0)
117Pts.InsertNextPoint(0.50,0.65,0.0)
118# Set up the lines that use these points.
119Lines = vtk.vtkCellArray()
120Lines.InsertNextCell(2)
121Lines.InsertCellPoint(0)
122Lines.InsertCellPoint(1)
123Lines.InsertNextCell(2)
124Lines.InsertCellPoint(2)
125Lines.InsertCellPoint(3)
126Lines.InsertNextCell(2)
127Lines.InsertCellPoint(4)
128Lines.InsertCellPoint(5)
129Lines.InsertNextCell(2)
130Lines.InsertCellPoint(6)
131Lines.InsertCellPoint(7)
132Lines.InsertNextCell(2)
133Lines.InsertCellPoint(8)
134Lines.InsertCellPoint(9)
135Lines.InsertNextCell(2)
136Lines.InsertCellPoint(10)
137Lines.InsertCellPoint(11)
138Lines.InsertNextCell(2)
139Lines.InsertCellPoint(12)
140Lines.InsertCellPoint(13)
141# Create a grid that uses these points and lines.
142Grid = vtk.vtkPolyData()
143Grid.SetPoints(Pts)
144Grid.SetLines(Lines)
145# Set up the coordinate system.
146normCoords = vtk.vtkCoordinate()
147normCoords.SetCoordinateSystemToNormalizedViewport()
148# Set up the mapper and actor (2D) for the grid.
149mapper = vtk.vtkPolyDataMapper2D()
150mapper.SetInputData(Grid)
151mapper.SetTransformCoordinate(normCoords)
152gridActor = vtk.vtkActor2D()
153gridActor.SetMapper(mapper)
154gridActor.GetProperty().SetColor(0.1,0.1,0.1)
155# Create the Renderer, RenderWindow, and RenderWindowInteractor
156#
157ren1 = vtk.vtkRenderer()
158renWin = vtk.vtkRenderWindow()
159renWin.SetMultiSamples(0)
160renWin.AddRenderer(ren1)
161iren = vtk.vtkRenderWindowInteractor()
162iren.SetRenderWindow(renWin)
163# Add the actors to the renderer; set the background and size; zoom in
164# closer to the image; render
165#
166ren1.AddActor2D(gridActor)
167ren1.AddActor2D(textActorL)
168ren1.AddActor2D(textActorC)
169ren1.AddActor2D(textActorR)
170ren1.AddActor2D(singleLineTextActorB)
171ren1.AddActor2D(singleLineTextActorC)
172ren1.AddActor2D(singleLineTextActorT)
173ren1.SetBackground(1,1,1)
174renWin.SetSize(500,300)
175ren1.GetActiveCamera().Zoom(1.5)
176renWin.Render()
177# Set the user method (bound to key 'u')
178#
179# Withdraw the default tk window.
180# --- end of script --
181