TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Red Nova on December 07, 2016, 11:20:08 PM

Title: Convert list of VLA Objects to a Selection Set
Post by: Red Nova on December 07, 2016, 11:20:08 PM
Hi,

For sure I am missing something...
I convert Selection Set into a list of VLA Objects using following function from Lee Mac.
But how do I convert a list of VLA Objects back to a Selection Set?

(defun LM:ss->vla ( ss / i l )
    (if ss
        (repeat (setq i (sslength ss))
            (setq l (cons (vlax-ename->vla-object (ssname ss (setq i (1- i)))) l))
        )
    )
)
Title: Re: Convert list of VLA Objects to a Selection Set
Post by: MP on December 07, 2016, 11:33:36 PM
Coded blind on an iPad (surprisingly horrible experience).

Code: [Select]
(defun _Objects->SS ( objects )
    (   (lambda ( ss )
            (foreach e (mapcar 'vlax-vla-object->ename objects)
                (ssadd e ss)
            )
            ss
        )
        (ssadd)
    )
)

Should work, apologies if it throws ones and zeros at you.

PS: If you don't like the order in the resulting selection set reverse the list of objects.
Title: Re: Convert list of VLA Objects to a Selection Set
Post by: MP on December 07, 2016, 11:46:16 PM
PS: Likely doesn't need the trailing ss as (ssadd e ss) should return the selection set. Can't test here so went the safe explicit route.
Title: Re: Convert list of VLA Objects to a Selection Set
Post by: roy_043 on December 08, 2016, 05:37:45 AM
Simpler and probably faster:
Code: [Select]
(defun Conv_ObjectList_To_Pickset (objLst / ss)
  (setq ss (ssadd))
  (foreach obj objLst (ssadd (vlax-vla-object->ename obj) ss))
)

Or if you want the function to return nil instead of an empty selection set:
Code: [Select]
(defun Conv_ObjectList_To_Pickset (objLst / ss) ; Returns nil if objLst is empty.
  (if objLst
    (progn
      (setq ss (ssadd))
      (foreach obj objLst (ssadd (vlax-vla-object->ename obj) ss))
    )
  )
)
Title: Re: Convert list of VLA Objects to a Selection Set
Post by: Red Nova on December 08, 2016, 07:42:20 AM
Thank you roy_043 and MP. What I needed :).
Title: Re: Convert list of VLA Objects to a Selection Set
Post by: Grrr1337 on December 08, 2016, 07:53:28 AM
Heres to convert into a selection set object:
Code: [Select]
(defun VlaObjs->SS ( LstObjs / SScoll SS SfArrayObjs i )

(setq SScoll (vla-get-SelectionSets (vla-get-ActiveDocument (vlax-get-acad-object))))
(if (not (vl-catch-all-error-p (vl-catch-all-apply 'vla-Item (list SScoll "NewSS"))))
(vla-Delete (vla-Item SScoll "NewSS"))
)
(setq SS (vla-Add SSColl "NewSS"))
(setq SfArrayObjs (vlax-make-safearray vlax-vbObject (cons 0 (1- (length LstObjs)))))
(setq i -1)
(foreach o LstObjs
(vlax-safearray-put-element SfArrayObjs (setq i (1+ i)) o)
)
(vla-AddItems SS SfArrayObjs)
SS
)
Seems useless, but who knows.
Title: Re: Convert list of VLA Objects to a Selection Set
Post by: Lee Mac on December 11, 2016, 05:24:25 PM
Another, just to offer an alternative to those posted:
Code - Auto/Visual Lisp: [Select]
  1. (defun vla->ss ( lst )
  2.     (apply 'command (append '("_.select") (mapcar 'vlax-vla-object->ename lst) '("")))
  3.     (ssget "_P")
  4. )
Title: Re: Convert list of VLA Objects to a Selection Set
Post by: Grrr1337 on December 12, 2016, 05:03:59 AM
Another, just to offer an alternative to those posted:
Code - Auto/Visual Lisp: [Select]
  1. (defun vla->ss ( lst )
  2.     (apply 'command (append '("_.select") (mapcar 'vlax-vla-object->ename lst) '("")))
  3.     (ssget "_P")
  4. )
Creative!  :-)