Array information passed back from AutoCAD ActiveX Automation is passed back as a variant.
If you know the data type of the array, you can simply access the variant as an array. If you do not know the data type contained in the variant, use the VarType or TypeName functions in VBA or the vlax-variant-type, vlax-variant-value, and type functions in AutoLISP. These functions return the type of data in the variant. If you need to iterate through the array, you can use the For Each VBA statement or vlax-for function in AutoLISP.
Calculate the distance between two points
The following code demonstrates calculating the distance between two points input by the user. In this example, the data type is known because all coordinates are doubles. 3D coordinates are a three-element array of doubles and 2D coordinates are a two-element array of doubles.
- AutoLISP
-
(vl-load-com) (defun c:Ch2_CalculateDistance() (setq acadObj (vlax-get-acad-object) doc (vla-get-ActiveDocument acadObj) utilObj (vla-get-Utility doc)) ;; Get the points from the user (setq point1 (vlax-variant-value (vla-GetPoint utilObj nil "First point: ")) point2 (vlax-variant-value (vla-GetPoint utilObj point1 "Second point: "))) ;; Calculate the distance between point1 and point2 (setq X (- (vlax-safearray-get-element point1 0) (vlax-safearray-get-element point2 0)) Y (- (vlax-safearray-get-element point1 1) (vlax-safearray-get-element point2 1)) Z (- (vlax-safearray-get-element point1 2) (vlax-safearray-get-element point2 2)) dist (sqrt (+ (expt (sqrt (+ (expt X 2)(expt Y 2))) 2) (expt Z 2)))) ;; Display the resulting distance (alert (strcat "The distance between the points is: " (rtos dist 2))) )
- VBA (AutoCAD Only)
-
Sub Ch2_CalculateDistance() Dim point1 As Variant Dim point2 As Variant '' Get the points from the user point1 = ThisDrawing.Utility.GetPoint(, vbCrLf & "First point: ") point2 = ThisDrawing.Utility.GetPoint(point1, vbCrLf & "Second point: ") '' Calculate the distance between point1 and point2 Dim x As Double, y As Double, z As Double Dim dist As Double x = point1(0) - point2(0) y = point1(1) - point2(1) z = point1(2) - point2(2) dist = Sqr((Sqr((x ^ 2) + (y ^ 2)) ^ 2) + (z ^ 2)) '' Display the resulting distance MsgBox "The distance between the points is: " _ & dist, , "Calculate Distance" End Sub