0% found this document useful (0 votes)
103 views39 pages

Chapter 1: Introduction To TCL: Innovation Intelligence

Uploaded by

Chieu Hua
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
103 views39 pages

Chapter 1: Introduction To TCL: Innovation Intelligence

Uploaded by

Chieu Hua
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 39

Chapter 1: Introduction to TCL

Innovation
Intelligence®

1
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.

TCL/Tk Introduction

• About TCL

• About Tk

• Basic TCL Syntax

• TCL Command Overview

• Tk Basic Commands

2
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.

TCL/Tk Introduction – About TCL and Tk

About TCL
• Simple and programmable syntax
• Can be used as standalone application or embedded in programs
• Open source
• Interpreted language
• New TCL commands can be implemented using C language

About Tk
• Graphical user interface (GUI) toolkit
• Tk adds about 35 TCL commands
• Create and manipulate widgets
• Widget is a window in GUI with particular appearance and behavior

3
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.

TCL/Tk Introduction – Basic Tcl Syntax

• First item of a command line is a command name.

• The words in the command line are separated by one or more spaces.

• Words can be grouped with double quotes or curly braces.

• Commands are terminated with new line or semi-colon.

• A word starting with a dollar sign ($) must be a variable name. The string
will be replaced by the value of the variable.

• Words enclosed within square brackets must be a legal Tcl command.


The strings would be replaced by the results of evaluating the command.

4
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.

TCL/Tk Introduction – TCL Command Overview


• Text Output

• Variables and Variable Substitution

• Expressions

• Command Substitution

• Comparisons and Loops

• Lists

• Arrays

• Strings

• Procedures

• Namespaces

5
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.

TCL Command Overview – Text Output

• Print a string using puts command

Input: puts HyperWorks

Output: HyperWorks

• If string is more than one word, the string must be enclosed in either
double quotes (“ “) or braces ({ })

Input: puts “This is an example with quotes”


puts {This is an example with braces}

Output: This is an example with quotes


This is an example with braces

6
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.

TCL Command Overview – Text Output

• TCL command terminated with newline or semicolon ;

• Comments are designated with # at beginning of line or after semicolon

Input: # An example using a semicolon


puts “This is line 1”; puts {This is line 2}; #Note after the ;

Output: This is line 1


This is line 2

7
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.

TCL Command Overview – Variables

• Variables do not need to be declared before using

• Create variables using set command

• Delete variables using unset command

• Variable substitution done using $ to access value stored in variable

Input: set software "HyperWorks"


puts "The software we are using is $software"

Output: The software we are using is HyperWorks

8
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.

TCL Command Overview – Variables


• Variable substitution inside strings
• Difference between using double quotes and braces

• Variable substitution with double quotes


• Using \ before variable results in literal value being printed

Input: set Z Albany


set Z_LABEL “The Capitol of New York is: “
puts “$Z_LABEL $Z” ; # Prints the value of Z
puts “$Z_LABEL \$Z”; #Prints a literal $Z instead of the value
of Z
Output Albany
The Capitol of New York is:
The Capitol of New York is: Albany
The Capitol of New York is: $Z

9
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.

TCL Command Overview – Variables


• Variable substitution with braces
• Braces disable the substitution of variables with braces

Input: puts "$Z_LABEL $Z"


puts {$Z_LABEL $Z}

Output: The Capitol of New York is: Albany


$Z_LABEL $Z

10
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.

TCL Command Overview – Variables


• Multiple variable substitution

Input: set month 2


set day 3
set year 09
set date "$month:$day:$year"
puts $date

Output: 2:3:09

• Using eval command

Input: set foo “puts hi”


eval $foo

Output: hi

11
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.

TCL Command Overview – Expressions


• Expressions are evaluated using expr command

• Mathematical and rational expressions allowed

• Expressions consist of operands, operators, parenthesis


• White space between these are allowed

12
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.

TCL Command Overview – Expressions


• Operands can be:
• Numerical values (integer or floating-point)
• TCL variable using $
• String enclosed in double quotes or braces
• TCL command enclosed in brackets

• Operators can be:


• - + ~ ! Unary minus, unary plus, bit-wise NOT, logical NOT.
• * / % Multiply, divide, remainder.
• + - Add and subtract.
• << >> Left and right shift.
• < > <= >= Boolean less, greater, less than or equal, and greater than or equal.
• == != Boolean equal and not equal.
• & Bit-wise AND.
• ^ Bit-wise exclusive OR.
• | Bit-wise OR.
• && Logical AND.
• || Logical OR.

13
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.

TCL Command Overview – Expressions


• Rational Expression
• 0 is false; 1 is true

Input: expr 0 == 1

Output: 0

Input: expr 1 == 1

Output: 1

