Jump to content

LISP to toggle a pair of layers on and off


Crazy J

Recommended Posts

I see a lot of the LISPs posted here. And I'd like to get into using them. I see how they have value to speed up repetitive tasks.

 

I currently have a project that is enclosed in a four piece frame. Two endcaps and two rails. Very often when I am checking something or moving mounting holes or such, I go to layer manager and turn off the layer "frame-rails" and turn on the layer "frame-endcaps" and vice-versa.

 

It got me thinking that a quick LISP should be possible to do this. I was going to try and learn the command line layer manager entries to do so, and see if I found that to be quicker than going into the layer manager or layer pull-down toolbar, but I never got to it at work today.

 

Thought I'd throw this out here and see if anyone had any thoughts. And I'm a LISP newbie, so go easy on me....

Link to comment
Share on other sites

This can be solved quickly by using Layer States Manager.

 

Regards,

 

I understand that... and some times I just hit the "layer previous" button. I also have layer states saved in the layer manager. And they are very helpful. I've gotten pretty familiar with layer manager. Unless there is something new in layer manager in versions more recent than 2002.

 

But it would be much faster if I had a LISP loaded and could just type "frame".

 

Or for architect types. If you include on/off toggles for electrical, plumbing, etc... some of your main layers.

 

While we're on the subject, is there perhaps a quick keystroke sequence to step through your various saved layer states? That would be useful also.

Link to comment
Share on other sites

This routine will allows to switch current state of above noted layers:

 

