【问题标题】:AutoCAD: Edit object attribute table automaticallyAutoCAD:自动编辑对象属性表
【发布时间】:2018-08-09 05:20:57
【问题描述】:
我在 AutoCAD 绘图中有几个(许多)对象,每个对象的首选项中都有相同的属性字段。现在我想用一个数字填充这个属性字段(对象一 - 数字 1,对象二 - 数字 2 等等)。手动输入数字非常耗时,因此我想请问您是否有自动化的方法来解决这个问题。
提前非常感谢!
【问题讨论】:
标签:
automation
attributes
label
autocad
【解决方案1】:
一个例子
以下程序是一个非常简单的示例,它会提示您输入要编号的属性标签和开始编号的整数,然后会不断提示您选择要编号的属性块引用,并递增编号每个有效选择加一:
(defun c:attnum ( / ent enx num tag )
(if (/= "" (setq tag (strcase (getstring "\nSpecify attribute tag <exit>: "))))
(progn
(setq num (cond ((getint "\nSpecify starting number <1>: ")) (1)))
(while
(not
(progn
(setvar 'errno 0)
(setq ent (car (entsel (strcat "\nSelect block number " (itoa num) " <exit>: "))))
(cond
( (= 7 (getvar 'errno))
(prompt "\nMissed, try again.")
)
( (null ent))
( (/= "INSERT" (cdr (assoc 0 (setq enx (entget ent)))))
(prompt "\nThe selected object is not a block.")
)
( (/= 1 (cdr (assoc 66 enx)))
(prompt "\nThe selected block is not attributed.")
)
( (progn
(setq ent (entnext ent)
enx (entget ent)
)
(while
(and
(= "ATTRIB" (cdr (assoc 0 enx)))
(/= tag (strcase (cdr (assoc 2 enx))))
)
(setq ent (entnext ent)
enx (entget ent)
)
)
(/= "ATTRIB" (cdr (assoc 0 enx)))
)
(prompt (strcat "\nThe selected block does not contain the attribute \"" tag "\"."))
)
( (entmod (subst (cons 1 (itoa num)) (assoc 1 enx) enx))
(entupd ent)
(setq num (1+ num))
nil
)
( (prompt "\nUnable to edit attribute value."))
)
)
)
)
)
)
(princ)
)
如何加载和运行上述内容
- 打开 Windows 记事本。
- 将上述代码复制并粘贴到记事本中。
- 使用您选择的文件名保存文件,文件扩展名为
.lsp(确保将 Save As Type 设置为 All Files (*.*))。
- 在新的或现有的图形中打开 AutoCAD。
- 在 AutoCAD 命令行中键入
APPLOAD。
- 浏览并选择上面保存的文件,然后点击
Load加载程序。
- 关闭
APPLOAD 对话框。
- 在 AutoCAD 命令行中键入
ATTNUM 以运行程序。
在我的 How to Run an AutoLISP Program 教程中可以找到类似的说明。
如果您希望为在 AutoCAD 中打开的每个新图形或现有图形自动加载程序,请参阅我在 Loading Programs Automatically 上的教程。
其他现有解决方案
除了上述之外,您可能还对以下程序感兴趣:
【解决方案2】:
此 lsp 可帮助您为任何块属性添加前缀或后缀,但同时对所有块,我有类似的情况,因此我将上述 attnum.lsp 与此 Presuf.lsp 结合起来。
首先使用 attnum 将标签字段设置为递增数字 1,2,3,4 等。然后隔离对象并运行presuf,指明是添加后缀还是前缀,指明需要的值,然后选择所有对象。
(defun c:presuf ( / as el en i ss str typ )
(initget "Prefix Suffix")
(setq typ (cond ((getkword "\nAdd Prefix or Suffix? [Prefix/Suffix] <Prefix>: ")) ("Prefix")))
(setq str (getstring t (strcat typ " to Add: ")))
(if (setq ss (ssget '((0 . "INSERT") (66 . 1))))
(repeat (setq i (sslength ss))
(setq en (ssname ss (setq i (1- i))))
(while (eq "ATTRIB" (cdr (assoc 0 (setq el (entget (setq en (entnext en)))))))
(setq as (cdr (assoc 1 el)))
(if (eq "Prefix" typ)
(if (not (wcmatch as (strcat str "*")))
(entmod (subst (cons 1 (strcat str as)) (assoc 1 el) el))
)
(if (not (wcmatch as (strcat "*" str)))
(entmod (subst (cons 1 (strcat as str)) (assoc 1 el) el))
)
)
)
)
)
(princ)
)