Solidworks VBA Macro - Create Rib

13 minute read

This article has "Beginner โžก Intermediate" level Macro program.
If you are following my articles then will not be an issue for you.
In this article we did not use ๐Ÿš€ UserForm for taking inputs, instead we use ๐Ÿš€ Input Box for value input and ๐Ÿš€ Message Box to notify user.
I hope you will also like this type of tutorials.
Thank you for reading.

Objective

In this article we create and understand VBA macro of Rib feature in SOLIDWORKS CAD Software.

This method is most updated method, so use this method if you want to create a new Rib Feature quickly.

Results We Can Get

After running our macro we successfully create Rib feature as a result.

Below GIF shows the result we get.

Rib Feature final result

We create Rib Feature in following steps in general.

  1. Ask user to input Rib Thickness.
  2. Create Rib thickness.

To get the correct result please follow the steps correctly.

Macro Video

Below ๐ŸŽฌ video shows Rib feature from SOLIDWORKS VBA Macros.


Above video is just for visualization and there are no explanation.

I have explained each and every line in this article.

It is advisable to watch video, since it help you to better understand the process.

VBA Macro

Below is the VBA macro for creating Rib Feature.

Option Explicit

' Solidworks application variable
Dim swApp As SldWorks.SldWorks

' Solidworks document variable
Dim swDoc As SldWorks.ModelDoc2

' Solidwors Feature variable
Dim swFeature As SldWorks.Feature

' Main program for Rib
Sub main()

  ' Set Solidworks application 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
  
  ' Set Solidworks document variable to open 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
  
  ' Get First feature in Feature tree
  Set swFeature = swDoc.FirstFeature
  
  ' Check if Solidworks document is selected or not
  If swFeature Is Nothing Then
    MsgBox ("Failed to selected First feature in Feature Tree.")
    Exit Sub
  End If
  
  ' Traversing through the Feature Tree,
  ' until Feature name is "Rib-Sketch"
  While swFeature.Name <> "Rib-Sketch"
    
    ' Print current Feature name is Immediate window
    Debug.Print swFeature.Name
    
    ' Get the next feature
    Set swFeature = swFeature.GetNextFeature
  Wend
  
  ' Print current Feature name is Immediate window
  Debug.Print swFeature.Name
  
  ' Get the faces of selected feature
  swFeature.Select True
  
  '-----------------------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

  '----------------------------------------------------------------
  
  ' Variable to store user response
  Dim response As String
  response = InputBox("Please input Rib Thickness:", "Rib Thickness")
  
  ' This will handle empty value or cancel case
  If Len(response) = 0 Then
    MsgBox "Empty or no value. Please try again."
    Exit Sub
  End If
  
  ' This will handle case for Non-numeric values
  If IsNumeric(response) = False Then
    MsgBox "Entered value is Non-numeric. Please try again."
    Exit Sub
  End If
  
  ' Variable to store Rib Thickness
  Dim RibThickness As Double
  RibThickness = CDbl(response) * LengthConversionFactor
  
  ' This will handle case for 0 thickness
  If RibThickness = 0 Then
    MsgBox "Entered value must be greater than 0. Please try again."
    Exit Sub
  End If
  
  ' Create Rib Feature
  swDoc.FeatureManager.InsertRib True, False, RibThickness, 0, False, False, False, 0, False, False
  
  ' Clear all selection
  swDoc.ClearSelection2 True
    
End Sub

Prerequisite

There are some prerequisite for this article.

  • Knowledge of VBA programming language is โ—required.
  • We are not creating feature from code but we use existing ๐Ÿš€ Extrude feature and ๐Ÿš€ Sketch Line to create Sketch Driven pattern feature.

Below image shown prerequisite 3D model for our demo.

prerequisite

As shown in above image, there are 1 Extrude features and 1 Sketch Line in our part.

  • Base Feature : This is our Base Extrude feature.
  • Sketch Line : This is our Sketch for Rib feature.

If you want to create these Extrude features and Sketch Line programmatically then please refer to below article.

We will apply checks in this article, so the code we write should be error free most of the time.

Steps To Follow

This Rib Feature VBA macro can be divided into following sections:

  1. Creating Global Variables
  2. Initializing required variables
  3. Select Rib Sketch
  4. Get unit Conversion factors
  5. Get Rib Thickness And Validation
  6. Create Rib Feature

Every section with each line is explained below.

I also give some links (see icon ๐Ÿš€) so that you can go through them if there are anything I explained in previous articles.

Creating Global Variables

