]> SALOME platform Git repositories - modules/homard.git/blob - src/HOMARDGUI/HomardQtCommun.cxx
Salome HOME
79746df41e1b44b6e2516484086fc0b64f817920
[modules/homard.git] / src / HOMARDGUI / HomardQtCommun.cxx
1 #include "HomardQtCommun.h"
2 #include "HOMARDGUI_Utils.h"
3
4 #include <utilities.h>
5
6 #include <qmessagebox.h>
7 #include <qcombobox.h>
8 #include <qfiledialog.h>
9 #include <qstring.h>
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <sys/stat.h>
13
14
15 #include "SalomeApp_Tools.h"
16
17 using namespace std;
18 extern "C"
19 {
20 #include <med.h>
21 }
22
23
24 // ============================================================================
25 QString HOMARD_QT_COMMUN::SelectionArbreEtude(QString commentaire, int grave )
26 // ============================================================================
27 // Regarde si l'objet selectionne correspond a un objet de tyoe
28 // commentaire. si c'est le cas, retourne le nom de  cet objet,
29 // sinon retourne une QString("")
30 // Si grave = 0, ce n'est pas grave de ne rien trouver et pas de message
31 // Si grave = 1, ce n'est pas grave de ne rien trouver mais on emet un message
32 {
33   int nbSel = HOMARD_UTILS::IObjectCount() ;
34   if ( nbSel == 0 )
35   {
36     if ( grave == 1 )
37     {
38       QMessageBox::warning( 0, QObject::tr("HOM_WARNING"),
39                                 QObject::tr("HOM_SELECT_OBJECT_1") );
40     }
41     return QString("");
42   }
43   if ( nbSel > 1 )
44   {
45     QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
46                               QObject::tr("HOM_SELECT_OBJECT_2") );
47     return QString("");
48   }
49 //
50   Handle(SALOME_InteractiveObject) aIO = HOMARD_UTILS::firstIObject();
51   if ( aIO->hasEntry() )
52   {
53     _PTR(Study) aStudy = HOMARD_UTILS::GetActiveStudyDocument();
54     _PTR(SObject) aSO ( aStudy->FindObjectID( aIO->getEntry() ) );
55     _PTR(GenericAttribute) anAttr;
56     if (aSO->FindAttribute(anAttr, "AttributeComment") )
57     {
58       _PTR(AttributeComment) attributComment = anAttr;
59       QString aComment= QString(attributComment->Value().data());
60       int iteration = aComment.lastIndexOf(commentaire);
61       if ( iteration !=0  )
62       {
63         QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
64                                   QObject::tr("HOM_SELECT_OBJECT_3").arg(commentaire) );
65         return QString("");
66       }
67       if (aSO->FindAttribute(anAttr, "AttributeName") )
68       {
69         _PTR(AttributeName) attributName = anAttr;
70         QString aName= QString(attributName->Value().data());
71         return aName;
72       }
73     }
74   }
75 //
76   return QString("");
77 }
78
79 // =======================================================================
80 QString HOMARD_QT_COMMUN::SelectionCasEtude()
81 // =======================================================================
82 {
83   QString aName    = QString("");
84   int nbSel = HOMARD_UTILS::IObjectCount() ;
85   if ( nbSel == 0 )
86   {
87     QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
88                               QObject::tr("HOM_SELECT_OBJECT_1") );
89     return QString("");
90   }
91   if ( nbSel > 1 )
92   {
93     QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
94                               QObject::tr("HOM_SELECT_OBJECT_2") );
95     return QString("");
96   }
97   Handle(SALOME_InteractiveObject) aIO = HOMARD_UTILS::firstIObject();
98   if ( aIO->hasEntry() )
99   {
100         _PTR(Study) aStudy = HOMARD_UTILS::GetActiveStudyDocument();
101         _PTR(SObject) aSO ( aStudy->FindObjectID( aIO->getEntry() ) );
102         _PTR(SObject) aSObjCas = aSO->GetFather();
103         _PTR(GenericAttribute) anAttr;
104         if (aSObjCas->FindAttribute(anAttr, "AttributeName") )
105         {
106             _PTR(AttributeName) attributName = anAttr;
107             aName= QString(attributName->Value().data());
108         }
109         return aName;
110     }
111     return QString("");
112 }
113
114 // =======================================================================
115 QString HOMARD_QT_COMMUN::PushNomFichier(bool avertir)
116 // =======================================================================
117 // Gestion les boutons qui permettent  de
118 // 1) retourne le nom d'un fichier par une fenetre de dialogue si aucun
119 //    objet est selectionne dans l arbre d etude
120 // 2) retourne le nom du fichier asocie a l objet
121 //    selectionne dans l arbre d etude
122 {
123   MESSAGE("HOMARD_QT_COMMUN::PushNomFichier");
124   QString aFile=QString::null;
125   int nbSel = HOMARD_UTILS::IObjectCount() ;
126   if ( nbSel == 0 )
127   {
128     aFile = QFileDialog::getOpenFileName(0,QString("File Selection"),QString("") ,QString("Med files (*.med);;all (*) ") );
129   }
130   if (nbSel > 1)
131   {
132     QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
133                               QObject::tr("HOM_SELECT_FILE_2") );
134   }
135   if (nbSel == 1)
136   {
137     Handle(SALOME_InteractiveObject) aIO = HOMARD_UTILS::firstIObject();
138     if ( aIO->hasEntry() )
139     {
140       _PTR(Study) aStudy = HOMARD_UTILS::GetActiveStudyDocument();
141       _PTR(SObject) aSO ( aStudy->FindObjectID( aIO->getEntry() ) );
142       _PTR(GenericAttribute) anAttr;
143       _PTR(AttributeFileType) aFileType;
144       _PTR(AttributeExternalFileDef) aFileName;
145       if (aSO) {
146         if (aSO->FindAttribute(anAttr, "AttributeFileType") ) {
147           aFileType=anAttr;
148           QString fileType=QString(aFileType->Value().data());
149           if ( fileType==QString("FICHIERMED")) {
150             if (aSO->FindAttribute(anAttr,"AttributeExternalFileDef")) {
151             aFileName=anAttr;
152             aFile= QString(aFileName->Value().data()); }
153           }
154         }
155       }
156     }
157
158     if ( aFile==QString::null )
159     {
160       if ( avertir ) {
161         QMessageBox::warning( 0, QObject::tr("HOM_WARNING"),
162                                 QObject::tr("HOM_SELECT_STUDY") );
163       }
164       aFile = QFileDialog::getOpenFileName();
165       if (!aFile.isEmpty())
166       {
167         aFile=aFile;
168       }
169     }
170   }
171   return aFile;
172
173 }
174
175
176 // =======================================================================
177 int HOMARD_QT_COMMUN::OuvrirFichier(QString aFile)
178 // =======================================================================
179 // renvoie le medId associe au fichier Med apres ouverture
180 {
181   int MedIdt = MEDouvrir(const_cast<char *>(aFile.toStdString().c_str()),MED_LECTURE);
182   if (MedIdt <0)
183   {
184     QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
185                               QObject::tr("HOM_MED_FILE_1") );
186   }
187   return MedIdt;
188 }
189
190 // ======================================================
191 QString HOMARD_QT_COMMUN::LireNomMaillage(QString aFile)
192 // ========================================================
193 {
194   int MedIdt = HOMARD_QT_COMMUN::OuvrirFichier(aFile);
195   int numberOfMeshes = MEDnMaa(MedIdt) ;
196   if (numberOfMeshes == 0 )
197   {
198     QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
199                               QObject::tr("HOM_MED_FILE_2") );
200   }
201   if (numberOfMeshes > 1 )
202   {
203     QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
204                               QObject::tr("HOM_MED_FILE_3") );
205   }
206
207   QString nomMaillage= HOMARD_QT_COMMUN::LireNomMaillage(MedIdt,1);
208   MEDfermer(MedIdt);
209   return nomMaillage;
210 }
211 // =======================================================================
212 QString HOMARD_QT_COMMUN::LireNomMaillage(int MedIdt ,int MeshId)
213 // =======================================================================
214 {
215      QString NomMaillage=QString::null;
216      char maa[MED_TAILLE_NOM+1];
217      char desc[MED_TAILLE_DESC+1];
218      med_int mdim;
219      med_maillage type;
220
221      if ( MEDmaaInfo(MedIdt,MeshId,maa,&mdim,&type,desc) < 0 )
222      {
223       QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
224                                 QObject::tr("HOM_MED_FILE_4") );
225      }
226      else
227      {
228         NomMaillage=QString(maa);
229      }
230      return NomMaillage;
231 }
232
233
234 // =======================================================================
235 std::list<QString> HOMARD_QT_COMMUN::GetListeChamps(QString aFile)
236 // =======================================================================
237 {
238 // Il faut voir si plusieurs maillages
239
240    MESSAGE("HOMARD_QT_COMMUN::GetListeChamps");
241    std::list<QString> ListeChamp;
242
243    char *comp, *unit;
244    char nomcha  [MED_TAILLE_NOM+1];
245    med_type_champ typcha;
246    med_int ncomp;
247
248    SCRUTE(aFile.toStdString());
249    int MedIdt = HOMARD_QT_COMMUN::OuvrirFichier(aFile);
250    if ( MedIdt < 0 ) { return ListeChamp; }
251
252    // Le fichier Med est lisible
253    // Lecture du maillage
254
255    // Lecture du nombre de champs
256    med_int ncha = MEDnChamp(MedIdt, 0) ;
257    if (ncha < 1 )
258    {
259     QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
260                               QObject::tr("HOM_MED_FILE_5") );
261    MEDfermer(MedIdt);
262    return ListeChamp;
263    }
264
265    for (int i=0; i< ncha; i++)
266    {
267    /* Lecture du type du champ, des noms des composantes et du nom de l'unite*/
268       ncomp = MEDnChamp(MedIdt,i+1);
269       comp = (char*) malloc(ncomp*MED_TAILLE_PNOM+1);
270       unit = (char*) malloc(ncomp*MED_TAILLE_PNOM+1);
271       if ( MEDchampInfo(MedIdt,i+1,nomcha,&typcha,comp,unit,ncomp) < 0 )
272       {
273         QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
274                                   QObject::tr("HOM_MED_FILE_6") );
275    MEDfermer(MedIdt);
276           return ListeChamp;
277         }
278
279       ListeChamp.push_back(QString(nomcha));
280       free(comp);
281       free(unit);
282    }
283    MEDfermer(MedIdt);
284    return ListeChamp;
285 }
286
287 // ======================================================================================
288 std::list<QString> HOMARD_QT_COMMUN::GetListeComposants(QString aFile, QString aChamp)
289 // ======================================================================================
290 {
291    MESSAGE ( "GetListeComposants pour le fichier " << aFile.toStdString().c_str());
292    MESSAGE ( "GetListeComposants pour le champ " << aChamp.toStdString().c_str());
293
294    std::list<QString> ListeComposants;
295
296    char *comp, *unit;
297    char nomcha  [MED_TAILLE_NOM+1];
298    med_type_champ typcha;
299    med_int ncomp;
300
301    int MedIdt = HOMARD_QT_COMMUN::OuvrirFichier(aFile);
302    if ( MedIdt < 0 ) { return ListeComposants; }
303
304
305    // Lecture du nombre de champs
306    med_int ncha = MEDnChamp(MedIdt, 0) ;
307    if (ncha < 1 )
308    {
309     QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
310                               QObject::tr("HOM_MED_FILE_5") );
311    MEDfermer(MedIdt);
312    return ListeComposants;
313    }
314
315    for (int i=0; i< ncha; i++)
316    {
317    /* Lecture du type du champ, des noms des composantes et du nom de l'unite*/
318       ncomp = MEDnChamp(MedIdt,i+1);
319       comp = (char*) malloc(ncomp*MED_TAILLE_PNOM+1);
320       unit = (char*) malloc(ncomp*MED_TAILLE_PNOM+1);
321
322       if ( MEDchampInfo(MedIdt,i+1,nomcha,&typcha,comp,unit,ncomp) < 0 )
323       {
324         QMessageBox::critical( 0, QObject::tr("HOM_ERROR"),
325                                   QObject::tr("HOM_MED_FILE_6") );
326     MEDfermer(MedIdt);
327            return ListeComposants;
328        }
329        if ( QString(nomcha) != aChamp ) {
330    free(comp);
331           free (unit);
332           continue;}
333
334        for (int j = 0; j <ncomp; j++)
335        {
336              char cible[MED_TAILLE_PNOM +1];
337              strncpy(cible,comp+j*MED_TAILLE_PNOM,MED_TAILLE_PNOM );
338              cible[MED_TAILLE_PNOM ]='\0';
339              ListeComposants.push_back(QString(cible));
340        }
341        break;
342    }
343    free(comp);
344    free(unit);
345    MEDfermer(MedIdt);
346    return ListeComposants;
347 }