copy thumbnails from files in directory to jpg

copy thumbnails from files in directory to jpg

paul.cuppens
Explorer Explorer
2,502 Views
13 Replies
Message 1 of 14

copy thumbnails from files in directory to jpg

paul.cuppens
Explorer
Explorer

Hello.

 

Is there a possibility to create some images from all thumbnails in a directory?

We want the thumbnails for a reference picture in our ERP system.

 

Any Suggestions?

 

0 Likes
2,503 Views
13 Replies
Replies (13)
Message 2 of 14

yuzeaa
Advocate
Advocate
Accepted solution

Here's an example for you.

AddReference "Microsoft.VisualBasic.Compatibility"
AddReference "system.drawing"
Dim doc As AssemblyDocument = ThisApplication.ActiveDocument
folderPath = "C:\temp"
If Not IO.Directory.Exists(odir) Then IO.Directory.CreateDirectory(folderPath)
For Each refdoc In doc.AllReferencedDocuments
	Dim thumb As Inventor.IPictureDisp
    thumb = refdoc.Thumbnail
	Dim img As Drawing.Image
    img = Compatibility.VB6.IPictureDispToImage(thumb)
    img.Save(folderPath & "\" & IO.Path.GetFileNameWithoutExtension(refdoc.Fulldocumentname) & ".bmp")
Next
Shell("explorer.exe """ & folderPath & """", 1)
Message 3 of 14

WCrihfield
Mentor
Mentor
Accepted solution

Hi guys.  I am going to post this one here too, because the code process you posted often creates a 'squished' looking image for me.  Code like what I am posting below has been posted here on the forums before, and did not entirely originate from me.  I just customized a few things.  This process seems to create a much better looking BMP file, that looks more closely like the preview image of the document that I see within the Windows file explorer screen when set to 'Extra Large Icons' view setting.  And as you can see within the code, you can change the size of the output image, if needed, but I primarily just needed small images, similar to what we see in the file system previews.

AddReference "System.Drawing"
AddReference "stdole"
AddReference "System.Windows.Forms"
Class ThisRule
	Sub Main
		Dim oDoc As Inventor.Document = ThisDoc.Document
		ConvertDocThumbnailToBMP(oDoc)
	End Sub
	
	Sub ConvertDocThumbnailToBMP(oDoc As Inventor.Document, Optional sBMPfile As String = vbNullString)
		If oDoc Is Nothing Then Return
		If String.IsNullOrEmpty(sBMPfile) Then
			'same path and file name, but with .bmp file extension
			sBMPfile = System.IO.Path.ChangeExtension(oDoc.FullFileName, ".bmp")
		End If
		Break
		Dim oThumb As stdole.IPictureDisp
		Do
			oThumb = oDoc.Thumbnail
		Loop While oThumb.Handle < 0
		Dim oAHConverter As New AxHostConverter
		Dim oImage As System.Drawing.Image = oAHConverter.GetImageFromIPictureDisp(oThumb)
		'this call must be created, but is never used
		Dim myCallback As New System.Drawing.Image.GetThumbnailImageAbort(AddressOf ThumbnailCallback)
		'create a thumbnail before save - seems to generate a better quality
		oImage.GetThumbnailImage(180, 180, myCallback, IntPtr.Zero).Save(sBMPfile, System.Drawing.Imaging.ImageFormat.Bmp)
	End Sub
	
	Public Function ThumbnailCallback() As Boolean
		Return False
	End Function
End Class

Class AxHostConverter
	Inherits System.Windows.Forms.AxHost
	Public Sub New()
		'MyBase.New("{63109182-966B-4e3c-A8B2-8BC4A88D221C}")
		MyBase.New((New Guid).ToString)
	End Sub
	Public Function GetImageFromIPictureDisp(ByVal IPDisp As stdole.IPictureDisp) As System.Drawing.Image
		Return MyBase.GetPictureFromIPicture(IPDisp)
	End Function
End Class

That special 'Class' could be saved out into its own external iLogic rule, then turn its 'Straight VB Code' option on, then you could use it by referencing it in the rule using the AddVBFile line within the 'Header' of the rule, instead of directly including the whole Class block of code within the rule.

And of course, there are also several built-in Inventor API methods for saving images of the active view out to the file system with various settings, but that may require visibly opening each document, and possibly reorienting the view and zoom level (by code) before capturing the image, and that may take a longer time to process.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 4 of 14

yuzeaa
Advocate
Advocate
Accepted solution

@WCrihfield  I tested your code and the image size obtained is the same as this one.

AddReference "Microsoft.VisualBasic.Compatibility"
AddReference "system.drawing"
Sub main
Dim doc As AssemblyDocument = ThisApplication.ActiveDocument
folderPath = "C:\temp"
If Not IO.Directory.Exists(folderPath) Then IO.Directory.CreateDirectory(folderPath)
For Each refdoc In doc.AllReferencedDocuments
	Dim thumb As Inventor.IPictureDisp
	thumb = refdoc.Thumbnail
	Dim img As Drawing.Image
    img = Compatibility.VB6.IPictureDispToImage(thumb)
	img=ResizeImage(img,180,180)
    img.Save(folderPath & "\" & IO.Path.GetFileNameWithoutExtension(refdoc.Fulldocumentname) & ".bmp")
Next
Shell("explorer.exe """ & folderPath & """", 1)
End Sub


Function ResizeImage(InputImage As System.Drawing.Image,width As Double,height As Double) As System.Drawing.Image
        Return New System.Drawing.Bitmap(InputImage, New System.Drawing.Size(width, height))
End Function

 

Message 5 of 14

WCrihfield
Mentor
Mentor
Accepted solution

Thanks @yuzeaa.  The images that version of your original code produces are better than the first version, and are the same physical size as the ones produces by the code I posted.  However, some of the code resources it references/uses are considered to be obsolete, so I would not want to rely upon them for any future automation projects.  You can read a bit more about it here:

https://learn.microsoft.com/en-us/dotnet/api/microsoft.visualbasic.compatibility.vb6.support.ipictur... 

I believe that process was originally posted online (Link) back around 2010 for use in an Inventor VBA macro, but the VBA system itself has been on its way out for years now too (Link1, Link2), and is no longer being maintained by Microsoft, so I have not been using it for anything the past few years.  Here is a link to a follow-up post on the same online blog in 2012 which mentions the AxHostConverter method.  Just as reference materials for others who may find this stuff later.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 6 of 14

yuzeaa
Advocate
Advocate
Accepted solution

Thank you. You have always been very friendly in helping others, pointing out issues, and providing a lot of valuable reference materials for learning. I have learned a lot from the posts you have shared. I do acknowledge that it is obsolete, as you pointed out. I am aware that I have come across the method you mentioned before, but at that time, I did not have a need for it, so I did not look into it. Now, I realize that I should not post obsolete methods on the forum in furture.

0 Likes
Message 7 of 14

paul.cuppens
Explorer
Explorer

Hello, allready many thanks for the suggested code.

Next question.

I'm really not into coding and that kind of stuff. So can anyone help me with where i need to put the code and so...

 

0 Likes
Message 8 of 14

fichri17
Enthusiast
Enthusiast

Hi everybody,

I know this is solved but it is kind of what I was looking for.
However, the code provided only gives me thumbnails from the items included in the model explorer of an assembly, and I was actually looking for just the code to provide the image of the top Assembly....

I`m sure this is pretty straight forward to fix, but I just cant seem to get it right...

Any help available??

 

Br.
Frank

 

@yuzeaa 
@WCrihfield
@paul.cuppens 

0 Likes
Message 9 of 14

WCrihfield
Mentor
Mentor

Hi @fichri17.  If you are trying to use the code shown in Message 4, then you would have to:

  • delete Lines 7 & 14.
  • In Lines 9 & 13 replace the 'refdoc' variable with the 'doc' variable.

Or, you can use the code shown in Message 3, because that one just focusses on the document that was active when the rule starts, without attempting to go any deeper.  Plus, as mentioned in Message 5, the code posted in Message 3 is an updated routine, while the routine being used in Message 4 is outdated.  But, if it is not being relied upon for anything super important in the future, then it really does not matter which one you use, as long as it works OK for you.  I know the one I posted in Message 3 is longer and more confusing looking, which does kind of suck, but that does not really matter if it is hidden behind a button, and all you are doing is clicking a button, or running a rule.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 10 of 14

fichri17
Enthusiast
Enthusiast

Hi @WCrihfield 

Thanks for all the great help.
However, when I edited the code like you said, I get the following error -->

fichri17_0-1709530018945.png


Any ideas ?

0 Likes
Message 11 of 14

WCrihfield
Mentor
Mentor

@fichri17 

OK, I see it now.  Any time we want to interact with the IPictureDisp Type that Inventor uses for thumbnail images, we need to include a reference to that external resource, because within that external resource is where the iPictureDisp Type is defined.  If you look at my example code in Message 3, you will see the needed code in Line 2.

AddReference "stdole"

Those types of lines of code (starting with AddReference or Imports) will go into the 'Header' of the iLogic rule.  They help by adding recognition to those object types that are defined in external resources, and enable their related functionality.  The other reference line (in Line 3) of my code in Message 3 would be necessary if you were going to be using the newer AxHostConverter route for converting the image types, as that code is doing, because the AxHost object is defined under that external resource.  The System.Drawing external resource is where the Image object is defined, so adding a reference to that helps the code recognize that type of object and work with it.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 12 of 14

fichri17
Enthusiast
Enthusiast

Thank you @WCrihfield, now its working excellent...

Not to get greedy, but I was just wondering about another thing...
Can this rule be adapted to be run on the vault server for say to export thumbnails for all the assemblies (IAM) within a folder??

-Frank

0 Likes
Message 13 of 14

WCrihfield
Mentor
Mentor

Hi @fichri17.  I do not currently use Vault, and have not used it in the past either, so I am simply not familiar enough with it to be able to tell you how to do that with Vault Server involved.  However, I have done many other code routines that iterate though a whole folder full of files, and process specific types of Inventor files in that folder in specific ways.  But I suspect that those types of codes would not be of much use without there being some sort of Vault stuff involved within the code, because the files would most likely seem like ReadOnly without the Vault related code being included when it is being used.  Maybe someone else here on the forum would be able to help show you how to process a folder of files with Vault Server being involved.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 14 of 14

fichri17
Enthusiast
Enthusiast

Thanks for all the great help @WCrihfield 

I will keep this query open a little bit more and hope to get some response for the vault specific question....

-Frank 

0 Likes