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

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



Preface

The traversal of components is mainly to facilitate subsequent operations on components, such as property modification of batch components, equation modification, etc. Here are four traversal methods that come with SolidWorks. This article introduces the fourth traversal method and the first three traversals. The link is as follows
Python SolidWorks secondary development—SolidWorks four ways to traverse components (1)
Python SolidWorks secondary development—SolidWorks four ways to traverse components (2)
Python SolidWorks secondary development—SolidWorks four ways to traverse components (three)


1. Official sample traversal code

In the SolidWorks official API help file, search for "Get Paths of Open Documents" to find specific sample code. The detailed sample code is as follows. This type of traversal is different from the previous three traversal methods. This type of traversal can traverse all open loads. To the full path name of the part in memory, the previous three methods traverse the currently activated component name:

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim vModels As Variant
Dim count As Long
Dim index As Long
Sub main()
Set swApp = Application.SldWorks
count = swApp.GetDocumentCount
Debug.Print "Number of open documents in this SolidWork session: " & count
vModels = swApp.GetDocuments
For index = LBound(vModels) To UBound(vModels)
    Set swModel = vModels(index)
    Debug.Print "Path and name of open document: " & swModel.GetPathName
    Next index
End Sub

2. Python traversal code

To facilitate subsequent comparison, the functions of the Python code and the functions of the VBA code remain the same and will not be deleted. The following is an example of the Python code:

import win32com.client

def main():
    sldver=2018
    swApp=win32com.client.Dispatch(f'SldWorks.Application.{
      
      sldver-1992}')
    swApp.CommandInProgress =True
    swApp.Visible =True
    vModels = swApp.GetDocuments
    print(f"已打开文件数量为:{
      
      len(vModels)}")
    for swModel in vModels:
        print(f"文件路径名:{
      
      swModel.GetPathName}")

if __name__ == '__main__':
    main()

Guess you like

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