Quick way to export polyline coordinates into .CSV file
using System;
using System.Text;
using System.IO;
using System.Collections;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using acadApp = Autodesk.AutoCAD.ApplicationServices.Application;
---------------------------------------
[CommandMethod("exps")]
static public void ExportCoordinates()
{
Document doc = acadApp.DocumentManager.MdiActiveDocument;
Database db = HostApplicationServices.WorkingDatabase;
Editor ed = doc.Editor;
string path = @"C:\PolyCoords.csv";
string carret = Environment.NewLine;
//set text delimiter
string sep = ",";
StringBuilder sb = new StringBuilder();
// set counter
int cnt = 0;
PromptEntityOptions opt = new PromptEntityOptions(
"\nSelect a polyline object: ");
opt.SetRejectMessage("\nObject must be LWPolyline, Polyline2d or Polyline3d only.");
opt.AddAllowedClass(typeof(Polyline), false);
opt.AddAllowedClass(typeof(Polyline2d), false);
opt.AddAllowedClass(typeof(Polyline3d), false);
PromptEntityResult res = ed.GetEntity(opt);
if (res.Status != PromptStatus.OK)
return;
//set precision
int prec = 3;
PromptIntegerOptions pio = new PromptIntegerOptions(
"\nEnter number of decimals <3>: " );
pio.AllowNone = true;
PromptIntegerResult pir =ed.GetInteger(pio);
if (pir.Status != PromptStatus.None &&
pir.Status != PromptStatus.OK)
return;
if (pir.Status == PromptStatus.OK)
prec = pir.Value;
Transaction tr = db.TransactionManager.StartTransaction();
using (tr)
{
DBObject obj = tr.GetObject(res.ObjectId, OpenMode.ForRead);
ed.WriteMessage("\n >>> {0}", obj.GetRXClass().Name);
if (obj == null)
return;
sb.Append("Point Number,X,Y,Z" + carret);
switch (obj.GetRXClass().Name)
{
case "AcDbPolyline":
{
Polyline lwpoly = obj as Polyline;
if (lwpoly != null)
{
for (int i = 0; i < lwpoly.NumberOfVertices; i++)
{
cnt += 1;
Point3d pt = lwpoly.GetPoint3dAt(i);
ed.WriteMessage("\nX = {0}; Y = {1}; Z = {2}", pt.X, pt.Y, pt.Z);
sb.Append(cnt.ToString() + sep +
Math.Round(pt.X, prec).ToString() + sep +
Math.Round(pt.Y, prec).ToString() + sep +
Math.Round(pt.Z, prec).ToString() + carret);
}
}
break;
}
case "AcDb2dPolyline":
{
Polyline2d poly2d = obj as Polyline2d;
if (poly2d != null)
{
foreach (ObjectId ix in poly2d)
{
cnt += 1;
Vertex2d vex = (Vertex2d)ix.GetObject(OpenMode.ForRead);
Point3d pt = vex.Position;
ed.WriteMessage("\nX = {0}; Y = {1}; Z = {2}", pt.X, pt.Y, pt.Z);
sb.Append(cnt.ToString() + sep +
Math.Round(pt.X, prec).ToString() + sep +
Math.Round(pt.Y, prec).ToString() + sep +
Math.Round(pt.Z, prec).ToString() + carret);
}
}
break;
}
case "AcDb3dPolyline":
{
Polyline3d poly3d = obj as Polyline3d;
if (poly3d != null)
{
foreach (ObjectId ix in poly3d)
{
cnt += 1;
PolylineVertex3d vex = (PolylineVertex3d)ix.GetObject(OpenMode.ForRead);
Point3d pt = vex.Position;
ed.WriteMessage("\nX = {0}; Y = {1}; Z = {2}", pt.X, pt.Y, pt.Z);
sb.Append(cnt.ToString() + sep +
Math.Round(pt.X, prec).ToString() + sep +
Math.Round(pt.Y, prec).ToString() + sep +
Math.Round(pt.Z, prec).ToString() + carret);
}
}
break;
}
default:
break;
}
System.IO.StreamWriter sw = new StreamWriter(path);
using (sw)
{
sw.Write(sb.ToString());
}
tr.Commit();
}
}
~'J'~