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