Setting up your Revit project for inputting macro codes

I have decided to write a blog following on from Harry’s post on the Boost Your BIM blog. Here Harry explains how to set up your Revit project to use the macro codes which he is kindly sharing on his website, or any other macro code in this case. If you are still in doubt about how to set up your project, follow this tutorial for simple, step-by-step instructions.

Open up Revit and make sure you have a project open which you wish to use this code on – Although the Macro profile you set up will be saved and available to use on all future projects as well, if you follow the instructions below.

Head over to the ‘Manage’ tab on the ribbon – click on ‘Macro Manager’ – You will now see a dialogue box with several options. At the top you see the 2 tabs ‘Application’ and the active tab ‘Name of project’. This is where you make the decision to either implement a macro code for ONLY the project your working on, or wether the macro settings will be saved for the application (Revit). In this case and most others, you will be using ‘Application’..

uploaded image

Click on the ‘Application; tab – If this is the first time you are setting up a macro, you will need to create a new ‘Module’. You can think of the module as a folder containing all your files (macro codes). Click on ‘Module’ on the right hand side under the ‘Create’ options. 

uploaded image

Create a new module by giving it a name, choosing which language the code was written in (if your only using Boost Your BIM code, this will always be C#) and giving the module a short description, as shown in the image above.

uploaded image

As soon as you have created a new module within Revit, you will notice a new application opens, SharpDevelop. This is the application which Revit uses to allow users to input their own macros. This will also be the place where you will input your code in order to run your programs within Revit. 

uploaded image

Before we do anything in SharpDevelop, we need to open Revit back up and create a macro, inside of the module we just created. Make sure your Module is highlighted, and simply click on ‘Macro’ on the right side under the ‘Create’ options. A dialogue similar to the module creation will appear. Again, give your macro a name and a short description. Make sure there are no spaces or invalid characters in the name field. I would recommend using a name such as MyFirstMacro.

uploaded image

Head back to SharpDevelop, you will now see a new peice of Revit macro generated code (pictured above), if you used the name I suggeste, the new code will look like this public void MyFirstMacro() followed by a { + } inside these brackets is where we will insert our code. If you have absolutely no knowledge of programming languages, you can think of these as the front and back cover of a book, with all the information (code) inside the covers. Each new macro we add to our module will have the same set up, just with a different name.

uploaded image

After this we must enter some new values at the top of the code to indicate which libraries we are referencing when we call functions in our code. Head to the top of the code in SharpDevelop and find the text ‘using System;’ underneath this you will need to add the following, 1 statement per line. Using Autodesk.Revit.UI; Using Autodesk.Revit.DB; using Autodesk.Revit.UI.Selection; using System.Collections.Generic; using System.Linq; (Pictured above).

uploaded image

We are now ready to input our code. If you are using code from the Boost your BIM blog, you will need to highlight the following code, ensuring you leave out any code which is not highlighted in this example (Pictured above). Be sure you do not copy extra { or } parenthesis as this will result in a fail when you try to build the code. A book only has 1 front and back cover! 

uploaded image

Once you have copied the code, paste it in between the { + } parenthesis. You may now wish to try and build your code and check to see that everything is responding as it should be. Click on the ‘Build’ tab at the top of SharpDevelop and then click on ‘Build solution (F8)’. You may notice a loading bar appear in the bottom right of the application as your module is being built and checked for errors. 
uploaded image

Your code should compile, and hopefully with no errors! If this is the case then you can return to Revit and see your Module with the new Macro embedded inside as shown above. You can now highlight the macro you have just created and click ‘Run’ on the right hand side to run your macro in Revit.

If you are receiving compilation errors, you may wish to check out these 2 posts over at Boost Your BIM to help troubleshoot the problem. If you still can’t solve your error, Harry invites you to contact him to discuss it further.

Many thanks again to Harry for posting all these useful macros and helping new users understand and solve problems relating to the Revit API. Be sure to subscribe to his Boost Your BIM blog and follow him on Twitter @BoostYourBIM


Tags: , , , ,

About Ben Malone

Information Manager for BIM.Technologies in London

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: