Idling&ExternalEvent

Idling Event

  • Idling event refers to executing commands when revit has no operation
  • TheBuildingCoder explanation
  • code

public static void IdlingHandler(object sender, Autodesk.Revit.UI.Events.IdlingEventArgs args)
        {
            UIApplication uiapp = sender as UIApplication;
            if (uiapp != null)
            {
                Autodesk.Revit.UI.UIDocument uidoc = uiapp.ActiveUIDocument;
                Autodesk.Revit.DB.Document doc = uidoc.Document;
                if (uidoc != null && doc != null)
                {
                    Reference re = uidoc.Selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, "");
                    Element e = doc.GetElement(re);
                    
                    UserControl1.mainPage.Label1.Content = e.Id.ToString() + "\r";
                    uiapp.Idling -= IdlingHandler;
                }
            }
            else
            {
                System.Windows.Forms.MessageBox.Show("Must should Idling!");
            }
        }
  • TIPS: It is best to append uninstall event after mounting event, otherwise event will not stop and will repeat operation in idling all the time :smile_cat:

External Event

  • External event refers to event used when calling revit command in form or externally
  • Need to reference interface: IexternalEventHandler
  • code
 public class ExternalHandler : IExternalEventHandler
        {
            public void Execute(UIApplication app)
            {
                
               ThisApplication.thisApp.AddIdlingEvent(app);
                
            }

            public string GetName()
            {
                return "ExternalHander.Excute()";
            }
        }