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)
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.
Public Sub Activate( _
ByVal addInSiteObject As Inventor.ApplicationAddInSite, _
ByVal firstTime As Boolean) _
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 As String = "ID_COMAND_1"
Try
' try get the existing command definition
_defComando1 = conDefs.Item(idCommand1)
Catch ex As Exception
' 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"))
End Try
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)
End If
End If
' register the method that will be executed
AddHandler _defComando1.OnExecute, AddressOf Command1Method
End Sub
' must be declared at the class level
Private _defComando1 As Inventor.ButtonDefinition
Private Sub Command1Method()
' ToDo: do your code here
End Sub
Private Function GetICOResource( _
ByVal icoResourceName As String) As Object
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)
Return PictureDispConverter.ToIPictureDisp(ico)
End Function
The PictureDispConverter converter is based on this AU class by Philippe Leefsma. Below is the piece required here converted to VB.NET
Public NotInheritable Class PictureDispConverter
<DllImport("OleAut32.dll",
EntryPoint:="OleCreatePictureIndirect",
ExactSpelling:=True, PreserveSig:=False)> _
Private Shared Function OleCreatePictureIndirect( _
<MarshalAs(UnmanagedType.AsAny)> picdesc As Object, _
ByRef iid As Guid, _
<MarshalAs(UnmanagedType.Bool)> fOwn As Boolean _
) As IPictureDisp
End Function
Shared iPictureDispGuid As Guid = GetType( _
IPictureDisp).GUID
Private NotInheritable Class PICTDESC
Private Sub New()
End Sub
'Picture Types
Public Const PICTYPE_UNINITIALIZED As Short = -1
Public Const PICTYPE_NONE As Short = 0
Public Const PICTYPE_BITMAP As Short = 1
Public Const PICTYPE_METAFILE As Short = 2
Public Const PICTYPE_ICON As Short = 3
Public Const PICTYPE_ENHMETAFILE As Short = 4
<StructLayout(LayoutKind.Sequential)> _
Public Class Icon
Friend cbSizeOfStruct As Integer = Marshal.SizeOf( _
GetType(PICTDESC.Icon))
Friend picType As Integer = PICTDESC.PICTYPE_ICON
Friend hicon As IntPtr = IntPtr.Zero
Friend unused1 As Integer
Friend unused2 As Integer
Friend Sub New(icon__1 As System.Drawing.Icon)
Me.hicon = icon__1.ToBitmap().GetHicon()
End Sub
End Class
<StructLayout(LayoutKind.Sequential)> _
Public Class Bitmap
Friend cbSizeOfStruct As Integer = Marshal.SizeOf( _
GetType(PICTDESC.Bitmap))
Friend picType As Integer = PICTDESC.PICTYPE_BITMAP
Friend hbitmap As IntPtr = IntPtr.Zero
Friend hpal As IntPtr = IntPtr.Zero
Friend unused As Integer
Friend Sub New(bitmap__1 As System.Drawing.Bitmap)
Me.hbitmap = bitmap__1.GetHbitmap()
End Sub
End Class
End Class
Public Shared Function ToIPictureDisp( _
icon As System.Drawing.Icon _
) As IPictureDisp
Dim pictIcon As New PICTDESC.Icon(icon)
Return OleCreatePictureIndirect(pictIcon, _
iPictureDispGuid, True)
End Function
Public Shared Function ToIPictureDisp( _
bmp As System.Drawing.Bitmap _
) As IPictureDisp
Dim pictBmp As New PICTDESC.Bitmap(bmp)
Return OleCreatePictureIndirect(pictBmp, _
iPictureDispGuid, True)
End Function
End Class