/// <summary> /// 添加事件 /// </summary> /// <param name="Key">唯一键</param> /// <param name="MatchName">匹配的名称</param> /// <param name="EventType">事件枚举类型</param> /// <param name="RegisterMethod">事件注册的方法</param> /// <param name="NeedRemove">是否先移除</param> internal void Add(object Key, string MatchName, SEEvent EventType, Action <object[]> RegisterMethod, bool NeedRemove = false) { lock (_mLock) { if (NeedRemove) { Remove(Key, MatchName, EventType); } if (!_mOccurrenceEventStorage.ContainsKey(Key, MatchName, EventType, out SolidEdgeDocumentEventHelper Helper)) { _mOccurrenceEventStorage.AddElement(Key, MatchName, EventType, Helper = new SolidEdgeDocumentEventHelper(EventType, RegisterMethod)); } else { throw new ArgumentException("重复添加事件:" + EventType.ToString()); } Helper.RegisterSolidEdgeEvent(GetActiveObjectDocument(MatchName)); } }
/// <summary> /// 添加事件 /// </summary> /// <param name="key">唯一键(如果Key是字符串类型,则寻找当前ASM下面匹配的ASM文档)</param> /// <param name="EventType">事件枚举值</param> /// <param name="RegisterMethod">事件执行方法</param> /// <param name="NeedRemove">是否需要移除已存在事件</param> internal void Add(object Key, SEEvent EventType, Action <object[]> RegisterMethod, bool NeedRemove = false) { lock (_mLock) { //系统事件 if (CheckSolidEdgeApplicationEvent(Key, EventType, RegisterMethod, true, NeedRemove)) { return; } //文档事件 if (NeedRemove) { Remove(Key, EventType); } SolidEdgeDocumentEventHelper Helper = new SolidEdgeDocumentEventHelper(EventType, RegisterMethod); if (!_mDicHelperOfDoc.ContainsKey(Key)) { _mDicHelperOfDoc.Add(Key, new Dictionary <SEEvent, SolidEdgeDocumentEventHelper>(new EnumComparer <SEEvent>()) { [EventType] = Helper }); } else { if (!_mDicHelperOfDoc[Key].ContainsKey(EventType)) { _mDicHelperOfDoc[Key].Add(EventType, Helper); } else { throw new ArgumentException("重复添加事件:" + EventType.ToString()); } } Helper.RegisterSolidEdgeEvent(GetActiveObjectDocument()); } }
/// <summary> /// 检查是否为系统事件,并且添加/替换或者移除事件 /// </summary> /// <param name="ObjectKey">唯一键</param> /// <param name="EventType">事件枚举类型</param> /// <param name="RegisterMethod">事件注册方法</param> /// <param name="AddOrRemove">添加或者移除</param> /// <param name="Replace">如果存在是否替换</param> /// <returns></returns> private bool CheckSolidEdgeApplicationEvent(object ObjectKey, SEEvent EventType , Action <object[]> RegisterMethod = null , bool AddOrRemove = false, bool Replace = false) { switch (EventType) { case SEEvent.ApplicationAfterActiveDocumentChange: case SEEvent.ApplicationAfterCommandRun: case SEEvent.ApplicationAfterDocumentOpen: case SEEvent.ApplicationAfterDocumentPrint: case SEEvent.ApplicationAfterDocumentSave: case SEEvent.ApplicationAfterEnvironmentActivate: case SEEvent.ApplicationAfterNewDocumentOpen: case SEEvent.ApplicationAfterNewWindow: case SEEvent.ApplicationAfterWindowActivate: case SEEvent.ApplicationBeforeCommandRun: case SEEvent.ApplicationBeforeDocumentClose: case SEEvent.ApplicationBeforeDocumentPrint: case SEEvent.ApplicationBeforeEnvironmentDeactivate: case SEEvent.ApplicationBeforeWindowDeactivate: case SEEvent.ApplicationBeforeQuit: case SEEvent.ApplicationBeforeDocumentSave: //添加 if (AddOrRemove) { SolidEdgeSystemEventHelper Helper = new SolidEdgeSystemEventHelper(ObjectKey, EventType, RegisterMethod); //不存在 if (!_mDicHelprOfApp.ContainsKey(ObjectKey)) { //添加 _mDicHelprOfApp.Add(ObjectKey, new Dictionary <SEEvent, SolidEdgeSystemEventHelper>(new EnumComparer <SEEvent>()) { [EventType] = Helper }); } //存在主键 else { //不存在该事件类型 if (!_mDicHelprOfApp[ObjectKey].ContainsKey(EventType)) { //添加 _mDicHelprOfApp[ObjectKey].Add(EventType, Helper); } //已存在 else { //需要替换 if (Replace) { //先注销事件 _mDicHelprOfApp[ObjectKey][EventType].UnregisterEvent(); _mDicHelprOfApp[ObjectKey].Remove(EventType); //添加 _mDicHelprOfApp[ObjectKey].Add(EventType, Helper); } //提示重复 else { throw new ArgumentException("重复添加事件:" + EventType.ToString()); } } } //最后统一注册事件 Helper.RegisterEvent(); } //移除 else { if (_mDicHelprOfApp.ContainsKey(ObjectKey)) { if (_mDicHelprOfApp[ObjectKey].ContainsKey(EventType)) { _mDicHelprOfApp[ObjectKey][EventType].UnregisterEvent(); } } } return(true); } return(false); }