Eplan API 初始化

发布时间 2023-10-20 14:33:10作者: 一杯清酒邀明月

Eplan支持的开发方式一共有3种

  1. 脚本
  2. dll文件形式
  3. 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文件保存。在【工具-脚本-执行】

 1 using Eplan.EplApi.Base;
 2 using Eplan.EplApi.Scripting;
 3 
 4 namespace EplanDemo.Script
 5 {
 6     class VerySimpleScript
 7     {
 8         [Start]
 9        public void MyFunction()
10         {
11           new Decider().Decide(EnumDecisionType.eOkDecision, "MyFunction was called!", "VerySimpleScript", EnumDecisionReturn.eOK, EnumDecisionReturn.eOK);
12           return;
13         }
14     }
15 }

加载

或者将其标注为 事件,动作,菜单加载项,在【工具-脚本-加载】

 1   public class SimpleEventHandler
 2     {
 3         [DeclareEventHandler("onMainStart")]
 4         public void MyEventHandlerFunction()
 5         {
 6             new Decider().Decide(EnumDecisionType.eOkDecision, "onMainStart was called!", "SimpleEventHandler", EnumDecisionReturn.eOK, EnumDecisionReturn.eOK);
 7             return;
 8         }
 9     }
10     public class RegisterScriptMenu
11     {
12         [DeclareAction("MyScriptActionWithMenu")]
13         public void MyFunctionAsAction()
14         {
15             new Decider().Decide(EnumDecisionType.eOkDecision, "MyFunctionAsAction was called!", "RegisterScriptMenu", EnumDecisionReturn.eOK, EnumDecisionReturn.eOK);
16             return;
17         }
18 
19         [DeclareMenu]
20         public void MenuFunction()
21         {
22             Eplan.EplApi.Gui.Menu oMenu = new Eplan.EplApi.Gui.Menu();
23             oMenu.AddMenuItem("MyMenuText", "MyScriptActionWithMenu");
24         }
25     }
26      public class SimpleScriptAction
27     {
28         [DeclareAction("MyScriptAction")]
29         public void MyFunctionAsAction()
30         {
31             new Decider().Decide(EnumDecisionType.eOkDecision, "MyFunctionAsAction was called!", "RegisterScriptAction", EnumDecisionReturn.eOK, EnumDecisionReturn.eOK);
32             return;
33         }
34     }

程序集(.dll)

dll文件对编译生成的名称有要求,名称的命名规范为<公司名称>.EplAddin. <NameOfTheProject>.dll.

为了减少不必要的其他bug的出现,推荐使用 .net framework4.7.2,输出的dll文件为64位。

编译完成之后打开epaln软件 选择【工具-API插件-加载-dll所在目录】

ps:dll文件重新编译之后,需要重新卸载之后加载,因为eplan对dll文件的引入采用的是复制dll文件到指定目录,防止在读取dll文件的时候,文件被修改暂用,对于引用的dll文件的其他依赖dll则会在dll的目录中查找。

这里简单做一个加载的菜单项

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using Eplan.EplApi.ApplicationFramework;
 7 using Eplan.EplApi.Gui;
 8 
 9 namespace EplanDemo
10 {
11     public class EplanMenu : IEplAddIn, IEplAddInShadowCopy
12     {
13 
14         private String m_strOriginalAssemblyPath;
15 
16         public void OnBeforeInit(string strOriginalAssemblyPath)
17         {
18             m_strOriginalAssemblyPath = strOriginalAssemblyPath;
19         }
20 
21         public bool OnExit()
22         {
23             return true;
24         }
25 
26         public bool OnInit()
27         {
28             return true;
29         }
30 
31       
32         public bool OnInitGui()
33         {
34             Init1();
35             return true;
36         }
37 
38         public bool OnRegister(ref bool bLoadOnStart)
39         {
40             bLoadOnStart = true;
41             return true;
42         }
43 
44         public bool OnUnregister()
45         {
46             return true;
47         }
48 
49       
50         public void Init1()
51         {
52             Menu menu = new Menu();
53             uint menuNum = menu.AddMainMenu("脚本加载", Menu.MainMenuName.eMainMenuHelp, "功能一", "MyScriptAction", "", 1);
54             menu.AddMenuItem("添加paramater", "AddParameterAction", "", menuNum, 0, false, false);
55             menu.AddMenuItem("显示文字", "ParameterAction", "", menuNum, 0, false, false);
56             menu.AddMenuItem("功能四", "PropertiesACTION", "", menuNum, 0, false, false);
57             menu.AddMenuItem("绘制宏", "DrawingAction", "", menuNum, 0, false, false);
58             menu.AddMenuItem("消息", "MessageAction", "", menuNum, 0, false, false);
59             menu.AddMenuItem("Action", "ActionMangerAction", "", menuNum, 0, false, false);
60         }
61     }
62 }

