using System.Reflection; using Autodesk.Revit.UI; using UIFramework; using UIFrameworkServices; namespace ShrlAlgoToolkit.Revit.Assists { /// /// 通过命令id设置快捷键 /// public class KeyboardShortcutService { /// /// 设置菜单快捷键 /// /// /// /// public bool SetRibbonItemShortcut(string id, string shortcutsRep) { Dictionary dictionary = this.LoadShortcuts(); if (this.Commands.TryGetValue(id, out UIFrameworkServices.ShortcutItem shortcutItem)) { shortcutItem.ShortcutsRep = shortcutsRep; shortcutItem.Shortcuts.Clear(); //多个快捷键中间以#分割 foreach (string text in shortcutsRep.Split('#')) { shortcutItem.Shortcuts.Add(text); } dictionary.Add(id, shortcutItem); List list = dictionary.Select((KeyValuePair e) => e.Value).ToList(); /* 项目“ShrlAlgo.Toolkit.Revit (net481)”的未合并的更改 在此之前: this.SaveShortcuts(list); this.ArmCommands(); 在此之后: SaveShortcuts(list); this.ArmCommands(); */ KeyboardShortcutService.SaveShortcuts(list); /* 项目“ShrlAlgo.Toolkit.Revit (net481)”的未合并的更改 在此之前: this.ArmCommands(); return true; 在此之后: ArmCommands(); return true; */ KeyboardShortcutService.ArmCommands(); return true; } return false; } /// /// 通过外部命令类设置快捷键 /// /// /// /// public bool SetRibbonItemShortcut() where T : IExternalCommand { var attribute = typeof(T).GetCustomAttribute(); if (attribute == null) { return false; } Dictionary dictionary = this.LoadShortcuts(); foreach (var item in Commands) { if (item.Key.EndsWith(nameof(T))) { ShortcutItem shortcutItem = item.Value; shortcutItem.ShortcutsRep = attribute.Shortcuts; shortcutItem.Shortcuts.Clear(); //多个快捷键中间以#分割 foreach (string text in attribute.Shortcuts.Split('#')) { shortcutItem.Shortcuts.Add(text); } dictionary.Add(item.Key, shortcutItem); List list = dictionary.Select((KeyValuePair e) => e.Value).ToList(); /* 项目“ShrlAlgo.Toolkit.Revit (net481)”的未合并的更改 在此之前: this.SaveShortcuts(list); this.ArmCommands(); 在此之后: SaveShortcuts(list); this.ArmCommands(); */ KeyboardShortcutService.SaveShortcuts(list); /* 项目“ShrlAlgo.Toolkit.Revit (net481)”的未合并的更改 在此之前: this.ArmCommands(); return true; 在此之后: ArmCommands(); return true; */ KeyboardShortcutService.ArmCommands(); return true; } } return false; } #region 组合使用 /// 基于Revit封装的RibbonButton设置其快捷键. /// /// RibbonButton. /// 快捷键字符串. /// public bool SetShortCut(Autodesk.Revit.UI.RibbonButton btn, string key) { if (btn == null) return false; var item = btn.GetType() .InvokeMember( "getRibbonItem", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod, Type.DefaultBinder, btn, null ); return item != null && (item is Autodesk.Windows.RibbonItem ribbonItem) && SetAdShortCut(ribbonItem, key); } /// /// 基于通用库封装的RibbonCommandItem设置其快捷键. /// /// RibbonCommandItem. /// 快捷键字符串. /// private static bool SetAdShortCut(Autodesk.Windows.RibbonItem commandItem, string key) { if (commandItem == null || string.IsNullOrEmpty(key)) return false; Autodesk.Windows.ComponentManager.Ribbon.FindItem( commandItem.Id, false, out var parentPanel, out var parentTab, true ); if (parentTab == null || parentPanel == null) return false; var path = string.Format("{0}>{1}", parentTab.Id, parentPanel.Source.Id); var cmdId = ControlHelper.GetCommandId(commandItem); if (string.IsNullOrEmpty(cmdId)) { cmdId = Guid.NewGuid().ToString(); ControlHelper.SetCommandId(commandItem, cmdId); } var shortcutItem = new ShortcutItem(commandItem.Text, cmdId, key, path) { ShortcutType = StType.RevitAPI }; UIFrameworkServices.KeyboardShortcutService.applyShortcutChanges( new Dictionary() { { cmdId, shortcutItem } } ); return true; } #endregion /// /// 移除菜单快捷键 /// /// 命令id /// public bool RemoveRibbonItemShortcut(string id) { Dictionary dictionary = this.LoadShortcuts(); UIFrameworkServices.ShortcutItem shortcutItem; if (dictionary.TryGetValue(id, out shortcutItem)) { dictionary.Remove(id); List list = dictionary.Select((KeyValuePair e) => e.Value).ToList(); /* 项目“ShrlAlgo.Toolkit.Revit (net481)”的未合并的更改 在此之前: this.SaveShortcuts(list); this.ArmCommands(); 在此之后: SaveShortcuts(list); this.ArmCommands(); */ KeyboardShortcutService.SaveShortcuts(list); /* 项目“ShrlAlgo.Toolkit.Revit (net481)”的未合并的更改 在此之前: this.ArmCommands(); return true; 在此之后: ArmCommands(); return true; */ KeyboardShortcutService.ArmCommands(); return true; } return false; } /// /// 是否具有菜单快捷键 /// /// /// public bool HasRibbonItemShortcut(string id) { return this.LoadShortcuts().TryGetValue(id, out var _); } /// /// 获取快捷键的映射(最多五个按键) /// /// CommandId /// 快捷键 public string GetRibbonItemShortcutsRep(string id) { if (this.LoadShortcuts().TryGetValue(id, out var shortcutItem)) { return shortcutItem.ShortcutsRep; } return ""; } /// /// 获取全部命令快捷键CommandId-ObservableCollect() /// /// public Dictionary> GetCommandShortcuts() { return (from e in this.Commands select e.Value into e group e by e.CommandId).ToDictionary((IGrouping i) => i.Key, (IGrouping j) => j.SelectMany((UIFrameworkServices.ShortcutItem i) => i.Shortcuts)); } /// /// 获取自定义快捷键被关联到的命令CommandId /// /// 自定义的快捷键 /// CommandId public string GetCommandShortcut(string shortcut) { return this.Commands.FirstOrDefault((KeyValuePair e) => e.Value.Shortcuts.Contains(shortcut)).Key; } /// /// 自定义的快捷键是否已经被关联到某个命令上 /// /// 自定义的快捷键 /// public bool HasCommandShortcut(string shortcut) { return this.GetCommandShortcuts().Any((KeyValuePair> e) => e.Value.Contains(shortcut)); } /// /// 加载命令 /// public static void LoadCommands() { ShortcutsHelper.LoadCommands(); } /// /// 应用快捷键更改 /// public void ApplyShortcutChanges() { UIFrameworkServices.KeyboardShortcutService.applyShortcutChanges(this.LoadShortcuts()); } /// /// 应用更改 /// private static void ArmCommands() { ShortcutsHelper.ArmCommands(); } /// /// 获取已经加载的ShortcutItems /// /// private Dictionary LoadShortcuts() { return ShortcutsHelper.LoadShortcuts(); } /// /// 保存ShortcutItems /// /// private static void SaveShortcuts(ICollection shortcutList) { ShortcutsHelper.SaveShortcuts(shortcutList); } /// /// 所有快捷命令条目字典 /// private Dictionary Commands { get { return ShortcutsHelper.Commands; } } /// /// 获取所有ShortcutItems /// /// ShortcutItems public IEnumerable GetShortcutItems() { return from e in this.LoadShortcuts() select new UIFrameworkServices.ShortcutItem(e.Value); } } }