Inventor Macro to Insert an Image

Inventor Macro to Insert an Image

andrew.galbreath
Participant Participant
2,965 Views
10 Replies
Message 1 of 11

Inventor Macro to Insert an Image

andrew.galbreath
Participant
Participant

Hello,

 

I am using Inventor 2019, and I have multiple .idw files with multiple .png files to be inserted into each of them.

 

I know how to do this manually: Manage Tab -> Insert Palette -> Insert Object -> Create from File ->Browse -> Select the applicable file

 

But I haven't found any information on how to do this with a macro, so I'm not sure if it is even possible. Once I know how to do one using a macro, I can easily iterate it to do everything I need.

 

Any help or information would be greatly appreciated.

 

Thanks in advance,

 

-Andrew

Accepted solutions (1)
2,966 Views
10 Replies
Replies (10)
Message 2 of 11

JhoelForshav
Mentor
Mentor
Accepted solution

Hi @andrew.galbreath 

Try this macro 🙂

 

 

Sub InsertImage()

Dim doc As Inventor.Document
Set doc = ThisApplication.ActiveDocument
Dim oImagePath As String
oImagePath = "C:\Users\Jhoel\Testimage.png"
'Use the path to your image


Dim oleReference As ReferencedOLEFileDescriptor

Set oleReference = doc.ReferencedOLEFileDescriptors.Add(oImagePath, OLEDocumentTypeEnum.kOLEDocumentEmbeddingObject)
oleReference.BrowserVisible = True
oleReference.Visible = True
oleReference.DisplayName = Mid$(oImagePath, InStrRev(oImagePath, "\") + 1)
End Sub

 

Message 3 of 11

j_steenbergen
Enthusiast
Enthusiast

nvm

0 Likes
Message 4 of 11

andrew.galbreath
Participant
Participant

This does exactly what I need, as far as I can tell. 

 

Thank you so much for the help!

0 Likes
Message 5 of 11

JSutherKAIDI
Participant
Participant

Hello @JhoelForshav,

 

Is it possible to set the following:

-> insertion point

-> scale

-> send to back

?

 

I have to create spec sheets for many products, following a template. This entails rendering a nice 4K cover image as PNG in the studio environment, embedding it on the front page of an IDW, and manually resizing it to consume all white space (so, so frustrating that images cannot be constrained in IDW). My goal would be insert and scale via ilogic so the placement is consistent.

 

Thanks!

0 Likes
Message 6 of 11

JhoelForshav
Mentor
Mentor

Hi @JSutherKAIDI 

Unfortunately I don't think there is any API exposed for this.

https://forums.autodesk.com/t5/inventor-customization/programing-with-ole-image/td-p/3749725

It's an old thread but I can't find anything suggesting that it's not the same today.

Hopefully, if I'm wrong, someone will correct me 🙂

 

0 Likes
Message 7 of 11

JSutherKAIDI
Participant
Participant

Understandable, thanks for the response!

0 Likes
Message 8 of 11

amaragkoudakis
Explorer
Explorer
This is helpful. Thank you.!
Using your code I am trying to move the image and set a new location into the sheet. Can you help me?
0 Likes
Message 9 of 11

gerrardhickson
Collaborator
Collaborator

So, there doesn't appear to be any way to insert and move an OLE File reference via the API.
As a work around, I figured that you can add the OLR File Reference into a sketched symbol, then move and re-size the sketched symbzHope this helpsol.

Here's some example code:

Sub InsertImageasSketchSymbol()

Dim oDoc As DrawingDocument
Set oDoc = ThisApplication.ActiveDocument

Dim oSkSymDef As SketchedSymbolDefinition
Set oSkSymDef  = oDoc.SketchedSymbolDefinitions.Add("YOUR_SKETCH_SYMBOL_NAME")

Dim oPoint2d As Point2d
'Suggest adding your image at coordinate 0,0. You may like to revise this to suit your application.
Set oPoint2d = ThisApplication.TransientGeometry.CreatePoint2d(0, 0)

Dim oImgFN As String
oImgFN = "C:\YOURFILENAME.PNG"

Dim oSK As DrawingSketch
Set oSK = Nothing

oSkSymDef .Edit oSK
oSK.SketchImages.Add oImgFN, oPoint2d, False
oSkSymDef .ExitEdit

Dim oSkSym as SketchedSymbol
set oSkSym = oDoc.ActiveSheet.SketchedSymbols.Add (oSkSymDef , oPoint2d)

'Then oSkSym is the object you can manipulate to suit your application.

End Sub

 

Hope this helps.

0 Likes
Message 10 of 11

Darkforce_the_ilogic_guy
Advisor
Advisor

can you give me an version of your code where the users select the file and maybe scale down a bit ? maybe the width scale down to 1/4 of the selecte papir size  

0 Likes
Message 11 of 11

Ralf_Krieg
Advisor
Advisor

Hello

 

Try this one. Don't forget to add the reference to the Microsoft Scripting Runtime in VBA Editor --> Tools --> References or it will fail.

Sub InsertImageasSketchSymbol()

'Add a reference to Microsoft Scripting Runtime !!!

Dim oDoc As DrawingDocument
Set oDoc = ThisApplication.ActiveDocument

Dim oImgFN As String
oImgFN = OpenFileDlg '"C:\YOURFILENAME.PNG"

If oImgFN = "" Then Exit Sub

Dim fso As New FileSystemObject
Dim sExt As String
sExt = "." & fso.GetExtensionName(oImgFN)
Dim sPicName As String
sPicName = Replace(fso.GetFileName(oImgFN), sExt, "")

Dim oSkSymDef As SketchedSymbolDefinition
On Error Resume Next
Set oSkSymDef = oDoc.SketchedSymbolDefinitions.Add("Pic_" & sPicName)
If Err.Number <> 0 Then
    Set oSkSymDef = oDoc.SketchedSymbolDefinitions.item("Pic_" & sPicName)
End If

Dim oPoint2d As Point2d
'Suggest adding your image at coordinate 0,0. You may like to revise this to suit your application.
Set oPoint2d = ThisApplication.TransientGeometry.CreatePoint2d(0, 0)

Dim oSK As DrawingSketch
Set oSK = Nothing

oSkSymDef.Edit oSK
oSK.SketchImages.Add oImgFN, oPoint2d, False
oSkSymDef.ExitEdit

Dim oSkSym As SketchedSymbol
Set oSkSym = oDoc.ActiveSheet.SketchedSymbols.Add(oSkSymDef, oPoint2d, 0, 0.25) '<------ 0 is the rotation, 0.25 is the scale factor

'Then oSkSym is the object you can manipulate to suit your application.

End Sub

Private Function OpenFileDlg() As String

    ' Create a new FileDialog object.
    Dim oFileDlg As FileDialog
    Call ThisApplication.CreateFileDialog(oFileDlg)

    ' Define the filter to select part and assembly files or any file.
    'oFileDlg.Filter = "Inventor Files (*.iam;*.ipt)|*.iam;*.ipt|All Files (*.*)|*.*"
    oFileDlg.filter = "All Image Files|*.bmp;*.gif;*.jpg;*.png;*.tif|BMP (*.bmp)|*.bmp|GIF (*.gif)|*.gif|JPEG (*.jpg)|*.jpg|PNG (*.png)|*.png|TIFF (*.tif)|*.tif"

    ' Define the part and assembly files filter to be the default filter.
    oFileDlg.FilterIndex = 1

    ' Set the title for the dialog.
    oFileDlg.DialogTitle = "Open File Test"

    ' Set the initial directory that will be displayed in the dialog.
    oFileDlg.InitialDirectory = "C:\"

    ' Set the flag so an error will be raised if the user clicks the Cancel button.
    oFileDlg.CancelError = True

    ' Show the open dialog.  The same procedure is also used for the Save dialog.
    ' The commented code can be used for the Save dialog.
    On Error Resume Next
    oFileDlg.ShowOpen
'    oFileDlg.ShowSave

    ' If an error was raised, the user clicked cancel, otherwise display the filename.
    If Err Then
        MsgBox "User cancelled out of dialog"
        OpenFileDlg = ""
    ElseIf oFileDlg.fileName <> "" Then
        'MsgBox "File " & oFileDlg.filename & " was selected."
        OpenFileDlg = oFileDlg.fileName
    End If
End Function

 


R. Krieg
RKW Solutions
www.rkw-solutions.com
0 Likes