Solidworks VBA Macro - Rename Mate

7 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 understand โ€œhow toโ€ Rename a Mate in Assembly document from VBA macro.

You can use this method to rename any feature.

In my example, I am renaming last added mate.

Results We Can Get

Below image shows the result we get.

assembly-rename-mate

We add Rename Mate in following steps.

  1. Ask a name from user.
  2. Rename Mate.

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

Macro Video

Below ๐ŸŽฌ video shows how to Rename 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 Rename 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 Mate Feature
Dim swMateFeature As SldWorks.Feature

' Program to Rename 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 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
  
  ' Variable for mate's new name
  Dim newName As String
  
  ' Get mate's new name
  newName = InputBox("New Name:", "Edit Mate")
  
  ' This will handle empty value or cancel case
  If Len(newName) = 0 Then
    MsgBox "Empty or no value. Please try again."
    swDoc.ClearSelection2 True
    Exit Sub
  End If
  
  ' Update mate's new name
  swMateFeature.Name = newName
  
  ' 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.

  • 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 mostly error free.

Steps To Follow

This VBA macro can be divided into following sections:

  1. Create global variables
  2. Initialize global variables
  3. Rename 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 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.

These all are our global variables.

They are SOLIDWORKS API Objects.

' Program to Rename Mate
Sub main()

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

Add Rename Mate

In this section, we Rename Mate.

' 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.
' Variable for mate's new name
Dim newName As String
  • Purpose: In above line, we create a variable to store new name.
  • Variable Name: newName
  • Type: String
' Get mate's new name
newName = InputBox("New Name:", "Edit Mate")
  • In above line of code we are doing 2 steps in one line.

    Those 2 steps are explained below.

    • Step 1 - Getting New Name from user.

    Below image shows the message for New Name to the user.

    message-for-new-name

    • Step 2 - Assigned input value to newName variable.
' This will handle empty value or cancel case
If Len(newName) = 0 Then
  MsgBox "Empty or no value. Please try again."
  swDoc.ClearSelection2 True
  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(newName) = 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 clear all selection and stop our macro here.
' Update mate's name
swMateFeature.Name = newName
  • In above code block, we update selected mateโ€™s name.
  • For this we set the value Name property of swMateFeature variable.
  • Reference: Please visit ๐Ÿš€ online SOLIDWORKS API Help for more help.
' 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 Rename Mate as shown in below image.

assembly-rename-mate

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!!!

Updated: