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 VARS_ICON = "icon_variables.png"
34 TELMA_FILE_TYPE = "TELMA_EFICAS_FILE"
35 TELMA_ICON = "telma_file.png"
38 STUDY_FILE_TYPE = "STUDY_EFICAS_FILE"
39 STUDY_ICON = "study_file.png"
42 PARAM_STUDY_FILE_TYPE = "PARAM_STUDY_EFICAS_FILE"
43 PARAM_STUDY_ICON = "param_study_file.png"
44 PARAM_STUDY_TYPE_ID = 3
46 def jdc_to_dict(jdc, command_list):
48 This tricky function transforms a JdC with a single command into a
49 dictionary that can be used more easily from a Python context (thanks to
50 M. Courtois and G. Boulant).
53 for command in command_list:
54 context[command] = _args_to_dict
55 exec("parameters = " + jdc.strip(), context)
56 return context['parameters']
58 def _args_to_dict(**kwargs):
61 def get_jdc_dict_var_as_tuple(jdc_dict, varname):
62 if varname not in jdc_dict:
64 elif not isinstance(jdc_dict[varname], tuple):
65 return (jdc_dict[varname],)
67 return jdc_dict[varname]
69 class HydroStudyEditor:
71 This class provides utility methods to edit the component "Hydro" in
72 the study. The parameter `studyId` defines the ID of the study to edit. If
73 it is :const:`None`, the edited study will be the current study.
75 def __init__(self, studyId = None):
76 self.editor = getStudyEditor()
79 def find_or_create_hydro_component(self):
81 Find the component "Hydro" or create it if none is found
82 :return: the SComponent found or created.
84 if self.hydroComp is None:
85 self.hydroComp = self.editor.findOrCreateComponent(MODULE_NAME,
90 def find_or_create_telma(self, filePath):
91 self.find_or_create_hydro_component()
92 itemName = os.path.splitext(os.path.basename(filePath))[0]
93 sobj = self.editor.findOrCreateItem(self.hydroComp,
95 fileType=TELMA_FILE_TYPE,
98 comment=str(filePath),
101 def find_or_create_param_study(self, filePath):
102 self.find_or_create_hydro_component()
103 itemName = os.path.splitext(os.path.basename(filePath))[0]
104 sobj = self.editor.findOrCreateItem(self.hydroComp,
106 fileType=PARAM_STUDY_FILE_TYPE,
108 icon=PARAM_STUDY_ICON,
109 comment=str(filePath),
110 typeId=PARAM_STUDY_TYPE_ID)
112 def find_or_create_run_study(self, filePath):
113 self.find_or_create_hydro_component()
114 itemName = os.path.splitext(os.path.basename(filePath))[0]
115 sobj = self.editor.findOrCreateItem(self.hydroComp,
117 fileType=STUDY_FILE_TYPE,
120 comment=str(filePath),
121 typeId=STUDY_TYPE_ID)
123 def generate_study_script(self, filePath):
125 Generating a python script from the eficas info
127 # Create "Python script" item
128 from TelApy.api.generate_study import generate_study_script
129 with open(filePath) as jdcfile:
131 params = jdc_to_dict(jdc, ["TELEMAC2D", "_F"])
133 # Generation script string
134 python_script = generate_study_script(params)
136 # Computing name of the file (same as filePath)
137 file_dir, filename = os.path.split(filePath)
138 root, _ = os.path.splitext(filename)
139 python_file = os.path.join(file_dir, root+".py")
142 with open(python_file, 'w') as pfile:
143 pfile.write(python_script)
145 def generate_study_yacs(self, filePath):
147 Generating a yacs file from the eficas info
149 # Create "Python script" item
150 from TelApy.api.generate_study import generate_study_yacs
151 with open(filePath) as jdcfile:
153 params = jdc_to_dict(jdc, ["TELEMAC2D", "_F"])
155 # Generation YACS scheme
156 yacs_scheme = generate_study_yacs(params)
158 # Computing name of the file (same as filePath)
159 file_dir, filename = os.path.split(filePath)
160 root, _ = os.path.splitext(filename)
161 yacs_file = os.path.join(file_dir, root+".xml")
164 yacs_scheme.saveSchema(yacs_file)