solid bodies saved out as unique parts

Use this space to ask how to do whatever you're trying to use SolidWorks to do.
Petertha
Posts: 62
Joined: Sun May 26, 2024 3:34 am
Answers: 0
x 14

solid bodies saved out as unique parts

Unread post by Petertha »

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?
Attachments
EDT-12-01-2025 3.59.17 PM.jpg
User avatar
SPerman
Posts: 2156
Joined: Wed Mar 17, 2021 4:24 pm
Answers: 14
x 2363
x 1977
Contact:

Re: solid bodies saved out as unique parts

Unread post by SPerman »

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.

Code: Select all

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
-
I may not have gone where I intended to go, but I think I have ended up where I needed to be. -Douglas Adams
DLZ_SWX_User
Posts: 79
Joined: Mon Dec 20, 2021 1:40 pm
Answers: 0
Location: Thumb Area of Michigan, USA
x 274
x 41

Re: solid bodies saved out as unique parts

Unread post by DLZ_SWX_User »

Petertha wrote: Sun Jan 12, 2025 6:11 pm
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?
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.
Petertha
Posts: 62
Joined: Sun May 26, 2024 3:34 am
Answers: 0
x 14

Re: solid bodies saved out as unique parts

Unread post by Petertha »

SPerman wrote: Mon Jan 13, 2025 6:49 am 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.
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?
User avatar
DanPihlaja
Posts: 906
Joined: Thu Mar 11, 2021 9:33 am
Answers: 26
Location: Traverse City, MI
x 827
x 1023

Re: solid bodies saved out as unique parts

Unread post by DanPihlaja »

Petertha wrote: Sun Jan 12, 2025 6:11 pm

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?
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
image.png (11.91 KiB) Viewed 1708 times
To check whether or not this box is checked:
image.png
image.png (17.37 KiB) Viewed 1708 times
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
-Dan Pihlaja
Solidworks 2022 SP4

2 Corinthians 13:14
Petertha
Posts: 62
Joined: Sun May 26, 2024 3:34 am
Answers: 0
x 14

Re: solid bodies saved out as unique parts

Unread post by Petertha »

DLZ_SWX_User wrote: Mon Jan 13, 2025 7:01 am 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.
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.
User avatar
DennisD
Posts: 727
Joined: Fri Mar 12, 2021 10:04 am
Answers: 1
Location: Near Jacksonville, FL
x 1065
x 1490

Re: solid bodies saved out as unique parts

Unread post by DennisD »

Petertha wrote: Mon Jan 13, 2025 3:47 pm 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.
Brick walls are there for a reason. The brick walls aren't there to keep us out. The brick walls are there to show us how badly we want things.
- - -Randy Pausch
User avatar
SPerman
Posts: 2156
Joined: Wed Mar 17, 2021 4:24 pm
Answers: 14
x 2363
x 1977
Contact:

Re: solid bodies saved out as unique parts

Unread post by SPerman »

Petertha wrote: Mon Jan 13, 2025 3:43 pm 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?
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.
-
I may not have gone where I intended to go, but I think I have ended up where I needed to be. -Douglas Adams
Petertha
Posts: 62
Joined: Sun May 26, 2024 3:34 am
Answers: 0
x 14

Re: solid bodies saved out as unique parts

Unread post by Petertha »

DennisD wrote: Mon Jan 13, 2025 4:02 pm Suppressed and Hidden are vastly different things.
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.
DLZ_SWX_User
Posts: 79
Joined: Mon Dec 20, 2021 1:40 pm
Answers: 0
Location: Thumb Area of Michigan, USA
x 274
x 41

Re: solid bodies saved out as unique parts

Unread post by DLZ_SWX_User »

Petertha wrote: Tue Jan 14, 2025 10:29 am 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/englis ... r_Part.htm
Petertha
Posts: 62
Joined: Sun May 26, 2024 3:34 am
Answers: 0
x 14

Re: solid bodies saved out as unique parts

Unread post by Petertha »

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.
User avatar
DanPihlaja
Posts: 906
Joined: Thu Mar 11, 2021 9:33 am
Answers: 26
Location: Traverse City, MI
x 827
x 1023

Re: solid bodies saved out as unique parts

Unread post by DanPihlaja »

DLZ_SWX_User wrote: Wed Jan 15, 2025 6:57 am

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.

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

Please see the explanation and the GIF files attached.

UU
-Dan Pihlaja
Solidworks 2022 SP4

2 Corinthians 13:14
DLZ_SWX_User
Posts: 79
Joined: Mon Dec 20, 2021 1:40 pm
Answers: 0
Location: Thumb Area of Michigan, USA
x 274
x 41

Re: solid bodies saved out as unique parts

Unread post by DLZ_SWX_User »

DanPihlaja wrote: Wed Jan 15, 2025 2:35 pm 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?
User avatar
DanPihlaja
Posts: 906
Joined: Thu Mar 11, 2021 9:33 am
Answers: 26
Location: Traverse City, MI
x 827
x 1023

Re: solid bodies saved out as unique parts

Unread post by DanPihlaja »

DLZ_SWX_User wrote: Thu Jan 16, 2025 7:21 am 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
-Dan Pihlaja
Solidworks 2022 SP4

2 Corinthians 13:14
Petertha
Posts: 62
Joined: Sun May 26, 2024 3:34 am
Answers: 0
x 14

Re: solid bodies saved out as unique parts

Unread post by Petertha »

That is another useful technique Dan. Thanks for video, shows the steps very clearly
DLZ_SWX_User
Posts: 79
Joined: Mon Dec 20, 2021 1:40 pm
Answers: 0
Location: Thumb Area of Michigan, USA
x 274
x 41

Re: solid bodies saved out as unique parts

Unread post by DLZ_SWX_User »

DLZ_SWX_User wrote: Thu Jan 16, 2025 7:21 am 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?
DanPihlaja wrote: Thu Jan 16, 2025 8:50 am Yes I believe so.
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.
Petertha
Posts: 62
Joined: Sun May 26, 2024 3:34 am
Answers: 0
x 14

Re: solid bodies saved out as unique parts

Unread post by Petertha »

SPerman wrote: Mon Jan 13, 2025 6:49 am 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.
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?
Attachments
SNAG-27-01-2025 11.36.58 PM.jpg
SNAG-27-01-2025 11.36.58 PM.jpg (5.55 KiB) Viewed 1246 times
SNAG-27-01-2025 11.20.33 PM.jpg
SNAG-27-01-2025 11.15.52 PM.jpg
Petertha
Posts: 62
Joined: Sun May 26, 2024 3:34 am
Answers: 0
x 14

Re: solid bodies saved out as unique parts

Unread post by Petertha »

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

viewtopic.php?t=3745&view=unread#unread
Attachments
SNAG-27-01-2025 11.59.44 PM.jpg
SNAG-27-01-2025 11.59.44 PM.jpg (10.6 KiB) Viewed 1243 times
User avatar
SPerman
Posts: 2156
Joined: Wed Mar 17, 2021 4:24 pm
Answers: 14
x 2363
x 1977
Contact:

Re: solid bodies saved out as unique parts

Unread post by SPerman »

Petertha wrote: Tue Jan 28, 2025 1:42 am 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
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.
Petertha wrote: Tue Jan 28, 2025 1:42 am 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?
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.



Petertha wrote: Tue Jan 28, 2025 1:42 am 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?
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.
-
I may not have gone where I intended to go, but I think I have ended up where I needed to be. -Douglas Adams
Post Reply