TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: DEVITG on September 01, 2004, 09:26:22 AM

Title: variant -> safearray ->list
Post by: DEVITG on September 01, 2004, 09:26:22 AM
Using this code

Code: [Select]
(defun vol-solido (/;| sel-solido objeto volumen|;)
 
(setq e (car (entsel"\nSelect a solid: ")))
(setq vlaobj (vlax-ename->vla-object e))
(SETQ VOLUMEN  (vlax-get-property vlaobj "VOLUME") )

(setq lista (vlax-dump-object  vlaobj))

  (setq centroid (vlax-get-property vlaobj "centroid") )
 
  (vlax-safearray->list centroid)
 
  (setq volumen1 (vla-get-volume vlaobj))
(setq centroid1 (vla-get-centroid vlaobj))

  (vlax-variant-value centroid1)
 
 
(princ   volumen)
);_end defun

(vol-solido)



how i can get the list of centroid or any other variant  :?:
Title: variant -> safearray ->list
Post by: SMadsen on September 01, 2004, 09:42:47 AM
Commented in code:

Code: [Select]
(defun vol-solido (/ e vlaobj centroid volumen)
  (cond ((setq e (car (entsel "\nSelect a solid: ")))
         ;; only proceed if entity is selected
         (setq vlaobj (vlax-ename->vla-object e))
         ;; check if volume is available
         (and (vlax-property-available-p vlaobj "Volume")
              (setq volumen (vlax-get-property vlaobj "Volume"))
              (princ (strcat "\nVolume:   " (rtos volumen)))
         )
         ;; check if centroid is available
         (and (vlax-property-available-p vlaobj "Centroid")
              ;; three step conversion to list:
              ;; 1. get property = variant
              ;; 2. extract variant value = safearray
              ;; 3. convert safearray to list
              (setq centroid
                     (vlax-safearray->list
                       (vlax-variant-value
                         (vlax-get-property vlaobj "centroid")
                       )
                     )
              )
              ;; if centroid is 3D (e.g. for solids) -> print 3 coordinates
              (if (> (length centroid) 2)
                (princ (strcat "\nCentroid: "
                               (rtos (car centroid))
                               ","
                               (rtos (cadr centroid))
                               ","
                               (rtos (caddr centroid))
                       )
                )
                ;; if centroid is 2D (e.g. regions) -> print 2 coordinates
                (princ (strcat "\nCentroid: "
                               (rtos (car centroid))
                               ","
                               (rtos (cadr centroid))
                       )
                )
              )
         )
        )
  )
  (princ)
)
Title: variant -> safearray ->list
Post by: DEVITG on September 01, 2004, 09:52:15 AM
I will  chew it , as I understand .
I preffer not to nest function , let me explode it .
Title: variant -> safearray ->list
Post by: sinc on September 02, 2004, 12:36:19 AM
A note about this kludgy vlax-variant stuff...

Sometimes code like the following will throw an invalid index exception:
Code: [Select]
(vlax-safearray->list
  (vlax-variant-value someVal)
)

It seems to happen when the safearray has no items in it, for example, if you try calling intersectWith and no intersections are found.  In order to prevent the exception, you can do the following:
Code: [Select]
(setq val (vlax-variant-value someVal))
(if (safearray-value val)
  (vlax-safearray->list val)
)

For some reason, the (safearray-value) function is not in the documentation.  Also note that, unlike every other variant-based command,  it fails to start with "vlax-".
Title: variant -> safearray ->list
Post by: DEVITG on September 02, 2004, 07:36:45 AM
thanks , I had notice that failure and your tip will allow me not to get it again.
 :D