File: TestTextActor3D.tcl

package info (click to toggle)
vtk 5.0.4-1.1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 51,084 kB
  • ctags: 70,426
  • sloc: cpp: 524,166; ansic: 220,276; tcl: 43,377; python: 14,037; perl: 3,102; java: 1,436; yacc: 1,033; sh: 339; lex: 248; makefile: 197; asm: 154
file content (206 lines) | stat: -rw-r--r-- 4,735 bytes parent folder | download | duplicates (12)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package require vtk
package require vtkinteraction

# Renderer, renwin

vtkRenderer ren1
ren1 SetBackground 0.1 0.2 0.4

vtkRenderWindow renWin
renWin AddRenderer ren1
renWin SetSize 600 600

# The Tk render widget

set vtkw [vtkTkRenderWidget .ren -width 450 -height 450 -rw renWin]
::vtk::bind_tk_render_widget $vtkw

[[renWin GetInteractor] GetInteractorStyle] SetCurrentStyleToTrackballCamera

pack $vtkw -side left -fill both -expand yes

# Base text property

vtkTextProperty base_text_prop
base_text_prop SetFontSize 48
base_text_prop ShadowOn
base_text_prop SetColor 1.0 0.0 0.0
base_text_prop SetFontFamilyToArial

set base_scale 0.0025

set base_text "This is a test"

# The text actors

set text_actors {}

# Add a sphere

proc add_sphere {} {
    vtkSphereSource obj_source

    vtkPolyDataMapper obj_mapper
    obj_mapper SetInputConnection [obj_source GetOutputPort]

    vtkActor obj_actor
    obj_actor SetMapper obj_mapper
    [obj_actor GetProperty] SetRepresentationToWireframe

    ren1 AddActor obj_actor
}

# Add one text actor, centered

proc add_one_text_actor {} {
    global text_actors

    vtkTextActor3D ia
    lappend text_actors ia

    set tprop [ia GetTextProperty]
    $tprop ShallowCopy base_text_prop
}

# Add many text actor

proc add_many_text_actors {} {
    global text_actors

    vtkColorTransferFunction lut
    lut SetColorSpaceToHSV
    lut AddRGBPoint 0.0 0.0 1.0 1.0
    lut AddRGBPoint 1.0 1.0 1.0 1.0

    for {set i 0} {$i < 10} {incr i} {
        set name "ia$i"
        vtkTextActor3D $name
        $name SetOrientation 0 [expr $i * 36] 0
  #      $name SetPosition [expr cos($i * 0.0314)] 0 0
        lappend text_actors $name

        set tprop [$name GetTextProperty]
        $tprop ShallowCopy base_text_prop
        set value [expr $i / 10.0]
        eval $tprop SetColor [lut GetColor $value]
    }

    lut Delete
}

set scale_length 200
frame .controls -relief groove -bd 2
pack .controls -padx 2 -pady 2 -anchor nw -side left -fill both -expand n

# Add control of text

set entry_text [entry .controls.text]

$entry_text insert 0 "$base_text"

pack $entry_text -padx 4 -pady 4 -side top -fill x -expand n

bind $entry_text <Return> {update_text_actors 0}
bind $entry_text <FocusOut> {update_text_actors 0}

# Add control of orientation

set scale_orientation [scale .controls.orientation \
        -from 0 -to 360 -res 1 \
        -length $scale_length \
        -orient horizontal \
        -label "Text orientation:" \
        -command update_text_actors]

$scale_orientation set [base_text_prop GetOrientation]
pack $scale_orientation -side top -fill x -expand n

# Add control of font size

set scale_font_size [scale .controls.font_size \
        -from 5 -to 150 -res 1 \
        -length $scale_length \
        -orient horizontal \
        -label "Font Size:" \
        -command update_text_actors]

$scale_font_size set [base_text_prop GetFontSize]
pack $scale_font_size -side top -fill x -expand n

# Add control of scale

set scale_scale [scale .controls.scale \
        -from 0 -to 100 -res 1 \
        -length $scale_length \
        -orient horizontal \
        -label "Actor scale:" \
        -command update_text_actors]

$scale_scale set [expr $base_scale * 10000.0]
pack $scale_scale -side top -fill x -expand n

# Add control of opacity

set scale_opacity [scale .controls.opacity \
        -from 0.0 -to 1.0 -res 0.01 \
        -length $scale_length \
        -orient horizontal \
        -label "Text opacity:" \
        -command update_text_actors]

$scale_opacity set [base_text_prop GetOpacity]
pack $scale_opacity -side top -fill x -expand n

# Update all text actors

proc update_text_actors {dummy} {
    global scale_orientation scale_font_size scale_scale entry_text scale_opacity
    set orientation [$scale_orientation get]
    set font_size [$scale_font_size get]
    set scale [expr [$scale_scale get] / 10000.0]
    set text [$entry_text get]
    set opacity [$scale_opacity get]

    global text_actors
    set i 0
    foreach actor $text_actors {
        $actor SetScale $scale
        $actor SetInput "$text"
        set tprop [$actor GetTextProperty]
        $tprop SetFontSize $font_size
        $tprop SetOrientation $orientation
        $tprop SetOpacity $opacity
        incr i
    }

    renWin Render
}

# Create and add all text actors

if {0} {
    add_sphere
    add_one_text_actor
    ren1 ResetCamera
} {
    add_many_text_actors
    ren1 ResetCamera

    set cam [ren1 GetActiveCamera]
    $cam Elevation 30
    $cam Dolly 0.4
}

update_text_actors 0

foreach actor $text_actors {
    ren1 AddActor $actor
}

# Interact

renWin Render

wm protocol . WM_DELETE_WINDOW ::vtk::cb_exit
tkwait window .
vtkCommand DeleteAllObject