using log4net;
|
using PdmSwPlugin.Common.Util;
|
using SolidWorks.Interop.sldworks;
|
using SolidWorks.Interop.swconst;
|
using System;
|
using System.Collections;
|
using System.Threading.Tasks;
|
using System.Windows.Controls;
|
|
namespace PdmSwPlugin.Main.EventHandler
|
{
|
/// <summary>
|
/// 事件附加器
|
/// </summary>
|
public class BaseAttacher : IEventAttacher, IDocEventHandler
|
{
|
public static ILog Logger { get; } = LogManager.GetLogger("SwAddin");
|
protected MainControl mainControl;
|
|
protected ISldWorks swApp;
|
protected SldWorks SwEventPtr;
|
protected ModelDoc2 lastActiveDoc;
|
protected ModelDoc2 currentDoc;
|
|
public ISldWorks SwApp => swApp;
|
|
public BaseAttacher(ISldWorks swApp, MainControl mainControl)
|
{
|
this.swApp = swApp;
|
this.mainControl = mainControl;
|
this.SwEventPtr = swApp as SldWorks;
|
}
|
|
/// <summary>
|
/// 全局附加事件
|
/// </summary>
|
/// <returns></returns>
|
public virtual bool AttachEventHandlers()
|
{
|
// 添加应用事件监听
|
AttachSwEvents();
|
// 添加文档事件监听
|
AttachEventsToAllDocuments();
|
return true;
|
}
|
|
public bool AttachSwEvents()
|
{
|
try
|
{
|
// 当前活动文档发生变化时执行
|
SwEventPtr.ActiveDocChangeNotify += new DSldWorksEvents_ActiveDocChangeNotifyEventHandler(OnActiveDocChange);
|
SwEventPtr.DocumentLoadNotify2 += new DSldWorksEvents_DocumentLoadNotify2EventHandler(OnDocLoad);
|
SwEventPtr.FileNewNotify2 += new DSldWorksEvents_FileNewNotify2EventHandler(OnFileNew);
|
SwEventPtr.ActiveModelDocChangeNotify += new DSldWorksEvents_ActiveModelDocChangeNotifyEventHandler(OnActiveModelChange);
|
SwEventPtr.FileOpenPostNotify += new DSldWorksEvents_FileOpenPostNotifyEventHandler(FileOpenPostNotify);
|
return true;
|
}
|
catch (Exception e)
|
{
|
Logger.Error("SolidWorks事件附加异常!", e);
|
return false;
|
}
|
}
|
|
private bool AttachEventsToAllDocuments()
|
{
|
//ModelDoc2 modDoc = (ModelDoc2)SwApp.GetFirstDocument();
|
//while (modDoc != null)
|
//{
|
// if (!OpenDocContext.IsOpenDoc(modDoc))
|
// {
|
// AttachModelDocEventHandler(modDoc);
|
// }
|
// modDoc = (ModelDoc2)modDoc.GetNext();
|
//}
|
|
/// 试一下异步附加事件
|
var objs = SwApp.GetDocuments();
|
if (objs != null && objs.Length > 0)
|
{
|
Task[] tasks = new Task[objs.Length];
|
for (int i = 0; i < objs.Length; i++)
|
{
|
var obj = objs[i];
|
if (obj is ModelDoc2 modDoc)
|
{
|
tasks[i] = Task.Run(() =>
|
{
|
if (!OpenDocContext.IsOpenDoc(modDoc))
|
{
|
AttachModelDocEventHandler(modDoc);
|
}
|
});
|
}
|
}
|
Task.WaitAll(tasks);
|
}
|
return true;
|
}
|
|
private bool AttachEventsToAllDocuments(ModelDoc2 modDoc)
|
{
|
if (!OpenDocContext.IsOpenDoc(modDoc))
|
{
|
AttachModelDocEventHandler(modDoc);
|
}
|
return true;
|
}
|
|
private bool AttachEventsToAllDocuments(string fileName)
|
{
|
ModelDoc2 modDoc = SwApp.GetOpenDocument(fileName);
|
if (!OpenDocContext.IsOpenDoc(modDoc))
|
{
|
AttachModelDocEventHandler(modDoc);
|
}
|
return true;
|
}
|
|
#region 附加事件
|
public bool AttachModelDocEventHandler(ModelDoc2 modDoc)
|
{
|
if (modDoc == null) return false;
|
BaseEventHandler docHandler;
|
if (!OpenDocContext.IsOpenDoc(modDoc))
|
{
|
string sb = modDoc.GetPathName();
|
switch (modDoc.GetType())
|
{
|
case (int)swDocumentTypes_e.swDocPART:
|
{
|
docHandler = new PartDocEventHandler(modDoc, this);
|
break;
|
}
|
case (int)swDocumentTypes_e.swDocASSEMBLY:
|
{
|
docHandler = new AssDocEventHandler(modDoc, this);
|
break;
|
}
|
case (int)swDocumentTypes_e.swDocDRAWING:
|
{
|
docHandler = new DrwDocEventHandler(modDoc, this);
|
break;
|
}
|
default:
|
{
|
return false; //Unsupported document type
|
}
|
}
|
docHandler.AttachEventHandlers();
|
if (!OpenDocContext.IsOpenDoc(modDoc))
|
{
|
OpenDocContext.AddDoc(modDoc, docHandler);
|
}
|
}
|
return true;
|
}
|
|
/// <summary>
|
/// 移除事件处理
|
/// </summary>
|
/// <returns></returns>
|
private bool DetachSwEvents()
|
{
|
try
|
{
|
SwEventPtr.ActiveDocChangeNotify -= new DSldWorksEvents_ActiveDocChangeNotifyEventHandler(OnActiveDocChange);
|
SwEventPtr.DocumentLoadNotify2 -= new DSldWorksEvents_DocumentLoadNotify2EventHandler(OnDocLoad);
|
SwEventPtr.FileNewNotify2 -= new DSldWorksEvents_FileNewNotify2EventHandler(OnFileNew);
|
SwEventPtr.ActiveModelDocChangeNotify -= new DSldWorksEvents_ActiveModelDocChangeNotifyEventHandler(OnActiveModelChange);
|
SwEventPtr.FileOpenPostNotify -= new DSldWorksEvents_FileOpenPostNotifyEventHandler(FileOpenPostNotify);
|
return true;
|
}
|
catch (Exception e)
|
{
|
Logger.Error("SolidWorks事件移除异常!", e);
|
return false;
|
}
|
}
|
|
/// <summary>
|
/// 释放设置的事件处理器
|
/// </summary>
|
/// <returns></returns>
|
public bool DetachEventHandlers()
|
{
|
DetachSwEvents();
|
|
//Close events on all currently open docs
|
BaseEventHandler docHandler;
|
int numKeys = OpenDocContext.OpenDocs.Count;
|
object[] keys = new Object[numKeys];
|
|
//Remove all document event handlers
|
OpenDocContext.OpenDocs.Keys.CopyTo(keys, 0);
|
foreach (ModelDoc2 key in keys)
|
{
|
docHandler = (BaseEventHandler)OpenDocContext.OpenDocs[key];
|
docHandler.DetachEventHandlers(); //This also removes the pair from the hash
|
docHandler = null;
|
}
|
return true;
|
}
|
|
public bool DetachModelEventHandler(ModelDoc2 modDoc)
|
{
|
BaseEventHandler docHandler;
|
docHandler = (BaseEventHandler)OpenDocContext.GetHandler(modDoc);
|
OpenDocContext.RemoveDoc(modDoc);
|
modDoc = null;
|
docHandler = null;
|
return true;
|
}
|
#endregion
|
|
public int OnActiveDocCustomPropertyAdded(string propName, string Configuration, string NewValue, int valueType)
|
{
|
mainControl.OnCustomPropertyChange(propName, Configuration, null, NewValue, valueType);
|
return 0;
|
}
|
|
public int OnActiveDocCustomPropertyChanged(string propName, string Configuration, string oldValue, string NewValue, int valueType)
|
{
|
mainControl.OnCustomPropertyChange(propName, Configuration, oldValue, NewValue, valueType);
|
return 0;
|
}
|
|
public int OnActiveDocSaved(int type, string fileName)
|
{
|
var doc = SwApp.IActiveDoc2;
|
mainControl.OnDocSaved(doc, null);
|
lastActiveDoc = null;
|
return 0;
|
}
|
|
#region Event Handlers 各种事件处理
|
/// <summary>
|
/// 当前活动文档发生变化时
|
/// </summary>
|
/// <returns>0 返回成功,其它的失败</returns>
|
public int OnActiveDocChange()
|
{
|
lastActiveDoc = currentDoc;
|
currentDoc = SwApp.IActiveDoc2;
|
mainControl.OnActiveDocChange(lastActiveDoc, currentDoc, null);
|
return 0;
|
}
|
|
public int OnDocLoad(string docTitle, string docPath)
|
{
|
return 0;
|
}
|
|
public int OnFileNew(object newDoc, int docType, string templateName)
|
{
|
AttachEventsToAllDocuments((ModelDoc2)newDoc);
|
return 0;
|
}
|
|
public int OnActiveModelChange()
|
{
|
return 0;
|
}
|
|
/// <summary>
|
/// Post-notifies an application when a file has been opened.
|
/// 在打开文件时 Post-通知应用程序。
|
/// </summary>
|
/// <param name="FileName"></param>
|
/// <returns></returns>
|
public int FileOpenPostNotify(string filePath)
|
{
|
AttachEventsToAllDocuments();
|
return 0;
|
}
|
|
public int OnDocDestory(ModelDoc2 doc)
|
{
|
return mainControl.OnDocDestroy(doc);
|
}
|
|
public void AfterDocDestroy()
|
{
|
mainControl.OnAfterDocDestroy();
|
}
|
#endregion
|
}
|
}
|