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

Relink references to renamed files

MTheDesigner
Advocate

Relink references to renamed files

MTheDesigner
Advocate
Advocate

I wrote some code that can go through all my referenced documents, change the job number of the item, save it, and then delete the old file. 

 

However this causes all the links in my assemblies to break. I know there is a FileDecriptor.ReplaceRefrence command, but I do not know how to make sure that the FileDesctriptor is the same document as the on in AllReferencedDocuments.

 

Because I am using a for each loop, I don't know how to address the FileDescripters enum.

 

Here Is the code I have so far

If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
	MsgBox("please only use on an assembly")
	Exit Sub
End If
Dim oDoc As AssemblyDocument = ThisApplication.ActiveDocument
Dim sOldProjectNumber As String = InputBox("Enter old project number", "Old Number",)
Dim sNewProjectNumber As String = InputBox("Enter new project number", "New Number")
Dim oRefDoc As Document
For Each oRefDoc In oDoc.AllReferencedDocuments
	Dim sOldName As String = System.IO.Path.GetFileName(oRefDoc.FullFileName)
	If sOldName.Contains(sOldProjectNumber) Then
		Dim sPath As String = System.IO.Path.GetDirectoryName(oRefDoc.FullFileName)
		Dim sNewName As String = sOldName.Replace(sOldProjectNumber, sNewProjectNumber)
		Try
			Dim oRefPropSet As PropertySet = oRefDoc.PropertySets.Item("Design Tracking Properties")
			Dim oRefProp As [Property] = oRefPropSet.Item("Part Number")
			oRefProp.Value = sNewName
		Catch
			MsgBox(System.IO.Path.GetFileName(oRefDoc.FullFileName))
		End Try
		oRefDoc.SaveAs(sPath & "\" & sNewName, True)
		Dim oDocDesc As FileDescriptor = oDoc.ReferencedDocumentDescriptors.Item(oRefDoc)
		oDocDesc.
		System.IO.File.Delete(sPath & "\" & sOldName)
	End If
Next
 

 

0 Likes
Reply
Accepted solutions (1)
357 Views
3 Replies
Replies (3)

JelteDeJong
Mentor
Mentor

you could use the AllReferencedOccurrences. Some thing like this:

 

Dim doc As AssemblyDocument = ThisDoc.Document
Dim def As AssemblyComponentDefinition = doc.ComponentDefinition

For Each refDoc As Document In doc.AllReferencedDocuments
	Dim newDoc As Document = CreateNewDoc(refDoc)
	
	Dim occs = def.Occurrences.AllReferencedOccurrences(refDoc)
	For Each occ As ComponentOccurrence In occs
		occ.ReferencedFileDescriptor.ReferencedDocument(....)
	Next
Next

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

0 Likes

MTheDesigner
Advocate
Advocate

Hey @JelteDeJong Thanks for responding.

 

I have a couple of questions regarding how your implementation works.

 

Dim newDoc As Document = CreateNewDoc(refDoc)

where are you accessing this CreateNewDoc command from, or is this supposed to be a reference to my code doing this?

Dim occs = def.Occurrences.AllReferencedOccurrences(refDoc)

Can you use a document to select an occurrence? The argument says it should be looking for a component occurrence.

occ.ReferencedFileDescriptor.ReferencedDocument(....)

I don't know what .... means. Is this an operator or is this where I am supposed to put my new document

 

0 Likes

Curtis_Waguespack
Consultant
Consultant
Accepted solution

Hi @MTheDesigner 

 

I think you might get by with just using SaveAs to create the copies and swap out the references, see your other thread for an example:

https://forums.autodesk.com/t5/inventor-ilogic-and-vb-net-forum/what-are-the-rules-for-links-and-ren...

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com