Solidworks VBA Macro - Copy With Mate

12 minute read

Objective

In this article, we understand โ€œhow toโ€ Copy With Mate in Assembly document from VBA macro.

This is most updated method of Copy Part/Assembly with Mate in an assembly document.

Results We Can Get

Below image shows the result we get.

assembly-copy-with-mates

We Copy With Mate in simple manners.

There are no extra steps required.

To get the correct result, please follow the steps correctly.

Macro Video

Below ๐ŸŽฌ video shows how to Copy With Mate from SOLIDWORKS VBA Macros.


Above video is just for visualization and there is no explanation.

I have explained every line in this article.

It is advisable to watch video, since it helps you to better understand the process.

VBA Macro

Below is the VBA macro for Copy With Mate.

Option Explicit

' Variable for Solidworks Application
Dim swApp As SldWorks.SldWorks

' Variable for Solidworks document
Dim swDoc As SldWorks.ModelDoc2

' Variable for Solidworks Assembly
Dim swAssembly As SldWorks.AssemblyDoc

' Variable for Solidworks Component
Dim swComponent As SldWorks.Component2

' Variable for Component array
Dim swComponentArray(0) As SldWorks.Component2

' Variable to define if repeat the mate or not
Dim repeatArray(2) As Boolean

' Set the value of mate if is accept the value
Dim valueArray(2) As Double

' Boolean variable to check if Opereation is succeed or not
Dim status As Boolean

' Main program for Copy with Mate
Sub main()

  ' Set Solidworks Application variable to current application
  Set swApp = Application.SldWorks
  
  ' Set Solidworks document variable to currently opened document
  Set swDoc = swApp.ActiveDoc
  
  ' Check if Solidworks document is opened or not
  If swDoc Is Nothing Then
    MsgBox "Solidworks document is not opened."
    Exit Sub
  End If
  
  ' Set Solidworks Assembly document
  Set swAssembly = swDoc
  
  ' Get the selected face and set it to the Solidworks Face variable
  Set swComponent = swDoc.SelectionManager.GetSelectedObject6(1, -1)
  
  ' If there are error
  If swComponent Is Nothing Then
    ' Inform user and exit function.
    MsgBox "Failed to get selected part."
    Exit Sub
  End If
  
  ' Local variables used as Conversion Factors
  Dim LengthConversionFactor As Double
  Dim AngleConversionFactor As Double
  
  ' Get updated unit conversion factors
  Call GetUnitConversionFactors(LengthConversionFactor, AngleConversionFactor)
  
  ' Copy component with profile center mate
  Set swComponentArray(0) = swComponent
  
  ' Variable for loop
  Dim i As Integer
  
  ' Loop for setting values
  For i = 0 To 2
    
    ' Repeat the mate
    repeatArray(i) = True
    
    ' Set the value of mate
    valueArray(i) = 10 * LengthConversionFactor
    
  Next
  
  ' Variables for Copy operation
  Dim mateReferencesArray(2) As Object
  Dim flipAlignmentArray(2) As Boolean
  Dim flipDimensionArray(2) As Boolean
  Dim lockRotationArray(2) As Boolean
  Dim orientationArray(2) As Long
  
  ' Create Copy with mate
  status = swAssembly.CopyWithMates2(swComponentArray, repeatArray, mateReferencesArray, valueArray, flipAlignmentArray, flipDimensionArray, lockRotationArray, orientationArray)
  
End Sub

' Function to update Unit conversion factors
Function GetUnitConversionFactors(ByRef LengthConversionFactor As Double, ByRef AngleConversionFactor As Double)
  ' Use a Select Case, to get the length of active Unit and set the different factors
  Select Case swDoc.GetUnits(0)       ' GetUnits function gives us, active unit
    
    Case swMETER    ' If length is in Meter
      LengthConversionFactor = 1
      AngleConversionFactor = 1
    
    Case swMM       ' If length is in MM
      LengthConversionFactor = 1 / 1000
      AngleConversionFactor = 1 * 0.01745329
    
    Case swCM       ' If length is in CM
      LengthConversionFactor = 1 / 100
      AngleConversionFactor = 1 * 0.01745329
    
    Case swINCHES   ' If length is in INCHES
      LengthConversionFactor = 1 * 0.0254
      AngleConversionFactor = 1 * 0.01745329
    
    Case swFEET     ' If length is in FEET
      LengthConversionFactor = 1 * (0.0254 * 12)
      AngleConversionFactor = 1 * 0.01745329
    
    Case swFEETINCHES     ' If length is in FEET & INCHES
      LengthConversionFactor = 1 * 0.0254  ' For length we use sama as Inch
      AngleConversionFactor = 1 * 0.01745329
    
    Case swANGSTROM        ' If length is in ANGSTROM
      LengthConversionFactor = 1 / 10000000000#
      AngleConversionFactor = 1 * 0.01745329
    
    Case swNANOMETER       ' If length is in NANOMETER
      LengthConversionFactor = 1 / 1000000000
      AngleConversionFactor = 1 * 0.01745329
    
    Case swMICRON       ' If length is in MICRON
      LengthConversionFactor = 1 / 1000000
      AngleConversionFactor = 1 * 0.01745329
  End Select
