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.

10 comments:

Anonymous said...

im trying to use your invert-layers, i can't figure out why

richard

Todd M. Shackelford said...

Richard,

I'm not sure if you have a question or a comment here.

Todd

Anonymous said...

At this moment I work in Acad LT and can't use lisp, instead what I do is: laywalk > invert selection > select objects. And sad thing is that laywalk can't be invoked transparently.

Wames said...

Hey!! I love the idea and would use this often, but it turns on the layer off, but it turn off the layers frozen and doesn't thaw them.

Any help?!?!

Todd M. Shackelford said...

Wames,

I guess you could use the lisp for the invert and then to go back just use the return previous layer states button included in AutoCAD. That should things back exactly as they were.

Nina said...

Hello. Been looking all over the net for something that you're describing but cannot get your lisp to work!! Am getting "malformed list on input" and cannot figure out why.

Dont know enough about lisps to be able to find the error myself. Or what am i doing wrong? Doing appload, load lisp and then Invert-layers. Nothing happens.

HELP!!

Todd M. Shackelford said...

Nina,

I think a open or closed parentheses has been lost somewhere. Look over the lisp character by character to make sure you have an equal number of open and closed parentheses.

nmulder said...

I am using ACAD 2009. I to had the malformed list on input error, but figured that out. But for some reason the routine doesn't do anything when I issue the command. My text window reveals that it is issuing the layer command, but doesn't seem to be doing anything. Nothing in my drawing changes. I can't figure out why.

Todd M. Shackelford said...

Nmulder,

You might want to make sure you have a dash before the Layer command like this "-layer". Otherwise AutoCAD will try and open the layer properties palette and screw everything up.

nmulder said...

I double checked all my layer commands and they are being issued correctly. I noticed you set the expert value to 5--but value of 1 suppresses the -layer command prompt? anyway, i changed it to 1 but that didn't do anything either.
i can't figure it out--it seems to be running the routine--but when i gets to the prompt for the variables, LNA, LNB, etc., it doesn't seem to do anything. my text window shows that it gets to "freeze" or "thaw", etc. in the layer command, but doesn't seem to be recognizing the list of layers given. not sure why it isn't giving an error then? i'll let you know if i figure something out.