Idling&ExternalEvent 空闲事件与外部事件

空闲事件

  • 空闲事件是指在revit无操作时执行命令
  • TheBuildingCoder解释
  • code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

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:在挂载事件后最好后缀卸载事件,不然事件不会停止将一直重复idling里面的操作:smile_cat:

外部事件

  • 外部事件是指在窗体或外部调用revit命令时使用的事件
  • 需要引用接口:IexternalEventHandler
  • code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class ExternalHandler : IExternalEventHandler
{
public void Execute(UIApplication app)
{

ThisApplication.thisApp.AddIdlingEvent(app);

}

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