How to replace all sub assy in top assy including nested sub assy

How to replace all sub assy in top assy including nested sub assy

tim11_manhhieu
Advocate Advocate
160 Views
2 Replies
Message 1 of 3

How to replace all sub assy in top assy including nested sub assy

tim11_manhhieu
Advocate
Advocate

Like as title, my minimum code is below.

 

Sub ReplaceSubAssy(oldPath As String, newPath As String)

Dim oAsm As AssemblyDocument
Set oAsm = ThisApplication.ActiveDocument

Dim oDoc As Document

For Each oDoc In oAsm.AllReferencedDocuments

    If oDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
    
        If UCase(oDoc.FullDocumentName) = UCase(oldPath) Then
            oDoc.Replace newPath, True
        End If
    
    End If
            
Next

End Sub
0 Likes
161 Views
2 Replies
Replies (2)
Message 2 of 3

WCrihfield
Mentor
Mentor

Replacing all sub assemblies in the entire structure of a large assembly can be a complicated task to fully automate properly, because it often involves a lot of references and dependencies being changed.  We don't really replace documents, but instead either component occurrences or file descriptors.  You can review the following two methods online help documentations for more information.

ComponentOccurrence.Replace2 

FileDescriptor.ReplaceReference 

To replace component occurrences, you need to be iterating them, instead of referenced documents.  And similarly, in order to replace file descriptors, you need to be iterating them, instead of referenced documents.  You can get to file descriptors several different ways, depending on the situation.

Every Inventor.Document type object (or its sub-types) has a property named File, which is supposed to return an Inventor.File type object, if one exists.  The Document object represents an active set of organized data being held in Inventor's memory.  The File object represents organized data that has already been written to a longer term storage medium.  A Document can exist without a File, such as when we use the 'New' tool to create a new Document, and we have not saved it yet.  And even when both the Document & File both exist, the data in the Document can be different than the data in the File, if changes have been made to the Document since it was last saved.

The File object has the File.ReferencedFileDescriptors property, which returns a FileDescriptorsEnumerator collection object that we can iterate through to work with each FileDescriptor in it.  There are also the following properties of other types of objects which can lead us to a FileDescriptor:

BOMRow.ReferencedFileDescriptor (property of the BOMRow object)

DocumentDescriptor.ReferencedFileDescriptor (property of the DocumentDescriptor object)

There are tons of other object's properties that can lead to a DocumentDescriptor type object, including the ComponentOccurrence.ReferencedDocumentDescriptor property of the ComponentOccurrence object type.

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 3

tim11_manhhieu
Advocate
Advocate

thank you for respond, Grok 3 helped me.

0 Likes