这段更改对象图层的代码怎么使它也能够先选择后执行?
本帖最后由 baoyizhu 于 2023-12-21 11:46 编辑各位路过的编程高手,大家好!
这段代码是用来做图层切换和更改对象图层的,目前执行没有问题,但我希望它能够更完美,
我想它也能够先选择对象之后输入命令也能执行,现在的情况是当图层不存在的情况下,
先输入命令后选择对象无法完成更改对象图层,但是目标图层存在的话是没有问题的。
望热心朋友能够解答,谢谢!
另:这段代码是否还有优化的空间?比如更高效,更精简,兼容性好的代码?
因为我要设置15个常用图层,这就需要写15段这样的代码,我想应该可以将重复代码提炼出来,对需要变动的地方定义一个变量列表,然后只需设置15个快捷键命令就好,不知我这样的想法是否正确,请大家多多指教。
(defun c:ge_qhtc01 ()
(setvar "cmdecho" 0)
(if (not (tblsearch "layer" "01"))
(progn (command "layer" "n" "01" "")
(command "layer" "c" 7 "01" "")
)
) ;保证一定有01图层
(and
(if(setq ss (ssget)) ;判断是否选择了对象
(command "change" ss "" "p" "la" "01" "p" "C" "bylayer" "")
;是,对选择的对象执行图层转换
(progn (command "layer" "s" "01" "")
(princ "\n已将01层置为当前层")
) ; 否,将01层置为当前层
)
)
(princ)
)
(defun c:tt ()
(setq i 1)
(repeat 66
(setq la (strcat (if (< i 10) "0" "" ) (itoa i))
tx (strcat "(defun c:c"
la
"(/)(setvar\"cmdecho\"0)(if(not(tblsearch\"layer\"\""
la
"\"))(command\"-layer\"\"n\"\""
la
"\"\"c\""
(itoa i)
"\""
la
"\"\"\"))(command\"-layer\"\"s\"\""
la
"\"\"\")(if(setq ss(ssget))(command\"chprop\"ss\"\"\"la\"\""
la
"\"\"C\"\"bylayer\"\"\"))(princ))"
)
i(1+ i)
)
(eval (read tx))
)
(princ)
) xyp1964 发表于 2023-12-21 12:28
院长太厉害了,代码可以写的这么精简,又完全实现了我的想法。
我稍加修改了一下,简化了命令,直接用数字做命令了,这就是我想要的。
(defun c:tt ()
(setq i 1)
(repeat 66
(setq la (strcat (if (< i 10) "0" "" ) (itoa i))
tx (strcat "(defun c:"
(itoa i)
"(/)(setvar\"cmdecho\"0)(if(not(tblsearch\"layer\"\""
la
"\"))(command\"-layer\"\"n\"\""
la
"\"\"c\""
(itoa i)
"\""
la
"\"\"\"))(command\"-layer\"\"s\"\""
la
"\"\"\")(if(setq ss(ssget))(command\"chprop\"ss\"\"\"la\"\""
la
"\"\"C\"\"bylayer\"\"\"))(princ))"
)
i(1+ i)
)
(eval (read tx))
)
(princ)
) xyp1964 发表于 2023-12-21 11:32
院长,我参照你的代码改了一个更改颜色的程序,怎么都调试不成功,代码理解能力还是太差,烦请您再帮我修正一下,谢谢啦:$
(defun c:tt ()
(setq i 1)
(repeat 255
(setq yanse (itoa i)
tx (strcat
"(defun c:S"yanse"(/)
(setvar\"cmdecho\"0)
(if (setq ss (ssget))
(command\"\"change\"\"ss\"\"\"\"\"p\"\"\"c\"\"\"yanse"\"\"\")
);if
(princ)
)"
)
i(1+ i)
)
(eval (read tx))
)
(princ)
)
Try===>
(if (setq ss (ssget))
(command \"chprop\" ss \"\" \"c\" yanse \"\")
);if
本帖最后由 xyp1964 于 2023-12-21 19:34 编辑
baoyizhu 发表于 2023-12-21 16:35
院长,我参照你的代码改了一个更改颜色的程序,怎么都调试不成功,代码理解能力还是太差,烦请您再帮我修 ...
(defun c:tt ()
(setq i 1)
(repeat 255
(setq c(itoa i)
x(strcat"(defun c:S"c"(/)(setvar\"cmdecho\"0)(if(setq ss(ssget))(command\"chprop\"ss\"\"\"c\"\""c"\"\"\"))(princ))")
i(1+ i)
)
(eval (read x))
)
(princ)
) xyp1964 发表于 2023-12-21 19:32
完美,:victory:,谢谢帮助!
页:
[1]