Solidworks Macro - Create Revolved Boss/Base

12 minute read

Objective

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

In this post, I tell you about FeatureRevolve2 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 Revolved Boss/Base.

Video of Code on YouTube

Please see below video on how to create Revolved Boss/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 Revolved Boss/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 Feature
Dim swFeature As SldWorks.Feature
' Boolean Variable
Dim boolStatus As Boolean

' Revolve 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
  
  '-----------------------BELOW IS THE SOLUTION----------------------------------------

  ' Local variables used as Conversion Factors
  Dim LengthConversionFactor As Double
  Dim AngleConversionFactor As Double

  ' Use a Select Case, to get the length of active Unit and set the different factors
  Select Case swDoc.GetUnits(0)       ' GetUnits function gives us, active unit
    
    Case swMETER    ' If length is in Meter
      LengthConversionFactor = 1
      AngleConversionFactor = 1
    
    Case swMM       ' If length is in MM
      LengthConversionFactor = 1 / 1000
      AngleConversionFactor = 1 * 0.01745329
    
    Case swCM       ' If length is in CM
      LengthConversionFactor = 1 / 100
      AngleConversionFactor = 1 * 0.01745329
    
    Case swINCHES   ' If length is in INCHES
      LengthConversionFactor = 1 * 0.0254
      AngleConversionFactor = 1 * 0.01745329
    
    Case swFEET     ' If length is in FEET
      LengthConversionFactor = 1 * (0.0254 * 12)
      AngleConversionFactor = 1 * 0.01745329
    
    Case swFEETINCHES     ' If length is in FEET & INCHES
      LengthConversionFactor = 1 * 0.0254  ' For length we use sama as Inch
      AngleConversionFactor = 1 * 0.01745329
    
    Case swANGSTROM        ' If length is in ANGSTROM
      LengthConversionFactor = 1 / 10000000000#
      AngleConversionFactor = 1 * 0.01745329
    
    Case swNANOMETER       ' If length is in NANOMETER
      LengthConversionFactor = 1 / 1000000000
      AngleConversionFactor = 1 * 0.01745329
    
    Case swMICRON       ' If length is in MICRON
      LengthConversionFactor = 1 / 1000000
      AngleConversionFactor = 1 * 0.01745329
  End Select
  
  '----------------------------------------------------------------
  
  ' Select Sketch 1 in opened part document
  boolStatus = swDoc.Extension.SelectByID2("Sketch1", "SKETCH", 0, 0, 0, True, 0, Nothing, swSelectOption_e.swSelectOptionDefault)
  
  ' Check if failed to select sketch 1
  If boolStatus = False Then
      MsgBox ("Failed to select sketch 1.")
      Exit Sub
  End If    
  
  ' Create Revolve Feature
  Set swFeature = swDoc.FeatureManager.FeatureRevolve2(True, True, False, False, False, True, swEndConditions_e.swEndCondBlind, swEndConditions_e.swEndCondBlind, AngleConversionFactor * 360, 0, False, False, 0, 0, swThinWallType_e.swThinWallOneDirection, 0, 0, True, False, True)
  
  ' Check if Revolve Feature creates or not
  If swFeature Is Nothing Then
      MsgBox ("Failed to create Revolve 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 Revolve feature.

Also, we will apply checks in this article, so that code we write should be error free.

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.

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.

'-----------------------BELOW IS THE SOLUTION----------------------------------------

' Local variables used as Conversion Factors
Dim LengthConversionFactor As Double
Dim AngleConversionFactor As Double

' Use a Select Case, to get the length of active Unit and set the different factors
Select Case swDoc.GetUnits(0)       ' GetUnits function gives us, active unit
  
  Case swMETER    ' If length is in Meter
    LengthConversionFactor = 1
    AngleConversionFactor = 1
  
  Case swMM       ' If length is in MM
    LengthConversionFactor = 1 / 1000
    AngleConversionFactor = 1 * 0.01745329
  
  Case swCM       ' If length is in CM
    LengthConversionFactor = 1 / 100
    AngleConversionFactor = 1 * 0.01745329
  
  Case swINCHES   ' If length is in INCHES
    LengthConversionFactor = 1 * 0.0254
    AngleConversionFactor = 1 * 0.01745329
  
  Case swFEET     ' If length is in FEET
    LengthConversionFactor = 1 * (0.0254 * 12)
    AngleConversionFactor = 1 * 0.01745329
  
  Case swFEETINCHES     ' If length is in FEET & INCHES
    LengthConversionFactor = 1 * 0.0254  ' For length we use sama as Inch
    AngleConversionFactor = 1 * 0.01745329
  
  Case swANGSTROM        ' If length is in ANGSTROM
    LengthConversionFactor = 1 / 10000000000#
    AngleConversionFactor = 1 * 0.01745329
  
  Case swNANOMETER       ' If length is in NANOMETER
    LengthConversionFactor = 1 / 1000000000
    AngleConversionFactor = 1 * 0.01745329
  
  Case swMICRON       ' If length is in MICRON
    LengthConversionFactor = 1 / 1000000
    AngleConversionFactor = 1 * 0.01745329
End Select

'----------------------------------------------------------------

In above line of code, we get and update unit conversion variable as per document unit.

For more detail about why we are using this code sample, please visit (Solidworks Macro - Fix Unit Issue)[/solidworks-macros/unit-correction/] article.

In this line, we set the value of our variable to new document.

For detailed information about these lines please visit Solidworks Macros - Open new Part document post.

' Select Sketch 1 in opened part document
boolStatus = swDoc.Extension.SelectByID2("Sketch1", "SKETCH", 0, 0, 0, True, 0, Nothing, swSelectOption_e.swSelectOptionDefault)

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

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

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

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

' Create Revolve Feature
Set swFeature = swDoc.FeatureManager.FeatureRevolve2(True, True, False, False, False, True, swEndConditions_e.swEndCondBlind, swEndConditions_e.swEndCondBlind, AngleConversionFactor * 360, 0, False, False, 0, 0, swThinWallType_e.swThinWallOneDirection, 0, 0, True, False, True)

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

FeatureRevolve2 method is part of FeatureManager object.

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

Method Parameters Details

This FeatureRevolve2 method takes following parameters as explained:

  • SingleDir - True if the revolve is in one direction, False if in both directions.

  • IsSolid - True if this is a Solid revolve feature, False if not.

  • IsThin - True if this is a Thin revolve feature, False if not.

  • IsCut - True if this is a Cut revolve feature, False if not.

  • ReverseDir - True reverses the angle of the revolution, False does not; only applies if Dir1Type is not swEndConditions_e.swEndCondMidPlane.

  • BothDirectionUpToSameEntity - True if the revolve is up to the same entity in both directions, False if not; only applies if SingleDir is False and Dir1Type and Dir2Type are swEndConditions_e.swEndCondUpToVertex, swEndConditions_e.swEndCondUpToSurface, or swEndConditions_e.swEndCondOffsetFromSurface).

  • Dir1Type - Revolve end condition in direction 1 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 by `swEndCondUpToSelection`

  - `swEndCondUpToVertex` or **3** = Do not use; superseded by `swEndCondUpToSelection`
  • Dir2Type - Revolve end condition in direction 2; as defined in swEndConditions_e and only applies if Dir1Type is not swEndConditions_e.swEndCondMidPlane.

  • Dir1Angle - Angle in radians of revolution in direction 1; only applies if Dir1Type is swEndConditions_e.swEndCondBlind.

  • Dir2Angle - Angle in radians of revolution in direction 2; only applies if Dir2Type is swEndConditions_e.swEndCondBlind.

  • OffsetReverse1 - True to reverse the offset direction in direction 1, False to not; only applies if Dir1Type is swEndConditions_e.swEndCondOffsetFromSurface.

  • OffsetReverse2 - True to reverse the offset direction in direction 2, False to not; only applies if Dir2Type is swEndConditions_e.swEndCondOffsetFromSurface.

  • OffsetDistance1 - Offset distance in direction 1; only applies if Dir1Type is swEndConditions_e.swEndCondOffsetFromSurface.

  • OffsetDistance2 - Offset distance in direction 2; only applies if Dir2Type is swEndConditions_e.swEndCondOffsetFromSurface.

  • OffsetDistance2 - Offset distance in direction 2; only applies if Dir2Type is swEndConditions_e.swEndCondOffsetFromSurface.

  • ThinType - Type and direction as defined in swThinWallType_e.

