如何 BlockReference 对象属性
How to BlockReference object attributes
问题是 BlockReference 对象上的字段 DEPT_NAME 未获取值。
如何获取值
我尝试了给定的代码但没有得到任何结果
from __future__ import print_function
from os.path import join, dirname, abspath
from xlutils.copy import copy
import xlrd
import xlwt
from pyautocad import Autocad, APoint
import os
import win32com.client
from pyautocad import Autocad, APoint
from pyautocad.contrib.tables import Table
from comtypes import COMError
def props(cls):
return [i for i in cls.__dict__.keys() if i[:1] != '_']
# Create workbook
book = xlwt.Workbook()
ws = book.add_sheet("ExportedData")
book.save("Exported.xls")
# Open the workbook
xl_workbook = xlrd.open_workbook("Exported.xls")
sheet_names = xl_workbook.sheet_names()
xl_sheet = xl_workbook.sheet_by_name(sheet_names[0])
wb = copy(xl_workbook)
sheet = wb.get_sheet(0)
dwgfiles = filter(os.path.isfile, os.listdir(os.curdir))
cwd = os.path.abspath(os.path.curdir) # current working dir
print(cwd)
for f in dwgfiles:
print("++++++++++++++++++++++++++++++")
print("++++++++++++++++++++++++++++++")
print("++++++++++++++++++++++++++++++")
print("++++++++++++++++++++++++++++++")
print(f)
if f.endswith(".dwg"):
print("sdaasdas")
""" open Document"""
acad = Autocad()
print(cwd)
acad.app.Documents.open(cwd + "/" + f)
print(acad.doc.Name)
num_cols = xl_sheet.ncols # Number of columns
idx = 1
acad = win32com.client.Dispatch("AutoCAD.Application")
doc = acad.ActiveDocument # Document object
print("MODEL SPACE")
count=0
for entity in acad.ActiveDocument.ModelSpace:
name = entity.EntityName
print(name)
if name == 'AcDbBlockReference':
HasAttributes = entity.HasAttributes
if HasAttributes:
# sheet.row(idx).write(0, entity.TextString)
sheet.row(idx).write(1, entity.ObjectID)
sheet.row(idx).write(2, cwd + "/" + f)
sheet.row(idx).write(3, str(entity.InsertionPoint))
sheet.row(idx).write(4, entity.Name)
sheet.row(idx).write(5,entity.Layer)
idx = idx + 1
print(entity.Name)
print(entity.Layer)
print(entity.ObjectID)
k=0
#print(entity.get_attrib_text('DEPT_NAME'))
for attrib in entity.GetAttributes():
print(attrib.DEPT_NAME)
print("+++++++")
exit()
print(count)
doc.Close(False)
acad = None
wb.save("Exported.xls")
我得到的错误是这个
追溯(最近一次通话):
文件 "auto1.py",第 78 行,位于
打印(attrib.DEPT_NAME)
文件 "D:\autocad_test\venv\lib\site-packages\win32com\client\dynamic.py",第 527 行,在 getattr 中
提高 AttributeError("%s.%s" % (self.username, attr))
属性错误:GetAttributes.DEPT_NAME
假设此Python插件的实现与AutoCAD ActiveX对象模型的属性和方法一致,那么您需要查询TagString
属性属性,如果它与您的目标属性匹配,则输出属性的 TextString
属性。
我猜您需要以下内容:
for attrib in entity.GetAttributes():
if attrib.TagString == 'DEPT_NAME':
print(attrib.TextString)
print("+++++++")
exit()
问题是 BlockReference 对象上的字段 DEPT_NAME 未获取值。 如何获取值
我尝试了给定的代码但没有得到任何结果
from __future__ import print_function
from os.path import join, dirname, abspath
from xlutils.copy import copy
import xlrd
import xlwt
from pyautocad import Autocad, APoint
import os
import win32com.client
from pyautocad import Autocad, APoint
from pyautocad.contrib.tables import Table
from comtypes import COMError
def props(cls):
return [i for i in cls.__dict__.keys() if i[:1] != '_']
# Create workbook
book = xlwt.Workbook()
ws = book.add_sheet("ExportedData")
book.save("Exported.xls")
# Open the workbook
xl_workbook = xlrd.open_workbook("Exported.xls")
sheet_names = xl_workbook.sheet_names()
xl_sheet = xl_workbook.sheet_by_name(sheet_names[0])
wb = copy(xl_workbook)
sheet = wb.get_sheet(0)
dwgfiles = filter(os.path.isfile, os.listdir(os.curdir))
cwd = os.path.abspath(os.path.curdir) # current working dir
print(cwd)
for f in dwgfiles:
print("++++++++++++++++++++++++++++++")
print("++++++++++++++++++++++++++++++")
print("++++++++++++++++++++++++++++++")
print("++++++++++++++++++++++++++++++")
print(f)
if f.endswith(".dwg"):
print("sdaasdas")
""" open Document"""
acad = Autocad()
print(cwd)
acad.app.Documents.open(cwd + "/" + f)
print(acad.doc.Name)
num_cols = xl_sheet.ncols # Number of columns
idx = 1
acad = win32com.client.Dispatch("AutoCAD.Application")
doc = acad.ActiveDocument # Document object
print("MODEL SPACE")
count=0
for entity in acad.ActiveDocument.ModelSpace:
name = entity.EntityName
print(name)
if name == 'AcDbBlockReference':
HasAttributes = entity.HasAttributes
if HasAttributes:
# sheet.row(idx).write(0, entity.TextString)
sheet.row(idx).write(1, entity.ObjectID)
sheet.row(idx).write(2, cwd + "/" + f)
sheet.row(idx).write(3, str(entity.InsertionPoint))
sheet.row(idx).write(4, entity.Name)
sheet.row(idx).write(5,entity.Layer)
idx = idx + 1
print(entity.Name)
print(entity.Layer)
print(entity.ObjectID)
k=0
#print(entity.get_attrib_text('DEPT_NAME'))
for attrib in entity.GetAttributes():
print(attrib.DEPT_NAME)
print("+++++++")
exit()
print(count)
doc.Close(False)
acad = None
wb.save("Exported.xls")
我得到的错误是这个 追溯(最近一次通话): 文件 "auto1.py",第 78 行,位于 打印(attrib.DEPT_NAME) 文件 "D:\autocad_test\venv\lib\site-packages\win32com\client\dynamic.py",第 527 行,在 getattr 中 提高 AttributeError("%s.%s" % (self.username, attr)) 属性错误:GetAttributes.DEPT_NAME
假设此Python插件的实现与AutoCAD ActiveX对象模型的属性和方法一致,那么您需要查询TagString
属性属性,如果它与您的目标属性匹配,则输出属性的 TextString
属性。
我猜您需要以下内容:
for attrib in entity.GetAttributes():
if attrib.TagString == 'DEPT_NAME':
print(attrib.TextString)
print("+++++++")
exit()