Skip to content

Commit 35e8b4f

Browse files
syntronadeas31
andauthored
[ModelicaSystem] add plot() function (#352)
* [ModelicaSystem] add plot() function; see #144 * [ModelicaSystem] update plot() function - include checks * check if OMCProcessLocal is used * check for available resultfile * [ModelicaSystem] fix mypy - plot_result_file could be None * [ModelicaSystem.plot] add missing raise for exceptions * [ModelicaSystem.plot] fix elif usage * [ModelicaSystem] replace pathlib by OMCPath * [ModelicaSystem.plot] add comment why limited to OMCProcessLocal --------- Co-authored-by: Adeel Asghar <[email protected]>
1 parent 675a2ad commit 35e8b4f

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

OMPython/ModelicaSystem.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,6 +1104,34 @@ def simulate(
11041104

11051105
self._simulated = True
11061106

1107+
def plot(
1108+
self,
1109+
plotdata: str,
1110+
resultfile: Optional[str | os.PathLike] = None,
1111+
) -> None:
1112+
"""
1113+
Plot a variable using OMC; this will work for local OMC usage only (OMCProcessLocal). The reason is that the
1114+
plot is created by OMC which needs access to the local display. This is not the case for docker and WSL.
1115+
"""
1116+
1117+
if not isinstance(self._getconn.omc_process, OMCProcessLocal):
1118+
raise ModelicaSystemError("Plot is using the OMC plot functionality; "
1119+
"thus, it is only working if OMC is running locally!")
1120+
1121+
if resultfile is not None:
1122+
plot_result_file = self._getconn.omcpath(resultfile)
1123+
elif self._result_file is not None:
1124+
plot_result_file = self._result_file
1125+
else:
1126+
raise ModelicaSystemError("No resultfile available - either run simulate() before plotting "
1127+
"or provide a result file!")
1128+
1129+
if not plot_result_file.is_file():
1130+
raise ModelicaSystemError(f"Provided resultfile {repr(plot_result_file.as_posix())} does not exists!")
1131+
1132+
expr = f'plot({plotdata}, fileName="{plot_result_file.as_posix()}")'
1133+
self.sendExpression(expr=expr)
1134+
11071135
def getSolutions(
11081136
self,
11091137
varList: Optional[str | list[str]] = None,

0 commit comments

Comments
 (0)