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’..
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.
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.
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.
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.
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.
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).
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!
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.
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.
- Error Handling (checking for null) and the GetSheetParams macro
- Resolving common compilation errors by adding Using statements
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
Happy New Year & Boost your BIM
If you’ve been following Harry’s blog over at Boost Your BIM – You will be sure to have noticed some of the useful time saving tips he has been posting using Macros and the Revit API. You may have also been wondering how you implement these sections of code in to your project so that you are able to use the code for something useful.
Harry has just written a blog explaining how you can use his code in your Revit projects. Check out the “How to Use These Macros – A Basic Overview to the Revit API” If you follow through this post you will have the knowledge to take full advantage of all the work he has been sharing.
I have tried out some of the code which has been shared on Boost Your BIM over the last couple of months and find some of the tips have helped out in every day Revit usage to increase efficiency and productivity. One thing which I learnt from this post is “that Macros can either be stored in the project (with the source code saved in the RVT file) or on your hard drive so that they are accessible for any RVT on your computer (these are called application macros). On this blog I am creating application macros. To do this, select the “Application” tab.”
Here are a few of my favourite posts from the Boost Your BIM site – check some of them out and using the method explained above, add them to your Revit application editor. Using 3D Section Box to set Plan View’s View Range, Divide Parts at Levels, Creating a Void to Cut Family Geometry.
As you use and implement these macros into your Revit projects you will also gain a basic understanding of the Revit API as well as picking up some of the basic functions of the code language you are using, in this case C#. If you are interested in learning more about code for Revit and the API, you may also want to check out a new book which has just been released by Don Rudder called ‘Customising Autodesk Revit 2013 with .NET How-to‘
BoostYourBIM – Making the Revit API Accessible for All
Have you ever tried to write Revit API code? Have you heard about the API and wonder how it could help you? Want to learn more? If so, visit the new blog BoostYourBIM by Harry Mattison where you are welcome to learn and ask questions about the greatness of the Revit API.
Harry joined Revit Technology Corp in 1998 and spent his last few years at Autodesk as a software developer on the Revit API team. Now Harry has set out on this own and would like to help more people appreciate and benefit from the Revit API.
Learning the Revit API can be a daunting task, but BoostYourBIM breaks it down into manageable, bit-sized pieces and shares lots of C# code that you can copy, adapt, and re-use. So please visit http://boostyourbim.wordpress.com, read some posts, and submit some comments about what intrigues you about the Revit API.
Geometry Gym Revit Addon
I recently discovered a new blog – Geometry Gym, which is run by Jon Mirtschin. In case you have not seen his blog, I suggest you head over and take a look at the projects he is working on. He has and continues to work on some exciting projects involving IFC interoperability as well as the newly added Grasshopper to Revit via IFC add-on. The video which you can see on the right hand side gives a small explanation of his plugin.
“Here’s some sample models and an explanation/demonstration of the work I’m advancing to import IFC models into Revit.
You can test for yourself. Here’s the grasshopper files used in the demonstration and some example IFC output.. You need to download the latest plugins from http://www.geometrygym.com/downloads
Note I’ve only been working on the Revit addon over a period of 3 months compared to 22 months for the Rhino plugin (although I can reuse 80 to 90% of the same code) so I still need to enable many aspects of IFC file importing. But I will prioritize any example models and requests that come from users, so please don’t hesitate to ask. I look forward to hearing from you.”
The above post is from the Geometry Gym blog posted on the 13th April 2012 and I would advise you to keep an eye on the site for future updates. Many thanks to Jon for his hard work on these great projects.