Coverage for ase/gui/add.py: 59.79%

97 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-11-27 15:21 +0000

1import os 

2 

3import numpy as np 

4 

5import ase.gui.ui as ui 

6from ase import Atoms 

7from ase.data import atomic_numbers, chemical_symbols 

8from ase.gui.i18n import _ 

9 

10current_selection_string = _('(selection)') 

11 

12 

13class AddAtoms: 

14 def __init__(self, gui): 

15 self.gui = gui 

16 win = self.win = ui.Window(_('Add atoms')) 

17 win.add(_('Specify chemical symbol, formula, or filename.')) 

18 

19 def choose_file(): 

20 chooser = ui.ASEFileChooser(self.win.win) 

21 filename = chooser.go() 

22 if filename is None: # No file selected 

23 return 

24 

25 self.combobox.value = filename 

26 

27 # Load the file immediately, so we can warn now in case of error 

28 self.readfile(filename, format=chooser.format) 

29 

30 if self.gui.images.selected.any(): 

31 default = current_selection_string 

32 else: 

33 default = 'H2' 

34 

35 self._filename = None 

36 self._atoms_from_file = None 

37 

38 from ase.collections import g2 

39 

40 labels = sorted(name for name in g2.names if len(g2[name]) > 1) 

41 values = labels 

42 

43 combobox = ui.ComboBox(labels, values) 

44 win.add( 

45 [ 

46 _('Add:'), 

47 combobox, 

48 ui.Button(_('File ...'), callback=choose_file), 

49 ] 

50 ) 

51 ui.bind_enter(combobox.widget, lambda e: self.add()) 

52 

53 combobox.value = default 

54 self.combobox = combobox 

55 

56 spinners = [ 

57 ui.SpinBox(0.0, -1e3, 1e3, 0.1, rounding=2, width=3) 

58 for __ in range(3) 

59 ] 

60 

61 win.add([_('Coordinates:')] + spinners) 

62 self.spinners = spinners 

63 win.add( 

64 _( 

65 'Coordinates are relative to the center of the selection, ' 

66 'if any, else absolute.' 

67 ) 

68 ) 

69 self.picky = ui.CheckButton(_('Check positions'), True) 

70 win.add([ui.Button(_('Add'), self.add), self.picky]) 

71 self.focus() 

72 

73 def readfile(self, filename, format=None): 

74 if filename == self._filename: 

75 # We have this file already 

76 return self._atoms_from_file 

77 

78 from ase.io import read 

79 

80 try: 

81 atoms = read(filename) 

82 except Exception as err: 

83 ui.show_io_error(filename, err) 

84 atoms = None 

85 filename = None 

86 

87 # Cache selected Atoms/filename (or None) for future calls 

88 self._atoms_from_file = atoms 

89 self._filename = filename 

90 return atoms 

91 

92 def get_atoms(self): 

93 # Get the text, whether it's a combobox item or not 

94 val = self.combobox.widget.get() 

95 

96 if val == current_selection_string: 

97 selection = self.gui.images.selected.copy() 

98 if selection.any(): 

99 atoms = self.gui.atoms.copy() 

100 return atoms[selection[: len(self.gui.atoms)]] 

101 

102 if val in atomic_numbers: # Note: This means val is a symbol! 

103 return Atoms(val) 

104 

105 if val.isdigit() and int(val) < len(chemical_symbols): 

106 return Atoms(numbers=[int(val)]) 

107 

108 from ase.collections import g2 

109 

110 if val in g2.names: 

111 return g2[val] 

112 

113 if os.path.exists(val): 

114 return self.readfile(val) # May show UI error 

115 

116 ui.showerror( 

117 _('Cannot add atoms'), 

118 _('{} is neither atom, molecule, nor file').format(val), 

119 ) 

120 

121 return None 

122 

123 def getcoords(self): 

124 addcoords = np.array([spinner.value for spinner in self.spinners]) 

125 

126 pos = self.gui.atoms.positions 

127 if self.gui.images.selected[: len(pos)].any(): 

128 pos = pos[self.gui.images.selected[: len(pos)]] 

129 center = pos.mean(0) 

130 addcoords += center 

131 

132 return addcoords 

133 

134 def focus(self): 

135 self.combobox.widget.focus_set() 

136 

137 def add(self): 

138 newatoms = self.get_atoms() 

139 if newatoms is None: # Error dialog was shown 

140 return 

141 

142 newcenter = self.getcoords() 

143 

144 # Not newatoms.center() because we want the same centering method 

145 # used for adding atoms relative to selections (mean). 

146 previous_center = newatoms.positions.mean(0) 

147 newatoms.positions += newcenter - previous_center 

148 

149 atoms = self.gui.atoms 

150 if len(atoms) and self.picky.value: 

151 from ase.geometry import get_distances 

152 

153 _disps, dists = get_distances(atoms.positions, newatoms.positions) 

154 mindist = dists.min() 

155 if mindist < 0.5: 

156 ui.showerror( 

157 _('Bad positions'), 

158 _( 

159 'Atom would be less than 0.5 Å from ' 

160 'an existing atom. To override, ' 

161 'uncheck the check positions option.' 

162 ), 

163 ) 

164 return 

165 

166 self.gui.add_atoms_and_select(newatoms) 

167 self.gui.update_history()