1. 程式人生 > >獲取所選取的多段線(polyline)的兩個端點

獲取所選取的多段線(polyline)的兩個端點

Imports ZwSoft.ZwCAD.Runtime
Imports ZwSoft.ZwCAD.Geometry
Imports ZwSoft.ZwCAD.DatabaseServices
Imports ZwSoft.ZwCAD.ApplicationServices
Imports ZwSoft.ZwCAD.EditorInput


Public Class Class2
    <CommandMethod("FindEndPts")> _
    Public Sub FindEndPts()
        Dim ZcDoc As Document = Application.DocumentManager.MdiActiveDocument
        Dim ZcDB As Database = ZcDoc.Database
        Dim ZcED As Editor = ZcDoc.Editor
        Using ZcTran As Transaction = ZcDB.TransactionManager.StartTransaction
            Dim objId As ObjectId = ObjectId.Null
            Dim picPt As Point3d = Nothing


            Do
                Dim peo As New PromptEntityOptions(vbLf & "Select a polyline:")
                Dim per As PromptEntityResult = ZcED.GetEntity(peo)
                If per.Status <> PromptStatus.OK Then
                    Exit Sub
                End If


                Dim ent As Entity = DirectCast(ZcTran.GetObject(per.ObjectId, OpenMode.ForRead), Entity)
                If TypeOf (ent) Is Line Or TypeOf (ent) Is Polyline Then
                    objId = per.ObjectId
                    picPt = per.PickedPoint
                Else
                    ZcED.WriteMessage("The object must be a line or a polyline!")
                End If


            Loop While objId = Nothing


            Dim Pt1 As Point3d = Nothing
            Dim Pt2 As Point3d = Nothing


            Dim pline As Polyline = DirectCast(ZcTran.GetObject(objId, OpenMode.ForRead), Polyline)
            Dim searchPoint As Point2d = pline.GetClosestPointTo(picPt, False).Add(pline.StartPoint.GetAsVector()).Convert2d(pline.GetPlane())
            For j As Integer = 0 To pline.NumberOfVertices - 2
                Dim SegType As SegmentType = pline.GetSegmentType(j)
                Dim flag As Boolean = pline.OnSegmentAt(j, searchPoint, SegType)
                If flag Then
                    '    Dim lineSegment As LineSegment3d = plineEntity.GetLineSegmentAt(segmentIndex)
                    '    Pt1 = lineSegment.StartPoint
                    '    Pt2 = lineSegment.EndPoint
                    Pt1 = pline.GetPoint3dAt(j)
                    Pt2 = pline.GetPoint3dAt(j + 1)
                    Exit For
                End If
            Next


            ZcED.WriteMessage("The end point of the  selected segment are:" & vbLf & "Start point:" & Pt1.ToString & vbLf & "End point:" & Pt2.ToString)




        End Using


    End Sub




End Class