Author Topic: Sorting polyline with multiple criteria  (Read 2163 times)

0 Members and 1 Guest are viewing this topic.

m4rdy

  • Newt
  • Posts: 62
Sorting polyline with multiple criteria
« on: December 28, 2016, 02:37:35 AM »
Hai,

How do we sort all polylines with multiple criteria like below :

(Layer_name Color Length Number_of_Pline_with_Same_Length)

All polylines are :
   - Visible
   - Open
   - Color can be BYLAYER or specific color

Example of Output is List:

(Bar-1 8 2500.00 59)...etc

Bar-1       = Layer_Name
8             = Color
2500.00  = Length of Polyline
59           = Number of Polylines with Same Length 2500.00

Thank you if anybody can help me.

m4rdy
Autocad 2007, Windows XP

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: Sorting polyline with multiple criteria
« Reply #1 on: December 28, 2016, 07:36:14 AM »
This?
Code - Auto/Visual Lisp: [Select]
  1. (defun c:sortpolylines ( / cnt ent enx idx itm len lst sel tmp )
  2.     (if (setq sel (ssget "_X" '((0 . "LWPOLYLINE") (-4 . "<NOT") (-4 . "&=") (70 . 1) (-4 . "NOT>"))))
  3.         (progn
  4.             (repeat (setq idx (sslength sel))
  5.                 (setq ent (ssname sel (setq idx (1- idx)))
  6.                       enx (entget ent)
  7.                       len (vlax-curve-getdistatparam ent (vlax-curve-getendparam ent))
  8.                       tmp (list (cdr (assoc 8 enx)) (cond ((cdr (assoc 62 enx))) (256)) len)
  9.                 )
  10.                 (or (vl-some '(lambda ( x ) (vl-every 'equal2 tmp x)) lst)
  11.                     (setq lst (cons tmp lst))
  12.                 )
  13.                 (if (setq itm (assocf len cnt))
  14.                     (setq cnt (subst (cons len (1+ (cdr itm))) itm cnt))
  15.                     (setq cnt (cons  (cons len 1) cnt))
  16.                 )
  17.             )
  18.             (mapcar '(lambda ( x ) (reverse (cons (cdr (assocf (caddr x) cnt)) (reverse x)))) lst)
  19.         )
  20.     )
  21. )
  22. (defun assocf ( x l )
  23.     (car (vl-member-if '(lambda ( y ) (equal x (car y) 1e-8)) l))
  24. )
  25. (defun equal2 ( a b )
  26.     (if (member (type a) '(int str)) (= a b) (equal a b 1e-8))
  27. )

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Sorting polyline with multiple criteria
« Reply #2 on: December 28, 2016, 08:21:15 AM »
@Lee:
It seems strange to get this result for a drawing with a total of 10 polylines:
Code: [Select]
(("0" 1 1443.53 10) ("0" 3 1443.53 10) ("0" 256 1443.53 10))But then again that is what the OP has requested. :-)

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: Sorting polyline with multiple criteria
« Reply #3 on: December 28, 2016, 08:57:26 AM »
@Lee:
It seems strange to get this result for a drawing with a total of 10 polylines:
Code: [Select]
(("0" 1 1443.53 10) ("0" 3 1443.53 10) ("0" 256 1443.53 10))But then again that is what the OP has requested. :-)

I agree Roy - I think 'number of polylines with the same properties' would be more logical (and would shorten the code) such that the first three items form the key for the association list, but we'll have to see how the OP responds.

Happy Holidays Roy.  :-)

m4rdy

  • Newt
  • Posts: 62
Re: Sorting polyline with multiple criteria
« Reply #4 on: December 28, 2016, 10:02:32 AM »
Hi Lee Mac,

Thank you for your help, but the list result doesn't have "number of same length".
I don't how to modify your code.

m4rdy
Autocad 2007, Windows XP

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: Sorting polyline with multiple criteria
« Reply #5 on: December 28, 2016, 10:43:41 AM »
the list result doesn't have "number of same length".

I believe you are mistaken - a quick test on an arbitrary drawing yields the following:
Code: [Select]
Command: sortpolylines (("0" 256 4.3 6) ("1" 256 4.3 6) ("0" 256 5.1 2))

m4rdy

  • Newt
  • Posts: 62
Re: Sorting polyline with multiple criteria
« Reply #6 on: December 28, 2016, 11:26:19 AM »
Hi Lee Mac,

I dont know if I'm missing something on my test, but the result is like on attachment.

m4rdy


Autocad 2007, Windows XP

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: Sorting polyline with multiple criteria
« Reply #7 on: December 28, 2016, 11:30:05 AM »
In your original post:

( ... Number_of_Pline_with_Same_Length)

Number of Polylines with Same Length

In your attachment, there are 6 polylines with the same length in each group.
« Last Edit: December 28, 2016, 11:35:10 AM by Lee Mac »

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: Sorting polyline with multiple criteria
« Reply #8 on: December 28, 2016, 11:34:35 AM »
Contrary to your description, I understand that this is what you actually wanted:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:sortpolylines ( / ent enx idx itm lst sel tmp )
  2.     (if (setq sel (ssget "_X" '((0 . "LWPOLYLINE") (-4 . "<NOT") (-4 . "&=") (70 . 1) (-4 . "NOT>"))))
  3.         (repeat (setq idx (sslength sel))
  4.             (if (setq ent (ssname sel (setq idx (1- idx)))
  5.                       enx (entget ent)
  6.                       tmp (list (cdr (assoc 8 enx)) (cond ((cdr (assoc 62 enx))) (256)) (vlax-curve-getdistatparam ent (vlax-curve-getendparam ent)))
  7.                       itm (car (vl-member-if '(lambda ( x ) (vl-every 'equal2 tmp x)) lst))
  8.                 )
  9.                 (setq lst (subst (append tmp  (list (1+ (last itm)))) itm lst))
  10.                 (setq lst (cons  (append tmp '(1)) lst))
  11.             )
  12.         )
  13.     )
  14. )
  15. (defun equal2 ( a b )
  16.     (if (member (type a) '(int str)) (= a b) (equal a b 1e-8))
  17. )

m4rdy

  • Newt
  • Posts: 62
Re: Sorting polyline with multiple criteria
« Reply #9 on: December 28, 2016, 11:42:08 AM »
WoooW, So quick replies and coding.  :laugh:

Lee Mac,

Yes, thats what i need.

Thank you very much Sir and Happy Holyday.

m4rdy

Autocad 2007, Windows XP

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: Sorting polyline with multiple criteria
« Reply #10 on: December 28, 2016, 12:00:14 PM »
You're welcome!  :-)