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.
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.
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
3
Creating a Ribbon Bar for AutoCAD® with VB.NET
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:
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 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:
Imports System.Drawing
Imports System.IO
Imports System.Media
NOTE: Line 2 should contain the name of the image you selected in place of wrench_orange
which was my image name.
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
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!