2.创建Cape-Open模块以混合器为例子

2.创建Cape-Open模块以混合器为例子

2 年前 · 来自专栏 CAPE-OPEN化工模拟学习

距离上次写完介绍已经过去了3个月,三个月里忙完了主修的毕业设计与辅修的毕业设计,现在终于又有时间好好的研究讲解CapeOpen了

在本篇中主要说一下如何从0开始创建一个属于自己的Cape-Open模块

本文的操作环境为windows10专业版,VS2019社区版本,CaoeOpen1.0,使用C#开发,Cape-open类库文末我会给出百度网盘链接。

1.项目创建与导入

在开始前需要先安装好CapeOpen类库,或者清楚"CapeOpen"dll文件的在电脑中存放位置。

开启VS2019,选择新建项目,创建类库(.Net Standard),配置项目名称以及存放位置后点击创建按钮,项目名为"mixer"。

在完成项目创建之后需要导入CapeOpen类库,在解决方案管理器中右击引用,后选择添加引用

在出现的界面中选择COM,然后选择所需要的CapeOpen版本,点击确定导入;或通过浏览找到CapeOpen.dll的所在目录导入。

2.组件注册

Windows系统中通过GUID标识COM对象的类,因此需要获得GUID来对项目与类进行标识。在VS2019中在搜索界面直接输入guid可以打开GUID生成器,选择注册表格式,后复制。

在代码中登记guid

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CapeOpen;
namespace Mixer
    [Serializable]
    [System.Runtime.InteropServices.ComVisible(true)]
    [System.Runtime.InteropServices.Guid("14740336-31E9-42B4-B4E3-8A727DCA2DA1")]//ICapeThermoMaterialObject_IID)
    [System.ComponentModel.Description("")]//模块的描述
    [CapeOpen.CapeNameAttribute("Mixerstudy")]//模块的名称
    [CapeOpen.CapeDescriptionAttribute("第一个用C#编写的混合器模块")]
    [CapeOpen.CapeVersionAttribute("1.0")]//Capeopen版本

在此时如果直接生成dll文件,则会出现不能实现继承的抽象成员问题,接下来我们来解决这个问题。

3.模块信息定义

每一个模块需要实现单元操作构造函数或初始化方法。模块的参数和端口集合是在CapeOpen.CapeUnitBase中创建的。CapeUnitBase为类构造函数,需要在创建对象时在派生类的构造函数之前调用。在流程建模环境中需要先调用ICapeUtilities.Initialize()方法初始化定义的组件参数或者组件管口。Initialize()方法本身以一个虚函数的形式存在。

添加管口或者参数有两种方法

1.类中构造

public Mixerstudy()
            // Add Ports using the UnitPort constructor.
            this.Ports.Add(new UnitPort("Inlet Port1", "Test Inlet Port1", CapePortDirection.CAPE_INLET, CapePortType.CAPE_MATERIAL));
            this.Ports.Add(new UnitPort("Inlet Port2", "Test Inlet Port2", CapePortDirection.CAPE_INLET, CapePortType.CAPE_MATERIAL));
            this.Ports.Add(new UnitPort("Outlet Port", "Test Outlet Port", CapePortDirection.CAPE_OUTLET, CapePortType.CAPE_MATERIAL));
            // Add a real valued parameter using the RealParameter  constructor.
            RealParameter real = new RealParameter("PressureDrop", "Drop in pressure between the outlet from the mixer and the pressure of the lower pressure inlet.", 0.0, 0.0, 0.0, 100000000.0, CapeParamMode.CAPE_INPUT, "Pa");
            this.Parameters.Add(real);
            // Add a real valued parameter using the IntegerParameter  constructor.
            this.Parameters.Add(new IntegerParameter("Integer Parameter", "This is an example of an integer parameter.", 12, 12, 0, 100, CapeParamMode.CAPE_INPUT_OUTPUT));
            // Add a real valued parameter using the BooleanParameter  constructor.
            this.Parameters.Add(new BooleanParameter("Boolean Parameter", "This is an example of a boolean parameter.", false, false, CapeOpen.CapeParamMode.CAPE_INPUT_OUTPUT));
            // Create an array of strings for the option parameter restricted value list.
            String[] options = { "Test Value", "Another Value" };
            // Add a string valued parameter using the OptionParameter constructor.
            this.Parameters.Add(new OptionParameter("OptionParameter", "This is an example of an option parameter.", "Test Value", "Test Value", options, true, CapeParamMode.CAPE_INPUT_OUTPUT));
            // Add an available report.
            this.Reports.Add("Report 2");
        }

2.采用Initialize()方法生成

public override void Initialize()
        this.Ports.Add(new CapeOpen.CUnitPort("Inlet Port1","Test Inlet Port1", CapeOpen.CapePortDirection.CAPE_INLET,CapeOpen.CapePortType.CAPE_MATERIAL));
        this.Ports.Add(new CapeOpen.CUnitPort("Inlet Port2", "Test Inlet Port2", CapeOpen.CapePortDirection.CAPE_INLET, CapeOpen.CapePortType.CAPE_MATERIAL));
        this.Ports.Add(new CapeOpen.CUnitPort("Outlet Port","Test Outlet Port", CapeOpen.CapePortDirection.CAPE_OUTLET, CapeOpen.CapePortType.CAPE_MATERIAL));
        this.Parameters.Add(new CapeOpen.CRealParameter("PressureDrop","Drop in pressure between the outlet from the mixer and thepressure of the lower pressure inlet.", 0.0, 0.0, 0.0,100000000.0, CapeOpen.CapeParamModeCAPE_INPUT, "Pa"));
        this.Parameters.Add(new CapeOpen.CIntParameter("Parameter2",12, CapeOpen.CapeParamMode.CAPE_INPUT_OUTPUT));
        this.Parameters.Add(new CapeOpen.CBoolParameter("Parameter3",false, CapeOpen.CapeParamMode.CAPE_INPUT_OUTPUT));
        String[] options = { "Test Value", "Another Value" };
        this.Parameters.Add(new CapeOpen.COptionParameter("Parameter4", "OptionParameter", "Test Value", "Test Value", options, true, CapeOpen.CapeParamMode.CAPE_INPUT_OUTPUT));
        this.AvailableReports.Add("Report 2");