Author Topic: Write polyline points to CSV file  (Read 765 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
Write polyline points to CSV file
« on: February 19, 2013, 04:37:10 PM »
Code: [Select]
        [CommandMethod("PolylineToCSV", "plcsv",CommandFlags.Modal | CommandFlags.UsePickSet)]
        public static void ExportPolyPointsToCSV()
        {
            Document doc =Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;

            Database db = doc.Database;

            Editor ed = doc.Editor;

            try
            {

                using (Transaction tr = db.TransactionManager.StartOpenCloseTransaction())
                {
                    string dec = ((short)Autodesk.AutoCAD.ApplicationServices.Application.GetSystemVariable("LUPREC")).ToString();

                    PromptSelectionOptions pso = new PromptSelectionOptions();

                    pso.MessageForRemoval = "\n >>  Nothing selected....";

                    pso.MessageForAdding = "\n  >>  Select a single polyline >> ";

                    pso.AllowDuplicates = false;

                    pso.SingleOnly = true;

                    SelectionFilter sf = new SelectionFilter
                        (new TypedValue[] { new TypedValue(0, "lwpolyline") });

                    PromptSelectionResult res = ed.GetSelection(pso, sf);

                    if (res.Status != PromptStatus.OK)  return;

                    StringBuilder sb = new StringBuilder();
                    ObjectId[] ids = res.Value.GetObjectIds();

                    Polyline poly = (Polyline)tr.GetObject(ids[0], OpenMode.ForRead, false);
                    for (int i = 0; i < poly.NumberOfVertices; i++)
                    {
                        Point2d pt = poly.GetPoint2dAt(i);
                        string vexstr = string.Format("{0:f" + dec + "};{1:f" + dec + "}", pt.X, pt.Y);
                        sb.AppendLine(vexstr);
                    }
                    String csvFile = String.Empty;

                    System.Windows.Forms.SaveFileDialog sfd =  new System.Windows.Forms.SaveFileDialog();
                    sfd.ValidateNames = true;
                    sfd.Title = "Save polyline vertices to CSV file";
                    sfd.DefaultExt = ".csv";
                    sfd.InitialDirectory=@"C:\Test\";
                    sfd.RestoreDirectory = true;
                   
                    if (sfd.ShowDialog() != System.Windows.Forms.DialogResult.OK)  return;

                    csvFile = sfd.FileName;

                    // write point to defined file
                    using (StreamWriter sw = new StreamWriter(csvFile))
                    {
                        sw.Write(sb.ToString());

                        sw.Flush();
                    }
                    sfd.Dispose();// non resident object, so kill 'em

                    tr.Commit();
                }
           }
            catch (Autodesk.AutoCAD.Runtime.Exception ex)
            {
                ed.WriteMessage("\n" + ex.Message + "\n" + ex.Source + "\n" + ex.StackTrace);
            }
        }