Hide Table of Contents

Detect Interferences Using a Transform Example (VBA)

This example shows how to specify a transform to detect interferences.

' Preconditions:
' 1. Open install_dir\samples\tutorial\api\InterferenceAssem.sldasm.
' 2. Uncomment one of the userOpt assignments.
' Postconditions:
' 1. When the macro stops, observe the position of the components in the
'    graphics area.
' 2. Press F5 to continue.
' 3. Inspect the Immediate Window for interfering components.
' NOTE: Because the model is used elsewhere,
' do not save changes when closing it.
' ---------------------------------------------------------------------------

Dim swApp As SldWorks.SldWorks
Dim swDoc As SldWorks.ModelDoc2
Dim swADoc As SldWorks.AssemblyDoc
Dim swSM As SldWorks.SelectionMgr
Dim swIDM As InterferenceDetectionMgr
Dim swComp() As SldWorks.Component2
Dim varComp As Variant
Dim numOfComp As Long
Dim swCompTrans() As SldWorks.MathTransform
Dim varCompTrans As Variant
Dim isInterfering As Boolean
Dim outIntComp As Variant
Dim outIntFaces As Variant
Dim gposX As Double
Dim gposY As Double
Dim I As Long

Option Explicit

' Uncomment one of the following lines to transform the components
    'Const userOpt = 0 'make components coincident
    Const userOpt = 1 'make components intersecting

Sub main()

    Set swApp = Application.SldWorks
    Set swDoc = swApp.ActiveDoc
    Set swADoc = swDoc
    Set swSM = swDoc.SelectionManager
    isInterfering = False
    numOfComp = swSM.GetSelectedObjectCount
    If (numOfComp) Then

        PrintComponentName varComp

        varCompTrans = swCompTrans

        Set swIDM = swADoc.InterferenceDetectionManager
        swIDM.TreatCoincidenceAsInterference = True
        isInterfering = swIDM.GetComponentsTransformInterference((varComp), (varCompTrans), outIntComp)

        Stop ' Observe transformation of objects for interference detection

        swIDM.Done ' Interference detection stopped
        varComp = Empty
        MsgBox ("Select components")
    End If

End Sub
Sub SetPosAsPerOption()
    If (userOpt = 0) Then
        gposX = 100 / 1000
        gposY = -50 / 1000
    ElseIf (userOpt = 1) Then
        gposX = 70 / 1000
        gposY = -50 / 1000
    End If
End Sub
Sub SelectAllComponent()
    Dim boolstatus As Boolean
    boolstatus = swDoc.Extension.SelectByID2("SquarePad_Red-1@InterferenceAssem", "COMPONENT", 0, 0, 0, False, 0, Nothing, 0)
    boolstatus = swDoc.Extension.SelectByID2("SquarePad_Green-1@InterferenceAssem", "COMPONENT", 0, 0, 0, True, 0, Nothing, 0)
    boolstatus = swDoc.Extension.SelectByID2("SquarePad_Blue-1@InterferenceAssem", "COMPONENT", 0, 0, 0, True, 0, Nothing, 0)
    boolstatus = swDoc.Extension.SelectByID2("SquarePad_Orange-1@InterferenceAssem", "COMPONENT", 0, 0, 0, True, 0, Nothing, 0)
    boolstatus = swDoc.Extension.SelectByID2("SquarePad_SkyBlue-1@InterferenceAssem", "COMPONENT", 0, 0, 0, True, 0, Nothing, 0)
    boolstatus = swDoc.Extension.SelectByID2("SquarePad_Yellow-1@InterferenceAssem", "COMPONENT", 0, 0, 0, True, 0, Nothing, 0)
    boolstatus = swDoc.Extension.SelectByID2("SquarePad_Pink-1@InterferenceAssem", "COMPONENT", 0, 0, 0, True, 0, Nothing, 0)
End Sub
Sub PrintInterferenceInfo()
    If (isInterfering) Then
        Debug.Print ("Interfering components:")
        Debug.Print ("")
        For I = 0 To UBound(outIntComp)
            Dim swOutComp As SldWorks.Component2
            Set swOutComp = outIntComp(I)
            Debug.Print (swOutComp.Name2)
            swOutComp.Select4 True, Nothing, False
            Set swOutComp = Nothing
        Next I
        Debug.Print ("No interference")
    End If
End Sub
Sub getSelectedComp()
    ReDim swComp(numOfComp - 1)
    For I = 1 To numOfComp
        Set swComp(I - 1) = swSM.GetSelectedObject6(I, -1)
    Next I
    varComp = swComp
End Sub
Sub PrintComponentName(varComp As Variant)
    If Not (IsEmpty(varComp)) Then
        If userOpt = 0 Then
            Debug.Print ("Detect interferences between coincident components:")
            Debug.Print ("")
            Debug.Print ("Detect interferences between intersecting components:")
            Debug.Print ("")
        End If

        For I = LBound(varComp) To UBound(varComp)
            Dim swComp As SldWorks.Component2
            Set swComp = varComp(I)

            Debug.Print ("Component name : " & swComp.Name2)
            Set swComp = Nothing
        Next I
    End If
    Debug.Print ("")
End Sub
Sub CreateSameTransMatAsCompCount()
    ReDim swCompTrans(numOfComp - 1)
    If Not (IsEmpty(varComp)) Then
        For I = LBound(varComp) To UBound(varComp)
            Dim swComp As SldWorks.Component2
            Set swComp = varComp(I)
            Set swCompTrans(I) = swComp.Transform2
            Set swComp = Nothing
        Next I
    End If
End Sub
Sub ChangePosOfCompAsPerOption()
    ChangeRefPosOfComp gposX, gposY
End Sub

Sub ChangePosOfComp(compNum As Long, pos As Double)
    Dim varData As Variant
    varData = swCompTrans(compNum).ArrayData
    varData(9) = pos
    swCompTrans(compNum).ArrayData = varData
End Sub

Sub ChangeRefPosOfComp(posX As Double, posY As Double)
    If Not (IsEmpty(varComp)) Then
        For I = LBound(varComp) To UBound(varComp)
            If (I > 0) Then
                Dim varData As Variant
                varData = swCompTrans(I).ArrayData
                varData(9) = posX * I
                varData(10) = posY * I
                swCompTrans(I).ArrayData = varData
                If (userOpt = 2) Then
                    swComp(I).Transform2 = swCompTrans(I)
                    swDoc.ForceRebuild3 True
                End If
            End If
        Next I
    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:   Detect Interferences Using a Transform Example (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) 2015 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.