LISP On/Off for set of layers

LISP On/Off for set of layers

sam
Advocate Advocate
5,455 Views
18 Replies
Message 1 of 19

LISP On/Off for set of layers

sam
Advocate
Advocate

Hi,

 

Can someone please help me with this issue.
Would it be possible to do it with code to keep only certain set of layers on and all others off.

For example: If i have layers naming (1,2,3,4...25) and if I call ALayers only layers 1-5 are on and all the rest are off from 6-25. So on if I call Blayers only Layers from 6-10 are on and 1-5, 11-25 are off.

 

Thanks

Regards,

Sam

0 Likes
Accepted solutions (4)
5,456 Views
18 Replies
Replies (18)
Message 2 of 19

dbhunia
Advisor
Advisor

If your layer names are only Number (integer) then try this.......

 

(defun C:Keep_Sel_Lay_On ( / Lay_Name pos 1st_Num 2nd_Num Layers acdoc)

(if (and (setq Lay_Name (getstring "\nInput Layer Names to Keep TurnOn (ex X-Y): "))
		 (/= nil (setq  pos (vl-string-search "-" Lay_Name)))
	)
	(progn
		(setq 1st_Num (substr Lay_Name 1 pos))
		(setq 2nd_Num (atoi (vl-string-subst "" (strcat 1st_Num "-") Lay_Name)))
		(setq 1st_Num (atoi 1st_Num))
		(setq Layers "")
		(while (<= 1st_Num 2nd_Num)
			(setq Layers (strcat Layers (rtos 1st_Num 2 0) ","))
			(setq 1st_Num (1+ 1st_Num))
		)
		(setq Layers (vl-string-right-trim "," Layers))
		(setq acdoc (vla-get-activedocument (vlax-get-acad-object)))
		(vlax-for obj (vla-get-layers acdoc)
			(if (not(wcmatch (vla-get-name obj) Layers))
				(vla-put-LayerOn obj :vlax-false)
				(vla-put-LayerOn obj :vlax-true)
			)
		)
	)
	(princ (strcat "\nYour input Format is wrtng: " Lay_Name))
)
(princ)
)

 


Debashis Bhunia
Co-Founder of Geometrifying Trigonometry(C)
________________________________________________
Walking is the First step of Running, Technique comes Next....
Message 3 of 19

sam
Advocate
Advocate

Thanks @dbhunia for quick reply. 

I used those numbers to explain my situation but my layers are not integers actually. Sorry for not making that clear. 

 

Best regards, 

Sam

0 Likes
Message 4 of 19

sam
Advocate
Advocate

@dbhunia I am totally new to LISP so I would really appreciate if you could add some comments to routines explain what does what and which areas I have to tweak to make it work for me. Sorry for being pain - Thanks for your help. 

0 Likes
Message 5 of 19

maratovich
Advisor
Advisor

Give a real file with example layers.

---------------------------------------------------------------------
Software development
Automatic creation layouts and viewport. Batch printing drawings from model.
www.kdmsoft.net
Message 6 of 19

cadffm
Consultant
Consultant
Message 7 of 19

dbhunia
Advisor
Advisor
Accepted solution

Check this.......

 

(defun C:Keep_Sel_Lay_On ( / Lay_Name acdoc)

(if (setq Lay_Name (getstring t "\nInput Layer Names to Keep TurnOn (Seperated by Comma): "))
	;;;Taking Input of Layers like "AAA,BBB,CCC......." ... Put Exact layers Name seperated by comma
	;;;Do not put space after comma No need to maintain any sequence
	(progn
		(setq acdoc (vla-get-activedocument (vlax-get-acad-object)));;;Gatting the Active Opened Document
		(vlax-for obj (vla-get-layers acdoc);;;Looping through each Layer Objects
			(if (wcmatch (vla-get-name obj) Lay_Name);;;Compairing each Layer Name with your given Input stored in "Lay_Name"
;;;Getting Each Layer object Name (vla-get-name obj) (vla-put-LayerOn obj :vlax-true);;;If Layer Names match Make it on (vla-put-LayerOn obj :vlax-false);;;If Layer Names does not match Make it off ) ) ) ) (princ) )

 


Debashis Bhunia
Co-Founder of Geometrifying Trigonometry(C)
________________________________________________
Walking is the First step of Running, Technique comes Next....
Message 8 of 19

_gile
Consultant
Consultant
Accepted solution

Hi,

 

Here's a way to define multiple functions (LISP commands) in a single expression.

You can change the contents of the two lists at the end of the code to suit your needs.

 

