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