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;
}
}
}













Must log in before commenting!
Sign Up