Solidworks VBA Macro - Add Angle Mate

20 minute read

This article has "Beginner ➡ Intermediate" level Macro program.
If you are following my articles then will not be an issue for you.
In this article we did not use 🚀 UserForm for taking inputs, instead we use 🚀 Input Box for value input and 🚀 Message Box to notify user.
I hope you will also like this type of tutorials.
Thank you for reading.

Objective

In this article we create and understand VBA macro of adding Angle Mate in Assembly document of SOLIDWORKS CAD Software.

This method is most updated method, so use this method if you want to add Angle Mate quickly.

Results We Can Get

After running our macro, we successfully add Angle Mate a Component in an Assembly as a result.

Below image shows the result we get.

assembly-angle-mate

We add Angle Mate in following steps in general.

  1. loop through each component in current assembly.
  2. Loop through each face.
  3. Get desired face from user confirmation.
  4. Select required faces for mate.
  5. Get Angle from user.
  6. Add Angle Mate.
  7. Final Work

To get the correct result please follow the steps correctly.

Macro Video

Below 🎬 video shows Adding Angle Mate from SOLIDWORKS VBA Macros.


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

I have explained each and every line in this article.

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

VBA Macro

Below is the VBA macro for Angle 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 Solidworks Body
Dim swBody As SldWorks.Body2

' Variable for Solidworks Mate Feature
Dim swMateFeature As SldWorks.Feature

' Variable for Solidworks Face
Dim swFace As SldWorks.Face2

' Variable for Solidworks Face List
Dim vFaces(1 To 2) As SldWorks.Face2

' Variable for Solidworks Mate Feature Data
Dim swMateData As SldWorks.MateFeatureData

' Variable for Solidworks Angle Mate Feature Data
Dim swAngleMateData As SldWorks.AngleMateFeatureData

' Program to add Angle 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
    
  ' Create Assembly Mate Data
  Set swMateData = swAssembly.CreateMateData(swMateType_e.swMateANGLE)
  
  ' Set Angle Mate data to Assembly Mate Data
  Set swAngleMateData = swMateData
  
  ' Set Mate Alignment
  swAngleMateData.MateAlignment = SwConst.swMateAlign_e.swMateAlignANTI_ALIGNED
  
  ' Set Mate Flip Dimension
  swAngleMateData.FlipDimension = True
  
  ' Variable for List of elements
  Dim vArray As Variant
  
  ' Get Components list in opened assembly
  vArray = swAssembly.GetComponents(True)
  
  ' Variable for component Index
  Dim componentIndex As Integer
  
  ' Loop Components List
  For componentIndex = 0 To UBound(vArray)
    
    ' Set Solidworks Component variable
    Set swComponent = vArray(componentIndex)
    
    ' Calling this function
    SelectFace swComponent, componentIndex
    
  Next
  
  ' Variable for Solidworks Select Data
  Dim swSelData As SldWorks.SelectData
  
  ' Set value of Solidworks Select Data variable
  Set swSelData = swDoc.SelectionManager.CreateSelectData
  
  ' Set Mark of Solidworks Select Data
  swSelData.Mark = 1
  
  ' Boolean variable
  Dim boolStatus As Boolean
  
  ' Select faces for Concentric mate
  boolStatus = swDoc.Extension.MultiSelect2(vFaces, False, swSelData)

  ' Check if faces are selected
  If boolStatus = False Then
    MsgBox "Failed to select faces."
    swDoc.ClearSelection2 True
    Exit Sub
  End If
  
  ' Set Angle Mate Entities to Mate
  swAngleMateData.EntitiesToMate = vFaces
  
  ' Local variables used as Conversion Factors
  Dim LengthConversionFactor As Double
  Dim 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
  
  ' Variable to hold user input
  Dim response As String
  
  ' Getting Angle from user.
  response = InputBox("Please Enter [Angle]:")
  
  ' This will handle empty value or cancel case
  If Len(response) = 0 Then
    MsgBox "Empty or no value. Please try again."
    swDoc.ClearSelection2 True
    Exit Sub
  End If
  
  ' This will handle case for Non-numeric values
  If IsNumeric(response) = False Then
    MsgBox "Entered value is Non-numeric. Please try again."
    swDoc.ClearSelection2 True
    Exit Sub
  End If
  
  ' Variable for Angle
  Dim angle As Double
  
  ' Set Angle
  angle = CDbl(response) * AngleConversionFactor
  
  ' This will handle case for 0 Distance
  If angle = 0 Then
    MsgBox "Entered value must be greater than 0. Please try again."
    swDoc.ClearSelection2 True
    Exit Sub
  End If
  
  ' Set Angle
  swAngleMateData.angle = angle
  
  ' Add Angle Mate
  Set swMateFeature = swAssembly.CreateMate(swAngleMateData)

  ' Check if Mate is added or not
  If swMateFeature Is Nothing Then
    MsgBox "Failed to Add Mate."
    swDoc.ClearSelection2 True
    Exit Sub
  End If
  
  ' Clear all selection
  swDoc.ClearSelection2 True
  
  ' Zoom view to fit
  swDoc.ViewZoomtofit2
  
  ' Rebuild assembly
  swDoc.ForceRebuild3 True
  
