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