1 # Copyright (C) 2012-2013 EDF
3 # This file is part of SALOME HYDRO module.
5 # SALOME HYDRO module is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # SALOME HYDRO module is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with SALOME HYDRO module. If not, see <http://www.gnu.org/licenses/>.
21 from salome.kernel.studyedit import getStudyEditor
22 from salome.kernel.parametric import study_exchange_vars
24 import HYDROSOLVER_ORB
27 MODULE_NAME = "HYDROSOLVER"
29 COMPONENT_NAME = "HydroSolver"
30 COMPONENT_ICON = "HYDRO_small.png"
32 MASCARET_FILE_TYPE = "MASCARET_EFICAS_FILE"
33 MASCARET_ICON = "case1d.png"
34 MASCARET_CASE_TYPE_ID = 1
36 VARS_ICON = "icon_variables.png"
41 TELEMAC2D_FILE_TYPE = "TELEMAC2D_EFICAS_FILE"
42 TELEMAC2D_ICON = "case2d.png"
43 TELEMAC2D_CASE_TYPE_ID = 3
45 PYTEL_FILE_TYPE = "PYTEL_EFICAS_FILE"
46 PYTEL_ICON = "case_pytel.png"
47 PYTEL_CASE_TYPE_ID = 5
49 # Dictionary used to map Eficas parameters to Mascaret file types
50 mascaretFileTypeDict = {"FICHIER_ABAQUES" : "abaques",
51 "FICHIER_DICO" : "dico",
52 "FICHIER_CASIER" : "casier",
53 "FICHIER_GEOMETRIE" : "geo",
54 "FICHIER_LOI" : "loi",
55 "FICHIER_MOT_CLE" : "cas",
56 "LISTING" : "listing",
57 "FICHIER_DAMOCLE" : "damocle",
59 "LISTING_CASIER" : "listing_casier",
60 "LISTING_LIAISON" : "listing_liaison",
61 "RESULTAT_CASIER" : "res_casier",
62 "RESULTAT_LIAISON" : "res_liaison"
65 def jdc_to_dict(jdc, command_list):
67 This tricky function transforms a JdC with a single command into a
68 dictionary that can be used more easily from a Python context (thanks to
69 M. Courtois and G. Boulant).
72 for command in command_list:
73 context[command] = _args_to_dict
74 exec "parameters = " + jdc.strip() in context
75 return context['parameters']
77 def _args_to_dict(**kwargs):
80 def get_jdc_dict_var_as_tuple(jdc_dict, varname):
81 if not jdc_dict.has_key(varname):
83 elif not isinstance(jdc_dict[varname], types.TupleType):
84 return (jdc_dict[varname],)
86 return jdc_dict[varname]
88 class HydroStudyEditor:
90 This class provides utility methods to edit the component "Hydro" in
91 the study. The parameter `studyId` defines the ID of the study to edit. If
92 it is :const:`None`, the edited study will be the current study.
94 def __init__(self, studyId = None):
95 self.editor = getStudyEditor(studyId)
98 def find_or_create_hydro_component(self):
100 Find the component "Hydro" or create it if none is found
101 :return: the SComponent found or created.
103 if self.hydroComp is None:
104 self.hydroComp = self.editor.findOrCreateComponent(MODULE_NAME,
107 return self.hydroComp
109 def find_or_create_mascaret_case(self, filePath):
110 self.find_or_create_hydro_component()
111 itemName = os.path.splitext(os.path.basename(filePath))[0]
112 sobj = self.editor.findOrCreateItem(self.hydroComp,
114 fileType = MASCARET_FILE_TYPE,
116 icon = MASCARET_ICON,
117 comment = str(filePath),
118 typeId = MASCARET_CASE_TYPE_ID)
120 def find_or_create_telemac2d_case(self, filePath):
121 self.find_or_create_hydro_component()
122 itemName = os.path.splitext(os.path.basename(filePath))[0]
123 sobj = self.editor.findOrCreateItem(self.hydroComp,
125 fileType = TELEMAC2D_FILE_TYPE,
127 icon = TELEMAC2D_ICON,
128 comment = str(filePath),
129 typeId = TELEMAC2D_CASE_TYPE_ID)
131 def generate_study_script(self, filePath):
133 Generating a python script from the eficas info
135 # Create "Python script" item
136 from TelApy.api.generate_study import generate_study_script
137 with open(filePath) as jdcfile:
139 params = jdc_to_dict(jdc, ["TELEMAC2D", "_F"])
141 # Generation script string
142 python_script = generate_study_script(params)
144 # Computing name of the file (same as filePath)
145 file_dir, filename = os.path.split(filePath)
146 root, sfx = os.path.splitext(filename)
147 python_file = os.path.join(file_dir,root+".py")
150 with open(python_file,'w') as pfile:
151 pfile.write(python_script)
153 def generate_study_yacs(self, filePath):
155 Generating a yacs file from the eficas info
157 # Create "Python script" item
158 from TelApy.api.generate_study import generate_study_yacs
159 with open(filePath) as jdcfile:
161 params = jdc_to_dict(jdc, ["TELEMAC2D", "_F"])
163 # Generation YACS scheme
164 yacs_scheme = generate_study_yacs(params)
166 # Computing name of the file (same as filePath)
167 file_dir, filename = os.path.split(filePath)
168 root, sfx = os.path.splitext(filename)
169 yacs_file = os.path.join(file_dir,root+".xml")
172 yacs_scheme.saveSchema(yacs_file)
174 def find_or_create_pytel_case(self, filePath):
175 self.find_or_create_hydro_component()
176 itemName = os.path.splitext(os.path.basename(filePath))[0]
177 sobj = self.editor.findOrCreateItem(self.hydroComp,
179 fileType = PYTEL_FILE_TYPE,
182 comment = str(filePath),
183 typeId = PYTEL_CASE_TYPE_ID)