Revit Ribbon PulldownButton is a ribbon container which can hold some PushButton and Separator items. The PulldownButton ribbon items can be hosted by a RibbonPanel as demonstrated before with both C# and VB.NET code. They can be stacked into a group as well to indicate they are brothers or sisters, related to each other and looking similar.
This is achieved through the AddStackedItems() call. It has two signatures one of which accepts two ribbon item data object arguments and the other three. That implies that only two or three ribbon items can be stacked together. Moreover, the stack group only supports four types of RibbonItem, PulldownButton, PushButton, ComboBox, and TextBox. In this article, we focus on creating stacked PulldownButton items using VB.NET.
In fact, another post has been there for long time discussing the Revit stacked group in detail, good or bad.
Ribbon of Revit API – 5: Stacked Group and AddStackedItems
It is always good to start with some succinct, cool, and still relatively complete code:
' Create two Ribbon Panels to host the same items and add one Panel to the 'AddIn' Ribbon Tab
' and the other to the custom 'RevitAddinWizard' Ribbon Tab if applicable in Revit 2012.
Function CreateRibbonPanel() As RibbonPanel
Dim prefix As String = "ABC" 'VendorId for example
Dim tabName1 As String = "AddIn"
Dim tabName2 As String = "RevitAddinWizard"
Dim panelTile As String = "Revit2012VBAddin"
Dim panelNameFormat As String = "{0}_{1}_{2}_{3}"
' This addes the new Ribbon Panel to the default native 'AddIn' Ribbon Tab in both Revit 2011 and 2012.
Dim panel As RibbonPanel = _cachedUiCtrApp.CreateRibbonPanel(Guid.NewGuid().ToString())
panel.Name = String.Format(panelNameFormat, prefix, tabName1, panelTile, 1) ' Unique and still possible to parse!
panel.Title = panelTile
AddRibbonItemsToRibbonPanel(panel)
' This addes the new Ribbon Panel to the custom 'RevitAddinWizard' Ribbon Tab in Revit 2012 only.
Try ' To avoid exceptions in case the same code is tried in Revit 2011
_cachedUiCtrApp.CreateRibbonTab(tabName2)
Dim panel1 As RibbonPanel = _cachedUiCtrApp.CreateRibbonPanel(tabName2, Guid.NewGuid().ToString())
panel1.Name = String.Format(panelNameFormat, prefix, tabName2, panelTile, 1) ' Unique and still possible to parse!
panel1.Title = panelTile
AddRibbonItemsToRibbonPanel(panel1)
Catch
End Try
Return panel
End Function
' Create Stacked Pulldown Items
Public Sub AddRibbonItemsToRibbonPanel(ByVal panel As RibbonPanel)
Dim assemFullName As String = Assembly.GetExecutingAssembly.Location
Dim assemPath As String = Path.GetDirectoryName(assemFullName)
Dim cnt1Stacked_grp1Data As PulldownButtonData = New PulldownButtonData("cnt1Stacked_grp1", "Pulldown 1")
Dim cnt1Stacked_grp2Data As PulldownButtonData = New PulldownButtonData("cnt1Stacked_grp2", "Pulldown 2")
Dim cnt1Stacked_grp3Data As PulldownButtonData = New PulldownButtonData("cnt1Stacked_grp3", "Pulldown 3")
Dim cnt1Stacked As IList(Of RibbonItem) = panel.AddStackedItems(cnt1Stacked_grp1Data, cnt1Stacked_grp2Data, cnt1Stacked_grp3Data)
Dim cnt1Stacked_grp1 As PulldownButton = CType(cnt1Stacked(0), PulldownButton)
cnt1Stacked_grp1.ToolTip = "Pulldown Group 1 in Stacked"
cnt1Stacked_grp1.Image = BmpImageSource("Revit2012VBAddin.groupsmall.bmp")
Dim cnt1Stacked_grp1_item1Data As PushButtonData = New PushButtonData("cnt1Stacked_grp1_item1", "PushButton 11", assemFullName, "RevitAddinCSProject.ExtCmd1")
Dim cnt1Stacked_grp1_item1 As PushButton = CType(cnt1Stacked_grp1.AddPushButton(cnt1Stacked_grp1_item1Data), PushButton)
cnt1Stacked_grp1_item1.ToolTip = "PushButton 1 in Pulldown 1"
cnt1Stacked_grp1_item1.LargeImage = BmpImageSource("Revit2012VBAddin.smalla.bmp")
Dim cnt1Stacked_grp2 As PulldownButton = CType(cnt1Stacked(1), PulldownButton)
cnt1Stacked_grp2.ToolTip = "Pulldown Group 2 in Stacked"
cnt1Stacked_grp2.Image = BmpImageSource("Revit2012VBAddin.groupsmall.bmp")
Dim cnt1Stacked_grp2_item1Data As PushButtonData = New PushButtonData("cnt1Stacked_grp2_item1", "PushButton 21", assemFullName, "RevitAddinCSProject.ExtCmd1")
Dim cnt1Stacked_grp2_item1 As PushButton = CType(cnt1Stacked_grp2.AddPushButton(cnt1Stacked_grp2_item1Data), PushButton)
cnt1Stacked_grp2_item1.ToolTip = "PushButton 1 in Pulldown 2"
cnt1Stacked_grp2_item1.LargeImage = BmpImageSource("Revit2012VBAddin.smalla.bmp")
Dim cnt1Stacked_grp2_item2Data As PushButtonData = New PushButtonData("cnt1Stacked_grp2_item2", "PushButton 22", assemFullName, "RevitAddinCSProject.ExtCmd2")
Dim cnt1Stacked_grp2_item2 As PushButton = CType(cnt1Stacked_grp2.AddPushButton(cnt1Stacked_grp2_item2Data), PushButton)
cnt1Stacked_grp2_item2.ToolTip = "PushButton 2 in Pulldown 2"
cnt1Stacked_grp2_item2.LargeImage = BmpImageSource("Revit2012VBAddin.smallb.bmp")
Dim cnt1Stacked_grp3 As PulldownButton = CType(cnt1Stacked(2), PulldownButton)
cnt1Stacked_grp3.ToolTip = "Pulldown Group 3 in Stacked"
cnt1Stacked_grp3.Image = BmpImageSource("Revit2012VBAddin.groupsmall.bmp")
Dim cnt1Stacked_grp3_item1Data As PushButtonData = New PushButtonData("cnt1Stacked_grp3_item1", "PushButton 31", assemFullName, "RevitAddinCSProject.ExtCmd1")
Dim cnt1Stacked_grp3_item1 As PushButton = CType(cnt1Stacked_grp3.AddPushButton(cnt1Stacked_grp3_item1Data), PushButton)
cnt1Stacked_grp3_item1.ToolTip = "PushButton 1 in Pulldown 3"
cnt1Stacked_grp3_item1.LargeImage = BmpImageSource("Revit2012VBAddin.smalla.bmp")
Dim cnt1Stacked_grp3_item2Data As PushButtonData = New PushButtonData("cnt1Stacked_grp3_item2", "PushButton 32", assemFullName, "RevitAddinCSProject.ExtCmd2")
Dim cnt1Stacked_grp3_item2 As PushButton = CType(cnt1Stacked_grp3.AddPushButton(cnt1Stacked_grp3_item2Data), PushButton)
cnt1Stacked_grp3_item2.ToolTip = "PushButton 2 in Pulldown 3"
cnt1Stacked_grp3_item2.LargeImage = BmpImageSource("Revit2012VBAddin.smallb.bmp")
Dim cnt1Stacked_grp3_item3Data As PushButtonData = New PushButtonData("cnt1Stacked_grp3_item3", "PushButton 33", assemFullName, "RevitAddinCSProject.ExtCmd3")
Dim cnt1Stacked_grp3_item3 As PushButton = CType(cnt1Stacked_grp3.AddPushButton(cnt1Stacked_grp3_item3Data), PushButton)
cnt1Stacked_grp3_item3.ToolTip = "PushButton 3 in Pulldown 3"
cnt1Stacked_grp3_item3.LargeImage = BmpImageSource("Revit2012VBAddin.smallc.bmp")
End Sub
' Retrieve a Bitmap image resource
Public Shared Function BmpImageSource(ByVal embeddedPath As String) As System.Windows.Media.ImageSource
Dim stream As Stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(embeddedPath)
Dim decoder = New System.Windows.Media.Imaging.BmpBitmapDecoder(stream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default)
Return decoder.Frames(0)
End Function
The code may look a bit confusing but that is exactly we have to do to create good stacked PulldownButton items. Let us get it straight in words as well.
• Create two or three (cannot be more or less) PulldownButtonData objects;
• Call the AddStackedItems() method against a RibbonPanel and pass the PulldownButtonData instances to its arguments;
• Collect back the stacked items into a List;
• Cast each stacked item to its right type, the PulldownButton, here;
• Set its necessary properties such as ToolTip and Image;
• Add some PushButton and Separator items to the PulldownButton container.
• Do similar to the other PulldownButton members in the List.
The BmpImageSource() is a helper method that can be used by a Ribbon item to retrieve a specific bitmap image for its Image or LargeImage property from the Embedded Resources. It may be worth of mentioning the resource name in a few words. In VB.NET, it is generally the project Default Namespace plus the image file name regardless whether the file is put into a project folder or not. However, in C#, things are different. We have to add the folder name if any in between.
The CreateRibbonPanel() is another helper method that will create Ribbon Panels and add them to the default native ‘AddIn’ (API name, the UI name is ‘Add-Ins’) Ribbon Tab and to our custom ‘RevitAddinWizard’ Ribbon Tab (the API name and the UI name are the same) respectively. It applies some good naming conventions and follows some good coding practices such as always feeding a GUID to the panel constructor and then setting its Name as easy to remember/parse and still unique and its Title as good for display which can be duplicate.
The AddIn (API name) or Add-Ins (UI name) Ribbon Tab may look like the following:
The RevitAddinWizard (both API and UI name) Ribbon Tab may look like this:
As can be seen, except for in different Ribbon Tabs, the Ribbon Panel, the Stacked Group, and its three PulldownButton items look the same. So do those hosted PushButton items.
The RevitAddinWizard is going to provide the VB.NET version of the Ribbon Creator soon.
Recent Comments