9 Replies Latest reply on Aug 15, 2018 10:53 AM by Dan Pihlaja

    Bulk Delete "Copy of" in virtual component name

    Michael Fernando

      How could I bulk delete "Copy of" in virtual component's name (I got over 2000 parts to rename)

        • Re: Bulk Delete "Copy of" in virtual component name
          Michael Fernando

          Here is a sample Vertual Component Assembly.

          • Re: Bulk Delete "Copy of" in virtual component name
            Michael Fernando

            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?

            • Re: Bulk Delete "Copy of" in virtual component name
              Danniel Sims

              Michael,

               

              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.

               

              Capture.PNG

              • Re: Bulk Delete "Copy of" in virtual component name
                Dan Pihlaja

                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. 

                • Re: Bulk Delete "Copy of" in virtual component name
                  Koos Kubus

                  Try the attached macro.

                  Source code:

                  Option Explicit
                  
                  
                  Dim swApp As SldWorks.SldWorks
                  
                  
                  Sub main()
                      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)
                      
                      TraverseAssembly swRootComp
                  
                  
                  End Sub
                  
                  
                  
                  
                  ' 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
                                  TraverseAssembly swChildComp
                                  ComponentsAtSameLevel.Add swChildComp
                              End If
                      Next vChild
                      
                      RenameComponentsCollection ComponentsAtSameLevel
                  
                  
                  End Sub
                  
                  
                  
                  
                  '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
                      Next swComponent
                  
                  
                  End Sub
                  
                  
                  
                  
                  ' 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)
                      End If
                  End Sub
                  
                  
                  
                  
                  ' 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
                      End If
                  End Sub
                  
                  
                  
                  
                  ' 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
                  End Function