首页 > 编程语言 >SE C# 添加 事件监听 --选择对象切换监听

SE C# 添加 事件监听 --选择对象切换监听

时间:2023-09-27 10:23:11浏览次数:44  
标签:选择对象 ISEApplicationEvents object C# LogEvent void applicationEvents new 监听

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;
using System.Runtime.InteropServices.ComTypes;
using System.Runtime.InteropServices;
using System.Threading;
using System.Reflection;
using SolidEdge.ApplicationEvents.Properties;
using SolidEdgeFramework;
using SolidEdgeCommunity.Extensions;
using RevisionManager;

namespace SolidEdge.ApplicationEvents
{
public partial class MainForm : Form
{
private SynchronizationContext _uiContext;
private SolidEdgeFramework.Application _application = null;
private SolidEdgeFramework.ISEApplicationEvents_Event _applicationEvents;

public MainForm()
{
InitializeComponent();
}

private void MainForm_Load(object sender, EventArgs e)
{
_uiContext = SynchronizationContext.Current;

imageList1.Images.Add(Resources.Event_16x16);

// Register with OLE to handle concurrency issues on the current thread.
SolidEdgeCommunity.OleMessageFilter.Register();

try
{
_application = SolidEdgeCommunity.SolidEdgeUtils.Connect();
eventButton.Checked = true;
}
catch
{
}
}

private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
// Unhook events.
DisconnectApplicationEvents();
_application = null;
}

private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Close();
}

