Valued Contributor
59 Posts
0 Kudos
Registered: ‎11-05-2013
Post 1 of 3

set startpoint for polyline

297 Views, 2 Replies
12-24-2013 10:55 PM

Hi All,

I have one question, hope everybody can help me. How can i set startpoint for polyline. And New StartPoint have to near Original System(O). I have attached image.


*Expert Elite*
1,569 Posts
171 Kudos
Registered: ‎10-08-2008
Post 2 of 3

Re: set startpoint for polyline

12-25-2013 08:00 AM in reply to: hoathuongphuoc

You can easily redraw this polyline, if this one

is have the straight segments only, here is a quick code for this case:


        ' for using to updating a straight polyline segments only
        <CommandMethod("repst", CommandFlags.Redraw)> _
        Public Shared Sub TestChangePolyStart()
            Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
            Dim ed As Editor = doc.Editor
            Dim db As Database = doc.Database
            Dim ucs As CoordinateSystem3d = ed.CurrentUserCoordinateSystem.CoordinateSystem3d

            Dim osm As Object = Nothing

            Dim mat As Matrix3d = Matrix3d.AlignCoordinateSystem(Point3d.Origin, Vector3d.XAxis, Vector3d.YAxis, Vector3d.ZAxis, ucs.Origin, ucs.Xaxis, _
             ucs.Yaxis, ucs.Zaxis)

                Using doclock As DocumentLock = doc.LockDocument()
                    osm = Autodesk.AutoCAD.ApplicationServices.Application.GetSystemVariable("osmode")

                    Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("osmode", 1)

                    Dim peo1 As New PromptEntityOptions(vbLf & "Select a  polyline : ")

                    peo1.SetRejectMessage(vbLf & "You have to select polyline only!")

                    peo1.AddAllowedClass(GetType(Polyline), False)

                    Dim res1 As PromptEntityResult = ed.GetEntity(peo1)

                    If res1.Status <> PromptStatus.OK Then
                    End If

                    Dim id As ObjectId = res1.ObjectId
                    Dim p1 As Point3d = res1.PickedPoint.TransformBy(mat)

                    Dim prOpt As New PromptPointOptions(vbLf & "Pick a new start vertex point: ")

                    Dim es As PromptPointResult = ed.GetPoint(prOpt)

                    If es.Status <> PromptStatus.OK Then
                        If es.Status = PromptStatus.Cancel Then
                            ed.WriteMessage(vbLf & "Interrupted by user")
                            ed.WriteMessage(vbLf & "Error on specifying a point")
                        End If
                    End If

                    Dim new3dPt As Point3d = es.Value.TransformBy(mat)

                    Using tr As Transaction = db.TransactionManager.StartTransaction()
                        Dim poly As Polyline = DirectCast(tr.GetObject(id, OpenMode.ForRead), Polyline)

                        Dim wid As Double = poly.ConstantWidth

                        Dim new2dPt As New Point2d(new3dPt.X, new3dPt.Y)

                        Dim c As Integer = 0

                        Dim points As New List(Of Point2d)()
                        Dim sb As New StringBuilder()
                        For c = 0 To poly.NumberOfVertices - 1


                        Dim pos As Integer = points.IndexOf(new2dPt)

                        'Reverse List from given position using LINQ implementation

                        Dim tail As IEnumerable(Of Point2d) = points.Take(pos)

                        Dim head As IEnumerable(Of Point2d) = points.Except(tail)

                        Dim newpoints As IEnumerable(Of Point2d) = head.Concat(tail)

                        Dim pts As List(Of Point2d) = newpoints.ToList()

                        Dim btr As BlockTableRecord = DirectCast(tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite), BlockTableRecord)


                        Dim newpoly As New Polyline(poly.NumberOfVertices)

                        newpoly.Closed = poly.Closed

                        For c = 0 To pts.Count - 1
                            newpoly.AddVertexAt(c, CType(pts(c), Point2d), 0, wid, wid)
                        newpoly.Closed = poly.Closed


                        tr.AddNewlyCreatedDBObject(newpoly, True)





                    End Using
                End Using
            Catch ex As System.Exception
                ed.WriteMessage(vbLf & "{0}", ex.StackTrace)
                Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(vbLf & "{0}" & vbLf & "{1}" & vbLf, ex.Message, ex.StackTrace)
                Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("osmode", osm)
            End Try
        End Sub



Valued Contributor
59 Posts
0 Kudos
Registered: ‎11-05-2013
Post 3 of 3

Re: set startpoint for polyline

12-25-2013 06:17 PM in reply to: hoathuongphuoc

Thanks. If my polyline have arc(CircularArc2d), line... I will do the same above code.

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Are you interested in helping shape the future of the Autodesk Community? To participate in this brief usability study, please click here. Your time and input is greatly appreciated!