Salome HOME
Moved PVViewer code from PARAVIS into GUI.
[modules/gui.git] / src / PVViewer / PVViewer_EngineWrapper.cxx
1 // Copyright (C) 2010-2014  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: Adrien Bruneton (CEA)
20
21 #include "PVViewer_EngineWrapper.h"
22 #include <Utils_SALOME_Exception.hxx>
23
24 PVViewer_EngineWrapper * PVViewer_EngineWrapper::instance = NULL;
25
26 PVViewer_EngineWrapper::PVViewer_EngineWrapper() :
27     paravisEngine(NULL)
28 {
29 //  const char * cmd = "import PARAVIS_utils;e=";
30   PyLockWrapper lock;
31   const char* code = "import PARAVIS_utils as pa;__enginePARAVIS=pa.getEngine()";
32   int ret = PyRun_SimpleString(const_cast<char*>(code));
33
34   if (ret == -1)
35     throw SALOME_Exception("Unable to retrieve PARAVIS engine!");
36
37   // Now get the reference to __engine and save the pointer.
38   PyObject* main_module = PyImport_AddModule((char*)"__main__");
39   PyObject* global_dict = PyModule_GetDict(main_module);
40   PyObjWrapper tmp(PyDict_GetItemString(global_dict, "__enginePARAVIS"));
41   paravisEngine = tmp;
42 }
43
44
45 PVViewer_EngineWrapper * PVViewer_EngineWrapper::GetInstance()
46 {
47   if (!instance)
48     instance = new PVViewer_EngineWrapper();
49   return instance;
50 }
51
52 bool PVViewer_EngineWrapper::GetGUIConnected()
53 {
54   PyLockWrapper lock;
55   PyObjWrapper obj(PyObject_CallMethod(paravisEngine, (char*)("GetGUIConnected"), NULL));
56   if (!obj)
57     {
58       PyErr_Print();
59       throw SALOME_Exception("Unable to invoke PARAVIS engine!");
60     }
61   return PyObject_IsTrue(obj);
62 }
63
64 void PVViewer_EngineWrapper::SetGUIConnected(bool isConnected)
65 {
66   PyLockWrapper lock;
67
68   PyObjWrapper obj(PyObject_CallMethod(paravisEngine, (char*)("SetGUIConnected"),
69                                        (char *)"i", (int)isConnected ) );
70   if (!obj)
71     {
72       PyErr_Print();
73       throw SALOME_Exception("Unable to invoke PARAVIS engine!");
74     }
75 }
76
77 std::string PVViewer_EngineWrapper::FindOrStartPVServer(int port)
78 {
79   PyLockWrapper lock;
80
81   PyObjWrapper obj(PyObject_CallMethod(paravisEngine, (char*)("FindOrStartPVServer"),
82                                          (char *)"i", port ) );
83   if (!obj)
84     {
85       PyErr_Print();
86       throw SALOME_Exception("Unable to invoke PARAVIS engine!");
87     }
88   char * s = PyString_AsString(obj);
89
90   return std::string(s);
91 }