Eplan API -初始化
Eplan支持的开发方式一共有3种
- 脚本
- dll文件形式
- exe离线程式形式
虽然eplan二次开发也支持vb语言,但这里只讨论c#
脚本(script)
Eplan脚本支持的功能有限,有限的原因在于其支持的程序集有限
c#中的
- System;
- System.XML;
- System.Drawing;
- System.Windows.Forms
Epaln API中的
- Namespace Eplan.EplApi.Base
- Namespace Eplan.EplApi.ApplicationFramework
- Namespace Eplan.EplApi.Scripting
- Namespace Eplan.EplApi.Gui
- NameSpace Eplan.EplApi.Scripting
所以现在基本使用第二种方式来代替,这里只做简单的说明。
脚本分为两种,执行和加载
执行
以Start标注,将写好的.cs文件保存。在【工具-脚本-执行】
using Eplan.EplApi.Base;
using Eplan.EplApi.Scripting;
namespace EplanDemo.Script
{
class VerySimpleScript
{
[Start]
public void MyFunction()
{
new Decider().Decide(EnumDecisionType.eOkDecision, "MyFunction was called!", "VerySimpleScript", EnumDecisionReturn.eOK, EnumDecisionReturn.eOK);
return;
}
}
}
加载
或者将其标注为 事件,动作,菜单加载项,在【工具-脚本-加载】
public class SimpleEventHandler
{
[DeclareEventHandler("onMainStart")]
public void MyEventHandlerFunction()
{
new Decider().Decide(EnumDecisionType.eOkDecision, "onMainStart was called!", "SimpleEventHandler", EnumDecisionReturn.eOK, EnumDecisionReturn.eOK);
return;
}
}
public class RegisterScriptMenu
{
[DeclareAction("MyScriptActionWithMenu")]
public void MyFunctionAsAction()
{
new Decider().Decide(EnumDecisionType.eOkDecision, "MyFunctionAsAction was called!", "RegisterScriptMenu", EnumDecisionReturn.eOK, EnumDecisionReturn.eOK);
return;
}
[DeclareMenu]
public void MenuFunction()
{
Eplan.EplApi.Gui.Menu oMenu = new Eplan.EplApi.Gui.Menu();
oMenu.AddMenuItem("MyMenuText", "MyScriptActionWithMenu");
}
}
public class SimpleScriptAction
{
[DeclareAction("MyScriptAction")]
public void MyFunctionAsAction()
{
new Decider().Decide(EnumDecisionType.eOkDecision, "MyFunctionAsAction was called!", "RegisterScriptAction", EnumDecisionReturn.eOK, EnumDecisionReturn.eOK);
return;
}
}
程序集(.dll)
dll文件对编译生成的名称有要求,名称的命名规范为<公司名称>.EplAddin. <NameOfTheProject>.dll.
为了减少不必要的其他bug的出现,推荐使用 .net framework4.7.2,输出的dll文件为64位。
编译完成之后打开epaln软件 选择【工具-API插件-加载-dll所在目录】
ps:dll文件重新编译之后,需要重新卸载之后加载,因为eplan对dll文件的引入采用的是复制dll文件到指定目录,防止在读取dll文件的时候,文件被修改暂用,对于引用的dll文件的其他依赖dll则会在dll的目录中查找。
这里简单做一个加载的菜单项
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Eplan.EplApi.ApplicationFramework;
using Eplan.EplApi.Gui;
namespace EplanDemo
{
public class EplanMenu : IEplAddIn, IEplAddInShadowCopy
{
private String m_strOriginalAssemblyPath;
public void OnBeforeInit(string strOriginalAssemblyPath)
{
m_strOriginalAssemblyPath = strOriginalAssemblyPath;
}
public bool OnExit()
{
return true;
}
public bool OnInit()
{
return true;
}
public bool OnInitGui()
{
Init1();
return true;
}
public bool OnRegister(ref bool bLoadOnStart)
{
bLoadOnStart = true;
return true;
}
public bool OnUnregister()
{
return true;
}
public void Init1()
{
Menu menu = new Menu();
uint menuNum = menu.AddMainMenu("脚本加载", Menu.MainMenuName.eMainMenuHelp, "功能一", "MyScriptAction", "", 1);
menu.AddMenuItem("添加paramater", "AddParameterAction", "", menuNum, 0, false, false);
menu.AddMenuItem("显示文字", "ParameterAction", "", menuNum, 0, false, false);
menu.AddMenuItem("功能四", "PropertiesACTION", "", menuNum, 0, false, false);
menu.AddMenuItem("绘制宏", "DrawingAction", "", menuNum, 0, false, false);
menu.AddMenuItem("消息", "MessageAction", "", menuNum, 0, false, false);
menu.AddMenuItem("Action", "ActionMangerAction", "", menuNum, 0, false, false);
}
}
}
脱机程式
脱机程式为.exe可执行文件,基本上在windows上分为两种 Winfrom,Wpf
其初始赖于四个dll文件,引入其中。但程序想运行起来还是需要依赖于其他的dll。 在这里有两种处理方式。
- 将生成的程式的目录放置在程式的安装目录中 <eplan main path>\Platform\ <version>\Bin folder,
- 指定dll文件的形式。
- 在程式运行的时候通过反射动态加载未找到的dll文件
- 在程式加载的时候引入所有的dll文件
这里只讨论第二种的一二两种方式。
引入dll文件
winfrom在program.cs中,wpf则在app.xaml.cs中引入
动态加载
static class Program
{
/// <summary>
/// 应用程序的主入口点。 winfrom
/// </summary>
[STAThread]
static void Main()
{
// Application.EnableVisualStyles();
// Application.SetCompatibleTextRenderingDefault(false);
// Environment.CurrentDirectory = @"C:\Program Files\EPLAN2.9\Platform\2.9.4\Bin\"; // x.x.x = your desired EPLAN version
AppDomain appDomain = AppDomain.CurrentDomain;
appDomain.AssemblyResolve += new ResolveEventHandler(MyResolveEventHandler);
Application.Run(new Form1());
}
static Assembly MyResolveEventHandler(object sender, ResolveEventArgs args)
{
Console.WriteLine("Resolving...");
string sAssemblyName = args.Name.Split(',')[0];
Assembly ass = Assembly.LoadFile(@"C:\Program Files\EPLAN2.9\Platform\2.9.4\Bin\" + sAssemblyName + ".dll");
return ass;
}
}
// wpf
public partial class App : Application
{
public App()
{
Environment.CurrentDirectory = @"C:\Program Files\EPLAN2.9\Platform\2.9.4\Bin\";
AppDomain appDomain = AppDomain.CurrentDomain;
appDomain.AssemblyResolve += new ResolveEventHandler(MyResolveEventHandler);
}
static System.Reflection.Assembly MyResolveEventHandler(object sender, ResolveEventArgs args)
{
Console.WriteLine("Resolving...");
string sAssemblyName = args.Name.Split(',')[0];
if (sAssemblyName.StartsWith("Eplan."))
{
System.Reflection.Assembly ass = System.Reflection.Assembly.LoadFile(@"C:\Program Files\EPLAN2.9\Platform\2.9.4\Bin\" + sAssemblyName + ".dll");
return ass;
}
else
{
// return null ;
return typeof(_Type).Assembly;
}
}
}
引入全部
public partial class App : Application
{
public App()
{
LoadEplanDll();
}
static void LoadEplanDll()
{
string path = @"C:\Program Files\EPLAN2.9\Platform\2.9.4\Bin";
DirectoryInfo folder = new DirectoryInfo(path);
foreach (FileInfo file in folder.GetFiles("*.dll"))
{
Console.WriteLine(file.FullName);
if (file.Name.StartsWith("Eplan."))
{
System.Reflection.Assembly.LoadFile(file.FullName);
}
}
}
}
初始化
private void Window_Loaded(object sender, RoutedEventArgs e)
{
m_oEplApp = new Eplan.EplApi.System.EplApplication();
String strAppModifier = "";
// System.String strAppModifier = "D:\\200-Document\\210-Work\\212-Code\\HFThridLine\\EplanWpfApp\\EplanWpfApp.exe.config";
m_oEplApp.Init(strAppModifier);
// Use the finder to find the correct eplan version if not yet known
EplanFinder oEplanFinder = new EplanFinder();
String strBinPath = oEplanFinder.SelectEplanVersion(true);
// Check if user has selected any Eplan variant (Electric P8, etc)
if (String.IsNullOrEmpty(strBinPath))
return;
//Use the AssemblyResolver to let the program know where all an Eplan variant can be found.
AssemblyResolver oResolver = new AssemblyResolver();
oResolver.SetEplanBinPath(strBinPath);
//Now pin to Eplan. This way all referenced eplan assemblies are loaded from the platform bin path.
oResolver.PinToEplan();
}
标签:初始化,return,System,Eplan,API,new,dll,public
From: https://www.cnblogs.com/alideluobo/p/17520010.html