前言
在现代化工行业中,过程模拟和优化是提升生产效率和降低成本的重要手段。Aspen Hysys作为全球领先的过程模拟软件,广泛应用于石油化工、天然气处理、炼油等领域。借助Aspen Hysys强大的模拟能力,工程师们可以对工艺流程进行详细的分析和优化。然而,在实际应用中,我们常常需要将Aspen Hysys与其他软件系统集成,以实现自动化操作和数据共享。
HYSYS 是一个 OLE 自动化服务器。这意味着任何可以通过 OLE 自动化访问 HYSYS 的编程工具都可以用来访问 HYSYS。一些可以访问 HYSYS 的工具示例包括 Microsoft Visual Basic 和 Microsoft Excel,C++ ,C#以及其他许多工具。
因为作者从事化工流程仿真行业,比较熟悉C#语言。在这篇文章中,我将介绍如何使用C#与Aspen Hysys的API接口进行交互,以实现对Hysys模拟过程的自动化控制和数据提取。这不仅能够提高工作效率,还能够减少人为操作的错误。
另外:鼓励您使用正版Hysys授权,可以联系作者,本公司有售。
环境准备
需要准备的开发环境和文档,包括:
1.Aspen Hysys 软件:确保已经安装并可以正常运行。
2.Visual Studio:用于编写C#代码。
3.Aspen Hysys API 文档:Aspen Hysys官方文档了解API的详细信息,本文档可以在Hysys.exe程序的根目录C:\Program Files\AspenTech\Aspen HYSYS V12.1中找到,名字为“xhysys.chm”.
4.Aspen Hysys API 动态链接库: Aspen Hysys官方提高了API接口的DLL文件,可以在Hysys.exe程序的根目录C:\Program Files\AspenTech\Aspen HYSYS V12.1中找到,名字为“Aspentech.HYSYS.Interop.dll”.
接口架构介绍
Level 1 Application Object
通过阅读Hysys API文档,我们可以了解到,外部程序在操作Hysys时,首先需要获取HYSYS Application Object。 它表示HYSYS应用程序。是最顶层对象,所有其他Object都可以从中访问。
Application Object中包括了众多的Property 和 Method,介绍几个常用的例子:
ActiveDocument Property: Returns a SimulationCase object that represents the current active Simulation Case in HYSYS. Read only. 这是最基本的Property,我们可以通过它来获取Hysys程序中运行的Hysys Case对象。
Visible Property:For the Application object, this returns and sets the visibility of the HYSYS window. For the SimulationCase object, this returns and sets the visibility of the simulation case. Read-write. 通常在Hysys server作为动态模型服务器时,如果您想让模型仅在后台运行,不显示模型界面时,可以操作隐藏。
Level2 SimulationCase Object
SimulationCase代表一个HYSYS仿真Case(或者叫工程文件、仿真文件)。与仿真Case关联的所有其他对象都可以从该对象访问。它通常是从Application Object的ActiveDocument Property中获取。
Application Object中包括了众多的Property 和 Method,介绍几个常用的例子:
DataTables Property:Collection of all process data tables. 这是SimulationCase Object中的一个重要Property。Datatable通常用于汇总Hysys case中的I/O数据,从而在动态模式下,将模型的工艺计算参数读取出来,或将外部的控制信号写入到设备模型上。通过API操作DataTable,可以轻松的实现批量增删、移动、编辑信号标签及I/O类型。
Flowsheet Property:Returns the Flowsheet object that contains this object. For the SimulationCase object, returns the main Flowsheet of the simulation case. Hysys的模型是创建在Flowsheet中的,所以要通过API操作任何模型对象、流对象都必须先获取到所在的Flowsheet,当然Sub-Flowsheet是在Flowsheet的Flowsheet Property中的。
Solver Property:Returns the Solver object associated with the simulation case. 动态模型的运行规则和控制受控于Solver解算器,它可以设置动态运行规则、并可以操作模型运算的启动、停止、复位,并跟踪模型运行时间等。
Save Method:Saves the simulation case.
Level3 and other lower levels:Operations、Streams and so on
在Flowsheet 对象中,我们通常会使用API操作它内部的模型对象Operations或物质流股、能量流股Streams(material and energy)。这是我们批量组态Hysys模型的基础,后续的代码案例中会做案例介绍。
代码实现
这部分是文章的核心,详细描述如何在C#中使用Aspen Hysys API。以下是一个简单的示例代码,可以帮助你开始:
1.创建工程:打开Visual Studio,创建一个新的C#控制台应用程序。
2.添加引用:在项目中添加对Aspen Hysys COM库的引用。你可以在项目右键菜单中选择“添加引用”,然后在COM选项卡中找到Aspentech.HYSYS.Interop.dll进行添加。
using System;
using Aspentech.HYSYS;//引用Hysys API 动态链接库
namespace HysysApiExample
{
class Program
{
static void Main(string[] args)
{
}
}
}
3.获取 Application Object 和 SimulationCase Object:
如果你想打开一个Hysys Case,再进行操作的话可以如下:
using System;
using Aspentech.HYSYS;//引用Hysys API 动态链接库
namespace HysysApiExample
{
class Program
{
static void Main(string[] args)
{
// 创建一个HYSYS应用程序实例
Aspentech.HYSYS.Application hyApp = new Aspentech.HYSYS.Application();
// Hysys Case文件地址
string HysyscasePath= "C:\\Users\\Administrator\\Desktop\\HysysCase.hsc";
// 创建Case实例
Aspentech.HYSYS.SimulationCase simCase =
(SimulationCase)hyApp.SimulationCases.Open(HysyscasePath);
// 设置Case界面可见
simCase.Visible = true;
}
}
}
如果你想连接一个已经运行的Hysys Case,注意在抓取System Type时的progId和version字符串,Hysys V12.1版本为"HYSYS.Application.v12.1",如下:
using System;
using Aspentech.HYSYS;//引用Hysys API 动态链接库
namespace HysysApiExample
{
class Program
{
static void Main(string[] args)
{
// 获取HYSYS应用程序实例
Aspentech.HYSYS.Application hyApp ;
Type type = Type.GetTypeFromProgID("HYSYS.Application.v12.1");
dynamic instance = Activator.CreateInstance(type);
hyApp = instance;
// 获取Case实例
Aspentech.HYSYS.SimulationCase simCase =
(SimulationCase)hyApp.ActiveDocument;
}
}
}
4.操作Hysys Datatable:
在日常Hysys施工作业中,Hysys Datatable的组态是非常适合API快捷操作的,从而提高工作效率。但是需要注意的是:在同一张Datatable中,模型的取值对象和Tag名都不能重复,否则系统报错。
using System;
using Aspentech.HYSYS;//引用Hysys API 动态链接库
namespace HysysApiExample
{
class Program
{
static void Main(string[] args)
{
// 获取HYSYS应用程序实例
Aspentech.HYSYS.Application hyApp ;
Type type = Type.GetTypeFromProgID("HYSYS.Application.v12.1");
dynamic instance = Activator.CreateInstance(type);
hyApp = instance;
// 获取Case实例
Aspentech.HYSYS.SimulationCase simCase =
(SimulationCase)hyApp.ActiveDocument;
//获取第一个Hysys Datatable
Aspentech.HYSYS.DataTable dataTable =
(Aspentech.HYSYS.DataTable)simCase.DataTables[0];
//删除第二行数据
dataTable.VarDefinitions.Remove(1);
//获取全部数据行
Aspentech.HYSYS.VarDefinitions varDefinitions =
(Aspentech.HYSYS.VarDefinitions)dataTable.VarDefinitions;
//修改第二行数据
Aspentech.HYSYS.VarDefinition varDefinition =(Aspentech.HYSYS.VarDefinition)
varDefinitions[1];
varDefinition.Tag = "FI01_AI_SIOTag";
varDefinition.accessMode = DataTableAccessMode_enum.dtam_Read;
//添加一行数据,注意模型的取值对象和Tag名都不能重复,否则系统报错
dataTable.VarDefinitions.Add(varDefinition.Variable,
varDefinition.Description, varDefinition.Tag, varDefinition.Units,
varDefinition.accessMode);
}
}
}
5.操作模型对象Operations:
C#可以在外部通过Hysys API批量操作或组态模型对象,以阀门为例:
using System;
using Aspentech.HYSYS;//引用Hysys API 动态链接库
namespace HysysApiExample
{
class Program
{
static void Main(string[] args)
{
// 获取HYSYS应用程序实例
Aspentech.HYSYS.Application hyApp ;
Type type = Type.GetTypeFromProgID("HYSYS.Application.v12.1");
dynamic instance = Activator.CreateInstance(type);
hyApp = instance;
// 获取Case实例
Aspentech.HYSYS.SimulationCase simCase =
(SimulationCase)hyApp.ActiveDocument;
// 获取Flowsheet下全部模型对象
Aspentech.HYSYS.Operations Operations =
simCase.Flowsheet.Operations[OperationClassification_enum.oc_All];
//获取全部阀门对象
List<Valve>valves = new List<Valve>();
for (int i = 0; i < Operations.Count; i++)
{
try
{
Valve x = (Valve)Operations[i];
valves.Add(x);
}
catch
{
}
}
//更改第一个阀门信息
valves[0].name="ValveName1";//修改阀门名称
valves[0].ResistanceValue = 200;//修改阀门CV
valves[0].PercentOpenValue = 50;//操作阀门开度
}
}
}
6.操作流对象Streams:
C#可以在外部通过Hysys API批量操作或组态流对象,以物料流股为例:
using System;
using Aspentech.HYSYS;//引用Hysys API 动态链接库
namespace HysysApiExample
{
class Program
{
static void Main(string[] args)
{
// 获取HYSYS应用程序实例
Aspentech.HYSYS.Application hyApp ;
Type type = Type.GetTypeFromProgID("HYSYS.Application.v12.1");
dynamic instance = Activator.CreateInstance(type);
hyApp = instance;
// 获取Case实例
Aspentech.HYSYS.SimulationCase simCase =
(SimulationCase)hyApp.ActiveDocument;
// 获取全部Stream对象
Aspentech.HYSYS.Streams streams = simCase.Flowsheet.Streams;
//获取全部materialStreams
List<ProcessStream> materialStreams = new List<ProcessStream>();
for (int i = 0; i < streams.Count; i++)
{
ProcessStream processStream= (ProcessStream)streams[i];
if (processStream.TypeName != "energystream")
{
materialStreams.Add(processStream);
}
}
//修改第一个物料流股的信息
materialStreams[0].name = "Stream1";
//获得第一个物料流股的信息
double massflow = materialStreams[0].MassFlowValue;
}
}
}
总结
通过本文的介绍,我们可以初步了解如何在C#中使用Aspen Hysys API接口。我们从基础的环境设置开始,一步步展示了如何进行对象的创建、属性的设置以及数据的提取。通过实例代码和详细的说明,相信大家已经掌握了这一强大工具的基本用法。
在未来的项目中,借助Aspen Hysys API接口,你将能够更加高效地进行化工流程模拟和优化,不仅节省时间成本,还能提升整体的工作效率。希望这篇文章能为你的工作带来新的思路和灵感,让我们共同迈向更加智能和自动化的工程世界。
感谢你的阅读,期待与你在技术探索的道路上再次相遇!