LISP构建variant array 和 safearray 数组
Constructing a variant array and safearray in LISP问题:
Several ActiveX objects require that Arrays be used in LISP, however, I do not
understand how to implement them correctly since they are different from a list.
Is there an example that constructs and deconstructs a variant array ?
解答:
There are several solutions that show how to construct objects via ActiveX that
require the use of variant arrays, look at DevNote TS16152 and DevNote TS57438
for complete examples.
The following code details how to construct a variant array:
;;;Make the Variant Array:
; First Create a SafeArray by identifying the Type, and the Array Dimension
; In this example it's a 2 dimensional array of objects
(setq aSafeArray (vlax-make-safearray vlax-vbobject '(0 . 1)))
; Add the first object 'lineobj' to the SafeArray
(vlax-safearray-put-element aSafeArray 0 lineobj)
; Add the second object 'arcobj' to the SafeArray
(vlax-safearray-put-element aSafeArray 1 arcobj)
; Create a Variant Array from the Safe Array
(setq VariantArrayOfObjects (vlax-make-variant aSafeArray (logior vlax-vbarray
vlax-vbobject)))
; Next let's Create another Variant
; First Create an Empty Two Dimensional SafeArray of Integers
(setq nilSafeArray (vlax-make-safearray vlax-vbinteger '(0 . 1)))
; Create a Variant Array Using That Empty SafeArray
(setq inoutVarArray (vlax-make-variant nilSafeArray (logior vlax-vbarray
vlax-vbinteger)))
; Populate that Empty Array, and return an Array of Objects with the CopyObjects
Method
(setq retnArray (vla-CopyObjects AcadDocument VariantArrayOfObjects *ModelSpace*
inoutVarArray))
And here's a code that will deconstruct the returned Variant Array:
;;;Deconstruct the variant array:
; Convert the Variant Array to SafeArray, and then to a List
(setq varLen (length (setq rtnLst (vlax-safearray->list (vlax-variant-value
retnArray)))))
; Process the List of Objects
(if (> varlen 0)
(mapcar '(lambda (x)
(vla-Move x (vlax-3d-point '(1.0 1.0 0.0)) (vlax-3d-point '(5.0 1.0 0.0)))
(vla-Put-Color x acRed)
)
rtnLst
)
)
For more information on Variant Data Types see DevNote #53397 and <Solution
53398>.
The preceding code is not used here in a complete example:
(defun RunCopyTest (/ AcadApplication AcadDocument *ModelSpace* aSafeArray
lineObj arcObj rtn
VariantArrayOfObjects nilSafeArray inoutVarArray retnArray
varLen rtnLst)
(vl-load-com)
(setq AcadApplication (vlax-get-Acad-Object))
(setq AcadDocument (vla-get-ActiveDocument AcadApplication))
(setq *ModelSpace*(vla-get-ModelSpace AcadDocument))
; Add a Line
(setq lineObj (vla-AddLine *ModelSpace*(vlax-3d-point '(1.0 1.0 0.0))
(vlax-3d-point '(5.0 5.0 0.0))))
(vla-Put-Color lineObj acBlue)
; Add an Arc
(setq arcObj (vla-AddArc *ModelSpace* (vlax-3d-point '(5.0 3.0 0.0)) 2.00.0
pi))
(vla-Put-Color arcObj acBlue)
(vla-ZoomExtents AcadApplication)
(initget "Yes No")
(setq rtn (getkword "\nNow Create the Copy <Y/n>:"))
(if (equal rtn "Yes")
(progn
;;;Make the Variant Array:
; First Create a SafeArray by identifying the Type, and the Array Dimension
; In this example it's a 2 dimensional array of objects
(setq aSafeArray (vlax-make-safearray vlax-vbobject '(0 . 1)))
; Add the first object 'lineobj' to the SafeArray
(vlax-safearray-put-element aSafeArray 0 lineobj)
; Add the second object 'arcobj' to the SafeArray
(vlax-safearray-put-element aSafeArray 1 arcobj)
;; Important - Create a Variant Array of vlax-vbobjects
; Create a Variant Array from the Safe Array
(setq VariantArrayOfObjects (vlax-make-variant aSafeArray (logior
vlax-vbarray vlax-vbobject)))
; Next let's Create another SafeArray
; Create an Empty Two Dimensional SafeArray of Integers
(setq nilSafeArray (vlax-make-safearray vlax-vbinteger '(0 . 1)))
; Create a Variant Array Using That Empty SafeArray
(setq inoutVarArray (vlax-make-variant nilSafeArray (logior vlax-vbarray
vlax-vbinteger)))
; Populate that Empty Array, and return an Array of Objects with the CopyObjects
Method
;; Now Copy the objects here:
(setq retnArray (vla-CopyObjects AcadDocument VariantArrayOfObjects
*ModelSpace* inoutVarArray))
;;;Deconstruct the variant array:
; Convert the Variant Array to SafeArray, and then to a List
(setq varLen (length (setq rtnLst (vlax-safearray->list
(vlax-variant-value retnArray)))))
; Process the List of Objects
;; Move the new objects, and give them a different color:
(if (> varlen 0)
(mapcar
'(lambda (x)
(vla-Move x (vlax-3d-point '(1.0 1.0 0.0)) (vlax-3d-point '(7.0
3.0 0.0)))
(vla-Put-Color x acYellow)
)
rtnLst
)
)
(vla-ZoomExtents AcadApplication)
)
)
;;; Release the Objects
(if arcObj (if (null (vlax-object-released-p arcObj)) (progn
(vlax-release-object arcObj) (setq arcObj nil))) )
(if lineObj (if (null (vlax-object-released-p lineObj)) (progn
(vlax-release-object lineObj) (setq lineObj nil))) )
(if *ModelSpace* (if (null (vlax-object-released-p *ModelSpace*)) (progn
(vlax-release-object *ModelSpace*) (setq *ModelSpace* nil))) )
(if AcadDocument (if (null (vlax-object-released-p AcadDocument)) (progn
(vlax-release-object AcadDocument) (setq AcadDocument nil))) )
(if AcadApplication (if (null (vlax-object-released-p AcadApplication)) (progn
(vlax-release-object AcadApplication) (setq AcadApplication nil))) )
(princ)
)
(princ "\nRunCopyTest loaded, type (RunCopyTest) to run.")
(princ)
页:
[1]