aspen_pysys.app

Package containing all Pythonic functionality for the HYSYS app.

1# Copyright 2026 Hariidaran Tamilmaran
2
3"""Package containing all Pythonic functionality for the HYSYS app."""
4
5from aspen_pysys.app.hysys import HysysApp
6
7__all__ = ["HysysApp"]
class HysysApp:
 24class HysysApp:
 25    """Class that interfaces with the HYSYS application."""
 26
 27    _logger = logging.getLogger(__name__)
 28
 29    def __init__(self, app: CDispatch) -> None:
 30        self._app: CDispatch = app
 31
 32    @staticmethod
 33    def dispatch(
 34        *,
 35        should_be_visible: bool = True,
 36        should_log_version: bool = False,
 37    ) -> HysysApp:
 38        """Dispatch the HYSYS app.
 39
 40        Args:
 41            should_be_visible (bool, optional): If the app should be visible. Defaults to True.
 42            should_log_version (bool, optional): If the app version should be logged. Defaults to False.
 43
 44        Returns:
 45            HysysApp: HYSYS app wrapper
 46
 47        Raises:
 48            PysysError: App was unable to be dispatched.
 49        """  # noqa: E501
 50        app: CDispatch | None = win32com.client.Dispatch(APP_STR)
 51
 52        if app is None:
 53            message = "The HYSYS app was unable to be dispatched."
 54            raise PysysError(message)
 55
 56        if should_be_visible:
 57            app.Visible = 1
 58
 59        if should_log_version:
 60            HysysApp._logger.info(app.Application.LongVersion)
 61
 62        return HysysApp(app)
 63
 64    def open(self, filepath: str | Path) -> HysysCase:
 65        """Open a simulation case.
 66
 67        Args:
 68            filepath (str | Path): Path to the simulation case
 69
 70        Returns:
 71            HysysCase: Simulation case
 72
 73        Raises:
 74            PysysError: Cannot open case at provided filepath.
 75        """
 76        try:
 77            case_object: CDispatch = self._app.SimulationCases.Open(filepath)
 78
 79            simcase = HysysCase(case_object)
 80            simcase.activate()
 81        except com_error as err:
 82            message = f"Error opening filepath '{filepath}'."
 83            raise PysysError(message) from err
 84        else:
 85            return simcase
 86
 87    def quit(self) -> None:
 88        """Quit the HYSYS app.
 89
 90        Raises:
 91            PysysError: Error quitting app.
 92        """
 93        try:
 94            self._app.Quit()
 95        except com_error as err:
 96            message = "Error quitting app."
 97            raise PysysError(message) from err
 98
 99    def _get_active_document(self) -> CDispatch:
100        return self._app.ActiveDocument
101
102    def has_active_document(self) -> bool:
103        """Check if the app has an active document.
104
105        Returns:
106            bool: If the app has an active document
107        """
108        return self._get_active_document() is not None
109
110    def open_active_document(self) -> HysysCase:
111        """Open the active document if present.
112
113        Returns:
114            HysysCase: Opened simulation case
115
116        Raises:
117            PysysError: No active document.
118            PysysError: Error opening active document.
119        """
120        if not self.has_active_document():
121            message = "There is no active document."
122            raise PysysError(message)
123
124        try:
125            case_object = self._get_active_document()
126
127            simcase = HysysCase(case_object)
128            simcase.activate()
129        except com_error as err:
130            message = "Error opening active document."
131            raise PysysError(message) from err
132        else:
133            return simcase
134
135    def close_all(self) -> None:
136        """Close all open simulation cases.
137
138        Raises:
139            PysysError: Error closing simulation cases.
140        """
141        try:
142            self._app.SimulationCases.Close()
143        except com_error as err:
144            message = "Error closing simulation cases."
145            raise PysysError(message) from err

Class that interfaces with the HYSYS application.

