getting thamnail Inventor giving error

getting thamnail Inventor giving error

shubham_joshiMKWPQ
Observer Observer
219 Views
3 Replies
Message 1 of 4

getting thamnail Inventor giving error

shubham_joshiMKWPQ
Observer
Observer

partDoc = (PartDocument)_inventorApp.ActiveDocument;

if (partDoc.Thumbnail == null)
{
Console.WriteLine("Done");
}

shubham_joshiMKWPQ_0-1698761674105.png

 

0 Likes
220 Views
3 Replies
Replies (3)
Message 2 of 4

WCrihfield
Mentor
Mentor

Hi @shubham_joshiMKWPQ.  I am not fluent in C#, but I can tell that you were attempting to access the Document.Thumbnail property in the code you posted above.  I am not sure if you were aware of this or not, but the object you get as the value of that property is an IPictureDisp type object.  That is not a normal, or native image file type.  It is defined within the "stdole" Namespace, which must add a reference to in our code, to be able to access its methods & properties.  I am not sure why it would cause a catastrophic failure by simply attempting to check if that property was Null (or Nothing) though.  Take a look at the following iLogic rule (uses vb.net, instead of C#.net), which attempts to save the documents thumbnail out as a bmp type image file.  It utilizes a 'Do...Loop' while setting the value of the oThumb variable, checking its 'Handle' each time until it is a positive value.  I did not originally author that code, so I do not fully understand the need for that loop, but I'm showing it here just in case it might be an important detail, for some reason.

AddReference "System.Drawing"
AddReference "stdole"
AddReference "System.Windows.Forms"
Class ThisRule
	Sub Main
		Break
		Dim oDoc As Document = ThisDoc.Document
		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("C:\Temp\Thumbnail To BMP test.bmp", 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

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 4

shubham_joshiMKWPQ
Observer
Observer

Can any one have solution in C#/Python

 

0 Likes
Message 4 of 4

JelteDeJong
Mentor
Mentor

You might want to have a look at this post. People seem to get good results. (I could not test it.)

https://forums.autodesk.com/t5/inventor-programming-ilogic/get-the-thumbnail-from-active-inventor-do...

 

In the same thread, another important issue is mentioned. And I have seen the issue before. You can access this property only from code running in process, like in an add-in and not from a standalone application.

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com