123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- /*-------------------------------------------------------------------------
- * 功能描述: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<PushButtonData> 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<PushButtonData>(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<PushButtonData>() {data});
- }
- /// <summary>
- /// panel创建stack按钮
- /// </summary>
- /// <param name="existPanel"></param>
- /// <param name="cacheGroupButtons"></param>
- public static void AddStackButton(RibbonPanel existPanel, List<GroupButtons> 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<GroupButtons> tempGroup = new List<GroupButtons>() { 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);
- }
- }
- /// <summary>
- /// 附加button数据
- /// </summary>
- /// <param name="buttonData"></param>
- /// <param name="button"></param>
- 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)
- {
- }
- }
- }
- }
- }
|