End Function

Prerequisite

There are some prerequisites for this article.

  • Knowledge of VBA programming language is โ—required.
  • We use existing parts in Assembly document.
  • Both components are fully constraint as shown in below image.
  • We select the part which we want to copy.

prerequisite

We will apply checks in this article, so the code we write, should be error free mostly.

Steps To Follow

This VBA macro can be divided into following sections:

  1. Create Global Variables
  2. Initialize Global Variables
  3. Get unit conversion factors
  4. Copy With Mate

Every section with each line is explained below.

I also give some links (see icon ๐Ÿš€) so that you can go through them if there are anything I explained in previous articles.

Create Global Variables

In this section, we create global variables.

Option Explicit
' Variable for Solidworks application
Dim swApp As SldWorks.SldWorks
  • Purpose: In above line, we create a variable for Solidworks application.
  • Variable Name: swApp
  • Type: SldWorks.SldWorks
  • Reference: Please visit ๐Ÿš€ online SOLIDWORKS API Help.
' Variable for Solidworks document
Dim swDoc As SldWorks.ModelDoc2
  • Purpose: In above line, we create a variable for Solidworks document.
  • Variable Name: swDoc
  • Type: SldWorks.ModelDoc2
  • Reference: Please visit ๐Ÿš€ online SOLIDWORKS API Help.
' Variable for Solidworks Assembly
Dim swAssembly As SldWorks.AssemblyDoc
  • Purpose: In above line, we create a variable for Solidworks Assembly.
  • Variable Name: swAssembly
  • Type: SldWorks.AssemblyDoc
  • Reference: Please visit ๐Ÿš€ online SOLIDWORKS API Help.
' Variable for Solidworks Component
Dim swComponent As SldWorks.Component2
  • Purpose: In above line, we create a variable for Solidworks Component.
  • Variable Name: swComponent
  • Type: SldWorks.Component2.
  • Reference: Please visit ๐Ÿš€ online SOLIDWORKS API Help.
' Variable for Component array
Dim swComponentArray(0) As SldWorks.Component2
  • Purpose: In above line, we create an array for Solidworks Component.
  • Variable Name: swComponentArray(0)
  • Type: SldWorks.Component2.
  • Reference: Please visit ๐Ÿš€ online SOLIDWORKS API Help.
' Variable to define if repeat the mate or not
Dim repeatArray(2) As Boolean
  • Purpose: In above line, we create an array to define if repeat the mate or not.
  • Variable Name: repeatArray(2)
  • Type: Boolean.
' Set the value of mate if is accept the value
Dim valueArray(2) As Double
  • Purpose: In above line, we create an array to set the value of mate if is accept the value.
  • Variable Name: valueArray(2)
  • Type: Double.
' Boolean variable to check if Opereation is succeed or not
Dim status As Boolean
  • Purpose: In above line, we create an variable to check if Opereation is succeed or not.
  • Variable Name: status
  • Type: Boolean.

These all are our global variables.

They are SOLIDWORKS API Objects.

' Main program for Copy with Mate
Sub main()

End Sub
  • In above line, we create main program for Copy with Mate.
  • This is a Sub procedure which has name of main.
  • This procedure hold all the statements (instructions) we give to computer.
  • Reference: Detailed information ๐Ÿš€ VBA Sub and Function Procedures article of this website.

Initialize Global Variables

In this section, we initialize global variables.

' Set Solidworks Application variable to current application
Set swApp = Application.SldWorks
  • In above line, we set value of swApp variable.
  • This value is currently opened Solidworks application.
' Set Solidworks document variable to currently opened document
Set swDoc = swApp.ActiveDoc
  • In above line, we set value of swDoc variable.
  • This value is currently opened part document.
' Check if Solidworks document is opened or not
If swDoc Is Nothing Then
  MsgBox ("Solidworks document is not opened.")
  Exit Sub
