20 Replies Latest reply on Mar 21, 2018 4:51 AM by Deepak Gupta

    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

       

      thanks

       

      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

        • Re: Rename files with custom properties
          Artem Taturevych

          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

               Dim swCompModel as SldWorks.ModelDoc2

               Set swCompModel = swChild.GetModelDoc2

               If Not swCompModel Is Nothing Then

                    JobNumber = swCompModel.CustomInfo("ProjName")

                    Description = swCompModel.CustomInfo("Comment")

                    DetailNo = swCompModel.CustomInfo("test")

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

                   swChild.Name2 = NewName

                End If

          Next i

          __________________________

          Regards,

          Artem Taturevych

          Application Engineer at Intercad

          http://intercad.com.au/

          Tel: +61 2 9454 4444

            • Re: Rename files with custom properties
              Ryan Hawley

              Artem

               

              Thank you for the reply

               

               

              unfortunatly it does not seem to work I get the same results as my previous code it takes the properties from that Subassembly and not the part file its renaming

                • Re: Rename files with custom properties
                  Deepak Gupta

                  What is your actual requirement of changing the file name in assembly as this will not change the actual file name. Can't you use save as and delete the original part file (both can be done by the macro).

                   

                  Also this may fail in case there is more than one instance of the file.

                    • Re: Rename files with custom properties
                      Ryan Hawley

                      My files names need to be change using the part  properties

                       

                      ex.) file name is 6223 form    with the following custom properties

                      Projname=6223  

                      Comment=form 

                      DetailNo= 123

                       

                      So once the macro is run the file name should be 6223 Det 123 Form

                       

                      The macro currently when it runs does this 6223 Det  it takes the Projname property from the assembly if I delete that property the the filename is just Det

                       

                       

                      I have to do this on all my files in the assembly for mutliple jobs for 100's of components

                          • Re: Rename files with custom properties
                            Ryan Hawley

                            Deepak

                             

                            That does exactly what I want. 

                             

                            Except something strange is happening it renames the file in the assembly design tree but when I open the part file its still the old part file name

                            • Re: Rename files with custom properties
                              Robbie Mosingo

                              This is almost exactly what I have been trying to accomplish. It works great but I cannot figure out how to alter the code to dig into the actual custom properties of each active configuration instead of just looking directly into the part custom properties. Any help on this?

                               

                              Thanks.

                                • Re: Rename files with custom properties
                                  Deepak Gupta

                                  Robbie, please try the attached macro.

                                   

                                  Make sure you try this on sample files first. Macro is tested on SW2014 so in case you've lower version then you'll have to fix the library references. If you get compiler error OR want to fix the library references in macro, check this video on how to fix that Fix SOLIDWORKS Macro Missing References

                                    • Re: Rename files with custom properties
                                      Robbie Mosingo

                                      Deepak,

                                       

                                      Works perfect. Thanks for the help, I will be using this macro often.

                                       

                                      Thanks

                                      • Re: Rename files with custom properties
                                        Vahid Mostofi

                                        Hi Deepak,

                                         

                                        I'm new to API, I tried this in '17. I have drawing_number custom properties defined for each of my parts, I included a variable in the code named Drawing_Number as string, but it seems this information is not getting extracted from the parts when I run code. I get no other error. I pasted the code here, not sure what I'm missing here.

                                         

                                        Thanks,

                                         

                                        Option Explicit

                                         

                                         

                                        Public Enum swUserPreferenceToggle_e

                                            swExtRefUpdateCompNames = 18

                                         

                                         

                                        End Enum

                                         

                                         

                                        Dim swApp            As SldWorks.SldWorks

                                        Dim swModel          As SldWorks.ModelDoc2

                                        Dim swAssy           As SldWorks.AssemblyDoc

                                        Dim swConf           As SldWorks.Configuration

                                        Dim swRootComp       As SldWorks.Component2

                                        Dim bRet             As Boolean

                                        Dim bOldSetting      As Boolean

                                         

                                         

                                        Sub TraverseComponent(swComp As SldWorks.Component2, nLevel As Long)

                                         

                                         

                                            Dim vChildComp              As Variant

                                            Dim swChildComp             As SldWorks.Component2

                                            Dim swCompConfig            As String

                                            Dim sPadStr                 As String

                                            Dim i                       As Long

                                            Dim swChildModel            As SldWorks.ModelDoc2

                                            Dim NewName                 As String

                                            Dim Description             As String

                                            Dim Drawing_Number          As String

                                            Dim JobNumber               As String

                                           

                                           

                                            

                                            bOldSetting = swApp.GetUserPreferenceToggle(swExtRefUpdateCompNames)

                                         

                                         

                                        swApp.SetUserPreferenceToggle swExtRefUpdateCompNames, False

                                         

                                         

                                         

                                         

                                           For i = 0 To nLevel - 1

                                                sPadStr = sPadStr + "  "

                                            Next i

                                           

                                            vChildComp = swComp.GetChildren

                                           

                                            For i = 0 To UBound(vChildComp)

                                         

                                         

                                                Set swChildComp = vChildComp(i)

                                               

                                                Set swChildModel = swChildComp.GetModelDoc

                                         

                                         

                                                If swChildModel.GetType = swDocPART Then

                                               

                                                swChildComp.Select2 False, 0

                                               

                                                'Get configuration name

                                                swCompConfig = swChildComp.ReferencedConfiguration

                                                     

                                                ' Get Configuration Properties Values

                                              

                                                JobNumber = swChildModel.CustomInfo2(swCompConfig, "JobNumber")

                                                Description = swChildModel.CustomInfo2(swCompConfig, "Description")

                                                Drawing_Number = swChildModel.CustomInfo2(swCompConfig, "Drawing_Number")

                                                NewName = Drawing_Number

                                           

                                               

                                                swChildComp.Name2 = NewName

                                               

                                                Debug.Print swChildComp.Name2

                                               

                                                 End If

                                                      

                                                TraverseComponent swChildComp, nLevel + 1

                                                      

                                            Next i

                                           

                                            swApp.SetUserPreferenceToggle swExtRefUpdateCompNames, bOldSetting

                                         

                                         

                                        End Sub

                                         

                                         

                                        Sub main()

                                         

                                         

                                            Set swApp = CreateObject("SldWorks.Application")

                                            Set swModel = swApp.ActiveDoc

                                            Set swConf = swModel.GetActiveConfiguration

                                            Set swRootComp = swConf.GetRootComponent3(True)

                                           

                                            TraverseComponent swRootComp, 1

                                         

                                         

                                        End Sub

                          • Re: Rename files with custom properties
                            Tomas Stasevskij

                            Is it possible to make this macro just to rename filenames?