QMG logo

A brief synopsis of Tcl syntax

Tcl is an interactive scripting language used to control QMG. Tcl is partnered with Tk, a set of functions for creating windows and GUI's. QMG 2.0 uses Tcl/Tk version 8.0. Tcl/Tk is a free software product originally written by J. Ousterhout and currently written and maintained by Scriptics, Inc.

The user of Tcl enters commands after the prompt sign % on the console. The result of each command is displayed on the console window. The format of commands is:

funcname arg1 ... argn
In other words a command is separated from its arguments by spaces, and the arguments are separated from each other by spaces. The end of the argument list is signaled by the end of the line. To continue a command onto a new line, use a backslash as the last character of the line.

Here is an example of a Tcl/Tk command:

set a 4
This command creates a variable a and sets it to 4. This could be either the integer 4 or the string 4. In Tcl, the user does not declare the types of variables, and there is no type system visible to the user. Internally, an object may have a certain type. The type is always convertible to and from a printable string.

The name of the variable (in this case "a") in Tcl can be fairly arbitrary strings, but you should probably stick to letters, digits, and underscores.

If an argument itself has a space character in it, then you have to enclose the argument in quotation marks or curly braces:

set a "p 4"
set a {p 4}
These commands are completely equivalent. In both cases, the result is that a is set to either a string with three characters or a list with two entries (p and 4). It is indistinguishable to the console user whether a is a 3-character string or a 2-entry list, since Tcl internally converts back and forth as needed.

Note that, unlike other languages, quotation marks do not differentiate string-literals from non-literals. The two commands set b a and set b "a" have identical meaning in Tcl/Tk.

To access the value of a variable, you must precede the name with a dollar-sign. Thus, after the commands

set a 4
set b $a
both a and b are assigned the value 4. The set command has the property that in addition to setting the variable to the value, it also returns its value, converted to a string, and prints it on your console.

In QMG, variables are used to stand for meshes and breps. It is often not desirable to convert them to string representation because the string representation can be huge if the mesh or brep is large. For this reason, QMG defines a variant of the "set" command called gmset which suppresses the conversion of the variable to a string and its subsequent display on your console.

Another example of a command is the expr command that causes a string to be evaluated as an arithmetic expression, and the value of that expression is returned. For example if you typed

set a 4
expr "$a + 19"
then the second line would return 23.

A second syntactic feature of Tcl/Tk is square-brackets. Square brackets invoke a function. Thus,

set a 4
set b [expr $a+19]
will set b to 23. A final major syntactic feature is curly-braces. Curly braces have several properties: like quotes, they suppress spaces as separators. Unlike quotes, they can be nested. In addition, unlike quotes, they also prevent square brackets from being evaluated and dollar-sign substitution.

You need curly braces to denote matrices and vectors to QMG. Here is an example. Suppose you want to set t to be a regular triangle, and t1 to be a skewed version of that triangle with the matrix [1,1,0;0,1,0] applied to it (this transformation applies a skew but no translation). The command in QMG to define a regular polygon is gmpolygon and to apply a transformation gmapply. Thus, you would type:

gmset t [gmpolygon 3]
gmset t1 [gmapply {{1 1 0} {0 1 0}} $t]
Unforunately, the following will not work:
gmset t [gmpolygon 3]
set skewamount 1
gmset t1 [gmapply {{1 $skewamount 0} {0 1 0}} $t]
The reason this fails is that (as mentioned above) curly braces prevent dollar-sign substitution, so this statement would cause the string literal $skewamount to be passed to the gmapply routine. This will result in an error, because gmapply will be unable to convert its argument to a numbers. How do you get around this problem? You need to use the list instruction in Tcl:
gmset t [gmpolygon 3]
set skewamount 1
gmset t1 [gmapply [list [list 1 $skewamount 0] [list 0 1 0]] $t]
At this point you probably should get a book to learn more! There is also information on the web.

Tcl include procedure and looping constructs to build arbitrarily complex programs. In addition, Tk makes it easy to attach a graphical user interface to Tcl scripts. QMG uses Tk for several features: the gmviz function will plot a 2D brep or mesh in a window, and the mesh generator optionally displays a window indicating its progress.

This documentation is written by Stephen A. Vavasis and is copyright ©1999 by Cornell University. Permission to reproduce this documentation is granted provided this notice remains attached. There is no warranty of any kind on this software or its documentation. See the accompanying file 'copyright' for a full statement of the copyright.

Stephen A. Vavasis, Computer Science Department, Cornell University, Ithaca, NY 14853, vavasis@cs.cornell.edu