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