TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Lee Mac on June 22, 2009, 07:28:02 AM

Title: Dimensions of Safearrays
Post by: Lee Mac on June 22, 2009, 07:28:02 AM
This is something that I have never really completely understood, even after reading, and re-reading the ACAD help file on it.

I have, in the past, used such functions as:

(vlax-safearray-get-u-bound <var> <dim>)

But I haven't quite understood exactly what is a dimension in a safearray, and what exactly the return of the above function means.

Any help is much appreciated.

Lee
Title: Re: Dimensions of Safearrays
Post by: CAB on June 22, 2009, 10:26:54 AM
Maybe this will illustrate the function:

Code: [Select]
_$ (setq matrix (vlax-make-safearray vlax-vbString '[color=red](1 . 2)[/color] '[color=green](1 . 2)[/color] ))
#<safearray...>
[color=red]_$ (vlax-safearray-get-u-bound matrix 1)
2[/color]
[color=green]_$ (vlax-safearray-get-u-bound matrix 2)
2[/color]
_$ (vlax-safearray-get-u-bound matrix 3)
; error: ActiveX Server returned an error: Invalid index
_1$
; reset after error

Code: [Select]
_1$ (setq matrix (vlax-make-safearray vlax-vbString '(1 . 2) '(1 . 3) '(1 . 4)))
#<safearray...>
_1$
; reset after error
_$ (vlax-safearray-get-u-bound matrix 1)
2
_$ (vlax-safearray-get-u-bound matrix 2)
3
_$ (vlax-safearray-get-u-bound matrix 3)
4
_$ (vlax-safearray-get-u-bound matrix 4)
; error: ActiveX Server returned an error: Invalid index


Code: [Select]
_2$ (setq matrix (vlax-make-safearray vlax-vbString '(1 . 2) '[color=red](2 . 5)[/color] '(1 . 4)))
#<safearray...>
_2$ (vlax-safearray-get-[color=red]u[/color]-bound matrix 2)
5
_3$ (vlax-safearray-get-[color=red]l[/color]-bound matrix 2)
2
_3$
Title: Re: Dimensions of Safearrays
Post by: Lee Mac on June 22, 2009, 11:48:10 AM
ahh, I see now.

The reason I ask this question is in relation to its use with the IntersectWith method.

I now use vlax-invoke... as it returns a list of points instead of a safearray, but, when using the vla-IntersectWith method, there is a check that is performed:

Code: [Select]
(if (> (vlax-safearray-get-u-bound iArr 1) 0)
(progn
  (setq iLst (vlax-safearray->list iArr))

If this is just returning the last element of the first dimension of the safearray, then why is it checking for a positive value, when the contents of the safearray is a set of points - surely these points can be zero?

Maybe I'm on another planet today...

Lee
Title: Re: Dimensions of Safearrays
Post by: hermanm on June 22, 2009, 11:50:07 AM
You can save some typing:

Code: [Select]

Command: (setq m (safearray vlax-vbString '(1 . 2) '(1 . 2) ))
#<safearray...>

Command: (safearray-get-u-bound m 2)
2

Title: Re: Dimensions of Safearrays
Post by: CAB on June 22, 2009, 12:08:15 PM
A safearray with a U-Bound of -1 means it's an empty array.
Title: Re: Dimensions of Safearrays
Post by: Lee Mac on June 22, 2009, 12:11:21 PM
A safearray with a U-Bound of -1 means it's an empty array.

Ahh I see now  :-D
Title: Re: Dimensions of Safearrays
Post by: JohnK on June 22, 2009, 02:47:24 PM
Ah safearrays. Them be fun:

A little food for thought; you can do so much if you know what you have before you create/parse it.
Quote
Command: (vlax-safearray->list (vlax-make-safearray vlax-vbString '(1 . 81)))
("" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""
"" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""
"" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""
"" "" "")

Command: (vlax-safearray->list (vlax-make-safearray vlax-vbString '(0 . 2) '(0 . 2) '(0 . 2) '(0 . 2)))
(
 ((("" "" "") ("" "" "") ("" "" ""))
  (("" "" "") ("" "" "") ("" "" ""))
  (("" "" "") ("" "" "") ("" "" "")))
 ((("" "" "") ("" "" "") ("" "" ""))
  (("" "" "") ("" "" "") ("" "" ""))
  (("" "" "") ("" "" "") ("" "" "")))
 ((("" "" "") ("" "" "") ("" "" ""))
  (("" "" "") ("" "" "") ("" "" ""))
  (("" "" "") ("" "" "") ("" "" "")))
 )
-e.g. a sentence, or several words?
Cool huh?
Title: Re: Dimensions of Safearrays
Post by: Lee Mac on June 22, 2009, 03:18:52 PM
Thanks for that Se7en - very helpful, but I am a little confused with the second example, why is it not just:

Code: [Select]
(("" "" "") ("" "" "") ("" "" "") ("" "" ""))

?
Title: Re: Dimensions of Safearrays
Post by: JohnK on June 22, 2009, 11:31:25 PM
sorry im typing this on my phone so i will keep this brief and hope you understand.

3 + 3 + 3 = 9
...
9 + 9 + 9 = 27
...
27 + 27 + 27 = 81
or
3^4 = 81
Title: Re: Dimensions of Safearrays
Post by: Lee Mac on June 23, 2009, 10:27:52 AM
Got it. Thanks Se7en  8-)
Title: Re: Dimensions of Safearrays
Post by: Lee Mac on June 23, 2009, 10:33:29 AM
This is how I visualised it:   :roll:

Code: [Select]
(
 (([color=red]("" "" "")[/color] ("" "" "") ("" "" ""))
  (("" "" "") ("" "" "") ("" "" ""))
  (("" "" "") ("" "" "") ("" "" "")))
 ((("" "" "") ("" "" "") ("" "" ""))
  (("" "" "") ("" "" "") ("" "" ""))
  (("" "" "") ("" "" "") ("" "" "")))
 ((("" "" "") ("" "" "") ("" "" ""))
  (("" "" "") ("" "" "") ("" "" ""))
  (("" "" "") ("" "" "") ("" "" "")))
)

(
 ([color=BLUE](("" "" "") ("" "" "") ("" "" ""))[/color]
  (("" "" "") ("" "" "") ("" "" ""))
  (("" "" "") ("" "" "") ("" "" "")))
 ((("" "" "") ("" "" "") ("" "" ""))
  (("" "" "") ("" "" "") ("" "" ""))
  (("" "" "") ("" "" "") ("" "" "")))
 ((("" "" "") ("" "" "") ("" "" ""))
  (("" "" "") ("" "" "") ("" "" ""))
  (("" "" "") ("" "" "") ("" "" "")))
)

(
 [color=green]((("" "" "") ("" "" "") ("" "" ""))
  (("" "" "") ("" "" "") ("" "" ""))
  (("" "" "") ("" "" "") ("" "" ""))) [/color]
 ((("" "" "") ("" "" "") ("" "" ""))
  (("" "" "") ("" "" "") ("" "" ""))
  (("" "" "") ("" "" "") ("" "" "")))
 ((("" "" "") ("" "" "") ("" "" ""))
  (("" "" "") ("" "" "") ("" "" ""))
  (("" "" "") ("" "" "") ("" "" "")))
)

[color=purple](
 ((("" "" "") ("" "" "") ("" "" ""))
  (("" "" "") ("" "" "") ("" "" ""))
  (("" "" "") ("" "" "") ("" "" "")))
 ((("" "" "") ("" "" "") ("" "" ""))
  (("" "" "") ("" "" "") ("" "" ""))
  (("" "" "") ("" "" "") ("" "" "")))
 ((("" "" "") ("" "" "") ("" "" ""))
  (("" "" "") ("" "" "") ("" "" ""))
  (("" "" "") ("" "" "") ("" "" "")))
)[/color]

[color=red]Red  = first Dimension[/color]
[color=blue]Blue = second Dimension[/color]
[color=green]Green = Third Dimension[/color]
[color=purple]Purple = Fourth Dimension[/color]

Title: Re: Dimensions of Safearrays
Post by: CAB on June 23, 2009, 10:58:55 AM
That's the way I see it.
Because this:
Code: [Select]
(vlax-safearray->list (vlax-make-safearray vlax-vbString '(0 . 2) '(0 . 2) '(0 . 2)))   
yields this:
             
Code: [Select]
'((("" "" "") ("" "" "") ("" "" ""))
  (("" "" "") ("" "" "") ("" "" ""))
  (("" "" "") ("" "" "") ("" "" "")))
Title: Re: Dimensions of Safearrays
Post by: Lee Mac on June 23, 2009, 11:46:58 AM
I find I can't take something at face value and accept it, I have to break it down and work out why it is the way it is.  :-P
Title: Re: Dimensions of Safearrays
Post by: JohnK on June 23, 2009, 01:10:51 PM
Not sure i follow your breakdown method exactly. I think i get what your saying, but its been a while since ive played with arrays and i never was all that crystal on everything about them to begin with...more of a theory kind of application.

However, i think you may have that order reversed. Im waiting for someone of a little more clout then a flunky to chime in but i dont see how your breakdown works on these?
Code: [Select]
(vlax-safearray->list (vlax-make-safearray vlax-vbString '(0 . 2) '(0 . 3)))
(("" "" "" "") ("" "" "" "") ("" "" "" ""))

(vlax-safearray->list (vlax-make-safearray vlax-vbString '(0 . 3) '(0 . 2)))
(("" "" "") ("" "" "") ("" "" "") ("" "" ""))

So: (("" "" "" "") ("" "" "" "") ("" "" "" ""))
or
'(0 . 2) '(0 . 3)
or
[lower]   [upper]
=>
Code: [Select]
(
 [lower bounds]
   ([upper bounds] "" "" "" "" [/upper bounds]) ("" "" "" "") ("" "" "" "")
 [/lower bounds]
 )

Title: Re: Dimensions of Safearrays
Post by: Lee Mac on June 23, 2009, 01:20:36 PM
Yes I believe you are correct Se7en,

I just took a look at this one:

Code: [Select]
(vlax-safearray->list (vlax-make-safearray vlax-vbString '[color=purple](0 . 3)[/color] '[color=green](0 . 2)[/color] '[color=blue](0 . 1)[/color] '[color=red](0 . 0)[/color]))
(
  (([color=red]("") [/color](""))
   (("") (""))
   (("") ("")))
  ((("") (""))
   (("") (""))
   (("") ("")))
  ((("") (""))
   (("") (""))
   (("") ("")))
  ((("") (""))
    ("") (""))
   (("") ("")))
)

(
  ([color=blue](("") (""))[/color]
   (("") (""))
   (("") ("")))
  ((("") (""))
   (("") (""))
   (("") ("")))
  ((("") (""))
   (("") (""))
   (("") ("")))
  ((("") (""))
    ("") (""))
   (("") ("")))
)

(
  [color=green]((("") (""))
   (("") (""))
   (("") ("")))[/color]
  ((("") (""))
   (("") (""))
   (("") ("")))
  ((("") (""))
   (("") (""))
   (("") ("")))
  ((("") (""))
    ("") (""))
   (("") ("")))
)

[color=purple](
  ((("") (""))
   (("") (""))
   (("") ("")))
  ((("") (""))
   (("") (""))
   (("") ("")))
  ((("") (""))
   (("") (""))
   (("") ("")))
  ((("") (""))
    ("") (""))
   (("") ("")))
)[/color]
Title: Re: Dimensions of Safearrays
Post by: CAB on June 23, 2009, 01:25:32 PM
Good catch John. 8-)