如何使用 comtypes (Python) 获取 Acad 对象的特定接口

How can I get a specific interface of an Acad object with comtypes (Python)

我正在编写一个 Python3 程序来使用 AutoCAD。 我使用 pyautocadcomtypes.

我可以在绘图上采取任何对象并获得其最佳界面。 例如,我可以分解一些块参考并使用 AutoCAD 创建的新对象:

for NewItem in BlockReference.Explode():
  # NewItem is unusable unknown object here
  NewItem = comtypes.client.GetBestInterface(NewItem)
  # Now NewItem is what it is in Acad (text, line or so on)
  if NewItem.ObjectName == 'AcDbMText':
    ....
如果我想获得 'the best' 接口,

GetBestInterface 方法是完美的,它支持与特定 Acad 对象(例如,AcDbMText)一起迭代所需的方法。但是如果我想,例如,分解多行文字或维度,我需要 AcDbEntity.

的方法

所以,任何人都可以告诉我如何才能获得对象的必要接口而不是 'the best' 吗?并且,作为理想,它支持的接口列表。

这仅在 python 2.7:

中测试过
from pyautocad import Autocad, APoint
from comtypes.client import GetBestInterface
from comtypes.gen.AutoCAD import IAcadEntity, IAcadObject

# Get acad application
acad = Autocad(create_if_not_exists=True)
# Create a new document
doc1 = GetBestInterface(acad.Application.Documents.Add())
# add a circle in this document and make it visible
circle = GetBestInterface(doc1.ModelSpace.AddCircle(APoint(0.0, 0.0), 1.0))

# to cast to a different interface:
circle = circle.QueryInterface(IDispatch)
circle = circle.QueryInterface(IAcadEntity)
circle = circle.QueryInterface(IAcadObject)

应该可以,不过。远离CopyObjects。就说吧。