Turning off Layers in AutoLisp

Turning off Layers in AutoLisp

Anonymous
Not applicable
8,884 Views
13 Replies
Message 1 of 14

Turning off Layers in AutoLisp

Anonymous
Not applicable

Hello guys,

 

for my AutoLisp routine i am looking for a program, which turns off certain layers.

 

This is the following code i wrote, but it doesnt works.

 

 

(DEFUN LAYER_OFF()
(setq Layoff(getstring "\Should the Layer C165-C345-EBENE turned off? (j/n): "))
(if (or (= Layoff "j") (= Layoff "J") (= Layoff "y") (= Layoff "Y"))
(if (= (tblsearch "layer" "C165-C345-EBENE") nil)
(command "_layer" "_off" "C165-C345-EBENE" "")
);IF
);IF
);DEFUN LAYER_OFF

 

 

Pls Help, Thanks!!

Accepted solutions (2)
8,885 Views
13 Replies
Replies (13)
Message 2 of 14

Anonymous
Not applicable

You can use "layoff" command instead of routine

 

or

 

 

(DEFUN c:LAYER_OFF()
(setq Layoff(getstring "\Should the Layer C165-C345-EBENE turned off? (j/n): "))
(if (or (= Layoff "j") (= Layoff "J") (= Layoff "y") (= Layoff "Y"))

(progn
(if (and (/= (getvar "clayer") "C165-C345-EBENE") (tblsearch "layer" "C165-C345-EBENE"))

(command "-layer" "off" "C165-C345-EBENE" "")

(command "-layer" "off" "C165-C345-EBENE" "y" "")
);IF

);progn
);IF
);DEFUN LAYER_OFF

 

 

STM

 

0 Likes
Message 3 of 14

Anonymous
Not applicable

Hello thavasi1982,

 

thank you for your help, but it doesn't work....

0 Likes
Message 4 of 14

Anonymous
Not applicable

The approach was right. I modified it at some places and it works now.

 

(DEFUN c:LAYER_OFF()
(setq Layoff(getstring "\Should the Layer C165-C345-EBENE turned off? (j/n): "))
(if (or (= Layoff "j") (= Layoff "J") (= Layoff "y") (= Layoff "Y"))

 

(progn
(if (and (/= (getvar "clayer") "C165-C345-EBENE") (tblsearch "layer" "C165-C345-EBENE"))
(command "_layer" "_off" "C165-C345-EBENE" "")
(command "_layer" "_off" "C165-C345-EBENE" "y" "")
);IF
);progn


);IF
);DEFUN LAYER_OFF

 

 

The next problem is to use this command for more layers at once. Like "c015_c195-EBENE" / "c030_c210-EBENE / ... but i don't want to use the command Layers_off just one time.

 

Can u help me??

0 Likes
Message 5 of 14

Kent1Cooper
Consultant
Consultant
Accepted solution

@Anonymous wrote:

.... 

The next problem is to use this command for more layers at once. Like "c015_c195-EBENE" / "c030_c210-EBENE / ... but i don't want to use the command Layers_off just one time. ....


If I understand some things correctly:

:: You do actually "want to use the command ... just one time," but to have it ask within one running of the command about multiple Layers;

:: Your J and N stand for [forgive my linguistic assumption, and adjust accordingly] Ja and Nein, but you also want the User to be able to answer with Yes or No;

then you can do the following.  Some comments:

 

Doing the turning off via (vla-put...) means you don't need to check whether it's the current Layer, to handle it differently depending on whether it is.  It does it even to the current Layer, without asking the User about it.

 

Using (initget)/(getkword) [read about them in Help] means it doesn't matter whether the User types in upper or lower case [so you don't need all those possibilities in your (or) function], nor whether they type just the initial or more [or all] of the word [more than the initial would fail in your version].  I included the "No" option only in case someone should choose to type that entire word, however unlikely that may be, rather than just the initial that would return "Nein."  And if they hit a wrong key entirely, it asks again, instead of failing.

 

You could add Yes/No to the options in the prompt if you like.  It currently requires an explicit answer each time [no Enter], but could be made to have a default [either way] that they can get by hitting Enter, if you like.

 

Lightly tested:

(defun C:LAYERS_OFF (/ layobj)
  (foreach layname '("c015_c195-EBENE" "c030_c210-EBENE"); add as many as you like
    (initget 1 "Ja Nein Yes No")
    (if
      (and
        (setq layobj (tblobjname "layer" layname)); it exists
        (wcmatch
          (getkword (strcat "\nShould the Layer " layname " be turned Off? [Ja/Nein]: "))
          "Ja,Yes"; User typed J/j/Y/y/Ja/Yes/JA/YES/Ye/jA/yEs/...
        ); wcmatch
      ); and
      (vla-put-LayerOn (vlax-ename->vla-object layobj) 0); then -- turn it Off
    ); if
  ); foreach
  (princ)
); defun LAYERS_OFF
(vl-load-com); if needed
Kent Cooper, AIA
Message 6 of 14

Anonymous
Not applicable

Hello Kent1Cooper,

 

0 Likes
Message 7 of 14

Anonymous
Not applicable

Thank you all

0 Likes
Message 8 of 14

Anonymous
Not applicable

Hello Kent1Cooper,

 

