SOLIDWORKS Macro - Create Extrude Cut

9 minute read

Objective

In this post, I tell you about how to create Extrude Cut through SOLIDWORKS VBA Macros in a sketch.

This method is most updated method, so use this method if you want to create a new Extrude Cut.

Video of Code on YouTube

Please see below 🎬 video on how to create Extrude Cut 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 post.

Code Sample

Below is the code sample for creating Extrude Cut.

Option Explicit

' Creating variable for Solidworks application
Dim swApp As SldWorks.SldWorks
' Creating variable for Solidworks document
Dim swDoc As SldWorks.ModelDoc2
' Creating variable for Solidworks Feature
Dim swFeature As SldWorks.Feature
' Boolean Variable
Dim BoolStatus As Boolean

' Main function of our VBA program
Sub main()

  ' Setting Solidworks variable to Solidworks 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 to new 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

  ' Selecting Sketch 2
  BoolStatus = swDoc.Extension.SelectByID2("Sketch2", "SKETCH", 0, 0, 0, False, 0, Nothing, swSelectOption_e.swSelectOptionDefault)

  ' Check if failed to select sketch 2
  If BoolStatus = False Then
      MsgBox ("Failed to select sketch 2.")
      Exit Sub
  End If

  ' Create Extrude Cut Feature
  Set swFeature = swDoc.FeatureManager.FeatureCut4(True, False, False, swEndCondThroughAll, 0, 0.01, 0.01, False, False, False, False, 1, 1, False, False, False, False, False, True, True, True, True, False, swStartSketchPlane, 0, False, False)

  ' Check if Extrude Cut Feature creates or not
  If swFeature Is Nothing Then
      MsgBox ("Failed to create Extrude Cut Feature.")
      Exit Sub
  End If

End Sub

Prerequisite

In this article there are some prerequisite.

We are not creating sketches from code but we use existing sketch to create Extrude Cut feature as shown in below picture.

prerequisite

As shown in above image, there are 1 sketch and 1 Extrude feature in our part.

  • Extrude Feature : This is our extrude part for Extrude Cut feature.
  • Sketch2 : This is our profile of holes for Extrude Cut feature.

If you want to create Sketch2 programmatically then please refer to below articles.

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.

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 📌 links so that you can go through them if there are anything I explained in previous articles.

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.

' 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

' Boolean Variable
Dim BoolStatus As Boolean

In this line, we create a variable named BoolStatus as Boolean object type.

These all are our global variables.

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 we use this variable frequently.

Thus, I have started placing it here.

' Main function of our VBA 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.

' 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.

' Setting 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.

After showing message our program exit from here itself.

' Selecting Sketch 2
BoolStatus = swDoc.Extension.SelectByID2("Sketch2", "SKETCH", 0, 0, 0, False, 0, Nothing, swSelectOption_e.swSelectOptionDefault)

In above line, we select the Sketch2 by using SelectByID2 method from Extension object.

For more information about selection method please visit 👉 SOLIDWORKS Macros - Selection Methods post.

' Check if Sketch 2 selected or not
If BoolStatus = False Then
  MsgBox ("Fail to select Sketch 2.")
  Exit Sub
End If

In above line of code, we use an 👉 IF statement to check if Sketch 2 is selected or not .

If Sketch 2 not selected then we execute code inside the 👉 IF statement and inform the user by a 👉 Message Window.

After showing message our program exit from here itself.

' Create Extrude Cut Feature
Set swFeature = swDoc.FeatureManager.FeatureCut4(True, False, False, swEndCondThroughAll, 0, 0.01, 0.01, False, False, False, False, 1, 1, False, False, False, False, False, True, True, True, True, False, swStartSketchPlane, 0, False, False)

In above line of code we create a Extrude Cut feature.

We create Extrude Cut by setting the value of variable swFeature by FeatureCut4 method.

FeatureCut4 method is part of FeatureManager object.

This FeatureManager is again part of ModelDoc2 object.

FeatureCut4 Method Parameters Details

