在多个选项卡/布局/纸张空间上迭代 Autocad 块属性 python

问题描述 投票:0回答:1

我正在尝试打印多个布局上的块的属性:

import win32com.client
acad = win32com.client.Dispatch("AutoCAD.Application")

# iterate through all objects (entities) in the currently opened drawing
# and if its a BlockReference, display its attributes.
for entity in acad.ActiveDocument.PaperSpace:
    name = entity.EntityName
    if name == 'AcDbBlockReference':
        HasAttributes = entity.HasAttributes
        if HasAttributes:
            for attrib in entity.GetAttributes():
                print("  {}: {}".format(attrib.TagString, attrib.TextString))

输出:

  DRAWING-NO: xxxx
  REV: D
  CI-NO_SHORT: 
  CI-NO_LONG: 
  SCALE: NONE
  SHEET: 2
  TOTAL_SHEETS: 17
  CADFILE: xxxx_D.DWG

您会看到它仅打印活动选项卡中的属性,而不打印其他 16 个属性。

我发现我需要使用 win32com 并且不能使用 pyautocad here。使用 pyautocad,我能够迭代所有布局,但无法访问属性。

python win32com autocad
1个回答
0
投票

我可能建议迭代块集合,然后迭代每个布局容器的组件,例如:

import win32com.client
acad = win32com.client.Dispatch("AutoCAD.Application")

# iterate through all objects (entities) in the currently opened drawing
# and if its a BlockReference, display its attributes.
for block in acad.ActiveDocument.Blocks:
    if block.isLayout and not block.ModelType
        for entity in block:
            name = entity.EntityName
            if name == 'AcDbBlockReference':
                HasAttributes = entity.HasAttributes
                if HasAttributes:
                    for attrib in entity.GetAttributes():
                        print("  {}: {}".format(attrib.TagString, attrib.TextString))
© www.soinside.com 2019 - 2024. All rights reserved.