1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2011-2015 CEA/DEN, EDF R&D
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.
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.
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
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 # Author : Guillaume Boulant (EDF)
23 # ===================================================================
24 # The functions of this module must be imported as global functions in
25 # the SALOME python console. It provides the console context with Tool
26 # functions to manipulate field throw there fieldproxy.
27 # ===================================================================
31 from xmed.fieldproxy import FieldProxy, newFieldProxy
33 # ===================================================================
34 # Operations on fields
38 This function returns a duplicate of the specified field.
39 It's equivalent to the call '>>> copy = f.dup()'.
42 clone = aFieldProxy.dup()
44 print "ERR: the argument can't be duplicated as a field object"
48 # ===================================================================
49 # Get list a field with information using the commands ls and/or la
52 # the pyConsoleGlobals variable should holds the globals() dictionnary of
53 # the python console context
56 def status(local=True,remote=False):
58 This function return the status of the medop context, i.e. the
59 list of fields defined in this python session.
63 dvars = pyConsoleGlobals
65 xmed.wrn("The stat function required the specification of the python context")
66 xmed.inf("Type this command \"import xmed; xmed.setConsoleGlobals(globals())")
68 status="========= Fields used in the current context ===\n"
69 for varkey in dvars.keys():
71 if isinstance(var, FieldProxy):
72 status+="%s \t(id=%s, name=%s)\n"%(varkey,var.id,var.fieldname)
76 status+="\n========= Fields available in the data manager ===\n"
77 fieldHandlerList = xmed.dataManager.getFieldHandlerList()
78 for fieldHandler in fieldHandlerList:
79 status+="id=%s\tname\t= %s\n\tmesh\t= %s\n\t(it,dt)\t= (%s,%s)\n\tsource\t= %s\n"%(
81 fieldHandler.fieldname,
82 fieldHandler.meshname,
83 fieldHandler.iteration,
88 if len(fieldHandlerList) > 0:
89 status+="(use 'f=get(id)' to get a field in the current context)"
93 # For simpler typing, one can create a python command for status
94 # (avoid to type "print status()")
95 class ListFields(object):
97 A stat object displays the status of the med operating context, i.e. the
98 list of fields defined in this python session.
100 def __init__(self,all=False):
103 # all = True means that the local metadata (fieldproxy) and the
104 # remote metadata on the engine (MEDCouplingFieldDouble) are
105 # displayed by the stat command. Otherwise, only the local
106 # metadata are displayed.
109 return status(self.__local, self.__remote)
111 # Creating the commands list (ls) and list all (la)
112 ls=ListFields(all=False)
113 la=ListFields(all=True)
115 # ===================================================================
116 # Remove variable from console
117 from xmed.fieldproxy import notifyGui_remove
118 def remove(aFieldProxy):
119 dvars = pyConsoleGlobals
122 for varkey, var in dvars.items():
123 if isinstance(var, FieldProxy) and var.id == aFieldProxy.id:
124 exec("del %s"%varkey) in pyConsoleGlobals
125 notifyGui_remove(aFieldProxy.id)
129 from xmed.fieldproxy import notifyGui_clean
131 dvars = pyConsoleGlobals
135 for varkey, var in dvars.items():
136 if isinstance(var, FieldProxy):
137 all_keys.append("%s"%varkey)
138 if len(all_keys) > 0:
139 exec "del "+",".join(all_keys) in pyConsoleGlobals
143 # ===================================================================
144 # Field Data Management
145 from xmed import properties
146 filepath = properties.testFilePath
148 def load(medFileName=filepath):
150 This function indicates that we want to use the fields from the
151 specified med file. The fields meta-data are loaded in the engine
152 part of the module. To get a fieldproxy on a field, call the get
153 function with the id of the required field. To display the whole
154 list of fields loaded in the engine, type 'ls' (la verbose).
156 xmed.dataManager.addDatasource(filepath)
157 print status(local=False,remote=True)
158 from xmed.fieldproxy import notifyGui_addsource
159 notifyGui_addsource(filename)
161 def get(fieldHandlerId):
163 This return a field proxy on the field identified by the specified
166 return newFieldProxy(fieldHandlerId)
168 from xmed.fieldproxy import notifyGui_add
169 def put(aFieldProxy):
171 This function puts a reference to this field in the GUI data
172 model. When a field is referenced in the GUI data model, then it
173 belongs to the workspace. When the workspace is saved, all the
174 field that belongs to the workspace are saved.
176 xmed.dataManager.markAsPersistent(aFieldProxy.id, True)
177 notifyGui_add(aFieldProxy.id)
182 Dump your job in a med file. Only the fields marked as persistent
183 are saved in the specified file.
186 xmed.dataManager.savePersistentFields(filename)
187 except SALOME.SALOME_Exception, ex:
188 xmed.err(ex.details.text)
190 # ===================================================================
191 # Field Data visualization
193 VIEWER_PARAVIS = "PARAVIS"
194 VIEWER_DEFAULT = VIEWER_PARAVIS
198 def view_using_paravis(aFieldProxy):
199 temp = tempfile.NamedTemporaryFile(prefix="medop_viewer", suffix='.med', delete=False)
201 # __GBO__ TO BE IMPROVED: we used a tmp file in this first step of
202 # development, but we should used at last a MEDCoupling corba
203 # object (see how to use the stuff in PARAVIS/src/Plugins)
204 xmed.dataManager.saveFields(temp.name, [aFieldProxy.id])
206 from xmed.driver_pvis import pvis_scalarmap
207 pvis_scalarmap(temp.name,
208 aFieldProxy.meshname,
209 aFieldProxy.fieldname,
211 aFieldProxy.iteration)
215 def view_using_visu(aFieldProxy):
217 raise Exception("view_using_visu: No more used")
219 VIEWER_TMP_FILE = "/tmp/medop_viewer.med"
221 # __GBO__ TO BE IMPROVED: we used a tmp file in this first step of
222 # development, but we should used at last a MEDCoupling corba
223 # object (see how to use the stuff in PARAVIS/src/Plugins)
224 xmed.dataManager.saveFields(VIEWER_TMP_FILE, [aFieldProxy.id])
226 # __GBO__: WARN due to a specific feature of VISU, when only one
227 # field timestamps exists in the med file, we have to specify an
228 # iteration number of 1, whatever the iteration value is in the
230 iteration = 1 # __GBO__ for bug VISU
232 # iteration = aFieldProxy.iteration
234 from xmed.driver_visu import visu_scalarmap
235 result = visu_scalarmap(VIEWER_TMP_FILE,
236 aFieldProxy.meshname,
237 aFieldProxy.fieldname,
241 xmed.err("the field can't be displayed")
243 def view(aFieldProxy, using=VIEWER_DEFAULT):
245 This displays a 3D view of the field using VISU or PARAVIS,
246 depending on the configuration. This view is for quick visual
247 control of a field and is not intended to be parametrizable. For
248 custom display, you should import the fields in VISU or PARAVIS
249 and continue the job in one of this SALOME module.
251 if using == VIEWER_PARAVIS:
252 view_using_paravis(aFieldProxy)
254 view_using_visu(aFieldProxy)