Solidworks VBA Macro - Create Reference Co-Ordinate

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

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.

To get the correct result please follow the steps correctly.

Macro Video

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

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.

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

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

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:

    Parameter Name Value 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 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!!!

Updated: