VBA FileDescriptor.ReplaceReference Clears Adaptivity

VBA FileDescriptor.ReplaceReference Clears Adaptivity

jdasilvaS39UQ
Advocate Advocate
516 Views
5 Replies
Message 1 of 6

VBA FileDescriptor.ReplaceReference Clears Adaptivity

jdasilvaS39UQ
Advocate
Advocate

Hi, I have some code that basically saves one assembly as another with new part numbers for everything in it. We are doing that by opening up each file and calling Document.SaveAs(filename, SaveCopyAs = True), then opening up our new assembly and running FileDescriptor.ReplaceReference(new filename) to get all the new parts in there.

 

Everything works great but it is clearing adaptivity on all the parts in our assembly when we want to keep them adaptive.

 

The other thing we tried was saving the files using a FileSystemObject and ThisApplication.FileManager.CopyFile but these do not reset the part number to its default behavior of updating to the current part number, which we need.

 

We need a way to keep adaptivity and also keep the automatically updating part number, any solutions?

0 Likes
Accepted solutions (1)
517 Views
5 Replies
Replies (5)
Message 2 of 6

SometimesInventorMakesMeAngry
Advocate
Advocate
You should use ComponentOccurrence.Replace instead of FileDescriptor.ReplaceReference. It will retain adaptivity in my testing.
0 Likes
Message 3 of 6

That would require looping through each level of the assembly and replacing any occurrences of the part? That sounds like it would work

0 Likes
Message 4 of 6

Your initial post made it sound like you only had one assembly. In either case, yes, I think it will work.

Alternatively, changing the part number is trivial with Document.PropertySets.Item("Design Tracking Properties").Item("Part Number").Value = partNumber.
0 Likes
Message 5 of 6

J-Camper
Advisor
Advisor
Accepted solution

I think you just need to replace the adaptive instance of each document.  Try something like this:

  • If you are starting with [DocumentDescriptor, ComponentDefinition or a Document object] for each document in the assembly, you can use the Main assembly ComponentDefinition's "ComponentOccurrences.AllReferencedOccurrences([Object])" to get all the Occurrences that refer to the input Object as an Enumerator.  (I believe this will get all levels of occurrences.) 
  • Then you can loop through that enumerator to find the Occurrence marked as Adaptive. (there should only be 1 per part file.)
  • Replace that instance (or possibly ReplaceAll method.  Not sure if that one keep the adaptive Property or not)
  • Then depending on the Replace/ReplaceAll Method, replace the file descriptor the way you currently are to capture the remainder of occurrences.
0 Likes
Message 6 of 6

jdasilvaS39UQ
Advocate
Advocate

This ended up being the best solution and I finally got it working.

The key was swapping out the adaptive occurrence first and then setting ComponentOccurrence.LocalAdaptive = True. Without that line, every adaptive part would still become not adaptive even though the locals window still showed Adaptive = True.

 

Dim curDoc As Document

' Open the document
' rpFile is the replacement filename, determined elsewhere in code
Set curDoc = ThisApplication.Documents.Open(rpFile, False)

' Loop to replace adaptive occurrences first
Dim loopOcc As ComponentOccurrence

     If curDoc.DocumentType = kAssemblyDocumentObject Then
          For Each loopOcc In curDoc.ComponentDefinition.Occurrences
               If loopOcc.Adaptive Then
                    Call loopOcc.Replace2(GetReplacementFile(loopOcc.ReferencedFileDescriptor.FullFileName), False, , True)
                    loopOcc.LocalAdaptive = True
                End If
            Next
        End If

' Next replace all file references
If curDoc.file.ReferencedFileDescriptors.Count > 0 Then
     For Each fd In curDoc.file.ReferencedFileDescriptors
          ' ReplaceReferences is another function that opens up each referenced file and does the same thing, checks for an adaptive occurrence, replaces it, then replaces file descriptors, then recursively opens each file and keeps looping until it makes its way through all the sub-assemblies of each level.
          ReplaceReferences fd
     Next fd
End If

ReplaceReferences function below

Private Sub ReplaceReferences(fd As FileDescriptor)
    Dim rpFile As String
    rpFile = GetReplacementFile(fd.FullFileName)
    If Not rpFile = "" Then
        fd.ReplaceReference rpFile
        Dim curDoc As Document
        Set curDoc = ThisApplication.Documents.Open(rpFile, False)
        If Len(curDoc.DisplayName) > 4 Then
            'Reset Part Number
            'EDITED WP 4-29-21
            curDoc.PropertySets("Design Tracking Properties")("Part Number").value = Left(curDoc.DisplayName, Len(curDoc.DisplayName) - 4)
        End If
        
        ' Loop to replace adaptive occurrences first
            Dim loopOcc As ComponentOccurrence

            If curDoc.DocumentType = kAssemblyDocumentObject Then
                For Each loopOcc In curDoc.ComponentDefinition.Occurrences
                    If loopOcc.Adaptive Then
                        Call loopOcc.Replace2(GetReplacementFile(loopOcc.ReferencedFileDescriptor.FullFileName), False, , True)
                        loopOcc.LocalAdaptive = True
                    End If
                Next
            End If
            
            If curDoc.Dirty = True Then
                curDoc.Save2
            End If
            
        If curDoc.file.ReferencedFileDescriptors.Count > 0 Then
            Dim sFD As FileDescriptor
            For Each sFD In curDoc.file.ReferencedFileDescriptors
                ReplaceReferences sFD
            Next sFD
        End If
        If curDoc.Dirty = True Then
            curDoc.Save2
        End If
        curDoc.Close
    End If
End Sub

 

0 Likes