;;; -----------------------------------------------------------------
;;; 2 图层命令
;;; ------------------------------------------------------------------
;;;  2.1 LayerOff 关闭物体所在的层（单选）
(defun c:LayerOff (/ ent lname)
	(setvar "cmdecho" 0)
	(setq ent (entsel "\nPick an entity on the target layer: "))
	(if ent
		(progn
			(setq ent (entget (car ent)))
			(setq lname (cdr (assoc 8 ent)))
		)
		(setq lname (getstring "\nNot to selected, Input layer name: "))
	)
	(if (= (getvar "clayer") lname)
		(setvar "clayer" "0")
	)
	(command "layer" "off" lname "" "y" "")
	(princ)
)

;;;  2.2 LayerOffM 关闭物体所在的层（多选）
(Defun C:LayerOffM ()
	(setvar "cmdecho" 0)
	(prompt "\nSelect entities to turn off:")
	(setq ss (ssget))
	(if (and
				ss
				(sslength ss)
				0
			)
		(progn
			(setq ct 0
						len (sslength ss)
						cl (getvar "clayer")
			)
			(command ".layer")
			(while (< ct len)
				(setq la (cdr (assoc 8 (entget (ssname ss ct)))))
				(if (/= cl la)
					(command "off" la)
					(progn
						(command "off" la "y")
;						(prompt "\nThe layer ")
;						(prompt la)
;						(prompt " is CURRENT!")
					)
				)
				(if (= old nil)
					(setq OLD la)
					(setq OLD (strcat OLD "," la))
				)
				(setq ct (1+ ct))
			)
			(command "")
		)
	)
	(princ)
	(setvar "cmdecho" 0)
	(prin1)
)

;;;  2.3 LayerOffOther 关闭物体以外的层
(defun c:LayerOffOther (/ ent lname)
	(setvar "cmdecho" 0)
	(setq ent (entsel "\nPick an entity on the target layer: "))
	(if ent
		(progn
			(setq ent (entget (car ent)))
			(setq lname (cdr (assoc 8 ent)))
			(setvar "clayer" lname)
		)
	)
	(command "layer" "off" "*" "y" "")
	(princ)
)

;;;  2.3.1 LayerOffOtherM 关闭物体以外的层（多选）
(Defun C:LayerOffOtherM (/ ss ct cl la old)
	(setvar "cmdecho" 0)
	(prompt "\nSelect entities on the layers you want to remain:")
	(setq ss (ssget))
	(setq ct 0
				len (sslength ss)
				cl (cdr (assoc 8 (entget (ssname ss 0))))
	)
	(setvar "clayer" cl)
	(while (< ct len)
		(setq la (cdr (assoc 8 (entget (ssname ss ct)))))
		(if (= old nil)
			(setq OLD la)
			(setq OLD (strcat OLD "," la))
		)
		(setq ct (1+ ct))
	)
	(command ".layer" "off" "*" "y" "")
	(command ".layer" "on" old "")
	(princ)
)

;;; ------------------------------------------------------------------
;;;  2.4 LayerLockM 锁住物体所在的层（多选）
(defun C:LayerLockM (/ ES EN EL A)
	(princ "Selected Entity(s) Layers Locked.")
	(setq ES (ssget)
				A 0
				EN ""
				EL nil
				FL nil
	)
	(while (/= EN nil)
		(setq EN (ssname ES A)
					EL (cons EN EL)
					A (1+ A)
		)
	)
	(setq EL (cdr EL)
				FL (cdr (assoc '8 (entget (car EL))))
				EL (cdr EL)
	)
	(repeat (- A 2)
		(setq EN (cdr (assoc '8 (entget (car EL))))
					FL (strcat EN "," FL)
					EL (cdr EL)
		)
	)
	(command "LAYER" "LO" (eval FL) "")
	(princ)
)

;;;  2.5 LayerUnlockM 解锁物体所在的层（多选）
(defun C:LayerUnlockM (/ ES EN EL A)
	(princ "Selected Entity(s) Layers Unlocked.")
	(setq ES (ssget)
				A 0
				EN ""
				EL nil
				FL nil
	)
	(while (/= EN nil)
		(setq EN (ssname ES A)
					EL (cons EN EL)
					A (1+ A)
		)
	)
	(setq EL (cdr EL)
				FL (cdr (assoc '8 (entget (car EL))))
				EL (cdr EL)
	)
	(repeat (- A 2)
		(setq EN (cdr (assoc '8 (entget (car EL))))
					FL (strcat EN "," FL)
					EL (cdr EL)
		)
	)
	(command "LAYER" "U" (eval FL) "")
	(princ)
)

;;; ------------------------------------------------------------------
;;;  2.6 LayerFreezeM 冻结物体所在的层（多选）
(defun C:LayerFreezeM (/ ES EN EL A)
	(princ "Selected Entity(s) Layers Freezed.")
	(setq ES (ssget)
				A 0
				EN ""
				EL nil
				FL nil
	)
	(while (/= EN nil)
		(setq EN (ssname ES A)
					EL (cons EN EL)
					A (1+ A)
		)
	)
	(setq EL (cdr EL)
				FL (cdr (assoc '8 (entget (car EL))))
				EL (cdr EL)
	)
	(repeat (- A 2)
		(setq EN (cdr (assoc '8 (entget (car EL))))
					FL (strcat EN "," FL)
					EL (cdr EL)
		)
	)
	(command "LAYER" "F" (eval FL) "")
	(princ)
)

;;;  2.7 LayerThawAll 解冻所有的层
(Defun C:LayerThawAll ()
	(COMMAND "LAYER" "THAW" "*" "")
	(PRINC)
)


;;; ------------------------------------------------------------------
;;;  2.8 LayerCurrent 将物体所在的层设为当前层
(defun c:LayerCurrent (/ ent lname)
	(setvar "cmdecho" 0)
	(setq ent (car (entsel "\nPick an entity on the target layer: ")))
	(if ent
		(progn
			(setq ent (entget ent)
						lname (cdr (assoc 8 ent))
			)
		)
		(progn
			(setq lname (getstring "\nNot to selected, Input layer name: "))
		)
	)
	(setvar "clayer" lname)
	(princ)
)

;;; ------------------------------------------------------------------
;;;  2.9 LayerOnAll 打开所有层
(Defun C:LayerOnAll ()
	(command "layer" "on" "*" "")
	(princ)
)

;;; ------------------------------------------------------------------
;;;  2.10 ToCurrentLayerM 将物体转到当前层（多选），并使用层颜色，线型
(defun c:ToCurrentLayerM (/ lname ss)
	(setq ss (ssget))
	(if ss
		(progn
			(setq lname (getvar "clayer"))
			(command "chprop" ss "" "la" lname "color" "bylayer" "ltype" "bylayer"
							 ""
			)
		)
	)
)

;;; ------------------------------------------------------------------
;;;  2.11 ToLayerMatch 通过目标物体改变选择实体的图层属性
(defun c:ToLayerMatch (/ lname ss ent)
	(setvar "cmdecho" 0)
	(prompt "\nSelect the entity(s): ")
	(setq ss (ssget))
	(if ss
		(progn
			(setq ent (entsel "\nPick an entity on the target layer: "))
			(if ent
				(progn
					(setq ent (entget (car ent)))
					(setq lname (cdr (assoc 8 ent)))
				)
				(progn
					(setq lname (getstring "\nNot to selected, Input layer name: "))
				)
			)
			(command "chprop" ss "" "la" lname "")
		)
	)
	(princ)
)
