在你的程序中使用 EPLAN API 对象最简单的方法是直接在代码中使用 API DLL 的功能。如果你的程序是一个 .NET 应用程序,那就更简单了:只需在项目中引用托管的 EPLAN API 程序集。这种类型的应用程序,我们称之为“离线应用程序”。

EplApi_AssemlyReference.jpg

然后——在适当的位置(例如在主窗体中)——创建一个 Eplan.EplApi.System.EplApplication 类的实例并进行初始化:

private Eplan.EplApi.System.EplApplication m_oEplApp;
public MainForm()
{
   //
   // Required for Windows Form Designer support
   //
   InitializeComponent();
   m_oEplApp = new Eplan.EplApi.System.EplApplication();
   System.String strAppModifier="";
   m_oEplApp.Init(strAppModifier);
}

字符串参数 strAppModifier 决定了使用哪个配置文件,从而确定加载哪些模块。如果像上述示例中那样传递一个空字符串,则会加载当前用户标准版本的 eplset.xml

在执行 Init() 函数后,除了一些暴露 GUI 功能的对象(如模态对话框、停靠对话框或 MDI 窗口)之外,EPLAN API 的所有功能/对象都可用。API 类和方法等的使用方式与编写普通 EPLAN 插件时相同。EPLAN 的一些选定的模态对话框由 Eplan.EplApi.System.EplApplication 类中的特殊方法提供。

当程序中不再需要 EPLAN API 时,应调用 EplApplication 对象的 Exit() 函数以卸载 API。

使用 Windows 窗体

在使用 Windows 窗体的离线应用程序中,应用程序在调用 EplApplication::Init 后可能会改变其大小。这种情况发生在操作系统的字体大小设置为非 100% 时,尤其是在使用大显示器时很常见。为了避免这种重新缩放的情况,请在应用程序的 .config 文件中设置 DPI 感知(DPI awareness):

<configuration>
  <system.windows.forms.applicationConfiguration>
    <add key="DpiAwareness" value="PerMonitorV2" />
  </system.windows.forms.applicationConfiguration>
</configuration>

通过在配置文件中添加上述设置,可以确保应用程序在不同 DPI 设置下保持正确的缩放比例。


<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
  <application>
    <!-- Windows 10 compatibility -->
    <supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />
  </application>
</compatibility>

如何确保 API 程序集直接从 EPLAN 平台的 BIN 文件夹加载?

正如在“EPLAN .NET API”主题中简要提到的,需要将路径设置为 <eplan main path>\\Platform\\<version>\\BIN 文件夹。更准确地说,你需要确保从这个文件夹中加载 EPLAN API 程序集。原因是这些 API 程序集静态链接了非托管依赖项,这些依赖项需要直接从当前目录加载。

这也是为什么通常无法将 EPLAN API DLL 注册到 GAC 中的原因。你的 Visual Studio 项目中引用的目录对 DLL 实际加载的位置没有影响。

你可以通过不同的方法确保 API 程序集从正确的 BIN 目录加载:

  1. 最简单的方法是:你可以将离线应用程序的可执行文件复制到 <eplan main path>\\Platform\\<version>\\BIN 文件夹中。
  2. 使用 EPLAN API 离线向导。这样,你的程序集将通过 Eplan.EplApi.Starter 库绑定到正确的 EPLAN 版本:
// Use the finder to find the correct EPLAN version if not yet known
EplanFinder oEplanFinder = new EplanFinder();
String strBinPath = oEplanFinder.SelectEplanVersion(true);

// Check if the user has selected any EPLAN variant (Electric P8, etc.)
if (String.IsNullOrEmpty(strBinPath))
    return;

// Use the AssemblyResolver to let the program know where all EPLAN variants 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();

// Use a separate class to initialize EplApplication. Pass the path to the EPLAN product variant BIN directory in order to set the EplApplication.EplanBinFolder property
Form1 oForm = new Form1();
oForm.EplanBinFolder = oResolver.GetEplanBinPath();
Application.Run(oForm);
  1. 在应用程序配置文件中发布所有所需的API程序集代码库。(这是一个XML文件,文件名与可执行文件相同,但附加了“.config”扩展名,例如“MyApplication.exe.config”)。以下代码展示了这样一个配置文件的示例内容。