johnhans 发表于 2003-11-18 13:35:00

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

请教如何用LISP遍历CAD图形文件中所有的元素,从中找出所需要的。
如:从文件中找出图块MK,并统计其在某图层中(或全部图层中)的总数。
希望能提供LISP源代码。

meflying 发表于 2003-11-18 13:53:00

你说确定点,不要如、或之类的,总不要每样给你写一个吧。
遍历这样。。。
(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))))

BDYCAD 发表于 2003-11-18 16:35:00

TO
   樓主, 不用LISP遍历CAD图形文件中所有的元素去找你要的元素, 你直接找出你要的元素就好了. 下面有個小程序供你參考., 另外你要找的圖塊我想在那個圖里面有很多個的(規範的作圖時可能性很大)因為不多你也不必用塊來處理, 所以那些塊通常使用也是在同一個圖層的可能性很大的.

(defun c:test(/ SSB)
(setq ssb (ssget "x" '((2 . "MK")(0 . "INSERT"))))
(ALERT (STRCAT "圖塊名為MK的有(" (RTOS (SSLENGTH SSB))"個)"))
(PRINC))

johnhans 发表于 2003-11-18 16:47:00

谢谢!

BDYCAD 发表于 2003-11-19 15:29:00

另外一种方式.,,, 用戶輸入塊名查找



;Get the number of block for specified name
(defun c:GetNB(/ nBlk SSB )
(setq nBlk (getstring "Please input the BLOCK NAME : "))
(setq ssb (ssget "x" (list (cons 2 nBlk)(cons 0"INSERT"))))
(ALERT (STRCAT "圖塊名為 "nBlk "的有(" (RTOS (SSLENGTH ssb))"個)"))
(PRINC)
)

johnhans 发表于 2003-11-20 09:36:00

多谢各位高手指点,现在做了两个小程序,主要用于统计元件的数量。现在把它拿出来供大家参考。
抛砖引玉,多谢,多谢!
根据图块名称在图中查找数量。
一、好像不太通用。
;Get the number of block for specified name
(defun c:GetNB(/ nBlk ssb numb ssb0)
(setq nBlk (getstring "Please input the BLOCK NAME : "))
(setq nlayer (getstring "Please 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 "Please input the block name : "))
(setq ct (getstring "Please 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
)

BDYCAD 发表于 2003-11-20 09:53:00

呵呵. 蠻不錯嘛. 好 ! !

cxzr8 发表于 2004-3-1 21:00:00

哇,真是受益非浅哪

myfreemind 发表于 2004-3-1 21:34:00

做的不错嘛!

jyzas 发表于 2014-12-30 22:53:03

功能不错,不过编成直接选要统计的1个块,自动统计出该块的数量会快些。
页: [1] 2
查看完整版本: 请教如何用LISP遍历CAD图形文件中所有的元素