Solidworks VBA Macro - Create Scale
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 Scale feature in SOLIDWORKS CAD Software.
This method is most updated method, so use this method if you want to create a new Scale Feature quickly.
Results We Can Get
After running our macro we successfully create Scale feature as a result.
Below image shows the result we get.
We create Scale Feature in following steps in general.
- Ask for Scale Factor.
To get the correct result please follow the steps correctly.
Macro Video
Below 🎬 video shows Scale feature 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 Scale Feature.
Option Explicit
' Main program for Scale Feature
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 Solidworks document
Dim swDoc As SldWorks.ModelDoc2
' 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. Please open a document.")
Exit Sub
End If
' Variable to hold user input
Dim response As String
' Getting Scale Factor from user.
response = InputBox("Please Enter [Scale Factor]:", "Scale Feature")
' 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
' 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
' Variable for Scale Factor
Dim scaleFactor As Integer
' Set Scale Factor
scaleFactor = CInt(response)
' This will handle case for 0 Scale Factor
If scaleFactor = 0 Then
MsgBox "Entered value must be greater than 0. Please try again."
swDoc.ClearSelection2 True
Exit Sub
End If
' Variable for Solidworks Scale feature
Dim swFeature As SldWorks.Feature
' Create Scale feature
Set swFeature = swDoc.FeatureManager.InsertScale(swScaleType_e.swScaleAboutCentroid, True, scaleFactor, 0, 0)
' Check if Scale Feature creates or not
If swFeature Is Nothing Then
MsgBox ("Failed to create Scale Feature.")
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.
- We are not creating feature from code but we use existing 🚀 Extrude feature to create Scale feature.
Below image shown prerequisite 3D model for our demo.
As shown in above image, there is one Extrude features in our part.
Base Feature
: This is our Base Extrude feature.
If you want to create this Extrude features programmatically then please refer to below article.
We will apply checks in this article, so the code we write should be error free most of the time.
Steps To Follow
This Scale Feature VBA macro can be divided into following sections:
- Create and Initialize required variables
- Get Scale Factor And Validation
- Create Scale feature
- 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 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 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. Please open a document.")
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. Please open a document.
- Then we stop our macro here.
Get Scale Factor And Validation
In this section, we get get the Scale Factor from user and apply some validation on Scale Factor.
' 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 Scale Factor from user
response = InputBox("Please Enter [Scale Factor]:", "Scale Feature")
-
In above line of code we are doing 2 steps in one line.
Those 3 steps are explained below.
- Step 1 - Getting Scale Factor from user.
Below image shows the message for Scale Factor to the user.
- 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.
-
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.
- 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.
- 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.
- Then we stop our macro here.
' Variable for Scale Factor
Dim scaleFactor As Integer
- In above line, we create a variable to store Scale Factor.
- Variable Name:
scaleFactor
- Type:
Integer
' Set Scale Factor
scaleFactor = CInt(response)
-
In above line of code we are doing 2 steps in one line.
Those 2 steps are explained below.
- Step 1 - Converting Scale Factor from user to
Integer
type. - Step 2 - Assigned input value to
scaleFactor
variable.
- Step 1 - Converting Scale Factor from user to
' This will handle case for 0 Scale Factor
If scaleFactor = 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 Scale Factor.
- We use 🚀 IF statement for checking.
- Condition:
scaleFactor = 0
- When this condition is
True
,- We show and 🚀 message window to user.
- Message: Entered value must be greater than 0. Please try again.
- Then we stop our macro here.
Create Scale feature
In this section, we create Scale feature.
' Variable for Solidworks Scale Feature
Dim swFeature As SldWorks.Feature
- Purpose: In above line, we create a variable for Solidworks Scale Feature.
- Variable Name:
swFeature
- Type:
SldWorks.Feature
- Reference: Please visit 🚀 online SOLIDWORKS API Help.
' Create Scale feature
Set swFeature = swDoc.FeatureManager.InsertScale(swScaleType_e.swScaleAboutCentroid, True, scaleFactor, 0, 0)
-
In above line, we set the value of variable
swFeature
byInsertScale
method. -
This
InsertScale
method takes following parameters as explained:-
Type - Type of Scale as defined in
swScaleType_e
:Member Description swScaleAboutCentroid
0 swScaleAboutCoordinateSystem
2 swScaleAboutOrigin
1 -
Uniform -
True
if scaling should be uniform,False
to not -
Xscale - X direction scale factor.
-
Yscale - Y direction scale factor; valid only if Uniform is
False
. -
Zscale - Z direction scale factor; valid only if Uniform is
False
.
-
-
Return Value : This
InsertScale
method return 🚀 Feature data object. -
In our code, I have used following values:
Parameter Name Value Used Type swScaleType_e.swScaleAboutCentroid
Uniform True
Xscale scaleFactor
Yscale 0
Zscale 0
Reference: For more details about
- Solidworks Feature Manager details: 🚀 online Solidworks API Help for Solidworks Feature Manager.
- InsertScale Method: 🚀 online
Solidworks API Help for
InsertScale
Method.
' Check if Scale Feature creates or not
If swFeature Is Nothing Then
MsgBox ("Failed to create Scale Feature.")
swDoc.ClearSelection2 True
Exit Sub
End If
- In above code block, we check if we successfully create Scale Feature 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 Scale Feature.
- Then we clear all selection and stop our macro here.
Now we run the macro and after running macro we get Scale feature as shown in below image.
Final work
In this section, after creating Scale Feature, 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.
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 Scale feature 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!!!