Automate the Boring Stuff with Revit Macros

Session 3.1 Automate the Boring Stuff with Revit Macros Michael Kilkelly AIA, ArchSmarter Class Description We’ve all been there - it’s an hour unti...
Author: Arnold Hubbard
40 downloads 0 Views 1MB Size
Session 3.1

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 program! 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 1999.

Session 3.1 Automate the Boring Stuff with Revit Macros 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: http://www.autodesk.com/developrevit

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

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

Write Your First Macro Ready to write your first macro? As you’ll see, the process is very easy. Follow the steps below and you’ll be on your way to macro mastery. 1. Open the Macro Manager Create a new project file. Click the Manage ribbon then click the Macro Manager icon. This will open the Macro Manager dialog.

Macros can reside in a project file or within the Revit application. Macros saved in the project file can be used by any user who opens that file. Macros saved in the application are saved to the user’s Revit configuration. These macros can be used on any model file but only by the user who created the macro.

Page 3 of 24

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

2. Create a New Module Macros are organized in modules. When creating a macro in a new project file, you must first create a module. A module is simply a collection of macros. A single project file can contain several modules with each module having its own macros. Module names cannot contain spaces or special characters. To create a module, click the “Project 1” tab then click the Module button in the “Create” section. In the “Create a New Module” dialog box, title your module “MyFirstModule. You can write macros in C#, VB.Net, Python or Ruby. For this exercise, choose VB.Net as the module’s language. Click OK to create the module.

Once Revit has created the module, SharpDevelop will launch. SharpDevelop is an open-source development environment that is built into Revit for programming macros.

Page 4 of 24

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

3. Create a New Macro Now that you have a module, you can create a macro inside the module. Click the Macro button in the “Create” section of the Macro Manager dialog. In the “Create a New Macro” dialog, title your first macro “MyFirstMacro” and set the language to VB.NET. Click OK to create the macro.

4. Write the Macro Switch over to SharpDevelop. You’ll see the standard VB.NET code that is automatically generated when you create a new module. Toward the bottom you’ll see the starting code for “MyFirstMacro”. Your first macro is simply going to popup a message box in Revit. It only takes one line of code. After the “public void MyFirstMacro()”, type the following between the brackets:

Page 5 of 24

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

5. Build the Macro Once you’ve typed the code, you’re ready to compile or “build” the macro. All macros must be built before Revit can run them. In the SharpDevelop menu bar, select “Build” then “Build Solution”.

SharpDevelop will compile your VB.NET code into the .Net intermediate code. Any errors or warning will show up in the Errors and Warning window located at the bottom of the SharpDevelop interface.

If you have an error, double-check your code. The code window will list errors by line number so they are easy to pinpoint. Page 6 of 24

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

6. Run the Macro If your macro compiled correctly, go back to Revit and open the Macro Manager dialog (Manage > Macro Manager). You should see “MyFirstMacro” in the list below “MyFirstModule”.

Select “MyFirstMacro” from the list then click the Run button. This will execute your macro. You should see the following on your screen:

You did it! You wrote your first Revit macro.

Page 7 of 24

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

To take this further, you can modify the code to report back something more useful. Change your code to the following:

The “Me.Application.ActiveUIDocument” object represents the current model file. The “Document” object contains data pertaining to the current file itself. To see the active view in the current project file, change “Document.PathName” to “ActiveView.Name”. Note that the “_” character in the code above indicates a line continuation symbol. This tells SharpDevelop that the code continues in the line below and is often used when printing long lines of code to a page. When you are typing the code, you can omit the “_” and type the code on a single line.

Next Steps Congratulations! You’re on your way to Revit macro mastery. The next challenge is learning to write code and utilize the Revit API. While teaching all the details of programming is beyond the scope of this workshop, I will highlight some key areas to guide you on your journey. Choose a Programming Language In the example above, we used VB.NET to write the macro. VB.NET is just one of four languages you can use to write macros. Since Revit uses the Microsoft .Net framework 4.0, you can write macros in either Python, Ruby, C# or VB.Net. All these languages compile into the same intermediate language so you have full access to Revit’s API from any of the languages. Below is additional information about the supported language as well as pros and cons to each.

