]> SALOME platform Git repositories - modules/homard.git/blob - src/HOMARDGUI/HomardQtCommun.cxx
Salome HOME
Catalogue pour YACS ; liberation de structures allouees ; documentation
[modules/homard.git] / src / HOMARDGUI / HomardQtCommun.cxx
1 // Copyright (C) 2011-2013  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 int HOMARD_QT_COMMUN::OuvrirFichier(QString aFile)
206 // =======================================================================
207 // renvoie le medId associe au fichier Med apres ouverture
208 {
209   med_int medIdt = MEDfileOpen(aFile.toStdString().c_str(),MED_ACC_RDONLY);
210   if (medIdt <0)
211   {
212     QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
213                               QObject::tr("HOM_MED_FILE_1") );
214   }
215   return (int) medIdt;
216 }
217
218 // ======================================================
219 QString HOMARD_QT_COMMUN::LireNomMaillage(QString aFile)
220 // ========================================================
221 {
222   QString nomMaillage = "" ;
223   int erreur = 0 ;
224   med_int medIdt ;
225   while ( erreur == 0 )
226   {
227     //  Ouverture du fichier
228     medIdt = HOMARD_QT_COMMUN::OuvrirFichier(aFile);
229     if ( medIdt < 0 )
230     {
231       erreur = 1 ;
232       break ;
233     }
234     med_int numberOfMeshes = MEDnMesh(medIdt) ;
235     if (numberOfMeshes == 0 )
236     {
237       QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
238                                 QObject::tr("HOM_MED_FILE_2") );
239       erreur = 2 ;
240       break ;
241     }
242     if (numberOfMeshes > 1 )
243     {
244       QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
245                                 QObject::tr("HOM_MED_FILE_3") );
246       erreur = 3 ;
247       break ;
248     }
249
250     nomMaillage = HOMARD_QT_COMMUN::LireNomMaillage(medIdt,1);
251     break ;
252   }
253   // Fermeture du fichier
254   if ( medIdt > 0 ) MEDfileClose(medIdt);
255
256   return nomMaillage;
257 }
258 // =======================================================================
259 QString HOMARD_QT_COMMUN::LireNomMaillage(int medIdt ,int meshId)
260 // =======================================================================
261 {
262   QString NomMaillage=QString::null;
263   char meshname[MED_NAME_SIZE+1];
264   med_int spacedim,meshdim;
265   med_mesh_type meshtype;
266   char descriptionription[MED_COMMENT_SIZE+1];
267   char dtunit[MED_SNAME_SIZE+1];
268   med_sorting_type sortingtype;
269   med_int nstep;
270   med_axis_type axistype;
271   int naxis = MEDmeshnAxis(medIdt,1);
272   char *axisname=new char[naxis*MED_SNAME_SIZE+1];
273   char *axisunit=new char[naxis*MED_SNAME_SIZE+1];
274   med_err aRet = MEDmeshInfo(medIdt,
275                           meshId,
276                           meshname,
277                           &spacedim,
278                           &meshdim,
279                           &meshtype,
280                           descriptionription,
281                           dtunit,
282                           &sortingtype,
283                           &nstep,
284                           &axistype,
285                           axisname,
286                           axisunit);
287
288   if ( aRet < 0 ) { QMessageBox::critical( 0, QObject::tr("HOM_ERROR"), \
289                                               QObject::tr("HOM_MED_FILE_4") );  }
290   else            { NomMaillage=QString(meshname); }
291
292   delete[] axisname ;
293   delete[] axisunit ;
294
295   return NomMaillage;
296 }
297
298
299 // =======================================================================
300 std::list<QString> HOMARD_QT_COMMUN::GetListeChamps(QString aFile)
301 // =======================================================================
302 {
303 // Il faut voir si plusieurs maillages
304
305   MESSAGE("GetListeChamps");
306   std::list<QString> ListeChamp ;
307
308   med_err erreur = 0 ;
309   med_int medIdt ;
310
311   while ( erreur == 0 )
312   {
313     // Ouverture du fichier
314     SCRUTE(aFile.toStdString());
315     medIdt = HOMARD_QT_COMMUN::OuvrirFichier(aFile);
316     if ( medIdt < 0 )
317     {
318       erreur = 1 ;
319       break ;
320     }
321   // Lecture du nombre de champs
322     med_int ncha = MEDnField(medIdt) ;
323     if (ncha < 1 )
324     {
325       QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
326                                 QObject::tr("HOM_MED_FILE_5") );
327       erreur = 2 ;
328       break ;
329     }
330   // Lecture des caracteristiques des champs
331     for (int i=0; i< ncha; i++)
332     {
333 //       Lecture du nombre de composantes
334       med_int ncomp = MEDfieldnComponent(medIdt,i+1);
335 //       Lecture du type du champ, des noms des composantes et du nom de l'unite
336       char nomcha  [MED_NAME_SIZE+1];
337       char meshname[MED_NAME_SIZE+1];
338       char * comp = (char*) malloc(ncomp*MED_SNAME_SIZE+1);
339       char * unit = (char*) malloc(ncomp*MED_SNAME_SIZE+1);
340       char dtunit[MED_SNAME_SIZE+1];
341       med_bool local;
342       med_field_type typcha;
343       med_int nbofcstp;
344       erreur = MEDfieldInfo(medIdt,i+1,nomcha,meshname,&local,&typcha,comp,unit,dtunit,&nbofcstp) ;
345       free(comp);
346       free(unit);
347       if ( erreur < 0 )
348       {
349         QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
350                                   QObject::tr("HOM_MED_FILE_6") );
351         break ;
352       }
353       ListeChamp.push_back(QString(nomcha));
354     }
355     break ;
356   }
357   // Fermeture du fichier
358   if ( medIdt > 0 ) MEDfileClose(medIdt);
359
360   return ListeChamp;
361 }
362
363 // ======================================================================================
364 std::list<QString> HOMARD_QT_COMMUN::GetListeComposants(QString aFile, QString aChamp)
365 // ======================================================================================
366 {
367   MESSAGE ( "GetListeComposants pour le fichier " << aFile.toStdString().c_str());
368   MESSAGE ( "GetListeComposants pour le champ " << aChamp.toStdString().c_str());
369
370   std::list<QString> ListeComposants;
371
372   med_err erreur = 0 ;
373   med_int medIdt ;
374
375   while ( erreur == 0 )
376   {
377     // Ouverture du fichier
378     SCRUTE(aFile.toStdString());
379     medIdt = HOMARD_QT_COMMUN::OuvrirFichier(aFile);
380     if ( medIdt < 0 )
381     {
382       erreur = 1 ;
383       break ;
384     }
385   // Lecture du nombre de champs
386     med_int ncha = MEDnField(medIdt) ;
387     if (ncha < 1 )
388     {
389       QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
390                                 QObject::tr("HOM_MED_FILE_5") );
391       erreur = 2 ;
392       break ;
393     }
394   // Lecture des caracteristiques des champs
395     for (int i=0; i< ncha; i++)
396     {
397 //       Lecture du nombre de composantes
398       med_int ncomp = MEDfieldnComponent(medIdt,i+1);
399 //       Lecture du type du champ, des noms des composantes et du nom de l'unite
400       char nomcha  [MED_NAME_SIZE+1];
401       char meshname[MED_NAME_SIZE+1];
402       char * comp = (char*) malloc(ncomp*MED_SNAME_SIZE+1);
403       char * unit = (char*) malloc(ncomp*MED_SNAME_SIZE+1);
404       char dtunit[MED_SNAME_SIZE+1];
405       med_bool local;
406       med_field_type typcha;
407       med_int nbofcstp;
408       erreur = MEDfieldInfo(medIdt,i+1,nomcha,meshname,&local,&typcha,comp,unit,dtunit,&nbofcstp) ;
409       free(unit);
410       if ( erreur < 0 )
411       {
412         free(comp);
413         QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
414                                   QObject::tr("HOM_MED_FILE_6") );
415         break ;
416       }
417       // Lecture des composantes si c'est le bon champ
418       if ( QString(nomcha) == aChamp )
419       {
420         for (int j = 0; j <ncomp; j++)
421         {
422           char cible[MED_SNAME_SIZE +1];
423           strncpy(cible,comp+j*MED_SNAME_SIZE,MED_SNAME_SIZE );
424           cible[MED_SNAME_SIZE ]='\0';
425           ListeComposants.push_back(QString(cible));
426         }
427       }
428       // Menage
429       free(comp);
430       // Sortie si c'est bon
431       if ( QString(nomcha) == aChamp ) { break ; }
432     }
433     break ;
434   }
435   // Fermeture du fichier
436   if ( medIdt > 0 ) MEDfileClose(medIdt);
437
438   return ListeComposants;
439 }