Files
Keep largest body and delete the rest
Code
Module: mMain
'Demonstrates how to delete all but the largest body from a part.
'Preconditions: Part is open.
'Results: If more than one body exists, a Delete Body feature is added _
to the part, deleting all but the largest bodies.
'Notes:
'If there are 2+ largest bodies, all of them are kept.
'Written by Keith Rice
'CADSharp LLC
'www.cadsharp.com
Const ROUND_DECIMALS As Integer = 10
Sub main()
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swPart As SldWorks.PartDoc
Dim swBody As SldWorks.Body2
Dim vMassProps As Variant
Dim vBodies As Variant
Dim dblVolLargest As Double
Dim i As Integer
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swPart = swModel
swModel.ClearSelection2 True
vBodies = swPart.GetBodies2(swBodyType_e.swAllBodies, False)
If IsEmpty(vBodies) Then Exit Sub
If UBound(vBodies) = 0 Then Exit Sub
dblVolLargest = 0
For i = 0 To UBound(vBodies)
Set swBody = vBodies(i)
vMassProps = swBody.GetMassProperties(1) 'arbitrary density
If vMassProps(3) > dblVolLargest Then dblVolLargest = vMassProps(3)
Next i
For i = 0 To UBound(vBodies)
Set swBody = vBodies(i)
vMassProps = swBody.GetMassProperties(1)
If Round(vMassProps(3), ROUND_DECIMALS) < Round(dblVolLargest, ROUND_DECIMALS) Then swBody.Select2 True, Nothing
Next i
swModel.FeatureManager.InsertDeleteBody
End Sub