MenuConfig.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*-------------------------------------------------------------------------
  2. * 功能描述:MenuManagerConfig
  3. * 作者:xulisong
  4. * 创建时间: 2019/3/7 15:58:29
  5. * 版本号:v1.0
  6. * -------------------------------------------------------------------------*/
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Reflection;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using Autodesk.Revit.UI;
  14. namespace FWindSoft.Revit.Menu
  15. {
  16. public class MenuConfig
  17. {
  18. public MenuConfig()
  19. {
  20. Tabs = new RevitComponmentCollection<RevitTab>(this);
  21. Panels = new RevitComponmentCollection<RevitPanel>(this);
  22. Buttons = new RevitComponmentCollection<RevitButton>(this);
  23. }
  24. public MenuConfig(string configFile) : this()
  25. { }
  26. public RevitComponmentCollection<RevitTab> Tabs { get; private set; }
  27. public RevitComponmentCollection<RevitPanel> Panels { get; private set; }
  28. public RevitComponmentCollection<RevitButton> Buttons { get; private set; }
  29. /// <summary>
  30. /// 加载配置字符串信息
  31. /// </summary>
  32. /// <param name="configData"></param>
  33. public void Load(string configData)
  34. {
  35. }
  36. public void CreateUIRibbion(UIControlledApplication app)
  37. {
  38. #region 限定tab个数
  39. var tabs = Tabs.ToList();
  40. if (tabs.Count > 20)
  41. {
  42. tabs = tabs.GetRange(0, 20);
  43. }
  44. #endregion
  45. for (int i = 0; i < tabs.Count; i++)
  46. {
  47. var currentTab = tabs[i];
  48. var panels = Panels.Where(p => p.TabName == currentTab.Name).ToList();
  49. var buttons = Buttons.Where(b => panels.Any(p => p.Name == b.PanelName)).ToList();
  50. if (buttons.Count == 0)
  51. {
  52. continue;
  53. }
  54. buttons=buttons.OrderBy(b => b.Index).ToList();
  55. #region tab相关操作处理
  56. List<RibbonPanel> ribbonPanels = new List<RibbonPanel>();
  57. try
  58. {
  59. //测试创建同名的tab会不会发生问题
  60. ribbonPanels = app.GetRibbonPanels(currentTab.Text);
  61. }
  62. catch (Exception e)
  63. {
  64. app.CreateRibbonTab(currentTab.Name);
  65. }
  66. #endregion
  67. foreach (var panel in panels)
  68. {
  69. var useButtons = buttons.Where(b => b.PanelName == panel.Name).ToList();
  70. if (!useButtons.Any())
  71. {
  72. continue;
  73. }
  74. #region 初始化panel
  75. var existPanel = ribbonPanels.FirstOrDefault(p => p.Name == panel.Text);
  76. if (existPanel == null)
  77. {
  78. existPanel = app.CreateRibbonPanel(currentTab.Text, panel.Text);
  79. }
  80. #endregion
  81. List<GroupButtons> groupButtons = new List<GroupButtons>();
  82. List<RevitButton> cacheButtons = new List<RevitButton>();
  83. #region 处理按钮分组
  84. for (int j = 0; j < useButtons.Count; j++)
  85. {
  86. var currentButton = useButtons[j];
  87. if (!cacheButtons.Any())
  88. {
  89. cacheButtons.Add(currentButton);
  90. }
  91. else
  92. {
  93. //缓存存在
  94. var lastButton = cacheButtons.LastOrDefault();
  95. if (lastButton.ButtonStyle != currentButton.ButtonStyle)
  96. {
  97. groupButtons.Add(new GroupButtons(cacheButtons));
  98. cacheButtons = new List<RevitButton>();
  99. }
  100. else
  101. {
  102. if (lastButton.ButtonStyle == RevitButtonStyle.Push || lastButton.ButtonStyle == RevitButtonStyle.Separator || lastButton.GroupName != currentButton.GroupName)
  103. {
  104. groupButtons.Add(new GroupButtons(cacheButtons));
  105. cacheButtons = new List<RevitButton>();
  106. }
  107. }
  108. cacheButtons.Add(currentButton);
  109. }
  110. }
  111. if (cacheButtons.Any())
  112. {
  113. groupButtons.Add(new GroupButtons(cacheButtons));
  114. }
  115. #endregion
  116. List<GroupButtons> cacheGroupButtons = new List<GroupButtons>();
  117. for (int j = 0; j < groupButtons.Count; j++)
  118. {
  119. var groupButton = groupButtons[j];
  120. if (j == 0 && groupButton.GroupButtonStyle == RevitButtonStyle.Separator)
  121. {
  122. continue;
  123. }
  124. #region 创建核心处理
  125. try
  126. {
  127. if (groupButton.GroupButtonStyle == RevitButtonStyle.Separator)
  128. {
  129. if (cacheGroupButtons.Any())
  130. {
  131. //创建异常处理
  132. RibbonItemUtil.AddStackButton(existPanel, cacheGroupButtons);
  133. }
  134. cacheGroupButtons = new List<GroupButtons>();
  135. existPanel.AddSeparator();
  136. }
  137. else
  138. {
  139. if (groupButton.IsStack)
  140. {
  141. cacheGroupButtons.Add(groupButton);
  142. }
  143. else
  144. {
  145. if (cacheGroupButtons.Any())
  146. {
  147. RibbonItemUtil.AddStackButton(existPanel, cacheGroupButtons);
  148. }
  149. cacheGroupButtons = new List<GroupButtons>();
  150. var data = groupButton.GetButtonData();
  151. var ribbonItem = existPanel.AddItem(data);
  152. groupButton.AppendButton(ribbonItem);
  153. }
  154. }
  155. }
  156. catch (Exception)
  157. {
  158. #if DEBUG
  159. throw;
  160. #endif
  161. }
  162. #endregion
  163. }
  164. if (cacheGroupButtons.Any())
  165. {
  166. RibbonItemUtil.AddStackButton(existPanel, cacheGroupButtons);
  167. }
  168. }
  169. }
  170. }
  171. }
  172. }