When you use PostCommand, the PostedCommand executes after the "termination" of your addin- command, and at that point Revit has control. To execute PostCommand multiple times, you'd have to take / get the control back from Revit.
I can see 2 ways to get the control back (there may be more)
- Subscribe to an Event that you know will happen during / after the execution of the PostedCommand. (for instance the DocumentChangedEvent)
- Subscribe to the FailureProcessing.
Once you have control again, you can use an ExternalEvent to interact with Revit
This code shows how to list the Saved Warnings on screen in an External Event.
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
public class ReVisitWarnings : IExternalCommand
{
public static UIApplication app;
public static FailuresAccessor fAccessor;
public static EventHandler<FailuresProcessingEventArgs> myHandler;
private static ExternalEvent m_ExEvent;
public Autodesk.Revit.UI.Result Execute(ExternalCommandData commandData, ref string message, Autodesk.Revit.DB.ElementSet elements)
{
app = commandData.Application;
myHandler = new EventHandler<FailuresProcessingEventArgs>(FailuresProcessing);
app.Application.FailuresProcessing += myHandler;
RevitCommandId id = RevitCommandId.LookupPostableCommandId(PostableCommand.ReviewWarnings);
app.PostCommand(id);
return Result.Succeeded;
}
private void FailuresProcessing(object sender, FailuresProcessingEventArgs e)
{
fAccessor = e.GetFailuresAccessor();
if (fAccessor.GetFailureMessages().Count == 0) return;
app.Application.FailuresProcessing -= myHandler;
List<FailureMessage> messageList = new List<FailureMessage>();
foreach ( FailureMessageAccessor fmes in fAccessor.GetFailureMessages())
{
messageList.Add(fmes.CloneFailureMessage());
fAccessor.DeleteWarning(fmes);
}
ExternalEventWarningReview handler = new ExternalEventWarningReview(messageList);
m_ExEvent = ExternalEvent.Create(handler);
m_ExEvent.Raise();
e.SetProcessingResult(FailureProcessingResult.ProceedWithRollBack);
}
public class ExternalEventWarningReview : IExternalEventHandler
{
List<FailureMessage> fMessages;
public ExternalEventWarningReview(List<FailureMessage> messages)
{
fMessages = messages;
}
public void Execute(UIApplication app)
{
m_ExEvent.Dispose();
StringBuilder sb = new StringBuilder();
// Document doc = app.ActiveUIDocument.Document;
foreach (FailureMessage fm in fMessages)
{
sb.AppendLine(string.Format( "{0}", fm.GetDescriptionText()));
foreach (var id in fm.GetFailingElements()) sb.AppendLine(string.Format(" failingelement <{0}>", id));
foreach (var id in fm.GetAdditionalElements()) sb.AppendLine(string.Format(" additionalelements <{0}>", id));
}
TaskDialog.Show("External Event", sb.ToString());
}
public string GetName()
{
return "Warning-Review External Event";
}
}
}