Salome HOME
29bc88dd04a39092dc851a7285ee7380c740247f
[modules/med.git] / src / MEDOP / gui / XmedConsoleDriver.cxx
1 // Copyright (C) 2011  CEA/DEN, EDF R&D, OPEN CASCADE
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.
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   _pyConsole = application->pythonConsole();
26   if ( !_pyConsole ) {
27     const char * msg = "The python console can't be obtained from the SALOME application";
28     throw SALOME_Exception(msg);
29   }
30   _importXmedDone = false;
31 }
32
33 /*!
34  * This function sends instructions to the python console to setup the
35  * environment for med operations (in particular, it imports the medop
36  * commands). Even if the function is called twice, then the
37  * instructions are sent once.
38  */
39 void XmedConsoleDriver::setup() {
40   
41   if ( !_importXmedDone ) {
42     QStringList commands;
43     // First import xmed to initialize the main objects, in particular
44     // the corba pointer to the event listener.
45     commands+="import xmed";
46     // Set the globals dictionnary so that the fields tools work properly.
47     commands+="xmed.setConsoleGlobals(globals())";
48     // Import the tools required for field operations
49     commands+="from xmed import load, get, put, dup, ls, la, save, view, doc, wipe";
50     // A last one to clear the console screen
51     //commands+="wipe";
52
53     this->exec(commands);
54     _importXmedDone = true;
55   }
56 }
57
58 /*!
59  * This function sends the specified list of commands to the console.
60  */
61 void XmedConsoleDriver::exec(QStringList commands) {
62   QStringListIterator it(commands);
63   while (it.hasNext()) {
64     _pyConsole->exec(it.next());
65   }
66 }