Tuesday, January 4, 2011

Visual Studio 2010 Revit 2011 Addin Templates

Have you begun using Visual Studio 2010 for your Addin development yet? Well if you have, then this post is for you! Just don't forget to always set your .NET Framework to 3.5

As you begin to develop with the Revit API, it can be annoying to have to duplicate your addin setup code and references to the Revit API. Did you know that you can create custom addin templates for your projects with all references and boiler plate code prepopulated? Just think of all the women that you can impress at the bar with a story about how you can do this!!!

Create a project with no special functionality but with all required references to the Revit API along with a basic command class setup the way you like and an application class as well. This will help make it easier to use the same template for both commands and applications. Links to the prebuilt template files that I use are at the bottom of this post.

You should maintain separate templates for each flavor of Revit that you develop for since the paths to the API references are different.


The provided "Command" class


'.NET common used namespaces
Imports System
Imports System.Windows.Forms
Imports System.Collections.Generic

'Revit.NET common used namespaces
Imports Autodesk.Revit.ApplicationServices
Imports Autodesk.Revit.Attributes
Imports Autodesk.Revit.DB
Imports Autodesk.Revit.UI
Imports Autodesk.Revit.UI.Selection

<Transaction(TransactionMode.Automatic)> _
<Regeneration(RegenerationOption.Manual)> _
Public Class Commands
    Implements IExternalCommand

    ''' <summary>
    ''' Main entry point for every external command.
    ''' </summary>
    ''' <param name="commandData">Provides access to the Revit app and docs</param>
    ''' <param name="message">Return message</param>
    ''' <param name="elements">elements</param>
    ''' <returns>Cancelled, Failed or Succeeded Result code.</returns>
    Public Function Execute(ByVal commandData As ExternalCommandData, _
                            ByRef message As String, _
                            ByVal elements As ElementSet) As Result Implements IExternalCommand.Execute
        Try
            ' Add your code here


            Return Result.Succeeded
        Catch ex As Exception
            ' Add failure handling here

            Return Result.Failed
        End Try

    End Function
End Class


The provided "Application" class


'.NET common used namespaces
Imports System.Windows.Forms
Imports System.Collections.Generic

'Revit.NET common used namespaces
Imports Autodesk.Revit.ApplicationServices
Imports Autodesk.Revit.Attributes
Imports Autodesk.Revit.DB
Imports Autodesk.Revit.UI
Imports Autodesk.Revit.UI.Selection

<Transaction(TransactionMode.Automatic)> _
 <Regeneration(RegenerationOption.Manual)> _
Class Application
    Implements IExternalApplication
    ''' <summary>
    ''' Implement the external application when Revit starts
    ''' before a file or default template is actually loaded.
    ''' </summary>
    ''' <param name="application">Contains the controlled application.</param>
    ''' <returns>Return the status </returns>
    Public Function OnStartup(ByVal application As UIControlledApplication) _
                    As Result Implements IExternalApplication.OnStartup
        ' Add your code here


        ' Return Success
        Return Result.Succeeded
    End Function

    ''' <summary>
    ''' Implement the external application when Revit is about to exit.
    ''' Any documents must have been closed before this method is called.
    ''' </summary>
    ''' <param name="application">Contains the controlled application.</param>
    ''' <returns>Return the status</returns>
    Public Function OnShutdown(ByVal application As UIControlledApplication) _
                    As Result Implements IExternalApplication.OnShutdown

        ' Add your code here


        ' Return Success
        Return Result.Succeeded
    End Function
End Class

All you have to do is export a project as a template (the result will be a zip file) and place them under your current user's profile at:
"%USERPROFILE%\Documents\Visual Studio 2010\Templates\ProjectTemplates\Visual Basic".

It is important to place your templates into a sub directory (we'll name ours "Revit 2011") and to NOT unzip the template files. Leave the zip files intact and this will work just fine. Your directory structure should look something like the image below.


Links to the above mentioned templates can be found here (for those that do not want or know how to create their own):

5 comments:

Jose Guia said...

MUY Bueno .. thanks for sharing!

Joshua S. said...

I am just starting to learn the API and want to apply the massing generation from rooms that you wrote. Will these templates work for Revit 2012?

Don said...

It will work, but there are quite a few modifications that you will need to make due to the changes in the API between 2011 and 2012.

The Boundaries classes are a little different in how they store each segment.

Unknown said...

Thanks for the post!

This morning I converted the templates over to the 2012 version, but unfortunately I keep getting a Null Reference Exception when instantiating the Form_Main that you describe in your Convert Rooms to 3D Masses tutorial:
http://revitnet.blogspot.com/2011/02/rest-of-convert-rooms-to-3d-masses.html

This (I think) is the relevant code from Commands.vb:

Public Class Commands
Implements IExternalCommand
Public Function Execute(ByVal commandData As ExternalCommandData, _
ByRef message As String, _
ByVal elements As ElementSet) As Result Implements IExternalCommand.Execute
Try
Dim myMain As Form_Main
myMain.Show()
Perhaps it's because I never pass the settings variable to the class?

Any advice you have would be much appreciated.

Don said...

why not just download the 2012 versions also posted on this blog...? http://revitnet.blogspot.com/2011/04/revit-2012-addin-templates-for-visual.html

Post a Comment

Note: Only a member of this blog may post a comment.