Salome HOME
Merge Python 3 porting.
[modules/paravis.git] / src / PV_SWIG / pvsimple.py
1 # Copyright (C) 2010-2016  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 PVSERVER engine.
24 """
25
26 __DEBUG = 0   # increase if you want more verbosity
27
28 def __my_log(msg):
29     if __DEBUG:
30       print("[PARAVIS] %s" % msg)
31
32 def __getFromGUI():
33     """ Identify if we are running inside SALOME's embedded interpreter.
34     @return a value strictly greater than 0 if we are in SALOME's embedded interpreter
35     @return 2 if we are in Salome embedded Python console.
36     """
37     import salome_iapp
38     ret = 0
39     if salome_iapp.IN_SALOME_GUI:
40       ret += 1
41     try:
42       if __IN_SALOME_GUI_CONSOLE:  # only defined if we are in SALOME's embedded console (not only GUI)
43         ret += 1
44     except NameError:
45       pass
46     return ret
47
48 def ShowParaviewView():
49     """
50     If the import is made from SALOME embedded console, the ParaView application needs to
51     be instanciated to avoid a future crash.
52     """
53     if __getFromGUI():
54       __my_log("Initializing ParaView main elements, please be patient ...")
55       import SalomePyQt
56       sgPyQt = SalomePyQt.SalomePyQt()
57       viewIds = sgPyQt.findViews("ParaView")
58       if len(viewIds):
59         sgPyQt.setViewVisible(viewIds[0], True)
60         sgPyQt.activateView(viewIds[0])
61       else:
62         sgPyQt.createView("ParaView")
63       # Now let the GUI main loop process the initialization event posted above
64       sgPyQt.processEvents()
65       __my_log("ParaView initialized.")
66
67 ## The below has to called BEFORE importing paraview!!! This is crazy, but it has to be.
68 ShowParaviewView()
69
70 import paraview
71 import pvserver
72 from paraview import simple
73
74 def SalomeConnectToPVServer():
75     """
76     Automatically connect to the right PVServer when not ("inside SALOME GUI" and "already connected").
77     """
78     __my_log("Connecting to PVServer ...")
79     server_url = ""
80     try:
81         isGUIConnected = pvserver.myPVServerService.GetGUIConnected()
82         if isGUIConnected and __getFromGUI():
83             __my_log("Importing pvsimple from GUI and already connected. Won't reconnect.")
84             return
85         server_url = pvserver.myPVServerService.FindOrStartPVServer(0)
86         # Extract host and port from URL:
87         a = server_url.split(':')
88         b = a[1].split('//')
89         host, port = b[-1], int(a[-1])
90         simple.Connect(host, port)
91         __my_log("Connected to %s!" % server_url)
92         if __getFromGUI():
93             pvserver.myPVServerService.SetGUIConnected(True)
94     except Exception as e:
95         __my_log("*******************************************")
96         __my_log("** Could not connect to a running PVServer!")
97         __my_log("*******************************************")
98         raise e
99     pass
100
101 if __getFromGUI() < 1:
102     # Only if not in GUI (otherwise the createView will do the connection)
103     SalomeConnectToPVServer()
104 del SalomeConnectToPVServer
105
106 # Forward namespace of simple into current pvsimple:
107 for name in dir(simple):
108   if not name.startswith("__"):
109     globals()[name] = getattr(simple, name)
110 del simple