12 Replies Latest reply on Apr 20, 2016 7:44 AM by Tomas Stasevskij

    Rename files with custom properties

    Ryan Hawley

      I'm trying to create a macro that traverses thru the sub assembly and renames the part files using the custom properties associated in that paticular file.


      I used the Rename macro that is in the Solidworks API help.  My problem is it takes the properties from the main subassembly and not the paticular part file.  I'm very green to macro's so any help you can provide would be helpful




      This is what I have so far


      Option Explicit


      Public Enum swUserPreferenceToggle_e


           swExtRefUpdateCompNames = 18


      End Enum



      Sub main()


           Dim swApp                   As SldWorks.SldWorks

           Dim swModel                 As SldWorks.ModelDoc2

           Dim swConfigMgr             As SldWorks.ConfigurationManager

           Dim swConfig                As SldWorks.Configuration

           Dim swRootComp              As SldWorks.Component2

           Dim SwCustProp              As SldWorks.CustomPropertyManager

           Dim Children                As Variant

           Dim bOldSetting             As Long

           Dim swChild                 As SldWorks.Component2

           Dim ChildCount              As Integer

           Dim DetailNo                As String

           Dim JobNumber               As String

           Dim Description             As String

           Dim NewName                 As String



           bOldSetting = swApp.GetUserPreferenceToggle(swExtRefUpdateCompNames)



          swApp.SetUserPreferenceToggle swExtRefUpdateCompNames, False

          Dim bRet                    As Boolean

          Dim i                       As Long

          Dim DetNumber               As String


          Set swApp = Application.SldWorks

          Set swModel = swApp.ActiveDoc

          Set swConfigMgr = swModel.ConfigurationManager

          Set swConfig = swConfigMgr.ActiveConfiguration

          Set swRootComp = swConfig.GetRootComponent

          bOldSetting = swApp.GetUserPreferenceToggle(swExtRefUpdateCompNames)

          swApp.SetUserPreferenceToggle swExtRefUpdateCompNames, False

          Children = swRootComp.GetChildren

          ChildCount = UBound(Children)


         For i = 0 To ChildCount

               Set swChild = Children(i)

           ' Changing component name requires component to be selected

                bRet = swChild.Select2(False, 0)

           ' Get Detail Number from Custom Properties


         JobNumber = swModel.CustomInfo("ProjName")

          Description = swModel.CustomInfo("Comment")

          DetailNo = swModel.CustomInfo("test")

          NewName = JobNumber + "Det " + DetailNo + " " + Description


          swChild.Name2 = NewName


          Next i

          swApp.SetUserPreferenceToggle swExtRefUpdateCompNames, bOldSetting


      End Sub