Coverage for /builds/ase/ase/ase/gui/save.py: 79.59%

49 statements  

« prev     ^ index     » next       coverage.py v7.5.3, created at 2025-08-02 00:12 +0000

1# fmt: off 

2 

3"""Dialog for saving one or more configurations.""" 

4 

5import numpy as np 

6 

7import ase.gui.ui as ui 

8from ase.gui.i18n import _ 

9from ase.io.formats import ( 

10 filetype, 

11 get_ioformat, 

12 parse_filename, 

13 string2index, 

14 write, 

15) 

16 

17text = _("""\ 

18Append name with "@n" in order to write image 

19number "n" instead of the current image. Append 

20"@start:stop" or "@start:stop:step" if you want 

21to write a range of images. You can leave out 

22"start" and "stop" so that "name@:" will give 

23you all images. Negative numbers count from the 

24last image. Examples: "name@-1": last image, 

25"name@-2:": last two.""") 

26 

27 

28def save_dialog(gui, filename=None): 

29 dialog = ui.SaveFileDialog(gui.window.win, _('Save ...')) 

30 # fix tkinter not automatically setting dialog type 

31 # remove from Python3.8+ 

32 # see https://github.com/python/cpython/pull/25187 

33 # and https://bugs.python.org/issue43655 

34 # and https://github.com/python/cpython/pull/25592 

35 ui.set_windowtype(dialog.top, 'dialog') 

36 ui.Text(text).pack(dialog.top) 

37 filename = filename or dialog.go() 

38 if not filename: 

39 return 

40 

41 filename, index = parse_filename(filename) 

42 if index is None: 

43 index = slice(gui.frame, gui.frame + 1) 

44 elif isinstance(index, str): 

45 index = string2index(index) 

46 elif isinstance(index, slice): 

47 pass 

48 else: 

49 if index < 0: 

50 index += len(gui.images) 

51 index = slice(index, index + 1) 

52 format = filetype(filename, read=False) 

53 io = get_ioformat(format) 

54 

55 extra = {} 

56 remove_hidden = False 

57 if format in ['png', 'eps', 'pov']: 

58 bbox = np.empty(4) 

59 size = gui.window.size / gui.scale 

60 bbox[0:2] = np.dot(gui.center, gui.axes[:, :2]) - size / 2 

61 bbox[2:] = bbox[:2] + size 

62 extra['rotation'] = gui.axes 

63 extra['show_unit_cell'] = gui.window['toggle-show-unit-cell'] 

64 extra['bbox'] = bbox 

65 colors = gui.get_colors(rgb=True) 

66 extra['colors'] = [rgb for rgb, visible 

67 in zip(colors, gui.images.visible) 

68 if visible] 

69 remove_hidden = True 

70 

71 images = [gui.images.get_atoms(i, remove_hidden=remove_hidden) 

72 for i in range(*index.indices(len(gui.images)))] 

73 

74 if len(images) > 1 and io.single: 

75 # We want to write multiple images, but the file format does not 

76 # support it. The solution is to write multiple files, inserting 

77 # a number in the file name before the suffix. 

78 j = filename.rfind('.') 

79 filename = filename[:j] + '{0:05d}' + filename[j:] 

80 for i, atoms in enumerate(images): 

81 write(filename.format(i), atoms, **extra) 

82 else: 

83 try: 

84 write(filename, images, **extra) 

85 except Exception as err: 

86 from ase.gui.ui import showerror 

87 showerror(_('Error'), err) 

88 raise