Python SolidWorks secondary development---Four ways of traversing components in SolidWorks (summary)

Python SolidWorks secondary development—SolidWorks four ways to traverse components (summary)



Preface

The four built-in traversal methods of SolidWorks have been introduced before, which are implemented using VBA and Python codes respectively. Here is a summary of the actual use effects of these four methods. The following are links to the four traversals. Python SolidWorks secondary
development— Four ways of traversing components in SolidWorks (1)
Python SolidWorks secondary development—Four ways of traversing components in SolidWorks (2)
Python SolidWorks secondary development—Four ways of traversing components in SolidWorks (3)
Python SolidWorks secondary development —Four ways to traverse components in SolidWorks (4)


1. Implementation of four principles of code traversal

1 The first traversal

The first form of traversing components is to first find the component object, and then traverse the components. When the part is traversed, the model tree name of the part is displayed. When the assembly is traversed, , display the model tree name of the assembly, and recurse sub-components until all traversals are completed, and output the model tree name;

2 The second traversal

Bold style The second form of traversing components is to first find the component object, and then traverse the features of the component to determine whether the feature type is "Reference" or "ReferencePattern", indicating that the feature is a component feature, and then Convert the feature to a component and output the model tree display name. Continue to determine whether the component type is an assembly. If it is an assembly, recurse the above steps until all traversals are completed and output the model tree name;

3 The third type of traversal

The third form of traversing components is somewhat similar to the second one. The difference is that it traverses through the nodes of the components. The rest of the process is similar to the second traversal method, except that the judgment conditions are different;

4 The fourth type of traversal

The fourth form of traversing components is different from the above. It directly obtains the full path name tuples of all open files through the API provided by SolidWorks, and then displays them in a loop.

2. Run VBA macro through Python

1 Function definition

Function RunMacro2( _
   ByVal FilePathName As System.String, _
   ByVal ModuleName As System.String, _
   ByVal ProcedureName As System.String, _
   ByVal Options As System.Integer, _
   ByRef Error As System.Integer _
) As System.Boolean

2 Function parameter description

FilePathName: The full path name of the macro
ModuleName The name of the macro module
ProcedureName The name of the function or procedure to be run in the macro module
Options Options for running the macro
Error The error generated after running the macro

3 Example of calling this function in Python

import win32com.client
from swconst import constants
import pythoncom

def main():
    sldver=2018
    swApp=win32com.client.Dispatch(f'SldWorks.Application.{
      
      sldver-1992}')
    swApp.CommandInProgress =True
    swApp.Visible =True
    runMacroError=win32com.client.VARIANT(pythoncom.VT_BYREF | pythoncom.VT_I4, -1)
    boolstatus = swApp.RunMacro2("c:\test\RunMacroSub.swp", "RunMacroSub1", "main",   
                                 constants.swRunMacroUnloadAfterRun, runMacroError)

if __name__ == '__main__':
    main()

3 The purpose of calling this function

This function is called mainly to compare the time taken by various recursive traversals.

3. Comparison of four traversal methods

In order to test the time spent by the four methods, the assembly with 864 parts was traversed uniformly and the names were printed. The time used is shown in the table below. The time used is the average of 8 calculations:

Calling form Time taken (s) advantage shortcoming
VBA macro first way 2.461551 Less time consuming, you can directly operate the component name or configuration name Model tree sequence not guaranteed
VBA macro second way 6.010254 Persistible model tree sequence Takes a little longer
VBA macro third way 9.043458 The model tree sequence can be maintained and nodes can be directly operated. The longest time
The fourth way of VBA macro 0.88086 least time consuming Only the component path name can be obtained and cannot be sorted by the model tree.
The first way to call VBA macro in Python 1.00662 Can't get return value to Python
The second way to call VBA macro in Python 2.29611 Can't get return value to Python
The third way to call VBA macro in Python 8.887979 Can't get return value to Python
The fourth way to call VBA macro in Python 0.302731 Can't get return value to Python
Python first way 3.902943 Less time consuming, you can directly operate the component name or configuration name Model tree sequence not guaranteed
Python second way 14.73471 Keep model tree sequence Takes a little longer
Python third way 80 Keep model tree sequence The longest time
Python fourth way 0.491186 It takes the least time and can directly operate the node. Only the component path name can be obtained and cannot be sorted by the model tree.

Time summary:

  • Python generally takes a long time, except for the fourth one, which is also in line with the characteristics of the Python language;
  • Python calls VBA macros in the shortest time because there is no need to print component names.
  • Of all the calling methods, the fourth is the most time-optimal, while the third is more time-consuming.

Summary of usage scenarios:

  • For efficient traversal, the fourth method can be used
  • For those who have model tree sorting requirements, the second method can be used
  • For node operations, the third method can be used, but this method is very inefficient and is not recommended.
  • Although the first method is relatively time-consuming, it cannot be sorted by model tree. If you have the configuration option to modify, you can use this sorting method.
  • For batch renames, the operation of changing attributes can be directly implemented using the fourth method, which is the most efficient.
  • The second method is most practical for exporting BOM in the form of model tree serial numbers.
  • The way Python calls VBA macros is not suitable for interactive operations with return values.
  • The specific how to use it can be chosen according to the actual use needs. Here is a simple description of the time consumption and functions.

Guess you like

Origin blog.csdn.net/Bluma/article/details/129032729