We demonstrated stacking some similar ribbon items such as ComboBox, PulldownButton, PushButton and TextBox together into a Stack Group before. In this article, let us see if it is possible to stack different items together and what the Stacked Group look like.
This is possible and can still be 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 either only two or three ribbon items (the same kind or different kinds) can be stacked together.
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 Various 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 in Stacked1", assemFullName, "Revit2012VBAddin.ExtCmd2")
Dim cnt1Stacked_grp1Data As PulldownButtonData = New PulldownButtonData("cnt1Stacked_grp1", "PulldownButton Group")
Dim cnt1Stacked_grp2Data As ComboBoxData = New ComboBoxData("cnt1Stacked_grp2")
Dim cnt1Stacked As IList(Of RibbonItem) = panel.AddStackedItems(cnt1Stacked_grp0_item1Data, cnt1Stacked_grp1Data, cnt1Stacked_grp2Data)
Dim cnt1Stacked_grp0_item1 As PushButton = CType(cnt1Stacked(0), PushButton)
cnt1Stacked_grp0_item1.ToolTip = "This is PushButton in Stacked1"
cnt1Stacked_grp0_item1.Image = BmpImageSource("Revit2012VBAddin.smalld.bmp")
Dim cnt1Stacked_grp1 As PulldownButton = CType(cnt1Stacked(1), PulldownButton)
cnt1Stacked_grp1.ToolTip = "This is PulldownButton Group in Stacked1"
cnt1Stacked_grp1.Image = BmpImageSource("Revit2012VBAddin.ExtCmd.bmp")
Dim cnt1Stacked_grp1_item1Data As PushButtonData = New PushButtonData("cnt1Stacked_grp1_item1", "PushButton in Stacked Pulldown", assemFullName, "Revit2012VBAddin.ExtCmd1")
Dim cnt1Stacked_grp1_item1 As PushButton = CType(cnt1Stacked_grp1.AddPushButton(cnt1Stacked_grp1_item1Data), PushButton)
cnt1Stacked_grp1_item1.ToolTip = "This is PushButton in Stacked Pulldown"
cnt1Stacked_grp1_item1.LargeImage = BmpImageSource("Revit2012VBAddin.smalla.bmp")
Dim cnt1Stacked_grp2 As Autodesk.Revit.UI.ComboBox = CType(cnt1Stacked(2), Autodesk.Revit.UI.ComboBox)
cnt1Stacked_grp2.ItemText = "ComboBox Group"
cnt1Stacked_grp2.ToolTip = "This is ComboBox Group in Stacked1"
Dim cnt1Stacked_grp2_item1Data As ComboBoxMemberData = New ComboBoxMemberData("cnt1Stacked_grp2_item1", "ComboBoxMemeber")
Dim cnt1Stacked_grp2_item1 As ComboBoxMember = cnt1Stacked_grp2.AddItem(cnt1Stacked_grp2_item1Data)
cnt1Stacked_grp2_item1.ToolTip = "This is ComboBoxMemeber in Stacked ComboxBox"
cnt1Stacked_grp2_item1.Image = BmpImageSource("Revit2012VBAddin.smallb.bmp")
Dim cnt2Stacked_grp0_item1Data As TextBoxData = New TextBoxData("cnt2Stacked_grp0_item1")
Dim cnt2Stacked_grp0_item2Data As PushButtonData = New PushButtonData("cnt2Stacked_grp0_item2", "PushButton in Stacked2", assemFullName, "Revit2012VBAddin.ExtCmd")
Dim cnt2Stacked_grp1Data As ComboBoxData = New ComboBoxData("cnt2Stacked_grp1")
Dim cnt2Stacked As IList(Of RibbonItem) = panel.AddStackedItems(cnt2Stacked_grp0_item1Data, cnt2Stacked_grp0_item2Data, cnt2Stacked_grp1Data)
Dim cnt2Stacked_grp0_item1 As Autodesk.Revit.UI.TextBox = CType(cnt2Stacked(0), Autodesk.Revit.UI.TextBox)
cnt2Stacked_grp0_item1.ToolTip = "This is TextBox in Stacked2"
cnt2Stacked_grp0_item1.Value = "TextBox in Stacked2"
cnt2Stacked_grp0_item1.Image = BmpImageSource("Revit2012VBAddin.CheckAndUpdate.bmp")
cnt2Stacked_grp0_item1.ShowImageAsButton = True
AddHandler cnt2Stacked_grp0_item1.EnterPressed, AddressOf CallbackOfTextBoxEnterPressed
Dim cnt2Stacked_grp0_item2 As PushButton = CType(cnt2Stacked(1), PushButton)
cnt2Stacked_grp0_item2.ToolTip = "This is PushButton in Stacked2"
cnt2Stacked_grp0_item2.Image = BmpImageSource("Revit2012VBAddin.smallc.bmp")
Dim cnt2Stacked_grp1 As Autodesk.Revit.UI.ComboBox = CType(cnt2Stacked(2), Autodesk.Revit.UI.ComboBox)
cnt2Stacked_grp1.ItemText = "ComboBox Group"
cnt2Stacked_grp1.ToolTip = "This is ComboBox Group in Stacked2"
Dim cnt2Stacked_grp1_item1Data As ComboBoxMemberData = New ComboBoxMemberData("cnt2Stacked_grp1_item1", "ComboBoxMemeber")
Dim cnt2Stacked_grp1_item1 As ComboBoxMember = cnt2Stacked_grp1.AddItem(cnt2Stacked_grp1_item1Data)
cnt2Stacked_grp1_item1.ToolTip = "This is ComboBoxMemeber in Stacked ComboxBox"
cnt2Stacked_grp1_item1.Image = BmpImageSource("Revit2012VBAddin.smallb.bmp")
End Sub
Public Shared Sub CallbackOfTextBoxEnterPressed(ByVal sender As Object, ByVal args As TextBoxEnterPressedEventArgs)
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 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 items look the same. So do those hosted member items.
The RevitAddinWizard is going to provide the VB.NET version of the Ribbon Creator soon.
Recent Comments