Create Ribbon Controls
next previous home

In this lab, we shall write code that adds user controls to the Ribbon panel, through which your External Commands can be called. As an example, we will be using the external commands created in Lab 1, and hook them up to our Ribbon controls.

First of all, we need to determine the path of our add-in DLL, so we can provide the absolute path to the images that we will be using.

   
void CreateRibbonItems( ControlledApplication a )
{
  string addInPath = typeof(Lab6_2_Ribbon).Assembly.Location;
  string imgDir = Path.Combine(Path.GetDirectoryName(addInPath), "img");
  
  
}    
  
Public Sub CreateRibbonItems(ByVal a As ControlledApplication)
    Dim addInPath As String = GetType(Lab6_2_Ribbon).Assembly.Location
    Dim imgDir As String = Path.Combine(Path.GetDirectoryName(addInPath), "img")
    
End Sub

Then we specify all the button names and image paths that we need.

  const string panelName = "Lab 6 Panel";

  const string cmd1 = "Labs.Lab1_1_HelloWorld";
  const string name1 = "HelloWorld";
  const string text1 = "Hello World";
  const string tooltip1 = "Run Lab1_1_HelloWorld command";
  const string img1 = "ImgHelloWorld.png";
  const string img31 = "ImgHelloWorldSmall.png";

  const string cmd2 = "Labs.Lab1_2_CommandArguments";
  const string name2 = "CommandArguments";
  const string text2 = "Command Arguments";
  const string tooltip2 = "Run Lab1_2_CommandArguments command";
  const string img2 = "ImgCommandArguments.png";
  const string img32 = "ImgCommandArgumentsSmall.png";
  
  const string name3 = "Lab1Commands";
  const string text3 = "Lab 1 Commands";
  const string tooltip3 = "Run a Lab 1 command";
  const string img33 = "ImgCommandSmall.png";
  Dim panelName As String = "Lab 6 Panel"

  Dim cmd1 As String = "Labs.Lab1_1_HelloWorld"
  Dim name1 As String = "HelloWorld"
  Dim text1 As String = "Hello World"
  Dim tooltip1 As String = "Run Lab1_1_HelloWorld command"
  Dim img1 As String = "ImgHelloWorld.png"
  Dim img31 As String = "ImgHelloWorldSmall.png"
  
  Dim cmd2 As String = "Labs.Lab1_2_CommandArguments"
  Dim name2 As String = "CommandArguments"
  Dim text2 As String = "Command Arguments"
  Dim tooltip2 As String = "Run Lab1_2_CommandArguments command"
  Dim img2 As String = "ImgCommandArguments.png"
  Dim img32 As String = "ImgCommandArgumentsSmall.png"

  Dim name3 As String = "Lab1Commands"
  Dim text3 As String = "Lab 1 Commands"
  Dim tooltip3 As String = "Run a Lab 1 command"
  Dim img33 As String = "ImgCommandSmall.png"

Before we can add our controls, we need to create a Ribbon Panel for them using CreateRibbonPanel. At the moment we can only add Panels to the AddOns Tab of the Ribbon.

  RibbonPanel panel = a.CreateRibbonPanel(panelName);
  Dim panel As RibbonPanel = a.CreateRibbonPanel(panelName)

Now we can start adding our controls.

At the moment, you can only create a push button or a pulldown button. Their size depends on whether they were added using the AddPushButton/AddPulldownButton or AddStackedButtons function. Let's add a simple large button to the panel to invoke the Lab 1 Hello World command.

  PushButton pb1 = panel.AddPushButton(name1, text1, addInPath, cmd1);
  pb1.ToolTip = tooltip1;
  pb1.LargeImage = new BitmapImage(new Uri(Path.Combine(imgDir, img1)));
  Dim pb1 As PushButton = panel.AddPushButton(name1, text1, addInPath, cmd1)
  pb1.ToolTip = tooltip1
  pb1.LargeImage = New BitmapImage(New Uri(Path.Combine(imgDir, img1)))

