如何添加您的知识

Walkthrough: Add Hello World Ribbon Panel

    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:

    1. Create a C# project in Visual Studio using the Class Library template.
    2. Type AddPanel as the project name.
    3. Add references to the RevitAPI.dll and RevitAPIUI.dll using the directions in the previous walkthrough, Walkthrough: Hello World.
    4. Add the PresentationCore reference:
      • In the Solution Explorer, right-click References to display a context menu.
      • From the context menu, click Add Reference. The Add Reference dialog box appears.
      • In the Add Reference dialog box, click the .NET Tab.
      • From the Component Name list, select PresentationCore.
      • Click OK to close the dialog box. PresentationCore appears in the Solution Explorer reference tree.

       

      Figure 8: Add Reference

    5. Add the WindowsBase reference following similar steps as above.

    Change the Class Name

    To change the class name, complete the following steps:

    1. In the class view window, right-click Class1 to display a context menu.
    2. From the context menu, select Rename and change the class' name to CsAddPanel.
    3. In the Solution Explorer, right-click the Class1.cs file to display a context.
    4. From the context menu, select Rename and change the file's name to CsAddPanel.cs.
    5. Double click CsAddPanel.cs 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 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.

    Create the .addin manifest file

    To invoke the application in Revit, create a manifest file to register it into Revit.

    1. Create a new text file using Notepad.
    2. 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>
    3. 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 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