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