TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Colby on November 08, 2019, 12:01:19 PM

Title: Can a PICKSET be treated as a list for use with mapcar?
Post by: Colby on November 08, 2019, 12:01:19 PM
I wanted to do something like the following:

Code: [Select]
(defun SelectionSet->VLA-Object (ssEnt / obj)
   (if (/= ssEnt nil)
      (setq obj (vlax-ename->vla-object (ssname ssEnt 0)))
      nil
      )
   )

(defun C:SSS ()
   (setq ssents (ssget))
   (setq temp (mapcar 'SelectionSet->vla-object ssents))
   )

If I review ssents in VLIDE, it appears very list-esq, but does not seem to function like one.  Is something like this possible? 

[edit] I know I can loop through it with repeat, but I'd like to minimize the amount of typing I need to do in the future.
Title: Re: Can a PICKSET be treated as a list?
Post by: ronjonp on November 08, 2019, 12:23:08 PM
Read this: http://www.lee-mac.com/selectionsettolist.html

You have to convert the selset to a list .. many examples there.

Welcome to TheSwamp. :)

Here is an example using mapcar albeit slower than many other methods:

Code - Auto/Visual Lisp: [Select]
  1. (defun selectionset->vla-object_>list (s)
  2.   (if s
  3.     (mapcar 'vlax-ename->vla-object (vl-remove-if 'listp (mapcar 'cadr (ssnamex s))))
  4.   )
  5. )
Title: Re: Can a PICKSET be treated as a list?
Post by: Colby on November 08, 2019, 12:33:20 PM
Read this: http://www.lee-mac.com/selectionsettolist.html

You have to convert the selset to a list .. many examples there.

Welcome to TheSwamp. :)

Here is an example using mapcar albeit slower than many other methods:

Code - Auto/Visual Lisp: [Select]
  1. (defun selectionset->vla-object_>list (s)
  2.   (if s
  3.     (mapcar 'vlax-ename->vla-object (vl-remove-if 'listp (mapcar 'cadr (ssnamex s))))
  4.   )
  5. )

Thanks ronjonp! 

It looks like using mapcar on the function selectionset->vla-object and passing each selection set item just isn't possible.

Thank you for your help.
Title: Re: Can a PICKSET be treated as a list for use with mapcar?
Post by: Lee Mac on November 08, 2019, 01:55:11 PM
In short: mapcar operates on lists; a selection set isn't a list.

If you specifically wish to use mapcar, you will need to first construct a list of entities from a given selection set either by iterating over the set or using ssnamex; however, rather than iterating over the selection set to construct a list, and then iterating over the list (using mapcar or other list iterators) to operate on each entity, you might as well operate on the entities whilst iterating over the set - I describe various methods to accomplish this in my tutorial on Selection Set Processing (http://lee-mac.com/selsetprocessing.html).
Title: Re: Can a PICKSET be treated as a list for use with mapcar?
Post by: Marc'Antonio Alessi on November 09, 2019, 03:57:17 AM
Another version:
Code: [Select]
(defun ALE_SelSet_ToObjList (SelSet / Countr ObjLst)
  (if SelSet
    (repeat (setq Countr (sslength SelSet))
      (setq ObjLst (cons (vlax-ename->vla-object (ssname SelSet (setq Countr (1- Countr)))) ObjLst))
    )
  )
)