Converting a Macro into an Add-In

Here is a summary of how to convert this macro into an add-in command.

public void MyMacro()
{
    Document doc = this.ActiveUIDocument.Document;
    ICollection<Element> wallList = new FilteredElementCollector(doc).OfClass(typeof(Wall)).ToElements();
    TaskDialog.Show("Wall Count", wallList.Count.ToString());
}

1) Create a new “Class Library” project in the development environment of your choice (I am using the free Microsoft Visual Studio Express 2012 for Windows Desktop for this example)

NewProject

2) Right click on the References folder and select “Add Reference”

addReferences

3) This is the code that goes into the “Class1.cs” file that Visual Studio creates

using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.Attributes;
namespace MyAddinTest
{
    // Specifies that the command will be responsible for creating any needed transactions
    [Transaction(TransactionMode.Manual)]
    // "MyAddInCommand" is the name of the command
    public class MyAddInCommand : IExternalCommand
    {
        // every command must have a unique GUID (globally unique identifier) of any 32 hexadecimal digits
        // you can modify an GUID you are using in some other macro or create one in Visual Studio with Tools - Create GUID
        // or http://www.guidgenerator.com/online-guid-generator.aspx
        static AddInId appId = new AddInId(new Guid("21A8C920-ED49-434A-AACD-176784316B92"));
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elementSet)
        {
            // the code get the document is changed from the Macro version
            Document doc = commandData.Application.ActiveUIDocument.Document;
            ICollection<Element> wallList = new FilteredElementCollector(doc).OfClass(typeof(Wall)).ToElements();
            TaskDialog.Show("Wall Count", wallList.Count.ToString());

            // Unlike a macro, which does not return any value, an add-in needs to return a "result" value
            // specifying if the command succeeded, failed, or was cancelled.
            return Result.Succeeded;
        }
    }
}

4) Compile it with the command “Build – Build Solution”. This creates a file MyAddinTest.dll. On my machine it is in the folder C:\Users\HP002\Documents\Visual Studio 2012\Projects\MyAddinTest\MyAddinTest\bin\Debug

5) Create an .addin file. Mine is in C:\Users\HP002\AppData\Roaming\Autodesk\revit\Addins\2013. The .addin file can have any name that you want. Here is the contents of my BoostYourBIM.addin file.

<?xml version="1.0" encoding="utf-8"?>
<RevitAddIns>
   <AddIn Type="Command">
    <Assembly>C:\Users\HP002\Documents\Visual Studio 2012\Projects\MyAddinTest\MyAddinTest\bin\Debug\MyAddinTest.dll</Assembly>
    <AddInId>21A8C920-ED49-434A-AACD-176784316B92</AddInId>
    <FullClassName>MyAddinTest.MyAddInCommand</FullClassName>
	<Text>Count Walls</Text>
	<Description>Count the number of walls in this project</Description>
    <VendorId>test</VendorId>
  </AddIn> 
 </RevitAddIns>

6) Run Revit. The External Tools menu on the Add-Ins”Count Walls” external command will include the Count Walls command.

addin

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s