SOLIDWORKS Macro - Create Holes from Hole Wizard

19 minute read

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

Objective of this article is to learn how to create Holes using ๐Ÿ‘‰ Hole Wizard through SOLIDWORKS VBA Macros in SOLIDWORKS.

We create Holes in 3 steps in general.

  1. Select a ๐Ÿ‘‰ Sketch from we take the Holes location.
  2. Loop through each ๐Ÿ‘‰ Sketch point.
  3. Create Holes using ๐Ÿ‘‰ Hole Wizard feature using HoleWizard method.

This method is not updated method, so use this method if you want to quickly create a new Holes using Hole Wizard.

Steps To Create Holes using Hole Wizard

We use following steps to create Holes using ๐Ÿ‘‰ Hole Wizard while writing macro

  1. Ask user to select a ๐Ÿ‘‰ Sketch from Feature tree.
  2. From this sketch we get ๐Ÿ‘‰ Sketch point for Holes position.
  3. Loop through each ๐Ÿ‘‰ Sketch point.
  4. Create Holes using ๐Ÿ‘‰ Hole Wizard method from ๐Ÿ‘‰ SOLIDWORKS Feature Manager.

Results We Can Get

After running our we successfully create Hole feature as a result.

Below image shows the result we get.

hole-wizard-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 Hole 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 post.

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

Code Sample

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

Option Explicit

' Solidworks application variable
Dim swApp As SldWorks.SldWorks

' Solidworks document variable
Dim swDoc As SldWorks.ModelDoc2

' Solidworks Feature variable
Dim swFeature As SldWorks.Feature

' Solidworks Sketch variable
Dim swSketch As SldWorks.Sketch

' Solidworks Selection Manager variable
Dim swSelMgr As SldWorks.SelectionMgr

' Sketch point array
Dim vSketchPointArray As Variant

' Solidworks Sketch Point variable
Dim sketchPoint As SldWorks.sketchPoint

' Solidworks Hole Feature
Dim swHoleFeature As SldWorks.Feature

' Sketch Point inside point array
Dim vpoint As Variant

' Create Hole from Hole wizard program
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
  
  ' Set Solidworks Selection Manager variable
  Set swSelMgr = swDoc.SelectionManager
    
  ' Message to show user
  Dim messageToUser As String

  ' Update Messages
  messageToUser = "Please select Sketch from Feature tree for Hole location."

  ' Show message to user
  MsgBox messageToUser
  
  ' Variable for Solidworks Entity
  Dim swObject As SldWorks.Entity
    
  ' Loop until we complete our selection
  While swObject Is Nothing
      
    ' Local integer for loop
    Dim i As Integer
    
    ' Loop until we select
    For i = 1 To swSelMgr.GetSelectedObjectCount2(-1)
        
      ' If the profile sketch is selected
      If swSelMgr.GetSelectedObjectType3(i, -1) = swSelectType_e.swSelSKETCHES Then

        ' Set the Solidworks Entity object to profile sketch
        Set swObject = swSelMgr.GetSelectedObject6(i, -1)
      
      ' If the selected Sketch is externaly
      ElseIf swSelMgr.GetSelectedObjectType3(i, -1) = swSelectType_e.swSelEXTSKETCHPOINTS Then
          
        ' Inform user to select sketch from Tree
        MsgBox "Please select Sketch from Feature Tree."
        
        ' Clear selection
        swDoc.ClearSelection2 True
      End If
    Next
    
    DoEvents
  Wend
  
  ' Get and Set Solidworks Feature variable of Sketch2
  Set swFeature = swObject
  
  ' Get and Set Solidworks Sketch variable of Sketch2
  Set swSketch = swFeature.GetSpecificFeature2
      
  ' Get all sketch points inside sketch2
  vSketchPointArray = swSketch.GetSketchPoints2
  
  '-----------------------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
  
  '----------------------------------------------------------------

  
  ' Loop through each point
  For Each vpoint In vSketchPointArray
    
    ' Set Solidworks Sketch point to current point in loop
    Set sketchPoint = vpoint
    
    ' Select current sketch point
    sketchPoint.Select True
    
    ' Create Counter Bore Hole at this point
    Set swHoleFeature = swDoc.FeatureManager.HoleWizard(swWzdCounterBore, swStandardISO, swStandardISOHexCapScrew, "M6", swEndCondBlind, 5 * LengthConversionFactor, 6 * LengthConversionFactor, 10 * LengthConversionFactor, 5 * LengthConversionFactor, 0, 1 * LengthConversionFactor, 2 * LengthConversionFactor, 0, 0, 0, 0, 0, 0, 0)
  Next
  
  ' Check if Holes from Hole Wizard creates or not
  If swHoleFeature Is Nothing Then
    MsgBox ("Failed to create Holes from Hole Wizard.")
    Exit Sub
  End If
  
  ' View zoom to fit
  swDoc.ViewZoomtofit2
  
  ' Clear all selection
  swDoc.ClearSelection2 True
  
End Sub

Prerequisite

There are some prerequisite for this article.

We are not creating sketches from code but we use existing sketch to create Holes using ๐Ÿ‘‰ Hole Wizard feature as shown in below picture.

prerequisite

As shown in above image, there are 1 sketches and 1 Extrude feature in our part.

  • Extrude Feature : This is our Extrude part for ๐Ÿ‘‰ Hole Wizard feature.
  • Sketch2 : This ๐Ÿ‘‰ Sketch contains ๐Ÿ‘‰ Sketch point. which work as location for Hole Feature.

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

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 Holes using ๐Ÿ‘‰ Hole Wizard, there are following steps:

  1. Creating Global Variables
  2. Initializing required variables
  3. Ask user to select Sketch segment
  4. Get all Sketch Points from selected Sketch
  5. Get unit Conversion factors
  6. Create Hole feature at each Sketch Point
  7. Final work

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

  • Variable for Solidworks Sketch
' Solidworks Sketch variable
Dim swSketch As SldWorks.Sketch

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

To see methods and properties related to SldWorks.Sketch 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.

  • Variable for Sketch point array
' Sketch point array
Dim vSketchPointArray As Variant

In this line, we create a variable named vSketchPointArray as Variant object type.

We create variable vSketchPointArray for storing all the sketch points we get from selected Sketch.

  • Variable for Solidworks Sketch Point
' Solidworks Sketch Point variable
Dim sketchPoint As SldWorks.sketchPoint

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

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

  • Variable for Solidworks Hole Feature
' Solidworks Hole Feature
Dim swHoleFeature As SldWorks.Feature

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

We create variable swFeature for SOLIDWORKS Hole Feature.

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

  • Variable for Sketch point inside array
' Sketch Point inside point array
Dim vpoint As Variant

In this line, we create a variable named vpoint as Variant object type.

We create variable vpoint for each point inside vSketchPointArray.

These all are our global variables.

They are SOLIDWORKS API Objects.

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

' Create Hole from Hole wizard 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.

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.

Ask user to select Sketch

Now we will ask user to select a Sketch for ๐Ÿ‘‰ Hole Wizard

Please follow steps given below.

  • Show Message to user
' Message to show 
Dim messageToUser As String

In above line of code we create a variable named messageToUser of String type.

This variable holds the message we want to show before selection.

' Update Message
messageToUser = "Please select Sketch from Feature tree for Hole location."

In above line of code, we set the value of messageToUser update to "Please select Sketch from Feature tree for Hole location."

' Show message to user
MsgBox messageToUser

In above line of code, we show the message to user.

Below image show the message to the user.

ask-user-to-select-sketch

  • Complete the selection
' 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.

We create variable swObject for SOLIDWORKS Entities (Selected Sketch) we ask use to select.

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

' Loop until we complete our selection
While swObject Is Nothing
    
Wend

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

This loop will continue until we select the our ๐Ÿ‘‰Sketch.

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
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 sketch is selected
If swSelMgr.GetSelectedObjectType3(i, -1) = swSelectType_e.swSelSKETCHES Then

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

' If the selected Sketch is externaly
ElseIf swSelMgr.GetSelectedObjectType3(i, -1) = swSelectType_e.swSelEXTSKETCHPOINTS Then

  ' Inform user to select sketch from Tree
  MsgBox "Please select Sketch from Feature Tree."

  ' Clear selection
  swDoc.ClearSelection2 True
End If

In above line of code, we use ๐Ÿ‘‰ IF/Else statement with conditions.

The ๐Ÿ‘‰ IF statement is True when selected object is type of SOLIDWORKS Sketch.

Similarly, ๐Ÿ‘‰ Else statement is True when select object is type of SOLIDWORKS External Sketch Point.

When we select the sketch from Model view, then selected object is SOLIDWORKS External Sketch Point.
When we select the sketch from Feature Tree, then select object is SOLIDWORKS Sketch.

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

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

In above line, we set the SOLIDWORKS Entities object swObject value by GetSelectedObject6() method of SOLIDWORKS Selection Manager.

Similarly, when ๐Ÿ‘‰ Else statement is True we execute code given below.

' Inform user to select sketch from Tree
MsgBox "Please select Sketch from Feature Tree."

' Clear selection
swDoc.ClearSelection2 True
  1. We show message to user to select ๐Ÿ‘‰Sketch from Feature Tree.
  2. After that we clear our selection.

We do this because if selected object type is SOLIDWORKS External Sketch Point then we can not assign this object as SOLIDWORKS Entity inside array.

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

This function repeats the While loop until we select the ๐Ÿ‘‰Sketch for ๐Ÿ‘‰ Hole Wizard.

Get all Sketch Points from selected Sketch

Now we need to get the all ๐Ÿ‘‰ Sketch point from previously selected ๐Ÿ‘‰Sketch .

We do this by below code of lines.

' Get and Set Solidworks Feature variable of Sketch2
Set swFeature = swObject

In above line of code we set the value of SOLIDWORKS Feature variable swFeature to swObject.

' Get and Set Solidworks Sketch variable of Sketch2
Set swSketch = swFeature.GetSpecificFeature2

In above line of code we set the value of SOLIDWORKS Sketch variable swSketch by GetSpecificFeature2 method from SOLIDWORKS Feature variable.