End If
  • In above code block, we check if we successfully set the value of swDoc variable.
  • We use ๐Ÿš€ IF statement for checking.
  • Condition: swDoc Is Nothing
  • When this condition is True,
    • We show and ๐Ÿš€ message window to user.
    • Message: SOLIDWORKS document is not opened.
    • Then we stop our macro here.
' Set Solidworks Assembly document
Set swAssembly = swDoc
  • In above line, we set value of swAssembly variable.
  • This value is swDoc variable.
' Get the selected face and set it to the Solidworks Face variable
Set swComponent = swDoc.SelectionManager.GetSelectedObject6(1, -1)
  • In above line, we set value of swComponent variable.
  • We set the value from GetSelectedObject6 method.
  • This GetSelectedObject6 method is part of SelectionManager object.
  • This SelectionManager object is part of swDoc object.
' If there are error
If swComponent Is Nothing Then
  ' Inform user and exit function.
  MsgBox "Failed to get selected part."
  Exit Sub
End If
  • In above code block, we check if we successfully set the value of swComponent variable.
  • We use ๐Ÿš€ IF statement for checking.
  • Condition: swComponent Is Nothing
  • When this condition is True,
    • We show and ๐Ÿš€ message window to user.
    • Message: Failed to get selected part.
    • Then we stop our macro here.

Get Unit Conversion Factors

In this section we get Unit conversion factors for new distance.

' Local variables used as Conversion Factors
Dim LengthConversionFactor As Double
Dim AngleConversionFactor As Double
  • Purpose: In above line, we create a variables to store Unit Conversion Factors.
  • Variable Name: LengthConversionFactor and AngleConversionFactor
  • Type: Double
' Get updated unit conversion factors
Call GetUnitConversionFactors(LengthConversionFactor, AngleConversionFactor)
  • In above line, we call a function.
  • Function Name: GetUnitConversionFactors
  • Function Parameters: They are
    • LengthConversionFactor
    • AngleConversionFactor
' Function to update Unit conversion factors
Function GetUnitConversionFactors(ByRef LengthConversionFactor As Double, ByRef AngleConversionFactor As Double)
  
End Function
  • In above line, we create a Function to update Unit conversion factors for editing Distance mate.
  • Function Name: GetUnitConversionFactors
  • Purpose: Hold all the statements (instructions) for getting Unit conversion factors.
  • Reference: Detailed information ๐Ÿš€ VBA Sub and Function Procedures article of this website.

This function has 2 parameters as follows:

  • ByRef LengthConversionFactor As Double
  • ByRef AngleConversionFactor As Double

  • Above parameters, passed as ByRef.
  • These parameters get the values in this function.
  • But we are not using them in this function, but we use them in main procedure.
  • ByRef provides us the ability to use the updated variables in main procedure.
  • Reference: For more details please visit ๐Ÿš€ this link.
' Use a Select Case, to get the length of active Unit and set the different factors
Select Case swDoc.GetUnits(0)       ' GetUnits function gives us, active unit
  
  Case swMETER    ' If length is in Meter
    LengthConversionFactor = 1
    AngleConversionFactor = 1
  
  Case swMM       ' If length is in MM
    LengthConversionFactor = 1 / 1000
    AngleConversionFactor = 1 * 0.01745329
  
  Case swCM       ' If length is in CM
    LengthConversionFactor = 1 / 100
    AngleConversionFactor = 1 * 0.01745329
  
  Case swINCHES   ' If length is in INCHES
    LengthConversionFactor = 1 * 0.0254
    AngleConversionFactor = 1 * 0.01745329
  
  Case swFEET     ' If length is in FEET
    LengthConversionFactor = 1 * (0.0254 * 12)
    AngleConversionFactor = 1 * 0.01745329
  
  Case swFEETINCHES     ' If length is in FEET & INCHES
    LengthConversionFactor = 1 * 0.0254  ' For length we use sama as Inch
    AngleConversionFactor = 1 * 0.01745329
  
  Case swANGSTROM        ' If length is in ANGSTROM
    LengthConversionFactor = 1 / 10000000000#
    AngleConversionFactor = 1 * 0.01745329
  
  Case swNANOMETER       ' If length is in NANOMETER
    LengthConversionFactor = 1 / 1000000000
    AngleConversionFactor = 1 * 0.01745329
  
  Case swMICRON       ' If length is in MICRON
    LengthConversionFactor = 1 / 1000000
    AngleConversionFactor = 1 * 0.01745329
End Select

