How to run addin after Revit Command (opposite of PostCommand)?

ARX_AntonioCarneiro
Explorer
Explorer

How to run addin after Revit Command (opposite of PostCommand)?

ARX_AntonioCarneiro
Explorer
Explorer

Hi all.
I have developed a small addin that if invoked after creating a section opens its view. It is inelegant but effective: it filters the ids of the views and the greater is the last view created.

 

FilteredElementCollector elementos = new FilteredElementCollector(doc);

            ICollection<ElementId> elementosIds = elementos.OfCategory(BuiltInCategory.OST_Views).ToElementIds();

            List<int> idsNumero = new List<int>();

            foreach(ElementId e in elementosIds)
            {
                idsNumero.Add(e.IntegerValue);
            }

            int ultimo = idsNumero.Max();

            ElementId ultimoId = new ElementId(ultimo);

            View vistaNueva = doc.GetElement(ultimoId) as View;

            uidoc.ActiveView = vistaNueva;

            return Result.Succeeded;


The idea is that after executing the section command, the addin is automatically executed.
I need the opposite of PostCommand, first the command of Revit and then my code, is it possible?

 

Thanks 😉

0 Likes
Reply
583 Views
3 Replies
Replies (3)

aignatovich
Advisor
Advisor

Hi! It seems you can subscribe to DocumentChanged event instead. If some new section views were added, activate the last one.

0 Likes

MarryTookMyCoffe
Collaborator
Collaborator

yes, look on events in

Autodesk.Windows.ComponentManager

 .PreviewExecute - is before,

 ItemExecuted - after

-------------------------------------------------------------
--------------------------------|\/\/|------------------------
do not worry it only gonna take Autodesk 5 years to fix bug
0 Likes

manuel.solis.lopez
Contributor
Contributor

Since you are not trying to modify the Revit Database, the best approach would be to subscribe an event handler method to the Application DocumentChanged event and use the Id of the new View to activate it.

 

Document.Application.DocumentChanged += (sender, args) =>
{
   if (args.Operation != Autodesk.Revit.DB.Events.UndoOperation.TransactionCommitted)
      return;

   var newElements = args.GetAddedElementIds();
   if (newElements.Count != 1)
      return;

   var doc = args.GetDocument();
   var view = doc.GetElement(newElements.First()) as ViewSection;
   if (view == null)
      return;

   var app = new Autodesk.Revit.UI.UIApplication(doc.Application);
   app.ActiveUIDocument.ActiveView = view;
};

 

Probably you would like to subscribe the event using an ExternalApplication in the OnStartup(...) method, in order to do it once when your addin is being loaded by Revit.

Regards.

0 Likes