Solidworks VBA Macro - Edit Distance Mate
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โ Edit Distance Mate in Assembly document from VBA macro.
You can use this method to Edit any Mate.
In my example, I am editing Distance mate.
Results We Can Get
Below image shows the result we get.
We Edit Distance Mate in following steps.
- Ask Distance from user.
- Update distance in Mate.
To get the correct result, please follow the steps correctly.
Macro Video
Below ๐ฌ video shows how to edit Distance 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 editing Distance 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
' 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 update Distance 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
' Local variables used as Conversion Factors
Dim LengthConversionFactor As Double
Dim AngleConversionFactor As Double
' Get updated unit conversion factors
Call GetUnitConversionFactors(LengthConversionFactor, AngleConversionFactor)
' Variable to hold user input
Dim response As String
' Getting Distance from user.
response = InputBox("Please Enter [Distance]:")
' 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 Distance
Dim newDistance As Double
' Set Distance
newDistance = CDbl(response) * LengthConversionFactor
' This will handle case for 0 Distance
If newDistance = 0 Then
MsgBox "Entered value must be greater than 0. Please try again."
swDoc.ClearSelection2 True
Exit Sub
End If
' 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
' Update distance to new value
swDistanceMateFeatureData.distance = newDistance
' Modify the definition
swMateFeature.ModifyDefinition swDistanceMateFeatureData, swDoc, Nothing
' Clear all selection
swDoc.ClearSelection2 True
' Rebuild assembly
swDoc.ForceRebuild3 True
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 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:
- Create global variables
- Initialize global variables
- Get unit conversion factors
- Get distance and Validations
- Edit distance 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
- Purpose: Above line forces us to define every variable we are going to use.
- Reference: ๐ SOLIDWORKS Macros - Open new Part document article.
' 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 update Distance Mate
Sub main()
End Sub
- In above line, we create main Program to update Distance Mate in assembly.
- This is a
Sub
procedure which has name ofmain
. - 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 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
andAngleConversionFactor
- 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 inmain
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.
Get Distance And Validations
In this section, we get get the required Distance from user and apply some validation on Distance.
' 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 Distance from user.
response = InputBox("Please Enter [Distance]:")
-
In above line of code we are doing 2 steps in one line.
Those 2 steps are explained below.
- Step 1 - Getting Distance from user.
Below image shows the message for Distance to the user.
- 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 Distance
Dim newDistance As Double
- In above line, we create a variable to store new distance.
- Variable Name:
newDistance
- Type:
Double
newDistance = CDbl(response) * LengthConversionFactor
-
In above line of code we are doing 3 steps in one line.
Those 3 steps are explained below.
- Step 1 - Converting newDistance from user to
Double
type. - Step 2 - Updating converted distance as per document unit system.
- Step 3 - Assigned input value to
newDistance
variable.
- Step 1 - Converting newDistance from user to
' This will handle case for 0 distance
If newDistance = 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:
newDistance = 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.
Edit Distance Mate
In this section, we Edit Distance Mate.
' Get mate feature
Set swMateFeature = swDoc.Extension.GetLastFeatureAdded
- In above line, we set the value of variable
swMateFeature
byGetLastFeatureAdded
method. - This
GetLastFeatureAdded
method is part ofExtension
object. - This
Extension
object is then part ofswDoc
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 eitherTrue
orFalse
.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 ofSldWorks.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 variableswMateFeatureData
. - Reference: Please visit ๐ online SOLIDWORKS API Help.
' Update distance to new value
swDistanceMateFeatureData.distance = newDistance
- In above code block, we selected mateโs distance.
- For this we set the value
distance
property ofswDistanceMateFeatureData
variable. - We set the value of
distance
property tonewDistance
variable, which we asked from user.
' 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 ofSldWorks.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 returnTrue
if the feature definition modified successfully,False
if not. -
In our code, I have used following values:
Parameter Name Value 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.eswDoc
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.eswDoc
variable.
Now we run the macro and after running macro we get Modified Distance Mate as shown in below image.
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!!!