Author Topic: Lisp Example: Selection set to and from a list  (Read 8001 times)

0 Members and 1 Guest are viewing this topic.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Lisp Example: Selection set to and from a list
« on: March 02, 2008, 10:09:25 AM »
Here is an example of creating a selection set, converting to to a list of entity names and then
converting the list of entity names back to a selection set.
Code: [Select]
(defun c:test (/ ss lst ss1)
  (and [color=green]; this will stop the lisp if any line below returns[/color] [color=red]nil[/color]
    (princ "\nSelect objects for the test.")
    (setq ss (ssget)) [color=green]; get user piciked selection set[/color]
    (princ (strcat "\nThere are " (itoa (sslength ss)) " items in selection set ss."))
    [color=green];;  convert a selection set to a list[/color]
    (setq lst (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss))))
    (princ (strcat "\nThere are " (itoa (length lst)) " items in the list lst."))
    (setq ss1 (ssadd))  [color=green]; create an empty selection set[/color]
    [color=green];;  add the items in a list to a selection set[/color]
    (mapcar '(lambda (x) (ssadd x ss1)) lst)
    (princ (strcat "\nThere are " (itoa (sslength ss1)) " items in selection set ss1."))
  )
  (princ) [color=green]; keeps any unwanted comments from the command line[/color]
)

Referencing this post.
 http://www.theswamp.org/index.php?topic=10370.msg262481#msg262481
« Last Edit: March 02, 2008, 12:04:37 PM by CAB »
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Lisp Example: Selection set to and from a list
« Reply #1 on: March 02, 2008, 03:01:21 PM »
.............   
    ;;  convert a selection set to a list
    (setq lst (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss))))
   

nice, I've never thought to try it like that :-)
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

gile

  • Gator
  • Posts: 2518
  • Marseille, France
Re: Lisp Example: Selection set to and from a list
« Reply #2 on: March 02, 2008, 03:40:02 PM »
Quote
(setq lst (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss))))
Nice and concise but slower than iterative forms with repeat or while.

Code: [Select]
(defun test1 (ss)
  (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss)))
)

(defun test2 (ss / n l)
  (repeat (setq n (sslength ss))
    (setq l (cons (ssname ss (setq n (1- n))) l))
  )
)

(defun test3 (ss / n e l)
  (setq n -1)
  (while (setq e (ssname ss (setq n (1+ n))))
    (setq l (cons e l))
  )
)

Quote
_$ (sslength ss)
275
_$ (benchmark '((test1 ss) (test2 ss) (test3 ss)))
Benchmarking ...............Elapsed milliseconds / relative speed for 4096 iteration(s):

    (TEST3 SS).....1156 / 5.34 <fastest>
    (TEST2 SS).....1687 / 3.66
    (TEST1 SS).....6172 / 1.00 <slowest>
Speaking English as a French Frog

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Lisp Example: Selection set to and from a list
« Reply #3 on: March 02, 2008, 08:02:43 PM »
Thanks gille, and yes I knew the fastest method.
I suspect it's because the method I posted iterates through the selection set once & then through the list to remove nil if it exist. So that two passes through the items selected.
I post that method most of the time because it so compact. I love compact. :)  Thanks kerry. 8-)

PS : http://www.theswamp.org/index.php?topic=14184.msg171131#msg171131
« Last Edit: March 02, 2008, 08:10:19 PM by CAB »
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Lisp Example: Selection set to and from a list
« Reply #4 on: March 02, 2008, 08:06:35 PM »
FYI: The performance penalty is 95% attributable to ssnamex. :)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Lisp Example: Selection set to and from a list
« Reply #5 on: March 02, 2008, 08:34:39 PM »
Thanks Michael, that's good to know. :roll:
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

litss

  • Guest
Re: Lisp Example: Selection set to and from a list
« Reply #6 on: March 04, 2008, 12:23:22 AM »
Thx CAB for the example, it helps:)

To me, the way of "repeat" or "while" is much easier to understand. I always avoid using the "mapcar". It's too complicated. Every time I use it, I need to check it out in the "hlep". 

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Lisp Example: Selection set to and from a list
« Reply #7 on: March 04, 2008, 09:53:46 AM »
OK, here it is without the Mapcar.
Code: [Select]
(defun c:test (/ ss lst ss1 i ent lst)
  (and [color=green]; this will stop the lisp if any line below returns[/color] [color=red]nil[/color]
    (princ "\nSelect objects for the test.")
    (setq ss (ssget)) [color=green]; get user piciked selection set[/color]
    (princ (strcat "\nThere are " (itoa (sslength ss)) " items in selection set ss."))
    [color=green];;  convert a selection set to a list[/color]
    (setq i -1)
    (while (setq ent (ssname ss (setq i (1+ i))))
      (setq lst (cons ent lst))
    )
    (princ (strcat "\nThere are " (itoa (length lst)) " items in the list lst."))
    (setq ss1 (ssadd))  [color=green]; create an empty selection set[/color]
    [color=green];;  add the items in a list to a selection set[/color]
    (setq i -1)
    (while (setq ent (nth (setq i (1+ i)) lst))
      (ssadd ent ss1)
    )
    (princ (strcat "\nThere are " (itoa (sslength ss1)) " items in selection set ss1."))
  )
  (princ)[color=green] ; keeps any unwanted comments from the command line[/color]
)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

litss

  • Guest
Re: Lisp Example: Selection set to and from a list
« Reply #8 on: March 04, 2008, 08:14:58 PM »
Wow, it is clear, I like that^_^

From this example, I can easily come to a conclusion that the "selection set" actrually is the same with the "list of entity names".

Look at this,
 (setq ent (ssname ss (setq i (1+ i))))___the "selection set"
 (setq ent (nth (setq i (1+ i)) lst))____the "list of entity names"

The "ent"s here are the same thing. They both represents the name of a entity.

The only difference is the way they are stored. One is stored in a list----(setq lst (cons ent lst)), the other one is stored in a set----(ssadd ent ss1).

Is that right?




CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Lisp Example: Selection set to and from a list
« Reply #9 on: March 05, 2008, 12:01:24 AM »
You got it :-)

Note that ACAD limits the number of selection sets you can have so be sure and use private variable names or set then to nil when done.
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.