2008年11月21日 星期五

方法 IntersectWith 範例

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) = 10: endPt(1) = 10: 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) = 5: centerPt(1) = 5: centerPt(2) = 0
    '指定圓的半徑
    radius = 1
    '在圖面上畫圓
    Set circleObj = ThisDrawing.ModelSpace.AddCircle(centerPt, radius)
    '縮放實際範圍
    ZoomExtents

    Dim intPoints As Variant
    '找出線與圓的交叉點
    'acExtendNone 意思為是不延伸元件
    intPoints = lineObj.IntersectWith(circleObj, acExtendNone)

    Dim I As Integer, j As Integer, k As Integer
    Dim str As String
    '如果兩個元件沒有交叉則變數 intPoints 將會未被初始化
    If VarType(intPoints) <> vbEmpty Then
        For I = LBound(intPoints) To UBound(intPoints)
            str = "Intersection Point[" & k & "] is: " & intPoints(j) & "," _
                & intPoints(j + 1) & "," & intPoints(j + 2)
            MsgBox str, , "IntersectWith Example"
            str = ""
            I = I + 2
            j = j + 3
            k = k + 1
        Next
    End If
End Sub

沒有留言: