| [97ebf8] | 1 | /*
 | 
|---|
 | 2 |  * FillWithMoleculeAction.cpp
 | 
|---|
 | 3 |  *
 | 
|---|
 | 4 |  *  Created on: May 10, 2010
 | 
|---|
 | 5 |  *      Author: heber
 | 
|---|
 | 6 |  */
 | 
|---|
 | 7 | 
 | 
|---|
| [bf3817] | 8 | // include config.h
 | 
|---|
 | 9 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 10 | #include <config.h>
 | 
|---|
 | 11 | #endif
 | 
|---|
 | 12 | 
 | 
|---|
| [112b09] | 13 | #include "Helpers/MemDebug.hpp"
 | 
|---|
 | 14 | 
 | 
|---|
| [97ebf8] | 15 | #include "Actions/MoleculeAction/FillWithMoleculeAction.hpp"
 | 
|---|
| [0430e3] | 16 | #include "Actions/ActionRegistry.hpp"
 | 
|---|
| [1a3c26] | 17 | #include "atom.hpp"
 | 
|---|
 | 18 | #include "bondgraph.hpp"
 | 
|---|
 | 19 | #include "boundary.hpp"
 | 
|---|
 | 20 | #include "config.hpp"
 | 
|---|
 | 21 | #include "molecule.hpp"
 | 
|---|
| [952f38] | 22 | #include "Helpers/Verbose.hpp"
 | 
|---|
| [1a3c26] | 23 | #include "World.hpp"
 | 
|---|
 | 24 | 
 | 
|---|
| [97ebf8] | 25 | 
 | 
|---|
 | 26 | #include <iostream>
 | 
|---|
 | 27 | #include <string>
 | 
|---|
 | 28 | 
 | 
|---|
 | 29 | using namespace std;
 | 
|---|
 | 30 | 
 | 
|---|
 | 31 | #include "UIElements/UIFactory.hpp"
 | 
|---|
 | 32 | #include "UIElements/Dialog.hpp"
 | 
|---|
| [861874] | 33 | #include "Actions/ValueStorage.hpp"
 | 
|---|
| [97ebf8] | 34 | 
 | 
|---|
 | 35 | /****** MoleculeFillWithMoleculeAction *****/
 | 
|---|
 | 36 | 
 | 
|---|
 | 37 | // memento to remember the state when undoing
 | 
|---|
 | 38 | 
 | 
|---|
 | 39 | //class MoleculeFillWithMoleculeState : public ActionState {
 | 
|---|
 | 40 | //public:
 | 
|---|
 | 41 | //  MoleculeFillWithMoleculeState(molecule* _mol,std::string _lastName) :
 | 
|---|
 | 42 | //    mol(_mol),
 | 
|---|
 | 43 | //    lastName(_lastName)
 | 
|---|
 | 44 | //  {}
 | 
|---|
 | 45 | //  molecule* mol;
 | 
|---|
 | 46 | //  std::string lastName;
 | 
|---|
 | 47 | //};
 | 
|---|
 | 48 | 
 | 
|---|
 | 49 | const char MoleculeFillWithMoleculeAction::NAME[] = "fill-molecule";
 | 
|---|
 | 50 | 
 | 
|---|
 | 51 | MoleculeFillWithMoleculeAction::MoleculeFillWithMoleculeAction() :
 | 
|---|
 | 52 |   Action(NAME)
 | 
|---|
 | 53 | {}
 | 
|---|
 | 54 | 
 | 
|---|
 | 55 | MoleculeFillWithMoleculeAction::~MoleculeFillWithMoleculeAction()
 | 
|---|
 | 56 | {}
 | 
|---|
 | 57 | 
 | 
|---|
| [1a3c26] | 58 | void MoleculeFillWithMolecule(std::string &fillername, Vector &distances, Vector &lengths, double MaxDistance, bool DoRotate) {
 | 
|---|
 | 59 |   ValueStorage::getInstance().setCurrentValue(MoleculeFillWithMoleculeAction::NAME, fillername);
 | 
|---|
 | 60 |   ValueStorage::getInstance().setCurrentValue("distances", distances);
 | 
|---|
 | 61 |   ValueStorage::getInstance().setCurrentValue("lengths", lengths);
 | 
|---|
 | 62 |   ValueStorage::getInstance().setCurrentValue("DoRotate", MaxDistance);
 | 
|---|
 | 63 |   ValueStorage::getInstance().setCurrentValue("MaxDistance", DoRotate);
 | 
|---|
 | 64 |   ActionRegistry::getInstance().getActionByName(MoleculeFillWithMoleculeAction::NAME)->call(Action::NonInteractive);
 | 
|---|
 | 65 | };
 | 
