How to draw polyline from collection of 3d points?

How to draw polyline from collection of 3d points?

Anonymous
Not applicable
2,232 Views
2 Replies
Message 1 of 3

How to draw polyline from collection of 3d points?

Anonymous
Not applicable

Hi. How can I draw a polyline from a previously defined collection of 3d points?

I create a collection by:

Dim Linia = New Point3dCollection

Then I add points to this collection through:

Linia.Add(New Point3d((Val(Py)), (Val(Px)), 0))

And I want to draw Polyline from this collection. I use this code:

            Using acLckDoc As DocumentLock = acDoc.LockDocument
                Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
                    acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)
                    acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace),
                OpenMode.ForWrite)
                    Using acPLine As Polyline = New Polyline()
                        acPLine.AddVertexAt(0, Linia, 0, 0, 0)
                        acBlkTblRec.AppendEntity(acPLine)
                        acTrans.AddNewlyCreatedDBObject(acPLine, True)
                        acPLine.ColorIndex = 1
                    End Using
                    acTrans.Commit()
                End Using
            End Using

But this code causes the following error:

Error BC30311 Value of type 'Point3dCollection' cannot be converted to 'Point2d'.

0 Likes
Accepted solutions (1)
2,233 Views
2 Replies
Replies (2)
Message 2 of 3

_gile
Consultant
Consultant
Accepted solution

Hi,

 

Polyline.AddVertexAt() method requires Point2d as argument, so you have to iterate through the Point3dCollection and convert each Point3d to a Point2d, this can be done using the Point3d.Convert2d() method passing it the plane you want to draw the polyline on.

 

Here's a little example to draw the polyline on the XY plane.

 

            using (doc.LockDocument())
            using (var tr = doc.TransactionManager.StartTransaction())
            using (var pline = new Polyline())
            {
                var plane = new Plane(Point3d.Origin, Vector3d.ZAxis);
                for (int i = 0; i < points.Count; i++)
                {
                    pline.AddVertexAt(i, points[i].Convert2d(plane), 0.0, 0.0, 0.0);
                }
                var ms = (BlockTableRecord)tr.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForWrite);
                ms.AppendEntity(pline);
                tr.AddNewlyCreatedDBObject(pline, true);
                tr.Commit();
            }


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 3

Anonymous
Not applicable

Thank you very much.

0 Likes