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 System;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI.Selection;
using System.Collections.Generic;
using System.Linq;

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");
        }
		
		
		#region Revit Macros generated code
		private void InternalStartup()
		{
			this.Startup += new System.EventHandler(Module_Startup);
			this.Shutdown += new System.EventHandler(Module_Shutdown);
		}
		#endregion
	}
}

Select All Elements

This code example will select all elements in the current model.
using System;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI.Selection;
using System.Collections.Generic;
using System.Linq;

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();
				}
			}
		}
		
		#region Revit Macros generated code
		private void InternalStartup()
		{
			this.Startup += new System.EventHandler(Module_Startup);
			this.Shutdown += new System.EventHandler(Module_Shutdown);
		}
		#endregion
	}
}