ApplicationExtension.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*-------------------------------------------------------------------------
  2. * 功能描述:ApplicationExtension
  3. * 作者:xulisong
  4. * 创建时间: 2019/3/12 11:30:00
  5. * 版本号:v1.0
  6. * -------------------------------------------------------------------------*/
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using Autodesk.Revit.DB;
  13. using Autodesk.Revit.UI;
  14. using Autodesk.Revit.UI.Selection;
  15. namespace FWindSoft.Revit
  16. {
  17. public static class ApplicationExtension
  18. {
  19. public static Selection GetSelection(this UIApplication app)
  20. {
  21. UIDocument doc = app.ActiveUIDocument;
  22. if (doc != null)
  23. return doc.Selection;
  24. return null;
  25. }
  26. public static void AddSelectElements(this UIApplication uiApp, List<Element> elements)
  27. {
  28. if (elements == null || elements.Count == 0)
  29. return;
  30. Selection selection = uiApp.GetSelection();
  31. if (selection == null) return;
  32. List<ElementId> idList = selection.GetElementIds().ToList();
  33. elements.ForEach(p => idList.Add(p.Id));
  34. selection.SetElementIds(idList);
  35. }
  36. public static void ClearSelectElements(this UIApplication uiApp)
  37. {
  38. Selection selection = uiApp.GetSelection();
  39. if (selection == null) return;
  40. selection.SetElementIds(new List<ElementId>());
  41. }
  42. public static void SelectElements(this UIApplication uiApp, List<Element> elements)
  43. {
  44. Selection selection = uiApp.GetSelection();
  45. if (selection == null) return;
  46. List<ElementId> idList = new List<ElementId>();
  47. elements.ForEach(p => idList.Add(p.Id));
  48. selection.SetElementIds(idList);
  49. }
  50. public static void SetShowElements(this UIApplication uiApp, List<Element> elements)
  51. {
  52. if (elements == null|| elements.Count==0)
  53. return;
  54. ElementSet set = new ElementSet();
  55. elements.ForEach(e => set.Insert(e));
  56. uiApp.ActiveUIDocument.ShowElements(set);
  57. uiApp.SelectElements(elements);
  58. }
  59. }
  60. }