- 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>
- /// 选择集比较
- /// </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.MdiActiveDocument.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 新语法 可以正常编译.net 4.0的项目
- }
- }
-
-
- 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")); //过滤条件很关键,对性能影响很大
- ed.WriteMessage($"linq count: {circles.Count().ToString()}\n");
-
- }
- }
-
-
- public void FilterByForeach()
- {
- var db = HostApplicationServices.WorkingDatabase;
- 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"); }
- }
- }
- }
目前看来 linq 效率挺高的,以后开发可以放心用linq做二次开发了.
------------------------
4m的dwg 图纸 测试结果:
ed count: 877
96.0967
linq count: 877
67.8107
Foreach count: 877
156.6604