]> SALOME platform Git repositories - modules/smesh.git/blob - src/SMESH_I/SMESH_Gen_i.cxx
Salome HOME
Clean the Load method.
[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         for (; itBig->More(); itBig->Next())
453         {
454                 SALOMEDS::SObject_var gotBranch = itBig->Value();
455
456 //************branch 1 : hypothesis
457                 if (gotBranch->Tag() == Tag_HypothesisRoot)
458                 {                                               //hypothesis = tag 1
459
460                         double length, maxElementsArea, maxElementsVolume;
461                         int numberOfSegments;
462
463                         destFile = fopen(hypofile.ToCString(), "w");
464                         it = Study->NewChildIterator(gotBranch);
465                         for (; it->More(); it->Next())
466                         {
467                                 mySObject = it->Value();
468                                 SALOMEDS::GenericAttribute_var anAttr;
469                                 SALOMEDS::AttributeIOR_var anIOR;
470                                 if (mySObject->FindAttribute(anAttr, "AttributeIOR"))
471                                 {
472                                         anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
473
474                                         SMESH::SMESH_Hypothesis_var myHyp =
475                                                 SMESH::SMESH_Hypothesis::_narrow(_orb->
476                                                 string_to_object(anIOR->Value()));
477                                         fprintf(destFile, "%s\n", myHyp->GetName());
478
479                                         if (strcmp(myHyp->GetName(), "LocalLength") == 0)
480                                         {
481                                                 SMESH::SMESH_LocalLength_var LL =
482                                                         SMESH::SMESH_LocalLength::_narrow(myHyp);
483                                                 length = LL->GetLength();
484                                                 fprintf(destFile, "%f\n", length);
485                                         }
486                                         else if (strcmp(myHyp->GetName(), "NumberOfSegments") == 0)
487                                         {
488                                                 SMESH::SMESH_NumberOfSegments_var NOS =
489                                                         SMESH::SMESH_NumberOfSegments::_narrow(myHyp);
490                                                 numberOfSegments = NOS->GetNumberOfSegments();
491                                                 fprintf(destFile, "%d\n", numberOfSegments);
492                                         }
493                                         else if (strcmp(myHyp->GetName(), "MaxElementArea") == 0)
494                                         {
495                                                 SMESH::SMESH_MaxElementArea_var MEA =
496                                                         SMESH::SMESH_MaxElementArea::_narrow(myHyp);
497                                                 maxElementsArea = MEA->GetMaxElementArea();
498                                                 fprintf(destFile, "%f\n", maxElementsArea);
499                                         }
500                                         else if (strcmp(myHyp->GetName(), "MaxElementVolume") == 0)
501                                         {
502                                                 SMESH::SMESH_MaxElementVolume_var MEV =
503                                                         SMESH::SMESH_MaxElementVolume::_narrow(myHyp);
504                                                 maxElementsVolume = MEV->GetMaxElementVolume();
505                                                 fprintf(destFile, "%f\n", maxElementsVolume);
506                                         }
507                                 }
508                         }
509                         fclose(destFile);
510
511 //writes the file name in the hdf file
512                         longueur = hypofile.Length() + 1;
513                         name_group = "Hypothesis";
514                         //SCRUTE(name_group);
515
516                         size[0] = longueur;
517                         hdf_group[1] = new HDFgroup(name_group, hdf_file);
518                         hdf_group[1]->CreateOnDisk();
519
520                         hdf_dataset[cmpt_ds] =
521                                 new HDFdataset(name_group, hdf_group[1], HDF_STRING, size, 1);
522                         hdf_dataset[cmpt_ds]->CreateOnDisk();
523                         hdf_dataset[cmpt_ds]->WriteOnDisk(hypofile.ToCString());
524                         hdf_dataset[cmpt_ds]->CloseOnDisk();
525                         cmpt_ds++;
526
527                         hdf_group[1]->CloseOnDisk();
528                         MESSAGE("End of Hypothesis Save");
529
530                 }
531 //************branch 2 : algorithms
532                 else if (gotBranch->Tag() == Tag_AlgorithmsRoot)
533                 {                                               //algos = tag 2
534
535                         destFile = fopen(algofile.ToCString(), "w");
536                         it = Study->NewChildIterator(gotBranch);
537                         for (; it->More(); it->Next())
538                         {
539                                 mySObject = it->Value();
540                                 SALOMEDS::GenericAttribute_var anAttr;
541                                 SALOMEDS::AttributeIOR_var anIOR;
542                                 if (mySObject->FindAttribute(anAttr, "AttributeIOR"))
543                                 {
544                                         anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
545                                         SMESH::SMESH_Algo_var myAlgo =
546                                                 SMESH::SMESH_Algo::_narrow(_orb->
547                                                 string_to_object(anIOR->Value()));
548                                         fprintf(destFile, "%s\n", myAlgo->GetName());
549                                 }
550                         }
551
552                         fclose(destFile);
553
554 //writes the file name in the hdf file
555                         longueur = algofile.Length() + 1;
556                         name_group = "Algorithms";
557                         //SCRUTE(name_group);
558
559                         size[0] = longueur;
560                         hdf_group[2] = new HDFgroup(name_group, hdf_file);
561                         hdf_group[2]->CreateOnDisk();
562
563                         hdf_dataset[cmpt_ds] =
564                                 new HDFdataset(name_group, hdf_group[2], HDF_STRING, size, 1);
565                         hdf_dataset[cmpt_ds]->CreateOnDisk();
566                         hdf_dataset[cmpt_ds]->WriteOnDisk(algofile.ToCString());
567                         hdf_dataset[cmpt_ds]->CloseOnDisk();
568                         cmpt_ds++;
569
570                         hdf_group[2]->CloseOnDisk();
571                         MESSAGE("End of Algos Save");
572
573                 }
574 //************branch 3 : meshes
575                 else if (gotBranch->Tag() >= 3)
576                 {                                               //meshes = tag > 3
577
578                         SALOMEDS::GenericAttribute_var anAttr;
579                         SALOMEDS::AttributeIOR_var anIOR;
580                         if (gotBranch->FindAttribute(anAttr, "AttributeIOR"))
581                         {
582                                 anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
583
584                                 SMESH::SMESH_Mesh_var myMesh =
585                                         SMESH::SMESH_Mesh::_narrow(_orb->string_to_object(anIOR->
586                                                 Value()));
587                                 studyId = myMesh->GetStudyId();
588                                 SCRUTE(studyId);
589
590                                 StudyContext_iStruct *myStudyContext =
591                                         _mapStudyContext_i[studyId];
592                                 int meshId = myMesh->GetId();
593                                 SMESH_Mesh_i *meshServant = myStudyContext->mapMesh_i[meshId];
594                                 ::SMESH_Mesh & myLocMesh = meshServant->GetImpl();
595                                 SMESHDS_Mesh *mySMESHDSMesh = myLocMesh.GetMeshDS();
596
597                                 SCRUTE(mySMESHDSMesh->NbNodes());
598                                 if (mySMESHDSMesh->NbNodes() > 0)
599                                 {
600                                         //checks if the mesh is not empty   
601                                         Mesh_Writer *myWriter = SMESHDriver::GetMeshWriter("MED");
602                                         myWriter->SetFile(meshfile.ToCString());
603                                         myWriter->SetMesh(mySMESHDSMesh);
604                                         myWriter->SetMeshId(gotBranch->Tag());
605                                         myWriter->Add();
606                                 }
607                                 else
608                                         meshfile = "No data";
609
610                                 //********** opening of the HDF group
611                                 sprintf(name_meshgroup, "Mesh %d", gotBranch->Tag());
612                                 SCRUTE(name_meshgroup);
613                                 hdf_group[gotBranch->Tag()] =
614                                         new HDFgroup(name_meshgroup, hdf_file);
615                                 hdf_group[gotBranch->Tag()]->CreateOnDisk();
616                                 //********** 
617
618                                 //********** file where the data are stored
619                                 longueur = strlen(meshfile.ToCString()) + 1;
620                                 size[0] = longueur;
621                                 strcpy(name_dataset, "Mesh data");
622                                 hdf_dataset[cmpt_ds] =
623                                         new HDFdataset(name_dataset, hdf_group[gotBranch->Tag()],
624                                         HDF_STRING, size, 1);
625                                 hdf_dataset[cmpt_ds]->CreateOnDisk();
626                                 hdf_dataset[cmpt_ds]->WriteOnDisk(meshfile.ToCString());
627                                 hdf_dataset[cmpt_ds]->CloseOnDisk();
628                                 cmpt_ds++;
629                                 //********** 
630
631                                 //********** ref on shape
632                                 Standard_CString myRefOnObject = "";
633                                 SALOMEDS::SObject_var myRef, myShape;
634                                 _found = gotBranch->FindSubObject(Tag_RefOnShape, myRef);
635                                 if (_found)
636                                 {
637                                         ok = myRef->ReferencedObject(myShape);
638                                         myRefOnObject = myShape->GetID();
639                                         SCRUTE(myRefOnObject);
640
641                                         longueur = strlen(myRefOnObject) + 1;
642                                         if (longueur > 1)
643                                         {
644                                                 size[0] = longueur;
645                                                 strcpy(name_dataset, "Ref on shape");
646                                                 hdf_dataset[cmpt_ds] =
647                                                         new HDFdataset(name_dataset,
648                                                         hdf_group[gotBranch->Tag()], HDF_STRING, size, 1);
649                                                 hdf_dataset[cmpt_ds]->CreateOnDisk();
650                                                 hdf_dataset[cmpt_ds]->WriteOnDisk(myRefOnObject);
651                                                 hdf_dataset[cmpt_ds]->CloseOnDisk();
652                                                 cmpt_ds++;
653
654                                         }
655                                 }
656                                 //********** 
657
658                                 //********** ref on applied hypothesis
659                                 _found =
660                                         gotBranch->FindSubObject(Tag_RefOnAppliedHypothesis,
661                                         myBranch);
662                                 if (_found)
663                                 {
664
665                                         strcpy(name_meshgroup, "Applied Hypothesis");
666                                         hdf_subgroup[Tag_RefOnAppliedHypothesis] =
667                                                 new HDFgroup(name_meshgroup,
668                                                 hdf_group[gotBranch->Tag()]);
669                                         hdf_subgroup[Tag_RefOnAppliedHypothesis]->CreateOnDisk();
670
671                                         it = Study->NewChildIterator(myBranch);
672                                         cmpt_it = 0;
673                                         for (; it->More(); it->Next())
674                                         {
675                                                 mySObject = it->Value();
676                                                 ok = mySObject->ReferencedObject(myRef);
677                                                 myRefOnObject = myRef->GetID();
678
679                                                 longueur = strlen(myRefOnObject) + 1;
680                                                 if (longueur > 1)
681                                                 {
682                                                         size[0] = longueur;
683                                                         sprintf(name_dataset, "Hyp %d", cmpt_it);
684                                                         hdf_dataset[cmpt_ds] =
685                                                                 new HDFdataset(name_dataset,
686                                                                 hdf_subgroup[Tag_RefOnAppliedHypothesis],
687                                                                 HDF_STRING, size, 1);
688                                                         hdf_dataset[cmpt_ds]->CreateOnDisk();
689                                                         hdf_dataset[cmpt_ds]->WriteOnDisk(myRefOnObject);
690                                                         hdf_dataset[cmpt_ds]->CloseOnDisk();
691                                                 }
692                                                 cmpt_ds++;
693                                                 cmpt_it++;
694                                         }
695                                         hdf_subgroup[Tag_RefOnAppliedHypothesis]->CloseOnDisk();
696                                 }
697                                 //********** 
698
699                                 //********** ref on applied algorithms
700                                 _found =
701                                         gotBranch->FindSubObject(Tag_RefOnAppliedAlgorithms,
702                                         myBranch);
703                                 if (_found)
704                                 {
705
706                                         strcpy(name_meshgroup, "Applied Algorithms");
707                                         hdf_subgroup[Tag_RefOnAppliedAlgorithms] =
708                                                 new HDFgroup(name_meshgroup,
709                                                 hdf_group[gotBranch->Tag()]);
710                                         hdf_subgroup[Tag_RefOnAppliedAlgorithms]->CreateOnDisk();
711
712                                         it = Study->NewChildIterator(myBranch);
713                                         cmpt_it = 0;
714                                         for (; it->More(); it->Next())
715                                         {
716                                                 mySObject = it->Value();
717                                                 ok = mySObject->ReferencedObject(myRef);
718                                                 myRefOnObject = myRef->GetID();
719
720                                                 longueur = strlen(myRefOnObject) + 1;
721                                                 if (longueur > 1)
722                                                 {
723                                                         size[0] = longueur;
724                                                         sprintf(name_dataset, "Algo %d", cmpt_it);
725                                                         hdf_dataset[cmpt_ds] =
726                                                                 new HDFdataset(name_dataset,
727                                                                 hdf_subgroup[Tag_RefOnAppliedAlgorithms],
728                                                                 HDF_STRING, size, 1);
729                                                         hdf_dataset[cmpt_ds]->CreateOnDisk();
730                                                         hdf_dataset[cmpt_ds]->WriteOnDisk(myRefOnObject);
731                                                         hdf_dataset[cmpt_ds]->CloseOnDisk();
732                                                 }
733                                                 cmpt_ds++;
734                                                 cmpt_it++;
735                                         }
736                                         hdf_subgroup[Tag_RefOnAppliedAlgorithms]->CloseOnDisk();
737                                 }
738                                 MESSAGE("end of algo applied");
739                                 //********** 
740
741                                 //********** submeshes on subshapes
742                                 int myLevel1Tag;
743                                 for (int i = Tag_SubMeshOnVertex; i <= Tag_SubMeshOnCompound;
744                                         i++)
745                                 {
746                                         _found = gotBranch->FindSubObject(i, myBranch);
747                                         if (_found)
748                                         {
749                                                 if (i == Tag_SubMeshOnVertex)
750                                                         strcpy(name_meshgroup, "SubMeshes On Vertex");
751                                                 else if (i == Tag_SubMeshOnEdge)
752                                                         strcpy(name_meshgroup, "SubMeshes On Edge");
753                                                 else if (i == Tag_SubMeshOnFace)
754                                                         strcpy(name_meshgroup, "SubMeshes On Face");
755                                                 else if (i == Tag_SubMeshOnSolid)
756                                                         strcpy(name_meshgroup, "SubMeshes On Solid");
757                                                 else if (i == Tag_SubMeshOnCompound)
758                                                         strcpy(name_meshgroup, "SubMeshes On Compound");
759
760                                                 cmpt_sm++;
761                                                 myLevel1Tag = 10 + cmpt_sm;
762                                                 hdf_subgroup[myLevel1Tag] =
763                                                         new HDFgroup(name_meshgroup,
764                                                         hdf_group[gotBranch->Tag()]);
765                                                 hdf_subgroup[myLevel1Tag]->CreateOnDisk();
766
767                                                 itSM = Study->NewChildIterator(myBranch);
768                                                 for (; itSM->More(); itSM->Next())
769                                                 {               //Loop on all submeshes
770                                                         mySObject = itSM->Value();
771                                                         cmpt_sm++;
772                                                         myTag = 10 + cmpt_sm;
773                                                         mySObject->FindAttribute(anAttr, "AttributeIOR");
774                                                         anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
775                                                         SMESH::SMESH_subMesh_var mySubMesh =
776                                                                 SMESH::SMESH_subMesh::_narrow(_orb->
777                                                                 string_to_object(anIOR->Value()));
778
779                                                         //sprintf(name_meshgroup,"SubMesh %d",myTag);
780                                                         sprintf(name_meshgroup, "SubMesh %ld",
781                                                                 mySubMesh->GetId());
782                                                         SCRUTE(name_meshgroup);
783
784                                                         hdf_subgroup[myTag] =
785                                                                 new HDFgroup(name_meshgroup,
786                                                                 hdf_subgroup[myLevel1Tag]);
787                                                         hdf_subgroup[myTag]->CreateOnDisk();
788
789                                                         //********** ref on shape
790                                                         Standard_CString myRefOnObject = "";
791                                                         SALOMEDS::SObject_var myRef, myShape;
792                                                         bool _found2;
793                                                         _found2 =
794                                                                 mySObject->FindSubObject(Tag_RefOnShape, myRef);
795                                                         if (_found2)
796                                                         {
797                                                                 ok = myRef->ReferencedObject(myShape);
798                                                                 myRefOnObject = myShape->GetID();
799                                                                 SCRUTE(myRefOnObject);
800
801                                                                 longueur = strlen(myRefOnObject) + 1;
802                                                                 if (longueur > 1)
803                                                                 {
804                                                                         size[0] = longueur;
805                                                                         strcpy(name_dataset, "Ref on shape");
806                                                                         hdf_dataset[cmpt_ds] =
807                                                                                 new HDFdataset(name_dataset,
808                                                                                 hdf_subgroup[myTag], HDF_STRING, size,
809                                                                                 1);
810                                                                         hdf_dataset[cmpt_ds]->CreateOnDisk();
811                                                                         hdf_dataset[cmpt_ds]->
812                                                                                 WriteOnDisk(myRefOnObject);
813                                                                         hdf_dataset[cmpt_ds]->CloseOnDisk();
814                                                                         cmpt_ds++;
815                                                                 }
816                                                         }
817                                                         //********** 
818
819                                                         //********** ref on applied hypothesis
820                                                         _found2 =
821                                                                 mySObject->
822                                                                 FindSubObject(Tag_RefOnAppliedHypothesis,
823                                                                 myBranch);
824                                                         if (_found2)
825                                                         {
826
827                                                                 strcpy(name_meshgroup, "Applied Hypothesis");
828                                                                 cmpt_sm++;
829                                                                 hdf_subgroup[10 + cmpt_sm] =
830                                                                         new HDFgroup(name_meshgroup,
831                                                                         hdf_subgroup[myTag]);
832                                                                 hdf_subgroup[10 + cmpt_sm]->CreateOnDisk();
833
834                                                                 it = Study->NewChildIterator(myBranch);
835                                                                 cmpt_it = 0;
836                                                                 for (; it->More(); it->Next())
837                                                                 {
838                                                                         mySObjectChild = it->Value();
839                                                                         ok = mySObjectChild->
840                                                                                 ReferencedObject(myRef);
841                                                                         myRefOnObject = myRef->GetID();
842
843                                                                         longueur = strlen(myRefOnObject) + 1;
844                                                                         if (longueur > 1)
845                                                                         {
846                                                                                 size[0] = longueur;
847                                                                                 sprintf(name_dataset, "Hyp %d",
848                                                                                         cmpt_it);
849                                                                                 SCRUTE(cmpt_it);
850                                                                                 hdf_dataset[cmpt_ds] =
851                                                                                         new HDFdataset(name_dataset,
852                                                                                         hdf_subgroup[10 + cmpt_sm],
853                                                                                         HDF_STRING, size, 1);
854                                                                                 hdf_dataset[cmpt_ds]->CreateOnDisk();
855                                                                                 hdf_dataset[cmpt_ds]->
856                                                                                         WriteOnDisk(myRefOnObject);
857                                                                                 hdf_dataset[cmpt_ds]->CloseOnDisk();
858                                                                         }
859                                                                         cmpt_ds++;
860                                                                         cmpt_it++;
861                                                                 }
862                                                                 hdf_subgroup[10 + cmpt_sm]->CloseOnDisk();
863                                                         }
864                                                         //********** 
865
866                                                         //********** ref on applied algorithms
867                                                         _found2 =
868                                                                 mySObject->
869                                                                 FindSubObject(Tag_RefOnAppliedAlgorithms,
870                                                                 myBranch);
871                                                         SCRUTE(_found2);
872                                                         if (_found2)
873                                                         {
874
875                                                                 strcpy(name_meshgroup, "Applied Algorithms");
876                                                                 cmpt_sm++;
877                                                                 hdf_subgroup[10 + cmpt_sm] =
878                                                                         new HDFgroup(name_meshgroup,
879                                                                         hdf_subgroup[myTag]);
880                                                                 hdf_subgroup[10 + cmpt_sm]->CreateOnDisk();
881
882                                                                 it = Study->NewChildIterator(myBranch);
883                                                                 cmpt_it = 0;
884                                                                 for (; it->More(); it->Next())
885                                                                 {
886                                                                         mySObjectChild = it->Value();
887                                                                         ok = mySObjectChild->
888                                                                                 ReferencedObject(myRef);
889                                                                         myRefOnObject = myRef->GetID();
890
891                                                                         longueur = strlen(myRefOnObject) + 1;
892                                                                         if (longueur > 1)
893                                                                         {
894                                                                                 size[0] = longueur;
895                                                                                 sprintf(name_dataset, "Algo %d",
896                                                                                         cmpt_it);
897                                                                                 hdf_dataset[cmpt_ds] =
898                                                                                         new HDFdataset(name_dataset,
899                                                                                         hdf_subgroup[10 + cmpt_sm],
900                                                                                         HDF_STRING, size, 1);
901                                                                                 hdf_dataset[cmpt_ds]->CreateOnDisk();
902                                                                                 hdf_dataset[cmpt_ds]->
903                                                                                         WriteOnDisk(myRefOnObject);
904                                                                                 hdf_dataset[cmpt_ds]->CloseOnDisk();
905                                                                         }
906                                                                         cmpt_ds++;
907                                                                         cmpt_it++;
908                                                                 }
909                                                                 hdf_subgroup[10 + cmpt_sm]->CloseOnDisk();
910                                                         }
911                                                         //MESSAGE("end of algo applied");
912                                                         //********** 
913
914                                                         hdf_subgroup[myTag]->CloseOnDisk();
915                                                 }
916
917                                                 hdf_subgroup[myLevel1Tag]->CloseOnDisk();
918                                         }
919
920                                 }
921                                 //********** 
922
923                                 //********** closing of the HDF group
924                                 hdf_group[gotBranch->Tag()]->CloseOnDisk();
925                                 MESSAGE("End of Mesh Save");
926                                 //********** 
927                         }
928                 }
929                 MESSAGE("End of Meshes Save");
930         }
931
932         hdf_file->CloseOnDisk();
933         delete hdf_file;
934         hdf_file = 0;
935
936         // Convert temporary files to stream
937         aStreamFile =
938                 SALOMEDS_Tool::PutFilesToStream(tmpDir.ToCString(), aFileSeq.in(),
939                 isMultiFile);
940
941         // Remove temporary files and directory
942         if (!isMultiFile)
943                 SALOMEDS_Tool::RemoveTemporaryFiles(tmpDir.ToCString(), aFileSeq.in(),
944                         true);
945
946         MESSAGE("End SMESH_Gen_i::Save");
947
948         return aStreamFile._retn();
949 }
950
951 SALOMEDS::TMPFile *
952         SMESH_Gen_i::SaveASCII(SALOMEDS::SComponent_ptr theComponent,
953         const char *theURL, bool isMultiFile)
954 {
955         SALOMEDS::TMPFile_var aStreamFile = Save(theComponent, theURL, isMultiFile);
956         return aStreamFile._retn();
957 }
958
959 void SMESH_Gen_i::loadHypothesis(char * name, HDFfile * hdf_file,
960         char * hypofile, int studyId)
961 {
962         char name_of_group[HDF_NAME_MAX_LEN + 1];
963         char objectId[10];
964         double length, maxElementsArea, maxElementsVolume;
965         int numberOfSegments;
966
967         HDFgroup * hdfGroup = new HDFgroup(name, hdf_file);
968         hdfGroup->OpenOnDisk();
969
970         hdfGroup->InternalObjectIndentify(0, name_of_group);
971         HDFdataset * dataset = new HDFdataset(name_of_group, hdfGroup);
972         dataset->OpenOnDisk();
973         char *name_of_file = new char[dataset->GetSize()];
974         dataset->ReadFromDisk(name_of_file);
975         SCRUTE(name_of_file);
976         dataset->CloseOnDisk();
977         hdfGroup->CloseOnDisk();        
978         delete[]name_of_file;
979
980         char * aLine = new char[100];
981         FILE * loadedFile = fopen(hypofile, "r");
982         while (!feof(loadedFile))
983         {
984                 fscanf(loadedFile, "%s", aLine);
985                 //SCRUTE(aLine);
986                 if (strcmp(aLine, "LocalLength") == 0)
987                 {
988                         SMESH::SMESH_Hypothesis_var myHyp =
989                                 this->CreateHypothesis(aLine, studyId);
990                         SMESH::SMESH_LocalLength_var LL =
991                                 SMESH::SMESH_LocalLength::_narrow(myHyp);
992                         fscanf(loadedFile, "%s", aLine);
993                         length = atof(aLine);
994                         LL->SetLength(length);
995                         string iorString = _orb->object_to_string(LL);
996                         sprintf(objectId, "%ld", LL->GetId());
997                         _SMESHCorbaObj[string("Hypo_") + string(objectId)] =
998                                 iorString;
999                 }
1000                 else if (strcmp(aLine, "NumberOfSegments") == 0)
1001                 {
1002                         SMESH::SMESH_Hypothesis_var myHyp =
1003                                 this->CreateHypothesis(aLine, studyId);
1004                         SMESH::SMESH_NumberOfSegments_var NOS =
1005                                 SMESH::SMESH_NumberOfSegments::_narrow(myHyp);
1006                         fscanf(loadedFile, "%s", aLine);
1007                         numberOfSegments = atoi(aLine);
1008                         NOS->SetNumberOfSegments(numberOfSegments);
1009                         string iorString = _orb->object_to_string(NOS);
1010                         sprintf(objectId, "%ld", NOS->GetId());
1011                         _SMESHCorbaObj[string("Hypo_") + string(objectId)] =
1012                                 iorString;
1013                 }
1014                 else if (strcmp(aLine, "MaxElementArea") == 0)
1015                 {
1016                         SMESH::SMESH_Hypothesis_var myHyp =
1017                                 this->CreateHypothesis(aLine, studyId);
1018                         SMESH::SMESH_MaxElementArea_var MEA =
1019                                 SMESH::SMESH_MaxElementArea::_narrow(myHyp);
1020                         fscanf(loadedFile, "%s", aLine);
1021                         maxElementsArea = atof(aLine);
1022                         MEA->SetMaxElementArea(maxElementsArea);
1023                         string iorString = _orb->object_to_string(MEA);
1024                         sprintf(objectId, "%ld", MEA->GetId());
1025                         _SMESHCorbaObj[string("Hypo_") + string(objectId)] =
1026                                 iorString;
1027                 }
1028                 else if (strcmp(aLine, "MaxElementVolume") == 0)
1029                 {
1030                         SMESH::SMESH_Hypothesis_var myHyp =
1031                                 this->CreateHypothesis(aLine, studyId);
1032                         SMESH::SMESH_MaxElementVolume_var MEV =
1033                                 SMESH::SMESH_MaxElementVolume::_narrow(myHyp);
1034                         fscanf(loadedFile, "%s", aLine);
1035                         maxElementsVolume = atof(aLine);
1036                         MEV->SetMaxElementVolume(maxElementsVolume);
1037                         string iorString = _orb->object_to_string(MEV);
1038                         sprintf(objectId, "%ld", MEV->GetId());
1039                         _SMESHCorbaObj[string("Hypo_") + string(objectId)] =
1040                                 iorString;
1041                 }
1042
1043         }
1044         fclose(loadedFile);
1045         delete[]aLine;
1046         MESSAGE("End of Hypos Load");
1047 }
1048
1049 void SMESH_Gen_i::loadAlgorithms(char * name, HDFfile * hdf_file,
1050         char* algofile, int studyId)
1051 {
1052         char name_of_group[HDF_NAME_MAX_LEN + 1];
1053         char objectId[10];
1054         HDFgroup * hdfGroup = new HDFgroup(name, hdf_file);
1055         hdfGroup->OpenOnDisk();
1056
1057         hdfGroup->InternalObjectIndentify(0, name_of_group);
1058         HDFdataset * dataset =
1059                 new HDFdataset(name_of_group, hdfGroup);
1060         dataset->OpenOnDisk();
1061
1062         char *name_of_file = new char[dataset->GetSize()];
1063         dataset->ReadFromDisk(name_of_file);
1064         dataset->CloseOnDisk();
1065         hdfGroup->CloseOnDisk();        
1066         delete[]name_of_file;
1067
1068         char * aLine = new char[100];
1069         FILE * loadedFile = fopen(algofile, "r");
1070         while (!feof(loadedFile))
1071         {
1072                 fscanf(loadedFile, "%s\n", aLine);
1073                 //SCRUTE(aLine);
1074                 if (strcmp(aLine, "") != 0)
1075                 {
1076                         SMESH::SMESH_Hypothesis_var myHyp =
1077                                 this->CreateHypothesis(aLine, studyId);
1078                         SMESH::SMESH_Algo_var myAlgo =
1079                                 SMESH::SMESH_Algo::_narrow(myHyp);
1080                         string iorString = _orb->object_to_string(myAlgo);
1081                         sprintf(objectId, "%ld", myAlgo->GetId());
1082                         _SMESHCorbaObj[string("Hypo_") + string(objectId)] =
1083                                 iorString;
1084                 }
1085         }
1086         fclose(loadedFile);
1087         delete[]aLine;
1088         aLine = 0;
1089         MESSAGE("End of Algos Load");
1090 }
1091
1092 /**
1093  * @param hdfGroupMeshId The group where to read the hypothesis
1094  * @param _found ???
1095  * @param Study The study where to create the hypothesis
1096  * @param myNewMesh The mesh on which the hypothesis is applied
1097  * @param aShape The shape with which this mesh is linked
1098  */
1099 void SMESH_Gen_i::loadAppliedHypothesis(HDFgroup * hdfGroupMeshId, 
1100         bool _found, SALOMEDS::Study_var Study, SMESH::SMESH_Mesh_var myNewMesh, 
1101         GEOM::GEOM_Shape_var aShape)
1102 {
1103         HDFgroup * hdf_subgroup = new HDFgroup("Applied Hypothesis", hdfGroupMeshId);
1104         hdf_subgroup->OpenOnDisk();
1105         int nb_datasets = hdf_subgroup->nInternalObjects();
1106         char name_dataset[10];
1107         
1108         for (int j = 0; j < nb_datasets; j++)
1109         {
1110                 sprintf(name_dataset, "Hyp %d", j);
1111                 HDFdataset * hdf_dataset = new HDFdataset(name_dataset, hdf_subgroup);
1112                 hdf_dataset->OpenOnDisk();
1113
1114                 char *refFromFile = new char[hdf_dataset->GetSize()];
1115                 hdf_dataset->ReadFromDisk(refFromFile);
1116                 hdf_dataset->CloseOnDisk();
1117                 delete hdf_dataset;
1118
1119                 if (_found)
1120                 {
1121                         SALOMEDS::SObject_var HypSO =
1122                                 Study->FindObjectID(refFromFile);
1123                         if (!CORBA::is_nil(HypSO))
1124                         {
1125                                 SALOMEDS::GenericAttribute_var anAttr;
1126                                 HypSO->FindAttribute(anAttr, "AttributeIOR");
1127                                 SALOMEDS::AttributeIOR_var anIOR =
1128                                         SALOMEDS::AttributeIOR::_narrow(anAttr);
1129                                 if (!CORBA::is_nil(anIOR))
1130                                 {
1131                                         char *HypIOR = anIOR->Value();
1132                                         SMESH::SMESH_Hypothesis_var anHyp =
1133                                                 SMESH::SMESH_Hypothesis::_narrow(_orb->
1134                                                 string_to_object(HypIOR));
1135                                         if (!CORBA::is_nil(anHyp))
1136                                         {
1137                                                 myNewMesh->AddHypothesis(aShape, anHyp);
1138                                                 MESSAGE("Hypothesis added ...");
1139                                         }
1140                                 }
1141                         }
1142                 }
1143         }
1144         hdf_subgroup->CloseOnDisk();
1145 }
1146
1147 /**
1148  * @param hdfGroupMeshId The group where to read the hypothesis
1149  * @param _found ???
1150  * @param Study The study where to create the hypothesis
1151  * @param myNewMesh The mesh on which the hypothesis is applied
1152  * @param aShape The shape with which this mesh is linked
1153  */
1154 void SMESH_Gen_i::loadAppliedAlgorithms(HDFgroup * hdfGroupMeshId, 
1155         bool _found, SALOMEDS::Study_var Study, SMESH::SMESH_Mesh_var myNewMesh, 
1156         GEOM::GEOM_Shape_var aShape)
1157 {
1158         HDFgroup * hdf_subgroup = new HDFgroup("Applied Algorithms", hdfGroupMeshId);
1159         hdf_subgroup->OpenOnDisk();
1160
1161         int nb_datasets = hdf_subgroup->nInternalObjects();
1162         SCRUTE(nb_datasets);
1163         char name_dataset[10];
1164         
1165         for (int j = 0; j < nb_datasets; j++)
1166         {
1167                 sprintf(name_dataset, "Algo %d", j);
1168                 HDFdataset * dataset =
1169                         new HDFdataset(name_dataset, hdf_subgroup);
1170                 dataset->OpenOnDisk();
1171
1172                 char *refFromFile = new char[dataset->GetSize()];
1173                 dataset->ReadFromDisk(refFromFile);
1174                 dataset->CloseOnDisk();
1175                 delete dataset;
1176
1177                 if (_found)
1178                 {
1179                         SALOMEDS::SObject_var AlgoSO =
1180                                 Study->FindObjectID(refFromFile);
1181                         if (!CORBA::is_nil(AlgoSO))
1182                         {
1183                                 SALOMEDS::GenericAttribute_var anAttr;
1184                                 AlgoSO->FindAttribute(anAttr, "AttributeIOR");
1185                                 SALOMEDS::AttributeIOR_var anIOR =
1186                                         SALOMEDS::AttributeIOR::_narrow(anAttr);
1187                                 if (!CORBA::is_nil(anIOR))
1188                                 {
1189                                         char *AlgoIOR = anIOR->Value();
1190                                         //SCRUTE(AlgoIOR);
1191                                         SMESH::SMESH_Hypothesis_var myHyp =
1192                                                 SMESH::SMESH_Hypothesis::_narrow(_orb->
1193                                                 string_to_object(AlgoIOR));
1194                                         SMESH::SMESH_Algo_var anAlgo =
1195                                                 SMESH::SMESH_Algo::_narrow(myHyp);
1196                                                                                         
1197                                         if (!CORBA::is_nil(anAlgo))
1198                                         {
1199                                                 myNewMesh->AddHypothesis(aShape, anAlgo);       //essayer avec _SMESHCorbaObj
1200                                                 MESSAGE("Algorithms added ...");
1201                                         }
1202                                 }
1203                         }
1204                 }
1205         }
1206         hdf_subgroup->CloseOnDisk();
1207 }
1208
1209 /**
1210  * @param hdfGroupMeshId The group where to read the hypothesis
1211  * @param msgname ???
1212  * @param Study The study where to create the hypothesis
1213  * @param myNewMesh The mesh on which the hypothesis is applied
1214  */
1215 void SMESH_Gen_i::loadSubMeshes(HDFgroup * hdfGroupMeshId, char * msgname,
1216         SALOMEDS::Study_var Study, SMESH::SMESH_Mesh_var myNewMesh)
1217 {
1218         MESSAGE("SMESH_Gen_i::loadSubMeshes");
1219         HDFgroup * hdf_subgroupmyLevel1Tag = new HDFgroup(msgname, hdfGroupMeshId);
1220         hdf_subgroupmyLevel1Tag->OpenOnDisk();
1221
1222         int nb_submeshes = hdf_subgroupmyLevel1Tag->nInternalObjects();
1223         char name_meshgroup[30];
1224         //SCRUTE(nb_submeshes);
1225
1226         for (int j = 0; j < nb_submeshes; j++)
1227         {
1228                 //cmpt_sm++;
1229                 //myTag = 10 + cmpt_sm;
1230                 hdf_subgroupmyLevel1Tag->InternalObjectIndentify(j, name_meshgroup);                            
1231
1232                 HDFgroup * hdf_subgroupmyTag = new HDFgroup(name_meshgroup, 
1233                         hdf_subgroupmyLevel1Tag);
1234                 hdf_subgroupmyTag->OpenOnDisk();
1235                 int subMeshId = atoi((string(name_meshgroup).substr(8, 18)).c_str());
1236
1237                 MESSAGE("Ref on shape");
1238                 //********** ref on shape               
1239                 HDFdataset * hdf_dataset =
1240                         new HDFdataset("Ref on shape", hdf_subgroupmyTag);
1241                 hdf_dataset->OpenOnDisk();
1242
1243                 char *refFromFile = new char[hdf_dataset->GetSize()];
1244                 hdf_dataset->ReadFromDisk(refFromFile);
1245                 hdf_dataset->CloseOnDisk();
1246                 delete hdf_dataset;
1247
1248                 bool _found3 = false;
1249                 SALOMEDS::SObject_var GSO = Study->FindObjectID(refFromFile);
1250                 SMESH::SMESH_subMesh_var aSubMesh;
1251                 GEOM::GEOM_Shape_var aSubShape;
1252
1253                 if (!CORBA::is_nil(GSO))
1254                 {
1255                         SALOMEDS::GenericAttribute_var anAttr;
1256                         GSO->FindAttribute(anAttr, "AttributeIOR");
1257                         SALOMEDS::AttributeIOR_var anIOR = 
1258                                 SALOMEDS::AttributeIOR::_narrow(anAttr);
1259                                 
1260                         char *SubShapeIOR = anIOR->Value();
1261                         aSubShape =
1262                                 GEOM::GEOM_Shape::_narrow(_orb-> string_to_object(SubShapeIOR));
1263
1264                         if (!CORBA::is_nil(aSubShape))
1265                         {
1266                                 aSubMesh = myNewMesh->GetElementsOnShape(aSubShape);
1267                                 string iorString = _orb->object_to_string(aSubMesh);
1268                                 char objectId[10];
1269                                 sprintf(objectId, "%d", subMeshId);
1270                                 _SMESHCorbaObj[string("SubMesh_") + string(objectId)] =
1271                                          iorString;
1272                                 _found3 = true;
1273                         }
1274                 }
1275
1276                 int nb_subgroup = hdf_subgroupmyTag->nInternalObjects();
1277                 SCRUTE(nb_subgroup);
1278                 char sgname[HDF_NAME_MAX_LEN + 1];
1279                 for (int k = 0; k < nb_subgroup; k++)
1280                 {
1281                         hdf_subgroupmyTag->InternalObjectIndentify(k, sgname);
1282                         if (strcmp(sgname, "Ref on shape") == 0)
1283                         {
1284                                 //nothing
1285                         }
1286                         else if (strcmp(sgname, "Applied Hypothesis") == 0)
1287                         {
1288                                 //********** ref on applied hypothesis
1289                                 MESSAGE("Applied Hypothesis");
1290                                 strcpy(name_meshgroup, "Applied Hypothesis");
1291                                 //cmpt_sm++;
1292                                 HDFgroup * hdf_subgroup10cmpt_sm = new HDFgroup(name_meshgroup,
1293                                         hdf_subgroupmyTag);
1294                                         
1295                                 hdf_subgroup10cmpt_sm->OpenOnDisk();
1296                                 int nb_datasets = hdf_subgroup10cmpt_sm->nInternalObjects();
1297                                 // SCRUTE(nb_datasets);
1298
1299                                 char name_dataset[30];
1300                                 for (int l = 0; l < nb_datasets; l++)
1301                                 {
1302                                         sprintf(name_dataset, "Hyp %d", l);
1303                                         HDFdataset * hdf_datasetcmpt_ds =
1304                                                 new HDFdataset(name_dataset, hdf_subgroup10cmpt_sm);
1305                                         hdf_datasetcmpt_ds->OpenOnDisk();
1306                                         int size = hdf_datasetcmpt_ds->GetSize();
1307
1308                                         char *refFromFile = new char[size];
1309                                         hdf_datasetcmpt_ds->ReadFromDisk(refFromFile);
1310                                         hdf_datasetcmpt_ds->CloseOnDisk();
1311                                         //cmpt_ds++;
1312
1313                                         if (_found3)
1314                                         {
1315                                                 SALOMEDS::SObject_var HypSO =
1316                                                         Study->FindObjectID(refFromFile);
1317                                                 if (!CORBA::is_nil(HypSO))
1318                                                 {
1319                                                         SALOMEDS::GenericAttribute_var anAttr;
1320                                                         SALOMEDS::AttributeIOR_var anIOR;
1321                                                         HypSO->FindAttribute(anAttr, "AttributeIOR");
1322                                                         anIOR = SALOMEDS::AttributeIOR:: _narrow(anAttr);
1323                                                         if (!CORBA::is_nil(anIOR))
1324                                                         {
1325                                                                 char *HypIOR = anIOR->Value();
1326                                                                 SMESH::SMESH_Hypothesis_var anHyp =
1327                                                                         SMESH::SMESH_Hypothesis::_narrow(_orb->
1328                                                                         string_to_object(HypIOR));
1329                                                                 if (!CORBA::is_nil(anHyp))
1330                                                                 {
1331                                                                         SMESH::SMESH_Mesh_var aMesh = 
1332                                                                                 aSubMesh->GetFather();
1333                                                                         aMesh->AddHypothesis(aSubShape, anHyp); //essayer avec _SMESHCorbaObj
1334                                                                         MESSAGE("Hypothesis added ...");
1335                                                                 }
1336                                                         }
1337                                                 }
1338                                         }
1339                                 }
1340                         }
1341                         else if (strcmp(sgname, "Applied Algorithms") == 0)
1342                         {
1343                                 //********** ref on applied algorithms
1344                                 MESSAGE("Applied Algorithms");
1345                                 strcpy(name_meshgroup, "Applied Algorithms");
1346                                 //cmpt_sm++;
1347                                 HDFgroup * hdf_subgroup10cmpt_sm = new HDFgroup(name_meshgroup,
1348                                         hdf_subgroupmyTag);
1349                                 hdf_subgroup10cmpt_sm->OpenOnDisk();
1350                                 int nb_datasets = hdf_subgroup10cmpt_sm->nInternalObjects();
1351                                 SCRUTE(nb_datasets);
1352
1353                                 char name_dataset[30];
1354                                 for (int l = 0; l < nb_datasets; l++)
1355                                 {
1356                                         sprintf(name_dataset, "Algo %d", l);
1357                                         HDFdataset * hdf_datasetcmpt_ds = new HDFdataset(
1358                                                 name_dataset, hdf_subgroup10cmpt_sm);
1359                                         hdf_datasetcmpt_ds->OpenOnDisk();
1360                                         int size = hdf_datasetcmpt_ds->GetSize();
1361
1362                                         char *refFromFile = new char[size];
1363                                         hdf_datasetcmpt_ds->ReadFromDisk(refFromFile);
1364                                         hdf_datasetcmpt_ds->CloseOnDisk();
1365                                         delete hdf_datasetcmpt_ds;
1366                                         //cmpt_ds++;
1367
1368                                         if (_found3)
1369                                         {
1370                                                 SALOMEDS::SObject_var AlgoSO =
1371                                                         Study->FindObjectID(refFromFile);
1372                                                 if (!CORBA::is_nil(AlgoSO))
1373                                                 {
1374                                                         SALOMEDS::GenericAttribute_var anAttr;
1375                                                         SALOMEDS::AttributeIOR_var anIOR;
1376                                                         AlgoSO->FindAttribute(anAttr, "AttributeIOR");
1377                                                         anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
1378                                                         if (!CORBA::is_nil(anIOR))
1379                                                         {
1380                                                                 char *AlgoIOR = anIOR->Value();
1381                                                                 //SCRUTE(AlgoIOR);
1382                                                                 SMESH::SMESH_Hypothesis_var myHyp =
1383                                                                         SMESH::SMESH_Hypothesis::_narrow(_orb->
1384                                                                         string_to_object(AlgoIOR));
1385                                                                 SMESH::SMESH_Algo_var anAlgo =
1386                                                                         SMESH::SMESH_Algo::_narrow(myHyp);
1387                                                                 //SMESH::SMESH_Algo_var anAlgo = SMESH::SMESH_Algo::_narrow(_orb->string_to_object(AlgoIOR));
1388                                                                 if (!CORBA::is_nil(anAlgo))
1389                                                                 {
1390                                                                         SMESH::SMESH_Mesh_var aMesh =
1391                                                                                 aSubMesh->GetFather();
1392                                                                         aMesh->AddHypothesis(aSubShape, anAlgo);        //essayer avec _SMESHCorbaObj
1393                                                                         MESSAGE("Algorithms added ...");
1394                                                                 }
1395                                                         }
1396                                                 }
1397                                         }
1398                                 }
1399                         }
1400                 }
1401                 hdf_subgroupmyTag->CloseOnDisk();
1402                 delete hdf_subgroupmyTag;
1403         }
1404         hdf_subgroupmyLevel1Tag->CloseOnDisk();
1405         delete hdf_subgroupmyLevel1Tag;
1406 }
1407
1408 GEOM::GEOM_Gen_var SMESH_Gen_i::getGeomEngine()
1409 {
1410         MESSAGE("SMESH_Gen_i::getGeomEngine");
1411         SALOME_NamingService *_NS = SINGLETON_ < SALOME_NamingService >::Instance();
1412         ASSERT(SINGLETON_ < SALOME_NamingService >::IsAlreadyExisting());
1413         _NS->init_orb(_orb);
1414         SALOME_LifeCycleCORBA *myEnginesLifeCycle = new SALOME_LifeCycleCORBA(_NS);
1415         Engines::Component_var geomEngine =
1416         myEnginesLifeCycle->FindOrLoad_Component("FactoryServer", "GEOM");
1417         GEOM::GEOM_Gen_var myGeomEngine = GEOM::GEOM_Gen::_narrow(geomEngine);
1418         return myGeomEngine;    
1419 }
1420
1421 GEOM::GEOM_Shape_var SMESH_Gen_i::getShape(SALOMEDS::Study_var Study, char * refFromFile)
1422 {
1423         MESSAGE("SMESH_Gen_i::getShape("<<Study<<","<<refFromFile<<")");
1424         SCRUTE(CORBA::is_nil(Study));
1425         SALOMEDS::SObject_var CSO = Study->FindObjectID(refFromFile);
1426         GEOM::GEOM_Shape_var aShape;
1427
1428         if (!CORBA::is_nil(CSO))
1429         {
1430                 SALOMEDS::GenericAttribute_var anAttr;
1431                 CSO->FindAttribute(anAttr, "AttributeIOR");
1432                 if (!CORBA::is_nil(CSO))
1433                 {
1434                         MESSAGE("The shape was not loaded. Try to load it.");
1435                         SALOMEDS::Driver_var driver = SALOMEDS::Driver::_narrow(getGeomEngine());
1436                         SALOMEDS::SComponent_var SCO = SALOMEDS::SComponent::_narrow(Study->FindObject("Geometry"));
1437                         SALOMEDS::StudyBuilder_var B = Study->NewBuilder();     
1438                         B->LoadWith(SCO,driver);
1439                         CSO->FindAttribute(anAttr, "AttributeIOR");
1440                 }
1441
1442                 SALOMEDS::AttributeIOR_var anIOR =
1443                         SALOMEDS::AttributeIOR::_narrow(anAttr);
1444                 
1445                 char *ShapeIOR = anIOR->Value();
1446                 aShape =
1447                         GEOM::GEOM_Shape::_narrow(_orb->string_to_object(ShapeIOR));
1448         }
1449         return aShape;
1450 }
1451
1452 void SMESH_Gen_i::loadMesh(char * name, HDFfile * hdf_file,
1453         char* meshfile, SALOMEDS::Study_var Study)
1454 {
1455         MESSAGE("in mesh load");
1456         char msgname[HDF_NAME_MAX_LEN + 1];     
1457         char objectId[10];
1458         char name_of_group[HDF_NAME_MAX_LEN + 1];
1459         
1460         int myMeshId =
1461                 atoi((string(name).substr(5, 5)).c_str());
1462         SCRUTE(myMeshId);
1463
1464         HDFgroup * hdfGroupMeshId = new HDFgroup(name, hdf_file);
1465         hdfGroupMeshId->OpenOnDisk();
1466
1467         int nb_meshsubgroup = hdfGroupMeshId->nInternalObjects();
1468         SCRUTE(nb_meshsubgroup);
1469
1470         //********** Loading of the file name where the data are stored
1471         MESSAGE("Mesh data file");
1472         strcpy(name_of_group, "Mesh data");
1473         HDFdataset * dataset =
1474                 new HDFdataset(name_of_group, hdfGroupMeshId);
1475         dataset->OpenOnDisk();
1476
1477         char *datafilename = new char[dataset->GetSize()];
1478         dataset->ReadFromDisk(datafilename);
1479         dataset->CloseOnDisk(); 
1480
1481         //********** Loading of the reference on the shape
1482         //********** and mesh initialization
1483         MESSAGE("Ref on shape");
1484         strcpy(name_of_group, "Ref on shape");
1485         dataset =
1486                 new HDFdataset(name_of_group, hdfGroupMeshId);
1487         dataset->OpenOnDisk();
1488         
1489         char *refFromFile = new char[dataset->GetSize()];
1490         dataset->ReadFromDisk(refFromFile);
1491         dataset->CloseOnDisk(); 
1492
1493         bool _found = false;
1494         SCRUTE(refFromFile);
1495         SMESH::SMESH_Mesh_var myNewMesh;
1496         GEOM::GEOM_Shape_var aShape=getShape(Study, refFromFile);
1497
1498         if (!CORBA::is_nil(aShape))
1499         {
1500                 _found = true;
1501                 myNewMesh = this->Init(getGeomEngine(), Study->StudyId(), aShape);
1502                 string iorString = _orb->object_to_string(myNewMesh);
1503                 sprintf(objectId, "%ld", myNewMesh->GetId());
1504                 _SMESHCorbaObj[string("Mesh_") + string(objectId)] = iorString;
1505
1506                 SCRUTE(datafilename);
1507                 //********** 
1508                 //********** Loading of mesh data
1509                 if (strcmp(datafilename, "No data") != 0)
1510                 {                       
1511                         StudyContext_iStruct *myStudyContext =
1512                                 _mapStudyContext_i[Study->StudyId()];
1513                         int meshId = myNewMesh->GetId();
1514                         SMESH_Mesh_i *meshServant =
1515                                 myStudyContext->mapMesh_i[meshId];
1516                         ::SMESH_Mesh & myLocMesh = meshServant->GetImpl();
1517                         SMESHDS_Mesh *mySMESHDSMesh = myLocMesh.GetMeshDS();
1518
1519                         Mesh_Reader *myReader = SMESHDriver::GetMeshReader("MED");
1520                         myReader->SetMesh(mySMESHDSMesh);
1521                         myReader->SetMeshId(myMeshId);
1522                         myReader->SetFile(datafilename);
1523                         myReader->Read();
1524                         MESSAGE("Loaded a mesh with " << mySMESHDSMesh->NbNodes() <<" nodes");
1525                 }
1526         }
1527         //********** 
1528         //}
1529         //else if (strcmp(msgname,"Applied Hypothesis")==0) {
1530         for (int ii = 0; ii < nb_meshsubgroup; ii++)
1531         {
1532                 hdfGroupMeshId->InternalObjectIndentify(ii, msgname);
1533                 if (strcmp(msgname, "Mesh data") == 0)
1534                 {
1535                         //nothing
1536                 }
1537                 else if (strcmp(msgname, "Ref on shape") == 0)
1538                 {
1539                         //nothing
1540                 }
1541                 else if (strcmp(msgname, "Applied Hypothesis") == 0)
1542                 {
1543                         loadAppliedHypothesis(hdfGroupMeshId, _found, Study, myNewMesh,
1544                                 aShape);
1545                 }
1546                 else if (strcmp(msgname, "Applied Algorithms") == 0)
1547                 {
1548                         loadAppliedAlgorithms(hdfGroupMeshId, _found, Study, myNewMesh,
1549                                 aShape);
1550                 }
1551                 else if (string(msgname).substr(0, 9) == string("SubMeshes"))
1552                 {
1553                         loadSubMeshes(hdfGroupMeshId, msgname, Study, myNewMesh);
1554                 }
1555         }
1556         hdfGroupMeshId->CloseOnDisk();
1557         MESSAGE("End of Meshes Load");
1558 }
1559
1560 /**
1561  * Load the part of the study related to SMESH.
1562  */
1563 bool SMESH_Gen_i::Load(SALOMEDS::SComponent_ptr theComponent,
1564         const SALOMEDS::TMPFile & theStream, const char *theURL, bool isMultiFile)
1565 {
1566         MESSAGE("SMESH_Gen_i::Load");   
1567         // Get temporary files location
1568         TCollection_AsciiString tmpDir =
1569                 isMultiFile ? TCollection_AsciiString((char *)theURL) : SALOMEDS_Tool::
1570                 GetTmpDir();
1571
1572         // Convert the stream into sequence of files to process
1573         SALOMEDS::ListOfFileNames_var aFileSeq =
1574                 SALOMEDS_Tool::PutStreamToFiles(theStream, tmpDir.ToCString(),
1575                 isMultiFile);
1576
1577         TCollection_AsciiString aStudyName("");
1578         if (isMultiFile)
1579                 aStudyName =
1580                         (SALOMEDS_Tool::GetNameFromPath(theComponent->GetStudy()->URL()));
1581
1582         // Set names of temporary files
1583         TCollection_AsciiString filename =
1584                 tmpDir + aStudyName + TCollection_AsciiString("_SMESH.hdf");
1585         TCollection_AsciiString hypofile =
1586                 tmpDir + aStudyName + TCollection_AsciiString("_SMESH_Hypo.txt");
1587         TCollection_AsciiString algofile =
1588                 tmpDir + aStudyName + TCollection_AsciiString("_SMESH_Algo.txt");
1589         TCollection_AsciiString meshfile =
1590                 tmpDir + aStudyName + TCollection_AsciiString("_SMESH_Mesh.med");
1591
1592         SALOMEDS::Study_var Study = theComponent->GetStudy();
1593         int studyId = Study->StudyId();
1594         SCRUTE(studyId);
1595
1596         SALOMEDS::AttributeName_var aName;
1597         
1598         SALOMEDS::SComponent_var fathergeom = Study->FindComponent("GEOM");
1599         SALOMEDS::SComponent_var myGeomSComp =
1600                 SALOMEDS::SComponent::_narrow(fathergeom);
1601         SCRUTE(fathergeom);
1602
1603         char name[HDF_NAME_MAX_LEN + 1];
1604         map < int, HDFgroup * > hdf_subgroup;   
1605
1606
1607         //************* HDF file opening
1608         HDFfile *hdf_file = new HDFfile(filename.ToCString());
1609         try
1610         {
1611                 hdf_file->OpenOnDisk(HDF_RDONLY);
1612         }
1613         catch(HDFexception)
1614         {
1615                 MESSAGE("Load(): " << filename << " not found!");
1616                 return false;
1617         }
1618
1619         //****************************
1620
1621         int nb_group = hdf_file->nInternalObjects();
1622         SCRUTE(nb_group);
1623         for (int i = 0; i < nb_group; i++)
1624         {
1625                 hdf_file->InternalObjectIndentify(i, name);
1626                 if (strcmp(name, "Hypothesis") == 0)
1627                         loadHypothesis(name, hdf_file, hypofile.ToCString(), studyId);
1628                 else if (strcmp(name, "Algorithms") == 0)
1629                         loadAlgorithms(name, hdf_file, algofile.ToCString(), studyId);
1630                 else if (string(name).substr(0, 4) == string("Mesh"))
1631                         loadMesh(name, hdf_file, meshfile.ToCString(), Study);
1632         }
1633
1634         MESSAGE("End of SMESH_Gen::Load");
1635
1636         hdf_file->CloseOnDisk();
1637
1638         // Remove temporary files created from the stream
1639         if (isMultiFile)
1640                 SALOMEDS_Tool::RemoveTemporaryFiles(tmpDir.ToCString(), aFileSeq.in(),
1641                         true);
1642
1643         return true;
1644 }
1645
1646 bool SMESH_Gen_i::LoadASCII(SALOMEDS::SComponent_ptr theComponent,
1647         const SALOMEDS::TMPFile & theStream, const char *theURL, bool isMultiFile)
1648 {
1649         return Load(theComponent, theStream, theURL, isMultiFile);
1650 }
1651
1652 //=============================================================================
1653 /*!
1654  *  
1655  */
1656 //=============================================================================
1657
1658 void SMESH_Gen_i::Close(SALOMEDS::SComponent_ptr theComponent)
1659 {
1660         MESSAGE("Close");
1661         SALOMEDS::Study_var aStudy = theComponent->GetStudy();
1662         SALOMEDS::ChildIterator_var itBig = aStudy->NewChildIterator(theComponent);
1663         for (; itBig->More(); itBig->Next())
1664         {
1665                 SALOMEDS::SObject_var gotBranch = itBig->Value();
1666
1667                 // branch 1 : hypothesis
1668                 if (gotBranch->Tag() == Tag_HypothesisRoot ||
1669                         gotBranch->Tag() == Tag_AlgorithmsRoot)
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_Hypothesis_var myHyp =
1682                                                 SMESH::SMESH_Hypothesis::_narrow(_orb->
1683                                                 string_to_object(anIOR->Value()));
1684                                         char objectId[10];
1685                                         sprintf(objectId, "%ld", myHyp->GetId());
1686 //        cout<<"********** delete Hyp "<<objectId<<endl;
1687                                         _SMESHCorbaObj.erase(string("Hypo_") + string(objectId));
1688                                         myHyp =
1689                                                 SMESH::SMESH_Hypothesis::_narrow(_orb->
1690                                                 string_to_object(anIOR->Value()));
1691                                 }
1692                         }
1693                 }
1694                 // branch 2 : algorithms
1695                 else if (gotBranch->Tag() >= 3)
1696                 {
1697                         SALOMEDS::ChildIterator_var it =
1698                                 aStudy->NewChildIterator(gotBranch);
1699                         for (; it->More(); it->Next())
1700                         {
1701                                 SALOMEDS::SObject_var mySObject = it->Value();
1702                                 SALOMEDS::GenericAttribute_var anAttr;
1703                                 SALOMEDS::AttributeIOR_var anIOR;
1704                                 if (mySObject->FindAttribute(anAttr, "AttributeIOR"))
1705                                 {
1706                                         anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
1707                                         SMESH::SMESH_Mesh_var myMesh =
1708                                                 SMESH::SMESH_Mesh::_narrow(_orb->
1709                                                 string_to_object(anIOR->Value()));
1710                                         if (!myMesh->_is_nil())
1711                                         {
1712                                                 char objectId[10];
1713                                                 sprintf(objectId, "%ld", myMesh->GetId());
1714 //          cout<<"********** delete Mesh "<<objectId<<endl;
1715                                                 _SMESHCorbaObj.erase(string("Mesh_") +
1716                                                         string(objectId));
1717                                                 CORBA::release(myMesh);
1718                                         }
1719                                         else
1720                                         {
1721                                                 SMESH::SMESH_subMesh_var mySubMesh =
1722                                                         SMESH::SMESH_subMesh::_narrow(_orb->
1723                                                         string_to_object(anIOR->Value()));
1724                                                 if (!mySubMesh->_is_nil())
1725                                                 {
1726                                                         char objectId[10];
1727                                                         sprintf(objectId, "%ld", mySubMesh->GetId());
1728 //            cout<<"********** delete SubMesh "<<objectId<<endl;
1729                                                         _SMESHCorbaObj.erase(string("SubMesh_") +
1730                                                                 string(objectId));
1731                                                         CORBA::release(mySubMesh);
1732                                                 }
1733                                         }
1734                                 }
1735                         }
1736                 }
1737         }
1738 }
1739
1740 //=============================================================================
1741 /*!
1742  *  
1743  */
1744 //=============================================================================
1745
1746 char *SMESH_Gen_i::ComponentDataType()
1747 {
1748         MESSAGE("SMESH_Gen_i::ComponentDataType");
1749         return strdup("SMESH");
1750 }
1751
1752 //=============================================================================
1753 /*!
1754  *  
1755  */
1756 //=============================================================================
1757
1758 char *SMESH_Gen_i::IORToLocalPersistentID(SALOMEDS::SObject_ptr theSObject,
1759         const char *IORString, CORBA::Boolean isMultiFile, CORBA::Boolean isASCII)
1760 {
1761         MESSAGE("SMESH_Gen_i::IORToLocalPersistentID");
1762
1763         char objectId[10];
1764
1765         SMESH::SMESH_Algo_var myAlgo =
1766                 SMESH::SMESH_Algo::_narrow(_orb->string_to_object(IORString));
1767         if (!CORBA::is_nil(myAlgo))
1768         {
1769                 string prefix = "Hypo_";
1770                 sprintf(objectId, "%ld", myAlgo->GetId());
1771                 string lpID = prefix + string(objectId);
1772                 return CORBA::string_dup(lpID.c_str());
1773         }
1774         else
1775         {
1776                 SMESH::SMESH_Hypothesis_var myHypo =
1777                         SMESH::SMESH_Hypothesis::_narrow(_orb->string_to_object(IORString));
1778                 if (!CORBA::is_nil(myHypo))
1779                 {
1780                         string prefix = "Hypo_";
1781                         sprintf(objectId, "%ld", myHypo->GetId());
1782                         string lpID = prefix + string(objectId);
1783                         return CORBA::string_dup(lpID.c_str());
1784                 }
1785                 else
1786                 {
1787                         SMESH::SMESH_Mesh_var myMesh =
1788                                 SMESH::SMESH_Mesh::_narrow(_orb->string_to_object(IORString));
1789                         if (!CORBA::is_nil(myMesh))
1790                         {
1791                                 string prefix = "Mesh_";
1792                                 sprintf(objectId, "%ld", myMesh->GetId());
1793                                 string lpID = prefix + string(objectId);
1794                                 return CORBA::string_dup(lpID.c_str());
1795                         }
1796                         else
1797                         {
1798                                 SMESH::SMESH_subMesh_var mySubMesh =
1799                                         SMESH::SMESH_subMesh::_narrow(_orb->
1800                                         string_to_object(IORString));
1801                                 if (!CORBA::is_nil(mySubMesh))
1802                                 {
1803                                         string prefix = "SubMesh_";
1804                                         sprintf(objectId, "%ld", mySubMesh->GetId());
1805                                         string lpID = prefix + string(objectId);
1806                                         return CORBA::string_dup(lpID.c_str());
1807                                 }
1808                                 else
1809                                         return (strdup("no object"));
1810                         }
1811                 }
1812         }
1813 }
1814
1815 //=============================================================================
1816 /*!
1817  *  
1818  */
1819 //=============================================================================
1820
1821 char *SMESH_Gen_i::LocalPersistentIDToIOR(SALOMEDS::SObject_ptr theSObject,
1822         const char *aLocalPersistentID,
1823         CORBA::Boolean isMultiFile, CORBA::Boolean isASCII)
1824 {
1825         MESSAGE("SMESH_Gen_i::LocalPersistentIDToIOR");
1826         SCRUTE(aLocalPersistentID);
1827         string clef = string(aLocalPersistentID);
1828         SCRUTE(_SMESHCorbaObj[clef].c_str());
1829         return CORBA::string_dup(_SMESHCorbaObj[clef].c_str());
1830 }
1831
1832 //=============================================================================
1833 /*!
1834  *  
1835  */
1836 //=============================================================================
1837
1838 SMESH_topo *SMESH_Gen_i::ExploreMainShape(GEOM::GEOM_Gen_ptr geomEngine,
1839         CORBA::Long studyId, GEOM::GEOM_Shape_ptr aShape)
1840 {
1841         MESSAGE("SMESH_Mesh_i::ExploreMainShape");
1842         // _narrow() duplicates the reference and check the type
1843         GEOM::GEOM_Gen_var geom = GEOM::GEOM_Gen::_narrow(geomEngine);
1844         GEOM::GEOM_Shape_var myShape = GEOM::GEOM_Shape::_narrow(aShape);
1845
1846         if (CORBA::is_nil(geom))
1847                 THROW_SALOME_CORBA_EXCEPTION("bad geom reference", SALOME::BAD_PARAM);
1848         if (CORBA::is_nil(myShape))
1849                 THROW_SALOME_CORBA_EXCEPTION("bad shape reference", SALOME::BAD_PARAM);
1850         MESSAGE("---");
1851         SCRUTE(myShape->Name());
1852         geom->GetCurrentStudy(studyId);
1853         SCRUTE(studyId);
1854         TopoDS_Shape mainShape = _ShapeReader->GetShape(geom, myShape);
1855         MESSAGE("---");
1856
1857         // create an SMESH_topo object for the mainShape
1858
1859         SMESH_topo *myTopo = new SMESH_topo();
1860         MESSAGE("---");
1861
1862         // explore local TopoDS_Shape, store reference of local TopoDS subShapes
1863
1864         for (TopExp_Explorer exp(mainShape, TopAbs_COMPOUND); exp.More();
1865                 exp.Next())
1866         {
1867                 const TopoDS_Compound & E = TopoDS::Compound(exp.Current());
1868                 int i = myTopo->_myShapes[TopAbs_COMPOUND].Add(E);
1869                 SCRUTE(i);
1870         }
1871         for (TopExp_Explorer exp(mainShape, TopAbs_COMPSOLID); exp.More();
1872                 exp.Next())
1873         {
1874                 const TopoDS_CompSolid & E = TopoDS::CompSolid(exp.Current());
1875                 int i = myTopo->_myShapes[TopAbs_COMPSOLID].Add(E);
1876                 SCRUTE(i);
1877         }
1878         for (TopExp_Explorer exp(mainShape, TopAbs_SOLID); exp.More(); exp.Next())
1879         {
1880                 const TopoDS_Solid & E = TopoDS::Solid(exp.Current());
1881                 int i = myTopo->_myShapes[TopAbs_SOLID].Add(E);
1882                 SCRUTE(i);
1883         }
1884         for (TopExp_Explorer exp(mainShape, TopAbs_SHELL); exp.More(); exp.Next())
1885         {
1886                 const TopoDS_Shell & E = TopoDS::Shell(exp.Current());
1887                 int i = myTopo->_myShapes[TopAbs_SHELL].Add(E);
1888                 SCRUTE(i);
1889         }
1890         for (TopExp_Explorer exp(mainShape, TopAbs_FACE); exp.More(); exp.Next())
1891         {
1892                 const TopoDS_Face & E = TopoDS::Face(exp.Current());
1893                 int i = myTopo->_myShapes[TopAbs_FACE].Add(E);
1894                 SCRUTE(i);
1895         }
1896         for (TopExp_Explorer exp(mainShape, TopAbs_WIRE); exp.More(); exp.Next())
1897         {
1898                 const TopoDS_Wire & E = TopoDS::Wire(exp.Current());
1899                 int i = myTopo->_myShapes[TopAbs_WIRE].Add(E);
1900                 SCRUTE(i);
1901         }
1902         for (TopExp_Explorer exp(mainShape, TopAbs_EDGE); exp.More(); exp.Next())
1903         {
1904                 const TopoDS_Edge & E = TopoDS::Edge(exp.Current());
1905                 int i = myTopo->_myShapes[TopAbs_EDGE].Add(E);
1906                 SCRUTE(i);
1907         }
1908         for (TopExp_Explorer exp(mainShape, TopAbs_VERTEX); exp.More(); exp.Next())
1909         {
1910                 const TopoDS_Vertex & E = TopoDS::Vertex(exp.Current());
1911                 int i = myTopo->_myShapes[TopAbs_VERTEX].Add(E);
1912                 SCRUTE(i);
1913         }
1914
1915         // explore subShapes of distant CORBA object,
1916         // associate distant CORBA subShape references
1917         // with local reference to local TopoDS subShape
1918
1919         string filenode = "toposhape.txt";
1920         ofstream fic(filenode.c_str());
1921
1922         for (int shapeType = TopAbs_COMPOUND; shapeType < TopAbs_SHAPE; shapeType++)
1923         {
1924                 fic << "shape type : " << SMESH_shapeTypeNames[shapeType];
1925
1926                 GEOM::GEOM_Gen::ListOfGeomShapes_var subShapes
1927                         = geom->SubShapeAll(myShape, shapeType);
1928                 int nbSubShapes = subShapes->length();
1929                 int nbLocal = myTopo->_myShapes[shapeType].Extent();
1930                 fic << " - number of elements: " << nbSubShapes << endl;
1931                 ASSERT(nbSubShapes == nbLocal);
1932
1933                 for (int i = 0; i < nbSubShapes; i++)
1934                 {
1935                         GEOM::GEOM_Shape_var aSubShape = subShapes[i];
1936                         string idShape = SMESH_topo::GetShapeLocalId(aSubShape);
1937                         fic << "  " << idShape;
1938                         SCRUTE(idShape);
1939                         TopoDS_Shape aLocShape = _ShapeReader->GetShape(geom, aSubShape);
1940                         for (int j = 1; j <= nbLocal; j++)
1941                                 if (aLocShape.IsSame(myTopo->_myShapes[shapeType].FindKey(j)))
1942                                 {
1943                                         MESSAGE("  --- trouve = " << j);
1944                                         myTopo->_mapIndShapes[shapeType][idShape] = j;
1945                                         fic << "  --- trouve = " << j;
1946                                         break;
1947                                 }
1948                         fic << endl;
1949                 }
1950         }
1951         fic.close();
1952
1953         return myTopo;
1954 }
1955
1956 /**
1957  * Import a mesh from a file
1958  * @param fileName file name to be imported
1959  * @param fileType Currently it could be either "DAT", "UNV" or "MED".
1960  */
1961 SMESH::SMESH_Mesh_ptr SMESH_Gen_i::Import(CORBA::Long studyId,
1962         const char *fileName, const char *fileType)
1963 {
1964         MESSAGE("SMESH_Gen_I::Import");
1965         SMESH_Mesh_i *meshServant;
1966         try
1967         {
1968                 if (_mapStudyContext_i.find(studyId) == _mapStudyContext_i.end())
1969                 {
1970                         _mapStudyContext_i[studyId] = new StudyContext_iStruct;
1971                 }
1972                 StudyContext_iStruct *myStudyContext = _mapStudyContext_i[studyId];
1973
1974                 // create a new mesh object servant, store it in a map in study context
1975                 meshServant = new SMESH_Mesh_i(this, NULL, studyId, _localId);
1976                 myStudyContext->mapMesh_i[_localId] = meshServant;
1977                 _localId++;
1978
1979                 // create a new mesh object
1980                 meshServant->SetImpl(_impl.Import(studyId, fileName, fileType));
1981         }
1982         catch(SALOME_Exception & S_ex)
1983         {
1984                 THROW_SALOME_CORBA_EXCEPTION(S_ex.what(), SALOME::BAD_PARAM);
1985         }
1986
1987         // activate the CORBA servant of Mesh
1988
1989         SMESH::SMESH_Mesh_var mesh
1990                 = SMESH::SMESH_Mesh::_narrow(meshServant->_this());
1991
1992         meshServant->SetIor(mesh);
1993         return SMESH::SMESH_Mesh::_duplicate(mesh);
1994 }
1995
1996 //=============================================================================
1997 /*! 
1998  * C factory, accessible with dlsym, after dlopen  
1999  */
2000 //=============================================================================
2001
2002 extern "C"
2003 {
2004         PortableServer::ObjectId * SMESHEngine_factory(CORBA::ORB_ptr orb,
2005                 PortableServer::POA_ptr poa,
2006                 PortableServer::ObjectId * contId,
2007                 const char *instanceName, const char *interfaceName)
2008         {
2009                 MESSAGE("PortableServer::ObjectId * SMESHEngine_factory()");
2010                 SCRUTE(interfaceName);
2011                 SMESH_Gen_i *mySMESH_Gen
2012                         = new SMESH_Gen_i(orb, poa, contId, instanceName, interfaceName);
2013                 return mySMESH_Gen->getId();
2014         }
2015 }