自己写的图块替换插件,有没有大侠们帮忙找一下错误
(defun c:blkreplace (/ oldblk newblk ss cnt)(setq oldblk (car (entsel "
选择要替换的原图块:")))
(if oldblk
(progn
(setq newblk (car (entsel "
选择新图块:")))
(if newblk
(progn
(setq ss (ssget "X" (list (cons 0 "INSERT") (cons 2 (cdr (assoc 2 (entget oldblk)))))))
(setq cnt 0)
(if ss
(progn
(repeat (sslength ss)
(setq ent (ssname ss (setq i (1+ i))))
(if (= oldblk (cdr (assoc 2 (entget ent))))
(progn
(entmod (subst (cons 2 (cdr (assoc 2 (entget newblk)))) (assoc 2 (entget ent)) (entget ent)))
(setq cnt (1+ cnt))
)
)
)
(princ (strcat "
共替换了 " (itoa cnt) " 个图块。"))
)
(princ "
未找到任何符合条件的图块。"))
)
(princ "
未选择新图块。"))
)
(princ "
未选择原图块。"))
(princ)
)
我是新手,求大神指点,提示参数类型错误,Numberp:nil
wutao8282 发表于 2023-6-27 15:06
谢谢,但是似乎旁边会有漏替换几个图块,我自己再研究一下,感谢
(defun c:blkreplace (/ ENT I NEWBLK OLDBLK SS)
(setq oldblk (car (entsel "\n选择要替换的原图块:")))
(if oldblk
(progn
(setq newblk (car (entsel "\n选择新图块:")))
(if newblk
(progn
(setq
ss (ssget "X"
(list (cons 0 "INSERT")
(cons 2 (cdr (assoc 2 (entget oldblk))))
)
)
)
(setq i 0)
(if ss
(progn
(repeat (sslength ss)
(setq ent (ssname ss i))
(setq
subss (subst
(assoc 2 (entget newblk))
(assoc 2 (entget ent))
(entget ent)
)
)
(entmod subss)
(entupd (cdr (assoc -1 subss)))
(setq i (1+ i))
)
(princ (strcat "\n共替换了 " (itoa i) " 个图块。"))
)
(princ "\n未找到任何符合条件的图块。")
)
)
(princ "\n未选择新图块。")
)
)
(princ "\n未选择原图块。")
)
(princ)
) andyding 发表于 2023-6-27 16:32
已经完美解决,谢谢大佬! i 还没有设定? 本帖最后由 springwillow 于 2023-6-27 11:50 编辑
;图块替换
(defun C:tkth ( / ss BlkName ents i)
(setvar "osmode" 0)
(princ "\n请选择作为源块的图块:")
(setq ss (ssget ":S"(list (cons 0 "INSERT"))))
(setq ent (entget (ssname ss 0)))
(setq BlkName (cdr (assoc 2 ent)))
(princ "\n请选择将替换的图块:")
(setq ss (ssget (list (cons 0 "INSERT"))))
(if ss
(progn
(setq i 0)
(repeat (sslength ss)
(setq ents (entget (ssname ss i)))
(setq ents (subst (cons 2 BlkName) (assoc 2 ents) ents))
(entmod ents)
(setq i (1+ i))
)
)
)
(princ)
)
springwillow 发表于 2023-6-27 11:38
和我这个的要求不一样。我是要选择被替换的图块后,自动将当前布局空间中所有被替换图块都全部替换,不需要选择对象替换区域 第15行 (setq i (1+ i)) 那个i 没有初始值,(1+ i) 出错。
btw:这种重复造轮子的事为啥要干啊,expresstools 不是有 BlockReplace 的功能么?
那个是把所有指定图块都进行替换(一般很少有这种需求,都是替换某一两个图块),正好合你意,你还去自己写一个干啥? (defun c:blkreplace (/ CNT ENT I NEWBLK OLDBLK SS)
(setq oldblk (car (entsel "\n选择要替换的原图块:")))
(if oldblk
(progn
(setq newblk (car (entsel "\n选择新图块:")))
(if newblk
(progn
(setq
ss (ssget "X"
(list (cons 0 "INSERT")
(cons 2 (cdr (assoc 2 (entget oldblk))))
)
)
)
(setq cnt 0
i -1
)
(if ss
(progn
(repeat (sslength ss)
(setq ent (ssname ss (setq i (1+ i))))
(if (= (cdr (assoc 2 (entget oldblk)))
(cdr (assoc 2 (entget ent)))
)
(progn
(entmod
(subst (cons 2 (cdr (assoc 2 (entget newblk))))
(assoc 2 (entget ent))
(entget ent)
)
)
(setq cnt (1+ cnt))
)
)
)
(princ (strcat "\n共替换了 " (itoa cnt) " 个图块。"))
)
(princ "\n未找到任何符合条件的图块。")
)
)
(princ "\n未选择新图块。")
)
)
(princ "\n未选择原图块。")
)
(princ)
) vormittag 发表于 2023-6-27 13:42
第15行 (setq i (1+ i)) 那个i 没有初始值,(1+ i) 出错。
btw:这种重复造轮子的事为啥要干啊,express ...
我就是要一次将全部图块替换, andyding 发表于 2023-6-27 13:56
(defun c:blkreplace (/ CNT ENT I NEWBLK OLDBLK SS)
(setq oldblk (car (entsel "\n选择要替换的原图 ...
:handshake:victory:谢谢,但是似乎旁边会有漏替换几个图块,我自己再研究一下,感谢
页:
[1]
2