[vtkusers] On Add Observer

Michnay Balázs michnay at freemail.hu
Tue Mar 29 10:09:45 EST 2005


Dear Nagarajan,
Here's a C++ code, which works for me. Just follow the following steps:

Create a class (CProgressObserver) with the following header:
--------------------------------

//You might don't need to include these headers, my application 
needs... 
#include "StatusControl.h"
#include "StatusProgress.h"
#include "MainFrm.h"

class CProgressObserver : public vtkCommand {
public:
static CProgressObserver* New() {
	return new CProgressObserver;
}

virtual void Execute(vtkObject* caller, unsigned long, void *callData) {
	double* dProgress = (double*)(callData);
    
                //I use a progressbar to visualize the progress...
	// set the progress using the value stored in *dProgress
	CMainFrame* pFrame = (CMainFrame*) AfxGetApp()-
>m_pMainWnd;
    CStatusProgress* setprogress = &pFrame->progress;	
	

	float i = vtkProcessObject::SafeDownCast(caller)-
>GetProgress();

	setprogress->SetPos(int(100*i));
}
};
---------------------
My .cpp file looks like this. (I don't think you'd need to create this as 
well...)
---------------------
#include "CProgressObserver.h"

void CProgressObserver::GetDocument()
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CAnalyzeItDoc)));
	return (CAnalyzeItDoc*)m_pDocument;
}
---------------------
Now you can use the class to visualize the progress.
What I do is to instantiate a object from the class we've just created 
and bind it to an event:

CProgressObserver Proc;
Reader->AddObserver(vtkCommand::ProgressEvent,&Proc);

where Reader is a vtkBmpReader and Proc is an instance of 
CProgressObserver.

I hope it was useful.
All the best,
Michnay



More information about the vtkusers mailing list