RibbonItemUtil.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*-------------------------------------------------------------------------
  2. * 功能描述:RibbonItemUtil
  3. * 作者:xulisong
  4. * 创建时间: 2019/3/8 9:13:09
  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 System.Windows.Media;
  13. using System.Windows.Media.Imaging;
  14. using Autodesk.Revit.UI;
  15. using FWindSoft.Wpf;
  16. namespace FWindSoft.Revit.Menu
  17. {
  18. /*
  19. * 具体实现功能和算法流程中的业务分开,就是常说的提炼出方法,这样以后代码变动容易修改,因为每次面对的实际改动代码
  20. * 量很小,而不用查看所有代码
  21. */
  22. public static class RibbonItemUtil
  23. {
  24. public static void AppendButtons(RibbonItem button,List<PushButtonData> datas)
  25. {
  26. var buttonDatas = datas;
  27. if (datas==null||!buttonDatas.Any())
  28. return;
  29. if (button is SplitButton splitButton)
  30. {
  31. buttonDatas.ForEach(d => splitButton.AddPushButton(d));
  32. return;
  33. }
  34. //这两个类处理顺序不能颠倒,他们是父子类关系
  35. if (button is PulldownButton pulldownButton)
  36. {
  37. //开头的第一个button数据,应该为PulldownButton的数据
  38. var first = datas.FirstOrDefault();
  39. var useDatas = new List<PushButtonData>(datas);
  40. if (first.Name == pulldownButton.Name)
  41. {
  42. useDatas.RemoveAt(0);
  43. }
  44. useDatas.ForEach(d => pulldownButton.AddPushButton(d));
  45. return;
  46. }
  47. }
  48. public static void AppendButton(RibbonItem button, PushButtonData data)
  49. {
  50. AppendButtons(button, new List<PushButtonData>() {data});
  51. }
  52. /// <summary>
  53. /// panel创建stack按钮
  54. /// </summary>
  55. /// <param name="existPanel"></param>
  56. /// <param name="cacheGroupButtons"></param>
  57. public static void AddStackButton(RibbonPanel existPanel, List<GroupButtons> cacheGroupButtons)
  58. {
  59. var constNum = 3;
  60. var mod = cacheGroupButtons.Count % constNum;
  61. var discuss = cacheGroupButtons.Count / constNum;
  62. for (int i = 0; i < discuss; i++)
  63. {
  64. var startIndex = i * constNum;
  65. var ribbonItems = existPanel.AddStackedItems(cacheGroupButtons[startIndex].GetButtonData(), cacheGroupButtons[startIndex + 1].GetButtonData(), cacheGroupButtons[startIndex + 2].GetButtonData());
  66. for (int h = 0; h < ribbonItems.Count; h++)
  67. {
  68. cacheGroupButtons[startIndex + h].AppendButton(ribbonItems[h]);
  69. }
  70. }
  71. if (mod == 2)
  72. {
  73. List<GroupButtons> tempGroup = new List<GroupButtons>() { cacheGroupButtons[cacheGroupButtons.Count - 2], cacheGroupButtons[cacheGroupButtons.Count - 1] };
  74. var ribbonItems = existPanel.AddStackedItems(tempGroup[0].GetButtonData(), tempGroup[1].GetButtonData());
  75. for (int h = 0; h < ribbonItems.Count; h++)
  76. {
  77. tempGroup[h].AppendButton(ribbonItems[h]);
  78. }
  79. }
  80. else if (mod == 1)
  81. {
  82. var lastGroup = cacheGroupButtons[cacheGroupButtons.Count - 1];
  83. var tempRibbonItem = existPanel.AddItem(lastGroup.GetButtonData());
  84. lastGroup.AppendButton(tempRibbonItem);
  85. }
  86. }
  87. /// <summary>
  88. /// 附加button数据
  89. /// </summary>
  90. /// <param name="buttonData"></param>
  91. /// <param name="button"></param>
  92. public static void AppendButtonData(ButtonData buttonData, RevitButton button)
  93. {
  94. buttonData.LongDescription = button.LongDescription;
  95. buttonData.ToolTip = button.Description;
  96. buttonData.Image = new DrawingImage(DrawingUtil.DrawLineText("×", 4));
  97. buttonData.LargeImage = new DrawingImage(DrawingUtil.DrawLineText("×", 4));
  98. if (!string.IsNullOrEmpty(button.ImageName))
  99. {
  100. try
  101. {
  102. buttonData.Image = new BitmapImage(new Uri(button.ImageName + "16.png"));
  103. buttonData.LargeImage = new BitmapImage(new Uri(button.ImageName + "32.png"));
  104. }
  105. catch (Exception ex)
  106. {
  107. }
  108. }
  109. }
  110. }
  111. }