Revit开发之非模态对话框下循环执行外部事件 WPF

个人参考文章时,自行练习程序。

疑惑:“参考文章1”中没有了事件注册和注销,“参考文章2”中说注册和注销发生变化,但是不理解无法融合。请知道的各位告知谢谢

参考文章1:Revit开发之外部事件IExternalEventHandler

参考文章2:RevitAPI: 注意Revit 2016事件注册和注销行为的改变

参考文章3:Revit二次开发——非模态窗口(将Transaction写在了Button里,很不好啊。。。

循环执行:

for,while,用键盘钩子判断结束条件 (例如判断按下了esc,然后终止循环 【不会,待学习】)

版本:Revit2016

实现代码:Revit开发之非模态对话框下循环执行外部事件 WPF_第1张图片

具体代码:

窗体:

Revit开发之非模态对话框下循环执行外部事件 WPF_第2张图片

MyWin.xaml.cs:

using Autodesk.Revit.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using static ClassLibrary1.Class1;


namespace ClassLibrary1
{
    /// 
    /// MyWin.xaml 的交互逻辑
    /// 
    public partial class MyWin : Window
    {
        NewWall myCommand = null;
        ExternalEvent hander = null;
        public MyWin()
        {
            InitializeComponent();
            myCommand = new NewWall();
            hander = ExternalEvent.Create(myCommand);
        }
        private void myBtn_Click(object sender, RoutedEventArgs e)
        {
            hander.Raise();
        }
    }
}


Class1.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.DB.Plumbing;
using System.Windows;
using Autodesk.Revit.ApplicationServices;
using System.Threading;
using System.IO;
using Autodesk.Revit.DB.Events;
using Autodesk.Revit.UI.Events;
using static ClassLibrary1.MyWin;




namespace ClassLibrary1
{
    [Transaction(TransactionMode.Manual)]
    [Regeneration(RegenerationOption.Manual)]
    public class Class1 : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            MyWin myWin = new MyWin();
            myWin.Show();
            return Result.Succeeded;
        }
    }


    public class NewWall : IExternalEventHandler    //可不可以在IExternalEventHandler下的Execute函数中注册事件,并在IExternalEventHandler下写事件  
    {
        public void Execute(UIApplication app)
        {
            UIDocument uidoc = app.ActiveUIDocument;
            Document doc = uidoc.Document;
            Selection sel = uidoc.Selection;
            FilteredElementCollector lvlFilter = new FilteredElementCollector(doc);
            lvlFilter.OfClass(typeof(Level));
            Level lvl = lvlFilter.First() as Level;


            int n = 2;//循环执行


            Transaction tans = new Transaction(doc, "New Wall");
            tans.Start();
            for (int i=0;i


你可能感兴趣的:(Revit二次开发,WPF)