Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Need this routine to turn off all layers except one SPECIFIC layer

8 REPLIES 8
SOLVED
Reply
Message 1 of 9
Anonymous
2135 Views, 8 Replies

Need this routine to turn off all layers except one SPECIFIC layer

I need help making this work.  I have a process that I'm trying to streamline in ACAD 2016.  I need this LISP to turn off everything but the "HILMOT-CONV" layer.

 

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

 

It is shutting every layer off, including the one i want to stay on, "HILMOT-CONV"

 

..and NO, I do NOT want to use LAYISO.  That command requires you to select an object on-screen.  I do NOT want to do that, I'm trying to automate as much of my workflow as I can.  That one layer name NEVER changes from project to project so I can use it as a filter to shut everything ELSE off.  Thanks for any help!

 

8 REPLIES 8
Message 2 of 9
Shneuph
in reply to: Anonymous

Try just:

(defun C:loff ( / HILMOT-CONV expert)
(setq expert (getvar "expert"))
(setvar "expert" 1)
(command "._layer" "_off" "*" "_on" "HILMOT-CONV" "")
(setvar "expert" expert)
(princ)
)
---sig---------------------------------------
'(83 104 110 101 117 112 104 64 71 109 97 105 108 46 99 111 109)
Message 3 of 9
Anonymous
in reply to: Shneuph

Thank you thank you THANK YOU! 🙂
Message 4 of 9
ВeekeeCZ
in reply to: Anonymous

I will change that a little bit... that could be a problem if the HILMOT-CONV layer is frozen (stays that way) and if the current layer is any other, it stays current, but off.

 

(defun C:loff ( / expert)
  (setq expert (getvar "expert"))
  (setvar "expert" 0)
  (command "._layer" "_t" "HILMOT-CONV" "_set" "HILMOT-CONV" "_off" "*" "_N" "")
  (setvar "expert" expert)
  (princ)
)

 

Message 5 of 9
Anonymous
in reply to: ВeekeeCZ

Thanks for the extra contribution....turns out I need one more thing at the end of this code.  I have another LISP routine that I want to have automatically start at the end of this routine.  I can't for the life of me figure it out, it seems it should be so simple....I normally start this other routine by typing in CHZ20 and pressing enter.  I can't get it to cooperate at the end of this layer-routine we've been discussing, I've tried things like adding the line (command-s "_CHZ20) into the end but no dice.  ARRRGH!  The CHZ20 routine is also encrypted, otherwise I'd try inserting the layer-off routine into the CHZ20 routine.

Message 6 of 9
Kent1Cooper
in reply to: Anonymous


@Anonymous wrote:

....  I need this LISP to turn off everything but the "HILMOT-CONV" layer.

 

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

 

It is shutting every layer off, including the one i want to stay on, "HILMOT-CONV"

.... 


That (getvar "HILMOT-CONV") would be the source of the trouble [there's no such System Variable].  But I don't see any need to put that in as a variable -- just put double-quotes around it in the Layer command.  It appears that's what @Shneuph did, but didn't remove it from the localized variables list.
But here's a way to do it that does not require dealing with the EXPERT System Variable, and therefore carries no risk that if something goes wrong, that will remain set to what might be the wrong value.  This one's way of turning a Layer off doesn't care whether it's the current Layer.  If it encounters the current Layer before it gets to the target Layer in working through the Layer list, it will turn it off anyway, no questions asked, and the target Layer will be made current when it comes by.

 

(defun LAOB (ONlayer / layobj layname); = Layers All Off But
  (while (setq layobj (tblnext "layer" (not layobj)))
    (setq
      laydata (entget (tblobjname "layer" (cdr (assoc 2 layobj))))
      layname (cdr (assoc 2 laydata))
    ); setq
    (if (= layname ONlayer)
      (command "_.layer" "_thaw" layname "_set" layname ""); then [will turn On if it's Off]
      (entmod (subst (cons 62 (- (abs (cdr (assoc 62 laydata))))) (assoc 62 laydata) laydata))
; else -- turn it off even if current ); if ); while ); defun

Note that it's a function with an argument for the Layer name you want kept [or turned] On, rather than a command with that built in, so that it can be used with any Layer name.  In your case, usage would be:

(LAOB "HILMOT-CONV")

Kent Cooper, AIA
Message 7 of 9
ВeekeeCZ
in reply to: Anonymous


@Anonymous wrote:

Thanks for the extra contribution....turns out I need one more thing at the end of this code.  I have another LISP routine that I want to have automatically start at the end of this routine.  I can't for the life of me figure it out, it seems it should be so simple....I normally start this other routine by typing in CHZ20 and pressing enter.  I can't get it to cooperate at the end of this layer-routine we've been discussing, I've tried things like adding the line (command-s "_CHZ20) into the end but no dice.  ARRRGH!  The CHZ20 routine is also encrypted, otherwise I'd try inserting the layer-off routine into the CHZ20 routine.


(C:CHZ20)

Message 8 of 9
Anonymous
in reply to: ВeekeeCZ

ah...the head-smacking simplicity of it all.

Message 9 of 9
Kent1Cooper
in reply to: ВeekeeCZ


@ВeekeeCZ wrote:

@Anonymous wrote:

.... I've tried things like adding the line (command-s "_CHZ20) into the end but no dice.  ....


(C:CHZ20)


Just to clarify:  The (command) and (command-s) functions accept only AutoCAD's own command names, not custom-defined ones.  That example, in parentheses and including the C: prefix, is the way to call a (defun C:... type of command name in AutoLisp.

Kent Cooper, AIA

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

State of Sustainability Webinar


AutoCAD Inside the Factory