Author Topic: Variants and Safearrays  (Read 2959 times)

0 Members and 1 Guest are viewing this topic.

jbuzbee

  • Swamp Rat
  • Posts: 851
Variants and Safearrays
« on: July 20, 2007, 09:20:27 AM »
Can one of you Vlisp gurus give a quick tutorial / examples on working with variants and safearrays.  This logic totally baffles me . . .  :|

vlax-safearray-
vlax-variant-

Thanks.
James Buzbee
Windows 8

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Variants and Safearrays
« Reply #1 on: July 20, 2007, 11:18:04 AM »
Depending on how you want to use them, you may not need to know how to use them, because you can use a list instead.  Simple example
Code: [Select]
(vla-get-InsertionPoint
 (vlax-ename->vla-object
  (car
   (entsel "\n Select a block sample: ")
  )
 )
)
vs.
Code: [Select]
(vlax-get
 (vlax-ename->vla-object
  (car
   (entsel "\n Select a block sample: ")
  )
 )
 'InsertionPoint
)

If that doesn't help, then I will wait for a master to enlighten us.  :-)
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

jbuzbee

  • Swamp Rat
  • Posts: 851
Re: Variants and Safearrays
« Reply #2 on: July 20, 2007, 12:02:28 PM »
Sorry Tim I guess I wasn't clear enough:  I'm only interested in how to create arrays using Vlisp.  I figured out the immediate question I had, but if anyone has exstensive experience with them it would be nice to see a quick run down . . . Luis?

Here's what I figured out: (I needed to pass an object to a vla-selectionset)

(setq SAwallobj (vlax-make-safearray vlax-vbObject '(0 . 0)))
(vlax-safearray-fill SAwallobj (list wallobj))
James Buzbee
Windows 8

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Variants and Safearrays
« Reply #3 on: July 20, 2007, 12:09:07 PM »
It's cool James.  It is Friday.  Here is code I found to make variants from lists, it might help you.
Code: [Select]
(defun list->variant (lst vartype)
; By Frank Oquendo

(vlax-make-variant
 (vlax-safearray-fill
  (vlax-make-safearray vartype (cons 0 (1- (length lst))))
  lst
  )
 )
)
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

jbuzbee

  • Swamp Rat
  • Posts: 851
Re: Variants and Safearrays
« Reply #4 on: July 20, 2007, 12:14:10 PM »
Nice.  Thanks for sharing.
James Buzbee
Windows 8

JohnK

  • Administrator
  • Seagull
  • Posts: 10634
Re: Variants and Safearrays
« Reply #5 on: July 20, 2007, 12:29:30 PM »
And your gonna need this one too...
Code: [Select]
(defun lisp-value (v)
   ;; Copyright 2002 Vladimir Nesterovsky.
   ;; Free for use by any commercial entity with
   ;; less then $100 million annual revenue.
   (cond
     ((= (type v) 'variant)
      (lisp-value (variant-value v)))
     ((= (type v) 'safearray)
      (mapcar 'lisp-value (safearray-value v)))
     (T v)
     )
   )

A good way to think of a variable is to think of it as a container of some-sort. Think of a variable as a empty glass that you can add stuff to. Now just imagine that that glass can only contain one type of ..."juice", you need a bunch of diff types of glasses to carry all the diff types of juice you have in your refrigerator. (One glass for orange juice and another for milk.) Now that doesn't sound very efficient --i.e. You would need to know what you're thirsty for before you open the refrigerator but that is how a computer works. They do what they are told and that is it. In other words, a computer cant "Judge for its self" what it needs for a specific task, the programmer has to do it for it, or build a condition into the code. But what happens if you don't really know what your thirsty for until you open the frig and look around a bit? You would need a special glass, one that can hold all sorts of diff types of juice. Well there is a glass like that, its called a variant. This glass is a bit over sized and that makes drinking out of it a bit awkward.

An Array is a different type of container; they allow you to clump a bunch of variables together. A safearray can only be filled by variables that are grouped by a common theme. i.e. strings, doubles, integers, etc. That is why Visual lisp calls them "safe"arrays; you can't accidentally assign values outside the array bounds and cause an error. So when you run, say, the "vlax-3d-point" function you are actually creating a safearray of "X" type and assigning it to a variant to send to the ARX.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org