Copy assembly to new location & replace

Copy assembly to new location & replace

NachoShaw
Advisor Advisor
683 Views
5 Replies
Message 1 of 6

Copy assembly to new location & replace

NachoShaw
Advisor
Advisor

Hey

 

Im copying an assembly from a stored location to a job location and attempting to replace the occurrences within the top level assembly but after much trial and error, i must admit defeat and accept that im going to need some additional help 🙂

 

im working in VBA (unfortunately, a legacy project)

I am copying the assembly over using FileSystemObject mainly because its super fast. I loop through the documents enumerator to get the full file path add add that to a list. Then i loop the list and copy each file to the new location with the new name.

 

I close the template and open the new copy in the new location. At this stage, all occurrences are looking at the originals which are what i need to change to the newly created ones.

 

There arent any resolve issues in the new assembly

 

Here is my ReplaceComponent sub

 

Sub ReplaceAllComponents(oComps As ComponentOccurrences, ByVal NewPath As String, ByVal prefix As String)
    
    Dim FSO As Object: Set FSO = CreateObject("Scripting.FileSystemObject")
    
    Dim oComp As ComponentOccurrence
    For Each oComp In oComps
        Stop
        Dim oDoc As Document: Set oDoc = oComp.Definition.Document
        Dim oldFileName As String: oldFileName = FSO.GetFileName(oDoc.FullFileName)
        Dim NewFileName As String: NewFileName = prefix & oldFileName
        Dim NewFilePath As String: NewFilePath = NewPath & NewFileName
        
            Call oComp.Replace(NewFilePath, False)

        If oComp.SubOccurrences.Count > 0 Then
            Call ReplaceAllComponents(oComp.SubOccurrences, NewPath, prefix)
        End If
    Next
End Sub

i can replace the first component which is a part then on the second component (an assembly), i get this error

NachitoMax_0-1684053180641.png

 

When i try to replace the component manually, i cant do it either. I cant see why i couldnt replace the component as the new version is a copy of the original and the sub components still look at the original components.

 

What is the error and how can i get around it?

 

Is there another / better way to change the components? 

 

 

Thanks

 

 

Nacho
Automation & Design Engineer

Inventor automation Programmer (C#, VB.Net / iLogic)
Furniture, Sheet Metal, Structural, Metal fab, Tradeshow, Fabrication, CNC

EESignature


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.


0 Likes
684 Views
5 Replies
Replies (5)
Message 2 of 6

NachoShaw
Advisor
Advisor

If you trace back the history of Tourettes, you'll find it all started with an Inventor user working with the API....

Nacho
Automation & Design Engineer

Inventor automation Programmer (C#, VB.Net / iLogic)
Furniture, Sheet Metal, Structural, Metal fab, Tradeshow, Fabrication, CNC

EESignature


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.


0 Likes
Message 3 of 6

WCrihfield
Mentor
Mentor

Funny.  If you have ModelStates involved in your assembly, and there may be multiple components in the assembly that represent the same file on disk, but different ModelStates, you may have to update the next component document representing the other ModelState (but same file) before it will allow you to make edits to it.  I have no idea if this specific situation is what you may be encountering though.  I am not sure if replacing a component would be seen as editing it in a way that would have anything to do with what ModelState the component may be set to though either.  Just something fairly new, and odd, related to assemblies I have come across, and thought I would mention it, just in case.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 4 of 6

NachoShaw
Advisor
Advisor

Hi

 

Thanks for replying. I dont have any modelstates in the assembly. I feel that im using a standard method, weird behaviour in the assembly sub components. For example: there are 4 occurrences of the same assembly, 1 of which has resolve issues wher ethe others do not even though they are the exact same assembly occurrence... This is the issue thats giving me the errors.

 

Currently, i am not doing a occurrence document saveas to the new location because the process takes too long with VBA. Im stuck with VBA as its a legacy project that we cant re-write at present. My method is using FileSystemObject to copy the files with the new name and this part works just fine, its the references creating a resolve issue before the Reference replace that im stuck with.

 

Thanks

 

Nacho
Automation & Design Engineer

Inventor automation Programmer (C#, VB.Net / iLogic)
Furniture, Sheet Metal, Structural, Metal fab, Tradeshow, Fabrication, CNC

EESignature


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.


0 Likes
Message 5 of 6

WCrihfield
Mentor
Mentor

OK.  I don't use the copy & replace type routine much in my work, but I know how frustrating it can be having to resolve references and constraints when opening many of our older assemblies.  I'm just tossing a few ideas around, to see if anything may help a bit.  Maybe switching from the older Replace method to the newer Replace2 method that became available in 2021 version would help, since it allows you to specify whether to save edits or not, and whether to keep adaptivity or not.  Or maybe instead of using the ComponentOccurrence.Replace type methods, you could use something like the FileDescriptor.ReplaceReference() method route, since everything it just a copy over at this point.  To use that last one you could get the main AssemblyDocument.File.AllReferencedFiles to a variable, then loop through them and for each File you could loop through its File.ReferencedFileDescriptors, then FileDescriptor.ReplaceReference on each of those...with lots of checks in there, of course.  Just some related thoughts.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 6 of 6

NachoShaw
Advisor
Advisor

Thanks for replying. I was originally using a documentEnumerator to get the document list of paths. I then generated the New name through iteration of the list and copied the files from folder A to Folder B using Scripting.fileSystemObject. I was using this because it can complete the process in about 3 seconds.

 

However.... When looping through the documents and replacing the references, the files wouldnt update links even though i was also using FileDescriptor to get any referenced files. This process was using the FullFileName as the location to the new reference.

 

Even more strange is that doing it in a way that iterates occurrences withi the assembly, using a SaveAs to the folder B with a new name then doing the exact the ReplaceReference with the same FullFileName string but at that moment, it works as expected but takes more than 10 times as long to complete.

 

So, whats the difference between doing a SaveAs from within the assembly vs copying the files first? If i open the file from the SaveAs method BEFORE i replace any references, its looking at the original template files in folder A.

 

For now, i have it working using the SaveAs method but im convinced there is a better / faster way, i just havent got round the Inventor nuances yet 🙂

 

Thanks bud

 

 

Nacho
Automation & Design Engineer

Inventor automation Programmer (C#, VB.Net / iLogic)
Furniture, Sheet Metal, Structural, Metal fab, Tradeshow, Fabrication, CNC

EESignature


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.


0 Likes