SOLIDWORKS Macro - Create Fillet
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 Fillet feature through SOLIDWORKS VBA Macros in SOLIDWORKS.
We create Fillet Feature in following steps in general.
- Ask user to select an edge.
- Ask user to input the Radius of fillet feature.
- Create Fillet feature from method
FeatureFillet3
.
This method is most updated method, so use this method if you want to create a new Fillet Feature.
Steps To Create Fillet
We use following steps to create Fillet Feature while writing macro.
- Ask user to select an edge.
- Update code for Unit Conversion.
- Ask user to input the Radius of fillet feature.
- Create parameters variables for
FeatureFillet3
method. - Create Fillet feature from method
FeatureFillet3
.
Results We Can Get
After running our macro we successfully create Fillet 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 Fillet 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
Fillet Feature feature in VBA is given.
Option Explicit
' Variable for Solidworks Application
Dim swApp As SldWorks.SldWorks
' Variable for Solidworks document
Dim swDoc As SldWorks.ModelDoc2
' Variable for Solidworks Fillet Feature
Dim swFeature As SldWorks.Feature
' Variable for Solidworks Selection Manager
Dim swSelMgr As SldWorks.SelectionMgr
' Variable for Solidworks Entity
Dim swObject As SldWorks.Entity
' Program to create Fillet feature
Sub main()
' Set Solidworks Application 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
' 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. Please open a document.")
Exit Sub
End If
' Set Solidworks Selection Manager variablt to opened document's selection manager
Set swSelMgr = swDoc.SelectionManager
' Inform user to select an edge for Fillet feature.
MsgBox "Please select an Edge for Fillet feature."
' Loop until Solidworks Entity variable is equal to selected edge
While swObject Is Nothing
' Local integer for loop
Dim i As Integer
' Looping until we select an edge
For i = 1 To swSelMgr.GetSelectedObjectCount2(-1)
' If the selection type edge
If swSelMgr.GetSelectedObjectType3(i, -1) = swSelectType_e.swSelEDGES Then
' Set the Solidworks Entity object to selected edge
Set swObject = swSelMgr.GetSelectedObject6(i, -1)
End If
Next
DoEvents
Wend
'-----------------------BELOW IS THE SOLUTION----------------------------------------
' Local variables used as Conversion Factors
Dim LengthConversionFactor As Double
Dim 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
'----------------------------------------------------------------
' Variable to store fillet radius
Dim filletRadius As Double
filletRadius = InputBox("Please enter radius of Fillet.") * LengthConversionFactor
' Radius array for Fillet feature
Dim radiiArray0 As Variant
' Radius variable
Dim radiis0 As Double
' Distance array for Fillet feature
Dim dist2Array0 As Variant
' Distance variable
Dim dists20 As Double
' Conic Rhombus array for Fillet feature
Dim conicRhosArray0 As Variant
' Conic Rhombus variable
Dim coniRhos0 As Double
' Set back array for Fillet feature
Dim setBackArray0 As Variant
' Set back variable
Dim setBacks0 As Double
' Point array for Fillet feature
Dim pointArray0 As Variant
' Point variable
Dim points0 As Double
' Point Distance array for Fillet feature
Dim pointDist2Array0 As Variant
' Point distance variable
Dim pointsDist20 As Double
' Point Rhombus array for Fillet feature
Dim pointRhoArray0 As Variant
' Point Rhombus variable
Dim pointsRhos0 As Double
' Set 1st instance to respective variable
radiiArray0 = radiis0
dist2Array0 = dists20
conicRhosArray0 = coniRhos0
setBackArray0 = setBacks0
pointArray0 = points0
pointDist2Array0 = pointsDist20
pointRhoArray0 = pointsRhos0
' Create Fillet feature
Set swFeature = swDoc.FeatureManager.FeatureFillet3(195, filletRadius, filletRadius, 0, 0, 0, 0, (radiiArray0), (dist2Array0), (conicRhosArray0), (setBackArray0), (pointArray0), (pointDist2Array0), (pointRhoArray0))
' Check if Fillet Feature created or not
If swFeature Is Nothing Then
MsgBox ("Failed to create Fillet Feature.")
Exit Sub
End If
' 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 Fillet feature as shown in below picture.
As shown in above image, there is only 1 Extrude feature in our part.
Extrude Feature
: This is our Extruded part for Fillet Feature.
If you want to create Extrude feature programmatically then please refer to below article.
Also, 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 Fillet Feature, there are following steps:
- Creating Global Variables
- Initializing required variables
- Ask user to select an edge
- Get unit Conversion factors
- User input for Fillet radius
- Preparing parameters
- Create Fillet feature using parameters
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 Fillet Feature
' Variable for Solidworks Fillet 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 Fillet Feature.
To see methods and properties related to Feature
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
.
To see methods and properties related to Entity
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.
' Program to create Fillet feature
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.
Ask user to select an edge
Now we will ask use to select an edge for Fillet.
' Inform user to select an edge for Fillet feature.
MsgBox "Please select an Edge for Fillet feature."
Image of above message is shown in below image.
In about line of code we show a Msgbox
to use displaying message to select an Edge of extrude.
' Loop until Solidworks Entity variable is equal to selected edge
While swObject Is Nothing
Wend
In above line of code, we create a While
loop.
This loops until we select the our Edge for Fillet feature.
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 an edge
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.
' If the selection type edge
If swSelMgr.GetSelectedObjectType3(i, -1) = swSelectType_e.swSelEDGES Then
' Set the Solidworks Entity object to selected edge
Set swObject = swSelMgr.GetSelectedObject6(i, -1)
End If
In above line of code, we use ๐ IF statement with a condition.
The ๐ IF statement is True
when selected object
is type of SOLIDWORKS Edge.
When ๐ IF statement is True we execute code given below.
' Set the Solidworks Entity object to selected edge
Set swObject = swSelMgr.GetSelectedObject6(i, -1)
In above line, we set the SOLIDWORKS Entity object to selected edge.
After For
loop, we have a call for
DoEvents
function.
Get unit Conversion factors
Now we need to get unit Conversion factors as shown in below code.
'-----------------------Unit Conversion Factors----------------------------------------
' Local variables used as Conversion Factors
Dim LengthConversionFactor As Double
Dim 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
'----------------------------------------------------------------
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.
User input for Fillet radius
Now we ask user to input Fillet Radius as shown in below code.
' Variable to store fillet radius
Dim filletRadius As Double
filletRadius = InputBox("Please enter radius of Fillet.") * LengthConversionFactor
In above code sample, we first create a variable.
- Name of variable =
filletRadius
- Type of variable =
Double
After that we show a input box to get the Fillet radius from user.
Image of input box is shown below.
After getting input value from InputBox
, we first multiply it with LengthConversionFactor
.
By doing this we set the input value to at fix with same unit system of part.
After conversion we assign the final value to filletRadius
variable.
This filletRadius
variable is our 1st
parameter value.
Preparing parameters
Now we need to prepare parameters for FeatureFillet3
method.
Please note that I found these parameter to use the way it shown
here.
If you have another way to for FeatureFillet3
method please let
me know.
' Radius array for Fillet feature
Dim radiiArray0 As Variant
In above code sample, we create a variable.
- Name of variable =
radiiArray0
- Type of variable =
Variant
' Radius variable
Dim radiis0 As Double
In above code sample, we create a variable.
- Name of variable =
radiis0
- Type of variable =
Double
' Distance array for Fillet feature
Dim dist2Array0 As Variant
In above code sample, we create a variable.
- Name of variable =
dist2Array0
- Type of variable =
Variant
' Distance variable
Dim dists20 As Double
In above code sample, we create a variable.
- Name of variable =
dists20
- Type of variable =
Double
' Conic Rhombus array for Fillet feature
Dim conicRhosArray0 As Variant
In above code sample, we create a variable.
- Name of variable =
conicRhosArray0
- Type of variable =
Variant
' Conic Rhombus variable
Dim coniRhos0 As Double
In above code sample, we create a variable.
- Name of variable =
coniRhos0
- Type of variable =
Double
' Set back array for Fillet feature
Dim setBackArray0 As Variant
In above code sample, we create a variable.
- Name of variable =
setBackArray0
- Type of variable =
Variant
' Set back variable
Dim setBacks0 As Double
In above code sample, we create a variable.
- Name of variable =
setBacks0
- Type of variable =
Double
' Point array for Fillet feature
Dim pointArray0 As Variant
In above code sample, we create a variable.
- Name of variable =
pointArray0
- Type of variable =
Variant
' Point variable
Dim points0 As Double
In above code sample, we create a variable.
- Name of variable =
points0
- Type of variable =
Double
' Point Distance array for Fillet feature
Dim pointDist2Array0 As Variant
In above code sample, we create a variable.
- Name of variable =
pointDist2Array0
- Type of variable =
Variant
' Point distance variable
Dim pointsDist20 As Double
In above code sample, we create a variable.
- Name of variable =
pointsDist20
- Type of variable =
Double
' Point Rhombus array for Fillet feature
Dim pointRhoArray0 As Variant
In above code sample, we create a variable.
- Name of variable =
pointRhoArray0
- Type of variable =
Variant
' Point Rhombus variable
Dim pointsRhos0 As Double
In above code sample, we create a variable.
- Name of variable =
pointsRhos0
- Type of variable =
Double
' Set 1st instance to respective variable
radiiArray0 = radiis0
dist2Array0 = dists20
conicRhosArray0 = coniRhos0
setBackArray0 = setBacks0
pointArray0 = points0
pointDist2Array0 = pointsDist20
pointRhoArray0 = pointsRhos0
In above line of code, we set 1st instance to respective variable.
Create Fillet feature using parameters
Now we create Fillet feature using parameters we define previously.
' Create Fillet feature
Set swFeature = swDoc.FeatureManager.FeatureFillet3(195, filletRadius, filletRadius, 0, 0, 0, 0, (radiiArray0), (dist2Array0), (conicRhosArray0), (setBackArray0), (pointArray0), (pointDist2Array0), (pointRhoArray0))
In above line of code we set the value of variable swFeature
by FeatureFillet3
method.
This FeatureFillet3
method takes
following parameters as explained:
-
Options - Feature fillet options as defined in
swFeatureFilletOptions_e
and are as follows.Member Description swFeatureFilletAsymmetric
16384 or 0x4000 swFeatureFilletAttachEdges
64 or 0x40 swFeatureFilletConstantWidth
512 or 0x200 swFeatureFilletCornerType
32 or 0x20 swFeatureFilletCurvatureContinuous
256 or 0x100; applies to face fillets only swFeatureFilletKeepFeatures
128 or 0x80 swFeatureFilletNoTrimNoAttached
1024 or 0x400; set to trim and attach fillet; do not set otherwise swFeatureFilletPropagate
1 or 0x1 swFeatureFilletPropagateFeatToParts
8192 or 0x2000 swFeatureFilletReverseFace1Dir
2048 or 0x800 swFeatureFilletReverseFace2Dir
4096 or 0x1000 swFeatureFilletUniformRadius
2 or 0x2; set for a uniform radius; do not set for multiple radii swFeatureFilletUseHelpPoint
8 or 0x8 swFeatureFilletUseTangentHoldLine
16 or 0x10 swFeatureFilletVarRadiusType
4 or 0x4; set for a straight transition; do not set for a smooth transition -
R1 - Uniform radius of the symmetric fillet.
-
R2 - Distance 2 radius of the asymmetric fillet.
-
Rho - Value that determines the conic shape of the fillet.
- Conic rho value [0.05, 0.95], if
ConicRhoType
=swFeatureFilletProfileType_e.swFeatureFilletConicRho
- Conic radius value, if
ConicRhoType
=swFeatureFilletProfileType_e.swFeatureFilletConicRadius
- Conic rho value [0.05, 0.95], if
-
Ftyp - Type of fillet as defined in
swFeatureFilletType_e
as follows:Member Description swFeatureFilletType_Face
2 swFeatureFilletType_FullRound
3 swFeatureFilletType_Simple
0 swFeatureFilletType_VariableRadius
1 -
OverflowType - Control of fillet overflowing onto adjacent surfaces as defined in
swFilletOverFlowType_e
as follows:Member Description swFilletOverFlowType_Default
0 = Default; swFilletOverFlowType_KeepEdge
1 = Edges that are overflowed by the fillet are not modified; the fillet surface is trimmed by all the adjacent edges; as a result, an additional transition fillet surface might be needed to complete the fillet swFilletOverFlowType_KeepSurface
2 = Fillet surface is either merged with the adjacent surfaces smoothly or trimmed by the adjacent surfaces; as a result, it is unlikely that an additional transition fillet surface is created -
ConicRhoType - Fillet profile type as defined in
swFeatureFilletProfileType_e
as follows:Member Description swFeatureFilletCircular
0 = Circular for symmetric fillets; elliptical for asymmetric fillets swFeatureFilletConicRadius
2 swFeatureFilletConicRho
1 swFeatureFilletConicRhoZeroChamfer
3 = Chamfer cross section - Radii - Array containing the radii for the symmetric fillet.
- Dist2Arr - Array containing the Distance 2 radii for the asymmetric fillet.
- RhoArr - Array of Rho values for the specified
ConicRhoType
for the variable radius fillet. - SetBackDistances - Array containing setback distances along the fillet edge.
- PointRadiusArray - Array containing radius control points along the length of the edge for symmetric fillets.
- PointDist2Array - Array containing Distance 2 radius control points along the length of the edge for asymmetric fillets.
- PointRhoArray - Array of Rho values for the specified
ConicRhoType
at various control points along the length of the edge.
Return Value : This FeatureFillet3
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 |
---|---|
Options | 195 (now this what I get from
recording and this works for me.) |
R1 | filletRadius |
R2 | filletRadius |
Rho | 0 |
Ftyp | 0 |
OverflowType | 0 |
ConicRhoType | 0 |
Radii | (radiiArray0) |
Dist2Arr | (dist2Array0) |
RhoArr | (conicRhosArray0) |
SetBackDistances | (setBackArray0) |
PointRadiusArray | (pointArray0) |
PointDist2Array | (pointDist2Array0) |
PointRhoArray | (pointRhoArray0) |
FeatureFillet3
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 Fillet Feature created or not
If swFeature Is Nothing Then
MsgBox ("Failed to create Fillet Feature.")
Exit Sub
End If
In above line of code, we use an ๐ IF statement to check if we able to create **Fillet 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 Fillet as shown in below image.
' 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 Fillet 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!!!