Jump to content

If layer is on turn it off if off turn it on


TheZman

Recommended Posts

There are several methods to do this, here is one

 

Make a layer even if it already exists - this will become the current layer

 

(command "layer" "m" "mylayer" "C" "150" "" "L" "Continuous" "" "")

 

now turn off all other layers but keep the current layer on:

 

(COMMAND "LAYER" "OFF" "*" "N" "")

 

Hope this helps

Link to comment
Share on other sites

(defun foo (layer / data)
 (if (and (setq data (tblobjname "LAYER" layer)) (setq data (entget data)))
   (entmod (subst (cons 62 (- (cdr (assoc 62 data)))) (assoc 62 data) data))
 )
)

 

eg.

(foo (getvar 'clayer))

Link to comment
Share on other sites

you can do it this way too..

 

(defun c:sa ()
  (vl-load-com)
  (if (= "your_layer"   
       (getvar "clayer"))
    (princ (strcat "\nYour_layer" " is the current layer"))
(progn    
  (if (= ur_flag  nil)
 (progn
    (setq ur_flag 'T)
    (vl-cmdf "_.layer" "on" "your_layer" "")
     )
   (progn
    (vl-cmdf "_.layer" "off" "your_layer" "")
    (setq ur_flag 'nil)
   )
    )
 )
)
(princ)
 )

 

since you only have one specific layer to swtich on and off :)

Edited by pBe
fat fingers syndrome
Link to comment
Share on other sites

you can do it this way too..

 

(defun c:sa ()
  (vl-load-com)
  (if (= "your_layer"   
       (getvar "clayer"))
    (princ (strcat "\nYour_layer" " is the current layer"))
(progn    
  (if (= ur_flag  nil)
 (progn
    (setq ur_flag 'T)
    (vl-cmdf "_.layer" "on" "your_layer" "")
     )
   (progn
    (vl-cmdf "_.layer" "off" "your_layer" "")
    (setq ur_flag 'nil)
   )
    )
 )
)
(princ)
 )

since you only have one specific layer to swtich on and off :)

All that does is turn the layer on/off based on the the variable ur_flag being T. Why not check the definition of the layer and just see if it's on or off?

Link to comment
Share on other sites

thats exactly the point.. :)

 

yup variable Ur-flag T or nil

 

if the layer is on it will turn it off and vice versa

assuming the layer is already existing ... :unsure:

well its the first thing pops into my head ..

Link to comment
Share on other sites

thats exactly the point.. :)

 

yup variable Ur-flag T or nil

 

if the layer is on it will turn it off and vice versa

assuming the layer is already existing ... :unsure:

well its the first thing pops into my head ..

And if the user wants to toggle the layer off the first time, they will have to issue your command twice, or someone changed the on/off state of the layer after issuing your routine, it will never give the desired toggle result.

Like I said, why not base the toggle on actual layer properties, rather than a user set variable?

Link to comment
Share on other sites

i know what you mean... :)

its just a simple lisp that assumesa particular condition i.e.

the layer is already there and the layer is currently turned "off" and you're right about invoking the command twice if you started when the layer is "on"

goodcatch alanjt....

what about reversing the sequence... better yet make the first line "off" ?

still the same result though... :lol: what am i thinking :shock:

 

ayt...

Link to comment
Share on other sites

Another, using VL:

 

(defun LayTog ( layer ) (vl-load-com)
 (if
   (not
     (vl-catch-all-error-p
       (setq layer
         (vl-catch-all-apply 'vla-item
           (list
             (vla-get-layers
               (vla-get-ActiveDocument (vlax-get-acad-object))
             )
             layer
           )
         )
       )
     )
   )
   (vlax-put layer 'layeron (~ (vlax-get layer 'layeron)))
 )
)

Link to comment
Share on other sites

There you go...

Exactly what Alanjit's pointing out all along...

First: Grab the layerstate, then from there it switches from on to off

Awesome......

Lee Mac / Alanjit

 

You guys are the best!!

 

I would have written the code that way if given the time.. say like a month!!! :lol:

Link to comment
Share on other sites

[b][color=BLACK]([/color][/b]defun toglay [b][color=FUCHSIA]([/color][/b]l[b][color=FUCHSIA])[/color][/b]
[b][color=FUCHSIA]([/color][/b]command [color=#2f4f4f]"_.LAYER"[/color]
  [b][color=NAVY]([/color][/b]if [b][color=MAROON]([/color][/b]minusp [b][color=GREEN]([/color][/b]cdr [b][color=BLUE]([/color][/b]assoc 62 [b][color=RED]([/color][/b]tblsearch [color=#2f4f4f]"LAYER"[/color] l[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b] [color=#2f4f4f]"_ON"[/color] [color=#2f4f4f]"_OFF"[/color][b][color=NAVY])[/color][/b] l [color=#2f4f4f]""[/color][b][color=FUCHSIA])[/color][/b]
[b][color=FUCHSIA]([/color][/b]prin1[b][color=FUCHSIA])[/color][/b][b][color=BLACK])[/color][/b]

 

usage: (toglay "3D")

 

 

Depending on the release your are using, turning off the current layer can lead to a extra input. QAFLAGS or NOMUTT may be able to compensate for that.

 

Also testing for the existence of the layer wouldn't be a bad thing. -David

Link to comment
Share on other sites

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...