Jump to content
Crazy J

LISP to toggle a pair of layers on and off

Recommended Posts

Crazy J

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

Share this post


Link to post
Share on other sites
MSasu

This can be solved quickly by using Layer States Manager.

 

Regards,

Share this post


Link to post
Share on other sites
Crazy J
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.

Share this post


Link to post
Share on other sites
MSasu

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

Share this post


Link to post
Share on other sites
Pablo Ferral

How about 'LAYISO' and 'LAYUNISO' ?

Share this post


Link to post
Share on other sites
alanjt
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

Share this post


Link to post
Share on other sites
alanjt

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

Share this post


Link to post
Share on other sites
Lee Mac

   (and (setq LayerFeatures (entget (tblobjname "LAYER" LayerItem))

 

Bear in mind that

 

(entget nil)

 

will error.

Share this post


Link to post
Share on other sites
alanjt
Bear in mind that

 

(entget nil)

will error.

You're absolutely right. I completely overlooked that.

Corrected above.

Share this post


Link to post
Share on other sites
DNK

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.

Share this post


Link to post
Share on other sites
MSasu
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,

Share this post


Link to post
Share on other sites
alanjt
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.

Share this post


Link to post
Share on other sites
roy437

how to toggle between all layers off, freeze, lock with all layers on, thaw, unlock

Share this post


Link to post
Share on other sites
Lee Mac

It's not possible to freeze all layers, as the current layer cannot be frozen.

Share this post


Link to post
Share on other sites
BIGAL

-la F * enter, but as Lee has said current layer will not be frozen. -la T * enter.

Share this post


Link to post
Share on other sites
roy437

Let's say we have 7 layers off, freeze, lock and 3 layers on, thaw, unlock. How to toggle between them ?

Share this post


Link to post
Share on other sites
Lee Mac

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

Share this post


Link to post
Share on other sites
roy437
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

Share this post


Link to post
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
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

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