Skip to content

Commit

Permalink
added code export for image exporter
Browse files Browse the repository at this point in the history
  • Loading branch information
rgerum committed Dec 28, 2023
1 parent 43e8ae8 commit 655be2c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
22 changes: 22 additions & 0 deletions saenopy/gui/solver/modules/exporter/Exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions saenopy/solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down

0 comments on commit 655be2c

Please sign in to comment.