EXTERNAL COMMAND IN 10 STEPS FOR REVIT 2015/2016 API

This is not yet another getting started tutorial on Revit API, but rather a concise checklist and minimum development environment that one can test and verify the system is working before diving into more complex code.

0. the prerequisites:

Make sure Revit 2015/2016 and Visual Studio 2012/2013 is installed. Revit 2015 will not work with older Visual Studios.

1. the project:

Create a new “Class Library” project in “Visual C#” with “.NET Framework 4.5”.

2. the references:

Add RevitAPI.dll, RevitAPIUI.dll and System.Windows.Forms as project references.

Change the “Copy Local” property of RevitAPI.dll and RevitAPIUI.dll to false.

3. the class:

Inherit the Autodesk.Revit.UI.IExternalCommand interface.

Add the following class attribute code before the class declaration.

[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Automatic)]

4. the method:

Implement the Execute method of the inherited interface.

public Autodesk.Revit.UI.Result Execute(
    Autodesk.Revit.UI.ExternalCommandData commandData,
    ref string message,
    Autodesk.Revit.DB.ElementSet elements)
{
    System.Windows.Forms.MessageBox.Show("Hello World");
    return Autodesk.Revit.UI.Result.Succeeded;
} 

5. the assembly:

Press F6 and VS should report “Build succeeded”.

Note the output path of the dll file shown in the Output window.

6. the GUID:

Create a unique ID for your command.

7. the .addin file

Create an empty text file in: C:\Users\yourUserName\AppData\Roaming\Autodesk\Revit\Addins\201X

(or C:\ProgramData\Autodesk\Revit\Addins\201X).

Change the file name to anyNameWillWork.addin.

Open the file and copy or type the following code:

<?xml version="1.0" encoding="utf-8"?>
<RevitAddIns>
    <AddIn Type="Command">
        <Assembly>C:\yourAddinPath\youAddinName.dll</Assembly>
        <AddInId>yourLong-Long-Long-Long-GuidGoesHere</AddInId>
        <FullClassName>yourAddinNamespace.YourAddinClassName</FullClassName>
        <Text>ExternalCommandTest</Text>
        <VendorId>LZNT</VendorId>
    </AddIn>
</RevitAddIns>

8. DONE!

Now start Revit and you should see an “Add-Ins” tab at the end of Revit tabs, in which there is an “External Tools” button with the newly created External Command.

9. (optional) the debugging:

Point the “Start external program” to the installed revit.exe.

Go to Tools – Options – Debugging, check “Use Managed Compatibility Mode”.

Set a break point in your code.

Press F5 in VS and Revit should be started.

Activate your plugin and you break point should be hit.

~~~~~~~~~~END of original post~~~~~~~~~~

~~~~~~~~~~Update 2015.5.15~~~~~~~~~~

GitHub: 

A GitHub repository is created, with the complete Test project for Revit 2016 API. The only thing you need to do is edit the Assembly line in the .addin file, and copy the file to your Revit Addins folder. You are welcome to download the code, and let me know if there are any questions and problems. Enjoy!

https://github.com/lezhangnet/RevitAPI

Reference:

As mentioned at the beginning of this post, the content in this post is as concise as possible. If you want more information, here is a tutorial from Autodesk Revit API documentation that contains more details. You may also find the other parts of the documentation to be useful when you try to dig dipper.

http://help.autodesk.com/view/RVT/2016/ENU/?guid=GUID-8EB25D2A-3CAF-486A-BA8E-C2BEF3DB68F6

3 thoughts on “EXTERNAL COMMAND IN 10 STEPS FOR REVIT 2015/2016 API”

Leave a Reply

Your email address will not be published. Required fields are marked *