Solidworks VBA Macro - Create Reference Co-Ordinate

In this article we create and understand VBA macro of Reference Co-Ordinate in SOLIDWORKS CAD Software.

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

Results We Can Get

After running our macro we successfully create Reference Co-Ordinate as a result.

Below image shows the result we get.

reference-coordinate-feature-final-result
Final Result - Reference Coordinate System

We create Reference Co-Ordinate in following steps in general.

  1. Ask User to select an Entity in Model.
  2. Create Reference Co-Ordinate on selected Entity.
Caution

To get the correct result please follow the steps correctly.



Macro Video

Below ๐ŸŽฌ video shows Reference Co-Ordinate from SOLIDWORKS VBA Macros.

How to Create Reference Coordinate System

Important

Please note that there are no explanations in the video.
Explanation of each step and why we write code this way is provided in this post.

VBA Macro

Below is the VBA macro for creating Reference Co-Ordinate.

Option Explicit

' Variable for Solidworks Application
Dim swApp As SldWorks.SldWorks

' Variable for Solidworks document
Dim swDoc As SldWorks.ModelDoc2

' Variable for Solidworks Reference Co-ordinate
Dim swFeature As SldWorks.Feature

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

' Variable for Solidworks Object
Dim swObject As Object

' Program to create Reference Co-ordinate
Sub main()

  ' Set Solidworks Application variable
  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
  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
  Set swSelMgr = swDoc.SelectionManager

  ' Inform user to select a Point for Reference Co-ordinate.
  MsgBox "Please select a Point for Reference Co-ordinate."

  ' 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 is a Sketch
      If swSelMgr.GetSelectedObjectType3(i, -1) = swSelectType_e.swSelEXTSKETCHPOINTS Then

        ' Set the Solidworks Entity object to selected point
        Set swObject = swSelMgr.GetSelectedObject6(i, -1)
      End If
    Next

    DoEvents
  Wend

  ' Create Reference CoOrdinate feature
  Set swFeature = swDoc.FeatureManager.CreateCoordinateSystem(swObject, Nothing, Nothing, Nothing)

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

Note

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 Co-Ordinate VBA macro can be divided into following sections:

  1. Create and Initialize required variables
  2. Ask user to select a Point
  3. Create Reference Co-Ordinate
  4. Final work

Every section with each line is explained below.

Note

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 Co-ordinate
Dim swFeature As SldWorks.Feature
  • Purpose: In above line, we create a variable for Solidworks Reference Co-ordinate.
  • 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 Co-ordinate
Sub main()

End Sub
  • In above line, we create main program for Reference Co-ordinate.
  • 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 a Point

Inside this section we ask user to select a Point and.

' Inform user to select an Object for Co-Ordinate feature.
MsgBox "Please select an Point for Co-Ordinate feature."
  • 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
Select Point Message Dialog
' 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
' Loop until we select
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 is a Sketch
If swSelMgr.GetSelectedObjectType3(i, -1) = swSelectType_e.swSelEXTSKETCHPOINTS Then

  ' Set the Solidworks Entity object to selected point
  Set swObject = swSelMgr.GetSelectedObject6(i, -1)
End If
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.

Create Reference Co-Ordinate

In this section, we create Reference Co-Ordinate.

' Create Reference CoOrdinate feature
Set swFeature = swDoc.FeatureManager.CreateCoordinateSystem(swObject, Nothing, Nothing, Nothing
  • In above line, we set the value of variable swFeature by CreateCoordinateSystem method.

  • This CreateCoordinateSystem method takes following parameters as explained:

    • OriginPointEntity - Entity for the coordinate system origin.
    • XAxisEntities - Array of entities for the X axis.
    • YAxisEntities - Array of entities for the Y axis.
    • ZAxisEntities - Array of entities for the Z axis.
  • Return Value : This CreateCoordinateSystem method return Feature data object.

  • In our code, I have used following values:

CreateCoordinateSystem Parameters
Parameter NameValue Used
OriginPointEntity swObject
XAxisEntities Nothing
YAxisEntities Nothing
ZAxisEntities Nothing

Reference: For more details about

' Check if Reference Co-ordinate created or not
If swFeature Is Nothing Then
  MsgBox ("Failed to create Reference Co-ordinate.")
  Exit Sub
End If
  • In above code block, we check if we successfully create Reference Co-ordinate or not.
  • 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 create Reference Co-ordinate.
    • Then we stop our macro here.

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

reference-coordinate-feature-final-result
Final Result - Reference Coordinate System

Final work

In this section, after creating Reference Co-ordinate, 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 Co-ordinate 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!!!