319 lines
12 KiB
C#
319 lines
12 KiB
C#
using System.Reflection;
|
|
using Autodesk.Revit.UI;
|
|
using UIFramework;
|
|
using UIFrameworkServices;
|
|
|
|
namespace ShrlAlgoToolkit.RevitCore.Assists
|
|
{
|
|
/// <summary>
|
|
/// 通过命令id设置快捷键
|
|
/// </summary>
|
|
public class KeyboardShortcutService
|
|
{
|
|
/// <summary>
|
|
/// 设置菜单快捷键
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <param name="shortcutsRep"></param>
|
|
/// <returns></returns>
|
|
public bool SetRibbonItemShortcut(string id, string shortcutsRep)
|
|
{
|
|
Dictionary<string, UIFrameworkServices.ShortcutItem> 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<UIFrameworkServices.ShortcutItem> list = dictionary.Select((KeyValuePair<string, UIFrameworkServices.ShortcutItem> e) => e.Value).ToList<UIFrameworkServices.ShortcutItem>();
|
|
|
|
/* 项目“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;
|
|
}
|
|
/// <summary>
|
|
/// 通过外部命令类设置快捷键
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <param name="shortcutsRep"></param>
|
|
/// <returns></returns>
|
|
public bool SetRibbonItemShortcut<T>() where T : IExternalCommand
|
|
{
|
|
var attribute = typeof(T).GetCustomAttribute<ShortcutRepAttribute>();
|
|
if (attribute == null)
|
|
{
|
|
return false;
|
|
}
|
|
Dictionary<string, UIFrameworkServices.ShortcutItem> 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<UIFrameworkServices.ShortcutItem> list = dictionary.Select((KeyValuePair<string, UIFrameworkServices.ShortcutItem> e) => e.Value).ToList<UIFrameworkServices.ShortcutItem>();
|
|
|
|
/* 项目“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设置其快捷键.
|
|
/// </summary>
|
|
/// <param name="btn">RibbonButton.</param>
|
|
/// <param name="key">快捷键字符串.</param>
|
|
/// <returns></returns>
|
|
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);
|
|
}
|
|
/// <summary>
|
|
/// 基于通用库封装的RibbonCommandItem设置其快捷键.
|
|
/// </summary>
|
|
/// <param name="commandItem">RibbonCommandItem.</param>
|
|
/// <param name="key">快捷键字符串.</param>
|
|
/// <returns></returns>
|
|
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<string, ShortcutItem>() { { cmdId, shortcutItem } }
|
|
);
|
|
return true;
|
|
}
|
|
#endregion
|
|
|
|
/// <summary>
|
|
/// 移除菜单快捷键
|
|
/// </summary>
|
|
/// <param name="id">命令id</param>
|
|
/// <returns></returns>
|
|
public bool RemoveRibbonItemShortcut(string id)
|
|
{
|
|
Dictionary<string, UIFrameworkServices.ShortcutItem> dictionary = this.LoadShortcuts();
|
|
UIFrameworkServices.ShortcutItem shortcutItem;
|
|
if (dictionary.TryGetValue(id, out shortcutItem))
|
|
{
|
|
dictionary.Remove(id);
|
|
List<UIFrameworkServices.ShortcutItem> list = dictionary.Select((KeyValuePair<string, UIFrameworkServices.ShortcutItem> e) => e.Value).ToList<UIFrameworkServices.ShortcutItem>();
|
|
|
|
/* 项目“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;
|
|
}
|
|
/// <summary>
|
|
/// 是否具有菜单快捷键
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
public bool HasRibbonItemShortcut(string id)
|
|
{
|
|
return this.LoadShortcuts().TryGetValue(id, out var _);
|
|
}
|
|
/// <summary>
|
|
/// 获取快捷键的映射(最多五个按键)
|
|
/// </summary>
|
|
/// <param name="id">CommandId</param>
|
|
/// <returns>快捷键</returns>
|
|
public string GetRibbonItemShortcutsRep(string id)
|
|
{
|
|
if (this.LoadShortcuts().TryGetValue(id, out var shortcutItem))
|
|
{
|
|
return shortcutItem.ShortcutsRep;
|
|
}
|
|
return "";
|
|
}
|
|
/// <summary>
|
|
/// 获取全部命令快捷键CommandId-ObservableCollect<string>()
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public Dictionary<string, IEnumerable<string>> GetCommandShortcuts()
|
|
{
|
|
return (from e in this.Commands
|
|
select e.Value into e
|
|
group e by e.CommandId).ToDictionary((IGrouping<string, UIFrameworkServices.ShortcutItem> i) => i.Key, (IGrouping<string, UIFrameworkServices.ShortcutItem> j) => j.SelectMany((UIFrameworkServices.ShortcutItem i) => i.Shortcuts));
|
|
}
|
|
/// <summary>
|
|
/// 获取自定义快捷键被关联到的命令CommandId
|
|
/// </summary>
|
|
/// <param name="shortcut">自定义的快捷键</param>
|
|
/// <returns>CommandId</returns>
|
|
public string GetCommandShortcut(string shortcut)
|
|
{
|
|
return this.Commands.FirstOrDefault((KeyValuePair<string, UIFrameworkServices.ShortcutItem> e) => e.Value.Shortcuts.Contains(shortcut)).Key;
|
|
}
|
|
/// <summary>
|
|
/// 自定义的快捷键是否已经被关联到某个命令上
|
|
/// </summary>
|
|
/// <param name="shortcut">自定义的快捷键</param>
|
|
/// <returns></returns>
|
|
public bool HasCommandShortcut(string shortcut)
|
|
{
|
|
return this.GetCommandShortcuts().Any((KeyValuePair<string, IEnumerable<string>> e) => e.Value.Contains(shortcut));
|
|
}
|
|
/// <summary>
|
|
/// 加载命令
|
|
/// </summary>
|
|
public static void LoadCommands()
|
|
{
|
|
ShortcutsHelper.LoadCommands();
|
|
}
|
|
/// <summary>
|
|
/// 应用快捷键更改
|
|
/// </summary>
|
|
public void ApplyShortcutChanges()
|
|
{
|
|
UIFrameworkServices.KeyboardShortcutService.applyShortcutChanges(this.LoadShortcuts());
|
|
}
|
|
/// <summary>
|
|
/// 应用更改
|
|
/// </summary>
|
|
private static void ArmCommands()
|
|
{
|
|
ShortcutsHelper.ArmCommands();
|
|
}
|
|
/// <summary>
|
|
/// 获取已经加载的ShortcutItems
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
private Dictionary<string, UIFrameworkServices.ShortcutItem> LoadShortcuts()
|
|
{
|
|
return ShortcutsHelper.LoadShortcuts();
|
|
}
|
|
/// <summary>
|
|
/// 保存ShortcutItems
|
|
/// </summary>
|
|
/// <param name="shortcutList"></param>
|
|
private static void SaveShortcuts(ICollection<UIFrameworkServices.ShortcutItem> shortcutList)
|
|
{
|
|
ShortcutsHelper.SaveShortcuts(shortcutList);
|
|
}
|
|
/// <summary>
|
|
/// 所有快捷命令条目字典
|
|
/// </summary>
|
|
private Dictionary<string, UIFrameworkServices.ShortcutItem> Commands
|
|
{
|
|
get
|
|
{
|
|
return ShortcutsHelper.Commands;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 获取所有ShortcutItems
|
|
/// </summary>
|
|
/// <returns>ShortcutItems</returns>
|
|
public IEnumerable<UIFrameworkServices.ShortcutItem> GetShortcutItems()
|
|
{
|
|
return from e in this.LoadShortcuts()
|
|
select new UIFrameworkServices.ShortcutItem(e.Value);
|
|
}
|
|
}
|
|
}
|