SOLIDWORKS Macro - Create Revolve Cut

11 minute read

Objective

In this post, I tell you about how to create Revolve 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 Revolve Cut.

Video of Code on YouTube

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

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 2 in opened part document
    boolStatus = swDoc.Extension.SelectByID2("Sketch2", "SKETCH", 0, 0, 0, True, 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 Revolve Cut Feature
    Set swFeature = swDoc.FeatureManager.FeatureRevolve2(True, True, False, True, 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 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 Revolve Cut feature as shown in below picture.

prerequisite

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

  • Extrude Feature : This is our Extrude part for Revolve Cut feature.
  • Sketch2 : This is our profile for Revolve Cut feature.

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

If you want to create Revolve 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.

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

' 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 Revolve Cut Feature
Set swFeature = swDoc.FeatureManager.FeatureRevolve2(True, True, False, True, 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 create a Revolve Cut feature.

We create Revolve Cut by setting the value of variable swFeature by FeatureRevolve method.

FeatureRevolve method is part of FeatureManager object.

This FeatureManager is again part of ModelDoc2 object.

FeatureRevolve2 Method Parameters Details

This FeatureRevolve 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.

  • 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. as defined below.

    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;
  • Dir2Type - Revolve end condition in direction 2; as defined in swEndConditions_e and only applies if Dir1Type is not swEndConditions_e.swEndCondMidPlane as defined below.

    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;
  • 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.

  • ThinType - Type and direction as defined in swThinWallType_e.

    Member Description
    swThinWallMidPlan 2
    swThinWallOneDirection 0
    swThinWallOppDirection 1
    swThinWallTwoDirection 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 return feature data 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:

Parameter Name Value Used
SingleDir True
IsSolid True
IsThin False
IsCut True
ReverseDir False
BothDirectionUpToSameEntity True
Dir1Type swEndConditions_e.swEndCondBlind
Dir2Type swEndConditions_e.swEndCondBlind
Dir1Angle AngleConversionFactor * 360
Dir2Angle 0
OffsetReverse1 False
OffsetReverse2 False
OffsetDistance1 0
OffsetDistance2 0
ThinType swThinWallType_e.swThinWallOneDirection
ThinThickness1 0
ThinThickness2 0
Merge True
UseFeatScope False
UseAutoSelect True

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

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

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

final-result-revolve-cut

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 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: