Salome HOME
integration of modifications from Gerald Nicolas
[modules/homard.git] / src / HOMARDGUI / HomardQtCommun.cxx
1 // Copyright (C) 2011-2012  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "HomardQtCommun.h"
21 #include "HOMARDGUI_Utils.h"
22
23 #include <utilities.h>
24
25 #include <qmessagebox.h>
26 #include <qcombobox.h>
27 #include <qfiledialog.h>
28 #include <qstring.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <sys/stat.h>
32
33
34 #include "SalomeApp_Tools.h"
35
36 using namespace std;
37 extern "C"
38 {
39 #include <med.h>
40 }
41
42
43 // ============================================================================
44 QString HOMARD_QT_COMMUN::SelectionArbreEtude(QString commentaire, int option )
45 // ============================================================================
46 // Retourne l'objet selectionne dans l'arbre d'etudes
47 // commentaire :
48 // . si le commentaire est une chaine vide, on ne tient pas compte du type de l'objet
49 //   et on retourne le nom de cet objet
50 // . sinon :
51 //   . si l'objet est du type defini par commentaitr, retourne le nom de cet objet
52 //   . sinon on retourne une QString("")
53 // option :
54 // . Si option = 0, ce n'est pas grave de ne rien trouver ; aucun message n'est emis
55 // . Si option = 1, ce n'est pas grave de ne rien trouver mais on emet un message
56 {
57   MESSAGE("SelectionArbreEtude : commentaire = " << commentaire.toStdString().c_str() << " et option = " << option);
58   int nbSel = HOMARD_UTILS::IObjectCount() ;
59   if ( nbSel == 0 )
60   {
61     if ( option == 1 )
62     {
63       QMessageBox::warning( 0, QObject::tr("HOM_WARNING"),
64                                QObject::tr("HOM_SELECT_OBJECT_1") );
65     }
66     return QString("");
67   }
68   if ( nbSel > 1 )
69   {
70     QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
71                               QObject::tr("HOM_SELECT_OBJECT_2") );
72     return QString("");
73   }
74 //
75   Handle(SALOME_InteractiveObject) aIO = HOMARD_UTILS::firstIObject();
76   if ( aIO->hasEntry() )
77   {
78     _PTR(Study) aStudy = HOMARD_UTILS::GetActiveStudyDocument();
79     _PTR(SObject) aSO ( aStudy->FindObjectID( aIO->getEntry() ) );
80     _PTR(GenericAttribute) anAttr;
81     if (aSO->FindAttribute(anAttr, "AttributeComment") )
82     {
83       if ( commentaire != "" )
84       {
85         _PTR(AttributeComment) attributComment = anAttr;
86         QString aComment= QString(attributComment->Value().data());
87         MESSAGE("... aComment = " << aComment.toStdString().c_str());
88         int iaux = aComment.lastIndexOf(commentaire);
89         MESSAGE("... iaux = " << iaux);
90         if ( iaux !=0  )
91         {
92           QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
93                                     QObject::tr("HOM_SELECT_OBJECT_3").arg(commentaire) );
94           return QString("");
95         }
96       }
97       if (aSO->FindAttribute(anAttr, "AttributeName") )
98       {
99         _PTR(AttributeName) attributName = anAttr;
100         QString aName= QString(attributName->Value().data());
101         return aName;
102       }
103     }
104   }
105 //
106   return QString("");
107 }
108
109 // =======================================================================
110 QString HOMARD_QT_COMMUN::SelectionCasEtude()
111 // =======================================================================
112 {
113   QString aName    = QString("");
114   int nbSel = HOMARD_UTILS::IObjectCount() ;
115   if ( nbSel == 0 )
116   {
117     QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
118                               QObject::tr("HOM_SELECT_OBJECT_1") );
119     return QString("");
120   }
121   if ( nbSel > 1 )
122   {
123     QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
124                               QObject::tr("HOM_SELECT_OBJECT_2") );
125     return QString("");
126   }
127   Handle(SALOME_InteractiveObject) aIO = HOMARD_UTILS::firstIObject();
128   if ( aIO->hasEntry() )
129   {
130         _PTR(Study) aStudy = HOMARD_UTILS::GetActiveStudyDocument();
131         _PTR(SObject) aSO ( aStudy->FindObjectID( aIO->getEntry() ) );
132         _PTR(SObject) aSObjCas = aSO->GetFather();
133         _PTR(GenericAttribute) anAttr;
134         if (aSObjCas->FindAttribute(anAttr, "AttributeName") )
135         {
136             _PTR(AttributeName) attributName = anAttr;
137             aName= QString(attributName->Value().data());
138         }
139         return aName;
140     }
141     return QString("");
142 }
143
144 // =======================================================================
145 QString HOMARD_QT_COMMUN::PushNomFichier(bool avertir)
146 // =======================================================================
147 // Gestion les boutons qui permettent  de
148 // 1) retourne le nom d'un fichier par une fenetre de dialogue si aucun
149 //    objet est selectionne dans l arbre d etude
150 // 2) retourne le nom du fichier asocie a l objet
151 //    selectionne dans l arbre d etude
152 {
153   MESSAGE("PushNomFichier");
154   QString aFile=QString::null;
155   int nbSel = HOMARD_UTILS::IObjectCount() ;
156   if ( nbSel == 0 )
157   {
158     aFile = QFileDialog::getOpenFileName(0,QString("File Selection"),QString("") ,QString("Med files (*.med);;all (*) ") );
159   }
160   if (nbSel > 1)
161   {
162     QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
163                               QObject::tr("HOM_SELECT_FILE_2") );
164   }
165   if (nbSel == 1)
166   {
167     Handle(SALOME_InteractiveObject) aIO = HOMARD_UTILS::firstIObject();
168     if ( aIO->hasEntry() )
169     {
170       _PTR(Study) aStudy = HOMARD_UTILS::GetActiveStudyDocument();
171       _PTR(SObject) aSO ( aStudy->FindObjectID( aIO->getEntry() ) );
172       _PTR(GenericAttribute) anAttr;
173       _PTR(AttributeFileType) aFileType;
174       _PTR(AttributeExternalFileDef) aFileName;
175       if (aSO) {
176         if (aSO->FindAttribute(anAttr, "AttributeFileType") ) {
177           aFileType=anAttr;
178           QString fileType=QString(aFileType->Value().data());
179           if ( fileType==QString("FICHIERMED")) {
180             if (aSO->FindAttribute(anAttr,"AttributeExternalFileDef")) {
181             aFileName=anAttr;
182             aFile= QString(aFileName->Value().data()); }
183           }
184         }
185       }
186     }
187
188     if ( aFile==QString::null )
189     {
190       if ( avertir ) {
191         QMessageBox::warning( 0, QObject::tr("HOM_WARNING"),
192                                 QObject::tr("HOM_SELECT_STUDY") );
193       }
194       aFile = QFileDialog::getOpenFileName();
195       if (!aFile.isEmpty())
196       {
197         aFile=aFile;
198       }
199     }
200   }
201   return aFile;
202
203 }
204
205
206 // =======================================================================
207 int HOMARD_QT_COMMUN::OuvrirFichier(QString aFile)
208 // =======================================================================
209 // renvoie le medId associe au fichier Med apres ouverture
210 {
211   med_int medIdt = MEDfileOpen(aFile.toStdString().c_str(),MED_ACC_RDONLY);
212   if (medIdt <0)
213   {
214     QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
215                               QObject::tr("HOM_MED_FILE_1") );
216   }
217   return (int) medIdt;
218 }
219
220 // ======================================================
221 QString HOMARD_QT_COMMUN::LireNomMaillage(QString aFile)
222 // ========================================================
223 {
224   med_int medIdt = HOMARD_QT_COMMUN::OuvrirFichier(aFile);
225   med_int numberOfMeshes = MEDnMesh(medIdt) ;
226   if (numberOfMeshes == 0 )
227   {
228     QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
229                               QObject::tr("HOM_MED_FILE_2") );
230   }
231   if (numberOfMeshes > 1 )
232   {
233     QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
234                               QObject::tr("HOM_MED_FILE_3") );
235   }
236
237   QString nomMaillage= HOMARD_QT_COMMUN::LireNomMaillage(medIdt,1);
238   MEDfileClose(medIdt);
239   return nomMaillage;
240 }
241 // =======================================================================
242 QString HOMARD_QT_COMMUN::LireNomMaillage(int medIdt ,int meshId)
243 // =======================================================================
244 {
245   QString NomMaillage=QString::null;
246   char meshname[MED_NAME_SIZE+1];
247   med_int spacedim,meshdim;
248   med_mesh_type meshtype;
249   char descriptionription[MED_COMMENT_SIZE+1];
250   char dtunit[MED_SNAME_SIZE+1];
251   med_sorting_type sortingtype;
252   med_int nstep;
253   med_axis_type axistype;
254   int naxis = MEDmeshnAxis(medIdt,1);
255   char *axisname=new char[naxis*MED_SNAME_SIZE+1];
256   char *axisunit=new char[naxis*MED_SNAME_SIZE+1];
257   med_err aRet = MEDmeshInfo(medIdt,
258                           meshId,
259                           meshname,
260                           &spacedim,
261                           &meshdim,
262                           &meshtype,
263                           descriptionription,
264                           dtunit,
265                           &sortingtype,
266                           &nstep,
267                           &axistype,
268                           axisname,
269                           axisunit);
270
271   if ( aRet < 0 )
272   {
273   QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
274                             QObject::tr("HOM_MED_FILE_4") );
275   }
276   else
277   {
278     NomMaillage=QString(meshname);
279   }
280   return NomMaillage;
281 }
282
283
284 // =======================================================================
285 std::list<QString> HOMARD_QT_COMMUN::GetListeChamps(QString aFile)
286 // =======================================================================
287 {
288 // Il faut voir si plusieurs maillages
289
290   MESSAGE("GetListeChamps");
291   std::list<QString> ListeChamp;
292
293   char *comp, *unit;
294   char nomcha  [MED_NAME_SIZE+1];
295   char meshname[MED_NAME_SIZE+1];
296   med_field_type typcha;
297   med_int ncomp;
298   med_bool local;
299   med_int nbofcstp;
300
301   SCRUTE(aFile.toStdString());
302   med_int medIdt = HOMARD_QT_COMMUN::OuvrirFichier(aFile);
303   if ( medIdt < 0 ) { return ListeChamp; }
304
305   // Le fichier Med est lisible
306   // Lecture du maillage
307
308   // Lecture du nombre de champs
309   med_int ncha = MEDnField(medIdt) ;
310   if (ncha < 1 )
311   {
312     QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
313                             QObject::tr("HOM_MED_FILE_5") );
314     MEDfileClose(medIdt);
315     return ListeChamp;
316   }
317
318   for (int i=0; i< ncha; i++)
319   {
320     /* Lecture du type du champ, des noms des composantes et du nom de l'unite*/
321     ncomp = MEDfieldnComponent(medIdt,i+1);
322     comp = (char*) malloc(ncomp*MED_SNAME_SIZE+1);
323     unit = (char*) malloc(ncomp*MED_SNAME_SIZE+1);
324     char dtunit[MED_SNAME_SIZE+1];
325     if ( MEDfieldInfo(medIdt,i+1,nomcha,meshname,&local,&typcha,comp,unit,dtunit,&nbofcstp) < 0 )
326     {
327       QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
328                                 QObject::tr("HOM_MED_FILE_6") );
329       MEDfileClose(medIdt);
330       return ListeChamp;
331     }
332
333     ListeChamp.push_back(QString(nomcha));
334     free(comp);
335     free(unit);
336   }
337   MEDfileClose(medIdt);
338   return ListeChamp;
339 }
340
341 // ======================================================================================
342 std::list<QString> HOMARD_QT_COMMUN::GetListeComposants(QString aFile, QString aChamp)
343 // ======================================================================================
344 {
345   MESSAGE ( "GetListeComposants pour le fichier " << aFile.toStdString().c_str());
346   MESSAGE ( "GetListeComposants pour le champ " << aChamp.toStdString().c_str());
347
348   std::list<QString> ListeComposants;
349
350   char *comp, *unit;
351   char nomcha  [MED_NAME_SIZE+1];
352   char meshname[MED_NAME_SIZE+1];
353   med_field_type typcha;
354   med_int ncomp;
355   med_bool local;
356   med_int nbofcstp;
357
358   int medIdt = HOMARD_QT_COMMUN::OuvrirFichier(aFile);
359   if ( medIdt < 0 ) { return ListeComposants; }
360
361
362   // Lecture du nombre de champs
363   med_int ncha = MEDnField(medIdt) ;
364   if (ncha < 1 )
365   {
366     QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
367                             QObject::tr("HOM_MED_FILE_5") );
368     MEDfileClose(medIdt);
369     return ListeComposants;
370   }
371
372   for (int i=0; i< ncha; i++)
373   {
374     /* Lecture du type du champ, des noms des composantes et du nom de l'unite*/
375     ncomp = MEDfieldnComponent(medIdt,i+1);
376     comp = (char*) malloc(ncomp*MED_SNAME_SIZE+1);
377     unit = (char*) malloc(ncomp*MED_SNAME_SIZE+1);
378     char dtunit[MED_SNAME_SIZE+1];
379
380     if ( MEDfieldInfo(medIdt,i+1,nomcha,meshname,&local,&typcha,comp,unit,dtunit,&nbofcstp) < 0 )
381     {
382       QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
383                                 QObject::tr("HOM_MED_FILE_6") );
384       MEDfileClose(medIdt);
385       return ListeComposants;
386     }
387
388     if ( QString(nomcha) != aChamp ) {
389       free(comp);
390       free(unit);
391       continue;
392     }
393
394     for (int j = 0; j <ncomp; j++)
395     {
396       char cible[MED_SNAME_SIZE +1];
397       strncpy(cible,comp+j*MED_SNAME_SIZE,MED_SNAME_SIZE );
398       cible[MED_SNAME_SIZE ]='\0';
399       ListeComposants.push_back(QString(cible));
400     }
401     break;
402   }
403   free(comp);
404   free(unit);
405   MEDfileClose(medIdt);
406   return ListeComposants;
407 }