(mapcar
  '(lambda (cmd lst)
     (eval (list
	     'defun
	     cmd
	     '(/ l n c)
	     (list 'while
		   '(setq l (tblnext "layer" (not l)))
		   '(setq
		     n
		     (cdr (assoc 2 l))
		     c
		     (assoc 62 l)
		    )
		   (list 'entmod
			 (list 'subst
			       (list 'cons
				     62
				     (list
				       (list 'if
					     (list 'member 'n (list 'quote lst))
					     +
					     -
				       )
				       '(abs (cdr c))
				     )
			       )
			       'c
			       '(entget (tblobjname "layer" n))
			 )
		   )
	     )
	     '(princ)
	   )
     )
   )
  '(c:ALAYERS c:BLAYERS)		; command names
  '(("1" "2" "3" "4" "5") ("6" "7" "8" "9" "10")) ; lists of layers
)


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 9 of 19

Kent1Cooper
Consultant
Consultant
Accepted solution

@sam wrote:
....

Would it be possible to do it with code to keep only certain set of layers on and all others off.

For example: If i have layers naming (1,2,3,4...25) and if I call ALayers only layers 1-5 are on and all the rest are off from 6-25. So on if I call Blayers only Layers from 6-10 are on and 1-5, 11-25 are off.

....

 

You can apply most Layer-command options [including On and Off] to multiple Layers at once, using comma separators between Layer names.  So if you save the sets of Layer names as comma-delimited strings, rather than lists, it's pretty simple:

(setq
  Alist "ThisLayer,ThatLayer,OtherLayer,SomeLayer"
  Blist "WhoLayer,WhatLayer,WhenLayer,WhereLayer"
  Clist "0Layer,1Layer,2Layer,3Layer"
); setq
(defun LayerStuff (Llist / expt); sub-routine called by commands
  (setq expt (getvar 'expert)
  (setvar 'expert 1); suppress "turn-off-current-Layer?" question
  (command "_.layer" "_off" "*" "_on" Llist ""); all off, then some on
  (setvar 'expert expt)
); defun
(defun C:Alayers () (LayerStuff Alist))
(defun C:Blayers () (LayerStuff Blist))
(defun C:Clayers () (LayerStuff Clist))

Consider whether you want one of those that is going to be On in the end to become the current Layer, or whether something like Layer 0 could be made current in the process.

 

Kent Cooper, AIA
Message 10 of 19

sam
Advocate
Advocate

hi @_gile Thank you very much worked like a charm.

0 Likes
Message 11 of 19

sam
Advocate
Advocate

@cadffm Thanks for the really helpful learning website. really helpful. 

0 Likes
Message 12 of 19

sam
Advocate
Advocate

@Kent1Cooper Thanks, most simplistic and most cleanest solution. Additional thanks for all the comments and explanation to make it easier for me. 

0 Likes
Message 13 of 19

Kent1Cooper
Consultant
Consultant

@sam wrote:
.... to keep only certain set of layers on and all others off....

 

Of course, there are also Layer STATES [see Help about them]that can do this for you, without any code or defining of command names.  But I guess it's a drawback that they need to be established in every drawing where you want to use them, whereas the command approach can be applied anywhere if you have the commands automatically loaded by such as acaddoc.lsp.

Kent Cooper, AIA
Message 14 of 19

sam
Advocate
Advocate

@Kent1Cooper Checked the option and it really does the thing that I was looking for but I think lisp is find as I have made a panel after using code for my purpose and it looks really nice, any of the layer group can be initiated with from command prompt etc. Thanks again. 

0 Likes
Message 15 of 19

jaimuthu
Advocate
Advocate

error: malformed list on input ---->  its may be expt it is command in lisp

0 Likes
Message 16 of 19

Kent1Cooper
Consultant
Consultant

@jaimuthu wrote:

error: malformed list on input ---->  its may be expt it is command in lisp


You're right [a function, not a command, but still...] -- that should have used a different variable name.  But I don't think that can be the cause of your error message, so I think there's something else going on.  Surely you're not using the Layer names in my code, so did something go wrong in changing to your real names?

Kent Cooper, AIA
Message 17 of 19

WeTanks
Mentor
Mentor

I want to use it,but it didn't work out.

Is there something wrong?

 

(setq
  Alist '("01" "02" "03")
  Blist '("04" "05" "06")
) ; setq

(defun LayerStuff (Llist / expt)
  (setq expt (getvar 'expert))
  (setvar 'expert 1)
  (command "_.layer" "_off" "*" "")
  (foreach layer Llist
    (command "_.layer" "_on" layer))
  (setvar 'expert expt)
); defun

(defun c:Alayers () (LayerStuff Alist))
(defun c:Blayers () (LayerStuff Blist))

 

We.Tanks

EESignature

A couple of Fusion improvement ideas that could your vote/support:
図面一括印刷

0 Likes
Message 18 of 19

Kent1Cooper
Consultant
Consultant
Accepted solution

You need to finish the Layer commands:

(command "_.layer" "_on" layer ""))

If that doen't make it work, explain in more detail what you mean by "it didn't work out" [which is never enough information].

Kent Cooper, AIA
Message 19 of 19

WeTanks
Mentor
Mentor

Thank you so so much.Its OK。

We.Tanks

EESignature

A couple of Fusion improvement ideas that could your vote/support:
図面一括印刷

0 Likes