Jump to content

Recommended Posts

Posted (edited)

Is there a way to incorporate the "-layer" command into a lisp routine to accomplish the task of turning off all layers except a certain 5 that will always be the same layers that I want to keep on?

 

I am currently using this to turn off all layers except the current one but do not know how to modify the code to keep the same 5 layers on all the time..

 

(defun C:loff ( / clayer expert)
 (setq clayer (getvar "clayer") expert (getvar "expert"))
 (setvar "expert" 1)
 (command "._layer" "_off" "*" "_on" clayer "")
 (setvar "expert" expert)
 (princ)
)

Edited by SLW210
Code tags added!
Posted (edited)

Is there a way to incorporate the "-layer" command into a lisp routine to accomplish the task of turning off all layers except a certain 5 that will always be the same layers that I want to keep on?

 

For example, I would like to turn off all layers except the following:

Storm_Drains

Building

HVAC

Sprinkler

Parking_Lot

 

 

I am currently using this LISP to turn off all layers except the current one but do not know how to modify the code to keep those same 5 layers on all the time..

 

(defun C:loff ( / clayer expert)
(setq clayer (getvar "clayer") expert (getvar "expert"))
(setvar "expert" 1)
(command "._layer" "_off" "*" "_on" clayer "")
(setvar "expert" expert)
(princ)
) 

Edited by SLW210
Added Code Tags!
Posted
Is there a way to incorporate the "-layer" command into a lisp routine to accomplish the task of turning off all layers except a certain 5 that will always be the same layers that I want to keep on?

 

I am currently using this to turn off all layers except the current one but do not know how to modify the code to keep the same 5 layers on all the time..

 

(defun C:loff ( / clayer expert)

(setq clayer (getvar "clayer") expert (getvar "expert"))

(setvar "expert" 1)

(command "._layer" "_off" "*" "_on" clayer "")

(setvar "expert" expert)

(princ)

)

 

Where it says clayer, you can add change it to "LAYER1,LAYER2,LAYER3,LAYER4,LAYER5".

 

LAYER# being the name of each layer you want to be left on.

Posted

I added the posts from the other thread to this one.

Posted
Where it says clayer, you can add change it to "LAYER1,LAYER2,LAYER3,LAYER4,LAYER5".

 

LAYER# being the name of each layer you want to be left on.

 

 

Thank you again for your help on that one! Is there a limit to the number of layers that I can have it turn on? Because when I go to leave 160 layers on I keep getting the following error in the command line:

 

; error: bad argument value:  AutoCAD command: #<SUBR @000000002cafd6b0 COND>

Posted

Although the error message you have received seems odd given the circumstances, there may well be a limit to the length of a string which may be passed to a command.

 

Try this instead:

(defun c:loff ( / l )
   (setq l
       (mapcar 'strcase
          '( [color=green];; Layers to keep ON:[/color]
               [highlight]"LAYER1"[/highlight]
               [highlight]"LAYER2"[/highlight]
               [highlight]"LAYER3"[/highlight]
           )
       )
   )
   (vlax-for x (vla-get-layers (vla-get-activedocument (vlax-get-acad-object)))
       (if (member (strcase (vla-get-name x)) l)
           (vla-put-layeron x :vlax-true)
           (vla-put-layeron x :vlax-false)
       )
   )
   (princ)
)
(vl-load-com) (princ)

Change the layers in the highlighted list as required.

Posted
Although the error message you have received seems odd given the circumstances, there may well be a limit to the length of a string which may be passed to a command.

 

Try this instead:

(defun c:loff ( / l )
   (setq l
       (mapcar 'strcase
          '( [color=green];; Layers to keep ON:[/color]
               [highlight]"LAYER1"[/highlight]
               [highlight]"LAYER2"[/highlight]
               [highlight]"LAYER3"[/highlight]
           )
       )
   )
   (vlax-for x (vla-get-layers (vla-get-activedocument (vlax-get-acad-object)))
       (if (member (strcase (vla-get-name x)) l)
           (vla-put-layeron x :vlax-true)
           (vla-put-layeron x :vlax-false)
       )
   )
   (princ)
)
(vl-load-com) (princ)

Change the layers in the highlighted list as required.

 

Another advantage to Lee's code is that it can be executed transparently, and will run a lot faster than using COMMAND.

I've never noticed limitations when feeding a string to the LAYER command, but I must admit, I've never fed it more than wildcards when writing quick macros. If if was any thing like the scale you are wanting, I'd definitely write it without the use of COMMAND.

Posted (edited)

If any of your layers is not existed in the drawing , all layers would be off .

 

(defun c:Test (/ l e c)
;;;--- Tharwat 23. May. 2013 ---;;;
 (while (setq l (tblnext "LAYER" (not l)))
   (setq c (assoc 62 (setq e (entget (tblobjname "LAYER" (cdr (assoc 2 l)))))
          )
   )
   (if (member (strcase (cdr (assoc 2 l)))
               (mapcar 'strcase
                       '("Storm_Drains" "Building" "HVAC" "Sprinkler"
                         "Parking_Lot"
                        )
               )
       )
     (if (minusp (cdr c))
       (entmod (subst (cons 62 (- (cdr c))) c e))
     )
     (if (not (minusp (cdr c)))
       (entmod (subst (cons 62 (- (cdr c))) c e))
     )
   )
 )
 (princ)
)

Edited by Tharwat
Posted
If any of your layers is not existed in the drawing , all layers would be off

 

Try running it twice.

Posted
Try running it twice.

 

Thank you .

 

I didn't expect that , codes modified . :D

Posted
I didn't expect that , codes modified . :D

 

What if one of the layers to remain on is already turned off?

Posted
What if one of the layers to remain on is already turned off?

 

Also modified .

Posted

For what its worth, if a Vanilla AutoLISP solution is required, here is how I might approach it:

([color=BLUE]defun[/color] c:loff ( [color=BLUE]/[/color] c l n x )
   ([color=BLUE]setq[/color] l
       ([color=BLUE]mapcar[/color] '[color=BLUE]strcase[/color]
          '( [color=GREEN];; Layers to keep ON:[/color]
               [color=MAROON]"LAYER1"[/color]
               [color=MAROON]"LAYER2"[/color]
               [color=MAROON]"LAYER3"[/color]
           )
       )
   )
   ([color=BLUE]while[/color] ([color=BLUE]setq[/color] x ([color=BLUE]tblnext[/color] [color=MAROON]"layer"[/color] ([color=BLUE]null[/color] x)))
       ([color=BLUE]setq[/color] n ([color=BLUE]cdr[/color] ([color=BLUE]assoc[/color] 02 x))
             c ([color=BLUE]abs[/color] ([color=BLUE]cdr[/color] ([color=BLUE]assoc[/color] 62 x)))
       )
       ([color=BLUE]if[/color] ([color=BLUE]not[/color] ([color=BLUE]member[/color] ([color=BLUE]strcase[/color] n) l))
           ([color=BLUE]setq[/color] c ([color=BLUE]-[/color] c))
       )
       ([color=BLUE]entmod[/color] ([color=BLUE]subst[/color] ([color=BLUE]cons[/color] 62 c) ([color=BLUE]assoc[/color] 62 x) ([color=BLUE]entget[/color] ([color=BLUE]tblobjname[/color] [color=MAROON]"layer"[/color] n))))
   )
   ([color=BLUE]princ[/color])
)

Posted
For what its worth, if a Vanilla AutoLISP solution is required, here is how I might approach it:

 

I have to admit that your work is very neat indeed . :thumbsup:

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...