Using system;using system.collections.generic;using system.linq;using system.text;using System.Threading.Tasks; Using system.diagnostics;using autodesk.autocad.runtime;using autodesk.autocad.databaseservices;using Autodesk.autocad.applicationservices;using autodesk.autocad.editorinput;///<summary>///Selection Set comparison///</ summary>///[Assembly:commandclass (typeof (Tipsandtricks.class1))]namespace tipsandtricks{public class Class1 { [Commandmethod ("Test")] public void Test () {DoFilter (filterbyedselectionfilter); DoFilter (FILTERBYLINQ); DoFilter (Filterbyforeach); } private static void DoFilter (Action action) {var ed = Application.DocumentManager.MdiActiveDoc Ument. Editor; var sw = stopwatch.startnew (); Action (); Sw. Stop (); Ed. Writemessage ($ "{SW. Elapsed.totalmilliseconds}\n "); } public void Filterbyedselectionfilter () { var ed = Application.DocumentManager.MdiActiveDocument.Editor; typedvalue[] Circtype = new Typedvalue[1]; Circtype.setvalue (New Typedvalue ((int) Dxfcode.start, "CIRCLE"), 0); Selectionfilter Filtcirc = new Selectionfilter (circtype); Promptselectionresult resprompt; resprompt = ed. SelectAll (FILTCIRC); if (Resprompt.status = = Promptstatus.ok) {Selectionset circselset = Resprompt.value; Ed. Writemessage ($ "ed Count: {circSelSet.Count.ToString ()}\n"),//vs2015 new syntax can compile. NET 4.0 Project}} public void Filterbylinq () {var db = hostapplicationservices.workingdatabase; var ed = Application.DocumentManager.MdiActiveDocument.Editor; using (var tr = db). Transactionmanager.starttransaction ()) {var bt = tr. GetObject (db. Blocktableid, Openmode.forread) as blocktable; var mspace = tr. GetObject (Bt[blocktablerecord.modelspace], Openmode.forread, false) as Blocktablerecord; var circles = Mspace. Cast<objectid> (). Where (id = = ID. ObjectClass.DxfName.Equals ("CIRCLE")); Filtration conditions are critical and have a large impact on performance ed. Writemessage ($ "LINQ count: {circles. Count (). ToString ()}\n "); }} public void Filterbyforeach () {var db = Hostapplicationservices.workingdataba Se Editor ed = Application.DocumentManager.MdiActiveDocument.Editor; using (var tr = db). Transactionmanager.starttransaction ()) {var bt = tr. GetObject (db. Blocktableid, Openmode.forread) as blocktable; var mspace = tr. GetObject (Bt[blocktablerecord.modelspace], Openmode.forread, false) as Blocktablerecord; var circles = new list<objectid> (); foreach (ObjectId ID in mspace) {var dbobj = tr. GetoBject (ID, openmode.forread) as Circle; if (dbobj! = null) {circles. ADD (dbobj.id); }} ed. Writemessage ($ "Foreach count: {circles. Count.tostring ()}\n ");
} } }}
At present, LINQ is very efficient, and later development can be assured with LINQ two times development.
------------------------
DWG drawing test results for 4m:
Ed count:877
96.0967
LINQ count:877
67.8107
Foreach count:877
156.6604
AutoCAD. NET development Selectionfilter Foreach LINQ Performance Comparison