Thin wall types has following enumeration values:

  - `swThinWallMidPlane` or **2**

  - `swThinWallOneDirection` or **0**

  - `swThinWallOppDirection` or **1**

  - `swThinWallTwoDirection` or **3**
  • ThinThickness1 - Wall thickness in direction 1 (if ThinType is swThinWallType_e.swThinWallMidPlane, (ThinThickness1)/2 is used for each direction).

  • ThinThickness2 - Wall thickness in direction 2 (only applies if ThinType is swThinWallType_e.swThinWallTwoDirection).

  • Merge - True to merge the results into a multi-body part, False to 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 or components that the feature affects.

Return Value : This FeatureRevolve2 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:

  • SingleDir - I use True for revolve is in one direction.

  • IsSolid - I use True for Solid revolve feature.

  • IsThin - I use False for Thin revolve feature.

  • IsCut - I use False for Cut revolve feature.

  • ReverseDir - I use False for reverses the angle of the revolution.

  • BothDirectionUpToSameEntity - I use True for revolve is up to the same entity in both directions.

    Since SingleDir is True, setting the value to True is invalid.

  • Dir1Type - I use swEndConditions_e.swEndCondBlind for Revolve end condition in direction 1.

  • Dir2Type - I use swEndConditions_e.swEndCondBlind for Revolve end condition in direction 2.

  • Dir1Angle - I use AngleConversionFactor * 360 for Angle in radians of revolution in direction 1.

  • Dir2Angle - I use 0 for Angle in radians of revolution in direction 2.

  • OffsetReverse1 - I use False for reverse the offset direction in direction 1.

  • OffsetReverse2 - I use False for reverse the offset direction in direction 2.

  • OffsetDistance1 - I use 0 for Offset distance in direction 1.

  • OffsetDistance2 - I use 0 for Offset distance in direction 2.

  • ThinType - I use swThinWallType_e.swThinWallOneDirection for Type and direction.

  • ThinThickness1 - I use 0 for Wall thickness in direction 1.

  • ThinThickness2 - I use 0 for Wall thickness in direction 2.

  • Merge - I use True for merge the results into a multi-body part.

  • UseFeatScope - I use False for the feature affects all bodies.

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

' Check if Revolve Feature creates or not
If swFeature Is Nothing Then
    MsgBox ("Failed to create Revolve 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.

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

final_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 Revolve 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!!!