TheSwamp

Code Red => .NET => Topic started by: sdunn on July 14, 2020, 10:16:43 PM

Title: Sorting Civil 3D CogoPoints
Post by: sdunn on July 14, 2020, 10:16:43 PM
I am trying to select points from a drawing, sort them by northing and then by easting so that I can renumber them from north to south and then by west to east.  I want to do this as efficiently as possible and I think linq would be the best way assuming I can work with a List<CogoPoint> in this manner.

This doesn't work yet.  Is there a better way to do this or a different path I should take?
Code - C#: [Select]
  1.             using (Transaction tr = db.TransactionManager.StartTransaction())
  2.             {
  3.                 // ' If the prompt status is OK, objects were selected
  4.                 if (acSSPrompt.Status == PromptStatus.OK)
  5.                 {
  6.                     ObjectIdCollection idcoll = new ObjectIdCollection(acSSPrompt.Value.GetObjectIds());
  7.                    
  8.                     foreach (ObjectId OID in idcoll)
  9.                     {
  10.                         CogoPoint p = (OID.GetObject(OpenMode.ForRead) as CogoPoint);
  11.                         SelectedPoints.Append<CogoPoint>(p);
  12.                     }
  13.                    var sortresult = SelectedPoints.OrderByDescending(p.Northing).ThenByDescending(p.Easting);
  14.  
  15.                     //Renumber the points here
  16.  
  17.                    tr.Commit();
  18.                 }
  19.                 else
  20.                 {
  21.                 }
  22.             }//end using
  23.  

Thank you,
Stacy
Title: Re: Sorting Civil 3D CogoPoints
Post by: MickD on July 15, 2020, 02:45:58 AM
Have you tried

Code - C#: [Select]
  1. var sortresult = SelectedPoints.OrderByDescending(p => p.Northing).ThenByDescending(p => p.Easting);

You usually need a Lambda expression with Linq that takes the object then gets the object property eg. object => object.Property

You might also want to tack on 'ToList()' to get back an ordered 'List' rather than an IOrderedEnumerable.

hth

Title: Re: Sorting Civil 3D CogoPoints
Post by: sdunn on July 15, 2020, 02:09:06 PM
Yes, that is what I was missing.  Thank you!
Title: Re: Sorting Civil 3D CogoPoints
Post by: MickD on July 15, 2020, 05:03:14 PM
no problemo :)