> Calculate Closest Distance Between Faces Example (VBA)
Welcome
Getting Started
SolidWorks API Help
FeatureWorks API Help
SolidWorks Document Manager API Help
eDrawings API Help
SolidWorks Routing API Help
SolidWorks Simulation API Help
SolidWorks Utilities API Help
SolidWorks Workgroup PDM API Help
Hide Table of Contents Show 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))

    Else

        '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))

    Else

        '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

'--------------------------------------------------



Related SolidWorks Forum Content

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:   Calculate Closest Distance Between Faces Example (VBA)
*Comment:  
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) 2012 SP05

The search functionality within the web help is in a beta test phase and you may experience periodic delays or interruptions in its performance. These are the normal and ordinary features of a beta test and shall not under any circumstances give rise to any liability on the part of DS SolidWorks or its licensors. The topics within the Web-based help are not beta topics; they document API Help (English only) 2012 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.