I copied and pasted the code sample from the help file in Inventor 2012. I tried to run the code after creating either a new part document or new assembly document, either way, I get the same error on this line:
Call oImageSet.Add(1, oImage)
The error is attached in a screenshot (Method 'Add' of object 'GraphicsImageSet' failed)
I made sure to have a bitmap at the location shown in the help file so that I didn't have to change a single line of code. I tried several different size bitmaps and none worked. Also I checked the watch window to make sure the bitmap had a value, it appeared to be loaded correctly, just wouldn't add to imageSet. Can anyone else try the code and see if it works for them?
Public Sub ImageInPointClientGraphics() Dim oDoc As Document Set oDoc = ThisApplication.ActiveDocument ' Set a reference to component definition of the active document. ' This assumes that a part or assembly document is active. Dim oCompDef As ComponentDefinition Set oCompDef = ThisApplication.ActiveDocument.ComponentDefinition ' Check to see if the test graphics data object already exists. ' If it does clean up by removing all associated of the client graphics ' from the document. If it doesn't create it. On Error Resume Next Dim oGraphicsData As GraphicsDataSets Set oGraphicsData = oDoc.GraphicsDataSetsCollection.Item("SampleGraphicsID") If Err.Number = 0 Then On Error GoTo 0 ' An existing client graphics object was successfully obtained so clean up. oGraphicsData.Delete oCompDef.ClientGraphicsCollection.Item("SampleGraphicsID").Delete ' update the display to see the results. ThisApplication.ActiveView.Update Else Err.Clear On Error GoTo 0 ' Set a reference to the transient geometry object for user later. Dim oTransGeom As TransientGeometry Set oTransGeom = ThisApplication.TransientGeometry ' Create a graphics data set object. This object contains all of the ' information used to define the graphics. Dim oDataSets As GraphicsDataSets Set oDataSets = oDoc.GraphicsDataSetsCollection.Add("SampleGraphicsID") ' Create a coordinate set. Dim oCoordSet As GraphicsCoordinateSet Set oCoordSet = oDataSets.CreateCoordinateSet(1) ' Create an array that contains coordinates that define a set ' of outwardly spiraling points. Dim oPointCoords(2) As Double oPointCoords(0) = 1 oPointCoords(1) = 1 oPointCoords(2) = 0 ' Assign the points into the coordinate set. Call oCoordSet.PutCoordinates(oPointCoords) ' Create an image set Dim oImageSet As GraphicsImageSet Set oImageSet = oDataSets.CreateImageSet(2) Dim oImage As IPictureDisp Set oImage = LoadPicture("C:\Temp\MyImage.bmp") Call oImageSet.Add(1, oImage) ' Create the ClientGraphics object. Dim oClientGraphics As ClientGraphics Set oClientGraphics = oCompDef.ClientGraphicsCollection.Add("SampleGraphicsID") ' Create a new graphics node within the client graphics objects. Dim oPointNode As GraphicsNode Set oPointNode = oClientGraphics.AddNode(1) ' Create a PointGraphics object within the node. Dim oPointGraphics As PointGraphics Set oPointGraphics = oPointNode.AddPointGraphics ' Assign the coordinate set to the point graphics. oPointGraphics.CoordinateSet = oCoordSet ' Set a custom image Call oPointGraphics.SetCustomImage(oImageSet, 1) ' Update the view. ThisApplication.ActiveView.Update End If End Sub
Thanks,
I was wondering if someone from Autodesk could try this. Feel free to copy/paste the code directly from the programming help just as I did.
I confirmed this problem with Win7 64bit, and am checking with our engineering team.
I will inform you as soon as I get any feedback.
Here is the answer that I received:
"It’s a known limitation on i64, no such problem when you run VBA on 32bit...
This is a generic Microsoft issue. IPicturedisp is an implementation on process local gdi resource handles (HBITMAP, HICON, etc…) and therefore cannot be marshaled out of process (where these handles are meaningless). IPictureDisp never marshals. When you run VBA in Inventor 64, VBA runs out of process in a 32 bit host process, and therefore all references need to marshal out of the Inventor process to the VBA host process. You’ll notice that every API property that returns an IPictureDisp will fail in this context."
So...on 64bit system, the resolution is to run those codes from an in-process customized application, e.g. AddIn dll.