- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I'm trying to make a script to replace all instances of a part/assembly in an assembly with a new copy of that file. So I'm trying to make an improved version of the "save and replace component" feature since it will only replace one part. I've created an ideastation post to have "save and replace component" be able to replace all instances of the part you replacing, but I want this feature in the mean time.
So far the script prompts you to select a part, enter a few filename, and does a check on the new file name. I need help with saving a new copy of the selected part and replacing all instances of the original part in the assembly. Is there a simple way to replace all instances or will it require a loop through all components and checking the filename then replacing ones that match, if so that's something I should be able to do. Thanks.
Public Sub Save_Replace_All() 'Checks if open document is assembly If (ThisApplication.ActiveDocument.DocumentType <> kAssemblyDocumentObject) Then MsgBox "This is NOT an assembly document!", vbExclamation Exit Sub End If Dim oPart As ComponentOccurrence Dim opartdoc As Document Dim oPartdoc2 As Document Dim NewFileName As String Dim NewFilePath As String Set oPart = ThisApplication.CommandManager.Pick(kAssemblyOccurrenceFilter, "Select the part to save and replace all.") Set opartdoc = oPart.Definition.Document NewFileName = InputBox("What is the new file name for " & filename_noext(opartdoc.FullFileName) & "?", "New File Name", filename_noext(opartdoc.FullFileName)) NewFilePath = ThisApplication.FileLocations.Workspace & "\" & NewFileName & "." & file_ext(opartdoc.FullFileName) If (fileExists(NewFilePath)) Or (NewFileName = "") Then MsgBox ("Error! Either file exists or no filename given") Exit Sub End If 'needs to save a copy of opartdoc as newfilepath, found this in another script but the script editor doesn't like it opartdoc.SaveAs(newfilepath,False)
'This replaces the selected part but I need to replace them all Call oPart.Replace(NewFilePath, False) End Sub
Solved! Go to Solution.