HysysApp(app: win32com.client.CDispatch)
29    def __init__(self, app: CDispatch) -> None:
30        self._app: CDispatch = app
@staticmethod
def dispatch( *, should_be_visible: bool = True, should_log_version: bool = False) -> HysysApp:
32    @staticmethod
33    def dispatch(
34        *,
35        should_be_visible: bool = True,
36        should_log_version: bool = False,
37    ) -> HysysApp:
38        """Dispatch the HYSYS app.
39
40        Args:
41            should_be_visible (bool, optional): If the app should be visible. Defaults to True.
42            should_log_version (bool, optional): If the app version should be logged. Defaults to False.
43
44        Returns:
45            HysysApp: HYSYS app wrapper
46
47        Raises:
48            PysysError: App was unable to be dispatched.
49        """  # noqa: E501
50        app: CDispatch | None = win32com.client.Dispatch(APP_STR)
51
52        if app is None:
53            message = "The HYSYS app was unable to be dispatched."
54            raise PysysError(message)
55
56        if should_be_visible:
57            app.Visible = 1
58
59        if should_log_version:
60            HysysApp._logger.info(app.Application.LongVersion)
61
62        return HysysApp(app)

Dispatch the HYSYS app.

Arguments:
  • should_be_visible (bool, optional): If the app should be visible. Defaults to True.
  • should_log_version (bool, optional): If the app version should be logged. Defaults to False.
Returns:

HysysApp: HYSYS app wrapper

Raises:
  • PysysError: App was unable to be dispatched.
def open( self, filepath: str | pathlib.Path) -> aspen_pysys.case.HysysCase:
64    def open(self, filepath: str | Path) -> HysysCase:
65        """Open a simulation case.
66
67        Args:
68            filepath (str | Path): Path to the simulation case
69
70        Returns:
71            HysysCase: Simulation case
72
73        Raises:
74            PysysError: Cannot open case at provided filepath.
75        """
76        try:
77            case_object: CDispatch = self._app.SimulationCases.Open(filepath)
78
79            simcase = HysysCase(case_object)
80            simcase.activate()
81        except com_error as err:
82            message = f"Error opening filepath '{filepath}'."
83            raise PysysError(message) from err
84        else:
85            return simcase

Open a simulation case.

Arguments:
  • filepath (str | Path): Path to the simulation case
Returns:

HysysCase: Simulation case

Raises:
  • PysysError: Cannot open case at provided filepath.
def quit(self) -> None:
87    def quit(self) -> None:
88        """Quit the HYSYS app.
89
90        Raises:
91            PysysError: Error quitting app.
92        """
93        try:
94            self._app.Quit()
95        except com_error as err:
96            message = "Error quitting app."
97            raise PysysError(message) from err

Quit the HYSYS app.

Raises:
  • PysysError: Error quitting app.
def has_active_document(self) -> bool:
102    def has_active_document(self) -> bool:
103        """Check if the app has an active document.
104
105        Returns:
106            bool: If the app has an active document
107        """
108        return self._get_active_document() is not None

Check if the app has an active document.

Returns:

bool: If the app has an active document

def open_active_document(self) -> aspen_pysys.case.HysysCase:
110    def open_active_document(self) -> HysysCase:
111        """Open the active document if present.
112
113        Returns:
114            HysysCase: Opened simulation case
115
116        Raises:
117            PysysError: No active document.
118            PysysError: Error opening active document.
119        """
120        if not self.has_active_document():
121            message = "There is no active document."
122            raise PysysError(message)
123
124        try:
125            case_object = self._get_active_document()
126
127            simcase = HysysCase(case_object)
128            simcase.activate()
129        except com_error as err:
130            message = "Error opening active document."
131            raise PysysError(message) from err
132        else:
133            return simcase

Open the active document if present.

Returns:

HysysCase: Opened simulation case

Raises:
  • PysysError: No active document.
  • PysysError: Error opening active document.
def close_all(self) -> None:
135    def close_all(self) -> None:
136        """Close all open simulation cases.
137
138        Raises:
139            PysysError: Error closing simulation cases.
140        """
141        try:
142            self._app.SimulationCases.Close()
143        except com_error as err:
144            message = "Error closing simulation cases."
145            raise PysysError(message) from err

Close all open simulation cases.

Raises:
  • PysysError: Error closing simulation cases.