Body names from a PRT

Library for macros
Peter Kite
Posts: 5
Joined: Thu Oct 07, 2021 11:03 am
Answers: 0
x 2

Body names from a PRT

Unread post by Peter Kite »

Hello macro Guru's and God's.

Let me sketch you the issue we're facing :

I have a PRT with lots of bodies. Each body has a distinct name.
Now we need to make a DRW of it which is not an issue.

However the problem starts when we want to display (be it in a note or balloon) the names of the different bodies.
You can hook a note/balloon to any of the bodies and Solidworks will even show you that you are indeed clicking on a body with a particular name but there doesn't seem to be a way to get the name of that body part automatically into a note/balloon.

Now my colleague has a PRT with +100 bodies. All properly named and even our VAR had to admit that there is no method to do that.
The best he himself came up with is by doing a save bodies as parts but then the problem is that Solidworks tends to "forget" about half of the proper names and just gives it a default name+ a number

There for the question : Has anyone ever run into a similiar,or indentical problem, and created, or know of, a macro that deals with it?

TLDR;:
We're looking for a macro that will grab the name of a body (created in a PRT) that when selected in a DRW will place a note/balloon on it to identify it as said body.

Thank you kindly in advance to anyone that can help or has a suggestion.
User avatar
Stefan Sterk
Posts: 30
Joined: Tue Aug 10, 2021 2:40 am
Answers: 2
x 39
x 61

Re: Body names from a PRT

Unread post by Stefan Sterk »

Hi Peter,
Have you tried making a Cut-List by going to the Weldments tab and then clicking on the Weldment command.
image.png
You can then add properties to the cut list item, a partname for example.
image.png
image.png (28.79 KiB) Viewed 2181 times
image.png
Which then can be linked to the drawing notes.
image.png
I believe there are some macro out there that can copy the body name to the cut-list-item propperties.
User avatar
JPARKER
Posts: 11
Joined: Mon Mar 15, 2021 4:16 pm
Answers: 0
x 8

Re: Body names from a PRT

Unread post by JPARKER »

I have something that is very close that would not require too much modification. Just need to know what version of SOLIDWORKS are you running?
Peter Kite
Posts: 5
Joined: Thu Oct 07, 2021 11:03 am
Answers: 0
x 2

Re: Body names from a PRT

Unread post by Peter Kite »

Goodmorning.

The version we're running is 2021SP4.

Looking forward to what you had in mind.
Peter Kite
Posts: 5
Joined: Thu Oct 07, 2021 11:03 am
Answers: 0
x 2

Re: Body names from a PRT

Unread post by Peter Kite »

Stefan Sterk wrote: Thu Oct 07, 2021 12:59 pm Hi Peter,
Have you tried making a Cut-List by going to the Weldments tab and then clicking on the Weldment command.


The problem with that is that the cutlist doesn't make a seperate cutlist for each body all of the time.
Second problem is that the cutlist will show you the names of the folder with the body(or bodies) but not the name of the actual bodies.
With which I mean that you get
afbeelding.png
afbeelding.png (15.46 KiB) Viewed 2150 times
And in the BOM you still don't get the names of the bodies. So unless I'm missing something that would mean that I would have to copy the name of the bodies to overwrite the name of the sub cut-lists which would still not really help completely because some sub cut-lists have multiple bodies in it.
Peter Kite
Posts: 5
Joined: Thu Oct 07, 2021 11:03 am
Answers: 0
x 2

Re: Body names from a PRT

Unread post by Peter Kite »

We are getting somewhere because there is a way with cut-list to get there (it didn't work for my colleague but it does on my identical computer with identical software and identical version of Solidworks)./

Now it would be great help if we could get the cut-list folder name to, by means of a macro, take the name of the body it contains. I know it can't be the exact same name but even if there was just a space added before or after the name of the body it should work.

I have searched and I can find all kinds of macro's but none that actually do what we need it.

To all it may concern, thank you in advance for any help, suggestions or links that might help.
User avatar
Stefan Sterk
Posts: 30
Joined: Tue Aug 10, 2021 2:40 am
Answers: 2
x 39
x 61

Re: Body names from a PRT

Unread post by Stefan Sterk »

The code below will put the bodies in a cut-list and add a custom property with the name of the body. Which you can link to a drawing note. And it also change the name of the cutlist folder to the name off the body with an extra space at the end. Let me know how it goes.

Code: Select all

Option Explicit

' Name of the custom properties for body name
Const CustomPropName As String = "PARTNUMBER"

' Make this True if you want to collect identical bodies
Const CollectIdenticalBodies As Boolean = False

Sub main()
    
    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim swModelExt As SldWorks.ModelDocExtension
    Dim swFeat As SldWorks.Feature
    
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    If swModel Is Nothing Then End
    Set swModelExt = swModel.Extension
    
    ' Insert weldment feature if model isn't a weldment
    If Not swModel.IsWeldment Then swModel.FeatureManager.InsertWeldmentFeature
    
    ' DOCUMENT PROPERTIES - WELDMENT OPTIONS
    ' Toggle on creating cut list automaticly
    swModelExt.SetUserPreferenceToggle swUserPreferenceToggle_e.swWeldmentEnableAutomaticCutList, Empty, True
    ' Toggle off automatically renaming cut list folders using the Description property value
    swModelExt.SetUserPreferenceToggle swUserPreferenceToggle_e.swWeldmentRenameCutlistDescriptionPropertyValue, Empty, False
    ' Toggle collect identical bodies On/Off
    swModelExt.SetUserPreferenceToggle swUserPreferenceToggle_e.swWeldmentCollectIdenticalBodies, Empty, CollectIdenticalBodies
    
    ' Loop through cut-list folder
    Set swFeat = swModel.FirstFeature
    LoopThroughCutListFolder swFeat

End Sub

' Loop though cut-list folder
Sub LoopThroughCutListFolder(thisFeat As SldWorks.Feature)
    Dim subfeat As SldWorks.Feature
    While Not thisFeat Is Nothing
        If thisFeat.Name = "Solid Bodies" Then
            ' Update Cut List
            thisFeat.GetSpecificFeature2.UpdateCutList
            Set subfeat = thisFeat.GetFirstSubFeature
            While Not subfeat Is Nothing
                ' If subfeat is a cut-list folder then do the work
                If subfeat.GetTypeName = "CutListFolder" Then
                    DoTheWork subfeat
                End If
                Set subfeat = subfeat.GetNextSubFeature
            Wend
        End If
        Set thisFeat = thisFeat.GetNextFeature
    Wend
End Sub

' Rename cut-list-item folder to body name
' and copy body name to custom cut-list-item properties
Sub DoTheWork(thisFeat As SldWorks.Feature)
    
    Dim CustomPropMgr As SldWorks.CustomPropertyManager
    Dim BodyFolder As SldWorks.BodyFolder
    Dim swBody As SldWorks.Body2
    Dim BodyCount As Long
    
    Set BodyFolder = thisFeat.GetSpecificFeature2
    If BodyFolder Is Nothing Then Exit Sub
    
    ' Get first Body in folder
    Set swBody = BodyFolder.GetBodies(0)
    
    ' Rename Cut-List-Item folder to body name
    thisFeat.Name = swBody.Name & " "
    
    ' Add custom propertie
    Set CustomPropMgr = thisFeat.CustomPropertyManager
    CustomPropMgr.Add3 CustomPropName, swCustomInfoType_e.swCustomInfoText, _
                       swBody.Name, _
                       swCustomPropertyAddOption_e.swCustomPropertyDeleteAndAdd
    
End Sub

Peter Kite
Posts: 5
Joined: Thu Oct 07, 2021 11:03 am
Answers: 0
x 2

Re: Body names from a PRT

Unread post by Peter Kite »

Stefan,

It works like advertised. :)

Thank you ever so much, it will safe my colleague a hell of a lot of time, not to mention the avoidance of lots of aggro.

Have a nice one.

Greets and thanks from Belgium.
Post Reply