Salome HOME
Updating copyright dates
[modules/adao.git] / src / daSalome / daGUI / daGuiImpl / adaoCase.py
1 # -*- coding: utf-8 -*-
2 #  Copyright (C) 2010-2012 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.
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
21 import os
22 import subprocess
23 import traceback
24 import SalomePyQt
25
26 import eficasSalome
27 from Ihm import CONNECTOR
28 import adaoGuiHelper
29 import adaoStudyEditor
30
31 class AdaoCase:
32
33   def __init__(self):
34
35     self.name = "not yet defined"           # Name of the case
36
37     self.filename = "not yet defined"       # Python filename generated by Eficas
38     self.yacs_filename = "not yet defined"  # Yacs schema filename
39
40     self.salome_study_id = -1               # Study of the case
41     self.salome_study_item = None           # Study item object
42
43     self.eficas_editor = None               # Editor object from Eficas
44
45   def setEditor(self, editor):
46     if editor is not self.eficas_editor:
47       self.eficas_editor = editor
48       # Connect to the jdc
49       CONNECTOR.Connect(self.eficas_editor.jdc, "valid", self.editorValidEvent, ())
50
51   # Rq on notera que l'on utilise isvalid dans isOk
52   #    et que isOk appelle editorValidEvent
53   #    il n'y a pas de boucle infini car isvalid n'émet
54   #    son signal que si l'état a changé
55   def editorValidEvent(self):
56     adaoStudyEditor.updateItem(self.salome_study_id, self.salome_study_item, self)
57     adaoGuiHelper.refreshObjectBrowser()
58
59   def isOk(self):
60     if self.eficas_editor.jdc:
61       return self.eficas_editor.jdc.isvalid()
62     return False
63
64   def createYACSFile(self):
65     rtn = ""
66     if (self.filename == ""):
67       return "You need to save your case to export it"
68
69     self.yacs_filename = self.filename[:self.filename.rfind(".")] + '.xml'
70     yacs_filename_backup = self.filename[:self.filename.rfind(".")] + '.xml.back'
71     if os.path.exists(self.yacs_filename):
72       os.rename(self.yacs_filename, yacs_filename_backup)
73
74     self.eficas_editor.modified = True
75     self.eficas_editor.saveFile()
76     filename = self.filename[:self.filename.rfind(".")] + '.py'
77     if not os.path.exists(filename):
78       msg =  "Cannot find the py file for YACS generation \n"
79       msg += "Is your case correct ? \n"
80       return msg
81
82     if not os.environ.has_key("ADAO_ROOT_DIR"):
83       return "Please add ADAO_ROOT_DIR to your environnement"
84
85     adao_path = os.environ["ADAO_ROOT_DIR"]
86     adao_exe = adao_path + "/bin/salome/AdaoYacsSchemaCreator.py"
87     args = ["python", adao_exe, filename, self.yacs_filename]
88     p = subprocess.Popen(args)
89     (stdoutdata, stderrdata) = p.communicate()
90     if not os.path.exists(self.yacs_filename):
91       msg  = "An error occured during the execution of AdaoYacsSchemaCreator.py \n"
92       msg += "See erros details in your terminal \n"
93       return msg
94     return rtn
95
96   def exportCaseToYACS(self):
97     rtn = ""
98     rtn = self.createYACSFile()
99     if rtn != "":
100       return rtn
101
102     try:
103       import libYACS_Swig
104       yacs_swig = libYACS_Swig.YACS_Swig()
105       yacs_swig.loadSchema(self.yacs_filename, 1, 1)
106     except:
107       msg =  "Please install YACS module, error was: \n"
108       msg += traceback.format_exc()
109       return msg
110     return rtn