SOLIDWORKS Macro - Create Mirror
If you are following my articles you will notice that till now we were hardcoding the selections for input parameters.
But from this post onward we will take user-inputs.
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
Objective of this article is to learn how to create Mirror feature through SOLIDWORKS VBA Macros in SOLIDWORKS.
We create Mirror Feature in following steps in general.
- Ask user to select a Feature to Mirror.
- Ask user to select a Plane for Mirror.
- Create Mirror feature from method
InsertMirrorFeature2
.
This method is most updated method, so use this method if you want to create a new Mirror Feature quickly.
Results We Can Get
After running our macro we successfully create Mirror feature as a result.
Below image shows the result we get.
To get the correct result please follow the steps correctly.
Video of Code on YouTube
Please see below ๐ฌ video on how to create Mirror feature from SOLIDWORKS VBA Macros.
Please note that there are no explanation in the video.
Explanation of each line and why we write code this way is given in this article.
It is advisable to watch video, since it help you to better understand the process.
Code Sample
Below is the code
for creating
Mirror Feature feature in VBA is given.
Option Explicit
' Create variable for Solidworks application
Dim swApp As SldWorks.SldWorks
' Create variable for Solidworks document
Dim swDoc As SldWorks.ModelDoc2
' Variable for Solidworks Selection Manager
Dim swSelMgr As SldWorks.SelectionMgr
' Variable for Solidworks Entity
Dim swObject As SldWorks.Entity
' Variable for Solidworks Mirror feature
Dim swFeature As SldWorks.Feature
' Variable for Solidworks Select Data
Dim swSelData As SldWorks.SelectData
' Mirror program
Sub main()
' Setting Solidworks variable to current application
Set swApp = Application.SldWorks
' Check if Solidworks is opened or not
If swApp Is Nothing Then
MsgBox ("Solidworks is not opened")
Exit Sub
End If
' Setting Solidworks document variable to opened part document
Set swDoc = swApp.ActiveDoc
' Check if Solidworks document is opened or not
If swDoc Is Nothing Then
MsgBox ("Solidworks document is not opened. Please open a document.")
Exit Sub
End If
' Set Solidworks Selection Manager variable
Set swSelMgr = swDoc.SelectionManager
' Array of Solidworks Entities
Dim swObjects(1 To 2) As SldWorks.Entity
' Local variable for selection
Dim selectItems As Integer
selectItems = 1
' Loop till we select all entities
While selectItems <= 2
' Message to show user
Dim messageToUser As String
' Update Messages
Select Case selectItems
Case 1
messageToUser = "Please select a Feature for Mirror feature."
Case 2
messageToUser = "Please select a Plane for Mirror feature."
Case Else
Exit Sub
End Select
' Show message to user
MsgBox messageToUser
' Loop until we complete our selection
While swObjects(selectItems) Is Nothing
' Local integer for loop
Dim i As Integer
' Loop until we select
For i = 1 To swSelMgr.GetSelectedObjectCount2(-1)
Select Case selectItems
Case 1
' If the feature is selected
If swSelMgr.GetSelectedObjectType3(i, -1) = swSelectType_e.swSelBODYFEATURES Then
' Set the Solidworks Entity object to feature for mirror
Set swObjects(selectItems) = swSelMgr.GetSelectedObject6(i, -1)
' If the face is selected
ElseIf swSelMgr.GetSelectedObjectType3(i, -1) = swSelectType_e.swSelFACES Then
' Inform user to select feature from Feature Tree
MsgBox "Please select Feature from Feature Tree."
' Clear selection
swDoc.ClearSelection2 True
End If
Case 2
' If the Plane is selected
If swSelMgr.GetSelectedObjectType3(i, -1) = swSelectType_e.swSelDATUMPLANES Then
' Set the Solidworks Entity object to selected plane
Set swObjects(selectItems) = swSelMgr.GetSelectedObject6(i, -1)
Else
' Inform user to select plane
MsgBox "Please select a Plane."
' Clear selection
swDoc.ClearSelection2 True
End If
End Select
Next
DoEvents
Wend
' Clear previous selection
swDoc.ClearSelection2 True
' Increase the selection count
selectItems = selectItems + 1
Wend
' Local variable for counter
Dim j As Integer
j = 1
' Loop till counter is 2, since we have 2 selection
While j < 3
' Set the current instance to Solidworks Entity variable
Set swObject = swObjects(j)
' Create Select data for this entity
Set swSelData = swSelMgr.CreateSelectData
Select Case j
Case 1
' For feature to mirror, set mark to 1
swSelData.Mark = 1
' Select the feature
swObject.Select4 True, swSelData
Case 2
' For plane, set mark to 2
swSelData.Mark = 2
' Select the plane
swObject.Select4 True, swSelData
End Select
j = j + 1
Wend
' Create Mirror feature
Set swFeature = swDoc.FeatureManager.InsertMirrorFeature2(False, False, False, False, swFeatureScope_e.swFeatureScope_AllBodies)
' Check if Mirror Feature creates or not
If swFeature Is Nothing Then
MsgBox ("Failed to create Mirror Feature.")
Exit Sub
End If
' Erase array data
Erase swObjects
' View zoom to fit
swDoc.ViewZoomtofit2
' Clear all selection
swDoc.ClearSelection2 True
End Sub
Prerequisite
There are some prerequisite for this article.
We are not creating feature from code but we use existing ๐ Extrude feature to create Mirror feature as shown in below picture.
As shown in above image, there are only 2 Extrude feature in our part.
Base Extrude Feature
: This is our Base Extruded feature.Mirror Extrude Feature
: This is our Extruded feature for mirror.
If you want to create Extrude feature programmatically then please refer to below article.
We will apply checks in this article, so the code we write should be error free most of the time.
Steps To Follow
To create Mirror Feature there are following steps:
- Creating Global Variables
- Initializing required variables
- Ask user to select feature and plane
- Mark selected entities
- Create Mirror feature
- Final work
Now let us walk through each step as given above, and understand every line.
I also give some links (see icon ๐) so that you can go through them if there are anything I explained in previous articles.
Creating Global Variables
Option Explicit
This line forces us to define every variable we are going to use.
For more information please visit ๐ SOLIDWORKS Macros - Open new Part document post.
We create following variables.
- Variable for Solidworks application
' Variable for Solidworks application
Dim swApp As SldWorks.SldWorks
In this line, we create a variable which we named as swApp
and the type of this swApp
variable is SldWorks.SldWorks
.
To see methods and properties related to SldWorks.SldWorks
object, please visit ๐
this
page of SOLIDWORKS API Help.
- Variable for Solidworks document
' Variable for Solidworks document
Dim swDoc As SldWorks.ModelDoc2
In this line, we create a variable which we named as swDoc
and the type of this swDoc
variable is SldWorks.ModelDoc2
.
To see methods and properties related to SldWorks.ModelDoc2
object, please visit ๐
this
page of SOLIDWORKS API Help.
- Variable for Solidworks Selection Manager
' Variable for Solidworks Selection Manager
Dim swSelMgr As SldWorks.SelectionMgr
In this line, we create a variable which we named as swSelMgr
and the type of this swSelMgr
variable is SldWorks.SelectionMgr
.
To see methods and properties related to SldWorks.SelectionMgr
object, please visit
๐ this
page of SOLIDWORKS API Help.
- Variable for Solidworks Entity
' Variable for Solidworks Entity
Dim swObject As SldWorks.Entity
In this line, we Create a variable which we named as swObject
and the type of this swObject
variable is SldWorks.Entity
.
We create variable swObject
for
SOLIDWORKS Entities (Profile and Path) we ask use to select.
To see methods and properties related to SldWorks.Entity
object, please visit ๐
this
page of SOLIDWORKS API Help.
- Variable for Solidworks Mirror feature
' Variable for Solidworks Mirror feature
Dim swFeature As SldWorks.Feature
In this line, we Create a variable which we named as swFeature
and the type of this swFeature
variable is SldWorks.Feature
.
We create variable swFeature
for
SOLIDWORKS Mirror Feature.
To see methods and properties related to Feature
object, please visit ๐ this
page of SOLIDWORKS API Help.
- Variable for Solidworks Select Data
' Variable for Solidworks Select Data
Dim swSelData As SldWorks.SelectData
In this line, we create a variable named swSelData
as SldWorks.SelectData
object type.
We create variable swSelData
for
SOLIDWORKS Select Data, which we use for Marking selected
object.
To see methods and properties related to SldWorks.SelectData
object, please visit ๐
this
page of SOLIDWORKS API Help.
These all are our global variables.
They are SOLIDWORKS API Objects.
So basically I group all the SOLIDWORKS API Objects in one place.
' Mirror program
Sub main()
End Sub
Next is our Sub
procedure which has
name of main
.
This procedure hold all the statements (instructions) we give to computer.
To know more about Sub Procedure you can check ๐ VBA Sub and Function Procedures article of this website.
Initializing Required Variables
Inside this procedure we first initialize required variables as given below.
- Set SOLIDWORKS variable to SOLIDWORKS application
' Set SOLIDWORKS variable to SOLIDWORKS application
Set swApp = Application.SldWorks
In this line, we set the value of our SOLIDWORKS variable swApp
; which we define earlier; to
SOLIDWORKS application.
' Check if SOLIDWORKS is opened or not
If swApp Is Nothing Then
MsgBox ("SOLIDWORKS is not opened")
Exit Sub
End If
In above line of code, we use an ๐ IF statement to check if SOLIDWORKS application variable is successfully assigned to current SOLIDWORKS application.
- Set SOLIDWORKS document variable to opened part document
' Set SOLIDWORKS document variable to opened part document
Set swDoc = swApp.ActiveDoc
In above line of code, we set SOLIDWORKS document swDoc
variable to currently open part
document.
' Check if SOLIDWORKS document is opened or not
If swDoc Is Nothing Then
MsgBox ("SOLIDWORKS document is not opened. Please open a document.")
Exit Sub
End If
In above line of code, we use an ๐ IF
statement to check if SOLIDWORKS document swDoc
is opened.
If SOLIDWORKS document is not opened then code execute inside the code and inform the user by a ๐ Message Window.
- Set SOLIDWORKS Selection Manager variable
' Set SOLIDWORKS Selection Manager variable
Set swSelMgr = swDoc.SelectionManager
In above line, we set SOLIDWORKS Selection ManagerswSelMgr
variable to current documentโs
Selection Manager.
- Array of SOLIDWORKS Entities
' Array of Solidworks Entities
Dim swObjects(1 To 2) As SldWorks.Entity
In this line, we create an Array of SOLIDWORKS Entities which we named as swObjects
and the type of this SldWorks.Entity
variable is SldWorks.Entity
.
This array consist two SldWorks.Entity
variables.
We define the number of variable this array holds inside (1 To 2).
For more information about the Arrays in VBA please ๐ Array on this website.
- Local variable for User selection
' Local variable for selection
Dim selectItems As Integer
selectItems = 1
In above line of code, we define a Local variable name selectItems
as Integer type.
In next line we assign a value of 1.
Ask user to select feature and plane
Now we will ask user to select feature and plane for Mirror feature.
Please follow steps given below.
' Loop till we select all entities
While selectItems <= 2
Wend
In above line of code we start a While
loop.
For more details about While
loop,
please see ๐ VBA Looping article from this website.
We want to loop until selectItems
variableโs value is equal to 2.
' Message to show user
Dim messageToUser As String
In above line of code we create a variable named messageToUser
of String
type.
This variable holds the message we want to show before selection.
' Update Messages
Select Case selectItems
Case 1
messageToUser = "Please select a Feature for Mirror feature."
Case 2
messageToUser = "Please select a Plane for Mirror feature."
Case Else
Exit Sub
End Select
In above line of code, we use a Select
statement to update message.
We use the case
on selectItems
.
When selectItems = 1
then value of
messageToUser
update to "Please select a Feature for Mirror feature."
Similarly, when selectItems = 2
then
value of messageToUser
update to "Please select a Plane for Mirror feature."
' Show message to user
MsgBox messageToUser
In above line of code, we show the message to user.
Below image shows the message for Feature selection to the user.
Below image shows the message for Plane selection to the user.
' Loop until we complete our selection
While swObjects(selectItems) Is Nothing
Wend
In above line of code, we create another While
loop.
This loops until we select the our Profile and Path.
For more details about While
loop,
please see ๐ VBA Looping article from this website.
' Local integer for loop
Dim i As Integer
In above line of code, we create a local integer name i
as a counter.
' Loop until we select
For i = 1 To swSelMgr.GetSelectedObjectCount2(-1)
Next
In above line of code, we create a For
loop.
This code loops from i = 1
to number of
objects
we select.
' Update Messages
Select Case selectItems
End Select
In above line of code, we use a Select
statement for selection entities.
We use the case
on selectItems
.
Case 1
' If the profile sketch is selected
If swSelMgr.GetSelectedObjectType3(i, -1) = swSelectType_e.swSelBODYFEATURES Then
' Set the Solidworks Entity object to feature for mirror
Set swObjects(selectItems) = swSelMgr.GetSelectedObject6(i, -1)
' If the face is selected
ElseIf swSelMgr.GetSelectedObjectType3(i, -1) = swSelectType_e.swSelFACES Then
' Inform user to select feature from Feature Tree
MsgBox "Please select Feature from Feature Tree."
' Clear selection
swDoc.ClearSelection2 True
End If
Above code sample we use for Case 1
.
In above line of code, we use ๐ IF/Else statement with conditions.
The ๐ IF statement is True
when selected object
is type of SOLIDWORKS Body
Feature.
Similarly, ๐ Else statement
is True when select object
is type of
SOLIDWORKS Face.
When we select the sketch from Model view, then selected object is SOLIDWORKS
Face.
When we select the sketch from Feature Tree, then select object is SOLIDWORKS Body
Feature.
When ๐ IF statement is True we execute code given below.
' Set the Solidworks Entity object to feature for mirror
Set swObjects(selectItems) = swSelMgr.GetSelectedObject6(i, -1)
In above line, we set the indexed object inside array.
- Array of SOLIDWORKS Entities =
swObjects
- Indexed Entity Object =
swObjects[selectItems]
We set the value of this Indexed Entity Object by GetSelectedObject6()
method of SOLIDWORKS
Selection Manager.
When we set the Indexed Entity Object value as feature to
mirror, we exit the 2nd While
loop.
Similarly, when ๐ Else statement is True we execute code given below.
' Inform user to select feature from Feature Tree
MsgBox "Please select feature from Feature Tree."
' Clear selection
swDoc.ClearSelection2 True
- We show message to user to select Feature from Feature Tree.
- After that we clear our selection.
We do this because if selected object type is SOLIDWORKS Face then we can not assign this object as SOLIDWORKS Entity inside array.
Case 2
' If the Plane is selected
If swSelMgr.GetSelectedObjectType3(i, -1) = swSelectType_e.swSelDATUMPLANES Then
' Set the Solidworks Entity object to selected plane
Set swObjects(selectItems) = swSelMgr.GetSelectedObject6(i, -1)
Else
' Inform user to select plane
MsgBox "Please select a Plane."
' Clear selection
swDoc.ClearSelection2 True
End If
Above code sample we use for Case 2
.
In above line of code, we use ๐ IF/Else statement with conditions.
The ๐ IF statement is True
when selected object
is type of SOLIDWORKS Datum
Plane.
If we the selected object
is
not type of SOLIDWORKS Datum Plane then ๐ Else statement code runs.
When ๐ IF statement is True we execute code given below.
' Set the Solidworks Entity object to selected plane
Set swObjects(selectItems) = swSelMgr.GetSelectedObject6(i, -1)
In above line, we set the indexed object inside array.
- Array of SOLIDWORKS Entities =
swObjects
- Indexed Entity Object =
swObjects[selectItems]
We set the value of this Indexed Entity Object by GetSelectedObject6()
method of SOLIDWORKS
Selection Manager.
When we set the Indexed Entity Object value as feature to
mirror, we exit the 2nd While
loop.
Similarly, when ๐ Else statement is True we execute code given below.
' Inform user to select plane
MsgBox "Please select a Plane."
' Clear selection
swDoc.ClearSelection2 True
- We show message to user to select Plane from Feature Tree.
- After that we clear our selection.
After For
loop, we have a call for
DoEvents
function.
This function repeats the While
loop
until we select the Profile or Path.
' Clear previous selection
swDoc.ClearSelection2 True
' Increase the selection count
selectItems = selectItems + 1
In above line, when we finished with the Profile or Path selection, we clear previous selection and increment the selected count by 1.
After increment the selected count we continue our while
loop.
Mark selected Entities
Till now we have completed our selection.
Now we need to do Select and Mark entities correctly from our Array.
' Local variable for counter
Dim j As Integer
j = 1
In above line of code we create integer
variable for counter.
' Loop till counter is 2, since we have 2 selection
While j < 3
Wend
In above line of code we create a while
loop with condition that counter value of j
should be less than 3.
' Set the current instance to Solidworks Entity variable
Set swObject = swObjects(j)
In above line of code, we set the SOLIDWORKS Entity variable to current object from array.
' Create Select data for this entity
Set swSelData = swSelMgr.CreateSelectData
In above line of code, we create SOLIDWORKS Select Data for current SOLIDWORKS Entity object.
' Update Messages
Select Case j
End Select
In above line of code, we use a Select
statement for selection entities.
We use the case
on j
.
Case 1
' For feature to mirror, set mark to 1
swSelData.Mark = 1
' Select the feature
swObject.Select4 True, swSelData
Above code sample we use for Case 1
.
In above lines, we update the Mark to 1
for our SOLIDWORKS Select
Data variable.
For Mirror feature, Mark
values can be follows:
Anyโฆ | Must be preselected and marked with a value ofโฆ |
---|---|
Features to be mirrored | 1 |
Faces to be mirrored | 128 |
Bodies to be mirrored | 256 |
Plane or planar face | 2 |
For more details about Mark please visit ๐ Mirror Feature in SOLIDWORS API Help.
' Select the feature
swObject.Select4 True, swSelData
In above line of code, we select the Feature object.
We select it by using Select4
method of
SOLIDWORKS Entity object.
This method takes 2 arguments.
- Append -
True
appends the entity to the selection list,False
replaces the selection list with this entity. - Data - Pointer to the
ISelectData
object.
We use following values as parameter.
- Append -
True
- Data -
swSelData
Case 2
' For plane, set mark to 2
swSelData.Mark = 2
' Select the plane
swObject.Select4 True, swSelData
Above code sample we use for Case 2
.
In above lines, we update the Mark to 2
for our SOLIDWORKS Select
Data variable.
For Mirror feature, Mark
values can be follows:
Anyโฆ | Must be preselected and marked with a value ofโฆ |
---|---|
Features to be mirrored | 1 |
Faces to be mirrored | 128 |
Bodies to be mirrored | 256 |
Plane or planar face | 2 |
For more details about Mark please visit ๐ Mirror Feature in SOLIDWORS API Help.
' Select the feature
swObject.Select4 True, swSelData
In above line of code, we select the Feature object.
We select it by using Select4
method of
SOLIDWORKS Entity object.
This method takes 2 arguments.
- Append -
True
appends the entity to the selection list,False
replaces the selection list with this entity. - Data - Pointer to the
ISelectData
object.
We use following values as parameter.
- Append -
True
- Data -
swSelData
j = j + 1
After Select
statement, we increment the counter variable j
by 1.
Create Mirror Feature
We have completed our selection and Marking of SOLIDWORKS Entities.
Now we create Mirror Feature.
' Create Mirror feature
Set swFeature = swDoc.FeatureManager.InsertMirrorFeature2(False, False, False, False, swFeatureScope_e.swFeatureScope_AllBodies)
In above line of code we set the value of variable swFeature
by InsertCutBlend
method.
This CreateFeature
method takes
following parameters as explained:
-
BMirrorBody -
True
to mirror solid bodies;False
to mirror a feature or face. -
BGeometryPattern -
True
to mirror only the feature geometry,False
to solve the entire feature; applies to mirroring features only. -
BMerge -
True
to merge any mirrored solid bodies,False
to not; applies to mirroring solid bodies only. -
BKnit -
True
to knit surfaces,False
to not; applies to mirroring surfaces only. -
ScopeOptions - Feature scope as defined by
swFeatureScope_e
as follows.Member Description swFeatureScope_AllBodies
0 = All of the bodies in the multibody part are affected by the Mirror feature. swFeatureScope_SelectedBodiesWithAutoSelect
1 = Only the specified bodies in the multibody part are affected by the Mirror feature when AutoSelect
is true.swFeatureScope_SelectedBodiesWithOutAutoSelect
2 = Only the specified bodies in the multibody part are affected by the Mirror feature when AuotSelect
is false.
Return Value : This InsertMirrorFeature2
method return
feature data object.
To see methods and properties related to Feature
object, please visit ๐ this
page of SOLIDWORKS API Help.
In our code, I have used following values:
Parameter Name | Value Used |
---|---|
BMirrorBody | False |
BGeometryPattern | False |
BMerge | False |
BKnit | False |
ScopeOptions | swFeatureScope_e.swFeatureScope_AllBodies
|
InsertMirrorFeature2
method is part of
FeatureManager
object.
This FeatureManager
is again part of
swDoc
variable i.e. ModelDoc2
object.
To see methods and properties related to FeatureManager
object, please visit ๐
this
page of SOLIDWORKS API Help.
' Check if Mirror Feature creates or not
If swFeature Is Nothing Then
MsgBox ("Failed to create Mirror Feature.")
Exit Sub
End If
In above line of code, we use an ๐ IF statement to check if we able to create Mirror Feature or not.
If we failed to select then inform the user by a ๐ Message Window.
After showing error message our program exit from here itself.
Now we run the macro and after running macro we get Revolve as shown in below image.
Final work
After creating Mirror feature, we have to do some cleaning work so that we can use this macro frequently.
- Empty SOLIDWORKS Entity Array
' Erase array data
Erase swObjects
In above line, we erase data from SOLIDWORKS Entity array.
For this we use Erase
function in-build in VBA.
- Make part Zoom to fit
' 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.
- Clear selection
' Clear all selection
swDoc.ClearSelection2 True
In above line, we clear all previous selection.
For this we use ClearSelection2
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 create Mirror feature with SOLIDWORKS VBA Macros.
For more such tutorials on SOLIDWORKS VBA Macro, do come to this blog 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!!!