Hide Table of Contents

Check Interference Among Bodies Example (VBA)

This example shows how to check interference among bodies in a part document.

'----------------------------------------------------------------
' Preconditions:
' 1. Open public_documents\samples\tutorial\api\weldment_box3.sldprt.
' 2. Open the Immediate window.
'
' Postconditions:
' 1. Finds a body in a solid body folder that interferes with
'    one or more solid bodies in another solid body folder.
' 2. Examine the Immediate window and expand Cut list(31) > 
'    Sub-weldment(8).
'----------------------------------------------------------------
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
    Dim FeatType As String
    Dim FeatTypeName As String
    FeatType = swFeature.Name
    FeatTypeName = swFeature.GetTypeName2
    Debug.Print FeatType & " [" & FeatTypeName & "]"
    Set swBodyFolder = swFeature.GetSpecificFeature2
    Dim count As Long
    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 Long
    
            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 (faces touching each other):")
                Debug.Print ("    No. of faces that interfered from the first body with the second body")
                Debug.Print ("      " & sizeOfOut1 + 1)
                Debug.Print ("    No. of faces that interfered from the second body with the first body:")
                Debug.Print ("      " & sizeOfOut2 + 1)
                Debug.Print ("    No. of interfering 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 (faces not touching each other): ")
                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


Provide feedback on this topic

SOLIDWORKS welcomes your feedback concerning the presentation, accuracy, and thoroughness of the documentation. Use the form below to send your comments and suggestions about this topic directly to our documentation team. The documentation team cannot answer technical support questions. Click here for information about technical support.

* Required

 
*Email:  
Subject:   Feedback on Help Topics
Page:   Check Interference Among Bodies Example (VBA)
*Comment:  
*   I acknowledge I have read and I hereby accept the privacy policy under which my Personal Data will be used by Dassault Systèmes

Print Topic

Select the scope of content to print:

x

We have detected you are using a browser version older than Internet Explorer 7. For optimized display, we suggest upgrading your browser to Internet Explorer 7 or newer.

 Never show this message again
x

Web Help Content Version: API Help (English only) 2021 SP05

To disable Web help from within SOLIDWORKS and use local help instead, click Help > Use SOLIDWORKS Web Help.

To report problems encountered with the Web help interface and search, contact your local support representative. To provide feedback on individual help topics, use the “Feedback on this topic” link on the individual topic page.