Hide Table of Contents

Copy Component With Profile Center Mate Example (VBA)

This example shows how to:

  • create a new assembly.
  • add two components to the assembly.
  • create a profile center mate between the components.
  • copy a component with that mate.
'--------------------------------------------------------
' Preconditions:
' 1. Open a new SOLIDWORKS session.
' 2. Copy public_documents\samples\tutorial\api\block20.sldprt and
'    cylinder20.sldprt to c:\temp.
' 3. Verify that the specified assembly document template exists.
'
' Postconditions:
' 1. Opens both parts.
' 2. Creates a new assembly.
' 3. Inserts both parts into the new assembly.
' 4. Creates a profile center mate between the two components.
' 5. Copies a component and the profile center mate.
' 6. To verify steps 4 and 5:
'    * Examine the graphics area and FeatureManager design tree.
'    * Expand Mates in the FeatureManager design tree.
'---------------------------------------------------------
Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swPart1 As SldWorks.PartDoc
Dim swPart2 As SldWorks.PartDoc
Dim swModel As SldWorks.ModelDoc2
Dim swAssemblyDoc As SldWorks.AssemblyDoc
Dim swComponent1 As SldWorks.Component2
Dim swComponent2 As SldWorks.Component2
Dim swModelDocExt As SldWorks.ModelDocExtension
Dim swMate As SldWorks.Mate2
Dim status As Boolean
Dim errors As Long
Dim warnings As Long
Dim swComponentArray(0) As SldWorks.Component2
Dim repeatArray(0) As Boolean
Dim mateReferencesArray(0) As Object
Dim valueArray(0) As Double
Dim flipAlignmentArray(0) As Boolean
Dim flipDimensionArray(0) As Boolean
Dim lockRotationArray(0) As Boolean
Dim orientationArray(0) As Long
Sub main()
    Set swApp = Application.SldWorks    
    ' Open parts for new assembly
    Set swPart1 = swApp.OpenDoc6("C:\temp\block20.sldprt", swDocumentTypes_e.swDocPART, swOpenDocOptions_e.swOpenDocOptions_Silent, "", errors, warnings)
    Set swPart2 = swApp.OpenDoc6("C:\temp\cylinder20.sldprt", swDocumentTypes_e.swDocPART, swOpenDocOptions_e.swOpenDocOptions_Silent, "", errors, warnings)
    
    'Open new assembly document
    Set swModel = swApp.NewDocument("C:\ProgramData\SolidWorks\SOLIDWORKS 2016\templates\Assembly.asmdot", 0, 0, 0)
    swApp.ActivateDoc2 "Assem1", False, errors
    Set swAssemblyDoc = swModel    
    ' Add components to assembly document
    Set swComponent1 = swAssemblyDoc.AddComponent5("C:\temp\block20.sldprt", swAddComponentConfigOptions_e.swAddComponentConfigOptions_CurrentSelectedConfig, "", False, "", 5.22792702800426E-02, 6.58677995643489E-02, 0.102016044707081)
    Set swComponent2 = swAssemblyDoc.AddComponent5("C:\temp\cylinder20.sldprt", swAddComponentConfigOptions_e.swAddComponentConfigOptions_CurrentSelectedConfig, "", False, "", 0.177061497059185, -1.51244836160913E-03, 6.73233033157885E-02)
    swModel.ViewZoomtofit2    
    ' Add profile center mate
    Set swModelDocExt = swModel.Extension
    status = swModelDocExt.SelectByID2("", "FACE", 6.31695178495306E-02, 8.56797995642182E-02, 0.137370061843797, True, 1, Nothing, 0)
    status = swModelDocExt.SelectByID2("", "FACE", 0.141204290267694, 0.031253551638315, 8.43440006535161E-02, True, 1, Nothing, 0)
    Set swMate = swAssemblyDoc.AddMate5(swMateType_e.swMatePROFILECENTER, swMateAlign_e.swMateAlignALIGNED, True, 0.0762, 0.0254, 0.0254, 0.0254, 0.0254, 0, 0.5235987755983, 0.5235987755983, False, True, swMateWidthOptions_e.swMateWidth_Centered, errors)
    swModel.ClearSelection2 True
    
    ' Copy component with profile center mate
    Set swComponentArray(0) = swComponent2
    repeatArray(0) = True
    Set mateReferencesArray(0) = Nothing
    valueArray(0) = 0.05
    flipAlignmentArray(0) = True
    flipDimensionArray(0) = True
    lockRotationArray(0) = False
    orientationArray(0) = 0
    status = swAssemblyDoc.CopyWithMates2(swComponentArray, repeatArray, mateReferencesArray, valueArray, flipAlignmentArray, flipDimensionArray, lockRotationArray, orientationArray)
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

 
*Email:  
Subject:   Feedback on Help Topics
Page:   Copy Component With Profile Center Mate Example (VBA)
*Comment:  
*   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:

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) 2023 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.