You are on page 1of 9

Creating a Ribbon Bar for AutoCAD® with VB.

NET
Mike Tuersley – Ohio Gratings, Inc.

CP208-2 AutoCAD 2010 introduces a new API for creating Ribbon Bars within the AutoCAD environment.
The class will examine the ribbon bar-specific classes available within the AcRibbon library as well as each of the
elements that make up a Ribbon control including tabs, panels, rows, commands, and tooltips.

About the Speaker:


Mike works for Ohio Gratings Inc. as the Technical Lead and Senior Developer focusing on enterprise level
automation using the latest Microsoft® technologies. Before this, he was the founding member and senior lead
application developer for RAND IMAGINiT’s National Services Team, which focuses on providing customization
services to meet customer visions. Mike has been customizing AutoCAD® since release 2.5 and he is a past
columnist for Cadalyst magazine. For 6 years, he wrote the AutoCAD "Help Clinic" and the "CAD Clinic"; the latter
focused on teaching AutoCAD programming topics.
Mike can be contacted at mike.tuersley@mtuersley.com
Creating a Ribbon Bar for AutoCAD® with VB.NET

Introduction
Love it or hate it, the Ribbon is the latest innovation in user interfaces. It was introduced by
Microsoft in Office 2007 and Autodesk followed in AutoCAD 2009. The Ribbon is created using
the Windows Presentation Foundation (WPF) that is part of all Microsoft .NET Frameworks
since 3.0. The purpose of WPF is to build rich Windows client applications and separate the
user interface elements from the code. While WPF can be a little daunting to learn, Autodesk
has stepped up and provided an API that wraps their stock controls. This allows developers to
derive their own Ribbon controls and customize the Ribbon without having to know WPF.

In AutoCAD 2009, there was an AcRibbon library that provided the interface to the WPF
content. With AutoCAD 2010, Autodesk overhauled the API to simplify development and
included the AcRibbon objects within the AdWindow library.

When creating a Ribbon, there is a hierarchy that must be followed:

 Ribbon: hosts all the content and is similar to a Windows Form Tab control
 RibbonBar: is similar to a TabPage in a Tab control but it can only contain RibbonPanels
 RibbonPanel: can contain rows (similar to toolbars), subpanels, panel separators, and
controls. A RibbonPanel is intended to organize commands by task as
seen above where all text related commands are in one panel, all draw
related in another, etc.

2
Creating a Ribbon Bar for AutoCAD® with VB.NET

Creating a Simple RibbonBar


Start by creating a new Class Library project within Visual Studio 2008 (Visual Studio 2005,
VBExpress 2008 or VBExpress 2005 could also be used although 2008 versions are
recommended).

Next, add references to: AcDbMgd, AcMgd,


AdWindows, PresentationCore,
PresentationFramework and WindowsBase. The
latter references are part of WPF.

NOTE: To create the project and limit the number of


references that need added, use either a WPF
UserControl Library and replace the WPF
UserControl with a Class or use the AutoCAD .NET
Wizard available from the Autodesk website.

3
Creating a Ribbon Bar for AutoCAD® with VB.NET

Finally, add the following code to the Class:

1: Imports System
2: Imports Autodesk.AutoCAD.EditorInput
3: Imports Autodesk.AutoCAD.Runtime
4: Imports Autodesk.AutoCAD.Windows
5: Imports Autodesk.Windows
6:
7:
8: Public Class Class1
9:
10: Implements IExtensionApplication
11:
12: Public Sub Initialize() Implements IExtensionApplication.Initialize
13: createRibbon()
14: End Sub
15:
16: Public Sub Terminate() Implements IExtensionApplication.Terminate
17:
18: End Sub
19:
20: Private Sub CreateRibbon()
21: 'declare a ribboncontrol object
22: Dim ribCntrl As RibbonControl = ComponentManager.Ribbon
23: 'create a ribbontab
24: Dim ribTab As New RibbonTab()
25: 'set a few properties
26: ribTab.Title = "AU2009"
27: ribTab.Id = "AU2009"
28: 'add the tab to the ribbon
29: ribCntrl.Tabs.Add(ribTab)
30: 'set as active tab
31: ribTab.IsActive = True
32: End Sub
33:
34: End Class

The majority of this code should be familiar to anyone who has written code for AutoCAD
especially anything that is user interface related. The ribbon-specific code is all contained
within the CreateRibbon subroutine.

Build the project and test it out. If the code was added properly, a blank ribbon panel should
have been added to AutoCAD with a tab entitled “AU2009”:

4
Creating a Ribbon Bar for AutoCAD® with VB.NET

Now assuming that worked, let’s add some controls to the Ribbon. Here are a few of the most
popular controls:

RibbonButton RibbonRowPanel

RibbonCombo RibbonSpinner

RibbonGallery RibbonSplitButton

RibbonListButton RibbonTextBox

RibbonMenuButton RibbonToggleButton

More can be found by looking in the Object Browser inside Visual Studio and expanding
AdWindows > Autodesk.Windows. Go back into the project and add the following code:

