diff --git a/saenopy/gui/solver/modules/exporter/Exporter.py b/saenopy/gui/solver/modules/exporter/Exporter.py index ca90378c..2b8fc5f7 100644 --- a/saenopy/gui/solver/modules/exporter/Exporter.py +++ b/saenopy/gui/solver/modules/exporter/Exporter.py @@ -170,6 +170,7 @@ class Null: QtShortCuts.QPushButton(None, "save parameters", self.save_parameters) QtShortCuts.QPushButton(None, "load parameters", self.load_parameters) QtShortCuts.QPushButton(None, "copy to clipboard parameters", self.copy_parameters) + QtShortCuts.QPushButton(None, "export code", self.generate_code) with QtShortCuts.QHBoxLayout(): self.input_use2D = QtShortCuts.QInputBool(None, "", False, icon=["3D", "2D"], group=True) @@ -590,6 +591,27 @@ def copy_parameters(self): cb = QtGui.QGuiApplication.clipboard() cb.setText(text, mode=cb.Clipboard) + def generate_code(self): + if self.result is None: + return + new_path = QtWidgets.QFileDialog.getSaveFileName(None, "Save Session as Script", os.getcwd(), "Python File (*.py)") + if new_path: + # ensure filename ends in .py + if not new_path.endswith(".py"): + new_path += ".py" + + run_code = f"""import matplotlib.pyplot as plt +import saenopy + +params = {json.dumps(self.get_parameters(), indent=2).replace("true", "True").replace("false", "False").replace("null", "None")} + +im = saenopy.render_image(params, saenopy.load("{self.result.output}")) +plt.imsave("output.png", im) +""" + #print(run_code) + with open(new_path, "w") as fp: + fp.write(run_code) + def progress_iterator(self, iter): self.render_progress.setRange(0, len(iter)) self.render_progress.setValue(0) diff --git a/saenopy/solver.py b/saenopy/solver.py index 5f831a5e..0dc497dc 100644 --- a/saenopy/solver.py +++ b/saenopy/solver.py @@ -1000,6 +1000,13 @@ def load_solver(filename: str) -> Solver: def load(filename: str) -> Result: + from saenopy.gui.spheroid.modules.result import ResultSpheroid + from saenopy.gui.tfm2d.modules.result import Result2D + + if filename.endswith(".saenopySpheroid"): + return ResultSpheroid.load(filename) + if filename.endswith(".saenopy2D"): + return Result2D.load(filename) return Result.load(filename)