Interpret Variant Arrays

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 don't know the data type contained in the variant, use the VBA functions VarType or Typename. These functions return the type of data in the variant. If you need to iterate through the array, you can use the VBA For Eachstatement.

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.

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