' Get all sketch points inside sketch2
vSketchPointArray = swSketch.GetSketchPoints2

In above line of code we get all ๐Ÿ‘‰ Sketch point by GetSketchPoints2 from SOLIDWORKS Sketch variable swSketch and store them in vSketchPointArray variable.

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.

Create Hole feature

Now we create Hole feature at every Sketch Point.

  • Loop through every Sketch Point
' Loop through each point
For Each vpoint In vSketchPointArray

Next

In above line of code we create a ๐Ÿ‘‰ For Each loop.

  • Select current Sketch Point
' Set Solidworks Sketch point to current point in loop
Set sketchPoint = vpoint

In above line of code we set SOLIDWORKS Sketch point variable sketchPoint to current point vpoint in loop

' Create Counter Bore Hole at this point
Set swHoleFeature = swDoc.FeatureManager.HoleWizard(swWzdCounterBore, swStandardISO, swStandardISOHexCapScrew, "M6", swEndCondBlind, 5 * LengthConversionFactor, 6 * LengthConversionFactor, 10 * LengthConversionFactor, 5 * LengthConversionFactor, 0, 1 * LengthConversionFactor, 2 * LengthConversionFactor, 0, 0, 0, 0, 0, 0, 0)

In above line of code we set the value of variable swHoleFeature by HoleWizard method.

This HoleWizard method takes following parameters as explained:

  • GenericHoleType - Type of hole or slot as defined in swWzdGeneralHoleTypes_e as given in below table

    Member Description
    swWzdCounterBore 0
    swWzdCounterBoreSlot 6
    swWzdCounterSink 1
    swWzdCounterSinkSlot 7
    swWzdHole 2
    swWzdHoleSlot 8
    swWzdLegacy 5
    swWzdPipeTap 3
    swWzdTap 4
  • StandardIndex - Hole or slot standard property as defined in swWzdHoleStandards_e as given in below table.

    Member Description
    swStandardAnsiInch 0
    swStandardAnsiMetric 1
    swStandardAS 16 = Australian
    swStandardBSI 2
    swStandardDIN 4
    swStandardDME 3
    swStandardGB 13
    swStandardHascoMetric 5
    swStandardHelicoilInch 6
    swStandardHelicoilMetric 7
    swStandardIS 15 = Indian
    swStandardISO 8
    swStandardJIS 9
    swStandardKS 14 = Korean
    swStandardPCS 10
    swStandardPEMInch 17 = PEM Inch
    swStandardPEMMetric 18 = PEM Metric
    swStandardProgressive 11
    swStandardSuperior 12
  • FastenerTypeIndex - *Hole or slot fastener type as defined in swWzdHoleStandardFastenerTypes_e as given in ๐Ÿ‘‰ this page of Solidworks API Help.

  • SSize - Size of the hole or slot.

  • EndType - Hole or slot end type as defined in swEndConditions_e as given in below table.

    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; superseded by swEndCondUpToSelection
    swEndCondUpToVertex 3 = Do not use; superseded by swEndCondUpToSelection
  • Diameter - Diameter of the hole or slot.

  • Depth - Depth of the hole or slot.

  • Value1 - Hole or slot parameter.

  • Value2 - Hole or slot parameter.

  • Value3 - Hole or slot parameter.

  • Value4 - Hole or slot parameter.

  • Value5 - Hole or slot parameter.

  • Value6 - Hole or slot parameter.

  • Value7 - Hole or slot parameter.

  • Value8 - Hole or slot parameter.

  • Value9 - Hole or slot parameter.

  • Value10 - Hole or slot parameter.

  • Value11 - Hole or slot parameter.

  • Value12 - Hole or slot parameter.

Return Value : This HoleWizard 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
GenericHoleType swWzdCounterBore
StandardIndex swStandardISO
FastenerTypeIndex swStandardISOHexCapScrew
SSize M6
EndType swEndCondBlind
Diameter 5 * LengthConversionFactor
Depth 6 * LengthConversionFactor
Value1 10 * LengthConversionFactor
Value2 5 * LengthConversionFactor
Value3 0
Value4 1 * LengthConversionFactor
Value5 2 * LengthConversionFactor
Value6 0
Value7 0
Value8 0
Value9 0
Value10 0
Value11 0
Value12 0

HoleWizard method is part of FeatureManager object.

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

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

Please see below image in which I have identity some value parameters.

hole-wizard-parameters

' Check if Holes from Hole Wizard creates or not
If swFeature Is Nothing Then
  MsgBox ("Failed to create Holes from Hole Wizard.")
  Exit Sub
End If

In above line of code, we use an ๐Ÿ‘‰ IF statement to check if we able to create Holes from ๐Ÿ‘‰ Hole Wizard 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.

hole-wizard-final-result

Final work

After creating Hole Cut feature, we have to do some cleaning work so that we can use this macro frequently.

  • Make part Zoom to fit
' View zoom to fit
swDoc.ViewZoomtofit2

In above line, we make our view zoom to fit the model.

For this we use ViewZoomtofit2 method which is part of SOLIDWORKS Document variable i.e swDoc variable.

  • Clear selection
' 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 Hole 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!!!

Updated: