diff --git a/docs/index.json b/docs/index.json index b42f0fe..3a7c1f9 100644 --- a/docs/index.json +++ b/docs/index.json @@ -28,5 +28,10 @@ "href": "src/introduction.html", "title": "| 🧩 SOLIDWORKS VBA Macros Library", "keywords": "Introduction to VBA Macros in SOLIDWORKS What is a VBA Macro? A VBA (Visual Basic for Applications) macro is a small program written using the VBA programming language, designed to automate repetitive tasks within software applications. In the context of SOLIDWORKS, VBA macros allow users to streamline their workflows by automating design tasks, reducing manual input, and increasing overall productivity. Instead of performing the same actions manually, a macro can execute them in seconds, saving time and minimizing human error. Why Use VBA Macros in SOLIDWORKS? SOLIDWORKS is a powerful CAD software used by engineers and designers to create complex 3D models and assemblies. However, as with any software, there are tasks that are repetitive or prone to error when performed manually. This is where VBA macros come in handy—they enable users to automate tasks such as: Modifying properties or configurations across multiple files Generating drawings or BOMs (Bill of Materials) Editing or creating features in parts and assemblies Exporting data to various formats such as DXF, PDF, or Excel Using VBA macros can significantly improve your efficiency, especially when dealing with large assemblies, multi-part projects, or standardized workflows. Instead of spending time on repetitive actions, users can focus on innovation and improving their designs. Getting Started with SOLIDWORKS VBA Macros To use VBA macros in SOLIDWORKS, you can either write your own macros or use existing ones provided by various sources, such as Blue Byte Systems’ SOLIDWORKS VBA Macro Library. The library offers a collection of macros that automate common tasks, each categorized for easy access based on functionality (e.g., part creation, assembly management, drawing automation). How to Create or Run a VBA Macro in SOLIDWORKS Open the VBA Editor: Go to the Tools menu in SOLIDWORKS, then navigate to Macro and select New to create a new macro, or Run to execute an existing one. Write or Edit the Macro: If you’re creating a new macro, the VBA editor will open, allowing you to write your code. Macros consist of VBA code that interfaces with SOLIDWORKS' API (Application Programming Interface) to control different aspects of the application. Execute the Macro: Once the macro is written or selected, you can run it directly within SOLIDWORKS. The macro will perform the tasks it's programmed for, interacting with the current part, assembly, or drawing. Test and Debug: Always test macros on sample files to ensure they work as expected, and make necessary adjustments to the code if any issues arise. Free VBA Macro Library by Blue Byte Systems Blue Byte Systems provides a comprehensive VBA macro library tailored for SOLIDWORKS users. These macros are designed to help users automate tasks in various areas, including part modeling, assembly management, and sketching. The library is completely free, making it accessible to all SOLIDWORKS users, from beginners to advanced engineers. The macros are organized into categories for easy navigation: Part Automation: Macros related to modifying or creating parts. Assembly Automation: Macros to streamline assembly management. Drawing Automation: Macros focused on automating drawing creation and export. Sketch Automation: Macros for working with sketches more efficiently. Whether you're looking to automate repetitive tasks, modify part properties, or generate drawings quickly, the library offers solutions that save you time and enhance your productivity." + }, + "src/traverse_assembly_component_tree_recursively_solidworks_vba_macro.html": { + "href": "src/traverse_assembly_component_tree_recursively_solidworks_vba_macro.html", + "title": "Traverse Assembly Components in SOLIDWORKS Using VBA | 🧩 SOLIDWORKS VBA Macros Library", + "keywords": "Traverse Assembly Components in SOLIDWORKS Using VBA Macro Description This article explains how to write a VBA macro in SOLIDWORKS to traverse through an assembly's components and list their hierarchy. The macro explores the assembly structure, identifies components, and logs their names with proper indentation to reflect their parent-child relationship within the assembly. VBA Macro Code ' ******************************************************************** ' DISCLAIMER: ' This code is provided as-is with no warranty or liability by ' Blue Byte Systems Inc. The company assumes no responsibility for ' any issues arising from the use of this code in production. ' ******************************************************************** Dim swApp As SldWorks.SldWorks Dim swRootAssemblyModelDoc As ModelDoc2 Sub main() Set swApp = Application.SldWorks swApp.CommandInProgress = True Set swRootAssemblyModelDoc = swApp.ActiveDoc Dim swFeature As Feature Set swFeature = swRootAssemblyModelDoc.FirstFeature While Not swFeature Is Nothing TraverseFeatureForComponents swFeature Set swFeature = swFeature.GetNextFeature Wend swApp.CommandInProgress = False End Sub Private Sub TraverseFeatureForComponents(ByVal swFeature As Feature) Dim swSubFeature As Feature Dim swComponent As Component2 Dim typeName As String typeName = swFeature.GetTypeName2 If typeName = \"Reference\" Then Set swComponent = swFeature.GetSpecificFeature2 If Not swComponent Is Nothing Then LogComponentName swComponent Set swSubFeature = swComponent.FirstFeature() While Not swSubFeature Is Nothing TraverseFeatureForComponents swSubFeature Set swSubFeature = swSubFeature.GetNextFeature() Wend End If End If End Sub Private Sub LogComponentName(ByVal swComponent As Component2) Dim parentCount As Long Dim swParentComponent As Component2 Set swParentComponent = swComponent.GetParent() While Not swParentComponent Is Nothing parentCount = parentCount + 1 Set swParentComponent = swParentComponent.GetParent() Wend Dim indentation As String indentation = Replicate(\" \", parentCount) Debug.Print indentation & Split(swComponent.GetPathName(), \"\\\")(UBound(Split(swComponent.GetPathName(), \"\\\"))) End Sub Public Function Replicate(RepeatString As String, ByVal NumOfTimes As Long) If NumOfTimes = 0 Then Replicate = \"\" Exit Function End If Dim s As String Dim c As Long Dim l As Long Dim i As Long l = Len(RepeatString) c = l * NumOfTimes s = Space$(c) For i = 1 To c Step l Mid(s, i, l) = RepeatString Next Replicate = s End Function System Requirements To run this VBA macro, ensure that your system meets the following requirements: SOLIDWORKS Version: SOLIDWORKS 2017 or later VBA Environment: Pre-installed with SOLIDWORKS (Access via Tools > Macro > New or Edit) Operating System: Windows 7, 8, 10, or later Customization Need to modify the macro to meet specific requirements or integrate it with other processes? We provide custom macro development tailored to your needs. Contact us." } } \ No newline at end of file diff --git a/docs/manifest.json b/docs/manifest.json index 2e8e92a..a6d6a0d 100644 --- a/docs/manifest.json +++ b/docs/manifest.json @@ -118,6 +118,16 @@ }, "version": "" }, + { + "type": "Conceptual", + "source_relative_path": "src/traverse_assembly_component_tree_recursively_solidworks_vba_macro.md", + "output": { + ".html": { + "relative_path": "src/traverse_assembly_component_tree_recursively_solidworks_vba_macro.html" + } + }, + "version": "" + }, { "type": "Toc", "source_relative_path": "toc.yml", diff --git a/docs/sitemap.xml b/docs/sitemap.xml index 2bc5d7c..5e9946a 100644 --- a/docs/sitemap.xml +++ b/docs/sitemap.xml @@ -2,37 +2,43 @@ https://solidworksvbamacros.bluebyte.biz/README.html - 2024-09-12T01:11:03-07:00 + 2024-09-12T05:06:53-07:00 daily 0.5 https://solidworksvbamacros.bluebyte.biz/index.html - 2024-09-12T01:11:03-07:00 + 2024-09-12T05:06:53-07:00 daily 0.5 https://solidworksvbamacros.bluebyte.biz/src/addmasscenter.html - 2024-09-12T01:11:03-07:00 + 2024-09-12T05:06:53-07:00 daily 0.5 https://solidworksvbamacros.bluebyte.biz/src/assembly_export_dxf.html - 2024-09-12T01:11:03-07:00 + 2024-09-12T05:06:53-07:00 daily 0.5 https://solidworksvbamacros.bluebyte.biz/src/exportbomtoexcelwiththumbnails.html - 2024-09-12T01:11:03-07:00 + 2024-09-12T05:06:53-07:00 daily 0.5 https://solidworksvbamacros.bluebyte.biz/src/introduction.html - 2024-09-12T01:11:03-07:00 + 2024-09-12T05:06:53-07:00 + daily + 0.5 + + + https://solidworksvbamacros.bluebyte.biz/src/traverse_assembly_component_tree_recursively_solidworks_vba_macro.html + 2024-09-12T05:06:53-07:00 daily 0.5 diff --git a/docs/src/toc.html b/docs/src/toc.html index b075338..b4bb477 100644 --- a/docs/src/toc.html +++ b/docs/src/toc.html @@ -42,6 +42,9 @@
  • Export All SheetMetal Components to DXF
  • +
  • + Traverse Assembly Component Tree Recursively +
  • diff --git a/docs/src/toc.json b/docs/src/toc.json index 1b1b494..a778134 100644 --- a/docs/src/toc.json +++ b/docs/src/toc.json @@ -1,2 +1,2 @@ -{"items":[{"name":"🧩 SOLIDWORKS VBA Macros"},{"name":"Introduction","href":"introduction.html","topicHref":"introduction.html"},{"name":"Part Documents"},{"name":"Part Documents","items":[{"name":"Add Mass Center","href":"addmasscenter.html","topicHref":"addmasscenter.html"}],"expanded":true},{"name":"Assembly Documents"},{"name":"Assembly Documents","items":[{"name":"Export All SheetMetal Components to DXF","href":"assembly_export_dxf.html","topicHref":"assembly_export_dxf.html"}],"expanded":true},{"name":"Bill Of Materials"},{"name":"Bill Of Materials","items":[{"name":"Export BOM To Excel with Thumbnails","href":"exportbomtoexcelwiththumbnails.html","topicHref":"exportbomtoexcelwiththumbnails.html"}],"expanded":true}],"pdf":false} +{"items":[{"name":"🧩 SOLIDWORKS VBA Macros"},{"name":"Introduction","href":"introduction.html","topicHref":"introduction.html"},{"name":"Part Documents"},{"name":"Part Documents","items":[{"name":"Add Mass Center","href":"addmasscenter.html","topicHref":"addmasscenter.html"}],"expanded":true},{"name":"Assembly Documents"},{"name":"Assembly Documents","items":[{"name":"Export All SheetMetal Components to DXF","href":"assembly_export_dxf.html","topicHref":"assembly_export_dxf.html"},{"name":"Traverse Assembly Component Tree Recursively","href":"traverse_assembly_component_tree_recursively_solidworks_vba_macro.html","topicHref":"traverse_assembly_component_tree_recursively_solidworks_vba_macro.html"}],"expanded":true},{"name":"Bill Of Materials"},{"name":"Bill Of Materials","items":[{"name":"Export BOM To Excel with Thumbnails","href":"exportbomtoexcelwiththumbnails.html","topicHref":"exportbomtoexcelwiththumbnails.html"}],"expanded":true}],"pdf":false} diff --git a/docs/src/traverse_assembly_component_tree_recursively_solidworks_vba_macro.html b/docs/src/traverse_assembly_component_tree_recursively_solidworks_vba_macro.html new file mode 100644 index 0000000..0ee6f6c --- /dev/null +++ b/docs/src/traverse_assembly_component_tree_recursively_solidworks_vba_macro.html @@ -0,0 +1,225 @@ + + + + + Traverse Assembly Components in SOLIDWORKS Using VBA | 🧩 SOLIDWORKS VBA Macros Library + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    + +
    + +
    +
    +
    +
    +
    Table of Contents
    + +
    +
    + +
    +
    +
    + +
    +
    + + + +
    + +
    +

    Traverse Assembly Components in SOLIDWORKS Using VBA

    + +

    Macro Description

    +

    This article explains how to write a VBA macro in SOLIDWORKS to traverse through an assembly's components and list their hierarchy. The macro explores the assembly structure, identifies components, and logs their names with proper indentation to reflect their parent-child relationship within the assembly.

    +

    VBA Macro Code

    +
    ' ********************************************************************
    +' DISCLAIMER: 
    +' This code is provided as-is with no warranty or liability by 
    +' Blue Byte Systems Inc. The company assumes no responsibility for 
    +' any issues arising from the use of this code in production.
    +' ********************************************************************
    +  Dim swApp As SldWorks.SldWorks
    +Dim swRootAssemblyModelDoc As ModelDoc2
    + 
    +
    +Sub main()
    +
    +    Set swApp = Application.SldWorks
    +    
    +    swApp.CommandInProgress = True
    +    
    +    Set swRootAssemblyModelDoc = swApp.ActiveDoc
    +    
    +    Dim swFeature As Feature
    +    
    +    Set swFeature = swRootAssemblyModelDoc.FirstFeature
    +           
    +    While Not swFeature Is Nothing
    +     TraverseFeatureForComponents swFeature
    +     Set swFeature = swFeature.GetNextFeature
    +    Wend
    +    
    +    
    +    swApp.CommandInProgress = False
    +    
    +End Sub
    +
    +Private Sub TraverseFeatureForComponents(ByVal swFeature As Feature)
    +    Dim swSubFeature As Feature
    +                
    +    Dim swComponent As Component2
    +    
    +    Dim typeName As String
    +    
    +    typeName = swFeature.GetTypeName2
    +   
    +    If typeName = "Reference" Then
    +        Set swComponent = swFeature.GetSpecificFeature2
    +         
    +        If Not swComponent Is Nothing Then
    +         
    +         LogComponentName swComponent
    +           
    +           Set swSubFeature = swComponent.FirstFeature()
    +             While Not swSubFeature Is Nothing
    +                TraverseFeatureForComponents swSubFeature
    +                Set swSubFeature = swSubFeature.GetNextFeature()
    +             Wend
    +        End If
    +    End If
    +End Sub
    +
    +Private Sub LogComponentName(ByVal swComponent As Component2)
    +    Dim parentCount As Long
    +    
    +    Dim swParentComponent As Component2
    +    Set swParentComponent = swComponent.GetParent()
    +    
    +    While Not swParentComponent Is Nothing
    +     parentCount = parentCount + 1
    +     Set swParentComponent = swParentComponent.GetParent()
    +    Wend
    +     
    +    Dim indentation As String
    +    indentation = Replicate(" ", parentCount)
    +    Debug.Print indentation & Split(swComponent.GetPathName(), "\")(UBound(Split(swComponent.GetPathName(), "\")))
    +End Sub
    +        
    +Public Function Replicate(RepeatString As String, ByVal NumOfTimes As Long)
    +
    +    If NumOfTimes = 0 Then
    +     Replicate = ""
    +     Exit Function
    +    End If
    +
    +    Dim s As String
    +    Dim c As Long
    +    Dim l As Long
    +    Dim i As Long
    +
    +    l = Len(RepeatString)
    +    c = l * NumOfTimes
    +    s = Space$(c)
    +
    +    For i = 1 To c Step l
    +        Mid(s, i, l) = RepeatString
    +    Next
    +
    +    Replicate = s
    + 
    +End Function
    +
    +

    System Requirements

    +

    To run this VBA macro, ensure that your system meets the following requirements:

    +
      +
    • SOLIDWORKS Version: SOLIDWORKS 2017 or later
    • +
    • VBA Environment: Pre-installed with SOLIDWORKS (Access via Tools > Macro > New or Edit)
    • +
    • Operating System: Windows 7, 8, 10, or later
    • +
    +

    Customization

    +

    Need to modify the macro to meet specific requirements or integrate it with other processes? We provide custom macro development tailored to your needs. Contact us.

    + +
    + + + + + +
    + +
    + +
    +
    + +
    + + + + diff --git a/src/toc.yml b/src/toc.yml index 46e5159..ce11e09 100644 --- a/src/toc.yml +++ b/src/toc.yml @@ -11,15 +11,14 @@ items: - name: Add Mass Center href: addmasscenter.md - - - name: Assembly Documents - name: Assembly Documents expanded: true items: - name: Export All SheetMetal Components to DXF href: assembly_export_dxf.md - + - name: Traverse Assembly Component Tree Recursively + href: traverse_assembly_component_tree_recursively_solidworks_vba_macro.md - name: Bill Of Materials - name: Bill Of Materials diff --git a/src/traverse_assembly_component_tree_recursively_solidworks_vba_macro.md b/src/traverse_assembly_component_tree_recursively_solidworks_vba_macro.md new file mode 100644 index 0000000..bc5396c --- /dev/null +++ b/src/traverse_assembly_component_tree_recursively_solidworks_vba_macro.md @@ -0,0 +1,118 @@ +# Traverse Assembly Components in SOLIDWORKS Using VBA + +## Macro Description + +This article explains how to write a VBA macro in SOLIDWORKS to traverse through an assembly's components and list their hierarchy. The macro explores the assembly structure, identifies components, and logs their names with proper indentation to reflect their parent-child relationship within the assembly. + + +## VBA Macro Code + +```vbnet +' ******************************************************************** +' DISCLAIMER: +' This code is provided as-is with no warranty or liability by +' Blue Byte Systems Inc. The company assumes no responsibility for +' any issues arising from the use of this code in production. +' ******************************************************************** + Dim swApp As SldWorks.SldWorks +Dim swRootAssemblyModelDoc As ModelDoc2 + + +Sub main() + + Set swApp = Application.SldWorks + + swApp.CommandInProgress = True + + Set swRootAssemblyModelDoc = swApp.ActiveDoc + + Dim swFeature As Feature + + Set swFeature = swRootAssemblyModelDoc.FirstFeature + + While Not swFeature Is Nothing + TraverseFeatureForComponents swFeature + Set swFeature = swFeature.GetNextFeature + Wend + + + swApp.CommandInProgress = False + +End Sub + +Private Sub TraverseFeatureForComponents(ByVal swFeature As Feature) + Dim swSubFeature As Feature + + Dim swComponent As Component2 + + Dim typeName As String + + typeName = swFeature.GetTypeName2 + + If typeName = "Reference" Then + Set swComponent = swFeature.GetSpecificFeature2 + + If Not swComponent Is Nothing Then + + LogComponentName swComponent + + Set swSubFeature = swComponent.FirstFeature() + While Not swSubFeature Is Nothing + TraverseFeatureForComponents swSubFeature + Set swSubFeature = swSubFeature.GetNextFeature() + Wend + End If + End If +End Sub + +Private Sub LogComponentName(ByVal swComponent As Component2) + Dim parentCount As Long + + Dim swParentComponent As Component2 + Set swParentComponent = swComponent.GetParent() + + While Not swParentComponent Is Nothing + parentCount = parentCount + 1 + Set swParentComponent = swParentComponent.GetParent() + Wend + + Dim indentation As String + indentation = Replicate(" ", parentCount) + Debug.Print indentation & Split(swComponent.GetPathName(), "\")(UBound(Split(swComponent.GetPathName(), "\"))) +End Sub + +Public Function Replicate(RepeatString As String, ByVal NumOfTimes As Long) + + If NumOfTimes = 0 Then + Replicate = "" + Exit Function + End If + + Dim s As String + Dim c As Long + Dim l As Long + Dim i As Long + + l = Len(RepeatString) + c = l * NumOfTimes + s = Space$(c) + + For i = 1 To c Step l + Mid(s, i, l) = RepeatString + Next + + Replicate = s + +End Function +``` + +## System Requirements +To run this VBA macro, ensure that your system meets the following requirements: + +- SOLIDWORKS Version: SOLIDWORKS 2017 or later +- VBA Environment: Pre-installed with SOLIDWORKS (Access via Tools > Macro > New or Edit) +- Operating System: Windows 7, 8, 10, or later + + +## Customization +Need to modify the macro to meet specific requirements or integrate it with other processes? We provide custom macro development tailored to your needs. [Contact us](https://bluebyte.biz/contact). \ No newline at end of file