BIM World
A Professional BIM Learning Platform


Revit Plugin Development: How to Repeat Commands Until You Press Esc

In Revit, some element creation commands are designed to remain active after execution. This means the command will continue to run until you press Esc or right-click to cancel. For instance, when placing a door, the typical workflow is: click the door button → enter selection mode → select a wall → create the door family → return to selection mode again.

If you want your plugin to mimic this interactive behavior, it’s quite straightforward. Simply encapsulate the command logic within a method and call that method recursively at the end. However, be aware that if the user cancels the selection, an OperationCanceledException will be thrown, which must be handled properly.

Below is a simple example demonstrating a command that repeatedly prompts the user to select an element and then displays its ID.

Code example:

using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.Attributes;

namespace ClassLibrary2
{
    [Transaction(TransactionMode.Manual)]
    class newTest : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            ShowElementId(commandData.Application.ActiveUIDocument);
            return Result.Succeeded;
        }

        bool ShowElementId(UIDocument uIDocument)
        {
            try
            {
                ElementId elemId = uIDocument.Selection.PickObject(
                    Autodesk.Revit.UI.Selection.ObjectType.Element,
                    "Select an element").ElementId;
                TaskDialog.Show("newTest", elemId.ToString());
                ShowElementId(uIDocument);
            }
            catch (Autodesk.Revit.Exceptions.OperationCanceledException)
            {
                return false;
            }
            return true;
        }
    }
}
xuebim
Follow the latest BIM developments in the architecture industry, explore innovative building technologies, and discover cutting-edge industry insights.
← Scan with WeChat
Like(0) 打赏
BIM WORLD » Revit Plugin Development: How to Repeat Commands Until You Press Esc

Comment Get first!

Must log in before commenting!

 

BIM World, A Professional BIM Learning Platform

Stay updated on the latest architecture trends and share new building technologies.

Contact UsAbout Us

觉得文章有用就打赏一下小编吧

非常感谢你的打赏,我们将继续提供更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫

微信扫一扫

Account Login

By signing in, you agree toUser Agreement

Sign Up