Revit 二次开发 事件练习

学习地址:https://www.bilibili.com/video/BV1mf4y1S72o?p=16

实例练习一(应用级别事件)

应用级别文件修改事件

 

创建项目WPF

Revit 二次开发 事件练习_第1张图片

 

Revit 二次开发 事件练习_第2张图片

 

创建Command类

Revit 二次开发 事件练习_第3张图片

添加引用

Revit 二次开发 事件练习_第4张图片

 

Command.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.DB.Events;

namespace ETest
{
    /// 
    /// 创建应用级别的事件
    /// 
    [TransactionAttribute(TransactionMode.Manual)]
    [RegenerationAttribute(RegenerationOption.Manual)]
    public class Command : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            commandData.Application.Application.DocumentChanged += appChange;
            return Result.Succeeded;
        }

        private void appChange(object sender, DocumentChangedEventArgs e)
        {
            TaskDialog.Show("改动", "已改动");
        }
    }
}

演示

实例练习二(文档级别)

修改刚才那个Command.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.DB.Events;

namespace ETest
{
    /// 
    /// 创建文档级别的事件
    /// 
    [TransactionAttribute(TransactionMode.Manual)]
    [RegenerationAttribute(RegenerationOption.Manual)]
    public class Command : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
        commandData.Application.ActiveUIDocument.Document.DocumentClosing += docCloing;
            return Result.Succeeded;
        }

        private void docCloing(object sender, DocumentClosingEventArgs e)
        {
            TaskDialog.Show("关闭", "已关闭");
        }
    }
}

演示

实例练习三(外部事件)

给WPF添加一个按钮

Revit 二次开发 事件练习_第5张图片

 

给按钮添加一个事件

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 Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB.Events;

namespace ETest
{
    /// 
    /// MainWindow.xaml 的交互逻辑
    /// 
    public partial class MainWindow : Window
    {
        ExternalEvent ee = null;
        ExternalCommand cmd =null;
        public MainWindow()
        {
            InitializeComponent();
            if (cmd==null)
            {
                cmd = new ExternalCommand();
            }
            if (ee==null)
            {
                ee = ExternalEvent.Create((IExternalEventHandler)cmd);
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            ee.Raise();
        }
    }
}

修改Command.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.DB.Events;

namespace ETest
{
    /// 
    /// 创建文档级别的事件
    /// 
    [TransactionAttribute(TransactionMode.Manual)]
    [RegenerationAttribute(RegenerationOption.Manual)]
    public class Command : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            //commandData.Application.Application.DocumentChanged += appChange;
            //commandData.Application.ActiveUIDocument.Document.DocumentClosing += docCloing;
            MainWindow mWin = new MainWindow();
            mWin.Show();
            return Result.Succeeded;
        }

        //private void docCloing(object sender, DocumentClosingEventArgs e)
        //{
        //    TaskDialog.Show("关闭", "已关闭");
        //}

        //private void appChange(object sender, DocumentChangedEventArgs e)
        //{
        //    TaskDialog.Show("改动", "已改动");
        //}
    }
    public class ExternalCommand : IExternalEventHandler
    {
        public void Execute(UIApplication app)
        {
            TaskDialog.Show("测试", "测试中");
        }

        public string GetName()
        {
            return "名称";
        }
    }
}

测试

实例练习四(闲置事件)

修改Command.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.DB.Events;
using Autodesk.Revit.UI.Events;

namespace ETest
{
    /// 
    /// 创建文档级别的事件
    /// 
    [TransactionAttribute(TransactionMode.Manual)]
    [RegenerationAttribute(RegenerationOption.Manual)]
    public class Command : IExternalCommand
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            //commandData.Application.Application.DocumentChanged += appChange;
            //commandData.Application.ActiveUIDocument.Document.DocumentClosing += docCloing;
            //MainWindow mWin = new MainWindow();
            //mWin.Show();
            commandData.Application.Idling += IdlingTest;
            return Result.Succeeded;
        }

        private void IdlingTest(object sender, IdlingEventArgs e)
        {
            UIApplication m_uiapp = sender as UIApplication;
            Autodesk.Revit.DB.Document m_doc = m_uiapp.ActiveUIDocument.Document;
            Transaction trans = new Transaction(m_doc, "idling");
            trans.Start();
            ElementId id = new ElementId(7366);
            TextNote tn = m_doc.GetElement(id) as TextNote;
            string str = tn.Text;
            int i = 0;
            int.TryParse(str, out i);
            tn.Text = (i + 1).ToString();
            trans.Commit();
        }


        //private void docCloing(object sender, DocumentClosingEventArgs e)
        //{
        //    TaskDialog.Show("关闭", "已关闭");
        //}

        //private void appChange(object sender, DocumentChangedEventArgs e)
        //{
        //    TaskDialog.Show("改动", "已改动");
        //}
    }
    public class ExternalCommand : IExternalEventHandler
    {
        public void Execute(UIApplication app)
        {
            TaskDialog.Show("测试", "测试中");
        }

        public string GetName()
        {
            return "名称";
        }
    }
}

测试

你可能感兴趣的:(Revit 二次开发 事件练习)