Salome HOME
MERGE stage 1: keep doc/dev and src/MEDCalc/doc
[modules/med.git] / src / MEDCalc / gui / XmedConsoleDriver.cxx
1 // Copyright (C) 2011-2015  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // Author : Guillaume Boulant (EDF)
20
21 #include "XmedConsoleDriver.hxx"
22 #include "Utils_SALOME_Exception.hxx"
23 #include "MEDModule.hxx"
24 #include "MEDCommandsHistoryManager_i.hxx"
25 #include "MEDFactoryClient.hxx"
26 #include CORBA_CLIENT_HEADER(MED_Gen)
27 #include CORBA_CLIENT_HEADER(MEDCommandsHistoryManager)
28
29 XmedConsoleDriver::XmedConsoleDriver(MEDModule* salomeModule)
30 {
31   _salomeModule = salomeModule;
32   bool forcePythonConsole = true;
33   _pyConsole = _salomeModule->getApp()->pythonConsole(forcePythonConsole);
34   if ( !_pyConsole ) {
35     const char * msg = "The python console can't be obtained from the SALOME application";
36     throw SALOME_Exception(msg);
37   }
38   _importXmedDone = false;
39 }
40
41 /*!
42  * This function sends instructions to the python console to setup the
43  * environment for med operations (in particular, it imports the medcalc
44  * commands). Even if the function is called twice, then the
45  * instructions are sent once.
46  */
47 void XmedConsoleDriver::setup() {
48
49   if ( !_importXmedDone ) {
50     QStringList commands;
51 #ifndef DISABLE_PVVIEWER
52     // start PVServer and show render view
53     commands+="import pvsimple as pvs";
54     commands+="";
55     //commands+="pvs.ShowParaviewView()";
56 #endif
57     commands += "import medcalc";
58     commands += "medcalc.medconsole.setConsoleGlobals(globals())";
59     commands += "import MEDCALC";
60     commands += "";
61     commands += "from medcalc.medconsole import saveWorkspace, cleanWorkspace";
62     commands += "from medcalc.medconsole import putInWorkspace, removeFromWorkspace";
63     commands += "from medcalc.medconsole import accessField";
64     commands += "from medcalc.medconsole import getEnvironment, ls, la";
65     commands += "";
66     this->exec(commands);
67     _importXmedDone = true;
68   }
69 }
70
71 /*!
72  * This function sends the specified list of commands to the console.
73  */
74 void XmedConsoleDriver::exec(const QStringList& commands) {
75   MEDCALC::MEDCommandsHistoryManager_ptr history = MEDFactoryClient::getCommandsHistoryManager();
76
77   QStringListIterator it(commands);
78   int i = 0;
79   while (it.hasNext()) {
80     const QString& command = it.next();
81     _pyConsole->exec(command);
82     // store command in history
83     history->addCommand(command.toStdString().c_str());
84   }
85 }