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