Python SolidWorks secondary development---Traversing views (GetViews) in SolidWorks engineering drawings pitfalls

Python SolidWorks secondary development—Traversing views (GetViews) in SolidWorks engineering drawings



Preface

Traversal is an indispensable link in secondary development. Basically, everything that cannot be obtained directly needs to be traversed. The four traversals of components have been summarized before. If you are interested, you can read the previous articles and study the engineering drawings here. In the view traversal, there is a place that needs special treatment. See the following article for details.


1. Traversal of engineering drawings

Paste the code directly, there are comments in the code

import win32com.client
from swconst import constants

def main():
    sldver=2018
    swApp=win32com.client.Dispatch(f'SldWorks.Application.{
      
      sldver-1992}')
    swApp.CommandInProgress =True
    swApp.Visible =True
    # 注意此处当前文档是工程图文档
    swModel = swApp.ActiveDoc
    # 获取所有sheet页以及内部视图对象
    views = swModel.GetViews
    # 遍历工程图中sheet页
    for sheet in views:
        # 遍历每页中的视图
        for view in sheet:
            # 输出视图名称
            print(view.GetName2)
            # 遍历视图中的注释
            for note in view.GetNotes:
                # 输出注释内容
                print(note.GetText)

if __name__ == '__main__':
    main()

2. Error reported after running

The above code will report the following error when running to the second for loop:

AttributeError: ‘PyIDispatch’ object has no attribute ‘GetName2’

3. Cause of the problem

Why does this problem occur? First check the view type and find that it is:

<class ‘PyIDispatch’>

Printing view is the following result:

<PyIDispatch at 0x000001AB1A712DD0 with obj at 0x000001AB1A599CE8>

It means that the object has changed at this time, and naturally the relevant attributes cannot be found.

4. Solutions

The solution is actually very simple, just let the view type return the COM object, and just add the following code:

view=win32com.client.Dispatch(view)

The view type at this time is:

<COMObject >

Related methods can also be called normally, and
the completion code for returning normal values ​​is as follows:

import win32com.client
from swconst import constants

def main():
    sldver=2018
    swApp=win32com.client.Dispatch(f'SldWorks.Application.{
      
      sldver-1992}')
    swApp.CommandInProgress =True
    swApp.Visible =True
    # 注意此处当前文档是工程图文档
    swModel = swApp.ActiveDoc
    # 获取所有sheet页以及内部视图对象
    views = swModel.GetViews
    # 遍历工程图中sheet页
    for sheet in views:
        # 遍历每页中的视图
        for view in sheet:
            # 新增行
            view=win32com.client.Dispatch(view)
            # 输出视图名称
            print(view.GetName2)
            # 遍历视图中的注释
            for note in view.GetNotes:
                # 输出注释内容
                print(note.GetText)

if __name__ == '__main__':
    main()

5. Problem summary

The reason for the problem is actually very simple, that is, the object interface called is incorrect. It can be easily solved by finding the corresponding interface and calling its properties.

Guess you like

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