EPLAN专题 ·

EPLAN P8 API开发入门

运行Microsoft Visual Studio并新建一个C#类库项目,如下图所示

EPLAN <wbr>API开发之入门篇

 

新建项目窗口上部选择.NET版本,并设置好项目名称和路径。

重命名C#源文件名为“AddInModule.cs”,类名改名为“AddInModule”。

EPLAN <wbr>API开发之入门篇   EPLAN <wbr>API开发之入门篇

 

在项目中添加引用“EPLAN API ApplicationFramework”、“EPLAN API Base”和“EPLAN API Gui”。

EPLAN <wbr>API开发之入门篇

 

 

修改项目属性里的程序集名为“EPLAN.EplAddin.MyAddIn”。

在类“AddInModule”中添加如下内容:

public class AddInModule : Eplan.EplApi.ApplicationFramework.IEplAddIn

    {

        public bool OnRegister(ref System.Boolean bLoadOnStart)

        {

            bLoadOnStart = true;

            return true;

        }

        public bool OnUnregister()

        {

            return true;

        }

        public bool OnInit()

        {

            return true;

        }

        public bool OnInitGui()

        {

            return true;

        }

        public bool OnExit()

        {

            return true;

        }

    }

 

在项目中添加类“MyAction.cs

EPLAN <wbr>API开发之入门篇

 

打开类“MyAction.cs”,在类中添加如下代码

class MyAction: IEplAction

    {

        public bool Execute(ActionCallingContext ctx )

      {

          System.Windows.Forms.MessageBox.Show("This is a test");

            return true;

      }

      public bool OnRegister(ref string Name, ref int Ordinal)

      {

            Name  = "MyAction";

            Ordinal     = 20;

            return true;

      }

      public  void GetActionProperties(ref ActionProperties actionProperties)

      {

           actionProperties.Description= "Action test with parameters.";

      }

    }

接下来在EPLAN菜单中添加自己的菜单项,在类“AddInModule”的函数“OnInitGui()”中添加如下代码

public bool OnInitGui()

{

      Eplan.EplApi.Gui.Menu oMenu = new Eplan.EplApi.Gui.Menu();

      oMenu.AddMenuItem("MyAction", "MyAction");

      return true;

}

到此,一个简单的EPLAN API接口就完成了,我们编译生成“EPLAN.EplAddin.MyAddIn.DLL”。运行EPLAN,并加载“EPLAN.EplAddin.MyAddIn.DLL”,大家观察一下工具菜单下多了一个“MyAction”菜单项,点击这个菜单项,会弹出一个“This is a test”的对话框,其实这就是类“MyAction.cs”中的函数“Execute()”的运行结果。

如果你需要点击菜单显示一个自己设计的窗口,只需要自己建立一个窗体类,然后在函数“Execute()”中建个对象就能实现,下面我们来试试看。

在项目中添加“windows 窗体”,并在窗体中插入一个label和一个button,如图所示

EPLAN <wbr>API开发之入门篇

修改函数“Execute()”如下

public bool Execute(ActionCallingContext ctx )

      {

            Form1 MyForm = new Form1();

            MyForm.ShowDialog();

            return true;

      }

      然后重新运行EPLAN,再次点击自建的菜单项“MyAction”,大家会发现刚才设计的窗体出现了。

EPLAN <wbr>API开发之入门篇

大家仔细浏览一下EPLAN API的帮助文件会发现,以上的这些内容都是在EPLAN API帮助文件中讲到的。所以大家不要认为这个帮助文件没什么用,对于一个没有钱去得到正规培训的人来说,这可是天上掉馅饼。能真正把这个帮助文件看透,我想,你需要实现的功能基本都可以实现了。像我这样ENGLISH不是很好的人靠金山词霸慢慢看都能学到些东西,相信大家学习的速度会比我快得多。

后面需要实现什么功能,需要大家自己去认真研究EPLAN提供的那些类了。当然,这是个很费脑力和时间的活,自己要想方设法去了解它熟悉它。只有真正熟悉了那些类的作用才能够正确的利用它来实现自己的需求。学习的过程是枯燥的,但是每找到一个方法或是搞懂一个问题的时候却也是欢喜的,祝大家收获不断,惊喜不断!

参与评论

  • East115

    低版本2.1里才有此属性,改用.AddParameter()

    3年前 (2022-01-03)
    回复
    回复East115
  • WALLE

    作者,你好,
    这一句有错误,不知道是怎么回事。能帮我分析一下吗?谢谢。
    actionProperties.Description= "Action test with parameters.";

    4年前 (2020-12-07)
    回复
    回复WALLE