End Sub

' Function to select required face for Angle mate
Function SelectFace(component As SldWorks.Component2, componentIndex As Integer)
  
  ' Get body of current component
  Set swBody = component.GetBody
  
  ' Get First face
  Set swFace = swBody.GetFirstFace
  
  ' Variable to store Message Box result
  Dim resp As VbMsgBoxResult
  
  ' Loop through all Faces
  Do While Not swFace Is Nothing
    
    ' Clear selection
    swDoc.ClearSelection2 True
    
    ' Select current face
    swFace.Select True
    
    ' Ask user to confirm this selection
    resp = MsgBox("Is this correct Face?", vbYesNo, "Select Face")
    
    ' Check if user response if Yes
    If resp = vbYes Then
      ' Add current face to List of Faces
      Set vFaces(componentIndex + 1) = swFace
      Exit Function
    End If
    
    ' Get next face
    Set swFace = swFace.GetNextFace
    
  Loop
  
End Function

Prerequisite

There are some prerequisite for this article.

  • Knowledge of VBA programming language is ❗required.

  • We use existing parts in Assembly document.

  • One component is fully constraint and other component is Float as shown in below image.

prerequisite

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

Steps To Follow

This VBA macro can be divided into following sections:

  1. Create and Initialize required variables
  2. Get Components and Loop through them
  3. Get desired face
  4. Select faces
  5. Get unit conversion factors
  6. Get angle and Validations
  7. Add angular Mate
  8. Final Work

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 and Initialize required 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 Solidworks Body
Dim swBody As SldWorks.Body2
  • Purpose: In above line, we create a variable for Solidworks Component.
  • Variable Name: swBody
  • Type: SldWorks.Body2.
  • Reference: Please visit 🚀 online SOLIDWORKS API Help.
' Variable for Solidworks Mate Feature
Dim swMateFeature As SldWorks.Feature
  • Purpose: In above line, we create a variable for Solidworks Mate Feature.
  • Variable Name: swMateFeature
  • Type: SldWorks.Feature.
  • Reference: Please visit 🚀 online SOLIDWORKS API Help.
' Variable for Solidworks Face
Dim swFace As SldWorks.Face2
  • Purpose: In above line, we create a variable for Solidworks Face.
  • Variable Name: swFace
  • Type: SldWorks.Face2.
  • Reference: Please visit 🚀 online SOLIDWORKS API Help.
' Variable for Solidworks Face List
Dim vFaces(1 To 2) As SldWorks.Face2
  • Purpose: Define 🚀 Array of Face2.
  • Variable Name: vFaces
  • Type: Face2
  • Length of Array: (1 To 2)
  • Reference: Please visit
' Variable for Solidworks Mate Feature Data
Dim swMateData As SldWorks.MateFeatureData
  • Purpose: In above line, we create a variable for Solidworks Mate Feature Data.
  • Variable Name: swMateData
  • Type: SldWorks.MateFeatureData.
  • Reference: Please visit 🚀 online SOLIDWORKS API Help.
' Variable for Solidworks Angle Mate Feature Data
Dim swAngleMateData As SldWorks.AngleMateFeatureData
  • Purpose: In above line, we create a variable for Solidworks Angle Mate Feature Data.
  • Variable Name: swAngleMateData
  • Type: SldWorks.AngleMateFeatureData.
  • Reference: Please visit 🚀 online SOLIDWORKS API Help.

These all are our global variables.

They are SOLIDWORKS API Objects.

' Program to add Angle Mate
Sub main()

End Sub
  • In above line, we create main Program to add Angle Mate in assembly.
  • 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.
' 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.
' Create Assembly Mate Data
Set swMateData = swAssembly.CreateMateData(swMateType_e.swMateANGLE)
  • In above line, we set the value of swMateData variable.
  • We set value by CreateMateData() method.
  • This CreateMateData() method is part of swAssembly variable.
  • This CreateMateData() method takes following parameter.
    • Type: Type of mate to create as defined in swMateType_e in below table.

      Member Description
      swMateANGLE 6
      swMateCAMFOLLOWER 9
      swMateCOINCIDENT 0
      swMateCONCENTRIC 1
      swMateCOORDINATE 20
      swMateDISTANCE 5
      swMateGEAR 10
      swMateHINGE 22
      swMateLINEARCOUPLER 18
      swMateLOCK 16
      swMateLOCKTOSKETCH 12
      swMateMAGNETIC 25
      swMateMAXMATES 14
      swMatePARALLEL 3
      swMatePATH 15
      swMatePERPENDICULAR 2
      swMatePROFILECENTER 24
      swMateRACKPINION 13
      swMateSCREW 17
      swMateSLIDER 23
      swMateSLOT 21
      swMateSYMMETRIC 8
      swMateTANGENT 4
      swMateUNIVERSALJOINT 19
      swMateUNKNOWN 7
      swMateWIDTH 11
  • Since we want to add Angle mate, hence we use value swMateANGLE as type.
  • Reference: Please visit 🚀 online SOLIDWORKS API Help.
' Set Angle Mate data to Assembly Mate Data
Set swAngleMateData = swMateData
  • In above line, we set Angle Mate data to previously created Assembly Mate data.
  • We set value of variable swAngleMateData to variable swMateData.
  • Reference: Please visit 🚀 online SOLIDWORKS API Help.
' Set Mate Alignment
swAngleMateData.MateAlignment = SwConst.swMateAlign_e.swMateAlignANTI_ALIGNED
  • In above line, set the Mate Alignment => “ANTI ALIGNED”.
' Set Mate Flip Dimension
swAngleMateData.FlipDimension = True
  • In above line, we set value of Mate Flip Dimension => True.

Get Components and Loop through them

In this section, we Get Components and Loop through them.

' Variable for List of elements
Dim vArray As Variant
  • Purpose: In above line, we create a variable for List of elements.
  • Variable Name: vArray
  • Type: Variant
' Get Components list in opened assembly
vArray = swAssembly.GetComponents(True)
  • In above line, we set the value of vArray variable.
  • We set value by GetComponents method of swAssembly variable.
' Variable for component Index
Dim componentIndex As Integer
  • In above line, we create a variable for component Index as a counter.
  • Variable Name: componentIndex
  • Type: Integer
' Loop Components List
For componentIndex = 0 To UBound(vArray)
  
Next
  • In above line, we create a For loop.
  • This loops start from i = 0 to maximum number of items in vArray we select.
' Set Solidworks Component variable
Set swComponent = vArray(componentIndex)
  • In above line, we set value of swComponent variable.
  • This value is current value of array vArray.

Get desired face

In this section we get desired face for Angular mate.

' Calling this function
SelectFace swComponent, componentIndex
  • In above line, we call a function.
  • Function Name: SelectFace
  • Function Parameters: They are
    • swComponent
    • componentIndex
' Function to select required face for Tangent mate
Function SelectFace(component As SldWorks.Component2, componentIndex As Integer)
  
End Function
  • In above line, we create a Function to select required face for Tangent mate.
  • This is a Function procedure which has name of SelectFace.
  • This procedure hold all the statements (instructions) for select required face for Tangent mate.
  • Reference: Detailed information 🚀 VBA Sub and Function Procedures article of this website.

This function has 2 parameters as follows:

  • component As SldWorks.Component2
  • componentIndex As Integer
' Get body of current component
Set swBody = component.GetBody
  • In above line, we set value of swBody variable.
  • Value of swBody variable is set by GetBody method of component variable.
' Get First face
Set swFace = swBody.GetFirstFace
  • In above line, we set value of swFace variable.
  • Value of swFace variable is set by GetFirstFace, which is part of swBody variable.
' Variable to store Message Box result
Dim resp As VbMsgBoxResult
  • Purpose: In above line, we create a variable to store Message Box result.
  • Variable Name: resp
  • Type: VbMsgBoxResult
' Loop through all Faces
Do While Not swFace Is Nothing
  
Loop
  • In above line, we loop through all Faces.
' Clear all selection
swDoc.ClearSelection2 True
  • In above line, we clear all selection.
  • For this we use ClearSelection2 method which is part of SOLIDWORKS Document variable i.e swDoc variable.
' Select current face
swFace.Select True
  • In above line, we select current face.

This function is not available in 🚀 Solidworks Face object.
Since 🚀 Solidworks Face object is also an 🚀 Solidworks Entity object, we can use methods from 🚀 Solidworks Entity object.

' Ask user to confirm this selection
resp = MsgBox("Is this correct Face?", vbYesNo, "Select Face")
  • In above line, we ask user to confirm this selection.
  • We do this in following steps.
    1. Show a Message Box to user as shown below.

    user-confirmation

    1. Store user response in resp variable.
' Check if user response if Yes
If resp = vbYes Then

End If
  • In above code block, we check if user response if Yes.
  • We use 🚀 IF statement for checking.
  • Condition: resp = vbYes
' Add current face to List of Faces
Set vFaces(componentIndex + 1) = swFace
Exit Function
  • When user response if Yes, then above code execute.
  • In above line, 1st we add current face to List of Faces.
  • 2nd, we exit this SelectFace function.
' Get next face
Set swFace = swFace.GetNextFace
  • In above line, we set the value of swFace variable.
  • This is done by GetNextFace method of swFace variable.

Select Faces

In this section, we Select Faces.

' Variable for Solidworks Select Data
Dim swSelData As SldWorks.SelectData
  • Purpose: In above line, we create a variable for Solidworks Select Data.
  • Variable Name: swSelData
  • Type: SldWorks.SelectData
  • Reference: Please visit 🚀 online SOLIDWORKS API Help.
' Set value of Solidworks Select Data variable
Set swSelData = swDoc.SelectionManager.CreateSelectData
  • In above line, we set value of swSelData variable.
  • Value of swSelData variable is set by CreateSelectData, which is part of SelectionManager. This SelectionManager is part of swDoc variable.
' Set Mark of Solidworks Select Data
swSelData.Mark = 1
  • In above line, we set Mark of swSelData variable to 1.
' Boolean variable
Dim boolStatus As Boolean
  • Purpose: In above line, we create a Boolean variable.
  • Variable Name: boolStatus
  • Type: Boolean
' Select faces for Angular mate
boolStatus = swDoc.Extension.MultiSelect2(vFaces, False, swSelData)
  • In above line, we select Faces by MultiSelect2 method.
  • This MultiSelect2 method takes following parameter.
    • Objects: Array of selectable objects.
    • AppendFlag: True to append the objects to the selection list, False to replace the current selection list with these objects.
    • Data: 🚀 ISelectData object, Nothing, or null
' Check if faces are selected
If boolStatus = False Then
  MsgBox "Failed to select faces."
  swDoc.ClearSelection2 True
  Exit Sub
End If
  • In above code block, we check if we successfully selected the faces.
  • We use 🚀 IF statement for checking.
  • Condition: boolStatus = False
  • When this condition is True,
    • We show and 🚀 message window to user.
    • Message: Failed to select faces.
    • Then we stop our macro here.

Get unit Conversion factors

Now we need to get unit Conversion factors as shown in below code.

'-----------------------Unit Conversion Factors----------------------------------------
  
' Local variables used as Conversion Factors
Dim LengthConversionFactor As Double
Dim 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

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

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.

Get Angle And Validations

In this section, we get get the required Angle from user and apply some validation on Input Angle.

' Variable to hold user input
Dim response As String
  • In above line, we create a variable as a counter.
  • Variable Name: response
  • Type: String
' Getting Angle from user.
response = InputBox("Please Enter [Angle]:")
  • In above line of code we are doing 2 steps in one line.

    Those 2 steps are explained below.

    • Step 1 - Getting Angle from user.

    Below image shows the message for Angle to the user.

    message-to-select-angle

    • Step 2 - Assigned input value to response variable.
' This will handle empty value or cancel case
If Len(response) = 0 Then
  MsgBox "Empty or no value. Please try again."
  Exit Sub
End If
  • In above code block, we check the length of input value.
  • This check will handle case for empty value or cancel operation case.
  • We use 🚀 IF statement for checking.
  • Condition: Len(response) = 0
    • Len() is pre-build VBA function which check the length of a object.
    • In above cases, we will get 0 value.
  • When this condition is True,
    • We show and 🚀 message window to user.
    • Message: Empty or no value. Please try again.
    • Then we stop our macro here.