脱机程式

脱机程式为.exe可执行文件,基本上在windows上分为两种 Winfrom,Wpf

其初始赖于四个dll文件,引入其中。但程序想运行起来还是需要依赖于其他的dll。 在这里有两种处理方式。

  1. 将生成的程式的目录放置在程式的安装目录中 <eplan main path>\Platform\ <version>\Bin folder,
  2. 指定dll文件的形式。
    1. 在程式运行的时候通过反射动态加载未找到的dll文件
    2. 在程式加载的时候引入所有的dll文件

这里只讨论第二种的一二两种方式。

引入dll文件

winfrom在program.cs中,wpf则在app.xaml.cs中引入

动态加载
 1  static class Program
 2     {
 3         /// <summary>
 4         /// 应用程序的主入口点。 winfrom
 5         /// </summary>
 6         [STAThread]
 7         static void Main()
 8         {
 9             
10 
11           //  Application.EnableVisualStyles();
12           //  Application.SetCompatibleTextRenderingDefault(false);
13           //  Environment.CurrentDirectory = @"C:\Program Files\EPLAN2.9\Platform\2.9.4\Bin\"; // x.x.x = your desired EPLAN version
14             AppDomain appDomain = AppDomain.CurrentDomain;
15             appDomain.AssemblyResolve += new ResolveEventHandler(MyResolveEventHandler);
16 
17             Application.Run(new Form1());
18         }
19 
20         static Assembly MyResolveEventHandler(object sender, ResolveEventArgs args)
21         {
22             Console.WriteLine("Resolving...");
23             string sAssemblyName = args.Name.Split(',')[0];
24             Assembly ass = Assembly.LoadFile(@"C:\Program Files\EPLAN2.9\Platform\2.9.4\Bin\" + sAssemblyName + ".dll");
25             return ass;
26         }
27     }
28 // wpf
29  public partial class App : Application
30     {
31        
32         public App()
33         {
34 
35             Environment.CurrentDirectory = @"C:\Program Files\EPLAN2.9\Platform\2.9.4\Bin\";
36              AppDomain appDomain = AppDomain.CurrentDomain;
37             appDomain.AssemblyResolve += new ResolveEventHandler(MyResolveEventHandler);
38         }
39 
40         static System.Reflection.Assembly MyResolveEventHandler(object sender, ResolveEventArgs args)
41         {
42            
43                 Console.WriteLine("Resolving...");
44                 string sAssemblyName = args.Name.Split(',')[0];
45             if (sAssemblyName.StartsWith("Eplan."))
46             {
47                 System.Reflection.Assembly ass = System.Reflection.Assembly.LoadFile(@"C:\Program Files\EPLAN2.9\Platform\2.9.4\Bin\" + sAssemblyName + ".dll");
48                 return ass;
49             }
50             else
51             {
52                // return null ;
53                 return typeof(_Type).Assembly;
54             }
55         }
56     }
引入全部
 1  public partial class App : Application
 2     {
 3         public App()
 4         {
 5            LoadEplanDll();
 6         }
 7   
 8 
 9         static void LoadEplanDll()
10         {
11             string path = @"C:\Program Files\EPLAN2.9\Platform\2.9.4\Bin";
12             DirectoryInfo folder = new DirectoryInfo(path);
13 
14             foreach (FileInfo file in folder.GetFiles("*.dll"))
15             {
16                 Console.WriteLine(file.FullName);
17                 if (file.Name.StartsWith("Eplan."))
18                 {
19                     System.Reflection.Assembly.LoadFile(file.FullName);
20                 }
21             }
22         }
23     }

初始化

 1   private void Window_Loaded(object sender, RoutedEventArgs e)
 2         {
 3          
 4             m_oEplApp = new Eplan.EplApi.System.EplApplication();
 5             String strAppModifier = "";
 6            // System.String strAppModifier = "D:\\200-Document\\210-Work\\212-Code\\HFThridLine\\EplanWpfApp\\EplanWpfApp.exe.config";
 7             m_oEplApp.Init(strAppModifier);
 8 
 9             // Use the finder to find the correct eplan version if not yet known
10             EplanFinder oEplanFinder = new EplanFinder();
11             String strBinPath = oEplanFinder.SelectEplanVersion(true);
12 
13             // Check if user has selected any Eplan variant (Electric P8, etc)
14             if (String.IsNullOrEmpty(strBinPath))
15                 return;
16 
17             //Use the AssemblyResolver to let the program know where all an Eplan variant can be found.
18             AssemblyResolver oResolver = new AssemblyResolver();
19             oResolver.SetEplanBinPath(strBinPath);
20 
21             //Now pin to Eplan. This way all referenced eplan assemblies are loaded from the platform bin path.
22             oResolver.PinToEplan();
23 
24         }