本帖最后由 dcl1214 于 2024-12-22 21:42 编辑
块中块深度嵌套的时候,要想获取块中所有图元,很多同仁一开始就想到递归,其实我是比较抵触递归的,所以,随手写了一个非递归模式获取块中块图元的代码,哪位还有速度更快的(块中块深度嵌套很多),可以一起切磋一下
请下载vlx测试,速度极快
- (defun $kuai-nei-tu-yuan$ (ENT-B lst / $kn-objs$ a blks e ents-all obj)
- ;非递归模式获取块内图元
- (defun $kn-objs$ (ENT-B / a blk ents km obj)
- (if
- (= (TYPE ENT-B) 'ENAME)
- (SETQ OBJ (vlax-ename->vla-object ENT-B))
- )
- (IF (and OBJ (= (type obj) 'VLA-object))
- (if (vlax-property-available-p obj 'effectivename)
- (setq km (vla-get-effectivename obj))
- (if (vlax-property-available-p obj 'name)
- (setq km (vla-get-name obj))
- )
- )
- )
- (setq blk (vl-catch-all-apply
- 'vla-item
- (list (vla-get-blocks
- (vla-get-activedocument
- (vlax-get-acad-object)
- )
- )
- km
- )
- )
- )
- (if blk
- (vlax-for a blk
- (setq ents (cons a ents))
- );遍历blk对象
- )
- ents
- )
- (setq ents-all nil)
- (setq obj nil)
- (setq blks ($kn-objs$ ENT-B));块中obj对象
- (while (setq obj (car blks));循环第一个obj
- (setq e nil)
- (setq e (vlax-vla-object->ename obj));转换为图元
- (if (= (vla-get-objectname obj) "AcDbBlockReference");如果是块对象
- (setq blks (append blks ($kn-objs$ e)));获取块中obj对象,拼接到blks尾巴上
- (setq ents-all (cons e ents-all));如果不是块对象就收集到ents-all里面,下面要返回给上一级
- )
- (setq blks (cdr blks));下一个obj对象
- )
- (setq ents-all (reverse ents-all));倒置
- ents-all
- )
- ;;; 示例
- (setq ent-b (car (entsel)))
- (setq ents ($kuai-nei-tu-yuan$ ent-b nil))
- (mapcar (function
- (lambda (a)
- (vl-catch-all-apply
- 'vla-put-color
- (list
- (vl-catch-all-apply 'vlax-ename->vla-object (list a))
- 1
- )
- )
- )
- )
- ents
- )
|