1 # Copyright (C) 2012-2019 CEA/DEN, EDF R&D
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.
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.
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
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 dataManager = medcalc.medcorba.factory.getDataManager()
25 # the pyConsoleGlobals variable should hold the globals() dictionary of
26 # the python console context
27 pyConsoleGlobals = None
30 # This function is to be called from the working python console to
31 # specify the globals() dictionary (used in fieldtools for stat analysis)
33 # >>> medcalc.setConsoleGlobals(globals())
35 def setConsoleGlobals(pyconsole_globals):
36 global pyConsoleGlobals
37 pyConsoleGlobals = pyconsole_globals
41 def saveWorkspace(filename):
43 Dump your job in a med file. Only the fields marked as persistent
44 are saved in the specified file.
47 dataManager.savePersistentFields(filename)
48 except SALOME.SALOME_Exception as ex:
49 medcalc.err(ex.details.text)
53 from .medevents import notifyGui_cleanWorkspace
55 dvars = pyConsoleGlobals
59 for varkey, var in list(dvars.items()):
60 if isinstance(var, medcalc.FieldProxy):
61 all_keys.append("%s"%varkey)
63 exec("del "+",".join(all_keys), pyConsoleGlobals)
64 notifyGui_cleanWorkspace()
67 # Remove variable from console
68 from .medevents import notifyGui_removeFromWorkspace
69 def removeFromWorkspace(fieldProxy):
70 dvars = pyConsoleGlobals
73 for varkey, var in list(dvars.items()):
74 if isinstance(var, medcalc.FieldProxy) and var.id == fieldProxy.id:
75 exec(("del %s"%varkey), pyConsoleGlobals)
76 notifyGui_removeFromWorkspace(fieldProxy.id)
79 # Get list a field with information using the commands ls and/or la
80 def getEnvironment(local=True, remote=False):
82 This function return the status of the medcalc context, i.e. the
83 list of fields defined in this python session.
87 dvars = pyConsoleGlobals
89 medcalc.wrn("The stat function required the specification of the python context")
90 medcalc.inf("Type this command \"import medcalc; medcalc.setConsoleGlobals(globals())")
92 status="========= Fields used in the current context ===\n"
93 for varkey in list(dvars.keys()):
95 if isinstance(var, medcalc.FieldProxy):
96 status+="%s \t(id=%s, name=%s)\n"%(varkey,var.id,var.fieldname)
100 status+="\n========= Fields available in the data manager ===\n"
101 fieldHandlerList = dataManager.getFieldHandlerList()
102 for fieldHandler in fieldHandlerList:
103 status+="id=%s\tname\t= %s\n\tmesh\t= %s\n\t(it,dt)\t= (%s,%s)\n\tsource\t= %s\n"%(
105 fieldHandler.fieldname,
106 fieldHandler.meshname,
107 fieldHandler.iteration,
110 status+="---------\n"
112 if len(fieldHandlerList) > 0:
113 status+="(use 'f=accessField(id)' to get a field in the current context)"
118 # For simpler typing, one can create a python command for status
119 # (avoid to type "print(getEnvironment())")
120 class ListFields(object):
122 A stat object displays the status of the med operating context, i.e. the
123 list of fields defined in this python session.
125 def __init__(self, all=False):
128 # all = True means that the local metadata (fieldproxy) and the
129 # remote metadata on the engine (MEDCouplingFieldDouble) are
130 # displayed by the stat command. Otherwise, only the local
131 # metadata are displayed.
134 return getEnvironment(self.__local, self.__remote)
137 # Creating the commands list (ls) and list all (la)
138 ls = ListFields(all=False)
139 la = ListFields(all=True)
142 # Add variable to workspace
143 from .medevents import notifyGui_putInWorkspace
144 def putInWorkspace(fieldProxy):
146 This function puts a reference to this field in the GUI data
147 model. When a field is referenced in the GUI data model, then it
148 belongs to the workspace. When the workspace is saved, all the
149 field that belongs to the workspace are saved.
151 dataManager.markAsPersistent(fieldProxy.id, True)
152 notifyGui_putInWorkspace(fieldProxy.id)
155 def accessField(fieldHandlerId):
157 This return a field proxy on the field identified by the specified
160 return medcalc.newFieldProxy(fieldHandlerId)