SelectionFilter.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*-------------------------------------------------------------------------
  2. * 功能描述:SelectionFilter
  3. * 作者:xulisong
  4. * 创建时间: 2019/5/23 14:14:35
  5. * 版本号:v1.0
  6. * -------------------------------------------------------------------------*/
  7. using Autodesk.Revit.DB;
  8. using Autodesk.Revit.UI.Selection;
  9. using FWindSoft.Revit.Extension;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. namespace FWindSoft.Revit.Common
  16. {
  17. public class SelectionFilter : ISelectionFilter
  18. {
  19. private Predicate<Element> m_AllowElement;
  20. private Func<Reference,XYZ,bool> m_AllowReference;
  21. public SelectionFilter(Predicate<Element> allowElement, Func<Reference, XYZ, bool> allowReference)
  22. {
  23. m_AllowElement = allowElement;
  24. m_AllowReference = allowReference;
  25. }
  26. public bool AllowElement(Element elem)
  27. {
  28. if (m_AllowElement != null)
  29. {
  30. return m_AllowElement.Invoke(elem);
  31. }
  32. return true;
  33. }
  34. public bool AllowReference(Reference reference, XYZ position)
  35. {
  36. if (m_AllowReference != null)
  37. {
  38. return m_AllowReference.Invoke(reference,position);
  39. }
  40. return true;
  41. }
  42. }
  43. /// <summary>
  44. /// 类型选择过滤器
  45. /// </summary>
  46. public class TypeSelectionFilter : ISelectionFilter
  47. {
  48. private Type m_Type;
  49. public TypeSelectionFilter(Type type)
  50. {
  51. m_Type = type;
  52. }
  53. public bool AllowElement(Element elem)
  54. {
  55. return elem.GetType() == m_Type;
  56. }
  57. public bool AllowReference(Reference reference, XYZ position)
  58. {
  59. return true;
  60. }
  61. }
  62. /// <summary>
  63. /// 类别过滤器
  64. /// </summary>
  65. public class CategorySelectionFilter : ISelectionFilter
  66. {
  67. private BuiltInCategory m_BuiltInCategory;
  68. public CategorySelectionFilter(BuiltInCategory builtInCategory)
  69. {
  70. m_BuiltInCategory= builtInCategory;
  71. }
  72. public bool AllowElement(Element elem)
  73. {
  74. var bit = elem.GetCategory();
  75. return bit== m_BuiltInCategory;
  76. }
  77. public bool AllowReference(Reference reference, XYZ position)
  78. {
  79. return true;
  80. }
  81. }
  82. /// <summary>
  83. /// 多类别过滤器
  84. /// </summary>
  85. public class CategoriesSelectionFilter : ISelectionFilter
  86. {
  87. private readonly List<BuiltInCategory> m_Categories = new List<BuiltInCategory>();
  88. public CategoriesSelectionFilter(BuiltInCategory bic)
  89. {
  90. m_Categories.Add(bic);
  91. }
  92. public CategoriesSelectionFilter(List<BuiltInCategory> categories)
  93. {
  94. m_Categories.AddRange(categories??new List<BuiltInCategory>());
  95. }
  96. public bool AllowElement(Element elem)
  97. {
  98. if (!m_Categories.Any())
  99. {
  100. return false;
  101. }
  102. var useCategory = elem.GetCategory();
  103. return m_Categories.Any(p => p == useCategory);
  104. }
  105. public bool AllowReference(Reference reference, XYZ position)
  106. {
  107. return true;
  108. }
  109. }
  110. }