当前位置:   article > 正文

AutoCAD .net 开发 SelectionFilter Foreach Linq 性能比较

cad selectionfilter
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Diagnostics;
  7. using Autodesk.AutoCAD.Runtime;
  8. using Autodesk.AutoCAD.DatabaseServices;
  9. using Autodesk.AutoCAD.ApplicationServices;
  10. using Autodesk.AutoCAD.EditorInput;
  11. /// <summary>
  12. /// 选择集比较
  13. /// </summary>
  14. ///
  15. [assembly: CommandClass(typeof(TipsAndTricks.Class1))]
  16. namespace TipsAndTricks
  17. {
  18. public class Class1
  19. {
  20. [CommandMethod("Test")]
  21. public void test()
  22. {
  23. DoFilter(FilterByEdSelectionFilter);
  24. DoFilter(FilterByLinq);
  25. DoFilter(FilterByForeach);
  26. }
  27. private static void DoFilter(Action action)
  28. {
  29. var ed = Application.DocumentManager.MdiActiveDocument.Editor;
  30. var sw = Stopwatch.StartNew();
  31. action();
  32. sw.Stop();
  33. ed.WriteMessage($"{sw.Elapsed.TotalMilliseconds}\n");
  34. }
  35. public void FilterByEdSelectionFilter()
  36. {
  37. var ed = Application.DocumentManager.MdiActiveDocument.Editor;
  38. TypedValue[] circType = new TypedValue[1];
  39. circType.SetValue(new TypedValue((int)DxfCode.Start, "CIRCLE"), 0);
  40. SelectionFilter filtCirc = new SelectionFilter(circType);
  41. PromptSelectionResult resPrompt;
  42. resPrompt = ed.SelectAll(filtCirc);
  43. if (resPrompt.Status == PromptStatus.OK)
  44. {
  45. SelectionSet circSelSet = resPrompt.Value;
  46. ed.WriteMessage($"ed count: {circSelSet.Count.ToString()}\n");//vs2015 新语法 可以正常编译.net 4.0的项目
  47. }
  48. }
  49. public void FilterByLinq()
  50. {
  51. var db = HostApplicationServices.WorkingDatabase;
  52. var ed = Application.DocumentManager.MdiActiveDocument.Editor;
  53. using (var tr = db.TransactionManager.StartTransaction())
  54. {
  55. var bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
  56. var mspace = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead, false) as BlockTableRecord;
  57. var circles = mspace.Cast<ObjectId>()
  58. .Where(id => id.ObjectClass.DxfName.Equals("CIRCLE")); //过滤条件很关键,对性能影响很大
  59. ed.WriteMessage($"linq count: {circles.Count().ToString()}\n");
  60. }
  61. }
  62. public void FilterByForeach()
  63. {
  64. var db = HostApplicationServices.WorkingDatabase;
  65. Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
  66. using (var tr = db.TransactionManager.StartTransaction())
  67. {
  68. var bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
  69. var mspace = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead, false) as BlockTableRecord;
  70. var circles = new List<ObjectId>();
  71. foreach (ObjectId id in mspace)
  72. {
  73. var dbObj = tr.GetObject(id, OpenMode.ForRead) as Circle;
  74. if (dbObj != null)
  75. {
  76. circles.Add(dbObj.Id);
  77. }
  78. }
  79. ed.WriteMessage($"Foreach count: {circles.Count.ToString()}\n"); }
  80. }
  81. }
  82. }

 

 

目前看来 linq 效率挺高的,以后开发可以放心用linq做二次开发了.

------------------------

4m的dwg 图纸 测试结果:  

ed count: 877
96.0967
linq count: 877
67.8107
Foreach count: 877
156.6604

转载于:https://www.cnblogs.com/DaLiao/p/6041225.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/114271
推荐阅读
相关标签
  

闽ICP备14008679号