空闲事件+外部命令
[Transaction(TransactionMode.Manual)]
public class Command : IExternalCommand
{
readonly string path = @"C:\Users\41022\Desktop\TestRvt\项目1.rvt";
//定义一个全局UIApplication,用来注销指定事件
UIApplication uiApp = null;
//定义一个字段通过属性变化来触发空闲事件
public static Boolean IdleFlag = false;
//记录DocumentChanged事件发生改变的构件
//ElementId id = new ElementId(313106);
ElementId id = null;
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
uiApp = commandData.Application;
UIDocument uiDoc = uiApp.ActiveUIDocument;
Document Doc = uiDoc.Document;
System.Diagnostics.Process.Start(path);
uiApp.PostCommand(RevitCommandId.LookupPostableCommandId(PostableCommand.Door));//调用“门”命令,用户创建门
uiApp.Idling += new EventHandler<IdlingEventArgs>(IdlingHandler);//先注册空闲事件
uiApp.Application.DocumentChanged += new EventHandler<DocumentChangedEventArgs>(DocumentChangedForSomething);//再注册DocumentChanged事件
return Result.Succeeded;
}
private void DocumentChangedForSomething(object sender, DocumentChangedEventArgs e)
{
//listId.Clear();
//ICollection<ElementId> collection = e.GetAddedElementIds();//获取创建的门的ids
id = new ElementId(313106);
IdleFlag = true;
uiApp.Application.DocumentChanged -= new EventHandler<DocumentChangedEventArgs>(DocumentChangedForSomething);//注销本事件
}
public void IdlingHandler(object sender, IdlingEventArgs args)
{
UIApplication uiapp = sender as UIApplication;
if (!IdleFlag)//true
{
return;//继续执行
}
ExecuteIdlingHandler.Execute(uiapp, id);//删除创建的门
IdleFlag = false;
uiApp.Idling -= new EventHandler<IdlingEventArgs>(IdlingHandler);//注销
}
}