cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - New to the community? Learn how to post a question and get help from PTC and industry experts! X

Usage of ActionListener - C++ Toolkit

michi
5-Regular Member

Usage of ActionListener - C++ Toolkit

Hi everybody,

I'm new at this topic, so this is some newbie question. I want to know, how to use Actionlisteners. So i added a same example. There is a tester-class and beneath user_initialize(){} is the OnAfterModelDisplay methode. In user_initialize() i tried different things to get any respone in Creo. I thought i have to take something like the pointer wSession and call addactionlistener with a smartpointer to a Listener (what ever a listener is?!). But nothing worked.

The same problem i have with the other Actionlisteners, which need a methode-call addactionlistner.

Can somebody help me at this point and show me the right direction please. i really want to understand the whole concept of this. thank you!

 

<inculdes>

class tester : public pfcSessionActionListener
{
public:
	tester(){} //default constructor
	void OnAfterModelDisplay();
};

extern "C" int user_initialize(
	int argc,
	char *argv[],
	char *version,
	char *build,
	wchar_t errbuf[80])
{  

	try
	{
		pfcSession_ptr Session = pfcGetProESession ();
		wfcWSession_ptr wSession = wfcWSession::cast (Session);
		wSession->AddActionListener(smartpointer to Listener );
		return (wfcTK_NO_ERROR);
	}

}

void tester::OnAfterModelDisplay()
{
	try
	{
        //do something great
	}
}
1 ACCEPTED SOLUTION

Accepted Solutions
FV
17-Peridot
17-Peridot
(To:michi)

Hello,

tester class should be extending pfcDefaultSessionActionListener (#include "pfcSession.h").

Otherwise you'll have to implement all methods even unused ones because pfcSessionActionListener class contains only pure virtual methods  - this is C++ , nothing to do with OTK.

Pure virtual method :

      virtual void Foo() = 0;

is not an empty function (method) : 

     virtual void Foo() {;}

 

HIH.

FV.

 

 

 

 


@michi wrote:

Thank you for your answer!

The thing is, that i can't use

pfcActionListener_ptr listener = new tester();

because visual studio throws an error: "the class is abstract"

My understanding was, that the class pfcSessionActionLIstner is an abstract class and it has all actionlistner methods declared as empty functions. Then a user-defined subclass (tester) is defined and inherits everything from "pSAL" and i overload the methods i need in this new class. So my class shouldn't be abstract anymore...

So now i don't know, what i have to do, to get an user-defined class, that's not abstract 😄 Do you see any mistake i make or what thing i didn't understand?

 

oh and thanks for the information about the xobject. Really appreciate that sort of tips and hints!

 


 

View solution in original post

4 REPLIES 4
FV
17-Peridot
17-Peridot
(To:michi)

Hello,

First of all, you should not use smart pointers with pfcActionListener otherwise you are for a number of unpleasant surprises. pfcActionListener is inheriting from xobject which is a reference counting 'thingy' from 1990's... 

The calling code should be modified like this:

instead of: 

wSession->AddActionListener(smartpointer to Listener );

 

should be:

pfcActionListener_ptr listener = new tester();

wSession->AddActionListener(listener);

 

The cleaner approach would be to define pfcActionListener variable as static/global outside of user_initialize(). This would allow to call  something like this from user_terminate():

...

wSession->RemoveActionListener( g_LISTENER) 

g_LISTENER = NULL; 

 

HIH.

FV.

 

 


@michi wrote:

Hi everybody,

I'm new at this topic, so this is some newbie question. I want to know, how to use Actionlisteners. So i added a same example. There is a tester-class and beneath user_initialize(){} is the OnAfterModelDisplay methode. In user_initialize() i tried different things to get any respone in Creo. I thought i have to take something like the pointer wSession and call addactionlistener with a smartpointer to a Listener (what ever a listener is?!). But nothing worked.

The same problem i have with the other Actionlisteners, which need a methode-call addactionlistner.

Can somebody help me at this point and show me the right direction please. i really want to understand the whole concept of this. thank you!

 

<inculdes>

class tester : public pfcSessionActionListener
{
public:
	tester(){} //default constructor
	void OnAfterModelDisplay();
};

extern "C" int user_initialize(
	int argc,
	char *argv[],
	char *version,
	char *build,
	wchar_t errbuf[80])
{  

	try
	{
		pfcSession_ptr Session = pfcGetProESession ();
		wfcWSession_ptr wSession = wfcWSession::cast (Session);
		wSession->AddActionListener(smartpointer to Listener );
		return (wfcTK_NO_ERROR);
	}

}

void tester::OnAfterModelDisplay()
{
	try
	{
        //do something great
	}
}

 

 

 

michi
5-Regular Member
(To:FV)

Thank you for your answer!

The thing is, that i can't use

pfcActionListener_ptr listener = new tester();

because visual studio throws an error: "the class is abstract"

My understanding was, that the class pfcSessionActionLIstner is an abstract class and it has all actionlistner methods declared as empty functions. Then a user-defined subclass (tester) is defined and inherits everything from "pSAL" and i overload the methods i need in this new class. So my class shouldn't be abstract anymore...

So now i don't know, what i have to do, to get an user-defined class, that's not abstract 😄 Do you see any mistake i make or what thing i didn't understand?

 

oh and thanks for the information about the xobject. Really appreciate that sort of tips and hints!

 

FV
17-Peridot
17-Peridot
(To:michi)

Hello,

tester class should be extending pfcDefaultSessionActionListener (#include "pfcSession.h").

Otherwise you'll have to implement all methods even unused ones because pfcSessionActionListener class contains only pure virtual methods  - this is C++ , nothing to do with OTK.

Pure virtual method :

      virtual void Foo() = 0;

is not an empty function (method) : 

     virtual void Foo() {;}

 

HIH.

FV.

 

 

 

 


@michi wrote:

Thank you for your answer!

The thing is, that i can't use

pfcActionListener_ptr listener = new tester();

because visual studio throws an error: "the class is abstract"

My understanding was, that the class pfcSessionActionLIstner is an abstract class and it has all actionlistner methods declared as empty functions. Then a user-defined subclass (tester) is defined and inherits everything from "pSAL" and i overload the methods i need in this new class. So my class shouldn't be abstract anymore...

So now i don't know, what i have to do, to get an user-defined class, that's not abstract 😄 Do you see any mistake i make or what thing i didn't understand?

 

oh and thanks for the information about the xobject. Really appreciate that sort of tips and hints!

 


 

michi
5-Regular Member
(To:FV)

Thank you very much! learned a lot.everything works great.

After testing i read the user guide again. Now i get it. The pfcactionlistner (of course the one i want to use) class is the base class and there is also a pfcdefaultactionlistner class, which sub-classes the pfcactionlistner-base-class. and in this class there are all methodes implemented emtpy. So i don't have to do that in my code.

 

Top Tags