Salome HOME
Various bug fixes for the PVViewer.
[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 //#include <SalomeApp_Application.h>
25 #include <SUIT_Session.h>
26
27 PVViewer_EngineWrapper * PVViewer_EngineWrapper::instance = NULL;
28
29 PVViewer_EngineWrapper * PVViewer_EngineWrapper::GetInstance()
30 {
31   if (!instance)
32     instance = new PVViewer_EngineWrapper();
33   return instance;
34 }
35
36 //static SalomeApp_Application* getApplication()
37 //{
38 //  if ( SUIT_Session::session() )
39 //    return dynamic_cast<SalomeApp_Application*>( SUIT_Session::session()->activeApplication() );
40 //  return 0;
41 //}
42 //
43 //PVViewer_EngineWrapper::PVViewer_EngineWrapper()
44 //// : paravisEngine(NULL)
45 //{
46 //  _component = getApplication()->lcc()->FindOrLoad_Component( "FactoryServer", "PARAVIS" );
47 //}
48 //
49 //bool PVViewer_EngineWrapper::GetGUIConnected()
50 //{
51 //  CORBA::Request_var req = _component->_request("GetGUIConnected");
52 //  req->set_return_type(CORBA::_tc_boolean);
53 //  req->invoke();
54 //  CORBA::Exception *exc =req->env()->exception();
55 //  if( exc )
56 //    throw SALOME_Exception("Unable to invoke PARAVIS engine!");
57 //  CORBA::Any & ret = req->return_value();
58 //  CORBA::Boolean bo;
59 //
60 //  if (ret >>= bo)
61 //    return bool(bo);
62 //  else
63 //    throw SALOME_Exception("Unable to convert engine result!");
64 //}
65 //
66 //void PVViewer_EngineWrapper::SetGUIConnected(bool isConnected)
67 //{
68 //  CORBA::Request_var req = _component->_request("SetGUIConnected");
69 //  CORBA::Boolean arg = isConnected;
70 //  req->add_in_arg() <<= arg;
71 //  req->set_return_type(CORBA::_tc_void);
72 //  req->invoke();
73 //  CORBA::Exception *exc =req->env()->exception();
74 //  if( exc )
75 //    throw SALOME_Exception("Unable to invoke PARAVIS engine!");
76 //}
77 //
78 //std::string PVViewer_EngineWrapper::FindOrStartPVServer(int port)
79 //{
80 //  CORBA::Request_var req = _component->_request("FindOrStartPVServer");
81 //  CORBA::Long arg = port;
82 //  req->add_in_arg() <<= arg;
83 //  req->set_return_type(CORBA::_tc_string);
84 //  req->invoke();
85 //  CORBA::Exception *exc =req->env()->exception();
86 //  if( exc )
87 //    throw SALOME_Exception("Unable to invoke PARAVIS engine!");
88 //
89 //  const char* ret;
90 //  if(req->return_value() >>= ret)
91 //    return std::string(ret);
92 //  else
93 //    throw SALOME_Exception("Unable to convert engine result!");
94 //}
95
96 PVViewer_EngineWrapper::PVViewer_EngineWrapper() :
97     paravisEngine(NULL)
98 {
99 //  const char * cmd = "import PARAVIS_utils;e=";
100   PyLockWrapper lock;
101   const char* code = "import PARAVIS_utils as pa;__enginePARAVIS=pa.getEngine()";
102   int ret = PyRun_SimpleString(const_cast<char*>(code));
103
104   if (ret == -1)
105     throw SALOME_Exception("Unable to retrieve PARAVIS engine!");
106
107   // Now get the reference to __engine and save the pointer.
108   PyObject* main_module = PyImport_AddModule((char*)"__main__");
109   PyObject* global_dict = PyModule_GetDict(main_module);
110   PyObjWrapper tmp(PyDict_GetItemString(global_dict, "__enginePARAVIS"));
111   paravisEngine = tmp;
112 }
113
114
115
116 bool PVViewer_EngineWrapper::GetGUIConnected()
117 {
118   PyLockWrapper lock;
119   PyObjWrapper obj(PyObject_CallMethod(paravisEngine, (char*)("GetGUIConnected"), NULL));
120   if (!obj)
121     {
122       PyErr_Print();
123       throw SALOME_Exception("Unable to invoke PARAVIS engine!");
124     }
125   return PyObject_IsTrue(obj);
126 }
127
128 void PVViewer_EngineWrapper::SetGUIConnected(bool isConnected)
129 {
130   PyLockWrapper lock;
131
132   PyObjWrapper obj(PyObject_CallMethod(paravisEngine, (char*)("SetGUIConnected"),
133                                        (char *)"i", (int)isConnected ) );
134   if (!obj)
135     {
136       PyErr_Print();
137       throw SALOME_Exception("Unable to invoke PARAVIS engine!");
138     }
139 }
140
141 std::string PVViewer_EngineWrapper::FindOrStartPVServer(int port)
142 {
143   PyLockWrapper lock;
144
145   PyObjWrapper obj(PyObject_CallMethod(paravisEngine, (char*)("FindOrStartPVServer"),
146                                          (char *)"i", port ) );
147   if (!obj)
148     {
149       PyErr_Print();
150       throw SALOME_Exception("Unable to invoke PARAVIS engine!");
151     }
152   char * s = PyString_AsString(obj);
153
154   return std::string(s);
155 }