Eplan-行为
Eplan-行为
初始化
想要在Eplan中实现行为的方法,则需要实现 IEplAction 接口,重写Execute()和 OnRegister()方法。
Execute()方法中为执行的逻辑,
OnRegister()中为行为在方法中注册的名称,若不注册,则为类的名称。
public class ActionParamAction : IEplAction
{
public bool Execute(ActionCallingContext oActionCallingContext)
{
return true;
}
public void GetActionProperties(ref ActionProperties actionProperties)
{
}
public bool OnRegister(ref string Name, ref int Ordinal)
{
return true;
}
}
实现场景
行为在Eplan中一共有三种调用方式
- 菜单项调用
- 行为调用行为
- 远程调用行为
菜单项调用
在菜单中绑定行为的名称可以点击触发行为
public void InitMenu()
{
Menu menu = new Menu();
uint menuNum = menu.AddMainMenu("附加菜单栏", Menu.MainMenuName.eMainMenuHelp, "BoxDevice", "BoxDeviceAction", "", 1);
menu.AddMenuItem("调用行为", "ActionParaAction", "", menuNum, 1, true, false);
menu.AddMenuItem("action调用action", "ExecuteAction", "", menuNum, 1, true, false);
}
public class ActionParamAction : IEplAction
{
public bool Execute(ActionCallingContext oActionCallingContext)
{
return true;
}
public void GetActionProperties(ref ActionProperties actionProperties)
{
}
public bool OnRegister(ref string Name, ref int Ordinal)
{
Name = "ActionParaAction";
Ordinal = 20;
return true;
}
}
行为调用行为
行为调用行为的方式,需要actionmanger通过名称找到行为,然后调用
internal class ExecuteAction : IEplAction
{
public bool Execute(ActionCallingContext oActionCallingContext)
{
// 查找
ActionManager manager = new ActionManager();
Eplan.EplApi.ApplicationFramework.Action action = manager.FindAction("ActionParaAction");
// 赋值
ActionCallingContext ctx = new ActionCallingContext();
// 执行
action.Execute(ctx);
return true;
}
public void GetActionProperties(ref ActionProperties actionProperties)
{
}
public bool OnRegister(ref string Name, ref int Ordinal)
{
Name = "ExecuteAction";
Ordinal = 20;
return true;
}
}
在此过程中需要注意,action还有传参和出参,相当于方法中的传参和返回值,这些都要借助于ActionCallingContext这个类来实现
public class ActionParaAction : IEplAction
{
public bool Execute(ActionCallingContext oActionCallingContext)
{
string value1 = "";
string value2 = "";
// 接收传入的参数
oActionCallingContext.GetParameter("Param1", ref value1);
oActionCallingContext.GetParameter("Param2", ref value2);
// 返回string类型的值 其实方式还可以序列化转化obj为string 然后在另一端解析
string value3 = (int.Parse(value1) + int.Parse(value2)).ToString();
oActionCallingContext.AddParameter("ReturnParam", value3);
// 通过ContextParameterBlock 返回object 或者 list<obj>类型的值
ContextParameterBlock block = new ContextParameterBlock();
Book BookVlaue = new Book() { Name = "百年孤独", Price = "12" };
block.Set("bookPara", BookVlaue);
List<object> stringList = new List<object>() { "aa", "bb", "cc" };
block.SetList(stringList);
oActionCallingContext.SetContextParameter(block);
}
}
internal class ExecuteAction : IEplAction
{
public bool Execute(ActionCallingContext oActionCallingContext)
{
ActionManager manager = new ActionManager();
Eplan.EplApi.ApplicationFramework.Action action = manager.FindAction("ActionParaAction");
ActionCallingContext ctx = new ActionCallingContext();
ctx.AddParameter("Param1", "1");
ctx.AddParameter("Param2", "2");
action.Execute(ctx);
// 接收返回值
string value3 = "";
ctx.GetParameter("ReturnParam", ref value3);
object obj = new object();
ContextParameterBlock contextBlock = ctx.GetContextParameter();
contextBlock.Get("objParam", ref obj);
List<object> objList = contextBlock.GetList();
return true;
}
}
远程调用行为
远程调用需要注意eplan占用的地址和端口
private void Button_Click(object sender, RoutedEventArgs e)
{
EplanRemoteClient m_pClient = new EplanRemoteClient();
// 建立连接
m_pClient.Connect("127.0.0.1", "49152"); //default port for EPLAN instance is 49155
// 调用
bool oResp = m_pClient.ExecuteAction("ActionParaAction");
// 断开连接
m_pClient.Disconnect();
}