Page 8 of 24

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

Language

History Based on C and C++

Pros Lots of Revit specific code samples available online. You can use C# to develop stand-alone desktop applications.

Cons The languages syntax is not as readable as other languages. The code is more compact, is case sensitive and uses obscure symbols

VB.Net

Evolved from Microsoft’s Visual Basic Language

VB.Net code is easier to read than VB.NET. The language is not as strict as VB.NET. You can use VB.Net to develop desktop applications.

VB.Net is “wordier” than VB.NET - it takes more lines of code to do the same thing. Some say the language isn’t as elegant as other languages.

Python

Created in 1991 by Guido van Rossum.

Lots of general code samples and learning resources available. Easy to learn. Python code is very readable. Can build web and desktop apps using Python.

Not many Revit specific code samples available online yet. Some debugging features not available in SharpDevelop

Ruby

Created in 1995 by Yukihiro Matsumoto.

Lots of general code samples and learning resources available. Easy to learn. Code is very readable. Can build web apps with Ruby.

Not many Revit specific code samples available online yet.

C#

Page 9 of 24

Session 3.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. If you find a good Revit code sample written in C#, you can easily convert it to VB.NET. To convert code, simply create a module and macro using the language of the code sample then, in SharpDevelop, select Project > Convert and choose the language to convert the code into.

Note that the conversion process is not always perfect. Sometimes you may find the code converts into a string of gibberish, unfortunately.

Learning the Revit API As you move beyond your first Revit macro, you’ll need to get familiar with the Revit API. The best way to do that is through the Revit API help file. The help file is your roadmap to learning the API. You can find the help file in the Revit 2017 SDK folder. Open the RevitAPI.chm file and click the “Content” tab. The help file lists all of the namespaces in the Revit API.

Page 10 of 24

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

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

Page 11 of 24

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

Reading the API help file is not easy. It takes some practice as it is not written in plain English. Rather, it is a description of all the elements within the API. The help file does contain code samples but it not a learning tool. Much like a road map will not teach you to drive a car, the API help file will not teach you to code but it will help you get where you’re going.

Troubleshooting Macros You will spend a lot of time troubleshooting and debugging your macros. One of the great things about coding is that the feedback is immediate. You write some code, compile it then run it. Your code will either work or it will not. Revit will tell you immediately if it does not work and you will feel a sense of accomplishment when it does work. SharpDevelop provides a number of tools to assist you while troubleshooting your code. Using Debug.Print and the Output Window While writing code, it is often useful to have your macro report back information while the macro is running. Writing code is an iterative process and you will need feedback as you develop your macro. SharpDevelop’s output window is useful for understanding what’s going on inside your macro. To output information to the output window, use the Debug.Print command. Before you can use the command, however, you’ll need to add the Systems.Diagnostics namespace to your macro. You do this by adding “using System.Diagnostics” to the beginning of your macro code.

Page 12 of 24

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

Stepping Through Your Code When you compile your code and run it, Revit will run through the code sequentially. While writing macros however, it is often useful to step through your code line by line so you can see exactly what is going on. You can step through your code using the Step Into button in the “Macro Manager” dialog.

Press the F10 key to step through the next line of code. While you are walking through the code, you can view the Output window to see any output from your Debug.Print lines. You can also view the current values in your variables through the “Local Variables” tab. Using Break Points In addition to stepping through your code, you can set specific points where you want the code to stop running so you can check out the Output or Local Variables windows. Clicking on the grey area to the left of the line number row will create a break point. A break point is represented as a red dot. Any line containing a break point will also be highlighted in red. When Revit encounters a break point when running the code, it will stop executing the code. Pressing F10 will step through your code or press F5 to continue running the remainder of the macro. Page 13 of 24

