The Revit Ribbon PushButton is a reaction item that will trigger a command when clicked. Revit PushButton 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 PushButton 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 PushButton items
Public Sub AddRibbonItemsToRibbonPanel(ByVal panel As RibbonPanel)
Dim assemFullName As String = Assembly.GetExecutingAssembly.Location
Dim assemPath As String = Path.GetDirectoryName(assemFullName)
Dim cnt1Stacked_grp0_item1Data As PushButtonData = New PushButtonData("cnt1Stacked_grp0_item1", "PushButton 1", assemFullName, "RevitAddinCSProject.ExtCmd1")
Dim cnt1Stacked_grp0_item2Data As PushButtonData = New PushButtonData("cnt1Stacked_grp0_item2", "PushButton 1", assemFullName, "RevitAddinCSProject.ExtCmd2")
Dim cnt1Stacked_grp0_item3Data As PushButtonData = New PushButtonData("cnt1Stacked_grp0_item3", "PushButton 1", assemFullName, "RevitAddinCSProject.ExtCmd3")
Dim cnt1Stacked As IList(Of RibbonItem) = panel.AddStackedItems(cnt1Stacked_grp0_item1Data, cnt1Stacked_grp0_item2Data, cnt1Stacked_grp0_item3Data)
Dim cnt1Stacked_grp0_item1 As PushButton = CType(cnt1Stacked(0), PushButton)
cnt1Stacked_grp0_item1.ToolTip = "PushButton 1 in a Stacked Group"
cnt1Stacked_grp0_item1.Image = BmpImageSource("Revit2012VBAddin.smalla.bmp")
Dim cnt1Stacked_grp0_item2 As PushButton = CType(cnt1Stacked(1), PushButton)
cnt1Stacked_grp0_item2.ToolTip = "PushButton 2 in a Stacked Group"
cnt1Stacked_grp0_item2.Image = BmpImageSource("Revit2012VBAddin.smallb.bmp")
Dim cnt1Stacked_grp0_item3 As PushButton = CType(cnt1Stacked(2), PushButton)
cnt1Stacked_grp0_item3.ToolTip = "PushButton 3 in a Stacked Group With a Big Image"
cnt1Stacked_grp0_item3.Image = BmpImageSource("Revit2012VBAddin.BigC.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 PushButton items. Let us get it straight in words as well.
• Create two or three (cannot be more or less) PushButtonData objects;
• Call the AddStackedItems() method against a RibbonPanel and pass the PushButtonData instances to its arguments;
• Collect back the stacked items into a List;
• Cast each stacked item to its right type, the PushButton, here;
• Set its necessary properties such as ToolTip and Image
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 PushButton items look the same. But wait, why does not the image of the third PushButton look good?
That is because a big image was assigned to the Image property as commented in the code. By the way, the LargeImage property is useless for stacked PushButton items.
The RevitAddinWizard is going to provide the VB.NET version of the Ribbon Creator soon.
Recent Comments