I’ve had a few recent posts that have talked briefly about using thumbnail images (Document Thumbnails and Button Icons, Parts List with Thumbnail Image, and Accessing iProperties). If you need to get a thumbnail image from an Inventor document there are several possibilities. I want to briefly cover those here.
Thumbnail as an iProperty
The thumbnail is stored as an iProperty within an Inventor document. One way to access the thumbnail is as an iProperty. The following VBA code accesses the thumbnail as an iProperty and writes it as a bmp file to disk. (This code won’t work on a 64-bit Inventor because of the issues discussed in this post.)
Public Sub WriteThumbnail()
' Get the currently active document.
Dim doc As Document
Set doc = ThisApplication.ActiveDocument
' Get the Summary Information property set.
Dim summaryInfo As PropertySet
Set summaryInfo = doc.PropertySets.Item( _
"Inventor Summary Information")
' Get the thumbnail property. When using VBA, this will
' only work with 32-bit Inventor.
Dim thumbProp As Property
Set thumbProp = summaryInfo.Item("Thumbnail")
' Get the thumbnail image.
Dim thumbnail As IPictureDisp
Set thumbnail = thumbProp.Value
' Check that an image was returned. It's possible for a
' file to not have a thumbnail.
If Not thumbnail Is Nothing Then
' Write the thumbnail to disk.
Call SavePicture(thumbnail, "C:\Temp\Thumb.bmp")
Else
MsgBox "The active document doesn't have a thumbnail."
End If
End Sub
Because Apprentice provides access to iProperties, you can use it instead of Inventor to access the thumbnail. Below is some VB.Net code that uses Apprentice to read a thumbnail from an Inventor document and display it within a picture box.
To use the code below, create a new “Windows Form Application”. On the form add a button (Button1) and a picture box (PictureBox1). Set the SizeMode property of the picture box to StretchImage. My dialog is shown below.
You’ll also need to add references to the following components using the .Net tab of the Add Reference dialog:
Autodesk.Inventor.Interop
Microsoft.VisualBasic.Compatibility
stdole
Here’s my implementation for the form and the button click sub.
Imports Inventor
Imports Microsoft.VisualBasic
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
' Create an instance of Apprentice.
Dim apprentice As New ApprenticeServerComponent
' Open a document.
Dim doc As ApprenticeServerDocument
doc = apprentice.Open("C:\Temp\Part1.ipt")
' Get the Summary Information property set.
Dim summaryInfo As PropertySet
summaryInfo = doc.PropertySets.Item( _
"Inventor Summary Information")
' Get the thumbnail property.
Dim thumbProp As Inventor.Property
thumbProp = summaryInfo.Item("Thumbnail")
' Get the thumbnail image.
Dim thumbnail As stdole.IPictureDisp
thumbnail = thumbProp.Value
' Convert the IPictureDisp object to an Image.
Dim img As Image = _
Compatibility.VB6.IPictureDispToImage(thumbnail)
' Display the image in the picture box.
PictureBox1.Image = img
End Sub
End Class
Document.Thumbnail Property
There’s another way you can get the thumbnail that’s simpler than going through the iProperty objects. Both the Document and ApprenticeServerDocument objects support the Thumbnail property. Here’s a modified version of the program above that uses the Thumbnail property.
Imports Inventor
Imports Microsoft.VisualBasic
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
' Create an instance of Apprentice.
Dim apprentice As New ApprenticeServerComponent
' Open a document.
Dim doc As ApprenticeServerDocument
doc = apprentice.Open("C:\Temp\Part1.ipt")
' Get the thumbnail image.
Dim thumbnail As stdole.IPictureDisp
thumbnail = doc.Thumbnail
' Convert the IPictureDisp object to an Image.
Dim img As Image = _
Compatibility.VB6.IPictureDispToImage(thumbnail)
' Display the image in the picture box.
PictureBox1.Image = img
End Sub
End Class
A big difference between using the Document.Thumbnail property and accessing the thumbnail as an iProperty is that the Document.Thumbnail property is read-only while the iProperty is writable. This means you can push any image you would like into the iProperty to be used as the thumbnail for that document. However, this is only supported in Inventor and setting the value of the thumbnail iProperty will fail when using Apprentice.
ThumbnailView Component
There’s one other way of accessing a thumbnail. Because iProperties are stored using some Microsoft standards, it’s possible to use Windows functionality to directly access the thumbnail without using Inventor or Apprentice. It requires some more advanced programming to do it this way, so to make it easier there’s a component delivered as part of the Inventor SDK that you can use rather than write the code yourself. The source code is also provided to serve as a sample for those that want to include this functionality within their applications.
To use this component you need to first install the DeveloperTools.msi package. This is in the Inventor SDK directory (C:\Program Files\Autodesk\Inventor 2011\SDK on WinNT and C:\Users\Public\Documents\Autodesk\Inventor 2011\SDK on Vista or Win7). Installing this will create an SDK\DeveloperTools\Tools\ThumbnailView directory that contains the component, its source code, and some samples demonstrating its use.
To use the component, go into the Bin directory and run the Register.bat file to register the component. In your project where you’ll be using the component you’ll need to add a reference to it. I typically use the Browse option and browse to the InventorThumbnailView.dll file. Here’s a version of the previous program that uses this component to get the thumbnail.
Imports Microsoft.VisualBasic
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
' Create an instance of the ThumbnailProvider component.
Dim thumbView As New InventorThumbnailViewLib.ThumbnailProvider
' Get the thumbnail image.
Dim thumbnail As stdole.IPictureDisp
thumbnail = thumbView.GetThumbnail("C:\Temp\Part1.ipt")
' Convert the IPictureDisp object to an Image.
Dim img As Image = _
Compatibility.VB6.IPictureDispToImage(thumbnail)
' Display the image in the picture box.
PictureBox1.Image = img
End Sub
End Class
One thing important to notice is that there’s no longer a reference to the Inventor component required since this isn’t using the Inventor API.
Inventor 2011 and ThumbnailView
The ThumbnailView component has been provided as a sample for several releases of Inventor. However, an issue was discovered soon after Inventor 2011 was shipped where it was found the component doesn’t work with Inventor 2011 files. In Inventor 2011 a change was made to how the thumbnail image is stored within the iProperty value. Previously to Inventor 2011 it was stored as a bmp image. In Inventor 2011 it’s now saved as a png image. All of the other functionality that deals with the thumbnail, iProperties and the Document.Thumbnail property, were updated to account for this change. However, the ThumbnailView utility slipped through the cracks and was not updated. You can access an updated version of the component here that works with Inventor 2011 and previous files.