Salome HOME
aeed1e4b8c35db49388fa076d59d63fbc6a791ac
[modules/smesh.git] / src / SMESH_I / SMESH_Gen_i.cxx
1 using namespace std;
2 //=============================================================================
3 // File      : SMESH_Gen_i.cxx
4 // Created   : lun mai  6 13:41:35 CEST 2002
5 // Author    : Paul RASCLE, EDF
6 // Project   : SALOME
7 // Copyright : EDF 2002
8 // $Header$
9 //=============================================================================
10 using namespace std;
11
12 #include <TopExp.hxx>
13 #include <TopExp_Explorer.hxx>
14 #include <TopoDS.hxx>
15 #include <TopoDS_Iterator.hxx>
16 #include <TopoDS_Compound.hxx>
17 #include <TopoDS_CompSolid.hxx>
18 #include <TopoDS_Solid.hxx>
19 #include <TopoDS_Shell.hxx>
20 #include <TopoDS_Face.hxx>
21 #include <TopoDS_Wire.hxx>
22 #include <TopoDS_Edge.hxx>
23 #include <TopoDS_Vertex.hxx>
24 #include <TopoDS_Shape.hxx>
25 #include <TopTools_MapOfShape.hxx>
26 #include <gp_Pnt.hxx>
27 #include <BRep_Tool.hxx>
28 #include <TCollection_AsciiString.hxx>
29
30 #include <fstream>
31 #include <stdio.h>
32
33 #include "SMESH_Gen_i.hxx"
34 #include "SMESH_Mesh_i.hxx"
35 #include "SMESH_LocalLength_i.hxx"
36 #include "SMESH_NumberOfSegments_i.hxx"
37 #include "SMESH_MaxElementArea_i.hxx"
38
39 #include "SMESHDS_Document.hxx"
40
41 #include "Document_Reader.h"
42 #include "DriverMED_W_SMESHDS_Mesh.h"
43 #include "DriverMED_R_SMESHDS_Mesh.h"
44 #include "DriverMED_R_SMESHDS_Document.h"
45 #include "DriverUNV_R_SMESHDS_Document.h"
46 #include "DriverDAT_R_SMESHDS_Document.h"
47
48 #include "Utils_CorbaException.hxx"
49 #include "utilities.h"
50
51 #include "SALOMEDS_Tool.hxx"
52 #include "SALOME_NamingService.hxx"
53 #include "SALOME_LifeCycleCORBA.hxx"
54 #include "Utils_SINGLETON.hxx"
55 #include "OpUtil.hxx"
56
57 //#include <TopAbs_ShapeEnum.hxx>
58
59 #include "GEOM_Client.hxx"
60
61 #include <map>
62
63 #define NUM_TMP_FILES 4
64
65 // Tags definition 
66 long Tag_HypothesisRoot  = 1;
67 long Tag_AlgorithmsRoot  = 2;
68
69 long Tag_RefOnShape      = 1;
70 long Tag_RefOnAppliedHypothesis = 2;
71 long Tag_RefOnAppliedAlgorithms = 3;
72
73 long Tag_SubMeshOnVertex = 4;
74 long Tag_SubMeshOnEdge = 5;
75 long Tag_SubMeshOnFace = 6;
76 long Tag_SubMeshOnSolid = 7;
77 long Tag_SubMeshOnCompound = 8;
78
79
80 //=============================================================================
81 /*!
82  *  default constructor: not for use
83  */
84 //=============================================================================
85
86 SMESH_Gen_i::SMESH_Gen_i()
87 {
88   MESSAGE("SMESH_Gen_i default constructor");
89   // ****
90 }
91
92 //=============================================================================
93 /*!
94  *  Standard constructor, used with Container.
95  */
96 //=============================================================================
97
98 SMESH_Gen_i::SMESH_Gen_i(CORBA::ORB_ptr orb,
99               PortableServer::POA_ptr poa,
100               PortableServer::ObjectId * contId, 
101               const char *instanceName, 
102                          const char *interfaceName) :
103   Engines_Component_i(orb, poa, contId, instanceName, interfaceName)
104 {
105   MESSAGE("activate object");
106   _thisObj = this ;
107   _id = _poa->activate_object(_thisObj);
108
109   _ShapeReader = NULL;
110   _localId = 0;  // number of created objects & local id 
111
112 }
113
114 //=============================================================================
115 /*!
116  *  Standard destructor
117  */
118 //=============================================================================
119
120 SMESH_Gen_i::~SMESH_Gen_i()
121 {
122   MESSAGE("~SMESH_Gen_i");
123   // ****
124 }
125   
126 //=============================================================================
127 /*!
128  *  
129  */
130 //=============================================================================
131
132 SMESH::SMESH_Hypothesis_ptr SMESH_Gen_i::CreateHypothesis(const char* anHyp,
133                                                           CORBA::Long studyId)
134   throw (SALOME::SALOME_Exception)
135 {
136   MESSAGE("CreateHypothesis");
137
138   // create a new hypothesis object servant
139
140   SMESH_Hypothesis_i* myHypothesis_i = 0;
141   try
142     {
143       myHypothesis_i = _hypothesisFactory_i.Create(anHyp,
144                                                    studyId,
145                                                    &_impl);
146     }
147   catch (SALOME_Exception& S_ex)
148     {
149       THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
150     }
151
152   // activate the CORBA servant of hypothesis
153
154   SMESH::SMESH_Hypothesis_var hypothesis_i
155     = SMESH::SMESH_Hypothesis::_narrow(myHypothesis_i->_this());
156   return SMESH::SMESH_Hypothesis::_duplicate(hypothesis_i);
157 }
158   
159 //=============================================================================
160 /*!
161  *  
162  */
163 //=============================================================================
164
165 SMESH::SMESH_Mesh_ptr SMESH_Gen_i::Init(GEOM::GEOM_Gen_ptr geomEngine,
166                                         CORBA::Long studyId,
167                                         GEOM::GEOM_Shape_ptr aShape)
168   throw (SALOME::SALOME_Exception)
169 {
170   MESSAGE("Init");
171   // _narrow() duplicates the reference and checks the type
172   GEOM::GEOM_Gen_var geom = GEOM::GEOM_Gen::_narrow(geomEngine);
173   GEOM::GEOM_Shape_var myShape = GEOM::GEOM_Shape::_narrow(aShape);
174
175   if (CORBA::is_nil(geom))
176     THROW_SALOME_CORBA_EXCEPTION("bad geom reference", \
177                                  SALOME::BAD_PARAM);
178   if (CORBA::is_nil(myShape))
179     THROW_SALOME_CORBA_EXCEPTION("bad shape reference", \
180                                  SALOME::BAD_PARAM);
181
182   // Get or create the GEOM_Client instance
183
184   SMESH_Mesh_i* meshServant = 0;
185   try
186     {
187       if (! _ShapeReader) _ShapeReader = new GEOM_Client();
188       ASSERT(_ShapeReader);
189       
190       // explore main Shape, get local TopoDS_Shapes of all subShapes
191       //  SMESH_topo* myTopo = ExploreMainShape(geom, studyId, myShape);
192       
193       // Get studyContext_i, create it if it does'nt exist
194       
195       if (_mapStudyContext_i.find(studyId) == _mapStudyContext_i.end())
196         {
197           _mapStudyContext_i[studyId] = new StudyContext_iStruct;      
198         }
199       StudyContext_iStruct* myStudyContext = _mapStudyContext_i[studyId];
200       
201       // create a new mesh object servant, store it in a map in study context
202       
203       meshServant = new SMESH_Mesh_i(this,
204                                      geom,
205                                      studyId,
206                                      _localId);
207       myStudyContext->mapMesh_i[_localId] = meshServant;
208       _localId++;
209
210       // create a new mesh object
211       
212       TopoDS_Shape myLocShape  = _ShapeReader->GetShape(geom,myShape);
213       meshServant->SetImpl(_impl.Init(studyId, myLocShape));
214     }
215   catch (SALOME_Exception& S_ex)
216     {
217       THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
218     }
219
220   // activate the CORBA servant of Mesh
221   
222   SMESH::SMESH_Mesh_var mesh
223     = SMESH::SMESH_Mesh::_narrow(meshServant->_this());
224   
225   meshServant->SetIor(mesh);
226
227   return SMESH::SMESH_Mesh::_duplicate(mesh);
228 }
229   
230
231 //=============================================================================
232 /*!
233  *  
234  */
235 //=============================================================================
236
237 CORBA::Boolean SMESH_Gen_i::IsReadyToCompute(SMESH::SMESH_Mesh_ptr aMesh,
238                                              GEOM::GEOM_Shape_ptr aShape)
239   throw (SALOME::SALOME_Exception)
240 {
241   MESSAGE("SMESH_Gen_i::IsReadyToCompute");
242   return true;
243 }
244
245 //=============================================================================
246 /*!
247  *  
248  */
249 //=============================================================================
250
251   SMESH::long_array* 
252   SMESH_Gen_i::GetSubShapesId(GEOM::GEOM_Gen_ptr geomEngine,
253                               CORBA::Long studyId,
254                               GEOM::GEOM_Shape_ptr mainShape,
255                               const SMESH::shape_array& listOfSubShape)
256     throw (SALOME::SALOME_Exception)
257 {
258   MESSAGE("SMESH_Gen_i::GetSubShapesId");
259   SMESH::long_array_var shapesId = new SMESH::long_array;
260   set<int> setId;
261
262   GEOM::GEOM_Gen_var geom = GEOM::GEOM_Gen::_narrow(geomEngine);
263   GEOM::GEOM_Shape_var myShape = GEOM::GEOM_Shape::_narrow(mainShape);
264
265   if (CORBA::is_nil(geom))
266     THROW_SALOME_CORBA_EXCEPTION("bad geom reference", \
267                                  SALOME::BAD_PARAM);
268   if (CORBA::is_nil(myShape))
269     THROW_SALOME_CORBA_EXCEPTION("bad shape reference", \
270                                  SALOME::BAD_PARAM);
271
272   try
273     {
274       if (! _ShapeReader) _ShapeReader = new GEOM_Client();
275       ASSERT(_ShapeReader);
276       TopoDS_Shape myMainShape  = _ShapeReader->GetShape(geom,myShape);
277       TopTools_IndexedMapOfShape myIndexToShape;      
278       TopExp::MapShapes(myMainShape,myIndexToShape);
279
280       for (int i=0; i<listOfSubShape.length(); i++)
281         {
282           GEOM::GEOM_Shape_var aShape
283             = GEOM::GEOM_Shape::_narrow(listOfSubShape[i]);
284           if (CORBA::is_nil(aShape))
285             THROW_SALOME_CORBA_EXCEPTION("bad shape reference", \
286                                          SALOME::BAD_PARAM);
287           TopoDS_Shape locShape  = _ShapeReader->GetShape(geom,aShape);
288           for (TopExp_Explorer exp(locShape,TopAbs_FACE); exp.More(); exp.Next())
289             {
290               const TopoDS_Face& F = TopoDS::Face(exp.Current());
291               setId.insert(myIndexToShape.FindIndex(F));
292               SCRUTE(myIndexToShape.FindIndex(F));
293             }
294           for (TopExp_Explorer exp(locShape,TopAbs_EDGE); exp.More(); exp.Next())
295             {
296               const TopoDS_Edge& E = TopoDS::Edge(exp.Current());
297               setId.insert(myIndexToShape.FindIndex(E));
298               SCRUTE(myIndexToShape.FindIndex(E));
299             }
300           for (TopExp_Explorer exp(locShape,TopAbs_VERTEX); exp.More(); exp.Next())
301             {
302               const TopoDS_Vertex& V = TopoDS::Vertex(exp.Current());
303               setId.insert(myIndexToShape.FindIndex(V));
304               SCRUTE(myIndexToShape.FindIndex(V));
305             }
306         }
307       shapesId->length(setId.size());
308       set<int>::iterator iind;
309       int i=0;
310       for (iind = setId.begin(); iind != setId.end(); iind++)
311         {
312           SCRUTE((*iind));
313           shapesId[i] = (*iind);
314           SCRUTE(shapesId[i]);
315           i++;
316         }
317     }
318   catch (SALOME_Exception& S_ex)
319     {
320       THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
321     }
322
323   return shapesId._retn();
324 }
325
326 //=============================================================================
327 /*!
328  *  
329  */
330 //=============================================================================
331
332 CORBA::Boolean SMESH_Gen_i::Compute(SMESH::SMESH_Mesh_ptr aMesh,
333                                     GEOM::GEOM_Shape_ptr aShape)
334   throw (SALOME::SALOME_Exception)
335 {
336   MESSAGE("SMESH_Gen_i::Compute");
337   GEOM::GEOM_Shape_var myShape = GEOM::GEOM_Shape::_narrow(aShape);
338   if (CORBA::is_nil(myShape))
339     THROW_SALOME_CORBA_EXCEPTION("bad shape reference", \
340                                  SALOME::BAD_PARAM);
341
342   SMESH::SMESH_Mesh_var myMesh = SMESH::SMESH_Mesh::_narrow(aMesh);
343   if (CORBA::is_nil(myMesh))
344     THROW_SALOME_CORBA_EXCEPTION("bad Mesh reference", \
345                                  SALOME::BAD_PARAM);
346
347   bool ret = false;
348   try
349     {
350       
351       // get study context from studyId given by CORBA mesh object
352       
353       int studyId = myMesh->GetStudyId();
354       ASSERT(_mapStudyContext_i.find(studyId) != _mapStudyContext_i.end());
355       StudyContext_iStruct* myStudyContext = _mapStudyContext_i[studyId];
356       
357       // get local Mesh_i object with Id and study context 
358       
359       int meshId = myMesh->GetId();
360       ASSERT(myStudyContext->mapMesh_i.find(meshId) != myStudyContext->mapMesh_i.end());
361       SMESH_Mesh_i* meshServant = myStudyContext->mapMesh_i[meshId];
362       ASSERT(meshServant);
363       
364       // get local TopoDS_Shape
365       
366       GEOM::GEOM_Gen_var geom = meshServant->GetGeomEngine();
367       TopoDS_Shape myLocShape  = _ShapeReader->GetShape(geom,myShape);
368
369       // implementation compute
370       
371       ::SMESH_Mesh& myLocMesh = meshServant->GetImpl();
372       ret = _impl.Compute(myLocMesh, myLocShape);
373     }
374   catch (SALOME_Exception& S_ex)
375     {
376       MESSAGE("catch exception "<< S_ex.what());
377       return false;
378 //       THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
379     }
380
381   return ret;
382 }
383
384
385 //=============================================================================
386 /*!
387  *  
388  */
389 //=============================================================================
390
391 SALOMEDS::TMPFile* SMESH_Gen_i::Save(SALOMEDS::SComponent_ptr theComponent,
392                                      const char* theURL,
393                                      bool isMultiFile)
394 {
395   MESSAGE("SMESH_Gen_i::SAVE");
396   SALOMEDS::Study_var Study = theComponent->GetStudy(); 
397   int studyId;
398   
399   // Declare a byte stream
400   SALOMEDS::TMPFile_var aStreamFile;
401   
402   // Obtain a temporary dir
403   TCollection_AsciiString tmpDir = (isMultiFile)?TCollection_AsciiString((char*)theURL):SALOMEDS_Tool::GetTmpDir();
404
405   // Create a sequence of files processed
406   SALOMEDS::ListOfFileNames_var aFileSeq = new SALOMEDS::ListOfFileNames;
407   aFileSeq->length(NUM_TMP_FILES);
408
409   TCollection_AsciiString aStudyName(SALOMEDS_Tool::GetNameFromPath(theComponent->GetStudy()->URL()));
410
411   // Set names of temporary files
412   TCollection_AsciiString filename = aStudyName + TCollection_AsciiString("SMESH.hdf");
413   TCollection_AsciiString hypofile = aStudyName + TCollection_AsciiString("SMESH_Hypo.txt");
414   TCollection_AsciiString algofile = aStudyName + TCollection_AsciiString("SMESH_Algo.txt");
415   TCollection_AsciiString meshfile = aStudyName + TCollection_AsciiString("SMESH_Mesh.med");
416   aFileSeq[0] = CORBA::string_dup(filename.ToCString());
417   aFileSeq[1] = CORBA::string_dup(hypofile.ToCString());
418   aFileSeq[2] = CORBA::string_dup(algofile.ToCString());
419   aFileSeq[3] = CORBA::string_dup(meshfile.ToCString());
420   filename = tmpDir + filename;
421   hypofile = tmpDir + hypofile;
422   algofile = tmpDir + algofile;
423   meshfile = tmpDir + meshfile;
424
425   HDFfile * hdf_file;
426   map <int,HDFgroup*> hdf_group, hdf_subgroup;
427   map <int,HDFdataset*> hdf_dataset;
428   FILE* destFile;
429
430   SALOMEDS::ChildIterator_var itBig,it,itSM;
431   SALOMEDS::SObject_var mySObject,myBranch,mySObjectChild;
432   hdf_size size[1];
433   int longueur,cmpt_ds = 0,cmpt_it;
434   char *name_group,name_dataset[30],name_meshgroup[30];
435   bool ok,_found;
436   int cmpt_sm = 0, myTag;
437
438 //************* HDF file creation
439   hdf_file = new HDFfile(filename.ToCString());
440   hdf_file->CreateOnDisk();
441 //****************************
442
443   itBig = Study->NewChildIterator(theComponent);
444   for (; itBig->More();itBig->Next()) {
445     SALOMEDS::SObject_var gotBranch = itBig->Value();
446
447 //************branch 1 : hypothesis
448     if (gotBranch->Tag()==Tag_HypothesisRoot) { //hypothesis = tag 1
449
450       double length,maxElementsArea;
451       int numberOfSegments;
452
453       destFile = fopen( hypofile.ToCString() ,"w");
454       it = Study->NewChildIterator(gotBranch);
455       for (; it->More();it->Next()) {
456         mySObject = it->Value();
457         SALOMEDS::GenericAttribute_var anAttr;
458         SALOMEDS::AttributeIOR_var anIOR;
459         if ( mySObject->FindAttribute(anAttr, "AttributeIOR")) {
460           anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
461
462           SMESH::SMESH_Hypothesis_var myHyp = SMESH::SMESH_Hypothesis::_narrow(_orb->string_to_object(anIOR->Value()));
463           fprintf(destFile,"%s\n",myHyp->GetName());
464           
465           if (strcmp(myHyp->GetName(),"LocalLength")==0) {
466             SMESH::SMESH_LocalLength_var LL = SMESH::SMESH_LocalLength::_narrow( myHyp );
467             length = LL->GetLength();
468             fprintf(destFile,"%f\n",length);
469           }
470           else if (strcmp(myHyp->GetName(),"NumberOfSegments")==0) {
471             SMESH::SMESH_NumberOfSegments_var NOS = SMESH::SMESH_NumberOfSegments::_narrow( myHyp );
472             numberOfSegments = NOS->GetNumberOfSegments();
473             fprintf(destFile,"%d\n",numberOfSegments);
474           }
475           else if (strcmp(myHyp->GetName(),"MaxElementArea")==0) {
476             SMESH::SMESH_MaxElementArea_var MEA = SMESH::SMESH_MaxElementArea::_narrow( myHyp );
477             maxElementsArea = MEA->GetMaxElementArea();
478             fprintf(destFile,"%f\n",maxElementsArea);
479           }
480         }
481       }
482       fclose(destFile);
483
484 //writes the file name in the hdf file
485       longueur = hypofile.Length() +1;
486       name_group="Hypothesis";
487       //SCRUTE(name_group);
488       
489       size[0]=longueur;
490       hdf_group[1] = new HDFgroup(name_group,hdf_file);
491       hdf_group[1]->CreateOnDisk();
492       
493       hdf_dataset[cmpt_ds]=new HDFdataset(name_group,hdf_group[1],HDF_STRING,size,1);
494       hdf_dataset[cmpt_ds]->CreateOnDisk();
495       hdf_dataset[cmpt_ds]->WriteOnDisk(hypofile.ToCString());
496       hdf_dataset[cmpt_ds]->CloseOnDisk();
497       cmpt_ds++;
498       
499       hdf_group[1]->CloseOnDisk();
500       MESSAGE("End of Hypothesis Save");
501
502     }
503 //************branch 2 : algorithms
504     else if (gotBranch->Tag()==Tag_AlgorithmsRoot) {//algos = tag 2
505       
506       destFile = fopen( algofile.ToCString() ,"w");
507       it = Study->NewChildIterator(gotBranch);
508       for (; it->More();it->Next()) {
509         mySObject = it->Value();
510         SALOMEDS::GenericAttribute_var anAttr;
511         SALOMEDS::AttributeIOR_var anIOR;
512         if (mySObject->FindAttribute(anAttr, "AttributeIOR")) {
513           anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
514           SMESH::SMESH_Algo_var myAlgo = SMESH::SMESH_Algo::_narrow(_orb->string_to_object(anIOR->Value()));
515           fprintf(destFile,"%s\n",myAlgo->GetName());
516         }
517       }
518     
519       fclose(destFile);
520   
521 //writes the file name in the hdf file
522       longueur = algofile.Length() +1;
523       name_group="Algorithms";
524       //SCRUTE(name_group);
525       
526       size[0]=longueur;
527       hdf_group[2] = new HDFgroup(name_group,hdf_file);
528       hdf_group[2]->CreateOnDisk();
529       
530       hdf_dataset[cmpt_ds]=new HDFdataset(name_group,hdf_group[2],HDF_STRING,size,1);
531       hdf_dataset[cmpt_ds]->CreateOnDisk();
532       hdf_dataset[cmpt_ds]->WriteOnDisk(algofile.ToCString());
533       hdf_dataset[cmpt_ds]->CloseOnDisk();
534       cmpt_ds++;
535       
536       hdf_group[2]->CloseOnDisk();
537       MESSAGE("End of Algos Save");
538
539     }
540 //************branch 3 : meshes
541     else if (gotBranch->Tag()>=3) {//meshes = tag > 3
542
543       SALOMEDS::GenericAttribute_var anAttr;
544       SALOMEDS::AttributeIOR_var anIOR;
545       if (gotBranch->FindAttribute(anAttr, "AttributeIOR")) {
546         anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
547         
548         SMESH::SMESH_Mesh_var myMesh =  SMESH::SMESH_Mesh::_narrow(_orb->string_to_object(anIOR->Value())) ;
549         studyId = myMesh->GetStudyId();
550         SCRUTE(studyId);
551         
552         StudyContext_iStruct* myStudyContext = _mapStudyContext_i[studyId];
553         int meshId = myMesh->GetId();
554         SMESH_Mesh_i* meshServant = myStudyContext->mapMesh_i[meshId];
555         ::SMESH_Mesh& myLocMesh = meshServant->GetImpl();
556         Handle(SMESHDS_Mesh) mySMESHDSMesh = myLocMesh.GetMeshDS();
557         
558         SCRUTE(mySMESHDSMesh->NbNodes());
559         if (mySMESHDSMesh->NbNodes()>0) {//checks if the mesh is not empty
560           
561           DriverMED_W_SMESHDS_Mesh* myWriter = new DriverMED_W_SMESHDS_Mesh;
562           myWriter->SetFile(meshfile.ToCString());
563           
564           myWriter->SetMesh(mySMESHDSMesh);
565           myWriter->SetMeshId(gotBranch->Tag());
566           myWriter->Add();
567         }
568         else meshfile = "No data";
569         
570         //********** opening of the HDF group
571         sprintf(name_meshgroup,"Mesh %d",gotBranch->Tag());
572         SCRUTE(name_meshgroup);
573         hdf_group[gotBranch->Tag()] = new HDFgroup(name_meshgroup,hdf_file);
574         hdf_group[gotBranch->Tag()]->CreateOnDisk();
575         //********** 
576         
577         //********** file where the data are stored
578         longueur = strlen(meshfile.ToCString()) +1;
579         size[0]=longueur;
580         strcpy(name_dataset,"Mesh data");
581         hdf_dataset[cmpt_ds]=new HDFdataset(name_dataset,hdf_group[gotBranch->Tag()],HDF_STRING,size,1);
582         hdf_dataset[cmpt_ds]->CreateOnDisk();
583         hdf_dataset[cmpt_ds]->WriteOnDisk(meshfile.ToCString());
584         hdf_dataset[cmpt_ds]->CloseOnDisk();
585         cmpt_ds++;
586         //********** 
587         
588         //********** ref on shape
589         Standard_CString  myRefOnObject="" ;
590         SALOMEDS::SObject_var myRef,myShape;
591         _found = gotBranch->FindSubObject(Tag_RefOnShape,myRef);
592         if (_found) {
593           ok = myRef->ReferencedObject(myShape);
594           myRefOnObject = myShape->GetID();
595           SCRUTE(myRefOnObject);
596           
597           longueur = strlen(myRefOnObject) +1;
598           if (longueur>1) {
599             size[0]=longueur;
600             strcpy(name_dataset,"Ref on shape");
601             hdf_dataset[cmpt_ds]=new HDFdataset(name_dataset,hdf_group[gotBranch->Tag()],HDF_STRING,size,1);
602             hdf_dataset[cmpt_ds]->CreateOnDisk();
603             hdf_dataset[cmpt_ds]->WriteOnDisk(myRefOnObject);
604             hdf_dataset[cmpt_ds]->CloseOnDisk();
605             cmpt_ds++;
606             
607           }
608         }
609         //********** 
610         
611         //********** ref on applied hypothesis
612         _found = gotBranch->FindSubObject(Tag_RefOnAppliedHypothesis,myBranch);
613         if (_found) {
614           
615           strcpy(name_meshgroup,"Applied Hypothesis");
616           hdf_subgroup[Tag_RefOnAppliedHypothesis] = new HDFgroup(name_meshgroup,hdf_group[gotBranch->Tag()]);
617           hdf_subgroup[Tag_RefOnAppliedHypothesis]->CreateOnDisk();
618           
619           it = Study->NewChildIterator(myBranch);
620           cmpt_it = 0;
621           for (; it->More();it->Next()) {
622             mySObject = it->Value();
623             ok = mySObject->ReferencedObject(myRef);
624             myRefOnObject = myRef->GetID();
625             
626             longueur = strlen(myRefOnObject) +1;
627             if (longueur>1) {
628               size[0]=longueur;
629               sprintf(name_dataset,"Hyp %d",cmpt_it);
630               hdf_dataset[cmpt_ds]=new HDFdataset(name_dataset,hdf_subgroup[Tag_RefOnAppliedHypothesis],HDF_STRING,size,1);
631               hdf_dataset[cmpt_ds]->CreateOnDisk();
632               hdf_dataset[cmpt_ds]->WriteOnDisk(myRefOnObject);
633               hdf_dataset[cmpt_ds]->CloseOnDisk();
634             }
635             cmpt_ds++;
636             cmpt_it++;
637           }
638           hdf_subgroup[Tag_RefOnAppliedHypothesis]->CloseOnDisk();
639         }
640         //********** 
641         
642         //********** ref on applied algorithms
643         _found = gotBranch->FindSubObject(Tag_RefOnAppliedAlgorithms,myBranch);
644         if (_found) {
645           
646           strcpy(name_meshgroup,"Applied Algorithms");
647           hdf_subgroup[Tag_RefOnAppliedAlgorithms] = new HDFgroup(name_meshgroup,hdf_group[gotBranch->Tag()]);
648           hdf_subgroup[Tag_RefOnAppliedAlgorithms]->CreateOnDisk();
649           
650           it = Study->NewChildIterator(myBranch);
651           cmpt_it = 0;
652           for (; it->More();it->Next()) {
653             mySObject = it->Value();
654             ok = mySObject->ReferencedObject(myRef);
655             myRefOnObject = myRef->GetID();
656             
657             longueur = strlen(myRefOnObject) +1;
658             if (longueur>1) {
659               size[0]=longueur;
660               sprintf(name_dataset,"Algo %d",cmpt_it);
661               hdf_dataset[cmpt_ds]=new HDFdataset(name_dataset,hdf_subgroup[Tag_RefOnAppliedAlgorithms],HDF_STRING,size,1);
662               hdf_dataset[cmpt_ds]->CreateOnDisk();
663               hdf_dataset[cmpt_ds]->WriteOnDisk(myRefOnObject);
664               hdf_dataset[cmpt_ds]->CloseOnDisk();
665             }
666             cmpt_ds++;
667             cmpt_it++;
668           }
669           hdf_subgroup[Tag_RefOnAppliedAlgorithms]->CloseOnDisk();
670         }
671         MESSAGE("end of algo applied");
672         //********** 
673         
674         //********** submeshes on subshapes
675         int myLevel1Tag;
676         for (int i=Tag_SubMeshOnVertex;i<=Tag_SubMeshOnCompound;i++) {
677           _found = gotBranch->FindSubObject(i,myBranch);
678           if (_found) {
679             if (i==Tag_SubMeshOnVertex)
680               strcpy(name_meshgroup,"SubMeshes On Vertex");
681             else if (i==Tag_SubMeshOnEdge)
682               strcpy(name_meshgroup,"SubMeshes On Edge");
683             else if (i==Tag_SubMeshOnFace)
684               strcpy(name_meshgroup,"SubMeshes On Face");
685             else if (i==Tag_SubMeshOnSolid)
686               strcpy(name_meshgroup,"SubMeshes On Solid");
687             else if (i==Tag_SubMeshOnCompound)
688               strcpy(name_meshgroup,"SubMeshes On Compound");
689
690             cmpt_sm++;
691             myLevel1Tag = 10+cmpt_sm;
692             hdf_subgroup[myLevel1Tag] = new HDFgroup(name_meshgroup,hdf_group[gotBranch->Tag()]);
693             hdf_subgroup[myLevel1Tag]->CreateOnDisk();
694             
695             itSM = Study->NewChildIterator(myBranch);
696             for (; itSM->More();itSM->Next()) {//Loop on all submeshes
697               mySObject = itSM->Value();
698               cmpt_sm++;
699               myTag = 10+cmpt_sm;
700               mySObject->FindAttribute(anAttr, "AttributeIOR");
701               anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
702               SMESH::SMESH_subMesh_var mySubMesh =  SMESH::SMESH_subMesh::_narrow(_orb->string_to_object(anIOR->Value())) ;
703
704               //sprintf(name_meshgroup,"SubMesh %d",myTag);
705               sprintf(name_meshgroup,"SubMesh %d",mySubMesh->GetId());
706               SCRUTE(name_meshgroup);
707               
708               hdf_subgroup[myTag] = new HDFgroup(name_meshgroup,hdf_subgroup[myLevel1Tag]);
709               hdf_subgroup[myTag]->CreateOnDisk();
710               
711               //********** ref on shape
712               Standard_CString  myRefOnObject="" ;
713               SALOMEDS::SObject_var myRef,myShape;
714               bool _found2;
715               _found2 = mySObject->FindSubObject(Tag_RefOnShape,myRef);
716               if (_found2) {
717                 ok = myRef->ReferencedObject(myShape);
718                 myRefOnObject = myShape->GetID();
719                 SCRUTE(myRefOnObject);
720                 
721                 longueur = strlen(myRefOnObject) +1;
722                 if (longueur>1) {
723                   size[0]=longueur;
724                   strcpy(name_dataset,"Ref on shape");
725                   hdf_dataset[cmpt_ds]=new HDFdataset(name_dataset,hdf_subgroup[myTag],HDF_STRING,size,1);
726                   hdf_dataset[cmpt_ds]->CreateOnDisk();
727                   hdf_dataset[cmpt_ds]->WriteOnDisk(myRefOnObject);
728                   hdf_dataset[cmpt_ds]->CloseOnDisk();
729                   cmpt_ds++;
730                 }
731               }
732               //********** 
733               
734               //********** ref on applied hypothesis
735               _found2 = mySObject->FindSubObject(Tag_RefOnAppliedHypothesis,myBranch);
736               if (_found2) {
737                 
738                 strcpy(name_meshgroup,"Applied Hypothesis");
739                 cmpt_sm++;
740                 hdf_subgroup[10+cmpt_sm] = new HDFgroup(name_meshgroup,hdf_subgroup[myTag]);
741                 hdf_subgroup[10+cmpt_sm]->CreateOnDisk();
742                 
743                 it = Study->NewChildIterator(myBranch);
744                 cmpt_it = 0;
745                 for (; it->More();it->Next()) {
746                   mySObjectChild = it->Value();
747                   ok = mySObjectChild->ReferencedObject(myRef);
748                   myRefOnObject = myRef->GetID();
749                   
750                   longueur = strlen(myRefOnObject) +1;
751                   if (longueur>1) {
752                     size[0]=longueur;
753                     sprintf(name_dataset,"Hyp %d",cmpt_it);
754                     SCRUTE(cmpt_it);
755                     hdf_dataset[cmpt_ds]=new HDFdataset(name_dataset,hdf_subgroup[10+cmpt_sm],HDF_STRING,size,1);
756                     hdf_dataset[cmpt_ds]->CreateOnDisk();
757                     hdf_dataset[cmpt_ds]->WriteOnDisk(myRefOnObject);
758                     hdf_dataset[cmpt_ds]->CloseOnDisk();
759                   }
760                   cmpt_ds++;
761                   cmpt_it++;
762                 }
763                 hdf_subgroup[10+cmpt_sm]->CloseOnDisk();
764               }
765               //********** 
766               
767               //********** ref on applied algorithms
768               _found2 = mySObject->FindSubObject(Tag_RefOnAppliedAlgorithms,myBranch);
769               SCRUTE(_found2);
770               if (_found2) {
771                 
772                 strcpy(name_meshgroup,"Applied Algorithms");
773                 cmpt_sm++;
774                 hdf_subgroup[10+cmpt_sm] = new HDFgroup(name_meshgroup,hdf_subgroup[myTag]);
775                 hdf_subgroup[10+cmpt_sm]->CreateOnDisk();
776                 
777                 it = Study->NewChildIterator(myBranch);
778                 cmpt_it = 0;
779                 for (; it->More();it->Next()) {
780                   mySObjectChild = it->Value();
781                   ok = mySObjectChild->ReferencedObject(myRef);
782                   myRefOnObject = myRef->GetID();
783                   
784                   longueur = strlen(myRefOnObject) +1;
785                   if (longueur>1) {
786                     size[0]=longueur;
787                     sprintf(name_dataset,"Algo %d",cmpt_it);
788                     hdf_dataset[cmpt_ds]=new HDFdataset(name_dataset,hdf_subgroup[10+cmpt_sm],HDF_STRING,size,1);
789                     hdf_dataset[cmpt_ds]->CreateOnDisk();
790                     hdf_dataset[cmpt_ds]->WriteOnDisk(myRefOnObject);
791                     hdf_dataset[cmpt_ds]->CloseOnDisk();
792                   }
793                   cmpt_ds++;
794                   cmpt_it++;
795                 }
796                 hdf_subgroup[10+cmpt_sm]->CloseOnDisk();
797               }
798               //MESSAGE("end of algo applied");
799               //********** 
800               
801               hdf_subgroup[myTag]->CloseOnDisk();
802             }
803             
804             hdf_subgroup[myLevel1Tag]->CloseOnDisk();
805           }
806           
807         }
808         //********** 
809         
810         //********** closing of the HDF group
811         hdf_group[gotBranch->Tag()]->CloseOnDisk();
812         MESSAGE("End of Mesh Save");
813         //********** 
814       }                  
815     }
816     MESSAGE("End of Meshes Save");
817   }
818
819   hdf_file->CloseOnDisk();
820   delete hdf_file;
821   hdf_file = 0;
822
823   // Convert temporary files to stream
824   aStreamFile = SALOMEDS_Tool::PutFilesToStream(tmpDir.ToCString(), aFileSeq.in(), isMultiFile);
825
826   // Remove temporary files and directory
827   if (isMultiFile) SALOMEDS_Tool::RemoveTemporaryFiles(tmpDir.ToCString(), aFileSeq.in(), true);
828
829   MESSAGE("End SMESH_Gen_i::Save");
830
831   return aStreamFile._retn();
832 }
833
834 //=============================================================================
835 /*!
836  *  
837  */
838 //=============================================================================
839
840 bool SMESH_Gen_i::Load(SALOMEDS::SComponent_ptr theComponent,
841                        const SALOMEDS::TMPFile& theStream,
842                        const char* theURL,
843                        bool isMultiFile)
844 {
845   MESSAGE("SMESH_Gen_i::Load\n");
846
847   // Get temporary files location
848   TCollection_AsciiString tmpDir = isMultiFile?TCollection_AsciiString((char*)theURL):SALOMEDS_Tool::GetTmpDir();
849
850   // Convert the stream into sequence of files to process
851   SALOMEDS::ListOfFileNames_var aFileSeq = SALOMEDS_Tool::PutStreamToFiles(theStream,
852                                                                            tmpDir.ToCString(),
853                                                                            isMultiFile);
854
855   // Set names of temporary files
856   TCollection_AsciiString filename = tmpDir + aFileSeq[0];//"SMESH.hdf";
857   TCollection_AsciiString hypofile = tmpDir + aFileSeq[1];//"SMESH_Hypo.txt";
858   TCollection_AsciiString algofile = tmpDir + aFileSeq[2];//"SMESH_Algo.txt";
859   TCollection_AsciiString meshfile = tmpDir + aFileSeq[3];//"SMESH_Mesh.med";
860
861   SALOMEDS::Study_var Study = theComponent->GetStudy(); 
862   int studyId = Study->StudyId();
863   SCRUTE(studyId);
864
865   SALOMEDS::GenericAttribute_var anAttr;
866   SALOMEDS::AttributeName_var    aName;
867   SALOMEDS::AttributeIOR_var    anIOR;
868
869   SALOMEDS::SComponent_var fathergeom = Study->FindComponent("GEOM");
870   SALOMEDS::SComponent_var myGeomSComp = SALOMEDS::SComponent::_narrow( fathergeom );
871   SCRUTE(fathergeom);
872
873   //to get the geom engine !!!
874   //useful to define our new mesh
875   SALOME_NamingService* _NS = SINGLETON_<SALOME_NamingService>::Instance() ;
876   ASSERT(SINGLETON_<SALOME_NamingService>::IsAlreadyExisting()) ;
877   _NS->init_orb( _orb ) ;
878   SALOME_LifeCycleCORBA* myEnginesLifeCycle = new SALOME_LifeCycleCORBA(_NS);
879   Engines::Component_var geomEngine =
880     myEnginesLifeCycle->FindOrLoad_Component("FactoryServer","GEOM");
881   GEOM::GEOM_Gen_var myGeomEngine = GEOM::GEOM_Gen::_narrow(geomEngine);
882
883
884   char* aLine;
885   bool ok;
886   char objectId[10],name_dataset[10];
887   int nb_datasets,size,cmpt_ds=0;
888   int cmpt_sm = 0;
889
890   char name[HDF_NAME_MAX_LEN+1];
891   char sgname[HDF_NAME_MAX_LEN+1];
892   char msgname[HDF_NAME_MAX_LEN+1];
893   char name_of_group[HDF_NAME_MAX_LEN+1];
894   char *name_meshgroup;
895   map <int,HDFgroup*> hdf_group, hdf_subgroup;
896   map <int,HDFdataset*> hdf_dataset;
897   FILE *loadedFile;
898
899   //************* HDF file opening
900   HDFfile * hdf_file = new HDFfile(filename.ToCString());
901   try {
902     hdf_file->OpenOnDisk(HDF_RDONLY);
903   }
904   catch (HDFexception) {
905     MESSAGE("Load(): " << filename << " not found!");
906     return false;
907   }
908
909   //****************************
910
911   int nb_group = hdf_file->nInternalObjects(); 
912   SCRUTE(nb_group);
913   for (int i=0;i<nb_group;i++) 
914     {
915       hdf_file->InternalObjectIndentify(i,name);
916       //SCRUTE(name);
917       
918 //***************
919 // Loading of the Hypothesis Branch
920 //***************
921       if (strcmp(name,"Hypothesis")==0) {
922         
923         double length,maxElementsArea;
924         int numberOfSegments;
925
926         hdf_group[Tag_HypothesisRoot] = new HDFgroup(name,hdf_file); 
927         hdf_group[Tag_HypothesisRoot]->OpenOnDisk();
928         
929         hdf_group[Tag_HypothesisRoot]->InternalObjectIndentify(0,name_of_group);
930         hdf_dataset[cmpt_ds]=new HDFdataset(name_of_group,hdf_group[Tag_HypothesisRoot]);
931         hdf_dataset[cmpt_ds]->OpenOnDisk();
932         size=hdf_dataset[cmpt_ds]->GetSize();
933
934         char * name_of_file =new char[size];
935         hdf_dataset[cmpt_ds]->ReadFromDisk(name_of_file);
936         SCRUTE(name_of_file);
937         hdf_dataset[cmpt_ds]->CloseOnDisk();
938         hdf_group[Tag_HypothesisRoot]->CloseOnDisk();
939         cmpt_ds++;
940         delete[] name_of_file;
941         name_of_file = 0;
942
943         aLine = new char[100];
944         loadedFile = fopen( hypofile.ToCString() ,"r");
945         while (!feof(loadedFile)) {
946           fscanf(loadedFile,"%s",aLine);
947           //SCRUTE(aLine);
948           if (strcmp(aLine,"LocalLength")==0) {
949             SMESH::SMESH_Hypothesis_var myHyp  = this->CreateHypothesis(aLine,studyId);
950             SMESH::SMESH_LocalLength_var LL = SMESH::SMESH_LocalLength::_narrow( myHyp );
951             fscanf(loadedFile,"%s",aLine);
952             length = atof(aLine);
953             LL->SetLength(length);
954             string iorString = _orb->object_to_string(LL);
955             sprintf(objectId,"%d",LL->GetId());
956             _SMESHCorbaObj[string("Hypo_")+string(objectId)] = iorString;
957           }
958           else if (strcmp(aLine,"NumberOfSegments")==0) {
959             SMESH::SMESH_Hypothesis_var myHyp  = this->CreateHypothesis(aLine,studyId);
960             SMESH::SMESH_NumberOfSegments_var NOS = SMESH::SMESH_NumberOfSegments::_narrow( myHyp );
961             fscanf(loadedFile,"%s",aLine);
962             numberOfSegments = atoi(aLine);
963             NOS->SetNumberOfSegments(numberOfSegments);
964             string iorString = _orb->object_to_string(NOS);
965             sprintf(objectId,"%d",NOS->GetId());
966             _SMESHCorbaObj[string("Hypo_")+string(objectId)] = iorString;
967           }
968           else if (strcmp(aLine,"MaxElementArea")==0) {
969             SMESH::SMESH_Hypothesis_var myHyp  = this->CreateHypothesis(aLine,studyId);
970             SMESH::SMESH_MaxElementArea_var MEA = SMESH::SMESH_MaxElementArea::_narrow( myHyp );
971             fscanf(loadedFile,"%s",aLine);
972             maxElementsArea = atof(aLine);
973             MEA->SetMaxElementArea(maxElementsArea);
974             string iorString = _orb->object_to_string(MEA);
975             sprintf(objectId,"%d",MEA->GetId());
976             _SMESHCorbaObj[string("Hypo_")+string(objectId)] = iorString;
977           }
978           
979
980         }
981         fclose(loadedFile);
982         delete[] aLine;
983         aLine = 0;
984         MESSAGE("End of Hypos Load");
985
986       }
987 //***************
988 // Loading of the Algorithms Branch
989 //***************
990       else if (strcmp(name,"Algorithms")==0) {
991
992         hdf_group[Tag_AlgorithmsRoot] = new HDFgroup(name,hdf_file); 
993         hdf_group[Tag_AlgorithmsRoot]->OpenOnDisk();
994         
995         hdf_group[Tag_AlgorithmsRoot]->InternalObjectIndentify(0,name_of_group);
996         hdf_dataset[cmpt_ds] = new HDFdataset(name_of_group,hdf_group[Tag_AlgorithmsRoot]);
997         hdf_dataset[cmpt_ds]->OpenOnDisk();
998         size=hdf_dataset[cmpt_ds]->GetSize();
999
1000         char * name_of_file =new char[size];
1001         hdf_dataset[cmpt_ds]->ReadFromDisk(name_of_file);
1002         hdf_dataset[cmpt_ds]->CloseOnDisk();
1003         hdf_group[Tag_AlgorithmsRoot]->CloseOnDisk();
1004         cmpt_ds++;
1005         delete[] name_of_file;
1006         name_of_file = 0;
1007         
1008         aLine = new char[100];
1009         loadedFile = fopen( algofile.ToCString(),"r");
1010         while (!feof(loadedFile)) {
1011           fscanf(loadedFile,"%s\n",aLine);
1012           //SCRUTE(aLine);
1013           if (strcmp(aLine,"")!=0) {
1014             SMESH::SMESH_Hypothesis_var myHyp = this->CreateHypothesis(aLine,studyId);
1015             SMESH::SMESH_Algo_var myAlgo = SMESH::SMESH_Algo::_narrow(myHyp);
1016             string iorString = _orb->object_to_string(myAlgo);
1017             sprintf(objectId,"%d",myAlgo->GetId());
1018             _SMESHCorbaObj[string("Hypo_")+string(objectId)] = iorString;
1019           }
1020         }
1021         fclose(loadedFile);
1022         delete[] aLine;
1023         aLine = 0;
1024         MESSAGE("End of Algos Load");
1025
1026       }
1027
1028 //***************
1029 // Loading of the Mesh Branch
1030 //***************
1031       else if (string(name).substr(0,4)==string("Mesh")) {
1032         MESSAGE("in mesh load");
1033
1034         Standard_Integer myMeshId = atoi((string(name).substr(5,5)).c_str());
1035         SCRUTE(myMeshId);
1036
1037         hdf_group[myMeshId] = new HDFgroup(name,hdf_file); 
1038         hdf_group[myMeshId]->OpenOnDisk();
1039
1040         int nb_meshsubgroup = hdf_group[myMeshId]->nInternalObjects(); 
1041         SCRUTE(nb_meshsubgroup);
1042               
1043         //********** Loading of the file name where the data are stored
1044         MESSAGE("Mesh data file");
1045         strcpy(name_of_group,"Mesh data");
1046         hdf_dataset[cmpt_ds]=new HDFdataset(name_of_group,hdf_group[myMeshId]);
1047         hdf_dataset[cmpt_ds]->OpenOnDisk();
1048         size=hdf_dataset[cmpt_ds]->GetSize();
1049         
1050         char * datafilename =new char[size];
1051         hdf_dataset[cmpt_ds]->ReadFromDisk(datafilename);
1052         hdf_dataset[cmpt_ds]->CloseOnDisk();
1053         cmpt_ds++;
1054               
1055         //********** 
1056         //}
1057         //else if (strcmp(msgname,"Ref on shape")==0) {
1058  
1059         //********** Loading of the reference on the shape
1060         //********** and mesh initialization
1061         MESSAGE("Ref on shape");
1062         strcpy(name_of_group,"Ref on shape");
1063         hdf_dataset[cmpt_ds] = new HDFdataset(name_of_group,hdf_group[myMeshId]);
1064         hdf_dataset[cmpt_ds]->OpenOnDisk();
1065         size=hdf_dataset[cmpt_ds]->GetSize();
1066               
1067         char * refFromFile =new char[size];
1068         hdf_dataset[cmpt_ds]->ReadFromDisk(refFromFile);
1069         hdf_dataset[cmpt_ds]->CloseOnDisk();
1070         cmpt_ds++;
1071               
1072         Standard_CString  myRefOnShape="";   //look for ref on shape
1073               
1074         bool _found = false;
1075         SALOMEDS::SObject_var CSO = Study->FindObjectID(refFromFile);
1076         SMESH::SMESH_Mesh_var myNewMesh;
1077         GEOM::GEOM_Shape_var aShape;
1078               
1079         if (!CORBA::is_nil(CSO)) {
1080           _found = true;
1081           CSO->FindAttribute(anAttr, "AttributeIOR");
1082           anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
1083           char* ShapeIOR = anIOR->Value();
1084           aShape = GEOM::GEOM_Shape::_narrow(_orb->string_to_object(ShapeIOR));
1085                 
1086           myNewMesh = this->Init(myGeomEngine,studyId,aShape);
1087           string iorString = _orb->object_to_string(myNewMesh);
1088           sprintf(objectId,"%d",myNewMesh->GetId());
1089           _SMESHCorbaObj[string("Mesh_")+string(objectId)] = iorString;
1090                                 
1091           //********** 
1092           //********** Loading of mesh data
1093           if (strcmp(datafilename,"No data")!=0) {
1094                   
1095             med_idt fid;
1096             int ret;
1097             
1098             //****************************************************************************
1099             //*                      OUVERTURE DU FICHIER EN LECTURE                      *
1100             //****************************************************************************
1101                   
1102             fid = MEDouvrir(datafilename,MED_LECT);
1103             if (fid < 0)
1104               {
1105                 printf(">> ERREUR : ouverture du fichier %s \n",datafilename);
1106                 exit(EXIT_FAILURE);
1107               }
1108             else {
1109                     
1110               StudyContext_iStruct* myStudyContext = _mapStudyContext_i[studyId];
1111               int meshId = myNewMesh->GetId();
1112               SMESH_Mesh_i* meshServant = myStudyContext->mapMesh_i[meshId];
1113               ::SMESH_Mesh& myLocMesh = meshServant->GetImpl();
1114               Handle(SMESHDS_Mesh) mySMESHDSMesh = myLocMesh.GetMeshDS();
1115                     
1116               DriverMED_R_SMESHDS_Mesh* myReader = new DriverMED_R_SMESHDS_Mesh;
1117                     
1118               myReader->SetMesh(mySMESHDSMesh);
1119               myReader->SetMeshId(myMeshId);
1120               myReader->SetFileId(fid);
1121               myReader->ReadMySelf();
1122               //SCRUTE(mySMESHDSMesh->NbNodes());
1123               //myNewMesh->ExportUNV("/tmp/test.unv");//only to check out
1124                     
1125               //****************************************************************************
1126               //*                      FERMETURE DU FICHIER                                 *
1127               //****************************************************************************
1128               ret = MEDfermer(fid);
1129                     
1130               if (ret != 0)
1131                 printf(">> ERREUR : erreur a la fermeture du fichier %s\n",datafilename);
1132                     
1133             }
1134           }
1135         }
1136         //********** 
1137         //}
1138         //else if (strcmp(msgname,"Applied Hypothesis")==0) {
1139         for (int ii=0;ii<nb_meshsubgroup;ii++) 
1140           {
1141             hdf_group[myMeshId]->InternalObjectIndentify(ii,msgname);
1142             if (strcmp(msgname,"Mesh data")==0) {
1143               //nothing
1144             }
1145             else if (strcmp(msgname,"Ref on shape")==0) {
1146               //nothing
1147             }
1148             else if (strcmp(msgname,"Applied Hypothesis")==0) {
1149               //********** Loading of the applied hypothesis
1150               strcpy(name_of_group,"Applied Hypothesis");
1151               hdf_subgroup[Tag_RefOnAppliedHypothesis] = new HDFgroup(name_of_group,hdf_group[myMeshId]);
1152               hdf_subgroup[Tag_RefOnAppliedHypothesis]->OpenOnDisk();
1153               
1154               nb_datasets = hdf_subgroup[Tag_RefOnAppliedHypothesis]->nInternalObjects();  
1155               SCRUTE(nb_datasets);
1156               
1157               for (int j=0;j<nb_datasets;j++) {
1158                 sprintf(name_dataset,"Hyp %d",j);
1159                 hdf_dataset[cmpt_ds] = new HDFdataset(name_dataset,hdf_subgroup[Tag_RefOnAppliedHypothesis]);
1160                 hdf_dataset[cmpt_ds]->OpenOnDisk();
1161                 size=hdf_dataset[cmpt_ds]->GetSize();
1162                 
1163                 char * refFromFile =new char[size];
1164                 hdf_dataset[cmpt_ds]->ReadFromDisk(refFromFile);
1165                 //SCRUTE(refFromFile);
1166                 hdf_dataset[cmpt_ds]->CloseOnDisk();
1167                 cmpt_ds++;
1168                 if (_found) {
1169                   SALOMEDS::SObject_var HypSO = Study->FindObjectID(refFromFile);
1170                   if (!CORBA::is_nil(HypSO)) {
1171                     HypSO->FindAttribute(anAttr, "AttributeIOR");
1172                     anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
1173                     if (!CORBA::is_nil(anIOR)) {
1174                       char* HypIOR = anIOR->Value();
1175                       SMESH::SMESH_Hypothesis_var anHyp = SMESH::SMESH_Hypothesis::_narrow(_orb->string_to_object(HypIOR));
1176                       if (!CORBA::is_nil(anHyp)) {
1177                         myNewMesh->AddHypothesis(aShape,anHyp);
1178                         MESSAGE("Hypothesis added ...");
1179                       }
1180                     }
1181                   }
1182                 }
1183               } 
1184               
1185               hdf_subgroup[Tag_RefOnAppliedHypothesis]->CloseOnDisk();
1186               //********** 
1187             }
1188             else if (strcmp(msgname,"Applied Algorithms")==0) {
1189
1190               //********** Loading of the applied algorithms 
1191               strcpy(name_of_group,"Applied Algorithms");
1192               hdf_subgroup[Tag_RefOnAppliedAlgorithms] = new HDFgroup(name_of_group,hdf_group[myMeshId]);
1193               hdf_subgroup[Tag_RefOnAppliedAlgorithms]->OpenOnDisk();
1194               
1195               nb_datasets = hdf_subgroup[Tag_RefOnAppliedAlgorithms]->nInternalObjects();  
1196               SCRUTE(nb_datasets);
1197               
1198               for (int j=0;j<nb_datasets;j++) {
1199                 sprintf(name_dataset,"Algo %d",j);
1200                 hdf_dataset[cmpt_ds] = new HDFdataset(name_dataset,hdf_subgroup[Tag_RefOnAppliedAlgorithms]);
1201                 hdf_dataset[cmpt_ds]->OpenOnDisk();
1202                 size=hdf_dataset[cmpt_ds]->GetSize();
1203                 
1204                 char * refFromFile =new char[size];
1205                 hdf_dataset[cmpt_ds]->ReadFromDisk(refFromFile);
1206                 hdf_dataset[cmpt_ds]->CloseOnDisk();
1207                 cmpt_ds++;
1208                 
1209                 if (_found) {
1210                   SALOMEDS::SObject_var AlgoSO = Study->FindObjectID(refFromFile);
1211                   if (!CORBA::is_nil(AlgoSO)) {
1212                     AlgoSO->FindAttribute(anAttr, "AttributeIOR");
1213                     anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
1214                     if (!CORBA::is_nil(anIOR)) {
1215                       char* AlgoIOR = anIOR->Value();
1216                       //SCRUTE(AlgoIOR);
1217                       SMESH::SMESH_Hypothesis_var myHyp = SMESH::SMESH_Hypothesis::_narrow(_orb->string_to_object(AlgoIOR));
1218                       SMESH::SMESH_Algo_var anAlgo = SMESH::SMESH_Algo::_narrow(myHyp);
1219                       //SMESH::SMESH_Algo_var anAlgo = SMESH::SMESH_Algo::_narrow(_orb->string_to_object(AlgoIOR));
1220                       if (!CORBA::is_nil(anAlgo)) {
1221                         myNewMesh->AddHypothesis(aShape,anAlgo);//essayer avec _SMESHCorbaObj
1222                         MESSAGE("Algorithms added ...");
1223                       }
1224                     }
1225                   }
1226                 }
1227               } 
1228               
1229               hdf_subgroup[Tag_RefOnAppliedAlgorithms]->CloseOnDisk();
1230  
1231               //********** 
1232             }
1233             else if (string(msgname).substr(0,9)==string("SubMeshes")) {
1234               
1235               
1236               //********** Loading of the submeshes on subshapes
1237               int myLevel1Tag, myTag;
1238               SCRUTE(msgname);
1239               cmpt_sm++;
1240               myLevel1Tag = 10+cmpt_sm;
1241               hdf_subgroup[myLevel1Tag] = new HDFgroup(name_of_group,hdf_group[myMeshId]);
1242               hdf_subgroup[myLevel1Tag] = new HDFgroup(msgname,hdf_group[myMeshId]);
1243               hdf_subgroup[myLevel1Tag]->OpenOnDisk();
1244         
1245               int nb_submeshes = hdf_subgroup[myLevel1Tag]->nInternalObjects();  
1246               SCRUTE(nb_submeshes);
1247
1248               for (int j=0;j<nb_submeshes;j++) {
1249                 cmpt_sm++;
1250                 myTag = 10+cmpt_sm;
1251                 hdf_subgroup[myLevel1Tag]->InternalObjectIndentify(j,name_meshgroup);
1252                 SCRUTE(name_meshgroup);
1253
1254                 hdf_subgroup[myTag] = new HDFgroup(name_meshgroup,hdf_subgroup[myLevel1Tag]);
1255                 hdf_subgroup[myTag]->OpenOnDisk();
1256                 int subMeshId = atoi((string(name_meshgroup).substr(8,18)).c_str());
1257                 
1258                 MESSAGE("Ref on shape");
1259                 //********** ref on shape
1260                 sprintf(name_dataset,"Ref on shape");
1261                 hdf_dataset[cmpt_ds] = new HDFdataset(name_dataset,hdf_subgroup[myTag]);
1262                 hdf_dataset[cmpt_ds]->OpenOnDisk();
1263                 size=hdf_dataset[cmpt_ds]->GetSize();
1264                 
1265                 char * refFromFile =new char[size];
1266                 hdf_dataset[cmpt_ds]->ReadFromDisk(refFromFile);
1267                 hdf_dataset[cmpt_ds]->CloseOnDisk();
1268                 cmpt_ds++;
1269                 
1270                 bool _found3 = false;
1271                 SALOMEDS::SObject_var GSO = Study->FindObjectID(refFromFile);
1272                 SMESH::SMESH_subMesh_var aSubMesh;
1273                 GEOM::GEOM_Shape_var aSubShape;
1274                 
1275                 if (!CORBA::is_nil(GSO)) {
1276                   GSO->FindAttribute(anAttr, "AttributeIOR");
1277                   anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
1278                   char* SubShapeIOR = anIOR->Value();
1279                   aSubShape = GEOM::GEOM_Shape::_narrow(_orb->string_to_object(SubShapeIOR));
1280                   
1281                   if (!CORBA::is_nil(aSubShape)) {
1282                     aSubMesh = myNewMesh->GetElementsOnShape(aSubShape);
1283                     string iorString = _orb->object_to_string(aSubMesh);
1284                     sprintf(objectId,"%d",subMeshId);
1285                     _SMESHCorbaObj[string("SubMesh_")+string(objectId)] = iorString;
1286                     _found3 = true;
1287                     //SCRUTE(aSubMesh->GetNumberOfNodes());
1288                     //MESSAGE("yes");
1289                     //SCRUTE(aSubMesh->GetNumberOfElements());
1290                   }
1291                 }
1292                     
1293                 int nb_subgroup = hdf_subgroup[myTag]->nInternalObjects(); 
1294                 SCRUTE(nb_subgroup);
1295                 for (int k=0;k<nb_subgroup;k++) 
1296                   {
1297                     hdf_subgroup[myTag]->InternalObjectIndentify(k,sgname);
1298                     if (strcmp(sgname,"Ref on shape")==0) {
1299                       //nothing
1300                     }
1301                     else if (strcmp(sgname,"Applied Hypothesis")==0) {
1302                       //********** ref on applied hypothesis
1303                       MESSAGE("Applied Hypothesis");
1304                       strcpy(name_meshgroup,"Applied Hypothesis");
1305                       cmpt_sm++;
1306                       hdf_subgroup[10+cmpt_sm] = new HDFgroup(name_meshgroup,hdf_subgroup[myTag]);
1307                       hdf_subgroup[10+cmpt_sm]->OpenOnDisk();
1308                       nb_datasets = hdf_subgroup[10+cmpt_sm]->nInternalObjects();  
1309                       SCRUTE(nb_datasets);
1310                       
1311                       for (int l=0;l<nb_datasets;l++) {
1312                         sprintf(name_dataset,"Hyp %d",l);
1313                         hdf_dataset[cmpt_ds] = new HDFdataset(name_dataset,hdf_subgroup[10+cmpt_sm]);
1314                         hdf_dataset[cmpt_ds]->OpenOnDisk();
1315                         size=hdf_dataset[cmpt_ds]->GetSize();
1316                         
1317                         char * refFromFile =new char[size];
1318                         hdf_dataset[cmpt_ds]->ReadFromDisk(refFromFile);
1319                         hdf_dataset[cmpt_ds]->CloseOnDisk();
1320                         cmpt_ds++;
1321                         
1322                         if (_found3) {
1323                           SALOMEDS::SObject_var HypSO = Study->FindObjectID(refFromFile);
1324                           if (!CORBA::is_nil(HypSO)) {
1325                             HypSO->FindAttribute(anAttr, "AttributeIOR");
1326                             anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
1327                             if (!CORBA::is_nil(anIOR)) {
1328                               char* HypIOR = anIOR->Value();
1329                               SMESH::SMESH_Hypothesis_var anHyp = SMESH::SMESH_Hypothesis::_narrow(_orb->string_to_object(HypIOR));
1330                               if (!CORBA::is_nil(anHyp)) {
1331                                 SMESH::SMESH_Mesh_var aMesh = aSubMesh->GetFather();
1332                                 aMesh->AddHypothesis(aSubShape,anHyp);//essayer avec _SMESHCorbaObj
1333                                 MESSAGE("Hypothesis added ...");
1334                               }
1335                             }
1336                           }
1337                         }
1338                       } 
1339                     }
1340                     else if (strcmp(sgname,"Applied Algorithms")==0) {
1341                       //********** ref on applied algorithms
1342                       MESSAGE("Applied Algorithms");
1343                       strcpy(name_meshgroup,"Applied Algorithms");
1344                       cmpt_sm++;
1345                       hdf_subgroup[10+cmpt_sm] = new HDFgroup(name_meshgroup,hdf_subgroup[myTag]);
1346                       hdf_subgroup[10+cmpt_sm]->OpenOnDisk();
1347                       nb_datasets = hdf_subgroup[10+cmpt_sm]->nInternalObjects();  
1348                       SCRUTE(nb_datasets);
1349                       
1350                       for (int l=0;l<nb_datasets;l++) {
1351                         sprintf(name_dataset,"Algo %d",l);
1352                         hdf_dataset[cmpt_ds] = new HDFdataset(name_dataset,hdf_subgroup[10+cmpt_sm]);
1353                         hdf_dataset[cmpt_ds]->OpenOnDisk();
1354                         size=hdf_dataset[cmpt_ds]->GetSize();
1355                         
1356                         char * refFromFile =new char[size];
1357                         hdf_dataset[cmpt_ds]->ReadFromDisk(refFromFile);
1358                         hdf_dataset[cmpt_ds]->CloseOnDisk();
1359                         cmpt_ds++;
1360                         
1361                         if (_found3) {
1362                           SALOMEDS::SObject_var AlgoSO = Study->FindObjectID(refFromFile);
1363                           if (!CORBA::is_nil(AlgoSO)) {
1364                             AlgoSO->FindAttribute(anAttr, "AttributeIOR");
1365                             anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
1366                             if (!CORBA::is_nil(anIOR)) {
1367                               char* AlgoIOR = anIOR->Value();
1368                               //SCRUTE(AlgoIOR);
1369                               SMESH::SMESH_Hypothesis_var myHyp = SMESH::SMESH_Hypothesis::_narrow(_orb->string_to_object(AlgoIOR));
1370                               SMESH::SMESH_Algo_var anAlgo = SMESH::SMESH_Algo::_narrow(myHyp);
1371                               //SMESH::SMESH_Algo_var anAlgo = SMESH::SMESH_Algo::_narrow(_orb->string_to_object(AlgoIOR));
1372                               if (!CORBA::is_nil(anAlgo)) {
1373                                 SMESH::SMESH_Mesh_var aMesh = aSubMesh->GetFather();
1374                                 aMesh->AddHypothesis(aSubShape,anAlgo);//essayer avec _SMESHCorbaObj
1375                                 MESSAGE("Algorithms added ...");
1376                               }
1377                             }
1378                           }
1379                         }
1380                         
1381                       } 
1382                     }
1383                   }
1384                 hdf_subgroup[myTag]->CloseOnDisk();
1385               }
1386               hdf_subgroup[myLevel1Tag]->CloseOnDisk();
1387             }
1388           }
1389         hdf_group[myMeshId]->CloseOnDisk();     
1390         
1391         MESSAGE("End of Meshes Load");
1392       }
1393     }
1394   
1395   MESSAGE("End of SMESH_Gen::Load");
1396   
1397   hdf_file->CloseOnDisk();
1398   delete  hdf_file;
1399   hdf_file=0;
1400
1401   // Remove temporary files created from the stream
1402   if (isMultiFile) SALOMEDS_Tool::RemoveTemporaryFiles(tmpDir.ToCString(), aFileSeq.in(), true);
1403
1404   return true;
1405 }
1406
1407 //=============================================================================
1408 /*!
1409  *  
1410  */
1411 //=============================================================================
1412
1413 void SMESH_Gen_i::Close(SALOMEDS::SComponent_ptr theComponent)
1414 {
1415   MESSAGE("Close");
1416   SALOMEDS::Study_var aStudy = theComponent->GetStudy();
1417   SALOMEDS::ChildIterator_var itBig = aStudy->NewChildIterator(theComponent);
1418   for (; itBig->More();itBig->Next()) {
1419     SALOMEDS::SObject_var gotBranch = itBig->Value();
1420     
1421     // branch 1 : hypothesis
1422     if (gotBranch->Tag()==Tag_HypothesisRoot || gotBranch->Tag()==Tag_AlgorithmsRoot) {
1423       SALOMEDS::ChildIterator_var it = aStudy->NewChildIterator(gotBranch);
1424       for (; it->More();it->Next()) {
1425         SALOMEDS::SObject_var mySObject = it->Value();
1426         SALOMEDS::GenericAttribute_var anAttr;
1427         SALOMEDS::AttributeIOR_var anIOR;
1428         if ( mySObject->FindAttribute(anAttr, "AttributeIOR")) {
1429           anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
1430           SMESH::SMESH_Hypothesis_var myHyp =
1431             SMESH::SMESH_Hypothesis::_narrow(_orb->string_to_object(anIOR->Value()));
1432           char objectId[10];
1433           sprintf(objectId,"%d",myHyp->GetId());
1434 //        cout<<"********** delete Hyp "<<objectId<<endl;
1435           _SMESHCorbaObj.erase(string("Hypo_")+string(objectId));
1436           myHyp = SMESH::SMESH_Hypothesis::_narrow(_orb->string_to_object(anIOR->Value()));
1437         }
1438       }
1439     }
1440     // branch 2 : algorithms
1441     else if (gotBranch->Tag()>=3) {
1442       SALOMEDS::ChildIterator_var it = aStudy->NewChildIterator(gotBranch);
1443       for (; it->More();it->Next()) {
1444         SALOMEDS::SObject_var mySObject = it->Value();
1445         SALOMEDS::GenericAttribute_var anAttr;
1446         SALOMEDS::AttributeIOR_var anIOR;
1447         if ( mySObject->FindAttribute(anAttr, "AttributeIOR")) {
1448           anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
1449           SMESH::SMESH_Mesh_var myMesh =
1450             SMESH::SMESH_Mesh::_narrow(_orb->string_to_object(anIOR->Value()));
1451           if (!myMesh->_is_nil()) {
1452             char objectId[10];
1453             sprintf(objectId,"%d",myMesh->GetId());
1454 //          cout<<"********** delete Mesh "<<objectId<<endl;
1455             _SMESHCorbaObj.erase(string("Mesh_")+string(objectId));
1456             CORBA::release(myMesh);
1457           }
1458           else {
1459             SMESH::SMESH_subMesh_var mySubMesh =
1460               SMESH::SMESH_subMesh::_narrow(_orb->string_to_object(anIOR->Value()));
1461             if (!mySubMesh->_is_nil()) {
1462               char objectId[10];
1463               sprintf(objectId,"%d",mySubMesh->GetId());
1464 //            cout<<"********** delete SubMesh "<<objectId<<endl;
1465               _SMESHCorbaObj.erase(string("SubMesh_")+string(objectId));
1466               CORBA::release(mySubMesh);
1467             }
1468           }
1469         }
1470       }
1471     }
1472   }
1473 }
1474
1475 //=============================================================================
1476 /*!
1477  *  
1478  */
1479 //=============================================================================
1480
1481 char* SMESH_Gen_i::ComponentDataType()
1482 {
1483   MESSAGE("SMESH_Gen_i::ComponentDataType");
1484   return strdup("SMESH");
1485 }
1486
1487     
1488 //=============================================================================
1489 /*!
1490  *  
1491  */
1492 //=============================================================================
1493
1494 char* SMESH_Gen_i::IORToLocalPersistentID(SALOMEDS::SObject_ptr theSObject,
1495                                           const char* IORString,
1496                                           CORBA::Boolean isMultiFile)
1497 {
1498   MESSAGE("SMESH_Gen_i::IORToLocalPersistentID");
1499
1500   char objectId[10];
1501
1502   SMESH::SMESH_Algo_var myAlgo = SMESH::SMESH_Algo::_narrow(_orb->string_to_object(IORString));
1503   if (! CORBA::is_nil(myAlgo))
1504     {
1505       string prefix = "Hypo_";
1506       sprintf(objectId,"%d",myAlgo->GetId());
1507       string lpID = prefix + string(objectId);
1508       return CORBA::string_dup(lpID.c_str());
1509     }
1510   else {
1511     SMESH::SMESH_Hypothesis_var myHypo = SMESH::SMESH_Hypothesis::_narrow(_orb->string_to_object(IORString));
1512     if (! CORBA::is_nil(myHypo))
1513       {
1514         string prefix = "Hypo_";
1515         sprintf(objectId,"%d",myHypo->GetId());
1516         string lpID = prefix + string(objectId);
1517         return CORBA::string_dup(lpID.c_str());
1518       }
1519     else {
1520       SMESH::SMESH_Mesh_var myMesh = SMESH::SMESH_Mesh::_narrow(_orb->string_to_object(IORString));
1521       if (! CORBA::is_nil(myMesh))
1522         {
1523           string prefix = "Mesh_";
1524           sprintf(objectId,"%d",myMesh->GetId());
1525           string lpID = prefix + string(objectId);
1526           return CORBA::string_dup(lpID.c_str());
1527         }
1528       else {
1529         SMESH::SMESH_subMesh_var mySubMesh = SMESH::SMESH_subMesh::_narrow(_orb->string_to_object(IORString));
1530         if (! CORBA::is_nil(mySubMesh))
1531           {
1532             string prefix = "SubMesh_";
1533             sprintf(objectId,"%d",mySubMesh->GetId());
1534             string lpID = prefix + string(objectId);
1535             return CORBA::string_dup(lpID.c_str());
1536           }
1537         else return (strdup("no object"));
1538       }
1539     }
1540   }
1541 }
1542
1543 //=============================================================================
1544 /*!
1545  *  
1546  */
1547 //=============================================================================
1548
1549 char* SMESH_Gen_i::LocalPersistentIDToIOR(SALOMEDS::SObject_ptr theSObject,
1550                                           const char* aLocalPersistentID,
1551                                           CORBA::Boolean isMultiFile)
1552 {
1553   MESSAGE("SMESH_Gen_i::LocalPersistentIDToIOR");
1554   SCRUTE(aLocalPersistentID);
1555   string clef = string(aLocalPersistentID);
1556   SCRUTE(_SMESHCorbaObj[clef].c_str());
1557   return CORBA::string_dup(_SMESHCorbaObj[clef].c_str());
1558 }
1559
1560 //=============================================================================
1561 /*!
1562  *  
1563  */
1564 //=============================================================================
1565
1566 SMESH_topo* SMESH_Gen_i::ExploreMainShape(GEOM::GEOM_Gen_ptr geomEngine,
1567                                           CORBA::Long studyId,
1568                                           GEOM::GEOM_Shape_ptr aShape)
1569 {
1570   MESSAGE("SMESH_Mesh_i::ExploreMainShape");
1571   // _narrow() duplicates the reference and check the type
1572   GEOM::GEOM_Gen_var geom = GEOM::GEOM_Gen::_narrow(geomEngine);
1573   GEOM::GEOM_Shape_var myShape = GEOM::GEOM_Shape::_narrow(aShape);
1574
1575   if (CORBA::is_nil(geom))
1576     THROW_SALOME_CORBA_EXCEPTION("bad geom reference", \
1577                                  SALOME::BAD_PARAM);
1578   if (CORBA::is_nil(myShape))
1579     THROW_SALOME_CORBA_EXCEPTION("bad shape reference", \
1580                                  SALOME::BAD_PARAM);
1581   MESSAGE("---");
1582   SCRUTE(myShape->Name());
1583   geom->GetCurrentStudy(studyId);
1584   SCRUTE(studyId);
1585   TopoDS_Shape mainShape  = _ShapeReader->GetShape(geom,myShape);
1586   MESSAGE("---");
1587
1588   // create an SMESH_topo object for the mainShape
1589
1590   SMESH_topo* myTopo = new SMESH_topo();
1591   MESSAGE("---");
1592
1593   // explore local TopoDS_Shape, store reference of local TopoDS subShapes
1594
1595   for (TopExp_Explorer exp(mainShape,TopAbs_COMPOUND); exp.More(); exp.Next())
1596     {
1597       const TopoDS_Compound& E = TopoDS::Compound(exp.Current());
1598       int i = myTopo->_myShapes[TopAbs_COMPOUND].Add(E);
1599       SCRUTE(i);
1600     }
1601   for (TopExp_Explorer exp(mainShape,TopAbs_COMPSOLID); exp.More(); exp.Next())
1602     {
1603       const TopoDS_CompSolid& E = TopoDS::CompSolid(exp.Current());
1604       int i = myTopo->_myShapes[TopAbs_COMPSOLID].Add(E);
1605       SCRUTE(i);
1606     }
1607   for (TopExp_Explorer exp(mainShape,TopAbs_SOLID); exp.More(); exp.Next())
1608     {
1609       const TopoDS_Solid& E = TopoDS::Solid(exp.Current());
1610       int i = myTopo->_myShapes[TopAbs_SOLID].Add(E);
1611       SCRUTE(i);
1612     }
1613   for (TopExp_Explorer exp(mainShape,TopAbs_SHELL); exp.More(); exp.Next())
1614     {
1615       const TopoDS_Shell& E = TopoDS::Shell(exp.Current());
1616       int i = myTopo->_myShapes[TopAbs_SHELL].Add(E);
1617       SCRUTE(i);
1618     }
1619   for (TopExp_Explorer exp(mainShape,TopAbs_FACE); exp.More(); exp.Next())
1620     {
1621       const TopoDS_Face& E = TopoDS::Face(exp.Current());
1622       int i = myTopo->_myShapes[TopAbs_FACE].Add(E);
1623       SCRUTE(i);
1624     }
1625   for (TopExp_Explorer exp(mainShape,TopAbs_WIRE); exp.More(); exp.Next())
1626     {
1627       const TopoDS_Wire& E = TopoDS::Wire(exp.Current());
1628       int i = myTopo->_myShapes[TopAbs_WIRE].Add(E);
1629       SCRUTE(i);
1630     }
1631   for (TopExp_Explorer exp(mainShape,TopAbs_EDGE); exp.More(); exp.Next())
1632     {
1633       const TopoDS_Edge& E = TopoDS::Edge(exp.Current());
1634       int i = myTopo->_myShapes[TopAbs_EDGE].Add(E);
1635       SCRUTE(i);
1636     }
1637   for (TopExp_Explorer exp(mainShape,TopAbs_VERTEX); exp.More(); exp.Next())
1638     {
1639       const TopoDS_Vertex& E = TopoDS::Vertex(exp.Current());
1640       int i = myTopo->_myShapes[TopAbs_VERTEX].Add(E);
1641       SCRUTE(i);
1642     }
1643   
1644   // explore subShapes of distant CORBA object,
1645   // associate distant CORBA subShape references
1646   // with local reference to local TopoDS subShape
1647
1648   string filenode = "toposhape.txt";
1649   ofstream fic(filenode.c_str());
1650
1651   for (int shapeType = TopAbs_COMPOUND; shapeType < TopAbs_SHAPE; shapeType++)
1652     {
1653       fic << "shape type : " << SMESH_shapeTypeNames[shapeType];
1654
1655       GEOM::GEOM_Gen::ListOfGeomShapes_var subShapes
1656         = geom->SubShapeAll(myShape,shapeType); 
1657       int nbSubShapes = subShapes->length();
1658       int nbLocal = myTopo->_myShapes[shapeType].Extent();
1659       fic << " - number of elements: " << nbSubShapes << endl;
1660       ASSERT(nbSubShapes == nbLocal);
1661
1662       for (int i=0; i< nbSubShapes; i++)
1663         {
1664           GEOM::GEOM_Shape_var aSubShape = subShapes[i];
1665           string idShape = SMESH_topo::GetShapeLocalId(aSubShape);
1666           fic << "  " <<  idShape;
1667           SCRUTE(idShape);
1668           TopoDS_Shape aLocShape  = _ShapeReader->GetShape(geom,aSubShape);
1669           for (int j=1; j <= nbLocal; j++)
1670             if (aLocShape.IsSame(myTopo->_myShapes[shapeType].FindKey(j)))
1671               {
1672                 MESSAGE("  --- trouve = " << j);
1673                 myTopo->_mapIndShapes[shapeType][idShape] = j;
1674                 fic << "  --- trouve = " << j;
1675                 break;
1676               }
1677           fic << endl;
1678         }
1679     }
1680   fic.close();
1681
1682   return myTopo;
1683 }
1684       
1685 //=============================================================================
1686 /*! 
1687  * C factory, accessible with dlsym, after dlopen  
1688  */
1689 //=============================================================================
1690
1691 extern "C"
1692 {
1693   PortableServer::ObjectId * SMESHEngine_factory(
1694                                CORBA::ORB_ptr orb,
1695                                PortableServer::POA_ptr poa, 
1696                                PortableServer::ObjectId * contId,
1697                                const char *instanceName, 
1698                                const char *interfaceName)
1699   {
1700     MESSAGE("PortableServer::ObjectId * SMESHEngine_factory()");
1701     SCRUTE(interfaceName);
1702     SMESH_Gen_i * mySMESH_Gen 
1703       = new SMESH_Gen_i(orb, poa, contId, instanceName, interfaceName);
1704     return mySMESH_Gen->getId() ;
1705   }
1706 }