Hide Table of Contents

Copy Components With Mates to Assembly Example (VBA)

This example shows how to copy components with mates in an assembly.


' Preconditions:

' 1. Open:

' <SolidWorks_install_dir>\samples\tutorial\driveworksxpress\mobile gantry.SLDASM.

' 2. Observe Leg<1> and Leg<2> in the FeatureManager design tree.


' Postconditions:

'    Leg<1> is replaced by a copy of Leg<2>:  observe Leg<3> in the mobile gantry.


' NOTE: Because this assembly is used in a SolidWorks online tutorial,

'       do not save any changes when you close it.


Option Explicit

Sub main()

    Dim SwApp                       As SldWorks.SldWorks

    Dim swModel                     As SldWorks.ModelDoc2

    Dim swModelDocExt               As SldWorks.ModelDocExtension

    Dim swSelMgr                    As SldWorks.SelectionMgr

    Dim blRepeat(2)                 As Boolean

    Dim SelectedComps               As Variant

    Dim RepeatOptions               As Variant

    Dim MateRefs                    As Variant

    Dim InpValues                   As Variant

    Dim FlipValues                  As Variant

    Dim swAssy                      As AssemblyDoc

    Dim swItem                      As Object

    Dim arrMateEntities(2)          As Object

    Dim arrCompsToCopy(0)           As Object

    Dim boolstatus                  As Boolean

    Dim dValues(2)                  As Double


    ' Disable Visual Basic error on implicit Query Interface

    On Error Resume Next


    Set SwApp = Application.SldWorks

    Set swModel = SwApp.ActiveDoc


    Set swAssy = swModel

    Set swSelMgr = swModel.SelectionManager

    Set swModelDocExt = swModel.Extension


    boolstatus = swModelDocExt.SelectByID2("Leg-1@mobile gantry", "COMPONENT", 0, 0, 0, False, 0, Nothing, 0)



    swModel.ClearSelection2 True

    boolstatus = swModelDocExt.SelectByID2("Leg-2@mobile gantry", "COMPONENT", 0, 0, 0, False, 0, Nothing, 0)

    Set swItem = swSelMgr.GetSelectedObject6(1, -1)

    Set arrCompsToCopy(0) = swItem


    swModel.ClearSelection2 True


    'Repeat all mates except the coincident mate with the Right End@Universal Beam-1; mate to Left End instead


    Set arrMateEntities(0) = Nothing

    Set arrMateEntities(1) = Nothing

    boolstatus = swModelDocExt.SelectByID2("Left End@Universal Beam-1@mobile gantry", "PLANE", 0, 0, 0, False, 0, Nothing, 0)

    Set swItem = swSelMgr.GetSelectedObject6(1, -1)

    Set arrMateEntities(2) = swItem


    swModel.ClearSelection2 True


    blRepeat(0) = True

    blRepeat(1) = True

    blRepeat(2) = False


    SelectedComps = arrCompsToCopy

    RepeatOptions = blRepeat

    MateRefs = arrMateEntities


    dValues(0) = 0#

    dValues(1) = 0#

    dValues(2) = 0#

    InpValues = dValues


    blRepeat(0) = False

    blRepeat(1) = False

    blRepeat(2) = False

    FlipValues = blRepeat


    swAssy.CopyWithMates SelectedComps, RepeatOptions, MateRefs, InpValues, FlipValues

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:   Copy Components With Mates to Assembly 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) 2014 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.