外部アプリケーション

外部アプリケーション

開発者は、外部アプリケーションと外部コマンドを使用して機能を追加することができます。リボン タブとリボン パネルは外部アプリケーションを使用してカスタマイズできます。リボン パネルのボタンは外部コマンドに結合されます。

IExternalApplication

外部アプリケーションを Revit に追加するには、IExternalApplication インタフェースを実装するオブジェクトを作成します。

IExternalApplication インタフェースには、外部アプリケーションで優先される 2 つの抽象メソッド OnStartup()と OnShutdown()があります。Revit は起動時に OnStartup()を、終了時に OnShutdown()を呼び出します。

これは次のような OnStartup()と OnShutdown()抽象定義です。

コード領域 3-6: OnShutdown()と OnStartup()

public interface IExternalApplication
{
        public Autodesk.Revit.UI.Result OnStartup(UIControlledApplication application);
        public Autodesk.Revit.UI.Result OnShutdown(UIControlledApplication application);
}

UIControlledApplication パラメータを使用すると特定の Revit イベントにアクセスできるようになり、リボン パネルとコントロールのカスタマイズやリボン タブの追加も可能になります。たとえば、UIControlledApplication のパブリック イベント DialogBoxShowing を使用すれば、表示されるダイアログのイベントをキャプチャできます。次のコード スニペットは、ダイアログの表示直後に呼び出される処理関数を登録します。

コード領域 3-7: DialogBoxShowing イベント

application.DialogBoxShowing += new 
        EventHandler<Autodesk.Revit.Events.DialogBoxShowingEventArgs>(AppDialogShowing);

次のコード例は、UIControlledApplication タイプを使用してイベント ハンドラを登録し、発生時にイベントを処理する方法を表しています。

コード領域 3-8: ControlledApplication を使用

public class Application_DialogBoxShowing : IExternalApplication
{
        // Implement the OnStartup method to register events when Revit starts.
        public Result OnStartup(UIControlledApplication application)
        {
                // Register related events
                application.DialogBoxShowing += 
        new EventHandler<Autodesk.Revit.UI.Events.DialogBoxShowingEventArgs>(AppDialogShowing);
                return Result.Succeeded;
        }

        // Implement this method to unregister the subscribed events when Revit exits.
        public Result OnShutdown(UIControlledApplication application)
        {

                // unregister events
                application.DialogBoxShowing -= 
        new EventHandler<Autodesk.Revit.UI.Events.DialogBoxShowingEventArgs>(AppDialogShowing);
                return Result.Succeeded;
        }

        // The DialogBoxShowing event handler, which allow you to 
        // do some work before the dialog shows
        void AppDialogShowing(object sender, DialogBoxShowingEventArgs args)
        {
                // Get the help id of the showing dialog
                int dialogId = args.HelpId;

                // Format the prompt information string
                String promptInfo = "A Revit dialog will be opened.\n";
                promptInfo += "The help id of this dialog is " + dialogId.ToString() + "\n";
                promptInfo += "If you don't want the dialog to open, please press cancel button";

                // Show the prompt message, and allow the user to close the dialog directly.
                TaskDialog taskDialog = new TaskDialog("Revit");
                taskDialog.MainContent = promptInfo;
                TaskDialogCommonButtons buttons = TaskDialogCommonButtons.Ok |
                                         TaskDialogCommonButtons.Cancel;
                taskDialog.CommonButtons = buttons;
                TaskDialogResult result = taskDialog.Show();
                if (TaskDialogResult.Cancel == result)
                {
                        // Do not show the Revit dialog
                        args.OverrideResult(1);
                }
                else
                {
                        // Continue to show the Revit dialog
                        args.OverrideResult(0);
                }
        }
}