枫叶棋语 发表于 2023-10-27 21:30:59

属性块属性读写

本帖最后由 枫叶棋语 于 2023-10-27 21:33 编辑

from pycad.system import *
from pycad.runtime import *


class PyAtt:
    def __init__(self, blockref: BlockReference):
      self.blockref = blockref
      self.Position = self.blockref.Position
      self.InitAtt()

    def InitAtt(self):
      attids = self.blockref.AttributeCollection
      for attid in attids:
            attblockref: AttributeReference = attid.GetObject(OpenMode.ForWrite)
            setattr(self, attblockref.Tag, attblockref)

    def SetAtt(self, name, value):
      getattr(self, name).TextString = value

    def GetAtt(self, name):
      return getattr(self, name).TextString


@Command()
def tspyatt(doc: Document):
    '''对块进行X排序,并修改属性'''
    res = ssget(":A", TV(0, "Insert"))
    if not res.ok:
      return
    ids = tuple(res)
    i = 1
    with dbtrans(doc) as tr:
      pyatts: List =
      pyatts.sort(key=lambda x: x.Position.X, reverse=False)
      for pyatt in pyatts:
            prinf(pyatt.GetAtt("编号"))
            pyatt.SetAtt("编号", f"num{i}")
            i += 1

qiyang 发表于 2023-10-27 22:03:51

我枫哥如此强大:lol 必须点赞

caoliu023 发表于 2023-10-29 16:13:15

感谢楼主的开源精神

paulpipi 发表于 2023-10-30 11:09:40

感谢分享,但不知道怎么用(`)**

榨菜人生 发表于 2023-10-30 17:29:39


感谢分享,但不知道怎么用(`)**
页: [1]
查看完整版本: 属性块属性读写