Please see the updated macro below. I have highlighted the changes in red:
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
vBodyArr = swPart.GetBodies2(0, False)
RenameBodies swModel, vBodyArr
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
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.
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
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?
[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!
Thanks a lot This just fixed my problem.
is there any easy way to change this to rename only visible bodies?
Yes, just pass True as the second parameter to get visible only:
vBodyArr = swPart.GetBodies2(0, True)
Thanks for your response, it works
Make 2 parts, one with the circle and one with the hexagon. Then create an assembly and insert those parts and use linear pattern.
You cannot give 2 bodies the same name.