日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Revit二次开发之双事件:空闲事件与DocumentChanged事件

發(fā)布時間:2023/12/20 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Revit二次开发之双事件:空闲事件与DocumentChanged事件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

對以下文章所展示的代碼進行了修改,當前展示的代碼可直接復制使用。

由于本人初學,下列語言為個人理解,如有錯誤請指正。

?

《引用1》中:

  • 使用模態(tài)對話框,或者盡量保證事件在ExternalCommand的Execute函數或者ExternalApplication的OnStartup函數里面注冊。
  • 如果一定要使用非模態(tài)對話框,那么請使用ExternalEvent事件的Raise函數,強迫程序進入Revit主線程,然后在里面進行注冊事件。

?

因為《引用1》和《引用2》都沒有OnStartup,所以再沒有例子的情況下暫時不會使用。

《原文章出處》:雙事件使用時,在IExternalCommand的Execute函數中進行了注冊,在各自的響應函數中進行了注銷。

《引用1》:為WinFrom程序,并且同時有Raise函數和注冊/注銷行為,因為沒用過WinFrom感覺不會用、沒看懂。。。

《引用2》:為WPF程序,并且使用非模態(tài)對話框的使用情況,沒有注冊和注銷行為。

?

對比三篇文章的時間,更傾向于參考《原文章出處》《引用2》,《引用1》的內容稍后會再進行復盤?!居懈玫睦诱埢貜透嬷?/span>

?

原文章出處(2016-09-28):http://blog.sina.com.cn/s/blog_16796559b0102y8ey.html

引用1(2015-09-09):RevitAPI: 注意Revit 2016事件注冊和注銷行為的改變

引用2(2016-09-20):Revit開發(fā)之外部事件IExternalEventHandler

引用2中的例子本人重新練習做的個人筆記,代碼可直接復制使用:Revit開發(fā)之非模態(tài)對話框下的外部事件 WPF

環(huán)境:Revit2016

原文:

前段時間做Revit開發(fā)項目時遇到一個棘手的問題,我們都知道Revit運行環(huán)境是不允許多線程并發(fā)的,也就是說同一時刻只能有一個事務或操作運行,然后可以根據具體情況去觸發(fā)不同的響應事件,如DocumentOpend,DocumentChanged等,那么問題是什么呢?問題是有時我們希望通過外部命令PostCommand調用Revit本身的命令,如調用繪制橋架命令,但是我們希望在此命令執(zhí)行后去觸發(fā)事件,后來經過測試發(fā)現PostCommand不會阻止線程的運行,而且還是外部命令運行完后才觸發(fā),那么我們就無法拿到PostCommand觸發(fā)完成后的事件了,所以我們需要一個全局監(jiān)控事件,來監(jiān)控活動文檔的改變,所以想到了DocumentChanged事件。但是發(fā)現在DocumentChanged事件里不能開始事務,也沒用了,最后想到用Revit空閑事件和DocumentChanged事件相結合來做,首先我們在外部命令下注冊這兩個事件,由DocumentChanged事件觸發(fā)空閑事件,在空閑事件里執(zhí)行響應的事務,最后再注銷兩個事件,到此問題得到有效解決。當然除了Idling還可以用ExternalEvent來做配合!

?

本代碼實現調用PostCommand命令放置一個門,然后觸發(fā)相應的事件再刪除此門,也可以擴展其他功能,本目的就是為了測試事務是否能成功開啟

?

?

?

?

新代碼為:

程序步驟:門命令→創(chuàng)建了多個門→創(chuàng)建獲取這些門的id→獲取創(chuàng)建的門的ids并添加進集合→注銷DocumentChanged事件→程序結束→啟動空閑事件,刪除

using Autodesk.Revit.UI; using Autodesk.Revit.DB; using Autodesk.Revit.Attributes; using Autodesk.Revit.UI.Selection; using Autodesk.Revit.DB.Plumbing; using Autodesk.Revit.DB.Mechanical; using System.Xml; using Autodesk.Revit.UI.Events; using System; using System.Collections.Generic; using System.Linq; using Autodesk.Revit.DB.Events;namespace HelloWorld {[Transaction(TransactionMode.Manual)][Regeneration(RegenerationOption.Manual)]public class Test : IExternalCommand{ //定義一個字段通過屬性變化來觸發(fā)空閑事件public static Boolean IdleFlag = false;//記錄DocumentChanged事件發(fā)生改變的構件IList<ElementId> listId = new List<ElementId>();//定義一個全局UIApplication,用來注銷指定事件UIApplication uiApp = null;public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements){uiApp = commandData.Application;UIDocument uiDoc = uiApp.ActiveUIDocument;Document Doc = uiDoc.Document;uiApp.PostCommand(RevitCommandId.LookupPostableCommandId(PostableCommand.Door));//調用“門”命令,用戶創(chuàng)建門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();//獲取創(chuàng)建的門的idslistId.Add(collection.ElementAt(0));IdleFlag = true;uiApp.Application.DocumentChanged -= new EventHandler<DocumentChangedEventArgs>(DocumentChangedForSomething);//注銷本事件}//Revit空閑事件public void IdlingHandler(object sender, IdlingEventArgs args){UIApplication uiapp = sender as UIApplication;if (!IdleFlag)//true{return;//繼續(xù)執(zhí)行}ExecuteIdlingHandler.Execute(uiapp, listId);//刪除創(chuàng)建的門IdleFlag = false;uiApp.Idling -= new EventHandler<IdlingEventArgs>(IdlingHandler);//注銷}}public static class ExecuteIdlingHandler{public static void Execute(UIApplication uiapp, IList<ElementId> listId){UIDocument uidoc = uiapp.ActiveUIDocument;if (uidoc != null){Transaction ts = new Transaction(uidoc.Document, "delete");ts.Start();uidoc.Document.Delete(listId);ts.Commit();}}} }

?

?

?

?

?

總結

以上是生活随笔為你收集整理的Revit二次开发之双事件:空闲事件与DocumentChanged事件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。