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