Macro Source Code Examples

Use these code samples to explore the Macro Manager tools.

Hello World

This code example opens a dialog box titled "Dialog" with the contents "Hello World".
using Autodesk.Revit.UI;

namespace My_First_Module
{
   [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
   [Autodesk.Revit.DB.Macros.AddInId("E04BAFB6-D442-457A-A88C-CF2D7208793E")]
   public partial class ThisApplication
   {
      private void Module_Startup(object? sender, EventArgs e)
      {

      }

      private void Module_Shutdown(object? sender, EventArgs e)
      {

      }

      public void hello()
      {
         TaskDialog.Show("Dialog", "Hello World");
      }

   }
}

Running the Sample Macro:

Select All Elements

This code example will select all elements in the current model.
using Autodesk.Revit.DB;

namespace My_First_Module
{
   [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
   [Autodesk.Revit.DB.Macros.AddInId("E04BAFB6-D442-457A-A88C-CF2D7208793E")]
   public partial class ThisApplication
   {
      private void Module_Startup(object? sender, EventArgs e)
      {

      }

      private void Module_Shutdown(object? sender, EventArgs e)
      {

      }


      public void SelectAllElements()
      {
         // Get the UIDocument
         Autodesk.Revit.UI.UIDocument uidoc = this.ActiveUIDocument;

         // Get the Document from the UIDocument
         Autodesk.Revit.DB.Document doc = uidoc.Document;

         // Create a FilteredElementCollector that will collect all elements in the Document
         FilteredElementCollector collector = new FilteredElementCollector(doc);

         // Apply no filter, which means all elements are collected
         collector.WherePasses(new LogicalOrFilter(new ElementIsElementTypeFilter(false), new ElementIsElementTypeFilter(true)));

         // Convert the collected elements to a list of element ids
         IList<ElementId> ids = collector.ToElementIds().ToList();

         // Check if there are elements to select
         if (ids.Count > 0)
         {
            // Start a new transaction (Revit API requires this for most changes to the model)
            using (Transaction trans = new Transaction(doc, "Select All Elements"))
            {
               trans.Start();

               // Make the selection
               uidoc.Selection.SetElementIds(ids);

               trans.Commit();
            }
         }
      }
   }
}

Running the Sample Macro with sample Revit model Snowdon Towers sample Architectural.rvt in a newly created 3D othorgraphic view and Zoom to Fit: