solid bodies saved out as unique parts

I know how to cut this solid into segments like sketch which will result in separate bodies within the part file.
Q1) what is the best workflow to make these bodies as separate standalone files? Specifically they will be .STL or similar format for 3DP. Seems to me I just hid the ones I didn’t want, then file save as name1.STL, rinse & repeat.

Q2) Side question & maybe what I’m getting hung up on another project. I brought a few instances of a similar multi-body part into an assembly & selectively turned bodies on & off between the distinct parts. It seemed to me selecting specific bodies of part-1 was simultaneously affecting part-2. I can provide a better example of this but I can’t recall how (or if I did) ultimately fix this. Possibly I incorporated configurations which suppressed unwanted bodies vs just hiding them?

Try this to export the bodies as STL files. I wish I knew who to give credit for this macro, it is not my work.

Dim swApp As Object

Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long

Sub main()

Dim swModel                     As SldWorks.ModelDoc2
Dim swPart                      As SldWorks.PartDoc
Dim vBody As Variant

Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swPart = swModel


swModel.ClearSelection2 True

Debug.Print "Start the STL Saver program"
Debug.Print "File = " & swModel.GetPathName

'longstatus = swModel.SaveAs3(Strings.Left(swModel.GetPathName, Strings.Len(swModel.GetPathName) - 7) & "_" & "teset" & ".STL", 0, 0)

vBody = swPart.GetBodies2(swSolidBody, True)
 
 SelectBodies swApp, swModel, vBody, ""

 MsgBox ("Done saving STL")
  Debug.Print "  END   "

End Sub

Sub SelectBodies(swApp As SldWorks.SldWorks, swModel As SldWorks.ModelDoc2, vBody As Variant, sPadStr As String)

    Dim swModExt                    As SldWorks.ModelDocExtension

    Dim swBody                      As SldWorks.Body2

    Dim sBodySelStr                 As String

    Dim sBodyTypeSelStr             As String

    Dim i, j, k, TEMP               As Long

    Dim bRet                        As Boolean
    
    Dim sFileName As String

    If IsEmpty(vBody) Then Exit Sub

    Set swModExt = swModel.Extension

    'MsgBox "hide all now"
    For i = 0 To UBound(vBody)
        TEMP = i
        SelectBody swApp, swModel, vBody, " ", TEMP
        
        swModel.FeatureManager.HideBodies
        swModel.ClearSelection2 True
        
    Next i

        
    'MsgBox "select each one at a time  now"
    Debug.Print "Start turning on bodies one at a time and save it!"
    For i = 0 To UBound(vBody)
        'Debug.Print "i= " & i
        'Debug.Print "vBody " & UBound(vBody)
        
        TEMP = i
        SelectBody swApp, swModel, vBody, " ", TEMP
        
        swModel.FeatureManager.ShowBodies
        'MsgBox "selected" & sBodySelStr
        Set swBody = vBody(i)
        sBodySelStr = swBody.GetSelectionId
        Debug.Print sBodySelStr
        
        sFileName = Strings.Left(swModel.GetPathName, Strings.Len(swModel.GetPathName) - 7) & "_" & CStr(i + 1) & ".STL"
        Debug.Print sFileName
        
        longstatus = swModel.SaveAs3(sFileName, 0, 0)
        
        swModel.FeatureManager.HideBodies
        swModel.ClearSelection2 True

    Next i
    
    'show all
    'MsgBox "show all now"
    For i = 0 To UBound(vBody)
    
        TEMP = i
        SelectBody swApp, swModel, vBody, " ", TEMP
        swModel.FeatureManager.ShowBodies
        swModel.ClearSelection2 True
        
    Next i

End Sub

Sub SelectBody(swApp As SldWorks.SldWorks, swModel As SldWorks.ModelDoc2, vBody As Variant, sPadStr As String, numberI As Long)

    Dim swModExt                    As SldWorks.ModelDocExtension

    Dim swBody                      As SldWorks.Body2

    Dim sBodySelStr                 As String

    Dim sBodyTypeSelStr             As String

    Dim i, j, k                         As Long

    Dim bRet                        As Boolean
    
    Dim sFileName As String

    
    If IsEmpty(vBody) Then Exit Sub

    Set swModExt = swModel.Extension

        Set swBody = vBody(numberI)
        sBodySelStr = swBody.GetSelectionId
        
        Debug.Print "  SELECBODY: " & sPadStr & sBodySelStr
        'MsgBox "selected body"
        
        Select Case swBody.GetType

            Case swSolidBody
                sBodyTypeSelStr = "SOLIDBODY"
                
            Case swSheetBody
                sBodyTypeSelStr = "SURFACEBODY"

            Case Else

                Debug.Assert False
        End Select
        
        bRet = swModExt.SelectByID2(sBodySelStr, sBodyTypeSelStr, 0#, 0#, 0#, True, 0, Nothing, swSelectOptionDefault): Debug.Assert bRet
        
    End Sub

In answer to question 2:
Using configurations should work. You mention hiding bodies but I would either suppress them or use the delete/keep bodies feature to control with configurations.

Thanks. I’m aware of & written VBA macros in Excel but its on my immediate to-do list to get up to speed in SW.
If I’m understanding this macro, it saves out any & all bodies it sees in the part file using their existing body name?

If you are hiding the bodies (and not suppressing features), then in order to have one instance show one set of bodies and another instance show a different set of bodies, you will have to use Display States.

You will need to set up the display states first at the part level to show what bodies you want to show in each instance.
Edit: Here is a better explanation:

Here is my part:
image.png
Go to your Config tab to see display states:
image.png
First, go to properties:
image.png
To check whether or not this box is checked:
image.png
If you check that box, then a set of display states will “live” with their configurations. If you have multiple configs, then it will create 1 display state for each config. If that box is checked and you create multiple display states, all of those particular display states will only be available for that particular configuration.

If that box is NOT checked, then each display state can be used in any configuration.

Configurations tend to control “physical” portions of the CAD model (suppress, unsuppress, dimension changes, etc…) while display states control the graphical portion (hidden state, colors, etc…)

So you can do this one of 2 ways:

You can control your hidden states of each body by using configurations and make sure that the above checkbox is checked, then make sure to hide/unhide as needed and it will carry though the different configurations.

OR

You can control your hidden states of each body by NOT checking that box, and then no matter what config you select, your display states will control the hide/unhide.

The first way with linked display states (This is a GIF…click it):
LinkedDisplayStates.gif
This is the second way (another GIF…click it):
NonLinkedDisplayStates.gif

Yes I think inside Assembly there is distinction between a suppressed body & hidden. Hidden kept expectantly popping up on file open or refresh…?
I cant recall offhand. It was like whack-a-mole. I’ll do a legitimate test. There are many times when I need to deal with mirrored bodies so left version mated here, right version mated there.

Suppressed and Hidden are vastly different things.

Hidden means just that, those bodies or parts are still there, but they are only hidden from your view. Anything done with that file will include those parts/bodies, such as mass calculations, mates, etc.

Suppressed means the part/body is NOT included in the calculations. By definition they will also not be visible. If a part has mates then those mates will also be suppressed with the suppression of a part.

That is correct. The original part name is used, with an number added to the end.

if you start with MyPart.sldprt you get:
MyPart_1.stl
MyPart_2.stl
etc.

Yes, its starting to come back to me now. This particular part file was a Right mirror body about a plane to make a Left version of the part. So I had 2 bodies Left & Right in a common part file. I 'recall I could suppress the second body as expected & save that as a configuration. The issue came when I tried to suppress the first, because now the second had nothing to work from so to speak? I think that’s what led me down the path of hiding to begin with, which I agree is not optimal. Now maybe the better way is to bring the part file into an assembly as Left only & do the mirroring in assembly file environment because it has the capability to save out as a separate part? I’ll come up with an example & upload if this isn’t clear.

True - “the issue came when I tried to suppress the first.”

If you suppress the first instance of a mirror then the next cannot be unsuppressed. In this instance I would use delete/keep bodies with configurations to control which body is kept/deleted. The other option is to mirror the part to its own part file though I have had instances where that connection can get broken.

I don’t have much experience with display states as I tried to use display states the way configuration work, when I first started CAD work, and got frustrated. I know there are others who use display state to control parts so maybe I need to do some more research & tutorials.

Edited to add:
https://help.solidworks.com/2024/english/solidworks/sldworks/t_Mirror_Part.htm

Thanks for that SW reference link! Looks like they already have a more structured approach to what I was attempting to do doing manually. And even more powerful sub-options for the mirroring operation.

OK, I updated my post above with a much better explanation.

Please see the explanation and the GIF files attached.

UU

Thanks for your explanation!

With using display states as you did in your second GIF you would still get a body count of 4 because they are hidden not suppressed. So in your assembly your total body count would be 16 even though only 4 are displayed? Is that correct?

Yes I believe so.

Here is a way to do it with Suppressions and configurations (This is a GIF…click it):
Using DeleteBody.gif

That is another useful technique Dan. Thanks for video, shows the steps very clearly



Thanks. That is probably why we use suppression and/or delete/keep bodies & configurations. We use a macro to create flat patterns that ads the actual qty in the DXF file name upon creation of the DXF.

Thanks again.

I finally stumbled my through how to load & run a macro in SW. This one is so slick! Thanks for suggesting! I so want to learn macros now!

Q1) It kind of looks like even though I have renamed my bodies within SW from default names, the macro is ignoring these names & saving them using its concatenated filename + number coded format. I didn’t check yet but (see attached sketch) presumably the embedded number in STL export filename corresponds to order as they appear in SW tree from top to bottom? Reason I ask is when I sliced a larger solid into these bodies, the first element was second on the list or it was somehow re-arranged a bit different. Not quite sure why SW did it that way to begin with. Maybe the order of of how I chunked them up? Anyway, no biggy to just spot check them & rename the STL’s if its actually necessary

Q2) alternately, if I wanted to save them out as .STEP for example vs .STL, presumably I could find the lines of code where it defines .STL & replace that? When I normally do that via File Save as, there are 2 flavors of .STEP (AP203, AP214) but they appear to have the exact same *.step extension. Maybe this isn’t possible via the macro without some more knowledge or code?

Q3) From what I can tell, there is no linked association between an exported .STL file & the SW Part file it originated from? For example how a Drawing is associated to a Part. I assume the .STL’s are kind of standalone & I could move them anywhere to another folder using File Explorer without broken link repercussions like the Part/Drawing analogy?
SNAG-27-01-2025 11.36.58 PM.jpg
SNAG-27-01-2025 11.20.33 PM.jpg

Re Q2 maybe answering my own question. Should have checked the Macros area. I just saw mention of a STEP exporter macro, but I need to check if its the all bodies capability

https://forum.cadmunity.com/t/re-sharing-my-macros/3352/1
SNAG-27-01-2025 11.59.44 PM.jpg

The macro uses the parts file name. It does not look for the names of any features. I’m sure this could be done, but would require a bit of coding.
if you start with MyPart.sldprt you get:
MyPart_1.stl
MyPart_2.stl
etc.

To save as an STP file you only need to find this line of code:

sFileName = Strings.Left(swModel.GetPathName, Strings.Len(swModel.GetPathName) - 7) & “_” & CStr(i + 1) & “.STL”

Change the “.STL” to “.STP”

Or for extra credit, copy the line and make the extension change to the copy. Now you get both formats.




Think of the exported file as a PDF. Once solidworks generates the PDF, it exists in the world with no reference to the original drawing. If you change the drawing, you have to re-export it for the PDF to change. The STL/STP files are the same way.