2016-06-09 79 views
1

我正在編寫一個Python3程序以使用AutoCAD。 我使用pyautocad和comtypes。如何使用comtypes(Python)獲取Acad對象的特定界面

我可以在圖紙上取任何物體並獲得最佳接口。 例如,我可能會爆炸一些塊參照,並與新對象工作的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或維度,我需要AcDbEntity的方法。

那麼,任何人都可以,請指教我怎樣才能得到'最好的',但一個對象的必要接口?作爲一個理想的,它支持的接口列表。

回答

0

這僅僅是與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。只是在說'。

+0

歡迎。你應該接受這個答案,這樣其他人就會發現你的問題知道解決方案是有效的。因爲我得到了它的分;) –

相關問題