Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Actions/BondAction/BondRemoveAction.cpp

    r26b4d62 r88afc9  
    5757ActionState::ptr BondRemoveAction::performCall() {
    5858  // check preconditions
    59   if (World::getInstance().countSelectedAtoms() != 2) {
    60     STATUS("Exactly two atoms must be selected for BondAction Remove.");
     59  World& world = World::getInstance();
     60  if (world.countSelectedAtoms() <= 1) {
     61    STATUS("At least two atoms must be selected for BondAction Remove.");
    6162    return Action::failure;
    6263  }
    63   const std::vector<atom *> selected_atoms = World::getInstance().getSelectedAtoms();
    64   if (!selected_atoms[0]->IsBondedTo(WorldTime::getTime(), selected_atoms[1])) {
    65     STATUS("There is no bond in between the two selected atoms.");
     64
     65  bondPairIds_t bondPairIds;
     66  for (World::AtomSelectionConstIterator firstiter = world.beginAtomSelection();
     67      firstiter != world.endAtomSelection(); ++firstiter) {
     68    for (World::AtomSelectionConstIterator seconditer = firstiter;
     69        seconditer != world.endAtomSelection(); ++seconditer) {
     70      if (firstiter == seconditer)
     71        continue;
     72      if ((firstiter->second)->IsBondedTo(WorldTime::getTime(), seconditer->second))
     73        bondPairIds.push_back(
     74            std::make_pair((firstiter->second)->getId(), (seconditer->second)->getId()));
     75    }
     76  }
     77  if (bondPairIds.empty()) {
     78    STATUS("No bonds are present.");
    6679    return Action::failure;
    6780  }
    6881
    6982  // create undo
    70   BondRemoveState *UndoState = new BondRemoveState(selected_atoms[0]->getId(), selected_atoms[1]->getId(), params);
     83  BondRemoveState *UndoState = new BondRemoveState(bondPairIds, params);
    7184
    7285  // execute action
    73   selected_atoms[0]->removeBond(WorldTime::getTime(), selected_atoms[1]);
    74   ASSERT( !selected_atoms[0]->IsBondedTo(WorldTime::getTime(), selected_atoms[1]),
    75       "BondRemoveAction::performCall() - removing bond in between "
    76       +toString(*selected_atoms[0])+" and "+toString(*selected_atoms[1])+" failed.");
     86  for (bondPairIds_t::const_iterator iter = bondPairIds.begin();
     87      iter != bondPairIds.end(); ++iter) {
     88    atom *firstatom = world.getAtom(AtomById(iter->first));
     89    atom *secondatom = world.getAtom(AtomById(iter->second));
     90    ASSERT((firstatom != NULL) && (secondatom != NULL),
     91        "BondAddAction::performCall() - at least one of the ids "
     92        +toString(iter->first)+" or "+toString(iter->second)+" is not present.");
     93    firstatom->removeBond(WorldTime::getTime(), secondatom);
     94    ASSERT( !firstatom->IsBondedTo(WorldTime::getTime(), secondatom),
     95      "BondAddAction::performCall() - adding bond in between "
     96      +toString(*firstatom)+" and "+toString(*secondatom)+" failed.");
     97  }
    7798
    7899  return ActionState::ptr(UndoState);
     
    82103  BondRemoveState *state = assert_cast<BondRemoveState*>(_state.get());
    83104
    84   // check whether bond already existed
    85   atom * const first = World::getInstance().getAtom(AtomById(state->firstId));
    86   atom * const second = World::getInstance().getAtom(AtomById(state->secondId));
    87   ASSERT((first != NULL) && (second != NULL),
    88       "BondRemoveAction::performUndo() - at least one of the ids "
    89       +toString(state->firstId)+" or "+toString(state->secondId)+" is not present.");
    90   if (!first->IsBondedTo(WorldTime::getTime(), second)) {
    91     first->addBond(WorldTime::getTime(), second);
    92   } else {
    93     ELOG(2, "There is already a bond in between "+toString(state->firstId)
    94         +" and "+toString(state->secondId)+".");
     105  World& world = World::getInstance();
     106  for (bondPairIds_t::const_iterator iter = state->bondPairIds.begin();
     107      iter != state->bondPairIds.end(); ++iter) {
     108    atom * const firstatom = world.getAtom(AtomById(iter->first));
     109    atom * const secondatom = world.getAtom(AtomById(iter->second));
     110    ASSERT((firstatom != NULL) && (secondatom != NULL),
     111        "BondAddAction::performCall() - at least one of the ids "
     112        +toString(iter->first)+" or "+toString(iter->second)+" is not present.");
     113    if (!firstatom->IsBondedTo(WorldTime::getTime(), secondatom)) {
     114      firstatom->addBond(WorldTime::getTime(), secondatom);
     115    } else {
     116      ELOG(2, "There is already a bond in between "+toString(iter->first)
     117          +" and "+toString(iter->second)+".");
     118    }
    95119  }
    96120
     
    101125  BondRemoveState *state = assert_cast<BondRemoveState*>(_state.get());
    102126
    103   // check whether bond already existed
    104   atom * const first = World::getInstance().getAtom(AtomById(state->firstId));
    105   atom * const second = World::getInstance().getAtom(AtomById(state->secondId));
    106   ASSERT((first != NULL) && (second != NULL),
    107       "BondRemoveAction::performRedo() - at least one of the ids "
    108       +toString(state->firstId)+" or "+toString(state->secondId)+" is not present.");
    109   if (first->IsBondedTo(WorldTime::getTime(), second)) {
    110     first->removeBond(WorldTime::getTime(), second);
    111   } else {
    112     ELOG(2, "There is no bond in between "+toString(state->firstId)
    113         +" and "+toString(state->secondId)+".");
     127  World& world = World::getInstance();
     128  for (bondPairIds_t::const_iterator iter = state->bondPairIds.begin();
     129      iter != state->bondPairIds.end(); ++iter) {
     130    atom *firstatom = world.getAtom(AtomById(iter->first));
     131    atom *secondatom = world.getAtom(AtomById(iter->second));
     132    ASSERT((firstatom != NULL) && (secondatom != NULL),
     133        "BondAddAction::performCall() - at least one of the ids "
     134        +toString(iter->first)+" or "+toString(iter->second)+" is not present.");
     135    if (firstatom->IsBondedTo(WorldTime::getTime(), secondatom)) {
     136      firstatom->removeBond(WorldTime::getTime(), secondatom);
     137    } else {
     138      ELOG(2, "There is no bond in between "+toString(iter->first)
     139          +" and "+toString(iter->second)+".");
     140    }
    114141  }
    115142
Note: See TracChangeset for help on using the changeset viewer.