• This forum is the machine-generated translation of www.cad3d.it/forum1 - the Italian design community. Several terms are not translated correctly.

eliminate layers not used with autolisp

Giuseppe Beatrice

Guest
Hi, guys.
can you use autolisp to delete unused layers?
I tried by extracting the layer entity name with the tblobjname function and then erasing it with entdel but it does not work.
Can anyone give me information about this?
thanks for the availability.
 
test this, the layers should not be blocked. :smile:

Code:
;elimina layer vuoti

(defun c:elv (/ cmd lay lista_lay lista_lay1)
    (command "_undo" "_begin")
    (setq cmd (getvar "cmdecho"))
    (setvar "cmdecho" 0)
    (setq lay (cdr (assoc 2 (tblnext "layer" t))))
    (setq lista_lay (list lay))     
    (while (/= lay nil)
	(setq lay (cdr (assoc 2 (tblnext "layer"))))
	(setq lista_lay1 (list lay))    
	(if (/= lay nil) (setq lista_lay (append lista_lay lista_lay1)))
    )
    (setq n -1)
    (repeat (length lista_lay)
	(setq lay (nth  (setq n (1+ n))lista_lay))
	(if (and
		(/= lay "0")
		(/= lay (getvar "clayer"))
	        (not (ssget "_x"  (list (cons 8 lay))))
	    )
	    (command "-laydel" "_n" lay "" "_y")
	)
    )
    (command "_undo" "_end")
    (setvar "cmdecho" cmd)
    (princ)
)
 
Thank you, I'll try it.
However it seems to me something really strange, since the online help of autocad says that the entdel function works both for graphics and not graphics.
Anyway, thank you again.
 
use the purge all command eliminates all unused layers! you can create a macro and not a lisp..
 
to stay in the field of the lisp perhaps all can be summarized in:
Code:
(refun c:elv2 ()
(vlax-for elemento (setq layers(vla-get-layers(vla-get-active document (vlax-get-acad object))))
(vl-catch-all-apply 'vla-delete (list elemento))
)
)
for the rest in fact would be enough a purge to...
 
to stay in the field of the lisp perhaps all can be summarized in:
Code:
(refun c:elv2 ()
(vlax-for elemento (setq layers(vla-get-layers(vla-get-active document (vlax-get-acad object))))
(vl-catch-all-apply 'vla-delete (list elemento))
)
)
very tight... :biggrin:


:finger:
 
but to use (command "_purge" "_la" "_n") is not better?
in any case it is good to remember that it is necessary to set plan 0 (zero) before starting removal.

bye
 
a heartfelt thanks to confutatis, to which I say only that I fully share the comment of gp, adding a ...bravo!
I say to others that the purge command is fine, but my request starts from the need to define such elimination within a much larger and articulated automation program, and this unfortunately can only be done with autolisp.
Anyway, thank you all.
 
I say to others that the purge command is fine, but my request starts from the need to define such elimination within a much larger and articulated automation program, and this unfortunately can only be done with autolisp.
Sorry, but who forbids you to use:

(defun miopurgelayer()
(command "_purge" "_la" "_n")
)
 
to stay in the field of the lisp perhaps all can be summarized in:
Code:
(refun c:elv2 ()
(vlax-for elemento (setq layers(vla-get-layers(vla-get-active document (vlax-get-acad object))))
(vl-catch-all-apply 'vla-delete (list elemento))
)
)
for the rest in fact would be enough a purge to...
only to demonstrate how many ways you can solve a problem in vlisp.

(defun c:elv3 ()
(vl-load-com)
(setq layers (vla-get-layers))
(vlax-map-collection layers '(lambda (x) (vl-catch-all-apply 'vla-delete (list x)) ))
)

in reality, writing only
(vlax-map-collection layers 'vla-delete)), as it should be for any method to apply, the code gives error because you can not erase an element of a collection, while you slide it.
 
Last edited by a moderator:
1) I always forget the (vl-load-com)...(but why does the autodesk not put it by default preloaded? )
2) Actually the assignment of the variable layers does not serve practically nothing, you can safely delete
Code:
(refun c:elv2 ()
(vl-load-com)
(vlax-for elemento (fla-get-layers(vla-get-active document (vlax-get-acad object)))
(vl-catch-all-apply 'vla-delete (list elemento))
)
)
 
what I need is a type of elimination "mirata" so far to layers that have in the name a particular substringa, and I can do so using the following function, to which I pass the names to be eliminated and that partially uses the command suggested by g.p.:
(defun lsp_purge_lyr (lst_lyr_flg /)
(mapcar '(lambda (x / item lay)
(tblnext "layer" t)
(while (setq item (tblnext "layer")
(if (wcmatch (setq lay) (cdr (assoc 2 item)) x)
(command "_-laydel" "_n" lay "_y")))
lst_lyr_flg))
But I'd like to know if there's a more "synthetic" way and that's why I think that confutatis, if you have some time, could definitely find the solution.
 
example:
layers: fixtures, interiors, prs, prs+dashed
(command "_purge" "_la" "*+*" "_n")
delete all plans containing +.

as used once.
 
esempio:
remove dalla lista dei layers sottostante tutti quelli che contengono la sottostring "-b"

("sec-c" "sec-b" "sec-a" "pri-c" "pri-b" "pri-a" "ter" "dry" "pri" "bc" "0")

(defun c:del-lay (/ layers obj)
(vl-load-com)
(setq layers (vla-get-layers (vla-get-activedocument)))
(vlax-for obj layers
(if (/= (vl-string-search "-b" (vla-get-name obj)) nil)
(vla-delete obj)
)
)
)

da provare
 
1)...
2) Actually the assignment of the variable layers does not serve practically nothing, you can safely delete. ...
generally the assignment to an external variable to a cycle is made for code clarity (especially, and this could be a case, if there are nested assignments) but also when it is necessary to refer to it in other parts of the program.
It is very similar to the case of the lambda function with which, if I need a procedure that I will use only at that time and in that position, I can write a local function, without having to write and declare a special external function (but, also in this case, it goes to the detriment of the legibility of the code, as always happens when nesting the brackets one inside the other).
in fact has a truth background the note joke:
l.i.s.p.= listprocessing = lost in stupid parenthesis opp. lost in a sea of parenthesis
 
in the previous code you can replace (/= (vl-string-search "ec" (vla-get-name obj)) nil)
con(wcmatch (vla-get-name hug) "*ec*")
 
Thank you, the program perfectly carries out its task!
and thanks also to all for the debate that was triggered and for the contributions that then help everyone grow, understanding and using always better a powerful design tool like autocad.
If I can add a humble comment to the discussion, I will say that I too hover continuously, in the preparation of lists, between the addition of more or less "useless" but explanatory variables and the concision, with the inevitable growth of brackets and the consequent "stupid lostitude... ".
Thanks again to everyone.
 
generally the assignment to an external variable to a cycle is made for code clarity
right observation your, but given the minimum length of the program the variable in question was more. Of course there are more variables, more memory is used and, as far as possible, I try to limit its use. It is true that it goes to the detriment of the legibility of the code, but if the programs are very short, as in this case, I close them to the minimum indispensable.
 

Forum statistics

Threads
44,997
Messages
339,767
Members
4
Latest member
ibt

Members online

No members online now.
Back
Top