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 -
Truefor single ended,Falsefor double ended. -
Flip -
Trueto flip the side to cutThis option activate when we select feature “Extruded Cut”.
-
Dir -
Trueto 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:
-
swEndCondBlindor 0 -
swEndCondMidPlaneor 6 -
swEndCondOffsetFromSurfaceor 5 -
swEndCondThroughAllor 1 -
swEndCondThroughAllBothor 9 -
swEndCondThroughNextor 2 -
swEndCondUpToBodyor 7 -
swEndCondUpToNextor 11 -
swEndCondUpToSelectionor 10 -
swEndCondUpToSurfaceor 4 = Do not use; superseded byswEndCondUpToSelection -
swEndCondUpToVertexor 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 -
Trueto allow drafting in the first direction,Falseto not -
Dchk2 -
Trueto allow drafting in the second direction,Falseto not -
Ddir1 -
Truefor first draft angle to be inward,Falseto be outward; valid only if Dchk1 is true -
Ddir2 -
Truefor second draft angle to be inward,Falseto 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 -
Trueto offset the first end from another face or plane in a direction away from the sketch,Falseto offset in a direction toward the sketch; valid only if T1 is set toswEndConditions_e.swEndCondOffsetFromSurface -
OffsetReverse2 -
Trueto offset the second end from another face or plane in a direction away from the sketch,Falseto offset in a direction toward the sketch; valid only if T2 is set toswEndConditions_e.swEndCondOffsetFromSurface -
TranslateSurface1 -
Trueif the first end of the extrusion is a translation of the reference surface,Falseif it has a true offset; valid only if T1 is set toswEndConditions_e.swEndCondOffsetFromSurface -
TranslateSurface2 -
Trueif the second end of the extrusion is a translation of the reference surface,Falseif it has a true offset; valid only if T2 is set toswEndConditions_e.swEndCondOffsetFromSurface -
Merge -
Trueto merge the results in a multibody part,Falseto notThis option visible when we Extrude from existing feature/body.
-
UseFeatScope -
Trueif the feature only affects selected bodies,Falseif the feature affects all bodies. -
UseAutoSelect - True to automatically select all bodies and have the feature affect those bodies,
Falseto select the bodies that the feature affects. -
T0 - Start condition as defined in
swStartConditions_eStart Conditions has following enumeration values:
-
swStartOffsetor 3 -
swStartSketchPlaneor 0 -
swStartSurfaceor 1 -
swStartVertexor 2
-
-
StartOffset - Distance from the sketch plane to start the extrude; valid only if T0 is set to
swStartConditions_e.swStartOffset -
FlipStartOffset -
Trueto flip the direction of the start offset,Falseto 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
Truefor single ended. -
Flip - I use
Falseto flip the side to cut -
Dir - I use
Falseto flip the direction of extrusion -
T1 - *I use
swEndConditions_e.swEndCondBlindof Termination type for first end of the extrusion * -
T2 - *I use
swEndConditions_e.swEndCondBlindof Termination type for second end of the extrusion * -
D1 - I use
2as *Depth of extrusion for first end in meters* -
D2 - I use
0as *Depth of extrusion for second end in meters* -
Dchk1 - I use
Falseto allow drafting in the first direction -
Dchk2 - I use
Falseto allow drafting in the second direction -
Ddir1 - I use
Falsefor first draft angle to be inward -
Ddir2 - I use
Truefor second draft angle to be inwardSince Dchk2 is
False, setting the value toTrueis invalid. -
Dang1 - I use
0for first end’s draft angle -
Dang2 - I use
0for second end’s draft angle -
OffsetReverse1 - I use
Falseto offset the first end from another face or plane in a direction away from the sketch -
OffsetReverse2 - I use
Falseto offset the second end from another face or plane in a direction away from the sketch -
TranslateSurface1 - I use
Falseto the first end of the extrusion is a translation of the reference surface -
TranslateSurface2 - I use
Falseto the second end of the extrusion is a translation of the reference surface -
Merge - I use
Trueto merge the results in a multibody part -
UseFeatScope - I use
Falseso that this feature affects to all bodies -
UseAutoSelect - I use
Trueto automatically select all bodies and have the feature affect those bodies -
T0 - I use
swEndConditions_e.swEndCondBlindas Start condition -
StartOffset - I use
0for distance from the sketch plane to start the extrude -
FlipStartOffset - I use
Falseto 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!!!