Python SolidWorks 二次开发—SolidWorks四种遍历零部件的方式(四)
前言
零部件的遍历主要是为了方便对零部件的后续操作,例如批量零部件的属性修改,方程式修改等,这里介绍四种SolidWorks自带的遍历方法,本文介绍第四种遍历方式,前三种遍历链接如下
Python SolidWorks 二次开发—SolidWorks四种遍历零部件的方式(一)
Python SolidWorks 二次开发—SolidWorks四种遍历零部件的方式(二)
Python SolidWorks 二次开发—SolidWorks四种遍历零部件的方式(三)
一、官方示例的遍历代码
在SolidWorks官方API帮助文件中,搜索“Get Paths of Open Documents ”,可找到具体的示例代码,详细示例代码如下,此种遍历不同于以往三种遍历方式,此种遍历可遍历出所有已打开加载到内存中的零件的全路径名,之前的三种都是遍历当前激活的零部件名:
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
二、Python遍历代码
此处为方便后续比较,Python代码功能和VBA代码功能保持一致,不进行删减,以下为Python代码示例:
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()