关于解释变体数组 (ActiveX)
从 AutoCAD ActiveX Automation 传回的阵列信息将作为变体传回。
如果您知道数组的数据类型,则可以简单地将变体作为数组进行访问。如果不知道变体中包含的数据类型,请使用 VBA 中的 or 函数或 AutoLISP 中的 、 和 函数。这些函数返回变体中的数据类型。如果需要循环访问数组,可以在 AutoLISP 中使用 VBA 语句或函数。VarTypeTypeNamevlax-variant-typevlax-variant-valuetypeFor Eachvlax-for
计算两点之间的距离
下面的代码演示如何计算用户输入的两点之间的距离。在此示例中,数据类型是已知的,因为所有坐标都是双精度坐标。3D 坐标是双精度的三元素数组,2D 坐标是双精度的双元素数组。
- 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)
-
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
|