|---|
 | 66 | 
 | 
|---|
| [047878] | 67 | Dialog* MoleculeFillWithMoleculeAction::fillDialog(Dialog *dialog) {
 | 
|---|
 | 68 |   ASSERT(dialog,"No Dialog given when filling action dialog");
 | 
|---|
| [a001b7] | 69 | 
 | 
|---|
 | 70 |   dialog->queryString(NAME, ValueStorage::getInstance().getDescription(NAME));
 | 
|---|
 | 71 |   dialog->queryVector("distances", false, ValueStorage::getInstance().getDescription("distances"));
 | 
|---|
 | 72 |   dialog->queryVector("lengths", false, ValueStorage::getInstance().getDescription("lengths"));
 | 
|---|
 | 73 |   dialog->queryBoolean("DoRotate", ValueStorage::getInstance().getDescription("DoRotate"));
 | 
|---|
 | 74 |   dialog->queryDouble("MaxDistance", ValueStorage::getInstance().getDescription("MaxDistance"));
 | 
|---|
 | 75 | 
 | 
|---|
 | 76 |   return dialog;
 | 
|---|
 | 77 | }
 | 
|---|
 | 78 | 
 | 
|---|
| [97ebf8] | 79 | Action::state_ptr MoleculeFillWithMoleculeAction::performCall() {
 | 
|---|
 | 80 |   string filename;
 | 
|---|
 | 81 |   Vector distances;
 | 
|---|
 | 82 |   Vector lengths;
 | 
|---|
 | 83 |   double MaxDistance = -1.;
 | 
|---|
 | 84 |   bool DoRotate = false;
 | 
|---|
 | 85 | 
 | 
|---|
| [a001b7] | 86 |   ValueStorage::getInstance().queryCurrentValue(NAME, filename);
 | 
|---|
 | 87 |   ValueStorage::getInstance().queryCurrentValue("distances", distances);
 | 
|---|
 | 88 |   ValueStorage::getInstance().queryCurrentValue("lengths", lengths);
 | 
|---|
 | 89 |   ValueStorage::getInstance().queryCurrentValue("DoRotate", DoRotate);
 | 
|---|
 | 90 |   ValueStorage::getInstance().queryCurrentValue("MaxDistance", MaxDistance);
 | 
|---|
 | 91 | 
 | 
|---|
 | 92 |   DoLog(1) && (Log() << Verbose(1) << "Filling Box with water molecules, lengths(" << lengths[0] << "," << lengths[1] << "," << lengths[2] << "), distances (" << distances[0] << "," << distances[1] << "," << distances[2] << "), MaxDistance " << MaxDistance << ", DoRotate " << DoRotate << "." << endl);
 | 
|---|
 | 93 |   // construct water molecule
 | 
|---|
 | 94 |   molecule *filler = World::getInstance().createMolecule();
 | 
|---|
 | 95 |   if (!filler->AddXYZFile(filename)) {
 | 
|---|
 | 96 |     DoeLog(0) && (eLog()<< Verbose(0) << "Could not parse filler molecule from " << filename << "." << endl);
 | 
|---|
 | 97 |   }
 | 
|---|
 | 98 |   filler->SetNameFromFilename(filename.c_str());
 | 
|---|
 | 99 |   molecule *Filling = NULL;
 | 
|---|
| [9907e7] | 100 | //    atom *first = NULL, *second = NULL, *third = NULL;
 | 
|---|
 | 101 | //    first = World::getInstance().createAtom();
 | 
|---|
 | 102 | //    first->type = World::getInstance().getPeriode()->FindElement(1);
 | 
|---|
 | 103 | //    first->x = Vector(0.441, -0.143, 0.);
 | 
|---|
 | 104 | //    filler->AddAtom(first);
 | 
|---|
 | 105 | //    second = World::getInstance().createAtom();
 | 
|---|
 | 106 | //    second->type = World::getInstance().getPeriode()->FindElement(1);
 | 
|---|
 | 107 | //    second->x = Vector(-0.464, 1.137, 0.0);
 | 
|---|
 | 108 | //    filler->AddAtom(second);
 | 
|---|
 | 109 | //    third = World::getInstance().createAtom();
 | 
|---|
 | 110 | //    third->type = World::getInstance().getPeriode()->FindElement(8);
 | 
|---|
 | 111 | //    third->x = Vector(-0.464, 0.177, 0.);
 | 
|---|
 | 112 | //    filler->AddAtom(third);
 | 
|---|
 | 113 | //    filler->AddBond(first, third, 1);
 | 
|---|
 | 114 | //    filler->AddBond(second, third, 1);
 | 
|---|
| [a001b7] | 115 |   World::getInstance().getConfig()->BG->ConstructBondGraph(filler);
 | 
|---|
| [9907e7] | 116 | //    filler->SetNameFromFilename("water");
 | 
|---|
| [a001b7] | 117 |   // call routine
 | 
|---|
 | 118 |   double distance[NDIM];
 | 
|---|
 | 119 |   for (int i=0;i<NDIM;i++)
 | 
|---|
 | 120 |     distance[i] = distances[i];
 | 
|---|
 | 121 |   Filling = FillBoxWithMolecule(World::getInstance().getMolecules(), filler, *(World::getInstance().getConfig()), MaxDistance, distance, lengths[0], lengths[1], lengths[2], DoRotate);
 | 
|---|
 | 122 |   if (Filling != NULL) {
 | 
|---|
 | 123 |     Filling->ActiveFlag = false;
 | 
|---|
 | 124 |     World::getInstance().getMolecules()->insert(Filling);
 | 
|---|
| [97ebf8] | 125 |   }
 | 
|---|
| [a001b7] | 126 |   for (molecule::iterator iter = filler->begin(); !filler->empty(); iter = filler->begin()) {
 | 
|---|
 | 127 |     atom *Walker = *iter;
 | 
|---|
 | 128 |     filler->erase(iter);
 | 
|---|
 | 129 |     World::getInstance().destroyAtom(Walker);
 | 
|---|
 | 130 |   }
 | 
|---|
 | 131 |   World::getInstance().destroyMolecule(filler);
 | 
|---|
 | 132 | 
 | 
|---|
 | 133 |   return Action::success;
 | 
|---|
| [97ebf8] | 134 | }
 | 
|---|
 | 135 | 
 | 
|---|
 | 136 | Action::state_ptr MoleculeFillWithMoleculeAction::performUndo(Action::state_ptr _state) {
 | 
|---|
 | 137 | //  MoleculeFillWithMoleculeState *state = assert_cast<MoleculeFillWithMoleculeState*>(_state.get());
 | 
|---|
 | 138 | 
 | 
|---|
 | 139 | //  string newName = state->mol->getName();
 | 
|---|
 | 140 | //  state->mol->setName(state->lastName);
 | 
|---|
 | 141 | 
 | 
|---|
 | 142 |   return Action::failure;
 | 
|---|
 | 143 | }
 | 
|---|
 | 144 | 
 | 
|---|
 | 145 | Action::state_ptr MoleculeFillWithMoleculeAction::performRedo(Action::state_ptr _state){
 | 
|---|
 | 146 |   // Undo and redo have to do the same for this action
 | 
|---|
 | 147 |   return performUndo(_state);
 | 
|---|
 | 148 | }
 | 
|---|
 | 149 | 
 | 
|---|
 | 150 | bool MoleculeFillWithMoleculeAction::canUndo() {
 | 
|---|
 | 151 |   return false;
 | 
|---|
 | 152 | }
 | 
|---|
 | 153 | 
 | 
|---|
 | 154 | bool MoleculeFillWithMoleculeAction::shouldUndo() {
 | 
|---|
 | 155 |   return false;
 | 
|---|
 | 156 | }
 | 
|---|
 | 157 | 
 | 
|---|
 | 158 | const string MoleculeFillWithMoleculeAction::getName() {
 | 
|---|
 | 159 |   return NAME;
 | 
|---|
 | 160 | }
 | 
|---|