Tuesday, June 10, 2008

Invert Visible Layers

I thought I might share a lisp that has come in handy on occasion.
This one inverts visible and non-visible layers. That is if you can see it now it turns it off, If it’s off or frozen now, it turns it on and thaws it. Some times it’s nice to see what you are not seeing I guess. A break down on the how and why is below if you are into that kind of thing.

(defun c:Invert-Layers ()
(setq oldexpert (getvar "expert"))
(setvar "expert" 5)
(setq L 1)
(while (setq X (tblnext "layer" L))
(setq L nil)
(setq B (assoc 62 X))
(setq F (assoc 70 X))
(setq FF (logand 1 (cdr F)))
; Make Off layers Visible
(if (< (cdr B) 0)
(progn
(setq LNA (cdr (assoc 2 X)))
(command "-layer" "T" LNA "ON" LNA "")))
;Make Thawed or On Layers Invisible
(if (> (cdr B) 0)
(progn
(setq LNB (cdr (assoc 2 X)))
(command "-layer" "OFF" LNB ""))
; Make Frozen layers Visible
(if (= 1 FF)
(progn
(setq LNC (cdr (assoc 2 X)))
(command "-layer" "T" LNC "ON" LNC "")))
)
(setvar "expert" oldexpert)
(princ)
)

The Break Down____________________________________

(defun c:Invert-Layers ()
Define the function and make it callable from the command line.

(setq oldexpert (getvar "expert"))
(setvar "expert" 5)

I had problems with AutoCAD warnings, changing the Expert setting suppresses them for the duration of the lisp.

(setq L 1)
Start a counter

(while (setq X (tblnext "layer" L))
Search the layer table

(setq L nil)
Ends the counter


(setq B (assoc 62 X))

Extract the color of the layer

(setq F (assoc 70 X))
(setq FF (logand 1 (cdr F)))

Extract the frozen/thawed indicator for the layer


(if (< (cdr B) 0)
(progn
(setq LNA (cdr (assoc 2 X)))
(command "-layer" "T" LNA "ON" LNA "")))
Layers that are off have negative layer colors. This bit finds layer colors less than zero and turns them on.

(if (> (cdr B) 0)
(progn
(setq LNB (cdr (assoc 2 X)))
(command "-layer" "OFF" LNB ""))

This bit finds layer colors greater than zero and turns them off.

(if (= 1 FF)
(progn
(setq LNC (cdr (assoc 2 X)))
(command "-layer" "T" LNC "ON" LNC "")))

This bit finds frozen layers, thaws them and turns them on.

)
(setvar "expert" oldexpert)
Returns the old expert setting


(princ)
)

Exits quietly. That is, it would print “nil” to the command line.
Post a Comment