is there a reverse Lisp command, which searches for the defined Layers: c000_c180-EBENE / c090_c270-EBENE / text / LVK_Netz and let them be turned on, but turns off all the other layers in the dwg-file. The problem is that the layers which shall be turned off is every time a different number that means it must be a command which maybe first turns off all layers in the file and after that turns on the above mentioned layers.

 

I tried like that:

 

(defun LAYERS_OFF()

(if (tblnext "layer" T)
	(if(or (/= (getvar "clayer") "C000-C180-EBENE")
		   (/= (getvar "clayer") "C090-C270-EBENE")
		   (/= (getvar "clayer") "lvk_netz")
		   (/= (getvar "clayer") "text")
		   
		   
		);OR
	);IF
);IF
(command "_layer" "_off" "clayer" "")
);DEFUN Layers_off

But of course it doesn't work 😕

0 Likes
Message 9 of 14

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

.... 

is there a reverse Lisp command, which searches for the defined Layers: c000_c180-EBENE / c090_c270-EBENE / text / LVK_Netz and let them be turned on, but turns off all the other layers in the dwg-file. ....


You can use more than one option in a single Layer command [that's why it needs that extra Enter at the end, to tell it you're done], and you can apply most options in the Layer command to more than one Layer at a time, with comma separators:

 

(command "_.layer" "_off" "*" "_yes" "on" "c000_c180-EBENE,c090_c270-EBENE,text,LVK_Netz" "")

 

Change that answer about the current Layer if you like, or add a "_set" option to make current one of those that are kept on.

Kent Cooper, AIA
0 Likes
Message 10 of 14

Anonymous
Not applicable

Unfortunately it doesn't work. Maybe because i use AutoCAD 2013 or i need to use german comands? But that can't be the problem because of underscore ahead of the command it should work isn't it?

 

I tried to rewrite your first listed command:

 

 

(defun LAYER_OFF()
      d
	(if (not (or (tblobjname "layer" "C000_C180-EBENE") (tblobjname "layer" "C090_C270-EBENE") (tblobjname "layer" "C045_C225-EBENE") (tblobjname "layer" "lvk_netz")))
	            
    (vla-put-LayerOn (vlax-ename->vla-object layobj) 0)
    
    (princ)
); defun LAYERS_OFF
(vl-load-com); do i need that?

first i checked if it's not one of the listed layer to turn it off after. But there is still an error...

 

0 Likes
Message 11 of 14

jdiala
Advocate
Advocate

This has been answered on port #5, but here is another one. Is this a sub function or a command? If it's a command, add C: before the LAYER_OFF.

 

(defun LAYER_OFF(/ l)
  (mapcar 
    (function
      (lambda (x)
        (if 
          (setq l (tblobjname "Layer" x))	
          (vla-put-LayerOn (vlax-ename->vla-object l) :vlax-false) 
	)
      )
    )
	'("C000_C180-EBENE" "C090_C270-EBENE" "C045_C225-EBENE"	"lvk_netz");add as many layer as you want
  )
(princ)
); defun LAYERS_OFF
(vl-load-com); do i need that?

 

Message 12 of 14

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

Unfortunately it doesn't work. Maybe because i use AutoCAD 2013 or i need to use german comands? But that can't be the problem because of underscore ahead of the command it should work isn't it?

 

I tried to rewrite your first listed command:

(defun LAYER_OFF()
      d
	(if (not (or (tblobjname "layer" "C000_C180-EBENE") (tblobjname "layer" "C090_C270-EBENE") (tblobjname "layer" "C045_C225-EBENE") (tblobjname "layer" "lvk_netz")))
	            
    (vla-put-LayerOn (vlax-ename->vla-object layobj) 0)
    
    (princ)
); defun LAYERS_OFF
(vl-load-com); do i need that?

first i checked if it's not one of the listed layer to turn it off after. But there is still an error...

 


Something clearly went wrong in copying/pasting, or some such problem, because there's a lot missing there [no setting of the layobj variable, for example], and that odd line with just a 'd' in it.  But once those problems are fixed, if you want to turn them on this time, the value in the (vla-put-LayerOn) function should be [inexplicably] -1, not 0.

 

And that makes me wonder whether something might have gone wrong in your use of the single-Layer-command approach.  It should work, regardless of version or language -- post what you did with it, and maybe something will be obvious.  Then if it really won't work for some reason, your alteration could be simplified, such as with a list or a (wcmatch) function rather than all those separate (tblobjname) functions.

 

Yes, you need (vl-load-com) to get the (vl...) functions to work, but you may not need it built into this command definition, if you have it done by something else that will always happen, such as acaddoc.lsp [or any routine loaded by acaddoc.lsp that includes it].

Kent Cooper, AIA
0 Likes
Message 13 of 14

Anonymous
Not applicable
This works perfectly!!

But it is the opposite result i need. I want to have this layer (like C000_C180-Ebene", ....) to be turned on but to turn off all other layers in the layerlist. So i need the reverse command..
0 Likes
Message 14 of 14

jdiala
Advocate
Advocate
Accepted solution
(defun LAYER_OFF  ()
(vlax-for x 
  (vla-get-layers 
    (vla-get-activedocument 
      (vlax-get-acad-object)
    )
  )
  (if 
    (member (vla-get-name x) '("C000_C180-EBENE" "C090_C270-EBENE" "C045_C225-EBENE" "lvk_netz"))
    (vla-put-layeron x :vlax-true)
    (vla-put-layeron x :vlax-false)
  )
)
(princ)
)