Event Handler

Event Handler

sragan
Collaborator Collaborator
2,944 Views
2 Replies
Message 1 of 3

Event Handler

sragan
Collaborator
Collaborator

I'm still somewhat of a beginner, and I've been trying all morning to get my first event handler working, with no luck.   I've been following this example code:

 

 

  IExternalEventHandler handler_event
    = new ExternalEventMy();
 
  ExternalEvent exEvent
    = ExternalEvent.Create( handler_event );

The external event handler class implements the actions I need to execute in a valid Revit API context:

  class ExternalEventMy : IExternalEventHandler
  {
    public void Execute(UIApplication uiapp)
    {
      UIDocument uidoc = uiapp.ActiveDocument;
      if( null == uidoc )
      {
        return; // no document, nothing to do
      }
      Document doc = uidoc.Document;
      using( Transaction tx = new Transaction(doc))
      {
        tx.Start("MyEvent");
        // Action within valid Revit API context thread
        tx.Commit();
      }
    }
    public string GetName()
    {
      return "my event";
    }
  }

I can raise the external event from another thread like this:

  exEvent.Raise();

 

 

 

Which is posted here:  The Building Coder: External Events and 10 Year Forum Anniversary (typepad.com)

 

But I can't get it to compile.  It doesn't seem to recognize the Class "ExternalEventMy" on the very first line of the code. 

 

I'm trying to run this with the Macro Editor, and I thought all the code for the class ExternalEventMy should go in the main module, just after the startup regions.  But that gives a "Namespace can not be found" error message.  

 

So I tried to put it in with the Window1.xaml.cs, but then the compiler gives me a "Non-static field" error.

 

I've included a couple of screen shots and a zip of the .sln file.  Any help would be appreciated. I'm trying to trigger this event from a WPF window, if that makes any difference.  

 

 

 

 

0 Likes
Accepted solutions (1)
2,945 Views
2 Replies
Replies (2)
Message 2 of 3

jeremytammik
Autodesk
Autodesk

Welcome to the Revit API.

 

Please start with something simpler that you can compile.

 

The Building Coder provides tutorials and lots of getting started material:

 

https://thebuildingcoder.typepad.com/blog/about-the-author.html#2

 

If you want to work with macros rather than with add-ins, please take a look at the numerous YouTube tutorials on getting started with Revit macros.

 

I recommend not struggling with external events until you can easily compile and execute the step by step tutorial material.

 



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

Message 3 of 3

sragan
Collaborator
Collaborator
Accepted solution

Jeremy;

 

Maybe I overstated the beginner part a little.   I should have stated that I'm a beginner at External Events, and I thought I was probably missing something simple, which was the case.  Anyhow I seem to have it working now.

 

I finally got a Macro to put a WPF window and a Modeless Form side by side with either one triggering the same external event to modify Revit.   That was pretty neat.  But the moment of truth was when I deleted the Form just to make sure everything worked without it.

0 Likes