明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 4599|回复: 10

请教如何用LISP遍历CAD图形文件中所有的元素

[复制链接]
发表于 2003-11-18 13:35 | 显示全部楼层 |阅读模式
请教如何用LISP遍历CAD图形文件中所有的元素,从中找出所需要的。
如:从文件中找出图块MK,并统计其在某图层中(或全部图层中)的总数。
希望能提供LISP源代码。
发表于 2003-11-18 13:53 | 显示全部楼层
你说确定点,不要如、或之类的,总不要每样给你写一个吧。
遍历这样。。。
(setq ss (ssget "x"))
(setq i 0)
(repeat (sslength ss)
  (setq ent (ssname ss i))
  ...
  (setq i (1+ i))
)

是图块这样判断(我想你指的应该是插入后的INSERT对象吧)
(if (= (cdr (assoc 0 (entget ent))) "INSERT")
...
)
ent为上面遍历中的ent,引号中的INSERT注意大写
图层这样找
(setq layername (cdr (assoc 8 (entget ent))))
发表于 2003-11-18 16:35 | 显示全部楼层
TO
     樓主, 不用LISP遍历CAD图形文件中所有的元素去找你要的元素, 你直接找出你要的元素就好了. 下面有個小程序供你參考., 另外你要找的圖塊我想在那個圖里面有很多個的(規範的作圖時可能性很大)因為不多你也不必用塊來處理, 所以那些塊通常使用也是在同一個圖層的可能性很大的.

(defun c:test(/ SSB)
  (setq ssb (ssget "x" '((2 . "MK")(0 . "INSERT"))))
  (ALERT (STRCAT "圖塊名為MK的有(" (RTOS (SSLENGTH SSB))"個)"))
  (PRINC))
 楼主| 发表于 2003-11-18 16:47 | 显示全部楼层
谢谢!
发表于 2003-11-19 15:29 | 显示全部楼层
另外一种方式.,,, 用戶輸入塊名查找



;Get the number of block for specified name
(defun c:GetNB(/ nBlk SSB )
  (setq nBlk (getstring "lease input the BLOCK NAME : "))
  (setq ssb (ssget "x" (list (cons 2 nBlk)(cons 0  "INSERT"))))
(ALERT (STRCAT "圖塊名為 "nBlk "的有(" (RTOS (SSLENGTH ssb))"個)"))
  (PRINC)
)
 楼主| 发表于 2003-11-20 09:36 | 显示全部楼层
多谢各位高手指点,现在做了两个小程序,主要用于统计元件的数量。现在把它拿出来供大家参考。
抛砖引玉,多谢,多谢!
根据图块名称在图中查找数量。
一、好像不太通用。
;Get the number of block for specified name
(defun c:GetNB(/ nBlk ssb numb ssb0)
  (setq nBlk (getstring "lease input the BLOCK NAME : "))
  (setq nlayer (getstring "lease input the Layer Name : "))
  (setq ssb (ssget "x" (list (cons 2 nBlk) (cons 0 "INSERT") (cons 8 nlayer))))
  (setq ssb0 (ssget "x" (list (cons 2 nBlk) (cons 0 "INSERT"))))
  (ALERT (STRCAT "There are " (RTOS (SSLENGTH ssb0)) " Blocks: " nBlk "\n" (rtos (sslength ssb)) " in Layer : " nlayer))
  (PRINC)
)

二、好像通用一点,但是麻烦一些。
;Get the number of the block specified name
(defun C:GETBLOCK (/ blk ss i j k ct)
  (alert "This program is for calculate the number of specified blocks \nin the specified layer in the drawing,\nPlease input the block name and layer name,\npay attention to the UPERCASE AND LOWERCASE of character.")
  (setq blk (getstring "lease input the block name : "))
  (setq ct (getstring "lease input the layer name : "))
  (setq ss (ssget "x"))
  (setq k 0)
  (setq j 0)
  (setq i 0)
  (repeat (sslength ss)
    (setq ent (ssname ss i))
    (if (= (cdr (assoc 0 (entget ent))) "INSERT")
      (if (= (strcase blk) (strcase (cdr (assoc 2 (entget ent)))))      ;change the upercase to lower case and compare
        (progn
          (if (= (strcase ct) (strcase (cdr (assoc 8 (entget ent)))))
             (setq k (1+ k))
          )   
          (setq j (1+ j))
        )
      )
    )
    (setq i (1+ i))
  )
  (alert (strcat "There are " (rtos j) " blocks : " blk "\n " (rtos k) " in Layer :" ct))
  (princ)                                Exits quietly
)
发表于 2003-11-20 09:53 | 显示全部楼层
呵呵. 蠻不錯嘛. 好 ! !
发表于 2004-3-1 21:00 | 显示全部楼层
哇,真是受益非浅哪
发表于 2004-3-1 21:34 | 显示全部楼层
做的不错嘛!
发表于 2014-12-30 22:53 | 显示全部楼层
功能不错,不过编成直接选要统计的1个块,自动统计出该块的数量会快些。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-3-29 18:02 , Processed in 0.160940 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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