using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using Autodesk.Revit.UI; using UIFramework; using UIFrameworkServices; namespace Sai.Toolkit.Revit.Helpers { /// /// 通过命令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(); this.SaveShortcuts(list); this.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(); this.SaveShortcuts(list); this.ArmCommands(); return true; } } return false; } /// /// 移除菜单快捷键 /// /// 命令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(); this.SaveShortcuts(list); this.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 void LoadCommands() { ShortcutsHelper.LoadCommands(); } /// /// 应用快捷键更改 /// public void ApplyShortcutChanges() { UIFrameworkServices.KeyboardShortcutService.applyShortcutChanges(this.LoadShortcuts()); } /// /// 应用更改 /// private void ArmCommands() { ShortcutsHelper.ArmCommands(); } /// /// 获取已经加载的ShortcutItems /// /// private Dictionary LoadShortcuts() { return ShortcutsHelper.LoadShortcuts(); } /// /// 保存ShortcutItems /// /// private 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); } } }