We help IT Professionals succeed at work.

Autocad Lisp Help Needed

Medium Priority
1,133 Views
Last Modified: 2013-11-15
I am very new to lisp programing and I am trying to know how to go about writing a lisp routine to freeze all layers and then turn certain layers back on and also zet the current layer to the zero layer. I was able to do all this, but gives me an error that you can not freeze the current layer. I was told by a guy in my office that I will need to do some kind of looping to get this to happen. can anyone send me in the right direction and mayme I would not have to do a loop since it is very confusion using if statements.
Comment
Watch Question

dhsindyRetired considering supplemental income.

Commented:
The program don't allow the current layer to be frozen but you can turn it "OFF" which has nearlly the same effect.

In your routine you can use variables that use wildcards characters to find things for you.  If you have a good naming system for your layers it really helps out.
Without looping, you can use a wildcard as dhsindy suggested. The Tilde "~" in "~0" tells it to match everything but the patten so it freezes everything but layer zero (0).
(defun c:test ()
  (setvar "clayer" "0")
  (command "layer" "f" "~0" "")
  (princ)
)

Open in new window

Here's one of many ways you could "loop" through the list of layers to do the same thing...
(defun c:test ()
  (setvar "clayer" "0")
  (setq rewind-flag T)
  (while (setq layer (tblnext "layer" rewind-flag))
    (setq rewind-flag nil)
    (if	(/= (setq layer-name (cdr (assoc 2 layer))) "0")
      (command "layer" "f" layer-name "")
    ) ;_END While-If
  ) ;_END While
  (princ)
)

Open in new window

Author

Commented:
I like the first suggestion it is easy to understand, and i works great for the zero layer, but what I need is layer zero to be current and on and 7 or 8 other layers to be on also. I tried adding "~c-cntr-anno" and it did not work. what do i have to do to to turn on other layers as well?
Here's one way, you make also added code to verify layer zero is thawed before turning it on, then freeze all others, then a simple (foreach) loop to loop through the ones you want back thawed.
(defun c:test ()
  (command "layer" "t" "0" "")
  (setvar "clayer" "0")
  (command "layer" "f" "~0" "")
  (foreach item
		(list "layer1"
		      "layer3"
		      "layer5"
		)
    (command "layer" "t" item "")
  ) ;_ENDforeach
  (princ)
)

Open in new window

PS: There's more eligant ways to do this but being that yuo are new, you gotta start somehere. This also doesn't do anything with on/off status, just freeze/thaw but you get the idea.

Also note, that you can keep adding layers to "thaw" to the list fairly easily.
Here's yet another way without the loop. It's syntax is closer to what you are looking for. Note that the comma (,) and tilde (~) used earlier are special AutoCAD wildcards. You can learn more about the wildcards and others by looking up the (wcmatch) autolisp function in the Autolisp reference which is the only place they are documented despite being applicable in many places insode AutoCAD (even outside Autolisp)
(defun c:test ()
  (command "layer" "t" "0" "")
  (setvar "clayer" "0")
  (command "layer" "f" "~0" "")
  (command "layer" "t" "layer1,layer3,layer5" "")
  (princ)
)

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Great Expert understood very well and easy to follow
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.