Get Ribbon images

<?xml encoding=”UTF-8″>By Adam Nagy

As shown in a previous post, the Ribbon API provided in Autodesk.Windows namespace by AdWindows.dll is not product specific. It provides an additional way of accessing things from the Ribbon that the product API might not have exposed.

In the Inventor API you can get back the images from the ControlDefinition objects, however, some of the Ribbon controls are not based on a ContolDefinition, or even if they are, they might not provide a valid icon. Here is some VBA code for testing:

Sub RibbonControls()
Dim r As Ribbon
Set r = ThisApplication.UserInterfaceManager.Ribbons("Part")
Dim rt As RibbonTab
For Each rt In r.RibbonTabs
Dim rp As RibbonPanel
For Each rp In rt.RibbonPanels
Dim cc As CommandControl
For Each cc In rp.CommandControls
If cc.ControlDefinition Is Nothing Then
Debug.Print "No ControlDefinition for " + _
cc.InternalName + ", ControlType = " + _
str(cc.ControlType)
Else
' The icon properties might throw an error
On Error Resume Next
Dim icon As IPictureDisp
Set icon = cc.ControlDefinition.StandardIcon
If Err <> 0 Then
Debug.Print "No StandardIcon for " + _
cc.InternalName + ", ControlType = " + _
str(cc.ControlType)
End If
On Error GoTo 0
End If
Next
Next
Next
End Sub

It seems using the Ribbon API you can get back all the images. If you have a C# Inventor AddIn then you can add a Form to it (id: MyForm) and a ListView (id: ltvImages) on top of it along with two buttons with id btnSmall and btnLarge, and provide the following code for the Form:

using System;

using System.Drawing;

using System.Windows.Forms;

using System.Windows.Media.Imaging;

using System.Windows.Interop;

using System.Windows;

using System.IO;

 

namespace SimpleAddIn

{

  public partial class MyForm : Form

  {

    public MyForm()

    {

      InitializeComponent();

    }

 

    private Bitmap BitmapImage2Bitmap(BitmapSource bitmapImage)

    {

      using (MemoryStream outStream = new MemoryStream())

      {

        BitmapEncoder enc = new BmpBitmapEncoder();

        enc.Frames.Add(BitmapFrame.Create(bitmapImage));

        enc.Save(outStream);

        System.Drawing.Bitmap bitmap =
          new System.Drawing.Bitmap(outStream);

 

        return new Bitmap(bitmap);

      }

    }

 

    public void AddItem(Autodesk.Windows.RibbonItem i)

    {

      Bitmap bmp;

      if (i.Image != null)

      {

        bmp = BitmapImage2Bitmap((BitmapSource)i.Image);

        ltvImages.SmallImageList.Images.Add(bmp);

        bmp = BitmapImage2Bitmap((BitmapSource)i.LargeImage);

        ltvImages.LargeImageList.Images.Add(bmp);

        ltvImages.Items.Add(i.Id, ltvImages.Items.Count);

      }

 

      if (i is Autodesk.Windows.RibbonListButton)

      {

        Autodesk.Windows.RibbonListButton li =

          (Autodesk.Windows.RibbonListButton)i;

        foreach (Autodesk.Windows.RibbonItem si in li.Items)

          AddItem(si);

      }

    }

 

    public void ShowIcons(Boolean small)

    {

      if (ltvImages.SmallImageList == null)

      {

        ltvImages.Items.Clear();

 

        ltvImages.SmallImageList = new ImageList();

        ltvImages.SmallImageList.ImageSize =
          new System.Drawing.Size(16, 16);

        ltvImages.SmallImageList.TransparentColor = Color.Black;

        ltvImages.LargeImageList = new ImageList();

        ltvImages.LargeImageList.ImageSize =
          new System.Drawing.Size(32, 32);

        ltvImages.LargeImageList.TransparentColor = Color.Black;

 

        Autodesk.Windows.RibbonControl r =
          Autodesk.Windows.ComponentManager.Ribbon;

 

        foreach (Autodesk.Windows.RibbonTab t in r.Tabs)

        {

          foreach (Autodesk.Windows.RibbonPanel p in t.Panels)

          {

            foreach (Autodesk.Windows.RibbonItem i in p.Source.Items)

            {

              AddItem(i);

            }

          }

        }

      }

 

      ltvImages.View = small ? View.SmallIcon : View.LargeIcon;

    }

 

    private void btnSmall_Click(object sender, EventArgs e)

    {

      ShowIcons(true);

    }

 

    private void btnLarge_Click(object sender, EventArgs e)

    {

      ShowIcons(false);

    }

  }

Now you can show this form from the event handler of any of the commands you registered inside Inventor, e.g.:

MyForm myForm;
override protected void ButtonDefinition_OnExecute(
  NameValueMap context)

{

  myForm = new MyForm();

  myForm.Show();

}

When the command is run and the “Small Images” button is clicked we’ll get this:

  Ribbonimages

 


Comments

Leave a Reply

Discover more from Autodesk Developer Blog

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

Continue reading