Hide Table of Contents

Calculate Closest Distance Between Faces Example (VBA)

This example shows how to calculate the closest distance between two faces.




' Problem:

'       The SolidWorks user interface has the ability to measure the

'       distance between two selected faces. The corresponding

'       API for this functionality is ModelDoc2::ClosestDistance.



'       If any of the faces is cylindrical, then the measurement point is

'       taken from the axis of the cylinder. This is by design and the intended

'       behaviour. However, in some circumstances, this measurement may not

'       be appropriate. For example, the minimum amount of material between

'       two holes is normally measured between the cylindrical faces and not

'       between the axes.


'       This sample code show how to detect such situations and to calculate

'       the distance between faces.


' Preconditions:

'       (1) Part or assembly is open

'       (2) Assembly is fully resolved

'       (3) Two faces are selected

'       (4) Faces do not intersect


' Postconditions:

'       (1) 3D sketch is inserted with results from user interface Measure dialog

'       (2) 3D sketch is inserted showing closest distance between faces



Option Explicit

Sub CreateLine _

( _

    swApp As SldWorks.SldWorks, _

    swModel As SldWorks.ModelDoc2, _

    vPt1 As Variant, _

    vPt2 As Variant _


    Dim swSketchSeg                 As SldWorks.SketchSegment

    Dim bRet                        As Boolean

    swModel.SetAddToDB True

    swModel.Insert3DSketch2 False


    Set swSketchSeg = swModel.CreateLine2( _

                        vPt1(0), vPt1(1), vPt1(2), _

                        vPt2(0), vPt2(1), vPt2(2))

    swModel.SetAddToDB False

    swModel.Insert3DSketch2 True


    bRet = swModel.EditRebuild3

    Debug.Assert bRet

End Sub

Sub main()

    Dim swApp                       As SldWorks.SldWorks

    Dim swModel                     As SldWorks.ModelDoc2

    Dim swSelMgr                    As SldWorks.SelectionMgr

    Dim swFace1                     As SldWorks.face2

    Dim swFace2                     As SldWorks.face2

    Dim swSurf1                     As SldWorks.surface

    Dim swSurf2                     As SldWorks.surface

    Dim vPoint1                     As Variant

    Dim vPoint2                     As Variant

    Dim vClosestPt1                 As Variant

    Dim vClosestPt2                 As Variant

    Dim nDist                       As Double

    Set swApp = CreateObject("SldWorks.Application")

    Set swModel = swApp.ActiveDoc

    Set swSelMgr = swModel.SelectionManager

    Set swFace1 = swSelMgr.GetSelectedObject5(1)

    Set swFace2 = swSelMgr.GetSelectedObject5(2)

    Set swSurf1 = swFace1.GetSurface

    Set swSurf2 = swFace2.GetSurface


    'Get the result of the measure command - we need the points returned.

    nDist = swModel.ClosestDistance(swFace1, swFace2, vPoint1, vPoint2)

    Debug.Print "  ClosestDistance = " & nDist * 1000# & " mm"


    CreateLine swApp, swModel, vPoint1, vPoint2


    'Use the points returned by the measure command to get the nearest point actually on the faces in question.

    If swSurf1.IsCylinder Then

        'Measure has returned the center point, so use the point on the other surface

        vClosestPt1 = swFace1.GetClosestPointOn(vPoint2(0), vPoint2(1), vPoint2(2))


        'Probably on the surface, but just to be sure...

        vClosestPt1 = swFace1.GetClosestPointOn(vPoint1(0), vPoint1(1), vPoint1(2))

    End If


    If swSurf2.IsCylinder Then

        'Measure has returned the center point, so use the point on the other surface

        vClosestPt2 = swFace2.GetClosestPointOn(vPoint1(0), vPoint1(1), vPoint1(2))


        'Probably on the surface, but just to be sure...

        vClosestPt2 = swFace2.GetClosestPointOn(vPoint2(0), vPoint2(1), vPoint2(2))

    End If


    nDist = Sqr( _

                (vClosestPt1(0) - vClosestPt2(0)) ^ 2 + _

                (vClosestPt1(1) - vClosestPt2(1)) ^ 2 + _

                (vClosestPt1(2) - vClosestPt2(2)) ^ 2 _


    Debug.Print "  Distance        = " & nDist * 1000# & " mm"


    CreateLine swApp, swModel, vClosestPt1, vClosestPt2

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:   Calculate Closest Distance Between Faces 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) 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.