TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: hudster on February 05, 2008, 11:17:17 AM

Title: Reverse Layers
Post by: hudster on February 05, 2008, 11:17:17 AM
Does anyone have a copy of a lisp which can reverse layers so that frozen layers thaw, thawed layers freeze, on to off locked to unlocked etc etc.
Title: Re: Reverse Layers
Post by: CAB on February 05, 2008, 11:32:12 AM
You can modify this one.
Note that the current layer must be changed.
Code: [Select]
  ;;  Toggle Layer On/Off -  CAB 091404 
  (defun layt (/ doc lname)
   (vl-load-com) ; load ActiveX support
   (setq doc (vla-get-activedocument (vlax-get-acad-object)))
                         
   (vlax-for for-item (vla-get-layers doc);(vla-GetExtensionDictionary)
     (setq lname (vlax-get-property for-item 'Name))
     (if (eq :vlax-true
             (vlax-get-property(vla-item (vla-get-Layers doc ) lname)'LayerOn))
        (vlax-put-property (vla-item (vla-get-Layers doc) lname)
          'LayerOn :vlax-false); layer OFF
        (vlax-put-property (vla-item (vla-get-Layers doc) lname)
          'LayerOn :vlax-true); layer ON
     ); endif
   ); vlax-for
  ); defun
Title: Re: Reverse Layers
Post by: gile on February 05, 2008, 12:06:24 PM
Hi,

If you want to reverse the 3 properties (lock, freeze and on) at the same time (note: current Layer wont be frozen)

Code: [Select]
(vlax-for l (vla-get-Layers
      (vla-get-ActiveDocument (vlax-get-acad-object))
    )
  (foreach p '(Lock Freeze LayerOn)
    (if (= (vlax-get l p) -1)
      (vlax-put l p 0)
      (vl-catch-all-apply 'vlax-put (list l p -1))
    )
  )
)
Title: Re: Reverse Layers
Post by: TimSpangler on February 05, 2008, 12:12:32 PM
Here is one I use for on/off layers.  It allows you to select layers that are off to turn them on.

It can propably be modified for the other possibilities


Code: [Select]
;;; ------------------------------------------------------------------------
;;;    LLO.LSP Version 1.0
;;;
;;;    Copyright© MAY, 2001
;;;    Timothy G. Spangler
;;;
;;;    Permission to use, copy, modify, and distribute this software
;;;    for any purpose and without fee is hereby granted, provided
;;;    that the above copyright notice appears in all copies and
;;;    that both that copyright notice and the limited warranty and
;;;    restricted rights notice below appear in all supporting
;;;    documentation.
;;;
;;;    Allows the user to turn on selected layers
;;;
;;; ------------------------------------------------------------------------
(defun C:LLO (/ Off Layer EntList LayerName LayerList Color)

;; Create lists o on/off layers
(while (setq Layer (tblnext "LAYER" (null Layer)))
(if (minusp (cdr (assoc 62 Layer)))
(setq Off T)
)
)
;; Select layers to turn on
(if Off
(progn
;; Invert layers
(while (setq Layer (tblnext "LAYER" (null Layer)))
(LAYER_SWITCH (cdr (assoc 2 Layer)))
)
;; Select layers to turn on
(while (setq EntName (car (entsel "\n Select layers(s) to turn on:")))
(setq EntList (entget EntName))
(setq LayerName (cdr(assoc 8 EntList)))
(setq LayerList (entget (tblobjname "LAYER" LayerName)))
(setq Color (assoc 62 LayerList))
(entmod (subst (cons 62 (- (cdr Color))) Color LayerList))
)
;; Reinvert layers
(while (setq Layer (tblnext "LAYER" (null Layer)))
(LAYER_SWITCH (cdr (assoc 2 Layer)))
)
)
(princ "\n All layers are turned on")
)
(princ)
)
;; Sub routine to turn layers on/off
(defun LAYER_SWITCH (Layer / Tmp Clr)

(setq Tmp (entget (tblobjname "LAYER" Layer)))
(setq Clr (assoc 62 Tmp))
(entmod (subst (cons 62 (- (cdr Clr))) Clr Tmp))
(princ)
)
;;;
;;; Echos to the command line
(princ "\nTurns on selected layers v1.0 \n ©Timothy Spangler, \n May, 2001....loaded.")
(terpri)
(princ "C:LLO")
(print)
;;; End echo
Title: Re: Reverse Layers
Post by: CAB on February 05, 2008, 12:26:03 PM
Nice one Gile. :-)
Title: Re: Reverse Layers
Post by: gile on February 05, 2008, 12:33:03 PM
Nice one Gile. :-)

Thanks.
Title: Re: Reverse Layers
Post by: JohnK on February 06, 2008, 09:15:05 AM
On a lighter side, you can always just flip the bits.

Here is a real quick stab (You can have the job of making it pretty).

Code: [Select]
( (lambda ( layername / layer lsylst props off freeze lock )

    ;; 70 - Standard flags (bit-coded values):
    ;; 1  = Layer is frozen; otherwise layer is thawed
    ;; ...
    ;; 4  = Layer is locked
    ;; ...
    ;; 62 - Color number (if negative, layer is off)

    (setq layer (tblobjname "LAYER" layername))
    (setq laylst (entget layer)
          props (cdr (assoc 70 laylst))
          off (cdr (assoc 62 laylst))
          freeze 1
          lock 4
          )

    (setq props (boole 6 props freeze))
    (setq off (1+ (~ off)))
    (setq props (boole 6 props lock))

    (setq laylst (subst (cons 70 props) (assoc 70 laylst) laylst)
          laylst (subst (cons 62 off) (assoc 62 laylst) laylst))

    (entmod laylst)
    )
 "<YOUR LAYER NAME HERE>"
 )

Oh, and just be careful with this lil baby ( HINT: current layer).
Title: Re: Reverse Layers
Post by: JohnK on February 06, 2008, 09:29:32 AM
Oh and just in case some one decides to be cute; i know 1+4=5. I choose the longer route so i didn't just post a two line obscure-ish procedure.
Title: Re: Reverse Layers
Post by: CAB on February 06, 2008, 10:13:52 AM
Interesting John, always thinking outside the box!   8-)
Title: Re: Reverse Layers
Post by: daron on February 06, 2008, 11:02:11 AM
>Oh, and just be careful with this lil baby ( HINT: current layer).
I ran your function John, with 0 as the current layer and it froze the current layer. However, it's a bit more of a trick to thaw it. Fun fun.
Title: Re: Reverse Layers
Post by: JohnK on February 06, 2008, 11:04:47 AM
Interesting John, always thinking outside the box!   8-)

yeah, just saw the post yesterday before i shutdown for the night and was thinking about it when i drove in this morn. I thought it would be a fun little exercise.

But i suppose i cant take all the credit, SMadsen and i had a discussion about bit codes (OSMODE and such) once or twice where he demonstrated to me the power of a quick little bit flipper procedure.  Good fun stuff.
Title: Re: Reverse Layers
Post by: JohnK on February 06, 2008, 11:06:45 AM
>Oh, and just be careful with this lil baby ( HINT: current layer).
I ran your function John, with 0 as the current layer and it froze the current layer. However, it's a bit more of a trick to thaw it. Fun fun.

Cool huh?
Just re-run the code

Now for my next trick i will delete layer zero in one of your production drawings.
Title: Re: Reverse Layers
Post by: daron on February 06, 2008, 11:08:31 AM
Just flip the bits again. That's what I figured and did, thanks. Interesting how you can't just go to the pull-down and thaw it though. At least it didn't work when I tried it.
Title: Re: Reverse Layers
Post by: daron on February 06, 2008, 11:09:18 AM
That would be awesome. Ooh, better yet, Defpoints. Make that baby go away.
Title: Re: Reverse Layers
Post by: JohnK on February 06, 2008, 11:21:47 AM
You cant thaw it via the Pull down or layer mgr cause your not supposed to be able to have the current layer frozen.
Title: Re: Reverse Layers
Post by: daron on February 06, 2008, 11:54:56 AM
Well, apparently bit flags are the back door to that problem. Funny thing is, you can attempt to draw and it will look like you are actually drawing something, but when you thaw and turn the layer back on, nothing shows up, at least not for me. I like that back door. It opens up lots of ideas: Like, is it possible to alter the coordinates of objects that are on a frozen layer, without thawing that layer?
Title: Re: Reverse Layers
Post by: JohnK on February 06, 2008, 01:06:07 PM
Well, apparently bit flags are the back door to that problem. Funny thing is, you can attempt to draw and it will look like you are actually drawing something, but when you thaw and turn the layer back on, nothing shows up, at least not for me. I like that back door. It opens up lots of ideas: Like, is it possible to alter the coordinates of objects that are on a frozen layer, without thawing that layer?

I dunno, never tried. Ive messed with objects set to "not visible" but never frozen. ...Give it a whirl and report back.
Title: Re: Reverse Layers
Post by: daron on February 06, 2008, 02:34:07 PM
That's strange. I took Quamper's circle problem froze the 0 layer using your function and attempted to increase the size of all the radii by 1. ACAD acted like it performed the function, but didn't. However, using your function again to thaw everything, the circles don't show up. If I use the same ssget function that selected them when they were frozen to collect them again, they are selected and in the database, but regen won't force them to show up. On that thought, I checked for flipped bit flags and tested them again, changing dxf code 67 from 0 to... 0 and they appeared. No idea why, but I did learn that I can't alter an object that is frozen, even through the back door.
Here's the code I used for changing the radii:
Code: [Select]
(setq ss (ssget "x" (list (cons 0 "CIRCLE"))))
(setq cnt (1- (sslength ss)))
(while (> cnt -1)
 (setq ename (ssname ss cnt))
       cnt (1- cnt)
       elist (entget ename)
       alter (subst (1+ (cdr (assoc 40 elist))) (assoc 40 elist) elist))
     (entmod alter)
     (entupd ename)
     )
Title: Re: Reverse Layers
Post by: JohnK on February 06, 2008, 04:49:57 PM
huh? Cool.

Here is a full blown demo of what Daron is referring to.

Code: [Select]
( (lambda ( / )

    ;; support procedures

    (defun tog_layer_stuff ( layername / layer lsylst props off freeze lock )

      ;; 70 - Standard flags (bit-coded values):
      ;; 1  = Layer is frozen; otherwise layer is thawed
      ;; ...
      ;; 4  = Layer is locked
      ;; ...
      ;; 62 - Color number (if negative, layer is off)
      ;;
      ;; Or if your feeling adventrous you can combine frozen and locked
      ;; (1+4=5). something like...
      ;;
      ;; (setq laylst (entget (tblobjname "LAYER" "<YOUR LAYER NAME HERE>")))
      ;; (entmod
      ;;    (subst
      ;;       (cons 70 (boole 6 (cdr (assoc 70 laylst)) 5))
      ;;       (assoc 70 laylst)
      ;;       laylst))
      ;;

      (setq layer (tblobjname "LAYER" layername))
      (setq laylst (entget layer)
            props (cdr (assoc 70 laylst))
            off (cdr (assoc 62 laylst))
            freeze 1
            lock 4
            )

      (setq props (boole 6 props freeze))
      (setq off (1+ (~ off)))
      (setq props (boole 6 props lock))

      (setq laylst (subst (cons 70 props) (assoc 70 laylst) laylst)
            laylst (subst (cons 62 off) (assoc 62 laylst) laylst))

      (entmod laylst)
      )


    (defun change_circ ()
      (setq ss (ssget "x" '((0 . "CIRCLE"))))
      (setq
        elist (entget (ssname ss 0))
        alter (subst (cons 40 (1+ (cdr (assoc 40 elist)))) (assoc 40 elist) elist))
      (entmod alter)
      )

    (defun draw_circ ( )
      (command "circle")
      (while (eq (logand 1 (getvar "CMDACTIVE")) 1)
             (COMMAND PAUSE)))

    ;; end support procedures


    ;; do some stuff
    (tog_layer_stuff "0")
    (draw_circ)
    (change_circ)
    (tog_layer_stuff "0")

    (command "change" "all" "" "p" "color" "251" "")

    )
 )

Daron
Toy around with this some more -e.g. move tog_layer_stuff up in the list and see what happens.
Title: Re: Reverse Layers
Post by: daron on February 06, 2008, 04:55:36 PM
A'right, another day though. I need to get something finished up here and get out of here. See you tomorrow.
Title: Re: Reverse Layers
Post by: daron on February 07, 2008, 09:14:59 AM
That is so crazy. You still have to thaw the layer to be able to make changes. It still bugs me that you have to make a physical change to it to make the objects visible. i.e. (command "change" "all" "" "p" "color" "251" "")
Title: Re: Reverse Layers
Post by: JohnK on February 07, 2008, 10:07:58 AM
Yeah that part confused me too. But i had fun toying with the thought.

...I wonder what a redraw will do?