命令

Revit API 提供对现有 Revit 命令的访问,这些命令位于选项卡、应用程序菜单或右键单击菜单中。使用 API 处理 Revit 命令的主要方法是替换现有命令实现或发布命令。

替代 Revit 命令

AddInCommandBinding 类可用于替代 Revit 中的现有命令。它有三个与替换现有命令实现相关的事件。

  • BeforeExecuted - 此只读事件在关联命令执行之前发生。应用程序可以对此事件做出反应,但不能更改文档或影响命令的调用。
  • CanExecute - 当关联的命令启动检查以确定是否可以在命令目标上执行命令时出现。
  • Executed - 当执行关联的命令时,将发生此事件,并且应在此处执行任何覆盖实现。

要创建 commandbinding,请调用 UIApplication.CreateAddInCommandBinding() 或 UIControlledApplication.CreateAddInCommandBinding()。这两种方法都需要 RevitCommandId ID 来标识要替换的命令处理程序。RevitCommandId 有两种用于获取命令 ID 的静态方法:

  • LookupCommandId - 使用给定的 ID 字符串检索 Revit 命令 ID。要查找命令 ID 字符串,请打开 Revit 的会话,调用所需的命令,关闭 Revit,然后查看该会话的日志。选择时记录的 “Jrn.Command” 条目将具有 LookupCommandId() 所需的字符串,并且看起来类似于 “ID_EDIT_DESIGNOPTIONS”。
  • LookupPostableCommandId - 使用 PostableCommand 枚举检索 Revit 命令 ID。这仅适用于可提交的命令(将在下一节中讨论)。

以下示例摘自 Revit 2014 SDK 的 DisableCommand 示例,演示了如何创建 AddInCommandBinding 并替代实现以禁用该命令,并向用户发送消息。

代码区域:替代命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/// 
/// Implements the Revit add-in interface IExternalApplication
///
public class Application : IExternalApplication
{
#region IExternalApplication Members

///
/// Implements the OnStartup event
///
///
///
public Result OnStartup(UIControlledApplication application)
{
// Lookup the desired command by name
s_commandId = RevitCommandId.LookupCommandId(s_commandToDisable);

// Confirm that the command can be overridden
if (!s_commandId.CanHaveBinding)
{
ShowDialog("Error", "The target command " + s_commandToDisable +
" selected for disabling cannot be overridden");
return Result.Failed;
}

// Create a binding to override the command.
// Note that you could also implement .CanExecute to override the accessibiliy of the command.
// Doing so would allow the command to be grayed out permanently or selectively, however,
// no feedback would be available to the user about why the command is grayed out.
try
{
AddInCommandBinding commandBinding = application.CreateAddInCommandBinding(s_commandId);
commandBinding.Executed += DisableEvent;
}
// Most likely, this is because someone else has bound this command already.
catch (Exception)
{
ShowDialog("Error", "This add-in is unable to disable the target command " + s_commandToDisable +
"; most likely another add-in has overridden this command.");
}

return Result.Succeeded;
}

///
/// Implements the OnShutdown event
///
///
///
public Result OnShutdown(UIControlledApplication application)
{
// Remove the command binding on shutdown
if (s_commandId.HasBinding)
application.RemoveAddInCommandBinding(s_commandId);
return Result.Succeeded;
}

#endregion

///
/// A command execution method which disables any command it is applied to (with a user-visible message).
///
/// Event sender.
/// Arguments.
private void DisableEvent(object sender, ExecutedEventArgs args)
{
ShowDialog("Disabled", "Use of this command has been disabled.");
}

///
/// Show a task dialog with a message and title.
///
/// The title.
/// The message.
private static void ShowDialog(string title, string message)
{
// Show the user a message.
TaskDialog td = new TaskDialog(title)
{
MainInstruction = message,
TitleAutoPrefix = false
};
td.Show();
}

///
/// The string name of the command to disable. To lookup a command id string, open a session of Revit,
/// invoke the desired command, close Revit, then look to the journal from that session. The command
/// id string will be toward the end of the journal, look for the "Jrn.Command" entry that was recorded
/// when it was selected.
///
static String s_commandToDisable = "ID_EDIT_DESIGNOPTIONS";

///
/// The command id, stored statically to allow for removal of the command binding.
///
static RevitCommandId s_commandId;

}

发布命令

方法 UIApplication.PostCommand() 将命令发布到 Revit 消息队列,以便在控件从当前 API 应用程序返回时调用该命令。只有某些命令可以以这种方式发布。它们包括 Autodesk.Revit.UI.PostableCommand 枚举类型中的所有命令,以及由任何附加模块创建的外部命令。

注意:使用 PostCommand() 时,即使是可发布的命令也可能无法执行。发生这种情况的一个原因是,如果已经发布了另一个命令。在给定时间,只能将一个命令发布到 Revit,因此,如果发布第二个命令,PostCommand() 将引发异常。已发布的命令可能无法执行的另一个原因是要执行的命令当时无法访问。它是否可访问仅在 Revit 从 API 上下文返回时确定,因此,由于此原因而执行失败时,不会直接报告给发布命令的应用程序。UIApplication 的 API 应用程序。CanPostCommand() 可用于识别是否可以发布给定的命令,这意味着它是 PostableCommand 的成员还是外部命令。它不标识该命令当前是否可访问。

PostCommand() 和 CanPostCommand() 都需要 RevitCommandId,可以按照上面的“替代 Revit 命令”部分所述获取该 ID。

注:翻译自Revit API Developers Guide