/// /// Implements the Revit add-in interface IExternalApplication /// publicclassApplication : 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. privatevoidDisableEvent(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. privatestaticvoidShowDialog(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 的成员还是外部命令。它不标识该命令当前是否可访问。