]> SALOME platform Git repositories - modules/homard.git/blob - src/HOMARDGUI/HomardQtCommun.cxx
Salome HOME
Japanese translations update
[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 #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 commentaite, 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     _PTR(Study) aStudy = HOMARD_UTILS::GetActiveStudyDocument();
81     _PTR(SObject) aSO ( aStudy->FindObjectID( aIO->getEntry() ) );
82     _PTR(GenericAttribute) anAttr;
83     if (aSO->FindAttribute(anAttr, "AttributeComment") )
84     {
85       if ( commentaire != "" )
86       {
87         _PTR(AttributeComment) attributComment = anAttr;
88         QString aComment= QString(attributComment->Value().data());
89         MESSAGE("... aComment = " << aComment.toStdString().c_str());
90         int iaux = aComment.lastIndexOf(commentaire);
91         MESSAGE("... iaux = " << iaux);
92         if ( iaux !=0  )
93         {
94           QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
95                                     QObject::tr("HOM_SELECT_OBJECT_3").arg(commentaire) );
96           return QString("");
97         }
98       }
99       if (aSO->FindAttribute(anAttr, "AttributeName") )
100       {
101         _PTR(AttributeName) attributName = anAttr;
102         QString aName= QString(attributName->Value().data());
103         return aName;
104       }
105     }
106   }
107 //
108   return QString("");
109 }
110
111 // =======================================================================
112 QString HOMARD_QT_COMMUN::SelectionCasEtude()
113 // =======================================================================
114 {
115   QString aName    = QString("");
116   int nbSel = HOMARD_UTILS::IObjectCount() ;
117   if ( nbSel == 0 )
118   {
119     QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
120                               QObject::tr("HOM_SELECT_OBJECT_1") );
121     return QString("");
122   }
123   if ( nbSel > 1 )
124   {
125     QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
126                               QObject::tr("HOM_SELECT_OBJECT_2") );
127     return QString("");
128   }
129   Handle(SALOME_InteractiveObject) aIO = HOMARD_UTILS::firstIObject();
130   if ( aIO->hasEntry() )
131   {
132         _PTR(Study) aStudy = HOMARD_UTILS::GetActiveStudyDocument();
133         _PTR(SObject) aSO ( aStudy->FindObjectID( aIO->getEntry() ) );
134         _PTR(SObject) aSObjCas = aSO->GetFather();
135         _PTR(GenericAttribute) anAttr;
136         if (aSObjCas->FindAttribute(anAttr, "AttributeName") )
137         {
138             _PTR(AttributeName) attributName = anAttr;
139             aName= QString(attributName->Value().data());
140         }
141         return aName;
142     }
143     return QString("");
144 }
145
146 // =======================================================================
147 QString HOMARD_QT_COMMUN::PushNomFichier(bool avertir)
148 // =======================================================================
149 // Gestion les boutons qui permettent  de
150 // 1) retourne le nom d'un fichier par une fenetre de dialogue si aucun
151 //    objet est selectionne dans l arbre d etude
152 // 2) retourne le nom du fichier asocie a l objet
153 //    selectionne dans l arbre d etude
154 {
155   MESSAGE("PushNomFichier");
156   QString aFile=QString::null;
157   int nbSel = HOMARD_UTILS::IObjectCount() ;
158   if ( nbSel == 0 )
159   {
160     aFile = QFileDialog::getOpenFileName(0,QString("File Selection"),QString("") ,QString("Med files (*.med);;all (*) ") );
161   }
162   if (nbSel > 1)
163   {
164     QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
165                               QObject::tr("HOM_SELECT_FILE_2") );
166   }
167   if (nbSel == 1)
168   {
169     Handle(SALOME_InteractiveObject) aIO = HOMARD_UTILS::firstIObject();
170     if ( aIO->hasEntry() )
171     {
172       _PTR(Study) aStudy = HOMARD_UTILS::GetActiveStudyDocument();
173       _PTR(SObject) aSO ( aStudy->FindObjectID( aIO->getEntry() ) );
174       _PTR(GenericAttribute) anAttr;
175       _PTR(AttributeFileType) aFileType;
176       _PTR(AttributeExternalFileDef) aFileName;
177       if (aSO) {
178         if (aSO->FindAttribute(anAttr, "AttributeFileType") ) {
179           aFileType=anAttr;
180           QString fileType=QString(aFileType->Value().data());
181           if ( fileType==QString("FICHIERMED")) {
182             if (aSO->FindAttribute(anAttr,"AttributeExternalFileDef")) {
183             aFileName=anAttr;
184             aFile= QString(aFileName->Value().data()); }
185           }
186         }
187       }
188     }
189
190     if ( aFile==QString::null )
191     {
192       if ( avertir ) {
193         QMessageBox::warning( 0, QObject::tr("HOM_WARNING"),
194                                 QObject::tr("HOM_SELECT_STUDY") );
195       }
196       aFile = QFileDialog::getOpenFileName();
197       if (!aFile.isEmpty())
198       {
199         aFile=aFile;
200       }
201     }
202   }
203   return aFile;
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   QString nomMaillage = "" ;
225   int erreur = 0 ;
226   med_int medIdt ;
227   while ( erreur == 0 )
228   {
229     //  Ouverture du fichier
230     medIdt = HOMARD_QT_COMMUN::OuvrirFichier(aFile);
231     if ( medIdt < 0 )
232     {
233       erreur = 1 ;
234       break ;
235     }
236     med_int numberOfMeshes = MEDnMesh(medIdt) ;
237     if (numberOfMeshes == 0 )
238     {
239       QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
240                                 QObject::tr("HOM_MED_FILE_2") );
241       erreur = 2 ;
242       break ;
243     }
244     if (numberOfMeshes > 1 )
245     {
246       QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
247                                 QObject::tr("HOM_MED_FILE_3") );
248       erreur = 3 ;
249       break ;
250     }
251
252     nomMaillage = HOMARD_QT_COMMUN::LireNomMaillage(medIdt,1);
253     break ;
254   }
255   // Fermeture du fichier
256   if ( medIdt > 0 ) MEDfileClose(medIdt);
257
258   return nomMaillage;
259 }
260 // =======================================================================
261 QString HOMARD_QT_COMMUN::LireNomMaillage(int medIdt ,int meshId)
262 // =======================================================================
263 {
264   QString NomMaillage=QString::null;
265   char meshname[MED_NAME_SIZE+1];
266   med_int spacedim,meshdim;
267   med_mesh_type meshtype;
268   char descriptionription[MED_COMMENT_SIZE+1];
269   char dtunit[MED_SNAME_SIZE+1];
270   med_sorting_type sortingtype;
271   med_int nstep;
272   med_axis_type axistype;
273   int naxis = MEDmeshnAxis(medIdt,1);
274   char *axisname=new char[naxis*MED_SNAME_SIZE+1];
275   char *axisunit=new char[naxis*MED_SNAME_SIZE+1];
276   med_err aRet = MEDmeshInfo(medIdt,
277                           meshId,
278                           meshname,
279                           &spacedim,
280                           &meshdim,
281                           &meshtype,
282                           descriptionription,
283                           dtunit,
284                           &sortingtype,
285                           &nstep,
286                           &axistype,
287                           axisname,
288                           axisunit);
289
290   if ( aRet < 0 ) { QMessageBox::critical( 0, QObject::tr("HOM_ERROR"), \
291                                               QObject::tr("HOM_MED_FILE_4") );  }
292   else            { NomMaillage=QString(meshname); }
293
294   delete[] axisname ;
295   delete[] axisunit ;
296
297   return NomMaillage;
298 }
299
300
301 // =======================================================================
302 std::list<QString> HOMARD_QT_COMMUN::GetListeChamps(QString aFile)
303 // =======================================================================
304 {
305 // Il faut voir si plusieurs maillages
306
307   MESSAGE("GetListeChamps");
308   std::list<QString> ListeChamp ;
309
310   med_err erreur = 0 ;
311   med_int medIdt ;
312
313   while ( erreur == 0 )
314   {
315     // Ouverture du fichier
316     SCRUTE(aFile.toStdString());
317     medIdt = HOMARD_QT_COMMUN::OuvrirFichier(aFile);
318     if ( medIdt < 0 )
319     {
320       erreur = 1 ;
321       break ;
322     }
323   // Lecture du nombre de champs
324     med_int ncha = MEDnField(medIdt) ;
325     if (ncha < 1 )
326     {
327       QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
328                                 QObject::tr("HOM_MED_FILE_5") );
329       erreur = 2 ;
330       break ;
331     }
332   // Lecture des caracteristiques des champs
333     for (int i=0; i< ncha; i++)
334     {
335 //       Lecture du nombre de composantes
336       med_int ncomp = MEDfieldnComponent(medIdt,i+1);
337 //       Lecture du type du champ, des noms des composantes et du nom de l'unite
338       char nomcha  [MED_NAME_SIZE+1];
339       char meshname[MED_NAME_SIZE+1];
340       char * comp = (char*) malloc(ncomp*MED_SNAME_SIZE+1);
341       char * unit = (char*) malloc(ncomp*MED_SNAME_SIZE+1);
342       char dtunit[MED_SNAME_SIZE+1];
343       med_bool local;
344       med_field_type typcha;
345       med_int nbofcstp;
346       erreur = MEDfieldInfo(medIdt,i+1,nomcha,meshname,&local,&typcha,comp,unit,dtunit,&nbofcstp) ;
347       free(comp);
348       free(unit);
349       if ( erreur < 0 )
350       {
351         QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
352                                   QObject::tr("HOM_MED_FILE_6") );
353         break ;
354       }
355       ListeChamp.push_back(QString(nomcha));
356     }
357     break ;
358   }
359   // Fermeture du fichier
360   if ( medIdt > 0 ) MEDfileClose(medIdt);
361
362   return ListeChamp;
363 }
364
365 // ======================================================================================
366 std::list<QString> HOMARD_QT_COMMUN::GetListeComposants(QString aFile, QString aChamp)
367 // ======================================================================================
368 {
369   MESSAGE ( "GetListeComposants pour le fichier " << aFile.toStdString().c_str());
370   MESSAGE ( "GetListeComposants pour le champ " << aChamp.toStdString().c_str());
371
372   std::list<QString> ListeComposants;
373
374   med_err erreur = 0 ;
375   med_int medIdt ;
376
377   while ( erreur == 0 )
378   {
379     // Ouverture du fichier
380     SCRUTE(aFile.toStdString());
381     medIdt = HOMARD_QT_COMMUN::OuvrirFichier(aFile);
382     if ( medIdt < 0 )
383     {
384       erreur = 1 ;
385       break ;
386     }
387   // Lecture du nombre de champs
388     med_int ncha = MEDnField(medIdt) ;
389     if (ncha < 1 )
390     {
391       QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
392                                 QObject::tr("HOM_MED_FILE_5") );
393       erreur = 2 ;
394       break ;
395     }
396   // Lecture des caracteristiques des champs
397     for (int i=0; i< ncha; i++)
398     {
399 //       Lecture du nombre de composantes
400       med_int ncomp = MEDfieldnComponent(medIdt,i+1);
401 //       Lecture du type du champ, des noms des composantes et du nom de l'unite
402       char nomcha  [MED_NAME_SIZE+1];
403       char meshname[MED_NAME_SIZE+1];
404       char * comp = (char*) malloc(ncomp*MED_SNAME_SIZE+1);
405       char * unit = (char*) malloc(ncomp*MED_SNAME_SIZE+1);
406       char dtunit[MED_SNAME_SIZE+1];
407       med_bool local;
408       med_field_type typcha;
409       med_int nbofcstp;
410       erreur = MEDfieldInfo(medIdt,i+1,nomcha,meshname,&local,&typcha,comp,unit,dtunit,&nbofcstp) ;
411       free(unit);
412       if ( erreur < 0 )
413       {
414         free(comp);
415         QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
416                                   QObject::tr("HOM_MED_FILE_6") );
417         break ;
418       }
419       // Lecture des composantes si c'est le bon champ
420       if ( QString(nomcha) == aChamp )
421       {
422         for (int j = 0; j <ncomp; j++)
423         {
424           char cible[MED_SNAME_SIZE +1];
425           strncpy(cible,comp+j*MED_SNAME_SIZE,MED_SNAME_SIZE );
426           cible[MED_SNAME_SIZE ]='\0';
427           ListeComposants.push_back(QString(cible));
428         }
429       }
430       // Menage
431       free(comp);
432       // Sortie si c'est bon
433       if ( QString(nomcha) == aChamp ) { break ; }
434     }
435     break ;
436   }
437   // Fermeture du fichier
438   if ( medIdt > 0 ) MEDfileClose(medIdt);
439
440   return ListeComposants;
441 }