Revit API Forum
Welcome to Autodesk’s Revit API Forums. Share your knowledge, ask questions, and explore popular Revit API topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Run code (to add event handlers) when Revit starts

11 REPLIES 11
SOLVED
Reply
Message 1 of 12
doni49
1277 Views, 11 Replies

Run code (to add event handlers) when Revit starts

I've got an add-in that I've been working on.  I want to have it run every time an RVT file is opened or saved.

 

I know that I'll need to use onDocumentOpen and onDocumentSave (I don't remember the exact names but I can look that up).  But my qestion is more about getting THAT code to run when Revit starts -- IOW when Revit starts, it will need to run the code containing the aforementioned event handlers.



Don Ireland
Engineering Design Technician




If a reply solves your issue, please remember to click on "Accept as Solution". This will help other users looking to solve a similar issue. Thank you.


Please do not send a PM asking for assistance. That's what the forums are for. This allows everyone to benefit from the question asked and the answers given.

11 REPLIES 11
Message 2 of 12
arnostlobel
in reply to: doni49

Hello Don;

 

What you need is to implement an external application. Its interface has OnStartup and OnShutdown methods your class must define. In the OnStartup method you then subscribe to the event(s) you are interested in handling. Ideally, you should later unsubscribe them either in the OnShutdown method or even earlier if you do not need them anymore. Samples of how to implement an external application are available in the SDK as well as in the on-line help for Autodesk Revit. There are also event samples in the SDK which, I believe, use the same approach.

Arnošt Löbel
Message 3 of 12
doni49
in reply to: arnostlobel

Ok thanks.  All my efforts to search for an answer on my own (before posting my question) had brought up example code for event handling.



Don Ireland
Engineering Design Technician




If a reply solves your issue, please remember to click on "Accept as Solution". This will help other users looking to solve a similar issue. Thank you.


Please do not send a PM asking for assistance. That's what the forums are for. This allows everyone to benefit from the question asked and the answers given.

Message 4 of 12
doni49
in reply to: arnostlobel

Hmmm.  I've continued to investigate this.  But I'm still confused.  The examples I'm seeing show putting this OnStartup eventhandler within my code.  But How do I load this code in the first place?



Don Ireland
Engineering Design Technician




If a reply solves your issue, please remember to click on "Accept as Solution". This will help other users looking to solve a similar issue. Thank you.


Please do not send a PM asking for assistance. That's what the forums are for. This allows everyone to benefit from the question asked and the answers given.

Message 5 of 12
arnostlobel
in reply to: doni49

Hello Don:

OnStartup gets called automatically for all external applications installed with Revit. This is quite well described in the SDK documentation as well as in the on-line help for Revit (the API section). Please follow those resources.

If you have your application installed yet the OnStartup method does not get called, it would indicate that there is something wrong with the information you provided in the manifest file (*.addin) for your application.

Arnošt Löbel
Message 6 of 12
doni49
in reply to: arnostlobel


@arnostlobel wrote:

Hello Don:

OnStartup gets called automatically for all external applications installed with Revit.


Thanks. That explains it. I thought I needed to tell Revit to load my app upon startup.


@arnostlobel wrote:
This is quite well described in the SDK documentation as well as in the on-line help for Revit (the API section). Please follow those resources.

If you have your application installed yet the OnStartup method does not get called, it would indicate that there is something wrong with the information you provided in the manifest file (*.addin) for your application.


I've been reading through examples but I feel like I'm going in circles. I try to do something that the examples show and run into a problem. 

 

For example, before you posted your reply, I read @jeremytammik's blog entry about event handling and a RevitAddin (http://thebuildingcoder.typepad.com/blog/2011/01/automate-designoption-and-64-bit-add-in-templates.h... I downloaded the template and followed the instructions to save it to my VS2010 folder under My Documents.

 

I opened the add-in but did not build it.  Having seen what the code looks like, I did the same thing in my own project.  Then I built *MY* project.  After I started Revit, I got the following error message.  But RevitAddin1 is the name of the downloaded example that I didn't build.  And I don't see a manifest file in the Revit addins folder (the same place I put the manifest for my project).

 

Now I can't get rid of the error message and have no idea why it's even trying to load RevitAddin1.

 

RevitAddin.png



Don Ireland
Engineering Design Technician




If a reply solves your issue, please remember to click on "Accept as Solution". This will help other users looking to solve a similar issue. Thank you.


Please do not send a PM asking for assistance. That's what the forums are for. This allows everyone to benefit from the question asked and the answers given.

