Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.

SketchImages method failed

J.Pelfrene
Enthusiast

SketchImages method failed

J.Pelfrene
Enthusiast
Enthusiast

I want to add some images into the drawing document, but seem to have an issue with accessing the DrawingSketch.SketchImages method.

 

Also code samples from the forum or documentation with this method throw the same error for me.

I am using Inventor 2021. Is this a known issue? And are there other ways to include an image at a specified location in the drawing document?

 

Sub InsertImage()

    Dim oDrawDoc As DrawingDocument
    Set oDrawDoc = ThisApplication.ActiveDocument
    
    Dim oSheet As Sheet
    Set oSheet = oDrawDoc.ActiveSheet
    
    Dim oDrawSketch As DrawingSketch
    Set oDrawSketch = oSheet.Sketches.Add()
    oDrawSketch.Edit
    
    Dim oImagePath As String
    oImagePath = "C:\Temp\image.png"
        
    Dim oPoint2d As Point2d
    Set oPoint2d = ThisApplication.TransientGeometry.CreatePoint2d(0, 0)
    
    Dim oSketchImage As SketchImage
    Set oSketchImage = oDrawSketch.SketchImages.Add(oImagePath, oPoint2d)

    oDrawSketch.ExitEdit

End Sub
0 Likes
Reply
560 Views
8 Replies
Replies (8)

WCrihfield
Mentor
Mentor

Hi @J.Pelfrene.  I am using Inventor 2022.3.2 and I just recreated the same exact issue, using your code and a random image of the same type that I saved to the same path and name.  Below are the screenshots of the error dialog, debug highlighted line, and a couple lines from the Locals window.  The code creates the new sketch in the drawing, but it appears to encounter some sort of error when it tries to access the SketchImages collection of the DrawingSketch object that it just created.  It is interesting that it calls the SketchImages a 'method', instead of a Property, because it is just listed as a Property in the online help documentation.  As you can see in the one image below, taken from the Locals window, the SketchImages node under the DrawingSketch object has the value <Application-defined or object-defined error>.  And the oSketchImage variable's value remains Nothing.  Out of curiosity, I also copied/pasted this code down into an internal iLogic rule within the drawing, got rid of the 'Set' keywords, and had similar problems.  It encounters an error at the 'DrawingSketch.get_SketchImages()' point in the process.  I even tried getting the SketchImages of the DrawingSketch to a variable first, before trying to use the Add method, and had the same results.  This definitely seems like a bug of some sort.

 

WCrihfield_0-1668451294810.png

WCrihfield_1-1668451323497.png

WCrihfield_2-1668451380032.png

WCrihfield_3-1668451411423.png

 

 

 

 

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes

J.Pelfrene
Enthusiast
Enthusiast

Hi, thank you for trying!

It's quite an annoying issue. Would there be any workaround to add some images on the sheet?

 

Cheers!

0 Likes

WCrihfield
Mentor
Mentor

Well...there is another way that I can think of.  We could bring it in similarly to how you would do it manually, if you were to use the Insert Object tool, on the Manage tab.  Here is some code I just used to bring that same image into my drawing.  When done, the image was at the 0,0 position on my sheet, and its source was under the 3rd Party node of my document, in the browser tree.

Dim oDDoc As DrawingDocument = ThisDoc.Document
Dim oImagePath As String = "C:\Temp\image.png"
Dim oROD As ReferencedOLEFileDescriptor = Nothing
oROD = oDDoc.ReferencedOLEFileDescriptors.Add(oImagePath, OLEDocumentTypeEnum.kOLEDocumentLinkObject)
oROD.BrowserVisible = True
oROD.Visible = True

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

WCrihfield
Mentor
Mentor

Perhaps someone from Autodesk could look into this DrawingSketch.SketchImages property error situation for us, mentioned above.  It may be something that they are already aware of, or have already fixed in newer releases, but I don't know.

@chandra.shekar.g

@MjDeck 

@johnsonshiue 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes

MjDeck
Autodesk
Autodesk

@WCrihfield and @J.Pelfrene , images are not supported in sketches that are created on the drawing sheet or in a view. However, they are supported in the sketches associated with title block, border, and sketch symbol definitions.

You can see this in the UI. If you create a new sketch or draft view, the Insert > Image button on the Sketch ribbon is grayed out.

Maybe we should add a note to the API help.


Mike Deck
Software Developer
Autodesk, Inc.

WCrihfield
Mentor
Mentor

@MjDeck 

I was thinking there was something restricting when/where you can work with images in sketches, but I could not recall exactly what it was and could not find any documentation about it to jog my memory either.  I knew that I had worked with images in sketches several times before by code, so it seemed really odd at the time, but thinking about all those previous examples now, none of them were trying to place images directly out in the main part of a drawing before.  They were all either working in part sketches, drawing title block sketches, or drawing sketched symbol sketches, just like you mentioned.  Thanks for clearing that up for us.

Yes, mentioning this in a Remarks section of the online help page would be nice, and probably save some folks some headache down the road, preventing another inquiry like this.

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

J.Pelfrene
Enthusiast
Enthusiast
Hi @WCrihfield
Thanks for the example. I tried quickly and it adds the image to the document well. But I don't see an API function to also position and scale the image. In the UI it is possible though.
0 Likes

MjDeck
Autodesk
Autodesk

It looks like it's not possible to move an OLE reference image with the API.

Here's an example of placing an image by first making a sketch symbol out of it. See Message 9 on this forum thread, and a more complete example in Message 11.
https://forums.autodesk.com/t5/inventor-ilogic-and-vb-net-forum/inventor-macro-to-insert-an-image/td...


Mike Deck
Software Developer
Autodesk, Inc.