SOLIDWORKS Macro - Create Chamfer

12 minute read

This post is an intermediate post and required you to have some knowledge of VBA.
If you are following my articles you will notice that till now we were hardcoding the selections for input parameters.
But from this post onward we will take user-inputs.
Please note that we will not use πŸ‘‰ UserForm for taking inputs, but use πŸ‘‰ Input Box and πŸ‘‰ Message Box for value input and will ask user to select SOLIDWORKS Entities from SOLIDWORKS.
I hope you will also like this type of tutorials.
Thank you for reading till now.

Objective

Objective of this article is to learn how to create Chamfer feature through SOLIDWORKS VBA Macros in SOLIDWORKS.

We create Chamfer Feature in following steps in general.

  1. Ask user to select an edge.
  2. Ask user to input the Chamfer Width of Chamfer feature.
  3. Ask user to input the Chamfer Angle of Chamfer feature.
  4. Create Chamfer feature from method FeatureChamfer.

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

Results We Can Get

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

Below image shows the result we get.

chamfer-final-result

To get the correct result please follow the steps correctly.

Video of Code on YouTube

Please see below 🎬 video on how to create Chamfer feature 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 article.

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

Code Sample

Below is the code for creating Fillet Feature feature in VBA is given.

Option Explicit

' Variable for Solidworks Application
Dim swApp As SldWorks.SldWorks

' Variable for Solidworks document
Dim swDoc As SldWorks.ModelDoc2

' Variable for Solidworks Fillet Feature
Dim swFeature As SldWorks.Feature

' Variable for Solidworks Selection Manager
Dim swSelMgr As SldWorks.SelectionMgr

' Program to create Chamfer feature
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 currently opened 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

  ' Set Solidworks Selection Manager variablt to opened document's selection manager
  Set swSelMgr = swDoc.SelectionManager
        
  ' Variable for Solidworks Entity
  Dim swObject As SldWorks.Entity
        
  ' Inform user to select an edge for Chamfer feature.
  MsgBox "Please select an Edge for Chamfer feature."
      
  ' Loop until Solidworks Entity variable is equal to selected edge
  While swObject Is Nothing
      
    ' Local integer for loop
    Dim i As Integer
    
    ' Looping until we select an edge
    For i = 1 To swSelMgr.GetSelectedObjectCount2(-1)
    
        ' If the selection type edge
        If swSelMgr.GetSelectedObjectType3(i, -1) = swSelectType_e.swSelEDGES Then
            
            ' Set the Solidworks Entity object to selected edge
            Set swObject = swSelMgr.GetSelectedObject6(i, -1)
        End If
    Next
    
    DoEvents
  Wend
  
  '-----------------------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 Chamfer Width
  Dim chamferWidth As Double
  chamferWidth = InputBox("Please enter Chamfer Width:") * LengthConversionFactor
  
  ' Variable to store Chamfer Angle
  Dim chamferAngle As Double
  chamferAngle = InputBox("Please enter Chamfer Angle:") * AngleConversionFactor
  
  ' Create Chamfer Feature
  swDoc.FeatureChamfer chamferWidth, chamferAngle, False
    
End Sub

Prerequisite

There are some prerequisite for this article.

We are not creating feature from code but we use existing πŸ‘‰ Extrude feature to create Chamfer feature as shown in below picture.

prerequisite

As shown in above image, there is only 1 Extrude feature in our part.

  • Extrude Feature : This is our Extruded part for Chamfer Feature.

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

Steps To Follow

To create Chamfer Feature there are following steps:

  1. Creating Global Variables
  2. Initializing required variables
  3. Ask user to select an edge
  4. Get unit Conversion factors
  5. User input for Chamfer radius
  6. Create Chamfer feature

Now let us walk through each step as given above, and understand every line.

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

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.

We create following variables.

  • Variable for Solidworks application
' 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.

To see methods and properties related to SldWorks.SldWorks object, please visit πŸ‘‰ this page of SOLIDWORKS API Help.

  • Variable for Solidworks document
' 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.

To see methods and properties related to SldWorks.ModelDoc2 object, please visit πŸ‘‰ this page of SOLIDWORKS API Help.

  • Variable for Solidworks Chamfer Feature
' Variable for Solidworks Chamfer 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 Chamfer Feature.

To see methods and properties related to Feature object, please visit πŸ‘‰ this page of SOLIDWORKS API Help.

  • Variable for Solidworks Selection Manager
' Variable for Solidworks Selection Manager
Dim swSelMgr As SldWorks.SelectionMgr

In this line, we create a variable which we named as swSelMgr and the type of this swSelMgr variable is SldWorks.SelectionMgr.

