Detailed example - Tcl/Tk extension creation

Here is a step by step example of how to create a Tcl/Tk extension. In this example we will create the file cmas2d.tcl, so we will be extending the capabilities of the cmas2d problem type. The file cmas2d.tcl has to be placed inside the cdmas2d Problem Type directory.

Note: The cmas2d problem type calculates the center of mass of a 2D surface. This problem type is located inside the Problem Types directory, in the GiD directory.

In this example, the cmas2d.tcl creates a window which appears when the problem type is selected.

Window created in the cmas2d.tcl example file

This window gives information about the location, materials and conditions of the problem type. The window has two buttons: CONTINUE lets you continue working with the cmas2d problem type; RANDOM SURFACE creates a random 2D surface in the plane XY.

What follows is the Tcl code for the example. There are three main procedures in the cmas2d.tcl file:

proc InitGIDProject {dir } {

set materials [GiD_Info materials]

set conditions [GiD_Info conditions ovpnt]

CreateWindow $dir $materials $conditions

}

This is the main procedure. It is executed when the problem type is selected. It calls the CreateWindow procedure.

proc CreateWindow {dir mat cond} {

set w .gid.win_example

InitWindow $w [= "CMAS2D.Tcl - Example tcl file"] ExampleCMAS2D "" "" 1

frame $w.top

label $w.top.title_text -text [= "Tcl window example for CMAS2D problem type"]

frame $w.information -relief ridge -bd 2

label $w.information.path -text [= "Problem Type path: %s " $dir]

label $w.information.materials -text [= "Available materials: %s" $mat]

label $w.information.conditions -text [= "Available conditions: %s" $cond]

frame $w.bottom

button $w.bottom.start -text [= "CONTINUE"] -height 1 -width 14 -command [list destroy $w]

button $w.bottom.random -text [= "RANDOM SURFACE"] \

-height 1 -width 20 -command [list CreateRandomSurface $w]

pack $w.top.title_text -pady 10

pack $w.information.path $w.information.materials \

$w.information.conditions -side top -anchor w

pack $w.bottom.start $w.bottom.random -side left -anchor center

pack $w.top

pack $w.information -expand yes -fill both

pack $w.bottom -side top -padx 6 -pady 10 -ipady 2

}

This procedure creates the window with information about the path, the materials and the conditions of the project. The window has two buttons: if CONTINUE is pressed the window is dismissed; if RANDOM SURFACE is pressed, it calls the CreateRandomSurface procedure.

proc CreateRandomSurface {w} {

set ret [tk_dialogRAM $w.dialog [= "Warning"] \

[= "Warning: this will create a nurbs surface in your current project"] "" 1 [= "Ok"] [= "Cancel"]]

if {$ret ==0} {

Create_surface

destroy $w

}

}

This procedure is called when the RANDOM SURFACE button is pressed. Before creating the surface, a dialog box asks you to continue with or cancel the creation of the surface. If the surface is to be created, the Create_surface procedure is called. Then, the window is destroyed.

proc Create_surface {} {

set a_x [expr rand()*10]

set a_y [expr rand()*10]

set b_x [expr $a_x + rand()*10]

set b_y [expr $a_y + rand()*10]

set c_x [expr $b_x + rand()*10]

set c_y [expr $b_y - rand()*10]

if {$a_y < $c_y} {

set d_y [expr $a_y - rand()*10]

set d_x [expr $a_x + rand()*10]

} else {

set d_y [expr $c_y - rand()*10]

set d_x [expr $c_x - rand()*10]

}

GiD_Process escape escape escape escape

GiD_Process geometry create line \

$a_x,$a_y,0.000000tol0.176991 \

$b_x,$b_y,0.000000tol0.176991 \

$c_x,$c_y,0.000000tol0.176991 \

$d_x,$d_y,0.000000tol0.176991 \

close

GiD_Process escape escape escape escape

GiD_Process geometry create NurbsSurface Automatic 4 escape

GiD_Process zoom frame escape escape escape escape

}

A 2D surface (a four-sided 2D polygon) is created. The points of this surface are chosen at random.