/*------------------------------------------------------------------------- * 功能描述:MenuManagerConfig * 作者:xulisong * 创建时间: 2019/3/7 15:58:29 * 版本号:v1.0 * -------------------------------------------------------------------------*/ using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using Autodesk.Revit.UI; namespace FWindSoft.Revit.Menu { public class MenuConfig { public MenuConfig() { Tabs = new RevitComponmentCollection(this); Panels = new RevitComponmentCollection(this); Buttons = new RevitComponmentCollection(this); } public MenuConfig(string configFile) : this() { } public RevitComponmentCollection Tabs { get; private set; } public RevitComponmentCollection Panels { get; private set; } public RevitComponmentCollection Buttons { get; private set; } /// /// 加载配置字符串信息 /// /// public void Load(string configData) { } public void CreateUIRibbion(UIControlledApplication app) { #region 限定tab个数 var tabs = Tabs.ToList(); if (tabs.Count > 20) { tabs = tabs.GetRange(0, 20); } #endregion for (int i = 0; i < tabs.Count; i++) { var currentTab = tabs[i]; var panels = Panels.Where(p => p.TabName == currentTab.Name).ToList(); var buttons = Buttons.Where(b => panels.Any(p => p.Name == b.PanelName)).ToList(); if (buttons.Count == 0) { continue; } buttons=buttons.OrderBy(b => b.Index).ToList(); #region tab相关操作处理 List ribbonPanels = new List(); try { //测试创建同名的tab会不会发生问题 ribbonPanels = app.GetRibbonPanels(currentTab.Text); } catch (Exception e) { app.CreateRibbonTab(currentTab.Name); } #endregion foreach (var panel in panels) { var useButtons = buttons.Where(b => b.PanelName == panel.Name).ToList(); if (!useButtons.Any()) { continue; } #region 初始化panel var existPanel = ribbonPanels.FirstOrDefault(p => p.Name == panel.Text); if (existPanel == null) { existPanel = app.CreateRibbonPanel(currentTab.Text, panel.Text); } #endregion List groupButtons = new List(); List cacheButtons = new List(); #region 处理按钮分组 for (int j = 0; j < useButtons.Count; j++) { var currentButton = useButtons[j]; if (!cacheButtons.Any()) { cacheButtons.Add(currentButton); } else { //缓存存在 var lastButton = cacheButtons.LastOrDefault(); if (lastButton.ButtonStyle != currentButton.ButtonStyle) { groupButtons.Add(new GroupButtons(cacheButtons)); cacheButtons = new List(); } else { if (lastButton.ButtonStyle == RevitButtonStyle.Push || lastButton.ButtonStyle == RevitButtonStyle.Separator || lastButton.GroupName != currentButton.GroupName) { groupButtons.Add(new GroupButtons(cacheButtons)); cacheButtons = new List(); } } cacheButtons.Add(currentButton); } } if (cacheButtons.Any()) { groupButtons.Add(new GroupButtons(cacheButtons)); } #endregion List cacheGroupButtons = new List(); for (int j = 0; j < groupButtons.Count; j++) { var groupButton = groupButtons[j]; if (j == 0 && groupButton.GroupButtonStyle == RevitButtonStyle.Separator) { continue; } #region 创建核心处理 try { if (groupButton.GroupButtonStyle == RevitButtonStyle.Separator) { if (cacheGroupButtons.Any()) { //创建异常处理 RibbonItemUtil.AddStackButton(existPanel, cacheGroupButtons); } cacheGroupButtons = new List(); existPanel.AddSeparator(); } else { if (groupButton.IsStack) { cacheGroupButtons.Add(groupButton); } else { if (cacheGroupButtons.Any()) { RibbonItemUtil.AddStackButton(existPanel, cacheGroupButtons); } cacheGroupButtons = new List(); var data = groupButton.GetButtonData(); var ribbonItem = existPanel.AddItem(data); groupButton.AppendButton(ribbonItem); } } } catch (Exception) { #if DEBUG throw; #endif } #endregion } if (cacheGroupButtons.Any()) { RibbonItemUtil.AddStackButton(existPanel, cacheGroupButtons); } } } } } }