用IntersectWith函数。

- [FONT=courier new]
- Sub Example_IntersectWith()
- ' 创建直线
- Dim lineObj As AcadLine
- Dim startPt(0 To 2) As Double
- Dim endPt(0 To 2) As Double
- startPt(0) = 1: startPt(1) = 1: startPt(2) = 0
- endPt(0) = 5: endPt(1) = 5: endPt(2) = 0
- Set lineObj = ThisDrawing.ModelSpace.AddLine(startPt, endPt)
-
- ' 创建圆
- Dim circleObj As AcadCircle
- Dim centerPt(0 To 2) As Double
- Dim radius As Double
- centerPt(0) = 3: centerPt(1) = 3: centerPt(2) = 0
- radius = 1
- Set circleObj = ThisDrawing.ModelSpace.AddCircle(centerPt, radius)
-
- ' 判断直线和圆是否有交点及交点坐标,返回值为空表示不相交,否则返回值就是所有交点的坐标。
- Dim intPoints As Variant
- intPoints = lineObj.IntersectWith(circleObj, acExtendNone)
-
- ' Print all the intersection points
- Dim I As Integer, j As Integer, k As Integer
- Dim str As String
- If VarType(intPoints) <> vbEmpty Then
- For I = LBound(intPoints) To UBound(intPoints)
- str = "交点[" & k & "] is: " & intPoints(j) & "," & intPoints(j + 1) & "," & intPoints(j + 2)
- MsgBox str, , "相交示例"
- str = ""
- I = I + 2
- j = j + 3
- k = k + 1
- Next
- End If
- End Sub
- [/FONT]
|