AutoLISP: Great Dialog Box to Turn On Layers from Various States

If you’re like me, you have a handful of routines, scripts or macros that let you basically “turn on” layers
Thaw those frozen layers; Unlock those locked layers; Turn on those Off layers…
The routine featured today combines all of those functions into one dialog box. The great thing about this routine is that it shows you only those layers that are currently Off, Frozen or Locked so that you don’t have the other layers that are turned on cluttering up your view. There is an optional function that lets you control these same layer states if they exist in an XREF. Simply check the box next to “Xrefs layers” for the ability to control these layers as well.

Here’s how:

  • Layer_On <enter> to start
  • Check the boxes under the “Properties” section that you would like to be turned “On”
  • Check the box next to “Xrefs Layers” to gain control over those layers as well

Select the layers under the “Layer List” section that you want to be turned “ON”. If all layers are needed to be selected, click the “Select all” button.

Layer On 1

 

This routine was provided by Stefan M. at the Autodesk forums. Please refer questions about the routine there: http://forums.autodesk.com/t5/Visual-LISP-AutoLISP-and-General/Thaw-layers-from-custom-dialogue-box/td-p/3944033/page/2


 ; Turn ON selected layers
 ; Stefan M. - 05.06.2013
 ; http://forums.autodesk.com/t5/Visual-LISP-AutoLISP-and-General/Thaw-layers-from-custom-dialogue-box/td-p/3944033/page/2
(defun C:LAYER_ON (/ *error* acDoc filen filew id l layers r prop var labels keys final l_name selected_layers)
  (vl-load-com)
  (setq acDoc (vla-get-activedocument (vlax-get-acad-object)))
  (vla-startundomark acDoc)

  (defun *error* (m)
    (and
      m
      (not (wcmatch (strcase m) "*CANCEL*,*QUIT*"))
      (princ (strcat "\nError: " m))
      )
    (vla-endundomark acDoc)
    )

  (defun remove_duplicates (lst)
    (if lst
      (if (vl-position (car lst) (cdr lst))
        (cons (car lst) (remove_duplicates (vl-remove (car lst) (cdr lst))))
        (cons (car lst) (remove_duplicates (cdr lst)))
        )
      )
    )

  (defun prompt_list ()
    (setq final (remove_duplicates (apply 'append (mapcar '(lambda (a b) (if (eq "1" a) b)) var l))))
    (if (eq "0" (last var))
      (setq final (vl-remove-if '(lambda (a) (wcmatch a "*|*")) final))
      )
    (if final (setq final (acad_strlsort final)))
    (start_list "a_list")
    (mapcar 'add_list final)
    (end_list)
    )

  (setq prop   '((LayerOn . 0) (Freeze . -1) (Lock . -1) (Plottable . 0))
        labels '("OFF Layers" "Frozen Layers" "Locked Layers" "UnPlottable Layers" "Xrefs Layers")
        keys   '("tog1" "tog2" "tog3" "tog4" "tog5")
        l      '(nil nil nil nil)
        )

  (vlax-for la (setq layers (vla-get-layers acDoc))
    (setq l (mapcar
              '(lambda (a b)
                 (if (= (vlax-get la (car a)) (cdr a))
                   (cons (vla-get-Name la) b)
                   b
                   )
                 )
              prop
              l
              )
          )
    )
  (setq l (reverse (cons (vl-remove-if '(lambda (a) (eq (strcase a) "DEFPOINTS")) (last l))
                         (cdr (reverse l))
                         )
                   )
        )

  (if (vl-some 'vl-consp l)
    (progn
      (setq filew (open (setq filen (strcat (getvar 'dwgprefix) "temp_layer_dialog.dcl")) "w"))
      (write-line
        "layer_on_dialog : dialog { label = \"SELECT LAYERS\";
       : row {
       : list_box { label = \"Layer List\"; key = \"a_list\"; width = 30; height = 15; multiple_select = true; allow_accept = true;}
       : column {
       : boxed_column { label = \"Properties\"; fixed_height = true;  alignment = top;"
        filew
        )
      (mapcar
        '(lambda (a b c)
           (if c
             (write-line (strcat "       : toggle { label = \"" a "\";    key = \"" b "\"; }") filew)
             )
           )
        labels keys (append l '(T))
        )
      (write-line "       }: button { label = \"Select all\"; key = \"selall\"; alignment = bottom;}
       }}ok_cancel;}" filew)
      (close filew)

      (if
        (>= (setq id (load_dialog filen)) 0)
         (if
           (new_dialog "layer_on_dialog" id)
            (progn
              (setq var (subst "0" nil (read (cond ((getenv "layer_dialog_box_settings")) ("(\"1\" \"0\" \"0\" \"0\" \"0\")")))))
              (prompt_list)
              (action_tile "a_list" "(setq selected_layers $value)")
              (action_tile "selall" "(set_tile \"a_list\" (setq selected_layers (apply 'strcat (mapcar '(lambda (a) (strcat (itoa (vl-position a final)) \" \")) final))))")
              (mapcar
                '(lambda (a b)
                   (if b
                     (progn
                       (action_tile
                         a
                         "(setq var (mapcar 'get_tile keys) selected_layers nil) (set_tile \"a_list\" \"\") (prompt_list)"
                         )
                       (set_tile a (nth (vl-position a keys) var))
                       )
                     )
                   nil
                   )
                keys
                (append l '(T))
                )
              (setq r (start_dialog))
              (unload_dialog id)
              (if var
                (setenv "layer_dialog_box_settings" (vl-prin1-to-string var))
                )
              )
            (princ "\nWrong dialog definition")
            )
         (princ "\nDCL file not found")
         )
      (if (findfile filen) (vl-file-delete filen))
      (if
        (and (= r 1) selected_layers)
        (progn
          (foreach x (read (strcat "(" selected_layers ")"))
            (mapcar
              (function
                (lambda (a b c)
                  (if
                    (and
                      (eq "1" c)
                      (vl-position (setq l_name (nth x final)) a)
                      )
                    (vlax-put (vla-item layers l_name) (car b) (- (1+ (cdr b))))
                    )
                  )
                )
              l
              prop
              var
              )
            )
          (if
            (or
              (eq (nth 1 var) "1")
              (eq (nth 2 var) "1")
            )
            (vla-regen acDoc acActiveViewport)
          )
        )
      )
    )
    (princ "\nAll layers are on and plottable.")
    )
  (*error* nil)
  (princ)
  )
About these ads

About AutoCAD Tips

I work for a large engineering firm and perform various CAD Administration duties and. I enjoy teaching/tutoring people in AutoCAD and seeing them enjoy using the program as much as I do. I hope that you find this blog a useful tool.
This entry was posted in AutoLISP, AutoLISP XREFs, AutoLISP: Manage, AutoLISP: Modify, Layers. Bookmark the permalink.

One Response to AutoLISP: Great Dialog Box to Turn On Layers from Various States

  1. Bernardo V. Sharp says:

    Thanks for the replies. I was hoping to get something like a lisp routine that would present a window just showing the off layers and allowing me to select the layer I want on. Its about making it quick by not hasving to open the layer manager as I find this a bit cumbersome.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s