(defun c:1( / LayerFeatures LayerState )
(foreach LayerItem '("frame-rails" "frame-endcaps")
 (setq LayerFeatures (entget (tblobjname "LAYER" LayerItem))   ;list layer's properties
       LayerState (cdr (assoc 62 LayerFeatures)))              ;get current state (on/off)
 (entmod (subst (cons '62 (* -1 LayerState))                   ;switch state
                (assoc 62 LayerFeatures)
                LayerFeatures))
)
(princ)
)

 

Regards,

  • Like 2
Link to comment
Share on other sites

This routine will allows to switch current state of above noted layers:

 

(defun c:1( / LayerFeatures LayerState )
(foreach LayerItem '("frame-rails" "frame-endcaps")
 (setq LayerFeatures (entget (tblobjname "LAYER" LayerItem))   ;list layer's properties
       LayerState (cdr (assoc 62 LayerFeatures)))              ;get current state (on/off)
 (entmod (subst (cons '62 (* -1 LayerState))                   ;switch state
                (assoc 62 LayerFeatures)
                LayerFeatures))
)
(princ)
)

Regards,

 

 

msasu, I hope you don't mind, but I made some slight modifications to your code.

 

(defun c:1 (/ LayerFeatures)
 (foreach LayerItem '("frame-rails" "frame-endcaps")
   (and (setq LayerFeatures (tblobjname "LAYER" LayerItem))
        (setq LayerFeatures (entget LayerFeatures)) ;list layer's properties
        (entmod (subst (cons 62 (- (cdr (assoc 62 LayerFeatures)))) ;switch state
                       (assoc 62 LayerFeatures)
                       LayerFeatures
                ) ;_ subst
        ) ;_ entmod
   ) ;_ and
 ) ;_ foreach
 (princ)
) ;_ defun

Link to comment
Share on other sites

Here's a very simple example of doing it with VLA...

 

 

(defun LayTog (#List / #Layers #Temp)
 (or *AcadDoc* (setq *AcadDoc* (vla-get-activedocument (vlax-get-acad-object))))
 (setq #Layers (vla-get-layers *AcadDoc*))
 (foreach x #List
   (and (not (vl-catch-all-error-p (setq #Temp (vl-catch-all-apply 'vla-item (list #Layers x)))))
        (vla-put-layeron
          #Temp
          (if (eq (vla-get-layeron #Temp) :vlax-false)
            :vlax-true
            :vlax-false
          ) ;_ if
        ) ;_ vla-put-layeron
   ) ;_ and
 ) ;_ foreach
) ;_ defun

Link to comment
Share on other sites

I supply this function to my team and engineers. Since I keep it as simple as possible (some use LT), I've got macros and use buttons to do what I need.

 

^C^C_-LAYER;FREEZE;LAYER1;THAW;LAYER2;;REGEN;

 

I suppose you could use the keyboard shortcuts if you didn't want a button.

Link to comment
Share on other sites

msasu, I hope you don't mind, but I made some slight modifications to your code.

 

No problem at all! In fact that error trapping solution (AND encapsulation) it’s an interesting approach that I will have to add to my programming practice! Thanks!

 

Regards,

Link to comment
Share on other sites

No problem at all! In fact that error trapping solution (AND encapsulation) it’s an interesting approach that I will have to add to my programming practice! Thanks!

 

Regards,

I just wanted to make sure. :)

Minus the error check, there was nothing wrong with your code. I was really wanting to show you a few ways to shorten the routine.

Link to comment
Share on other sites

  • 10 years later...

Here's one possible way:

(defun c:toglay ( )
    (vlax-for lay (vla-get-layers (vla-get-activedocument (vlax-get-acad-object)))
        (foreach prp '(lock layeron freeze)
            (vl-catch-all-apply 'vlax-put (list lay prp (~ (vlax-get lay prp))))
        )
    )
    (princ)
)
(vl-load-com) (princ)

 

  • Like 2
Link to comment
Share on other sites

33 minutes ago, Lee Mac said:

Here's one possible way:


(defun c:toglay ( )
    (vlax-for lay (vla-get-layers (vla-get-activedocument (vlax-get-acad-object)))
        (foreach prp '(lock layeron freeze)
            (vl-catch-all-apply 'vlax-put (list lay prp (~ (vlax-get lay prp))))
        )
    )
    (princ)
)
(vl-load-com) (princ)

 

It is very short and concise with a special elegance.
Only one command needs to be added, "regen".
What more could I say, it's not for nothing that you are called a cad guru, you are fantastic, you are the greatest man, thank you very much Lee Mac.

  • Like 1
Link to comment
Share on other sites

  • 9 months later...

Hi!
I use layiso (lock layer mode) a lot while editing drawings.
When I do another layer Re-layiso I will have to unlock that layer. Can lisp do the layiso of another layer and unlock that layer? Many thanks

Edited by vuxvix
Link to comment
Share on other sites

 

On 3/17/2010 at 10:21 PM, Crazy J said:

Or for architect types. If you include on/off toggles for electrical, plumbing, etc... some of your main layers.

 

I use wildcards with layer name toggles

 

(defun c:ANOFF   () (SAA_TurnLayersOff (strcat SAA_LAYANNOP "," SAA_LAYGRID "," SAA_LAYCLOUD)) (princ))
(defun c:ANON    () (command "_.-layer" "on"  (strcat SAA_LAYANNOP "," SAA_LAYGRID "," SAA_LAYCLOUD) "") (princ))
(defun c:CLOUDOFF () (SAA_TurnLayersOff (strcat SAA_LAYCLOUD)) (princ))
(defun c:CLOUDON () (command "_.-layer" "on"  (strcat SAA_LAYCLOUD) "") (princ))
(defun c:COFF    () (SAA_TurnLayersOff SAA_LAYCOLR) (princ))
(defun c:CON     () (command "_.-layer" "on"  SAA_LAYCOLR "") (princ))
(defun c:CREDOFF () (SAA_TurnLayersOff "c-red,*|c-red") (princ))
(defun c:DOFF    () (SAA_TurnLayersOff SAA_LAYDEMO) (princ))
(defun c:DON     () (command "_.-layer" "on"  SAA_LAYDEMO "") (princ))
(defun c:FOFF    () (SAA_TurnLayersOff SAA_LAYFIRE) (princ))
(defun c:FON     () (command "_.-layer" "on"  SAA_LAYFIRE "") (princ))
(defun c:FRAMOFF () (SAA_TurnLayersOff SAA_LAYFRAME) (princ))
(defun c:FRAMON  () (command "_.-layer" "on"  SAA_LAYFRAME "") (princ))
(defun c:FURNOFF () (SAA_TurnLayersOff SAA_LAYFURN) (princ))
(defun c:FURNON  () (command "_.-layer" "on"  SAA_LAYFURN "") (princ))
(defun c:GOFF    () (SAA_TurnLayersOff SAA_LAYGRID) (princ))
(defun c:GON     () (command "_.-layer" "on"  SAA_LAYGRID "") (princ))
(defun c:HIDOFF  () (SAA_TurnLayersOff SAA_LAYLTHIDD) (princ))
(defun c:HIDON   () (command "_.-layer" "on"  SAA_LAYLTHIDD "") (princ))
(defun c:HOFF    () (SAA_TurnLayersOff SAA_LAYHACH) (princ))
(defun c:HON     () (command "_.-layer" "on"  SAA_LAYHACH "") (princ))
(defun c:IOFF    () (SAA_TurnLayersOff SAA_LayImage) (princ))
(defun c:ION     () (command "_.-layer" "on"  SAA_LayImage "") (princ))
(defun c:MOFF    () (SAA_TurnLayersOff SAA_LAYMASK) (princ))
(defun c:MON     () (command "_.-layer" "on"  SAA_LAYMASK "") (princ))
(defun c:NFCOFF  () (SAA_TurnLayersOff SAA_LAYNFC) (princ))			;turn off not for construction layer
(defun c:NFCON   () (command "_.-layer" "on"  SAA_LAYNFC "") (princ))
(defun c:PRHTOFF () (command "_.-layer" "off" "*prht*" "")(princ))	;Turn off partial height
(defun c:PRHTON  () (command "_.-layer" "on"  "*prht*" "")(princ))	;Turn on partial height
(defun c:RMOFF   () (SAA_TurnLayersOff SAA_LAYROOMNAME) (princ))
(defun c:RMON    () (command "_.-layer" "on"  SAA_LAYROOMNAME "") (princ))
(defun c:SCRNOFF () (SAA_TurnLayersOff SAA_LAYSCRN) (princ))
(defun c:SCRNON  () (command "_.-layer" "on"  SAA_LAYSCRN "") (princ))
(defun c:SHADOFF () (SAA_TurnLayersOff SAA_LAYSHAD) (princ))
(defun c:SHADON  () (command "_.-layer" "on"  SAA_LAYSHAD "") (princ))
(defun c:SITEOFF () (command "_.-layer" "off" "*site*|*,*site-mask" "")(princ))	;Turn off site xrefs
(defun c:SKYOFF  () (SAA_TurnLayersOff SAA_LAYSKY) (princ))
(defun c:SKYON   () (command "_.-layer" "on"  SAA_LAYSKY "") (princ))
(defun c:TOFF    () (SAA_TurnLayersOff (strcat SAA_LAYTBLK)) (princ))
(defun c:TON     () (command "_.-layer" "on"  (strcat SAA_LAYTBLK) "") (princ))
(defun c:XOFF    () (SAA_TurnLayersOff SAA_LAYXREF) (princ))		;Turn off xref layers
(defun c:XZOFF   () (command "_.-layer" "off" "z*|*,*nplt*|*" "")(princ))	;Turn off NPLT xrefs
(defun c:XZON    () (command "_.-layer" "on" "z*|*,*nplt*|*" "")(princ))	;Turn on NPLT xrefs
(defun c:XON     () (command "_.-layer" "on"  SAA_LAYXREF "") (princ))	;Turn on xref layers

;;; ==============================================================================
;;; SAA Layer variables for shortcuts
;;; ==============================================================================

(setq 
      ;; order is last on top
      ;; last *DIMS is to make sure above secondary dims like A-ANNO-DIMS-FTIN
      SAA_LAYANNO  "*ctrl*,*center*,*f#hr*,*spotelev*,*bndry*,*easement*,*iden*,*a-area*,*name*,*label*,*delta*,*_id*,*tar*,*t-mach*,*iden*,*txt*,*text*,*note*,*anno*,*symb*,*dim*,*dims"
      SAA_LAY3DON  "*z-3d-saveBox*"	;for 3d x-plan & x-elev prep, layers to keep on (and not delete), using c:z3don
      SAA_LAYANNOP (strcat "*prop*," SAA_LAYANNO)
      SAA_LAYCLOUD "*cloud*,*rev-mark*,*delta*,*revc*,*revs*"
      SAA_LAYCOLR  "25[0-4],*|25[0-4],c-*,*|c-*,*color*"	;OLD NOT USED, see LAY-HACH",*htch*,*patt*,*hach*,*hatch*,*fill*,*exist-nic*,*poche*,*shad*"
      SAA_LAYDEMO  "*demo*,*rmve*"
      SAA_LAYFIRE  "*f#hr*"
      SAA_LAYFRAME "*g-*framing*,*a-wall-fram*"
      SAA_LAYFREEZENOT  "~_*,*keynotes|*"	;exclude from FOXL freezing
      SAA_LAYFURN  "*furn*"
      SAA_LAYGRID  "[as]-grid*,*|[as]-grid*,*$?$*[as]-grid*"
      SAA_LAYHACH  "*patt*,*hach*,*hatch*,*-fill*,*htch*,*poche*,*shad*,*g-####-tree*,*g-####-????-tree*"
      SAA_LAYIMAGE "im-*,*[|$]im-*,z-im-*,*[|$]z-im-*,*image*"
      SAA_LAYLOCK  "s-n-*,x*,a-e-*,$A-XREF*,G-Xref*,$A-IMAGE*,G-Image*,z-nplt-grid"
      SAA_LAYLTHIDD "*hid#*,*hidd*,*ctr#*,*dsh#*,*dsd#*,*ovhd*,*flor-risr"
      SAA_LAYMASK  "*MASK*,*HIDE*,*255*"
      SAA_LAYNFC   "*A-Anno-Title-NFC"
      SAA_LAYOFF   "*layout*,*nplt*,*noplot*,*no?plot*,*non?plot*,Z-*,*|Z-*,*vport*,*viewport*"
      SAA_LAYPLANTHACH  "c*tree*,c*plant*,c*grass*,c*shrub*,c*lawn*,*|c*tree*,*|c*plant*,*|c*grass*,*|c*shrub*,*|c*lawn*"
      SAA_LAYROOMNAME  "*area-iden*,*area-occp*,*area-room*"
      SAA_LAYSCRN  "*SCREEN*,*SCRN*,*SHAD*,*SOLD*,*f#hr*"
      SAA_LAYSHAD  "*SHAD*,*SOLD*"
      SAA_LAYSKY   "*C*SKY"
      SAA_LAYTBLK  "*tblk*,*titl*"
      SAA_LAYXREF  "G-XREF*,*|*,X-*,*|[~Z]*"		;exlude xref z layers with [~Z]
)

;;;=========================================================================
;;; turn off layers, if current layer would be turned off
;;; set to layer 0 and alert
;;;=========================================================================
(defun SAA_TurnLayersOff ( patt / )
  (if (and (wcmatch (strcase (getvar "clayer")) (strcase patt))
      ) ;_and
    ;; if match to current layer, check options on warning
    (progn
      (if (or
            (> (getvar "expert") 0)	;no dialog if expert mode set
            (/= 1 (dos_msgbox "Current layer turned off\nNow set to layer 0" "Info"  2 4 2))
          ) ;_or
        ;; if no answer or OK, set layer to 0 and turn current layer off
        (progn
          (command "_.-layer" "thaw" "0" "on" "0" "set" "0" "off" patt "")
        ) ;_progn
        ;; if cancel, don't turn current layer off
        (progn
          (SETV "expert" 0) ;make sure to ask for warning
          (command "_.-layer" "off" patt "no" "")
          (RSETV "expert") ;make sure to ask for warning
        ) ;_progn
      ) ;_if
    ) ;_progn
    ;; no match to current layer
    (progn
      (command "_.-layer" "off" patt "")
    ) ;_progn
  ) ;_if
  (redraw)
)

 

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