You are on page 1of 24

Session 3.


Automate the Boring Stuff with Revit Macros
Michael Kilkelly AIA, ArchSmarter

Class Description
We’ve all been there - it’s an hour until your deadline and your project manager
wants to make one little change. The problem is, this change will take hours of
tedious work - hours you simply don’t have. However, through the power of the
Revit API and some basic knowledge of computer programming, you’ll learn how
to write macros to automate Revit and save a ton of time on your next project.

This course is designed to get you started automating Revit using macros written in
VB.Net. We will begin with an introduction to Revit's macro environment and the
VB.Net programming language. Through exploring the Revit API, we will create a
series of time-saving macros that solve real-world Revit problems. We will dive into
the Revit Software Development Kit and discuss methods for troubleshooting your
macros. At the end of the class, you will have a good foundation from which to
start writing your own macros. Take command of your software and learn to

This class is geared toward Revit users with no programming experience as well as
seasoned programmers who are curious about VB.Net.

About the Speaker:
Michael Kilkelly AIA is the founder of ArchSmarter, a website dedicated to helping
architects and designers work smarter, not harder. Michael is also a principal at
Space Command, an architecture and consulting firm in Middletown, CT.
Previously, Michael was an associate at Gehry Partners in Los Angeles. Michael
received his B.Arch from Norwich University in 1995 and his SMArchS from MIT in

Session 3.1
Automate the Boring Stuff with Revit
Michael Kilkelly, ArchSmarter

What Are Macros?
Macros are one of the easiest ways to Automate Revit and access the inner
workings of the software. Macros do not require any additional software other than
Revit and are a great way for beginners to learn programming.

So what exactly is a macro? A macro is a user-created command that is coded
using Revit’s API. Macros are run directly inside of Revit and are saved in the project
file. Other applications, like MS Office, provide the ability to record macros directly
from your actions on the screen. Unfortunately, Revit does not have this
functionality. You must code your Revit macros directly.

Getting Started with Revit Macros
To start writing your own macros, you should first install the Revit 2017 Software
Development Kit or SDK. The SDK contains help files and sample code that will assist
you as you learn to program macros. The Revit 2017 SDK be installed from the main
page of the Revit installer or it can be downloaded from the Autodesk Developer
Network website:

The SDK will install on your hard drive and create a bunch of subfolders and files.
Take some time to review the files. The macro samples are particularly useful as you
get started creating your own macros.

Page 2 of 24

Macros can reside in a project file or within the Revit application. Page 3 of 24 . 1.Session 3. the process is very easy. This will open the Macro Manager dialog. Open the Macro Manager Create a new project file. ArchSmarter Write Your First Macro Ready to write your first macro? As you’ll see. Click the Manage ribbon then click the Macro Manager icon. Follow the steps below and you’ll be on your way to macro mastery. Macros saved in the application are saved to the user’s Revit configuration.1 Automate the Boring Stuff with Revit Macros Michael Kilkelly. Macros saved in the project file can be used by any user who opens that file. These macros can be used on any model file but only by the user who created the macro.

Net. title your module “MyFirstModule. Click OK to create the module. Module names cannot contain spaces or special characters. SharpDevelop is an open-source development environment that is built into Revit for programming macros. click the “Project 1” tab then click the Module button in the “Create” section. VB. Python or Ruby. When creating a macro in a new project file. Page 4 of 24 . you must first create a module. SharpDevelop will launch. You can write macros in C#. In the “Create a New Module” dialog box. Create a New Module Macros are organized in modules. ArchSmarter 2.Session 3. Once Revit has created the module. For this exercise. To create a module. A module is simply a collection of macros.1 Automate the Boring Stuff with Revit Macros Michael Kilkelly. choose VB. A single project file can contain several modules with each module having its own macros.Net as the module’s language.

4. you can create a macro inside the module. Click the Macro button in the “Create” section of the Macro Manager dialog. title your first macro “MyFirstMacro” and set the language to VB. Create a New Macro Now that you have a module. In the “Create a New Macro” dialog. type the following between the brackets: Page 5 of 24 . You’ll see the standard VB.NET code that is automatically generated when you create a new module. It only takes one line of code. Click OK to create the macro.Session 3. Write the Macro Switch over to SharpDevelop.NET. Your first macro is simply going to popup a message box in Revit. After the “public void MyFirstMacro()”. ArchSmarter 3.1 Automate the Boring Stuff with Revit Macros Michael Kilkelly. Toward the bottom you’ll see the starting code for “MyFirstMacro”.

