def get_atoms(self): # Get the text, whether it's a combobox item or not val = self.combobox.widget.get() if val == current_selection_string: selection = self.gui.images.selected.copy() if selection.any(): atoms = self.gui.atoms.copy() return atoms[selection[:len(self.gui.atoms)]] if val in atomic_numbers: # Note: This means val is a symbol! return Atoms(val) if val.isdigit() and int(val) < len(chemical_symbols): return Atoms(numbers=[int(val)]) from ase.collections import g2 if val in g2.names: return g2[val] if os.path.exists(val): return self.readfile(val) # May show UI error ui.showerror(_('Cannot add atoms'), _('{} is neither atom, molecule, nor file') .format(val)) return None
def add(self): newatoms = self.get_atoms() if newatoms is None: # Error dialog was shown return newcenter = self.getcoords() # Not newatoms.center() because we want the same centering method # used for adding atoms relative to selections (mean). previous_center = newatoms.positions.mean(0) newatoms.positions += newcenter - previous_center atoms = self.gui.atoms if len(atoms) and self.picky.value: from ase.geometry import get_distances disps, dists = get_distances(atoms.positions, newatoms.positions) mindist = dists.min() if mindist < 0.5: ui.showerror( _('Bad positions'), _('Atom would be less than 0.5 Å from ' 'an existing atom. To override, ' 'uncheck the check positions option.')) return self.gui.add_atoms_and_select(newatoms)
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, basestring): 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
def get_atoms(self): val = self.entry.value if val in atomic_numbers: # Note: This means val is a symbol! return Atoms(val) if val.isdigit() and int(val) < len(chemical_symbols): return Atoms(numbers=[int(val)]) from ase.collections import g2 if val in g2.names: return g2[val] if os.path.exists(val): return self.readfile(val) # May show UI error ui.showerror(_('Cannot add atoms'), _('{} is neither atom, molecule, nor file').format(val)) return None
def add(self): newatoms = self.get_atoms() if newatoms is None: # Error dialog was shown return newcenter = self.getcoords() # Not newatoms.center() because we want the same centering method # used for adding atoms relative to selections (mean). previous_center = newatoms.positions.mean(0) newatoms.positions += newcenter - previous_center atoms = self.gui.atoms if len(atoms) and self.picky.value: from ase.geometry import get_distances disps, dists = get_distances(atoms.positions, newatoms.positions) mindist = dists.min() if mindist < 0.5: ui.showerror( _('Bad positions'), _('Atom would be less than 0.5 Å from ' 'an existing atom. To override, ' 'uncheck the check positions option.')) return atoms += newatoms if len(atoms) > self.gui.images.maxnatoms: self.gui.images.initialize(list(self.gui.images), self.gui.images.filenames) self.gui.images.selected[:] = False # 'selected' array may be longer than current atoms self.gui.images.selected[len(atoms) - len(newatoms):len(atoms)] = True self.gui.set_frame() self.gui.draw()