next previous home

Hello world in C#

This lab will focus on creating a C# Revit application project, referencing appropriate assemblies, implementing required interfaces and setting up the project. Consequently, we shall also write our first "Hello World" program

Start Microsoft Visual Studio .NET 2005, select File > New > Project..., select Visual C# as project type and use the Class Library template. Name the project 'LabsCode' and select a suitable directory.

Create new Visual Studio project

Visual Studio will automatically create a class and display its code.

Add a reference to the Revit API .NET assembly by right clicking on the 'References' item in the solution explorer, selecting 'Add References...' and then the 'Add...' button.

Add a reference to RevitAPI.dll

You can select the 'Browse' tab and navigate to 'RevitAPI.dll' in the 'Program' subfolder of the Revit installation folder, typically something like 'C:\Program Files\Revit Architecture 2009\Program\RevitAPI.dll'.

Add a reference to RevitAPI.dll

After adding the reference, right click on it to display its properties. Make sure that the 'Copy Local' property is set to False and 'Specific Version' to True (both defaults are opposite). Otherwise, the wrong version of the DLL may be loaded on machines with side-by-side Revit installations. You can normally remove the references to System.Data and System.Xml, which are added by default but often not required.

Set 'Copy Local' to false

We will also need a reference to System.Windows.Forms to display a dialogue box. That is found in the list of .NET assemblies in the first tab of the 'Add Reference' dialogue.

Rename the Class1.cs file, for example by right-clicking on it in the solution explorer, e.g. to Lab1_1_HelloWorld.cs, for consistency with the later command paths, and clear all the file contents.

We need to make use of some namespaces from the referenced assemblies by adding some using statements at the top of each file. For this lab, we only need the Autodesk.Revit namespace. We will be adding others in later labs.

using System;
using System.Windows.Forms;
using Autodesk.Revit;

Now we implement the Revit external command. A Revit external command is implemented in a class using any name which implements the Revit API IExternalCommand interface and its Execute() method.

In this lab, we only display a typical message and return a successful return code:

namespace LabsCode
{
  public class Lab1_1_HelloWorld : IExternalCommand
  {
    public IExternalCommand.Result Execute(
      Autodesk.Revit.ExternalCommandData commandData,
      ref string message,
      Autodesk.Revit.ElementSet elements )
    {
      MessageBox.Show( "Hello world!", "Revit API C# Sample" );
      return IExternalCommand.Result.Succeeded;
    }
  }
}

The project should now compile and link properly.

Finally, we need to let Revit know how to load this command. By design, Revit reads such information only once from the Revit.ini file when the application is started. So, before starting Revit, locate this file in your Revit installation folder Program subdirectory, open it in a text editor (e.g. in Notepad or Visual Studio) and add the following section at the end:

[ExternalCommands]
ECCount=1

ECName1=Lab 1-1 Hello World
ECDescription1=Basic VB.NET sample that displays a message box in Revit
ECClassName1=LabsCode.Lab1_1_HelloWorld
ECAssembly1=C:\tmp\revit\LabsCode\LabsCode\bin\Debug\LabsCode.dll

You must adjust the above full assembly path and fully qualified class name Namespace.Class settings to match your project or the application cannot be loaded by Revit.

The specified name is displayed in the Revit external tools menu, and the description is displayed in the status bar when that menu entry is highlighted.

You can set up Revit.exe to be the program to debug your application with.

Set the debugging program to Revit.exe

Start Revit in the debugger by hitting F5 and look at the External Tools submenu under the Tools menu. The command should be accessible and the message box should display when it is selected.

Your new command in Revit

Congratulations on completing your first C# Revit application!

next previous home copyright © 2007-2008 jeremy tammik, autodesk inc. all rights reserved.