(defun c:LayerCtrl (/			 all
		    dcl			 layer
		    layers		 laysobj
		    ss			 vss
		    LAYERCTRL:ACT:SELALL LAYERCTRL:ACT:SELREV
		    SELLAYERS		 LAYERCTRL:DCL:SHOW
		    LAYERCTRL:ACT:SEL	 LAYERCTRL
		    LAYERCTRL:ACT:FREE	 LAYERCTRL:ACT:FREEZE
		    LAYERCTRL:ACT:UNLOCK LAYERCTRL:ACT:LOCK
		    LAYERCTRL:ACT:ON	 LAYERCTRL:ACT:OFF
		   )
  (setq	dcl '("ls"
	      (:DIALOG
	       (LABEL "飞诗CAD - 图层控制")
	       (:ROW
		(:BOXED_ROW
		 (LABEL "所选图层")
		 "spacer;"
		 (:COLUMN
		  (:ROW
		   (:BUTTON
		    (HEIGHT 3)
		    (KEY "sel")
		    (LABEL ">")
		    (HORIZONTAL_MARGIN NONE)
		   )
		   (:BUTTON
		    (HEIGHT 3)
		    (KEY "selall")
		    (LABEL "全选")
		    (HORIZONTAL_MARGIN NONE)
		   )
		   (:BUTTON
		    (HEIGHT 3)
		    (IS_DEFAULT TRUE)
		    (KEY "selrev")
		    (LABEL "反选")
		    (HORIZONTAL_MARGIN NONE)
		   )
		  )
		  (:LIST_BOX
		   (HEIGHT 20)
		   (KEY "laylist")
		   (MULTIPLE_SELECT TRUE)
		   (WIDTH 25)
		   (HORIZONTAL_MARGIN NONE)
		  )
		  "spacer;"
		 )
		 "spacer;"
		)
		(:COLUMN
		 (:BOXED_COLUMN
		  (LABEL "图层操作")
		  (:BUTTON
		   (HEIGHT 3)
		   (IS_CANCEL TRUE)
		   (KEY "cancel")
		   (LABEL "取消")
		  )
		  (:ROW
		   (:BTN (KEY "off") (LABEL "关闭"))
		   (:BTN (KEY "on") (LABEL "打开"))
		  )
		  (:ROW
		   (:BTN (KEY "freeze") (LABEL "冻结"))
		   (:BTN (KEY "free") (LABEL "解冻"))
		  )
		  (:ROW
		   (:BTN (KEY "lock") (LABEL "锁定"))
		   (:BTN (KEY "unlock") (LABEL "解锁"))
		  )
		 )
		)
	       )
	      )
	      "btn"
	      (:BUTTON (HEIGHT 5) (WIDTH 12))
	     )
  )
  (setq LaysObj (vla-get-layers *doc*))
  (vlax-for a LaysObj (setq all (cons (vla-get-name a) all)))
  (setq all (acad_strlsort all))
  ;;vla-put-layeron vla-put-Freeze vla-put-lock
  ;;关闭
  (defun layerctrl:act:off ()
    (layerctrl 'vla-put-LayerOn 0)
  )
  (defun layerctrl:act:on ()
    (layerctrl 'vla-put-LayerOn 1)
  )
  ;;锁定
  (defun layerctrl:act:lock ()
    (layerctrl 'vla-put-Lock 1)
  )
  (defun layerctrl:act:unlock ()
    (layerctrl 'vla-put-Lock 0)
  )
  ;;冻结
  (defun layerctrl:act:Freeze ()
    (layerctrl 'vla-put-Freeze 1)
  )
  (defun layerctrl:act:Free ()
    (layerctrl 'vla-put-Freeze 0)
    (vla-Regen *doc* acAllViewports)
  )
  ;;设置图层状态
  (defun layerctrl (fun val)
    (foreach lay (dcl:list-getv "laylist")
      (vl-catch-all-apply fun (list (vla-item LaysObj lay) val))
    )
    (return t)
  )
  ;;开始
  (defun layerctrl:dcl:show ()
    (selLayers layers)
  )
  ;;设置光标
  (defun selLayers (lst)
    (dcl:list-set "laylist" all)
    (set_tile "laylist" "")
    (foreach a lst
      (set_tile "laylist" (itoa (vl-position a all)))
    )
  )
  ;;反选
  (defun layerctrl:act:selrev (/ sel)
    (get_tile "laylist")
    (setq sel (dcl:list-getv "laylist"))
    (selLayers
      (vl-remove-if '(lambda (a) (vl-position a sel)) all)
    )
  )
  ;;全选
  (defun layerctrl:act:selall ()
    (selLayers all)
  )
  ;;选择
  (defun layerctrl:act:sel ()
    (return "sel")
  )
  (if (setq ss (ssget))
    (progn
      (setq vss (vla-get-ActiveSelectionSet *doc*))
      (setq layers nil)
      (vlax-for	obj vss
	(setq layer (vla-get-Layer obj))
	(or (vl-position layer layers)
	    (setq layers (cons layer layers))
	)
      )
    )
    (setq layers all)
  )
  (if (= (fsxm-ldcl dcl "layerctrl") "sel")
    (c:LayerCtrl)
  )
  (princ)
)