Hello World External Application
next previous home

In this lab, we will create a simple external application, which only shows a hello message box when Revit starts up. External application provides an opportunity to do initialization tasks when Revit starts up, and do final clean up tasks when Revit shuts down.

First of all, create a class derived from the IExternalApplication interface. The interface has two methods, OnStartup() and OnShutdown(). OnStartup() is called when Revit starts up, and OnShutdown() is called when Revit shuts down. Implement the two methods in a new class.

#region Namespaces
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using WinForms = System.Windows.Forms;
using Autodesk.Revit;
using Autodesk.Revit.Elements;
using Autodesk.Revit.Symbols;
using Autodesk.Revit.Events;
using System.Diagnostics;
#endregion // Namespaces

namespace Labs
{
  public class Lab6_1_HelloWorldExternalApplication : IExternalApplication
  {
    public IExternalApplication.Result OnStartup(
      ControlledApplication a )
    {
      return IExternalApplication.Result.Succeeded;
    }

    public IExternalApplication.Result OnShutdown(
      ControlledApplication a )
    {
      return IExternalApplication.Result.Succeeded;
    }
  }
}
#Region "Namespaces"
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.IO
Imports WinForms = System.Windows.Forms
Imports Autodesk.Revit
Imports Autodesk.Revit.Events
Imports System.Diagnostics
#End Region

Namespace Labs
    Public Class Lab6_1_HelloWorldExternalApplication
        Implements IExternalApplication

        Public Function OnStartup(ByVal a As ControlledApplication) _
            As IExternalApplication.Result _
            Implements IExternalApplication.OnStartup

            Return IExternalApplication.Result.Succeeded

        End Function

        Public Function OnShutdown(ByVal a As ControlledApplication) _
            As IExternalApplication.Result _
            Implements IExternalApplication.OnShutdown

            Return IExternalApplication.Result.Succeeded

        End Function

    End Class
End Namespace

The project should already compile, link and load into Revit properly, but does not have any visible effect when run.

Add code to display the hello message box:

  public class Lab6_1_HelloWorldExternalApplication : IExternalApplication
  {
    public IExternalApplication.Result OnStartup(
      ControlledApplication a )
    {
      LabUtils.InfoMsg( "Hello World from an external application in C#." );
      return IExternalApplication.Result.Succeeded;
    }

    public IExternalApplication.Result OnShutdown(
      ControlledApplication a )
    {
      return IExternalApplication.Result.Succeeded;
    }
  }

      Public Class Lab6_1_HelloWorldExternalApplication
        Implements IExternalApplication

        Public Function OnStartup(ByVal a As ControlledApplication) _
            As IExternalApplication.Result _
            Implements IExternalApplication.OnStartup

            MsgBox("Hello World from an external application in VB.")
            Return IExternalApplication.Result.Succeeded

        End Function

        Public Function OnShutdown(ByVal a As ControlledApplication) _
            As IExternalApplication.Result _
            Implements IExternalApplication.OnShutdown

            Return IExternalApplication.Result.Succeeded

        End Function

    End Class

The project should still compile and link properly.

We need to let Revit know how to load this external applications. The loading settings are similar to the ones required to load an external command, except the key names are different. Replace the prefix "EC" with "EA", which means External Application. Add the entries below to Revit.ini. The name and description are not required for external applications, since they define their own user interface.

[ExternalApplications]
EACount=1
EAAssembly1=C:\tmp\revit\LabsCode\LabsCode\bin\Debug\LabsCode.dll
EAClassName1=Labs.Lab6_1_HelloWorldExternalApplication

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

Restart Revit. The Hello message box will be shown automatically.

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