Help with vl-remove

Help with vl-remove

msarqui
Collaborator Collaborator
634 Views
4 Replies
Message 1 of 5

Help with vl-remove

msarqui
Collaborator
Collaborator

Hi guys,

 

I am pretty sure that the code below can be written in a better way to avoid the repetition (in red).

I am asking because if I have a large list to remove, I will have a lot of repetition.
Could you help me please?

 

(vlax-for lay (vla-get-layers (vla-get-activedocument (vlax-get-acad-object)))
(setq laylst (cons (vla-get-name lay) laylst))
)
(foreach layname laylst
(if (not (member layname laylstfinal))
	(setq laylstfinal (cons layname laylstfinal)))
)
(setq remove0 "0")
(setq removeD "Defpoints")
(setq laylstfinal (vl-remove remove0 laylstfinal))
(setq laylstfinal (vl-remove removeD laylstfinal))

Thanks

Marcelo

0 Likes
Accepted solutions (2)
635 Views
4 Replies
Replies (4)
Message 2 of 5

Anonymous
Not applicable
Accepted solution

Marcelo,

Try this approach..

(if (and

     (/= layname "0")(/= layname "Defpoints")

     (not (member layname laylstfinal))

   ); and

   (setq laylstfinal (cons layname laylstfinal))

); if

 

Bob

ECCAD

0 Likes
Message 3 of 5

msarqui
Collaborator
Collaborator
I can not believe I was not seeing this... 🙂
Many thanks ECCAD!
0 Likes
Message 4 of 5

Kent1Cooper
Consultant
Consultant
Accepted solution

@msarqui wrote:

.... 

I am pretty sure that the code below can be written in a better way to avoid the repetition (in red).

....


It can be simpler than that [no (foreach) with embedded (if) / (setq) / (cons) functions needed]:

 

(vlax-for lay (vla-get-layers (vla-get-activedocument (vlax-get-acad-object)))
  (setq laylst (cons (vla-get-name lay) laylst))
)

(setq laylstfinal (vl-remove-if '(lambda (x) (wcmatch x "0,DEFPOINTS")) laylst))

 

EDIT:

And you can do the initial layer-list part with less code, too:

(while (setq lay (cdadr (tblnext "layer" (not lay))))
  (setq laylst (cons lay laylst))
)

Kent Cooper, AIA
0 Likes
Message 5 of 5

msarqui
Collaborator
Collaborator

Many thanks Kent!

It is a very cool solution. I will study more about LAMBDA.

Marcelo

0 Likes