Ribbon Creation Utility

Creating and populating a ribbon panel is not a very complicated matter, although it normally does require instantiating and manipulating quite a number of different cooperating class instances.

Now Victor Chekalin, or Виктор Чекалин, presents a much simpler solution for this using dedicated wrapper classes.

In his own words:

Create Buttons without Effort

As you know, you can create your own buttons or even your own tab in the Revit ribbon to perform a command.

For me, creating button always wasn’t easy.
Especially I didn’t like to pass the assembly location and name of the external Command class to perform when I click the button.
Also, the standard API requires me to create an ImageSource for each button image while I have an image stored in the assembly resources.

To avoid these troubles and ease my life I decided to create a utility which helps me to create my Revit ribbon buttons.
The utility I created is really useful for me and I want to share it with everybody.

Here are the main features:

  • Fluent interface:
    You can create all your buttons in one single line of code.
  • Command name as generic parameter:
    You don’t need to write the command name as text and set the assembly location.
  • Images from resource:
    You can easily use images from resources.

The sample below illustrates all this.

This is the result in the Revit ribbon:

Victor's ribbon utility sample

Look at the source code generating it:


  Ribbon ribbon = new Ribbon( a );
 
  ribbon.Tab( "MyTab" )
    .Panel( "Panel1" )
 
    .CreateButton( "btn1", "Button1",
      typeof( Command1 ),
      btn => btn
        .SetLargeImage(
          Resources._1348119708_face_monkey_32 )
        .SetSmallImage(
          Resources._1348119708_face_monkey_16 ) )
 
    .CreateSeparator()
 
    .CreateButton<Command2>( "btn2", "Button2",
      btn => btn
        .SetLongDescription(
          "This is a description of the button2" )
        .SetLargeImage(
          Resources._1348119643_face_glasses_32 ) )
 
    .CreateStackedItems( si => si
 
      .CreateButton<Command3>( "btn3", "Button3",
        btn => btn.SetSmallImage( Resources
          ._1348119594_preferences_system_16 ) )
 
      .CreateButton<Command4>( "btn4", "Button4",
        btn => btn.SetSmallImage( Resources
          ._1348119621_internet_web_browser_16 ) ) )
 
    .CreateSeparator()
 
    .CreateStackedItems( si => si
 
      .CreateButton<Command3>( "btn3_1", "Button3",
        btn => btn.SetSmallImage( Resources
          ._1348119594_preferences_system_16 ) )
 
      .CreateButton<Command4>( "btn4_1", "Button4",
        btn => btn.SetSmallImage( Resources
          ._1348119621_internet_web_browser_16 ) )
 
      .CreateButton<Command1>( "btn1_1", "Button1",
        btn => btn.SetSmallImage( Resources
          ._1348119553_face_smile_big_16 ) ) );

Pretty easy, isn’t it?

At first I set the tab, where I want to create my buttons.
I can specify my own tab or one of the system tabs, e.g. Autodesk.Revit.UI.Tab.AddIns.
If the tab doesn’t exist, it will be created.
So, you can easily use a tab from a different add-in.
You specify a panel to group the buttons on the tab.
Next, the most important part, create the buttons.
I won’t describe how to create the buttons.
The code is self-explanatory.

My utility currently supports push button, push button as stacked item and separator.

Here is the
compiled assembly.

The source code is available on

GitHub
.
You can also download it as a

zip archive file
.

It provides the namespaces VCRevitRibbonUtil and VCRevitRibbonUtil.Helpers containing the following classes:

  • StackedItem
  • Button
  • Panel
  • Ribbon
  • Tab
  • Helpers.BitmapSourceConverter

Many thanks to Victor for this neat time and labour saving utility!


Comments

14 responses to “Ribbon Creation Utility”

  1. Thanks Jeremy and Victor Chekalin for sharing this utility.
    I used this today and everything works fine except one minor issue.
    .SetLongDescription for button description takes few seconds to show up. Why is that?
    When I hover over normal Revit buttons, quick+short description comes up very quickly and then detailed description comes after few seconds.
    Is there a way to add .SetDescription (equivalent of .Tooltip?? standard Revit ribbon creation) method to add quick & short description?
    Thanks
    Rahul

  2. Hi, Rahul.
    Glad you like my RibbonUtil and found it useful.
    Can you provide some code how do you set quick+short description for a button? SetLongDescription method use LongDescription property of PushButtonData class.
    As I undertand you want set Tooltip as well. I can add this method later. Or you can download source code and add this method yourself.
    Regards,
    Victor.

  3. Thanks Victor,
    Yes od course! you have provided the source code so I will add a method for .Tooltip.
    Thanks for this utility. It is defintiely useful.
    R

  4. AHMED MOKHTAR Avatar
    AHMED MOKHTAR

    Hello Special thanks to your effort
    i am done with this project and successfully add the project to visual studio and add reference to it.
    but please describe how to build this project into exe and how to reuse

  5. Dear Ahmed,
    This creates a .NET assembly, i.e. a DLL, not an EXE.
    Cheers, Jeremy.

  6. AHMED MOKHTAR Avatar
    AHMED MOKHTAR

    thanks for your replay but Can you describe how to use this DLL in anther Computer i am successfully add tab to revit by running the revit in visual studio as External program but if i need anther person to add this tab in his application how can i do this ?

  7. Dear Ahmed,
    I suggest you read the getting started material and answer the question for yourself:
    http://thebuildingcoder.typepad.com/blog/getting_started
    Good luck and have fun!
    Cheers, Jeremy.

  8. AHMED MOKHTAR Avatar
    AHMED MOKHTAR

    Thank you Jeremy Tammik
    but i am anew in this type of programming i know a C# code but not a professional Dot Net Developer i used class you created before to add the Tab and button into revit and it is works good in revit that installed in my PC what i need to know how to deploy this code and use this DLL on anther computer what is the easiest way for do this

  9. AHMED MOKHTAR Avatar
    AHMED MOKHTAR

    I am sorry Jeremy Tammik for discomfort …
    Can you help me in this ? and if possible to speak together on skype or facebook ?

  10. Dear Ahmed,
    If you need that kind of support, I suggest you contact a paid consultant.
    Cheers, Jeremy.

  11. You are a nice guy jeremy for taking a time to politely answer to this type of helplessness

  12. hi,,
    thanks for your very valued issues
    its very useful
    but how can we put any of these objects in the Options Bar like wall command for example…
    thanks for help…
    hazem_el_amir@hotmail.com

  13. hi Mr Ahmed
    i’m also interested in revit api and I want to know u to help each other…
    hazem_el_amir@hotmail.com

  14. Hello,
    Thank’s a lot to Victor for this.
    The pros for this :
    – own tab in Revit, and not in add-ins
    – easy to use
    Just one thing miss :
    – combobox
    Starting C# for Revit, I’m not competent enough to add this in he code.

Leave a Reply to RahulCancel reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading