Salome HOME
sources v1.2
[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 using namespace std;
31 #include <TopExp.hxx>
32 #include <TopExp_Explorer.hxx>
33 #include <TopoDS.hxx>
34 #include <TopoDS_Iterator.hxx>
35 #include <TopoDS_Compound.hxx>
36 #include <TopoDS_CompSolid.hxx>
37 #include <TopoDS_Solid.hxx>
38 #include <TopoDS_Shell.hxx>
39 #include <TopoDS_Face.hxx>
40 #include <TopoDS_Wire.hxx>
41 #include <TopoDS_Edge.hxx>
42 #include <TopoDS_Vertex.hxx>
43 #include <TopoDS_Shape.hxx>
44 #include <TopTools_MapOfShape.hxx>
45 #include <gp_Pnt.hxx>
46 #include <BRep_Tool.hxx>
47 #include <TCollection_AsciiString.hxx>
48
49 #include <fstream>
50 #include <stdio.h>
51
52 #include "SMESH_Gen_i.hxx"
53 #include "SMESH_Mesh_i.hxx"
54 #include "SMESH_LocalLength_i.hxx"
55 #include "SMESH_NumberOfSegments_i.hxx"
56 #include "SMESH_MaxElementArea_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;
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         }
500       }
501       fclose(destFile);
502
503 //writes the file name in the hdf file
504       longueur = hypofile.Length() +1;
505       name_group="Hypothesis";
506       //SCRUTE(name_group);
507       
508       size[0]=longueur;
509       hdf_group[1] = new HDFgroup(name_group,hdf_file);
510       hdf_group[1]->CreateOnDisk();
511       
512       hdf_dataset[cmpt_ds]=new HDFdataset(name_group,hdf_group[1],HDF_STRING,size,1);
513       hdf_dataset[cmpt_ds]->CreateOnDisk();
514       hdf_dataset[cmpt_ds]->WriteOnDisk(hypofile.ToCString());
515       hdf_dataset[cmpt_ds]->CloseOnDisk();
516       cmpt_ds++;
517       
518       hdf_group[1]->CloseOnDisk();
519       MESSAGE("End of Hypothesis Save");
520
521     }
522 //************branch 2 : algorithms
523     else if (gotBranch->Tag()==Tag_AlgorithmsRoot) {//algos = tag 2
524       
525       destFile = fopen( algofile.ToCString() ,"w");
526       it = Study->NewChildIterator(gotBranch);
527       for (; it->More();it->Next()) {
528         mySObject = it->Value();
529         SALOMEDS::GenericAttribute_var anAttr;
530         SALOMEDS::AttributeIOR_var anIOR;
531         if (mySObject->FindAttribute(anAttr, "AttributeIOR")) {
532           anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
533           SMESH::SMESH_Algo_var myAlgo = SMESH::SMESH_Algo::_narrow(_orb->string_to_object(anIOR->Value()));
534           fprintf(destFile,"%s\n",myAlgo->GetName());
535         }
536       }
537     
538       fclose(destFile);
539   
540 //writes the file name in the hdf file
541       longueur = algofile.Length() +1;
542       name_group="Algorithms";
543       //SCRUTE(name_group);
544       
545       size[0]=longueur;
546       hdf_group[2] = new HDFgroup(name_group,hdf_file);
547       hdf_group[2]->CreateOnDisk();
548       
549       hdf_dataset[cmpt_ds]=new HDFdataset(name_group,hdf_group[2],HDF_STRING,size,1);
550       hdf_dataset[cmpt_ds]->CreateOnDisk();
551       hdf_dataset[cmpt_ds]->WriteOnDisk(algofile.ToCString());
552       hdf_dataset[cmpt_ds]->CloseOnDisk();
553       cmpt_ds++;
554       
555       hdf_group[2]->CloseOnDisk();
556       MESSAGE("End of Algos Save");
557
558     }
559 //************branch 3 : meshes
560     else if (gotBranch->Tag()>=3) {//meshes = tag > 3
561
562       SALOMEDS::GenericAttribute_var anAttr;
563       SALOMEDS::AttributeIOR_var anIOR;
564       if (gotBranch->FindAttribute(anAttr, "AttributeIOR")) {
565         anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
566         
567         SMESH::SMESH_Mesh_var myMesh =  SMESH::SMESH_Mesh::_narrow(_orb->string_to_object(anIOR->Value())) ;
568         studyId = myMesh->GetStudyId();
569         SCRUTE(studyId);
570         
571         StudyContext_iStruct* myStudyContext = _mapStudyContext_i[studyId];
572         int meshId = myMesh->GetId();
573         SMESH_Mesh_i* meshServant = myStudyContext->mapMesh_i[meshId];
574         ::SMESH_Mesh& myLocMesh = meshServant->GetImpl();
575         Handle(SMESHDS_Mesh) mySMESHDSMesh = myLocMesh.GetMeshDS();
576         
577         SCRUTE(mySMESHDSMesh->NbNodes());
578         if (mySMESHDSMesh->NbNodes()>0) {//checks if the mesh is not empty
579           
580           DriverMED_W_SMESHDS_Mesh* myWriter = new DriverMED_W_SMESHDS_Mesh;
581           myWriter->SetFile(meshfile.ToCString());
582           
583           myWriter->SetMesh(mySMESHDSMesh);
584           myWriter->SetMeshId(gotBranch->Tag());
585           myWriter->Add();
586         }
587         else meshfile = "No data";
588         
589         //********** opening of the HDF group
590         sprintf(name_meshgroup,"Mesh %d",gotBranch->Tag());
591         SCRUTE(name_meshgroup);
592         hdf_group[gotBranch->Tag()] = new HDFgroup(name_meshgroup,hdf_file);
593         hdf_group[gotBranch->Tag()]->CreateOnDisk();
594         //********** 
595         
596         //********** file where the data are stored
597         longueur = strlen(meshfile.ToCString()) +1;
598         size[0]=longueur;
599         strcpy(name_dataset,"Mesh data");
600         hdf_dataset[cmpt_ds]=new HDFdataset(name_dataset,hdf_group[gotBranch->Tag()],HDF_STRING,size,1);
601         hdf_dataset[cmpt_ds]->CreateOnDisk();
602         hdf_dataset[cmpt_ds]->WriteOnDisk(meshfile.ToCString());
603         hdf_dataset[cmpt_ds]->CloseOnDisk();
604         cmpt_ds++;
605         //********** 
606         
607         //********** ref on shape
608         Standard_CString  myRefOnObject="" ;
609         SALOMEDS::SObject_var myRef,myShape;
610         _found = gotBranch->FindSubObject(Tag_RefOnShape,myRef);
611         if (_found) {
612           ok = myRef->ReferencedObject(myShape);
613           myRefOnObject = myShape->GetID();
614           SCRUTE(myRefOnObject);
615           
616           longueur = strlen(myRefOnObject) +1;
617           if (longueur>1) {
618             size[0]=longueur;
619             strcpy(name_dataset,"Ref on shape");
620             hdf_dataset[cmpt_ds]=new HDFdataset(name_dataset,hdf_group[gotBranch->Tag()],HDF_STRING,size,1);
621             hdf_dataset[cmpt_ds]->CreateOnDisk();
622             hdf_dataset[cmpt_ds]->WriteOnDisk(myRefOnObject);
623             hdf_dataset[cmpt_ds]->CloseOnDisk();
624             cmpt_ds++;
625             
626           }
627         }
628         //********** 
629         
630         //********** ref on applied hypothesis
631         _found = gotBranch->FindSubObject(Tag_RefOnAppliedHypothesis,myBranch);
632         if (_found) {
633           
634           strcpy(name_meshgroup,"Applied Hypothesis");
635           hdf_subgroup[Tag_RefOnAppliedHypothesis] = new HDFgroup(name_meshgroup,hdf_group[gotBranch->Tag()]);
636           hdf_subgroup[Tag_RefOnAppliedHypothesis]->CreateOnDisk();
637           
638           it = Study->NewChildIterator(myBranch);
639           cmpt_it = 0;
640           for (; it->More();it->Next()) {
641             mySObject = it->Value();
642             ok = mySObject->ReferencedObject(myRef);
643             myRefOnObject = myRef->GetID();
644             
645             longueur = strlen(myRefOnObject) +1;
646             if (longueur>1) {
647               size[0]=longueur;
648               sprintf(name_dataset,"Hyp %d",cmpt_it);
649               hdf_dataset[cmpt_ds]=new HDFdataset(name_dataset,hdf_subgroup[Tag_RefOnAppliedHypothesis],HDF_STRING,size,1);
650               hdf_dataset[cmpt_ds]->CreateOnDisk();
651               hdf_dataset[cmpt_ds]->WriteOnDisk(myRefOnObject);
652               hdf_dataset[cmpt_ds]->CloseOnDisk();
653             }
654             cmpt_ds++;
655             cmpt_it++;
656           }
657           hdf_subgroup[Tag_RefOnAppliedHypothesis]->CloseOnDisk();
658         }
659         //********** 
660         
661         //********** ref on applied algorithms
662         _found = gotBranch->FindSubObject(Tag_RefOnAppliedAlgorithms,myBranch);
663         if (_found) {
664           
665           strcpy(name_meshgroup,"Applied Algorithms");
666           hdf_subgroup[Tag_RefOnAppliedAlgorithms] = new HDFgroup(name_meshgroup,hdf_group[gotBranch->Tag()]);
667           hdf_subgroup[Tag_RefOnAppliedAlgorithms]->CreateOnDisk();
668           
669           it = Study->NewChildIterator(myBranch);
670           cmpt_it = 0;
671           for (; it->More();it->Next()) {
672             mySObject = it->Value();
673             ok = mySObject->ReferencedObject(myRef);
674             myRefOnObject = myRef->GetID();
675             
676             longueur = strlen(myRefOnObject) +1;
677             if (longueur>1) {
678               size[0]=longueur;
679               sprintf(name_dataset,"Algo %d",cmpt_it);
680               hdf_dataset[cmpt_ds]=new HDFdataset(name_dataset,hdf_subgroup[Tag_RefOnAppliedAlgorithms],HDF_STRING,size,1);
681               hdf_dataset[cmpt_ds]->CreateOnDisk();
682               hdf_dataset[cmpt_ds]->WriteOnDisk(myRefOnObject);
683               hdf_dataset[cmpt_ds]->CloseOnDisk();
684             }
685             cmpt_ds++;
686             cmpt_it++;
687           }
688           hdf_subgroup[Tag_RefOnAppliedAlgorithms]->CloseOnDisk();
689         }
690         MESSAGE("end of algo applied");
691         //********** 
692         
693         //********** submeshes on subshapes
694         int myLevel1Tag;
695         for (int i=Tag_SubMeshOnVertex;i<=Tag_SubMeshOnCompound;i++) {
696           _found = gotBranch->FindSubObject(i,myBranch);
697           if (_found) {
698             if (i==Tag_SubMeshOnVertex)
699               strcpy(name_meshgroup,"SubMeshes On Vertex");
700             else if (i==Tag_SubMeshOnEdge)
701               strcpy(name_meshgroup,"SubMeshes On Edge");
702             else if (i==Tag_SubMeshOnFace)
703               strcpy(name_meshgroup,"SubMeshes On Face");
704             else if (i==Tag_SubMeshOnSolid)
705               strcpy(name_meshgroup,"SubMeshes On Solid");
706             else if (i==Tag_SubMeshOnCompound)
707               strcpy(name_meshgroup,"SubMeshes On Compound");
708
709             cmpt_sm++;
710             myLevel1Tag = 10+cmpt_sm;
711             hdf_subgroup[myLevel1Tag] = new HDFgroup(name_meshgroup,hdf_group[gotBranch->Tag()]);
712             hdf_subgroup[myLevel1Tag]->CreateOnDisk();
713             
714             itSM = Study->NewChildIterator(myBranch);
715             for (; itSM->More();itSM->Next()) {//Loop on all submeshes
716               mySObject = itSM->Value();
717               cmpt_sm++;
718               myTag = 10+cmpt_sm;
719               mySObject->FindAttribute(anAttr, "AttributeIOR");
720               anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
721               SMESH::SMESH_subMesh_var mySubMesh =  SMESH::SMESH_subMesh::_narrow(_orb->string_to_object(anIOR->Value())) ;
722
723               //sprintf(name_meshgroup,"SubMesh %d",myTag);
724               sprintf(name_meshgroup,"SubMesh %d",mySubMesh->GetId());
725               SCRUTE(name_meshgroup);
726               
727               hdf_subgroup[myTag] = new HDFgroup(name_meshgroup,hdf_subgroup[myLevel1Tag]);
728               hdf_subgroup[myTag]->CreateOnDisk();
729               
730               //********** ref on shape
731               Standard_CString  myRefOnObject="" ;
732               SALOMEDS::SObject_var myRef,myShape;
733               bool _found2;
734               _found2 = mySObject->FindSubObject(Tag_RefOnShape,myRef);
735               if (_found2) {
736                 ok = myRef->ReferencedObject(myShape);
737                 myRefOnObject = myShape->GetID();
738                 SCRUTE(myRefOnObject);
739                 
740                 longueur = strlen(myRefOnObject) +1;
741                 if (longueur>1) {
742                   size[0]=longueur;
743                   strcpy(name_dataset,"Ref on shape");
744                   hdf_dataset[cmpt_ds]=new HDFdataset(name_dataset,hdf_subgroup[myTag],HDF_STRING,size,1);
745                   hdf_dataset[cmpt_ds]->CreateOnDisk();
746                   hdf_dataset[cmpt_ds]->WriteOnDisk(myRefOnObject);
747                   hdf_dataset[cmpt_ds]->CloseOnDisk();
748                   cmpt_ds++;
749                 }
750               }
751               //********** 
752               
753               //********** ref on applied hypothesis
754               _found2 = mySObject->FindSubObject(Tag_RefOnAppliedHypothesis,myBranch);
755               if (_found2) {
756                 
757                 strcpy(name_meshgroup,"Applied Hypothesis");
758                 cmpt_sm++;
759                 hdf_subgroup[10+cmpt_sm] = new HDFgroup(name_meshgroup,hdf_subgroup[myTag]);
760                 hdf_subgroup[10+cmpt_sm]->CreateOnDisk();
761                 
762                 it = Study->NewChildIterator(myBranch);
763                 cmpt_it = 0;
764                 for (; it->More();it->Next()) {
765                   mySObjectChild = it->Value();
766                   ok = mySObjectChild->ReferencedObject(myRef);
767                   myRefOnObject = myRef->GetID();
768                   
769                   longueur = strlen(myRefOnObject) +1;
770                   if (longueur>1) {
771                     size[0]=longueur;
772                     sprintf(name_dataset,"Hyp %d",cmpt_it);
773                     SCRUTE(cmpt_it);
774                     hdf_dataset[cmpt_ds]=new HDFdataset(name_dataset,hdf_subgroup[10+cmpt_sm],HDF_STRING,size,1);
775                     hdf_dataset[cmpt_ds]->CreateOnDisk();
776                     hdf_dataset[cmpt_ds]->WriteOnDisk(myRefOnObject);
777                     hdf_dataset[cmpt_ds]->CloseOnDisk();
778                   }
779                   cmpt_ds++;
780                   cmpt_it++;
781                 }
782                 hdf_subgroup[10+cmpt_sm]->CloseOnDisk();
783               }
784               //********** 
785               
786               //********** ref on applied algorithms
787               _found2 = mySObject->FindSubObject(Tag_RefOnAppliedAlgorithms,myBranch);
788               SCRUTE(_found2);
789               if (_found2) {
790                 
791                 strcpy(name_meshgroup,"Applied Algorithms");
792                 cmpt_sm++;
793                 hdf_subgroup[10+cmpt_sm] = new HDFgroup(name_meshgroup,hdf_subgroup[myTag]);
794                 hdf_subgroup[10+cmpt_sm]->CreateOnDisk();
795                 
796                 it = Study->NewChildIterator(myBranch);
797                 cmpt_it = 0;
798                 for (; it->More();it->Next()) {
799                   mySObjectChild = it->Value();
800                   ok = mySObjectChild->ReferencedObject(myRef);
801                   myRefOnObject = myRef->GetID();
802                   
803                   longueur = strlen(myRefOnObject) +1;
804                   if (longueur>1) {
805                     size[0]=longueur;
806                     sprintf(name_dataset,"Algo %d",cmpt_it);
807                     hdf_dataset[cmpt_ds]=new HDFdataset(name_dataset,hdf_subgroup[10+cmpt_sm],HDF_STRING,size,1);
808                     hdf_dataset[cmpt_ds]->CreateOnDisk();
809                     hdf_dataset[cmpt_ds]->WriteOnDisk(myRefOnObject);
810                     hdf_dataset[cmpt_ds]->CloseOnDisk();
811                   }
812                   cmpt_ds++;
813                   cmpt_it++;
814                 }
815                 hdf_subgroup[10+cmpt_sm]->CloseOnDisk();
816               }
817               //MESSAGE("end of algo applied");
818               //********** 
819               
820               hdf_subgroup[myTag]->CloseOnDisk();
821             }
822             
823             hdf_subgroup[myLevel1Tag]->CloseOnDisk();
824           }
825           
826         }
827         //********** 
828         
829         //********** closing of the HDF group
830         hdf_group[gotBranch->Tag()]->CloseOnDisk();
831         MESSAGE("End of Mesh Save");
832         //********** 
833       }                  
834     }
835     MESSAGE("End of Meshes Save");
836   }
837
838   hdf_file->CloseOnDisk();
839   delete hdf_file;
840   hdf_file = 0;
841
842   // Convert temporary files to stream
843   aStreamFile = SALOMEDS_Tool::PutFilesToStream(tmpDir.ToCString(), aFileSeq.in(), isMultiFile);
844
845   // Remove temporary files and directory
846   if (isMultiFile) SALOMEDS_Tool::RemoveTemporaryFiles(tmpDir.ToCString(), aFileSeq.in(), true);
847
848   MESSAGE("End SMESH_Gen_i::Save");
849
850   return aStreamFile._retn();
851 }
852
853 SALOMEDS::TMPFile* SMESH_Gen_i::SaveASCII(SALOMEDS::SComponent_ptr theComponent,
854                                           const char* theURL,
855                                           bool isMultiFile) {
856   SALOMEDS::TMPFile_var aStreamFile = Save(theComponent, theURL, isMultiFile);
857   return aStreamFile._retn();
858 }
859
860 //=============================================================================
861 /*!
862  *  
863  */
864 //=============================================================================
865
866 bool SMESH_Gen_i::Load(SALOMEDS::SComponent_ptr theComponent,
867                        const SALOMEDS::TMPFile& theStream,
868                        const char* theURL,
869                        bool isMultiFile)
870 {
871   MESSAGE("SMESH_Gen_i::Load\n");
872
873   // Get temporary files location
874   TCollection_AsciiString tmpDir = isMultiFile?TCollection_AsciiString((char*)theURL):SALOMEDS_Tool::GetTmpDir();
875
876   // Convert the stream into sequence of files to process
877   SALOMEDS::ListOfFileNames_var aFileSeq = SALOMEDS_Tool::PutStreamToFiles(theStream,
878                                                                            tmpDir.ToCString(),
879                                                                            isMultiFile);
880
881   // Set names of temporary files
882   TCollection_AsciiString filename = tmpDir + aFileSeq[0];//"SMESH.hdf";
883   TCollection_AsciiString hypofile = tmpDir + aFileSeq[1];//"SMESH_Hypo.txt";
884   TCollection_AsciiString algofile = tmpDir + aFileSeq[2];//"SMESH_Algo.txt";
885   TCollection_AsciiString meshfile = tmpDir + aFileSeq[3];//"SMESH_Mesh.med";
886
887   SALOMEDS::Study_var Study = theComponent->GetStudy(); 
888   int studyId = Study->StudyId();
889   SCRUTE(studyId);
890
891   SALOMEDS::GenericAttribute_var anAttr;
892   SALOMEDS::AttributeName_var    aName;
893   SALOMEDS::AttributeIOR_var    anIOR;
894
895   SALOMEDS::SComponent_var fathergeom = Study->FindComponent("GEOM");
896   SALOMEDS::SComponent_var myGeomSComp = SALOMEDS::SComponent::_narrow( fathergeom );
897   SCRUTE(fathergeom);
898
899   //to get the geom engine !!!
900   //useful to define our new mesh
901   SALOME_NamingService* _NS = SINGLETON_<SALOME_NamingService>::Instance() ;
902   ASSERT(SINGLETON_<SALOME_NamingService>::IsAlreadyExisting()) ;
903   _NS->init_orb( _orb ) ;
904   SALOME_LifeCycleCORBA* myEnginesLifeCycle = new SALOME_LifeCycleCORBA(_NS);
905   Engines::Component_var geomEngine =
906     myEnginesLifeCycle->FindOrLoad_Component("FactoryServer","Geometry");
907   GEOM::GEOM_Gen_var myGeomEngine = GEOM::GEOM_Gen::_narrow(geomEngine);
908
909
910   char* aLine;
911   bool ok;
912   char objectId[10],name_dataset[10];
913   int nb_datasets,size,cmpt_ds=0;
914   int cmpt_sm = 0;
915
916   char name[HDF_NAME_MAX_LEN+1];
917   char sgname[HDF_NAME_MAX_LEN+1];
918   char msgname[HDF_NAME_MAX_LEN+1];
919   char name_of_group[HDF_NAME_MAX_LEN+1];
920   char *name_meshgroup;
921   map <int,HDFgroup*> hdf_group, hdf_subgroup;
922   map <int,HDFdataset*> hdf_dataset;
923   FILE *loadedFile;
924
925   //************* HDF file opening
926   HDFfile * hdf_file = new HDFfile(filename.ToCString());
927   try {
928     hdf_file->OpenOnDisk(HDF_RDONLY);
929   }
930   catch (HDFexception) {
931     MESSAGE("Load(): " << filename << " not found!");
932     return false;
933   }
934
935   //****************************
936
937   int nb_group = hdf_file->nInternalObjects(); 
938   SCRUTE(nb_group);
939   for (int i=0;i<nb_group;i++) 
940     {
941       hdf_file->InternalObjectIndentify(i,name);
942       //SCRUTE(name);
943       
944 //***************
945 // Loading of the Hypothesis Branch
946 //***************
947       if (strcmp(name,"Hypothesis")==0) {
948         
949         double length,maxElementsArea;
950         int numberOfSegments;
951
952         hdf_group[Tag_HypothesisRoot] = new HDFgroup(name,hdf_file); 
953         hdf_group[Tag_HypothesisRoot]->OpenOnDisk();
954         
955         hdf_group[Tag_HypothesisRoot]->InternalObjectIndentify(0,name_of_group);
956         hdf_dataset[cmpt_ds]=new HDFdataset(name_of_group,hdf_group[Tag_HypothesisRoot]);
957         hdf_dataset[cmpt_ds]->OpenOnDisk();
958         size=hdf_dataset[cmpt_ds]->GetSize();
959
960         char * name_of_file =new char[size];
961         hdf_dataset[cmpt_ds]->ReadFromDisk(name_of_file);
962         SCRUTE(name_of_file);
963         hdf_dataset[cmpt_ds]->CloseOnDisk();
964         hdf_group[Tag_HypothesisRoot]->CloseOnDisk();
965         cmpt_ds++;
966         delete[] name_of_file;
967         name_of_file = 0;
968
969         aLine = new char[100];
970         loadedFile = fopen( hypofile.ToCString() ,"r");
971         while (!feof(loadedFile)) {
972           fscanf(loadedFile,"%s",aLine);
973           //SCRUTE(aLine);
974           if (strcmp(aLine,"LocalLength")==0) {
975             SMESH::SMESH_Hypothesis_var myHyp  = this->CreateHypothesis(aLine,studyId);
976             SMESH::SMESH_LocalLength_var LL = SMESH::SMESH_LocalLength::_narrow( myHyp );
977             fscanf(loadedFile,"%s",aLine);
978             length = atof(aLine);
979             LL->SetLength(length);
980             string iorString = _orb->object_to_string(LL);
981             sprintf(objectId,"%d",LL->GetId());
982             _SMESHCorbaObj[string("Hypo_")+string(objectId)] = iorString;
983           }
984           else if (strcmp(aLine,"NumberOfSegments")==0) {
985             SMESH::SMESH_Hypothesis_var myHyp  = this->CreateHypothesis(aLine,studyId);
986             SMESH::SMESH_NumberOfSegments_var NOS = SMESH::SMESH_NumberOfSegments::_narrow( myHyp );
987             fscanf(loadedFile,"%s",aLine);
988             numberOfSegments = atoi(aLine);
989             NOS->SetNumberOfSegments(numberOfSegments);
990             string iorString = _orb->object_to_string(NOS);
991             sprintf(objectId,"%d",NOS->GetId());
992             _SMESHCorbaObj[string("Hypo_")+string(objectId)] = iorString;
993           }
994           else if (strcmp(aLine,"MaxElementArea")==0) {
995             SMESH::SMESH_Hypothesis_var myHyp  = this->CreateHypothesis(aLine,studyId);
996             SMESH::SMESH_MaxElementArea_var MEA = SMESH::SMESH_MaxElementArea::_narrow( myHyp );
997             fscanf(loadedFile,"%s",aLine);
998             maxElementsArea = atof(aLine);
999             MEA->SetMaxElementArea(maxElementsArea);
1000             string iorString = _orb->object_to_string(MEA);
1001             sprintf(objectId,"%d",MEA->GetId());
1002             _SMESHCorbaObj[string("Hypo_")+string(objectId)] = iorString;
1003           }
1004           
1005
1006         }
1007         fclose(loadedFile);
1008         delete[] aLine;
1009         aLine = 0;
1010         MESSAGE("End of Hypos Load");
1011
1012       }
1013 //***************
1014 // Loading of the Algorithms Branch
1015 //***************
1016       else if (strcmp(name,"Algorithms")==0) {
1017
1018         hdf_group[Tag_AlgorithmsRoot] = new HDFgroup(name,hdf_file); 
1019         hdf_group[Tag_AlgorithmsRoot]->OpenOnDisk();
1020         
1021         hdf_group[Tag_AlgorithmsRoot]->InternalObjectIndentify(0,name_of_group);
1022         hdf_dataset[cmpt_ds] = new HDFdataset(name_of_group,hdf_group[Tag_AlgorithmsRoot]);
1023         hdf_dataset[cmpt_ds]->OpenOnDisk();
1024         size=hdf_dataset[cmpt_ds]->GetSize();
1025
1026         char * name_of_file =new char[size];
1027         hdf_dataset[cmpt_ds]->ReadFromDisk(name_of_file);
1028         hdf_dataset[cmpt_ds]->CloseOnDisk();
1029         hdf_group[Tag_AlgorithmsRoot]->CloseOnDisk();
1030         cmpt_ds++;
1031         delete[] name_of_file;
1032         name_of_file = 0;
1033         
1034         aLine = new char[100];
1035         loadedFile = fopen( algofile.ToCString(),"r");
1036         while (!feof(loadedFile)) {
1037           fscanf(loadedFile,"%s\n",aLine);
1038           //SCRUTE(aLine);
1039           if (strcmp(aLine,"")!=0) {
1040             SMESH::SMESH_Hypothesis_var myHyp = this->CreateHypothesis(aLine,studyId);
1041             SMESH::SMESH_Algo_var myAlgo = SMESH::SMESH_Algo::_narrow(myHyp);
1042             string iorString = _orb->object_to_string(myAlgo);
1043             sprintf(objectId,"%d",myAlgo->GetId());
1044             _SMESHCorbaObj[string("Hypo_")+string(objectId)] = iorString;
1045           }
1046         }
1047         fclose(loadedFile);
1048         delete[] aLine;
1049         aLine = 0;
1050         MESSAGE("End of Algos Load");
1051
1052       }
1053
1054 //***************
1055 // Loading of the Mesh Branch
1056 //***************
1057       else if (string(name).substr(0,4)==string("Mesh")) {
1058         MESSAGE("in mesh load");
1059
1060         Standard_Integer myMeshId = atoi((string(name).substr(5,5)).c_str());
1061         SCRUTE(myMeshId);
1062
1063         hdf_group[myMeshId] = new HDFgroup(name,hdf_file); 
1064         hdf_group[myMeshId]->OpenOnDisk();
1065
1066         int nb_meshsubgroup = hdf_group[myMeshId]->nInternalObjects(); 
1067         SCRUTE(nb_meshsubgroup);
1068               
1069         //********** Loading of the file name where the data are stored
1070         MESSAGE("Mesh data file");
1071         strcpy(name_of_group,"Mesh data");
1072         hdf_dataset[cmpt_ds]=new HDFdataset(name_of_group,hdf_group[myMeshId]);
1073         hdf_dataset[cmpt_ds]->OpenOnDisk();
1074         size=hdf_dataset[cmpt_ds]->GetSize();
1075         
1076         char * datafilename =new char[size];
1077         hdf_dataset[cmpt_ds]->ReadFromDisk(datafilename);
1078         hdf_dataset[cmpt_ds]->CloseOnDisk();
1079         cmpt_ds++;
1080               
1081         //********** 
1082         //}
1083         //else if (strcmp(msgname,"Ref on shape")==0) {
1084  
1085         //********** Loading of the reference on the shape
1086         //********** and mesh initialization
1087         MESSAGE("Ref on shape");
1088         strcpy(name_of_group,"Ref on shape");
1089         hdf_dataset[cmpt_ds] = new HDFdataset(name_of_group,hdf_group[myMeshId]);
1090         hdf_dataset[cmpt_ds]->OpenOnDisk();
1091         size=hdf_dataset[cmpt_ds]->GetSize();
1092               
1093         char * refFromFile =new char[size];
1094         hdf_dataset[cmpt_ds]->ReadFromDisk(refFromFile);
1095         hdf_dataset[cmpt_ds]->CloseOnDisk();
1096         cmpt_ds++;
1097               
1098         Standard_CString  myRefOnShape="";   //look for ref on shape
1099               
1100         bool _found = false;
1101         SALOMEDS::SObject_var CSO = Study->FindObjectID(refFromFile);
1102         SMESH::SMESH_Mesh_var myNewMesh;
1103         GEOM::GEOM_Shape_var aShape;
1104               
1105         if (!CORBA::is_nil(CSO)) {
1106           _found = true;
1107           CSO->FindAttribute(anAttr, "AttributeIOR");
1108           anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
1109           char* ShapeIOR = anIOR->Value();
1110           aShape = GEOM::GEOM_Shape::_narrow(_orb->string_to_object(ShapeIOR));
1111                 
1112           myNewMesh = this->Init(myGeomEngine,studyId,aShape);
1113           string iorString = _orb->object_to_string(myNewMesh);
1114           sprintf(objectId,"%d",myNewMesh->GetId());
1115           _SMESHCorbaObj[string("Mesh_")+string(objectId)] = iorString;
1116                                 
1117           //********** 
1118           //********** Loading of mesh data
1119           if (strcmp(datafilename,"No data")!=0) {
1120                   
1121             med_idt fid;
1122             int ret;
1123             
1124             //****************************************************************************
1125             //*                      OUVERTURE DU FICHIER EN LECTURE                      *
1126             //****************************************************************************
1127                   
1128             fid = MEDouvrir(datafilename,MED_LECT);
1129             if (fid < 0)
1130               {
1131                 printf(">> ERREUR : ouverture du fichier %s \n",datafilename);
1132                 exit(EXIT_FAILURE);
1133               }
1134             else {
1135                     
1136               StudyContext_iStruct* myStudyContext = _mapStudyContext_i[studyId];
1137               int meshId = myNewMesh->GetId();
1138               SMESH_Mesh_i* meshServant = myStudyContext->mapMesh_i[meshId];
1139               ::SMESH_Mesh& myLocMesh = meshServant->GetImpl();
1140               Handle(SMESHDS_Mesh) mySMESHDSMesh = myLocMesh.GetMeshDS();
1141                     
1142               DriverMED_R_SMESHDS_Mesh* myReader = new DriverMED_R_SMESHDS_Mesh;
1143                     
1144               myReader->SetMesh(mySMESHDSMesh);
1145               myReader->SetMeshId(myMeshId);
1146               myReader->SetFileId(fid);
1147               myReader->ReadMySelf();
1148               //SCRUTE(mySMESHDSMesh->NbNodes());
1149               //myNewMesh->ExportUNV("/tmp/test.unv");//only to check out
1150                     
1151               //****************************************************************************
1152               //*                      FERMETURE DU FICHIER                                 *
1153               //****************************************************************************
1154               ret = MEDfermer(fid);
1155                     
1156               if (ret != 0)
1157                 printf(">> ERREUR : erreur a la fermeture du fichier %s\n",datafilename);
1158                     
1159             }
1160           }
1161         }
1162         //********** 
1163         //}
1164         //else if (strcmp(msgname,"Applied Hypothesis")==0) {
1165         for (int ii=0;ii<nb_meshsubgroup;ii++) 
1166           {
1167             hdf_group[myMeshId]->InternalObjectIndentify(ii,msgname);
1168             if (strcmp(msgname,"Mesh data")==0) {
1169               //nothing
1170             }
1171             else if (strcmp(msgname,"Ref on shape")==0) {
1172               //nothing
1173             }
1174             else if (strcmp(msgname,"Applied Hypothesis")==0) {
1175               //********** Loading of the applied hypothesis
1176               strcpy(name_of_group,"Applied Hypothesis");
1177               hdf_subgroup[Tag_RefOnAppliedHypothesis] = new HDFgroup(name_of_group,hdf_group[myMeshId]);
1178               hdf_subgroup[Tag_RefOnAppliedHypothesis]->OpenOnDisk();
1179               
1180               nb_datasets = hdf_subgroup[Tag_RefOnAppliedHypothesis]->nInternalObjects();  
1181               SCRUTE(nb_datasets);
1182               
1183               for (int j=0;j<nb_datasets;j++) {
1184                 sprintf(name_dataset,"Hyp %d",j);
1185                 hdf_dataset[cmpt_ds] = new HDFdataset(name_dataset,hdf_subgroup[Tag_RefOnAppliedHypothesis]);
1186                 hdf_dataset[cmpt_ds]->OpenOnDisk();
1187                 size=hdf_dataset[cmpt_ds]->GetSize();
1188                 
1189                 char * refFromFile =new char[size];
1190                 hdf_dataset[cmpt_ds]->ReadFromDisk(refFromFile);
1191                 //SCRUTE(refFromFile);
1192                 hdf_dataset[cmpt_ds]->CloseOnDisk();
1193                 cmpt_ds++;
1194                 if (_found) {
1195                   SALOMEDS::SObject_var HypSO = Study->FindObjectID(refFromFile);
1196                   if (!CORBA::is_nil(HypSO)) {
1197                     HypSO->FindAttribute(anAttr, "AttributeIOR");
1198                     anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
1199                     if (!CORBA::is_nil(anIOR)) {
1200                       char* HypIOR = anIOR->Value();
1201                       SMESH::SMESH_Hypothesis_var anHyp = SMESH::SMESH_Hypothesis::_narrow(_orb->string_to_object(HypIOR));
1202                       if (!CORBA::is_nil(anHyp)) {
1203                         myNewMesh->AddHypothesis(aShape,anHyp);
1204                         MESSAGE("Hypothesis added ...");
1205                       }
1206                     }
1207                   }
1208                 }
1209               } 
1210               
1211               hdf_subgroup[Tag_RefOnAppliedHypothesis]->CloseOnDisk();
1212               //********** 
1213             }
1214             else if (strcmp(msgname,"Applied Algorithms")==0) {
1215
1216               //********** Loading of the applied algorithms 
1217               strcpy(name_of_group,"Applied Algorithms");
1218               hdf_subgroup[Tag_RefOnAppliedAlgorithms] = new HDFgroup(name_of_group,hdf_group[myMeshId]);
1219               hdf_subgroup[Tag_RefOnAppliedAlgorithms]->OpenOnDisk();
1220               
1221               nb_datasets = hdf_subgroup[Tag_RefOnAppliedAlgorithms]->nInternalObjects();  
1222               SCRUTE(nb_datasets);
1223               
1224               for (int j=0;j<nb_datasets;j++) {
1225                 sprintf(name_dataset,"Algo %d",j);
1226                 hdf_dataset[cmpt_ds] = new HDFdataset(name_dataset,hdf_subgroup[Tag_RefOnAppliedAlgorithms]);
1227                 hdf_dataset[cmpt_ds]->OpenOnDisk();
1228                 size=hdf_dataset[cmpt_ds]->GetSize();
1229                 
1230                 char * refFromFile =new char[size];
1231                 hdf_dataset[cmpt_ds]->ReadFromDisk(refFromFile);
1232                 hdf_dataset[cmpt_ds]->CloseOnDisk();
1233                 cmpt_ds++;
1234                 
1235                 if (_found) {
1236                   SALOMEDS::SObject_var AlgoSO = Study->FindObjectID(refFromFile);
1237                   if (!CORBA::is_nil(AlgoSO)) {
1238                     AlgoSO->FindAttribute(anAttr, "AttributeIOR");
1239                     anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
1240                     if (!CORBA::is_nil(anIOR)) {
1241                       char* AlgoIOR = anIOR->Value();
1242                       //SCRUTE(AlgoIOR);
1243                       SMESH::SMESH_Hypothesis_var myHyp = SMESH::SMESH_Hypothesis::_narrow(_orb->string_to_object(AlgoIOR));
1244                       SMESH::SMESH_Algo_var anAlgo = SMESH::SMESH_Algo::_narrow(myHyp);
1245                       //SMESH::SMESH_Algo_var anAlgo = SMESH::SMESH_Algo::_narrow(_orb->string_to_object(AlgoIOR));
1246                       if (!CORBA::is_nil(anAlgo)) {
1247                         myNewMesh->AddHypothesis(aShape,anAlgo);//essayer avec _SMESHCorbaObj
1248                         MESSAGE("Algorithms added ...");
1249                       }
1250                     }
1251                   }
1252                 }
1253               } 
1254               
1255               hdf_subgroup[Tag_RefOnAppliedAlgorithms]->CloseOnDisk();
1256  
1257               //********** 
1258             }
1259             else if (string(msgname).substr(0,9)==string("SubMeshes")) {
1260               
1261               
1262               //********** Loading of the submeshes on subshapes
1263               int myLevel1Tag, myTag;
1264               SCRUTE(msgname);
1265               cmpt_sm++;
1266               myLevel1Tag = 10+cmpt_sm;
1267               hdf_subgroup[myLevel1Tag] = new HDFgroup(name_of_group,hdf_group[myMeshId]);
1268               hdf_subgroup[myLevel1Tag] = new HDFgroup(msgname,hdf_group[myMeshId]);
1269               hdf_subgroup[myLevel1Tag]->OpenOnDisk();
1270         
1271               int nb_submeshes = hdf_subgroup[myLevel1Tag]->nInternalObjects();  
1272               SCRUTE(nb_submeshes);
1273
1274               for (int j=0;j<nb_submeshes;j++) {
1275                 cmpt_sm++;
1276                 myTag = 10+cmpt_sm;
1277                 hdf_subgroup[myLevel1Tag]->InternalObjectIndentify(j,name_meshgroup);
1278                 SCRUTE(name_meshgroup);
1279
1280                 hdf_subgroup[myTag] = new HDFgroup(name_meshgroup,hdf_subgroup[myLevel1Tag]);
1281                 hdf_subgroup[myTag]->OpenOnDisk();
1282                 int subMeshId = atoi((string(name_meshgroup).substr(8,18)).c_str());
1283                 
1284                 MESSAGE("Ref on shape");
1285                 //********** ref on shape
1286                 sprintf(name_dataset,"Ref on shape");
1287                 hdf_dataset[cmpt_ds] = new HDFdataset(name_dataset,hdf_subgroup[myTag]);
1288                 hdf_dataset[cmpt_ds]->OpenOnDisk();
1289                 size=hdf_dataset[cmpt_ds]->GetSize();
1290                 
1291                 char * refFromFile =new char[size];
1292                 hdf_dataset[cmpt_ds]->ReadFromDisk(refFromFile);
1293                 hdf_dataset[cmpt_ds]->CloseOnDisk();
1294                 cmpt_ds++;
1295                 
1296                 bool _found3 = false;
1297                 SALOMEDS::SObject_var GSO = Study->FindObjectID(refFromFile);
1298                 SMESH::SMESH_subMesh_var aSubMesh;
1299                 GEOM::GEOM_Shape_var aSubShape;
1300                 
1301                 if (!CORBA::is_nil(GSO)) {
1302                   GSO->FindAttribute(anAttr, "AttributeIOR");
1303                   anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
1304                   char* SubShapeIOR = anIOR->Value();
1305                   aSubShape = GEOM::GEOM_Shape::_narrow(_orb->string_to_object(SubShapeIOR));
1306                   
1307                   if (!CORBA::is_nil(aSubShape)) {
1308                     aSubMesh = myNewMesh->GetElementsOnShape(aSubShape);
1309                     string iorString = _orb->object_to_string(aSubMesh);
1310                     sprintf(objectId,"%d",subMeshId);
1311                     _SMESHCorbaObj[string("SubMesh_")+string(objectId)] = iorString;
1312                     _found3 = true;
1313                     //SCRUTE(aSubMesh->GetNumberOfNodes());
1314                     //MESSAGE("yes");
1315                     //SCRUTE(aSubMesh->GetNumberOfElements());
1316                   }
1317                 }
1318                     
1319                 int nb_subgroup = hdf_subgroup[myTag]->nInternalObjects(); 
1320                 SCRUTE(nb_subgroup);
1321                 for (int k=0;k<nb_subgroup;k++) 
1322                   {
1323                     hdf_subgroup[myTag]->InternalObjectIndentify(k,sgname);
1324                     if (strcmp(sgname,"Ref on shape")==0) {
1325                       //nothing
1326                     }
1327                     else if (strcmp(sgname,"Applied Hypothesis")==0) {
1328                       //********** ref on applied hypothesis
1329                       MESSAGE("Applied Hypothesis");
1330                       strcpy(name_meshgroup,"Applied Hypothesis");
1331                       cmpt_sm++;
1332                       hdf_subgroup[10+cmpt_sm] = new HDFgroup(name_meshgroup,hdf_subgroup[myTag]);
1333                       hdf_subgroup[10+cmpt_sm]->OpenOnDisk();
1334                       nb_datasets = hdf_subgroup[10+cmpt_sm]->nInternalObjects();  
1335                       SCRUTE(nb_datasets);
1336                       
1337                       for (int l=0;l<nb_datasets;l++) {
1338                         sprintf(name_dataset,"Hyp %d",l);
1339                         hdf_dataset[cmpt_ds] = new HDFdataset(name_dataset,hdf_subgroup[10+cmpt_sm]);
1340                         hdf_dataset[cmpt_ds]->OpenOnDisk();
1341                         size=hdf_dataset[cmpt_ds]->GetSize();
1342                         
1343                         char * refFromFile =new char[size];
1344                         hdf_dataset[cmpt_ds]->ReadFromDisk(refFromFile);
1345                         hdf_dataset[cmpt_ds]->CloseOnDisk();
1346                         cmpt_ds++;
1347                         
1348                         if (_found3) {
1349                           SALOMEDS::SObject_var HypSO = Study->FindObjectID(refFromFile);
1350                           if (!CORBA::is_nil(HypSO)) {
1351                             HypSO->FindAttribute(anAttr, "AttributeIOR");
1352                             anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
1353                             if (!CORBA::is_nil(anIOR)) {
1354                               char* HypIOR = anIOR->Value();
1355                               SMESH::SMESH_Hypothesis_var anHyp = SMESH::SMESH_Hypothesis::_narrow(_orb->string_to_object(HypIOR));
1356                               if (!CORBA::is_nil(anHyp)) {
1357                                 SMESH::SMESH_Mesh_var aMesh = aSubMesh->GetFather();
1358                                 aMesh->AddHypothesis(aSubShape,anHyp);//essayer avec _SMESHCorbaObj
1359                                 MESSAGE("Hypothesis added ...");
1360                               }
1361                             }
1362                           }
1363                         }
1364                       } 
1365                     }
1366                     else if (strcmp(sgname,"Applied Algorithms")==0) {
1367                       //********** ref on applied algorithms
1368                       MESSAGE("Applied Algorithms");
1369                       strcpy(name_meshgroup,"Applied Algorithms");
1370                       cmpt_sm++;
1371                       hdf_subgroup[10+cmpt_sm] = new HDFgroup(name_meshgroup,hdf_subgroup[myTag]);
1372                       hdf_subgroup[10+cmpt_sm]->OpenOnDisk();
1373                       nb_datasets = hdf_subgroup[10+cmpt_sm]->nInternalObjects();  
1374                       SCRUTE(nb_datasets);
1375                       
1376                       for (int l=0;l<nb_datasets;l++) {
1377                         sprintf(name_dataset,"Algo %d",l);
1378                         hdf_dataset[cmpt_ds] = new HDFdataset(name_dataset,hdf_subgroup[10+cmpt_sm]);
1379                         hdf_dataset[cmpt_ds]->OpenOnDisk();
1380                         size=hdf_dataset[cmpt_ds]->GetSize();
1381                         
1382                         char * refFromFile =new char[size];
1383                         hdf_dataset[cmpt_ds]->ReadFromDisk(refFromFile);
1384                         hdf_dataset[cmpt_ds]->CloseOnDisk();
1385                         cmpt_ds++;
1386                         
1387                         if (_found3) {
1388                           SALOMEDS::SObject_var AlgoSO = Study->FindObjectID(refFromFile);
1389                           if (!CORBA::is_nil(AlgoSO)) {
1390                             AlgoSO->FindAttribute(anAttr, "AttributeIOR");
1391                             anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
1392                             if (!CORBA::is_nil(anIOR)) {
1393                               char* AlgoIOR = anIOR->Value();
1394                               //SCRUTE(AlgoIOR);
1395                               SMESH::SMESH_Hypothesis_var myHyp = SMESH::SMESH_Hypothesis::_narrow(_orb->string_to_object(AlgoIOR));
1396                               SMESH::SMESH_Algo_var anAlgo = SMESH::SMESH_Algo::_narrow(myHyp);
1397                               //SMESH::SMESH_Algo_var anAlgo = SMESH::SMESH_Algo::_narrow(_orb->string_to_object(AlgoIOR));
1398                               if (!CORBA::is_nil(anAlgo)) {
1399                                 SMESH::SMESH_Mesh_var aMesh = aSubMesh->GetFather();
1400                                 aMesh->AddHypothesis(aSubShape,anAlgo);//essayer avec _SMESHCorbaObj
1401                                 MESSAGE("Algorithms added ...");
1402                               }
1403                             }
1404                           }
1405                         }
1406                         
1407                       } 
1408                     }
1409                   }
1410                 hdf_subgroup[myTag]->CloseOnDisk();
1411               }
1412               hdf_subgroup[myLevel1Tag]->CloseOnDisk();
1413             }
1414           }
1415         hdf_group[myMeshId]->CloseOnDisk();     
1416         
1417         MESSAGE("End of Meshes Load");
1418       }
1419     }
1420   
1421   MESSAGE("End of SMESH_Gen::Load");
1422   
1423   hdf_file->CloseOnDisk();
1424   delete  hdf_file;
1425   hdf_file=0;
1426
1427   // Remove temporary files created from the stream
1428   if (isMultiFile) SALOMEDS_Tool::RemoveTemporaryFiles(tmpDir.ToCString(), aFileSeq.in(), true);
1429
1430   return true;
1431 }
1432
1433 bool SMESH_Gen_i::LoadASCII(SALOMEDS::SComponent_ptr theComponent,
1434                             const SALOMEDS::TMPFile& theStream,
1435                             const char* theURL,
1436                             bool isMultiFile) {
1437   return Load(theComponent, theStream, theURL, isMultiFile);
1438 }
1439
1440 //=============================================================================
1441 /*!
1442  *  
1443  */
1444 //=============================================================================
1445
1446 void SMESH_Gen_i::Close(SALOMEDS::SComponent_ptr theComponent)
1447 {
1448   MESSAGE("Close");
1449   SALOMEDS::Study_var aStudy = theComponent->GetStudy();
1450   SALOMEDS::ChildIterator_var itBig = aStudy->NewChildIterator(theComponent);
1451   for (; itBig->More();itBig->Next()) {
1452     SALOMEDS::SObject_var gotBranch = itBig->Value();
1453     
1454     // branch 1 : hypothesis
1455     if (gotBranch->Tag()==Tag_HypothesisRoot || gotBranch->Tag()==Tag_AlgorithmsRoot) {
1456       SALOMEDS::ChildIterator_var it = aStudy->NewChildIterator(gotBranch);
1457       for (; it->More();it->Next()) {
1458         SALOMEDS::SObject_var mySObject = it->Value();
1459         SALOMEDS::GenericAttribute_var anAttr;
1460         SALOMEDS::AttributeIOR_var anIOR;
1461         if ( mySObject->FindAttribute(anAttr, "AttributeIOR")) {
1462           anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
1463           SMESH::SMESH_Hypothesis_var myHyp =
1464             SMESH::SMESH_Hypothesis::_narrow(_orb->string_to_object(anIOR->Value()));
1465           char objectId[10];
1466           sprintf(objectId,"%d",myHyp->GetId());
1467 //        cout<<"********** delete Hyp "<<objectId<<endl;
1468           _SMESHCorbaObj.erase(string("Hypo_")+string(objectId));
1469           myHyp = SMESH::SMESH_Hypothesis::_narrow(_orb->string_to_object(anIOR->Value()));
1470         }
1471       }
1472     }
1473     // branch 2 : algorithms
1474     else if (gotBranch->Tag()>=3) {
1475       SALOMEDS::ChildIterator_var it = aStudy->NewChildIterator(gotBranch);
1476       for (; it->More();it->Next()) {
1477         SALOMEDS::SObject_var mySObject = it->Value();
1478         SALOMEDS::GenericAttribute_var anAttr;
1479         SALOMEDS::AttributeIOR_var anIOR;
1480         if ( mySObject->FindAttribute(anAttr, "AttributeIOR")) {
1481           anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
1482           SMESH::SMESH_Mesh_var myMesh =
1483             SMESH::SMESH_Mesh::_narrow(_orb->string_to_object(anIOR->Value()));
1484           if (!myMesh->_is_nil()) {
1485             char objectId[10];
1486             sprintf(objectId,"%d",myMesh->GetId());
1487 //          cout<<"********** delete Mesh "<<objectId<<endl;
1488             _SMESHCorbaObj.erase(string("Mesh_")+string(objectId));
1489             CORBA::release(myMesh);
1490           }
1491           else {
1492             SMESH::SMESH_subMesh_var mySubMesh =
1493               SMESH::SMESH_subMesh::_narrow(_orb->string_to_object(anIOR->Value()));
1494             if (!mySubMesh->_is_nil()) {
1495               char objectId[10];
1496               sprintf(objectId,"%d",mySubMesh->GetId());
1497 //            cout<<"********** delete SubMesh "<<objectId<<endl;
1498               _SMESHCorbaObj.erase(string("SubMesh_")+string(objectId));
1499               CORBA::release(mySubMesh);
1500             }
1501           }
1502         }
1503       }
1504     }
1505   }
1506 }
1507
1508 //=============================================================================
1509 /*!
1510  *  
1511  */
1512 //=============================================================================
1513
1514 char* SMESH_Gen_i::ComponentDataType()
1515 {
1516   MESSAGE("SMESH_Gen_i::ComponentDataType");
1517   return strdup("SMESH");
1518 }
1519
1520     
1521 //=============================================================================
1522 /*!
1523  *  
1524  */
1525 //=============================================================================
1526
1527 char* SMESH_Gen_i::IORToLocalPersistentID(SALOMEDS::SObject_ptr theSObject,
1528                                           const char* IORString,
1529                                           CORBA::Boolean isMultiFile,
1530                                           CORBA::Boolean isASCII)
1531 {
1532   MESSAGE("SMESH_Gen_i::IORToLocalPersistentID");
1533
1534   char objectId[10];
1535
1536   SMESH::SMESH_Algo_var myAlgo = SMESH::SMESH_Algo::_narrow(_orb->string_to_object(IORString));
1537   if (! CORBA::is_nil(myAlgo))
1538     {
1539       string prefix = "Hypo_";
1540       sprintf(objectId,"%d",myAlgo->GetId());
1541       string lpID = prefix + string(objectId);
1542       return CORBA::string_dup(lpID.c_str());
1543     }
1544   else {
1545     SMESH::SMESH_Hypothesis_var myHypo = SMESH::SMESH_Hypothesis::_narrow(_orb->string_to_object(IORString));
1546     if (! CORBA::is_nil(myHypo))
1547       {
1548         string prefix = "Hypo_";
1549         sprintf(objectId,"%d",myHypo->GetId());
1550         string lpID = prefix + string(objectId);
1551         return CORBA::string_dup(lpID.c_str());
1552       }
1553     else {
1554       SMESH::SMESH_Mesh_var myMesh = SMESH::SMESH_Mesh::_narrow(_orb->string_to_object(IORString));
1555       if (! CORBA::is_nil(myMesh))
1556         {
1557           string prefix = "Mesh_";
1558           sprintf(objectId,"%d",myMesh->GetId());
1559           string lpID = prefix + string(objectId);
1560           return CORBA::string_dup(lpID.c_str());
1561         }
1562       else {
1563         SMESH::SMESH_subMesh_var mySubMesh = SMESH::SMESH_subMesh::_narrow(_orb->string_to_object(IORString));
1564         if (! CORBA::is_nil(mySubMesh))
1565           {
1566             string prefix = "SubMesh_";
1567             sprintf(objectId,"%d",mySubMesh->GetId());
1568             string lpID = prefix + string(objectId);
1569             return CORBA::string_dup(lpID.c_str());
1570           }
1571         else return (strdup("no object"));
1572       }
1573     }
1574   }
1575 }
1576
1577 //=============================================================================
1578 /*!
1579  *  
1580  */
1581 //=============================================================================
1582
1583 char* SMESH_Gen_i::LocalPersistentIDToIOR(SALOMEDS::SObject_ptr theSObject,
1584                                           const char* aLocalPersistentID,
1585                                           CORBA::Boolean isMultiFile,
1586                                           CORBA::Boolean isASCII)
1587 {
1588   MESSAGE("SMESH_Gen_i::LocalPersistentIDToIOR");
1589   SCRUTE(aLocalPersistentID);
1590   string clef = string(aLocalPersistentID);
1591   SCRUTE(_SMESHCorbaObj[clef].c_str());
1592   return CORBA::string_dup(_SMESHCorbaObj[clef].c_str());
1593 }
1594
1595 //=============================================================================
1596 /*!
1597  *  
1598  */
1599 //=============================================================================
1600
1601 SMESH_topo* SMESH_Gen_i::ExploreMainShape(GEOM::GEOM_Gen_ptr geomEngine,
1602                                           CORBA::Long studyId,
1603                                           GEOM::GEOM_Shape_ptr aShape)
1604 {
1605   MESSAGE("SMESH_Mesh_i::ExploreMainShape");
1606   // _narrow() duplicates the reference and check the type
1607   GEOM::GEOM_Gen_var geom = GEOM::GEOM_Gen::_narrow(geomEngine);
1608   GEOM::GEOM_Shape_var myShape = GEOM::GEOM_Shape::_narrow(aShape);
1609
1610   if (CORBA::is_nil(geom))
1611     THROW_SALOME_CORBA_EXCEPTION("bad geom reference", \
1612                                  SALOME::BAD_PARAM);
1613   if (CORBA::is_nil(myShape))
1614     THROW_SALOME_CORBA_EXCEPTION("bad shape reference", \
1615                                  SALOME::BAD_PARAM);
1616   MESSAGE("---");
1617   SCRUTE(myShape->Name());
1618   geom->GetCurrentStudy(studyId);
1619   SCRUTE(studyId);
1620   TopoDS_Shape mainShape  = _ShapeReader->GetShape(geom,myShape);
1621   MESSAGE("---");
1622
1623   // create an SMESH_topo object for the mainShape
1624
1625   SMESH_topo* myTopo = new SMESH_topo();
1626   MESSAGE("---");
1627
1628   // explore local TopoDS_Shape, store reference of local TopoDS subShapes
1629
1630   for (TopExp_Explorer exp(mainShape,TopAbs_COMPOUND); exp.More(); exp.Next())
1631     {
1632       const TopoDS_Compound& E = TopoDS::Compound(exp.Current());
1633       int i = myTopo->_myShapes[TopAbs_COMPOUND].Add(E);
1634       SCRUTE(i);
1635     }
1636   for (TopExp_Explorer exp(mainShape,TopAbs_COMPSOLID); exp.More(); exp.Next())
1637     {
1638       const TopoDS_CompSolid& E = TopoDS::CompSolid(exp.Current());
1639       int i = myTopo->_myShapes[TopAbs_COMPSOLID].Add(E);
1640       SCRUTE(i);
1641     }
1642   for (TopExp_Explorer exp(mainShape,TopAbs_SOLID); exp.More(); exp.Next())
1643     {
1644       const TopoDS_Solid& E = TopoDS::Solid(exp.Current());
1645       int i = myTopo->_myShapes[TopAbs_SOLID].Add(E);
1646       SCRUTE(i);
1647     }
1648   for (TopExp_Explorer exp(mainShape,TopAbs_SHELL); exp.More(); exp.Next())
1649     {
1650       const TopoDS_Shell& E = TopoDS::Shell(exp.Current());
1651       int i = myTopo->_myShapes[TopAbs_SHELL].Add(E);
1652       SCRUTE(i);
1653     }
1654   for (TopExp_Explorer exp(mainShape,TopAbs_FACE); exp.More(); exp.Next())
1655     {
1656       const TopoDS_Face& E = TopoDS::Face(exp.Current());
1657       int i = myTopo->_myShapes[TopAbs_FACE].Add(E);
1658       SCRUTE(i);
1659     }
1660   for (TopExp_Explorer exp(mainShape,TopAbs_WIRE); exp.More(); exp.Next())
1661     {
1662       const TopoDS_Wire& E = TopoDS::Wire(exp.Current());
1663       int i = myTopo->_myShapes[TopAbs_WIRE].Add(E);
1664       SCRUTE(i);
1665     }
1666   for (TopExp_Explorer exp(mainShape,TopAbs_EDGE); exp.More(); exp.Next())
1667     {
1668       const TopoDS_Edge& E = TopoDS::Edge(exp.Current());
1669       int i = myTopo->_myShapes[TopAbs_EDGE].Add(E);
1670       SCRUTE(i);
1671     }
1672   for (TopExp_Explorer exp(mainShape,TopAbs_VERTEX); exp.More(); exp.Next())
1673     {
1674       const TopoDS_Vertex& E = TopoDS::Vertex(exp.Current());
1675       int i = myTopo->_myShapes[TopAbs_VERTEX].Add(E);
1676       SCRUTE(i);
1677     }
1678   
1679   // explore subShapes of distant CORBA object,
1680   // associate distant CORBA subShape references
1681   // with local reference to local TopoDS subShape
1682
1683   string filenode = "toposhape.txt";
1684   ofstream fic(filenode.c_str());
1685
1686   for (int shapeType = TopAbs_COMPOUND; shapeType < TopAbs_SHAPE; shapeType++)
1687     {
1688       fic << "shape type : " << SMESH_shapeTypeNames[shapeType];
1689
1690       GEOM::GEOM_Gen::ListOfGeomShapes_var subShapes
1691         = geom->SubShapeAll(myShape,shapeType); 
1692       int nbSubShapes = subShapes->length();
1693       int nbLocal = myTopo->_myShapes[shapeType].Extent();
1694       fic << " - number of elements: " << nbSubShapes << endl;
1695       ASSERT(nbSubShapes == nbLocal);
1696
1697       for (int i=0; i< nbSubShapes; i++)
1698         {
1699           GEOM::GEOM_Shape_var aSubShape = subShapes[i];
1700           string idShape = SMESH_topo::GetShapeLocalId(aSubShape);
1701           fic << "  " <<  idShape;
1702           SCRUTE(idShape);
1703           TopoDS_Shape aLocShape  = _ShapeReader->GetShape(geom,aSubShape);
1704           for (int j=1; j <= nbLocal; j++)
1705             if (aLocShape.IsSame(myTopo->_myShapes[shapeType].FindKey(j)))
1706               {
1707                 MESSAGE("  --- trouve = " << j);
1708                 myTopo->_mapIndShapes[shapeType][idShape] = j;
1709                 fic << "  --- trouve = " << j;
1710                 break;
1711               }
1712           fic << endl;
1713         }
1714     }
1715   fic.close();
1716
1717   return myTopo;
1718 }
1719       
1720 //=============================================================================
1721 /*! 
1722  * C factory, accessible with dlsym, after dlopen  
1723  */
1724 //=============================================================================
1725
1726 extern "C"
1727 {
1728   PortableServer::ObjectId * SMESHEngine_factory(
1729                                CORBA::ORB_ptr orb,
1730                                PortableServer::POA_ptr poa, 
1731                                PortableServer::ObjectId * contId,
1732                                const char *instanceName, 
1733                                const char *interfaceName)
1734   {
1735     MESSAGE("PortableServer::ObjectId * SMESHEngine_factory()");
1736     SCRUTE(interfaceName);
1737     SMESH_Gen_i * mySMESH_Gen 
1738       = new SMESH_Gen_i(orb, poa, contId, instanceName, interfaceName);
1739     return mySMESH_Gen->getId() ;
1740   }
1741 }