Check Interference Among Bodies (VBA)
This example shows how to check inteference among bodies in a part document.
'-------------------------
' Preconditions: Part document is open that
' contains
multiple solid body folders
' that
contain one or more solid bodies.
' Example
works best if one body in a
' a
solid body folder interferes with
' one
or more solid bodies in another solid body
' folder.
'
' Postconditions: None
'-------------------------
Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swPart As SldWorks.PartDoc
Dim swPartDoc As SldWorks.ModelDoc2
Dim swFeature As SldWorks.Feature
Dim swBodyFolder As SldWorks.BodyFolder
Dim BodyFolder As SldWorks.BodyFolder
Dim toolBodyList As Variant
Dim targetBodyList As Variant
Dim contLoop As Boolean
Dim areBodyClash As Boolean
Dim swModeler As SldWorks.Modeler
Dim vBodies As Variant
Dim vFaces1 As Variant
Dim vFaces2 As Variant
Dim sldBody1 As SldWorks.Body2
Dim sldBody2 As SldWorks.Body2
Dim sizeOfOut1, sizeOfOut2, sizeOfOut3 As Long
Sub GetVariantOfBody(swFeature As SldWorks.Feature, bodyList
As Variant)
Dim
tt As Variant
Set
swBodyFolder = swFeature.GetSpecificFeature2
Dim
count As Integer
count
= swBodyFolder.GetBodyCount
If
(count < 1) Then
MsgBox
("There are no bodies. Create a body in the folder.")
Else
bodyList
= swBodyFolder.GetBodies
End
If
End Sub
Sub main()
Set swApp = Application.SldWorks
Set swPartDoc = swApp.ActiveDoc
Set swFeature = swPartDoc.FirstFeature
Set swModeler = swApp.GetModeler
contLoop = True
While Not swFeature Is Nothing And contLoop = True
Dim
Name As String
Name
= swFeature.GetTypeName2
If
(Name = "SolidBodyFolder") Then
contLoop
= False
GetVariantOfBody
swFeature, targetBodyList
Set
swFeature = swFeature.GetFirstSubFeature
GetVariantOfBody
swFeature, toolBodyList
Dim
i As Integer
areBodyClash
= swModeler.CheckInterference3(targetBodyList,
toolBodyList, (swBodyInterference_ReturnInterferingObject + swBodyInterference_IncludeCoincidentFaces),
vFaces1, vFaces2, vBodies)
If
(areBodyClash) Then
sizeOfOut1
= UBound(vFaces1)
sizeOfOut2
= UBound(vFaces2)
sizeOfOut3
= UBound(vBodies)
Debug.Print
("Returning interfering body:")
Debug.Print
("No. of faces1")
Debug.Print
(sizeOfOut1 + 1)
Debug.Print
("No. of faces2")
Debug.Print
(sizeOfOut2 + 1)
Debug.Print
("No. of bodies")
Debug.Print
(sizeOfOut3 + 1)
End
If
Debug.Print
" "
areBodyClash
= swModeler.CheckInterference3(targetBodyList,
toolBodyList, (swBodyInterference_OptionDefault), vFaces1, vFaces2, vBodies)
If
(areBodyClash) Then
sizeOfOut1
= UBound(vFaces1)
sizeOfOut2
= UBound(vFaces2)
sizeOfOut3
= UBound(vBodies)
Debug.Print
("Returning interfering body: ")
Debug.Print
("No. of faces1")
Debug.Print
(sizeOfOut1 + 1)
Debug.Print
("No. of faces2")
Debug.Print
(sizeOfOut2 + 1)
Debug.Print
("No. of bodies")
Debug.Print
(sizeOfOut3 + 1)
End
If
Else
Set
swFeature = swFeature.GetNextFeature
End
If
Wend
End Sub