Author Topic: Small Sample for selecting lines and reading coordinates  (Read 1325 times)

0 Members and 1 Guest are viewing this topic.

Offline Kevin

  • Newbie
  • *
  • Posts: 8
  • Karma: +0/-0
  • Gender: Male
    • prefered language: C
    • Prog expertise: Beginner
    • View Profile
Small Sample for selecting lines and reading coordinates
« on: October 09, 2012, 10:00:19 PM »
Hi everybody,

Im looking for a Sample for selecting all lines.
After that i want to read the coordinates.

Sorry im a noob, so could someone give me a link for solving my Problem, please.

Thank you!

Regards

Offline fixo

  • Full Member
  • ***
  • Posts: 135
  • Karma: +4/-0
  • Gender: Male
    • prefered language: C
    • Prog expertise: Good
    • View Profile
Re: Small Sample for selecting lines and reading coordinates
« Reply #1 on: October 12, 2012, 07:17:30 AM »
Hi Kevin, you may want to use this code,
mostly borrowed from Kean Walmsley:
Code: [Select]
        //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'~

Offline Kevin

  • Newbie
  • *
  • Posts: 8
  • Karma: +0/-0
  • Gender: Male
    • prefered language: C
    • Prog expertise: Beginner
    • View Profile
Re: Small Sample for selecting lines and reading coordinates
« Reply #2 on: October 12, 2012, 10:03:58 PM »
That is what I was looking for.
 
That will help me very much!

Thanks.

Offline fixo

  • Full Member
  • ***
  • Posts: 135
  • Karma: +4/-0
  • Gender: Male
    • prefered language: C
    • Prog expertise: Good
    • View Profile
Re: Small Sample for selecting lines and reading coordinates
« Reply #3 on: October 12, 2012, 10:45:32 PM »
Glad I could help
Happy coding :)