How could I bulk delete "Copy of" in virtual component's name (I got over 2000 parts to rename)
Here is a sample Vertual Component Assembly.
I feel SW's Visual Component naming system isn't following a well thought method.
1.) First I created VC and out of some of them, created another Sub Assembly to simplify it further. Now the file naming is:
2.) In the next assembly and with above experience, I started with small sub-assemblies; to follow the same above file structure. Now the file naming is:
#1 and #2 has the same file structure but SW renamed them differently. Why does it keep adding "Copy of" on top of "Copy of" and on and on? Is it really necessary to show how many steps taken to create a sub assembly?
Retrieved #1 old copy and reworked to get the same file names as #2. SW hasn't thought of this very well. I need "Copy of" is gone as well.
I tried this and if you delete all the copies there is nothing left in the model. I had to force the bRet so I had to hold onto the step button to run it all the way through.
I can rename them no problem, but deleting is more difficult for some reason. I already had this macro, but it doesn't account for your folders. Otherwise it works fine.
Have you tried macro from this post Rename the virtual component in Assembly design tree
1) Shift select all components
2) RMB all components and select Save Part(In External File)
3) Select a dummy folder and save them all
4) Run Pack and go on the assembly
5) Use Select / Replace Function inside Pack and go to remove "Copy of" from all filenames.
6) Save Pack and go assembly to a different dummy folder
7) Open new assembly and Shift Select all components and make them virtual.
8) Save as and overwrite or replace as needed.
Bah...doesn't work as pointed out below.
When I create VCs as #7, I assume that it adds "Copy of" again. Am I mistaken?
BTW, i've abounded this VC idea and creating multibody parts (previously Sub-assemblies) assemblies.
Michael Fernando wrote: When I create VCs as #7, I assume that it adds "Copy of" again. Am I mistaken?BTW, i've abounded this VC idea and creating multibody parts (previously Sub-assemblies) assemblies.
Michael Fernando wrote:
Bah! Yes...you are correct. That is what I get for not actually testing out the method that I put in. I thought it would work. Sorry about that!
I guess a macro is the way to go.
Try the attached macro.
Dim swApp As SldWorks.SldWorks
Dim swModel As ModelDoc2
Dim swRootComp As SldWorks.Component2
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
'check if an assembly is open
If swModel Is Nothing Then Exit Sub
If Not swModel.GetType = swDocASSEMBLY Then Exit Sub
'get root component for this assembly (configuration)
Set swRootComp = swModel.ConfigurationManager.ActiveConfiguration.GetRootComponent3(True)
' Traverse an assembly, to find all child-components
Sub TraverseAssembly(swComp As Component2)
Dim swChildComp As SldWorks.Component2
Dim vChild As Variant
Dim ComponentsAtSameLevel As Collection
Set ComponentsAtSameLevel = New Collection
'try to loop through children of this component
For Each vChild In swComp.GetChildren
Set swChildComp = vChild
'only change virtual components
If swChildComp.IsVirtual Then
'use this procedure as recursive procedure, to go one level deeper
'Rename all components in a collection
Sub RenameComponentsCollection(Components As Collection)
Const Find1 As String = "Copy of "
Const Find2 As String = ") of "
Dim swComponent As Component2
Dim NewName As String
For Each swComponent In Components
NewName = GetCompName(swComponent)
RemoveLeftOfString NewName, Find1
RemoveLeftOfString NewName, Find2
RenameComponent swComponent, NewName
' Remove SubString (and all characters to the left of it) from the MainString
Sub RemoveLeftOfString(ByRef MainString As String, SubString As String)
If InStrRev(MainString, SubString) <> 0 Then
MainString = Right(MainString, Len(MainString) - InStrRev(MainString, SubString) - Len(SubString) + 1)
' Rename a Component and add an iterator if the new Component-name is not available
Sub RenameComponent(swComponent As Component2, NewName As String, Optional Iteration As Long)
Dim NewNameWithIterator As String
NewNameWithIterator = NewName
If Iteration > 0 Then NewNameWithIterator = NewName & " " & Iteration
swComponent.Select4 False, Nothing, False
swComponent.Name2 = NewNameWithIterator
If GetCompName(swComponent) <> NewNameWithIterator And Iteration < 10 Then
RenameComponent swComponent, NewName, Iteration + 1
' Get the short name of the Component
Function GetCompName(swComp As Component2) As String
Dim compname As String
compname = swComp.Name2
compname = Right(compname, Len(compname) - InStrRev(compname, "/"))
compname = Left(compname, InStr(compname, "^") - 1)
GetCompName = compname
Retrieving data ...