From 41fe0e55364b7449ca729f6f4da7a96bd731382a Mon Sep 17 00:00:00 2001 From: syntron Date: Thu, 21 Aug 2025 21:08:54 +0200 Subject: [PATCH 1/7] [ModelicaSystem] add plot() function; see #144 --- OMPython/ModelicaSystem.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/OMPython/ModelicaSystem.py b/OMPython/ModelicaSystem.py index ae4a5108..012f6262 100644 --- a/OMPython/ModelicaSystem.py +++ b/OMPython/ModelicaSystem.py @@ -1141,6 +1141,28 @@ def simulate( self._simulated = True + def plot( + self, + plotdata: str, + resultfile: Optional[str] = None, + ) -> None: + """ + Plot via OMC. + """ + + if resultfile is None: + # default result file generated by OM + plot_result_file = self.getWorkDirectory() / f"{self._model_name}_res.mat" + elif os.path.exists(resultfile): + plot_result_file = pathlib.Path(resultfile) + else: + ModelicaSystemError("No resultfile available - either run simulate() before plotting " + "or provide a result file!") + + expr = f'plot({plotdata}, fileName="{plot_result_file.as_posix()}")' + + self.sendExpression(expr=expr) + def getSolutions( self, varList: Optional[str | list[str]] = None, From 35b1d9351ab134dbba8f7c8147ec94d307f51985 Mon Sep 17 00:00:00 2001 From: syntron Date: Sat, 23 Aug 2025 14:41:35 +0200 Subject: [PATCH 2/7] [ModelicaSystem] update plot() function - include checks * check if OMCProcessLocal is used * check for available resultfile --- OMPython/ModelicaSystem.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/OMPython/ModelicaSystem.py b/OMPython/ModelicaSystem.py index 012f6262..4cd5c6e0 100644 --- a/OMPython/ModelicaSystem.py +++ b/OMPython/ModelicaSystem.py @@ -1144,21 +1144,28 @@ def simulate( def plot( self, plotdata: str, - resultfile: Optional[str] = None, + resultfile: Optional[str | os.PathLike] = None, ) -> None: """ - Plot via OMC. + Plot a variable using OMC; this will work for local OMC usage only (OMCProcessLocal). """ - if resultfile is None: - # default result file generated by OM - plot_result_file = self.getWorkDirectory() / f"{self._model_name}_res.mat" - elif os.path.exists(resultfile): + if not isinstance(self._getconn.omc_process, OMCProcessLocal): + raise ModelicaSystemError("Plot is using the OMC plot functionality; " + "thus, it is only working if OMC is running locally!") + + plot_result_file = None + if resultfile is not None: plot_result_file = pathlib.Path(resultfile) + elif self._result_file is not None: + plot_result_file = pathlib.Path(self._result_file) else: ModelicaSystemError("No resultfile available - either run simulate() before plotting " "or provide a result file!") + if not plot_result_file.is_file(): + ModelicaSystemError(f"Provided resultfile {repr(plot_result_file.as_posix())} does not exists!") + expr = f'plot({plotdata}, fileName="{plot_result_file.as_posix()}")' self.sendExpression(expr=expr) From 4b01e00a3272018d5022eb391ab0cc80bb714b9d Mon Sep 17 00:00:00 2001 From: syntron Date: Tue, 26 Aug 2025 21:06:33 +0200 Subject: [PATCH 3/7] [ModelicaSystem] fix mypy - plot_result_file could be None --- OMPython/ModelicaSystem.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/OMPython/ModelicaSystem.py b/OMPython/ModelicaSystem.py index 4cd5c6e0..1907addb 100644 --- a/OMPython/ModelicaSystem.py +++ b/OMPython/ModelicaSystem.py @@ -1163,12 +1163,13 @@ def plot( ModelicaSystemError("No resultfile available - either run simulate() before plotting " "or provide a result file!") - if not plot_result_file.is_file(): + if plot_result_file is None: + ModelicaSystemError("No resultfile defined!") + elif not plot_result_file.is_file(): ModelicaSystemError(f"Provided resultfile {repr(plot_result_file.as_posix())} does not exists!") - - expr = f'plot({plotdata}, fileName="{plot_result_file.as_posix()}")' - - self.sendExpression(expr=expr) + else: + expr = f'plot({plotdata}, fileName="{plot_result_file.as_posix()}")' + self.sendExpression(expr=expr) def getSolutions( self, From 0f58f655ee970ff906b8839af3abc87eab18be11 Mon Sep 17 00:00:00 2001 From: syntron Date: Thu, 16 Oct 2025 08:58:17 +0200 Subject: [PATCH 4/7] [ModelicaSystem.plot] add missing raise for exceptions --- OMPython/ModelicaSystem.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/OMPython/ModelicaSystem.py b/OMPython/ModelicaSystem.py index 1907addb..c0d3fc09 100644 --- a/OMPython/ModelicaSystem.py +++ b/OMPython/ModelicaSystem.py @@ -1160,13 +1160,13 @@ def plot( elif self._result_file is not None: plot_result_file = pathlib.Path(self._result_file) else: - ModelicaSystemError("No resultfile available - either run simulate() before plotting " - "or provide a result file!") + raise ModelicaSystemError("No resultfile available - either run simulate() before plotting " + "or provide a result file!") if plot_result_file is None: - ModelicaSystemError("No resultfile defined!") + raise ModelicaSystemError("No resultfile defined!") elif not plot_result_file.is_file(): - ModelicaSystemError(f"Provided resultfile {repr(plot_result_file.as_posix())} does not exists!") + raise ModelicaSystemError(f"Provided resultfile {repr(plot_result_file.as_posix())} does not exists!") else: expr = f'plot({plotdata}, fileName="{plot_result_file.as_posix()}")' self.sendExpression(expr=expr) From 904b292badd22f8f224f8e8d745b08f18312a6ac Mon Sep 17 00:00:00 2001 From: syntron Date: Thu, 16 Oct 2025 09:00:52 +0200 Subject: [PATCH 5/7] [ModelicaSystem.plot] fix elif usage --- OMPython/ModelicaSystem.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/OMPython/ModelicaSystem.py b/OMPython/ModelicaSystem.py index c0d3fc09..7f5fcf1d 100644 --- a/OMPython/ModelicaSystem.py +++ b/OMPython/ModelicaSystem.py @@ -1165,11 +1165,11 @@ def plot( if plot_result_file is None: raise ModelicaSystemError("No resultfile defined!") - elif not plot_result_file.is_file(): + if not plot_result_file.is_file(): raise ModelicaSystemError(f"Provided resultfile {repr(plot_result_file.as_posix())} does not exists!") - else: - expr = f'plot({plotdata}, fileName="{plot_result_file.as_posix()}")' - self.sendExpression(expr=expr) + + expr = f'plot({plotdata}, fileName="{plot_result_file.as_posix()}")' + self.sendExpression(expr=expr) def getSolutions( self, From 63475c726ee38c0938cc31855946744fef4b9c0e Mon Sep 17 00:00:00 2001 From: syntron Date: Tue, 4 Nov 2025 12:26:43 +0100 Subject: [PATCH 6/7] [ModelicaSystem] replace pathlib by OMCPath --- OMPython/ModelicaSystem.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/OMPython/ModelicaSystem.py b/OMPython/ModelicaSystem.py index 7f5fcf1d..a64a1637 100644 --- a/OMPython/ModelicaSystem.py +++ b/OMPython/ModelicaSystem.py @@ -1154,17 +1154,14 @@ def plot( raise ModelicaSystemError("Plot is using the OMC plot functionality; " "thus, it is only working if OMC is running locally!") - plot_result_file = None if resultfile is not None: - plot_result_file = pathlib.Path(resultfile) + plot_result_file = self._getconn.omcpath(resultfile) elif self._result_file is not None: - plot_result_file = pathlib.Path(self._result_file) + plot_result_file = self._result_file else: raise ModelicaSystemError("No resultfile available - either run simulate() before plotting " "or provide a result file!") - if plot_result_file is None: - raise ModelicaSystemError("No resultfile defined!") if not plot_result_file.is_file(): raise ModelicaSystemError(f"Provided resultfile {repr(plot_result_file.as_posix())} does not exists!") From 6023eff6fb854da12114b89eb3d9ac13bca6ffa8 Mon Sep 17 00:00:00 2001 From: syntron Date: Tue, 4 Nov 2025 12:28:46 +0100 Subject: [PATCH 7/7] [ModelicaSystem.plot] add comment why limited to OMCProcessLocal --- OMPython/ModelicaSystem.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/OMPython/ModelicaSystem.py b/OMPython/ModelicaSystem.py index a64a1637..afdeeb63 100644 --- a/OMPython/ModelicaSystem.py +++ b/OMPython/ModelicaSystem.py @@ -1147,7 +1147,8 @@ def plot( resultfile: Optional[str | os.PathLike] = None, ) -> None: """ - Plot a variable using OMC; this will work for local OMC usage only (OMCProcessLocal). + Plot a variable using OMC; this will work for local OMC usage only (OMCProcessLocal). The reason is that the + plot is created by OMC which needs access to the local display. This is not the case for docker and WSL. """ if not isinstance(self._getconn.omc_process, OMCProcessLocal):