You are on page 1of 3

AspenTech: Knowledge Base

Support Center Logout

Sample Macro: Automation of Aspen HYSYS with


Python
Products: Aspen HYSYS
Last Updated: 18-May-2017

Versions: V9.0
Article ID: 000044410

Primary Subject: 

Add Favorite
Add Favorite  

Applicable Version(s)
All versions
Problem Statement
How can I automate Aspen HYSYS from the Python language?

Solution
Regardless of which programming language you choose to automate Aspen HYSYS with, it is all
accomplished by creating and interacting with a HYSYS COM object. Unlike languages such as VBA,
VB.NET, and C#, Python does not automatically create a wrapper to interact with COM interfaces.

You can download and install a third-party extension known as "pywin32" to act as the additional
layer to facilitate communication between Python code and COM interfaces. Note that AspenTech
does not endorse the use of third-party openly-sourced modules, although Python owes its popularity
to the open-source implementation and the community of developers that this fosters.

Aside from the code to create the HYSYS COM object, the interaction with HYSYS interfaces

https://esupport.aspentech.com/S_Article?id=000044410[03.11.2017 14:54:00]
AspenTech: Knowledge Base

through COM is the same as with any other language. For more information on HYSYS interfaces,
you can browse the HYSYS type library with another program (such as Microsoft Excel or Visual
Studio). The knowledge base contains sample code for performing certain tasks via automation, and
most of these examples can be translated into Python code. However, since Python is a dynamically-
typed language that does not support static object declaration, Python does not support the creation
of BackDoor objects, so certain tasks that require you to create and access BackDoor objects cannot
be accomplished in Python.
 

# Requires the "pywin32" extension. You can get it from the Internet.
# Note: AspenTech does not condone the use of external openly-sourced modules.
# But then again, Python owes its popularity to the open-source implementation and the community that t
his fosters.
# Use at your own discretion and consult online resources for support with this extension.
from win32com.client import Dispatch

# Create the HYSYS COM object and open HYSYS V9.0.


hysysApp = Dispatch("HYSYS.Application.V9.0")
hysysApp.Visible = True

# Open the simulation case.


hysysCase = hysysApp.SimulationCases.Open(
"C:\Program Files (x86)\AspenTech\Aspen HYSYS V9.0\Samples\Ammonia Synthesis.hsc")
hysysCase.Visible = True

hysysCase.Solver.CanSolve = False

# Navigate to the Main flowsheet.


mainFlowsheet = hysysCase.Flowsheet

# Print the temperature of stream 25.


stream25 = mainFlowsheet.MaterialStreams.Item("25")
print("The temperature of stream 25 is", round(stream25.Temperature.GetValue("C"), 2), "C.")

# Set the temperature of stream 5 equal to 250 C.


stream5 = mainFlowsheet.MaterialStreams.Item("5")
stream5.Temperature.SetValue(250, "C")
print("Stream 5 temperature increased from 200 C to 250 C.")

# Print the temperature of stream 25.


print("The temperature of stream 25 is now", round(stream25.Temperature.GetValue("C"), 2), "C.")

# Output

The temperature of stream 25 is 68.7 C.


Stream 5 temperature increased from 200 C to 250 C.
The temperature of stream 25 is now 68.7 C.

Keywords
HYSYS, Automation, COM, Python, ActiveX

https://esupport.aspentech.com/S_Article?id=000044410[03.11.2017 14:54:00]
AspenTech: Knowledge Base

Rate this article:

Comments

Submit Comment

© 1994- 2017, Aspen Technology, Inc. All rights reserved Privacy Policy

https://esupport.aspentech.com/S_Article?id=000044410[03.11.2017 14:54:00]

You might also like