Revit API 开发 (7): Event 事件的应用

前言

Event 是一个在各种编程语言中,都非常重要的概念。它是一种重要的设计模式,Revit API 也需要利用它来完成一些工作。
在介绍 Event 之前,首先说明一下Revit API 中的两个 Application 类。Autodesk.Revit.Creation.ApplicationAutodesk.Revit.ApplicationServices.Application
前者主要功能是在 Revit 里创建新的元素,比如族实例信息,几何图形等等。
后者主要功能包括文档的创建和打开,文档整体的操作,还有 Event 事件处理。

细节

例子

在下面这个例子中:

  1. 使用 IExternalApplication,在它的 OnStartup 中注册事件,在它的 OnShutdown 中取消事件。
  2. 事件处理方法是在注册的时候传进去的,这里是 application_DocumentOpened
  3. 在事件处理方法中,可以编写你自己的逻辑,他会在文档打开之后执行。
public class Application_DocumentOpened : IExternalApplication
{
    public Result OnStartup(UIControlledApplication application)
    {
        application.ControlledApplication.DocumentOpened += new EventHandler
             <Autodesk.Revit.DB.Events.DocumentOpenedEventArgs>(application_DocumentOpened);
        return Result.Succeeded;
    }

    public Result OnShutdown(UIControlledApplication application)
    {
       application.ControlledApplication.DocumentOpened -= application_DocumentOpened;
        return Result.Succeeded;
    }

    public void application_DocumentOpened(object sender, DocumentOpenedEventArgs args)
    {
        Document doc = args.Document;
        using (Transaction transaction = new Transaction(doc, "Edit Address"))
        {
           if (transaction.Start() == TransactionStatus.Started)
           {
              // 在这里,你可以编写你想要的操作。
              doc.ProjectInformation.Address =
                  "United States - Massachusetts - Waltham - 1560 Trapelo Road";
              transaction.Commit();
           }
        }
    }
}

事件类型

Autodesk.Revit.ApplicationServices.Application 类中的主要事件:

作用级别 / 范围事件
整个应用ApplicationInitialized
文档操作DocumentChanged 以及DocumentXXX
导入导出FileExporting, FileImported, FileImporting
链接文件LinkedResourceOpened, LinkedResourceOpening
视图导出/打印ViewExported, ViewExporting, ViewPrinted, ViewPrinting
文件协同DocumentSynchronizedWithCentral, DocumentSynchronizingWithCentral, WorksharedOperationProgressChanged
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

极客BIM工作室

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值