Use InwOpStateEvents in C#

By Xiaodong Liang

This is actually a question of the topic: how to use COM event in .NET. I searched MSDN and found two articles. One for VB.NET, another for C#.

http://support.microsoft.com/kb/810228
http://support.microsoft.com/kb/811645

The code below is a small skeleton I wrote to demo how to use in C#. I added it to the SDK sample \api\net\examples\PlugIns\APICallsCOMPlugin to test.

#region CallingCOMFromPlugin

using System.Windows.Forms;

using System;

 

//Add the namespaces

using Autodesk.Navisworks.Api.Plugins;

 

using ComApi = Autodesk.Navisworks.Api.Interop.ComApi;

using ComApiBridge = Autodesk.Navisworks.Api.ComApi;

 

namespace APICallsCOMPlugin

{

    // event class

    public class myEventSink : ComApi._InwOpStateEvents

    {

        public myEventSink() { }

 

        public void OnCurrentSceneChanged()

        {

            MessageBox.Show("OnCurrentSceneChanged");

        }

        public void OnCurrentSceneChanging()

        {

            MessageBox.Show("OnCurrentSceneChanging");

        }

        public void OnCurrentAnimationChanged()

        {

            MessageBox.Show("OnCurrentAnimationChanged");

        }

        public void OnCurrentSelectionChanged()

        {

            MessageBox.Show("OnCurrentSelectionChanged");

        }

        public void OnCustomChanged(string custom_name)

        {

            MessageBox.Show("OnCustomChanged");

        }

        public void OnCurrentViewChanged()

        {

            MessageBox.Show("OnCurrentViewChanged");

        }

        public void OnPlanViewChanged()

        {

            MessageBox.Show("OnPlanViewChanged");

        }

        public void OnSavedViewsChanged()

        {

            MessageBox.Show("OnSavedViewsChanged");

        }

        public void OnSectionViewChanged()

        {

            MessageBox.Show("OnSectionViewChanged");

        }

    }

 

    [PluginAttribute("APICallsCOMPlugin.APICallsCOMPlugin",                                    
                                "ADSK"
,
         
                     ToolTip = "Demonstrates using the COM API within a .NET API Plugin",  
                             
DisplayName =
".NET_COM")]                                   
 

    public class APICallsCOMPlugin : AddInPlugin                              

    {

        // to start the event

        private void startEvent()

        {

            ComApi.InwOpState10 state;

            state = ComApiBridge.ComApiBridge.State;

            int cookie;

 

            //create sink of the event

            myEventSink mySink = new myEventSink();

 

            // connection of the state object

            System.Runtime.InteropServices.ComTypes.IConnectionPointContainer icpc =

                (System.Runtime.InteropServices.ComTypes.IConnectionPointContainer)state;

            System.Runtime.InteropServices.ComTypes.IConnectionPoint icp;

            Guid IID_IMyEvents = typeof(ComApi._InwOpStateEvents).GUID;

 

            // find the connection point of the state event

            icpc.FindConnectionPoint(ref IID_IMyEvents, out icp);

            //delegate the event

            icp.Advise(mySink, out cookie);

            //enable the event

            state.EventsEnabled = true;

 

            //remember to unadvise when you do not require the event;

            //you would need to have another member function to end event.

 

            //icp.Unadvise(cookie);

 

        }

 

        public override int Execute(params string[] parameters)

        {

            startEvent();

 

            return 0;

        }

    }

}

#endregion

 

<

p style=”line-height: normal;margin: 0in 0in 0pt” class=”MsoNormal”> 


Comments

One response to “Use InwOpStateEvents in C#”

  1. Alexander Avatar
    Alexander

    Could you provide example how handle OnCurrentSelectionChanged event from JavaScript.

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading