11 Replies Latest reply on Nov 1, 2017 12:38 PM by Alessandro Benzi

    Rename all bodies in part

    Pieter Jan Van Der Stoel

      Hi,

       

      I am facing a couple of parts with >100 bodies. I want to save these bodies as part and make an assembly with the "Save Bodies" feature.

      Before this, I'd like to rename all bodies so they have a unique name. I'm looking to do this with a macro.

       

      I now have this: but the renaming part is not working...

       

      Option Explicit

       

      Sub main()

          Dim swApp                   As SldWorks.SldWorks

          Dim swModel                 As SldWorks.ModelDoc2

          Dim swPart                  As SldWorks.PartDoc

          Dim vBodyArr                As Variant

          Set swApp = Application.SldWorks

          Set swModel = swApp.ActiveDoc

          Set swPart = swModel

          swModel.ClearSelection2 True  

          vBodyArr = swPart.GetBodies2(0, False)

          RenameBodies swModel, vBodyArr 

      End Sub

       

       

      Sub RenameBodies(swModel As SldWorks.ModelDoc2, vBodyArr As Variant)

          Dim swBody                      As SldWorks.Body2

          Dim prefixName As String

          Dim bodycount As Integer

          bodycount = 1

          If IsEmpty(vBodyArr) Then Exit Sub

          prefixName = "Projectnumber"

          For Each vBody In vBodyArr

              vBody.Name = prefixName + bodycount '<-- error is here....

              bodycount = bodycount + 1

          Next vBody

          swModel.Rebuild

      End Sub

       

      Any tips on how to rename the bodies?

      Thanks in advance!

        • Re: Rename all bodies in part
          Artem Taturevych

          Please see the updated macro below. I have highlighted the changes in red:

           

          Option Explicit

           

          Sub main()

           

              Dim swApp As SldWorks.SldWorks

              Dim swModel As SldWorks.ModelDoc2

              Dim swPart As SldWorks.PartDoc

              Dim vBodyArr As Variant

           

              Set swApp = Application.SldWorks

              Set swModel = swApp.ActiveDoc

              Set swPart = swModel

           

              swModel.ClearSelection2 True

           

              vBodyArr = swPart.GetBodies2(0, False)

           

              RenameBodies swModel, vBodyArr

           

          End Sub

           

          Sub RenameBodies(swModel As SldWorks.ModelDoc2, vBodyArr As Variant)

           

              Dim vBody As Variant

              Dim swBody As SldWorks.Body2

              Dim prefixName As String

              Dim bodycount As Integer

              bodycount = 1

           

              If IsEmpty(vBodyArr) Then Exit Sub

           

              prefixName = "Projectnumber"

           

              For Each vBody In vBodyArr

           

                  vBody.Name = prefixName & bodycount

           

                  bodycount = bodycount + 1

           

              Next vBody

           

              swModel.EditRebuild3

           

          End Sub

           

            • Re: Rename all bodies in part
              Alessandro Benzi

              Hi Artem,

              sorry if I reply so long from your precious answer, and thank you for your code (very useful and it works perfectly).

              I am a beginner in SW macros and Visual Basic language, so I have just only a simple question: why doesn’t your macro renumber bodies according to the order of features I created?

              In particular, I created an extrusion of a sketch, then a circular pattern of this extrusion and another extrusion in the end, so I have 25 bodies in total. When I run your macro, these bodies are renamed casually, while I need they are renamed in order as “Projectnumber1”, “Projectnumber2”, and so on, according to the order of creation of the features.

              I would like that my first extrusion becomes “Projectnumber1”, the circular pattern becomes “Projectnumber2”,…, “Projectnumber24”, and the final extrusion becomes “Projectnumber25”.

               

              I hope I was clear.

               

              Thank you in advance, best regards.

                • Re: Rename all bodies in part
                  Nilesh Patel

                  Have a look at the following codes:

                   

                  Option Explicit
                  
                  Sub main()
                  
                      Dim swApp           As SldWorks.SldWorks
                      Dim swModel         As ModelDoc2
                      Dim swFeat          As Feature
                      Dim swBodyFol       As BodyFolder
                      Dim vBodyArr        As Variant
                  
                      Set swApp = Application.SldWorks
                      Set swModel = swApp.ActiveDoc
                  
                      swModel.ClearSelection2 True
                  
                      Set swFeat = swModel.FirstFeature
                      
                      Do While Not swFeat Is Nothing
                          If swFeat.GetTypeName = "SolidBodyFolder" Or swFeat.GetTypeName = "SurfaceBodyFolder" Or _
                          swFeat.GetTypeName = "CutListFolder" Or swFeat.GetTypeName = "SubWeldFolder" Or swFeat.GetTypeName = "SubAtomFolder" Then
                              Set swBodyFol = swFeat.GetSpecificFeature2
                              vBodyArr = swBodyFol.GetBodies
                              RenameBodies swModel, vBodyArr
                              Exit Do
                          End If
                          Set swFeat = swFeat.GetNextFeature
                      Loop
                  
                  End Sub
                  
                  Sub RenameBodies(swModel As SldWorks.ModelDoc2, vBodyArr As Variant)
                  
                      Dim vBody           As Variant
                      Dim swBody          As Body2
                      Dim prefixName      As String
                      Dim bodycount       As Integer
                      bodycount = 1
                  
                      If IsEmpty(vBodyArr) Then Exit Sub
                  
                      prefixName = "Projectnumber"
                  
                      For Each vBody In vBodyArr
                  
                          vBody.Name = prefixName & bodycount
                  
                          bodycount = bodycount + 1
                  
                      Next vBody
                  
                      swModel.EditRebuild3
                  
                  End Sub
                  
                    • Re: Rename all bodies in part
                      Alessandro Benzi

                      Hi Nilesh, thank you for your celerity.

                      I had a look at your code and I realised what you meant to do with your modifications.

                      There's only a problem: when I compile and execute your code (while I am keeping open  the Solidworks part meanwhile  in the background) nothing happens! No part is renamed, all remains unchanged!

                      Do you have any idea about it?

                      Best regards,

                      Alessandro.

                       

                      [EDIT]: I studied your code and I solved my problem. At lines 19-20 I eliminated all the GetTypeNames except "SolidBodyFolder".

                      My new line simply becomes : If swFeat.GetTypeName = "SolidBodyFolder" Then

                       

                      I don't know why the code does not work if others GetTypeNames are present (maybe because I have only solid bodies?) but I reached my goal.

                       

                      Thank you very much!

                • Re: Rename all bodies in part
                  Pieter Jan Van Der Stoel

                  Thanks a lot This just fixed my problem.

                  • Re: Rename all bodies in part
                    Martin Schulmeister

                    Hi,

                    is there any easy way to change this to rename only visible bodies?