[vtkusers] Newbie in vtkTclUtil 2

Pedro García Patrón zz00f049 at etsiig.uniovi.es
Wed Dec 12 14:32:18 EST 2001


Sorry, the example code :-) :

#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




More information about the vtkusers mailing list