The code window will list errors by line number so they are easy to pinpoint. select “Build” then “Build Solution”. Build the Macro Once you’ve typed the code.Net intermediate code.Session 3.NET code into the . Page 6 of 24 . All macros must be built before Revit can run them. you’re ready to compile or “build” the macro. double-check your code.1 Automate the Boring Stuff with Revit Macros Michael Kilkelly. Any errors or warning will show up in the Errors and Warning window located at the bottom of the SharpDevelop interface. In the SharpDevelop menu bar. SharpDevelop will compile your VB. If you have an error. ArchSmarter 5.

This will execute your macro. go back to Revit and open the Macro Manager dialog (Manage > Macro Manager). Run the Macro If your macro compiled correctly. Select “MyFirstMacro” from the list then click the Run button.Session 3.1 Automate the Boring Stuff with Revit Macros Michael Kilkelly. You should see “MyFirstMacro” in the list below “MyFirstModule”. ArchSmarter 6. Page 7 of 24 . You should see the following on your screen: You did it! You wrote your first Revit macro.

Next Steps Congratulations! You’re on your way to Revit macro mastery. The “Document” object contains data pertaining to the current file itself. The next challenge is learning to write code and utilize the Revit API.Session 3. VB. you can omit the “_” and type the code on a single line.ActiveUIDocument” object represents the current model file. ArchSmarter To take this further. Change your code to the following: The “Me. change “Document.Application. we used VB. Note that the “_” character in the code above indicates a line continuation symbol. All these languages compile into the same intermediate language so you have full access to Revit’s API from any of the languages. Choose a Programming Language In the example above. Since Revit uses the Microsoft .1 Automate the Boring Stuff with Revit Macros Michael Kilkelly. Below is additional information about the supported language as well as pros and cons to each. C# or VB. To see the active view in the current project file. This tells SharpDevelop that the code continues in the line below and is often used when printing long lines of code to a page.0. When you are typing the code. you can write macros in either Python.Net framework 4. Ruby. While teaching all the details of programming is beyond the scope of this workshop. Page 8 of 24 . you can modify the code to report back something more useful.NET is just one of four languages you can use to write macros.Name”.NET to write the macro. I will highlight some key areas to guide you on your journey.Net.PathName” to “ActiveView.

Ruby Created in 1995 by Lots of general Not many Revit Yukihiro code samples and specific code Matsumoto. ArchSmarter Language History Pros Cons C# Based on C and Lots of Revit The languages C++ specific code syntax is not as samples available readable as other online. features not Can build web and available in desktop apps using SharpDevelop Python. You can use languages. Some can use VB. You same thing. obscure symbols VB. The C# to develop code is more stand-alone Basic Language VB. learn. Easy to online yet.Net Evolved from VB. The takes more lines of language is not as code to do the strict as VB.Net code is VB.Session 3.Net to say the language develop desktop isn’t as elegant as applications. Code is very readable. is case desktop sensitive and uses applications. Python Created in 1991 by Lots of general Not many Revit Guido van Rossum. Can build web apps with Ruby.NET. Some learn. learning resources samples available available.1 Automate the Boring Stuff with Revit Macros Michael Kilkelly.Net is “wordier” Microsoft’s Visual easier to read than than VB.NET . Page 9 of 24 . code samples and specific code learning resources samples available available. other languages. Easy to online yet.NET. Python code debugging is very readable.

Learning the Revit API As you move beyond your first Revit macro. unfortunately. you’ll need to get familiar with the Revit API.NET. If you find a good Revit code sample written in C#. select Project > Convert and choose the language to convert the code into. To convert code. The best way to do that is through the Revit API help file. Note that the conversion process is not always perfect.chm file and click the “Content” tab.1 Automate the Boring Stuff with Revit Macros Michael Kilkelly. ArchSmarter Converting Code from One Language to Another SharpDevelop can convert code from one language to another. The help file is your roadmap to learning the API. in SharpDevelop. you can easily convert it to VB.Session 3. Page 10 of 24 . Open the RevitAPI. simply create a module and macro using the language of the code sample then. The help file lists all of the namespaces in the Revit API. You can find the help file in the Revit 2017 SDK folder. Sometimes you may find the code converts into a string of gibberish.