I have already explained about this in previous ๐Ÿš€Solidworks Macro - Fix Unit Issue article in this website.

Please visit ๐Ÿš€Solidworks Macro - Fix Unit Issue article for more details.

Copy With Mate

In this section, we perform Copy With Mate action.

' Copy component with profile center mate
Set swComponentArray(0) = swComponent
  • In above line, we set the value of swComponentArray variable.
  • Since we want to copy only 1 part.
  • Hence we set only 1 element of array.
  • This value is swComponent variable.
' Variable for loop
Dim i As Integer
  • Purpose: In above line, we create a variable for Loop.
  • Variable Name: i
  • Type: Integer.
' Loop for setting values
For i = 0 To 2

Next
  • In above line, we create an Loop for setting values.
  • We set the values of variables required for Copy mate.
' Repeat the mate
repeatArray(i) = True
  • In above line, we set the value of repeatArray array variable.
  • This array tell that we want to repeat the mates in new component or not.
  • Setting the value to True confirms repeat mate.
' Set the value of mate
valueArray(i) = 10 * LengthConversionFactor
  • In above line, we set the value of valueArray array variable.
  • This array sets the value for distance or angle of each mate.
' Variables for Copy operation
Dim mateReferencesArray(2) As Object
Dim flipAlignmentArray(2) As Boolean
Dim flipDimensionArray(2) As Boolean
Dim lockRotationArray(2) As Boolean
Dim orientationArray(2) As Long
  • In above code, we create some array variables.
  • These arrray variables are of different types i.e. Object, Boolean, Long.
  • We did not set any value for these arrays. We just need them for copy operation.
' Create Copy with mate
status = swAssembly.CopyWithMates2(swComponentArray, repeatArray, mateReferencesArray, valueArray, flipAlignmentArray, flipDimensionArray, lockRotationArray, orientationArray)
  • In above code, we Copy With Mate into assemly.
  • For this, we use CopyWithMates2 method.
  • This CopyWithMates2 method is part of swAssembly variable.
  • This method takes 1 parameter.
    • ComponentsToCopy: Array of components to copy.
    • Repeat: Array of boolean values; each value indicates whether to use the existing mate reference for the corresponding component to copy; if True, copies the existing mate reference; if False, uses the corresponding entry in the NewEntityToMateTo array for the new mate reference.
    • NewEnityToMateTo: Array of new mate references that map to the Repeat array; if an entry in the Repeat array is False, then the corresponding entry in this array is the new entity with which to mate the component to copy.
    • Values: Array of distance or angle values for the mate references; specify distance in meters and angle in radians; valid for distance, angle, and profile center mates only.
    • FlipAlignment: Array of booleans that map to the NewEntityToMateTo array; each value indicates the corresponding mateโ€™s alignment; True to flip alignment, False to not.
    • FlipDimension: Array of booleans that map to the Values array; each value indicates the corresponding mateโ€™s distance; True for a positive distance dimension, False for a negative distance dimension; valid for distance, angle, and profile center mates only.
    • LockRotation: Array of booleans that map to the NewEntityToMateTo array; True to prevent the components from rotating, False to allow the components to rotate; valid for concentric and profile center mates only.
    • Orientation: Array of longs or integers that map to the Values array; each long or integer indicates the number of clicks in the user interface with which to orient the mate; a positive value indicates to orient the mate clockwise, a negative value indicates to orient the mate counterclockwise; valid for profile center mates only.
  • Return Value : True if calling this method succeeded, False if it failed.

  • In our code, I have used following values:

    Parameter Name Value Used
    ComponentsToCopy swComponentArray
    Repeat repeatArray
    NewEnityToMateTo mateReferencesArray
    Values valueArray
    FlipAlignment flipAlignmentArray
    FlipDimension flipDimensionArray
    LockRotation lockRotationArray
    Orientation orientationArray
  • Reference: For more details please visit ๐Ÿš€ online SOLIDWORKS API Help.

Now we run the macro and after running macro we get a new part/assembly from Copy with Mate operation as shown in below image.

assembly-copy-with-mates

This is it !!!

I hope my efforts will helpful to someone! ๐Ÿ˜Š

If you found anything to add or update, please let me know on my e-mail ๐Ÿ“ง.

Hope this post helps you to Copy With Mate with SOLIDWORKS VBA Macros.

For more such tutorials on SOLIDWORKS VBA Macro, do come to this website after sometime.

If you like the post then please share it with your friends also. ๐Ÿ™๐Ÿป

Do let me know by you like this post or not!

Till then, Happy learning!!!

Updated: