Solidworks VBA Macro - Toggle Mate Alignment

In this article, we understand β€œhow to” Toggle Mate Alignment in Assembly document from VBA macro.

You can use this method to Toggle Mate Alignment of any Mate.

Results We Can Get

Below image shows the result we get.

assembly-toggle-mate-alignment
Figure 1: Final Result

We Toggle Mate Alignment in simple manners.

There are no extra steps required.

Important

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



Macro Video

Below 🎬 video shows how to Toggle Mate Alignment from SOLIDWORKS VBA Macros.

How to Toggle Mate Alignment in Assembly

Important

Please note that there are no explanations in the video.
Explanation of each step and why we write code this way is provided in this post.

VBA Macro

Below is the VBA macro for Toggle Mate Alignment.

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 Mate Feature
Dim swMateFeature As SldWorks.Feature

' Variable for Solidworks Mate Feature data
Dim swMateFeatureData As SldWorks.MateFeatureData

' Variable for Solidworks Distance Mate feature data
Dim swDistanceMateFeatureData As SldWorks.DistanceMateFeatureData

' Program to Toggle distance Mate Alignment
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 mate feature
  Set swMateFeature = swDoc.Extension.GetLastFeatureAdded
  
  ' Check if successfully Get mate
  If swMateFeature Is Nothing Then
    MsgBox "Failed to Get Mate."
    swDoc.ClearSelection2 True
    Exit Sub
  End If
  
  ' Select the mate
  swMateFeature.Select True
  
  ' Start editing mate feature
  swDoc.FeatEdit
  
  ' Get feature definition of mate
  Set swMateFeatureData = swMateFeature.GetDefinition
  
  ' Set Distance mate feature data
  Set swDistanceMateFeatureData = swMateFeatureData
  
  ' Print Mate Alignment
  Debug.Print swDistanceMateFeatureData.MateAlignment
  
  ' Toggle distance Mate Alignment
  If swDistanceMateFeatureData.MateAlignment = 1 Then
    swDistanceMateFeatureData.MateAlignment = 2
  Else
    swDistanceMateFeatureData.MateAlignment = 1
  End If
  
  ' Print updated Mate Alignment
  Debug.Print swDistanceMateFeatureData.MateAlignment
  
  ' Modify the definition
  swMateFeature.ModifyDefinition swDistanceMateFeatureData, swDoc, Nothing
  
  ' Clear all selection
  swDoc.ClearSelection2 True
  
  ' Rebuild assembly
  swDoc.ForceRebuild3 True
  
End Sub

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.

prerequisite
Figure 2: Prerequisite
Error Prevention

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 Feature Data
  4. Toggle Mate Alignment

Every section with each line is explained below.

Additional Resources

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 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 Mate Feature data
Dim swMateFeatureData As SldWorks.MateFeatureData
  • Purpose: In above line, we create a variable for Solidworks Mate Feature Data.
  • Variable Name: swMateFeatureData
  • Type: SldWorks.MateFeatureData.
  • Reference: Please visit πŸš€ online SOLIDWORKS API Help .
' Variable for Solidworks Distance Mate feature data
Dim swDistanceMateFeatureData As SldWorks.DistanceMateFeatureData
  • Purpose: In above line, we create a variable for Solidworks Symmetric Mate Feature Data.
  • Variable Name: swDistanceMateFeatureData
  • Type: SldWorks.DistanceMateFeatureData .
  • Reference: Please visit πŸš€ online SOLIDWORKS API Help .

These all are our global variables.

They are SOLIDWORKS API Objects.

' Program to Toggle distance Mate Alignment
Sub main()

End Sub
  • In above line, we create main Program to Toggle Distance 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.

β€”### 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 Feature Data

In this section, we Get Feature Data.

' Get mate feature
Set swMateFeature = swDoc.Extension.GetLastFeatureAdded
  • In above line, we set the value of variable swMateFeature by GetLastFeatureAdded method.
  • This GetLastFeatureAdded method is part of Extension object.
  • This Extension object is then part of swDoc object.
  • GetLastFeatureAdded method gives us last added mate.
' Check if successfully Get mate
If swMateFeature Is Nothing Then
  MsgBox "Failed to Get Mate."
  swDoc.ClearSelection2 True
  Exit Sub
End If
  • In above code block, we check if we successfully get Last 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 Get Mate.
    • After that we clear the selection.
    • Then we stop our macro here.
' Select the mate
swMateFeature.Select True
  • In above line we select the mate by Select method.
  • This Select method take either True or False.
    • True: Appends the feature to the current selection list.
    • False: Replaces the current selection list.
' Start editing mate feature
swDoc.FeatEdit
  • In above line we start editing mate feature by FeatEdit method.
  • This FeatEdit method puts the current feature into edit mode.
' Get feature definition of mate
Set swMateFeatureData = swMateFeature.GetDefinition
  • In above line, we get the feature definition of select distance mate.
  • We get this feature definition by GetDefinition method.
  • This GetDefinition method is part of SldWorks.Feature object.
' Set Distance mate feature data
Set swDistanceMateFeatureData = swMateFeatureData
  • In above line, we set Distance mate feature data to feature definition of distance mate.
  • We set value of variable swDistanceMateFeatureData to variable swMateFeatureData.
  • Reference: Please visit πŸš€ online SOLIDWORKS API Help .

Toggle Mate Alignment

In this section, we Toggle Mate Alignment.

' Print Mate Alignment
Debug.Print swDistanceMateFeatureData.MateAlignment
  • In above code block, we print selected mate’s current Alignment.
  • For this we set the value MateAlignment property of swDistanceMateFeatureData variable.
  • We get the value of MateAlignment property to newDistance variable.
' Toggle distance Mate Alignment
If swDistanceMateFeatureData.MateAlignment = 1 Then
  swDistanceMateFeatureData.MateAlignment = 2
Else
  swDistanceMateFeatureData.MateAlignment = 1
End If
  • In above code block, we check Mate Alignment.
  • We use πŸš€ IF statement for checking.
  • Condition: swDistanceMateFeatureData.MateAlignment = 1
  • When this condition is True,
    • We set the Mate Alignment value to 2.
  • When this condition is False,
    • We set the Mate Alignment value to 1.
' Print updated Mate Alignment
Debug.Print swDistanceMateFeatureData.MateAlignment
  • In above code block, we print selected mate’s current Alignment.
  • For this we set the value MateAlignment property of swDistanceMateFeatureData variable.
  • We get the value of MateAlignment property to newDistance variable.
' Modify the definition
swMateFeature.ModifyDefinition swDistanceMateFeatureData, swDoc, Nothing
  • In above line, we modify the definition of selected distance mate.
  • We use ModifyDefinition method to update definition.
  • This ModifyDefinition method is part of SldWorks.Feature object.
  • This ModifyDefinition method takes 3 parameters as follows:

    • Data: Feature data object
    • TopDoc: Top-level document
    • Component: Component for the feature
  • Reference: For more details please visit πŸš€ online SOLIDWORKS API Help .

  • Return Value : This ModifyDefinition method return True if the feature definition modified successfully, False if not.

  • In our code, I have used following values:
Table 1: ModifyDefinition Parameters Used
Parameter NameValue Used
Data swDistanceMateFeatureData
TopDoc swDoc
Component Nothing
' 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.
' 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.

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

assembly-toggle-mate-alignment
Figure 1: Final Result

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 Rename Mate or any Feature 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!!!