-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcsdl_shapes.itcl
executable file
·272 lines (229 loc) · 7.17 KB
/
csdl_shapes.itcl
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# -----------------------------------*-Tcl-*--------------------------
#
# shapes.itcl
#
# These Incr Tcl classes define the shapes of transmission
# line cross section structures.
#
# Bob Techentin
# January 23, 2000
#
# Copyright 2000-2004 Mayo Foundation. All Rights Reserved.
# $Id: csdl_shapes.itcl,v 1.3 2004/02/09 18:56:01 techenti Exp $
#
# --------------------------------------------------------------------
package require Itcl
package require units
package provide csdl 1.0.1
# --------------------------------------------------------------------
#
# itcl::class Shape
#
# Defines a shape of a structure in a transmission line
# cross section. While we can't exactly declare a
# virtual class, we can document the expected
# functionality of an object of type Shape.
#
# A shape doesn't know _where_ it is, but it does
# know its height and width and color.
#
# Shapes don't do much interesting, but they can accept
# a visitor class which can do interesting things like
# draw them.
#
# options
#
# -color
# Each shape has an intrinsic color, which is defined
# by the object which creates it. Note that some simulators,
# (i.e., BEM MMTL) depend upon color to properly identify
# the type of structure. (blue==ground)
#
# -description
# A text description, set by the creator. This text is
# displayed when the Shape draws itself. The creator
# would typically use this description to display the
# name and/or attributes of the structure.
#
# methods
#
# width
# height
# area
# circumference
# These methods return the width, height, area, or
# circumference of the shape, as a floating point number,
# scaled to the default length units, as defined in the variable
# ::units::default(Length)
#
# These are useful for drawing routines which operate
# on lists of Shapes. Since the caller doesn't know the
# specific gemoetry of any given shape, it can just ask
# the shape for the amount of space that it needs.
#
# --------------------------------------------------------------------
itcl::class Shape {
public variable name ""
public variable color ""
public variable description ""
method accept { visitor x y } {
# Cleverly call a visitor based on our class name
scan [info class] "::%s" myClass
$visitor visit$myClass $this $x $y
}
method width {}
method height {}
method area {}
method circumference {}
}
itcl::class Rectangle {
inherit Shape
public variable width 0.0
public variable height 0.0
constructor {args} {
eval configure $args
}
method width {}
method height {}
method area {}
method circumference {}
}
itcl::class Trapezoid {
inherit Shape
public variable topWidth 0.0
public variable bottomWidth 0.0
public variable height 0.0
constructor {args} {
eval configure $args
}
method width {}
method height {}
method area {}
method circumference {}
}
itcl::class Circle {
inherit Shape
public variable diameter 0.0
constructor {args} {
eval configure $args
}
method width {}
method height {}
method area {}
method circumference {}
}
itcl::class Layer {
inherit Shape
public variable thickness 0.0
constructor {args} {
eval configure $args
}
method width {}
method height {}
method area {}
method circumference {}
}
# --------------------------------------------------------------------
# Rectangle methods
# --------------------------------------------------------------------
itcl::configbody Rectangle::width {check_length $width "width"}
itcl::configbody Rectangle::height {check_length $height "height"}
itcl::body Rectangle::width {} {return [length $width]}
itcl::body Rectangle::height {} {return [length $height]}
itcl::body Rectangle::area {} {
return [expr {[length $width]*[length $height]}]
}
itcl::body Rectangle::circumference {} {
return [expr {2.0*([length $width]+[length $height])}]
}
# --------------------------------------------------------------------
# Trapezoid methods
# --------------------------------------------------------------------
itcl::configbody Trapezoid::topWidth {check_length $topWidth "top width"}
itcl::configbody Trapezoid::bottomWidth {check_length $bottomWidth "bottom width"}
itcl::configbody Trapezoid::height {check_length $height "height"}
itcl::body Trapezoid::width {} {
if {[length $topWidth]>[length $bottomWidth]} {
return [length $topWidth]
} else {
return [length $bottomWidth]
}
}
itcl::body Trapezoid::height {} {return [length $height]}
itcl::body Trapezoid::area {} {
set h [length $height]
set tw [length $topWidth]
set bw [length $bottomWidth]
return [expr {$h * ($tw + $bw)/2.0}]
}
itcl::body Trapezoid::circumference {} {
#
# +-------------+ +
# / \ | h = height
# / \ |
# +-------------------+ +
# +--+ base
set h [length $height]
set tw [length $topWidth]
set bw [length $bottomWidth]
set base [expr {0.5*abs($tw-$bw)}]
set sidelength [expr {sqrt($h*$h + $base*$base)}]
return [expr {$tw + $bw + 2.0*$sidelength}]
}
# --------------------------------------------------------------------
# Circle methods
# --------------------------------------------------------------------
itcl::configbody Circle::diameter {check_length $diameter "diameter"}
itcl::body Circle::width {} {return [length $diameter]}
itcl::body Circle::height {} {return [length $diameter]}
itcl::body Circle::area {} {
set pi 3.14159265358979323846
set d [length $diameter]
return [expr {$pi * $d*$d / 4.0}]
}
itcl::body Circle::circumference {} {
set pi 3.14159265358979323846
set d [length $diameter]
return [expr {$pi * $d}]
}
# --------------------------------------------------------------------
# Layer methods
# --------------------------------------------------------------------
itcl::configbody Layer::thickness {check_length $thickness "thickness"}
itcl::body Layer::width {} {
# as far as anybody checking on the width of all the
# cross section structures is concerned, the layer
# width is 2x the thickness. But we draw it "wide"
# when compared to the width of all the other layers
# and structures.
return [expr {[length $thickness]*2}]
}
itcl::body Layer::height {} {return [length $thickness]}
set ::units::default(Length) meter
set ::units::default(Time) second
proc check_length { value name } {
global units::default
if { $value == "" } {
error "'$name' value is required"
}
if { [catch {units::convert $value $units::default(Length)} result] } {
error "Invalid Dimension '$name': $result"
}
if { $result < 0.0 } {
error "Invalid '$name': Negative dimensions are not allowed"
}
}
proc length { value } {
# add default units, if necessary
if { [string is double $value] } {
append value $units::default(Length)
}
units::convert $value $units::default(Length)
}
proc time { value } {
# add default units, if necessary
if { [string is double $value] } {
append value $units::default(Time)
}
units::convert $value $units::default(Time)
}