Sunday, May 1, 2011

Hello World!

The first application to create when learning a new language is traditionally "Hello World". This is a very simple application that performs a single task, it displays the text "Hello World!"

This post shows step by step instructions to create a "Hello World" command using the Revit API. it is assumed that you have already installed Revit 2012 and Visual C# 2010 Express.
  1. In Visual C# 2010 Express start a new project by selecting "New Project..." from the "File" menu.
  2. From the "New Project" dialog select "Class Library" as the project type and change the "Name" to "HelloWorld" then click "OK".
  3. Visual C# will now create the files required for the new project and open the "Class1.cs" file in the text editor.
  4. Right click on the "References" object within the "Solution Explorer" window and select "Add Reference...". (If the Solution Explorer is not visible you can open it by selecting "Solution Explorer" from the "View" menu).
  5. In the "Add Reference" dialog change to the "Browse" tab and then browse to the "Program" folder within the Revit installation directory. (The default location for Revit Architecture 2012 on Windows XP is "C:\Program Files\Autodesk\Revit Architecture 2012\Program").
  6. Select both the "RevitAPI.dll" and "RevitAPIUI.dll" files and click "OK".
  7. Select both of the new references in the Solution Explorer and change the "Copy Local" parameter to "False" in the "Properties" window. (If the Properties window is not visible you can open it by selecting "Properties Window" from the "View" menu).
  8. Add the following two lines directly below the last using statement
    using Autodesk.Revit.Attributes;
    using Autodesk.Revit.UI;

    so that the top of the "Class1.cs" file now looks like this:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Autodesk.Revit.Attributes;
    using Autodesk.Revit.UI;
  9. Add a new blank line directly above the "public class Class1" line and enter the following line:
    [Transaction(TransactionMode.Automatic)]
  10. At the end of the "public class Class1" line add the following text:
    public class Class1 : IExternalCommand
  11. With the "IExternalCommand" text highlighted you should see a small blue underline at the start of the text, hover over this and click the button that appears then select the "Implement interface 'IExternalCommand'" option.
  12. Visual C# inserts the necessary methods required by the interface.
  13. Replace the "throw new NotImplementedException();" line with two new lines so that the "Execute" method now looks like this:
    public Result Execute(ExternalCommandData commandData, ref string message, Autodesk.Revit.DB.ElementSet elements)
    {
        TaskDialog.Show("TITLE", "Hello World!");
        return Result.Succeeded;
    }
  14. This is all that is required to create the *.dll file.
  15. Save the project. The default location on Windows XP is within the "Visual Studio 2010\Projects" folder in "My Documents".
  16. Build the *.dll file by selecting "Build Solution" from the "Build" menu. This will create the *.dll file in the "bin/Debug" folder within your project's folder.
  17. To tell Revit that there is a new add-in to load it is now necessary to create an addin manifest file.
  18. Still within Visual C#, right click on your project in the Solution Explorer and select "Add -> New Item...".
  19. Select "XML File" as the type and change the name to "HelloWorld.addin" then click "Add".
  20. In the new file, add new lines of text so that the contents now looks like this:
    
    
      
        C:\Documents and Settings\>%username%\My Documents\Visual Studio 2010\Projects\HelloWorld\HelloWorld\bin\Debug\HelloWorld.dll
        9c672c99-ed2a-4fb7-a05a-bbc7eb108ed3
        HelloWorld.Class1
        Hello World
        KRSP
        KrispCAD: krisp.design@gmail.com
      
    
  21. Select "Save HelloWorld.addin As..." from the "File" menu and save it to the "Addins" folder. (The default location for Revit Architecture 2012 on Windows XP is "C:\Documents and Settings\%username%\Application Data\Autodesk\REVIT\Addins\2012").
  22. The set up is now complete, launch Revit and you should find a "Hello World" command under the "External Commands" button on the "Add-Ins" tab of the Ribbon. Running the command will display the following dialog:

No comments:

Post a Comment