123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- /*-------------------------------------------------------------------------
- * 功能描述: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<RevitTab>(this);
- Panels = new RevitComponmentCollection<RevitPanel>(this);
- Buttons = new RevitComponmentCollection<RevitButton>(this);
- }
- public MenuConfig(string configFile) : this()
- { }
- public RevitComponmentCollection<RevitTab> Tabs { get; private set; }
- public RevitComponmentCollection<RevitPanel> Panels { get; private set; }
- public RevitComponmentCollection<RevitButton> Buttons { get; private set; }
- /// <summary>
- /// 加载配置字符串信息
- /// </summary>
- /// <param name="configData"></param>
- 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<RibbonPanel> ribbonPanels = new List<RibbonPanel>();
- 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> groupButtons = new List<GroupButtons>();
- List<RevitButton> cacheButtons = new List<RevitButton>();
- #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<RevitButton>();
- }
- else
- {
- if (lastButton.ButtonStyle == RevitButtonStyle.Push || lastButton.ButtonStyle == RevitButtonStyle.Separator || lastButton.GroupName != currentButton.GroupName)
- {
- groupButtons.Add(new GroupButtons(cacheButtons));
- cacheButtons = new List<RevitButton>();
- }
- }
- cacheButtons.Add(currentButton);
- }
- }
- if (cacheButtons.Any())
- {
- groupButtons.Add(new GroupButtons(cacheButtons));
- }
-
- #endregion
- List<GroupButtons> cacheGroupButtons = new List<GroupButtons>();
- 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<GroupButtons>();
- existPanel.AddSeparator();
- }
- else
- {
- if (groupButton.IsStack)
- {
- cacheGroupButtons.Add(groupButton);
- }
- else
- {
- if (cacheGroupButtons.Any())
- {
- RibbonItemUtil.AddStackButton(existPanel, cacheGroupButtons);
- }
- cacheGroupButtons = new List<GroupButtons>();
- 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);
- }
- }
- }
- }
- }
- }
|