UIDocumentExtension.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*-------------------------------------------------------------------------
  2. * 功能描述:UIDocumentExtension
  3. * 作者:xulisong
  4. * 创建时间: 2019/5/23 11:22:36
  5. * 版本号:v1.0
  6. * -------------------------------------------------------------------------*/
  7. using Autodesk.Revit.DB;
  8. using Autodesk.Revit.UI;
  9. using Autodesk.Revit.UI.Selection;
  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
  16. {
  17. /// <summary>
  18. /// UIDocument 相关扩展信息
  19. /// </summary>
  20. public static class UIDocumentExtension
  21. {
  22. /// <summary>
  23. /// 获取当前选中元素
  24. /// </summary>
  25. /// <param name="uiDocument"></param>
  26. /// <returns></returns>
  27. public static Element GetSelectedElement(this UIDocument uiDocument)
  28. {
  29. Element selected = null;
  30. UIDocument doc = uiDocument;
  31. var useId = doc.Selection.GetElementIds().FirstOrDefault();
  32. if (useId != null)
  33. {
  34. selected = doc.Document.GetElement(useId);
  35. }
  36. return selected;
  37. }
  38. /// <summary>
  39. /// 获取选中元素
  40. /// </summary>
  41. /// <param name="uiDocument"></param>
  42. /// <returns></returns>
  43. public static List<Element> GetSelectedElements(this UIDocument uiDocument)
  44. {
  45. List<Element> selectedElements = new List<Element>();
  46. UIDocument uiDoc = uiDocument;
  47. var useIds = uiDoc.Selection.GetElementIds();
  48. if (useIds!=null)
  49. {
  50. var doc = uiDoc.Document;
  51. selectedElements = useIds.Select(id=>doc.GetElement(id)).ToList();
  52. }
  53. return selectedElements;
  54. }
  55. }
  56. }