Author Topic: New to me Namespace, UIBindings  (Read 2124 times)

0 Members and 1 Guest are viewing this topic.

Jeff_M

  • King Gator
  • Posts: 4096
  • C3D user & customizer
New to me Namespace, UIBindings
« on: May 07, 2023, 01:24:08 PM »
Thanks to @gile's post HERE 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.  


gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: New to me Namespace, UIBindings
« Reply #1 on: May 07, 2023, 01:41:36 PM »
@Jeff_M UIBindings may be really usefull.
I used UIBindings.Collections.Layers in these code samples about user interfaces.
Speaking English as a French Frog

Jeff_M

  • King Gator
  • Posts: 4096
  • C3D user & customizer
Re: New to me Namespace, UIBindings
« Reply #2 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!

n.yuan

  • Bull Frog
  • Posts: 348
Re: New to me Namespace, UIBindings
« Reply #3 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.

Jeff_M

  • King Gator
  • Posts: 4096
  • C3D user & customizer
Re: New to me Namespace, UIBindings
« Reply #4 on: May 08, 2023, 02:42:40 PM »
Thanks for the clarification, Norman.