/*------------------------------------------------------------------------- * 功能描述:RibbonItemUtil * 作者:xulisong * 创建时间: 2019/3/8 9:13:09 * 版本号:v1.0 * -------------------------------------------------------------------------*/ using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Media; using System.Windows.Media.Imaging; using Autodesk.Revit.UI; using FWindSoft.Wpf; namespace FWindSoft.Revit.Menu { /* * 具体实现功能和算法流程中的业务分开,就是常说的提炼出方法,这样以后代码变动容易修改,因为每次面对的实际改动代码 * 量很小,而不用查看所有代码 */ public static class RibbonItemUtil { public static void AppendButtons(RibbonItem button,List datas) { var buttonDatas = datas; if (datas==null||!buttonDatas.Any()) return; if (button is SplitButton splitButton) { buttonDatas.ForEach(d => splitButton.AddPushButton(d)); return; } //这两个类处理顺序不能颠倒,他们是父子类关系 if (button is PulldownButton pulldownButton) { //开头的第一个button数据,应该为PulldownButton的数据 var first = datas.FirstOrDefault(); var useDatas = new List(datas); if (first.Name == pulldownButton.Name) { useDatas.RemoveAt(0); } useDatas.ForEach(d => pulldownButton.AddPushButton(d)); return; } } public static void AppendButton(RibbonItem button, PushButtonData data) { AppendButtons(button, new List() {data}); } /// /// panel创建stack按钮 /// /// /// public static void AddStackButton(RibbonPanel existPanel, List cacheGroupButtons) { var constNum = 3; var mod = cacheGroupButtons.Count % constNum; var discuss = cacheGroupButtons.Count / constNum; for (int i = 0; i < discuss; i++) { var startIndex = i * constNum; var ribbonItems = existPanel.AddStackedItems(cacheGroupButtons[startIndex].GetButtonData(), cacheGroupButtons[startIndex + 1].GetButtonData(), cacheGroupButtons[startIndex + 2].GetButtonData()); for (int h = 0; h < ribbonItems.Count; h++) { cacheGroupButtons[startIndex + h].AppendButton(ribbonItems[h]); } } if (mod == 2) { List tempGroup = new List() { cacheGroupButtons[cacheGroupButtons.Count - 2], cacheGroupButtons[cacheGroupButtons.Count - 1] }; var ribbonItems = existPanel.AddStackedItems(tempGroup[0].GetButtonData(), tempGroup[1].GetButtonData()); for (int h = 0; h < ribbonItems.Count; h++) { tempGroup[h].AppendButton(ribbonItems[h]); } } else if (mod == 1) { var lastGroup = cacheGroupButtons[cacheGroupButtons.Count - 1]; var tempRibbonItem = existPanel.AddItem(lastGroup.GetButtonData()); lastGroup.AppendButton(tempRibbonItem); } } /// /// 附加button数据 /// /// /// public static void AppendButtonData(ButtonData buttonData, RevitButton button) { buttonData.LongDescription = button.LongDescription; buttonData.ToolTip = button.Description; buttonData.Image = new DrawingImage(DrawingUtil.DrawLineText("×", 4)); buttonData.LargeImage = new DrawingImage(DrawingUtil.DrawLineText("×", 4)); if (!string.IsNullOrEmpty(button.ImageName)) { try { buttonData.Image = new BitmapImage(new Uri(button.ImageName + "16.png")); buttonData.LargeImage = new BitmapImage(new Uri(button.ImageName + "32.png")); } catch (Exception ex) { } } } } }