Hide Table of Contents

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.")


        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


        Set swFeature = swFeature.GetNextFeature

    End If



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

Subject:   Feedback on Help Topics
Page:   Check Interference Among Bodies (VBA)
*   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:


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

Web Help Content Version: API Help (English only) 2010 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.