• Industries
  • Products
  • Buy
  • Services & Support
  • Communities
  • Autodesk Inventor Customization

    Reply
    Valued Contributor
    Posts: 86
    Registered: ‎07-13-2007
    Accepted Solution

    Events in the API

    146 Views, 4 Replies
    10-03-2008 12:29 AM
    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'
    Please use plain text.
    *Brian Ekins \(Autodesk\)

    Re: Events in the API

    10-03-2008 10:04 AM 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'
    Please use plain text.
    Valued Contributor
    Posts: 61
    Registered: ‎03-21-2007

    Re: Events in the API

    01-17-2013 02:07 AM in reply to: *Brian Ekins \(Autodesk\)

    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 ?

    Please use plain text.
    Active Contributor
    cadull_rb
    Posts: 36
    Registered: ‎11-16-2011

    Re: Events in the API

    01-17-2013 02:24 PM 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

    Please use plain text.
    Valued Contributor
    Posts: 61
    Registered: ‎03-21-2007

    Re: Events in the API

    01-18-2013 07:14 AM 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!

    Please use plain text.