1 Automate the Boring Stuff with Revit Macros Michael Kilkelly.Revit. if I want to learn more about the properties of wall objects.Session 3. To reference the wall geometry property.Geometry. There may be elements within the API that are named the same. you type Autodesk. Namespaces work the same way. For instance. The folders can contain similarly named files but the path to each file will be unique as the folder names are unique.DB. Each folder at the same level of the directory structure must have a unique name. For example. The help file lists all the properties of wall elements.Revit. A good analogy for namespaces is your computer’s folder structure. I click Autodesk. many elements have a “Geometry” property but namespaces provide a way to accurately identify which geometry you’re specifying. Page 11 of 24 . ArchSmarter A namespace is essentially a hierarchical container for the elements within the API.Wall. To find more information about a specific element within the API.DB Namespace > Wall Class > Wall Properties. simply drill down through the namespaces to find the element.

SharpDevelop provides a number of tools to assist you while troubleshooting your code. Much like a road map will not teach you to drive a car.Session 3. One of the great things about coding is that the feedback is immediate. compile it then run it. The help file does contain code samples but it not a learning tool. Using Debug. Troubleshooting Macros You will spend a lot of time troubleshooting and debugging your macros. it is a description of all the elements within the API. Before you can use the command. Rather. SharpDevelop’s output window is useful for understanding what’s going on inside your macro.1 Automate the Boring Stuff with Revit Macros Michael Kilkelly. Your code will either work or it will not. you’ll need to add the Systems.Print and the Output Window While writing code. It takes some practice as it is not written in plain English.Diagnostics namespace to your macro. You do this by adding “using System. use the Debug.Print command. Revit will tell you immediately if it does not work and you will feel a sense of accomplishment when it does work. it is often useful to have your macro report back information while the macro is running. however. the API help file will not teach you to code but it will help you get where you’re going. You write some code. ArchSmarter Reading the API help file is not easy. Writing code is an iterative process and you will need feedback as you develop your macro. To output information to the output window.Diagnostics” to the beginning of your macro code. Page 12 of 24 .

Press the F10 key to step through the next line of code. Clicking on the grey area to the left of the line number row will create a break point. When Revit encounters a break point when running the code. Using Break Points In addition to stepping through your code. Any line containing a break point will also be highlighted in red. While writing macros however. you can set specific points where you want the code to stop running so you can check out the Output or Local Variables windows.1 Automate the Boring Stuff with Revit Macros Michael Kilkelly. You can step through your code using the Step Into button in the “Macro Manager” dialog. you can view the Output window to see any output from your Debug. You can also view the current values in your variables through the “Local Variables” tab. Pressing F10 will step through your code or press F5 to continue running the remainder of the macro. A break point is represented as a red dot.Print lines. it will stop executing the code.Session 3. it is often useful to step through your code line by line so you can see exactly what is going on. While you are walking through the code. Revit will run through the code sequentially. ArchSmarter Stepping Through Your Code When you compile your code and run it. Page 13 of 24 .

it can still crash or throw an exception when you run it. This can seem like finding a needle in a haystack when you are first starting out but as you code more and more macros. Exceptions Face it. Comments can also be used to block code from running. If you are getting errors from one section of the macro.Session 3. your code is not going to be perfect. If you get an error.1 Automate the Boring Stuff with Revit Macros Michael Kilkelly. If you have three options for the code. Commenting out can also be used to test very specific parts of your macro. SharpDevelop highlights all comments in green. This focused approach will save you a lot of time while troubleshooting.NET by an apostrophe at the beginning of the line. ArchSmarter Commenting Your Code One of the most critical practices to follow when writing code is to add comments as you are writing your code. These comments should serve as a reminder for what the code does and why it’s structured in that particular way. Each language has its own syntax for writing comments. You can “comment out” parts of the code that you do not want to run. use the methods listed above to systematically work through your code to identify the problem. build the macro and step through it. This is simply the nature of coding. you will get better at identifying problems in your code. Say you are testing some alternate approaches to a specific part of the macro. Page 14 of 24 . comment out everything else. comment out two and run the macro with one of the options. Comments are identified in VB. Even if your code compiles without an error.

