Found in my codes see if this working good for you
// using System.Linq;
// using System.IO;
[CommandMethod("pointstocsv","cssp", CommandFlags.UsePickSet )]
static public void testWritePoints()
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database db=doc.Database;
Editor ed = doc.Editor;
Point3dCollection pts = new Point3dCollection();
Point3d pt=new Point3d();
PromptPointOptions ppo = new PromptPointOptions("\nPick point (or hit Enter to Exit loop): ");
ppo.AllowNone = true;
PromptPointResult res;
do
{
res = ed.GetPoint(ppo);
ed.WriteMessage("\n" + res.Status.ToString());
if (res.Status == PromptStatus.OK)
pt = res.Value;
pts.Add(pt);
} while (res.Status == PromptStatus.OK);
string points = PointsToCsv(pts, ";", db.Dimdec);// change number of decimals here, I'm using drawing decimals variable
WriteToTextFile(@"huh.csv", points);// change full path name here
}
public static string DoublesToCsv(List<List<double>> myList, string del)
{
StringBuilder sb = new StringBuilder();
myList.ForEach(x => sb.Append(string.Join(del, x.Select(a => a.ToString()).ToArray()) + "\n"));
return sb.ToString();
}
/// <summary>
/// Convert Point3dCollection to csv text
/// </summary>
/// <param name="ptlist">points</param>
/// <param name="sep">delimiter</param>
/// <param name="prec">precision</param>
/// <returns></returns>
public static string PointsToCsv(Point3dCollection ptlist, string sep, int prec)
{
StringBuilder sb = new StringBuilder();
string pc = ":f" + prec.ToString();
foreach(Point3d pt in ptlist)
sb.Append(string.Format("{0" + pc + "}" + sep + "{1" + pc + "}" + sep + "{2" + pc + "}\n", pt.X, pt.Y, pt.Z));
return sb.ToString();
}
public static void WriteToTextFile(string fileName, string txt)//O'Reily
{
if (!File.Exists(fileName))
{
using (StreamWriter file = File.CreateText(fileName))
{
file.Write(txt);
}
}
else
{
using (StreamWriter file = File.AppendText(fileName))
{
file.Write(txt);
}
}
}