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