@Anonymous wrote:
.... when there is no objects selected, it will switch the current layer; when there is any objects selected it will change the layer property of the object(s). ….
Here's what I use for exactly that in relation to Layer 0. You can make others and substitute any other Layer name.
(defun C:L0 ()
; put pre-selected object(s) [if any] on Layer 0, otherwise set current Layer to 0
(if (ssget "_I")
(command "_.chprop" "_layer" "0" ""); then
(command "_.layer" "_thaw" "0" "_set" "0" ""); else
); if
(princ)
)
[EDITED -- the first version was written for an older version, but doesn't work right in newer ones, and this is now the same as @ВeekeeCZ's version, except that mine will not have a problem if the Layer exists but is frozen.] Since Layer 0 always exists, this doesn't need to account for it possibly not existing. For other Layer names, you could substitute "_make" in place of "_set" in the second (command), but for the first one, more would need to be done. If that's needed, a question arises: Would you want it to merely put the selected objects on that Layer without making that Layer current, or would you also want it made current?
FURTHER EDIT: It occurs to me that rather than defining separate commands for each Layer you might want to do this with, you can define a function with an argument:
(defun dolayer (layname)
; put pre-selected object(s) [if any] on specified Layer, otherwise set that Layer current
(if (ssget "_I")
(command "_.chprop" "_layer" layname ""); then
(command "_.layer" "_thaw" layname "_set" layname ""); else
); if
(princ)
)
That way, you can put simpler entries into Tool Palette items, or other menu items:
(dolayer "0"); equivalent to my first routine above
(dolayer "GeorgeClooney")
(dolayer "MarieAntoinette")
Kent Cooper, AIA