Use the Revit Platform API and C# to create a Hello World program using the directions provided. For information about how to create an add-in application using VB.NET, refer to Hello World for VB.NET .
The Hello World walkthrough covers the following topics:
All operations and code in this section were created using Visual Studio 2015.
The first step in writing a C# program with Visual Studio is to choose a project type and create a new Class Library.
Figure 1: Add New Project
Click OK to select the .dll and close the dialog box. RevitAPI appears in the Solution Explorer reference tree.
Repeat the steps above for the RevitAPIUI.dll.
Add the following code to create the add-in:
Code Region 2-1: Getting Started |
using System; using Autodesk.Revit.UI; using Autodesk.Revit.DB; namespace HelloWorld { [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)] public class Class1 : IExternalCommand { public Autodesk.Revit.UI.Result Execute(ExternalCommandData revit, ref string message, ElementSet elements) { TaskDialog.Show("Revit", "Hello World"); return Autodesk.Revit.UI.Result.Succeeded; } } } |
Figure 2: Using Intellisense to Implement Interface
Every Revit add-in application must have an entry point class that implements the IExternalCommand interface, and you must implement the Execute() method. The Execute() method is the entry point for the add-in application similar to the Main() method in other programs. The add-in entry point class definition is contained in an assembly. For more details, refer to Add-in Integration.
After completing the code, you must build the file. From the Build menu, click Build Solution. Output from the build appears in the Output window indicating that the project compiled without errors.
The HelloWorld.dll file appears in the project output directory. If you want to invoke the application in Revit, create a manifest file to register it into Revit.
Code Region 2-2: Creating a .addin manifest file for an external command |
<?xml version="1.0" encoding="utf-8" standalone="no"?> <RevitAddIns> <AddIn Type="Command"> <Assembly>C:\Samples\HelloWorld\HelloWorld\bin\Debug\HelloWorld.dll</Assembly> <AddInId>239BD853-36E4-461f-9171-C5ACEDA4E721</AddInId> <FullClassName>HelloWorld.Class1</FullClassName> <Text>HelloWorld</Text> <VendorId>NAME</VendorId> <VendorDescription>Your Company Information</VendorDescription> </AddIn> </RevitAddIns> |
<runtime> <generatePublisherEvidence enabled="false" /> <loadFromRemoteSources enabled="true"/> </runtime>
Refer to Add-in Integration for more details using manifest files.
Running a program in Debug mode uses breakpoints to pause the program so that you can examine the state of variables and objects. If there is an error, you can check the variables as the program runs to deduce why the value is not what you might expect.
Figure 3: Set debug environment
TaskDialog.Show("Revit", "Hello World");
Test debugging:
Figure 4: HelloWorld External Tools command
Figure 5: TaskDialog message
Q: My add-in application will not compile.
A: If an error appears when you compile the sample code, the problem may be with the version of the RevitAPI used to compile the add-in. Delete the old RevitAPI reference and load a new one. For more details, refer to Add Reference.
Q: Why is there no Add-Ins tab or why isn't my add-in application displayed under External Tools?
A: In many cases, if an add-in application fails to load, Revit will display an error dialog on startup with information about the failure. For example, if the add-in DLL cannot be found in the location specified in the manifest file, a message similar to the following appears.
Figure 6: External Tools Error Message
In this case, ensure that the .addin file has the correct path to the assembly.
Error messages will also be displayed if the class name specified in FullClassName is not found or does not inherit from IExternalCommand.
However, in some cases, an add-in application may fail to load without any message. Possible causes include:
Q: Why does my add-in application not work?
A: Even though your add-in application is available under External Tools, it may not work. This is most often caused by an exception in the code.
Revit will display an error dialog with information about the exception when the command fails.
Figure 7: Unhandled exception in External Command
This is intended as an aid to debugging your command; commands deployed to users should use try..catch..finally in the example entry method to prevent the exception from being caught by Revit. Here is an example:
Code Region 2-4: Using try catch in execute: |
public IExternalCommand.Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { ExternalCommandData cdata = commandData; Autodesk.Revit.ApplicationServices.Application app = cdata.Application; try { // Your code here } catch (Exception ex) { message = ex.Message; return Autodesk.Revit.UI.Result.Failed; } return Autodesk.Revit.UI.Result.Succeeded; } |