' This will handle case for Non-numeric values
If IsNumeric(response) = False Then
  MsgBox "Entered value is Non-numeric. Please try again."
  Exit Sub
End If
  • In above code block, we check if the input value is Non-numeric.
  • This check will handle case for Non-numeric values.
  • We use 🚀 IF statement for checking.
  • Condition: IsNumeric(response) = False
    • IsNumeric() is pre-build VBA function which check if passing object is Numeric or not.
    • In above cases, we will get False value.
  • When this condition is True,
    • We show and 🚀 message window to user.
    • Message: Entered value is Non-numeric. Please try again.
    • Then we stop our macro here.
' Variable for angle
Dim angle As Double
  • In above line, we create a variable to store Rib Thickness.
  • Variable Name: angle
  • Type: Double
angle = CDbl(response) * AngleConversionFactor
  • In above line of code we are doing 3 steps in one line.

    Those 3 steps are explained below.

    • Step 1 - Converting angle from user to Double type.
    • Step 2 - Updating converted distance as per document unit system.
    • Step 3 - Assigned input value to angle variable.
' This will handle case for 0 distance
If distance = 0 Then
  MsgBox "Entered value must be greater than 0. Please try again."
  Exit Sub
End If
  • In above code block, we check if the input value is zero (0).
  • This check will handle case for 0 thickness.
  • We use 🚀 IF statement for checking.
  • Condition: RibThickness = 0
  • When this condition is True,
    • We show and 🚀 message window to user.
    • Message: Entered value must be greater than 0. Please try again.
    • Then we stop our macro here.

Add Angle Mate

In this section, we add Angle Mate.

' Set Angle
swAngleMateData.angle = angle
  • In above line, Mate Angle of swAngleMateData => angle variable.
' Add Angle Mate
Set swMateFeature = swAssembly.CreateMate(swAngleMateData)
  • In above line, we set the value of variable swMateFeature by CreateMate() method.

  • This CreateMate() method takes following parameters as explained:

    • MateData - Mate-specific object:

      Mate-specific object
      IAngleMateFeatureData
      ICamFollowerMateFeatureData
      ICoincidentMateFeatureData
      IConcentricMateFeatureData
      IDistanceMateFeatureData
      IGearMateFeatureData
      IHingeMateFeatureData
      ILinearCouplerMateFeatureData
      ILockMateFeatureData
      IParallelMateFeatureData
      IPerpendicularMateFeatureData
      IProfileCenterMateFeatureData
      IRackPinionMateFeatureData
      IScrewMateFeatureData
      ISlotMateFeatureData
      ISymmetricMateFeatureData
      ITangentMateFeatureData
      IUniversalJointMateFeatureData
      IWidthMateFeatureData
  • Return Value : This CreateMate() method return 👉 Mate2 data object.

  • In our code, I have used following values:

    Parameter Name Value Used
    MateData swAngleMateData

Reference: For more details about

' Check if Mate is added or not
If swMateFeature Is Nothing Then
  MsgBox "Failed to Add Mate."
  swDoc.ClearSelection2 True
  Exit Sub
End If
  • In above code block, we check if we successfully added Angle Mate or not.
  • We use 🚀 IF statement for checking.
  • Condition: swMateFeature Is Nothing
  • When this condition is True,
    • We show and 🚀 message window to user.
    • Message: *Failed to Add Mate.
    • After that we clear the selection.
    • Then we stop our macro here.

Now we run the macro and after running macro we get Angle Mate as shown in below image.

assembly-angle-mate

Final work

In this section, after adding Angle Mate, we have to do some cleaning work so that we can use this macro frequently.

' Clear all selection
swDoc.ClearSelection2 True
  • In above line, we clear all selection.
  • For this we use ClearSelection2 method which is part of SOLIDWORKS Document variable i.e swDoc variable.
' View zoom to fit
swDoc.ViewZoomtofit2
  • In above line, we make our view zoom to fit the model.
  • For this we use ViewZoomtofit2 method which is part of SOLIDWORKS Document variable i.e swDoc variable.
' Rebuild assembly
swDoc.ForceRebuild3 True
  • In above line, we Rebuild assembly.
  • For this we use ForceRebuild3 method which is part of SOLIDWORKS Document variable i.e swDoc variable.

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 Angle 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: