[vtkusers] vtkTkRenderWidget in interpreter created within C++

Oladele Martins oladele_martins at hotmail.com
Tue Dec 5 12:47:53 EST 2000


To whom it may concern.

I mailed the vtkusers mailing list concerning some problems I had  usig 
vtkTkRenderWidget. I wanted to create a Tcl-interpreter within some C code 
which should load a Tcl-script defining my user interface. I wanted to embed 
a vtkTkRenderWidget into this interface in order to display some actors I 
had created within the C code (See the November 2000 archive of the vtk 
mailing list for a detailed description of my problem). I received some help 
from Xenios Papademetris who mailed me a few excerpts of some code he had 
written. I was able to combine these to a small C programm which solved my 
problems. Should anyone encounter similar problems they may find it helpful. 
I have included the code and the Tcl-script in this mail.

To succesfully run the program you will need to build the vtktcl dynamic 
library located in your /vtk3.1/tcl/ directory. Be sure to use the 
--with-tkwidget option when building or the program will not work. This is 
true for UNIX systems - I am not sure about windows. You will also have to 
adjust the path to this library appropriately when loading it in the 
app_init() function.
The header vtkTclUtil.h which needs to be included is located in the 
/vtk3.1/common directory. It is necessary for using 
vtkTclGetPointerFromObject().

I hope you will find this helpful.

Oladele.



#include	<tcl.h>
#include	<tk.h>
#include	<stdio.h>

#include	"vtk.h"
#include	"vtkTclUtil.h"


int app_init(Tcl_Interp *interp);
int testVtkStuff(Tcl_Interp *interp);



int	main(int argc, char **argv)
{
	Tk_Main(argc, argv, app_init);

	return 0;
}



int app_init(Tcl_Interp *interp)
{

	if(Tcl_Init(interp) == TCL_ERROR)
		return TCL_ERROR;

	if(Tk_Init(interp) == TCL_ERROR)
		return TCL_ERROR;

	// dynamically load vtk library. VERY IMPORTANT !!!
	Tcl_GlobalEval(interp,"catch {load /vtk3.1/tcl/vtktcl}");

// Tcl_SetVar(interp,"tcl_rcFileName","~/.vtkrc",TCL_GLOBAL_ONLY);


	testVtkStuff(interp);

	Tcl_EvalFile(interp,"test.tcl");

	return 0;
}



int testVtkStuff(Tcl_Interp *interp)
{
	int error=0;
	char buffer[400];

	// create renderer
	sprintf(buffer,"vtkRenderer renderer");
	Tcl_GlobalEval(interp,buffer);

	// create renderWidget
	sprintf(buffer,"vtkTkRenderWidget .rwdgt -width 300 -height 300");
	int a=Tcl_GlobalEval(interp,buffer);

	if (a!=TCL_OK)
		return TCL_ERROR;

	// get renderWindow from renderWidget
	sprintf(buffer,"set renWin [ .rwdgt GetRenderWindow]");
	Tcl_GlobalEval(interp,buffer);

	// add renderer to renderWindow
	sprintf(buffer,"$renWin AddRenderer renderer");
	Tcl_GlobalEval(interp,buffer);

	// create interactor
	sprintf(buffer,"vtkRenderWindowInteractor iren");
	Tcl_GlobalEval(interp,buffer);

	// link renderWindow to interactor
	sprintf(buffer,"iren SetRenderWindow $renWin");
	Tcl_GlobalEval(interp,buffer);

	// get pointer to renderer
	vtkRenderer *renderer = (vtkRenderer 
*)(vtkTclGetPointerFromObject("renderer","vtkRenderer",interp,error));
	if (error==1)
	{
		return TCL_ERROR;
	}


	renderer->SetBackground( 0.0, 0.0, 1.0 );

	// get renderWindow from renderer
	vtkRenderWindow *renderWindow=renderer->GetRenderWindow();

	// create an actor
	vtkCubeSource *cubeSource = vtkCubeSource::New();

	cubeSource->SetXLength( 1.0 );
	cubeSource->SetYLength( 1.0 );
	cubeSource->SetZLength( 1.0 );

	vtkPolyDataMapper *cubeMapper = vtkPolyDataMapper::New();

	cubeMapper->SetInput( cubeSource->GetOutput() );

	vtkActor *cubeActor = vtkActor::New();

	cubeActor->SetMapper( cubeMapper );

	// add actor to renderer
	renderer->AddActor( cubeActor );

	return TCL_OK;
}




#######################################################

test.tcl

This Tcl-script defines a primitive interface.

#######################################################


frame .mbar -relief raised -bd 1

menubutton .mbar.file		-text File      -menu .mbar.file.menu		-underline 0

menu .mbar.file.menu
.mbar.file.menu add command -label Exit	-command exit

pack .mbar.file -side left
pack .mbar -side top -fill x
pack .rwdgt -expand 1 -fill both

$renWin Render
iren Start



_____________________________________________________________________________________
Get more from the Web.  FREE MSN Explorer download : http://explorer.msn.com





More information about the vtkusers mailing list