TheSwamp

Code Red => .NET => Topic started by: nobody on August 15, 2017, 09:38:42 PM

Title: Is there a ObjectIdCollection one .Combine with ObjectIdCollection Two ?
Post by: nobody on August 15, 2017, 09:38:42 PM
Want to combine two ObjectIdCollections ... only what to iterate through each objectid and add to the second collection? Suppose I could create lists but just wondering more out of curiosity than anything. 

Thanks!
Title: Re: Is there a ObjectIdCollection one .Combine with ObjectIdCollection Two ?
Post by: gile on August 16, 2017, 03:49:41 AM
Hi,

There is no built-in method for combining / concatenating two ObjectIdCollections.
For my part, I avoid using ObjectIdCollection except when the AutoCAD API requires it.
Generic collections (List <Objectid>, HashSet <ObjectId>, ...) are more powerful and more flexible.

You can create an extension method to mimic the List<T>.AddRange() method:
Code - C#: [Select]
  1.     public static class Extension
  2.     {
  3.         public static void AddRange(this ObjectIdCollection ids, ObjectIdCollection otherIds)
  4.         {
  5.             foreach (ObjectId id in otherIds) ids.Add(id);
  6.         }
  7.     }

By my side, I'd rather use Linq extension methods which return a new IEnumerable<ObjectId> so that the original collections remain unchanged.
Code - C#: [Select]
  1.     public static class Extension
  2.     {
  3.         public static IEnumerable<ObjectId> Concat(this ObjectIdCollection ids, IEnumerable<ObjectId> otherIds)
  4.         {
  5.             return ids.Cast<ObjectId>().Concat(otherIds);
  6.         }
  7.  
  8.         public static IEnumerable<ObjectId> Concat(this ObjectIdCollection ids, ObjectIdCollection otherIds)
  9.         {
  10.             return ids.Concat(otherIds.Cast<ObjectId>());
  11.         }
  12.     }

Then, if needed, you can easily convert the returned IEnumerable<objectId> into a concrete collection:

Code - C#: [Select]
  1. List<ObjectId> ids3 = ids1.Concat(ids2).ToList();

Code - C#: [Select]
  1. ObjectIdCollection ids4 = new ObjectIdCollection(ids1.Concat(ids2).ToArray());
Title: Re: Is there a ObjectIdCollection one .Combine with ObjectIdCollection Two ?
Post by: nobody on August 17, 2017, 10:26:39 PM
wow...thank you :) Looks super efficient.
Title: Re: Is there a ObjectIdCollection one .Combine with ObjectIdCollection Two ?
Post by: CADbloke on August 17, 2017, 10:42:47 PM
I think the functionality you are looking for is "Union" - you want to combine the ObjectIdCollections without introducing duplicates.

https://msdn.microsoft.com/en-us/library/bb341731(v=vs.110).aspx

ObjectIdCollection doesn't have it but Linq does so you would need to write it yourself. Perhaps break out the collections into List<>, Union that and then rebuild the ObjectIdCollection

The other way is to convert the ObjectIdCollections into HashTables and then combine those.
Title: Re: Is there a ObjectIdCollection one .Combine with ObjectIdCollection Two ?
Post by: gile on August 18, 2017, 01:02:00 AM
Hi,

If you want to remove duplicates, you can use Union (as CADbloke suggested) instead of Concat in the upper Linq extension methods.

Code - C#: [Select]
  1.         public static IEnumerable<ObjectId> Union(this ObjectIdCollection ids, IEnumerable<ObjectId> otherIds) =>
  2.             ids.Cast().Union(otherIds);
  3.  
  4.         public static IEnumerable<ObjectId> Union(this ObjectIdCollection ids, ObjectIdCollection otherIds) =>
  5.             ids.Union(otherIds.Cast());
Title: Re: Is there a ObjectIdCollection one .Combine with ObjectIdCollection Two ?
Post by: nobody on August 18, 2017, 09:36:59 PM
Thanks again :)