【问题标题】:How can I get a specific interface of an Acad object with comtypes (Python)如何使用 comtypes (Python) 获取 Acad 对象的特定接口
【发布时间】:2016-10-09 13:19:25
【问题描述】:

我正在编写一个 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':
    ....

GetBestInterface 方法是完美的,如果我想获得“最佳”接口,它支持像使用特定 Acad 对象(例如,AcDbMText)一样对其进行迭代所需的方法。但是,例如,如果我想分解 MText 或 Dimension,我需要 AcDbEntity 的方法。

那么,请任何人告诉我,我怎样才能获得不是“最好的”而是对象的必要接口?并且,理想情况下,它支持的接口列表。

【问题讨论】:

    标签: python com autocad


    【解决方案1】:

    这仅在 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。只是说说而已。

    【讨论】:

    • 欢迎。您应该接受答案,以便其他偶然发现您的问题的人知道该解决方案有效。因为我得到了积分;)
    猜你喜欢
    • 2013-07-09
    • 1970-01-01
    • 2017-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-12
    • 2019-12-17
    • 2011-01-19
    相关资源
    最近更新 更多