private void eventButton_CheckedChanged(object sender, EventArgs e)
{
try
{
if (eventButton.Checked)
{
if (_application == null)
{
_application = SolidEdgeCommunity.SolidEdgeUtils.Connect(true);
_application.Visible = true;
}

ConnectApplicationEvents();
}
else
{
DisconnectApplicationEvents();
}
}
catch (System.Exception ex)
{
MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

private void clearButton_Click(object sender, EventArgs e)
{
lvEvents.Items.Clear();
}

/// <summary>
/// Synchronously updates the UI. This will block the UI thread until the event is complete.
/// </summary>
/// <remarks>
/// If one of the event arguments is a COM object, you should use this approach as the COM object could be
/// freed before the method completes causing an exception.
/// </remarks>
public void LogEvent(MethodBase method, object[] args)
{
// COM events are received on background threads.
if (Thread.CurrentThread.IsBackground)
{
// Dispatch a synchronous message to the UI thread.
_uiContext.Send(new SendOrPostCallback(x => { LogEvent(method, args); }), null);
}
else
{
StringBuilder sb = new StringBuilder();

var parameters = method.GetParameters();

if (parameters.Length > 0)
{
for (int i = 0; i < parameters.Length; i++)
{
var parameter = parameters[i];
sb.AppendFormat("{0} = '{1}', ", parameter.Name, args[i]);
}

sb.Remove(sb.Length - 2, 2);
}
Console.WriteLine(sb);

ListViewItem item = new ListViewItem(method.Name);
item.SubItems.Add(sb.ToString());
item.ImageIndex = 0;

lvEvents.Items.Add(item);
item.EnsureVisible();

lvEvents.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
}
}

/// <summary>
/// Asynchronously updates the UI. This will no block the UI thread until the event is complete.
/// </summary>
public void LogEventAsync(MethodBase method, object[] args)
{
// COM events are received on background threads.
if (Thread.CurrentThread.IsBackground)
{
// Dispatch an asynchronous message to the UI thread.
_uiContext.Post(new SendOrPostCallback(x => { LogEventAsync(method, args); }), null);
}
else
{
StringBuilder sb = new StringBuilder();

var parameters = method.GetParameters();

if (parameters.Length > 0)
{
for (int i = 0; i < parameters.Length; i++)
{
var parameter = parameters[i];
sb.AppendFormat("{0} = '{1}',", parameter.Name, args[i]);
}

sb.Remove(sb.Length - 2, 2);
}

ListViewItem item = new ListViewItem(method.Name);
item.SubItems.Add(sb.ToString());
item.ImageIndex = 0;

lvEvents.Items.Add(item);
item.EnsureVisible();
}
}

#region SolidEdgeFramework.ISEApplicationEvents

void ISEApplicationEvents_AfterActiveDocumentChange(object theDocument)
{
LogEvent(MethodInfo.GetCurrentMethod(), new object[] { theDocument });
}

void ISEApplicationEvents_AfterCommandRun(int theCommandID)
{
LogEvent(MethodInfo.GetCurrentMethod(), new object[] { theCommandID });
}

void ISEApplicationEvents_AfterDocumentOpen(object theDocument)
{
LogEvent(MethodInfo.GetCurrentMethod(), new object[] { theDocument });
SolidEdgeDocument seDoc = _application.GetActiveDocument();

ISEDocumentEvents_Event docEvents = (SolidEdgeFramework.ISEDocumentEvents_Event)seDoc.DocumentEvents;
docEvents.BeforeSave += DocEvents_BeforeSave;
docEvents.SelectSetChanged += DocEvents_SelectSetChanged;
}


void ISEApplicationEvents_BeforeDocumentClose(object theDocument)
{
LogEvent(MethodInfo.GetCurrentMethod(), new object[] { theDocument });

SolidEdgeDocument seDoc = _application.GetActiveDocument();

ISEDocumentEvents_Event docEvents = (ISEDocumentEvents_Event)seDoc.DocumentEvents;

docEvents.BeforeSave -= DocEvents_BeforeSave;
docEvents.SelectSetChanged -= DocEvents_SelectSetChanged;

}

private void DocEvents_SelectSetChanged(object SelectSet)
{
SelectSet selectSet = (SelectSet)SelectSet;
Console.WriteLine("selectSet:" + selectSet.ToString());
//throw new NotImplementedException();
}

void ISEApplicationEvents_AfterDocumentPrint(object theDocument, int hDC, ref double ModelToDC, ref int Rect)
{
LogEvent(MethodInfo.GetCurrentMethod(), new object[] { theDocument, hDC, ModelToDC, Rect });
}

void ISEApplicationEvents_AfterDocumentSave(object theDocument)
{
LogEvent(MethodInfo.GetCurrentMethod(), new object[] { theDocument });
}

void ISEApplicationEvents_AfterEnvironmentActivate(object theEnvironment)
{

LogEvent(MethodInfo.GetCurrentMethod(), new object[] { theEnvironment });
}

void ISEApplicationEvents_AfterNewDocumentOpen(object theDocument)
{
LogEvent(MethodInfo.GetCurrentMethod(), new object[] { theDocument });
}

void ISEApplicationEvents_AfterNewWindow(object theWindow)
{
LogEvent(MethodInfo.GetCurrentMethod(), new object[] { theWindow });
}

void ISEApplicationEvents_AfterWindowActivate(object theWindow)
{
LogEvent(MethodInfo.GetCurrentMethod(), new object[] { theWindow });
}

void ISEApplicationEvents_BeforeCommandRun(int theCommandID)
{
LogEvent(MethodInfo.GetCurrentMethod(), new object[] { theCommandID });
MethodBase method = MethodInfo.GetCurrentMethod();
string name = method.Name;
Console.WriteLine("method.Name:" + name);
SolidEdgeDocument seDoc = _application.GetActiveDocument();

SolidEdgeFramework.PropertySets propertySets = seDoc.GetProperties();

var properties = (SolidEdgeFramework.Properties)propertySets.Item("Custom");

foreach (var property in properties.OfType<SolidEdgeFramework.Property>())
{
System.Runtime.InteropServices.VarEnum nativePropertyType = System.Runtime.InteropServices.VarEnum.VT_EMPTY;
Type runtimePropertyType = null;

object value = null;

nativePropertyType = (System.Runtime.InteropServices.VarEnum)property.Type;
string propertyName = property.Name;
Console.WriteLine("propertyName:" + propertyName);

// Accessing Value property may throw an exception...
try
{
value = property.get_Value();
}
catch (System.Exception ex)
{
value = ex.Message;
}

if (value != null)
{
runtimePropertyType = value.GetType();
}

Console.WriteLine("\t{0} = '{1}' ({2} | {3}).", property.Name, value, nativePropertyType, runtimePropertyType);
}

Console.WriteLine();
}

private void DocEvents_BeforeSave()
{
//文档保存的事件
Console.WriteLine("DocEvents_BeforeSave---------" );

//throw new NotImplementedException();
}


void ISEApplicationEvents_BeforeDocumentPrint(object theDocument, int hDC, ref double ModelToDC, ref int Rect)
{
LogEvent(MethodInfo.GetCurrentMethod(), new object[] { theDocument, hDC, ModelToDC, Rect });
}

void ISEApplicationEvents_BeforeDocumentSave(object theDocument)
{


LogEvent(MethodInfo.GetCurrentMethod(), new object[] { theDocument });
}

void ISEApplicationEvents_BeforeEnvironmentDeactivate(object theEnvironment)
{
LogEvent(MethodInfo.GetCurrentMethod(), new object[] { theEnvironment });
}

void ISEApplicationEvents_BeforeQuit()
{
// COM events are received on background threads.
if (Thread.CurrentThread.IsBackground)
{
// Dispatch an synchronous message to the UI thread.
_uiContext.Send(new SendOrPostCallback(x => { ISEApplicationEvents_BeforeQuit(); }), null);
}
else
{
LogEvent(MethodInfo.GetCurrentMethod(), new object[] { });

eventButton.Checked = false;
}
}

void ISEApplicationEvents_BeforeWindowDeactivate(object theWindow)
{
LogEvent(MethodInfo.GetCurrentMethod(), new object[] { theWindow });
}

#endregion

#region SolidEdgeFramework.ISEDocumentEvents

void ISEDocumentEvents_AfterSave()
{
LogEvent(MethodInfo.GetCurrentMethod(), new object[] { });
}

void ISEDocumentEvents_BeforeClose()
{
LogEvent(MethodInfo.GetCurrentMethod(), new object[] { });
}

void ISEDocumentEvents_BeforeSave()
{
LogEvent(MethodInfo.GetCurrentMethod(), new object[] { });
}

void ISEDocumentEvents_SelectSetChanged(object SelectSet)
{
LogEvent(MethodInfo.GetCurrentMethod(), new object[] { SelectSet });
}

#endregion

#region "Event hooking-unhooking"

private void ConnectApplicationEvents()
{
_applicationEvents = (SolidEdgeFramework.ISEApplicationEvents_Event)_application.ApplicationEvents;

_applicationEvents.AfterActiveDocumentChange += ISEApplicationEvents_AfterActiveDocumentChange;
_applicationEvents.AfterCommandRun += ISEApplicationEvents_AfterCommandRun;
_applicationEvents.AfterDocumentOpen += ISEApplicationEvents_AfterDocumentOpen;
_applicationEvents.AfterDocumentPrint += ISEApplicationEvents_AfterDocumentPrint;
_applicationEvents.AfterDocumentSave += ISEApplicationEvents_AfterDocumentSave;
_applicationEvents.AfterEnvironmentActivate += ISEApplicationEvents_AfterEnvironmentActivate;
_applicationEvents.AfterNewDocumentOpen += ISEApplicationEvents_AfterNewDocumentOpen;
_applicationEvents.AfterNewWindow += ISEApplicationEvents_AfterNewWindow;
_applicationEvents.AfterWindowActivate += ISEApplicationEvents_AfterWindowActivate;
_applicationEvents.BeforeCommandRun += ISEApplicationEvents_BeforeCommandRun;
_applicationEvents.BeforeDocumentClose += ISEApplicationEvents_BeforeDocumentClose;
_applicationEvents.BeforeDocumentPrint += ISEApplicationEvents_BeforeDocumentPrint;
_applicationEvents.BeforeDocumentSave += ISEApplicationEvents_BeforeDocumentSave;
_applicationEvents.BeforeEnvironmentDeactivate += ISEApplicationEvents_BeforeEnvironmentDeactivate;
_applicationEvents.BeforeQuit += ISEApplicationEvents_BeforeQuit;
_applicationEvents.BeforeWindowDeactivate += ISEApplicationEvents_BeforeWindowDeactivate;
}

private void DisconnectApplicationEvents()
{
if (_applicationEvents != null)
{
_applicationEvents.AfterActiveDocumentChange -= ISEApplicationEvents_AfterActiveDocumentChange;
_applicationEvents.AfterCommandRun -= ISEApplicationEvents_AfterCommandRun;
_applicationEvents.AfterDocumentOpen -= ISEApplicationEvents_AfterDocumentOpen;
_applicationEvents.AfterDocumentPrint -= ISEApplicationEvents_AfterDocumentPrint;
_applicationEvents.AfterDocumentSave -= ISEApplicationEvents_AfterDocumentSave;
_applicationEvents.AfterEnvironmentActivate -= ISEApplicationEvents_AfterEnvironmentActivate;
_applicationEvents.AfterNewDocumentOpen -= ISEApplicationEvents_AfterNewDocumentOpen;
_applicationEvents.AfterNewWindow -= ISEApplicationEvents_AfterNewWindow;
_applicationEvents.AfterWindowActivate -= ISEApplicationEvents_AfterWindowActivate;
_applicationEvents.BeforeCommandRun -= ISEApplicationEvents_BeforeCommandRun;
_applicationEvents.BeforeDocumentClose -= ISEApplicationEvents_BeforeDocumentClose;
_applicationEvents.BeforeDocumentPrint -= ISEApplicationEvents_BeforeDocumentPrint;
_applicationEvents.BeforeDocumentSave -= ISEApplicationEvents_BeforeDocumentSave;
_applicationEvents.BeforeEnvironmentDeactivate -= ISEApplicationEvents_BeforeEnvironmentDeactivate;
_applicationEvents.BeforeQuit -= ISEApplicationEvents_BeforeQuit;
_applicationEvents.BeforeWindowDeactivate -= ISEApplicationEvents_BeforeWindowDeactivate;

_applicationEvents = null;
}
}

#endregion

private void eventButton_Click(object sender, EventArgs e)
{

}
}
}

