在该示例中,插件在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. 纹理缓存(Texture Cache)

    纹理缓存是将纹理缓存起来方便之后的绘制工作.每一个缓存的图像的大小,颜色和区域范围都是可以被修改的.这些信息都是存储在内存中的,不用在每一次绘制的时候都发送给GPU. CCTextureCache C ...

  2. win7 64位系统HP LaserJet P1008 / HP LaserJet P1008 P1007 驱动安装成功,但无法打印的原因

    HP LaserJet P1008 打印机驱动安装成功,但是无法打印相关文档的原因是: 1.打印机是水货,惠普中国提供的驱动和该打印机不符合.显示的应该是HP LaserJet Professiona ...

  3. GDB 使用大法

    一.GDB 我用的是 GCC+POWERSHELL+GDB,  GDB刚刚接触也有很多要记的. 二.一个调试示例 tst.c #include <stdio.h> int func(int ...

  4. centOS设置zookeeper开机自动启动

    在/etc/rc.local文件中追加: # java_homeexport JAVA_HOME=/opt/java/jdk1.7.0_75# zookeeper/home/cent2014/zook ...

  5. 2.10 用最少次数寻找数组中的最大值和最小值[find min max of array]

    [本文链接] http://www.cnblogs.com/hellogiser/p/find-min-max-of-array.html [题目] 对于一个由N个整数组成的数组,需要比较多少次才能把 ...

  6. Delphi经验总结(3)

    ------------------------------------------------------- ◇删掉程序自己的exe文件 procedure TForm1.FormClose(Sen ...

  7. Java for LeetCode 063 Unique Paths II

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  8. Java数据类型和运算符

    一,数据类型分类(2种) 1. 基本数据类型(3种) 数值型: 整数类型(4种): byte(1字节):范围(-128~127): short(2字节):范围(-32768~32767): int(4 ...

  9. (六)STM32的时钟系统

    在STM32中,一共有5个时钟源,分别是HSI.HSE.LSI.LSE.PLL (1) HSI是高速内部时钟,RC振荡器,频率为8MHz: (2) HSE是高速外部时钟,可接石英/陶瓷谐振器,或者接外 ...

  10. ping 有端口的ip

    使用工具 tcping 使用方法,将此工具放在C:/Windows/System32 目录下, 在cmd 中: tcping 127.0.0.1 8080