Solidworks Macro - Create Extrude/Boss
In this post, I tell you about how to create Extrude/Boss through Solidworks VBA Macros in a sketch.
In this post, I tell you about FeatureExtrusion3
method from
Solidworks FeatureManager
object.
This method is most updated method, I found in Solidworks API Help.
So use this method if you want to create a new Corner Extrude/Boss.
Code Sample
Below is the code
sample for creating
Corner Rectangle.
Option Explicit
' Creating variable for Solidworks application
Dim swApp As SldWorks.SldWorks
' Creating variable for Solidworks document
Dim swDoc As SldWorks.ModelDoc2
' Boolean Variable
Dim BoolStatus As Boolean
' Creating variable for Solidworks Sketch Manager
Dim swSketchManager As SldWorks.SketchManager
' Creating variable for Solidworks Feature
Dim swFeature As SldWorks.Feature
' Main function of our VBA program
Sub main()
' Setting Solidworks variable to Solidworks application
Set swApp = Application.SldWorks
' Creating string type variable for storing default part location
Dim defaultTemplate As String
' Setting value of this string type variable to "Default part template"
defaultTemplate = swApp.GetUserPreferenceStringValue(swUserPreferenceStringValue_e.swDefaultTemplatePart)
' Setting Solidworks document to new part document
Set swDoc = swApp.NewDocument(defaultTemplate, 0, 0, 0)
' Selecting Front Plane
BoolStatus = swDoc.Extension.SelectByID2("Front Plane", "PLANE", 0, 0, 0, False, 0, Nothing, swSelectOption_e.swSelectOptionDefault)
' Setting Sketch manager for our sketch
Set swSketchManager = swDoc.SketchManager
' Inserting a sketch into selected plane
swSketchManager.InsertSketch True
' Creating a "Variant" Variable which holds the values return by "CreateCornerRectangle" method
Dim vSketchLines As Variant
' Creating a Corner Rectangle
vSketchLines = swSketchManager.CreateCornerRectangle(0, 1, 0, 1, 0, 0)
' De-select the lines after creation
swDoc.ClearSelection2 True
' Zoom to fit screen in Solidworks Window
swDoc.ViewZoomtofit2
' Exit the Sketch
swSketchManager.InsertSketch True
' Create Extrude Feature
Set swFeature = swDoc.FeatureManager.FeatureExtrusion3(True, False, False, swEndConditions_e.swEndCondBlind, swEndConditions_e.swEndCondBlind, 2, 0, False, False, False, True, 0, 0, False, False, False, False, True, False, True, swEndConditions_e.swEndCondBlind, 0, False)
End Sub
Understanding the Code
Now let us walk through each line in the above code, and understand the meaning and purpose of every line.
I also give some link so that you can go through them if there are anything I explained in previous posts.
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.
' Create 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
.
' Create 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
.
' Boolean Variable
Dim BoolStatus As Boolean
In this line, we create a variable named BoolStatus
as Boolean
object type.
' Create variable for Solidworks Sketch Manager
Dim swSketchManager As SldWorks.SketchManager
In above line, we create variable swSketchManager
for Solidworks
Sketch Manager.
As the name suggested, a Sketch Manager holds variours methods and properties to manage Sketches.
To see methods and properties related to SketchManager
object, please visit
this
page of Solidworks API Help
' Creating variable for Solidworks 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 Feature.
To see methods and properties related to Feature
object, please visit this
page of Solidworks API Help
These all are our global variables.
As you can see in code sample, they are Solidworks API Objects.
So basically I group all the Solidworks API Objects in one place.
I have also place boolean
type object
at top also, because after certain point we will need this variable frequently.
Thus, I have started placing it here.
Next is our Sub
procedure which has
name of main
.
This procedure hold all the statements (instructions) we give to computer.
' 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.
' Create string type variable for storing default part location
Dim defaultTemplate As String
' Set value of this string type variable to "Default part template"
defaultTemplate = swApp.GetUserPreferenceStringValue(swUserPreferenceStringValue_e.swDefaultTemplatePart)
In 1st statement of above example, we are defining a variable of string
type and named it as defaultTemplate
.
This variable defaultTemplate
, hold the
location the location of Default Part Template.
In 2nd line of above example. we assign value to our newly define defaultTemplate
variable.
We assign the value by using a Method named GetUserPreferenceStringValue()
.
This GetUserPreferenceStringValue()
method is a part of our main Solidworks variable swApp
.
' Set Solidworks document to new part document
Set swDoc = swApp.NewDocument(defaultTemplate, 0, 0, 0)
In this line, we set the value of our swDoc
variable to new document.
For detailed information about these lines please visit Solidworks Macros - Open new Part document post.
I have discussed them thoroghly in Solidworks Macros - Open new Part document post, so do checkout that post if you want to understand above code in more detail.
' Select Front Plane
BoolStatus = swDoc.Extension.SelectByID2("Front Plane", "PLANE", 0, 0, 0, False, 0, Nothing, swSelectOption_e.swSelectOptionDefault)
In above line, we select the front plane by using SelectByID2
method from Extension
object.
For more information about selection method please visit Solidworks Macros - Selection Methods post.
' Set Sketch manager for our sketch
Set swSketchManager = swDoc.SketchManager
In above line, we set the Sketch manager variable to current document’s sketch manager.
' Insert a sketch into selected plane
swSketchManager.InsertSketch True
In above line, we use InsertSketch
method of SketchManager and give True
value.
This method allows us to insert a sketch in selected plane.
' Creating a "Variant" Variable which holds the values return by "CreateCornerRectangle" method
Dim vSketchLines As Variant
' Creating a Corner Rectangle
vSketchLines = swSketchManager.CreateCornerRectangle(0, 1, 0, 1, 0, 0)
In above sample code, we 1st create a variable named vSketchLines
of type Variant
.
A Variant
type variable can hold
any type of value depends upon the use of variable.
In 2nd line, we set the value of variable vSketchLines
.
In the above code sample I have used (0, 1, 0) Upper-left point in Y-direction.
For Lower-right point I used (1, 0, 0) which is 1 point distance in X-direction.
This CreateCornerRectangle
method
returns an array of sketch segments that represent the edges created for
this corner rectangle.
For more information about Create Corner Rectangle please visit Solidworks Macros - Selection Methods post.
It is very important to remember that, when you
give distance or any other numeric value in Solidworks API, Solidworks takes that
numeric value in Meter only.
Solidworks API does not care about your application’s Unit systems.
For example, I works in ANSI system means inches for distance.
But when I used Solidworks
API through VBA macros or C#, I need to use converted numeric values.
Because Solidworks API output the distance in Meter which is not my requirement.
' De-select the lines after creation
swDoc.ClearSelection2 True
In the this line of code, we deselect the Corner rectangle we have created.
For de-selecting, we use ClearSelection2
method from our Solidworks
document name swDoc
.
' Zoom to fit screen in Solidworks Window
swDoc.ViewZoomtofit2
In this last line we use zoom to fit command.
For Zoom to fit, we use ViewZoomtofit
method from our Solidworks document variable swDoc
.
' Exit the Sketch
swSketchManager.InsertSketch True
In above line, we use InsertSketch
method of SketchManager and give True
value.
This method allows us to exit a sketch in selected plane.
' Create Extrude Feature
Set swFeature = swDoc.FeatureManager.FeatureExtrusion3(True, False, False, swEndConditions_e.swEndCondBlind, swEndConditions_e.swEndCondBlind, 2, 0, False, False, False, True, 0, 0, False, False, False, False, True, True, True, swEndConditions_e.swEndCondBlind, 0, False)
In above line of code we set the value of variable swFeature
by FeatureExtrusion3
method.
FeatureExtrusion3
method is part of
FeatureManager
object.
This FeatureManager
is again part of
swDoc
variable i.e. ModelDoc2
object.
Method Parameters Details
This FeatureExtrusion3
method takes
following parameters as explained:
-
Sd -
True
for single ended,False
for double ended. -
Flip -
True
to flip the side to cutThis option activate when we select feature “Extruded Cut”.
-
Dir -
True
to flip the direction of extrusion. -
T1 - Termination type for first end of the extrusion as defined in
swEndConditions_e
.End Conditions has following enumeration values:
-
swEndCondBlind
or 0 -
swEndCondMidPlane
or 6 -
swEndCondOffsetFromSurface
or 5 -
swEndCondThroughAll
or 1 -
swEndCondThroughAllBoth
or 9 -
swEndCondThroughNext
or 2 -
swEndCondUpToBody
or 7 -
swEndCondUpToNext
or 11 -
swEndCondUpToSelection
or 10 -
swEndCondUpToSurface
or 4 = Do not use; superseded byswEndCondUpToSelection
-
swEndCondUpToVertex
or 3 = Do not use; superseded byswEndCondUpToSelection
-
-
T2 - Termination type for second end of the extrusion as defined in
swEndConditions_e
.End Conditions has enumeration values as described in T1.
-
D1 - Depth of extrusion for first end in meters; offset, if T1 is set to
swEndConditions_e.swEndCondOffsetFromSurface
-
D2 - Depth of extrusion for second end in meters; offset, if T2 is set to
swEndConditions_e.swEndCondOffsetFromSurface
-
Dchk1 -
True
to allow drafting in the first direction,False
to not -
Dchk2 -
True
to allow drafting in the second direction,False
to not -
Ddir1 -
True
for first draft angle to be inward,False
to be outward; valid only if Dchk1 is true -
Ddir2 -
True
for second draft angle to be inward,False
to be outward; valid only if Dchk2 is true -
Dang1 - Draft angle for first end; valid only if Dchk1 is true
-
Dang2 - Draft angle for second end; valid only if Dchk2 is true
-
OffsetReverse1 -
True
to offset the first end from another face or plane in a direction away from the sketch,False
to offset in a direction toward the sketch; valid only if T1 is set toswEndConditions_e.swEndCondOffsetFromSurface
-
OffsetReverse2 -
True
to offset the second end from another face or plane in a direction away from the sketch,False
to offset in a direction toward the sketch; valid only if T2 is set toswEndConditions_e.swEndCondOffsetFromSurface
-
TranslateSurface1 -
True
if the first end of the extrusion is a translation of the reference surface,False
if it has a true offset; valid only if T1 is set toswEndConditions_e.swEndCondOffsetFromSurface
-
TranslateSurface2 -
True
if the second end of the extrusion is a translation of the reference surface,False
if it has a true offset; valid only if T2 is set toswEndConditions_e.swEndCondOffsetFromSurface
-
Merge -
True
to merge the results in a multibody part,False
to notThis option visible when we Extrude from existing feature/body.
-
UseFeatScope -
True
if the feature only affects selected bodies,False
if the feature affects all bodies. -
UseAutoSelect - True to automatically select all bodies and have the feature affect those bodies,
False
to select the bodies that the feature affects. -
T0 - Start condition as defined in
swStartConditions_e
Start Conditions has following enumeration values:
-
swStartOffset
or 3 -
swStartSketchPlane
or 0 -
swStartSurface
or 1 -
swStartVertex
or 2
-
-
StartOffset - Distance from the sketch plane to start the extrude; valid only if T0 is set to
swStartConditions_e.swStartOffset
-
FlipStartOffset -
True
to flip the direction of the start offset,False
to not; valid only if T0 is set toswStartConditions_e.swStartOffset
Return Value : This FeatureExtrusion3
method retun Feature
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:
-
Sd - I use
True
for single ended. -
Flip - I use
False
to flip the side to cut -
Dir - I use
False
to flip the direction of extrusion -
T1 - *I use
swEndConditions_e.swEndCondBlind
of Termination type for first end of the extrusion * -
T2 - *I use
swEndConditions_e.swEndCondBlind
of Termination type for second end of the extrusion * -
D1 - I use
2
as *Depth of extrusion for first end in meters* -
D2 - I use
0
as *Depth of extrusion for second end in meters* -
Dchk1 - I use
False
to allow drafting in the first direction -
Dchk2 - I use
False
to allow drafting in the second direction -
Ddir1 - I use
False
for first draft angle to be inward -
Ddir2 - I use
True
for second draft angle to be inwardSince Dchk2 is
False
, setting the value toTrue
is invalid. -
Dang1 - I use
0
for first end’s draft angle -
Dang2 - I use
0
for second end’s draft angle -
OffsetReverse1 - I use
False
to offset the first end from another face or plane in a direction away from the sketch -
OffsetReverse2 - I use
False
to offset the second end from another face or plane in a direction away from the sketch -
TranslateSurface1 - I use
False
to the first end of the extrusion is a translation of the reference surface -
TranslateSurface2 - I use
False
to the second end of the extrusion is a translation of the reference surface -
Merge - I use
True
to merge the results in a multibody part -
UseFeatScope - I use
False
so that this feature affects to all bodies -
UseAutoSelect - I use
True
to automatically select all bodies and have the feature affect those bodies -
T0 - I use
swEndConditions_e.swEndCondBlind
as Start condition -
StartOffset - I use
0
for distance from the sketch plane to start the extrude -
FlipStartOffset - I use
False
to flip the direction of the start offset
After running macro we get extrude 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 create Extrude/Boss 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!!!