TheSwamp

Code Red => .NET => Topic started by: A-SABER on February 14, 2021, 04:47:37 AM

Title: create list from selection
Post by: A-SABER on February 14, 2021, 04:47:37 AM
How to make a number of lists, from the selection?
for example, in the attached image after selecting all the texts, I need to make 7 lists,
according to the number of rows that exist.
To I can export all lists to the Excel file and each list in a separate column
.

Title: Re: create list from selection
Post by: nobody on February 18, 2021, 07:35:18 PM
It's actually pretty involved to do what you're asking.  I'd suggest starting with a smaller task and work your way up.

Figure out how to export the first row of the first column. That would be your first step. You'll use streamreader to write the data to a file, and add data to an existing file. Start with writing to a text file before diving into writing to excel.
Title: Re: create list from selection
Post by: mrbeann on August 22, 2021, 07:17:05 AM
I need help too. Gile please help!
Title: Re: create list from selection
Post by: gile on August 22, 2021, 10:58:35 AM
Hi,
I'm absolutely unable to understand the request.
Title: Re: create list from selection
Post by: mrbeann on August 22, 2021, 09:28:01 PM
Hi,
I'm absolutely unable to understand the request.
Get a selection of all (7) rows above and divides into (7) arrays (in a array of arrays) (each row sort from left to right).
Title: Re: create list from selection
Post by: gile on August 23, 2021, 03:07:22 AM
Please, provide a drawing sample.
Title: Re: create list from selection
Post by: mrbeann on August 23, 2021, 04:28:08 AM
Please, provide a drawing sample.
Thank you sir.
Title: Re: create list from selection
Post by: gile on August 23, 2021, 07:29:50 AM
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 (https://en.wikipedia.org/wiki/Garbage_in,_garbage_out) syndrome.
Nevertheless, here is an attempt that uses LINQ and seems to give correct results.

Code - C#: [Select]
  1.         static string[][] GroupTextsByRow(SelectionSet selection, double rowHeight)
  2.         {
  3.             double roundToRowHeight(double d) =>
  4.                 Math.Round(d / rowHeight) * rowHeight;
  5.             Point3d textPosition(DBText txt) =>
  6.                 txt.Justify == AttachmentPoint.BaseLeft ? txt.Position : txt.AlignmentPoint;
  7.             using (var tr = new OpenCloseTransaction())
  8.             {
  9.                 return selection.GetObjectIds()
  10.                     .Where(id => id.ObjectClass.DxfName == "TEXT")
  11.                     .Select(id => (DBText)tr.GetObject(id, OpenMode.ForRead))
  12.                     .GroupBy(txt => roundToRowHeight(textPosition(txt).Y))
  13.                     .OrderByDescending(grp => grp.Key)
  14.                     .Select(grp => grp.OrderBy(txt => textPosition(txt).X)
  15.                                       .Select(txt => txt.TextString)
  16.                                       .ToArray())
  17.                     .ToArray();
  18.             }
  19.         }

A testing command:
Code - C#: [Select]
  1.         [CommandMethod("TEST")]
  2.         public static void Test()
  3.         {
  4.             var doc = Application.DocumentManager.MdiActiveDocument;
  5.             var db = doc.Database;
  6.             var ed = doc.Editor;
  7.             var filter = new SelectionFilter(new[] { new TypedValue(0, "TEXT") });
  8.             var psr = ed.GetSelection(filter);
  9.             if (psr.Status != PromptStatus.OK)
  10.                 return;
  11.             var pdr = ed.GetDistance("\nRow height: ");
  12.             if (pdr.Status != PromptStatus.OK)
  13.                 return;
  14.             var rows = GroupTextsByRow(psr.Value, pdr.Value);
  15.             foreach (var row in rows)
  16.             {
  17.                 ed.WriteMessage($"\n[{string.Join(", ", row)}]");
  18.             }
  19.         }
Title: Re: create list from selection
Post by: mrbeann on August 24, 2021, 09:35:02 AM
Thank you Gile but I get error.
Title: Re: create list from selection
Post by: gile on August 24, 2021, 10:49:59 AM
These are local functions (https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-7.0/local-functions) using the lambda operator (https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/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#: [Select]
  1.         static double RoundToRowHeight(double d, double rowHeight)
  2.         {
  3.             return Math.Round(d / rowHeight) * rowHeight;
  4.         }
  5.  
  6.         static Point3d TextPosition(DBText txt)
  7.         {
  8.             return txt.Justify == AttachmentPoint.BaseLeft ?
  9.                 txt.Position : txt.AlignmentPoint;
  10.         }
  11.  
  12.         static string[][] GroupTextsByRow(SelectionSet selection, double rowHeight)
  13.         {
  14.             using (var tr = new OpenCloseTransaction())
  15.             {
  16.                 return selection.GetObjectIds()
  17.                     .Where(id => id.ObjectClass.DxfName == "TEXT")
  18.                     .Select(id => (DBText)tr.GetObject(id, OpenMode.ForRead))
  19.                     .GroupBy(txt => RoundToRowHeight(TextPosition(txt).Y, rowHeight))
  20.                     .OrderByDescending(grp => grp.Key)
  21.                     .Select(grp => grp.OrderBy(txt => TextPosition(txt).X)
  22.                                       .Select(txt => txt.TextString)
  23.                                       .ToArray())
  24.                     .ToArray();
  25.             }
  26.         }
Title: Re: create list from selection
Post by: mrbeann on August 25, 2021, 11:41:49 AM
This works like a charm. Thank you! (I use VS 2015).
But Can you help me with a similar problem? I want to select all blocks in rows and reorder into a list with some conditions. Drawing sample is attached below. I work for three weeks but I can't solve this with my low IQ :( Hope you can solve my problem. Thank you very much!
Title: Re: create list from selection
Post by: mrbeann on September 13, 2021, 11:30:46 PM
Gile please help me! Thank you very much.