zoukankan      html  css  js  c++  java
  • Revit二次开发示例:EventsMonitor

    在该示例中,插件在Revit启动时弹出事件监控选择界面,供用户设置,也可在添加的Ribbon界面完成设置。当Revit进行相应操作时,弹出窗体会记录事件时间和名称。

    #region Namespaces
    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    using Autodesk.Revit.ApplicationServices;
    using Autodesk.Revit.Attributes;
    using Autodesk.Revit.DB;
    using Autodesk.Revit.UI;
    #endregion
    
    namespace EventsMonitor
    {
        [Autodesk.Revit.Attributes.Transaction(TransactionMode.Manual)]
        [Autodesk.Revit.Attributes.Regeneration(RegenerationOption.Manual)]
        [Autodesk.Revit.Attributes.Journaling(JournalingMode.NoCommandData)]
        class App : IExternalApplication
        {
            private static UIControlledApplication m_ctrlApp;
            private static LogManager m_logManager;
            private static EventsInfoWindows m_infWindows;
            private static EventsSettingForm m_settingDialog;
            private static List<String> m_appEventsSelection;
            private static EventManager m_appEventMgr;
    
            public static EventsInfoWindows InfoWindows
            {
                get
                {
                    if (null == m_infWindows)
                    {
                        m_infWindows = new EventsInfoWindows();
                    }
                    return m_infWindows;
                }
                set
                {
                    m_infWindows = value;
                }
            }
    
            public static EventsSettingForm SettingDialog
            {
                get
                {
                    if (null == m_settingDialog)
                    {
                        m_settingDialog = new EventsSettingForm();
                    }
                    return m_settingDialog;
                }
            }
    
            public static LogManager EventLogManager
            {
                get
                {
                    if (null == m_logManager)
                    {
                        m_logManager = new LogManager();
                    }
                    return m_logManager;
                }
    
            }
    
            public static List<String> ApplicationEvents
            {
                get
                {
                    if (null == m_appEventsSelection)
                    {
                        m_appEventsSelection = new List<string>();
                    }
                    return m_appEventsSelection;
                }
                set
                {
                    m_appEventsSelection = value;
                }
            }
    
            public static EventManager AppEventMgr
            {
                get
                {
                    if (null == m_appEventMgr)
                    {
                        m_appEventMgr = new EventManager(m_ctrlApp);
                    }
                    return m_appEventMgr;
                }
            }
    
    
            public Result OnStartup(UIControlledApplication a)
            {
                m_ctrlApp = a;
                m_logManager = new LogManager();
                m_infWindows = new EventsInfoWindows(m_logManager);
                m_settingDialog = new EventsSettingForm();
                m_appEventsSelection = new List<string>();
                m_appEventMgr = new EventManager(m_ctrlApp);
    
                try
                {
                    m_settingDialog.ShowDialog();
                    if (DialogResult.OK == m_settingDialog.DialogResult)
                    {
                        m_appEventsSelection = m_settingDialog.AppSelectionList;
                    }
    
                    m_appEventMgr.Update(m_appEventsSelection);
                    m_infWindows.Show();
                    AddCustomPanel(a);
                }
                catch (Exception)
                {
                    return Result.Failed;
                }
    
                return Result.Succeeded;
            }
    
            public Result OnShutdown(UIControlledApplication a)
            {
                Dispose();
                return Result.Succeeded;
            }
    
            public static void Dispose()
            {
                if (m_infWindows != null)
                {
                    m_infWindows.Close();
                    m_infWindows = null;
                }
                if (m_settingDialog != null)
                {
                    m_settingDialog.Close();
                    m_settingDialog = null;
                }
                m_appEventMgr = null;
                m_logManager.CloseLogFile();
                m_logManager = null;
            }
    
            static private void AddCustomPanel(UIControlledApplication application)
            {
                string panelName = "Events Monitor";
                RibbonPanel ribbonPanelPushButtons = application.CreateRibbonPanel(panelName);
                PushButtonData pushButtonData = new PushButtonData("EventsSetting",
                    "Set Events", System.Reflection.Assembly.GetExecutingAssembly().Location,
                    "EventsMonitor.Command");
                PushButton pushButtonCreateWall = ribbonPanelPushButtons.AddItem(pushButtonData) as PushButton;
                pushButtonCreateWall.ToolTip = "Setting Events";
            }
    
        }
    }
    View Code
    #region Namespaces
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Windows.Forms;
    using Autodesk.Revit.ApplicationServices;
    using Autodesk.Revit.Attributes;
    using Autodesk.Revit.DB;
    using Autodesk.Revit.UI;
    using Autodesk.Revit.UI.Selection;
    #endregion
    
    namespace EventsMonitor
    {
        [Autodesk.Revit.Attributes.Transaction(TransactionMode.Manual)]
        [Autodesk.Revit.Attributes.Regeneration(RegenerationOption.Manual)]
        [Autodesk.Revit.Attributes.Journaling(JournalingMode.NoCommandData)]
        public class Command : IExternalCommand
        {
            public Result Execute(
              ExternalCommandData commandData,
              ref string message,
              ElementSet elements)
            {
                IDictionary<string, string> journaldata = commandData.JournalData;
                App.SettingDialog.ShowDialog();
                if (DialogResult.OK == App.SettingDialog.DialogResult)
                {
                    App.ApplicationEvents = App.SettingDialog.AppSelectionList;
                }
    
                App.AppEventMgr.Update(App.ApplicationEvents);
                App.InfoWindows.Show();
    
                return Result.Succeeded;
            }
        }
    }
    View Code
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Autodesk.Revit.DB.Events;
    using Autodesk.Revit.UI;
    using Autodesk.Revit.UI.Events;
    
    namespace EventsMonitor
    {
        public class EventManager
        {
            private UIControlledApplication m_app;
            private List<String> historySelection;
    
            private EventManager()
            {
    
            }
    
            public EventManager(UIControlledApplication app)
            {
                m_app = app;
                historySelection = new List<string>();
            }
    
            public void Update(List<String> selection)
            {
                foreach (String eventname in historySelection)
                {
                    if (!selection.Contains(eventname))
                    {
                        subtractEvents(eventname);
                    }
                }
    
                foreach (String eventname in selection)
                {
                    if (!historySelection.Contains(eventname))
                    {
                        addEvents(eventname);
                    }
                }
    
                historySelection.Clear();
                foreach (String eventname in selection)
                {
                    historySelection.Add(eventname);
                }
    
            }
    
            private void addEvents(String eventName)
            {
                switch (eventName)
                {
                    case "DocumentCreating":
                        m_app.ControlledApplication.DocumentCreating += new EventHandler<DocumentCreatingEventArgs>(app_eventsHandlerMethod);
                        break;
                    case "DocumentCreated":
                        m_app.ControlledApplication.DocumentCreated += new EventHandler<DocumentCreatedEventArgs>(app_eventsHandlerMethod);
                        break;
                    case "DocumentOpening":
                        m_app.ControlledApplication.DocumentOpening += new EventHandler<DocumentOpeningEventArgs>(app_eventsHandlerMethod);
                        break;
                    case "DocumentOpened":
                        m_app.ControlledApplication.DocumentOpened += new EventHandler<DocumentOpenedEventArgs>(app_eventsHandlerMethod);
                        break;
                    case "DocumentClosing":
                        m_app.ControlledApplication.DocumentClosing += new EventHandler<DocumentClosingEventArgs>(app_eventsHandlerMethod);
                        break;
                    case "DocumentClosed":
                        m_app.ControlledApplication.DocumentClosed += new EventHandler<DocumentClosedEventArgs>(app_eventsHandlerMethod);
                        break;
                    case "DocumentSavedAs":
                        m_app.ControlledApplication.DocumentSavedAs += new EventHandler<DocumentSavedAsEventArgs>(app_eventsHandlerMethod);
                        break;
                    case "DocumentSavingAs":
                        m_app.ControlledApplication.DocumentSavingAs += new EventHandler<DocumentSavingAsEventArgs>(app_eventsHandlerMethod);
                        break;
                    case "DocumentSaving":
                        m_app.ControlledApplication.DocumentSaving += new EventHandler<DocumentSavingEventArgs>(app_eventsHandlerMethod);
                        break;
                    case "DocumentSaved":
                        m_app.ControlledApplication.DocumentSaved += new EventHandler<DocumentSavedEventArgs>(app_eventsHandlerMethod);
                        break;
                    case "DocumentSynchronizingWithCentral":
                        m_app.ControlledApplication.DocumentSynchronizingWithCentral += new EventHandler<DocumentSynchronizingWithCentralEventArgs>(app_eventsHandlerMethod);
                        break;
                    case "DocumentSynchronizedWithCentral":
                        m_app.ControlledApplication.DocumentSynchronizedWithCentral += new EventHandler<DocumentSynchronizedWithCentralEventArgs>(app_eventsHandlerMethod);
                        break;
                    case "FileExporting":
                        m_app.ControlledApplication.FileExporting += new EventHandler<FileExportingEventArgs>(app_eventsHandlerMethod);
                        break;
                    case "FileExported":
                        m_app.ControlledApplication.FileExported += new EventHandler<FileExportedEventArgs>(app_eventsHandlerMethod);
                        break;
                    case "FileImporting":
                        m_app.ControlledApplication.FileImporting += new EventHandler<FileImportingEventArgs>(app_eventsHandlerMethod);
                        break;
                    case "FileImported":
                        m_app.ControlledApplication.FileImported += new EventHandler<FileImportedEventArgs>(app_eventsHandlerMethod);
                        break;
                    case "DocumentPrinting":
                        m_app.ControlledApplication.DocumentPrinting += new EventHandler<DocumentPrintingEventArgs>(app_eventsHandlerMethod);
                        break;
                    case "DocumentPrinted":
                        m_app.ControlledApplication.DocumentPrinted += new EventHandler<DocumentPrintedEventArgs>(app_eventsHandlerMethod);
                        break;
                    case "ViewPrinting":
                        m_app.ControlledApplication.ViewPrinting += new EventHandler<ViewPrintingEventArgs>(app_eventsHandlerMethod);
                        break;
                    case "ViewPrinted":
                        m_app.ControlledApplication.ViewPrinted += new EventHandler<ViewPrintedEventArgs>(app_eventsHandlerMethod);
                        break;
                    case "ViewActivating":
                        m_app.ViewActivating += new EventHandler<ViewActivatingEventArgs>(app_eventsHandlerMethod);
                        break;
                    case "ViewActivated":
                        m_app.ViewActivated += new EventHandler<ViewActivatedEventArgs>(app_eventsHandlerMethod);
                        break;
                    case "ProgressChanged":
                        m_app.ControlledApplication.ProgressChanged += new EventHandler<ProgressChangedEventArgs>(app_eventsHandlerMethod);
                        break;
                }
            }
    
            private void subtractEvents(String eventName)
            {
                switch (eventName)
                {
                    case "DocumentCreating":
                        m_app.ControlledApplication.DocumentCreating -= app_eventsHandlerMethod;
                        break;
                    case "DocumentCreated":
                        m_app.ControlledApplication.DocumentCreated -= app_eventsHandlerMethod;
                        break;
                    case "DocumentOpening":
                        m_app.ControlledApplication.DocumentOpening -= app_eventsHandlerMethod;
                        break;
                    case "DocumentOpened":
                        m_app.ControlledApplication.DocumentOpened -= app_eventsHandlerMethod;
                        break;
                    case "DocumentClosing":
                        m_app.ControlledApplication.DocumentClosing -= app_eventsHandlerMethod;
                        break;
                    case "DocumentClosed":
                        m_app.ControlledApplication.DocumentClosed -= app_eventsHandlerMethod;
                        break;
                    case "DocumentSavedAs":
                        m_app.ControlledApplication.DocumentSavedAs -= app_eventsHandlerMethod;
                        break;
                    case "DocumentSavingAs":
                        m_app.ControlledApplication.DocumentSavingAs -= app_eventsHandlerMethod;
                        break;
                    case "DocumentSaving":
                        m_app.ControlledApplication.DocumentSaving -= app_eventsHandlerMethod;
                        break;
                    case "DocumentSaved":
                        m_app.ControlledApplication.DocumentSaved -= app_eventsHandlerMethod;
                        break;
                    case "DocumentSynchronizingWithCentral":
                        m_app.ControlledApplication.DocumentSynchronizingWithCentral -= app_eventsHandlerMethod;
                        break;
                    case "DocumentSynchronizedWithCentral":
                        m_app.ControlledApplication.DocumentSynchronizedWithCentral -= app_eventsHandlerMethod;
                        break;
                    case "FileExporting":
                        m_app.ControlledApplication.FileExporting -= app_eventsHandlerMethod;
                        break;
                    case "FileExported":
                        m_app.ControlledApplication.FileExported -= app_eventsHandlerMethod;
                        break;
                    case "FileImporting":
                        m_app.ControlledApplication.FileImporting -= app_eventsHandlerMethod;
                        break;
                    case "FileImported":
                        m_app.ControlledApplication.FileImported -= app_eventsHandlerMethod;
                        break;
                    case "DocumentPrinting":
                        m_app.ControlledApplication.DocumentPrinting -= app_eventsHandlerMethod;
                        break;
                    case "DocumentPrinted":
                        m_app.ControlledApplication.DocumentPrinted -= app_eventsHandlerMethod;
                        break;
                    case "ViewPrinting":
                        m_app.ControlledApplication.ViewPrinting -= app_eventsHandlerMethod;
                        break;
                    case "ViewPrinted":
                        m_app.ControlledApplication.ViewPrinted -= app_eventsHandlerMethod;
                        break;
                    case "ViewActivating":
                        m_app.ViewActivating -= app_eventsHandlerMethod;
                        break;
                    case "ViewActivated":
                        m_app.ViewActivated -= app_eventsHandlerMethod;
                        break;
                    case "ProgressChanged":
                        m_app.ControlledApplication.ProgressChanged -= app_eventsHandlerMethod;
                        break;
                }
            }
    
            public void app_eventsHandlerMethod(Object obj, EventArgs args)
            {
                // generate event information and set to information window 
                // to track what event be touch off.
                App.EventLogManager.TrackEvent(obj, args);
                // write log file.
                App.EventLogManager.WriteLogFile(obj, args);
            }
    
        }
    }
    View Code
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace EventsMonitor
    {
        public partial class EventsInfoWindows : Form
        {
            private LogManager m_dataBuffer;
    
    
    
            public EventsInfoWindows()
            {
                InitializeComponent();
            }
    
            public EventsInfoWindows(LogManager dataBuffer)
                : this()
            {
                m_dataBuffer = dataBuffer;
                Initialize();
            }
    
            private void Initialize()
            {
                appEventsLogDataGridView.AutoGenerateColumns = false;
                appEventsLogDataGridView.DataSource = m_dataBuffer.EventsLog;
                timeColumn.DataPropertyName = "Time";
                eventColumn.DataPropertyName = "Event";
                typeColumn.DataPropertyName = "Type";
            }
    
            private void EventsInfoWindows_FormClosed(object sender, FormClosedEventArgs e)
            {
                App.InfoWindows = null;
            }
    
            private void EventsInfoWindows_Shown(object sender, EventArgs e)
            {
                int left = Screen.PrimaryScreen.WorkingArea.Right - this.Width - 5;
                int top = Screen.PrimaryScreen.WorkingArea.Bottom - this.Height;
                Point windowLocation = new Point(left, top);
                this.Location = windowLocation;
            }
    
            private void appEventsLogDataGridView_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
            {
                appEventsLogDataGridView.CurrentCell = appEventsLogDataGridView.Rows[appEventsLogDataGridView.Rows.Count - 1].Cells[0];
            }
    
    
    
        }
    }
    View Code
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace EventsMonitor
    {
        public partial class EventsSettingForm : Form
        {
    
            private List<String> m_appSelection;
    
            public List<String> AppSelectionList
            {
                get
                {
                    if (null == m_appSelection)
                    {
                        m_appSelection = new List<string>();
                    }
                    return m_appSelection;
                }
                set
                {
                    m_appSelection = value;
                }
            }
    
            public EventsSettingForm()
            {
                InitializeComponent();
                m_appSelection = new List<string>();
            }
    
            private void FinishToggle_Click(object sender, EventArgs e)
            {
                m_appSelection.Clear();
                foreach (object item in AppEventsCheckedList.CheckedItems)
                {
                    m_appSelection.Add(item.ToString());
                }
                this.DialogResult = DialogResult.OK;
                this.Hide();
            }
    
            private void EventsSettingForm_FormClosed(object sender, FormClosedEventArgs e)
            {
                this.Hide();
            }
    
            private void checkAllButton_Click(object sender, EventArgs e)
            {
                for (int i = 0; i < AppEventsCheckedList.Items.Count; i++ )
                {
                    AppEventsCheckedList.SetItemChecked(i, true);
                }
            }
    
            private void checkNoneButton_Click(object sender, EventArgs e)
            {
                for (int i = 0; i < AppEventsCheckedList.Items.Count; i++)
                {
                    AppEventsCheckedList.SetItemChecked(i, false);
                }
            }
    
            private void cancelButton_Click(object sender, EventArgs e)
            {
                this.DialogResult = DialogResult.Cancel;
                this.Hide();
            }
    
    
        }
    }
    View Code
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Diagnostics;
    using System.IO;
    using System.Linq;
    using System.Reflection;
    using System.Text;
    
    namespace EventsMonitor
    {
        public class LogManager
        {
            private DataTable m_eventsLog;
            private TextWriterTraceListener m_txtListener;
            private string m_filePath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
            private string m_tempFile;
    
            public DataTable EventsLog
            {
                get
                {
                    return m_eventsLog;
                }
            }
    
            public LogManager()
            {
                CreateLogFile();
                m_eventsLog = CreateEventsLogTable();
            }
    
            private void CreateLogFile()
            {
                m_tempFile = Path.Combine(m_filePath, "Temp.log");
                if (File.Exists(m_tempFile)) File.Delete(m_tempFile);
                m_txtListener = new TextWriterTraceListener(m_tempFile);
                Trace.Listeners.Add(m_txtListener);
            }
    
            public void CloseLogFile()
            {
                Trace.Flush();
                Trace.Listeners.Remove(m_txtListener);
                Trace.Close();
                m_txtListener.Close();
    
                string log = Path.Combine(m_filePath, "EventsMonitor.log");
                if (File.Exists(log)) File.Delete(log);
                File.Copy(m_tempFile, log);
                File.Delete(m_tempFile);
            }
    
            private DataTable CreateEventsLogTable()
            {
                DataTable eventsInfoLogTable = new DataTable("EventsLogInfoTable");
    
                DataColumn timeColumn = new DataColumn("Time", typeof(System.String));
                timeColumn.Caption = "Time";
                eventsInfoLogTable.Columns.Add(timeColumn);
    
                DataColumn eventColum = new DataColumn("Event", typeof(System.String));
                eventColum.Caption = "Event";
                eventsInfoLogTable.Columns.Add(eventColum);
    
                DataColumn typeColumn = new DataColumn("Type", typeof(System.String));
                typeColumn.Caption = "Type";
                eventsInfoLogTable.Columns.Add(typeColumn);
    
                return eventsInfoLogTable;
            }
    
            public void TrackEvent(Object sender, EventArgs args)
            {
                DataRow newRow= m_eventsLog.NewRow();
                newRow["Time"] = System.DateTime.Now.ToString();
                newRow["Event"] = GetEventsName(args.GetType());
                newRow["Type"] = sender.GetType().ToString();
    
                m_eventsLog.Rows.Add(newRow);
            }
    
            public void WriteLogFile(Object sender, EventArgs args)
            {
                Trace.WriteLine("*********************************************************");
                if (null == args)
                {
                    return;
                }
    
                Type type = args.GetType();
                String eventName = GetEventsName(type);
                Trace.WriteLine("Raised " + sender.GetType().ToString() + "." + eventName);
                Trace.WriteLine("---------------------------------------------------------");
    
                Trace.WriteLine("  Start to dump Sender and EventArgs of Event... 
    ");
                if (null != sender)
                {
                    Trace.WriteLine("    [Event Sender]: " + sender.GetType().FullName);
                }
                else
                {
                    Trace.WriteLine("    Sender is null, it's unexpected!!!");
                }
    
    
                PropertyInfo[] propertyInfos = type.GetProperties();
    
                foreach (PropertyInfo propertyInfo in propertyInfos)
                {
                    try
                    {
                        if (!propertyInfo.CanRead)
                        {
                            continue;
                        }
                        else
                        {
                            Object propertyValue;
                            String propertyName = propertyInfo.Name;
                            switch (propertyName)
                            {
                                case "Document":
                                case "Cancellable":
                                case "Cancel":
                                case "Status":
                                case "DocumentType":
                                case "Format":
                                    propertyValue = propertyInfo.GetValue(args, null);
                                    // Dump current property value
                                    Trace.WriteLine("    [Property]: " + propertyInfo.Name);
                                    Trace.WriteLine("    [Value]: " + propertyValue.ToString());
                                    break;
                            }
                        }
    
                    }
                    catch (Exception ex)
                    {
                        Trace.WriteLine("    [Property Exception]: " + propertyInfo.Name + ", " + ex.Message);
                    }
                }
    
            }
    
    
            private String GetEventsName(Type type)
            {
                String argName = type.ToString();
                String tail = "EventArgs";
                String head = "Autodesk.Revit.DB.Events.";
                int firstIndex = head.Length;
                int length = argName.Length - head.Length - tail.Length;
                String eventName = argName.Substring(firstIndex, length);
                return eventName;
            }
    
        }
    }
    View Code
  • 相关阅读:
    MySQL(错误1064)
    如何判断是手机还是电脑访问网站
    Oracle表分区
    分离Date数据
    多对多
    一对多
    SQLalchemy基础
    paramiko上传下载
    paramiko
    automap
  • 原文地址:https://www.cnblogs.com/xpvincent/p/3611814.html
Copyright © 2011-2022 走看看