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