1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
# fmt: off
"""Dialog for saving one or more configurations."""
import numpy as np
import ase.gui.ui as ui
from ase.gui.i18n import _
from ase.io.formats import (
filetype,
get_ioformat,
parse_filename,
string2index,
write,
)
text = _("""\
Append name with "@n" in order to write image
number "n" instead of the current image. Append
"@start:stop" or "@start:stop:step" if you want
to write a range of images. You can leave out
"start" and "stop" so that "name@:" will give
you all images. Negative numbers count from the
last image. Examples: "name@-1": last image,
"name@-2:": last two.""")
def save_dialog(gui, filename=None):
dialog = ui.SaveFileDialog(gui.window.win, _('Save ...'))
ui.Text(text).pack(dialog.top)
filename = filename or dialog.go()
if not filename:
return
filename, index = parse_filename(filename)
if index is None:
index = slice(gui.frame, gui.frame + 1)
elif isinstance(index, str):
index = string2index(index)
elif isinstance(index, slice):
pass
else:
if index < 0:
index += len(gui.images)
index = slice(index, index + 1)
format = filetype(filename, read=False)
io = get_ioformat(format)
extra = {}
remove_hidden = False
if format in ['png', 'eps', 'pov']:
bbox = np.empty(4)
size = gui.window.size / gui.scale
bbox[0:2] = np.dot(gui.center, gui.axes[:, :2]) - size / 2
bbox[2:] = bbox[:2] + size
extra['rotation'] = gui.axes
extra['show_unit_cell'] = gui.window['toggle-show-unit-cell']
extra['bbox'] = bbox
colors = gui.get_colors(rgb=True)
extra['colors'] = [rgb for rgb, visible
in zip(colors, gui.images.visible)
if visible]
remove_hidden = True
images = [gui.images.get_atoms(i, remove_hidden=remove_hidden)
for i in range(*index.indices(len(gui.images)))]
if len(images) > 1 and io.single:
# We want to write multiple images, but the file format does not
# support it. The solution is to write multiple files, inserting
# a number in the file name before the suffix.
j = filename.rfind('.')
filename = filename[:j] + '{0:05d}' + filename[j:]
for i, atoms in enumerate(images):
write(filename.format(i), atoms, **extra)
else:
try:
write(filename, images, **extra)
except Exception as err:
from ase.gui.ui import showerror
showerror(_('Error'), err)
raise
|