DocumentExtension.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using Autodesk.Revit.DB;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace FWindSoft.Revit
  9. {
  10. public static class DocumentExtension
  11. {
  12. #region 过滤
  13. public static List<Element> GetElements(this Document doc, ElementFilter filter)
  14. {
  15. var collector = new FilteredElementCollector(doc);
  16. return collector.WherePasses(filter).ToList<Element>();
  17. }
  18. public static List<T> GetElements<T>(this Document doc) where T : Element
  19. {
  20. var collector = new FilteredElementCollector(doc);
  21. collector.OfClass(typeof(T));
  22. return collector.OfType<T>().ToList<T>();
  23. }
  24. public static List<T> GetElementTypes<T>(this Document doc, ElementFilter filter) where T : ElementType
  25. {
  26. var collector = new FilteredElementCollector(doc);
  27. collector.WherePasses(filter);
  28. collector.WhereElementIsElementType();
  29. collector.OfClass(typeof(T));
  30. return collector.OfType<T>().ToList<T>();
  31. }
  32. #endregion
  33. public static ElementType GetElementType(this Document doc, string typeName)
  34. {
  35. var types = doc.GetElements<ElementType>();
  36. return types.FirstOrDefault(type => type != null && type.Name == typeName);
  37. }
  38. }
  39. }