Results 1 to 4 of 4

Thread: Select polyline segment

  1. #1
    Member
    Join Date
    2011-02
    Posts
    17
    Login to Give a bone
    0

    Default Select polyline segment

    Hi

    I want to make routine to select polyline segment?


    what Am I doing wrong?

    Problem is in this line:
    Code:
    Dim nestedObject As DBObject = nestedResults.ObjectId.GetObject(OpenMode.ForRead)
    i alwasy get exeption here: Object reference not set to an instance of an object

    Code:
     Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
            Dim doc As Document = DocumentManager.MdiActiveDocument
            Dim db As Database = doc.Database
            Dim ed As Editor = doc.Editor
            Dim rad As Double = 40 * ppc / 1000
            Dim nestedOptions As New PromptNestedEntityOptions("Odaberi segment: ")
            nestedOptions.AllowNone = False
            Dim nestedResults As PromptNestedEntityResult = ed.GetNestedEntity(nestedOptions)
            If nestedResults.Status = PromptStatus.OK Then
                Dim nestedObject As DBObject = nestedResults.ObjectId.GetObject(OpenMode.ForRead)
                If nestedObject.[GetType]().Equals(GetType(Polyline)) Then
                    MsgBox("nije polyline")
                    Dim plineEntity As Polyline = DirectCast(nestedObject, Polyline)
                    Dim searchPoint As Point2d = plineEntity.GetClosestPointTo(nestedResults.PickedPoint, False).Add(plineEntity.StartPoint.GetAsVector()).Convert2d(plineEntity.GetPlane())
                    Dim segmentIndex As Integer
                    For segmentIndex = 0 To plineEntity.NumberOfVertices - 2
                        If plineEntity.OnSegmentAt(segmentIndex, searchPoint, 0.0) Then
                            Exit For
                        End If
                    Next
                    Dim lineSegment As LineSegment3d = plineEntity.GetLineSegmentAt(segmentIndex)
                    MsgBox("Broj segmenta je: " & segmentIndex + 1)
                Else
                    MsgBox("nije polyline")
                    Exit Sub
                End If
    
    
            End If
        End Sub
    End Class
    Please help.

    KK

  2. #2
    AUGI Addict fixo's Avatar
    Join Date
    2005-05
    Location
    Pietari, Venäjä
    Posts
    1,269
    Login to Give a bone
    0

    Default Re: Select polyline segment

    Explain us what is your goal, because from your code
    is too difficult to understand you steps,
    btw, small picture may help to see that, so make
    a screenshot from drawing and upload here

  3. #3
    Past Vice President / AUGI Volunteer peter's Avatar
    Join Date
    2000-09
    Location
    Honolulu HI
    Posts
    1,115
    Login to Give a bone
    0

    Default Re: Select polyline segment

    My first observation is you are trying to do something with your open for read object, usually in .net that would require a transaction...

    Remember this is managed code!

    I haven't tested it but it might something this below.

    P=

    Quote Originally Posted by krkeec763189 View Post
    Hi

    I want to make routine to select polyline segment?


    what Am I doing wrong?

    Problem is in this line:
    Code:
    Dim nestedObject As DBObject = nestedResults.ObjectId.GetObject(OpenMode.ForRead)
    i alwasy get exeption here: Object reference not set to an instance of an object

    Code:
     Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
            Dim doc As Document = DocumentManager.MdiActiveDocument
            Dim db As Database = doc.Database
            Dim ed As Editor = doc.Editor
            Dim rad As Double = 40 * ppc / 1000
            Dim nestedOptions As New PromptNestedEntityOptions("Odaberi segment: ")
            nestedOptions.AllowNone = False
            Dim nestedResults As PromptNestedEntityResult = ed.GetNestedEntity(nestedOptions)
    
            Dim tr as transaction = db.TransactionManager.StartTransaction()
    
            If nestedResults.Status = PromptStatus.OK Then
    
                Using tr
    
                Dim nestedObject As DBObject = nestedResults.ObjectId.GetObject(OpenMode.ForRead)
                If nestedObject.[GetType]().Equals(GetType(Polyline)) Then
                    MsgBox("nije polyline")
                    Dim plineEntity As Polyline = DirectCast(nestedObject, Polyline)
                    Dim searchPoint As Point2d = plineEntity.GetClosestPointTo(nestedResults.PickedPoint, False).Add(plineEntity.StartPoint.GetAsVector()).Convert2d(plineEntity.GetPlane())
                    Dim segmentIndex As Integer
                    For segmentIndex = 0 To plineEntity.NumberOfVertices - 2
                        If plineEntity.OnSegmentAt(segmentIndex, searchPoint, 0.0) Then
                            Exit For
                        End If
                    Next
                    Dim lineSegment As LineSegment3d = plineEntity.GetLineSegmentAt(segmentIndex)
    
    
                    MsgBox("Broj segmenta je: " & segmentIndex + 1)
                Else
                    MsgBox("nije polyline")
                    Exit Sub
                End If
    
    
                tr.commit
                End Using
    
    
            End If
        End Sub
    End Class
    Please help.

    KK
    AutomateCAD

  4. #4
    I could stop if I wanted to
    Join Date
    2007-08
    Posts
    202
    Login to Give a bone
    0

    Default Re: Select polyline segment

    Hi,

    If I do not misunderstand what you're trying to do (prompt for selecting a nested polyline and get the selected segment on polyline), here's a way which will work whatever the nesting level (even not nested at all).

    C#
    Code:
            public void SelectNestedPolyline()
            {
                Document doc = Application.DocumentManager.MdiActiveDocument;
                Database db = doc.Database;
                Editor ed = doc.Editor;
    
                // Prompt the user to select a polyline segment.
                PromptNestedEntityOptions options =
                    new PromptNestedEntityOptions("\nSelect a polyline segment: ");
                options.AllowNone = false;
                PromptNestedEntityResult result = ed.GetNestedEntity(options);
                if (result.Status != PromptStatus.OK)
                    return;
    
                // If the selected entity is a polyline.
                if (result.ObjectId.ObjectClass.Name == "AcDbPolyline")
                {
                    // Start a transaction to open the selected polyline.
                    using (Transaction tr = db.TransactionManager.StartTransaction())
                    {
                        // Transform the picked point from current UCS to WCS.
                        Point3d wcsPickedPoint = result.PickedPoint.TransformBy(ed.CurrentUserCoordinateSystem);
    
                        // Open the polyline.
                        Polyline pline = (Polyline)tr.GetObject(result.ObjectId, OpenMode.ForRead);
    
                        // 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.
                        Point3d pointOnPline = result.GetContainers().Length == 0 ?
                            pline.GetClosestPointTo(wcsPickedPoint, false) : // not nested polyline
                            pline.GetClosestPointTo(wcsPickedPoint.TransformBy(result.Transform.Inverse()), false); // nested polyline
    
                        // Get the selected segment index.
                        int segmentIndex = (int)pline.GetParameterAtPoint(pointOnPline);
                        ed.WriteMessage("\nSelected segment index: {0}", segmentIndex);
                        tr.Commit();
                    }
                }
            }
    VB
    Code:
            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
    F#
    Code:
    let SelectNestedPolyline () =
        let doc = Application.DocumentManager.MdiActiveDocument
        let db = doc.Database
        let ed = doc.Editor
    
        // Prompt the user to select a polyline segment.
        let options = new PromptNestedEntityOptions("\nSelect a polyline segment: ")
        options.AllowNone <- false
        let result = ed.GetNestedEntity(options)
    
        // If the selected entity is a polyline.
        if result.Status = PromptStatus.OK && 
            result.ObjectId.ObjectClass.Name = "AcDbPolyline" then
    
            // Start a transaction to open the selected polyline
            use tr = db.TransactionManager.StartTransaction()
    
            // Transform the picked point from current UCS to WCS.
            let wcsPickedPoint = result.PickedPoint.TransformBy(ed.CurrentUserCoordinateSystem)
            
            // Open the polyline.
            let pline = 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.
            let pointOnPline =
                if result.GetContainers().Length = 0 then pline.GetClosestPointTo(wcsPickedPoint, false) 
                else pline.GetClosestPointTo(wcsPickedPoint.TransformBy(result.Transform.Inverse()), false);
    
            // Get the selected segment index.
            let segmentIndex = int (pline.GetParameterAtPoint(pointOnPline))
            ed.WriteMessage("\nSelected segment index: {0}", segmentIndex)
    
            tr.Commit()

Similar Threads

  1. Select all Objects inside a Polyline
    By avinash00002002 in forum VBA/COM Interop
    Replies: 8
    Last Post: 2015-10-21, 07:09 AM
  2. Replies: 5
    Last Post: 2014-06-22, 04:36 AM
  3. Select polyline and get coordinates
    By avinash patil in forum Dot Net API
    Replies: 5
    Last Post: 2012-11-04, 08:24 PM
  4. help aligning mtext to a select polyline
    By cmccartney in forum AutoLISP
    Replies: 10
    Last Post: 2011-09-28, 03:45 PM
  5. One segment polyline curve
    By iranpour36 in forum AutoCAD Tips & Tricks
    Replies: 6
    Last Post: 2007-07-13, 05:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •