Hello World VB.NET
next previous home

In this first lab, we shall learn about creating a VB.Net project, adding references to required assemblies, and write our first VB.Net Revit application "Hello World" program.

Start Microsoft Visual Studio .NET 2008, select File > New > Project..., select Visual Basic 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 double-clicking on the 'My Project' item in the solution explorer, selecting 'References' and then the 'Add...' button. 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\Autodesk Revit Architecture 2010\Program\RevitAPI.dll'.

Add a reference to RevitAPI.dll

After adding the reference, double-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.

Set 'Copy Local' to false

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

We need to import some namespaces from the referenced assemblies. There are 2 ways to do this in VB.NET:

For this lab, we only need the Autodesk.Revit namespace. We will be using others in later labs.

Now we need to implement the Revit external command. To do so, implement a class using any name which implements the Revit API IExternalCommand interface. If typing (rather than cut-and-pasting from below), Intellisense should automatically create the Execute() function skeleton with the specific argument list (more about these later), as below.

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

Imports Autodesk.Revit

Public Class Lab1_1_HelloWorld
    Implements IExternalCommand

    Public Function Execute( _
        ByVal commandData As Autodesk.Revit.ExternalCommandData, _
        ByRef message As String, _
        ByVal elements As Autodesk.Revit.ElementSet) _
    As Autodesk.Revit.IExternalCommand.Result _
    Implements Autodesk.Revit.IExternalCommand.Execute

        MsgBox("Hello World")
        Return IExternalCommand.Result.Succeeded

    End Function

End Class

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 VB.NET Revit application!

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