Python SolidWorks 二次开发---SolidWorks四种遍历零部件的方式(四)

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()
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值