Create Body From Selected Faces Example
This example shows how to create a body from one or more selected faces
using:
'------------------------------------------
'
' Problem:
' This
example shows how to use some of
' the
geometry and topolgy APIs to
' construct
a temporary API body from a
' set
of selected faces and then create a solid
' body
feature from this temporary body.
'
' Preconditions:
' (1)
Part is open.
' (2)
At least one face is selected.
'
' Postconditions: If the selected faces can be knitted
into a solid,
' then
a new part is created and the solid (from the
' selected
faces) is inserted as an imported solid
' body
feature.
'
'-------------------------------------------
Option Explicit
Public Enum swBodyType_e
swAllBodies
= -1
swSolidBody
= 0
swSheetBody
= 1
swWireBody
= 2
swMinimumBody
= 3
swGeneralBody
= 4
swEmptyBody
= 5
End Enum
Public Enum swCreateFeatureBodyOpts_e
swCreateFeatureBodyCheck
= &H1
swCreateFeatureBodySimplify
= &H2
End Enum
Sub main()
Dim
swApp As
SldWorks.SldWorks
Dim
swModel As
SldWorks.ModelDoc2
Dim
swPart As
SldWorks.PartDoc
Dim
swNewPart As
SldWorks.PartDoc
Dim
swModeller As
SldWorks.Modeler
Dim
swSelMgr As
SldWorks.SelectionMgr
Dim
swSelFace() As
SldWorks.face2
Dim
vFaceList As
Variant
Dim
swBody As
SldWorks.body2
Dim
swNewBody As
SldWorks.body2
Dim
swFeat As
SldWorks.feature
Dim
nSelCount As
Long
Dim
i As
Long
Dim
bRet As
Boolean
Set
swApp = CreateObject("SldWorks.Application")
Set
swModel = swApp.ActiveDoc
Set
swPart = swModel
Set
swSelMgr = swModel.SelectionManager
Set
swModeller = swApp.GetModeler
nSelCount
= swSelMgr.GetSelectedObjectCount
ReDim
swSelFace(nSelCount - 1)
For
i = 1 To nSelCount
Set
swSelFace(i - 1) = swSelMgr.GetSelectedObject5(i)
Next
vFaceList
= swSelFace
Set
swBody = swPart.CreateNewBody
Set
swNewBody = swBody.CreateBodyFromFaces(nSelCount,
(vFaceList))
If
swNewBody Is Nothing Then
Debug.Print
"Failed to form solid body from selected faces."
Exit
Sub
End
If
Debug.Assert
swSolidBody = swNewBody.GetType
Set
swNewPart = swApp.NewPart
'
Force creation of a solid body feature
Set
swFeat = swNewPart.CreateFeatureFromBody3(swNewBody,
False, _
swCreateFeatureBodyCheck
+ swCreateFeatureBodySimplify)
'
Must be able to create a solid body feature from a solid body
Debug.Assert
Not swFeat Is Nothing
End Sub
'------------------------------------------