zoukankan      html  css  js  c++  java
  • Revit 二次开发 事件练习

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

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

    应用级别文件修改事件

    创建项目WPF

    创建Command类

    添加引用

    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
    {
        /// <summary>
        /// 创建应用级别的事件
        /// </summary>
        [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
    {
        /// <summary>
        /// 创建文档级别的事件
        /// </summary>
        [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添加一个按钮

    给按钮添加一个事件

    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
    {
        /// <summary>
        /// MainWindow.xaml 的交互逻辑
        /// </summary>
        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
    {
        /// <summary>
        /// 创建文档级别的事件
        /// </summary>
        [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
    {
        /// <summary>
        /// 创建文档级别的事件
        /// </summary>
        [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 "名称";
            }
        }
    }

    测试

  • 相关阅读:
    ExecuteScalar requires the command to have a transaction when the connection assigned to the command is in a pending
    如何从vss中分离程序
    String or binary data would be truncated
    the pop3 service failed to retrieve authentication type and cannot continue
    The POP3 service failed to start because
    IIS Error he system cannot find the file specified _找不到页面
    pku2575Jolly Jumpers
    pku2940Wine Trading in Gergovia
    pku3219二项式系数
    pku1029false coin
  • 原文地址:https://www.cnblogs.com/chenyanbin/p/13288205.html
Copyright © 2011-2022 走看看