EPLAN 有自己的机制来发送通知并对通知做出反应。通知,也称为“事件”,在 EPLAN 中通过它们的名称(字符串)来标识。这意味着发送通知时并没有特定的类类型。

EPLAN 及其每个模块可以发送和处理事件,而无需在系统中注册事件。EPLAN 的事件机制非常灵活。API 用户甚至可以发送和处理具有新名称的事件。

关于 EPLAN 事件的列表,请参考此链接:Eplan.EplApi.ApplicationFramework.Events。

如何对 EPLAN 事件做出反应?

要对事件做出反应,只需实现一个事件处理函数并将其注册到 EPLAN 的 EventHandler 对象中。

class MyEventListener
{
    // create an EventHandler object
    Eplan.EplApi.ApplicationFramework.EventHandler myHandler = new
    Eplan.EplApi.ApplicationFramework.EventHandler();
    public MyEventListener()
    {
        // react on the EPLAN event "onActionStart.String.*"
        myHandler.SetEvent("onActionStart.String.*");
        // If the event "onActionStart.String.*" is raised,
        // the function myHandler_EplanEvent should be called
        myHandler.EplanEvent +=  myHandler_EplanEvent;
    }

    private void myHandler_EplanEvent(IEventParameter iEventParameter)
    {
        // TODO: do something, when the event is caught
    }
}

现在,你需要创建一个事件监听器类的实例。在这个对象的生命周期内,事件将被处理。例如,你可以在你的插件的 API 模块类中实例化该对象:

public class AddInModule: IEplAddIn
{
    private MyEventListener m_EventHandler;
    ///<summary>
    /// This function is called, when starting EPLAN,
    /// if the add-in is loaded on system startup.
    ///</summary>
    /// <returns></returns>
    ///<seealso cref="OnRegister"/>
     public bool OnInit()
    {
        m_EventHandler = new MyEventListener ();
        return true;
    }
//...
}

事件参数

每个事件可能会有特定类型的附加参数。为此,我们有 EventParameter 类,例如 EventParameterString

OnEvent() 函数具有通用接口作为参数。它将特定的 EventParameter 类作为构造函数参数。随后它尝试创建这个参数对象。如果接口中不包含合适的对象,EPLAN 会抛出异常。

因此,当你处理特定事件时,你需要事先知道事件参数的类型,以便从接口中创建正确的参数。

下面是一个示例,展示如何处理带有参数的事件:

private void myHandler_EplanEvent(IEventParameter iEventParameter)
{
	try
	{ 
		EventParameterString oEventParameterString = new
		EventParameterString(iEventParameter);
		String strActionName = oEventParameterString.String; 
	}
	catch (System.InvalidCastException exc)
	{
		String strexc = exc.Message;
	}
}

触发事件

你可以创建并发送带有任意名称的自定义事件。然而,你无法控制你的事件是否会在某处被处理。在下面的示例中,一个名为 "EventFromCSharpAddIn" 的事件被触发。该事件具有一个类型为 EventParameterString 的参数。

以下是如何创建和发送自定义事件的示例:

EventParameterString oEventParamString = new EventParameterString();
oEventParamString.String = "ParameterFromCSharpAddIn";
long lRetVal = new EventManager().Send("EventFromCSharpAddIn", oEventParamString);