Message 7 of 12
arnostlobel
in reply to: doni49

Don:

 

First, to get rid of the error message, you need to go to the addin file and either remove it completely (from the Addins\2015 folder), or you need to fix the path in the <Assembly> tag inside the file.

 

Second: I think the samples Jeremy Tammik posts or re-posts on his blog may be sometimes rather complicated to start learning from. I recommend our simple walkthroughs in the help file, for example:  http://help.autodesk.com/view/RVT/2014/ENU/?guid=GUID-01F579CB-AB46-4C00-86E4-D189510D3774This particular sample demonstrates implementing an external application which  has one ribbon button that triggers an external command. You can even start without the button and the command to make it simpler yet. What’s left is the very bare minimum an external application should have, including the startup and shutdown routines.

Arnošt Löbel
Message 8 of 12
doni49
in reply to: arnostlobel


@arnostlobel wrote:

Don:

 

First, to get rid of the error message, you need to go to the addin file and either remove it completely (from the Addins\2015 folder), or you need to fix the path in the <Assembly> tag inside the file.


I thought it was something like that.  But as I said, I haven't been able to find the file to delete it.

 


@arnostlobel wrote:

Second: I think the samples Jeremy Tammik posts or re-posts on his blog may be sometimes rather complicated to start learning from. I recommend our simple walkthroughs in the help file, for example:  http://help.autodesk.com/view/RVT/2014/ENU/?guid=GUID-01F579CB-AB46-4C00-86E4-D189510D3774This particular sample demonstrates implementing an external application which  has one ribbon button that triggers an external command. You can even start without the button and the command to make it simpler yet. What’s left is the very bare minimum an external application should have, including the startup and shutdown routines.


Thanks.  Yes.  I think you could safely say "complicated".  When looking for things to learn from, I'd much rather find very simple examples that I can modify and see what effect my changes have on the code -- once I'm able to do that, my understanding of HOW to use the code takes off.

 

But the "simple" examples I've seen seem to assume that I've never done any programming at all.  I end up skimming over the things that I already understand and I guess that in the process, I miss some items that IS new to me.



Don Ireland
Engineering Design Technician




If a reply solves your issue, please remember to click on "Accept as Solution". This will help other users looking to solve a similar issue. Thank you.


Please do not send a PM asking for assistance. That's what the forums are for. This allows everyone to benefit from the question asked and the answers given.

Message 9 of 12
arnostlobel
in reply to: doni49

Don:

I think there is plenty of quite simple examples in the Revit API SDK. Assuming you had downloaded it, if you go to the "Samples" folder and do a search for IExternalAplication you should find at least 35 examples of usage. Some of examples demonstrate using events, possibly something very close to what you want.
Arnošt Löbel
Message 10 of 12
doni49
in reply to: arnostlobel

I've spent the most of my free time over the past two days looking through the SDK samples and any tutorials I can find.  But this is still not working.

 

It's telling me that DocumentSaved is not a valid method.

 

RevitAPIOnSaveEvent.png



Don Ireland
Engineering Design Technician




If a reply solves your issue, please remember to click on "Accept as Solution". This will help other users looking to solve a similar issue. Thank you.


Please do not send a PM asking for assistance. That's what the forums are for. This allows everyone to benefit from the question asked and the answers given.

Message 11 of 12
doni49
in reply to: doni49

Ok I figured it out. 

 

        private static ControlledApplication ctrlApp;
        public Result OnStartup(UIControlledApplication a)
        {
            ctrlApp = a.ControlledApplication;
            ctrlApp.DocumentSaved += new EventHandler<Autodesk.Revit.DB.Events.DocumentSavedEventArgs>(ctrlApp_DocumentSaved);
                
                //+= DocEventHandlers1.DocEvent_DocumentSaved_Handler;
            return Result.Succeeded;
        }

 



Don Ireland
Engineering Design Technician




If a reply solves your issue, please remember to click on "Accept as Solution". This will help other users looking to solve a similar issue. Thank you.


Please do not send a PM asking for assistance. That's what the forums are for. This allows everyone to benefit from the question asked and the answers given.

Message 12 of 12
Anonymous
in reply to: doni49

You'll find the events in the ControlledAplication property of the UIControlledApplication.

 

a.ControlledApplication.DocumentSaved  will get you going.

 

 

 Edit: Oh nevermind, just saw over the page that you had already figured this out.

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

Post to forums  

Forma Design Contest


Rail Community