在该示例中,插件在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";
} }
}
#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;
}
}
}
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);
} }
}
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 - ;
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 - ].Cells[];
} }
}
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 = ; i < AppEventsCheckedList.Items.Count; i++ )
{
AppEventsCheckedList.SetItemChecked(i, true);
}
} private void checkNoneButton_Click(object sender, EventArgs e)
{
for (int i = ; i < AppEventsCheckedList.Items.Count; i++)
{
AppEventsCheckedList.SetItemChecked(i, false);
}
} private void cancelButton_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
this.Hide();
} }
}
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... \n");
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;
} }
}

Revit二次开发示例:EventsMonitor的更多相关文章

  1. Revit二次开发示例:HelloRevit

    本示例实现Revit和Revit打开的文件的相关信息. #region Namespaces using System; using System.Collections.Generic; using ...

  2. Revit二次开发示例:ErrorHandling

    本示例介绍了Revit的错误处理.   #region Namespaces using System; using System.Collections.Generic; using Autodes ...

  3. Revit二次开发示例:ChangesMonitor

    在本示例中,程序监控Revit打开文件事件,并在创建的窗体中更新文件信息.   #region Namespaces using System; using System.Collections.Ge ...

  4. Revit二次开发示例:AutoStamp

    该示例中,在Revit启动时添加打印事件,在打印时向模型添加水印,打印完成后删除该水印.   #region Namespaces using System; using System.Collect ...

  5. Revit二次开发示例:ModelessForm_ExternalEvent

    使用Idling事件处理插件任务. #region Namespaces using System; using System.Collections.Generic; using Autodesk. ...

  6. Revit二次开发示例:Journaling

    关于Revit Journal读写的例子.   #region Namespaces using System; using System.Collections.Generic; using Sys ...

  7. Revit二次开发示例:DisableCommand

    Revit API 不支持调用Revit内部命令,但可以用RevitCommandId重写它们(包含任意选项卡,菜单和右键命令).使用RevitCommandId.LookupCommandId()可 ...

  8. Revit二次开发示例:DesignOptions

    本例只要演示Revit的类过滤器的用法,在对话框中显示DesignOption元素. #region Namespaces using System; using System.Collections ...

  9. Revit二次开发示例:DeleteObject

    在本例中,通过命令可以删除选中的元素. 需要注意的是要在代码中加入Transaction,否则的话会出现Modifying  is forbidden because the document has ...

随机推荐

  1. MFC 中控件的启用与禁用

    启用和禁用控件可以调用CWnd::EnableWindow 函数. BOOL EnableWindow(BOOL bEnable = TRUE); 判断控件是否可用可以调用 CWnd::IsWindo ...

  2. redis和memcached比较

    1.Memcached采用客户端-服务器的架构,服务器维护了一个键-值关系的数据表,服务器之间相互独立,互相之间不共享数据也不做任何通讯操作.客户端需要知道所有的服务器,并自行负责管理数据在各个服务器 ...

  3. Java中Properties类的操作

    知识学而不用,就等于没用,到真正用到的时候还得重新再学.最近在看几款开源模拟器的源码,里面涉及到了很多关于Properties类的引用,由于Java已经好久没用了,而这些模拟器大多用Java来写,外加 ...

  4. puppet组织架构

    树结构如下: |-- puppet.conf #主配置配置文件 |-- fileserver.conf #文件服务器配置文件 |-- auth.conf #认证配置文件 |-- autosign.co ...

  5. 算法训练 Torry的困惑

    问题描述 Torry从小喜爱数学.一天,老师告诉他,像2.3.5.7……这样的数叫做质数.Torry突然想到一个问题,前10.100.1000.10000……个质数的乘积是多少呢?他把这个问题告诉老师 ...

  6. Nginx和PHP-FPM的启动/重启脚本 [转发]

    Nginx和PHP-FPM的启动/重启脚本 [转发] (2012-07-27 16:07:52) 标签: it 分类: 学习 转载自:http://blog.sina.com.cn/s/blog_53 ...

  7. cookie注入的形成,原理,利用总结

    一:cookie注入的形成 程序对提交数据获取方式是直接request("c.s.t")的方式.未指明使用request对象的具体方法进行获取. 二:原理 request(&quo ...

  8. iOS国际化(Xcode5)

    如何将你的app内的语言可以根据系统语言切换而切换呢?这是本篇所要解决的问题.废话先不说,上软硬件环境: 硬件:Macbook Pro 软件:Xcode 5.1 代码:https://github.c ...

  9. JSTL分类查询

    index.jsp <%@ page language="java" import="java.util.*" pageEncoding="UT ...

  10. Struts2中的ActionContext、OGNL及EL的使用

    文章分类:Java编程 本文基于struts2.1.8.1,xwork2.1.6 1.EL         EL(Expression Language)源于jsp页面标签jstl,后来被jsp2.0 ...