This FeatureCut4 takes following parameters as explained:

  • Sd - True for a single-ended cut, False for a doubled-ended cut.

  • Flip- True to remove material outside of the profile of the flip side to cut, False to not.

  • Dir - True for Direction 1 to be opposite of the default direction.

  • T1- Termination type for the first end as defined in swEndConditions_e.

    Member Description
    swEndCondBlind 0
    swEndCondMidPlane 6
    swEndCondOffsetFromSurface 5
    swEndCondThroughAll 1
    swEndCondThroughAllBoth 9
    swEndCondThroughNext 2
    swEndCondUpToBody 7
    swEndCondUpToNext 11
    swEndCondUpToSelection 10
    swEndCondUpToSurface 4 = Do not use;
    swEndCondUpToVertex 3 = Do not use;
  • T2- Termination type for the second end as defined in swEndConditions_e.

    Member Description
    swEndCondBlind 0
    swEndCondMidPlane 6
    swEndCondOffsetFromSurface 5
    swEndCondThroughAll 1
    swEndCondThroughAllBoth 9
    swEndCondThroughNext 2
    swEndCondUpToBody 7
    swEndCondUpToNext 11
    swEndCondUpToSelection 10
    swEndCondUpToSurface 4 = Do not use;
    swEndCondUpToVertex 3 = Do not use;
  • D1 - Depth of extrusion for the first end in meters.

  • D2 - Depth of extrusion for the second end in meters.

  • Dchk1 - True allows a draft angle in the first direction, False does not allow drafting in the first direction.

  • Dchk2 - True allows a draft angle in the second direction, False does not allow drafting in the second direction

  • Ddir1 - True for the first draft angle to be inward, False to be outward; only valid when Dchk1 is true.

  • Ddir2 - True for the second draft angle to be inward, False to be outward; only valid when Dchk2 is true.

  • Dang1 - Draft angle for the first end; only valid when Dchk1 is True.

  • Dang2 - Draft angle for the second end; only valid when Dchk2 is True.

  • OffsetReverse1 - True to specifies offset in direction away from the sketch, False specifies offset from the face or plane in a direction toward the sketch.

  • OffsetReverse2 - True to specifies offset in direction away from the sketch, False specifies offset from the face or plane in a direction toward the sketch.

  • TranslateSurface1 - True specifies that the end of the extrusion is a translation of the reference surface, False specifies to use a true offset.

  • TranslateSurface2 - True specifies that the end of the extrusion is a translation of the reference surface, False specifies to use a true offset.

  • NormalCut - True to create the cut normal to the sheet metal thickness, False to not.

  • UseFeatScope - True if the feature only affects selected bodies or components, False if the feature affects all bodies or components.

  • UseAutoSelect - True to automatically select all bodies or components and have the feature affect those bodies or components, False to only select the bodies or components the feature affects.

  • AssemblyFeatureScope - True if the assembly feature only affects selected components in the assembly, False if the assembly feature affects all components in the assembly.

  • AutoSelectComponents - True to automatically select all affected components, False to use only the selected components.

  • PropagateFeatureToParts - True to propagate the assembly feature to the components in the model that it affects, False to not.

  • T0 - Start conditions as defined in swStartConditions_e.

    Member Description
    swStartOffset 3
    swStartSketchPlane 0
    swStartSurface 1
    swStartVertex 2
  • StartOffset - If T0 is swStartConditions_e.swStartOffset, then specify an offset value.
  • FlipStartOffset - If T0 is swStartConditions_e.swStartOffset, then True to flip the direction of cut, False to not.
  • OptimizeGeometry - True to optimize the normal cut in a sheet metal part, False to not; only valid for sheet metal parts and when NormalCut is true.

Return Value : This InsertProtrusionBlend2 method return feature data object.

In our code, I have used following values:

Parameter Name Value Used
Sd True
Flip False
Dir False
T1 swEndCondThroughAll
T2 0
D1 0.01
D2 0.01
Dchk1 False
Dchk2 False
Ddir1 False
Ddir2 False
Dang1 1
Dang2 1
OffsetReverse1 False
OffsetReverse2 False
TranslateSurface1 False
TranslateSurface2 False
NormalCut False
UseFeatScope True
UseAutoSelect True
AssemblyFeatureScope True
AutoSelectComponents True
PropagateFeatureToParts False
T0 swStartSketchPlane
StartOffset 0
FlipStartOffset False
OptimizeGeometry False

To see methods and properties related to FeatureManager object, please visit 👉 this page of SOLIDWORKS API Help.

' Check if Extrude Cut Feature creates or not
If swFeature Is Nothing Then
    MsgBox ("Failed to create Extrude Cut Feature.")
    Exit Sub
End If

In above line of code, we use an 👉 IF statement to check if we able to create Extrude Cut 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 extrude as shown in below image.

extrude-cut-feature-final-result

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 Cut 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!!!

Updated: