Author Topic: Finding same XY coordinates is a list  (Read 988 times)

0 Members and 1 Guest are viewing this topic.

Woabow

  • Newt
  • Posts: 56
Finding same XY coordinates is a list
« on: January 14, 2013, 10:37:28 AM »
After comparing all blocks with a file with blocknames I end up with a list of blocks. Actually it is a list of lists like this:

Code: [Select]
(("sp25" (11095.0 51.4526 605.0) (605.0 . 1210.0)) ("vs" (11095.0 51.4526 0.0) (-8.52651e-014 . 235.0)) ("vs" (11580.2 35.8026 0.0) (-8.52651e-014 . 235.0)) ("sp10" (11095.0 51.4526 0.0) (0.0 . 605.0)))
So the list consists of lists with: name, XYZ coordinate, MinZ Maxz.

Now I would like to collect all blocks with the same YX position in a sublist. So an extra level in the source list with alls blocks with same XY in a sublist. I tried it in several ways but I end up with long code and wrong results. Can somebody please help me with this?

Lee Mac

  • Seagull
  • Posts: 12290
  • London, England
Re: Finding same XY coordinates is a list
« Reply #1 on: January 14, 2013, 11:02:46 AM »
Here is a generic grouping function that I use regularly:

Code - Auto/Visual Lisp: [Select]
  1. ;; Group By Function  -  Lee Mac
  2. ;; Groups items considered equal by a given predicate function
  3.  
  4. (defun LM:GroupByFunction ( lst fun / tmp1 tmp2 x1 )
  5.     (if (setq x1 (car lst))
  6.         (progn
  7.             (foreach x2 (cdr lst)
  8.                 (if (fun x1 x2)
  9.                     (setq tmp1 (cons x2 tmp1))
  10.                     (setq tmp2 (cons x2 tmp2))
  11.                 )
  12.             )
  13.             (cons (cons x1 (reverse tmp1)) (LM:GroupByFunction (reverse tmp2) fun))
  14.         )
  15.     )
  16. )

For your purpose:

Code - Auto/Visual Lisp: [Select]
  1. (setq lst
  2.    '(
  3.         ("sp25" (11095.0 51.4526 605.0) (605.0 . 1210.0))
  4.         ("vs"   (11095.0 51.4526 0.0) (-8.52651e-014 . 235.0))
  5.         ("vs"   (11580.2 35.8026 0.0) (-8.52651e-014 . 235.0))
  6.         ("sp10" (11095.0 51.4526 0.0) (0.0 . 605.0))
  7.     )
  8. )

Code - Auto/Visual Lisp: [Select]
  1. (LM:GroupByFunction lst
  2.     (lambda ( a b )
  3.         (and
  4.             (equal (caadr  a) (caadr  b) 1e-8)
  5.             (equal (cadadr a) (cadadr b) 1e-8)
  6.         )
  7.     )
  8. )

Returns:
Code - Auto/Visual Lisp: [Select]
  1. (
  2.   (
  3.     (
  4.       "sp25"
  5.       (11095.0 51.4526 605.0)
  6.       (605.0 . 1210.0)
  7.     )
  8.     (
  9.       "vs"
  10.       (11095.0 51.4526 0.0)
  11.       (-8.52651e-014 . 235.0)
  12.     )
  13.     (
  14.       "sp10"
  15.       (11095.0 51.4526 0.0)
  16.       (0.0 . 605.0)
  17.     )
  18.   )
  19.   (
  20.     (
  21.       "vs"
  22.       (11580.2 35.8026 0.0)
  23.       (-8.52651e-014 . 235.0)
  24.     )
  25.   )
  26. )



Woabow

  • Newt
  • Posts: 56
Re: Finding same XY coordinates is a list
« Reply #2 on: January 14, 2013, 12:34:05 PM »
Nice function. Does exactly what I had in mind.

Thank you very much.

Lee Mac

  • Seagull
  • Posts: 12290
  • London, England
Re: Finding same XY coordinates is a list
« Reply #3 on: January 14, 2013, 12:42:52 PM »
You're very welcome  :-)