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