Salome HOME
Introduction of informations on the meshes
[modules/homard.git] / src / HOMARD_I / HOMARD_Gen_i.cxx
1 // Copyright (C) 2011-2012  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 "HOMARD_Gen_i.hxx"
21 #include "HOMARD_Cas_i.hxx"
22 #include "HOMARD_Hypothesis_i.hxx"
23 #include "HOMARD_Iteration_i.hxx"
24 #include "HOMARD_Boundary_i.hxx"
25 #include "HOMARD_Zone_i.hxx"
26 #include "HomardDriver.hxx"
27 #include "HOMARD_DriverTools.hxx"
28 #include "HomardMedCommun.h"
29
30 #include "HOMARD_version.h"
31
32 #include "utilities.h"
33 #include "Utils_SINGLETON.hxx"
34 #include "Utils_CorbaException.hxx"
35 #include "SALOMEDS_Tool.hxx"
36 #include "SALOME_LifeCycleCORBA.hxx"
37 #include "SALOMEconfig.h"
38 #include CORBA_CLIENT_HEADER(SALOME_ModuleCatalog)
39 #include CORBA_CLIENT_HEADER(SMESH_Gen)
40
41 #include <stdlib.h>
42 #include <sys/stat.h>
43 #include <dirent.h>
44 #include <string>
45 #include <iostream>
46 #include <iomanip>
47 #include <sys/stat.h>
48 #include <set>
49 #include <vector>
50 #include <stdio.h>
51
52 using  namespace std;
53
54 //=============================================================================
55 //functions
56 //=============================================================================
57 std::string RemoveTabulation( std::string theScript )
58 {
59   std::string::size_type aPos = 0;
60   while( aPos < theScript.length() )
61   {
62     aPos = theScript.find( "\n\t", aPos );
63     if( aPos == std::string::npos )
64       break;
65     theScript.replace( aPos, 2, "\n" );
66     aPos++;
67   }
68   return theScript;
69 }
70 //=============================================================================
71 /*!
72  *  standard constructor
73  */
74 //=============================================================================
75 HOMARD_Gen_i::HOMARD_Gen_i( CORBA::ORB_ptr orb,
76                             PortableServer::POA_ptr poa,
77                             PortableServer::ObjectId * contId,
78                             const char *instanceName,
79                             const char *interfaceName) :
80 Engines_Component_i(orb, poa, contId, instanceName, interfaceName)
81 {
82   MESSAGE("constructor");
83   _thisObj = this;
84   _id = _poa->activate_object(_thisObj);
85
86   myHomard = new ::HOMARD_Gen();
87   _NS = SINGLETON_<SALOME_NamingService>::Instance();
88   ASSERT(SINGLETON_<SALOME_NamingService>::IsAlreadyExisting());
89   _NS->init_orb(_orb);
90 }
91 //=================================
92 /*!
93  *  standard destructor
94  */
95 //================================
96 HOMARD_Gen_i::~HOMARD_Gen_i()
97 {
98 }
99 //=============================================================================
100 //=============================================================================
101 // Utilitaires pour l'étude
102 //=============================================================================
103 //=============================================================================
104 void HOMARD_Gen_i::addInStudy(SALOMEDS::Study_ptr theStudy)
105 {
106   ASSERT(!CORBA::is_nil(theStudy));
107   MESSAGE("addInStudy: ajout eventuel du composant HOMARD dans current study ID = " << GetCurrentStudyID()) ;
108   SALOMEDS::StudyBuilder_var myBuilder = theStudy->NewBuilder();
109
110   // Create SComponent labelled 'homard' if it doesn't already exit
111   SALOMEDS::SComponent_var homardFather = theStudy->FindComponent(ComponentDataType());
112   if (CORBA::is_nil(homardFather))
113   {
114     myBuilder->NewCommand();
115     MESSAGE("Add Component HOMARD");
116
117     bool aLocked = theStudy->GetProperties()->IsLocked();
118     if (aLocked) theStudy->GetProperties()->SetLocked(false);
119
120     homardFather = myBuilder->NewComponent(ComponentDataType());
121     SALOMEDS::GenericAttribute_var anAttr = myBuilder->FindOrCreateAttribute(homardFather,"AttributeName");
122     SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
123     CORBA::Object_var objVarN = _NS->Resolve("/Kernel/ModulCatalog");
124     SALOME_ModuleCatalog::ModuleCatalog_var Catalogue =
125                 SALOME_ModuleCatalog::ModuleCatalog::_narrow(objVarN);
126     SALOME_ModuleCatalog::Acomponent_var Comp = Catalogue->GetComponent(ComponentDataType());
127     if (!Comp->_is_nil())
128     {
129       aName->SetValue(ComponentDataType());
130     }
131
132     anAttr = myBuilder->FindOrCreateAttribute(homardFather,"AttributePixMap");
133     SALOMEDS::AttributePixMap_var aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
134     aPixmap->SetPixMap("HOMARD_2.png");
135     myBuilder->DefineComponentInstance(homardFather, HOMARD_Gen::_this());
136
137     if (aLocked) theStudy->GetProperties()->SetLocked(true);
138     myBuilder->CommitCommand();
139   }
140 }
141 //=============================================================================
142 void HOMARD_Gen_i::SetCurrentStudy(SALOMEDS::Study_ptr theStudy)
143 {
144   MESSAGE("SetCurrentStudy: current study Id = " << GetCurrentStudyID());
145   myCurrentStudy = SALOMEDS::Study::_duplicate(theStudy);
146   this->addInStudy(myCurrentStudy);
147 }
148 //=============================================================================
149 SALOMEDS::Study_ptr HOMARD_Gen_i::GetCurrentStudy()
150 //=============================================================================
151 {
152   MESSAGE("GetCurrentStudy: study Id = " << GetCurrentStudyID());
153   return SALOMEDS::Study::_duplicate(myCurrentStudy);
154 }
155 //=============================================================================
156 CORBA::Long HOMARD_Gen_i::GetCurrentStudyID()
157 //=============================================================================
158 {
159   return myCurrentStudy->_is_nil() ? -1 : myCurrentStudy->StudyId();
160 }
161 //=============================================================================
162 //=============================================================================
163
164 //=============================================================================
165 //=============================================================================
166 // Utilitaires pour l'iteration
167 //=============================================================================
168 //=============================================================================
169 void HOMARD_Gen_i::SetEtatIter(const char* nomIter, const CORBA::Boolean EtatCalcul)
170 //=====================================================================================
171 {
172   MESSAGE( "SetEtatIter : affectation de l etat " << EtatCalcul << " a l iteration " << nomIter );
173   HOMARD::HOMARD_Iteration_var myIteration = myContextMap[GetCurrentStudyID()]._mesIterations[nomIter];
174   if (CORBA::is_nil(myIteration))
175   {
176       SALOME::ExceptionStruct es;
177       es.type = SALOME::BAD_PARAM;
178       es.text = "Invalid Iteration ";
179       throw SALOME::SALOME_Exception(es);
180       return ;
181   };
182
183   myIteration->SetEtat(EtatCalcul);
184
185   SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
186   SALOMEDS::SObject_var aIterSO = SALOMEDS::SObject::_narrow(myCurrentStudy->FindObjectIOR(_orb->object_to_string(myIteration)));
187
188   int number = myIteration->GetNumber() ;
189   const char* icone ;
190   if ( number == 0 )
191     icone = "iter0.png" ;
192   else if (EtatCalcul)
193     icone = "iter_calculee.png" ;
194   else
195     icone = "iter_non_calculee.png" ;
196   PublishInStudyAttr(aStudyBuilder, aIterSO, NULL , NULL, icone, NULL) ;
197
198   aStudyBuilder->CommitCommand();
199
200 }
201 //=============================================================================
202 //=============================================================================
203 //
204 //=============================================================================
205 //=============================================================================
206 // Destruction des structures identifiees par leurs noms
207 //=============================================================================
208 //=============================================================================
209 CORBA::Long HOMARD_Gen_i::DeleteBoundary(const char* BoundaryName)
210 {
211   MESSAGE ( "DeleteBoundary : BoundaryName = " << BoundaryName );
212   HOMARD::HOMARD_Boundary_var myBoundary = myContextMap[GetCurrentStudyID()]._mesBoundarys[BoundaryName];
213   if (CORBA::is_nil(myBoundary))
214   {
215 //     const char* message = "Invalid Boundary " ;
216 //     SALOMEException(message) ;
217     SALOME::ExceptionStruct es;
218     es.type = SALOME::BAD_PARAM;
219     es.text = "Invalid Boundary ";
220     throw SALOME::SALOME_Exception(es);
221     return 1 ;
222   };
223
224 // On verifie que la frontiere n'est plus utilisee
225   HOMARD::listeCases* maListe = GetAllCasesName();
226   int numberOfCases = maListe->length();
227   MESSAGE ( ".. Nombre de cas = " << numberOfCases );
228   std::string CaseName ;
229   HOMARD::ListBoundaryGroupType* ListBoundaryGroupType ;
230   int numberOfitems ;
231   HOMARD::HOMARD_Cas_var myCase ;
232   for (int NumeCas = 0; NumeCas< numberOfCases; NumeCas++)
233   {
234     CaseName = std::string((*maListe)[NumeCas]);
235     MESSAGE ( "... Examen du cas = " << CaseName.c_str() );
236     myCase = myContextMap[GetCurrentStudyID()]._mesCas[CaseName];
237     ASSERT(!CORBA::is_nil(myCase));
238     ListBoundaryGroupType = myCase->GetBoundaryGroup();
239     numberOfitems = ListBoundaryGroupType->length();
240     MESSAGE ( "... number of string for Boundary+Group = " << numberOfitems);
241     for (int NumBoundary = 0; NumBoundary< numberOfitems; NumBoundary=NumBoundary+2)
242     {
243       if ( std::string((*ListBoundaryGroupType)[NumBoundary]) == BoundaryName )
244       {
245         SALOME::ExceptionStruct es;
246         es.type = SALOME::BAD_PARAM;
247         es.text = "This boundary is used in a case and cannot be deleted.";
248         throw SALOME::SALOME_Exception(es);
249         return 2 ;
250       };
251     };
252   }
253
254   // comme on a un _var comme pointeur CORBA, on ne se preoccupe pas du delete
255   myContextMap[GetCurrentStudyID()]._mesBoundarys.erase(BoundaryName);
256   SALOMEDS::Study::ListOfSObject_var listSO = myCurrentStudy->FindObjectByName(BoundaryName, ComponentDataType());
257   SALOMEDS::SObject_var aSO =listSO[0];
258   SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
259   myCurrentStudy->NewBuilder()->RemoveObjectWithChildren(aSO);
260
261   return 0 ;
262 }
263 //=============================================================================
264 CORBA::Long HOMARD_Gen_i::DeleteCase(const char* nomCas)
265 {
266   // Pour detruire un cas
267   MESSAGE ( "DeleteCase : nomCas = " << nomCas );
268   HOMARD::HOMARD_Cas_var myCase = myContextMap[GetCurrentStudyID()]._mesCas[nomCas];
269   if (CORBA::is_nil(myCase))
270   {
271     SALOME::ExceptionStruct es;
272     es.type = SALOME::BAD_PARAM;
273     es.text = "Invalid Case Context ";
274     throw SALOME::SALOME_Exception(es);
275     return 1;
276   };
277   // On commence par detruire toutes les iterations en partant de l'initiale et y compris elle
278   CORBA::String_var nomIter = myCase->GetIter0Name();
279   CORBA::Long Option = 0 ;
280   if ( DeleteIterationOption(nomIter, Option) != 0 )
281   {
282     return 2;
283   };
284
285   // comme on a un _var comme pointeur CORBA, on ne se preoccupe pas du delete
286   myContextMap[GetCurrentStudyID()]._mesCas.erase(nomCas);
287   SALOMEDS::Study::ListOfSObject_var listSO = myCurrentStudy->FindObjectByName(nomCas, ComponentDataType());
288   SALOMEDS::SObject_var aSO =listSO[0];
289   SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
290   myCurrentStudy->NewBuilder()->RemoveObjectWithChildren(aSO);
291
292   return 0 ;
293 }
294 //=============================================================================
295 CORBA::Long HOMARD_Gen_i::DeleteHypo(const char* nomHypo)
296 {
297   MESSAGE ( "DeleteHypo : nomHypo = " << nomHypo );
298   HOMARD::HOMARD_Hypothesis_var myHypo = myContextMap[GetCurrentStudyID()]._mesHypotheses[nomHypo];
299   if (CORBA::is_nil(myHypo))
300   {
301     SALOME::ExceptionStruct es;
302     es.type = SALOME::BAD_PARAM;
303     es.text = "Invalid Hypothesis ";
304     throw SALOME::SALOME_Exception(es);
305     return 1 ;
306   };
307
308 // On verifie que l'hypothese n'est plus utilisee
309   HOMARD::listeIters* maListeIter = myHypo->GetIterations();
310   int numberOfIter = maListeIter->length();
311   if ( numberOfIter > 0 )
312   {
313     SALOME::ExceptionStruct es;
314     es.type = SALOME::BAD_PARAM;
315     es.text = "This hypothesis is used and cannot be deleted.";
316     throw SALOME::SALOME_Exception(es);
317     return 2 ;
318   };
319
320   // suppression du lien avec les zones eventuelles
321   HOMARD::listeZonesHypo* maListe = myHypo->GetZones();
322   int numberOfZones = maListe->length();
323   MESSAGE ( ".. Nombre de zones = " << numberOfZones );
324   for (int NumeZone = 0; NumeZone< numberOfZones; NumeZone++)
325   {
326     std::string ZoneName = std::string((*maListe)[NumeZone]);
327     MESSAGE ( ".. suppression du lien avec la zone = " << ZoneName.c_str() );
328     DissociateHypoZone(nomHypo, ZoneName.c_str()) ;
329     NumeZone += 1 ;
330   }
331
332   // comme on a un _var comme pointeur CORBA, on ne se preoccupe pas du delete
333   myContextMap[GetCurrentStudyID()]._mesHypotheses.erase(nomHypo);
334   SALOMEDS::Study::ListOfSObject_var listSO = myCurrentStudy->FindObjectByName(nomHypo, ComponentDataType());
335   SALOMEDS::SObject_var aSO =listSO[0];
336   SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
337   myCurrentStudy->NewBuilder()->RemoveObjectWithChildren(aSO);
338
339   return 0 ;
340 }
341 //=============================================================================
342 CORBA::Long HOMARD_Gen_i::DeleteIteration(const char* nomIter)
343 {
344   // Pour detruire une iteration courante
345   MESSAGE ( "DeleteIteration : nomIter = " << nomIter );
346   CORBA::Long Option = 1 ;
347   return DeleteIterationOption(nomIter, Option);
348 }
349 //=============================================================================
350 CORBA::Long HOMARD_Gen_i::DeleteIterationOption(const char* nomIter, CORBA::Long Option)
351 {
352   //  Option = 0 : On autorise la destruction de l'iteration 0
353   //  Option = 1 : On interdit la destruction de l'iteration 0
354   MESSAGE ( "DeleteIterationOption : nomIter = " << nomIter << ", avec option = " << Option );
355   HOMARD::HOMARD_Iteration_var myIteration = myContextMap[GetCurrentStudyID()]._mesIterations[nomIter];
356   if (CORBA::is_nil(myIteration))
357   {
358     SALOME::ExceptionStruct es;
359     es.type = SALOME::BAD_PARAM;
360     es.text = "Invalid Iteration ";
361     throw SALOME::SALOME_Exception(es);
362     return 1 ;
363   };
364
365   int numero = myIteration->GetNumber();
366   MESSAGE ( "DeleteIterationOption : numero = " << numero );
367   if ( numero == 0 and Option == 1 )
368   {
369     SALOME::ExceptionStruct es;
370     es.type = SALOME::BAD_PARAM;
371     es.text = "This iteration cannot be deleted.";
372     throw SALOME::SALOME_Exception(es);
373     return 2 ;
374   };
375
376   // On detruit recursivement toutes les filles
377   HOMARD::listeIterFilles* maListe = myIteration->GetIterations();
378   int numberOfIter = maListe->length();
379   for (int NumeIter = 0; NumeIter< numberOfIter; NumeIter++)
380   {
381     std::string nomIterFille = std::string((*maListe)[NumeIter]);
382     MESSAGE ( ".. appel recursif de DeleteIterationOption pour nomIter = " << nomIterFille.c_str() );
383     DeleteIterationOption(nomIterFille.c_str(), Option);
384   }
385
386   // On arrive ici pour une iteration sans fille
387   MESSAGE ( "Destruction effective de " << nomIter );
388   // On commence par invalider l'iteration pour faire le menage des dependances
389   // et des publications dans SMESH
390   InvalideIter(nomIter) ;
391
392   // Retrait dans la descendance de l'iteration parent
393   if ( numero > 0 )
394   {
395     const char* nomIterationParent = myIteration->GetIterParentName();
396     MESSAGE ( "Retrait dans la descendance de nomIterationParent " << nomIterationParent );
397     HOMARD::HOMARD_Iteration_var myIterationParent = myContextMap[GetCurrentStudyID()]._mesIterations[nomIterationParent];
398     if (CORBA::is_nil(myIterationParent))
399     {
400       SALOME::ExceptionStruct es;
401       es.type = SALOME::BAD_PARAM;
402       es.text = "Invalid Iteration ";
403       throw SALOME::SALOME_Exception(es);
404       return 3 ;
405     };
406     myIterationParent->UnLinkNextIteration(nomIter);
407   }
408
409   // suppression du lien avec l'hypothese
410   if ( numero > 0 )
411   {
412     const char* nomHypo = myIteration->GetHypoName();
413     HOMARD::HOMARD_Hypothesis_var myHypo = myContextMap[GetCurrentStudyID()]._mesHypotheses[nomHypo];
414     ASSERT(!CORBA::is_nil(myHypo));
415     myHypo->UnLinkIteration(nomIter);
416   }
417
418   // comme on a un _var comme pointeur CORBA, on ne se preoccupe pas du delete
419   myContextMap[GetCurrentStudyID()]._mesIterations.erase(nomIter);
420   SALOMEDS::Study::ListOfSObject_var listSO = myCurrentStudy->FindObjectByName(nomIter, ComponentDataType());
421   SALOMEDS::SObject_var aSO =listSO[0];
422   SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
423   myCurrentStudy->NewBuilder()->RemoveObjectWithChildren(aSO);
424   // on peut aussi faire RemoveObject
425 //   MESSAGE ( "Au final" );
426 //   HOMARD::listeIterations* Liste = GetAllIterationsName() ;
427 //   numberOfIter = Liste->length();
428 //   for (int NumeIter = 0; NumeIter< numberOfIter; NumeIter++)
429 //   {
430 //       std::string nomIterFille = std::string((*Liste)[NumeIter]);
431 //       MESSAGE ( ".. nomIter = " << nomIterFille.c_str() );
432 //   }
433
434   return 0 ;
435 }
436 //=============================================================================
437 CORBA::Long HOMARD_Gen_i::DeleteZone(const char* nomZone)
438 {
439   MESSAGE ( "DeleteZone : nomZone = " << nomZone );
440   HOMARD::HOMARD_Zone_var myZone = myContextMap[GetCurrentStudyID()]._mesZones[nomZone];
441   if (CORBA::is_nil(myZone))
442   {
443     SALOME::ExceptionStruct es;
444     es.type = SALOME::BAD_PARAM;
445     es.text = "Invalid Zone ";
446     throw SALOME::SALOME_Exception(es);
447     return 1 ;
448   };
449
450 // On verifie que la zone n'est plus utilisee
451   HOMARD::listeHypo* maListe = myZone->GetHypo();
452   int numberOfHypo = maListe->length();
453   MESSAGE ( ".. Nombre d'hypotheses = " << numberOfHypo );
454   if ( numberOfHypo > 0 )
455   {
456     SALOME::ExceptionStruct es;
457     es.type = SALOME::BAD_PARAM;
458     es.text = "This zone is used and cannot be deleted.";
459     throw SALOME::SALOME_Exception(es);
460     return 2 ;
461   };
462 //
463   // comme on a un _var comme pointeur CORBA, on ne se preoccupe pas du delete
464   myContextMap[GetCurrentStudyID()]._mesZones.erase(nomZone);
465   SALOMEDS::Study::ListOfSObject_var listSO = myCurrentStudy->FindObjectByName(nomZone, ComponentDataType());
466   SALOMEDS::SObject_var aSO =listSO[0];
467   SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
468   myCurrentStudy->NewBuilder()->RemoveObjectWithChildren(aSO);
469
470   return 0 ;
471 }
472 //=============================================================================
473 //=============================================================================
474 //
475 //=============================================================================
476 //=============================================================================
477 // Invalidation des structures identifiees par leurs noms
478 //=============================================================================
479 //=============================================================================
480 void HOMARD_Gen_i::InvalideBoundary(const char* BoundaryName)
481 {
482   MESSAGE( "InvalideBoundary : BoundaryName = " << BoundaryName  );
483   HOMARD::HOMARD_Boundary_var myBoundary = myContextMap[GetCurrentStudyID()]._mesBoundarys[BoundaryName];
484   if (CORBA::is_nil(myBoundary))
485   {
486     SALOME::ExceptionStruct es;
487     es.type = SALOME::BAD_PARAM;
488     es.text = "Invalid Boundary ";
489     throw SALOME::SALOME_Exception(es);
490     return ;
491   }
492   else
493   {
494     SALOME::ExceptionStruct es;
495     es.type = SALOME::BAD_PARAM;
496     es.text = "No change is allowed in a boundary. Ask for evolution.";
497     throw SALOME::SALOME_Exception(es);
498     return ;
499   };
500 }
501 //=============================================================================
502 void HOMARD_Gen_i::InvalideHypo(const char* nomHypo)
503 {
504   MESSAGE( "InvalideHypo : nomHypo    = " << nomHypo  );
505   HOMARD::HOMARD_Hypothesis_var myHypo = myContextMap[GetCurrentStudyID()]._mesHypotheses[nomHypo];
506   if (CORBA::is_nil(myHypo))
507   {
508       SALOME::ExceptionStruct es;
509       es.type = SALOME::BAD_PARAM;
510       es.text = "Invalid Hypothesis ";
511       throw SALOME::SALOME_Exception(es);
512       return ;
513   };
514
515   HOMARD::listeIters* maListe = myHypo->GetIterations();
516   int numberOfIter = maListe->length();
517   for (int NumeIter = 0; NumeIter< numberOfIter; NumeIter++)
518   {
519       std::string nomIter = std::string((*maListe)[NumeIter]);
520       MESSAGE( ".. nomIter = " << nomIter );
521       InvalideIter(nomIter.c_str());
522   }
523 }
524 //=============================================================================
525 void HOMARD_Gen_i::InvalideIter(const char* nomIter)
526 {
527   MESSAGE("InvalideIter : nomIter = " << nomIter);
528   HOMARD::HOMARD_Iteration_var myIteration = myContextMap[GetCurrentStudyID()]._mesIterations[nomIter];
529   if (CORBA::is_nil(myIteration))
530   {
531       SALOME::ExceptionStruct es;
532       es.type = SALOME::BAD_PARAM;
533       es.text = "Invalid Iteration ";
534       throw SALOME::SALOME_Exception(es);
535       return ;
536   };
537
538   HOMARD::listeIterFilles* maListe = myIteration->GetIterations();
539   int numberOfIter = maListe->length();
540   for (int NumeIter = 0; NumeIter< numberOfIter; NumeIter++)
541   {
542       std::string nomIterFille = std::string((*maListe)[NumeIter]);
543       MESSAGE ( ".. appel recursif de InvalideIter pour nomIter = " << nomIterFille.c_str() );
544       InvalideIter(nomIterFille.c_str());
545   }
546
547   // On arrive ici pour une iteration sans fille
548   MESSAGE ( "Invalidation effective de " << nomIter );
549   SALOMEDS::SObject_var aIterSO = SALOMEDS::SObject::_narrow(myCurrentStudy->FindObjectIOR(_orb->object_to_string(myIteration)));
550   SALOMEDS::ChildIterator_var  aIter = myCurrentStudy->NewChildIterator(aIterSO);
551   for (; aIter->More(); aIter->Next())
552   {
553       SALOMEDS::SObject_var so = aIter->Value();
554       SALOMEDS::GenericAttribute_var anAttr;
555       if (!so->FindAttribute(anAttr, "AttributeComment")) continue;
556       SALOMEDS::AttributeComment_var aCommentAttr = SALOMEDS::AttributeComment::_narrow(anAttr);
557       std::string value (aCommentAttr->Value());
558       if(value == std::string("HypoHomard")) continue;
559       SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
560       aStudyBuilder->RemoveObject(so);
561   }
562
563   int number = myIteration->GetNumber();
564   if ( number > 0 )
565   {
566     SetEtatIter(nomIter,false);
567     const char * nomCas = myIteration->GetCaseName();
568     HOMARD::HOMARD_Cas_var myCase = myContextMap[GetCurrentStudyID()]._mesCas[nomCas];
569     if (CORBA::is_nil(myCase))
570     {
571         SALOME::ExceptionStruct es;
572         es.type = SALOME::BAD_PARAM;
573         es.text = "Invalid Case Context ";
574         throw SALOME::SALOME_Exception(es);
575         return ;
576     };
577     const char* dirCase    = myCase->GetDirName();
578     const char* nomDir     = myIteration->GetDirName();
579     const char* nomFichier = myIteration->GetMeshFile();
580     std::string commande= "rm -rf " + std::string(dirCase) + "/" + std::string(nomDir);
581     commande = commande + ";rm -rf " + std::string(nomFichier);
582     MESSAGE ( "commande = " << commande );
583     if ((system(commande.c_str())) != 0)
584     {
585           SALOME::ExceptionStruct es;
586           es.type = SALOME::BAD_PARAM;
587           es.text = "Menage du repertoire de calcul impossible" ;
588           throw SALOME::SALOME_Exception(es);
589           return ;
590     }
591   // Suppression du maillage publie dans SMESH
592     const char* MeshName = myIteration->GetMeshName();
593     DeleteResultInSmesh(nomFichier, MeshName) ;
594   };
595
596 }
597 //=============================================================================
598 void HOMARD_Gen_i::InvalideIterInfo(const char* nomIter)
599 {
600   MESSAGE("InvalideIterInfo : nomIter = " << nomIter);
601   HOMARD::HOMARD_Iteration_var myIteration = myContextMap[GetCurrentStudyID()]._mesIterations[nomIter];
602   if (CORBA::is_nil(myIteration))
603   {
604       SALOME::ExceptionStruct es;
605       es.type = SALOME::BAD_PARAM;
606       es.text = "Invalid Iteration ";
607       throw SALOME::SALOME_Exception(es);
608       return ;
609   };
610
611   SALOMEDS::SObject_var aIterSO = SALOMEDS::SObject::_narrow(myCurrentStudy->FindObjectIOR(_orb->object_to_string(myIteration)));
612   SALOMEDS::ChildIterator_var  aIter = myCurrentStudy->NewChildIterator(aIterSO);
613   for (; aIter->More(); aIter->Next())
614   {
615       SALOMEDS::SObject_var so = aIter->Value();
616       SALOMEDS::GenericAttribute_var anAttr;
617       if (!so->FindAttribute(anAttr, "AttributeComment")) continue;
618       SALOMEDS::AttributeComment_var aCommentAttr = SALOMEDS::AttributeComment::_narrow(anAttr);
619       std::string value (aCommentAttr->Value());
620 /*      MESSAGE("... value = " << value);*/
621       if( (value == std::string("logInfo")) or ( value == std::string("SummaryInfo")) )
622       {
623         SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
624         aStudyBuilder->RemoveObject(so);
625       }
626   }
627
628   const char * nomCas = myIteration->GetCaseName();
629   HOMARD::HOMARD_Cas_var myCase = myContextMap[GetCurrentStudyID()]._mesCas[nomCas];
630   if (CORBA::is_nil(myCase))
631   {
632       SALOME::ExceptionStruct es;
633       es.type = SALOME::BAD_PARAM;
634       es.text = "Invalid Case Context ";
635       throw SALOME::SALOME_Exception(es);
636       return ;
637   };
638   const char* dirCase  = myCase->GetDirName();
639   const char* nomDir   = myIteration->GetDirName();
640   std::string commande = "rm -f " + std::string(dirCase) + "/" + std::string(nomDir) + "/info* " ;
641   commande += std::string(dirCase) + "/" + std::string(nomDir) + "/Liste.*info" ;
642 /*  MESSAGE ( "commande = " << commande );*/
643   if ((system(commande.c_str())) != 0)
644   {
645         SALOME::ExceptionStruct es;
646         es.type = SALOME::BAD_PARAM;
647         es.text = "Menage du repertoire de calcul impossible" ;
648         throw SALOME::SALOME_Exception(es);
649         return ;
650   }
651
652 }
653 //=============================================================================
654 void HOMARD_Gen_i::InvalideZone(const char* ZoneName)
655 {
656   MESSAGE( "InvalideZone : ZoneName    = " << ZoneName );
657   HOMARD::HOMARD_Zone_var myZone = myContextMap[GetCurrentStudyID()]._mesZones[ZoneName];
658   if (CORBA::is_nil(myZone))
659   {
660       SALOME::ExceptionStruct es;
661       es.type = SALOME::BAD_PARAM;
662       es.text = "Invalid Zone ";
663       throw SALOME::SALOME_Exception(es);
664       return ;
665   };
666   HOMARD::listeHypo* maListe = myZone->GetHypo();
667   int numberOfHypo = maListe->length();
668   MESSAGE( ".. numberOfHypo = " << numberOfHypo );
669   for (int NumeHypo = 0; NumeHypo< numberOfHypo; NumeHypo++)
670   {
671       std::string nomHypo = std::string((*maListe)[NumeHypo]);
672       MESSAGE( ".. nomHypo = " << nomHypo );
673       InvalideHypo(nomHypo.c_str());
674   }
675 }
676 //=============================================================================
677 //=============================================================================
678 //
679 //=============================================================================
680 //=============================================================================
681 // Association de lien entre des structures identifiees par leurs noms
682 //=============================================================================
683 //=============================================================================
684 void HOMARD_Gen_i::AssociateCaseIter(const char* nomCas, const char* nomIter, const char* labelIter)
685 {
686   MESSAGE( "AssociateCaseIter : " << nomCas << " ," << nomIter << ","  << labelIter );
687
688   HOMARD::HOMARD_Cas_var myCase = myContextMap[GetCurrentStudyID()]._mesCas[nomCas];
689   if (CORBA::is_nil(myCase))
690   {
691       SALOME::ExceptionStruct es;
692       es.type = SALOME::BAD_PARAM;
693       es.text = "Invalid Case ";
694       throw SALOME::SALOME_Exception(es);
695       return ;
696   };
697
698   HOMARD::HOMARD_Iteration_var myIteration = myContextMap[GetCurrentStudyID()]._mesIterations[nomIter];
699   if (CORBA::is_nil(myIteration))
700   {
701       SALOME::ExceptionStruct es;
702       es.type = SALOME::BAD_PARAM;
703       es.text = "Invalid iteration ";
704       throw SALOME::SALOME_Exception(es);
705       return ;
706   };
707
708   SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
709   SALOMEDS::SObject_var aCasSO = SALOMEDS::SObject::_narrow(myCurrentStudy->FindObjectIOR(_orb->object_to_string(myCase)));
710   if (CORBA::is_nil(aCasSO))
711   {
712       SALOME::ExceptionStruct es;
713       es.type = SALOME::BAD_PARAM;
714       es.text = "Invalid Case ";
715       throw SALOME::SALOME_Exception(es);
716       return ;
717   };
718
719   aStudyBuilder->NewCommand();
720   SALOMEDS::SObject_var newStudyIter = aStudyBuilder->NewObject(aCasSO);
721   PublishInStudyAttr(aStudyBuilder, newStudyIter, nomIter , labelIter,
722                      "iter_non_calculee.png", _orb->object_to_string(myIteration)) ;
723   aStudyBuilder->CommitCommand();
724
725   myCase->AddIteration(nomIter);
726   myIteration->SetCaseName(nomCas);
727 }
728 //=====================================================================================
729 void HOMARD_Gen_i::AssociateHypoZone(const char* nomHypothesis, const char* ZoneName, CORBA::Long TypeUse)
730 {
731   MESSAGE ( "AssociateHypoZone : nomHypo = " << nomHypothesis << ", ZoneName= " << ZoneName << ", TypeUse = " << TypeUse);
732
733   HOMARD::HOMARD_Hypothesis_var myHypo = myContextMap[GetCurrentStudyID()]._mesHypotheses[nomHypothesis];
734   ASSERT(!CORBA::is_nil(myHypo));
735   SALOMEDS::SObject_var aHypoSO = SALOMEDS::SObject::_narrow(myCurrentStudy->FindObjectIOR(_orb->object_to_string(myHypo)));
736   ASSERT(!CORBA::is_nil(aHypoSO));
737
738   HOMARD::HOMARD_Zone_var myZone = myContextMap[GetCurrentStudyID()]._mesZones[ZoneName];
739   ASSERT(!CORBA::is_nil(myZone));
740   SALOMEDS::SObject_var aZoneSO = SALOMEDS::SObject::_narrow(myCurrentStudy->FindObjectIOR(_orb->object_to_string(myZone)));
741   ASSERT(!CORBA::is_nil(aZoneSO));
742
743   SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
744
745   aStudyBuilder->NewCommand();
746
747   SALOMEDS::SObject_var aSubSO = aStudyBuilder->NewObject(aHypoSO);
748   aStudyBuilder->Addreference(aSubSO, aZoneSO);
749
750   aStudyBuilder->CommitCommand();
751
752   myZone->AddHypo(nomHypothesis);
753   myHypo->AddZone0(ZoneName, TypeUse);
754 };
755 //=============================================================================
756 void HOMARD_Gen_i::AssociateIterHypo(const char* nomIter, const char* nomHypo)
757 {
758   MESSAGE("AssociateIterHypo : nomHypo = " << nomHypo << " nomIter = " << nomIter);
759
760   HOMARD::HOMARD_Hypothesis_var myHypo = myContextMap[GetCurrentStudyID()]._mesHypotheses[nomHypo];
761   ASSERT(!CORBA::is_nil(myHypo));
762   SALOMEDS::SObject_var aHypoSO = SALOMEDS::SObject::_narrow(myCurrentStudy->FindObjectIOR(_orb->object_to_string(myHypo)));
763   ASSERT(!CORBA::is_nil(aHypoSO));
764
765   HOMARD::HOMARD_Iteration_var myIteration = myContextMap[GetCurrentStudyID()]._mesIterations[nomIter];
766   ASSERT(!CORBA::is_nil(myIteration));
767   SALOMEDS::SObject_var aIterSO = SALOMEDS::SObject::_narrow(myCurrentStudy->FindObjectIOR(_orb->object_to_string(myIteration)));
768   ASSERT(!CORBA::is_nil(aIterSO));
769
770   SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
771
772   aStudyBuilder->NewCommand();
773
774   SALOMEDS::SObject_var aSubSO = aStudyBuilder->NewObject(aIterSO);
775   aStudyBuilder->Addreference(aSubSO, aHypoSO);
776
777   aStudyBuilder->CommitCommand();
778
779   myIteration->SetHypoName(nomHypo);
780   myHypo->LinkIteration(nomIter);
781 };
782 //=============================================================================
783 //=============================================================================
784 //
785 //=============================================================================
786 //=============================================================================
787 // Dissociation de lien entre des structures identifiees par leurs noms
788 //=============================================================================
789 //=============================================================================
790 void HOMARD_Gen_i::DissociateHypoZone(const char* nomHypothesis, const char* ZoneName)
791 {
792   MESSAGE ( "DissociateHypoZone : ZoneName= " << ZoneName << ", nomHypo = " << nomHypothesis);
793
794   HOMARD::HOMARD_Hypothesis_var myHypo = myContextMap[GetCurrentStudyID()]._mesHypotheses[nomHypothesis];
795   ASSERT(!CORBA::is_nil(myHypo));
796   SALOMEDS::SObject_var aHypoSO = SALOMEDS::SObject::_narrow(myCurrentStudy->FindObjectIOR(_orb->object_to_string(myHypo)));
797   ASSERT(!CORBA::is_nil(aHypoSO));
798
799   HOMARD::HOMARD_Zone_var myZone = myContextMap[GetCurrentStudyID()]._mesZones[ZoneName];
800   ASSERT(!CORBA::is_nil(myZone));
801   SALOMEDS::SObject_var aZoneSO = SALOMEDS::SObject::_narrow(myCurrentStudy->FindObjectIOR(_orb->object_to_string(myZone)));
802   ASSERT(!CORBA::is_nil(aZoneSO));
803
804   SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
805
806   SALOMEDS::ChildIterator_var it = myCurrentStudy->NewChildIterator(aHypoSO);
807   for (; it->More(); it->Next())
808   {
809     SALOMEDS::SObject_var aHypObj = it->Value();
810     SALOMEDS::SObject_var ptrObj;
811     if (aHypObj->ReferencedObject(ptrObj))
812     {
813       if (std::string(ptrObj->GetName()) == std::string(aZoneSO->GetName()))
814       {
815         aStudyBuilder->NewCommand();
816         aStudyBuilder->RemoveObject(aHypObj);
817         aStudyBuilder->CommitCommand();
818         break;
819       }
820     }
821   }
822
823   myZone->SupprHypo(nomHypothesis);
824   myHypo->SupprZone(ZoneName);
825 };
826 //=============================================================================
827 //=============================================================================
828 //
829
830 //=============================================================================
831 //=============================================================================
832 // Recuperation des listes
833 //=============================================================================
834 //=============================================================================
835 HOMARD::listeCases* HOMARD_Gen_i::GetAllCasesName()
836 {
837   MESSAGE("GetAllCasesName");
838   IsValidStudy () ;
839
840   HOMARD::listeCases_var ret = new HOMARD::listeCases;
841   ret->length(myContextMap[GetCurrentStudyID()]._mesCas.size());
842   std::map<std::string, HOMARD::HOMARD_Cas_var>::const_iterator it;
843   int i = 0;
844   for (it = myContextMap[GetCurrentStudyID()]._mesCas.begin();
845   it != myContextMap[GetCurrentStudyID()]._mesCas.end(); it++)
846   {
847     ret[i++] = CORBA::string_dup((*it).first.c_str());
848   }
849
850   return ret._retn();
851 }
852 //=============================================================================
853 HOMARD::listeHypotheses* HOMARD_Gen_i::GetAllHypothesesName()
854 {
855   MESSAGE("GetAllHypothesesName");
856   IsValidStudy () ;
857
858   HOMARD::listeHypotheses_var ret = new HOMARD::listeHypotheses;
859   ret->length(myContextMap[GetCurrentStudyID()]._mesHypotheses.size());
860   std::map<std::string, HOMARD::HOMARD_Hypothesis_var>::const_iterator it;
861   int i = 0;
862   for (it = myContextMap[GetCurrentStudyID()]._mesHypotheses.begin();
863   it != myContextMap[GetCurrentStudyID()]._mesHypotheses.end(); it++)
864   {
865     ret[i++] = CORBA::string_dup((*it).first.c_str());
866   }
867
868   return ret._retn();
869 }
870 //=============================================================================
871 HOMARD::listeZones* HOMARD_Gen_i::GetAllZonesName()
872 {
873   MESSAGE("GetAllZonesName");
874   IsValidStudy () ;
875
876   HOMARD::listeZones_var ret = new HOMARD::listeZones;
877   ret->length(myContextMap[GetCurrentStudyID()]._mesZones.size());
878   std::map<std::string, HOMARD::HOMARD_Zone_var>::const_iterator it;
879   int i = 0;
880   for (it = myContextMap[GetCurrentStudyID()]._mesZones.begin();
881   it != myContextMap[GetCurrentStudyID()]._mesZones.end(); it++)
882   {
883     ret[i++] = CORBA::string_dup((*it).first.c_str());
884   }
885
886   return ret._retn();
887 }
888 //=============================================================================
889 HOMARD::listeIterations* HOMARD_Gen_i::GetAllIterationsName()
890 {
891   MESSAGE("GetAllIterationsName");
892   IsValidStudy () ;
893
894   HOMARD::listeIterations_var ret = new HOMARD::listeIterations;
895   ret->length(myContextMap[GetCurrentStudyID()]._mesIterations.size());
896   std::map<std::string, HOMARD::HOMARD_Iteration_var>::const_iterator it;
897   int i = 0;
898   for (it = myContextMap[GetCurrentStudyID()]._mesIterations.begin();
899   it != myContextMap[GetCurrentStudyID()]._mesIterations.end(); it++)
900   {
901     ret[i++] = CORBA::string_dup((*it).first.c_str());
902   }
903
904   return ret._retn();
905 }
906 //=============================================================================
907 HOMARD::listeBoundarys* HOMARD_Gen_i::GetAllBoundarysName()
908 {
909   MESSAGE("GetAllBoundarysName");
910   IsValidStudy () ;
911
912   HOMARD::listeBoundarys_var ret = new HOMARD::listeBoundarys;
913   ret->length(myContextMap[GetCurrentStudyID()]._mesBoundarys.size());
914   std::map<std::string, HOMARD::HOMARD_Boundary_var>::const_iterator it;
915   int i = 0;
916   for (it = myContextMap[GetCurrentStudyID()]._mesBoundarys.begin();
917   it != myContextMap[GetCurrentStudyID()]._mesBoundarys.end(); it++)
918   {
919     ret[i++] = CORBA::string_dup((*it).first.c_str());
920   }
921
922   return ret._retn();
923 }
924 //=============================================================================
925 //=============================================================================
926
927 //=============================================================================
928 //=============================================================================
929 // Recuperation des structures identifiees par leurs noms
930 //=============================================================================
931 //=============================================================================
932 HOMARD::HOMARD_Cas_ptr HOMARD_Gen_i::GetCase(const char* nomCas)
933 {
934   HOMARD::HOMARD_Cas_var myCase = myContextMap[GetCurrentStudyID()]._mesCas[nomCas];
935   ASSERT(!CORBA::is_nil(myCase));
936   return HOMARD::HOMARD_Cas::_duplicate(myCase);
937 }
938 //=============================================================================
939 HOMARD::HOMARD_Zone_ptr HOMARD_Gen_i::GetZone(const char* ZoneName)
940 {
941   HOMARD::HOMARD_Zone_var myZone = myContextMap[GetCurrentStudyID()]._mesZones[ZoneName];
942   ASSERT(!CORBA::is_nil(myZone));
943   return HOMARD::HOMARD_Zone::_duplicate(myZone);
944 }
945 //=============================================================================
946 HOMARD::HOMARD_Hypothesis_ptr HOMARD_Gen_i::GetHypothesis(const char* nomHypothesis)
947 {
948   HOMARD::HOMARD_Hypothesis_var myHypothesis = myContextMap[GetCurrentStudyID()]._mesHypotheses[nomHypothesis];
949   ASSERT(!CORBA::is_nil(myHypothesis));
950   return HOMARD::HOMARD_Hypothesis::_duplicate(myHypothesis);
951 }
952 //=============================================================================
953 HOMARD::HOMARD_Iteration_ptr  HOMARD_Gen_i::GetIteration(const char* NomIterationation)
954 {
955   HOMARD::HOMARD_Iteration_var myIteration = myContextMap[GetCurrentStudyID()]._mesIterations[NomIterationation];
956   ASSERT(!CORBA::is_nil(myIteration));
957   return HOMARD::HOMARD_Iteration::_duplicate(myIteration);
958 }
959 //=============================================================================
960 HOMARD::HOMARD_Boundary_ptr HOMARD_Gen_i::GetBoundary(const char* nomBoundary)
961 {
962   HOMARD::HOMARD_Boundary_var myBoundary = myContextMap[GetCurrentStudyID()]._mesBoundarys[nomBoundary];
963   ASSERT(!CORBA::is_nil(myBoundary));
964   return HOMARD::HOMARD_Boundary::_duplicate(myBoundary);
965 }
966 //=============================================================================
967 //=============================================================================
968
969 //=============================================================================
970 //=============================================================================
971 // Informations
972 //=============================================================================
973 //=============================================================================
974 void HOMARD_Gen_i::MeshInfo(const char* nomCas, const char* MeshName, const char* MeshFile, const char* DirName, CORBA::Long Qual, CORBA::Long Diam, CORBA::Long Conn, CORBA::Long Tail, CORBA::Long Inte)
975 {
976   MESSAGE ( "MeshInfo : nomCas = " << nomCas << ", MeshName = " << MeshName << ", MeshFile = " << MeshFile  );
977   MESSAGE ( "Qual = " << Qual << ", Diam = " << Diam << ", Conn = " << Conn << ", Tail = " << Tail << ", Inte = " << Inte  );
978   IsValidStudy () ;
979
980 // Creation du cas
981   HOMARD::HOMARD_Cas_ptr myCase = CreateCase(nomCas, MeshName, MeshFile) ;
982   myCase->SetDirName(DirName) ;
983 // Analyse
984   myCase->MeshInfo(Qual, Diam, Conn, Tail, Inte) ;
985
986   return ;
987 }
988 //=============================================================================
989 //=============================================================================
990
991 //=============================================================================
992 //=============================================================================
993 // Recuperation des structures par le contexte
994 //=============================================================================
995 //=============================================================================
996 HOMARD::HOMARD_Iteration_ptr HOMARD_Gen_i::LastIteration(const char* nomCas)
997 {
998   HOMARD::HOMARD_Cas_var myCase = myContextMap[GetCurrentStudyID()]._mesCas[nomCas];
999   ASSERT(!CORBA::is_nil(myCase));
1000 //
1001   HOMARD::HOMARD_Iteration_var myIteration = myCase->LastIteration();
1002   ASSERT(!CORBA::is_nil(myIteration));
1003 //
1004   return HOMARD::HOMARD_Iteration::_duplicate(myIteration);
1005 }
1006 //=============================================================================
1007 //=============================================================================
1008
1009 //=============================================================================
1010 //=============================================================================
1011 // Nouvelles structures
1012 //=============================================================================
1013 //=============================================================================
1014 HOMARD::HOMARD_Cas_ptr HOMARD_Gen_i::newCase()
1015 {
1016   HOMARD::HOMARD_Gen_var engine = POA_HOMARD::HOMARD_Gen::_this();
1017   HOMARD_Cas_i* aServant = new HOMARD_Cas_i(_orb, engine);
1018   HOMARD::HOMARD_Cas_var aCase = HOMARD::HOMARD_Cas::_narrow(aServant->_this());
1019   return aCase._retn();
1020 }
1021 //=============================================================================
1022 HOMARD::HOMARD_Hypothesis_ptr HOMARD_Gen_i::newHypothesis()
1023 {
1024   HOMARD::HOMARD_Gen_var engine = POA_HOMARD::HOMARD_Gen::_this();
1025   HOMARD_Hypothesis_i* aServant = new HOMARD_Hypothesis_i(_orb, engine);
1026   HOMARD::HOMARD_Hypothesis_var aHypo = HOMARD::HOMARD_Hypothesis::_narrow(aServant->_this());
1027   return aHypo._retn();
1028 }
1029 //=============================================================================
1030 HOMARD::HOMARD_Iteration_ptr HOMARD_Gen_i::newIteration()
1031 {
1032   HOMARD::HOMARD_Gen_var engine = POA_HOMARD::HOMARD_Gen::_this();
1033   HOMARD_Iteration_i* aServant = new HOMARD_Iteration_i(_orb, engine);
1034   HOMARD::HOMARD_Iteration_var aIter = HOMARD::HOMARD_Iteration::_narrow(aServant->_this());
1035   return aIter._retn();
1036 }
1037 //=============================================================================
1038 HOMARD::HOMARD_Boundary_ptr HOMARD_Gen_i::newBoundary()
1039 {
1040   HOMARD::HOMARD_Gen_var engine = POA_HOMARD::HOMARD_Gen::_this();
1041   HOMARD_Boundary_i* aServant = new HOMARD_Boundary_i(_orb, engine);
1042   HOMARD::HOMARD_Boundary_var aBoundary = HOMARD::HOMARD_Boundary::_narrow(aServant->_this());
1043   return aBoundary._retn();
1044 }
1045 //=============================================================================
1046 HOMARD::HOMARD_Zone_ptr HOMARD_Gen_i::newZone()
1047 {
1048   HOMARD::HOMARD_Gen_var engine = POA_HOMARD::HOMARD_Gen::_this();
1049   HOMARD_Zone_i* aServant = new HOMARD_Zone_i(_orb, engine);
1050   HOMARD::HOMARD_Zone_var aZone = HOMARD::HOMARD_Zone::_narrow(aServant->_this());
1051   return aZone._retn();
1052 }
1053 //=============================================================================
1054 //=============================================================================
1055
1056 //=============================================================================
1057 //=============================================================================
1058 // Creation des structures identifiees par leurs noms
1059 //=============================================================================
1060 //=============================================================================
1061 HOMARD::HOMARD_Cas_ptr HOMARD_Gen_i::CreateCase(const char* nomCas, const char* MeshName, const char* MeshFile)
1062 {
1063   MESSAGE ( "CreateCase : nomCas = " << nomCas << ", MeshName = " << MeshName << ", MeshFile = " << MeshFile );
1064   IsValidStudy () ;
1065
1066   if ((myContextMap[GetCurrentStudyID()]._mesCas).find(nomCas)!=(myContextMap[GetCurrentStudyID()]._mesCas).end())
1067   {
1068     SALOME::ExceptionStruct es;
1069     es.type = SALOME::BAD_PARAM;
1070     es.text = "This case has already been defined";
1071     throw SALOME::SALOME_Exception(es);
1072     return 0;
1073   };
1074
1075   int existe = MEDFileExist ( MeshFile ) ;
1076   if ( existe == 0 )
1077   {
1078     SALOME::ExceptionStruct es;
1079     es.type = SALOME::BAD_PARAM;
1080     es.text = "The mesh file does not exist.";
1081     throw SALOME::SALOME_Exception(es);
1082     return 0;
1083   }
1084
1085   HOMARD::HOMARD_Cas_var myCase = newCase();
1086   myCase->SetName(nomCas);
1087   SALOMEDS::SObject_var aSO;
1088   PublishInStudy(myCurrentStudy, aSO, myCase, nomCas);
1089   myContextMap[GetCurrentStudyID()]._mesCas[nomCas] = myCase;
1090
1091   std::vector<double> LesExtremes =GetBoundingBoxInMedFile(MeshFile);
1092   HOMARD::extrema_var aSeq = new HOMARD::extrema();
1093   if (LesExtremes.size()!=10) { return false; }
1094   aSeq->length(10);
1095   for (int i =0; i< LesExtremes.size(); i++)
1096        aSeq[i]=LesExtremes[i];
1097   myCase->SetBoundingBox(aSeq);
1098
1099   std::set<std::string> LesGroupes  =GetListeGroupesInMedFile(MeshFile);
1100   HOMARD::ListGroupType_var aSeqGroupe = new HOMARD::ListGroupType;
1101   aSeqGroupe->length(LesGroupes.size());
1102   std::set<std::string>::const_iterator it;
1103   int i = 0;
1104   for (it=LesGroupes.begin() ; it != LesGroupes.end(); it++)
1105      aSeqGroupe[i++]=(*it).c_str();
1106   myCase->SetGroups(aSeqGroupe);
1107
1108 // Recherche d'un nom pour l'iteration 0. Par defaut, on prend le nom
1109 // du maillage du cas. Si ce nom existe deja, on incremente avec 0, 1, 2, etc.
1110   int monNum=0;
1111   std::string NomIteration = std::string(MeshName) ;
1112   while ((myContextMap[GetCurrentStudyID()]._mesIterations).find(NomIteration) != (myContextMap[GetCurrentStudyID()]._mesIterations.end()))
1113   {
1114      std::ostringstream nom;
1115      nom << MeshName << monNum;
1116      NomIteration=nom.str();
1117      monNum = monNum+1;
1118   }
1119   MESSAGE ( "CreateCase : NomIteration = " << NomIteration );
1120
1121   HOMARD::HOMARD_Iteration_var anIter = newIteration();
1122   myContextMap[GetCurrentStudyID()]._mesIterations[NomIteration] = anIter;
1123   std::ostringstream DirName;
1124   DirName << "I00";
1125
1126   anIter->SetDirName(DirName.str().c_str());
1127   anIter->SetName(NomIteration.c_str());
1128   anIter->SetMeshFile(MeshFile);
1129   anIter->SetMeshName(MeshName);
1130   anIter->SetNumber(0);
1131
1132   AssociateCaseIter (nomCas,NomIteration.c_str(),"IterationHomard");
1133   SetEtatIter(NomIteration.c_str(),true);
1134 //
1135   PublishResultInSmesh(MeshFile, 0);
1136
1137 // Valeurs par defaut des filtrages
1138   myCase->SetPyram(0);
1139
1140   return HOMARD::HOMARD_Cas::_duplicate(myCase);
1141 }
1142 //=============================================================================
1143 HOMARD::HOMARD_Hypothesis_ptr HOMARD_Gen_i::CreateHypothesis(const char* nomHypothesis)
1144 {
1145   MESSAGE ( "CreateHypothesis : nomHypothesis  = " << nomHypothesis );
1146   IsValidStudy () ;
1147
1148   if ((myContextMap[GetCurrentStudyID()]._mesHypotheses).find(nomHypothesis) != (myContextMap[GetCurrentStudyID()]._mesHypotheses).end())
1149   {
1150       SALOME::ExceptionStruct es;
1151       es.type = SALOME::BAD_PARAM;
1152       es.text = "This hypothesis is already defined.";
1153       throw SALOME::SALOME_Exception(es);
1154       return 0;
1155     }
1156
1157   HOMARD::HOMARD_Hypothesis_var myHypothesis = newHypothesis();
1158   myHypothesis->SetName(nomHypothesis);
1159   myContextMap[GetCurrentStudyID()]._mesHypotheses[nomHypothesis] = myHypothesis;
1160   SALOMEDS::SObject_var aSO;
1161   PublishInStudy(myCurrentStudy, aSO, myHypothesis, nomHypothesis);
1162
1163 // Valeurs par defaut des options avancees
1164   myHypothesis->SetNivMax(-1);
1165   myHypothesis->SetDiamMin(-1.0);
1166   myHypothesis->SetAdapInit(0);
1167   myHypothesis->SetLevelOutput(0);
1168
1169   return HOMARD::HOMARD_Hypothesis::_duplicate(myHypothesis);
1170 }
1171
1172 //=============================================================================
1173 HOMARD::HOMARD_Iteration_ptr HOMARD_Gen_i::CreateIteration(const char* NomIteration, const char* nomIterParent)
1174 //=============================================================================
1175 {
1176   MESSAGE ("CreateIteration : NomIteration  = " << NomIteration << ", nomIterParent = " << nomIterParent);
1177   IsValidStudy () ;
1178
1179   HOMARD::HOMARD_Iteration_var myIterationParent = myContextMap[GetCurrentStudyID()]._mesIterations[nomIterParent];
1180   if (CORBA::is_nil(myIterationParent))
1181   {
1182       SALOME::ExceptionStruct es;
1183       es.type = SALOME::BAD_PARAM;
1184       es.text = "The parent iteration is not defined.";
1185       throw SALOME::SALOME_Exception(es);
1186       return 0;
1187   };
1188
1189   const char* nomCas = myIterationParent->GetCaseName();
1190   MESSAGE ("CreateIteration : nomCas = " << nomCas);
1191   HOMARD::HOMARD_Cas_var myCase = myContextMap[GetCurrentStudyID()]._mesCas[nomCas];
1192   if (CORBA::is_nil(myCase))
1193   {
1194       SALOME::ExceptionStruct es;
1195       es.type = SALOME::BAD_PARAM;
1196       es.text = "Invalid Case Context ";
1197       throw SALOME::SALOME_Exception(es);
1198       return 0;
1199   };
1200
1201   if ((myContextMap[GetCurrentStudyID()]._mesIterations).find(NomIteration)!=(myContextMap[GetCurrentStudyID()]._mesIterations).end())
1202   {
1203       SALOME::ExceptionStruct es;
1204       es.type = SALOME::BAD_PARAM;
1205       es.text = "This iteration is already defined. ";
1206       throw SALOME::SALOME_Exception(es);
1207       return 0;
1208   };
1209
1210    HOMARD::HOMARD_Iteration_var myIteration = newIteration();
1211    if (CORBA::is_nil(myIteration))
1212   {
1213       SALOME::ExceptionStruct es;
1214       es.type = SALOME::BAD_PARAM;
1215       es.text = "Unable to create Iteration ";
1216       throw SALOME::SALOME_Exception(es);
1217       return 0;
1218   };
1219   myContextMap[GetCurrentStudyID()]._mesIterations[std::string(NomIteration)] = myIteration;
1220 // Nom de l'iteration et du maillage
1221   myIteration->SetName(NomIteration);
1222   myIteration->SetMeshName(NomIteration);
1223
1224   int numero = myIterationParent->GetNumber() + 1;
1225   myIteration->SetNumber(numero);
1226
1227 // Nombre d'iterations deja connues pour le cas, permettant
1228 // la creation d'un sous-repertoire unique
1229    int nbitercase = myCase->GetNumber();
1230    std::ostringstream iaux ;
1231    iaux << std::setw(2) << std::setfill('0') << nbitercase ;
1232    std::stringstream DirName;
1233    DirName << "I" << iaux.str();
1234    myIteration->SetDirName(DirName.str().c_str());
1235
1236 // Le nom du fichier du maillage MED est indice par le nombre d'iterations du cas.
1237 // Si on a une chaine unique depuis le depart, ce nombre est le meme que le
1238 // numero d'iteration dans la sucession : maill.01.med, maill.02.med, etc... C'est la
1239 // situation la plus frequente.
1240 // Si on a plusieurs branches, donc des iterations du meme niveau d'adaptation, utiliser
1241 // le nombre d'iterations du cas permet d'eviter les collisions.
1242    std::stringstream MeshFile;
1243    const char* nomDir = myCase->GetDirName();
1244    MeshFile << nomDir << "/maill." << iaux.str() << ".med";
1245    myIteration->SetMeshFile(MeshFile.str().c_str());
1246
1247 // Association avec le cas
1248   std::string label = "IterationHomard_" + std::string(nomIterParent);
1249   AssociateCaseIter(nomCas, NomIteration, label.c_str());
1250 // Lien avec l'iteration precedente
1251   myIterationParent->LinkNextIteration(NomIteration);
1252   myIteration->SetIterParentName(nomIterParent);
1253
1254   return HOMARD::HOMARD_Iteration::_duplicate(myIteration);
1255 }
1256 //=============================================================================
1257 HOMARD::HOMARD_Boundary_ptr HOMARD_Gen_i::CreateBoundary(const char* BoundaryName, CORBA::Long BoundaryType)
1258 {
1259   MESSAGE ("CreateBoundary : BoundaryName  = " << BoundaryName << ", BoundaryType = " << BoundaryType);
1260   IsValidStudy () ;
1261
1262   if ((myContextMap[GetCurrentStudyID()]._mesBoundarys).find(BoundaryName)!=(myContextMap[GetCurrentStudyID()]._mesBoundarys).end())
1263   {
1264       SALOME::ExceptionStruct es;
1265       es.type = SALOME::BAD_PARAM;
1266       es.text = "This boundary has already been defined";
1267       throw SALOME::SALOME_Exception(es);
1268       return 0;
1269   };
1270
1271   HOMARD::HOMARD_Boundary_var myBoundary = newBoundary();
1272   myBoundary->SetName(BoundaryName);
1273   myBoundary->SetType(BoundaryType);
1274
1275   myContextMap[GetCurrentStudyID()]._mesBoundarys[BoundaryName] = myBoundary;
1276
1277   SALOMEDS::SObject_var aSO;
1278   SALOMEDS::SObject_var aResultSO=PublishInStudy(myCurrentStudy, aSO, myBoundary, BoundaryName);
1279
1280   return HOMARD::HOMARD_Boundary::_duplicate(myBoundary);
1281 }
1282 //=============================================================================
1283 HOMARD::HOMARD_Boundary_ptr HOMARD_Gen_i::CreateBoundaryDi(const char* BoundaryName, const char* MeshName, const char* MeshFile)
1284 {
1285   MESSAGE ("CreateBoundaryDi : BoundaryName  = " << BoundaryName << "MeshName = " << MeshName );
1286   HOMARD::HOMARD_Boundary_var myBoundary = CreateBoundary(BoundaryName, 0);
1287   myBoundary->SetMeshFile( MeshFile ) ;
1288   myBoundary->SetMeshName( MeshName ) ;
1289
1290   return HOMARD::HOMARD_Boundary::_duplicate(myBoundary);
1291 }
1292 //=============================================================================
1293 HOMARD::HOMARD_Boundary_ptr HOMARD_Gen_i::CreateBoundaryCylinder(const char* BoundaryName,
1294                                       CORBA::Double Xcentre, CORBA::Double Ycentre, CORBA::Double Zcentre,
1295                                       CORBA::Double Xaxis, CORBA::Double Yaxis, CORBA::Double Zaxis,
1296                                       CORBA::Double Rayon)
1297 {
1298   MESSAGE ("CreateBoundaryCylinder : BoundaryName  = " << BoundaryName ) ;
1299   HOMARD::HOMARD_Boundary_var myBoundary = CreateBoundary(BoundaryName, 1) ;
1300   myBoundary->SetCylinder( Xcentre, Ycentre, Zcentre, Xaxis, Yaxis, Zaxis, Rayon ) ;
1301
1302   return HOMARD::HOMARD_Boundary::_duplicate(myBoundary) ;
1303 }
1304 //=============================================================================
1305 HOMARD::HOMARD_Boundary_ptr HOMARD_Gen_i::CreateBoundarySphere(const char* BoundaryName,
1306                                       CORBA::Double Xcentre, CORBA::Double Ycentre, CORBA::Double Zcentre,
1307                                       CORBA::Double Rayon)
1308 {
1309   MESSAGE ("CreateBoundarySphere : BoundaryName  = " << BoundaryName ) ;
1310   HOMARD::HOMARD_Boundary_var myBoundary = CreateBoundary(BoundaryName, 2) ;
1311   myBoundary->SetSphere( Xcentre, Ycentre, Zcentre, Rayon ) ;
1312
1313   return HOMARD::HOMARD_Boundary::_duplicate(myBoundary) ;
1314 }
1315 //=============================================================================
1316 HOMARD::HOMARD_Zone_ptr HOMARD_Gen_i::CreateZone(const char* ZoneName, CORBA::Long ZoneType)
1317 {
1318   MESSAGE ("CreateZone : ZoneName  = " << ZoneName << ", ZoneType = " << ZoneType);
1319   IsValidStudy () ;
1320
1321   if ((myContextMap[GetCurrentStudyID()]._mesZones).find(ZoneName)!=(myContextMap[GetCurrentStudyID()]._mesZones).end())
1322   {
1323       SALOME::ExceptionStruct es;
1324       es.type = SALOME::BAD_PARAM;
1325       es.text = "This zone has already been defined";
1326       throw SALOME::SALOME_Exception(es);
1327       return 0;
1328   };
1329
1330   HOMARD::HOMARD_Zone_var myZone = newZone();
1331   myZone->SetName(ZoneName);
1332   myZone->SetType(ZoneType);
1333
1334   myContextMap[GetCurrentStudyID()]._mesZones[ZoneName] = myZone;
1335
1336   SALOMEDS::SObject_var aSO;
1337   SALOMEDS::SObject_var aResultSO=PublishInStudy(myCurrentStudy, aSO, myZone, ZoneName);
1338
1339   return HOMARD::HOMARD_Zone::_duplicate(myZone);
1340 }
1341 //=============================================================================
1342 HOMARD::HOMARD_Zone_ptr HOMARD_Gen_i::CreateZoneBox(const char* ZoneName,
1343                                       CORBA::Double Xmini, CORBA::Double Xmaxi,
1344                                       CORBA::Double Ymini, CORBA::Double Ymaxi,
1345                                       CORBA::Double Zmini, CORBA::Double Zmaxi)
1346 {
1347   MESSAGE ("CreateZoneBox : ZoneName  = " << ZoneName ) ;
1348   HOMARD::HOMARD_Zone_var myZone = CreateZone(ZoneName, 2) ;
1349   myZone->SetBox ( Xmini, Xmaxi, Ymini, Ymaxi, Zmini, Zmaxi) ;
1350
1351   return HOMARD::HOMARD_Zone::_duplicate(myZone) ;
1352 }
1353 //=============================================================================
1354 HOMARD::HOMARD_Zone_ptr HOMARD_Gen_i::CreateZoneSphere(const char* ZoneName,
1355                                       CORBA::Double Xcentre, CORBA::Double Ycentre, CORBA::Double Zcentre, CORBA::Double Rayon)
1356 {
1357   MESSAGE ("CreateZoneSphere : ZoneName  = " << ZoneName ) ;
1358   HOMARD::HOMARD_Zone_var myZone = CreateZone(ZoneName, 4) ;
1359   myZone->SetSphere( Xcentre, Ycentre, Zcentre, Rayon ) ;
1360
1361   return HOMARD::HOMARD_Zone::_duplicate(myZone) ;
1362 }
1363 //=============================================================================
1364 HOMARD::HOMARD_Zone_ptr HOMARD_Gen_i::CreateZoneCylinder(const char* ZoneName,
1365                                       CORBA::Double Xcentre, CORBA::Double Ycentre, CORBA::Double Zcentre,
1366                                       CORBA::Double Xaxe, CORBA::Double Yaxe, CORBA::Double Zaxe,
1367                                       CORBA::Double Rayon, CORBA::Double Haut)
1368 {
1369   MESSAGE ("CreateZoneCylinder : ZoneName  = " << ZoneName ) ;
1370   HOMARD::HOMARD_Zone_var myZone = CreateZone(ZoneName, 5) ;
1371   myZone->SetCylinder( Xcentre, Ycentre, Zcentre, Xaxe, Yaxe, Zaxe, Rayon, Haut ) ;
1372
1373   return HOMARD::HOMARD_Zone::_duplicate(myZone) ;
1374 }
1375 //=============================================================================
1376 HOMARD::HOMARD_Zone_ptr HOMARD_Gen_i::CreateZonePipe(const char* ZoneName,
1377                                       CORBA::Double Xcentre, CORBA::Double Ycentre, CORBA::Double Zcentre,
1378                                       CORBA::Double Xaxe, CORBA::Double Yaxe, CORBA::Double Zaxe,
1379                                       CORBA::Double Rayon, CORBA::Double Haut, CORBA::Double Rayonint)
1380 {
1381   MESSAGE ("CreateZonePipe : ZoneName  = " << ZoneName ) ;
1382   HOMARD::HOMARD_Zone_var myZone = CreateZone(ZoneName, 7) ;
1383   myZone->SetPipe( Xcentre, Ycentre, Zcentre, Xaxe, Yaxe, Zaxe, Rayon, Haut, Rayonint ) ;
1384
1385   return HOMARD::HOMARD_Zone::_duplicate(myZone) ;
1386 }
1387 //=============================================================================
1388 HOMARD::HOMARD_Zone_ptr HOMARD_Gen_i::CreateZoneBox2D(const char* ZoneName,
1389                                       CORBA::Double Umini, CORBA::Double Umaxi,
1390                                       CORBA::Double Vmini, CORBA::Double Vmaxi,
1391                                       CORBA::Long Orient)
1392 {
1393   MESSAGE ("CreateZoneBox2D : ZoneName  = " << ZoneName ) ;
1394 //   MESSAGE ("Umini = " << Umini << ", Umaxi =" << Umaxi ) ;
1395 //   MESSAGE ("Vmini = " << Vmini << ", Vmaxi =" << Vmaxi ) ;
1396 //   MESSAGE ("Orient = " << Orient ) ;
1397
1398   double Xmini, Xmaxi ;
1399   double Ymini, Ymaxi ;
1400   double Zmini, Zmaxi ;
1401   if ( Orient == 1 )
1402   { Xmini = Umini ;
1403     Xmaxi = Umaxi ;
1404     Ymini = Vmini ;
1405     Ymaxi = Vmaxi ;
1406     Zmini = 0. ;
1407     Zmaxi = 0. ; }
1408   else if ( Orient == 2 )
1409   { Xmini = 0. ;
1410     Xmaxi = 0. ;
1411     Ymini = Umini ;
1412     Ymaxi = Umaxi ;
1413     Zmini = Vmini ;
1414     Zmaxi = Vmaxi ; }
1415   else if ( Orient == 3 )
1416   { Xmini = Vmini ;
1417     Xmaxi = Vmaxi ;
1418     Ymini = 0. ;
1419     Ymaxi = 0. ;
1420     Zmini = Umini ;
1421     Zmaxi = Umaxi ; }
1422   else { ASSERT( Orient >= 1 and Orient <= 3 ) ; }
1423
1424   HOMARD::HOMARD_Zone_var myZone = CreateZone(ZoneName, 10+Orient) ;
1425   myZone->SetBox ( Xmini, Xmaxi, Ymini, Ymaxi, Zmini, Zmaxi) ;
1426
1427   return HOMARD::HOMARD_Zone::_duplicate(myZone) ;
1428 }
1429 //=============================================================================
1430 HOMARD::HOMARD_Zone_ptr HOMARD_Gen_i::CreateZoneDisk(const char* ZoneName,
1431                                       CORBA::Double Ucentre, CORBA::Double Vcentre,
1432                                       CORBA::Double Rayon,
1433                                       CORBA::Long Orient)
1434 {
1435   MESSAGE ("CreateZoneDisk : ZoneName  = " << ZoneName ) ;
1436   double Xcentre ;
1437   double Ycentre ;
1438   double Zcentre ;
1439   if ( Orient == 1 )
1440   { Xcentre = Ucentre ;
1441     Ycentre = Vcentre ;
1442     Zcentre = 0. ; }
1443   else if ( Orient == 2 )
1444   { Xcentre = 0. ;
1445     Ycentre = Ucentre ;
1446     Zcentre = Vcentre ; }
1447   else if ( Orient == 3 )
1448   { Xcentre = Vcentre ;
1449     Ycentre = 0. ;
1450     Zcentre = Ucentre ; }
1451   else { ASSERT( Orient >= 1 and Orient <= 3 ) ; }
1452
1453   HOMARD::HOMARD_Zone_var myZone = CreateZone(ZoneName, 30+Orient) ;
1454   myZone->SetCylinder( Xcentre, Ycentre, Zcentre, 0., 0., 1., Rayon, 1. ) ;
1455
1456   return HOMARD::HOMARD_Zone::_duplicate(myZone) ;
1457 }
1458 //=============================================================================
1459 HOMARD::HOMARD_Zone_ptr HOMARD_Gen_i::CreateZoneDiskWithHole(const char* ZoneName,
1460                                       CORBA::Double Ucentre, CORBA::Double Vcentre,
1461                                       CORBA::Double Rayon, CORBA::Double Rayonint,
1462                                       CORBA::Long Orient)
1463 {
1464   MESSAGE ("CreateZoneDiskWithHole : ZoneName  = " << ZoneName ) ;
1465   double Xcentre ;
1466   double Ycentre ;
1467   double Zcentre ;
1468   if ( Orient == 1 )
1469   { Xcentre = Ucentre ;
1470     Ycentre = Vcentre ;
1471     Zcentre = 0. ; }
1472   else if ( Orient == 2 )
1473   { Xcentre = 0. ;
1474     Ycentre = Ucentre ;
1475     Zcentre = Vcentre ; }
1476   else if ( Orient == 3 )
1477   { Xcentre = Vcentre ;
1478     Ycentre = 0. ;
1479     Zcentre = Ucentre ; }
1480   else { ASSERT( Orient >= 1 and Orient <= 3 ) ; }
1481
1482   HOMARD::HOMARD_Zone_var myZone = CreateZone(ZoneName, 60+Orient) ;
1483   myZone->SetPipe( Xcentre, Ycentre, Zcentre, 0., 0., 1., Rayon, 1., Rayonint ) ;
1484
1485   return HOMARD::HOMARD_Zone::_duplicate(myZone) ;
1486 }
1487 //=============================================================================
1488 //=============================================================================
1489
1490
1491
1492
1493 //=============================================================================
1494 //=============================================================================
1495 // Traitement d'une iteration
1496 // etatMenage = 1 : destruction du repertoire d'execution
1497 // modeHOMARD  = 1 : adaptation
1498 //            != 1 : information avec les options modeHOMARD
1499 // Option >0 : appel depuis python
1500 //        <0 : appel depuis GUI
1501 //=============================================================================
1502 //=============================================================================
1503 CORBA::Long HOMARD_Gen_i::Compute(const char* NomIteration, CORBA::Long etatMenage, CORBA::Long modeHOMARD, CORBA::Long Option)
1504 {
1505   MESSAGE ( "Compute : traitement de " << NomIteration << ", avec modeHOMARD = " << modeHOMARD << ", avec Option = " << Option );
1506
1507   // A. Prealable
1508   int codret = 0;
1509
1510   // A.1. L'objet iteration
1511   HOMARD::HOMARD_Iteration_var myIteration = myContextMap[GetCurrentStudyID()]._mesIterations[NomIteration];
1512   ASSERT(!CORBA::is_nil(myIteration));
1513
1514   // A.2. Numero de l'iteration
1515   //     siterp1 : numero de l'iteration a traiter
1516   //     Si adaptation :
1517   //        siter   : numero de l'iteration parent, ou 0 si deja au debut mais cela ne servira pas !
1518   //     Ou si information :
1519   //        siter = siterp1
1520   int NumeIter = myIteration->GetNumber();
1521   std::string siterp1 ;
1522   std::stringstream saux1 ;
1523   saux1 << NumeIter ;
1524   siterp1 = saux1.str() ;
1525   if (NumeIter < 10) { siterp1 = "0" + siterp1 ; }
1526
1527   std::string siter ;
1528   if ( modeHOMARD==1 )
1529   {
1530     std::stringstream saux0 ;
1531     int iaux = max(0, NumeIter-1) ;
1532     saux0 << iaux ;
1533     siter = saux0.str() ;
1534     if (NumeIter < 11) { siter = "0" + siter ; }
1535   }
1536   else
1537   { siter = siterp1 ; }
1538
1539   // A.3. Le cas
1540   const char* nomCas = myIteration->GetCaseName();
1541   HOMARD::HOMARD_Cas_var myCase = myContextMap[GetCurrentStudyID()]._mesCas[nomCas];
1542   ASSERT(!CORBA::is_nil(myCase));
1543
1544   // B. Les repertoires
1545   // B.1. Le repertoire courant
1546   char* nomDirWork = getenv("PWD") ;
1547   // B.2. Le sous-repertoire de l'iteration a traiter
1548   char* DirCompute = ComputeDir(myCase, myIteration, etatMenage);
1549   MESSAGE( ". DirCompute = " << DirCompute );
1550
1551   // C. Le fichier des messages
1552   // C.1. Le deroulement de l'execution de HOMARD
1553   std::string LogFile = DirCompute ;
1554   LogFile += "/Liste" ;
1555   if ( modeHOMARD == 1 ) { LogFile += "." + siter + ".vers." + siterp1 ; }
1556   LogFile += ".log" ;
1557   MESSAGE (". LogFile = " << LogFile);
1558   if ( modeHOMARD == 1 ) { myIteration->SetLogFile(LogFile.c_str()); }
1559   // C.2. Le bilan de l'analyse du maillage
1560   std::string FileInfo = DirCompute ;
1561   FileInfo += "/" ;
1562   if ( modeHOMARD == 1 ) { FileInfo += "apad" ; }
1563   else
1564   { if ( NumeIter == 0 ) { FileInfo += "info_av" ; }
1565     else                 { FileInfo += "info_ap" ; }
1566   }
1567   FileInfo += "." + siterp1 + ".bilan" ;
1568   myIteration->SetFileInfo(FileInfo.c_str());
1569
1570    // D. On passe dans le repertoire de l'iteration a calculer
1571   MESSAGE ( ". On passe dans DirCompute = " << DirCompute );
1572   chdir(DirCompute) ;
1573
1574   // E. Les donnees de l'execution HOMARD
1575   // E.1. L'objet du texte du fichier de configuration
1576   HomardDriver* myDriver = new HomardDriver(siter, siterp1);
1577   myDriver->TexteInit(DirCompute, LogFile);
1578
1579   // E.2. Le maillage associe a l'iteration
1580   const char* NomMesh = myIteration->GetMeshName();
1581   MESSAGE ( ". NomMesh = " << NomMesh );
1582   const char* MeshFile = myIteration->GetMeshFile();
1583   MESSAGE ( ". MeshFile = " << MeshFile );
1584
1585   // E.3. Les donnees du traitement HOMARD
1586   int iaux ;
1587   if ( modeHOMARD == 1 )
1588   {
1589     iaux = 1 ;
1590     myDriver->TexteMaillageHOMARD( DirCompute, siterp1, iaux ) ;
1591     myDriver->TexteMaillage(NomMesh, MeshFile, 1);
1592     codret = ComputeAdap(myCase, myIteration, etatMenage, myDriver, Option) ;
1593   }
1594   else
1595   {
1596     InvalideIterInfo(NomIteration);
1597     myDriver->TexteInfo( modeHOMARD, NumeIter ) ;
1598     iaux = 0 ;
1599     myDriver->TexteMaillageHOMARD( DirCompute, siterp1, iaux ) ;
1600     myDriver->TexteMaillage(NomMesh, MeshFile, 0);
1601     myDriver->CreeFichierDonn();
1602   }
1603
1604   // E.4. Ajout des informations liees a l'eventuel suivi de frontiere
1605   DriverTexteBoundary(myCase, myDriver) ;
1606
1607   // E.5. Ecriture du texte dans le fichier
1608   if (codret == 0)
1609   { myDriver->CreeFichier(); }
1610
1611 // G. Execution
1612 //
1613   int codretexec = 12 ;
1614   if (codret == 0)
1615   {
1616     codretexec = myDriver->ExecuteHomard(Option);
1617 //
1618     MESSAGE ( "Erreur en executant HOMARD : " << codretexec );
1619     if ( modeHOMARD == 1 )
1620     {
1621       if (codretexec == 0) { SetEtatIter(NomIteration,true); }
1622       else                 { SetEtatIter(NomIteration,false); }
1623       // GERALD -- QMESSAGE BOX
1624     }
1625   }
1626
1627   // H. Gestion des resultats
1628   if (codret == 0)
1629   {
1630     std::string Commentaire ;
1631     // H.1. Le fichier des messages, dans tous les cas
1632     Commentaire = "log" ;
1633     if ( modeHOMARD == 1 ) { Commentaire += " " + siterp1 ; }
1634     else                   { Commentaire += "Info" ; }
1635     PublishFileUnderIteration(NomIteration, LogFile.c_str(), Commentaire.c_str());
1636
1637     // H.2. Si tout s'est bien passe :
1638     if (codretexec == 0)
1639     {
1640     // H.2.1. Le fichier de bilan
1641       Commentaire = "Summary" ;
1642       if ( modeHOMARD == 1 ) { Commentaire += " " + siterp1 ; }
1643       else                   { Commentaire += "Info" ; }
1644       PublishFileUnderIteration(NomIteration, FileInfo.c_str(), Commentaire.c_str());
1645     // H.2.2. Le fichier de  maillage obtenu
1646       if ( modeHOMARD == 1 )
1647       {
1648         std::stringstream saux0 ;
1649         Commentaire = "Iteration" ;
1650         Commentaire += " " + siter ;
1651         PublishFileUnderIteration(NomIteration, MeshFile, Commentaire.c_str());
1652         PublishResultInSmesh(MeshFile, 1);
1653       }
1654     }
1655   // H.3 Message d'erreur en cas de probleme
1656     else
1657     {
1658       SALOME::ExceptionStruct es;
1659       es.type = SALOME::BAD_PARAM;
1660       std::string text = "Error during the adaptation.\n" ;
1661       try
1662       {
1663           ifstream fichier(LogFile.c_str(), ios::in);
1664           string ligne;
1665           while(getline(fichier, ligne) and (ligne != "===== HOMARD ===== STOP ====="));
1666           while (getline(fichier, ligne)) { text += ligne+ "\n";};
1667       }
1668       catch (...) {
1669         text += "no log file ....";
1670       }
1671       es.text = CORBA::string_dup(text.c_str());
1672       throw SALOME::SALOME_Exception(es);
1673     }
1674   }
1675
1676   // I. Menage et retour dans le repertoire du cas
1677   if (codret == 0)
1678   {
1679     delete myDriver;
1680     MESSAGE ( ". On retourne dans nomDirWork = " << nomDirWork );
1681     chdir(nomDirWork);
1682   }
1683
1684   return codretexec ;
1685 }
1686 //=============================================================================
1687 // Calcul d'une iteration : partie specifique a l'adaptation
1688 //=============================================================================
1689 CORBA::Long HOMARD_Gen_i::ComputeAdap(HOMARD::HOMARD_Cas_var myCase, HOMARD::HOMARD_Iteration_var myIteration, CORBA::Long etatMenage, HomardDriver* myDriver, CORBA::Long Option)
1690 {
1691   MESSAGE ( "ComputeAdap" );
1692
1693   // A. Prealable
1694   // A.1. Bases
1695   int codret = 0;
1696   // Numero de l'iteration
1697   int NumeIter = myIteration->GetNumber();
1698   std::stringstream saux0 ;
1699   saux0 << NumeIter-1 ;
1700   std::string siter = saux0.str() ;
1701   if (NumeIter < 11) { siter = "0" + siter ; }
1702
1703   // A.2. On ne calcule pas l iteration 0
1704   if ( NumeIter == 0 )
1705   {
1706       SALOME::ExceptionStruct es;
1707       es.type = SALOME::BAD_PARAM;
1708       es.text = "This iteration is the first of the case and cannot be computed.";
1709       throw SALOME::SALOME_Exception(es);
1710       return 1;
1711   };
1712
1713   // A.3. On verifie qu il y a une hypothese (erreur improbable);
1714   const char* nomHypo = myIteration->GetHypoName();
1715   if (std::string(nomHypo) == std::string(""))
1716   {
1717       SALOME::ExceptionStruct es;
1718       es.type = SALOME::BAD_PARAM;
1719       es.text= "This iteration does not have any associated hypothesis.";
1720       throw SALOME::SALOME_Exception(es);
1721       return 2;
1722   };
1723   HOMARD::HOMARD_Hypothesis_var myHypo = myContextMap[GetCurrentStudyID()]._mesHypotheses[nomHypo];
1724   ASSERT(!CORBA::is_nil(myHypo));
1725
1726   // B. L'iteration parent
1727   const char* nomIterationParent = myIteration->GetIterParentName();
1728   HOMARD::HOMARD_Iteration_var myIterationParent = myContextMap[GetCurrentStudyID()]._mesIterations[nomIterationParent];
1729   ASSERT(!CORBA::is_nil(myIterationParent));
1730   // Si l'iteration parent n'est pas calculee, on le fait (recursivite amont)
1731   if ( ! myIterationParent->GetEtat() )
1732   {
1733       int iaux = 1 ;
1734       int codret = Compute(nomIterationParent, etatMenage, iaux, Option);
1735       if (codret != 0)
1736       {
1737         // GERALD -- QMESSAGE BOX
1738         ASSERT("Pb au calcul de l'iteration precedente" == 0);
1739       }
1740   };
1741
1742   // C. Le sous-repertoire de l'iteration precedente
1743   char* DirComputePa = ComputeDirPa(myCase, myIteration);
1744   MESSAGE( ". DirComputePa = " << DirComputePa );
1745
1746   // D. Les donnees de l'adaptation HOMARD
1747   // D.1. Le type de conformite
1748   int ConfType = myCase->GetConfType();
1749   MESSAGE ( ". ConfType = " << ConfType );
1750
1751   // D.2. Le maillage de depart
1752   const char* NomMeshParent = myIterationParent->GetMeshName();
1753   MESSAGE ( ". NomMeshParent = " << NomMeshParent );
1754   const char* MeshFileParent = myIterationParent->GetMeshFile();
1755   MESSAGE ( ". MeshFileParent = " << MeshFileParent );
1756
1757   // D.3. Le maillage associe a l'iteration
1758   const char* MeshFile = myIteration->GetMeshFile();
1759   MESSAGE ( ". MeshFile = " << MeshFile );
1760   FILE *file = fopen(MeshFile,"r");
1761   if (file != NULL)
1762   {
1763     fclose(file);
1764     if (etatMenage == 0)
1765     {
1766           SALOME::ExceptionStruct es;
1767           es.type = SALOME::BAD_PARAM;
1768           std::string text = "MeshFile : " + std::string(MeshFile) + " already exists ";
1769           es.text = CORBA::string_dup(text.c_str());
1770           throw SALOME::SALOME_Exception(es);
1771           return 4;
1772     }
1773     else
1774     {
1775         std::string commande = "rm -f " + std::string(MeshFile);
1776         codret = system(commande.c_str());
1777         if (codret != 0)
1778         {
1779           SALOME::ExceptionStruct es;
1780           es.type = SALOME::BAD_PARAM;
1781           std::string text = "PB with meshfile destruction ";
1782           es.text = CORBA::string_dup(text.c_str());
1783           throw SALOME::SALOME_Exception(es);
1784           return 5;
1785         }
1786       }
1787   }
1788
1789   // D.4. Les types de raffinement et de deraffinement
1790   // Les appels corba sont lourds, il vaut mieux les grouper
1791   HOMARD::listeTypes* ListTypes = myHypo->GetAdapRefinUnRef();
1792   ASSERT(ListTypes->length() == 3);
1793   int TypeAdap = (*ListTypes)[0];
1794   int TypeRaff = (*ListTypes)[1];
1795   int TypeDera = (*ListTypes)[2];
1796
1797   // E. Texte du fichier de configuration
1798   // E.1. Incontournables du texte
1799   myDriver->TexteAdap();
1800   int iaux = 0 ;
1801   myDriver->TexteMaillageHOMARD( DirComputePa, siter, iaux ) ;
1802   myDriver->TexteMaillage(NomMeshParent, MeshFileParent, 0);
1803   myDriver->TexteConfRaffDera(ConfType, TypeAdap, TypeRaff, TypeDera);
1804
1805   // E.2. Ajout des informations liees aux zones eventuelles
1806   if ( TypeAdap == 0 )
1807   { DriverTexteZone(myHypo, myDriver) ; }
1808
1809   // E.3. Ajout des informations liees aux champs eventuels
1810   if ( TypeAdap == 1 )
1811   { DriverTexteField(myIteration, myHypo, myDriver) ; }
1812
1813   // E.4. Ajout des informations liees au filtrage eventuel par les groupes
1814   HOMARD::ListGroupType* listeGroupes = myHypo->GetGroups();
1815   int numberOfGroups = listeGroupes->length();
1816   MESSAGE( ". Filtrage par " << numberOfGroups << " groupes");
1817   if (numberOfGroups > 0)
1818   {
1819     for (int NumGroup = 0; NumGroup< numberOfGroups; NumGroup++)
1820     {
1821       std::string GroupName = std::string((*listeGroupes)[NumGroup]);
1822       MESSAGE( "... GroupName = " << GroupName );
1823       myDriver->TexteGroup(GroupName);
1824     }
1825   }
1826
1827   // E.5. Ajout des informations liees a l'eventuelle interpolation des champs
1828   DriverTexteFieldInterp(myIteration, myHypo, myDriver) ;
1829
1830   // E.6. Ajout des options avancees
1831   int Pyram = myCase->GetPyram();
1832   MESSAGE ( ". Pyram = " << Pyram );
1833   int NivMax = myHypo->GetNivMax();
1834   MESSAGE ( ". NivMax = " << NivMax );
1835   double DiamMin = myHypo->GetDiamMin() ;
1836   MESSAGE ( ". DiamMin = " << DiamMin );
1837   int AdapInit = myHypo->GetAdapInit();
1838   MESSAGE ( ". AdapInit = " << AdapInit );
1839   int LevelOutput = myHypo->GetLevelOutput();
1840   MESSAGE ( ". LevelOutput = " << LevelOutput );
1841   myDriver->TexteAdvanced(Pyram, NivMax, DiamMin, AdapInit, LevelOutput);
1842
1843   return codret ;
1844 }
1845 //=============================================================================
1846 // Calcul d'une iteration : gestion du repertoire de calcul
1847 //        Si le sous-repertoire existe :
1848 //         etatMenage =  0 : on sort en erreur si le repertoire n'est pas vide
1849 //         etatMenage =  1 : on fait le menage du repertoire
1850 //         etatMenage = -1 : on ne fait rien
1851 //=============================================================================
1852 char* HOMARD_Gen_i::ComputeDir(HOMARD::HOMARD_Cas_var myCase, HOMARD::HOMARD_Iteration_var myIteration, CORBA::Long etatMenage)
1853 {
1854   MESSAGE ( "ComputeDir : repertoires pour le calcul" );
1855   // B.2. Le repertoire du cas
1856   const char* nomDirCase = myCase->GetDirName();
1857   MESSAGE ( ". nomDirCase = " << nomDirCase );
1858
1859   // B.3. Le sous-repertoire de l'iteration a calculer, puis le repertoire complet a creer
1860   // B.3.1. Le nom du sous-repertoire
1861   const char* nomDirIt = myIteration->GetDirName();
1862
1863   // B.3.2. Le nom complet du sous-repertoire
1864   std::stringstream DirCompute ;
1865   DirCompute << nomDirCase << "/" << nomDirIt;
1866   MESSAGE (". DirCompute = " << DirCompute.str() );
1867
1868   // B.3.3. Si le sous-repertoire n'existe pas, on le cree
1869   if (chdir(DirCompute.str().c_str()) != 0)
1870   {
1871 //  Creation du repertoire car il n'existe pas :
1872     if (mkdir(DirCompute.str().c_str(), S_IRWXU|S_IRGRP|S_IXGRP) != 0)
1873     {
1874        // GERALD -- QMESSAGE BOX
1875        std::cerr << "Pb Creation du repertoire DirCompute = " << DirCompute.str() << std::endl;
1876        ASSERT("Pb a la creation du repertoire" == 0);
1877     }
1878   }
1879   else
1880   {
1881 //  Le repertoire existe
1882 //  On demande de faire le menage de son contenu :
1883     if (etatMenage == 1)
1884     {
1885        MESSAGE (". Menage du repertoire DirCompute = " << DirCompute.str());
1886        std::string commande= "rm -rf " + DirCompute.str()+"/*" ;
1887        int codret = system(commande.c_str());
1888        if (codret != 0)
1889        {
1890          // GERALD -- QMESSAGE BOX
1891          std::cerr << ". Menage du repertoire de calcul" << DirCompute.str() << std::endl;
1892          ASSERT("Pb au menage du repertoire de calcul" == 0);
1893        }
1894     }
1895 //  On n'a pas demande de faire le menage de son contenu : on sort en erreur :
1896     else
1897     {
1898       if (etatMenage == 0)
1899       {
1900         DIR *dp;
1901         struct dirent *dirp;
1902         dp  = opendir(DirCompute.str().c_str());
1903         bool result = true;
1904         while ((dirp = readdir(dp)) != NULL && result )
1905         {
1906               std::string file_name(dirp->d_name);
1907               result = file_name.empty() || file_name == "." || file_name == ".."; //if any file - break and return false
1908         }
1909         closedir(dp);
1910         if ( result == false)
1911         {
1912             SALOME::ExceptionStruct es;
1913             es.type = SALOME::BAD_PARAM;
1914             std::string text = "Directory : " + DirCompute.str() + "is not empty";
1915             es.text = CORBA::string_dup(text.c_str());
1916             throw SALOME::SALOME_Exception(es);
1917             ASSERT("Directory is not empty" == 0);
1918         }
1919       }
1920     }
1921   }
1922
1923   return CORBA::string_dup( DirCompute.str().c_str() );
1924 }
1925 //=============================================================================
1926 // Calcul d'une iteration : gestion du repertoire de calcul de l'iteration parent
1927 //=============================================================================
1928 char* HOMARD_Gen_i::ComputeDirPa(HOMARD::HOMARD_Cas_var myCase, HOMARD::HOMARD_Iteration_var myIteration)
1929 {
1930   MESSAGE ( "ComputeDirPa : repertoires pour le calcul" );
1931   // Le repertoire du cas
1932   const char* nomDirCase = myCase->GetDirName();
1933   MESSAGE ( ". nomDirCase = " << nomDirCase );
1934
1935   // Le sous-repertoire de l'iteration precedente
1936
1937   const char* nomIterationParent = myIteration->GetIterParentName();
1938   HOMARD::HOMARD_Iteration_var myIterationParent = myContextMap[GetCurrentStudyID()]._mesIterations[nomIterationParent];
1939   const char* nomDirItPa = myIterationParent->GetDirName();
1940   std::stringstream DirComputePa ;
1941   DirComputePa << nomDirCase << "/" << nomDirItPa;
1942   MESSAGE( ". nomDirItPa = " << nomDirItPa);
1943   MESSAGE( ". DirComputePa = " << DirComputePa.str() );
1944
1945   return CORBA::string_dup( DirComputePa.str().c_str() );
1946 }
1947 //=============================================================================
1948 // Calcul d'une iteration : ecriture des zones dans le fichier de configuration
1949 //=============================================================================
1950 void HOMARD_Gen_i::DriverTexteZone(HOMARD::HOMARD_Hypothesis_var myHypo, HomardDriver* myDriver)
1951 {
1952   MESSAGE ( "... DriverTexteZone" );
1953   HOMARD::listeZonesHypo* ListZone = myHypo->GetZones();
1954   int numberOfZonesx2 = ListZone->length();
1955   int NumZone ;
1956
1957   for (int iaux = 0; iaux< numberOfZonesx2; iaux++)
1958   {
1959     std::string ZoneName = std::string((*ListZone)[iaux]);
1960     MESSAGE ( "... ZoneName = " << ZoneName);
1961     HOMARD::HOMARD_Zone_var myZone = myContextMap[GetCurrentStudyID()]._mesZones[ZoneName];
1962     ASSERT(!CORBA::is_nil(myZone));
1963
1964     int ZoneType = myZone->GetType();
1965     std::string TypeUsestr = std::string((*ListZone)[iaux+1]);
1966     int TypeUse = atoi( TypeUsestr.c_str() );
1967     MESSAGE ( "... ZoneType = " << ZoneType << ", TypeUse = "<<TypeUse);
1968     NumZone = iaux/2 + 1 ;
1969     HOMARD::double_array* zone = myZone->GetCoords();
1970     if ( ZoneType == 2 or ( ZoneType>=11 and ZoneType <=13 ) ) // Cas d un parallelepipede ou d'un rectangle
1971     {
1972       myDriver->TexteZone(NumZone, ZoneType, TypeUse, (*zone)[0], (*zone)[1], (*zone)[2], (*zone)[3], (*zone)[4], (*zone)[5], 0., 0., 0.);
1973     }
1974     else if ( ZoneType == 4 ) // Cas d une sphere
1975     {
1976       myDriver->TexteZone(NumZone, ZoneType, TypeUse, (*zone)[0], (*zone)[1], (*zone)[2], (*zone)[3], 0., 0., 0., 0., 0.);
1977     }
1978     else if ( ZoneType == 5 or ( ZoneType>=31 and ZoneType <=33 ) ) // Cas d un cylindre ou d'un disque
1979     {
1980       myDriver->TexteZone(NumZone, ZoneType, TypeUse, (*zone)[0], (*zone)[1], (*zone)[2], (*zone)[3], (*zone)[4], (*zone)[5], (*zone)[6], (*zone)[7], 0.);
1981     }
1982     else if ( ZoneType == 7 or ( ZoneType>=61 and ZoneType <=63 ) ) // Cas d un tuyau ou disque perce
1983     {
1984       myDriver->TexteZone(NumZone, ZoneType, TypeUse, (*zone)[0], (*zone)[1], (*zone)[2], (*zone)[3], (*zone)[4], (*zone)[5], (*zone)[6], (*zone)[7], (*zone)[8]);
1985     }
1986     else { ASSERT("ZoneType est incorrect." == 0) ; }
1987     iaux += 1 ;
1988   }
1989   return ;
1990 }
1991 //=============================================================================
1992 // Calcul d'une iteration : ecriture des champs dans le fichier de configuration
1993 //=============================================================================
1994 void HOMARD_Gen_i::DriverTexteField(HOMARD::HOMARD_Iteration_var myIteration, HOMARD::HOMARD_Hypothesis_var myHypo, HomardDriver* myDriver)
1995 {
1996   MESSAGE ( "... DriverTexteField" );
1997 //  Le fichier du champ
1998   char* FieldFile = myIteration->GetFieldFile();
1999   MESSAGE ( ". FieldFile = " << FieldFile );
2000   if (strlen(FieldFile) == 0)
2001   {
2002     // GERALD -- QMESSAGE BOX
2003     std::cerr << "Le fichier du champ n'a pas ete fourni." << std::endl;
2004     ASSERT("The file for the field is not given." == 0);
2005   }
2006 //  Les caracteristiques d'instants
2007   int TimeStep = myIteration->GetTimeStep();
2008   MESSAGE( ". TimeStep = " << TimeStep );
2009   int Rank = myIteration->GetRank();
2010   MESSAGE( ". Rank = " << Rank );
2011 //  Les informations sur les champ
2012   HOMARD::InfosHypo* aInfosHypo = myHypo->GetField();
2013 //  Le nom
2014   const char* FieldName = aInfosHypo->FieldName;
2015 //  Les seuils
2016   int TypeThR = aInfosHypo->TypeThR;
2017   double ThreshR = aInfosHypo->ThreshR;
2018   int TypeThC = aInfosHypo->TypeThC;
2019   double ThreshC = aInfosHypo->ThreshC;
2020 //  Saut entre mailles ou non ?
2021   int UsField = aInfosHypo->UsField;
2022   MESSAGE( ". UsField = " << UsField );
2023 //  L'usage des composantes
2024   int UsCmpI = aInfosHypo->UsCmpI;
2025   MESSAGE( ". UsCmpI = " << UsCmpI );
2026 //
2027   myDriver->TexteField(FieldName, FieldFile, TimeStep, Rank, TypeThR, ThreshR, TypeThC, ThreshC, UsField, UsCmpI);
2028 //
2029 //  Les composantes
2030   HOMARD::listeComposantsHypo* mescompo = myHypo->GetListComp();
2031   int numberOfCompos = mescompo->length();
2032   MESSAGE( ". numberOfCompos = " << numberOfCompos );
2033   for (int NumeComp = 0; NumeComp< numberOfCompos; NumeComp++)
2034   {
2035     std::string nomCompo = std::string((*mescompo)[NumeComp]);
2036     MESSAGE( "... nomCompo = " << nomCompo );
2037     myDriver->TexteCompo(NumeComp, nomCompo);
2038   }
2039   return ;
2040 }
2041 //=============================================================================
2042 // Calcul d'une iteration : ecriture des frontieres dans le fichier de configuration
2043 //=============================================================================
2044 void HOMARD_Gen_i::DriverTexteBoundary(HOMARD::HOMARD_Cas_var myCase, HomardDriver* myDriver)
2045 {
2046   MESSAGE ( "... DriverTexteBoundary" );
2047   // On ecrit d'abord la definition des frontieres, puis les liens avec les groupes
2048   std::list<std::string>  ListeBoundaryTraitees ;
2049   HOMARD::ListBoundaryGroupType* ListBoundaryGroupType = myCase->GetBoundaryGroup();
2050   int numberOfitems = ListBoundaryGroupType->length();
2051   MESSAGE ( "... number of string for Boundary+Group = " << numberOfitems);
2052   int BoundaryOption = 1 ;
2053   int NumBoundaryAnalytical = 0 ;
2054   for (int NumBoundary = 0; NumBoundary< numberOfitems; NumBoundary=NumBoundary+2)
2055   {
2056     std::string BoundaryName = std::string((*ListBoundaryGroupType)[NumBoundary]);
2057     MESSAGE ( "... BoundaryName = " << BoundaryName);
2058     int A_faire = 1 ;
2059     std::list<std::string>::const_iterator it = ListeBoundaryTraitees.begin();
2060     while (it != ListeBoundaryTraitees.end())
2061     {
2062       MESSAGE ( "... BoundaryNameTraitee = " << *it);
2063       if ( BoundaryName == *it ) { A_faire = 0 ; }
2064       it++;
2065     }
2066     if ( A_faire == 1 )
2067     {
2068 // Caracteristiques de la frontiere
2069       HOMARD::HOMARD_Boundary_var myBoundary = myContextMap[GetCurrentStudyID()]._mesBoundarys[BoundaryName];
2070       ASSERT(!CORBA::is_nil(myBoundary));
2071       int BoundaryType = myBoundary->GetType();
2072       MESSAGE ( "... BoundaryType = " << BoundaryType );
2073 // Ecriture selon le type
2074       if (BoundaryType == 0) // Cas d une frontiere discrete
2075       {
2076         const char* MeshName = myBoundary->GetMeshName() ;
2077         const char* MeshFile = myBoundary->GetMeshFile() ;
2078         myDriver->TexteBoundaryDi( MeshName, MeshFile);
2079         BoundaryOption = BoundaryOption*2 ;
2080       }
2081       else // Cas d une frontiere analytique
2082       {
2083         NumBoundaryAnalytical++ ;
2084         HOMARD::double_array* coor = myBoundary->GetCoords();
2085         if (BoundaryType == 1) // Cas d un cylindre
2086         {
2087           myDriver->TexteBoundaryAn(BoundaryName, NumBoundaryAnalytical, BoundaryType, (*coor)[0], (*coor)[1], (*coor)[2], (*coor)[3], (*coor)[4], (*coor)[5], (*coor)[6]);
2088           BoundaryOption = BoundaryOption*3 ;
2089         }
2090         else if (BoundaryType == 2) // Cas d une sphere
2091         {
2092           myDriver->TexteBoundaryAn(BoundaryName, NumBoundaryAnalytical, BoundaryType, (*coor)[0], (*coor)[1], (*coor)[2], (*coor)[3], 0., 0., 0.);
2093           BoundaryOption = BoundaryOption*3 ;
2094         }
2095       }
2096 // Memorisation du traitement
2097       ListeBoundaryTraitees.push_back( BoundaryName );
2098     }
2099   }
2100   NumBoundaryAnalytical = 0 ;
2101   for (int NumBoundary = 0; NumBoundary< numberOfitems; NumBoundary=NumBoundary+2)
2102   {
2103     std::string BoundaryName = std::string((*ListBoundaryGroupType)[NumBoundary]);
2104     MESSAGE ( "... BoundaryName = " << BoundaryName);
2105     HOMARD::HOMARD_Boundary_var myBoundary = myContextMap[GetCurrentStudyID()]._mesBoundarys[BoundaryName];
2106     ASSERT(!CORBA::is_nil(myBoundary));
2107     int BoundaryType = myBoundary->GetType();
2108     MESSAGE ( "... BoundaryType = " << BoundaryType );
2109 //  Recuperation du nom du groupe
2110     std::string GroupName = std::string((*ListBoundaryGroupType)[NumBoundary+1]);
2111     MESSAGE ( "... GroupName = " << GroupName);
2112     if (BoundaryType == 0) // Cas d une frontiere discrete
2113     {
2114       if ( GroupName.size() > 0 ) { myDriver->TexteBoundaryDiGr ( GroupName ) ; }
2115     }
2116     else // Cas d une frontiere analytique
2117     {
2118       NumBoundaryAnalytical++ ;
2119       myDriver->TexteBoundaryAnGr ( BoundaryName, NumBoundaryAnalytical, GroupName ) ;
2120     }
2121   }
2122   myDriver->TexteBoundaryOption(BoundaryOption);
2123   return ;
2124 }
2125 //=============================================================================
2126 // Calcul d'une iteration : ecriture des interpolations dans le fichier de configuration
2127 //=============================================================================
2128 void HOMARD_Gen_i::DriverTexteFieldInterp(HOMARD::HOMARD_Iteration_var myIteration, HOMARD::HOMARD_Hypothesis_var myHypo, HomardDriver* myDriver)
2129 {
2130   MESSAGE ( "... DriverTexteFieldInterp" );
2131   int TypeFieldInterp = myHypo->GetTypeFieldInterp();
2132   if (TypeFieldInterp != 0)
2133   {
2134 //  Le fichier des champs
2135     char* FieldFile = myIteration->GetFieldFile();
2136     MESSAGE ( ". FieldFile = " << FieldFile );
2137     if (strlen(FieldFile) == 0)
2138     {
2139       // GERALD -- QMESSAGE BOX
2140       std::cerr << "Le fichier du champ n'a pas ete fourni." << std::endl;
2141       ASSERT("The file for the field is not given." == 0);
2142     }
2143   //  Les caracteristiques d'instants
2144     int TimeStep = myIteration->GetTimeStep();
2145     MESSAGE( ". TimeStep = " << TimeStep );
2146     int Rank = myIteration->GetRank();
2147     MESSAGE( ". Rank = " << Rank );
2148   //
2149     const char* MeshFile = myIteration->GetMeshFile();
2150     myDriver->TexteFieldInterp(TypeFieldInterp, FieldFile, MeshFile, TimeStep, Rank);
2151   //  Les champs
2152     if (TypeFieldInterp == 2)
2153     {
2154       HOMARD::listFieldInterpHypo* meschamps = myHypo->GetListFieldInterp();
2155       int numberOfFields = meschamps->length();
2156       MESSAGE( ". numberOfFields = " << numberOfFields );
2157       for (int NumeChamp = 0; NumeChamp< numberOfFields; NumeChamp++)
2158       {
2159         std::string nomChamp = std::string((*meschamps)[NumeChamp]);
2160         MESSAGE( "... nomChamp = " << nomChamp );
2161         myDriver->TexteFieldInterpName(NumeChamp, nomChamp);
2162       }
2163     }
2164   }
2165   return ;
2166 }
2167 //===========================================================================
2168 //===========================================================================
2169
2170
2171 //===========================================================================
2172 //===========================================================================
2173 // Publications
2174 //===========================================================================
2175 //===========================================================================
2176 SALOMEDS::SObject_ptr HOMARD_Gen_i::PublishInStudy(SALOMEDS::Study_ptr theStudy,
2177                                                    SALOMEDS::SObject_ptr theSObject,
2178                                                    CORBA::Object_ptr theObject,
2179                                                    const char* theName)
2180 {
2181   MESSAGE("PublishInStudy pour " << theName);
2182   SALOMEDS::SObject_var aResultSO;
2183   if (CORBA::is_nil(theStudy))
2184   {
2185     SALOME::ExceptionStruct es;
2186     es.type = SALOME::BAD_PARAM;
2187     es.text = "Invalid Study Context ";
2188     throw SALOME::SALOME_Exception(es);
2189     return 0;
2190   };
2191
2192 // Recuperation de l'objet correspondant, en essayant chacun des types possibles
2193 // Rq : Iteration est publiee ailleurs
2194   HOMARD::HOMARD_Cas_var        aCase  = HOMARD::HOMARD_Cas::_narrow(theObject);
2195   HOMARD::HOMARD_Hypothesis_var aHypo = HOMARD::HOMARD_Hypothesis::_narrow(theObject);
2196   HOMARD::HOMARD_Zone_var       aZone = HOMARD::HOMARD_Zone::_narrow(theObject);
2197   HOMARD::HOMARD_Boundary_var   aBoundary = HOMARD::HOMARD_Boundary::_narrow(theObject);
2198
2199    addInStudy(theStudy);
2200
2201 // Controle de la non publication d'un objet de meme nom
2202    if ((!aHypo->_is_nil()) or (!aZone->_is_nil()) or (!aBoundary->_is_nil()))
2203     {
2204       SALOMEDS::Study::ListOfSObject_var listSO = theStudy->FindObjectByName(theName, ComponentDataType());
2205       if (listSO->length() >= 1)
2206       {
2207           MESSAGE("This name "<<theName<<" is already used "<<listSO->length()<<" time(s)");
2208           std::cerr <<"This name "<<theName<<" is already used "<<listSO->length()<<" time(s)" << std::endl;
2209           aResultSO = listSO[0];
2210           return aResultSO._retn();
2211       }
2212     }
2213
2214   // Caracteristiques de l'etude
2215   SALOMEDS::StudyBuilder_var aStudyBuilder = theStudy->NewBuilder();
2216   aStudyBuilder->NewCommand();
2217   if(!aCase->_is_nil())
2218     aResultSO = PublishCaseInStudy(theStudy, aStudyBuilder, aCase, theName);
2219   else if(!aHypo->_is_nil())
2220     aResultSO = PublishHypotheseInStudy(theStudy, aStudyBuilder, aHypo, theName);
2221   else if(!aZone->_is_nil())
2222     aResultSO = PublishZoneInStudy(theStudy, aStudyBuilder, aZone, theName);
2223   else if(!aBoundary->_is_nil())
2224     aResultSO = PublishBoundaryInStudy(theStudy, aStudyBuilder, aBoundary, theName);
2225
2226     aStudyBuilder->CommitCommand();
2227   return aResultSO._retn();
2228 };
2229 //=============================================================================
2230 SALOMEDS::SObject_ptr HOMARD_Gen_i::PublishCaseInStudy(SALOMEDS::Study_ptr theStudy,
2231                                                        SALOMEDS::StudyBuilder_var aStudyBuilder,
2232                                                        HOMARD::HOMARD_Cas_ptr theObject, const char* theName)
2233 {
2234   MESSAGE("PublishCaseInStudy pour "<<theName);
2235   SALOMEDS::SObject_var aResultSO;
2236   SALOMEDS::GenericAttribute_var anAttr;
2237
2238   if (CORBA::is_nil(theObject)) {
2239     MESSAGE("HOMARD_Gen_i::theObject->_is_nil()");
2240     return aResultSO._retn();
2241   }
2242   if (theStudy->_is_nil()) {
2243     MESSAGE("HOMARD_Gen_i::theStudy->_is_nil()");
2244     return aResultSO._retn();
2245   }
2246
2247   SALOMEDS::SComponent_var theFatherHomard = theStudy->FindComponent(ComponentDataType());
2248   if (theFatherHomard->_is_nil())
2249   {
2250     MESSAGE("theFatherHomard->_is_nil()");
2251     return aResultSO._retn();
2252   }
2253
2254   aResultSO = aStudyBuilder->NewObject(theFatherHomard);
2255   PublishInStudyAttr(aStudyBuilder, aResultSO, theName, "CasHomard", "cas_calcule.png",
2256                      _orb->object_to_string(theObject) ) ;
2257   return aResultSO._retn();
2258 }
2259
2260 //=============================================================================
2261 SALOMEDS::SObject_ptr HOMARD_Gen_i::PublishZoneInStudy(SALOMEDS::Study_ptr theStudy,
2262                     SALOMEDS::StudyBuilder_var aStudyBuilder,
2263                    HOMARD::HOMARD_Zone_ptr theObject, const char* theName)
2264 {
2265   MESSAGE("PublishZoneStudy pour "<<theName);
2266   SALOMEDS::SObject_var aResultSO;
2267   SALOMEDS::GenericAttribute_var anAttr;
2268
2269   if (CORBA::is_nil(theObject))
2270   {
2271     MESSAGE("PublishZoneInStudy : theObject->_is_nil()");
2272     return aResultSO._retn();
2273   }
2274   if (theStudy->_is_nil())
2275   {
2276     MESSAGE("PublishZoneInStudy : theStudy->_is_nil()");
2277     return aResultSO._retn();
2278   }
2279   SALOMEDS::SComponent_var theFatherHomard = theStudy->FindComponent(ComponentDataType());
2280   if (theFatherHomard->_is_nil())
2281   {
2282     MESSAGE("PublishZoneInStudy : theFatherHomard->_is_nil()");
2283     return aResultSO._retn();
2284   }
2285
2286   // Caracteristique de la zone
2287   HOMARD::HOMARD_Zone_var myZone = myContextMap[GetCurrentStudyID()]._mesZones[theName];
2288   CORBA::Long ZoneType = myZone->GetType();
2289
2290   // On ajoute la categorie des zones dans l etude si necessaire
2291   SALOMEDS::SObject_var aSOZone;
2292   if (!theFatherHomard->FindSubObject(100, aSOZone))
2293   {
2294     MESSAGE("Ajout de la categorie des zones");
2295     aSOZone = aStudyBuilder->NewObjectToTag(theFatherHomard, 100);
2296     PublishInStudyAttr(aStudyBuilder, aSOZone, "Zones", "ZoneList", "zone_icone_2.png", NULL ) ;
2297   }
2298   else { MESSAGE("La categorie des zones existe deja."); }
2299
2300   aResultSO = aStudyBuilder->NewObject(aSOZone);
2301   const char* icone ;
2302   switch (ZoneType)
2303   {
2304     case 11 :
2305     { }
2306     case 12 :
2307     { }
2308     case 13 :
2309     { icone = "boxdxy_2.png" ;
2310       break ;
2311     }
2312     case 2 :
2313     { icone = "boxdxyz_2.png" ;
2314       break ;
2315     }
2316     case 31 :
2317     { }
2318     case 32 :
2319     { }
2320     case 33 :
2321     { icone = "disk_2.png" ;
2322       break ;
2323      }
2324     case 4 :
2325     { icone = "spherepoint_2.png" ;
2326       break ;
2327     }
2328     case 5 :
2329     { icone = "cylinderpointvector_2.png" ;
2330       break ;
2331     }
2332     case 61 :
2333     { }
2334     case 62 :
2335     { }
2336     case 63 :
2337     { icone = "diskwithhole_2.png" ;
2338       break ;
2339      }
2340     case 7 :
2341     { icone = "pipe_2.png" ;
2342       break ;
2343     }
2344   }
2345   PublishInStudyAttr(aStudyBuilder, aResultSO, theName, "ZoneHomard", icone, _orb->object_to_string(theObject) ) ;
2346
2347   return aResultSO._retn();
2348 }
2349 //=============================================================================
2350 SALOMEDS::SObject_ptr HOMARD_Gen_i::PublishBoundaryInStudy(SALOMEDS::Study_ptr theStudy,
2351                    SALOMEDS::StudyBuilder_var aStudyBuilder,
2352                    HOMARD::HOMARD_Boundary_ptr theObject, const char* theName)
2353 {
2354   MESSAGE("PublishBoundaryStudy pour "<<theName);
2355   SALOMEDS::SObject_var aResultSO;
2356   SALOMEDS::GenericAttribute_var anAttr;
2357
2358   // Caracteristique de la Boundary
2359   HOMARD::HOMARD_Boundary_var myBoundary = myContextMap[GetCurrentStudyID()]._mesBoundarys[theName];
2360
2361   // On recupere le module pere dans l etude
2362   SALOMEDS::SComponent_var       theFatherHomard = theStudy->FindComponent(ComponentDataType());
2363   if (theFatherHomard->_is_nil())
2364   {
2365     MESSAGE("theFatherHomard->_is_nil()");
2366     return aResultSO._retn();
2367   }
2368
2369   // On ajoute la categorie des boundarys dans l etude si necessaire
2370   SALOMEDS::SObject_var aSOBoundary;
2371   if (!theFatherHomard->FindSubObject(101, aSOBoundary))
2372   {
2373     MESSAGE("Ajout de la categorie des boundarys");
2374     aSOBoundary = aStudyBuilder->NewObjectToTag(theFatherHomard, 101);
2375     PublishInStudyAttr(aStudyBuilder, aSOBoundary, "Boundaries", "BoundList", "zone_icone_2.png", NULL ) ;
2376   }
2377   else { MESSAGE("La categorie des boundarys existe deja."); }
2378
2379   aResultSO = aStudyBuilder->NewObject(aSOBoundary);
2380   CORBA::Long BoundaryType = myBoundary->GetType();
2381 //   MESSAGE("BoundaryType : "<<BoundaryType);
2382   const char* icone ;
2383   const char* value ;
2384   switch (BoundaryType)
2385   {
2386     case 0 :
2387     { value = "BoundaryDiHomard" ;
2388       icone = "mesh_tree_mesh.png" ;
2389       break;
2390     }
2391     case 1 :
2392     { value = "BoundaryAnHomard" ;
2393       icone = "cylinderpointvector_2.png" ;
2394       break;
2395     }
2396     case 2 :
2397     { value = "BoundaryAnHomard" ;
2398       icone = "spherepoint_2.png" ;
2399       break;
2400     }
2401   }
2402   PublishInStudyAttr(aStudyBuilder, aResultSO, theName, value, icone, _orb->object_to_string(theObject));
2403   return aResultSO._retn();
2404 }
2405
2406 //=============================================================================
2407 SALOMEDS::SObject_ptr HOMARD_Gen_i::PublishHypotheseInStudy(SALOMEDS::Study_ptr theStudy,
2408                    SALOMEDS::StudyBuilder_var aStudyBuilder,
2409                    HOMARD::HOMARD_Hypothesis_ptr theObject, const char* theName)
2410 {
2411   MESSAGE("PublishHypotheseInStudy pour "<<theName);
2412   SALOMEDS::SObject_var aResultSO;
2413   SALOMEDS::GenericAttribute_var anAttr;
2414
2415   // On recupere le module pere dans l etude
2416   // On ajoute la categorie des hypotheses dans l etude si necessaire
2417   SALOMEDS::SComponent_var theFatherHomard = theStudy->FindComponent(ComponentDataType());
2418   if (theFatherHomard->_is_nil())
2419   {
2420     MESSAGE("theFatherHomard->_is_nil()");
2421     return aResultSO._retn();
2422   }
2423   SALOMEDS::SObject_var aSOHypothese;
2424   if (!theFatherHomard->FindSubObject(0, aSOHypothese))
2425   {
2426     MESSAGE("Ajout de la categorie des hypotheses");
2427     aSOHypothese = aStudyBuilder->NewObjectToTag(theFatherHomard, 0);
2428     PublishInStudyAttr(aStudyBuilder, aSOHypothese, "Hypothesis", "HypoList","hypotheses.png", NULL);
2429   }
2430   else { MESSAGE("La categorie des hypotheses existe deja."); }
2431
2432 // Creation du resultat dans l'etude
2433   aResultSO = aStudyBuilder->NewObject(aSOHypothese);
2434   PublishInStudyAttr(aStudyBuilder, aResultSO, theName, "HypoHomard", NULL, _orb->object_to_string(theObject) ) ;
2435
2436   return aResultSO._retn();
2437 }
2438 //===========================================================================
2439 void HOMARD_Gen_i::PublishInStudyAttr(SALOMEDS::StudyBuilder_var aStudyBuilder,
2440                                       SALOMEDS::SObject_var aResultSO,
2441                                       const char* name, const char* value, const char* icone, const char* ior)
2442 {
2443   SALOMEDS::GenericAttribute_var anAttr ;
2444 //  Ajout du nom
2445   if ( name != NULL )
2446   {
2447     anAttr = aStudyBuilder->FindOrCreateAttribute(aResultSO, "AttributeName");
2448     SALOMEDS::AttributeName_var aNameAttrib = SALOMEDS::AttributeName::_narrow(anAttr);
2449     aNameAttrib->SetValue(name);
2450   }
2451
2452 //  Ajout du commentaire
2453   if ( value != NULL )
2454   {
2455     anAttr = aStudyBuilder->FindOrCreateAttribute(aResultSO, "AttributeComment");
2456     SALOMEDS::AttributeComment_var aCommentAttrib = SALOMEDS::AttributeComment::_narrow(anAttr);
2457     aCommentAttrib->SetValue(value);
2458   }
2459
2460 //  Ajout de l'icone
2461   if ( icone != NULL  )
2462   {
2463     anAttr = aStudyBuilder->FindOrCreateAttribute(aResultSO,"AttributePixMap");
2464     SALOMEDS::AttributePixMap_var aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
2465     aPixmap->SetPixMap(icone);
2466   }
2467
2468 //  Ajout de l ior
2469   if ( ior != NULL  )
2470   {
2471     anAttr = aStudyBuilder->FindOrCreateAttribute(aResultSO, "AttributeIOR");
2472     SALOMEDS::AttributeIOR_var anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
2473     anIOR->SetValue(ior);
2474   }
2475 };
2476
2477 //=====================================================================================
2478 void HOMARD_Gen_i::PublishBoundaryUnderCase(const char* CaseName, const char* BoundaryName)
2479 {
2480   MESSAGE ( "PublishBoundaryUnderCase : CaseName = " << CaseName << ", BoundaryName= " << BoundaryName );
2481
2482   HOMARD::HOMARD_Cas_var myCase = myContextMap[GetCurrentStudyID()]._mesCas[CaseName];
2483   ASSERT(!CORBA::is_nil(myCase));
2484   SALOMEDS::SObject_var aCaseSO = SALOMEDS::SObject::_narrow(myCurrentStudy->FindObjectIOR(_orb->object_to_string(myCase)));
2485   ASSERT(!CORBA::is_nil(aCaseSO));
2486
2487   HOMARD::HOMARD_Boundary_var myBoundary = myContextMap[GetCurrentStudyID()]._mesBoundarys[BoundaryName];
2488   ASSERT(!CORBA::is_nil(myBoundary));
2489   SALOMEDS::SObject_var aBoundarySO = SALOMEDS::SObject::_narrow(myCurrentStudy->FindObjectIOR(_orb->object_to_string(myBoundary)));
2490   ASSERT(!CORBA::is_nil(aBoundarySO));
2491
2492   SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
2493
2494   aStudyBuilder->NewCommand();
2495
2496   SALOMEDS::SObject_var aSubSO = aStudyBuilder->NewObject(aCaseSO);
2497   aStudyBuilder->Addreference(aSubSO, aBoundarySO);
2498
2499   aStudyBuilder->CommitCommand();
2500
2501 };
2502 //=============================================================================
2503 void HOMARD_Gen_i::PublishResultInSmesh(const char* NomFich, CORBA::Long IconeType)
2504 {
2505   MESSAGE( "PublishResultInSmesh " << NomFich);
2506   if (CORBA::is_nil(myCurrentStudy))
2507   {
2508       SALOME::ExceptionStruct es;
2509       es.type = SALOME::BAD_PARAM;
2510       es.text = "Invalid Study Context ";
2511       throw SALOME::SALOME_Exception(es);
2512       return ;
2513   };
2514
2515 // Le module SMESH est-il actif ?
2516   SALOMEDS::SObject_var aSmeshSO = myCurrentStudy->FindComponent("SMESH");
2517 //
2518   if (!CORBA::is_nil(aSmeshSO))
2519   {
2520 // On verifie que le fichier n est pas deja publie
2521     SALOMEDS::ChildIterator_var aIter = myCurrentStudy->NewChildIterator(aSmeshSO);
2522     for (; aIter->More(); aIter->Next())
2523     {
2524        SALOMEDS::SObject_var  aSO = aIter->Value();
2525        SALOMEDS::GenericAttribute_var aGAttr;
2526        if (aSO->FindAttribute(aGAttr,"AttributeExternalFileDef"))
2527        {
2528            SALOMEDS::AttributeExternalFileDef_var anAttr = SALOMEDS::AttributeExternalFileDef::_narrow(aGAttr);
2529            CORBA::String_var value=anAttr->Value();
2530            if (strcmp((const char*)value,NomFich) == 0)
2531            {
2532                 // GERALD -- QMESSAGE BOX
2533                 std::cerr << "fichier : "<< NomFich << " deja publie "<< std::endl;
2534                 return;
2535            }
2536        }
2537      }
2538   }
2539
2540 // On enregistre le fichier
2541   MESSAGE( "Enregistrement du fichier");
2542   SALOME_LifeCycleCORBA* myLCC = new SALOME_LifeCycleCORBA(_NS);
2543   SMESH::SMESH_Gen_var aSmeshEngine = SMESH::SMESH_Gen::_narrow(myLCC->FindOrLoad_Component("FactoryServer","SMESH"));
2544   ASSERT(!CORBA::is_nil(aSmeshEngine));
2545   aSmeshEngine->SetCurrentStudy(myCurrentStudy);
2546   SMESH::DriverMED_ReadStatus theStatus;
2547   //aSmeshEngine->CreateMeshesFromMED(NomFich, theStatus);
2548
2549 // On met a jour les attributs AttributeExternalFileDef et AttributePixMap
2550   SMESH::mesh_array* mesMaillages=aSmeshEngine->CreateMeshesFromMED(NomFich, theStatus);
2551   for (int i = 0; i < mesMaillages->length();  i++)
2552   {
2553     MESSAGE( ". Mise a jour des attributs du maillage");
2554     SMESH::SMESH_Mesh_var monMaillage= (*mesMaillages)[i];
2555     SALOMEDS::SObject_var aSO=SALOMEDS::SObject::_narrow(myCurrentStudy->FindObjectIOR(_orb->object_to_string(monMaillage)));
2556     SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
2557     SALOMEDS::GenericAttribute_var aGAttr = aStudyBuilder->FindOrCreateAttribute(aSO, "AttributeExternalFileDef");
2558     SALOMEDS::AttributeExternalFileDef_var anAttr = SALOMEDS::AttributeExternalFileDef::_narrow(aGAttr);
2559     anAttr->SetValue(NomFich);
2560     SALOMEDS::GenericAttribute_var aPixMap = aStudyBuilder->FindOrCreateAttribute(aSO, "AttributePixMap" );
2561     SALOMEDS::AttributePixMap_var anAttr2 = SALOMEDS::AttributePixMap::_narrow(aPixMap);
2562 //  IconeType = 0 : fichier issu d'une importation
2563 //  IconeType = 1 : fichier issu d'une execution HOMARD
2564     const char* icone ;
2565     if ( IconeType == 0 ) { icone = "mesh_tree_importedmesh.png" ; }
2566     else                  { icone = "mesh_tree_mesh.png" ; }
2567     anAttr2->SetPixMap( icone );
2568   }
2569
2570 }
2571 //=============================================================================
2572 void HOMARD_Gen_i::DeleteResultInSmesh(const char* NomFich, const char* MeshName)
2573 {
2574   MESSAGE (" DeleteResultInSmesh pour le maillage " << MeshName << " dans le fichier " << NomFich );
2575   if (CORBA::is_nil(myCurrentStudy))
2576   {
2577       SALOME::ExceptionStruct es;
2578       es.type = SALOME::BAD_PARAM;
2579       es.text = "Invalid Study Context ";
2580       throw SALOME::SALOME_Exception(es);
2581       return ;
2582   };
2583
2584 // Le module SMESH est-il actif ?
2585   SALOMEDS::SObject_var aSmeshSO = myCurrentStudy->FindComponent("SMESH");
2586 //
2587   if (CORBA::is_nil(aSmeshSO))
2588   {
2589       return ;
2590   };
2591 // On verifie que le fichier est deja publie
2592   SALOMEDS::StudyBuilder_var myBuilder = myCurrentStudy->NewBuilder();
2593   SALOMEDS::ChildIterator_var aIter = myCurrentStudy->NewChildIterator(aSmeshSO);
2594   for (; aIter->More(); aIter->Next())
2595   {
2596      SALOMEDS::SObject_var  aSO = aIter->Value();
2597      SALOMEDS::GenericAttribute_var aGAttr;
2598      if (aSO->FindAttribute(aGAttr,"AttributeExternalFileDef"))
2599      {
2600        SALOMEDS::AttributeExternalFileDef_var anAttr = SALOMEDS::AttributeExternalFileDef::_narrow(aGAttr);
2601        CORBA::String_var value=anAttr->Value();
2602        if (strcmp((const char*)value,NomFich) == 0)
2603        {
2604          if (aSO->FindAttribute(aGAttr,"AttributeName"))
2605          {
2606            SALOMEDS::AttributeName_var anAttr2 = SALOMEDS::AttributeName::_narrow(aGAttr);
2607            CORBA::String_var value2=anAttr2->Value();
2608            if (strcmp((const char*)value2,MeshName) == 0)
2609            {
2610              myBuilder->RemoveObjectWithChildren( aSO ) ;
2611            }
2612          }
2613        }
2614      }
2615   }
2616
2617 }
2618 //=============================================================================
2619 void HOMARD_Gen_i::PublishFileUnderIteration(const char* NomIter, const char* NomFich, const char* Commentaire)
2620 {
2621 //   MESSAGE (" PublishFileUnderIteration pour l'iteration " << NomIter << " du fichier " << NomFich << " avec le commentaire " << Commentaire );
2622   if (CORBA::is_nil(myCurrentStudy))
2623   {
2624       SALOME::ExceptionStruct es;
2625       es.type = SALOME::BAD_PARAM;
2626       es.text = "Invalid Study Context ";
2627       throw SALOME::SALOME_Exception(es);
2628       return ;
2629   };
2630
2631   HOMARD::HOMARD_Iteration_var myIteration = myContextMap[GetCurrentStudyID()]._mesIterations[NomIter];
2632   if (CORBA::is_nil(myIteration))
2633   {
2634       SALOME::ExceptionStruct es;
2635       es.type = SALOME::BAD_PARAM;
2636       es.text = "Invalid Iteration ";
2637       throw SALOME::SALOME_Exception(es);
2638       return ;
2639   };
2640   SALOMEDS::SObject_var aIterSO=SALOMEDS::SObject::_narrow(myCurrentStudy->FindObjectIOR(_orb->object_to_string(myIteration)));
2641   if (CORBA::is_nil(myIteration))
2642   {
2643       SALOME::ExceptionStruct es;
2644       es.type = SALOME::BAD_PARAM;
2645       es.text = "Invalid Iteration Study Object";
2646       throw SALOME::SALOME_Exception(es);
2647       return ;
2648   };
2649
2650   SALOMEDS::StudyBuilder_var aStudyBuilder = myCurrentStudy->NewBuilder();
2651
2652   aStudyBuilder->NewCommand();
2653
2654   SALOMEDS::SObject_var aSubSO = aStudyBuilder->NewObject(aIterSO);
2655 // Pour les fichiers med, on affiche une icone de maillage
2656 // Pour les fichiers qui sont texte, on affiche une icone de fichier texte 'texte'
2657 // Le reperage se fait par la 1ere lettre du commentaire : I pour Iteration n
2658   const char* icone ;
2659   const char* ior = " " ;
2660   if ( Commentaire[0] == 'I' )
2661   { icone = "med.png" ; }
2662   else
2663   { icone = "texte_2.png" ; }
2664   PublishInStudyAttr(aStudyBuilder, aSubSO, NomFich, Commentaire, icone, ior ) ;
2665
2666   aStudyBuilder->CommitCommand();
2667 }
2668 //=============================================================================
2669 //=============================================================================
2670 //
2671 //=============================================================================
2672 //=============================================================================
2673 // Next functions are inherited from SALOMEDS::Driver interface
2674 //=============================================================================
2675 //=============================================================================
2676 SALOMEDS::TMPFile* HOMARD_Gen_i::Save(SALOMEDS::SComponent_ptr theComponent,
2677                                       const char* theURL,
2678                                       CORBA::Boolean isMultiFile)
2679 {
2680   MESSAGE (" Save for theURL = "<< theURL);
2681   SALOMEDS::TMPFile_var aStreamFile;
2682
2683   // get temporary directory name
2684   std::string tmpDir = isMultiFile ? std::string(theURL) : SALOMEDS_Tool::GetTmpDir();
2685
2686   SALOMEDS::Study_var aStudy = theComponent->GetStudy();
2687   StudyContext& context = myContextMap[ aStudy->StudyId() ];
2688
2689   // HOMARD data file name
2690   std::string aFileName = "";
2691   if (isMultiFile)
2692     aFileName = SALOMEDS_Tool::GetNameFromPath(aStudy->URL());
2693   aFileName += "_HOMARD.dat";
2694
2695   // initialize sequence of file names
2696   SALOMEDS::ListOfFileNames_var aFileSeq = new SALOMEDS::ListOfFileNames;
2697   aFileSeq->length(1);
2698   aFileSeq[0] = CORBA::string_dup(aFileName.c_str()) ;
2699
2700   // get full path to the data file
2701   aFileName = tmpDir + aFileName;
2702
2703   // save data
2704   // -> create file
2705   std::ofstream f(aFileName.c_str());
2706
2707   // clear temporary id map
2708   context._idmap.clear();
2709
2710   int id = 1;
2711
2712   // -> save cases
2713   std::map<std::string, HOMARD::HOMARD_Cas_var>::const_iterator it_case;
2714   for (it_case = context._mesCas.begin(); it_case != context._mesCas.end(); ++it_case) {
2715     HOMARD::HOMARD_Cas_var aCas = it_case->second;
2716     PortableServer::ServantBase_var aServant = GetServant(aCas);
2717     HOMARD_Cas_i* aCasServant = dynamic_cast<HOMARD_Cas_i*>(aServant.in());
2718     if (aCasServant) {
2719       f << HOMARD::GetSignature(HOMARD::Case) << aCasServant->Dump() << std::endl;
2720       context._idmap[id++] = dynamic_cast<PortableServer::ServantBase*>(aCasServant);
2721     }
2722   }
2723   // -> save zones
2724   std::map<std::string, HOMARD::HOMARD_Zone_var>::const_iterator it_zone;
2725   for (it_zone = context._mesZones.begin(); it_zone != context._mesZones.end(); ++it_zone) {
2726     HOMARD::HOMARD_Zone_var aZone = it_zone->second;
2727     PortableServer::ServantBase_var aServant = GetServant(aZone);
2728     HOMARD_Zone_i* aZoneServant = dynamic_cast<HOMARD_Zone_i*>(aServant.in());
2729     if (aZoneServant) {
2730       f << HOMARD::GetSignature(HOMARD::Zone) << aZoneServant->Dump() << std::endl;
2731       context._idmap[id++] = dynamic_cast<PortableServer::ServantBase*>(aZoneServant);
2732     }
2733   }
2734   // -> save hypotheses
2735   std::map<std::string, HOMARD::HOMARD_Hypothesis_var>::const_iterator it_hypo;
2736   for (it_hypo = context._mesHypotheses.begin(); it_hypo != context._mesHypotheses.end(); ++it_hypo) {
2737     HOMARD::HOMARD_Hypothesis_var aHypo = it_hypo->second;
2738     PortableServer::ServantBase_var aServant = GetServant(aHypo);
2739     HOMARD_Hypothesis_i* aHypoServant = dynamic_cast<HOMARD_Hypothesis_i*>(aServant.in());
2740     if (aHypoServant) {
2741       f << HOMARD::GetSignature(HOMARD::Hypothesis) << aHypoServant->Dump() << std::endl;
2742       context._idmap[id++] = dynamic_cast<PortableServer::ServantBase*>(aHypoServant);
2743     }
2744   }
2745   // -> save iterations
2746   std::map<std::string, HOMARD::HOMARD_Iteration_var>::const_iterator it_iter;
2747   for (it_iter = context._mesIterations.begin(); it_iter != context._mesIterations.end(); ++it_iter) {
2748     HOMARD::HOMARD_Iteration_var aIter = it_iter->second;
2749     PortableServer::ServantBase_var aServant = GetServant(aIter);
2750     HOMARD_Iteration_i* aIterServant = dynamic_cast<HOMARD_Iteration_i*>(aServant.in());
2751     if (aIterServant) {
2752       f << HOMARD::GetSignature(HOMARD::Iteration) << aIterServant->Dump() << std::endl;
2753       context._idmap[id++] = dynamic_cast<PortableServer::ServantBase*>(aIterServant);
2754     }
2755   }
2756   // -> save boundaries
2757   std::map<std::string, HOMARD::HOMARD_Boundary_var>::const_iterator it_boundary;
2758   for (it_boundary = context._mesBoundarys.begin(); it_boundary != context._mesBoundarys.end(); ++it_boundary) {
2759     HOMARD::HOMARD_Boundary_var aBoundary = it_boundary->second;
2760     PortableServer::ServantBase_var aServant = GetServant(aBoundary);
2761     HOMARD_Boundary_i* aBoundaryServant = dynamic_cast<HOMARD_Boundary_i*>(aServant.in());
2762     if (aBoundaryServant) {
2763       f << HOMARD::GetSignature(HOMARD::Boundary) << aBoundaryServant->Dump() << std::endl;
2764       context._idmap[id++] = dynamic_cast<PortableServer::ServantBase*>(aBoundaryServant);
2765     }
2766   }
2767   // -> close file
2768   MESSAGE ("close file");
2769   f.close();
2770
2771   // put temporary files to the stream
2772   MESSAGE ("put temporary files to the stream");
2773   aStreamFile = SALOMEDS_Tool::PutFilesToStream(tmpDir.c_str(), aFileSeq.in(), isMultiFile);
2774
2775   // remove temporary files
2776   MESSAGE ("remove temporary files");
2777   if (!isMultiFile) SALOMEDS_Tool::RemoveTemporaryFiles(tmpDir.c_str(), aFileSeq.in(), true);
2778
2779   // return data stream
2780   MESSAGE ("return data stream");
2781   return aStreamFile._retn();
2782 };
2783
2784 //===========================================================================
2785 SALOMEDS::TMPFile* HOMARD_Gen_i::SaveASCII(SALOMEDS::SComponent_ptr theComponent,
2786                                            const char* theURL,
2787                                            CORBA::Boolean isMultiFile)
2788 {
2789   // No specific ASCII persistence
2790   SALOMEDS::TMPFile_var aStreamFile = Save(theComponent, theURL, isMultiFile);
2791   return aStreamFile._retn();
2792 };
2793
2794 //===========================================================================
2795 CORBA::Boolean HOMARD_Gen_i::Load(SALOMEDS::SComponent_ptr theComponent,
2796                                    const SALOMEDS::TMPFile& theStream,
2797                                    const char* theURL,
2798                                    CORBA::Boolean isMultiFile)
2799 {
2800   MESSAGE (" Load pour theURL = "<< theURL);
2801   SALOMEDS::Study_var aStudy = theComponent->GetStudy();
2802
2803   // set current study
2804   if (myCurrentStudy->_is_nil() || aStudy->StudyId() != myCurrentStudy->StudyId())
2805     SetCurrentStudy(aStudy);
2806
2807   // get temporary directory name
2808   std::string tmpDir = isMultiFile ? std::string(theURL) : SALOMEDS_Tool::GetTmpDir();
2809
2810   // Convert the stream into sequence of files to process
2811   SALOMEDS::ListOfFileNames_var aFileSeq = SALOMEDS_Tool::PutStreamToFiles(theStream,
2812                                                                             tmpDir.c_str(),
2813                                                                             isMultiFile);
2814   // HOMARD data file name
2815   std::string aFileName = "";
2816   if (isMultiFile)
2817     aFileName = SALOMEDS_Tool::GetNameFromPath(aStudy->URL());
2818   aFileName = tmpDir + aFileName + "_HOMARD.dat";
2819
2820   StudyContext& context = myContextMap[ aStudy->StudyId() ];
2821
2822   // save data
2823   // -> create file
2824   std::ifstream f(aFileName.c_str());
2825
2826   // clear context
2827   context._mesCas.clear();
2828   context._mesHypotheses.clear();
2829   context._mesIterations.clear();
2830   context._mesZones.clear();
2831   context._mesBoundarys.clear();
2832   context._idmap.clear();
2833
2834   int id = 1;
2835   std::string line;
2836
2837   while (f) {
2838     std::getline(f, line);
2839     std::string caseSignature = HOMARD::GetSignature(HOMARD::Case);
2840     std::string zoneSignature = HOMARD::GetSignature(HOMARD::Zone);
2841     std::string iterSignature = HOMARD::GetSignature(HOMARD::Iteration);
2842     std::string hypoSignature = HOMARD::GetSignature(HOMARD::Hypothesis);
2843     std::string bounSignature = HOMARD::GetSignature(HOMARD::Boundary);
2844     if (line.substr(0, caseSignature.size()) == caseSignature) {
2845       // re-create case
2846       MESSAGE (" Recreation du cas" );
2847       HOMARD::HOMARD_Cas_var aCase = newCase();
2848       PortableServer::ServantBase_var aServant = GetServant(aCase);
2849       HOMARD_Cas_i* aCaseServant = dynamic_cast<HOMARD_Cas_i*>(aServant.in());
2850       if (aCaseServant && aCaseServant->Restore(line.substr(caseSignature.size()))) {
2851         context._mesCas[aCase->GetName()] = aCase;
2852         context._idmap[id] = dynamic_cast<PortableServer::ServantBase*>(aCaseServant);
2853       }
2854     }
2855     else if (line.substr(0, zoneSignature.size()) == zoneSignature) {
2856       MESSAGE (" Recreation de la zone" );
2857       // re-create zone
2858       HOMARD::HOMARD_Zone_var aZone = newZone();
2859       PortableServer::ServantBase_var aServant = GetServant(aZone);
2860       HOMARD_Zone_i* aZoneServant = dynamic_cast<HOMARD_Zone_i*>(aServant.in());
2861       if (aZoneServant && aZoneServant->Restore(line.substr(zoneSignature.size()))) {
2862         context._mesZones[aZone->GetName()] = aZone;
2863         context._idmap[id] = dynamic_cast<PortableServer::ServantBase*>(aZoneServant);
2864       }
2865     }
2866     else if (line.substr(0, iterSignature.size()) == iterSignature) {
2867       // re-create iteration
2868       MESSAGE (" Recreation de l iteration" );
2869       HOMARD::HOMARD_Iteration_var aIter = newIteration();
2870       PortableServer::ServantBase_var aServant = GetServant(aIter);
2871       HOMARD_Iteration_i* aIterServant = dynamic_cast<HOMARD_Iteration_i*>(aServant.in());
2872       if (aIterServant && aIterServant->Restore(line.substr(iterSignature.size()))) {
2873         context._mesIterations[aIter->GetName()] = aIter;
2874         context._idmap[id] = dynamic_cast<PortableServer::ServantBase*>(aIterServant);
2875       }
2876     }
2877     else if (line.substr(0, hypoSignature.size()) == hypoSignature) {
2878       // re-create hypothesis
2879       MESSAGE (" Recreation de l hypothese" );
2880       HOMARD::HOMARD_Hypothesis_var aHypo = newHypothesis();
2881       PortableServer::ServantBase_var aServant = GetServant(aHypo);
2882       HOMARD_Hypothesis_i* aHypoServant = dynamic_cast<HOMARD_Hypothesis_i*>(aServant.in());
2883       if (aHypoServant && aHypoServant->Restore(line.substr(hypoSignature.size()))) {
2884         context._mesHypotheses[aHypo->GetName()] = aHypo;
2885         context._idmap[id] = dynamic_cast<PortableServer::ServantBase*>(aHypoServant);
2886       }
2887     }
2888     else if (line.substr(0, bounSignature.size()) == bounSignature) {
2889       // re-create boundary
2890       MESSAGE (" Recreation de la frontiere" );
2891       HOMARD::HOMARD_Boundary_var aBoundary = newBoundary();
2892       PortableServer::ServantBase_var aServant = GetServant(aBoundary);
2893       HOMARD_Boundary_i* aBoundaryServant = dynamic_cast<HOMARD_Boundary_i*>(aServant.in());
2894       if (aBoundaryServant && aBoundaryServant->Restore(line.substr(bounSignature.size()))) {
2895         context._mesBoundarys[aBoundary->GetName()] = aBoundary;
2896         context._idmap[id] = dynamic_cast<PortableServer::ServantBase*>(aBoundaryServant);
2897       }
2898     }
2899     id++;
2900   }
2901
2902   // -> close file
2903   f.close();
2904
2905   // Remove temporary files created from the stream
2906   if (!isMultiFile)
2907     SALOMEDS_Tool::RemoveTemporaryFiles(tmpDir.c_str(), aFileSeq.in(), true);
2908
2909   return true;
2910 };
2911
2912 //===========================================================================
2913 CORBA::Boolean HOMARD_Gen_i::LoadASCII(SALOMEDS::SComponent_ptr theComponent,
2914                                         const SALOMEDS::TMPFile& theStream,
2915                                         const char* theURL,
2916                                         CORBA::Boolean isMultiFile)
2917 {
2918   // No specific ASCII persistence
2919   return Load(theComponent, theStream, theURL, isMultiFile);
2920 };
2921
2922 //===========================================================================
2923 void HOMARD_Gen_i::Close(SALOMEDS::SComponent_ptr theComponent)
2924 {
2925   if (theComponent->GetStudy()->StudyId() == GetCurrentStudyID()) {
2926     // clearing study context should be done here:
2927     // - destroy all servants and related CORBA objects
2928     // ... (TODO)
2929     // - remove context from myContextMap
2930     myContextMap.erase(theComponent->GetStudy()->StudyId());
2931     // - nullify myCurrentStudy
2932     myCurrentStudy = SALOMEDS::Study::_nil();
2933   }
2934 };
2935
2936 //===========================================================================
2937 char* HOMARD_Gen_i::ComponentDataType()
2938 {
2939   return CORBA::string_dup("HOMARD");
2940 };
2941
2942 //===========================================================================
2943 char* HOMARD_Gen_i::IORToLocalPersistentID(SALOMEDS::SObject_ptr theSObject,
2944                                             const char* IORString,
2945                                             CORBA::Boolean isMultiFile,
2946                                             CORBA::Boolean isASCII)
2947 {
2948   CORBA::String_var aString("");
2949   if (!CORBA::is_nil(theSObject) && strcmp(IORString, "") != 0) {
2950     StudyContext context = myContextMap[ theSObject->GetStudy()->StudyId() ];
2951     CORBA::Object_var anObj = _orb->string_to_object(IORString);
2952     if (!CORBA::is_nil(anObj)) {
2953       PortableServer::ServantBase_var aServant = GetServant(anObj);
2954       PortableServer::ServantBase* aStorable = dynamic_cast<PortableServer::ServantBase*>(aServant.in());
2955       if (aStorable) {
2956         std::map<int, PortableServer::ServantBase*>::const_iterator it;
2957         for (it = context._idmap.begin(); it != context._idmap.end(); ++it) {
2958           if (it->second == aStorable) {
2959             std::stringstream os;
2960             os << it->first;
2961             aString = CORBA::string_dup(os.str().c_str());
2962           }
2963         }
2964       }
2965     }
2966   }
2967   return aString._retn();
2968 };
2969
2970 //===========================================================================
2971 char* HOMARD_Gen_i::LocalPersistentIDToIOR(SALOMEDS::SObject_ptr theSObject,
2972                                             const char* aLocalPersistentID,
2973                                             CORBA::Boolean isMultiFile,
2974                                             CORBA::Boolean isASCII)
2975 {
2976   CORBA::String_var aString("");
2977   if (!CORBA::is_nil(theSObject) && strcmp(aLocalPersistentID, "") != 0) {
2978     StudyContext context = myContextMap[ theSObject->GetStudy()->StudyId() ];
2979     int id = atoi(aLocalPersistentID);
2980     if (id > 0 && context._idmap.find(id) != context._idmap.end()) {
2981       CORBA::Object_var object = _poa->servant_to_reference(context._idmap[ id ]);
2982       if (!CORBA::is_nil(object)) {
2983         aString = _orb->object_to_string(object);
2984       }
2985     }
2986   }
2987   return aString._retn();
2988 };
2989
2990 //===========================================================================
2991 CORBA::Boolean HOMARD_Gen_i::CanPublishInStudy(CORBA::Object_ptr theIOR)
2992 {
2993   if(CORBA::is_nil(myCurrentStudy))
2994     return false;
2995
2996   HOMARD::HOMARD_Cas_var aCas = HOMARD::HOMARD_Cas::_narrow(theIOR);
2997   if(!aCas->_is_nil())
2998     return true;
2999
3000   HOMARD::HOMARD_Hypothesis_var aHypo = HOMARD::HOMARD_Hypothesis::_narrow(theIOR);
3001   if(!aHypo->_is_nil())
3002     return true;
3003
3004   HOMARD::HOMARD_Zone_var aZone = HOMARD::HOMARD_Zone::_narrow(theIOR);
3005   if(!aZone->_is_nil())
3006     return true;
3007
3008   HOMARD::HOMARD_Boundary_var aBoundary = HOMARD::HOMARD_Boundary::_narrow(theIOR);
3009   if(!aBoundary->_is_nil())
3010     return true;
3011
3012   /* Iteration is not published directly
3013   HOMARD::HOMARD_Iteration_var aIter = HOMARD::HOMARD_Iteration::_narrow(theIOR);
3014   if(!aIter->_is_nil())
3015     return true;
3016   */
3017   return false;
3018 };
3019
3020 //===========================================================================
3021 CORBA::Boolean HOMARD_Gen_i::CanCopy(SALOMEDS::SObject_ptr theObject)
3022 {
3023   // No Copy/Paste support
3024   return false;
3025 };
3026
3027 //===========================================================================
3028 SALOMEDS::TMPFile* HOMARD_Gen_i::CopyFrom(SALOMEDS::SObject_ptr theObject,
3029                                            CORBA::Long& theObjectID)
3030 {
3031   // No Copy/Paste support
3032   SALOMEDS::TMPFile_var aStreamFile = new SALOMEDS::TMPFile(0);
3033   return aStreamFile._retn();
3034 };
3035
3036 //===========================================================================
3037 CORBA::Boolean  HOMARD_Gen_i::CanPaste(const char *theComponentName,
3038                                         CORBA::Long theObjectID)
3039 {
3040   // No Copy/Paste support
3041   return false;
3042 };
3043
3044 //===========================================================================
3045 SALOMEDS::SObject_ptr HOMARD_Gen_i::PasteInto(const SALOMEDS::TMPFile& theStream,
3046                                                CORBA::Long theObjectID,
3047                                                SALOMEDS::SObject_ptr theSObject)
3048 {
3049   // No Copy/Paste support
3050   SALOMEDS::SObject_var aResultSO;
3051   return aResultSO._retn();
3052 };
3053
3054 //===========================================================================
3055 PortableServer::ServantBase_var HOMARD_Gen_i::GetServant(CORBA::Object_ptr theObject)
3056 {
3057   PortableServer::Servant aServant = 0;
3058   if (!CORBA::is_nil(theObject)) {
3059     try {
3060       aServant = _poa->reference_to_servant(theObject);
3061     }
3062     catch (...) {
3063     }
3064   }
3065   return aServant;
3066 }
3067
3068 //==========================================================================
3069 Engines::TMPFile* HOMARD_Gen_i::DumpPython(CORBA::Object_ptr theStudy,
3070                                        CORBA::Boolean isPublished,
3071                                        CORBA::Boolean isMultiFile,
3072                                        CORBA::Boolean& isValidScript)
3073 {
3074    MESSAGE ("Entree dans DumpPython");
3075    isValidScript=1;
3076    SALOMEDS::Study_var aStudy = SALOMEDS::Study::_narrow(theStudy);
3077    if(CORBA::is_nil(aStudy))
3078      return new Engines::TMPFile(0);
3079
3080    SALOMEDS::SObject_var aSO = aStudy->FindComponent("HOMARD");
3081    if(CORBA::is_nil(aSO))
3082       return new Engines::TMPFile(0);
3083
3084    std::string aScript = "\"\"\"\n";
3085    aScript += "Python script for HOMARD\n";
3086    aScript += "Copyright EDF-R&D 2013\n";
3087    aScript += "\"\"\"\n";
3088    aScript += "__revision__ = \"V1.2\"\n";
3089    aScript += "import HOMARD\n";
3090    if( isMultiFile )
3091       aScript += "import salome\n";
3092    aScript += "homard = salome.lcc.FindOrLoadComponent('FactoryServer','HOMARD')\n";
3093    if( isMultiFile ) {
3094       aScript += "def RebuildData(theStudy):\n";
3095       aScript += "\thomard.SetCurrentStudy(theStudy)\n";
3096    }
3097    else
3098       aScript += "\thomard.SetCurrentStudy(salome.myStudy)\n";
3099    MESSAGE (". Au depart \n"<<aScript);
3100
3101
3102    if (myContextMap[GetCurrentStudyID()]._mesBoundarys.size() > 0)
3103    {
3104     MESSAGE (". Ecritures des frontieres");
3105     aScript += "#\n# Creation of the boundaries";
3106     aScript +=  "\n# ==========================";
3107    }
3108    std::map<std::string, HOMARD::HOMARD_Boundary_var>::const_iterator it_boundary;
3109    for (it_boundary  = myContextMap[GetCurrentStudyID()]._mesBoundarys.begin();
3110         it_boundary != myContextMap[GetCurrentStudyID()]._mesBoundarys.end(); ++it_boundary)
3111    {
3112     HOMARD::HOMARD_Boundary_var maBoundary = (*it_boundary).second;
3113     CORBA::String_var dumpCorbaBoundary = maBoundary->GetDumpPython();
3114     std::string dumpBoundary = dumpCorbaBoundary.in();
3115     MESSAGE (dumpBoundary<<"\n");
3116     aScript += dumpBoundary;
3117    }
3118
3119
3120    if (myContextMap[GetCurrentStudyID()]._mesZones.size() > 0)
3121    {
3122     MESSAGE (". Ecritures des zones");
3123     aScript += "#\n# Creation of the zones";
3124     aScript +=  "\n# =====================";
3125    }
3126    std::map<std::string, HOMARD::HOMARD_Zone_var>::const_iterator it_zone;
3127    for ( it_zone  = myContextMap[GetCurrentStudyID()]._mesZones.begin();
3128          it_zone != myContextMap[GetCurrentStudyID()]._mesZones.end(); ++it_zone)
3129    {
3130     HOMARD::HOMARD_Zone_var maZone = (*it_zone).second;
3131     CORBA::String_var dumpCorbaZone = maZone->GetDumpPython();
3132     std::string dumpZone = dumpCorbaZone.in();
3133     MESSAGE (dumpZone<<"\n");
3134     aScript += dumpZone;
3135    }
3136
3137
3138    if (myContextMap[GetCurrentStudyID()]._mesHypotheses.size() > 0)
3139    {
3140     MESSAGE (". Ecritures des hypotheses");
3141     aScript += "#\n# Creation of the hypotheses";
3142     aScript +=  "\n# ==========================";
3143    }
3144    std::map<std::string, HOMARD::HOMARD_Hypothesis_var>::const_iterator it_hypo;
3145    for ( it_hypo  = myContextMap[GetCurrentStudyID()]._mesHypotheses.begin();
3146          it_hypo != myContextMap[GetCurrentStudyID()]._mesHypotheses.end(); it_hypo++)
3147    {
3148     HOMARD::HOMARD_Hypothesis_var monHypo = (*it_hypo).second;
3149     CORBA::String_var dumpCorbaHypo = monHypo->GetDumpPython();
3150     std::string dumpHypo = dumpCorbaHypo.in();
3151     MESSAGE (dumpHypo<<"\n");
3152     aScript += dumpHypo;
3153    }
3154
3155
3156    if (myContextMap[GetCurrentStudyID()]._mesCas.size() > 0)
3157    {
3158     MESSAGE (". Ecritures des cas");
3159     aScript += "#\n# Creation of the cases";
3160     aScript += "\n# =====================";
3161    }
3162    std::map<std::string, HOMARD::HOMARD_Cas_var>::const_iterator it_cas;
3163    for (it_cas  = myContextMap[GetCurrentStudyID()]._mesCas.begin();
3164         it_cas != myContextMap[GetCurrentStudyID()]._mesCas.end(); it_cas++)
3165         {
3166            std::string nomCas = (*it_cas).first;
3167            std::string dumpCas = std::string("\n# Creation of the case ") ;
3168            dumpCas +=  nomCas + std::string("\n");
3169            dumpCas += std::string("\t") + nomCas;
3170            dumpCas += std::string(" = homard.CreateCase('") + nomCas + std::string("', '");
3171
3172            HOMARD::HOMARD_Cas_var myCase = (*it_cas).second;
3173            CORBA::String_var cIter0= myCase->GetIter0Name();
3174            std::string iter0 = cIter0.in();
3175
3176            HOMARD::HOMARD_Iteration_var myIteration = myContextMap[GetCurrentStudyID()]._mesIterations[iter0];
3177            CORBA::String_var cMesh0= myIteration->GetMeshFile();
3178            std::string mesh0 = cMesh0.in();
3179            CORBA::String_var cMeshName0= myIteration->GetMeshName();
3180            std::string meshName0 = cMeshName0.in();
3181            dumpCas += meshName0 + std::string("', '")+ mesh0 + std::string("')\n");
3182            CORBA::String_var dumpCorbaCase = myCase->GetDumpPython();
3183            std::string dumpCas2= dumpCorbaCase.in();
3184
3185            MESSAGE (dumpCas<<dumpCas2<<"\n");
3186            aScript += dumpCas + dumpCas2;
3187         };
3188
3189
3190    if (myContextMap[GetCurrentStudyID()]._mesIterations.size() > 0)
3191    {
3192     MESSAGE (". Ecritures des iterations");
3193     aScript += "#\n# Creation of the iterations" ;
3194     aScript += "\n# ==========================";
3195    }
3196    std::map<std::string, HOMARD::HOMARD_Iteration_var>::const_iterator it_iter;
3197    for (it_iter  = myContextMap[GetCurrentStudyID()]._mesIterations.begin();
3198         it_iter != myContextMap[GetCurrentStudyID()]._mesIterations.end(); ++it_iter)
3199    {
3200     HOMARD::HOMARD_Iteration_var aIter = (*it_iter).second;
3201     CORBA::String_var dumpCorbaIter = aIter->GetDumpPython();
3202     std::string dumpIter = dumpCorbaIter.in();
3203     MESSAGE (dumpIter<<"\n");
3204     aScript += dumpIter;
3205    }
3206
3207   MESSAGE (". Ecritures finales");
3208   if( isMultiFile )
3209     aScript += "\n\tpass";
3210   aScript += "\n";
3211
3212   if( !isMultiFile ) // remove unnecessary tabulation
3213     aScript = RemoveTabulation( aScript );
3214
3215 //   MESSAGE ("A ecrire \n"<<aScript);
3216   const size_t aLen = strlen(aScript.c_str());
3217   char* aBuffer = new char[aLen+1];
3218   strcpy(aBuffer, aScript.c_str());
3219
3220   CORBA::Octet* anOctetBuf =  (CORBA::Octet*)aBuffer;
3221   Engines::TMPFile_var aStreamFile = new Engines::TMPFile(aLen+1, aLen+1, anOctetBuf, 1);
3222
3223   MESSAGE ("Sortie de DumpPython");
3224   return aStreamFile._retn();
3225 }
3226
3227
3228 //=============================================================================
3229 //=============================================================================
3230 // Utilitaires
3231 //=============================================================================
3232 //=============================================================================
3233 void HOMARD_Gen_i::IsValidStudy( )
3234 {
3235 //   MESSAGE( "IsValidStudy" );
3236   if (CORBA::is_nil(myCurrentStudy))
3237   {
3238     SALOME::ExceptionStruct es;
3239     es.type = SALOME::BAD_PARAM;
3240     es.text = "Invalid Study Context";
3241     throw SALOME::SALOME_Exception(es);
3242   };
3243   return ;
3244 }
3245
3246 //=============================================================================
3247 CORBA::Boolean HOMARD_Gen_i::VerifieDir(const char* nomDir)
3248 {
3249   std::map<std::string, HOMARD::HOMARD_Cas_var>::const_iterator it;
3250   for (it = myContextMap[GetCurrentStudyID()]._mesCas.begin();
3251   it != myContextMap[GetCurrentStudyID()]._mesCas.end(); it++)
3252   {
3253    if (std::string(nomDir) == std::string(it->second->GetDirName())) return false;
3254   }
3255   return true;
3256 }
3257 /*//=============================================================================
3258 void SALOMEException( std::string message )
3259 {
3260   SALOME::ExceptionStruct es;
3261   es.type = SALOME::BAD_PARAM;
3262   es.text = message;
3263   throw SALOME::SALOME_Exception(es);
3264   return ;
3265 }*/
3266 //=============================================================================
3267 char* HOMARD_Gen_i::getVersion()
3268 {
3269 #if HOMARD_DEVELOPMENT
3270   return CORBA::string_dup(HOMARD_VERSION_STR"dev");
3271 #else
3272   return CORBA::string_dup(HOMARD_VERSION_STR);
3273 #endif
3274 }
3275
3276 //=============================================================================
3277 extern "C"
3278 {
3279   PortableServer::ObjectId* HOMARDEngine_factory(CORBA::ORB_ptr orb,
3280                                                   PortableServer::POA_ptr poa,
3281                                                   PortableServer::ObjectId* contId,
3282                                                   const char* instanceName,
3283                                                   const char* interfaceName)
3284   {
3285     MESSAGE("PortableServer::ObjectId* HOMARDEngine_factory()");
3286     HOMARD_Gen_i* myHOMARD_Gen = new HOMARD_Gen_i(orb, poa, contId, instanceName, interfaceName);
3287     return myHOMARD_Gen->getId();
3288   }
3289 }