Option Explicit
' Variable for Solidworks application
Dim swApp As SldWorks.SldWorks
  • Purpose: In above line, we create a variable for Solidworks application.
  • Variable Name: swApp
  • Type: SldWorks.SldWorks
  • Reference: Please visit ๐Ÿš€ online SOLIDWORKS API Help.
' Variable for Solidworks document
Dim swDoc As SldWorks.ModelDoc2
  • Purpose: In above line, we create a variable for Solidworks document.
  • Variable Name: swDoc
  • Type: SldWorks.ModelDoc2
  • Reference: Please visit ๐Ÿš€ online SOLIDWORKS API Help.
' Solidwors Feature variable
Dim swFeature As SldWorks.Feature
  • Purpose: In above line, we create a variable for looping through Feature Tree and feature selection.
  • Variable Name: swFeature
  • Type: SldWorks.Feature
  • Reference: Please visit ๐Ÿš€ online SOLIDWORKS API Help.

These all are our global variables.

They are SOLIDWORKS API Objects.

' Main program for Rib
Sub main()

End Sub
  • In above line, we create Main program for Rib.
  • This is a Sub procedure which has name of main.
  • This procedure hold all the statements (instructions) we give to computer.
  • Reference: Detailed information ๐Ÿš€ VBA Sub and Function Procedures article of this website.

Initializing Required Variables

Inside this section we initialize required variables.

' Setting Solidworks variable to current application
Set swApp = Application.SldWorks
  • In above line, we set value of swApp variable.
  • This value is currently opened 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 code block, we check if we successfully set the value of swApp variable.
  • We use ๐Ÿš€ IF statement for checking.
  • Condition: swApp Is Nothing
  • When this condition is True,
    • We show and ๐Ÿš€ message window to user.
    • Message: SOLIDWORKS is not opened
    • Then we stop our macro here.
' Setting Solidworks document variable to opened part document
Set swDoc = swApp.ActiveDoc
  • In above line, we set value of swDoc variable.
  • This value is currently opened 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 code block, we check if we successfully set the value of swDoc variable.
  • We use ๐Ÿš€ IF statement for checking.
  • Condition: swDoc Is Nothing
  • When this condition is True,
    • We show and ๐Ÿš€ message window to user.
    • Message: Solidworks document is not opened. Please open a document.
    • Then we stop our macro here.
' Get First feature in Feature tree
Set swFeature = swDoc.FirstFeature
  • In above line, we set value of swFeature variable.
  • We set swFeature variableโ€™s value to 1st feature in Feature Tree.
' Check if Solidworks document is selected or not
If swFeature Is Nothing Then
  MsgBox ("Failed to selected First feature in Feature Tree.")
  Exit Sub
End If
  • In above code block, we check if we successfully set the value of swFeature variable.
  • We use ๐Ÿš€ IF statement for checking.
  • Condition: swFeature Is Nothing
  • When this condition is True,
    • We show and ๐Ÿš€ message window to user.
    • Message: Failed to selected First feature in Feature Tree.
    • Then we stop our macro here.

Select Rib Sketch

In this section, we select Rib-Sketch Feature for Rib feature.

' Traversing through the Feature Tree,
' until Feature name is "Rib-Sketch"
While swFeature.Name <> "Rib-Sketch"

Wend
  • In above line of code we start a While loop.
  • We loop until swFeature.Name variableโ€™s value is not equal to Rib-Sketch.
  • Reference: ๐Ÿš€ VBA Looping article from this website.
' Print current Feature name is Immediate window
Debug.Print swFeature.Name
  • In above line, we print current Feature name is Immediate window.
' Get the next feature
Set swFeature = swFeature.GetNextFeature
  • In above line, we again set value of swFeature variable.
  • We set swFeature variableโ€™s value to next feature in Feature Tree.
' Print current Feature name is Immediate window
Debug.Print swFeature.Name
  • In above line, we print current Feature name is Immediate window.
' Select the current feature
swFeature.Select True
  • In above line, we select the current feature.

Get unit Conversion factors

In this section we get unit Conversion factors.

'-----------------------Unit Conversion Factors----------------------------------------
  
' 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

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

Get Rib Thickness And Validation

In this section, we get get the Rib Thickness from user and apply some validation on Rib Thickness.

' Variable to store user response
Dim response As String
  • In above line, we create a variable as a counter.
  • Variable Name: response
  • Type: String
