应用程序:DisableCommand

Revit平台:所有版本

Revit版本:2013.0

首次发布版本:2013.0

编程语言:C#

技能水平:初学者

类别:基础知识

类型:外部应用程序

主题:命令替换

摘要:通过替换其实现为简单的弹出消息来禁用命令

相关类:

Autodesk.Revit.UI.IExternalApplication

Autodesk.Revit.UI.RevitCommandId

Autodesk.Revit.UI.AddInCommandBinding

Autodesk.Revit.UI.UIControlledApplication

项目文件:

Application.cs

外部应用程序

功能:

此示例重写了“构建选项(B)"命令,以防止用户访问它。

查找RevitCommandId以匹配命令的日志名称。

为此命令ID创建AddInCommandBinding。

为命令绑定提供替代实现。

实施:

打开 Revit。

打开或创建一个项目。

选择 Manage 选项卡上的 Design Options 按钮。

一个弹出窗口会解释该命令已被禁用。

完整的源代码请加入QQ群649037449,在群文件中下载RevitSDK.exe,解压后在文件夹中搜索本文中应用程序名称即可获得完整源码

Application.cs

//
// (C) Copyright 2003-2019 by Autodesk, Inc. All rights reserved.
//
// Permission to use, copy, modify, and distribute this software in
// object code form for any purpose and without fee is hereby granted
// provided that the above copyright notice appears in all copies and
// that both that copyright notice and the limited warranty and
// restricted rights notice below appear in all supporting
// documentation.

//
// AUTODESK PROVIDES THIS PROGRAM 'AS IS' AND WITH ALL ITS FAULTS.
// AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC.
// DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE
// UNINTERRUPTED OR ERROR FREE.
//
// Use, duplication, or disclosure by the U.S. Government is subject to
// restrictions set forth in FAR 52.227-19 (Commercial Computer
// Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii)
// (Rights in Technical Data and Computer Software), as applicable. 

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.IO;

using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Events;

namespace Revit.SDK.Samples.DisableCommand.CS
{
    /// <summary>
    /// Implements the Revit add-in interface IExternalApplication
    /// </summary>
    public class Application : IExternalApplication
    {
        #region IExternalApplication Members
       
        /// <summary>
        /// Implements the OnStartup event
        /// </summary>
        /// <param name="application"></param>
        /// <returns></returns>
        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;
        }

        /// <summary>
        /// Implements the OnShutdown event
        /// </summary>
        /// <param name="application"></param>
        /// <returns></returns>
        public Result OnShutdown(UIControlledApplication application)
        {
            // Remove the command binding on shutdown
            if (s_commandId.HasBinding)
                application.RemoveAddInCommandBinding(s_commandId);
            return Result.Succeeded;
        }

        #endregion

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

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

        /// <summary>
        /// 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.
        /// </summary>
        static String s_commandToDisable = "ID_EDIT_DESIGNOPTIONS";

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

    }
}