1: Private Sub addContent(ByVal ribTab As RibbonTab)


2: 'create the panel source
3: Dim ribSourcePanel As RibbonPanelSource = New RibbonPanelSource()
4: ribSourcePanel.Title = "Dev Tools"
5: 'now the panel
6: Dim ribPanel As New RibbonPanel()
7: ribPanel.Source = ribSourcePanel
8: ribTab.Panels.Add(ribPanel)
9: 'create a button
10: Dim ribButton1 As RibbonButton = New RibbonButton()
11: ribButton1.Text = "Net load"
12: ribButton1.CommandParameter = "NETLOAD "
13: ribButton1.ShowText = True
14: ribButton1.CommandHandler = New AdskCommandHandler()
15: 'add the button
16: ribSourcePanel.Items.Add(ribButton1)
17: End Sub

5
Creating a Ribbon Bar for AutoCAD® with VB.NET

In the CreateRibbon subroutine, add a call to addContent and pass the RibbonTab as the
parameter: addContent(ribTab).

Looking at Line 14 of the addContent sub on the previous page, there is a CommandHandler
property being set to a new AdskCommandHandler. This needs some explanation.

In regular WPF, the UI is separate from the code-behind as mentioned earlier. When the WPF
was wrapped into the API, the click event on the button was not exposed – either accidentally
or due to technical issues. Therefore, the button needs to have a command string just like
toolbar buttons have and a command handler so the code knows what to do with the command
string. Since the command IS a string, the command is executed by being sent to the
command line. While there are a couple of solutions to this problem, there is a standard boiler
plate solution that works well that is easy to implement.

To the project, add a new class as follows:

1: Public Class AdskCommandHandler


2:
3: Implements System.Windows.Input.ICommand
4:
5: Public Function CanExecute(ByVal parameter As Object) As Boolean _
Implements System.Windows.Input.ICommand.CanExecute
6: Return True
7: End Function
8:
9: Public Event CanExecuteChanged As EventHandler Implements _
System.Windows.Input.ICommand.CanExecuteChanged
10:
11: Public Sub Execute(ByVal parameter As Object) Implements _
System.Windows.Input.ICommand.Execute
12: Dim ribBtn As RibbonButton = TryCast(parameter, RibbonButton)
13: If ribBtn IsNot Nothing Then
14: Autodesk.AutoCAD.ApplicationServices.Application.
DocumentManager.MdiActiveDocument.
SendStringToExecute(_
ribBtn.CommandParameter, True, False, True)
15: End If
16: End Sub
17:
18: End Class

To oversimplify this, the Execute sub tests the supplied object to determine what type of object
it is – in this case, we are supplying a RibbonButton. Then, based upon the object, it sends the
object’s CommandParameter to the command prompt. In this specific case, it will send the
command “NETLOAD “ that was set in Line 12 of the AddContent function.

6
Creating a Ribbon Bar for AutoCAD® with VB.NET

As stated, this class is a boiler plate. The Execute sub can be expanded to test for each type of
control that is added to the Ribbon and then pass the specific control’s command to the
command line. To test this, build and run the application. The result should look like this:

Selecting the Net Load button should execute the Netload command.

Next, let’s add an image to the button. Back in the project, right click on the solution and select
“Add new item…” then select a Resources file:

7
Creating a Ribbon Bar for AutoCAD® with VB.NET

After it has loaded, pick Add Resource > Add Existing File… at the top and select an image
from somewhere on your computer. I prefer to use PNG files but a bitmap would work as well:

Add a reference to System.Drawing and the following imports statements:

Imports System.Drawing
Imports System.IO
Imports System.Media

Then this code:

1: Private Shared Function LoadImage() As Imaging.BitmapImage


2: Dim pic As Bitmap = My.Resources.Resource1.wrench_orange
3: Dim ms As New MemoryStream()
4: pic.Save(ms, Imaging.ImageFormat.Png)
5: Dim bi As New Imaging.BitmapImage()
6: bi.BeginInit()
7: bi.StreamSource = ms
8: bi.EndInit()
9: Return bi
10: End Function

NOTE: Line 2 should contain the name of the image you selected in place of wrench_orange
which was my image name.

Lastly, add this line of code to where we setup RibbonButton1:

ribButton1.Image = LoadImage()

After building and running the project, the RibbonButton should now
look like this:

8
Creating a Ribbon Bar for AutoCAD® with VB.NET

The Next Steps…


This has just been an introduction into creating a RibbonBar in AutoCAD 2010 and there is
plenty more to learn and do with this sample project. A good place to start is the free video
courses offered by Autodesk: http://www.adskconsulting.com/adn/cs/api_course_sched.php.
There are two good courses on using WPF in AutoCAD as well as courses on CUI and
deployment.

I hope you enjoyed this course and you continue to explore what is possible with Ribbons. If
you have any questions, please contact me through my website [www.mtuersley.com] or email
me at mike.tuersley@mtuersley.com and please include this course Id (CP208-2) in the subject
line. Thank you!