Filter list/selection by highest property

Filter list/selection by highest property

jotaferrer
Advocate Advocate
649 Views
17 Replies
Message 1 of 18

Filter list/selection by highest property

jotaferrer
Advocate
Advocate

Hey guys, it's me again!

 

I've been trying to reach a way to do this, but I just can't figure it out.

As you can see, there's a step in my developing (with @pbejse  help!) list that I need to filter my selection.

I need want to automatically select the highest perimeter in a list and all other regions that I may have into another list. So that I can subtract to create a region.

 

Is that possible to be done? Thanks in advance!

 

(defun c:secm ( / )

 (setq ss (ssget "_A"))

 (command ".region" ss "") ;create region
 
 (setq $ss (ssget "_A"))
 
; (if $ss
;        (repeat (setq i (sslength $ss))
;            (setq lst (cons (vlax-ename->vla-object (ssname $ss (setq i (1- ;i)))) lst))
;        )
;    ) ; turns ssget a list and vla-object too
    


 (repeat (setq i (sslength $ss))
            (setq lst (cons
			(vlax-get 
				(vlax-ename->vla-object (ssname $ss (setq i (1- i)))) 'perimeter)
				lst))
        )

 

0 Likes
Accepted solutions (1)
650 Views
17 Replies
Replies (17)
Message 2 of 18

pbejse
Mentor
Mentor

@jotaferrer wrote:

I need want to automatically select the highest perimeter in a list and all other regions that I may have into another list.


For starters, Look into max or vl-sort 

 

Message 3 of 18

jotaferrer
Advocate
Advocate

So I tried to do an (apply 'max lst) and it does gives me the highest perimeter, but I still trying to "relink" it to its region. 

 

But maybe I can sort the looped list by the highest perimeter... I'll try it!

Message 4 of 18

john.uhden
Mentor
Mentor

@jotaferrer 

Hint:

When you make your list, include an object identifier, such as its handle or vla-object name.

So your list would look like '((handle_1 . perim_1)(handle_2 . perim_2)...(handle_n . perim_n))

Then you would sort it via (setq sorted (vl-sort lst '(lambda (a b)(> (cdr a)(cdr b)))))

and the first one would be the one with the largest perimeter.

John F. Uhden

Message 5 of 18

ronjonp
Advisor
Advisor

HERE is some example code sorting objects ( in this case circles ) then converting to regions and subtracting from the largest.

Message 6 of 18

john.uhden
Mentor
Mentor

@ronjonp 

I almost never use regions.  What happens if you subtract more than the largest?  A negative region?  A black hole?

At least we can show him how to sort the sizes in ranges.  😁

John F. Uhden

0 Likes
Message 7 of 18

ronjonp
Advisor
Advisor

@john.uhden  I'd guess black hole depending on your screen color 🙂

0 Likes
Message 8 of 18

john.uhden
Mentor
Mentor

@ronjonp 

Guess?!  Haven't you tried it?  I guess not.  It's too big a risk what with "the greys" awaiting at the other end.

But maybe you would meet Lilu.  <Wow, was she hot!>  "Multipass, multipass!"

John F. Uhden

Message 9 of 18

jotaferrer
Advocate
Advocate
So @juhn.uhden that I'm intending to do, but I'm still figuring out how it's possible. But I won't quit my researches, I'll find a way (I think). But if not, I'll ask you guys a little help hehe.

And yeah, you're almost right, subtract by the smallest and you create a worm hole right head to Gargantua! Thanks, now I'm 89 y.o. ‌‌‌‌‌‌‌‌‌‌
0 Likes
Message 10 of 18

jotaferrer
Advocate
Advocate
I'll take a look into it, thans @ronjonp
0 Likes
Message 11 of 18

ronjonp
Advisor
Advisor

@jotaferrer wrote:
I'll take a look into it, thans @ronjonp

Glad to help 🙂

Message 12 of 18

ronjonp
Advisor
Advisor

@john.uhden wrote:

@ronjonp 

Guess?!  Haven't you tried it?  I guess not.  It's too big a risk what with "the greys" awaiting at the other end.

But maybe you would meet Lilu.  <Wow, was she hot!>  "Multipass, multipass!"


@john.uhden  I did try it and the code did not fail. I ended up with nothing or negative matter which I could not see :).

Message 13 of 18

john.uhden
Mentor
Mentor

@ronjonp wrote, " negative matter which I could not see"

Just think.  You could tell Kent's cousin Sheldon Cooper that you can create negative matter with AutoLisp.

Of course the downside of that is that he might befriend you.  😕

John F. Uhden

Message 14 of 18

jotaferrer
Advocate
Advocate
@john.uhden Can you please tell me what am I doing wrong!? I'm pretty new with lisp and both mapcar and lambda functions are obscure to me...

(defun c:jsm (/ ss p1 MoIx l $ss i lst 1st 2nd perim)

(vl-load-com)

(setvar "cmdecho" 0)

(setq ss (ssget "_A"))

(command ".region" ss "") ;create region

(setq $ss (ssget "_A"))

(if $ss
(repeat (setq i (sslength $ss))
(setq lst (cons (vlax-ename->vla-object (ssname $ss (setq i (1- i)))) lst))
)
) ; turns ssget a list and vla-object too


(setq 1st (list (cons 5 (nth 0 lst)) (cons 140 (vla-get-Perimeter (nth 0 lst)))))
(setq 2nd (list (cons 5 (nth 1lstT)) (cons 140 (vla-get-Perimeter (nth 1 lst)))))
(setq perim (append 1st 2nd))

(setq sorted (vl-sort perim '(lambda (1st 2dn)(> (cdr 1st)(cdr 2nd)))))
0 Likes
Message 15 of 18

Kent1Cooper
Consultant
Consultant

@jotaferrer wrote:
....
(setq 1st (list (cons 5 (nth 0 lst)) (cons 140 (vla-get-Perimeter (nth 0 lst)))))
(setq 2nd (list (cons 5 (nth 1lstT)) (cons 140 (vla-get-Perimeter (nth 1 lst)))))
....
(setq sorted (vl-sort perim '(lambda (1st 2dn)(> (cdr 1st)(cdr 2nd)))))

At the least, the red parts will cause problems....

 

But I also wonder what you're really going for.  For example, the (cons 5) part would typically be about an entity handle, not a VLA object that is what you're pairing it with.

Kent Cooper, AIA
0 Likes
Message 16 of 18

john.uhden
Mentor
Mentor
Accepted solution

@jotaferrer 

Well, you've got a couple of typos but they don't matter because we are changing your code below, but I'm concerned about your ssget with no filter.

Do you know for sure that all your objects have a perimeter property?

 

(defun c:jsm (/ ss p1 MoIx l $ss i lst 1st 2nd perim)
(vl-load-com)
(setvar "cmdecho" 0)
(setq ss (ssget "_A"))
(command ".region" ss "") ;create region
(setq $ss (ssget "_A"))
(if $ss
(repeat (setq i (sslength $ss))
(setq lst (cons (vlax-ename->vla-object (ssname $ss (setq i (1- i)))) lst))
)
) ; turns ssget a list and vla-object too

;; There's no sense in adding codes to your list because it messes up your sorting

;; In fact there's no need to sort if all you have are two (2) objects, but that's okay.

;; BUT, you don't sort two separate lists.  You sort one list, which you have (all objects).

;; So...

(setq sorted (vl-sort lst '(lambda (a b)(> (vla-get-perimeter a)(vla-get-perimeter b)))))

;;  What that returns is still the same list of objects but sorted in descending order of perimeter.
;; To get the actual largest perimeter, use...

(setq maxperim (vla-get-perimeter (car sorted)))

(setq 1st (list (cons 5 (nth 0 lst)) (cons 140 (vla-get-Perimeter (nth 0 lst)))))
(setq 2nd (list (cons 5 (nth 1 lst)) (cons 140 (vla-get-Perimeter (nth 1 lst)))))
(setq perim (append 1st 2nd))

(setq sorted (vl-sort perim '(lambda (1st 2nd)(> (cdr 1st)(cdr 2nd)))))

John F. Uhden

0 Likes
Message 17 of 18

jotaferrer
Advocate
Advocate
hey @Kent1Cooper thanks for your reply

I'll try to figure out why the red parts will cause problems, thanks!

So, I'm developing a code that will give me the section modulus for any shape. My lisp does work but only if there is no hole whitin the region...
I know that's so much easier for the user to just subtract the regions and make the final region (with hole or holes) by themselves, but while I'm write this lisp with all you guys help, I'm learning and improving my knowledges.

"CONS 5" was my first thought of what @john.uhden told me to do. No need to say I'm pretty new on lisps as well haha
0 Likes
Message 18 of 18

jotaferrer
Advocate
Advocate
There's no filter in my ssget because we always open a new dwg and draw what we want in order to get the section modulus. So that's why...

I was thinking about sorting because there's a possible to have more than two regions, it's a small possibility, but we can skip it if is too complicated. At least for me this lisp is driving me crazy haha

Thanks for all your tips!
0 Likes