Not sure what is the problem...
Attachment is video to demo how it works,
Full code I'm using:
[TransactionAttribute(TransactionMode.Manual)]
public class Pickwhile : IExternalCommand
{
public Document doc;
public Autodesk.Revit.ApplicationServices.Application RevitApp;
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
RevitApp = commandData.Application.Application;
var uidoc = commandData.Application.ActiveUIDocument;
if (uidoc == null)
{
message = "Please open a document";
return Result.Failed;
}
doc = commandData.Application.ActiveUIDocument.Document;
List<ElementId> elemIdList = new List<ElementId>();
try
{
while (true)
{
elemIdList.Add((uidoc.Selection.PickObject(ObjectType.Element, "Pick a room").ElementId));
}
}
catch
{
uidoc.Selection.SetElementIds(elemIdList);
uidoc.RefreshActiveView();
var td = new TaskDialog("Info");
td.MainInstruction = commandData.Application.ActiveUIDocument.Selection.GetElementIds().Aggregate("", (ss, el) => ss + "," + el).TrimStart(',');
td.TitleAutoPrefix = false;
td.Show();
}
return Result.Succeeded;
}
}