Session 3.1 Automate the Boring Stuff with Revit Macros Michael Kilkelly, 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. Comments are identified in VB.NET by an apostrophe at the beginning of the line. SharpDevelop highlights all comments in green.

Comments can also be used to block code from running. Say you are testing some alternate approaches to a specific part of the macro. You can “comment out” parts of the code that you do not want to run. If you have three options for the code, comment out two and run the macro with one of the options. Commenting out can also be used to test very specific parts of your macro. If you are getting errors from one section of the macro, comment out everything else, build the macro and step through it. This focused approach will save you a lot of time while troubleshooting.

Exceptions Face it, your code is not going to be perfect. Even if your code compiles without an error, it can still crash or throw an exception when you run it. This is simply the nature of coding. If you get an error, use the methods listed above to systematically work through your code to identify the problem. This can seem like finding a needle in a haystack when you are first starting out but as you code more and more macros, you will get better at identifying problems in your code.

Page 14 of 24

Session 3.1 Automate the Boring Stuff with Revit Macros Michael Kilkelly, 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. If a view is not on a sheet, it is deleted. Note this macro does not work with dependent views.

Page 15 of 24

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

Macro Sample 2 – Create Sheets This macro reads a CSV file containing sheet numbers, sheet names and view names. The macro then creates sheets based on this data. The sheets are renamed and numbered according to the CSV file. The views listed in the file are then added to each corresponding sheet. The macro creates the sheets using the first title block loaded into the project. The path to the CSV file is hard-coded into the macro. In this example, the macro looks for a file titled “sheet list.csv” in the C:\RTCNA 2016 folder. The CSV file contains the following data:

Column A is the sheet number. 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, ArchSmarter

Continued on next page

Page 17 of 24

Session 3.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. The macro collects all the model lines and arcs in the project file (it does not collect ellipses or splines). For each model line or arc, the macro uses its level and line type to create a new wall. The wall types are hard-coded into the macro as is the wall height. The macro uses two functions – one to get the wall type object and the other to convert millimetres to feet.

Page 18 of 24

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

Continued on next page Page 19 of 24

Session 3.1 Automate the Boring Stuff with Revit Macros Michael Kilkelly, 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. The doors are given a letter suffix to differentiate multiple doors in a room. The macro uses the “ToRoom” door property to determine the door’s room. The macro does not create a link between the door number and the room number. If a room number changes, the macro needs to be run again in order to update the door numbers. Since the macro uses the room object to get the door’s room number, you need to add a reference to the Revit Architecture namespace. Use the following code to add the reference at the beginning of your macro: Imports Autodesk.Revit.DB.Architecture

Page 20 of 24

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

Continued on next page

Page 21 of 24

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

Page 22 of 24

Session 3.1 Automate the Boring Stuff with Revit Macros Michael Kilkelly, 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. Some examples include:  Update all window family instances with manufacturer data from spreadsheets.  Check that all doors in fire-rated walls are actually fire-rated doors.  Rename all custom families in the project file using a specific prefix for your company.  Automatically place specific views on a sheet. Think about the tasks you do on a regular basis. 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

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

Additional Resources Want to learn more about writing your own macros? Check out these resources for more information. •



Blogs –

ArchSmarter – http://archsmarter.com



The Building Coder - http://thebuildingcoder.typepad.com/



Boost Your BIM - http://boostyourbim.wordpress.com/

Online Forums –





Augi - http://forums.augi.com/forumdisplay.php?218-Revit-API

Online Courses –

Mastering Revit Macros – http://archsmarter.com/go/mrm



Learn to Program the Revit API - https://www.udemy.com/revitapi/

Books –

Autodesk Revit 2013 Customization with .Net How-to by Don Rudder

Conclusion Learning to write macros and automate Revit will drastically improve your efficiency. A well-written macro can do more in five minutes than a regular user can accomplish in one hour. Learning to program takes time and patience. Start small and work systematically. You’ll be on your way to macro mastery in no time!

Page 24 of 24