【问题标题】:How to get the TextString property of an MText object?如何获取 MText 对象的 TextString 属性?
【发布时间】:2021-07-20 07:02:42
【问题描述】:

我有一个非常简单的 AutoCAD 绘图,只有两个实体:一个 MText 对象和一个 Polyline 对象,如下面的屏幕截图所示。

我想通过以下代码以编程方式从MText 对象(即字符串'foo')中获取TextString 属性:

import win32com.client

acad = win32com.client.gencache.EnsureDispatch('AutoCAD.Application')
dwg = acad.Documents.Open(r'C:\path\to\my\drawing\test.dwg')
ms = dwg.ModelSpace

for obj in ms:
    if obj.EntityName == 'AcDbMText':
        print(obj.TextString)

但我收到此错误:

Traceback (most recent call last):

  File "<ipython-input-299-e8bbc8ec7f92>", line 3, in <module>
    print(obj.TextString)

  File "D:\anaconda3\lib\site-packages\win32com\client\__init__.py", line 473, in __getattr__
    raise AttributeError("'%s' object has no attribute '%s'" % (repr(self), attr))

AttributeError: '<win32com.gen_py.AutoCAD 2021 Type Library.IAcadEntity instance at 0x2562970331648>' object has no attribute 'TextString'

这很奇怪,因为一年前这段代码运行良好。我在这里错过了什么?

【问题讨论】:

    标签: python activex win32com autocad autolisp


    【解决方案1】:

    我对使用 Python 编写 AutoCAD 知之甚少。据我所知,对于 COM/ActiveX API,MText object 确实有一个 TextString 属性但没有 EntityName 属性,您应该使用ObjectName

    【讨论】:

    • 我没有这个问题,因为print(obj.EntityName) 输出AcDbMText
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-23
    相关资源
    最近更新 更多