Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Events in the API

5 REPLIES 5
SOLVED
Reply
Message 1 of 6
simonsson
1741 Views, 5 Replies

Events in the API

I'm working with an Inventor addin (C#), but I have problems with the events. I use the Inventor 2009 API Reference, but the syntax is in VB or something. For instance:



Public Event OnFileNew( _

ByVal DocumentType As DocumentTypeEnum, _

ByRef TemplateFileName As String, _

ByVal Context As NameValueMap, _

ByRef HandlingCode As HandlingCodeEnum _

)



In C# the "ByVal" seems to be default so I don't have to write anything there. But "ByRef" can be both "ref" and "out" it seems. My problem is to translate this syntax to C#. I managed to translate this OnFileNew event, but generally - how do I know that "ByRef TemplateFileName As String" should be "ref string TemplateFileName" and that "ByRef HandlingCode As HandlingCodeEnum" should be "out HandlingCodeEnum HandlingCode"?



It seems just to be a matter of syntax, so is there any way to autogenerate this code? I use Visual Studio 2005. First I do this:



fileUIEvents.OnFileNewDialog += new FileUIEventsSink_OnFileNewDialogEventHandler(fileUIEvents_OnFileNewDialog);



And here I get problems:



void fileUIEvents_OnFileInsertNewDialog( [PROBLEMS] );



Error 1 No overload for 'fileUIEvents_OnFileNewDialog' matches delegate 'Inventor.FileUIEventsSink_OnFileNewDialogEventHandler'
5 REPLIES 5
Message 2 of 6
Anonymous
in reply to: simonsson


My latest blog posting shows how to listen to an
event in C#.  Check out
href="http://modthemachine.typepad.com/">http://modthemachine.typepad.com

--

Brian Ekins
Autodesk Inventor API

href="http://modthemachine.typepad.com">http://modthemachine.typepad.com

 


style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
I'm
working with an Inventor addin (C#), but I have problems with the events. I
use the Inventor 2009 API Reference, but the syntax is in VB or something. For
instance:

Public Event OnFileNew( _
ByVal DocumentType As
DocumentTypeEnum, _
ByRef TemplateFileName As String, _
ByVal Context As
NameValueMap, _
ByRef HandlingCode As HandlingCodeEnum _
)

In C#
the "ByVal" seems to be default so I don't have to write anything there. But
"ByRef" can be both "ref" and "out" it seems. My problem is to translate this
syntax to C#. I managed to translate this OnFileNew event, but generally - how
do I know that "ByRef TemplateFileName As String" should be "ref string
TemplateFileName" and that "ByRef HandlingCode As HandlingCodeEnum" should be
"out HandlingCodeEnum HandlingCode"?

It seems just to be a matter of
syntax, so is there any way to autogenerate this code? I use Visual Studio
2005. First I do this:

fileUIEvents.OnFileNewDialog += new
FileUIEventsSink_OnFileNewDialogEventHandler(fileUIEvents_OnFileNewDialog);

And
here I get problems:

void fileUIEvents_OnFileInsertNewDialog(
[PROBLEMS] );

Error 1 No overload for 'fileUIEvents_OnFileNewDialog'
matches delegate
'Inventor.FileUIEventsSink_OnFileNewDialogEventHandler'
Message 3 of 6
noontz
in reply to: Anonymous

Brians links seems to be broken, and I can´t find any information on delegates in the help file or VBA objectbrowser.

 

It might be due to some esoteric self-explanatory convention within C# / COM, but for newcomers as myself a straight forward explanation on the delegate naming would be great and welcome.

 

Going through several samples and module 10 from the API virtual training videos, I asume it works like this:

 

Object.Event  :  XXEvents.OnXX(arguments)

 

Delegate :  XXEventsSink_OnXXEventHandler(handler)

 

Handler : handler(arguments){ code }

 

Is this understood correctly and if yes, is it consistent throughout the API ?

Message 4 of 6
cadull_rb
in reply to: noontz

In my experience the naming follows the convention you describe. In practice, you may not need the delegate name.

 

// C#

// keep reference to event manager

XXEvents events = ...

 

// subscribe

events.OnXX += handler

 

// unsubscribe

events.OnXX -= handler

 

From the documentation it seems we need to maintain a reference to the event manager XXEvents so that garbage collection does not remove the subscribed handlers.

 

The other catch to be aware of is it is not safe to unsubscribe during the handler execution. Doing so can cause the execution of other subscribed handlers to be skipped. I haven't tested whether this impacts all handlers of an event (e.g. Inventor and other add-ins) or if it is just limited to subscriptions from the same add-in.

 

// multiple subscriptions

events.OnXX += handler1

events.OnXX += handler2

 

// in hander1

events.OnXX -= handler1

 

// handler2 is not executed

 

Regards,

cadull

Message 5 of 6
noontz
in reply to: cadull_rb

Thanks for the reply..

 

Your right :In practice I don´t need the names as VS pops them up. ( I was studying before executing )

 

& thanks for the additional information.. It will most likely save me days of confusion later on!

Message 6 of 6
dba78
in reply to: cadull_rb

The GC had made me Headaches. Holding an explicite Reference to the Document.DocumentEvents Object fixed it!

Never had  figured this out by myself 🙂

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report