forum32 发表于 2022-7-6 09:56:07

更改块属性

大家好,
 
我对autocad很陌生,但对编程基本熟悉。我正在尝试编写一个脚本来更改块中的一些信息。
 
不幸的是,更改很复杂,需要在大约一百张图纸上完成。
 
当前块具有:
设计人:批准人:
 
最初创建块的人应该将这两个标记分开,以使过程更容易(DName&AName会更好)。
 
有没有办法只更改“批准人”框中的XXX标签?我尝试过处理-attedit和命令,但它似乎适用于标记的两个实例。我可以使用数值引用标签吗?比如说XXX(1)类似于数组?我已经看到VBA可以用于使用数组进行引用,但不幸的是,2010年没有安装VBA,需要调用它。
 
谢谢

Lee Mac 发表于 2022-7-6 10:04:40

您好,论坛32,
 
欢迎来到CADTutor
 
看看这里的子函数选择——SetAttributeValue函数只会设置它用特定标记找到的第一个属性的值——你可以使用Visual LISP或Vanilla LISP来解决这个问题。
 

David Bethel 发表于 2022-7-6 10:13:53

这是一个snipet,它逐步遍历属性插入的选择集,并将其属性标记名和值转储到关联列表中:
 

(defun c:dump-att (/ i ss en ed an ad al)
(and (setq i -1
         ss (ssget '((0 . "INSERT")(66 . 1))))
      (while (setq en (ssname ss (setq i (1+ i))))
             (setq ed (entget en)
                   an (entnext en)
                   ad (entget an)
                   al nil)
             (while (= "ATTRIB" (cdr (assoc 0 ad)))
                  (setq al (cons (cons (cdr (assoc 2 ad))
                                       (cdr (assoc 1 ad))) al)
                        an (entnext an)
                        ad (entget an)))
             (setq al (reverse al))
             (textpage)
             (princ (strcat "\n" (cdr (assoc 2 ed)) " Attributes (Tag . Value)\n"))
             (prin1 al)
             (getstring "\nPress Enter To Continue...")))
   (prin1))

 
我是否理解您(2)具有相同标记名的属性-大卫

forum32 发表于 2022-7-6 10:16:18

谢谢大家,这已经是好消息了。SetAttrib是否可以轻松修改以拾取给定标记的第二个实例?我以前从未使用过lisp,但我开始掌握它了。
 
大卫:是的。没错,该块最初设置为使用相同的信息填充“设计人”和“批准人”字段(我不知道为什么)
 
这是块编辑器的屏幕截图

 
其想法是只在“Approved:”字段中填写一个名称。(在100张图纸上相同)
 
这两个字段当前均为空。
 
以下是该脚本的输出:

STANDARD Attributes (Tag . Value)
(("DRAWING_NAME" . "removed") ("DRAWING-LINE_TWO" . "removed") ("XXX" . "<blank>") ("XXX" . "<blank>") ("MM_YY" . "FEB/05")
("SCALE" . "NTS") ("X-1" . "removed") ("FILE_NAME" . "removed"))

 
我计划将需要更改的图形复制到一个文件夹中,运行一个批处理文件来处理它们。最初我希望简单地制作一个脚本来做一些类似于-attedit,n,n,standard,XXX,*的事情,但它改变了XXX的两个实例。
 
看起来命令entnext在标记之间循环?

David Bethel 发表于 2022-7-6 10:25:28

你能告诉我你是第一次还是第二次需要更换XXX吗-大卫

forum32 发表于 2022-7-6 10:31:35

我运行了李的Lisp,它改变了“设计人:”字段。所以我认为我需要改变的是第二次发生。

Lee Mac 发表于 2022-7-6 10:33:55

 
嗨,论坛,
 
要首先达到第二个目标,请使用以下命令:
 

;;----------------=={ Set Attribute Value }==-----------------;;
;;                                                            ;;
;;Populates the first attribute matching the tag specified;;
;;found within the block supplied with the value specified, ;;
;;if present.                                             ;;
;;------------------------------------------------------------;;
;;Author: Lee Mac, Copyright © 2010 - www.lee-mac.com       ;;
;;------------------------------------------------------------;;
;;Arguments:                                                ;;
;;block - VLA Block Reference Object                        ;;
;;tag   - Attribute TagString                               ;;
;;value - Value to which the Attribute will be set          ;;
;;------------------------------------------------------------;;
;;Returns:Value the attribute was set to, else nil      ;;
;;------------------------------------------------------------;;

;; Modified to process the attributes in reverse order.

(defun LM:SetAttributeValue ( block tag value )
;; © Lee Mac 2010
(vl-some
   (function
   (lambda ( attrib )
       (if (eq tag (vla-get-TagString attrib))
         (progn (vla-put-TextString attrib value) value)
       )
   )
   )
   (reverse (vlax-invoke block 'GetAttributes))
)
)

David Bethel 发表于 2022-7-6 10:38:14

根据需要更改nv的字符串值
 
这将更改标记名为“XXX”的第二个属性
 

(defun c:xxx-att (/ nv i ss en ed an ad tg xt)
(setq nv "New Name Value")
(and (setq i -1
         ss (ssget '((0 . "INSERT")(66 . 1))))
      (while (setq en (ssname ss (setq i (1+ i))))
             (setq ed (entget en)
                   an (entnext en)
                   ad (entget an)
                   xt nil)
             (while (= "ATTRIB" (cdr (assoc 0 ad)))
                  (setq tg (cdr (assoc 2 ad)))
                  (cond ((and (= tg "XXX")
                              (not xt))
                           (setq xt T))
                        ((and (= tg "XXX")
                              xt)
                           (entmod (subst (cons 1 nv) (assoc 1 ad) ad))))
                  (setq an (entnext an)
                        ad (entget an)))))
   (prin1))

 
我明白了,它也会改变第三个、第四个、第五个。。。。XXX也是。只是不是第一个。
 
玩得开心-大卫

BlackBox 发表于 2022-7-6 10:50:04

您是否考虑过使用图纸集管理器(SSM)?
 
我们使用属性标题栏,将其转换为字段(属性值),并由SSM自定义属性填充。一次更改,每个具有修订属性的计划表将在打印时反映更改。对于已打开的图形,只需重新生成即可。
 
希望这有帮助!

forum32 发表于 2022-7-6 10:55:12

你们太棒了。。。。到目前为止,它工作得很好。我在文件夹中运行了一个批处理文件和几个测试pdf。
 
1) 我将lisp放入autoloader for autocad(忘记确切的名称,但在autocad启动时为您加载lsp)。
2) 批处理文件调用acad。exe/b“替换”所有图纸。
3) 更换。scr:基本上运行lsp,提供块。保存,然后退出。
 
可能不是最优雅的解决方案,但可以节省一些人手动编辑图形的时间。
 
SSM嗯?对AutoCAD来说太陌生了,我不熟悉它,但我会用它来看看我能想出什么。
页: [1] 2
查看完整版本: 更改块属性