Select polyline segment

https://forums.augi.com/showthread ... lect-polyline-segment
程序代码:

    Public Sub SelectNestedPolyline()
            Dim doc As Document = Application.DocumentManager.MdiActiveDocument
            Dim db As Database = doc.Database
            Dim ed As Editor = doc.Editor

            ' Prompt the user to select a polyline segment.
            Dim options As New PromptNestedEntityOptions(vbLf & "Select a polyline segment: ")
            options.AllowNone = False
            Dim result As PromptNestedEntityResult = ed.GetNestedEntity(options)
            If result.Status <> PromptStatus.OK Then
                Return
            End If

            ' If the selected entity is a polyline.
            If result.ObjectId.ObjectClass.Name = "AcDbPolyline" Then

                ' Start a transaction to open the selected polyline.
                Using tr As Transaction = db.TransactionManager.StartTransaction()

                    ' Transform the picked point from current UCS to WCS.
                    Dim wcsPickedPoint As Point3d = result.PickedPoint.TransformBy(ed.CurrentUserCoordinateSystem)

                    ' Open the polyline.
                    Dim pline As Polyline = DirectCast(tr.GetObject(result.ObjectId, OpenMode.ForRead), Polyline)

                    ' Get the closest point to picked point on the polyline.
                    ' If the polyline is nested, it's needed to transform the picked point using the 
                    ' the transformation matrix that is applied to the polyline by its containers.
                    Dim pointOnPline As Point3d = _
                        If(result.GetContainers().Length = 0, _
                            pline.GetClosestPointTo(wcsPickedPoint, False), _
                            pline.GetClosestPointTo(wcsPickedPoint.TransformBy(result.Transform.Inverse()), False))

                    ' Get the selected segment index.
                    Dim segmentIndex As Integer = CInt(pline.GetParameterAtPoint(pointOnPline))
                    ed.WriteMessage(vbLf & "Selected segment index: {0}", segmentIndex)
                    tr.Commit()
                End Using
            End If
        End Sub






Please follow WeChat's public account ByCAD