Solidworks VBA Macro - Create Reference Axis
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 Axis in SOLIDWORKS CAD Software.
This method is most updated method, so use this method if you want to create a new Reference Axis quickly.
Results We Can Get
After running our macro we successfully create Reference Axis as a result.
Below image shows the result we get.
We create Reference Axis in following steps in general.
- Create and Initialize required variables
- Create Reference Axis
- Final Work
To get the correct result please follow the steps correctly.
Macro Video
Below 🎬 video shows Reference Axis 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 Axis.
Option Explicit
' Main program for Reference Axis
Sub main()
' Variable for Solidworks application
Dim swApp As SldWorks.SldWorks
' 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
' Variable for storing default part location
Dim defaultTemplate As String
' Setting value of variable to "Default part template"
defaultTemplate = swApp.GetUserPreferenceStringValue(swUserPreferenceStringValue_e.swDefaultTemplatePart)
' Variable for Solidworks document
Dim swDoc As SldWorks.ModelDoc2
' Setting Solidworks document to new part document
Set swDoc = swApp.NewDocument(defaultTemplate, 0, 0, 0)
' Check if Solidworks document is opened or not
If swDoc Is Nothing Then
MsgBox ("Solidworks document is not opened.")
Exit Sub
End If
' Boolean Variable
Dim BoolStatus As Boolean
' Selecting Front Plane
BoolStatus = swDoc.Extension.SelectByID2("Front Plane", "PLANE", 0, 0, 0, False, 0, Nothing, swSelectOption_e.swSelectOptionDefault)
' Check if Front Plane is selected or not
If BoolStatus = False Then
MsgBox ("Failed to select [Front Plane].")
Exit Sub
End If
' Selecting Top Plane
BoolStatus = swDoc.Extension.SelectByID2("Top Plane", "PLANE", 0, 0, 0, True, 0, Nothing, swSelectOption_e.swSelectOptionDefault)
' Check if Top Plane is selected or not
If BoolStatus = False Then
MsgBox ("Failed to select [Top Plane].")
Exit Sub
End If
' Create Reference Axis
BoolStatus = swDoc.InsertAxis2(True)
' Check if Reference Axis creates or not
If BoolStatus = False Then
MsgBox ("Failed to create Reference Axis.")
swDoc.ClearSelection2 True
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 Axis VBA macro can be divided into following sections:
- Create and Initialize required variables
- Create Reference Axis
- 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
In this section we create and initialize required variables.
Option Explicit
- Purpose: Above line forces us to define every variable we are going to use.
- Reference: 🚀 SOLIDWORKS Macros - Open new Part document article.
' Main program for Scale Feature
Sub main()
End Sub
- In above line, we create main program for Scale Feature.
- This is a
Sub
procedure which has name ofmain
. - This procedure hold all the statements (instructions) we give to computer.
- Reference: Detailed information 🚀 VBA Sub and Function Procedures article of this website.
' 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.
Inside this section we initialize required variables.
' 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.
' Variable for storing default part location
Dim defaultTemplate As String
- Purpose: In above line, we create a variable for storing default part location.
- Variable Name:
defaultTemplate
- Type:
String
' Setting value of variable to "Default part template"
defaultTemplate = swApp.GetUserPreferenceStringValue(swUserPreferenceStringValue_e.swDefaultTemplatePart)
- In above line, we set value of
defaultTemplate
variable. - This value is set to “Default part template”.
' 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.
' Set Solidworks document variable to new part document
Set swDoc = swApp.NewDocument(defaultTemplate, 0, 0, 0)
- In above line, we set value of
swDoc
variable. - This value is new 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.
Create Reference Axis
In this section, we create Reference Axis.
' Boolean Variable
Dim BoolStatus As Boolean
- Purpose: In above line, we create a variable Boolean values or function.
- Variable Name:
BoolStatus
- Type:
Boolean
' Selecting Front Plane
BoolStatus = swDoc.Extension.SelectByID2("Front Plane", "PLANE", 0, 0, 0, False, 0, Nothing, swSelectOption_e.swSelectOptionDefault)
- In above line, we select Front plane by
SelectByID2
method. - If we succeed in selecting Front plane, we get
True
otherwise we getFalse
.
' Check if Front Plane is selected or not
If BoolStatus = False Then
MsgBox ("Failed to select [Front Plane].")
Exit Sub
End If
- In above code block, we check if we successfully selected
Front Plane
or not. - We use 🚀 IF statement for checking.
- Condition:
BoolStatus = False
- When this condition is
True
,- We show and 🚀 message window to user.
- Message: Failed to select [Front Plane].
- Then we stop our macro here.
' Selecting Top Plane
BoolStatus = swDoc.Extension.SelectByID2("Top Plane", "PLANE", 0, 0, 0, True, 0, Nothing, swSelectOption_e.swSelectOptionDefault)
- In above line, we select Top plane by
SelectByID2
method. - If we succeed in selecting Top plane, we get
True
otherwise we getFalse
.
' Check if Top Plane is selected or not
If BoolStatus = False Then
MsgBox ("Failed to select [Top Plane].")
Exit Sub
End If
- In above code block, we check if we successfully selected
Top Plane
or not. - We use 🚀 IF statement for checking.
- Condition:
BoolStatus = False
- When this condition is
True
,- We show and 🚀 message window to user.
- Message: Failed to select [Top Plane].
- Then we stop our macro here.
' Create Reference Axis
BoolStatus = swDoc.InsertAxis2(True)
-
In above line, we insert Reference Axis by
InsertAxis2
method from Solidworks Document Object. -
This
InsertAxis2
method takes following parameters as explained:- AutoSize -
True
if axis is to be automatically sized,False
if not.
- AutoSize -
-
Return Value : This
InsertAxis2
method returnTrue
if the Reference Axis is created successfully,False
if not. -
In our code, I have used following values:
Parameter Name Value Used AutoSize True
Reference: For more details about
- Solidworks Feature Manager details: 🚀 online Solidworks API Help for Solidworks Feature Manager.
- InsertAxis2 Method: 🚀 online
Solidworks API Help for
InsertAxis2
Method.
' Check if Reference Axis creates or not
If BoolStatus = False Then
MsgBox ("Failed to create Reference Axis.")
swDoc.ClearSelection2 True
Exit Sub
End If
- In above code block, we check if we successfully create Reference Axis or not.
- We use 🚀 IF statement for checking.
- Condition:
BoolStatus = False
- When this condition is
True
,- We show and 🚀 message window to user.
- Message: Failed to create Reference Axis.
- Then we clear all selection and stop our macro here.
Now we run the macro and after running macro we get Reference Axis as shown in below image.
Final work
In this section, after creating Reference Axis, 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.eswDoc
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.eswDoc
variable.
Error-Solutions
After posting this article, I got to know that code sample is not working for already opened document.
I thought writing a section for these error will be a better idea or future.
Below I list out the error message we got and their probable solution(s).
Error Message 1
Solidworks document is opened error message. Image of this error is shown below.
Cause: Cause of this error is not setting File Location setting for Document Templates.
Please see below image for detail.
After setting Document Templates path, this message will not come.
Error Message 2
Failed to create Reference Axis error message. Image of this error is shown below.
Cause: There might be following case for this error.
- Name of selected plane is different.
You need to confirm plane name first, as I shown in below image.
As shown in above image, plane name is “Front”.
Hence for selecting this plane, I use below code.
' Selecting Front Plane
BoolStatus = swDoc.Extension.SelectByID2("Front", "PLANE", 0, 0, 0, False, 0, Nothing, swSelectOption_e.swSelectOptionDefault)
If your default part have plane name as shown in below image.
Then you need to use below code for selecting Front Plane.
' Selecting Front Plane
BoolStatus = swDoc.Extension.SelectByID2("Front Plane", "PLANE", 0, 0, 0, False, 0, Nothing, swSelectOption_e.swSelectOptionDefault)
Error 3
If you are using code sample provided in this article into an already open document.
And hoping that you will get the result of new Reference Plane in opened document.
Then I suggest you to know few things:
- This code sample, create new part and in that new part it create Reference Axis.
- If you are in this error section, I seriously want you to read all articles I had written. Then you will understand the code and change it to fit your need.
If you still want to create new Reference Axis in already opened document, do following.
' Variable for storing default part location
Dim defaultTemplate As String
' Setting value of variable to "Default part template"
defaultTemplate = swApp.GetUserPreferenceStringValue(swUserPreferenceStringValue_e.swDefaultTemplatePart)
' Variable for Solidworks document
Dim swDoc As SldWorks.ModelDoc2
' Setting Solidworks document to new part document
Set swDoc = swApp.NewDocument(defaultTemplate, 0, 0, 0)
Replace above code, with below code in your macro.
' Variable for Solidworks document
Dim swDoc As SldWorks.ModelDoc2
' Set Solidworks document variable to currently opened document
Set swDoc = swApp.ActiveDoc
I hope this will work.
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 Axis 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!!!