TheSwamp

Code Red => .NET => Topic started by: Jeff_M on May 07, 2023, 01:24:08 PM

Title: New to me Namespace, UIBindings
Post by: Jeff_M on May 07, 2023, 01:24:08 PM
Thanks to @gile's post HERE (http://www.theswamp.org/index.php?topic=31864.msg614243#msg614243) I learned something new that has been, apparently, around for quite some time. Not sure how I've missed it all these years. Well I did some messing around with it yesterday and found I could replace an extension method which has been in use for many years (c. 2008). Can anyone see any reason to not do this?

Old method:
Code - C#: [Select]
  1.         public static List<string> GetNamesOfLayers(this Database db, bool includeFrozen, bool includeOff, bool includeXref, Transaction tr)
  2.         {
  3.             List<string> layerNames = new List<string>();
  4.             LayerTable lt = (LayerTable)tr.GetObject(db.LayerTableId, OpenMode.ForRead);
  5.             foreach (ObjectId layerId in lt)
  6.             {
  7.                 LayerTableRecord layer = (LayerTableRecord)tr.GetObject(layerId, OpenMode.ForRead);
  8.                 if (includeXref || !layer.IsDependent)
  9.                     if ((includeOff || !layer.IsOff) && (includeFrozen || !layer.IsFrozen))
  10.                         layerNames.Add(layer.Name);
  11.             }
  12.             layerNames.Sort(new ComparerHyphen());
  13.             return layerNames;
  14.         }
  15.  

New method:
Code - C#: [Select]
  1.         public static List<string> GetNamesOfLayers(this Database db, bool includeFrozen, bool includeOff, bool includeXref)
  2.         {
  3.             var itemCollection = Application.UIBindings.Collections.Layers;
  4.             var names = itemCollection
  5.                 .Where(s => (bool)s.GetProperties()["IsFrozen"].GetValue(s) == false || includeFrozen)
  6.                 .Where(s => (bool)s.GetProperties()["IsOff"].GetValue(s) == false || includeOff)
  7.                 .Where(s => (bool)s.GetProperties()["IsDependent"].GetValue(s) == false || includeXref)
  8.                 .Select(s => s.GetProperties()["Name"].GetValue(s).ToString())
  9.                 .OrderBy(name => name, new ComparerHyphen());
  10.             return names.ToList();
  11.         }
  12.  
  13.  

Title: Re: New to me Namespace, UIBindings
Post by: gile on May 07, 2023, 01:41:36 PM
@Jeff_M UIBindings may be really usefull.
I used UIBindings.Collections.Layers in these code samples (https://gilecad.azurewebsites.net/UserInterfaces_en.aspx) about user interfaces.
Title: Re: New to me Namespace, UIBindings
Post by: Jeff_M on May 07, 2023, 03:05:36 PM
Thanks, Gile, for the memory jog. I actually bookmarked your examples a long time ago, before I decided to explore WPF, thinking it could be useful down the road. I guess I should look at my bookmarks more often!
Title: Re: New to me Namespace, UIBindings
Post by: n.yuan on May 08, 2023, 11:56:28 AM
Jeff, since you asked "any reason to not do this", here is a reason of "not": UIBindings can only be used with running AutoCAD session (because of acmgd.dll). If want your code can be also used in core console/Platform API service (former Forge Service), then you would keep the old good extension method in your base/shared code library. That is, only use UIBindings in the code base that is strictly used in plugins specifically running in a desktop AutoCAD session.
Title: Re: New to me Namespace, UIBindings
Post by: Jeff_M on May 08, 2023, 02:42:40 PM
Thanks for the clarification, Norman.