您要查找的事件是 OnDocumentOpened(如果您希望它在模型打开后运行)或 OnDocumentOpening(如果您希望它在模型打开之前运行)。
您需要将事件处理程序添加到应用程序的 OnStartup 方法中:
public Result OnStartup(UIControlledApplication application) {
application.ControlledApplication.DocumentOpened += OnDocOpened;
//Rest of your code here...
return Result.Succeeded;
}
private void OnDocOpened(object sender, DocumentOpenedEventArgs args) {
Autodesk.Revit.ApplicationServices.Application app = (Autodesk.Revit.ApplicationServices.Application)sender;
Document doc = args.Document;
//Your code here...
}
您还应该在应用程序的 OnShutdown 方法中删除事件处理程序:
public Result OnShutdown(UIControlledApplication application) {
application.ControlledApplication.DocumentOpened -= OnDocOpened;
//Rest of your code here...
return Result.Succeeded;
}