Salome HOME
Switch from runSession to salome shell in the salome shell plugin of GUI.
[modules/gui.git] / src / PVViewer / PVViewer_EngineWrapper.cxx
1 // Copyright (C) 2010-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: Adrien Bruneton (CEA)
20
21 #include "PVViewer_EngineWrapper.h"
22 #include <Utils_SALOME_Exception.hxx>
23
24 #include <SUIT_Session.h>
25 #include <PyInterp_Utils.h>
26
27 class PVViewer_EngineWrapper::Private
28 {
29 public:
30   PyObjWrapper pvserverEngine;
31 };
32
33 PVViewer_EngineWrapper * PVViewer_EngineWrapper::instance = NULL;
34
35 PVViewer_EngineWrapper * PVViewer_EngineWrapper::GetInstance()
36 {
37   if (!instance)
38     instance = new PVViewer_EngineWrapper();
39   return instance;
40 }
41
42 PVViewer_EngineWrapper::PVViewer_EngineWrapper()
43 {
44   myData = new Private;
45   PyLockWrapper lock;
46   const char* code = "import PVSERVER_utils as pa;__enginePVSERVER=pa.getEngine()";
47   int ret = PyRun_SimpleString(const_cast<char*>(code));
48
49   if (ret == -1)
50     throw SALOME_Exception("Unable to retrieve PVSERVER engine!");
51
52   // Now get the reference to __engine and save the pointer.
53   PyObject* main_module = PyImport_AddModule((char*)"__main__");
54   PyObject* global_dict = PyModule_GetDict(main_module);
55   PyObjWrapper tmp(PyDict_GetItemString(global_dict, "__enginePVSERVER"));
56   myData->pvserverEngine = tmp;
57 }
58
59 PVViewer_EngineWrapper::~PVViewer_EngineWrapper()
60 {
61   delete myData;
62 }
63
64 bool PVViewer_EngineWrapper::GetGUIConnected()
65 {
66   PyLockWrapper lock;
67   PyObjWrapper obj(PyObject_CallMethod(myData->pvserverEngine, (char*)("GetGUIConnected"), NULL));
68   if (!obj)
69     {
70       PyErr_Print();
71       throw SALOME_Exception("Unable to invoke PVSERVER engine!");
72     }
73   return PyObject_IsTrue(obj);
74 }
75
76 void PVViewer_EngineWrapper::SetGUIConnected(bool isConnected)
77 {
78   PyLockWrapper lock;
79
80   PyObjWrapper obj(PyObject_CallMethod(myData->pvserverEngine, (char*)("SetGUIConnected"),
81                                        (char *)"i", (int)isConnected ) );
82   if (!obj)
83     {
84       PyErr_Print();
85       throw SALOME_Exception("Unable to invoke PVSERVER service!");
86     }
87 }
88
89 std::string PVViewer_EngineWrapper::FindOrStartPVServer(int port)
90 {
91   PyLockWrapper lock;
92   PyObjWrapper obj(PyObject_CallMethod(myData->pvserverEngine, (char*)("FindOrStartPVServer"),
93                                          (char *)"i", port ) );
94   if (!obj)
95     {
96       PyErr_Print();
97       throw SALOME_Exception("Unable to invoke PVSERVER service!");
98     }
99   char * s = PyString_AsString(obj);
100
101   return std::string(s);
102 }
103
104 void PVViewer_EngineWrapper::PutPythonTraceStringToEngine(const char * str)
105 {
106   PyLockWrapper lock;
107   PyObjWrapper obj(PyObject_CallMethod(myData->pvserverEngine, (char*)("PutPythonTraceStringToEngine"),
108                                        (char *)"s",  str) );
109   if (!obj)
110     {
111       PyErr_Print();
112       throw SALOME_Exception("Unable to invoke PVSERVER service!");
113     }
114 }