Quantcast
Channel: Manufacturing DevBlog
Viewing all articles
Browse latest Browse all 518

Get Ribbon images

$
0
0

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

{

  publicpartialclassMyForm : Form

  {

    public MyForm()

    {

      InitializeComponent();

    }

 

    privateBitmap BitmapImage2Bitmap(BitmapSource bitmapImage)

    {

      using (MemoryStream outStream = newMemoryStream())

      {

        BitmapEncoder enc = newBmpBitmapEncoder();

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

        enc.Save(outStream);

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

 

        returnnewBitmap(bitmap);

      }

    }

 

    publicvoid 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);

      }

    }

 

    publicvoid ShowIcons(Boolean small)

    {

      if (ltvImages.SmallImageList == null)

      {

        ltvImages.Items.Clear();

 

        ltvImages.SmallImageList = newImageList();

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

        ltvImages.SmallImageList.TransparentColor = Color.Black;

        ltvImages.LargeImageList = newImageList();

        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;

    }

 

    privatevoid btnSmall_Click(object sender, EventArgs e)

    {

      ShowIcons(true);

    }

 

    privatevoid 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;

overrideprotectedvoid 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

 


Viewing all articles
Browse latest Browse all 518

Trending Articles