Solidworks Macro - Create Lofted Base/Boss

12 minute read

Objective

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

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

Video of Code on YouTube

Please see below video on how to create Lofted Base/Boss 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 Lofted Base/Boss.

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 Feature
Dim swFeature As SldWorks.Feature
' Boolean Variable
Dim boolStatus As Boolean

' Lofted Boss/Extrude 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 3 in opened part document
  boolStatus = swDoc.Extension.SelectByID2("Sketch3", "SKETCH", 0, 0, 0, False, 1, Nothing, 0)
  
  ' Check if select Sketch 3 selected or not
  If boolStatus = False Then
    MsgBox ("Fail to select Sketch 3 as profile for Loft.")
    Exit Sub
  End If
  
  ' Select Sketch 2 in opened part document
  boolStatus = swDoc.Extension.SelectByID2("Sketch2", "SKETCH", 0, 0, 0, True, 1, Nothing, 0)
  
  ' Check if select Sketch 2 selected or not
  If boolStatus = False Then
    MsgBox ("Fail to select Sketch 2 as profile for Loft.")
    Exit Sub
  End If
  
  ' Select Sketch 1 in opened part document
  boolStatus = swDoc.Extension.SelectByID2("Sketch1", "SKETCH", 0, 0, 0, True, 1, Nothing, 0)
  
  ' Check if select Sketch 1 selected or not
  If boolStatus = False Then
    MsgBox ("Fail to select Sketch 1 as profile for Loft.")
    Exit Sub
  End If
  
  ' Select 3DSketch1 in opened part document
  boolStatus = swDoc.Extension.SelectByID2("3DSketch1", "SKETCH", 0, 0, 0, True, 2, Nothing, 0)
  
  ' Check if select 3DSketch1 selected or not
  If boolStatus = False Then
    MsgBox ("Fail to select 3DSketch1 as Guide curve for Loft.")
    Exit Sub
  End If
  
  ' Create Lofted Boss feature
  Set swFeature = swDoc.FeatureManager.InsertProtrusionBlend2(False, True, True, 0, 0, 0, 0, 0, True, True, False, 0, 0, 0, True, True, True, swGuideCurveInfluence_e.swGuideCurveInfluenceNextGlobal)

  ' Check if Loft boss feature is created or not
  If swFeature Is Nothing Then
    MsgBox ("Failed to create Lofted boss.")
    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 Lofted Base/Boss feature as shown in below picture.

sketch-to-use-for-lofted-bose-feature

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

  1. Sketch1 : This is our profile 1 for Lofted feature.

  2. Sketch2 : This is our profile 2 for Lofted feature.

  3. Sketch3 : This is our profile 3 for Lofted feature.

  4. 3DSketch1 : This is our Guide Curve for Lofted feature.

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

For Circle 👉 read Solidworks Macros - Create Circle article.

For CenterPoint Arc 👉 read Solidworks Macros - Create Center Rectangle 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

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

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 3 as profile in opened part document
boolStatus = swDoc.Extension.SelectByID2("Sketch3", "SKETCH", 0, 0, 0, True, 1, Nothing, swSelectOption_e.swSelectOptionDefault)

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

Please note that for selecting profile we need to Mark the selected entity as 1.

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

' Check if Sketch 3 selected or not
If boolStatus = False Then
  MsgBox ("Fail to select Sketch 3 as profile for Loft.")
  Exit Sub
End If

In above line of code, we use an 👉 IF statement to check if Solidworks document swDoc is opened.

If Sketch 3 selected or not 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 2 as profile in opened part document
boolStatus = swDoc.Extension.SelectByID2("Sketch2", "SKETCH", 0, 0, 0, True, 1, Nothing, swSelectOption_e.swSelectOptionDefault)

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

Please note that for selecting profile we need to Mark the selected entity as 1.

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 as profile for Loft.")
  Exit Sub
End If

In above line of code, we use an 👉 IF statement to check if Solidworks document swDoc is opened.

If Sketch 2 selected or not 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 in opened part document
boolStatus = swDoc.Extension.SelectByID2("Sketch1", "SKETCH", 0, 0, 0, True, 1, Nothing, swSelectOption_e.swSelectOptionDefault)

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

Please note that for selecting profile we need to Mark the selected entity as 1.

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

' Check if Sketch 1 selected or not
If boolStatus = False Then
  MsgBox ("Fail to select Sketch 1 as profile for Loft.")
  Exit Sub
End If

In above line of code, we use an 👉 IF statement to check if Solidworks document swDoc is opened.

If Sketch 1 selected or not then code execute inside the code and inform the user by a 👉 Message Window.

