Code Red > .NET

create list from selection

<< < (2/3) > >>

gile:
Please, provide a drawing sample.

mrbeann:

--- Quote from: gile on August 23, 2021, 03:07:22 AM ---Please, provide a drawing sample.

--- End quote ---
Thank you sir.

gile:
That's what I was afraid of, the input data is poorly structured. It would have been so much easier with an AutoCAD table...
Automating a poorly organized drawing is the way to the GIGO syndrome.
Nevertheless, here is an attempt that uses LINQ and seems to give correct results.


--- Code - C#: ---        static string[][] GroupTextsByRow(SelectionSet selection, double rowHeight)        {            double roundToRowHeight(double d) =>                Math.Round(d / rowHeight) * rowHeight;            Point3d textPosition(DBText txt) =>                txt.Justify == AttachmentPoint.BaseLeft ? txt.Position : txt.AlignmentPoint;            using (var tr = new OpenCloseTransaction())            {                return selection.GetObjectIds()                    .Where(id => id.ObjectClass.DxfName == "TEXT")                    .Select(id => (DBText)tr.GetObject(id, OpenMode.ForRead))                    .GroupBy(txt => roundToRowHeight(textPosition(txt).Y))                    .OrderByDescending(grp => grp.Key)                    .Select(grp => grp.OrderBy(txt => textPosition(txt).X)                                      .Select(txt => txt.TextString)                                      .ToArray())                    .ToArray();            }        }
A testing command:

--- Code - C#: ---        [CommandMethod("TEST")]        public static void Test()        {            var doc = Application.DocumentManager.MdiActiveDocument;            var db = doc.Database;            var ed = doc.Editor;            var filter = new SelectionFilter(new[] { new TypedValue(0, "TEXT") });            var psr = ed.GetSelection(filter);            if (psr.Status != PromptStatus.OK)                return;            var pdr = ed.GetDistance("\nRow height: ");            if (pdr.Status != PromptStatus.OK)                return;            var rows = GroupTextsByRow(psr.Value, pdr.Value);            foreach (var row in rows)            {                ed.WriteMessage($"\n[{string.Join(", ", row)}]");            }        }

mrbeann:
Thank you Gile but I get error.

gile:
These are local functions using the lambda operator.
These features came with C#7 (VS 2017) for the local functions and C#6 (VS2015) for the lambda operator.
Which version of Visual Studio are you using?

If you absolutely want to use an older version of Visual Studio you can define 'old style' methods.

--- Code - C#: ---        static double RoundToRowHeight(double d, double rowHeight)        {            return Math.Round(d / rowHeight) * rowHeight;        }         static Point3d TextPosition(DBText txt)        {            return txt.Justify == AttachmentPoint.BaseLeft ?                 txt.Position : txt.AlignmentPoint;        }         static string[][] GroupTextsByRow(SelectionSet selection, double rowHeight)        {            using (var tr = new OpenCloseTransaction())            {                return selection.GetObjectIds()                    .Where(id => id.ObjectClass.DxfName == "TEXT")                    .Select(id => (DBText)tr.GetObject(id, OpenMode.ForRead))                    .GroupBy(txt => RoundToRowHeight(TextPosition(txt).Y, rowHeight))                    .OrderByDescending(grp => grp.Key)                    .Select(grp => grp.OrderBy(txt => TextPosition(txt).X)                                      .Select(txt => txt.TextString)                                      .ToArray())                    .ToArray();            }        }

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version