Author Topic: Create 3d polyline from points  (Read 1776 times)

0 Members and 1 Guest are viewing this topic.

Offline fixo

  • Full Member
  • ***
  • Posts: 135
  • Karma: +4/-0
  • Gender: Male
    • prefered language: C
    • Prog expertise: Good
    • View Profile
Create 3d polyline from points
« on: January 12, 2013, 11:03:35 PM »
Code: [Select]
        [CommandMethod("c3p")]
        static public void Create3dPolyline()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            Editor ed = doc.Editor;

         Transaction tr = db.TransactionManager.StartTransaction();

            using (tr)
            {
                // Get blocktable and modelspace (for write)
                BlockTable bt =  (BlockTable)tr.GetObject(   db.BlockTableId, OpenMode.ForRead,  false);

                BlockTableRecord btr =(BlockTableRecord)tr.GetObject( bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite, false );
               
                // Create a 3D polyline with 6 segments (closed)
                Point3d[] pts = new Point3d[]
                        { new Point3d(0,0,0),
                          new Point3d(60,0,0),
                         new Point3d(60,0,60),
                         new Point3d(60,60,60),
                         new Point3d(0,60,60),
                         new Point3d(0,0,60)
                        };
                Point3dCollection points = new Point3dCollection(pts);

                Polyline3d poly = new Polyline3d();
                // First add polyline to model space and transaction
                btr.AppendEntity(poly);

                tr.AddNewlyCreatedDBObject(poly, true);
                // Then add all vertices to polyline from point collection
                foreach (Point3d pt in points)
                {
                    // Now create the vertices

                    PolylineVertex3d vex3d = new PolylineVertex3d(pt);

                    // And add them to the 3dpoly (this adds them to the db also)

                    poly.AppendVertex(vex3d);

                    tr.AddNewlyCreatedDBObject(vex3d, true);
                   
                }
                // Make polyline closed
                poly.Closed = true;
                // Change color
                poly.ColorIndex = 14;
                // Commit transaction
                tr.Commit();

            }
        }