Hi Kevin, you may want to use this code,
mostly borrowed from Kean Walmsley:
//using System.IO;
private SelectionSet LineSelection(Editor ed)
{
// select lines on screen using filter
PromptSelectionOptions pso = new PromptSelectionOptions();
pso.MessageForRemoval = "\n Select lines only: ";
pso.MessageForAdding = "\nSelect lines: ";
pso.RejectObjectsOnLockedLayers = false;
TypedValue[] dxfcodes = new TypedValue[1] { new TypedValue(0, "LINE") };
SelectionFilter filter = new SelectionFilter(dxfcodes);
PromptSelectionResult psr = ed.GetSelection(pso, filter);
if (psr.Status != PromptStatus.OK)
return null;
return psr.Value;
}
[CommandMethod("LinesToFile", "ltf", CommandFlags.Modal | CommandFlags.UsePickSet)]
public void testLineSelection()
{
bool isOk = true;
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
List<Point3d[]> linedata = new List<Point3d[]>();
string txtname = (string)Autodesk.AutoCAD.ApplicationServices.Application.GetSystemVariable("dwgprefix");
string dwgname = (string)Autodesk.AutoCAD.ApplicationServices.Application.GetSystemVariable("dwgname");
dwgname = dwgname.Substring(0, dwgname.Length - 4) + ".csv";// <-- change on .txt if it's need
txtname = Path.Combine(txtname, dwgname);
try
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
SelectionSet sset = LineSelection(ed);
foreach (SelectedObject sobj in sset)
{
Line ln = tr.GetObject(sobj.ObjectId, OpenMode.ForRead) as Line;
if (ln != null)
{
Point3d p1 = ln.StartPoint;
Point3d p2 = ln.EndPoint;
Point3d[] pts = new Point3d[2];
pts[0] = p1;
pts[1] = p2;
linedata.Add(pts);
}
}
using (StreamWriter sw = new StreamWriter(txtname))
{
foreach (Point3d[] lnItem in linedata)
{
string txtline = string.Format("{0:f6}\t{1:f6}\t{2:f6}\t{3:f6}\t{4:f6}\t{5:f6}", lnItem[0].X, lnItem[0].Y, lnItem[0].Z, lnItem[1].X, lnItem[1].Y, lnItem[1].Z);
sw.WriteLine(txtline);
}
sw.Flush();
sw.Close();
}
tr.Commit();
}
}
catch (System.Exception ex)
{
isOk = false;
ed.WriteMessage(ex.Message + "\n" + ex.StackTrace);
}
finally
{
if (isOk)
{
ed.WriteMessage("\nProgram ended up with success\nSee record in:\n{0}", txtname);
}
else
{
ed.WriteMessage("\nProgram failed");
}
}
}
~'J'~