1package require vtk
2package require vtkinteraction
3
4# Renderer, renwin
5
6vtkRenderer ren1
7ren1 SetBackground 0.1 0.2 0.4
8
9vtkRenderWindow renWin
10renWin AddRenderer ren1
11renWin SetSize 600 600
12
13# The Tk render widget
14
15set vtkw [vtkTkRenderWidget .ren -width 450 -height 450 -rw renWin]
16::vtk::bind_tk_render_widget $vtkw
17
18[[renWin GetInteractor] GetInteractorStyle] SetCurrentStyleToTrackballCamera
19
20pack $vtkw -side left -fill both -expand yes
21
22# Base text property
23
24vtkTextProperty base_text_prop
25base_text_prop SetFontSize 48
26base_text_prop ShadowOn
27base_text_prop SetColor 1.0 0.0 0.0
28base_text_prop SetFontFamilyToArial
29
30set base_scale 0.0025
31
32set base_text "This is a test"
33
34# The text actors
35
36set text_actors {}
37
38# Add a sphere
39
40proc add_sphere {} {
41    vtkSphereSource obj_source
42
43    vtkPolyDataMapper obj_mapper
44    obj_mapper SetInputConnection [obj_source GetOutputPort]
45
46    vtkActor obj_actor
47    obj_actor SetMapper obj_mapper
48    [obj_actor GetProperty] SetRepresentationToWireframe
49
50    ren1 AddActor obj_actor
51}
52
53# Add one text actor, centered
54
55proc add_one_text_actor {} {
56    global text_actors
57
58    vtkTextActor3D ia
59    lappend text_actors ia
60
61    set tprop [ia GetTextProperty]
62    $tprop ShallowCopy base_text_prop
63}
64
65# Add many text actor
66
67proc add_many_text_actors {} {
68    global text_actors
69
70    vtkColorTransferFunction lut
71    lut SetColorSpaceToHSV
72    lut AddRGBPoint 0.0 0.0 1.0 1.0
73    lut AddRGBPoint 1.0 1.0 1.0 1.0
74
75    for {set i 0} {$i < 10} {incr i} {
76        set name "ia$i"
77        vtkTextActor3D $name
78        $name SetOrientation 0 [expr $i * 36] 0
79  #      $name SetPosition [expr cos($i * 0.0314)] 0 0
80        lappend text_actors $name
81
82        set tprop [$name GetTextProperty]
83        $tprop ShallowCopy base_text_prop
84        set value [expr $i / 10.0]
85        eval $tprop SetColor [lut GetColor $value]
86    }
87
88    lut Delete
89}
90
91set scale_length 200
92frame .controls -relief groove -bd 2
93pack .controls -padx 2 -pady 2 -anchor nw -side left -fill both -expand n
94
95# Add control of text
96
97set entry_text [entry .controls.text]
98
99$entry_text insert 0 "$base_text"
100
101pack $entry_text -padx 4 -pady 4 -side top -fill x -expand n
102
103bind $entry_text <Return> {update_text_actors 0}
104bind $entry_text <FocusOut> {update_text_actors 0}
105
106# Add control of orientation
107
108set scale_orientation [scale .controls.orientation \
109        -from 0 -to 360 -res 1 \
110        -length $scale_length \
111        -orient horizontal \
112        -label "Text orientation:" \
113        -command update_text_actors]
114
115$scale_orientation set [base_text_prop GetOrientation]
116pack $scale_orientation -side top -fill x -expand n
117
118# Add control of font size
119
120set scale_font_size [scale .controls.font_size \
121        -from 5 -to 150 -res 1 \
122        -length $scale_length \
123        -orient horizontal \
124        -label "Font Size:" \
125        -command update_text_actors]
126
127$scale_font_size set [base_text_prop GetFontSize]
128pack $scale_font_size -side top -fill x -expand n
129
130# Add control of scale
131
132set scale_scale [scale .controls.scale \
133        -from 0 -to 100 -res 1 \
134        -length $scale_length \
135        -orient horizontal \
136        -label "Actor scale:" \
137        -command update_text_actors]
138
139$scale_scale set [expr $base_scale * 10000.0]
140pack $scale_scale -side top -fill x -expand n
141
142# Add control of opacity
143
144set scale_opacity [scale .controls.opacity \
145        -from 0.0 -to 1.0 -res 0.01 \
146        -length $scale_length \
147        -orient horizontal \
148        -label "Text opacity:" \
149        -command update_text_actors]
150
151$scale_opacity set [base_text_prop GetOpacity]
152pack $scale_opacity -side top -fill x -expand n
153
154# Update all text actors
155
156proc update_text_actors {dummy} {
157    global scale_orientation scale_font_size scale_scale entry_text scale_opacity
158    set orientation [$scale_orientation get]
159    set font_size [$scale_font_size get]
160    set scale [expr [$scale_scale get] / 10000.0]
161    set text [$entry_text get]
162    set opacity [$scale_opacity get]
163
164    global text_actors
165    set i 0
166    foreach actor $text_actors {
167        $actor SetScale $scale
168        $actor SetInput "$text"
169        set tprop [$actor GetTextProperty]
170        $tprop SetFontSize $font_size
171        $tprop SetOrientation $orientation
172        $tprop SetOpacity $opacity
173        incr i
174    }
175
176    renWin Render
177}
178
179# Create and add all text actors
180
181if {0} {
182    add_sphere
183    add_one_text_actor
184    ren1 ResetCamera
185} {
186    add_many_text_actors
187    ren1 ResetCamera
188
189    set cam [ren1 GetActiveCamera]
190    $cam Elevation 30
191    $cam Dolly 0.4
192}
193
194update_text_actors 0
195
196foreach actor $text_actors {
197    ren1 AddActor $actor
198}
199
200# Interact
201
202renWin Render
203
204wm protocol . WM_DELETE_WINDOW ::vtk::cb_exit
205tkwait window .
206vtkCommand DeleteAllObject
207