How to Write Your First Revit Macro in 7 Easy Steps

automation macros programming revit Mar 24, 2018

Macros are one of the easiest ways to Automate Revit. They let you get under the hood of your software and put it to work for you. 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 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.

 

Your First Revit 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 Revit 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.

 

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 C# 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.

 

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 C# 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:

public void MyFirstMacro()

{

TaskDialog.Show("My First Macro", "Hello World!");

}

 

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.

 

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.

 

7. Make Some Changes

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

public void MyFirstMacro()

{

TaskDialog.Show("My First Macro", "The current model file is " + this.Application.ActiveUIDocument.Document.PathName);

}

The “this.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”.

 

Your Next Revit Macro

Our first macro was useful for illustrating the process of creating a macro but let’s 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. To test out your new macro coding skills, create a new macro and type the following code:

 

public void DeleteUnusedViews() {

 

// define current document

 

Document curDoc = this.Application.ActiveUIDocument.Document;

 

 

 

// get all views

 

FilteredElementCollector viewCollector = new FilteredElementCollector(curDoc);

 

viewCollector.OfCategory(BuiltInCategory.OST_Views);

 

 

 

// get all sheets

 

FilteredElementCollector sheetCollector = new FilteredElementCollector(curDoc);

 

sheetCollector.OfCategory(BuiltInCategory.OST_Sheets);

 

 

 

// create a list of views to delete

 

List<View> viewsToDelete = new List<View>();

 

 

 

// loop through views and check if it's on a sheet

 

foreach (View curView in viewCollector) {

 

// check if current view is a template

 

if(curView.IsTemplate == false) {

 

// check if view can be added to sheet

 

if(Viewport.CanAddViewToSheet(curDoc, sheetCollector.FirstElementId(), curView.Id) == true) {

 

// add view to delete list

 

viewsToDelete.Add(curView);

 

}

 

}

 

}

 

 

 

// create transaction

 

Transaction curTrans = new Transaction(curDoc);

 

curTrans.Start("Delete Views");

 

 

 

// delete views in list

 

foreach (View viewToDelete in viewsToDelete) {

 

curDoc.Delete(viewToDelete.Id);

 

}

 

 

 

// commit changes

 

curTrans.Commit();

 

curTrans.Dispose();

 

 

 

// alert user

 

TaskDialog.Show("Deleted Views", "Deleted " + viewsToDelete.Count().ToString() + " views.");

 

}

 

After typing the code, go to Build > Build Solution to compile it. You may get an error or two, that’s normal. Use the steps outlined in the troubleshooting section above and try to resolve the errors. Check for typos in your code – that’s what usually gets me! Once the code builds successfully, switch over to Revit and run the macro.

 

Next Steps

Congratulations! You are on your way to Revit macro mastery! While this tutorial is very basic, it illustrates the principles of writing Revit macros. For the next step, I recommend downloading and installing the Revit 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 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.

 

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!

 

Join ArchSmarter!

Sign up for ArchSmarter updates and get access to the ArchSmarter Toolbox, a collection of time-saving Revit macros, Dynamo scripts, and other resources. Plus you'll get weekly productivity tips, webinar invitations and more! Don't worry, your information will not be shared.

We hate SPAM. We will never sell your information, for any reason.