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