Check Interference using IAssemblyDoc::ToolsCheckInterference2 Example
(VBA)
This example shows how to check an assembly for interference using IAssemblyDoc::ToolsCheckInterference2.
NOTE: It is recommended that
you step through this macro in the debugger, especially in the sections
where the interfering faces and components are highlighted or selected.
'----------------------------------------------------------
'
' Preconditions:
' (1)
Assembly is open.
' (2)
Assembly contains at least two parts.
' (3)
At least two components or component entities are selected.
' (4)
Assembly is fully resolved.
'
' Postconditions:
' (1)
Any interfering faces are selected and the macro pauses.
' (2)
Any interfering components are selected and the macro pauses.
'
' NOTE: If components touch and coincident faces are not
considered
' to
be interfering, then IAssemblyDoc::ToolsCheckInterferenc2
' returns
an empty array of faces. However,
' IAssemblyDoc::ToolsCheckIntererence2
returns an array of
' components
for components that touch.
'
'----------------------------------------------------------
Option Explicit
Sub main()
Const
bCoincidentInterference As
Boolean = False
Dim
swApp As
SldWorks.SldWorks
Dim
swModel As
SldWorks.ModelDoc2
Dim
swAssy As
SldWorks.AssemblyDoc
Dim
swSelMgr As
SldWorks.SelectionMgr
Dim
CompArray() As
SldWorks.Component2
Dim
swSelData As
SldWorks.SelectData
Dim
vCompArray As
Variant
Dim
vIntCompArray As
Variant
Dim
vIntFaceArray As
Variant
Dim
swFace As
SldWorks.face2
Dim
swEnt As
SldWorks.entity
Dim
swComp As
SldWorks.Component2
Dim
i As
Long
Dim
nSelCount As
Long
Dim
bRet As
Boolean
Set
swApp = Application.SldWorks
Set
swModel = swApp.ActiveDoc
Set
swAssy = swModel
Set
swSelMgr = swModel.SelectionManager
Set
swSelData = SwSelMgr.CreateSelectData
Debug.Print
"File = " & swModel.GetPathName
nSelCount
= swSelMgr.GetSelectedObjectCount
ReDim
CompArray(nSelCount - 1)
For
i = 0 To (nSelCount - 1)
Set
CompArray(i) = swSelMgr.GetSelectedObjectsComponent2(i
+ 1)
Debug.Print
"Comp[" + i + "] = " + CompArray(i).Name2
Next
i
vCompArray
= CompArray
swAssy.ToolsCheckInterference2 nSelCount, (vCompArray),
bCoincidentInterference, vIntCompArray, vIntFaceArray
If
(IsEmpty(vIntCompArray) = True) And (IsEmpty(vIntFaceArray) = True) Then
Debug.Print
" No
contact"
Exit
Sub
End
If
If Not IsEmpty(vIntFaceArray) Then
Debug.Print
" "
& UBound(vIntFaceArray) + 1 & " Faces interfere!"
swModel.ClearSelection2 True
For
i = 0 To UBound(vIntFaceArray)
Set
swFace = vIntFaceArray(i)
Set
swEnt = swFace
Set
swComp = swEnt.GetComponent
Debug.Print
" CompFace["
& i & "] = " & swComp.Name2
bRet
= swEnt.Select4(True, swSelData):
Debug.Assert bRet
Next
i
'
Interfering faces selected
Stop
Else
Debug.Assert
Not IsEmpty(vIntCompArray)
Debug.Assert
False = bCoincidentInterference
Debug.Print
" Faces
touch but not checking for coincident interference!"
End
If
If
Not IsEmpty(vIntCompArray) Then
Debug.Print
" "
& UBound(vIntCompArray) + 1 & " Components interfere!"
swModel.ClearSelection2 True
For
i = 0 To UBound(vIntCompArray)
Set
swComp = vIntCompArray(i)
Debug.Print
" Comp["
& i & "] = " & swComp.Name2
bRet
= swComp.Select2(True, 0): Debug.Assert
bRet
Next
i
'
Interfering components selected
Stop
End
If
End Sub
'-----------------------------------------------