Revit二次开发之ExternalEvent实现非模态窗体

Revit从2013版之后就不允许在外部窗体下直接开启事务,当然我们可以使用模态窗体阻止线程的运行,其实说白了窗体不就是用户与程序的交互界面么,在窗体上设置好数据然后传递给主程序么,但是我们需要更强的数据交互功能,或者连续的命令调用。那么这个时候模态窗体就显得非常笨拙了,好在RevitAPI给我们提供了两个外部事件,一个是Idling,一个是ExternalEvent,那么上篇我们已经讲过Idling事件了,这里就不在赘述了,接下来我们使用ExternalEvent实现非模态窗体命令,其实很简单哦!——兵者,诡道也!故能而示之不能,用而示之不用,近而示之远,远而示之近。所以不要怕问题,大不了兵来将挡,水来土掩而已!

 

类代码

using Autodesk.Revit.UI;

using Autodesk.Revit.DB;

using Autodesk.Revit.Attributes;

using Autodesk.Revit.UI.Selection;

using Autodesk.Revit.UI.Events;

 

namespace HelloWorld

{

    [Transaction(TransactionMode.Manual)]

    [Regeneration(RegenerationOption.Manual)]

    public class Test : IExternalCommand

    {  

        public  Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)

        {

            UIApplication uiApp = commandData.Application;

            UIDocument uiDoc = uiApp.ActiveUIDocument;

            Document Doc = uiDoc.Document;

            Form1 frm = new Form1();

            frm.Show();             

            return Result.Succeeded;

        }

       

    }

   

}

 

窗体代码

using System.Windows.Forms;

using Autodesk.Revit.UI;

using Autodesk.Revit.DB;

 

namespace HelloWorld

{

    public partial class Form1 : System.Windows.Forms.Form

    {

        ExecuteEvent Exc = null;

        ExternalEvent eventHandler = null;

        public Form1()

        {

            InitializeComponent();          

        }

 

        private void Form1_Load(object sender, EventArgs e)

        {

            Exc = new ExecuteEvent();

            eventHandler = ExternalEvent.Create(Exc);

        }

 

        private void button1_Click(object sender, EventArgs e)

        {

            eventHandler.Raise();

        }

    }

  //新建一个类 继承 IExternalEventHandler接口

    public class ExecuteEvent : IExternalEventHandler

    {

        public void Execute(UIApplication app)

        {

            Document doc = app.ActiveUIDocument.Document;

            UIDocument uidoc = app.ActiveUIDocument;

            Reference refer = uidoc.Selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, "");

            Transaction ts = new Transaction(doc,"BIM");

            ts.Start();

            Element elem = doc.GetElement(refer);

            doc.Delete(elem.Id);

            ts.Commit();

        }

       //估计是记录外部事件名称的 和事务名称相同

        public string GetName()

        {

            return "this is a Test";

        }

    }

 

}


Revit二次开发之ExternalEvent实现非模态窗体_第1张图片

 

 

 

 

 

你可能感兴趣的:(Revit二次开发之ExternalEvent实现非模态窗体)