Solidworks VBA Macro - Create Reference Point

11 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 Reference Point in SOLIDWORKS CAD Software.

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

Results We Can Get

After running our macro we successfully create Reference Point as a result.

Below image shows the result we get.

reference-coordinate-feature-final-result

We create Reference Point in following steps in general.

  1. Ask User to select an Entity in Model.
  2. Ask User for Number of Point.
  3. Create Reference Point on selected Entity.

To get the correct result please follow the steps correctly.

Macro Video

Below ๐ŸŽฌ video shows Reference Point 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 Reference Point.

Option Explicit

' Variable for Solidworks Application
Dim swApp As SldWorks.SldWorks

' Variable for Solidworks document
Dim swDoc As SldWorks.ModelDoc2

' Variable for Solidworks Reference Point
Dim swFeature As Variant

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

' Variable for Solidworks Object
Dim swObject As Object

' Program to create Reference Point
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.")
    Exit Sub
  End If

  ' Set Solidworks Selection Manager variable
  Set swSelMgr = swDoc.SelectionManager
  
  ' Inform user to select a Sketch for Reference Point
  MsgBox "Please select a Sketch for Reference Point."
      
  ' Loop until we select an object
  While swObject Is Nothing
      
    ' Local integer for loop
    Dim i As Integer
    
    ' Looping until we select an Object
    For i = 1 To swSelMgr.GetSelectedObjectCount2(-1)
    
      ' If the selection type Object
      If swSelMgr.GetSelectedObjectType3(i, -1) = swSelectType_e.swSelEXTSKETCHSEGS Then
          
        ' Set the Solidworks object
        Set swObject = swSelMgr.GetSelectedObject6(i, -1)
      End If
    Next
    
    DoEvents
  Wend
  
  ' Variable to store Number of Points
  Dim response As String
  response = InputBox("Number of Points:", "Reference Point", "1")
  
  ' 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 Number of Points
  Dim numberOfPoints As Double
  numberOfPoints = CDbl(response)
  
  ' This will handle case for 0 points
  If numberOfPoints = 0 Then
    MsgBox "Entered value must be greater than 0. Please try again."
    Exit Sub
  End If
  
  ' Create Reference Point
  swFeature = swDoc.FeatureManager.InsertReferencePoint(swRefPointAlongCurve, swRefPointAlongCurveEvenlyDistributed, 0, numberOfPoints)

  ' Check if Reference Point created or not
  If swFeature(0) Is Nothing Then
    MsgBox ("Failed to create Reference Point.")
    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.

  • Knowledge of VBA programming language is โ—required.

Since we are creating new part, there are no feature to create.

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

Steps To Follow

This Reference Point VBA macro can be divided into following sections:

  1. Create and Initialize required variables
  2. Ask user to select an Entity
  3. Get Number of Point And Validation
  4. Create Reference Point
  5. Final work

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.

Create and Initialize required 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.
' Variable for Solidworks Reference Point
Dim swFeature As SldWorks.Feature
  • Purpose: In above line, we create a variable for Solidworks Reference Point.
  • Variable Name: swFeature
  • Type: SldWorks.Feature
  • Reference: Please visit ๐Ÿš€ online SOLIDWORKS API Help.
' Variable for Solidworks Selection Manager
Dim swSelMgr As SldWorks.SelectionMgr
  • Purpose: In above line, we create a variable for Solidworks Selection Manager.
  • Variable Name: swSelMgr
  • Type: SldWorks.SelectionMgr.
  • Reference: Please visit ๐Ÿš€ online SOLIDWORKS API Help.
' Variable for Solidworks Object
Dim swObject As Object
  • Purpose: In above line, we create a variable for Object.
  • Variable Name: swObject
  • Type: Object

These all are our global variables.

They are SOLIDWORKS API Objects.

' Main program for Reference Point
Sub main()

End Sub
  • In above line, we create main program for Reference Point.
  • 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.
' Set Solidworks Application 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.
' Set Solidworks document variable to currently opened 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.")
  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.
    • Then we stop our macro here.
' Set Solidworks Selection Manager variable
Set swSelMgr = swDoc.SelectionManager
  • In above line, we set value of swSelMgr variable.
  • This value is current documentโ€™s Selection Manager.

Ask user to select an Entity

Inside this section we ask user to select an Entity.

' Inform user to select an Object for Point feature.
MsgBox "Please select a Sketch for Reference Point."
  • In above line, we show a messages to user.
  • Below image shows the message for select an Object to the user.

message-to-select-point

' Loop until we complete our selection
While swObjects(selectItems) Is Nothing    

Wend
  • In above line, we create another While loop.
  • This loops continues to run until we select the our Feature and Edge for circular pattern.
  • Reference: ๐Ÿš€ VBA Looping article from this website.
' Local integer for loop
Dim i As Integer
  • In above line, we create a variable as a counter.
  • Variable Name: i
  • Type: Integer
' Looping until we select an Object
For i = 1 To swSelMgr.GetSelectedObjectCount2(-1)

Next
  • In above line, we create a For loop.
  • This loops start from i = 1 to number of objects we select.
