TheSwamp

Code Red => .NET => Topic started by: A-SABER on January 30, 2021, 06:51:07 PM

Title: renumber points
Post by: A-SABER on January 30, 2021, 06:51:07 PM
this code makes an automatic number for points, but I need to control arrange points number start from the top left side
and how can sort points collection according to position x


Code - C#: [Select]
  1. [ [CommandMethod("numberpoint")]
  2.         public void tpointt()
  3.         {
  4.             Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
  5.             Database db = doc.Database;
  6.             Editor ed = doc.Editor;
  7.             using (Transaction trans = db.TransactionManager.StartTransaction())
  8.             {
  9.  
  10.                 BlockTable bt = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
  11.                 BlockTableRecord btr = trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
  12.                 int num = 1;
  13.                 TypedValue[] tv = new TypedValue[1];
  14.                 tv.SetValue(new TypedValue((int)DxfCode.Start, "point"), 0);
  15.                 SelectionFilter filter = new SelectionFilter(tv);
  16.                 PromptSelectionResult ssPrompt;
  17.                 ssPrompt = ed.GetSelection(filter);
  18.                 if (ssPrompt.Status == PromptStatus.OK)
  19.                 {
  20.                     SelectionSet ss = ssPrompt.Value;
  21.                     foreach (SelectedObject sObj in ss)
  22.                     {
  23.                         DBPoint spoint = trans.GetObject(sObj.ObjectId, OpenMode.ForWrite) as DBPoint;
  24.                         DBText txt = new DBText();
  25.                         txt.SetDatabaseDefaults();
  26.                         txt.TextString = num.ToString();
  27.                         txt.Position = new Point3d(spoint.Position.X, spoint.Position.Y, spoint.Position.Z);
  28.                         txt.Rotation = 0;
  29.                         txt.Height = 2;
  30.                         btr.AppendEntity(txt);
  31.                         trans.AddNewlyCreatedDBObject(txt, true);
  32.                         num++;
  33.                     }
  34.  
  35.                     trans.Commit();
  36.                 }
  37.             }
  38.         }
  39.  
i attach pic for example
Title: Re: renumber points
Post by: MickD on January 30, 2021, 08:46:34 PM
just spit balling here but could you use Linq to sort the objects? Create a list of the actual point entities then sort them.
something like:
Code - C#: [Select]
  1. var sortedPointsList = pointsList.OrderBy(p => p.X).ThenBy(p => p.Y);
  2.  

then foreach over the new list to add your numbering?
Title: Re: renumber points
Post by: gile on January 31, 2021, 03:07:44 AM
Hi,
Assuming points are vertically and horizontally aligned, it seems to me it should be:
Code - C#: [Select]
  1. var sortedPointsList = pointsList.OrderByDescending(p => p.Y).ThenBy(p => p.X);

Title: Re: renumber points
Post by: A-SABER on February 02, 2021, 10:09:39 AM
just spit balling here but could you use Linq to sort the objects? Create a list of the actual point entities then sort them.
something like:
Code - C#: [Select]
  1. var sortedPointsList = pointsList.OrderBy(p => p.X).ThenBy(p => p.Y);
  2.  

then foreach over the new list to add your numbering?

thank you sir
Title: Re: renumber points
Post by: A-SABER on February 02, 2021, 10:11:27 AM
Hi,
Assuming points are vertically and horizontally aligned, it seems to me it should be:
Code - C#: [Select]
  1. var sortedPointsList = pointsList.OrderByDescending(p => p.Y).ThenBy(p => p.X);
thank you sir