We demonstrated creating Revit Ribbon SlideOut with C# before. In this article, let us see how to do so with VB.NET. 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 panelTile As String = "SlideOut"
' 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.Title = panelTile
AddRibbonItemsToRibbonPanel(panel)
Return panel
End Function
Private Sub AddRibbonItemsToRibbonPanel(ByVal panel As RibbonPanel)
Dim assemFullName As String = Assembly.GetExecutingAssembly.Location
Dim assemPath As String = Path.GetDirectoryName(assemFullName)
panel.AddSlideOut()
Dim rbgData2 As RadioButtonGroupData = New RadioButtonGroupData("RadioButtonGroup2")
Dim rbGroup2 As RadioButtonGroup = CType(panel.AddItem(rbgData2), RadioButtonGroup)
Dim rb2MemData1 As ToggleButtonData = New ToggleButtonData("rbg2Mem1", "Toggle21", assemFullName, "Revit2012VBAddin.ExtCmd1")
Dim rb2Mem1 As ToggleButton = rbGroup2.AddItem(rb2MemData1)
rb2Mem1.Image = BmpImageSource("Revit2012VBAddin.Embedded_Item1_16x16.bmp")
rb2Mem1.LargeImage = BmpImageSource("Revit2012VBAddin.Embedded_Item1_32x32.bmp")
Dim rb2MemData2 As ToggleButtonData = New ToggleButtonData("rbg2Mem2", "Toggle22", assemFullName, "Revit2012VBAddin.ExtCmd2")
Dim rb2Mem2 As ToggleButton = rbGroup2.AddItem(rb2MemData2)
rb2Mem2.Image = BmpImageSource("Revit2012VBAddin.Embedded_Item1_16x16.bmp")
rb2Mem2.LargeImage = BmpImageSource("Revit2012VBAddin.Embedded_Item2_32x32.bmp")
Dim rb2MemData3 As ToggleButtonData = New ToggleButtonData("rbg2Mem3", "Toggle23", assemFullName, "Revit2012VBAddin.ExtCmd3")
Dim rb2Mem3 As ToggleButton = rbGroup2.AddItem(rb2MemData3)
rb2Mem3.Image = BmpImageSource("Revit2012VBAddin.Embedded_Item2_16x16.bmp")
rb2Mem3.LargeImage = BmpImageSource("Revit2012VBAddin.Embedded_Item2_32x32.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 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.
The AddIn (API name) or Add-Ins (UI name) Ribbon Tab may look like the following:
Before slide out:
After slide out:
As can be seen, there will be a little arrow beside the panel name if a Ribbon SlideOut has been created. When the arrow is clicked upon, the content of the slide out will show up. Other than that, all ribbon items of the SlideOut behave the same.
The Ribbon Creator of RevitAddinWizard looks cooler, has more features, and supports both C# and VB.NET.
Recent Comments