To see methods and properties related to SldWorks.SelectionMgr object, please visit πŸ‘‰ this page of SOLIDWORKS API Help.

These all are our global variables.

They are SOLIDWORKS API Objects.

So basically I group all the SOLIDWORKS API Objects in one place.

' Program to create Chamfer feature
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.

To know more about Sub Procedure you can check πŸ‘‰ VBA Sub and Function Procedures article of this website.

Initializing Required Variables

Inside this procedure we first initialize required variables as given below.

  • Set SOLIDWORKS variable to SOLIDWORKS application
' 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.

  • Set SOLIDWORKS document variable to opened part document
' Set 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.

  • Set SOLIDWORKS Selection Manager variable
' Set SOLIDWORKS Selection Manager variable
Set swSelMgr = swDoc.SelectionManager

In above line, we set SOLIDWORKS Selection ManagerswSelMgr variable to current document’s Selection Manager.

' Variable for Solidworks Entity
Dim swObject As SldWorks.Entity

In this line, we create a variable which we named as swObject and the type of this swObject variable is SldWorks.Entity.

To see methods and properties related to Entity object, please visit πŸ‘‰ this page of SOLIDWORKS API Help.

Ask user to select an edge

Now we will ask use to select an edge for Chamfer feature.

' Inform user to select an edge for Chamfer feature.
MsgBox "Please select an Edge for Chamfer feature."

Image of above message is shown in below image.

message to use for edge selection

In about line of code we show a Msgbox to use displaying message to select an Edge of extrude.

' Loop until Solidworks Entity variable is equal to selected edge
While swObject Is Nothing

Wend

In above line of code, we create a While loop.

This loops until we select the our Edge for Chamfer feature.

For more details about While loop, please see πŸ‘‰ VBA Looping article from this website.

' Local integer for loop
Dim i As Integer

In above line of code, we create a local integer name i as a counter.

' Loop until we select an edge
For i = 1 To swSelMgr.GetSelectedObjectCount2(-1)

Next

In above line of code, we create a For loop.

This code loops from i = 1 to number of objects we select.

' If the selection type edge
If swSelMgr.GetSelectedObjectType3(i, -1) = swSelectType_e.swSelEDGES Then

End If

In above line of code, we use πŸ‘‰ IF statement with a condition.

The πŸ‘‰ IF statement is True when selected object is type of SOLIDWORKS Edge.

When πŸ‘‰ IF statement is True we execute code given below.

' Set the Solidworks Entity object to selected edge
Set swObject = swSelMgr.GetSelectedObject6(i, -1)

In above line, we set the SOLIDWORKS Entity object to selected edge.

After For loop, we have a call for DoEvents function.

Get unit Conversion factors

Now we need to get unit Conversion factors as shown in below code.

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

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

I have already explained about this in previous πŸ‘‰ Solidworks Macro - Fix Unit Issue article in this website.

Please visit πŸ‘‰ Solidworks Macro - Fix Unit Issue article for more details.

User inputs for Chamfer feature

Now we ask user to input following values:

  1. Chamfer Width
' Variable to store Chamfer Width
Dim chamferWidth As Double
chamferWidth = InputBox("Please enter Chamfer Width:") * LengthConversionFactor

In above code sample, we first create a variable.

  1. Name of variable = chamferWidth
  2. Type of variable = Double

After that we show a input box to get the Fillet radius from user.

Image of input box is shown below.

message to use for Chamfer Width input

After getting input value from InputBox, we first multiply it with LengthConversionFactor.

By doing this we set the input value to at fix with same unit system of part.

After conversion we assign the final value to chamferWidth variable.

  1. Chamfer Angle
' Variable to store Chamfer Angle
Dim chamferAngle As Double
chamferAngle = InputBox("Please enter Chamfer Angle:") * LengthConversionFactor

In above code sample, we first create a variable.

  1. Name of variable = chamferAngle
  2. Type of variable = Double

After that we show a input box to get the Fillet radius from user.

Image of input box is shown below.

message to use for Chamfer Angle input

After getting input value from InputBox, we first multiply it with LengthConversionFactor.

By doing this we set the input value to at fix with same unit system of part.

After conversion we assign the final value to chamferAngle variable.

Create Chamfer feature

Now we create Chamfer feature using above inputs.

' Create Chamfer Feature
swDoc.FeatureChamfer chamferWidth, chamferAngle, False

In above line of code we create Chamfer feature.

This FeatureChamfer method takes following parameters as explained:

  • Width - Width of the chamfer.
  • Angle - Angle of the chamfer.
  • Flip - Angle measures as follows:
    • 0 if angle is to be measured from the right face
    • 1 if angle is to be measured from the left face

Return Value : There are no return value from this method.

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

chamfer-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 Chamfer Feature 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!!!