Salome HOME
511cddd735eb272cfc78fe786644654bf74466b2
[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
1149                 if (_found)
1150                 {
1151                         SALOMEDS::SObject_var AlgoSO =
1152                                 Study->FindObjectID(refFromFile);
1153                         if (!CORBA::is_nil(AlgoSO))
1154                         {
1155                                 SALOMEDS::GenericAttribute_var anAttr;
1156                                 AlgoSO->FindAttribute(anAttr, "AttributeIOR");
1157                                 SALOMEDS::AttributeIOR_var anIOR =
1158                                         SALOMEDS::AttributeIOR::_narrow(anAttr);
1159                                 if (!CORBA::is_nil(anIOR))
1160                                 {
1161                                         char *AlgoIOR = anIOR->Value();
1162                                         //SCRUTE(AlgoIOR);
1163                                         SMESH::SMESH_Hypothesis_var myHyp =
1164                                                 SMESH::SMESH_Hypothesis::_narrow(_orb->
1165                                                 string_to_object(AlgoIOR));
1166                                         SMESH::SMESH_Algo_var anAlgo =
1167                                                 SMESH::SMESH_Algo::_narrow(myHyp);
1168                                                                                         
1169                                         if (!CORBA::is_nil(anAlgo))
1170                                         {
1171                                                 myNewMesh->AddHypothesis(aShape, anAlgo);       //essayer avec _SMESHCorbaObj
1172                                                 MESSAGE("Algorithms added ...");
1173                                         }
1174                                 }
1175                         }
1176                 }
1177         }
1178         hdf_subgroup->CloseOnDisk();
1179 }
1180
1181 /**
1182  * @param hdfGroupMeshId The group where to read the hypothesis
1183  * @param msgname ???
1184  * @param Study The study where to create the hypothesis
1185  * @param myNewMesh The mesh on which the hypothesis is applied
1186  */
1187 void SMESH_Gen_i::loadSubMeshes(HDFgroup * hdfGroupMeshId, char * msgname,
1188         SALOMEDS::Study_var Study, SMESH::SMESH_Mesh_var myNewMesh)
1189 {
1190         MESSAGE("SMESH_Gen_i::loadSubMeshes");
1191         HDFgroup * hdf_subgroupmyLevel1Tag = new HDFgroup(msgname, hdfGroupMeshId);
1192         hdf_subgroupmyLevel1Tag->OpenOnDisk();
1193
1194         int nb_submeshes = hdf_subgroupmyLevel1Tag->nInternalObjects();
1195         char name_meshgroup[30];
1196         //SCRUTE(nb_submeshes);
1197
1198         for (int j = 0; j < nb_submeshes; j++)
1199         {
1200                 //cmpt_sm++;
1201                 //myTag = 10 + cmpt_sm;
1202                 hdf_subgroupmyLevel1Tag->InternalObjectIndentify(j, name_meshgroup);                            
1203
1204                 HDFgroup * hdf_subgroupmyTag = new HDFgroup(name_meshgroup, 
1205                         hdf_subgroupmyLevel1Tag);
1206                 hdf_subgroupmyTag->OpenOnDisk();
1207                 int subMeshId = atoi((string(name_meshgroup).substr(8, 18)).c_str());
1208
1209                 MESSAGE("Ref on shape");
1210                 //********** ref on shape               
1211                 HDFdataset * hdf_dataset =
1212                         new HDFdataset("Ref on shape", hdf_subgroupmyTag);
1213                 hdf_dataset->OpenOnDisk();
1214
1215                 char *refFromFile = new char[hdf_dataset->GetSize()];
1216                 hdf_dataset->ReadFromDisk(refFromFile);
1217                 hdf_dataset->CloseOnDisk();
1218
1219                 bool _found3 = false;
1220                 SALOMEDS::SObject_var GSO = Study->FindObjectID(refFromFile);
1221                 SMESH::SMESH_subMesh_var aSubMesh;
1222                 GEOM::GEOM_Shape_var aSubShape;
1223
1224                 if (!CORBA::is_nil(GSO))
1225                 {
1226                         SALOMEDS::GenericAttribute_var anAttr;
1227                         GSO->FindAttribute(anAttr, "AttributeIOR");
1228                         SALOMEDS::AttributeIOR_var anIOR = 
1229                                 SALOMEDS::AttributeIOR::_narrow(anAttr);
1230                                 
1231                         char *SubShapeIOR = anIOR->Value();
1232                         aSubShape =
1233                                 GEOM::GEOM_Shape::_narrow(_orb-> string_to_object(SubShapeIOR));
1234
1235                         if (!CORBA::is_nil(aSubShape))
1236                         {
1237                                 aSubMesh = myNewMesh->GetElementsOnShape(aSubShape);
1238                                 string iorString = _orb->object_to_string(aSubMesh);
1239                                 char objectId[10];
1240                                 sprintf(objectId, "%d", subMeshId);
1241                                 _SMESHCorbaObj[string("SubMesh_") + string(objectId)] =
1242                                          iorString;
1243                                 _found3 = true;
1244                         }
1245                 }
1246
1247                 int nb_subgroup = hdf_subgroupmyTag->nInternalObjects();
1248                 SCRUTE(nb_subgroup);
1249                 char sgname[HDF_NAME_MAX_LEN + 1];
1250                 for (int k = 0; k < nb_subgroup; k++)
1251                 {
1252                         hdf_subgroupmyTag->InternalObjectIndentify(k, sgname);
1253                         if (strcmp(sgname, "Ref on shape") == 0)
1254                         {
1255                                 //nothing
1256                         }
1257                         else if (strcmp(sgname, "Applied Hypothesis") == 0)
1258                         {
1259                                 //********** ref on applied hypothesis
1260                                 MESSAGE("Applied Hypothesis");
1261                                 strcpy(name_meshgroup, "Applied Hypothesis");
1262                                 //cmpt_sm++;
1263                                 HDFgroup * hdf_subgroup10cmpt_sm = new HDFgroup(name_meshgroup,
1264                                         hdf_subgroupmyTag);
1265                                         
1266                                 hdf_subgroup10cmpt_sm->OpenOnDisk();
1267                                 int nb_datasets = hdf_subgroup10cmpt_sm->nInternalObjects();
1268                                 // SCRUTE(nb_datasets);
1269
1270                                 char name_dataset[30];
1271                                 for (int l = 0; l < nb_datasets; l++)
1272                                 {
1273                                         sprintf(name_dataset, "Hyp %d", l);
1274                                         HDFdataset * hdf_datasetcmpt_ds =
1275                                                 new HDFdataset(name_dataset, hdf_subgroup10cmpt_sm);
1276                                         hdf_datasetcmpt_ds->OpenOnDisk();
1277                                         int size = hdf_datasetcmpt_ds->GetSize();
1278
1279                                         char *refFromFile = new char[size];
1280                                         hdf_datasetcmpt_ds->ReadFromDisk(refFromFile);
1281                                         hdf_datasetcmpt_ds->CloseOnDisk();
1282                                         //cmpt_ds++;
1283
1284                                         if (_found3)
1285                                         {
1286                                                 SALOMEDS::SObject_var HypSO =
1287                                                         Study->FindObjectID(refFromFile);
1288                                                 if (!CORBA::is_nil(HypSO))
1289                                                 {
1290                                                         SALOMEDS::GenericAttribute_var anAttr;
1291                                                         SALOMEDS::AttributeIOR_var anIOR;
1292                                                         HypSO->FindAttribute(anAttr, "AttributeIOR");
1293                                                         anIOR = SALOMEDS::AttributeIOR:: _narrow(anAttr);
1294                                                         if (!CORBA::is_nil(anIOR))
1295                                                         {
1296                                                                 char *HypIOR = anIOR->Value();
1297                                                                 SMESH::SMESH_Hypothesis_var anHyp =
1298                                                                         SMESH::SMESH_Hypothesis::_narrow(_orb->
1299                                                                         string_to_object(HypIOR));
1300                                                                 if (!CORBA::is_nil(anHyp))
1301                                                                 {
1302                                                                         SMESH::SMESH_Mesh_var aMesh = 
1303                                                                                 aSubMesh->GetFather();
1304                                                                         aMesh->AddHypothesis(aSubShape, anHyp); //essayer avec _SMESHCorbaObj
1305                                                                         MESSAGE("Hypothesis added ...");
1306                                                                 }
1307                                                         }
1308                                                 }
1309                                         }
1310                                 }
1311                         }
1312                         else if (strcmp(sgname, "Applied Algorithms") == 0)
1313                         {
1314                                 //********** ref on applied algorithms
1315                                 MESSAGE("Applied Algorithms");
1316                                 strcpy(name_meshgroup, "Applied Algorithms");
1317                                 //cmpt_sm++;
1318                                 HDFgroup * hdf_subgroup10cmpt_sm = new HDFgroup(name_meshgroup,
1319                                         hdf_subgroupmyTag);
1320                                 hdf_subgroup10cmpt_sm->OpenOnDisk();
1321                                 int nb_datasets = hdf_subgroup10cmpt_sm->nInternalObjects();
1322                                 SCRUTE(nb_datasets);
1323
1324                                 char name_dataset[30];
1325                                 for (int l = 0; l < nb_datasets; l++)
1326                                 {
1327                                         sprintf(name_dataset, "Algo %d", l);
1328                                         HDFdataset * hdf_datasetcmpt_ds = new HDFdataset(
1329                                                 name_dataset, hdf_subgroup10cmpt_sm);
1330                                         hdf_datasetcmpt_ds->OpenOnDisk();
1331                                         int size = hdf_datasetcmpt_ds->GetSize();
1332
1333                                         char *refFromFile = new char[size];
1334                                         hdf_datasetcmpt_ds->ReadFromDisk(refFromFile);
1335                                         hdf_datasetcmpt_ds->CloseOnDisk();
1336                                         delete hdf_datasetcmpt_ds;
1337                                         //cmpt_ds++;
1338
1339                                         if (_found3)
1340                                         {
1341                                                 SALOMEDS::SObject_var AlgoSO =
1342                                                         Study->FindObjectID(refFromFile);
1343                                                 if (!CORBA::is_nil(AlgoSO))
1344                                                 {
1345                                                         SALOMEDS::GenericAttribute_var anAttr;
1346                                                         SALOMEDS::AttributeIOR_var anIOR;
1347                                                         AlgoSO->FindAttribute(anAttr, "AttributeIOR");
1348                                                         anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
1349                                                         if (!CORBA::is_nil(anIOR))
1350                                                         {
1351                                                                 char *AlgoIOR = anIOR->Value();
1352                                                                 //SCRUTE(AlgoIOR);
1353                                                                 SMESH::SMESH_Hypothesis_var myHyp =
1354                                                                         SMESH::SMESH_Hypothesis::_narrow(_orb->
1355                                                                         string_to_object(AlgoIOR));
1356                                                                 SMESH::SMESH_Algo_var anAlgo =
1357                                                                         SMESH::SMESH_Algo::_narrow(myHyp);
1358                                                                 //SMESH::SMESH_Algo_var anAlgo = SMESH::SMESH_Algo::_narrow(_orb->string_to_object(AlgoIOR));
1359                                                                 if (!CORBA::is_nil(anAlgo))
1360                                                                 {
1361                                                                         SMESH::SMESH_Mesh_var aMesh =
1362                                                                                 aSubMesh->GetFather();
1363                                                                         aMesh->AddHypothesis(aSubShape, anAlgo);        //essayer avec _SMESHCorbaObj
1364                                                                         MESSAGE("Algorithms added ...");
1365                                                                 }
1366                                                         }
1367                                                 }
1368                                         }
1369                                 }
1370                         }
1371                 }
1372                 hdf_subgroupmyTag->CloseOnDisk();
1373         }
1374         hdf_subgroupmyLevel1Tag->CloseOnDisk();
1375 }
1376
1377 GEOM::GEOM_Gen_var SMESH_Gen_i::getGeomEngine()
1378 {
1379         MESSAGE("SMESH_Gen_i::getGeomEngine");
1380         SALOME_NamingService *_NS = SINGLETON_ < SALOME_NamingService >::Instance();
1381         ASSERT(SINGLETON_ < SALOME_NamingService >::IsAlreadyExisting());
1382         _NS->init_orb(_orb);
1383         SALOME_LifeCycleCORBA *myEnginesLifeCycle = new SALOME_LifeCycleCORBA(_NS);
1384         Engines::Component_var geomEngine =
1385         myEnginesLifeCycle->FindOrLoad_Component("FactoryServer", "GEOM");
1386         GEOM::GEOM_Gen_var myGeomEngine = GEOM::GEOM_Gen::_narrow(geomEngine);
1387         return myGeomEngine;    
1388 }
1389
1390 GEOM::GEOM_Shape_var SMESH_Gen_i::getShape(SALOMEDS::Study_var Study, char * refFromFile)
1391 {
1392         MESSAGE("SMESH_Gen_i::getShape("<<Study<<","<<refFromFile<<")");
1393         SCRUTE(CORBA::is_nil(Study));
1394         SALOMEDS::SObject_var CSO = Study->FindObjectID(refFromFile);
1395         GEOM::GEOM_Shape_var aShape;
1396
1397         if (!CORBA::is_nil(CSO))
1398         {
1399                 SALOMEDS::GenericAttribute_var anAttr;
1400                 CSO->FindAttribute(anAttr, "AttributeIOR");
1401                 if (!CORBA::is_nil(CSO))
1402                 {
1403                         MESSAGE("The shape was not loaded. Try to load it.");
1404                         SALOMEDS::Driver_var driver = SALOMEDS::Driver::_narrow(getGeomEngine());
1405                         SALOMEDS::SComponent_var SCO = SALOMEDS::SComponent::_narrow(Study->FindObject("Geometry"));
1406                         SALOMEDS::StudyBuilder_var B = Study->NewBuilder();     
1407                         B->LoadWith(SCO,driver);
1408                         CSO->FindAttribute(anAttr, "AttributeIOR");
1409                 }
1410
1411                 SALOMEDS::AttributeIOR_var anIOR =
1412                         SALOMEDS::AttributeIOR::_narrow(anAttr);
1413                 
1414                 char *ShapeIOR = anIOR->Value();
1415                 aShape =
1416                         GEOM::GEOM_Shape::_narrow(_orb->string_to_object(ShapeIOR));
1417         }
1418         return aShape;
1419 }
1420
1421 void SMESH_Gen_i::loadMesh(char * name, HDFfile * hdf_file,
1422         char* meshfile, SALOMEDS::Study_var Study)
1423 {
1424         MESSAGE("in mesh load");
1425         char msgname[HDF_NAME_MAX_LEN + 1];     
1426         char objectId[10];
1427         char name_of_group[HDF_NAME_MAX_LEN + 1];
1428         
1429         int myMeshId =
1430                 atoi((string(name).substr(5, 5)).c_str());
1431         SCRUTE(myMeshId);
1432
1433         HDFgroup * hdfGroupMeshId = new HDFgroup(name, hdf_file);
1434         hdfGroupMeshId->OpenOnDisk();
1435
1436         int nb_meshsubgroup = hdfGroupMeshId->nInternalObjects();
1437         SCRUTE(nb_meshsubgroup);
1438
1439         //********** Loading of the file name where the data are stored
1440         MESSAGE("Mesh data file");
1441         strcpy(name_of_group, "Mesh data");
1442         HDFdataset * dataset =
1443                 new HDFdataset(name_of_group, hdfGroupMeshId);
1444         dataset->OpenOnDisk();
1445
1446         char *datafilename = new char[dataset->GetSize()];
1447         dataset->ReadFromDisk(datafilename);
1448         dataset->CloseOnDisk(); 
1449         MESSAGE("datafilename="<<datafilename<<" but this is ignored. We will read from "<<meshfile);
1450         datafilename=meshfile;
1451
1452         //********** Loading of the reference on the shape
1453         //********** and mesh initialization
1454         MESSAGE("Ref on shape");
1455         strcpy(name_of_group, "Ref on shape");
1456         dataset =
1457                 new HDFdataset(name_of_group, hdfGroupMeshId);
1458         dataset->OpenOnDisk();
1459         
1460         char *refFromFile = new char[dataset->GetSize()];
1461         dataset->ReadFromDisk(refFromFile);
1462         dataset->CloseOnDisk(); 
1463
1464         bool _found = false;
1465         SCRUTE(refFromFile);
1466         SMESH::SMESH_Mesh_var myNewMesh;
1467         GEOM::GEOM_Shape_var aShape=getShape(Study, refFromFile);
1468
1469         if (!CORBA::is_nil(aShape))
1470         {
1471                 _found = true;
1472                 myNewMesh = this->Init(getGeomEngine(), Study->StudyId(), aShape);
1473                 string iorString = _orb->object_to_string(myNewMesh);
1474                 sprintf(objectId, "%ld", myNewMesh->GetId());
1475                 _SMESHCorbaObj[string("Mesh_") + string(objectId)] = iorString;
1476
1477                 
1478                 //********** 
1479                 //********** Loading of mesh data
1480                 if (strcmp(datafilename, "No data") != 0)
1481                 {                       
1482                         StudyContext_iStruct *myStudyContext =
1483                                 _mapStudyContext_i[Study->StudyId()];
1484                         int meshId = myNewMesh->GetId();
1485                         SMESH_Mesh_i *meshServant =
1486                                 myStudyContext->mapMesh_i[meshId];
1487                         ::SMESH_Mesh & myLocMesh = meshServant->GetImpl();
1488                         SMESHDS_Mesh *mySMESHDSMesh = myLocMesh.GetMeshDS();
1489
1490                         Mesh_Reader *myReader = SMESHDriver::GetMeshReader("MED");
1491                         myReader->SetMesh(mySMESHDSMesh);
1492                         myReader->SetMeshId(myMeshId);
1493                         myReader->SetFile(datafilename);
1494                         myReader->Read();
1495                         mySMESHDSMesh->logFullUpdate();
1496                         MESSAGE("Loaded a mesh with " << mySMESHDSMesh->NbNodes() <<" nodes");
1497                 }
1498         }
1499         //********** 
1500         //}
1501         //else if (strcmp(msgname,"Applied Hypothesis")==0) {
1502         for (int ii = 0; ii < nb_meshsubgroup; ii++)
1503         {
1504                 hdfGroupMeshId->InternalObjectIndentify(ii, msgname);
1505                 if (strcmp(msgname, "Mesh data") == 0)
1506                 {
1507                         //nothing
1508                 }
1509                 else if (strcmp(msgname, "Ref on shape") == 0)
1510                 {
1511                         //nothing
1512                 }
1513                 else if (strcmp(msgname, "Applied Hypothesis") == 0)
1514                 {
1515                         loadAppliedHypothesis(hdfGroupMeshId, _found, Study, myNewMesh,
1516                                 aShape);
1517                 }
1518                 else if (strcmp(msgname, "Applied Algorithms") == 0)
1519                 {
1520                         loadAppliedAlgorithms(hdfGroupMeshId, _found, Study, myNewMesh,
1521                                 aShape);
1522                 }
1523                 else if (string(msgname).substr(0, 9) == string("SubMeshes"))
1524                 {
1525                         loadSubMeshes(hdfGroupMeshId, msgname, Study, myNewMesh);
1526                 }
1527         }
1528         hdfGroupMeshId->CloseOnDisk();
1529         MESSAGE("End of Meshes Load");
1530 }
1531
1532 /**
1533  * Load the part of the study related to SMESH.
1534  */
1535 bool SMESH_Gen_i::Load(SALOMEDS::SComponent_ptr theComponent,
1536         const SALOMEDS::TMPFile & theStream, const char *theURL, bool isMultiFile)
1537 {
1538         MESSAGE("SMESH_Gen_i::Load");   
1539         // Get temporary files location
1540         TCollection_AsciiString tmpDir =
1541                 isMultiFile ? TCollection_AsciiString((char *)theURL) : SALOMEDS_Tool::
1542                 GetTmpDir();
1543
1544         // Convert the stream into sequence of files to process
1545         SALOMEDS::ListOfFileNames_var aFileSeq =
1546                 SALOMEDS_Tool::PutStreamToFiles(theStream, tmpDir.ToCString(),
1547                 isMultiFile);
1548
1549         TCollection_AsciiString aStudyName("");
1550         if (isMultiFile)
1551                 aStudyName =
1552                         (SALOMEDS_Tool::GetNameFromPath(theComponent->GetStudy()->URL()));
1553
1554         // Set names of temporary files
1555         TCollection_AsciiString filename =
1556                 tmpDir + aStudyName + TCollection_AsciiString("_SMESH.hdf");
1557         TCollection_AsciiString hypofile =
1558                 tmpDir + aStudyName + TCollection_AsciiString("_SMESH_Hypo.txt");
1559         TCollection_AsciiString algofile =
1560                 tmpDir + aStudyName + TCollection_AsciiString("_SMESH_Algo.txt");
1561         TCollection_AsciiString meshfile =
1562                 tmpDir + aStudyName + TCollection_AsciiString("_SMESH_Mesh.med");
1563
1564         SALOMEDS::Study_var Study = theComponent->GetStudy();
1565         int studyId = Study->StudyId();
1566         SCRUTE(studyId);
1567
1568         SALOMEDS::AttributeName_var aName;
1569         
1570         SALOMEDS::SComponent_var fathergeom = Study->FindComponent("GEOM");
1571         SALOMEDS::SComponent_var myGeomSComp =
1572                 SALOMEDS::SComponent::_narrow(fathergeom);
1573         SCRUTE(fathergeom);
1574
1575         char name[HDF_NAME_MAX_LEN + 1];
1576         map < int, HDFgroup * > hdf_subgroup;   
1577
1578
1579         //************* HDF file opening
1580         HDFfile *hdf_file = new HDFfile(filename.ToCString());
1581         try
1582         {
1583                 hdf_file->OpenOnDisk(HDF_RDONLY);
1584         }
1585         catch(HDFexception)
1586         {
1587                 MESSAGE("Load(): " << filename << " not found!");
1588                 return false;
1589         }
1590
1591         //****************************
1592
1593         int nb_group = hdf_file->nInternalObjects();
1594         SCRUTE(nb_group);
1595         for (int i = 0; i < nb_group; i++)
1596         {
1597                 hdf_file->InternalObjectIndentify(i, name);
1598                 if (strcmp(name, "Hypothesis") == 0)
1599                         loadHypothesis(name, hdf_file, hypofile.ToCString(), studyId);
1600                 else if (strcmp(name, "Algorithms") == 0)
1601                         loadAlgorithms(name, hdf_file, algofile.ToCString(), studyId);
1602                 else if (string(name).substr(0, 4) == string("Mesh"))
1603                         loadMesh(name, hdf_file, meshfile.ToCString(), Study);
1604         }
1605
1606         MESSAGE("End of SMESH_Gen::Load");
1607
1608         hdf_file->CloseOnDisk();
1609
1610         // Remove temporary files created from the stream
1611         if (isMultiFile)
1612                 SALOMEDS_Tool::RemoveTemporaryFiles(tmpDir.ToCString(), aFileSeq.in(),
1613                         true);
1614
1615         return true;
1616 }
1617
1618 bool SMESH_Gen_i::LoadASCII(SALOMEDS::SComponent_ptr theComponent,
1619         const SALOMEDS::TMPFile & theStream, const char *theURL, bool isMultiFile)
1620 {
1621         return Load(theComponent, theStream, theURL, isMultiFile);
1622 }
1623
1624 //=============================================================================
1625 /*!
1626  *  
1627  */
1628 //=============================================================================
1629
1630 void SMESH_Gen_i::Close(SALOMEDS::SComponent_ptr theComponent)
1631 {
1632         MESSAGE("Close");
1633         SALOMEDS::Study_var aStudy = theComponent->GetStudy();
1634         SALOMEDS::ChildIterator_var itBig = aStudy->NewChildIterator(theComponent);
1635         for (; itBig->More(); itBig->Next())
1636         {
1637                 SALOMEDS::SObject_var gotBranch = itBig->Value();
1638
1639                 // branch 1 : hypothesis
1640                 if (gotBranch->Tag() == Tag_HypothesisRoot ||
1641                         gotBranch->Tag() == Tag_AlgorithmsRoot)
1642                 {
1643                         SALOMEDS::ChildIterator_var it =
1644                                 aStudy->NewChildIterator(gotBranch);
1645                         for (; it->More(); it->Next())
1646                         {
1647                                 SALOMEDS::SObject_var mySObject = it->Value();
1648                                 SALOMEDS::GenericAttribute_var anAttr;
1649                                 SALOMEDS::AttributeIOR_var anIOR;
1650                                 if (mySObject->FindAttribute(anAttr, "AttributeIOR"))
1651                                 {
1652                                         anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
1653                                         SMESH::SMESH_Hypothesis_var myHyp =
1654                                                 SMESH::SMESH_Hypothesis::_narrow(_orb->
1655                                                 string_to_object(anIOR->Value()));
1656                                         char objectId[10];
1657                                         sprintf(objectId, "%ld", myHyp->GetId());
1658 //        cout<<"********** delete Hyp "<<objectId<<endl;
1659                                         _SMESHCorbaObj.erase(string("Hypo_") + string(objectId));
1660                                         myHyp =
1661                                                 SMESH::SMESH_Hypothesis::_narrow(_orb->
1662                                                 string_to_object(anIOR->Value()));
1663                                 }
1664                         }
1665                 }
1666                 // branch 2 : algorithms
1667                 else if (gotBranch->Tag() >= 3)
1668                 {
1669                         SALOMEDS::ChildIterator_var it =
1670                                 aStudy->NewChildIterator(gotBranch);
1671                         for (; it->More(); it->Next())
1672                         {
1673                                 SALOMEDS::SObject_var mySObject = it->Value();
1674                                 SALOMEDS::GenericAttribute_var anAttr;
1675                                 SALOMEDS::AttributeIOR_var anIOR;
1676                                 if (mySObject->FindAttribute(anAttr, "AttributeIOR"))
1677                                 {
1678                                         anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
1679                                         SMESH::SMESH_Mesh_var myMesh =
1680                                                 SMESH::SMESH_Mesh::_narrow(_orb->
1681                                                 string_to_object(anIOR->Value()));
1682                                         if (!myMesh->_is_nil())
1683                                         {
1684                                                 char objectId[10];
1685                                                 sprintf(objectId, "%ld", myMesh->GetId());
1686 //          cout<<"********** delete Mesh "<<objectId<<endl;
1687                                                 _SMESHCorbaObj.erase(string("Mesh_") +
1688                                                         string(objectId));
1689                                                 CORBA::release(myMesh);
1690                                         }
1691                                         else
1692                                         {
1693                                                 SMESH::SMESH_subMesh_var mySubMesh =
1694                                                         SMESH::SMESH_subMesh::_narrow(_orb->
1695                                                         string_to_object(anIOR->Value()));
1696                                                 if (!mySubMesh->_is_nil())
1697                                                 {
1698                                                         char objectId[10];
1699                                                         sprintf(objectId, "%ld", mySubMesh->GetId());
1700 //            cout<<"********** delete SubMesh "<<objectId<<endl;
1701                                                         _SMESHCorbaObj.erase(string("SubMesh_") +
1702                                                                 string(objectId));
1703                                                         CORBA::release(mySubMesh);
1704                                                 }
1705                                         }
1706                                 }
1707                         }
1708                 }
1709         }
1710 }
1711
1712 //=============================================================================
1713 /*!
1714  *  
1715  */
1716 //=============================================================================
1717
1718 char *SMESH_Gen_i::ComponentDataType()
1719 {
1720         MESSAGE("SMESH_Gen_i::ComponentDataType");
1721         return strdup("SMESH");
1722 }
1723
1724 //=============================================================================
1725 /*!
1726  *  
1727  */
1728 //=============================================================================
1729
1730 char *SMESH_Gen_i::IORToLocalPersistentID(SALOMEDS::SObject_ptr theSObject,
1731         const char *IORString, CORBA::Boolean isMultiFile, CORBA::Boolean isASCII)
1732 {
1733         MESSAGE("SMESH_Gen_i::IORToLocalPersistentID");
1734
1735         char objectId[10];
1736
1737         SMESH::SMESH_Algo_var myAlgo =
1738                 SMESH::SMESH_Algo::_narrow(_orb->string_to_object(IORString));
1739         if (!CORBA::is_nil(myAlgo))
1740         {
1741                 string prefix = "Hypo_";
1742                 sprintf(objectId, "%ld", myAlgo->GetId());
1743                 string lpID = prefix + string(objectId);
1744                 return CORBA::string_dup(lpID.c_str());
1745         }
1746         else
1747         {
1748                 SMESH::SMESH_Hypothesis_var myHypo =
1749                         SMESH::SMESH_Hypothesis::_narrow(_orb->string_to_object(IORString));
1750                 if (!CORBA::is_nil(myHypo))
1751                 {
1752                         string prefix = "Hypo_";
1753                         sprintf(objectId, "%ld", myHypo->GetId());
1754                         string lpID = prefix + string(objectId);
1755                         return CORBA::string_dup(lpID.c_str());
1756                 }
1757                 else
1758                 {
1759                         SMESH::SMESH_Mesh_var myMesh =
1760                                 SMESH::SMESH_Mesh::_narrow(_orb->string_to_object(IORString));
1761                         if (!CORBA::is_nil(myMesh))
1762                         {
1763                                 string prefix = "Mesh_";
1764                                 sprintf(objectId, "%ld", myMesh->GetId());
1765                                 string lpID = prefix + string(objectId);
1766                                 return CORBA::string_dup(lpID.c_str());
1767                         }
1768                         else
1769                         {
1770                                 SMESH::SMESH_subMesh_var mySubMesh =
1771                                         SMESH::SMESH_subMesh::_narrow(_orb->
1772                                         string_to_object(IORString));
1773                                 if (!CORBA::is_nil(mySubMesh))
1774                                 {
1775                                         string prefix = "SubMesh_";
1776                                         sprintf(objectId, "%ld", mySubMesh->GetId());
1777                                         string lpID = prefix + string(objectId);
1778                                         return CORBA::string_dup(lpID.c_str());
1779                                 }
1780                                 else
1781                                         return (strdup("no object"));
1782                         }
1783                 }
1784         }
1785 }
1786
1787 //=============================================================================
1788 /*!
1789  *  
1790  */
1791 //=============================================================================
1792
1793 char *SMESH_Gen_i::LocalPersistentIDToIOR(SALOMEDS::SObject_ptr theSObject,
1794         const char *aLocalPersistentID,
1795         CORBA::Boolean isMultiFile, CORBA::Boolean isASCII)
1796 {
1797         MESSAGE("SMESH_Gen_i::LocalPersistentIDToIOR");
1798         SCRUTE(aLocalPersistentID);
1799         string clef = string(aLocalPersistentID);
1800         SCRUTE(_SMESHCorbaObj[clef].c_str());
1801         return CORBA::string_dup(_SMESHCorbaObj[clef].c_str());
1802 }
1803
1804 //=============================================================================
1805 /*!
1806  *  
1807  */
1808 //=============================================================================
1809
1810 SMESH_topo *SMESH_Gen_i::ExploreMainShape(GEOM::GEOM_Gen_ptr geomEngine,
1811         CORBA::Long studyId, GEOM::GEOM_Shape_ptr aShape)
1812 {
1813         MESSAGE("SMESH_Mesh_i::ExploreMainShape");
1814         // _narrow() duplicates the reference and check the type
1815         GEOM::GEOM_Gen_var geom = GEOM::GEOM_Gen::_narrow(geomEngine);
1816         GEOM::GEOM_Shape_var myShape = GEOM::GEOM_Shape::_narrow(aShape);
1817
1818         if (CORBA::is_nil(geom))
1819                 THROW_SALOME_CORBA_EXCEPTION("bad geom reference", SALOME::BAD_PARAM);
1820         if (CORBA::is_nil(myShape))
1821                 THROW_SALOME_CORBA_EXCEPTION("bad shape reference", SALOME::BAD_PARAM);
1822         MESSAGE("---");
1823         SCRUTE(myShape->Name());
1824         geom->GetCurrentStudy(studyId);
1825         SCRUTE(studyId);
1826         TopoDS_Shape mainShape = _ShapeReader->GetShape(geom, myShape);
1827         MESSAGE("---");
1828
1829         // create an SMESH_topo object for the mainShape
1830
1831         SMESH_topo *myTopo = new SMESH_topo();
1832         MESSAGE("---");
1833
1834         // explore local TopoDS_Shape, store reference of local TopoDS subShapes
1835
1836         for (TopExp_Explorer exp(mainShape, TopAbs_COMPOUND); exp.More();
1837                 exp.Next())
1838         {
1839                 const TopoDS_Compound & E = TopoDS::Compound(exp.Current());
1840                 int i = myTopo->_myShapes[TopAbs_COMPOUND].Add(E);
1841                 SCRUTE(i);
1842         }
1843         for (TopExp_Explorer exp(mainShape, TopAbs_COMPSOLID); exp.More();
1844                 exp.Next())
1845         {
1846                 const TopoDS_CompSolid & E = TopoDS::CompSolid(exp.Current());
1847                 int i = myTopo->_myShapes[TopAbs_COMPSOLID].Add(E);
1848                 SCRUTE(i);
1849         }
1850         for (TopExp_Explorer exp(mainShape, TopAbs_SOLID); exp.More(); exp.Next())
1851         {
1852                 const TopoDS_Solid & E = TopoDS::Solid(exp.Current());
1853                 int i = myTopo->_myShapes[TopAbs_SOLID].Add(E);
1854                 SCRUTE(i);
1855         }
1856         for (TopExp_Explorer exp(mainShape, TopAbs_SHELL); exp.More(); exp.Next())
1857         {
1858                 const TopoDS_Shell & E = TopoDS::Shell(exp.Current());
1859                 int i = myTopo->_myShapes[TopAbs_SHELL].Add(E);
1860                 SCRUTE(i);
1861         }
1862         for (TopExp_Explorer exp(mainShape, TopAbs_FACE); exp.More(); exp.Next())
1863         {
1864                 const TopoDS_Face & E = TopoDS::Face(exp.Current());
1865                 int i = myTopo->_myShapes[TopAbs_FACE].Add(E);
1866                 SCRUTE(i);
1867         }
1868         for (TopExp_Explorer exp(mainShape, TopAbs_WIRE); exp.More(); exp.Next())
1869         {
1870                 const TopoDS_Wire & E = TopoDS::Wire(exp.Current());
1871                 int i = myTopo->_myShapes[TopAbs_WIRE].Add(E);
1872                 SCRUTE(i);
1873         }
1874         for (TopExp_Explorer exp(mainShape, TopAbs_EDGE); exp.More(); exp.Next())
1875         {
1876                 const TopoDS_Edge & E = TopoDS::Edge(exp.Current());
1877                 int i = myTopo->_myShapes[TopAbs_EDGE].Add(E);
1878                 SCRUTE(i);
1879         }
1880         for (TopExp_Explorer exp(mainShape, TopAbs_VERTEX); exp.More(); exp.Next())
1881         {
1882                 const TopoDS_Vertex & E = TopoDS::Vertex(exp.Current());
1883                 int i = myTopo->_myShapes[TopAbs_VERTEX].Add(E);
1884                 SCRUTE(i);
1885         }
1886
1887         // explore subShapes of distant CORBA object,
1888         // associate distant CORBA subShape references
1889         // with local reference to local TopoDS subShape
1890
1891         string filenode = "toposhape.txt";
1892         ofstream fic(filenode.c_str());
1893
1894         for (int shapeType = TopAbs_COMPOUND; shapeType < TopAbs_SHAPE; shapeType++)
1895         {
1896                 fic << "shape type : " << SMESH_shapeTypeNames[shapeType];
1897
1898                 GEOM::GEOM_Gen::ListOfGeomShapes_var subShapes
1899                         = geom->SubShapeAll(myShape, shapeType);
1900                 int nbSubShapes = subShapes->length();
1901                 int nbLocal = myTopo->_myShapes[shapeType].Extent();
1902                 fic << " - number of elements: " << nbSubShapes << endl;
1903                 ASSERT(nbSubShapes == nbLocal);
1904
1905                 for (int i = 0; i < nbSubShapes; i++)
1906                 {
1907                         GEOM::GEOM_Shape_var aSubShape = subShapes[i];
1908                         string idShape = SMESH_topo::GetShapeLocalId(aSubShape);
1909                         fic << "  " << idShape;
1910                         SCRUTE(idShape);
1911                         TopoDS_Shape aLocShape = _ShapeReader->GetShape(geom, aSubShape);
1912                         for (int j = 1; j <= nbLocal; j++)
1913                                 if (aLocShape.IsSame(myTopo->_myShapes[shapeType].FindKey(j)))
1914                                 {
1915                                         MESSAGE("  --- trouve = " << j);
1916                                         myTopo->_mapIndShapes[shapeType][idShape] = j;
1917                                         fic << "  --- trouve = " << j;
1918                                         break;
1919                                 }
1920                         fic << endl;
1921                 }
1922         }
1923         fic.close();
1924
1925         return myTopo;
1926 }
1927
1928 /**
1929  * Import a mesh from a file
1930  * @param fileName file name to be imported
1931  * @param fileType Currently it could be either "DAT", "UNV" or "MED".
1932  */
1933 SMESH::SMESH_Mesh_ptr SMESH_Gen_i::Import(CORBA::Long studyId,
1934         const char *fileName, const char *fileType)
1935 {
1936         MESSAGE("SMESH_Gen_I::Import");
1937         SMESH_Mesh_i *meshServant;
1938         try
1939         {
1940                 if (_mapStudyContext_i.find(studyId) == _mapStudyContext_i.end())
1941                 {
1942                         _mapStudyContext_i[studyId] = new StudyContext_iStruct;
1943                 }
1944                 StudyContext_iStruct *myStudyContext = _mapStudyContext_i[studyId];
1945
1946                 // create a new mesh object servant, store it in a map in study context
1947                 meshServant = new SMESH_Mesh_i(this, NULL, studyId, _localId);
1948                 myStudyContext->mapMesh_i[_localId] = meshServant;
1949                 _localId++;
1950
1951                 // create a new mesh object
1952                 meshServant->SetImpl(_impl.Import(studyId, fileName, fileType));
1953         }
1954         catch(SALOME_Exception & S_ex)
1955         {
1956                 THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
1957         }
1958
1959         // activate the CORBA servant of Mesh
1960
1961         SMESH::SMESH_Mesh_var mesh
1962                 = SMESH::SMESH_Mesh::_narrow(meshServant->_this());
1963
1964         meshServant->SetIor(mesh);
1965         return SMESH::SMESH_Mesh::_duplicate(mesh);
1966 }
1967
1968 //=============================================================================
1969 /*! 
1970  * C factory, accessible with dlsym, after dlopen  
1971  */
1972 //=============================================================================
1973
1974 extern "C"
1975 {
1976         PortableServer::ObjectId * SMESHEngine_factory(CORBA::ORB_ptr orb,
1977                 PortableServer::POA_ptr poa,
1978                 PortableServer::ObjectId * contId,
1979                 const char *instanceName, const char *interfaceName)
1980         {
1981                 MESSAGE("PortableServer::ObjectId * SMESHEngine_factory()");
1982                 SCRUTE(interfaceName);
1983                 SMESH_Gen_i *mySMESH_Gen
1984                         = new SMESH_Gen_i(orb, poa, contId, instanceName, interfaceName);
1985                 return mySMESH_Gen->getId();
1986         }
1987 }