2

I have found a macro for solidworks that works for an individual part, but I will have to (an the rest of the office) will have to change the name taken by the cut list bodies each time.

is there a way to take the file name from the part (we work in multibody parts) and apply it to the cutlist bodies +1 fo each consecutive body?

As mentioned before this macro renames the bodies, it would be absolutely stellar if it took the name from the part file name as this is in accordance with our standard.

code:

Dim swApp As Object
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 = "Set swCustPropMgr = TheModel.Extension.CustomPropertyManager"

    For Each vBody In vBodyArr
        vBody.Name = prefixName & bodycount
        bodycount = bodycount + 1
    Next vBody

    swModel.EditRebuild3
End Sub
Eddy Alleman
  • 1,096
  • 2
  • 10
  • 21
Alisdair
  • 21
  • 4

1 Answers1

1

I don't know if you already solved your problem, but others might need this too.

You can use

prefixName = swModel.GetPathName

I tested your code and all works well on SolidWorks 2017. It should work on later versions also.

You will probably want to get rid of the full path and extension of the file. Here is a link on stackoverflow showing how to do it: how to remove path and extension

Eddy Alleman
  • 1,096
  • 2
  • 10
  • 21