明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 768|回复: 4

[【PyCAD】] 属性块属性读写

  [复制链接]
发表于 2023-10-27 21:30 | 显示全部楼层 |阅读模式
本帖最后由 枫叶棋语 于 2023-10-27 21:33 编辑
  1. from pycad.system import *
  2. from pycad.runtime import *


  3. class PyAtt:
  4.     def __init__(self, blockref: BlockReference):
  5.         self.blockref = blockref
  6.         self.Position = self.blockref.Position
  7.         self.InitAtt()

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

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

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


  17. @Command()
  18. def tspyatt(doc: Document):
  19.     '''对块进行X排序,并修改属性'''
  20.     res = ssget(":A", TV(0, "Insert"))
  21.     if not res.ok:
  22.         return
  23.     ids = tuple(res)
  24.     i = 1
  25.     with dbtrans(doc) as tr:
  26.         pyatts: List[PyAtt] = [PyAtt(objid.GetObject(OpenMode.ForRead)) for objid in ids]
  27.         pyatts.sort(key=lambda x: x.Position.X, reverse=False)
  28.         for pyatt in pyatts:
  29.             prinf(pyatt.GetAtt("编号"))
  30.             pyatt.SetAtt("编号", f"num{i}")
  31.             i += 1
复制代码

发表于 2023-10-27 22:03 | 显示全部楼层
我枫哥如此强大 必须点赞
发表于 2023-10-29 16:13 | 显示全部楼层
感谢楼主的开源精神
发表于 2023-10-30 11:09 来自手机 | 显示全部楼层
感谢分享,但不知道怎么用(`)**
发表于 2023-10-30 17:29 | 显示全部楼层

感谢分享,但不知道怎么用(`)**
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-5-5 00:27 , Processed in 0.329931 second(s), 22 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表