イベントの使用は、2 つの手順で処理されます。最初に、イベントの通知を処理する関数が必要です。この関数は次の 2 つのパラメータを取得します。最初のパラメータはイベント通知の「送信者」を示す Object で、2 番目はイベントに固有のイベント引数が含まれているイベント固有のオブジェクトです。 たとえば、DocumentSavingAs イベントを登録するには、イベント ハンドラは DocumentSavingAsEventArgs オブジェクトである 2 番目のパラメータを取る必要があります。
イベントの使用についての 2 番目は、Revit を使用してイベントを登録することです。これは、ControlledApplication パラメータからOnStartup()関数を使用して即時に、または Revit の起動後にいつでも実行できます。イベントは、外部アプリケーションと同様に外部コマンドに登録することができますが、外部コマンドが同じ外部コマンド内でイベントを登録および登録を解除しない限り推奨しません。
次の例では、DocumentOpened イベントを登録し、イベントがトリガされたときに、このアプリケーションがプロジェクトのアドレスを設定しています。
コード領域 24-1: Application.DocumentOpened を登録 |
public class Application_DocumentOpened : IExternalApplication { public IExternalApplication.Result OnStartup(ControlledApplication application) { try { // Register event. application.DocumentOpened += new EventHandler <Autodesk.Revit.Events.DocumentOpenedEventArgs>(application_DocumentOpened); } catch (Exception) { return Autodesk.Revit.UI.Result.Failed; } return Autodesk.Revit.UI.Result.Succeeded; } public IExternalApplication.Result OnShutdown(ControlledApplication application) { // remove the event. application.DocumentOpened -= application_DocumentOpened; return Autodesk.Revit.UI.Result.Succeeded; } public void application_DocumentOpened(object sender, DocumentOpenedEventArgs args) { // get document from event args. Document doc = args.Document; Transaction transaction = new Transaction(doc, "Edit Address"); if (transaction.Start() == TransactionStatus.Started) { doc.ProjectInformation.Address = "United States - Massachusetts - Waltham - 1560 Trapelo Road"; transaction.Commit(); } } } |