If a view is not on a sheet. ArchSmarter Macro Sample 1 – Delete Unused Views Our first macro was useful for illustrating the process for creating a macro but let us take what we just learned and put it to use on a macro that is more useful. The following code deletes unused views in the current model file. Note this macro does not work with dependent views. Page 15 of 24 .1 Automate the Boring Stuff with Revit Macros Michael Kilkelly.Session 3. it is deleted.

ArchSmarter Macro Sample 2 – Create Sheets This macro reads a CSV file containing sheet numbers. In this example. The views listed in the file are then added to each corresponding sheet. Column B is the sheet title and column C is the view to be placed on the sheet. Page 16 of 24 .Session 3.1 Automate the Boring Stuff with Revit Macros Michael Kilkelly. The CSV file contains the following data: Column A is the sheet number. The sheets are renamed and numbered according to the CSV file. sheet names and view names. The macro creates the sheets using the first title block loaded into the project.csv” in the C:\RTCNA 2016 folder. The path to the CSV file is hard-coded into the macro. the macro looks for a file titled “sheet list. The macro then creates sheets based on this data.

ArchSmarter Continued on next page Page 17 of 24 .Session 3.1 Automate the Boring Stuff with Revit Macros Michael Kilkelly.

The macro uses two functions – one to get the wall type object and the other to convert millimetres to feet. the macro uses its level and line type to create a new wall.Session 3. The wall types are hard-coded into the macro as is the wall height.1 Automate the Boring Stuff with Revit Macros Michael Kilkelly. ArchSmarter Macro Sample 3 – Walls from Lines This macro converts model lines to Revit walls. For each model line or arc. The macro collects all the model lines and arcs in the project file (it does not collect ellipses or splines). Page 18 of 24 .

1 Automate the Boring Stuff with Revit Macros Michael Kilkelly.Session 3. ArchSmarter Continued on next page Page 19 of 24 .

Session 3. If a room number changes.Revit. the macro needs to be run again in order to update the door numbers. The macro does not create a link between the door number and the room number. Since the macro uses the room object to get the door’s room number.DB.Architecture Page 20 of 24 .1 Automate the Boring Stuff with Revit Macros Michael Kilkelly. The doors are given a letter suffix to differentiate multiple doors in a room. Use the following code to add the reference at the beginning of your macro: Imports Autodesk. you need to add a reference to the Revit Architecture namespace. The macro uses the “ToRoom” door property to determine the door’s room. ArchSmarter Macro Sample 4 – Door Number from Room Number The last macro changes the door numbers of all the doors in the project file to match their respective room number.

Session 3. ArchSmarter Continued on next page Page 21 of 24 .1 Automate the Boring Stuff with Revit Macros Michael Kilkelly.

Session 3. ArchSmarter Page 22 of 24 .1 Automate the Boring Stuff with Revit Macros Michael Kilkelly.

Which of these do you like the least? Would you like to automate it? Could you write a macro that would do the task for you? Page 23 of 24 . Some examples include:  Update all window family instances with manufacturer data from spreadsheets.  Rename all custom families in the project file using a specific prefix for your company.  Automatically place specific views on a sheet.  Check that all doors in fire-rated walls are actually fire-rated doors.Session 3.1 Automate the Boring Stuff with Revit Macros Michael Kilkelly. Think about the tasks you do on a regular basis. ArchSmarter Next Steps What else can you do with Revit macros? Pretty much anything you can think of! Good candidates are tasks that are fairly standardized or require lots of user input.

• Blogs – ArchSmarter – http://archsmarter.http://boostyourbim.php?218-Revit-API • Online Courses – Mastering Revit Macros – http://archsmarter.Session 3. – Learn to Program the Revit API . Learning to program takes time and patience.Net How-to by Don Rudder Conclusion Learning to write macros and automate Revit will drastically improve your efficiency.augi. ArchSmarter Additional Resources Want to learn more about writing your own macros? Check out these resources for more • Books – Autodesk Revit 2013 Customization with . You’ll be on your way to macro mastery in no time! Page 24 of 24 .1 Automate the Boring Stuff with Revit Macros Michael Kilkelly.udemy. Start small and work • Online Forums – Augi .com – The Building Coder .wordpress. A well-written macro can do more in five minutes than a regular user can accomplish in one hour. – Boost Your BIM .