明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1596|回复: 6

[提问] 求一个“块改零层”的程序

[复制链接]
发表于 2014-2-12 22:45:35 | 显示全部楼层 |阅读模式
求高手帮忙写一个LISP,预实现如下效果:输入命令BT后,将块内所有对象更改到0图层,颜色随层。小弟先再此谢谢了!


发表于 2014-2-12 23:12:58 | 显示全部楼层
  1. (defun c:tt ( / e i l n s x )
  2.     (if (setq s (ssget '((0 . "INSERT"))))
  3.         (repeat (setq i (sslength s))
  4.             (if (not (member (setq n (cdr (assoc 2 (entget (ssname s (setq i (1- i))))))) l))
  5.                 (progn
  6.                     (setq e (tblobjname "block" n)
  7.                           l (cons n l)
  8.                     )
  9.                     (while (setq e (entnext e))
  10.                         (setq x (entget e))
  11.                         (entmod (subst '(8 . "0") (assoc 8 x) x))
  12.                     )
  13.                 )
  14.             )
  15.         )
  16.     )
  17.     (command "_.regen")
  18.     (princ)
  19. )

 楼主| 发表于 2014-2-12 23:36:36 | 显示全部楼层
flyfox1047 发表于 2014-2-12 23:12

感谢回复。程序我刚才试过了,针对块内只有单一图层时程序没问题,如果原图块中天正墙体时,会直接关闭CAD而不弹出任何提示,包括错误报告也没有。
发表于 2014-2-12 23:51:29 | 显示全部楼层
sinommw 发表于 2014-2-12 23:36
感谢回复。程序我刚才试过了,针对块内只有单一图层时程序没问题,如果原图块中天正墙体时,会直接关闭CA ...

应该是你CAD问题吧
发表于 2014-2-12 23:52:06 | 显示全部楼层
  1. (defun c:test (/ *error* adoc lst_layer func_restore-layers)

  2.   (defun *error* (msg)
  3.     (func_restore-layers)
  4.     (vla-endundomark adoc)
  5.     (princ msg)
  6.     (princ)
  7.     ) ;_ end of defun

  8.   (defun func_restore-layers ()
  9.     (foreach item lst_layer
  10.       (vla-put-lock (car item) (cdr (assoc "lock" (cdr item))))
  11.       (vl-catch-all-apply
  12.         '(lambda ()
  13.            (vla-put-freeze
  14.              (car item)
  15.              (cdr (assoc "freeze" (cdr item)))
  16.              ) ;_ end of vla-put-freeze
  17.            ) ;_ end of lambda
  18.         ) ;_ end of vl-catch-all-apply
  19.       ) ;_ end of foreach
  20.     ) ;_ end of defun

  21.   (vl-load-com)
  22.   (vla-startundomark
  23.     (setq adoc (vla-get-activedocument (vlax-get-acad-object)))
  24.     ) ;_ end of vla-startundomark
  25.   (if (and (not (vl-catch-all-error-p
  26.                   (setq selset
  27.                          (vl-catch-all-apply
  28.                            (function
  29.                              (lambda ()
  30.                                (ssget '((0 . "INSERT")))
  31.                                ) ;_ end of lambda
  32.                              ) ;_ end of function
  33.                            ) ;_ end of vl-catch-all-apply
  34.                         ) ;_ end of setq
  35.                   ) ;_ end of vl-catch-all-error-p
  36.                 ) ;_ end of not
  37.            selset
  38.            ) ;_ end of and
  39.     (progn
  40.       (vlax-for item (vla-get-layers adoc)
  41.         (setq
  42.           lst_layer (cons (list item
  43.                                 (cons "lock" (vla-get-lock item))
  44.                                 (cons "freeze" (vla-get-freeze item))
  45.                                 ) ;_ end of list
  46.                           lst_layer
  47.                           ) ;_ end of cons
  48.           ) ;_ end of setq
  49.         (vla-put-lock item :vlax-false)
  50.         (vl-catch-all-apply
  51.           '(lambda () (vla-put-freeze item :vlax-false))
  52.           ) ;_ end of vl-catch-all-apply
  53.         ) ;_ end of vlax-for
  54.       (foreach blk_def
  55.                (mapcar
  56.                  (function
  57.                    (lambda (x)
  58.                      (vla-item (vla-get-blocks adoc) x)
  59.                      ) ;_ end of lambda
  60.                    ) ;_ end of function
  61.                  ((lambda (/ res)
  62.                     (foreach item (mapcar
  63.                                     (function
  64.                                       (lambda (x)
  65.                                         (vla-get-name
  66.                                           (vlax-ename->vla-object x)
  67.                                           ) ;_ end of vla-get-name
  68.                                         ) ;_ end of lambda
  69.                                       ) ;_ end of function
  70.                                     ((lambda (/ tab item)
  71.                                        (repeat (setq tab  nil
  72.                                                      item (sslength selset)
  73.                                                      ) ;_ end setq
  74.                                          (setq
  75.                                            tab
  76.                                             (cons
  77.                                               (ssname selset
  78.                                                       (setq item (1- item))
  79.                                                       ) ;_ end of ssname
  80.                                               tab
  81.                                               ) ;_ end of cons
  82.                                            ) ;_ end of setq
  83.                                          ) ;_ end of repeat
  84.                                        tab
  85.                                        ) ;_ end of lambda
  86.                                      )
  87.                                     ) ;_ end of mapcar
  88.                       (if (not (member item res))
  89.                         (setq res (cons item res))
  90.                         ) ;_ end of if
  91.                       ) ;_ end of foreach
  92.                     (reverse res)
  93.                     ) ;_ end of lambda
  94.                   )
  95.                  ) ;_ end of mapcar
  96.         (vlax-for ent blk_def
  97.           (vla-put-layer ent "0")
  98.           (vla-put-color ent 0)
  99.           (vla-put-lineweight ent aclnwtbyblock)
  100.           (vla-put-linetype ent "byblock")
  101.           ) ;_ end of vlax-for
  102.         ) ;_ end of foreach
  103.       (func_restore-layers)
  104.       (vla-regen adoc acallviewports)
  105.       ) ;_ end of progn
  106.     ) ;_ end of if
  107.   (vla-endundomark adoc)
  108.   (princ)
  109.   ) ;_ end of defun

评分

参与人数 1明经币 +1 收起 理由
liuhaixin88 + 1 赞一个!

查看全部评分

 楼主| 发表于 2014-2-13 12:49:38 | 显示全部楼层
提示Automation 错误。 图层无效
发表于 2014-2-13 16:10:48 | 显示全部楼层
第一的一个程式感觉也很不错!兄弟幸苦了。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2025-5-25 14:17 , Processed in 0.213771 second(s), 32 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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