response = InputBox("Please input Rib Thickness:", "Rib Thickness")
  • In above line of code we are doing 2 steps in one line.

    Those 3 steps are explained below.

    • Step 1 - Getting Rib Thickness from user.

    Below image shows the message for Rib Thickness to the user.

    message-to-select-rib-thickness

    • Step 2 - Assigned input value to response variable.
' This will handle empty value or cancel case
If Len(response) = 0 Then
  MsgBox "Empty or no value. Please try again."
  Exit Sub
End If
  • In above code block, we check the length of input value.
  • This check will handle case for empty value or cancel operation case.
  • We use ๐Ÿš€ IF statement for checking.
  • Condition: Len(response) = 0
    • Len() is pre-build VBA function which check the length of a object.
    • In above cases, we will get 0 value.
  • When this condition is True,
    • We show and ๐Ÿš€ message window to user.
    • Message: Empty or no value. Please try again.
    • Then we stop our macro here.
' This will handle case for Non-numeric values
If IsNumeric(response) = False Then
  MsgBox "Entered value is Non-numeric. Please try again."
  Exit Sub
End If
  • In above code block, we check if the input value is Non-numeric.
  • This check will handle case for Non-numeric values.
  • We use ๐Ÿš€ IF statement for checking.
  • Condition: IsNumeric(response) = False
    • IsNumeric() is pre-build VBA function which check if passing object is Numeric or not.
    • In above cases, we will get False value.
  • When this condition is True,
    • We show and ๐Ÿš€ message window to user.
    • Message: Entered value is Non-numeric. Please try again.
    • Then we stop our macro here.
' Variable to store Rib Thickness
Dim RibThickness As Double
  • In above line, we create a variable to store Rib Thickness.
  • Variable Name: RibThickness
  • Type: Double
RibThickness = CDbl(response) * LengthConversionFactor
  • In above line of code we are doing 3 steps in one line.

    Those 3 steps are explained below.

    • Step 1 - Converting Rib Thickness from user to Double type.
    • Step 2 - Updating converted Rib Thickness as per document unit system.

    • Step 3 - Assigned input value to RibThickness variable.
' This will handle case for 0 thickness
If RibThickness = 0 Then
  MsgBox "Entered value must be greater than 0. Please try again."
  Exit Sub
End If
  • In above code block, we check if the input value is zero (0).
  • This check will handle case for 0 thickness.
  • We use ๐Ÿš€ IF statement for checking.
  • Condition: RibThickness = 0
  • When this condition is True,
    • We show and ๐Ÿš€ message window to user.
    • Message: Entered value must be greater than 0. Please try again.
    • Then we stop our macro here.

Create Rib Feature

In this section we create Rib Feature.

' Create Rib Feature
swDoc.FeatureManager.InsertRib True, False, RibThickness, 0, False, False, False, 0, False, False
  • In above line of code we create Rib Feature by InsertRib method.

  • InsertRib method is part of FeatureManager object.

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

  • This InsertRib method takes following parameters as explained:
    • Is2Sided - True if the rib is double-sided, False if the rib is single sided.
    • ReverseThicknessDir - True to thicken the opposite side of the sketch normal, False to not.
    • Thickness - Rib thickness.
    • ReferenceEdgeIndex - Edge in the sketch to use to determine the material direction and for draft reference.
    • ReverseMaterialDir - True to flip the direction of the extrusion, False to not.
    • IsDrafted - True to draft the rib, False to not.
    • DraftOutward - True to draft the rib outward, False to not. only valid if IsDrafted is True.
    • DraftAngle - Draft angle of the rib; only valid if IsDrafted is True.
    • IsNormToSketch - True if extrusion direction is normal to sketch, False if parallel to sketch.
    • IsDraftedFromWall - True if draft is from wall, False if not; only valid if IsDrafted is True.
  • Return Value : This InsertRib method did not return any value.

  • In our code, I have used following values:

    Parameter Name Value Used
    Is2Sided True
    ReverseThicknessDir False
    Thickness RibThickness
    ReferenceEdgeIndex 0
    ReverseMaterialDir False
    IsDrafted False
    DraftOutward False
    DraftAngle 0
    IsNormToSketch False
    IsDraftedFromWall False

Reference: For more details about

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

Rib Feature final result

' Clear all selection
swDoc.ClearSelection2 True
  • In above line, we clear all previous selection.
  • For this we use ClearSelection2 method which is part of SOLIDWORKS Document variable i.e swDoc variable.

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 Rib feature with SOLIDWORKS VBA Macros.

For more such tutorials on SOLIDWORKS VBA Macro, do come to this website 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: