Hide Table of Contents

Add Distance Mates Example (VBA)

This example shows how to add distance mate to an assembly.

' Preconditions: Open install_dir\samples\tutorial\api\assem20.sldasm.
' Postconditions:
' 1. Selects Front Plane of the block20<1> component. Click OK to
'    to close the message box.
' 2. Selects Front Plane of the cylinder20<1> component. Click OK
'    to close the message box.
' 3. Adds a distance mate using the Front Plane of each component.
' 4. Displays the number of mates created. Click OK to close the
'    message box.
' 5. Expand Mates in the FeatureManager design tree and examine
'    the graphics area to verify steps 3 and 4.
' NOTE: Because this assembly is used elsewhere, do not save changes.
Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swAssy As SldWorks.AssemblyDoc
Dim swModel As SldWorks.ModelDoc2
Dim swModelDocExt As SldWorks.ModelDocExtension
Dim longstatus As Long
Dim mateFeature As Object
Dim mateSelMark As Long
Dim numberOfMatesCreated As Long
Dim status As Boolean
Dim strMessage As String
Sub main()
    Set swApp = Application.SldWorks
    Set swAssy = swApp.ActiveDoc
    Set swModel = swAssy
    Set swModelDocExt = swModel.Extension
    mateSelMark = 1
    numberOfMatesCreated = 0    
    ' Insert distance mate
    swModel.ClearSelection2 True
    status = swModelDocExt.SelectByID2("Front Plane@block20-1@assem20", "PLANE", 0, 0, 0, True, mateSelMark, Nothing, swSelectOption_e.swSelectOptionDefault)
    If status Then MsgBox "Front Plane@block20-1@assem20 selected!"
    status = swModelDocExt.SelectByID2("Front Plane@cylinder20-1@assem20", "PLANE", 0, 0, 0, True, mateSelMark, Nothing, swSelectOption_e.swSelectOptionDefault)
    If status Then MsgBox "Front Plane@cylinder20-1@assem20 selected!"
    Set mateFeature = swAssy.AddMate5(swMateType_e.swMateDISTANCE, swMateAlign_e.swMateAlignALIGNED, True, 0.254, 0.254, 0.254, 0.0254, 0.0254, 0, 0.5235987755983, 0.5235987755983, False, False, 0, longstatus)
    If mateFeature Is Nothing Then
       MsgBox "Distance mate failed! "
       numberOfMatesCreated = numberOfMatesCreated + 1
    End If    
    swModel.ClearSelection2 True
    strMessage = "Number of mates created = "
    strMessage = strMessage + CStr(numberOfMatesCreated)
    MsgBox strMessage    
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:   Add Distance Mates 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) 2016 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.