• Mathematical Expression

Input: expr 4 + 5

Output: 9

14
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.

TCL Command Overview – Expressions


• Mathematical functions supported in expressions:
abs cosh log sqrt
acos double log10 srand
asin exp pow tan
atan floor rand tanh
atan2 fmod round wide
ceil hypot sin
cos int sinh

Input: expr sin(2)

Output: 0.909297

15
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.

TCL Command Overview – Command Substitution


• Square brackets [ ] are used to achieve command substitution

• Text between [ ] are evaluated and its result is substituted in its place

Input: set my_height 6.0

puts "If I was 2 inches taller, I would be [expr


$my_height + (2.0 / 12.0)] feet tall"

Output: If I was 2 inches taller, I would be 6.16667 feet tall

16
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.

TCL Command Overview – Comparisons & Loops


• Decision making commands
• If-else statements
• Switch statement

• Looping statements
• while
• for
• foreach

• Commands can alter the flow of execution in response to some condition

17
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.

TCL Command Overview – Comparisons & Loops


• Using if-else statements
• if test1 body1 ?elseif test2 body2 elseif ...? ?else bodyn?

Input: set k 35
if {$k == 35} {
puts "Handling is good."
} elseif {$k == 20} {
puts "Ride is good."
} else {
puts "I am not sure of the quality of ride or
handling."
}

Output: Handling is good.

• Indentations are not required, but they make it easier to read the code

18
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.

TCL Command Overview – Comparisons & Loops


• Using switch statements
• switch ?options? string {pattern body ?pattern
body ...?}

Input: set num_legs 4


switch $num_legs {
2 {puts "It could be a human."}
4 {puts "It could be a cow."}
6 {puts "It could be an ant."}
8 {puts "It could be a spider."}
default {puts "It could be anything."}
}

Output: It could be a cow.

19
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.

TCL Command Overview – Comparisons & Loops


• Using for statements
• for init test reinit body

Input: for {set i 0} {$i < 5} {incr i 1} {


puts "In the for loop, and i == $i"
}

Output: In the for loop, and i == 0


In the for loop, and i == 1
In the for loop, and i == 2
In the for loop, and i == 3
In the for loop, and i == 4

20
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.

TCL Command Overview – Comparisons & Loops


• Using while statements
• while test body
Input: set i 0
while {$i < 5} {
puts "In the while loop, and i == $i"
incr i 1
}

Output: In the while loop, and i == 0


In the while loop, and i == 1
In the while loop, and i == 2
In the while loop, and i == 3
In the while loop, and i == 4

21
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.

TCL Command Overview – Comparisons & Loops


• Using foreach statements
• foreach varName list body

Input: foreach vowel {a e i o u} {


puts "$vowel is a vowel"
}

Output: a is a vowel
e is a vowel
i is a vowel
o is a vowel
u is a vowel

22
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.

TCL Command Overview – Lists


• Simple way to group items

• Deal with the collection of items as a single entity

• Lists can be defined by:


• A string:
• set lst “item1 item2 item3”
• A variable to be a list of values
• set lst {{item1} {item2} {item3}}
• Using the split command
• set lst [split “item1.item2.item3” “.”]
• Using the list command
• set lst [list “item1” “item2” “item3”]

23
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.

TCL Command Overview – Lists


• An individual member of list can be accessed with lindex

Input: set simple_list “John Joe Mary Susan”


puts [lindex $simple_list 0]
puts [lindex $simple_list 2]

Output: John
Mary

• List indexing is zero-based

• Lists can be iterated through using the foreach command

24
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.

TCL Command Overview – Lists


• The length of a list can be determined with llength

Input: set names {Canada India USA UK}


puts “List length is: [llength $names]”

Output: List length is: 4

• A value can be inserted into a list using linsert

Input: set names {Canada India USA UK}


set newnames [linsert $names 2 China]
puts “New list is: $newnames”

Output: New list is: Canada India China USA UK

25
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.

TCL Command Overview – Lists


• A list can be appended to using the lappend command

Input: set names {Canada India USA UK}


set newnames [lappend names China]
puts “New list is: $newnames”

Output: New list is: Canada India USA UK China

• Notice how with lappend the name of a variable containing the list
(names) is used rather than the list itself ($names).

26
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.

TCL Command Overview – Arrays


• Unlike arrays in many other languages, Tcl arrays are indexed by
keywords. The keywords can be easy to remember strings or integers,
just as you like.

• The values of the array elements can also be strings or numbers. A Tcl
array is created when you assign the first array element:
Input: set myArray(foo) "bar"
puts $myArray(foo)

Output: bar

• The array command is used in Tcl to manipulate array data.


Input: array set fruitColors {tomato red banana yellow}
puts $fruitColors(tomato)

