Solidworks VBA Macro - Hide Component
Objective
In this article, we understand “how to” Hide selected Component in Assembly document from VBA macro.
This is most updated method of Hiding selected Component in an assembly document.
Results We Can Get
Below image shows the result we get.
We Hide selected Component 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 Hide selected Component 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 Hide selected Component.
Option Explicit
' Variable for Solidworks Application
Dim swApp As SldWorks.SldWorks
' Variable for Solidworks document
Dim swDoc As SldWorks.ModelDoc2
' Variable for Solidworks Component
Dim swComponent As SldWorks.Component2
' Program to Hide Selected Component part
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
' Get the selected component and set it to the Solidworks Component variable
Set swComponent = swDoc.SelectionManager.GetSelectedObject6(1, -1)
' If there are error
If swComponent Is Nothing Then
' Inform user and exit function.
MsgBox "Failed to get selected part."
Exit Sub
End If
' Select the component
swComponent.Select False
' Hide selected component
swDoc.HideComponent2
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.
- We select the part which we want to Hide.
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
- Hide selected Component
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 Component
Dim swComponent As SldWorks.Component2
- Purpose: In above line, we create a variable for Solidworks Component.
- Variable Name:
swComponent
- Type:
SldWorks.Component2
. - Reference: Please visit 🚀 online SOLIDWORKS API Help.
These all are our global variables.
They are SOLIDWORKS API Objects.
' Program to Hide Selected Component part
Sub main()
End Sub
- In above line, we create Program to Hide Selected Component part.
- 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.
Hide selected Component
In this section, we perform Hide selected Component action.
' Get the selected face and set it to the Solidworks Face variable
Set swComponent = swDoc.SelectionManager.GetSelectedObject6(1, -1)
- In above line, we set value of
swComponent
variable. - We set the value from
GetSelectedObject6
method. - This
GetSelectedObject6
method is part ofSelectionManager
object. - This
SelectionManager
object is part of swDoc object.
' If there are error
If swComponent Is Nothing Then
' Inform user and exit function.
MsgBox "Failed to get selected part."
Exit Sub
End If
- In above code block, we check if we successfully set the value of
swComponent
variable. - We use 🚀 IF statement for checking.
- Condition:
swComponent Is Nothing
- When this condition is
True
,- We show and 🚀 message window to user.
- Message: Failed to get selected part.
- Then we stop our macro here.
' Select the component
swComponent.Select False
- In above line, we select the component.
- We use
Select
method ofswComponent
variable.
' Hide selected component
swDoc.HideComponent2
- In above line, we Hide selected component.
- We use
HideComponent2
method ofswDoc
variable. - This method return nothing.
Now we run the macro and after running macro we hide 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 Hide selected Component 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!!!