以下是代码的快速修复:
- (vl-load-com)
- (defun c:cntblk ( / allblocks blklst blkobj blkobjname i n ss )
- [color=red] (if[/color] (setq allblocks (ssget "_X" (list (cons 0 "INSERT"))))
- [color=red] (progn[/color]
- (setq n (sslength allblocks))
- (setq i [color=red]0[/color])
- (repeat n
- (setq blkobj (ssname allblocks i))
- (setq blkobjname (getEffectiveName blkobj))
- [color=red] (if (not (member blkobjname blklst))
- (setq blklst (cons blkobjname blklst))
- )[/color]
- (setq i (1+ i))
- )
- (textscr)
- [color=red] (foreach blk blklst
- (if (setq ss (ssget "_X" (list (cons 0 "INSERT") (cons 2 blk))))
- (prompt (strcat "\n" blk "-----------------" (itoa (sslength ss))))
- )
- )[/color]
- [color=red] )
- )[/color]
- (princ)
- )
- (defun getEffectiveName ( ent / [color=red]obj [/color])
- (setq obj (vlax-ename->vla-object ent))
- (vlax-get obj "EffectiveName")
- )
但这不是一种很好的计数方法,因为在收集选择集时,将排除已更改可见性状态(因此变得匿名)的动态块。此外,通过这种方式,您可以有效地对块进行两次计数,因为您已经遍历了选择集以获得块列表。
考虑以下代码:
- (defun c:cntblk ( / allblocks blockentity blocklist blockname counter item )
- (if (setq AllBlocks (ssget "_X" '((0 . "INSERT"))))
- (progn
- (setq counter 0)
- (repeat (sslength AllBlocks)
- (setq blockentity (ssname AllBlocks counter)
- blockname (GetEffectiveName (vlax-ename->vla-object blockentity))
- )
- (if (setq item (assoc blockname blocklist))
- (setq blocklist (subst (cons blockname (1+ (cdr item))) item blocklist))
- (setq blocklist (cons (cons blockname 1) blocklist))
- )
- (setq counter (1+ counter))
- )
- (foreach blk blocklist
- (princ (strcat "\n" (car blk) " ----------------- " (itoa (cdr blk))))
- )
- )
- )
- (princ)
- )
- (defun GetEffectiveName ( obj )
- (if (vlax-property-available-p obj 'effectivename)
- (vla-get-effectivename obj)
- (vla-get-name obj)
- )
- )
我试图使程序的结构尽可能接近您的原始代码,以便您更容易学习。我强烈建议您仔细研究上述两种解决方案,并询问有关我使用的代码的任何问题。 |