' If the selection type Object
If swSelMgr.GetSelectedObjectType3(i, -1) = swSelectType_e.swSelEXTSKETCHSEGS Then
    
  ' Set the Solidworks object
  Set swObject = swSelMgr.GetSelectedObject6(i, -1)
End If
  • In above code block, we use ๐Ÿš€ IF statement with conditions.
  • The ๐Ÿš€ IF statement is True when selected object is type of SOLIDWORKS Pount.
DoEvents
  • After For loop end, we have a call for DoEvents function.
  • This function repeats the While loop until we complete Feature or Edge selection.

Get Number of Point And Validation

In this section, ask user for Number of Point.

' Variable to hold user input
Dim response As String
  • In above line, we create a variable hold user input.
  • Variable Name: response
  • Type: String
' Getting Number of Points from user
response = InputBox("Number of Points:", "Reference Point", "1")
  • In above line of code we are doing 2 steps in one line.

    Those 3 steps are explained below.

    • Step 1 - Getting Number of Points from user.

    Below image shows the message for Number of Points to the user.

    message-to-enter-scale-factor

    • 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."  
  swDoc.ClearSelection2 True
  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.

message-to-enter-scale-factor

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

    message-to-show-when-empty-or-no-value-given

    • 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."  
  swDoc.ClearSelection2 True
  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.

    entering-non-numeric-value

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

    message-to-show-non-numeric-value-given

    • Then we stop our macro here.
' Variable for Number of Points
Dim numberOfPoints As Double
  • In above line, we create a variable to store Number of Points.
  • Variable Name: numberOfPoints
  • Type: Double
' Set Number of Points
numberOfPoints = CDbl(response)
  • In above line of code we are doing 2 steps in one line.

    Those 2 steps are explained below.

    • Step 1 - Converting Number of Points from user to Double type.
    • Step 2 - Assigned input value to numberOfPoints variable.
' This will handle case for 0 points
If numberOfPoints = 0 Then
  MsgBox "Entered value must be greater than 0. Please try again."
  swDoc.ClearSelection2 True
  Exit Sub
End If
  • In above code block, we check if the input value is zero (0).
  • This check will handle case for 0 Number of Points.
  • We use ๐Ÿš€ IF statement for checking.
  • Condition: numberOfPoints = 0

entering-zera-as-value

  • When this condition is True,
    • We show and ๐Ÿš€ message window to user.
    • Message: Entered value must be greater than 0. Please try again.

    message-to-show-on-zero-given

    • Then we stop our macro here.

Create Reference Point

In this section, we create Reference Point.

' Create Reference Point
swFeature = swDoc.FeatureManager.InsertReferencePoint(swRefPointAlongCurve, swRefPointAlongCurveEvenlyDistributed, 0, numberOfPoints)
  • In above line, we set the value of variable swFeature by InsertReferencePoint method.

  • This InsertReferencePoint method takes following parameters as explained:

    • NRefPointType - Type of Scale as defined in swRefPointType_e:

      Member Description
      swRefPointAlongCurve 2
      swRefPointCenterEdge 3
      swRefPointFaceCenter 4
      swRefPointFaceVertexProjection 5
      swRefPointIntersection 6
      swRefPointInvalid 0
      swRefPointSketchPoint 7
      swRefPointUndefined 1
    • NRefPointAlongCurveType - Distance, percentage, or evenly distributed as defined by swRefPointAlongCurveType_e:

      Member Description
      swRefPointAlongCurveDistance 0
      swRefPointAlongCurveEvenlyDistributed 2
      swRefPointAlongCurvePercentage 1
    • DDistance_or_Percent - Distance at which to create the reference point on the selected entities or percentage of the length of the selected entities at which to create the reference point if NRefPointAlongCurveType is swRefPointAlongCurveDistance or swRefPointAlongCurvePercentage, respectively.

    • NumberOfRefPoints - Number of reference points to create and evenly distribute on the selected entities if swRefPointAlongCurveType is swRefPointAlongCurveEvenlyDistributed.

  • Return Value : This InsertReferencePoint method return ๐Ÿ‘‰ Feature data object.

  • In our code, I have used following values:

    Parameter Name Value Used
    NRefPointType swRefPointAlongCurve
    NRefPointAlongCurveType swRefPointAlongCurveEvenlyDistributed
    DDistance_or_Percent 0
    NumberOfRefPoints numberOfPoints

Reference: For more details about

' Check if Reference Point created or not
If swFeature(0) Is Nothing Then
  MsgBox ("Failed to create Reference Point.")
  Exit Sub
End If
  • In above code block, we check if we successfully create Reference Point or not.
  • We use ๐Ÿš€ IF statement for checking.
  • Condition: swFeature(0) Is Nothing
  • When this condition is True,
    • We show and ๐Ÿš€ message window to user.
    • Message: Failed to create Reference Point.
    • Then we stop our macro here.

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

reference-coordinate-feature-final-result

Final work

In this section, after creating Reference Point, we have to do some cleaning work so that we can use this macro frequently.

' 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 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 Reference Point 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: