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

Creating a Ribbon item for Inventor

$
0
0

By Augusto Goncalves

Here is a quick sample on how create a new Ribbon Tab, Panel and Button with Icon on Inventor. Although there are some samples on the blog, this just summarizes the whole process.

The Icon used on the Ribbon button can be placed inside the project, in this case is even better when it’s embedded on the project, that way your plugin just need to be delivered with the DLL file (no additional .ico files). To do so, put the .ico on the project folder, include on the project, then go to the file properties and select ‘Build Action’. The method GetICOResource receive the resource name, usually the ProjectName.FileName.ico (see sample call for this method)

embedded_resource_icon

Now it’s time to create the code. The following sample show it for a single button, just place it inside the Activate method for a quick test.

PublicSub Activate( _

      ByVal addInSiteObject As Inventor.ApplicationAddInSite, _

      ByVal firstTime AsBoolean) _

    Implements Inventor.ApplicationAddInServer.Activate

  ' Inventor application

  m_inventorApplication = addInSiteObject.Application

 

  ' get the command manager control definition

  Dim conDefs As Inventor.ControlDefinitions = _

      m_inventorApplication. _

      CommandManager.ControlDefinitions

 

  ' our custom command ID

  Dim idCommand1 AsString = "ID_COMAND_1"

 

  Try

    ' try get the existing command definition

    _defComando1 = conDefs.Item(idCommand1)

  Catch ex AsException

    ' or create it

    _defComando1 = conDefs.AddButtonDefinition( _

        "Command 1", idCommand1, _

        CommandTypesEnum.kEditMaskCmdType, _

        Guid.NewGuid().ToString(), _

        "Command 1 description", _

        "Command 1 Tooltip", _

        GetICOResource("ProjetName.StdIcon.ico"), _

        GetICOResource("ProjetName.Large.ico"))

  EndTry

 

  If (firstTime) Then

    If (m_inventorApplication.UserInterfaceManager.

        InterfaceStyle =

        InterfaceStyleEnum.kRibbonInterface) Then

 

      ' 1. access the Zero Doc ribbon

      Dim ribbonPart As Inventor.Ribbon =

        m_inventorApplication.

          UserInterfaceManager.

          Ribbons.Item("ZeroDoc")

 

      ' 2. create our custom tab

      Dim tabSampleBlog As Inventor.RibbonTab =

        ribbonPart.RibbonTabs.Add( _

          "Sample Blog", _

          "TAB_SAMPLE_BLOG", _

          Guid.NewGuid().ToString())

 

      ' 3. criar um painel

      Dim pnlMyCommands As Inventor.RibbonPanel =

        tabSampleBlog. _

        RibbonPanels.Add("My Command", _

                         "PNL_MY_COMMANDS", _

                         Guid.NewGuid().ToString())

 

      ' 4. colocar o botão no painel

      pnlMyCommands.CommandControls.AddButton(_defComando1, True)

    EndIf

  EndIf

 

  ' register the method that will be executed

  AddHandler _defComando1.OnExecute, AddressOf Command1Method

 

EndSub

' must be declared at the class level

Private _defComando1 As Inventor.ButtonDefinition

PrivateSub Command1Method()

  ' ToDo: do your code here

EndSub

 

PrivateFunction GetICOResource( _

                  ByVal icoResourceName AsString) AsObject

  Dim assemblyNet As System.Reflection.Assembly = _

    System.Reflection.Assembly.GetExecutingAssembly()

  Dim stream As System.IO.Stream = _

    assemblyNet.GetManifestResourceStream(icoResourceName)

  Dim ico As System.Drawing.Icon = _

    New System.Drawing.Icon(stream)

  ReturnPictureDispConverter.ToIPictureDisp(ico)

EndFunction

The PictureDispConverter converter is based on this AU class by Philippe Leefsma. Below is the piece required here converted to VB.NET

PublicNotInheritableClassPictureDispConverter

  <DllImport("OleAut32.dll",

    EntryPoint:="OleCreatePictureIndirect",

    ExactSpelling:=True, PreserveSig:=False)> _

  PrivateSharedFunction OleCreatePictureIndirect( _

    <MarshalAs(UnmanagedType.AsAny)> picdesc AsObject, _

    ByRef iid AsGuid, _

    <MarshalAs(UnmanagedType.Bool)> fOwn AsBoolean _

    ) AsIPictureDisp

  EndFunction

 

  Shared iPictureDispGuid AsGuid = GetType( _

    IPictureDisp).GUID

 

  PrivateNotInheritableClassPICTDESC

    PrivateSubNew()

    EndSub

    'Picture Types

    PublicConst PICTYPE_UNINITIALIZED AsShort = -1

    PublicConst PICTYPE_NONE AsShort = 0

    PublicConst PICTYPE_BITMAP AsShort = 1

    PublicConst PICTYPE_METAFILE AsShort = 2

    PublicConst PICTYPE_ICON AsShort = 3

    PublicConst PICTYPE_ENHMETAFILE AsShort = 4

 

    <StructLayout(LayoutKind.Sequential)> _

    PublicClassIcon

      Friend cbSizeOfStruct AsInteger = Marshal.SizeOf( _

        GetType(PICTDESC.Icon))

      Friend picType AsInteger = PICTDESC.PICTYPE_ICON

      Friend hicon AsIntPtr = IntPtr.Zero

      Friend unused1 AsInteger

      Friend unused2 AsInteger

 

      FriendSubNew(icon__1 As System.Drawing.Icon)

        Me.hicon = icon__1.ToBitmap().GetHicon()

      EndSub

    EndClass

 

    <StructLayout(LayoutKind.Sequential)> _

    PublicClassBitmap

      Friend cbSizeOfStruct AsInteger = Marshal.SizeOf( _

        GetType(PICTDESC.Bitmap))

      Friend picType AsInteger = PICTDESC.PICTYPE_BITMAP

      Friend hbitmap AsIntPtr = IntPtr.Zero

      Friend hpal AsIntPtr = IntPtr.Zero

      Friend unused AsInteger

 

      FriendSubNew(bitmap__1 As System.Drawing.Bitmap)

        Me.hbitmap = bitmap__1.GetHbitmap()

      EndSub

    EndClass

  EndClass

 

  PublicSharedFunction ToIPictureDisp( _

                    icon As System.Drawing.Icon _

                    ) AsIPictureDisp

    Dim pictIcon AsNewPICTDESC.Icon(icon)

    Return OleCreatePictureIndirect(pictIcon, _

                                    iPictureDispGuid, True)

  EndFunction

 

  PublicSharedFunction ToIPictureDisp( _

                    bmp As System.Drawing.Bitmap _

                    ) AsIPictureDisp

    Dim pictBmp AsNewPICTDESC.Bitmap(bmp)

    Return OleCreatePictureIndirect(pictBmp, _

                                    iPictureDispGuid, True)

  EndFunction

EndClass


Viewing all articles
Browse latest Browse all 531

Trending Articles