Salome HOME
medcalc and commands history
[modules/med.git] / src / MEDOP / tui / xmedpy / __init__.py
1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2011-2015  CEA/DEN, EDF R&D
3 #
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
8 #
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20 # Author : Guillaume Boulant (EDF)
21
22 #
23 # ===============================================================
24 # This functions are to be used to notify the USER of some events
25 # arising on the field operation. It is NOT to be used for logging
26 # purpose
27 # ===============================================================
28 #
29 def inf(msg):
30     print "INF: "+str(msg)
31
32 def wrn(msg):
33     print "WRN: "+str(msg)
34
35 def err(msg):
36     print "ERR: "+str(msg)
37
38 def dbg(msg):
39     print "DBG: "+str(msg)
40
41 #
42 # ===============================================================
43 # Initializing some CORBA stuff
44 # ===============================================================
45 #
46
47 # Remember SALOME definitions:
48 # ---------------------------
49 #
50 # componentName = Name of the component (a library lib<componentName>Engine
51 # should exist with a C function named <componentName>Engine_factory, and the
52 # component should be registered in the catalog MEDCatalog.xml).
53 #
54 # corbaModule = Name of the corba module that contains the IDL
55 # specifications of the component (name as defined in the idl file)
56 #
57 # containerType = Name of the container factory
58 #
59 componentName = "MEDOPFactory"
60 corbaModule   = "MEDOP"
61 containerType = "FactoryServer"
62
63 import salome
64 if salome.lcc is None:
65     salome.salome_init()
66 __import__(corbaModule)
67 factory = salome.lcc.FindOrLoadComponent(containerType,componentName)
68 # The factory is not the main CORBA component of the SALOME module MED
69 # (i.e. the engine associated to the active study), but the CORBA
70 # entry point for MED fields operations (i.e. a CORBA component
71 # reachable throught the LifeCycleCORBA). This entry point is used to
72 # get the other SALOME CORBA components required for MED field
73 # operations, in particular the dataManager and the calculator
74
75 #
76 # ===============================================================
77 # Initializing the MED components required for MED fields operations
78 # ===============================================================
79 #
80 dataManager = factory.getDataManager()
81 calculator  = factory.getCalculator()
82
83 #
84 # ===============================================================
85 # Initializing the notification system (events from components to GUI)
86 # ===============================================================
87 #
88
89 # The MEDEventListener is created in the GUI (WorkspaceController) and
90 # a reference is transmitted to the python context throw its IOR. The
91 # transmission consists in initializing the variable below from the
92 # GUI (who knows the IOR) when importing xmed in the python console
93 # (see WorkspaceController)
94 eventListener = None
95 import SALOME
96 def connectEventListener():
97     global eventListener
98     try:
99         eventListenerIOR = dataManager.getEventListenerIOR()
100         eventListener = salome.orb.string_to_object(eventListenerIOR)
101     except SALOME.SALOME_Exception, e:
102         wrn("The event listener is not running yet")
103         msg ="When you'll have loaded the MED GUI, "
104         msg+="call explicitely \"xmed.connectEventListener()\" "
105         msg+="to connect the GUI event listener"
106         inf(msg)
107         eventListener = None
108     except Exception, e:
109         err("An unknown error occurs. Check if this ior=%s is valid."%eventListenerIOR)
110
111 def eventListenerIsRunning():
112     global eventListener
113     if eventListener is not None:
114         return True
115
116     # Try to define the event listener
117     connectEventListener()
118     if eventListener is None:
119         # it definitly does not work
120         wrn("the GUI is not loaded yet and will not be notified of the modification")
121         return False
122
123     return True
124
125 # One can try to connect to the eventListener, but it will fail if the
126 # MED GUI is not loaded. That does not matter, because the event
127 # listener is used only to notify the GUI of some event. If the GUI is
128 # not loaded, there is no use of notification.
129 connectEventListener()
130
131 #
132 # ===============================================================
133 # Automatic import of some elements of the package
134 # ===============================================================
135 #
136 from fieldtools import load, get, put, dup, ls, la, save, view, remove, clean
137 from fieldguide import doc
138 from cmdtools import cls, wipe
139
140 #
141 # ===============================================================
142 # Some helper functions to deal with the fields and meshes
143 # ===============================================================
144 #
145 import MEDCoupling
146 __mapTypeOfFieldLabel={
147     MEDCoupling.ON_CELLS:"ON_CELLS",
148     MEDCoupling.ON_NODES:"ON_NODES",
149     MEDCoupling.ON_GAUSS_PT:"ON_GAUSS_PT",
150     MEDCoupling.ON_GAUSS_NE:"ON_GAUSS_NE"}
151
152 def typeOfFieldLabel(typeOfField):
153     # A field name could identify several MEDCoupling fields, that
154     # differ by their spatial discretization on the mesh (values on
155     # cells, values on nodes, ...). This spatial discretization is
156     # specified (at least) by the TypeOfField that is an integer value
157     # in this list:
158     # 0 = ON_CELLS
159     # 1 = ON_NODES
160     # 2 = ON_GAUSS_PT
161     # 3 = ON_GAUSS_NE
162     try:
163         return __mapTypeOfFieldLabel[typeOfField]
164     except IndexError, e:
165         return "UNCKNOWN"
166
167 #-----
168 # This function is to be called from the working python console to
169 # specify the globals() dictionnary (used in fieldtools for stat analysis)
170 #
171 # >>> xmed.setConsoleGlobals(globals())
172 #
173 import fieldtools
174 def setConsoleGlobals(pyConsoleGlobals):
175     fieldtools.pyConsoleGlobals = pyConsoleGlobals
176
177 #
178 # ===================================================================
179 # unit test functions
180 # ===================================================================
181 #
182 def TEST_typeOfFieldLabel():
183     print typeOfFieldLabel(0)
184     print typeOfFieldLabel(5)
185
186 # ===================================================================
187 if __name__ == "__main__":
188     TEST_typeOfFieldLabel()