Output: red

27
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.

TCL Command Overview – Strings


• Strings are a set of alphanumeric characters stored and manipulated together

• All data items in Tcl, including numeric values, are treated as strings.
• They are treated as other data types only as needed.
• This makes string manipulation and the associated commands very important and
frequently utilized.

Input:
set str "This is Canada"
puts "The string is: $str"
puts "The length of the string is: [string length $str]"
puts "The character at index 3 is: [string index $str 3]"
puts "The characters from index 3 through end are: [string range $str 3 end]"
puts "The index of the first occurrence of letter \"i\" is: [string first i $str]“

Output:
This is Canada
The string is: This is Canada
The length of the string is: 14
The character at index 3 is: s
The characters from index 3 through end are: s is Canada
28
The index of the first occurrence of letter “i” is: 2
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.

TCL Command Overview – Procedures


• TCL procedures similar to functions in C

• Procedures may take arguments and may return values

• The basic syntax for defining a procedure is:

• proc name argList body

• Once a procedure is created, it is considered to be a command

• Called using its name, followed by a value for each of its arguments

• By default, the return value from a procedure is the result of the last
command in its body

• However, to return another value, the return command may be used

29
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.

TCL Command Overview – Procedures


Input: proc sum_proc {a b} { Output: The sum is 26
return [expr $a + $b] The magnitude of 3 is 3
} The magnitude of -2 is 2
proc magnitude {num} {
if {$num > 0} {
return $num
}
set num [expr $num * (-1)]
return $num
}
set num1 12
set num2 14
set sum [sum_proc $num1 $num2]
puts "The sum is $sum"
puts "The magnitude of 3 is [magnitude 3]"
puts "The magnitude of -2 is [magnitude -2]"

30
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.

TCL Command Overview – Namespace


• A namespace is a collection of commands and variables.

• Create and manipulate contexts for commands and variables

• It encapsulates the commands and variables to ensure that they won't


interfere with the commands and variables of other namespaces.

• Tcl has always had one such collection, which we refer to as the global
namespace.

31
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.

TCL Command Overview – Namespace


• The namespace eval command lets • Creates a new namespace
you create new namespaces. For containing the variable num and
example, the procedure bump.
namespace eval Counter {
• The commands and variables
namespace export bump
in this namespace are separate
variable num 0
from other commands and
proc bump {} {
variables in the same program.
variable num
incr num • If there is a command named
} bump in the global namespace,
} for example, it will be different
from the command bump in the
Counter namespace.

32
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.

TCL/Tk Introduction – Tk Basic Commands


• tk_getOpenFile

• tk_getSaveFile

• tk_chooseDirectory

• tk_messageBox

33
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.

Tk Basic Commands - tk_getOpenFile


• tk_getOpenFile pops up a dialog box for the user to select a file to
open
• Associated with the Open command in the File menu
• Its purpose is for the user to select an existing file only
• This dialog box does not open a file, it simply returns the filename so that
it can be used in your script.

34
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.

Tk Basic Commands - tk_getOpenFile

Input: set filename [tk_getOpenFile]


puts $filename

Output:

C:/Documents and Settings/training/My Documents/autosave.mvw

35
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.

Tk Basic Commands - tk_getOpenFile


• Additional options can be used with
the following format:
tk_getOpenFile ?option value ...?

• Example with option –filetypes

set types {
{{Text Files} {.txt} }
{{TCL Scripts} {.tcl}}
{{All Files} * }
}

set filename [tk_getOpenFile -filetypes $types]

36
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.

Tk Basic Commands - tk_getSaveFile


• tk_getSaveFile pops up dialog box for user to select a file to save

• Associated with the Saveas command in the File menu

• This dialog box does not save a file, it simply returns the filename so that it can be
used in your script

• Behaves the same way as tk_getOpenFile procedure


• Have the same options

• Example using –title option


Input: set filename [tk_getSaveFile –title “Select a File”]
puts $filename
Output: C:/Documents and Settings/training/My Documents/test.txt

37
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.

Tk Basic Commands - tk_chooseDirectory


• tk_chooseDirectory pops up a dialog box for the user to select a
directory

• Allows user to set a directory name to a variable to be used in a script

Input: set dirname [tk_chooseDirectory]

puts $dirname

Output: C:/temp

38
Copyright © 2012 Altair Engineering, Inc. Proprietary and Confidential. All rights reserved.

Tk Basic Commands - tk_messageBox


• tk_messageBox procedure creates and displays a message window with
an application-specified message, an icon, and a set of buttons.
• The following example illustrates the tk_messageBox command with the
message option.
• This option allows you to specify the message to be displayed in the
message box.
• Input: tk_messageBox -message "This is the message to be displayed"
• Output:

39

You might also like