Friday, 20 February 2009

Simple menu and toolbar in VB

The focus on C# in the Revit SDK code samples can be a hindrance to those of us who prefer VB. Yesterday I had a request from a reader (or is it a viewer? subscriber? follower? what do you call someone that visits your blog?) asking for a sample of VB code that creates a simple menu and toolbar. Well, here's some that should do the trick:


Imports System
Imports System.Collections.Generic
Imports System.Text

Imports Autodesk.Revit


Public Class EdsToolbar
Implements Autodesk.Revit.IExternalApplication


Public Function OnShutdown(ByVal application As Autodesk.Revit.ControlledApplication) As Autodesk.Revit.IExternalApplication.Result Implements Autodesk.Revit.IExternalApplication.OnShutdown

Return Autodesk.Revit.IExternalApplication.Result.Succeeded

End Function


Public Function OnStartup(ByVal application As Autodesk.Revit.ControlledApplication) As _
Autodesk.Revit.IExternalApplication.Result Implements Autodesk.Revit.IExternalApplication.OnStartup

Try



'custom tool bar with buttons
Dim toolBar As Autodesk.Revit.Toolbar = application.CreateToolbar()
toolBar.Name = "Ed's Tools"

'image for toolbar, set to nothing by default
Dim imagePath As String = ""
toolBar.Image = imagePath

'menu
Dim menuItem As MenuItem = application.CreateTopMenu("Ed's Tools")
Dim menuItem1 As MenuItem = menuItem.Append(menuItem.MenuType.BasicMenu, "Open Working Directory", "C:\Program Files\Revit Architecture 2009\Program\EdPittOpenWrkDir.dll", "EdPittOpenWrkDir.OpenWD")

'toolbar
Dim item As ToolbarItem = toolBar.AddItem("C:\Program Files\Revit Architecture 2009\Program\OpenWorkingDirectory.dll", "OpenWorkingDirectory.OpenWorkingDirectory")
item.ItemType = ToolbarItem.ToolbarItemType.BtnRText
item.StatusbarTip = "Open Working Directory"
item.ToolTip = "Open Working Directory"
item.ItemText = "Open Working Directory"


Catch ex As Exception
MsgBox("Failed")
Return IExternalApplication.Result.Failed
End Try

Return IExternalApplication.Result.Succeeded

End Function

End Class

Create a dll from this, and then in your .ini file drop in the following:

[ExternalApplications]
EACount=1
EAClassName1=EdsTools.EdsToolbar
EAAssembly1=C:\Program Files\Revit Architecture 2009\Program\EdsTools.dll

Obviously if you already have some External Applications you need to modify this to suit.

1 comment:

  1. This is good for VB only users, though it should be noted to the reader that in Revit 2010, coming out in just over a months time (approx) the toolbar system is changed, due to the migration to a ribbon.

    See Matt Masons blog for a quick rundown
    http://cadappdev.blogspot.com/2009/02/whats-new-in-revit-2010-api-user.html

    Thanks,
    Rod
    http://roddotnet.blogspot.com

    ReplyDelete

Comments are moderated, so you'll have to wait a little bit before they appear!