Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

iLogic Insert OLE Object problem

13 REPLIES 13
SOLVED
Reply
Message 1 of 14
AlexFielder
2552 Views, 13 Replies

iLogic Insert OLE Object problem

Hi All,

 

I have the following piece of iLogic cobbled together from various sources:

 

Dim doc as Document
doc = ThisApplication.ActiveDocument

'Verify the current document has been saved.
If doc.FullFileName = "" Then
	MessageBox.Show("This document must be saved first.")
	Exit Sub
End If

Dim selectedfile As String = String.Empty
Dim oFileDlg As inventor.FileDialog = Nothing
InventorVb.Application.CreateFileDialog(oFileDlg)
oFileDlg.Filter = "Dwg files (*.dwg)|*.dwg|Excel files (*.xlsx)|*.xlsx|pdf files (*.pdf)|*.pdf|Inventor parts (*.ipt)|*.ipt"
oFileDlg.InitialDirectory = oOrigRefName
oFileDlg.CancelError = True
oFileDlg.MultiSelectEnabled = True
On Error Resume Next
oFileDlg.ShowOpen()
If Err.Number <> 0 Then
Return
ElseIf oFileDlg.FileName <> "" Then
selectedfile = oFileDlg.FileName
End If

MessageBox.Show("You selected: " & selectedfile , "iLogic")
Dim oleReference As ReferencedOLEFileDescriptor
If selectedfile.Contains("|") Then ' we have multiple files selected.
	Dim file As String() = selectedfile.Split("|")
	For Each s as String in file
		MessageBox.Show("You selected: " & s , "iLogic")
		oleReference = doc.ReferencedOLEFileDescriptors.Add(s,kOLEDocumentLinkObject)
		oleReference.BrowserVisible = True
		oleReference.Visible = false
		oleReference.DisplayName = Mid$(s, InStrRev(s, "\") + 1)
	Next
Else
	oleReference = doc.ReferencedOLEFileDescriptors.Add(selectedFile,kOLEDocumentLinkObject)
	oleReference.BrowserVisible = True
	oleReference.Visible = false
	oleReference.DisplayName = Mid$(selectedFile, InStrRev(selectedFile, "\") + 1)
End If

 It works fine except that the resultant "3rd Party" folder contains icons with this image:

 

notepad.PNG

Instead of the image created when you manually click the "Insert Object" button on the Manage Ribbon -> Insert tab:

 

normal.PNG

 

Am I missing something which means that iLogic-inserted olereferences aren't treated the same as manually input ones?

 

Also, when I do this manually, the files are added to Vault Pro 2014 as "children" of whichever file I have run the iLogic within.

 

Thanks in advance,

 

Alex.

13 REPLIES 13
Message 2 of 14
AlexFielder
in reply to: AlexFielder

Strange...

 

I just modified the same ilogic code above to work in vba and lo, it works as expected.

 

Here's my original source modified to be vba-friendly:

 

Public Sub addolereferences()


Dim doc As Document
Set doc = ThisApplication.ActiveDocument

'Verify the current document has been saved.
If doc.FullFileName = "" Then
    MessageBox.Show ("This document must be saved first.")
    Exit Sub
End If

Dim selectedfile As String
'Set selectedfile = ""
Dim oFileDlg As Inventor.FileDialog
Set oFileDlg = Nothing
Call ThisApplication.CreateFileDialog(oFileDlg)
oFileDlg.filter = "Dwg files (*.dwg)|*.dwg|Excel files (*.xlsx)|*.xlsx|pdf files (*.pdf)|*.pdf|Inventor parts (*.ipt)|*.ipt"
oFileDlg.InitialDirectory = oOrigRefName
oFileDlg.CancelError = True
oFileDlg.MultiSelectEnabled = True
On Error Resume Next
Call oFileDlg.ShowOpen
If Err.Number <> 0 Then
Return
ElseIf oFileDlg.FileName <> "" Then
selectedfile = oFileDlg.FileName
End If

'MessageBox.Show("You selected: " & selectedfile , "iLogic")
Dim oleReference As ReferencedOLEFileDescriptor
If InStr(1, selectedfile, "|") > 0 Then
    'If selectedfile.Contains("|") Then ' we have multiple files selected.
    Dim file() As String
    file() = Split(selectedfile, "|")
        For Each s In file
            'MessageBox.Show("You selected: " & s , "iLogic")
            oleReference = doc.ReferencedOLEFileDescriptors.Add(s, kOLEDocumentLinkObject)
            oleReference.BrowserVisible = True
            oleReference.Visible = True
            oleReference.DisplayName = Mid$(s, InStrRev(s, "\") + 1)
        Next
    Else
        oleReference = doc.ReferencedOLEFileDescriptors.Add(selectedfile, kOLEDocumentLinkObject)
        oleReference.BrowserVisible = True
        oleReference.Visible = False
        oleReference.DisplayName = Mid$(selectedfile, InStrRev(selectedfile, "\") + 1)
    End If
End Sub

 

the one functional change (which makes no difference in iLogic!) is 

oleReference.Visible = True

 instead of the previous:

 

oleReference.Visible = False

 

Is this a bug or "as intended"? Smiley Embarassed

Message 3 of 14

I get the equivalent results in the iLogic and VBA versions of your rule.

To work correctly in VBA it is necessary to add SET to the both code lines:

Set oleReference = doc.ReferencedOLEFileDescriptors.Add(selectedfile, kOLEDocumentLinkObject)

 

In my test I added a bitmap image to the drawing sheet. Both properties

oleReference.BrowserVisible = True / False  and  oleReference.Visible = True / False

work as expected.


Vladimir Ananyev
Developer Technical Services
Autodesk Developer Network

Message 4 of 14

Thank Vladimir, I had forgotten to "Set" the alternating oleReference objects.

 

That doesn't answer the question as to why code that is functionally identical works when used via vba but not in iLogic?

Message 5 of 14

Strange, I don't see any difference between iLogic and VBA versions.

I used Inventor Prof 2015 SP1 Update 1 (Build 203).

What is your configuration?


Vladimir Ananyev
Developer Technical Services
Autodesk Developer Network

Message 6 of 14

Hi Vladimir,

 

I am using Inventor 2014 Pro SP2 Update 3 (Build 246)

 

Thanks,

 

Alex.

Message 7 of 14

Hi Alex,

It looks like the problem with icons in case of iLogic rule has surprisingly simple solution suggested by Mike Deck.

 

In VB.NET (and thus iLogic), Enum members (constants) must be qualified with their type name.

To find the type name, search for the enum (in Inventor, almost all enum constants start with the letter k)

in the Inventor API help. Searching for kOLEDocumentLinkObject,  you will find the OLEDocumentTypeEnum page.

 

To help detect these constants in code, you can add the statement

        Option Explicit On

at the top of the rule. This will require that all variables be declared explicitly.

 

So I've added "Option Explicit On" in the first line, and then added OLEDocumentTypeEnum  qualifier to both constants kOLEDocumentLinkObject.

Other minor changes do not influence to the icons and were added to simplify code a little.

Here is my final version.  This code works both in Inventor 2014 and 2015.

 
Option Explicit On

'current document
Dim doc as Inventor.Document = ThisDoc.Document

'Verify the current document has been saved.
If doc.FullFileName = "" Then
	MessageBox.Show("This document must be saved first.")
	Exit Sub
End If

'default folder
Dim FolderName As String  = Left$(doc.FullFileName, InStrRev(doc.FullFileName, "\") )	
Dim selectedfile As String = String.Empty
Dim oFileDlg As inventor.FileDialog = Nothing
InventorVb.Application.CreateFileDialog(oFileDlg)
'oFileDlg.Filter = "Dwg files (*.dwg)|*.dwg|Excel files (*.xlsx)|*.xlsx|pdf files (*.pdf)|*.pdf|Inventor parts (*.ipt)|*.ipt"
oFileDlg.InitialDirectory = FolderName
oFileDlg.CancelError = True
oFileDlg.MultiSelectEnabled = True

Try
	oFileDlg.ShowOpen()
	selectedfile = oFileDlg.FileName
Catch
	Return  'operation was cancelled by the user
End Try

Dim oleReference As ReferencedOLEFileDescriptor
If selectedfile.Contains("|") Then ' we have multiple files selected.
	Dim file As String() = selectedfile.Split("|")
	For Each s as String in file
		oleReference = doc.ReferencedOLEFileDescriptors _
			.Add(s, OLEDocumentTypeEnum.kOLEDocumentLinkObject)
		oleReference.BrowserVisible = True
		oleReference.Visible = False
		oleReference.DisplayName = Mid$(s, InStrRev(s, "\") + 1)
	Next
Else
	oleReference = doc.ReferencedOLEFileDescriptors _
			.Add(selectedFile,OLEDocumentTypeEnum.kOLEDocumentLinkObject)
	oleReference.BrowserVisible = True
	oleReference.Visible = False
	oleReference.DisplayName = Mid$(selectedFile, InStrRev(selectedFile, "\") + 1)
End If

The following figures illustrate the difference in results: 

Without qualifiersWith qualifier

 

cheers,


Vladimir Ananyev
Developer Technical Services
Autodesk Developer Network

Message 8 of 14

Thanks Vladimir (And Mike) for the solution.

 

🙂

Message 9 of 14
Anonymous
in reply to: Vladimir.Ananyev

Hi.

I have similar problem.

I make an PDF and DWG drawing of my idw with I logic, that works fine, but then I need to add it manually by Insert object button and so on to get it as a "3rd Party" files in the idw.

What would be the easiest code to add the files with ilogic?

 

Message 10 of 14
AlexFielder
in reply to: Anonymous

@Anonymous - not sure why I hadn't seen this reply before now, so apologies for that! For anyone else interested here is my completed Rule:

 

Option Explicit On
Sub Main()
	
	'current document
	Dim doc as Inventor.Document = ThisDoc.Document
	
	'Verify the current document has been saved.
	If doc.FullFileName = "" Then
		MessageBox.Show("This document must be saved first.")
		Exit Sub
	End If
	
	'default folder
	Dim FolderName As String  = Left$(doc.FullFileName, InStrRev(doc.FullFileName, "\") )	
	Dim selectedfile As String = String.Empty
	Dim oFileDlg As inventor.FileDialog = Nothing
	InventorVb.Application.CreateFileDialog(oFileDlg)
	oFileDlg.Filter = "Dwg files (*.dwg)|*.dwg|Excel files (*.xlsx)|*.xlsx|pdf files (*.pdf)|*.pdf|Inventor parts (*.ipt)|*.ipt|Inventor iFeatures (*.ide)|*.ide|Other files (*.*)|*.*"
	oFileDlg.InitialDirectory = FolderName
	oFileDlg.CancelError = True
	oFileDlg.MultiSelectEnabled = True
	
	Try
		oFileDlg.ShowOpen()
		selectedfile = oFileDlg.FileName
	Catch
		Return  'operation was cancelled by the user
	End Try
	AddReferences(doc, selectedfile)
End Sub
Public Sub AddReferences(ByVal odoc as Inventor.Document, ByVal selectedfile As String)
	Dim oleReference As ReferencedOLEFileDescriptor
	If selectedfile.Contains("|") Then ' we have multiple files selected.
		Dim file As String() = selectedfile.Split("|")
		For Each s as String in file
			oleReference = odoc.ReferencedOLEFileDescriptors _
				.Add(s, OLEDocumentTypeEnum.kOLEDocumentLinkObject)
			oleReference.BrowserVisible = True
			oleReference.Visible = False
			oleReference.DisplayName = Mid$(s, InStrRev(s, "\") + 1)
		Next
	Else
		oleReference = odoc.ReferencedOLEFileDescriptors _
				.Add(selectedFile,OLEDocumentTypeEnum.kOLEDocumentLinkObject)
		oleReference.BrowserVisible = True
		oleReference.Visible = False
		oleReference.DisplayName = Mid$(selectedFile, InStrRev(selectedFile, "\") + 1)
	End If
End Sub
Message 11 of 14
Jonathanspies
in reply to: AlexFielder

Hi,

Its an older post but maybe I get an answer here.

I used the rule above to attache pdf/stp/dxf exports to the file. It works fine.

 

My problem is the following.

When I change something on the drawing i will export the files again and it will automaticlly update the export-files always the same location. With my current rule it always adds new links in the 3rd party section. Can i add a command to the rule which is looking if these Reference is already existing, if so then just cancel and don't add new links?

 

Option Explicit On

Option Explicit On
Sub Main()
	
	'current document
	Dim doc As Inventor.Document = ThisDoc.Document
	
	'Verify the current document has been saved.
	If doc.FullFileName = "" Then
		MessageBox.Show("This document must be saved first.")
		Exit Sub
	End If
	
	'default folder
	Dim FolderName As String  = Left$(doc.FullFileName, InStrRev(doc.FullFileName, "\") )	
	Dim selectedfile As String = String.Empty
	Dim selectedfile2 As String = String.Empty
	Dim selectedfile3 As String = String.Empty
	Dim oFileDlg As Inventor.FileDialog = Nothing
	InventorVb.Application.CreateFileDialog(oFileDlg)
	'oFileDlg.Filter = "Dwg files (*.dwg)|*.dwg|Excel files (*.xlsx)|*.xlsx|pdf files (*.pdf)|*.pdf|Inventor parts (*.ipt)|*.ipt|Inventor iFeatures (*.ide)|*.ide|Other files (*.*)|*.*"
	'oFileDlg.InitialDirectory = FolderName
	'oFileDlg.CancelError = True
	'oFileDlg.MultiSelectEnabled = True
	
	Try
		'oFileDlg.ShowOpen()
		selectedfile = "C:\Vault_WORK\TC_VAULT\DOCUMENTS\KS US\00_EXPORT-2D\" & ThisDoc.FileName(False) & ".pdf"
		selectedfile2 = "C:\Vault_WORK\TC_VAULT\DOCUMENTS\KS US\00_EXPORT-2D\" & ThisDoc.FileName(False) & ".dxf"
		selectedfile3 = "C:\Vault_WORK\TC_VAULT\DOCUMENTS\KS US\00_EXPORT-2D\" & ThisDoc.FileName(False) & ".stp"
	Catch
		Return  'operation was cancelled by the user
	End Try
	AddReferences(doc, selectedfile)
	AddReferences2(doc, selectedfile2)
	AddReferences3(doc, selectedfile3)
End Sub
Public Sub AddReferences(ByVal odoc As Inventor.Document, ByVal selectedfile As String)
	Dim oleReference As ReferencedOLEFileDescriptor
		oleReference = odoc.ReferencedOLEFileDescriptors _
				.Add(selectedfile,OLEDocumentTypeEnum.kOLEDocumentLinkObject)
		oleReference.BrowserVisible = True
		oleReference.Visible = False
		oleReference.DisplayName = Mid$(selectedfile, InStrRev(selectedfile, "\") + 1)
End Sub
Public Sub AddReferences2(ByVal odoc As Inventor.Document, ByVal selectedfile2 As String)
	Dim oleReference As ReferencedOLEFileDescriptor
		oleReference = odoc.ReferencedOLEFileDescriptors _
				.Add(selectedfile2,OLEDocumentTypeEnum.kOLEDocumentLinkObject)
		oleReference.BrowserVisible = True
		oleReference.Visible = False
		oleReference.DisplayName = Mid$(selectedfile2, InStrRev(selectedfile2, "\") + 1)
End Sub
Public Sub AddReferences3(ByVal odoc As Inventor.Document, ByVal selectedfile3 As String)
	Dim oleReference As ReferencedOLEFileDescriptor
		oleReference = odoc.ReferencedOLEFileDescriptors _
				.Add(selectedfile3,OLEDocumentTypeEnum.kOLEDocumentLinkObject)
		oleReference.BrowserVisible = True
		oleReference.Visible = False
		oleReference.DisplayName = Mid$(selectedfile3, InStrRev(selectedfile3, "\") + 1)
End Sub

 

Message 12 of 14

Hi AlexFielder.

Thanks a lot for this. I still had this problem daily or weekly but now you have solved it for me!

Me and my colleges will now use this rule. It works perfect!

Message 13 of 14

Finally I found out myself how to check if the link is already existing... in case it is there it will delete it

I have now a complete  function which saves PDF,DXF and STP of a drawing and attaches the "Export" files to the drawing. So when I check it into the vault all files are there...

 

PDF/DXF/STEP Export is not included in the code below...

 

Option Explicit On
Sub Main()
	'current document
	Dim doc As Inventor.Document = ThisDoc.Document
	Dim oleReference As ReferencedOLEFileDescriptor = Nothing
	Dim selectedfilePDF As String = String.Empty
	Dim selectedfileDXF As String = String.Empty
	Dim selectedfileSTP As String = String.Empty
	
'Verify the current document has been saved.
If doc.FullFileName = "" Then
MessageBox.Show("This document must be saved first.")
Exit Sub
End If

' Link file
Try
selectedfilePDF = "C:\Vault_WORK\TC_VAULT\DOCUMENTS\KS US\00_EXPORT-2D\" & "EXPORT_" & ThisDoc.FileName(False) & ".pdf"
selectedfileDXF = "C:\Vault_WORK\TC_VAULT\DOCUMENTS\KS US\00_EXPORT-2D\" & "EXPORT_" & ThisDoc.FileName(False) & ".dxf"
selectedfileSTP = "C:\Vault_WORK\TC_VAULT\DOCUMENTS\KS US\00_EXPORT-2D\" & "EXPORT_" & ThisDoc.FileName(False) & ".stp"
Catch
Return  'operation was cancelled by the user
End Try
	AddReferencesPDF(doc, selectedfilePDF)
	AddReferencesDXF(doc, selectedfileDXF)
	AddReferencesSTP(doc, selectedfileSTP)
End Sub

'PDF-Attachment
Public Function AddReferencesPDF(ByVal odoc As Inventor.Document, ByVal selectedfilePDF As String)
	Dim doc As Inventor.Document = ThisDoc.Document
	Dim OLERefPDF As ReferencedOLEFileDescriptor = Nothing
	Dim OLERefsPDF As ReferencedOLEFileDescriptors = doc.ReferencedOLEFileDescriptors
		Try
		OLERefPDF = OLERefsPDF.ItemByName(selectedfilePDF)
	            ' File is already attached - delete the attachment so it can be recreated...
	            OLERefPDF.Delete()
		Catch
	            ' The file is not already attached - continue...
	    End Try
		Try
        OLERefPDF = doc.ReferencedOLEFileDescriptors.Add(selectedfilePDF, OLEDocumentTypeEnum.kOLEDocumentLinkObject)
        Catch
        MsgBox("Failed to link to the file", 64, "Export vault")
        Return False
        End Try
End Function
'DXF-Attachment
Public Function AddReferencesDXF(ByVal odoc As Inventor.Document, ByVal selectedfileDXF As String)
	Dim doc As Inventor.Document = ThisDoc.Document
	Dim OLERefDXF As ReferencedOLEFileDescriptor = Nothing
	Dim OLERefsDXF As ReferencedOLEFileDescriptors = doc.ReferencedOLEFileDescriptors
		Try
		OLERefDXF = OLERefsDXF.ItemByName(selectedfileDXF)
	            ' File is already attached - delete the attachment so it can be recreated...
	            OLERefDXF.Delete()
		Catch
	            ' The file is not already attached - continue...
	    End Try
		Try
        OLERefDXF = doc.ReferencedOLEFileDescriptors.Add(selectedfileDXF, OLEDocumentTypeEnum.kOLEDocumentLinkObject)
        Catch
        MsgBox("Failed to link to the file", 64, "Export vault")
        Return False
        End Try
End Function
'STEP-Attachment
Public Function AddReferencesSTP(ByVal odoc As Inventor.Document, ByVal selectedfileSTP As String)
	Dim doc As Inventor.Document = ThisDoc.Document
	Dim OLERefSTP As ReferencedOLEFileDescriptor = Nothing
	Dim OLERefsSTP As ReferencedOLEFileDescriptors = doc.ReferencedOLEFileDescriptors
		Try
		OLERefSTP = OLERefsSTP.ItemByName(selectedfileSTP)
	            ' File is already attached - delete the attachment so it can be recreated...
	            OLERefSTP.Delete()
		Catch
	            ' The file is not already attached - continue...
	    End Try
		Try
        OLERefSTP = doc.ReferencedOLEFileDescriptors.Add(selectedfileSTP, OLEDocumentTypeEnum.kOLEDocumentLinkObject)
        Catch
        MsgBox("Failed to link to the file", 64, "Export vault")
        Return False
        End Try
End Function
Message 14 of 14
AlexFielder
in reply to: Jonathanspies

I'm glad you figured out a process that works for you, but you know that those three methods are functionally and objectively identical right?

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report