Salome HOME
52ad6bf4a84080c1688c1275ef9b74bd259c7fb5
[modules/med.git] / src / MEDCalc / gui / XmedConsoleDriver.cxx
1 // Copyright (C) 2011-2020  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 "MEDModule.hxx"
22 #include "XmedConsoleDriver.hxx"
23 #include "Utils_SALOME_Exception.hxx"
24 #include "MEDCommandsHistoryManager_i.hxx"
25 #include "MEDFactoryClient.hxx"
26 #include CORBA_CLIENT_HEADER(MED_Gen)
27 #include CORBA_CLIENT_HEADER(MEDCommandsHistoryManager)
28 #include <iostream>
29
30 XmedConsoleDriver::XmedConsoleDriver(MEDModule* salomeModule)
31 {
32   _salomeModule = salomeModule;
33   bool forcePythonConsole = true;
34   _pyConsole = _salomeModule->getApp()->pythonConsole(forcePythonConsole);
35   if ( !_pyConsole ) {
36     const char * msg = "The python console can't be obtained from the SALOME application";
37     throw SALOME_Exception(msg);
38   }
39   _importXmedDone = false;
40 }
41
42 /*!
43  * This function sends instructions to the python console to setup the
44  * environment for med operations (in particular, it imports the medcalc
45  * commands). Even if the function is called twice, then the
46  * instructions are sent once.
47  */
48 void XmedConsoleDriver::setup() {
49
50   if ( !_importXmedDone ) {
51     QStringList commands;
52 #ifndef DISABLE_PVVIEWER
53     // start PVServer and show render view
54     commands+="import pvsimple as pvs";
55     commands+="";
56     //commands+="pvs.ShowParaviewView()";
57 #endif
58     commands += "import medcalc";
59     commands += "medcalc.medconsole.setConsoleGlobals(globals())";
60     commands += "import MEDCALC";
61     commands += "";
62     commands += "from medcalc.medconsole import saveWorkspace, cleanWorkspace";
63     commands += "from medcalc.medconsole import putInWorkspace, removeFromWorkspace";
64     commands += "from medcalc.medconsole import accessField, view";
65     commands += "from medcalc.medconsole import getEnvironment, ls, la";
66     commands += "";
67     this->exec(commands);
68     _importXmedDone = true;
69   }
70 }
71
72 /*!
73  * This function sends the specified list of commands to the console.
74  */
75 void XmedConsoleDriver::exec(const QStringList& commands) {
76   MEDCALC::MEDCommandsHistoryManager_ptr history = MEDFactoryClient::getCommandsHistoryManager();
77
78   QStringListIterator it(commands);
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 }