We separate the large button from the following small ones.

  panel.AddSeparator();
  panel.AddSeparator()

When adding small, stacked buttons, we need to use PushButtonData/PulldownButtondata to tell AddStackedButtons what to create, and then the function will return the list of created items, containing PushButtons and PulldownButtons. The first button will refer to the Hello World command, the second one to the Command Arguments one, and the third one will provide a pulldown menu containing both the Lab 1 commands.

  // prepare data for creating stackable buttons
  PushButtonData pbd1 = new PushButtonData(name1, text1, addInPath, cmd1);
  pbd1.ToolTip = tooltip1;
  pbd1.Image = new BitmapImage(new Uri(Path.Combine(imgDir, img31)));

  PushButtonData pbd2 = new PushButtonData(name2, text2, addInPath, cmd2);
  pbd2.ToolTip = tooltip2;
  pbd2.Image = new BitmapImage(new Uri(Path.Combine(imgDir, img32)));

  PulldownButtonData pbd3 = new PulldownButtonData(name3, text3);
  pbd3.ToolTip = tooltip3;
  pbd3.Image = new BitmapImage(new Uri(Path.Combine(imgDir, img33)));

  // add stackable buttons
  List<RibbonItem> ribbonItems = panel.AddStackedButtons(pbd1, pbd2, pbd3);
  ' prepare data for creating stackable buttons
  Dim pbd1 As New PushButtonData(name1, text1, addInPath, cmd1)
  pbd1.ToolTip = tooltip1
  pbd1.Image = New BitmapImage(New Uri(Path.Combine(imgDir, img31)))

  Dim pbd2 As New PushButtonData(name2, text2, addInPath, cmd2)
  pbd2.ToolTip = tooltip2
  pbd2.Image = New BitmapImage(New Uri(Path.Combine(imgDir, img32)))

  Dim pbd3 As New PulldownButtonData(name3, text3)
  pbd3.ToolTip = tooltip3
  pbd3.Image = New BitmapImage(New Uri(Path.Combine(imgDir, img33)))

  ' add stackable buttons.
  Dim ribbonItems As List(Of RibbonItem) = panel.AddStackedButtons(pbd1, pbd2, pbd3)

The last item provided to AddStackedButtons is a PulldownButtonData, so the last item added is a PulldownButton. We retrieve that button and fill it up with additional Push Buttons that will pop up when the Pulldown Button is clicked:

  PulldownButton pb3 = ribbonItems[2] as PulldownButton;

  PushButton pb3_1 = pb3.AddItem(text1, addInPath, cmd1);
  pb3_1.ToolTip = tooltip1;
  pb3_1.LargeImage = new BitmapImage(new Uri(Path.Combine(imgDir, img1)));

  PushButton pb3_2 = pb3.AddItem(text2, addInPath, cmd2);
  pb3_2.ToolTip = tooltip2;
  pb3_2.LargeImage = new BitmapImage(new Uri(Path.Combine(imgDir, img2)));
  Dim pb3 As PulldownButton = ribbonItems(2)

  Dim pb3_1 As PushButton = pb3.AddItem(text1, addInPath, cmd1)
  pb3_1.ToolTip = tooltip1
  pb3_1.LargeImage = New BitmapImage(New Uri(Path.Combine(imgDir, img1)))

  Dim pb3_2 As PushButton = pb3.AddItem(text2, addInPath, cmd2)
  pb3_2.ToolTip = tooltip2
  pb3_2.LargeImage = New BitmapImage(New Uri(Path.Combine(imgDir, img2)))

Before building the project, we need to add references to the PresentationCore and WindowsBase .NET assemblies and use the System.Windows.Media.Imaging namespace.

Now, build the project, adjust the ini file and switch to the Add-Ins tab on the ribbon bar to see the result.

next previous home copyright © 2007-2009 jeremy tammik, autodesk inc. all rights reserved.