TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: abe123456789 on October 07, 2022, 05:33:01 PM

Title: Variant To Object
Post by: abe123456789 on October 07, 2022, 05:33:01 PM
Good afternoon,

When I use Vla-offset on a line I get a variant. How can I turn the new offset line into a object instead of a variant?

Thank you in advance.

Abe
Title: Re: Variant To Object
Post by: hmspe on October 07, 2022, 06:02:54 PM
I use this approach:

          (vla-offset pline_object offset)
          (vla-delete pline_object)
          (setq pline (entlast))
          (setq pline_object (vlax-ename->vla-object pline))
Title: Re: Variant To Object
Post by: mhupp on October 07, 2022, 06:11:43 PM
Guess your using something like this?

Code - Auto/Visual Lisp: [Select]
  1. (setq newline (vla-offset vla-objectname 10)) ;newline = variant :(

What I do is this
Code - Auto/Visual Lisp: [Select]
  1. (vla-offset vla-objectname 10)  ;strip the setq
  2. (setq newline (entlast))  ;use entlast to set entname
  3. (setq newline (vlax-ename->vla-object (entlast))  ;or vla-object name

or the long way around
Code - Auto/Visual Lisp: [Select]
  1. (setq newline (vlax-safearray->list (vlax-variant-value (vla-offset vla-objectname 10))))


 
Title: Re: Variant To Object
Post by: gile on October 08, 2022, 02:29:53 AM
Hi,
Because offseting some curves may generate more than one curve, the Offset (https://help.autodesk.com/view/OARX/2023/ENU/?guid=GUID-50EF273A-7552-4D6B-8523-BB956334D08A) method returns a variant which is an array of objects.
To get a more LISP friendly return value, we can convert this variant into a list:
Code - Auto/Visual Lisp: [Select]
  1. (vlax-safearray->list (vlax-variant-value (vla-offset line offsetValue)))
If you are absolutely sure that offseting the object will generate a single object (i.e. a line, an arc, a circle, ...), simply get the 'car' of this list.
Title: Re: Variant To Object
Post by: gile on October 08, 2022, 10:01:58 AM
A more generic one (from AutomationHelpers at the bottom of this page).
Code - Auto/Visual Lisp: [Select]
  1. ;; gc:VariantToLispData
  2. ;; Converts a variant or a safearry into an AutoLISP data
  3. ;;
  4. ;; Argument
  5. ;; var : variant or safearray
  6.  
  7. (defun gc:VariantToLispData (var)
  8.   (cond
  9.     ((= (type var) 'variant)
  10.      (gc:VariantToLispData (vlax-variant-value var)))
  11.     ((= (type var) 'safearray)
  12.      (if (< -1 (vlax-safearray-get-u-bound var 1))
  13.        (mapcar 'gc:VariantToLispData (vlax-safearray->list var))
  14.      )
  15.     )
  16.     (T var)
  17.   )
  18. )
Title: Re: Variant To Object
Post by: ronjonp on October 10, 2022, 12:10:34 PM
Good afternoon,

When I use Vla-offset on a line I get a variant. How can I turn the new offset line into a object instead of a variant?

Thank you in advance.

Abe
You can also use VLAX-INVOKE to return a list of objects:
Code - Auto/Visual Lisp: [Select]
  1. (if (setq e (car (entsel)))
  2.   (vlax-invoke (vlax-ename->vla-object e) 'offset 1)
  3. )
Title: Re: Variant To Object
Post by: abe123456789 on October 10, 2022, 04:37:42 PM
That answers my question.

Thanks for the help :-D