标签:选择对象,ISEApplicationEvents,object,C#,LogEvent,void,applicationEvents,new,监听
From: https://www.cnblogs.com/PLM-Teamcenter/p/17732041.html

相关文章

  • Idea 的 Ctrl + Shift + F 快捷键失效
    失效的原因:是因为和搜狗输入法的"简繁"切换的快捷键冲突了解决方案:设置搜狗输入法的"简繁"快捷键,把"简繁"快捷键换成其他,不要用Ctrl+Shift+F......
  • 视频监控\安防视频监控平台EasyCVR的远程控制有什么意义?
    EasyCVR国标视频融合云平台采用端-边-云一体化架构,具备海量视频接入、汇聚与管理、处理及分发等视频能力。该平台部署简单轻量,功能灵活多样。在视频能力方面,它可以实时视频直播、语音对讲、录像回放、云存储等,以实现动火作业现场的在线监测和作业安全预警。此外,还能进行报警联动和......
  • C# 获取文件夹和文件列表,与Windows系统看到的保持一致(包括隐藏文件)
    Windows系统中有很多系统隐藏的文件,如果不经过筛选,就会查出来多很多文件夹和文件。所以需要过滤掉FileAttributes.Hidden|FileAttributes.System的文件夹和文件//创建一个DirectoryInfo对象vardirectoryInfo=newDirectoryInfo(folderPa......
  • C# BeginInvoke实现异步编程
    C#BeginInvoke实现异步编程-CSDN博客https://blog.csdn.net/Nire_Yeyu/article/details/133203267 C#BeginInvoke实现异步编程BeginInvoke实现异步编程的三种模式:1.等待模式在发起了异步方法以及做了一些其他处理之后,原始线程就中断并且等异步方法完成之后再继续;eg:usingS......
  • 【RocketMQ】主从同步实现原理
    RocketMQ支持集群部署来保证高可用。它基于主从模式,将节点分为Master、Slave两个角色,集群中可以有多个Master节点,一个Master节点可以有多个Slave节点。Master节点负责接收生产者发送的写入请求,将消息写入CommitLog文件,Slave节点会与Master节点建立连接,从Master节点同步消息数据。......
  • C# 枚举使用[Flags] 特性形成一个位掩码及判断是否存在某个枚举组合
    在C#中,通过给枚举类型添加 [Flags] 特性,可以指示该枚举类型是用于表示位标志的枚举。使用带有 [Flags] 特性的枚举类型允许将多个枚举值组合在一起,形成一个位掩码,提供了一种更方便和可读性更好的方式来表示多个选项的组合。当给枚举类型添加 [Flags] 特性后,可以使用按位或......
  • macOS Sonoma 14 (23A344) 正式版发布,ISO、IPSW、PKG 下载
    macOSSonoma今日推出,全面提升生产力和创意工作流macOSSonoma14(23A344)正式版发布,ISO、IPSW、PKG下载2023年9月26日(北京时间27日凌晨)macOSSonoma正式版现已发布。本站下载的macOS软件包,既可以拖拽到Applications(应用程序)下直接安装,也可以制作启动U盘安装,......
  • macOS Sonoma 14 (23A344) 正式版 Boot ISO 原版可引导镜像下载
    macOSSonoma14(23A344)正式版BootISO原版可引导镜像下载2023年9月26日(北京时间27日凌晨)macOSSonoma正式版现已发布。本站下载的macOS软件包,既可以拖拽到Applications(应用程序)下直接安装,也可以制作启动U盘安装,或者在虚拟机中启动安装。另外也支持在Windows......
  • 进入BIOS推荐方法ThinkPad , ThinkCentre , ThinkStation
    描述用户可以轻松地调整计算机时间,日期,查看硬件配置信息(机器类型,序列号,内存和HDD等),设置无线或热键功能以及在BIOS下调整开机启动过程。推荐的进入BIOS方法如下所示。通过功能键(Fn)进入BIOS从Windows10进入BIOS从Windows8.1/8进入BIOS观看我们的视频:[视频]什么是BIOS?[视频]如......
  • 视频监控/监控汇聚平台EasyCVR助力档案库房可视化管理的应用方案
    档案作为一种特殊的留存记录,具有珍贵的历史价值和文化遗产意义。它是人类活动真实的见证,记录了辉煌时刻和普通人的生活轨迹,对社会发展和经济建设起着举足轻重的作用。如今随着市场经济的不断发展和人类文明的飞速推进,档案的价值更加凸显,档案的储存和管理也备受关注。提升档案数字......