Solidworks VBA Macro - Add Width Mate
If you are following my articles then will not be an issue for you.
In this article I created various ๐ Functions to avoid duplication of code.
I tried to write code as I would write in real application.
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 Width Mate in Assembly document of SOLIDWORKS CAD Software.
This method is most updated method, so use this method if you want to add Width Mate quickly.
Results We Can Get
After running our macro, we successfully add Width Mate a Component in our Assembly as a result.
Below image shows the result we get.
We add Width Mate in following steps in general.
- loop through each component in current assembly.
- Loop through each face.
- Get desired face from user confirmation.
- Select required faces for mate.
- Add Width Mate.
- Final Work
To get the correct result please follow the steps correctly.
Macro Video
Below ๐ฌ video shows Adding Width 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 Width 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 Tab Face List
Dim vTabFaces(1 To 2) As SldWorks.Face2
' Variable for Solidworks Width Face List
Dim vWidthFaces(1 To 2) As SldWorks.Face2
' Variable for Solidworks Mate Feature Data
Dim swMateData As SldWorks.MateFeatureData
' Variable for Solidworks Width Mate Feature Data
Dim swWidthMateData As SldWorks.WidthMateFeatureData
' Program to add Width 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.swMateWIDTH)
' Set Width Mate data to Assembly Mate Data
Set swWidthMateData = swMateData
' Set Mate Type
swWidthMateData.ConstraintType = swMateWidthOptions_e.swMateWidth_Centered
' 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
GetFaces swComponent, componentIndex
Next
' Select Faces for Width Mate
If SelectFaces() = False Then
' On failed select, return from here.
Exit Sub
End If
' Set Width Mate Tab faces
swWidthMateData.TabSelection = vTabFaces
' Set Width Mate Width faces
swWidthMateData.WidthSelection = vWidthFaces
' Add Width Mate
Set swMateFeature = swAssembly.CreateMate(swWidthMateData)
' 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 get required face for Width mate
Function GetFaces(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
' Variable to count added faces
Dim faceNumber As Integer: faceNumber = 0
' 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
' Calling function for adding faces
AddFaces faceNumber, componentIndex
' Increment face count
faceNumber = faceNumber + 1
End If
' When both faces are added
If faceNumber = 2 Then
Exit Function
End If
' Get next face
Set swFace = swFace.GetNextFace
Loop
End Function
' Function to add Faces for Width mate
Function AddFaces(faceNumber As Integer, componentIndex As Integer)
If componentIndex = 1 Then
' Add current face to List of Faces
Set vWidthFaces(faceNumber + 1) = swFace
Exit Function
End If
' Add current face to List of Faces
Set vTabFaces(faceNumber + 1) = swFace
End Function
' Function to select faces for Width mate
Function SelectFaces() As Boolean
' Clear any previous selection
swDoc.ClearSelection2 True
' Variable for Solidworks Select Data
Dim swSelData As SldWorks.SelectData
' Set value of Solidworks Select Data variable
Set swSelData = swDoc.SelectionManager.CreateSelectData
' Variable to hold return value of SelectFaceArrays function
Dim returnValue As Boolean
' Calling function to select Tab faces
returnValue = SelectFaceArrays(swSelData, 1)
' Calling function to select Width faces
returnValue = SelectFaceArrays(swSelData, 16)
' Send this return value
SelectFaces = returnValue
End Function
' Function to Select Required Face Arrays
Function SelectFaceArrays(swSelData As SldWorks.SelectData, selectMark As Integer) As Boolean
' Set Mark of Solidworks Select Data
swSelData.mark = selectMark
' Boolean variable
Dim boolStatus As Boolean
' Error Message
Dim errorMessage As String
If selectMark = 1 Then
' Select Tab faces
boolStatus = swDoc.Extension.MultiSelect2(vTabFaces, False, swSelData)
errorMessage = "Failed to select Tab faces."
Else
' Select Width faces
boolStatus = swDoc.Extension.MultiSelect2(vWidthFaces, False, swSelData)
errorMessage = "Failed to select Width faces."
End If
' Check if faces are selected
If boolStatus = False Then
MsgBox errorMessage
swDoc.ClearSelection2 True
SelectFaceArrays = boolStatus
Exit Function
End If
SelectFaceArrays = True
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.
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:
- Create and Initialize required variables
- Get Components and Loop through them
- Get faces
- Select faces
- Add Width Mate
- 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
- 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 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 Tab Face List
Dim vTabFaces(1 To 2) As SldWorks.Face2
- Purpose: Define ๐ Array of Face2.
- Variable Name:
vTabFaces - Type:
Face2 - Length of Array: (1 To 2)
- Reference: Please visit
- For Arrays in VBA : ๐ Array on this website.
' Variable for Solidworks Width Face List
Dim vWidthFaces(1 To 2) As SldWorks.Face2
- Purpose: Define ๐ Array of Face2.
- Variable Name:
vWidthFaces - Type:
Face2 - Length of Array: (1 To 2)
- Reference: Please visit
- For Arrays in VBA : ๐ Array on this website.
' 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 Width Mate Feature Data
Dim swWidthMateData As SldWorks.WidthMateFeatureData
- Purpose: In above line, we create a variable for Solidworks Width Mate Feature Data.
- Variable Name:
swWidthMateData - Type:
SldWorks.WidthMateFeatureData. - Reference: Please visit ๐ online SOLIDWORKS API Help.
These all are our global variables.
They are SOLIDWORKS API Objects.
' Program to add Width Mate
Sub main()
End Sub
- In above line, we create main Program to add Width Mate in assembly.
- This is a
Subprocedure 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.
' Set Solidworks Application variable to current application
Set swApp = Application.SldWorks
- In above line, we set value of
swAppvariable. - 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
swDocvariable. - 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
swDocvariable. - 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
swAssemblyvariable. - This value is
swDocvariable.
' Create Assembly Mate Data
Set swMateData = swAssembly.CreateMateData(swMateType_e.swMateWIDTH)
- In above line, we set the value of
swMateDatavariable. - We set value by
CreateMateData()method. - This
CreateMateData()method is part ofswAssemblyvariable. - This
CreateMateData()method takes following parameter.-
Type: Type of mate to create as defined in
swMateType_ein below table.Member Description swMateANGLE6 swMateCAMFOLLOWER9 swMateCOINCIDENT0 swMateCONCENTRIC1 swMateCOORDINATE20 swMateDISTANCE5 swMateGEAR10 swMateHINGE22 swMateLINEARCOUPLER18 swMateLOCK16 swMateLOCKTOSKETCH12 swMateMAGNETIC25 swMateMAXMATES14 swMatePARALLEL3 swMatePATH15 swMatePERPENDICULAR2 swMatePROFILECENTER24 swMateRACKPINION13 swMateSCREW17 swMateSLIDER23 swMateSLOT21 swMateSYMMETRIC8 swMateTANGENT4 swMateUNIVERSALJOINT19 swMateUNKNOWN7 swMateWIDTH11
-
- Since we want to add Width mate, hence we use value
swMateWIDTHas type. - Reference: Please visit ๐ online SOLIDWORKS API Help.
' Set Width Mate data to Assembly Mate Data
Set swWidthMateData = swMateData
- In above line, we set Width Mate data to previously created Assembly Mate data.
- We set value of variable
swWidthMateDatato variableswMateData. - Reference: Please visit ๐ online SOLIDWORKS API Help.
' Set Mate Type
swWidthMateData.ConstraintType = swMateWidthOptions_e.swMateWidth_Centered
- In above line, set the โMate Typeโ โก โCenteredโ Type as shown in below image.
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
vArrayvariable. - We set value by
GetComponentsmethod ofswAssemblyvariable.
' 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
Forloop. - This loops start from
i = 0to maximum number of items invArraywe select.
' Set Solidworks Component variable
Set swComponent = vArray(componentIndex)
- In above line, we set value of
swComponentvariable. - This value is current value of array
vArray.
Get Faces
In this section we get desired face for Width mate.
' Calling this function
GetFaces swComponent, componentIndex
- In above line, we call a function.
- Function Name:
GetFaces - Function Parameters: They are
swComponentcomponentIndex
' Function to get required face for Tangent mate
Function GetFaces(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
Functionprocedure which has name ofSelectFace. - 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.Component2componentIndex As Integer
' Get body of current component
Set swBody = component.GetBody
- In above line, we set value of
swBodyvariable. - Value of
swBodyvariable is set byGetBodymethod ofcomponentvariable.
' Get First face
Set swFace = swBody.GetFirstFace
- In above line, we set value of
swFacevariable. - Value of
swFacevariable is set byGetFirstFace, which is part ofswBodyvariable.
' 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
' Variable to count added faces
Dim faceNumber As Integer: faceNumber = 0
- Purpose: In above line, we create a variable to count number of faces added.
- At the same time we set the value of this variable
faceNumberto0i.e.faceNumber = 0
- At the same time we set the value of this variable
- Variable Name:
faceNumber - Type:
Integer
' Loop through all Faces
Do While Not swFace Is Nothing
Loop
- In above line, we loop through all ๐ Faces of current component.
' Clear all selection
swDoc.ClearSelection2 True
- In above line, we clear all previous selection.
- For this we use
ClearSelection2method which is part of SOLIDWORKS Document variable i.eswDocvariable.
' 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.
- Show a Message Box to user as shown below.
- Store user response in
respvariable.
' 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
' Calling function for adding faces
AddFaces faceNumber, componentIndex
- In above line, we call a function.
- Function Name:
AddFaces - Function Parameters: They are
faceNumbercomponentIndex
' Function to add Faces for Width mate
Function AddFaces(faceNumber As Integer, componentIndex As Integer)
End Function
- In above line, we create a ๐ Function to add required face for Width mate.
- Name of ๐ Function is
AddFaces. - This procedure hold all the statements (instructions) for adding required faces for Width mate.
- Reference: Detailed information ๐ VBA Sub and Function Procedures article of this website.
This function has 2 parameters as follows:
faceNumber As IntegercomponentIndex As Integer
If componentIndex = 1 Then
End If
- In above code block, we check if
componentIndexis 1. - We use ๐ IF statement for checking.
- Condition:
componentIndex = 1
' Add current face to List of Width Faces
Set vWidthFaces(faceNumber + 1) = swFace
Exit Function
- When
componentIndexis 1, then above code execute. - In above line, 1st we add current face to List of Width Faces.
- 2nd, we exit this
AddFacesfunction.
' Add current face to List of Faces
Set vTabFaces(faceNumber + 1) = swFace
- When
componentIndexis not 1, then above code execute. - In above line, 1st we add current face to List of Tab Faces.
- 2nd, we exit this
AddFacesfunction.
' Increment face count
faceNumber = faceNumber + 1
- In above line, we Increment face count variable i.e.
faceNumberby 1.
' When both faces are added
If faceNumber = 2 Then
Exit Function
End If
- In above code block, we check if
faceNumberis 2. - We use ๐ IF statement for checking.
- Condition:
faceNumber = 2- When this condition is True, then we exit
GetFacesfunction.
- When this condition is True, then we exit
' Get next face
Set swFace = swFace.GetNextFace
- In above line, we set the value of
swFacevariable. - This is done by
GetNextFacemethod ofswFacevariable.
Select Faces
In this section, we Select Faces.
' Select Faces for Width Mate
If SelectFaces() = False Then
' On failed select, return from here.
Exit Sub
End If
- In above code block, we check the returning value of function
SelectFaces(). - We use ๐ IF statement for checking.
- Condition:
SelectFaces() = False- When this condition is
True, then we exitmainprocedure.
- When this condition is
- In above condition, we call a
SelectFaces()function. - Function Name:
SelectFaces - Function Parameters: This function did not take any parameters.
- Return Value: This function return
TrueorFalse.
' Function to select faces for Width mate
Function SelectFaces() As Boolean
End Function
- In above line, we create a ๐ Function to select required face for Width mate.
- Name of ๐ Function is
SelectFaces. - This procedure hold all the statements (instructions) for Selecting required faces for Width mate.
- Reference: Detailed information ๐ VBA Sub and Function Procedures article of this website.
- Function Parameters: This function did not take any parameters.
- Return Value: This function return
TrueorFalse.
' Clear all selection
swDoc.ClearSelection2 True
- In above line, we clear all previous selection.
- For this we use
ClearSelection2method which is part of SOLIDWORKS Document variable i.eswDocvariable.
' 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
swSelDatavariable. - Value of
swSelDatavariable is set byCreateSelectData, which is part ofSelectionManager. ThisSelectionManageris part ofswDocvariable.
' Variable to hold return value of SelectFaceArrays function
Dim returnValue As Boolean
- Purpose: In above line, we create a variable to hold return value of
SelectFaceArraysfunction. - Variable Name:
returnValue - Type:
Boolean
' Calling function to select Tab faces
returnValue = SelectFaceArrays(swSelData, 1)
- In above line, we are doing 2 things at same time.
- Calling
SelectFaceArraysfunction.- Function Name:
SelectFaceArrays - Function Parameters: They are
swSelData1
- Function Name:
- Setting value of
returnValuevariable.- This value is return value of
SelectFaceArraysfunction.
- This value is return value of
- Calling
' Function to Select Required Face Arrays
Function SelectFaceArrays(swSelData As SldWorks.SelectData, selectMark As Integer) As Boolean
End Function
- In above line, we create a ๐ Function to select required Face Arrays.
- Name of ๐ Function is
SelectFaceArrays. - This procedure hold all the statements (instructions) for Selecting Required Face Arrays.
- Reference: Detailed information ๐ VBA Sub and Function Procedures article of this website.
- Function Parameters: They are
swSelData As SldWorks.SelectDataselectMark As Integer
- Return Value: This function return
TrueorFalse.
' Set Mark of Solidworks Select Data
swSelData.Mark = selectMark
- In above line, we set Mark of
swSelDatavariable to 1.
' Boolean variable
Dim boolStatus As Boolean
- Purpose: In above line, we create a Boolean variable.
- Variable Name:
boolStatus - Type:
Boolean
' Error Message
Dim errorMessage As String
- Purpose: In above line, we create a variable for Error Message.
- Variable Name:
errorMessage - Type:
String
If selectMark = 1 Then
' Select Tab faces
boolStatus = swDoc.Extension.MultiSelect2(vTabFaces, False, swSelData)
errorMessage = "Failed to select Tab faces."
Else
' Select Width faces
boolStatus = swDoc.Extension.MultiSelect2(vWidthFaces, False, swSelData)
errorMessage = "Failed to select Width faces."
End If
- In above code block, we check the returning value of
selectMarkvariable. - We use ๐ IF statement for checking this condition.
- Condition:
selectMark = 1 - When above condition is
True, then below code executes and select Tab Faces.
' Select Tab faces
boolStatus = swDoc.Extension.MultiSelect2(vTabFaces, False, swSelData)
errorMessage = "Failed to select Tab faces."
- In above line, we select Faces by
MultiSelect2method. - This
MultiSelect2method takes following parameter.- Objects: Array of selectable objects.
- AppendFlag:
Trueto append the objects to the selection list,Falseto replace the current selection list with these objects. - Data: ๐
ISelectDataobject, Nothing, or null
- When condition
selectMark = 1isFalse, then below code executes and select Width Faces.
' Select Width faces
boolStatus = swDoc.Extension.MultiSelect2(vWidthFaces, False, swSelData)
errorMessage = "Failed to select Width faces."
- In above line, we select Faces by
MultiSelect2method. - This
MultiSelect2method takes following parameter.- Objects: Array of selectable objects.
- AppendFlag:
Trueto append the objects to the selection list,Falseto replace the current selection list with these objects. - Data: ๐
ISelectDataobject, Nothing, or null
' Check if faces are selected
If boolStatus = False Then
MsgBox errorMessage
swDoc.ClearSelection2 True
SelectFaceArrays = boolStatus
Exit Function
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:
errorMessagevariable contains the message. - Clear all previous selection.
- Then we set the value of this
SelectFaceArrays()function toboolStatusvariable. - Then we Exit this
SelectFaceArrays()function.
SelectFaceArrays = True
If there are no errors then in above line, we set the value of this SelectFaceArrays() function to True and then exit the function.
' Calling function to select Width faces
returnValue = SelectFaceArrays(swSelData, 16)
- In above line, we are doing 2 things at same time.
- Calling
SelectFaceArraysfunction.- Function Name:
SelectFaceArrays - Function Parameters: They are
swSelData16
- Function Name:
- Setting value of
returnValuevariable.- This value is return value of
SelectFaceArraysfunction.
- This value is return value of
- Calling
' Send this return value
SelectFaces = returnValue
In above line, we return the value of returnValue variable for this function.
Add Width Mate
In this section, we add Width Mate.
' Set Width Mate Tab faces
swWidthMateData.TabSelection = vTabFaces
- In above line we set TabSelection of
swWidthMateData=>vTabFacesvariable.
' Set Width Mate Width faces
swWidthMateData.WidthSelection = vWidthFaces
- In above line we set WidthSelection of
swWidthMateData=>vWidthFacesvariable.
' Add Width Mate
Set swMateFeature = swAssembly.CreateMate(swWidthMateData)
-
In above line, we set the value of variable
swMateFeaturebyCreateMate()method. -
This
CreateMate()method takes following parameters as explained:-
MateData - Mate-specific object:
Mate-specific object IAngleMateFeatureDataICamFollowerMateFeatureDataICoincidentMateFeatureDataIConcentricMateFeatureDataIDistanceMateFeatureDataIGearMateFeatureDataIHingeMateFeatureDataILinearCouplerMateFeatureDataILockMateFeatureDataIParallelMateFeatureDataIPerpendicularMateFeatureDataIProfileCenterMateFeatureDataIRackPinionMateFeatureDataIScrewMateFeatureDataISlotMateFeatureDataISymmetricMateFeatureDataITangentMateFeatureDataIUniversalJointMateFeatureDataIWidthMateFeatureData
-
-
Return Value : This
CreateMate()method return ๐ Mate2 data object. -
In our code, I have used following values:
Parameter Name Value Used MateData swWidthMateData
Reference: For more details about
- Solidworks AssemblyDoc details: ๐ online Solidworks API Help for Solidworks Feature Manager.
- CreateMate Method: ๐ online Solidworks API Help for
CreateMateMethod.
' 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 Width 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 Width Mate as shown in below image.
Final work
In this section, after adding Width 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
ClearSelection2method which is part of SOLIDWORKS Document variable i.eswDocvariable.
' View zoom to fit
swDoc.ViewZoomtofit2
- In above line, we make our view zoom to fit the model.
- For this we use
ViewZoomtofit2method which is part of SOLIDWORKS Document variable i.eswDocvariable.
' Rebuild assembly
swDoc.ForceRebuild3 True
- In above line, we Rebuild assembly.
- For this we use
ForceRebuild3method which is part of SOLIDWORKS Document variable i.eswDocvariable.
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 Width 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!!!



