In the Walkthrough: Hello World section you learn how to create an add-in application and invoke it in Revit. You also learn to create a .addin manifest file to register the add-in application as an external tool. Another way to invoke the add-in application in Revit is through a custom ribbon panel.
Complete the following steps to create a new project:
Figure 8: Add Reference
To change the class name, complete the following steps:
The Add Panel project is different from Hello World because it is automatically invoked when Revit runs. Use the IExternalApplication interface for this project. The IExternalApplication interface contains two abstract methods, OnStartup() and OnShutdown(). For more information about IExternalApplication, refer to Add-in Integration.
Add the following code to the file:
Code Region 2-5: Adding a ribbon panel |
using System; using System.Reflection; using Autodesk.Revit.DB; using Autodesk.Revit.UI; using System.Windows.Media.Imaging; namespace Walkthrough { /// <remarks> /// This application's main class. The class must be Public. /// </remarks> public class CsAddPanel : IExternalApplication { // Both OnStartup and OnShutdown must be implemented as public method public Result OnStartup(UIControlledApplication application) { // Add a new ribbon panel RibbonPanel ribbonPanel = application.CreateRibbonPanel("NewRibbonPanel"); // Create a push button to trigger a command add it to the ribbon panel. string thisAssemblyPath = Assembly.GetExecutingAssembly().Location; PushButtonData buttonData = new PushButtonData("cmdHelloWorld", "Hello World", thisAssemblyPath, "Walkthrough.HelloWorld"); PushButton pushButton = ribbonPanel.AddItem(buttonData) as PushButton; // Optionally, other properties may be assigned to the button // a) tool-tip pushButton.ToolTip = "Say hello to the entire world."; // b) large bitmap Uri uriImage = new Uri(@"D:\Sample\HelloWorld\bin\Debug\39-Globe_32x32.png"); BitmapImage largeImage = new BitmapImage(uriImage); pushButton.LargeImage = largeImage; return Result.Succeeded; } public Result OnShutdown(UIControlledApplication application) { // nothing to clean up in this simple case return Result.Succeeded; } } /// <remarks> /// The "HelloWorld" external command. The class must be Public. /// </remarks> [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)] public class HelloWorld : IExternalCommand { // The main Execute method (inherited from IExternalCommand) must be public public Autodesk.Revit.UI.Result Execute(ExternalCommandData revit, ref string message, ElementSet elements) { TaskDialog.Show("Revit", "Hello World"); return Autodesk.Revit.UI.Result.Succeeded; } } } |
After completing the code, build the application. From the Build menu, click Build Solution. Output from the build appears in the Output window indicating that the project compiled without errors. AddPanel.dll is located in the project output directory.
To invoke the application in Revit, create a manifest file to register it into Revit.
Code Region 2-6: Creating a .addin file for the external application |
<?xml version="1.0" encoding="utf-8" standalone="no"?> <RevitAddIns> <AddIn Type="Application"> <Assembly>D:\Sample\AddPanel\AddPanel\bin\Debug\AddPanel.dll</Assembly> <AddInId>604b1052-f742-4951-8576-c261d1993108</AddInId> <FullClassName>Walkthrough.CsAddPanel</FullClassName> <Name>HelloWorld</Name> <VendorId>NAME</VendorId> <VendorDescription>Your Company Information</VendorDescription> </AddIn> </RevitAddIns> |
Refer to Add-in Integration for more information about .addin manifest files.
To begin debugging, build the project, and run Revit. A new ribbon panel appears on the Add-Ins tab named NewRibbonPanel and Hello World appears as the only button on the panel, with a large globe image.
Figure 9: Add a new ribbon panel to Revit
Click Hello World to run the application and display the following dialog box.
Figure 10: Hello World dialog box