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";
} }
}
#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的更多相关文章
- Revit二次开发示例:HelloRevit
本示例实现Revit和Revit打开的文件的相关信息. #region Namespaces using System; using System.Collections.Generic; using ...
- Revit二次开发示例:ErrorHandling
本示例介绍了Revit的错误处理. #region Namespaces using System; using System.Collections.Generic; using Autodes ...
- Revit二次开发示例:ChangesMonitor
在本示例中,程序监控Revit打开文件事件,并在创建的窗体中更新文件信息. #region Namespaces using System; using System.Collections.Ge ...
- Revit二次开发示例:AutoStamp
该示例中,在Revit启动时添加打印事件,在打印时向模型添加水印,打印完成后删除该水印. #region Namespaces using System; using System.Collect ...
- Revit二次开发示例:ModelessForm_ExternalEvent
使用Idling事件处理插件任务. #region Namespaces using System; using System.Collections.Generic; using Autodesk. ...
- Revit二次开发示例:Journaling
关于Revit Journal读写的例子. #region Namespaces using System; using System.Collections.Generic; using Sys ...
- Revit二次开发示例:DisableCommand
Revit API 不支持调用Revit内部命令,但可以用RevitCommandId重写它们(包含任意选项卡,菜单和右键命令).使用RevitCommandId.LookupCommandId()可 ...
- Revit二次开发示例:DesignOptions
本例只要演示Revit的类过滤器的用法,在对话框中显示DesignOption元素. #region Namespaces using System; using System.Collections ...
- Revit二次开发示例:DeleteObject
在本例中,通过命令可以删除选中的元素. 需要注意的是要在代码中加入Transaction,否则的话会出现Modifying is forbidden because the document has ...
随机推荐
- MBProgressHUD使用
//方式1.直接在View上show HUD = [[MBProgressHUD showHUDAddedTo:self.view animated:YES] retain]; HUD.delegat ...
- JAVA-- M选N的组合算法
M选N的组合算法 只要每个数字出现一次就可以 举例 :也就是说123与321和213属于重复 只算一组 此算法已经排除了重复数据 应用--彩票的注数算法 本程序的思路是开一个数组b,其长度 ...
- DICOM:DICOM3.0网络通信协议
转载:http://blog.csdn.net/zssureqh/article/details/41016091 背景: 专栏取名为DICOM医学图像处理原因是:博主是从医学图像处理算法研究时开始接 ...
- Linux Haproxy 安装和部署
一.Haproxy 安装 下载地址 http://pan.baidu.com/s/1mggViXE cd /usr/local tar xzvf haproxy-.tar.gz cd haproxy- ...
- Linux Apache和Nginx的比较
1.nginx相对于apache的优点: 轻量级,同样起web 服务,比apache占用更少的内存及资源 抗并发,nginx 处理请求是异步非阻塞的,而apache 则是阻塞型的,在高并发下nginx ...
- MongoDB概述&语法
Nosql DB 这是一个非关系型数据库. 通常我们的数据库有三类: 关系型数据库(RDBMS),联机分析处理数据库(OLAP),和菲关系型数据库(NoSql). MongoDB属于第三种,而且是一 ...
- Java和C#运行速度对比:Java比C#快约3倍
测试条件: Java版本: Java 8, .NET版本:v4.0, Release模式,针对x86平台优化 单线程模式. 测试1:(测试1的代码摘自http://blog.csdn.net/houj ...
- 为nginx增加nginx_http_concat模块
为nginx增加nginx_http_concat模块 时间 2013-06-05 22:14:56 我行我思 原文 http://www.fanjun.me/?p=562 主题 Nginx 缘由 ...
- 2.10 用最少次数寻找数组中的最大值和最小值[find min max of array]
[本文链接] http://www.cnblogs.com/hellogiser/p/find-min-max-of-array.html [题目] 对于一个由N个整数组成的数组,需要比较多少次才能把 ...
- Android 中的AsyncTask
在后台下载图片,下载完成后更新UI是一个很常见的需求.在没有AsyncTask类之前,我们需要写许多thread和Handler的代码去实现这个功能,有了AsyncTask,一切变得简单了.下面摘抄谷 ...