Revit buttons support two sizes, small one with 16x16 pixels and big one with 32x32 pixels, so we need to prepare two images for each button. The image can be in BMP, PNG format or any other that the .NET Framework itself can handle.
After the images are created and added into the Revit addin project, please do not forget to mark them as Embedded Resources. Here are some code snippets in both C# and VB.NET which can create a ribbon and add a button onto it:
C# code:
private RibbonPanel CreateRibbonPanel()
{
RibbonPanel panel = _cachedUiCtrApp.CreateRibbonPanel("RevitAddinCS");
PushButtonData pbDataExtCmd = new PushButtonData("ExtCmd", "ExtCmd", Assembly.GetExecutingAssembly().Location, "RevitAddinCS.ExtCmd");
PushButton pbExtCmd = panel.AddItem(pbDataExtCmd) as PushButton;
pbExtCmd.ToolTip = "ExtCmd";
pbExtCmd.LargeImage = BmpImageSource("RevitAddinCS.Resources.ExtCmd32x32.bmp");
pbExtCmd.Image = BmpImageSource("RevitAddinCS.Resources.ExtCmd16x16.bmp");
return panel;
}
private System.Windows.Media.ImageSource BmpImageSource(string embeddedPath)
{
Stream stream = this.GetType().Assembly.GetManifestResourceStream(embeddedPath);
var decoder = new System.Windows.Media.Imaging.BmpBitmapDecoder(stream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
return decoder.Frames[0];
}
VB.NET code:
Function CreateRibbonPanel() As RibbonPanel
Dim panel As RibbonPanel = _cachedUiCtrApp.CreateRibbonPanel("RevitAddinVB")
Dim pbDataExtCmd As PushButtonData = New PushButtonData("ExtCmd", "ExtCmd", Assembly.GetExecutingAssembly().Location, "RevitAddinVB.ExtCmd")
Dim pbExtCmd As PushButton = CType(panel.AddItem(pbDataExtCmd), PushButton)
pbExtCmd.ToolTip = "ExtCmd"
pbExtCmd.LargeImage = BmpImageSource("RevitAddinVB.ExtCmd32x32.bmp")
pbExtCmd.Image = BmpImageSource("RevitAddinVB.ExtCmd16x16.bmp")
Return panel
End Function
Private Function BmpImageSource(ByVal embeddedPath As String) As System.Windows.Media.ImageSource
Dim stream As Stream = Me.GetType().Assembly.GetManifestResourceStream(embeddedPath)
Dim decoder As BmpBitmapDecoder = New System.Windows.Media.Imaging.BmpBitmapDecoder(stream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default)
Return decoder.Frames(0)
End Function
All these can be done by the RevitAddinWizard.
Recent Comments