After showing message our program exit from here itself.

' Select 3DSketch1 as Guide curve in opened part document
boolStatus = swDoc.Extension.SelectByID2("3DSketch1", "SKETCH", 0, 0, 0, True, 2, Nothing, swSelectOption_e.swSelectOptionDefault)

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

Please note that for selecting profile we need to Mark the selected entity as 2.

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

' Check if 3DSketch1 selected or not
If boolStatus = False Then
  MsgBox ("Fail to select 3DSketch1 as Guide curve for Loft.")
  Exit Sub
End If

In above line of code, we use an 👉 IF statement to check if Solidworks document swDoc is opened.

If 3DSketch1 selected or not then code execute inside the code and inform the user by a 👉 Message Window.

After showing message our program exit from here itself.

' Create Lofted feature
Set swFeature = swDoc.FeatureManager.InsertProtrusionBlend2(False, True, True, 0, 0, 0, 0, 0, True, True, False, 0, 0, 0, True, True, True, swGuideCurveInfluence_e.swGuideCurveInfluenceNextGlobal)

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

InsertProtrusionBlend2 method is part of FeatureManager object.

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

InsertProtrusionBlend2 Method Parameters Details

This InsertProtrusionBlend2 method takes following parameters as explained:

  • Closed - True closes the loft, False leaves the loft open. If True and less than three profiles are selected, then any selected guide curves must be closed curves.

  • KeepTangency - True maintains the tangency as seen in the section curves, False does not.

  • ForceNonRational - True obtains smoother surfaces, False does not.

  • TessToleranceFactor - Factor that controls the number of intermediate sections used for loft with centerline; the default value is 1.0; the greater the value, the more intermediate sections are created.

  • StartMatchingType - Tangency type at the start profile as follows:

    • 0 = none.
    • 1 = tangent to the normal of the profile.
    • 2 = tangent to a selected vector.
    • 3 = tangency to all the adjacent faces sharing an edge with the start profile.
    • 4 = tangent to some of the selected faces sharing an edge with the start profile (not available).
  • EndMatchingType - Tangency type at the end profile as follows:

    • 0 = none.
    • 1 = tangent to the normal of the profile.
    • 2 = tangent to a selected vector.
    • 3 = tangency to all the adjacent faces sharing an edge with the start profile.
    • 4 = tangent to some of the selected faces sharing an edge with the start profile (not available).
  • StartTangentLength - Start tangent length.

  • EndTangentLength - End tangent length.

  • StartTangentDir - True is one direction, False is the opposite.

  • EndTangentDir - True is one direction, False is the opposite.

  • IsThinBody - True if this feature is a thin body, False if it is not.

  • Thickness1 - Thickness value for the first direction.

  • Thickness2 - Thickness value for the second direction.

  • ThinType - Thin wall type :

    • 0 = One direction
    • 1 = One direction reverse
    • 2 = Mid-plane
    • 3 = Two direction
  • Merge - True merges the results in a multibody part, False does not.

  • 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 the feature affects.

  • GuideCurveInfluence - Guide curves influence as defined in swGuideCurveInfluence_e

    • 0 = swGuideCurveInfluenceNextGuide
    • 1 = swGuideCurveInfluenceNextSharp
    • 2 = swGuideCurveInfluenceNextEdge
    • 3 = swGuideCurveInfluenceNextGlobal

Return Value : This InsertProtrusionBlend2 method return feature data object.

In our code, I have used following values:

  • Closed - I use False to leave the loft open.

  • KeepTangency - I use True to maintains the tangency.

  • ForceNonRational - I use True to obtains smoother surfaces.

  • TessToleranceFactor - I use 0 number of intermediate sections used for loft with centerline.

  • StartMatchingType - I use 0 at the start profile.

  • EndMatchingType - I use 0 at the start profile.

  • StartTangentLength - I use 0 at the start tangent length.

  • EndTangentLength - I use 0 at the end tangent length.

  • StartTangentDir - I use True in one direction.

  • EndTangentDir - I use True in one direction.

  • IsThinBody - I use False for this feature is a thin body.

  • Thickness1 - I use 0 for the first direction.

  • Thickness2 - I use 0 for the second direction.

  • ThinType - I use 0 for Thin wall type.

  • Merge - I use True for merges the results in a multibody part.

  • UseFeatScope - I use True for the feature only affects selected bodies.

  • UseAutoSelect - I use True for automatically select all bodies and have the feature affect those bodies.

  • GuideCurveInfluence - I use swGuideCurveInfluence_e.swGuideCurveInfluenceNextGlobal for Guide curves influence.

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

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

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

loft-boss-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 Lofted Base/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!!!