Description
This macro automates the process of toggle solid body visibility in a SOLIDWORKS model. It works by programmatically selecting the “Solid Bodies” folder (named “Corps volumiques” in French) and then using the HideBodies and ShowBodies methods. This macro is an efficient tool for engineers who need to quickly hide and then show all solid bodies within a part document, streamlining their workflow.
System Requirements
- SolidWorks Version: SolidWorks 2014 or newer
- Operating System: Windows 7 or later
Pre-Conditions
Note
- The active document needs to be a part file with solid bodies.
- To use the macro as-is, the name of the “Solid Bodies” folder needs to be “Corps volumiques” due to it being in its French localization. If not in French, changes will need to be made in the code.
Results
Note
- All the solid bodies of the model will be sequentially hidden and then shown again.
- The macro handles selections automatically. It clears any selection and, if necessary, it will also re-select the “Solid Bodies” folder.
VBA Macro Code
' Disclaimer:
' The code provided should be used at your own risk.
' Blue Byte Systems Inc. assumes no responsibility for any issues or damages that may arise from using or modifying this code.
' For more information, visit [Blue Byte Systems Inc.](https://bluebyte.biz).
Option Explicit
' Main subroutine to toggle visibility of solid bodies
Dim swApp As Object
Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Sub main()
' Initialize the SolidWorks application object
Set swApp = Application.SldWorks
' Get the active document (must be a part)
Set Part = swApp.ActiveDoc
' Select the "Solid Bodies" folder (localized as "Corps volumiques")
boolstatus = Part.Extension.SelectByID2("Corps volumiques", "BDYFOLDER", 0, 0, 0, False, 0, Nothing, 0)
boolstatus = Part.Extension.SelectByID2("Corps volumiques", "BDYFOLDER", 0, 0, 0, False, 0, Nothing, 0)
' Hide all solid bodies in the folder
Part.FeatureManager.HideBodies
' Clear selection to reset
Part.ClearSelection2 True
' Re-select the "Solid Bodies" folder to toggle visibility back
boolstatus = Part.Extension.SelectByID2("Corps volumiques", "BDYFOLDER", 0, 0, 0, False, 0, Nothing, 0)
boolstatus = Part.Extension.SelectByID2("Corps volumiques", "BDYFOLDER", 0, 0, 0, False, 0, Nothing, 0)
' Show all solid bodies in the folder
Part.FeatureManager.ShowBodies
End Sub
You can download the macro from here.
Customization
Does your project have special needs that are not addressed by this macro, such as advanced operations like toggle solid body visibility? We can help! If you need to customize the macro to work with a non-standard file structure, are working with a different language version of SOLIDWORKS, or are working on a solution that requires a fairly complex multi-step process, we are here to assist.
We work with you directly to understand your unique needs and develop a custom solution that fits in your SOLIDWORKS environment. Contact us today to discuss your project and make full use of the power of automation to achieve utmost efficiency.
