Author Topic: renumber points  (Read 1375 times)

0 Members and 1 Guest are viewing this topic.

A-SABER

  • Mosquito
  • Posts: 11
renumber points
« 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

MickD

  • King Gator
  • Posts: 3520
  • (x-in)->[process]->(y-out)
Re: renumber points
« Reply #1 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?
Forth is like the Tao: it is a Way, and is realized when followed.
Its fragility is its strength; its simplicity is its direction - Michael Ham

"The E in Javascript stands for 'easy'." Florin Pop tweet

gile

  • Water Moccasin
  • Posts: 2409
  • Marseille, France
Re: renumber points
« Reply #2 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);

« Last Edit: January 31, 2021, 03:29:31 AM by gile »
Speaking English as a French Frog

A-SABER

  • Mosquito
  • Posts: 11
Re: renumber points
« Reply #3 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

A-SABER

  • Mosquito
  • Posts: 11
Re: renumber points
« Reply #4 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