using log4net;
using PdmSwPlugin.Common.Util;
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System;
using System.Collections;
using System.Windows.Controls;
namespace PdmSwPlugin.Main.EventHandler
{
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;
}
///
/// 全局附加事件
///
///
public virtual bool AttachEventHandlers()
{
AttachSwEvents();
//Listen for events on all currently open docs
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();
}
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;
}
///
/// 移除事件处理
///
///
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;
}
}
///
/// 释放设置的事件处理器
///
///
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 各种事件处理
///
/// 当前活动文档发生变化时
///
/// 0 返回成功,其它的失败
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();
return 0;
}
public int OnActiveModelChange()
{
return 0;
}
public int FileOpenPostNotify(string FileName)
{
AttachEventsToAllDocuments();
return 0;
}
public int OnDocDestory(ModelDoc2 doc)
{
return mainControl.OnDocDestroy(doc);
}
public void AfterDocDestroy()
{
mainControl.OnAfterDocDestroy();
}
#endregion
}
}