]> SALOME platform Git repositories - modules/paravis.git/blob - src/PV_SWIG/no_wrap/pvsimple.py
Salome HOME
simplification
[modules/paravis.git] / src / PV_SWIG / no_wrap / pvsimple.py
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 r""" This module is a direct forward to the initial 'simple' module of ParaView.
22 On top of that it also establishes a connection to a valid PVServer whose address
23 is provided by the PARAVIS engine.
24 """
25
26 def __my_log(msg):
27     print "[PARAVIS] %s" % msg
28
29 def __getFromGUI():
30     """ Identify if we are running inside SALOME's embedded console.
31     """
32     fromGUI = False
33     try:
34       import salome
35       fromGUI = salome.fromEmbeddedConsole
36     except AttributeError:
37       pass
38     return fromGUI
39
40 def InitParaViewForGUI():
41     """
42     If the import is made from SALOME embedded console, the ParaView application needs to 
43     be instanciated to avoid a future crash. 
44     """
45     if __getFromGUI():
46       __my_log("Initializing ParaView main elements, please be patient ...")
47       import SalomePyQt
48       sgPyQt = SalomePyQt.SalomePyQt()
49       sgPyQt.createView("ParaView")
50       # Now let the GUI main loop process the initialization event posted above
51       sgPyQt.processEvents()  
52       __my_log("ParaView initialized.")
53
54 ## The below has to called BEFORE importing paraview!!! This is crazy, but it has to be.
55 InitParaViewForGUI()  
56 del InitParaViewForGUI
57
58 import paraview
59 import paravis
60
61 # Forward namespace of simple into current pvsimple:
62 from paraview import simple
63 for name in dir(simple):
64   if not name.startswith("__"):
65     globals()[name] = getattr(simple, name)
66 del simple
67
68 def SalomeConnectToPVServer():
69     """
70     Automatically connect to the right PVServer when not ("inside SALOME GUI" and "already connected").
71     """
72     __my_log("Connecting to PVServer ...")
73     server_url = ""
74     try:
75         isGUIConnected = paravis.myParavisEngine.GetGUIConnected()
76         if isGUIConnected and __getFromGUI():
77             __my_log("Importing pvsimple from GUI and already connected. Won't reconnect.")
78             return
79         server_url = paravis.myParavisEngine.FindOrStartPVServer(0)
80         # Extract host and port from URL:
81         a = server_url.split(':')
82         b = a[1].split('//')
83         host, port = b[-1], int(a[-1])
84         Connect(host, port)
85         __my_log("Connected to %s!" % server_url)
86         if __getFromGUI():
87             paravis.myParavisEngine.SetGUIConnected(True)
88     except Exception as e:
89         __my_log("*******************************************")
90         __my_log("** Could not connect to a running PVServer!")
91         __my_log("*******************************************")
92         raise e
93     pass
94     
95 SalomeConnectToPVServer()