Ribbon嵌入图片Ribbon Embed Image
An embedded bitmap resource with its build action set to 'Embedded Resource'.Krispy's method GetEmbeddedImage to read the embedded bitmap resource and return a BitmapSource instance.A method AddRibbonPanel to create a custom ribbon panel labelled "Ribbon Embed Image" containing a push button labelled "Hello" and displaying the embedded resource bitmap as its image.An external application calling AddRibbonPanel in its OnStartup method.An external command implementing a trivial Execute method which is invoked by the Hello button.This is what the resulting custom ribbon panel looks like when I drag it off the ribbon to a free floating state:http://thebuildingcoder.typepad.com/.a/6a00e553e1689788330120a656ffd8970b-800wi
Let's look at the source code of the items mentioned above one by one. There is no code to display for the bitmap, but as said, it needs to have its build action set to 'Embedded Resource'.Here are the namespaces we make use of:using System;using System.Collections.Generic;using System.Diagnostics;using System.IO;usingSystem.Reflection;using System.Windows.Media.Imaging;using Autodesk.Revit;Besides the standard references to System and RevitAPI, these will require references to the following assemblies:
[*]PresentationCore
[*]WindowsBase
The application defines its own namespace RibbonEmbedImage, and within it two classes named App and Command, i.e. the following global source code structure:namespace RibbonEmbedImage{public class App : IExternalApplication{} public class Command :IExternalCommand{}}Within the App class, we first of all define a constant message to be used as the ribbon panel button tooltip and displayed by the external command invoked by the button:public const string Message ="Ribbon Embed Image says 'Hello world'"+ " via an embedded bitmap resource.";Then we add the definition of Krispy's method GetEmbeddedImage to read the embedded bitmap resource and return a BitmapSource instance:static BitmapSource GetEmbeddedImage( string name ){try{ Assembly a = Assembly.GetExecutingAssembly(); Stream s = a.GetManifestResourceStream( name ); return BitmapFrame.Create( s );}catch{ return null;}}It extracts a bitmap resource embedded within the executing assembly and returns a corresponding BitmapSource object. The 'name' argument is the fully qualified resource name, i.e. includes the application namespace etc.Next comes the AddRibbonPanel method which creates a custom ribbon panel labelled "Ribbon Embed Image" containing a push button labelled "Hello" and displaying the embedded resource bitmap as its image:static void AddRibbonPanel(ControlledApplication a ){string path =Assembly.GetExecutingAssembly().Location; PushButtonData data = new PushButtonData( "Hello", "Hello", path, "RibbonEmbedImage.Command" ); BitmapSource bitmap = GetEmbeddedImage( "RibbonEmbedImage.Bitmap1.bmp" ); data.Image = bitmap;data.LargeImage = bitmap;data.ToolTip = Message; RibbonPanel panel = a.CreateRibbonPanel( "Ribbon Embed Image" ); RibbonItem item = panel.AddButton( data );}The only remaining part of the external application class is the implementation of the required interface methods and the call to AddRibbonPanel in the OnStartup method:public IExternalApplication.Result OnStartup( ControlledApplication a ){AddRibbonPanel( a );returnIExternalApplication.Result.Succeeded;} public IExternalApplication.Result OnShutdown(ControlledApplication a ){return IExternalApplication.Result.Succeeded;}All that is needed for the external command is the implementation of the required interface, i.e. the Execute method. It reuses the application class Message constant and simply passes it back as a warning message to Revit: public IExternalCommand.Result Execute( ExternalCommandData commandData, ref string message, ElementSet elements ) { message = App.Message; return IExternalCommand.Result.Failed; }Here is the complete RibbonEmbedImage source code and Visual Studio solution.Many thanks to Krispy for providing this solution! I hope it will prove useful to everybody needing bitmaps or any other resources in a Revit plug-in, whether it be for ribbon panel buttons or other purposes.
顶起来…………
页:
[1]