Solidworks Macro - Create Sweep Base

8 minute read

Objective

In this post, I tell you about how to create Sweep Base through Solidworks VBA Macros in a sketch.

From Solidworks 2018, creating Sweep feature method is changed.

It is now done through creating Feature Definition and create feature from this Feature Definition.

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

Video of Code on YouTube

Please see below video on how to create Sweep Base from Solidworks VBA Macros.


Please note that there are no explaination in the video.

Explaination of each line and why we write code this way is given in this post.

Code Sample

Below is the code sample for creating Sweep Base.

Option Explicit

' Create variable for Solidworks application
Dim swApp As SldWorks.SldWorks
' Create variable for Solidworks document
Dim swDoc As SldWorks.ModelDoc2
' Creating variable for Solidworks Sweep Feature
Dim swFeature As SldWorks.Feature
' Creating variable for Solidworks Sweep Feature data
Dim swSweep As SldWorks.SweepFeatureData
' Boolean Variable
Dim boolStatus As Boolean

' Sweep Base 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
   
  ' Select Sketch 1 as profile and Sketch 2 as path in opened part document
  boolStatus = swDoc.Extension.SelectByID2("Sketch1", "SKETCH", 0, 0, 0, True, 1, Nothing, swSelectOption_e.swSelectOptionDefault)
  boolStatus = swDoc.Extension.SelectByID2("Sketch2", "SKETCH", 0, 0, 0, True, 4, Nothing, swSelectOption_e.swSelectOptionDefault)
  
  ' Create feature definition
  Set swSweep = swDoc.FeatureManager.CreateDefinition(swFmSweep)
  
  ' Create sweep feature
  Set swFeature = swDoc.FeatureManager.CreateFeature(swSweep)

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

  ' Zoom to fit
  swDoc.ViewZoomtofit2
    
End Sub

Prerequisite

In this article there are some prerequisite.

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

sketches-to-use-for-sweep-feature

As shown in above image, there are 2 sketch in our part.

  1. Sketch1 : This is our profile for Sweep feture.

  2. Sketch2 : This is our path for Sweep feture.

If you want to create these 2 sketch programatically then please refer to below articles.

For Circle 👉 read Solidworks Macros - Create Circle article.

For Centerpoint Arc 👉 read Solidworks Macros - Create Centerpoint Arc article.

Also, we will apply checks in this article, so that 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 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.

' 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

' Creating variable for Solidworks Sweep Feature data
Dim swSweep As SldWorks.SweepFeatureData

In this line, we Create a variable which we named as swSweep and the type of this swSweep variable is SldWorks.SweepFeatureData.

We create swSweep variable for Solidworks Sweep feature data before we create Sweep feature using CreateFeature method from Solidworks Feature Manager.

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

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.

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

' Select Sketch 1 as profile and Sketch 2 as path in opened part document
boolStatus = swDoc.Extension.SelectByID2("Sketch1", "SKETCH", 0, 0, 0, True, 1, Nothing, swSelectOption_e.swSelectOptionDefault)
boolStatus = swDoc.Extension.SelectByID2("Sketch2", "SKETCH", 0, 0, 0, True, 4, Nothing, swSelectOption_e.swSelectOptionDefault)

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

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

❗ Please note that we need to define profile and path for Sweep feature, hence while selecting sketches we need to define which sketch is profile and path as describe below.

  • Profile

    • If a sketch profile, use Mark = 1 to select a face, edge, or curve. For a swept-boss feature, the sketch profile must be closed ❗. For a swept-surface feature, the sketch profile is open or closed ❗.

    • If a circular profile, use Mark = 4 to select a sketch line, edge or curve. The circular profile is open or closed ❗.

    • If a solid profile, use Mark = 1 to select the tool body to use to make the cut and use Mark = 2048 to select the solid body to cut ❗. Solid profiles are used only in swept-cut features.

  • Sweep path using Mark = 4

' Create Sweep feature definition
Set swSweep = swDoc.FeatureManager.CreateDefinition(swFmSweep)

In above line of code we set the value of variable swSweep by CreateDefinition method.

CreateDefinition method is part of FeatureManager object.

This FeatureManager is again part of swDoc variable i.e. ModelDoc2 object.

Feature Definition Method CreateDefinition Parameters Details

This CreateDefinition method takes following parameters as explained:

  • Type - Feature name ID as defined in swFeatureNameID_e.

    • swFmBoundingBox (bounding box)
    • swFmCirPattern (circular pattern)
    • swFmCurvePattern (curve-driven pattern)
    • swFmDerivedLPattern (derived-driven pattern)
    • swFmDimPattern (variable/dimension pattern)
    • swFmFillPattern (fill pattern)
    • swFmGroundPlane (ground plane)
    • swFmLibraryFeature (library)
    • swFmLocalChainPattern (chain component pattern)
    • swFmLocalCirPattern (circular component pattern)
    • swFmLocalCurvePattern (curve-driven component pattern)
    • swFmLocalLPattern (linear component pattern)
    • swFmLocalSketchPattern (sketch-driven component pattern)
    • swFmLPattern (linear pattern)
    • swFmNormalCut (sheet metal normal cut)
    • swFmRefCurve (projection curve)
    • swFmRefSurface (surface sweep)
    • swFmSketchPattern (sketch-driven pattern)
    • swFmSweep (boss sweep)
    • swFmSweepCut (cut sweep)
    • swFmSweepThread (sweep thread)
    • swFmTabAndSlot (tab and slot)
    • swFmTablePattern (table pattern)

Return Value : This CreateDefinition method retun feature or pattern-specific feature data object.

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

In our code, I have used following values:

  • Type - I use swFmSweep as Feature name ID.

If you want to know more information about Sweep Feature data then please visit this page of Solidworks API Help.

This page will give you information about various properties and methods of ISweepFeatureData Interface.

' Create sweep feature
Set swFeature = swDoc.FeatureManager.CreateFeature(swSweep)

In above line of code we set the value of variable swFeature by CreateFeature method.

CreateFeature method is part of FeatureManager object.

This FeatureManager is again part of swDoc variable i.e. ModelDoc2 object.

Feature Creation Method CreateFeature Parameters Details

This CreateFeature method takes following parameters as explained:

  • FeatureData - Feature or pattern-specific feature data object.

Return Value : This CreateFeature method retun feature data object.

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

In our code, I have used following values:

  • FeatureData - I use swFmSweep as feature data object which we defined prevously.
' Check if Sweep Feature creates or not
If swFeature Is Nothing Then
    MsgBox ("Failed to create Sweep Feature.")
    Exit Sub
End If

In above line of code, we use an IF statement to check if we able to create Revolve 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.

' Zoom to fit
swDoc.ViewZoomtofit2

In above line of code, ViewZoomtofit2 property to Zoom to fit current view.

Now we run the macro and after running macro we get extrude as shown in below image.

sweep-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 Sweep Base 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!!!