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.
Create a New Project
Complete the following steps to create a new project:
- Create a C# project in Visual Studio using the Class Library template.
- Type AddPanel as the project name.
- Add references to the RevitAPI.dll and RevitAPIUI.dll using the directions in the previous walkthrough, Walkthrough: Hello World.
- Add the PresentationCore reference:
- In the , right-click to display a menu.
- From the menu, click . The dialog box appears.
- In the dialog box, click the Tab.
- From the list, select .
- Click to close the dialog box. PresentationCore appears in the Solution Explorer reference tree.
- Add the WindowsBase reference following similar steps as above.
Change the Class Name
To change the class name, complete the following steps:
- In the window, right-click to display a menu.
- From the menu, select and change the class' name to CsAddPanel.
- In the , right-click the Class1.cs file to display a context.
- From the menu, select and change the file's name to CsAddPanel.cs.
- Double click to open it for editing.
Add Code
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 for the ribbon panel:
Code Region 2-5: Adding a ribbon panel |
|---|
public class CsAddpanel : Autodesk.Revit.UI.IExternalApplication
{
public Autodesk.Revit.UI.Result OnStartup(UIControlledApplication application)
{
// add new ribbon panel
RibbonPanel ribbonPanel = application.CreateRibbonPanel("NewRibbonPanel");
//Create a push button in the ribbon panel "NewRibbonPanel"
//the add-in application "HelloWorld" will be triggered when button is pushed
PushButton pushButton = ribbonPanel.AddItem(new PushButtonData("HelloWorld",
"HelloWorld", @"D:\HelloWorld.dll", "HelloWorld.CsHelloWorld")) as PushButton;
// Set the large image shown on button
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)
{
return Result.Succeeded;
}
}
|
Build the Application
After completing the code, build the application. From the menu, click . 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.
Create the .addin manifest file
To invoke the application in Revit, create a manifest file to register it into Revit.
- Create a new text file using Notepad.
- Add the following text to the file:
Code Region 2-6: Creating a .addin file for an external application |
|---|
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<RevitAddIns>
<AddIn Type="Application">
<Name>SampleApplication</Name>
<Assembly>D:\Sample\AddPanel\AddPanel\bin\Debug\AddPanel.dll</Assembly>
<AddInId>604B1052-F742-4951-8576-C261D1993107</AddInId>
<FullClassName>AddPanel.CsAddPanel</FullClassName>
<VendorId>ADSK</VendorId>
<VendorDescription>Autodesk, www.autodesk.com</VendorDescription>
</AddIn>
</RevitAddIns> |
- Save the file as HelloWorldRibbon.addin and put it in the following location:
- For Windows XP - C:\Documents and Settings\All Users\Application Data\Autodesk\Revit\Addins\2012\
- For Vista/Windows 7 - C:\ProgramData\Autodesk\Revit\Addins\2012\
Note The AddPanel.dll file is in the default file folder in a new folder called Debug (D:\Sample\HelloWorld\bin\Debug\AddPanel.dll). Use the file path to evaluate Assembly.
Refer to Add-in Integration for more information about .addin manifest files.
Debugging
To begin debugging, build the project, and run Revit. A new ribbon panel appears on the tab named NewRibbonPanel and Hello World appears as the only button on the panel, with a large globe image.
Click to run the application and display the following dialog box.