Code Red > .NET

New to me Namespace, UIBindings

(1/1)

Jeff_M:
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#: ---        public static List<string> GetNamesOfLayers(this Database db, bool includeFrozen, bool includeOff, bool includeXref, Transaction tr)        {            List<string> layerNames = new List<string>();            LayerTable lt = (LayerTable)tr.GetObject(db.LayerTableId, OpenMode.ForRead);            foreach (ObjectId layerId in lt)            {                LayerTableRecord layer = (LayerTableRecord)tr.GetObject(layerId, OpenMode.ForRead);                if (includeXref || !layer.IsDependent)                    if ((includeOff || !layer.IsOff) && (includeFrozen || !layer.IsFrozen))                        layerNames.Add(layer.Name);            }            layerNames.Sort(new ComparerHyphen());            return layerNames;        } 
New method:

--- Code - C#: ---        public static List<string> GetNamesOfLayers(this Database db, bool includeFrozen, bool includeOff, bool includeXref)        {            var itemCollection = Application.UIBindings.Collections.Layers;            var names = itemCollection                .Where(s => (bool)s.GetProperties()["IsFrozen"].GetValue(s) == false || includeFrozen)                .Where(s => (bool)s.GetProperties()["IsOff"].GetValue(s) == false || includeOff)                .Where(s => (bool)s.GetProperties()["IsDependent"].GetValue(s) == false || includeXref)                .Select(s => s.GetProperties()["Name"].GetValue(s).ToString())                .OrderBy(name => name, new ComparerHyphen());            return names.ToList();        }  

gile:
@Jeff_M UIBindings may be really usefull.
I used UIBindings.Collections.Layers in these code samples about user interfaces.

Jeff_M:
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:
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:
Thanks for the clarification, Norman.

Navigation

[0] Message Index

Go to full version