Get and Fill Gaps in Body Example (VBA)
This example shows how to get and fill the gaps in a body.
'---------------------------------------
'
' Preconditions: Model
document is open and contains
' at
least one body with one or more gaps.
'
' Postconditions: A Surface-Fill feature fills each gap.
'
'----------------------------------------
Option Explicit
Public Enum swBodyType_e
swSolidBody
= 0
swSheetBody
= 1
swWireBody
= 2
swMinimumBody
= 3
swGeneralBody
= 4
swEmptyBody
= 5
End Enum
Sub ProcessBody _
( _
swApp
As SldWorks.SldWorks, _
swModel
As SldWorks.ModelDoc2, _
swBody
As SldWorks.body2 _
)
Dim
nRetval1 As
Long
Dim
nRetval2 As
Long
Dim
swDiagnose As
SldWorks.DiagnoseResult
Dim
vCoEdgeArr As
Variant
Dim
vCoEdge As
Variant
Dim
swCoEdge As
SldWorks.CoEdge
Dim
swEdge As
SldWorks.Edge
Dim
swEnt As
SldWorks.entity
Dim
swSelMgr As
SldWorks.SelectionMgr
Dim
swSelData As
SldWorks.SelectData
Dim
swFeatMgr As
SldWorks.FeatureManager
Dim
swFeat As
SldWorks.feature
Dim
nNumGap As
Long
Dim
i As
Long
Dim
bRet As
Boolean
Set
swSelMgr = swModel.SelectionManager
Set
swSelData = swSelMgr.CreateSelectData
Set
swFeatMgr = swModel.FeatureManager
Set
swDiagnose = swBody.Diagnose
nRetval1
= swBody.Check2
nRetval2
= swBody.Check2
swSelData.mark = 1
nNumGap
= swDiagnose.GetGapsCount
For
i = 1 To nNumGap
swModel.ClearSelection2 True
vCoEdgeArr
= swDiagnose.GetCoEdgesAtGap(i
- 1)
For
Each vCoEdge In vCoEdgeArr
Set
swCoEdge = vCoEdge
Set
swEdge = swCoEdge.GetEdge
Set
swEnt = swEdge
bRet
= swEnt.Select4(True, swSelData):
Debug.Assert bRet
Next
vCoEdge
Set
swFeat = swFeatMgr.InsertFillSurface(3)
Next
i
Debug.Print
" Body
check1 =
" & nRetval1
Debug.Print
" Body
check2 (Number of faults) =
" & nRetval2
Debug.Print
" Number
of gaps =
" & nNumGap
End Sub
Sub main()
Dim
swApp As
SldWorks.SldWorks
Dim
swModel As
SldWorks.ModelDoc2
Dim
swPart As
SldWorks.PartDoc
Dim
vBodyArr As
Variant
Dim
vBody As
Variant
Dim
swBody As
SldWorks.body2
Dim
i As
Long
Dim
bRet As
Boolean
Set
swApp = Application.SldWorks
Set
swModel = swApp.ActiveDoc
Set
swPart = swModel
Debug.Print
"File = " & swModel.GetPathName
For
i = 0 To 5
vBodyArr
= swPart.GetBodies2(i, False)
If
Not IsEmpty(vBodyArr) Then
Debug.Print
" NumBody["
& i & "] = " & UBound(vBodyArr) + 1
For
Each vBody In vBodyArr
Set
swBody = vBody
ProcessBody
swApp, swModel, swBody
Next
vBody
End
If
Next
i
End Sub
'---------------------------------------