Salome HOME
[Huge] Introducing MEDCalc
[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
24 XmedConsoleDriver::XmedConsoleDriver(SalomeApp_Application * application) {
25
26   bool forcePythonConsole = true;
27   _pyConsole = application->pythonConsole(forcePythonConsole);
28   if ( !_pyConsole ) {
29     const char * msg = "The python console can't be obtained from the SALOME application";
30     throw SALOME_Exception(msg);
31   }
32   _importXmedDone = false;
33 }
34
35 /*!
36  * This function sends instructions to the python console to setup the
37  * environment for med operations (in particular, it imports the medcalc
38  * commands). Even if the function is called twice, then the
39  * instructions are sent once.
40  */
41 void XmedConsoleDriver::setup() {
42
43   if ( !_importXmedDone ) {
44     QStringList commands;
45     /*
46     // First import xmed to initialize the main objects, in particular
47     // the corba pointer to the event listener.
48     commands+="import xmed";
49     // Set the globals dictionnary so that the fields tools work properly.
50     commands+="xmed.setConsoleGlobals(globals())";
51     // Import the tools required for field operations
52     commands+="from xmed import load, get, put, dup, ls, la, save, view, doc, wipe, remove, clean";
53     // A last one to clear the console screen
54     //commands+="wipe";
55     */
56 #ifndef DISABLE_PVVIEWER
57     // start PVServer and show render view
58     commands+="import pvsimple as pvs";
59     commands+="";
60     //commands+="pvs.ShowParaviewView()";
61 #endif
62     commands += "import medcalc";
63     commands += "medcalc.medconsole.setConsoleGlobals(globals())";
64     commands += "import MEDCALC";
65     commands += "";
66     commands += "from medcalc.medconsole import saveWorkspace, cleanWorkspace";
67     commands += "from medcalc.medconsole import putInWorkspace, removeFromWorkspace";
68     commands += "from medcalc.medconsole import accessField";
69     commands += "from medcalc.medconsole import getEnvironment, ls, la";
70     commands += "";
71     this->exec(commands);
72     _importXmedDone = true;
73   }
74 }
75
76 /*!
77  * This function sends the specified list of commands to the console.
78  */
79 void XmedConsoleDriver::exec(const QStringList& commands) {
80   QStringListIterator it(commands);
81   while (it.hasNext()) {
82     _pyConsole->exec(it.next());
83   }
84   this->_history.append(commands);
85 }