Solidworks VBA Macro - Insert Folder
Objective
In this article, we understand โhow toโ Insert Folder in Assembly document from VBA macro.
We will insert an empty folder in Feature Tree.
This is most updated method of Insert Folder in an assembly document.
Results We Can Get
Below image shows the result we get.
We Insert Folder in simple manners.
There are no extra steps required.
To get the correct result, please follow the steps correctly.
Macro Video
Below ๐ฌ video shows how to Insert Folder from SOLIDWORKS VBA Macros.
Above video is just for visualization and there is no explanation.
I have explained every line in this article.
It is advisable to watch video, since it helps you to better understand the process.
VBA Macro
Below is the VBA macro for Insert Folder.
Option Explicit
' Variable for Solidworks Application
Dim swApp As SldWorks.SldWorks
' Variable for Solidworks document
Dim swDoc As SldWorks.ModelDoc2
' Variable for Solidworks Feature Manager
Dim swFeatureManager As SldWorks.FeatureManager
' Variable for Solidworks Feature
Dim feature As SldWorks.feature
' Program to Add New Folder
Sub main()
' Set Solidworks Application variable to current application
Set swApp = Application.SldWorks
' 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 Feature manager
Set swFeatureManager = swDoc.FeatureManager
' Insert Folder
Set feature = swFeatureManager.InsertFeatureTreeFolder2(swFeatureTreeFolder_EmptyBefore)
' Check if Folder is added or not
If feature Is Nothing Then
MsgBox "Failed to Insert Folder."
Exit Sub
End If
' Variable to store user response
Dim response As String
response = InputBox("Folder Name:", "Folder")
' 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
' Update Folder Name
feature.Name = response
End Sub
Prerequisite
There are some prerequisites for this article.
- Knowledge of VBA programming language is โrequired.
- We use existing parts in Assembly document.
- Both components are fully constraint as shown in below image.
- For inserting folder atleast one part must be selected. Otherwise this method will not work.
We will apply checks in this article, so the code we write, should be error free mostly.
Steps To Follow
This VBA macro can be divided into following sections:
- Create Global Variables
- Initialize Global Variables
- Insert Folder
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 Global Variables
In this section, we create global 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.
' 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 Feature Manager
Dim swFeatureManager As SldWorks.FeatureManager
- Purpose: In above line, we create a variable for Solidworks Feature Manager.
- Variable Name:
swFeatureManager
- Type:
SldWorks.FeatureManager
- Reference: Please visit ๐ online SOLIDWORKS API Help.
' Variable for Solidworks Feature
Dim feature As SldWorks.feature
- Purpose: In above line, we create a variable for Solidworks Feature.
- Variable Name:
feature
- Type:
SldWorks.feature
. - Reference: Please visit ๐ online SOLIDWORKS API Help.
These all are our global variables.
They are SOLIDWORKS API Objects.
' Program to Add New Folder
Sub main()
End Sub
- In above line, we create Program to Add New Folder.
- 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.
Initialize Global Variables
In this section, we initialize global 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.
' 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.
Insert Folder
In this section, we perform UnSuppress Component action.
' Set Feature manager
Set swFeatureManager = swDoc.FeatureManager
- In above line, we set value of
swFeatureManager
variable. - We set value to
FeatureManager
property ofswDoc
variable.
' Insert Folder
Set feature = swFeatureManager.InsertFeatureTreeFolder2(swFeatureTreeFolder_EmptyBefore)
- In above line, we โInsert Folderโ by setting the value of
feature
variable. - This
InsertFeatureTreeFolder2()
method is part ofswFeatureManager
variable. - This
InsertFeatureTreeFolder2()
method takes following parameter.-
Type: Type of folder as defined in
swFeatureTreeFolderType_e
in below table.Member Description swFeatureTreeFolder_Containing
2 = Create and insert a folder to contain the pre-selected features swFeatureTreeFolder_EmptyBefore
1 = Create and insert an empty folder before the pre-selected features swFeatureTreeFolder_Mold
3
-
-
Return Value : This
InsertFeatureTreeFolder2()
method return ๐ feature data object. -
In our code, I have used following values:
Parameter Name Value Used Type swFeatureTreeFolder_EmptyBefore
Reference: For more details about
- Solidworks FeatureManager details: ๐ online Solidworks API Help for Solidworks Feature Manager.
- InsertFeatureTreeFolder2 Method: ๐ online
Solidworks API Help for
InsertFeatureTreeFolder2
Method.
' Check if Folder is added or not
If feature Is Nothing Then
MsgBox "Failed to Insert Folder."
Exit Sub
End If
- In above code block, we check if we successfully added Insert Folder or not.
- We use ๐ IF statement for checking.
- Condition:
feature Is Nothing
- When this condition is
True
,- We show and ๐ message window to user.
- Message: Failed to Insert Folder.
- Then we stop our macro here.
' Variable to hold user input
Dim response As String
- In above line, we create a variable as a counter.
- Variable Name:
response
- Type:
String
' Getting Folder Name from user.
response = InputBox("Folder Name:", "Folder")
-
In above line of code we are doing 2 steps in one line.
Those 2 steps are explained below.
- Step 1 - Getting Folder Name from user.
Below image shows the message for Folder Name 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."
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.
' Update Folder Name
feature.Name = response
- In above line, we set value of
Name
property offeature
variable. - We set value to
response
, which we asked from user.
Now we run the macro and after running macro we show selected component as shown in below image.
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 Insert Folder 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!!!