TCL/TK Tutorial
Part 2

TCL/TK Part 2: Visualizing Molecules with VTK.

Step 1: Install 3dWin4

If you did tutorial 1 you should now have vtk and tcl/tk properly installed. Now we can use it to start doing some interesting stuff. In this tutorial we will use vtk to display various molecules. For this tutorial we will first need to install some more software. We are going to need to install the program 3dwin4.

Download the following files:


Run 3Dwin_Setup.exe to install 3dWin4. After the installation is finished run Demo_Reg_Setup.exe and extract the files to the location where you installed 3dWin4 (default: C:\Program Files\3dwin4)

Browse to the 3dWin4 directory and double click on the following files:


3Dwin4 should now be properly installed.

Step 2: Get Molecule to Visualize

Now for this tutorial we will be using molecules in the Protein Database Bank (.pdb) format. There are tens of thousands of molecules already mapped and ready to use. Here are a few locations where you can retrieve some on the web:

The Protein Databank - At the protein databank you can find literally tens of thousands of molecules.

Rasmol_Structures - This is the location of several interesting molecules. Another website with a variety of molecules.

For now let's use one of my favorite molecules Caffeine

Now we need to convert the file from the .pdb format to one that vtk understands.

Under the import sections chang the file type to Protein Data Bank, and click import. Browse to the caffeine.pdb file. Next change the export type to Alias Wavefront and select export. Select a name for the exported file (caffeine.obj).

Next we will use the script molecule.tcl to load the file. You will need to modify the following line (line 14) to make sure that it points to the correct file:

sr SetFileName caffeine.obj

Now run your script (by double-clicking), and you should see something like the following image:

Following is an explanation of the code in molecule.tcl

************Begin Code***************

package require vtk
package require vtkinteraction

These load the vtk libraries.

vtkOBJReader sr

This creates a vtkOBJReader instance. vtkOBJReader is a class which reads in Alias Wavefront Object Files.

sr SetFileName caffeine.obj

Set the vtkOBJReader file to the newly created object file.

vtkPolyDataMapper stlMapper

vtkPolyDataMapper is a class that maps polygonal data (i.e., vtkPolyData) to graphics primitives. vtkPolyDataMapper serves as a superclass for device-specific poly data mappers, that actually do the mapping to the rendering/graphics hardware/software.

stlMapper SetInput [sr GetOutput]

Set the input for the Poly Data Mapper to the Object Reader.

vtkLODActor stlActor

Creates a new actor. Actors represent distinct entities in a scene.

stlActor SetMapper stlMapper

This connects the actor to the end of a visualization pipeline. Basically this connects the actor to a previously created mapper (ex. vtkPolyDataMapper).

vtkRenderer ren1
vtkRenderWindow renWin
renWin AddRenderer ren1
vtkRenderWindowInteractor iren
iren SetRenderWindow renWin

The above first creates a renderer. The renderer is what actually draws the scene. It then creates a render window. The render window is a window in which the scene will be drawn. Next it attaches the renderer to the render window. The next step is to create a RenderWindowInteractor and attach it to the renderwindow. The RenderWindowInteractor provides a mechanism for handling input like mouse actions sent to the render window. The basic render window interactor provides for being able to zoom in and out of a scene, as well as rotate the scene.

set cam1 [ren1 GetActiveCamera]
$cam1 Zoom 1.4

This puts a reference to the renderer's active camera in the variable cam1. It then sets the zoom level to 1.4

iren AddObserver UserEvent {wm deiconify .vtkInteract}
iren Initialize

This adds an Observer (aka, Event or Callback) to the interactor. When UserEvent is fired (by pressing 'U') it will create an instance of the vtkInteract window, which will allow you to enter commands to vtk. The wm is a TCL/TK reference to the window manager.

Here is the command documentation on wm deiconify:

wm deiconify window
Arrange for window to be displayed in normal (non-iconified) form. This is done by mapping the window. If the window has never been mapped then this command will not map the window, but it will ensure that when the window is first mapped it will be displayed in de-iconified form. On Windows, a deiconified window will also be raised and be given the focus (made the active window). Returns an empty string.

Note: Because we are running under window it will actually create a new instance of the vtkInteract window class and display that.

************End Code****************


C++ Tutorial
Python Tutorial
TCL/TK Tutorial

About Us

TCL/TK Tutorial
Part 1: Installation
Part 2: Molecules
Part 3: Terrain
Part 4: Interactive
Part 5: Shapes