Salome HOME
0021803: EDF 2351 : Available versions of MED in TUI function ExportMED aren't consis...
[modules/smesh.git] / src / SMESH_I / SMESH_PreMeshInfo.cxx
1 // Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  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, or (at your option) any later version.
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.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 // File      : SMESH_PreMeshInfo.cxx
23 // Created   : Fri Feb 10 17:36:39 2012
24 // Author    : Edward AGAPOV (eap)
25 //
26
27 #include "SMESH_PreMeshInfo.hxx"
28
29 #include "DriverMED.hxx"
30 #include "DriverMED_R_SMESHDS_Mesh.h"
31 #include "MED_Factory.hxx"
32 #include "SMDS_EdgePosition.hxx"
33 #include "SMDS_FacePosition.hxx"
34 #include "SMDS_SpacePosition.hxx"
35 #include "SMDS_VertexPosition.hxx"
36 #include "SMESHDS_Group.hxx"
37 #include "SMESHDS_GroupOnFilter.hxx"
38 #include "SMESHDS_Mesh.hxx"
39 #include "SMESH_Gen_i.hxx"
40 #include "SMESH_Group_i.hxx"
41 #include "SMESH_Mesh_i.hxx"
42 #include "SMESH_subMesh_i.hxx"
43
44 #include <HDFarray.hxx>
45 #include <HDFdataset.hxx>
46 #include <HDFfile.hxx>
47 #include <HDFgroup.hxx>
48 #include <SALOMEDS_Tool.hxx>
49 #include <SALOMEDS_wrap.hxx>
50
51 #include <TopoDS_Iterator.hxx>
52 #include <TopoDS_Shape.hxx>
53
54 #include "SMESH_TryCatch.hxx"
55
56 #include CORBA_SERVER_HEADER(SALOME_Session)
57
58 using namespace std;
59
60 #define MYDEBUGOUT(msg) //std::cout << msg << std::endl;
61
62 namespace
63 {
64   enum {  GroupOnFilter_OutOfDate = -1 };
65
66   // count not yet loaded meshes
67   static int theMeshCounter = 0;
68
69   //================================================================================
70   /*!
71    * \brief Counts not fully loaded meshes
72    */
73   //================================================================================
74
75   void meshInfoLoaded( SMESH_Mesh_i* mesh )
76   {
77     theMeshCounter++;
78   }
79   //================================================================================
80   /*!
81    * \brief Removes temporary files if none of meshes needs them
82    */
83   //================================================================================
84
85   void filesNoMoreNeeded(SMESH_Mesh_i* mesh,
86                          std::string   medFile,
87                          std::string   hdfFile)
88   {
89     if ( --theMeshCounter == 0 )
90     {
91       std::string tmpDir = SALOMEDS_Tool::GetDirFromPath( hdfFile );
92
93       SALOMEDS_Tool::ListOfFiles aFiles;
94       aFiles.reserve(2);
95       medFile = SALOMEDS_Tool::GetNameFromPath( medFile ) + ".med";
96       hdfFile = SALOMEDS_Tool::GetNameFromPath( hdfFile ) + ".hdf";
97       aFiles.push_back(medFile.c_str());
98       aFiles.push_back(hdfFile.c_str());
99
100       SALOMEDS_Tool::RemoveTemporaryFiles( tmpDir.c_str(), aFiles, true );
101     }
102   }
103
104   //=============================================================================
105   /*!
106    * \brief Class sending signals on start and finish of loading
107    */
108   //=============================================================================
109
110   class SignalToGUI
111   {
112     std::string         _messagePrefix;
113     SALOME::Session_var _session;
114   public:
115     SignalToGUI( SMESH_Mesh_i* mesh )
116     {
117       SMESH_Gen_i* gen = SMESH_Gen_i::GetSMESHGen();
118
119       SALOMEDS::SObject_wrap meshSO = gen->ObjectToSObject( mesh->_this() );
120       CORBA::Object_var        obj = gen->GetNS()->Resolve( "/Kernel/Session" );
121       _session = SALOME::Session::_narrow( obj );
122       if ( !meshSO->_is_nil() && !_session->_is_nil() )
123       {
124         CORBA::String_var meshEntry = meshSO->GetID();
125         _messagePrefix = "SMESH/mesh_loading/";
126         _messagePrefix += meshEntry.in();
127
128         std::string msgToGUI = _messagePrefix + "/";
129         msgToGUI += SMESH_Comment( mesh->NbNodes() );
130         msgToGUI += "/";
131         msgToGUI += SMESH_Comment( mesh->NbElements() );
132
133         _session->emitMessageOneWay( msgToGUI.c_str());
134       }
135     }
136     void sendStop()
137     {
138       if ( !_messagePrefix.empty() )
139       {
140         std::string msgToGUI = _messagePrefix + "/stop";
141         _session->emitMessageOneWay( msgToGUI.c_str());
142         _messagePrefix.clear();
143       }
144     }
145     ~SignalToGUI() { sendStop(); }
146   };
147
148   //=============================================================================
149   /*!
150    * \brief Creates SMDS_Position according to shape type
151    */
152   //=============================================================================
153
154   class PositionCreator {
155   public:
156     SMDS_PositionPtr MakePosition(const TopAbs_ShapeEnum type) {
157       return (this->*myFuncTable[ type ])();
158     }
159     PositionCreator() {
160       myFuncTable.resize( (size_t) TopAbs_SHAPE, & PositionCreator::defaultPosition );
161       myFuncTable[ TopAbs_SOLID  ] = & PositionCreator::volumePosition;
162       myFuncTable[ TopAbs_FACE   ] = & PositionCreator::facePosition;
163       myFuncTable[ TopAbs_EDGE   ] = & PositionCreator::edgePosition;
164       myFuncTable[ TopAbs_VERTEX ] = & PositionCreator::vertexPosition;
165     }
166   private:
167     SMDS_PositionPtr edgePosition()    const { return SMDS_PositionPtr( new SMDS_EdgePosition  ); }
168     SMDS_PositionPtr facePosition()    const { return SMDS_PositionPtr( new SMDS_FacePosition  ); }
169     SMDS_PositionPtr volumePosition()  const { return SMDS_PositionPtr( new SMDS_SpacePosition ); }
170     SMDS_PositionPtr vertexPosition()  const { return SMDS_PositionPtr( new SMDS_VertexPosition); }
171     SMDS_PositionPtr defaultPosition() const { return SMDS_SpacePosition::originSpacePosition();  }
172     typedef SMDS_PositionPtr (PositionCreator:: * FmakePos)() const;
173     std::vector<FmakePos> myFuncTable;
174   };
175
176   //================================================================================
177   /*!
178    * \brief Returns ids of simple shapes composing a complex one
179    */
180   //================================================================================
181
182   std::vector<int> getSimpleSubMeshIds( SMESHDS_Mesh* meshDS, int shapeId )
183   {
184     std::vector<int> ids;
185
186     std::list<TopoDS_Shape> shapeQueue( 1, meshDS->IndexToShape( shapeId ));
187     std::list<TopoDS_Shape>::iterator shape = shapeQueue.begin();
188     for ( ; shape != shapeQueue.end(); ++shape )
189     {
190       if ( shape->IsNull() ) continue;
191       if ( shape->ShapeType() == TopAbs_COMPOUND ||
192            shape->ShapeType() == TopAbs_COMPSOLID )
193       {
194         for ( TopoDS_Iterator it( *shape ); it.More(); it.Next() )
195           shapeQueue.push_back( it.Value() );
196       }
197       else
198       {
199         ids.push_back( meshDS->ShapeToIndex( *shape ));
200       }
201     }
202     return ids;
203   }
204
205   //================================================================================
206   /*!
207    * \brief Return EEntiteMaillage by EGeometrieElement
208    */
209   //================================================================================
210
211   MED::EEntiteMaillage entityByGeom(const MED::EGeometrieElement geom )
212   {
213     return geom == MED::eBALL ? MED::eSTRUCT_ELEMENT : MED::eMAILLE;
214   }
215
216   //================================================================================
217   /*!
218    * \brief Return a map< EGeometrieElement, SMDSAbs_EntityType >
219    */
220   //================================================================================
221
222   typedef std::map< MED::EGeometrieElement, SMDSAbs_EntityType > Tmed2smeshElemTypeMap;
223   const Tmed2smeshElemTypeMap& med2smeshElemTypeMap()
224   {
225     static Tmed2smeshElemTypeMap med2smeshTypes;
226     if ( med2smeshTypes.empty() )
227     {
228       for  ( int iG = 0; iG < SMDSEntity_Last; ++iG )
229       {
230         SMDSAbs_EntityType    smdsType = (SMDSAbs_EntityType) iG;
231         MED::EGeometrieElement medType =
232           (MED::EGeometrieElement) DriverMED::GetMedGeoType( smdsType );
233         med2smeshTypes.insert( std::make_pair( medType, smdsType ));
234       }
235     }
236     return med2smeshTypes;
237   }
238
239   //================================================================================
240   /*!
241    * \brief Writes meshInfo into a HDF file
242    */
243   //================================================================================
244
245   void meshInfo2hdf( SMESH::long_array_var meshInfo,
246                      const std::string&    name,
247                      HDFgroup*             hdfGroup)
248   {
249     // we use med identification of element (MED::EGeometrieElement) types
250     // but not enum SMDSAbs_EntityType because values of SMDSAbs_EntityType may
251     // change at insertion of new items in the middle.
252     //const vector<MED::EGeometrieElement>& medTypes = mesh2medElemType();
253
254     std::vector<int> data;
255
256     for ( size_t i = 0; i < meshInfo->length(); ++i )
257       if ( meshInfo[i] > 0 )
258       {
259         data.push_back( DriverMED::GetMedGeoType( SMDSAbs_EntityType( i ))); //medTypes[ i ] );
260         data.push_back( meshInfo[ i ] );
261       }
262
263     if ( !data.empty() )
264     {
265       hdf_size datasetSize[] = { data.size() };
266       HDFarray* anArray = new HDFarray(0, HDF_INT32, 1, datasetSize);
267       anArray->CreateOnDisk();
268       datasetSize[0] = 1;
269       HDFdataset* dataset = new HDFdataset( name.c_str(), hdfGroup, HDF_ARRAY, datasetSize, 1 );
270       dataset->SetArrayId(anArray->GetId());
271       dataset->CreateOnDisk();
272       dataset->WriteOnDisk( & data[0]  );
273       dataset->CloseOnDisk();
274       anArray->CloseOnDisk();
275     }
276   }
277 }
278
279 //================================================================================
280 /*!
281  * \brief Reads meshInfo from a HDF file
282  */
283 //================================================================================
284
285 void SMESH_PreMeshInfo::hdf2meshInfo( const std::string& name,
286                                       HDFgroup*          hdfGroup)
287 {
288   if ( hdfGroup->ExistInternalObject( name.c_str()) )
289   {
290     HDFdataset* dataset = new HDFdataset( name.c_str(), hdfGroup );
291     dataset->OpenOnDisk();
292
293     // // hdf_size datasetSize[ 1 ];
294     // // HDFarray *array = new HDFarray(dataset);
295     // // array->GetDim( datasetSize );
296     // int size = dataset->GetSize();
297
298     std::vector<int> info( SMDSEntity_Last * 2, 0 );
299     dataset->ReadFromDisk( &info[0] );
300     dataset->CloseOnDisk();
301
302     const Tmed2smeshElemTypeMap& med2smesh = med2smeshElemTypeMap();
303     Tmed2smeshElemTypeMap::const_iterator me2sme, me2smeEnd = med2smesh.end();
304     for ( size_t i = 0; i < info.size(); )
305     {
306       int medType = info[i++];
307       int nbElems = info[i++];
308       if ( !nbElems ) break;
309       me2sme = med2smesh.find( (MED::EGeometrieElement) medType );
310       if ( me2sme != me2smeEnd )
311         setNb( me2sme->second, nbElems );
312     }
313   }
314   _isInfoOk = true;
315
316   if ( NbNodes() == GroupOnFilter_OutOfDate ) // case of !SMESHDS_GroupOnFilter::IsUpToDate()
317   {
318     _isInfoOk = false;
319     setNb( SMDSEntity_Node, 0 );
320   }
321 }
322
323 //================================================================================
324 /*!
325  * \brief Constructor callable by SMESH_PreMeshInfo only
326  */
327 //================================================================================
328
329 SMESH_PreMeshInfo::SMESH_PreMeshInfo(SMESH_Mesh_i*      mesh,
330                                      const int          meshID,
331                                      const std::string& medFile,
332                                      const std::string& hdfFile)
333   : _medFileName( medFile ),
334     _hdfFileName( hdfFile ),
335     _toRemoveFiles( false ),
336     _meshID( meshID ),
337     _mesh( mesh ),
338     _isInfoOk( false ),
339     _elemCounter( 0 )
340 {
341 }
342
343 //================================================================================
344 /*!
345  * \brief Release temporary files
346  */
347 //================================================================================
348
349 SMESH_PreMeshInfo::~SMESH_PreMeshInfo()
350 {
351   if ( _toRemoveFiles ) // it can be true only for SMESH_PreMeshInfo of the mesh
352     filesNoMoreNeeded( _mesh, _medFileName, _hdfFileName );
353
354   _toRemoveFiles = false;
355 }
356
357 //================================================================================
358 /*!
359  * \brief fills SMESH_PreMeshInfo field of all objects of mesh
360  */
361 //================================================================================
362
363 void SMESH_PreMeshInfo::LoadFromFile( SMESH_Mesh_i*      mesh,
364                                       const int          meshID,
365                                       const std::string& medFile,
366                                       const std::string& hdfFile,
367                                       const bool         toRemoveFiles)
368 {
369   SMESH_TRY;
370
371   SMESH_PreMeshInfo* meshPreInfo = new SMESH_PreMeshInfo( mesh,meshID,medFile,hdfFile );
372   mesh->changePreMeshInfo() = meshPreInfo;
373
374   meshPreInfo->_toRemoveFiles = toRemoveFiles;
375   if ( toRemoveFiles )
376     meshInfoLoaded( mesh );
377
378   if ( meshPreInfo->readPreInfoFromHDF() )
379     // all SMESH_PreMeshInfo's are stored in HDF file (written after
380     // implementing SMESH_PreMeshInfo)
381     return;
382
383   // try to read SMESH_PreMeshInfo from med file (as study is older than SMESH_PreMeshInfo)
384   if ( meshPreInfo->readMeshInfo() )
385   {
386     meshPreInfo->readGroupInfo();
387     meshPreInfo->readSubMeshInfo();
388   }
389   else
390   {
391     meshPreInfo->FullLoadFromFile();
392   }
393   SMESH_CATCH( SMESH::doNothing );
394 }
395
396 //================================================================================
397 /*!
398  * \brief Tries to read all SMESH_PreMeshInfo from a HDF file
399  *  \retval bool - true if succeeded
400  *
401  * This method is symmetrical to SaveToFile()
402  */
403 //================================================================================
404
405 bool SMESH_PreMeshInfo::readPreInfoFromHDF()
406 {
407   HDFfile* aFile = new HDFfile( (char*) _hdfFileName.c_str() );
408   aFile->OpenOnDisk( HDF_RDONLY );
409
410   SMESH_Comment hdfGroupName("SMESH_PreMeshInfo"); hdfGroupName << _meshID;
411   const bool infoAvailable = aFile->ExistInternalObject( hdfGroupName );
412   if ( infoAvailable )
413   {
414     HDFgroup* infoHdfGroup = new HDFgroup( hdfGroupName, aFile );
415     infoHdfGroup->OpenOnDisk();
416
417     _mesh->changePreMeshInfo()->hdf2meshInfo( "Mesh", infoHdfGroup );
418
419     // read SMESH_PreMeshInfo of groups
420     map<int, SMESH::SMESH_GroupBase_ptr>::const_iterator i2group = _mesh->_mapGroups.begin();
421     for ( ; i2group != _mesh->_mapGroups.end(); ++i2group )
422     {
423       if ( SMESH_GroupBase_i* group_i =
424            SMESH::DownCast<SMESH_GroupBase_i*>( i2group->second ))
425       {
426         group_i->changePreMeshInfo() = newInstance();
427         if ( SMESHDS_GroupBase* group = group_i->GetGroupDS() )
428         {
429           const std::string name = group->GetStoreName();
430           group_i->changePreMeshInfo()->hdf2meshInfo( name, infoHdfGroup );
431         }
432       }
433     }
434
435     // read SMESH_PreMeshInfo of sub-meshes
436     map<int, SMESH::SMESH_subMesh_ptr>::iterator id2sm = _mesh->_mapSubMeshIor.begin();
437     for ( ; id2sm != _mesh->_mapSubMeshIor.end(); ++id2sm )
438     {
439       if ( SMESH_subMesh_i* sm = SMESH::DownCast<SMESH_subMesh_i*>( id2sm->second ))
440       {
441         sm->changePreMeshInfo() = newInstance();
442         sm->changePreMeshInfo()->hdf2meshInfo( SMESH_Comment( sm->GetId()), infoHdfGroup );
443       }
444     }
445   }
446
447   aFile->CloseOnDisk();
448   delete aFile;
449
450   return infoAvailable;
451 }
452
453 //================================================================================
454 /*!
455  * \brief Reads mesh info of mesh from the med file
456  */
457 //================================================================================
458
459 bool SMESH_PreMeshInfo::readMeshInfo()
460 {
461   _isInfoOk = true;
462
463   MED::PWrapper aMed = MED::CrWrapperR(_medFileName);
464   MED::PMeshInfo medMeshInfo = aMed->CrMeshInfo(3,3,SMESH_Comment( _meshID ));
465
466   // read nb nodes
467   int nbNodes = std::max( 0, aMed->GetNbNodes( medMeshInfo ));
468   if ( nbNodes > 0 )
469   {
470     setNb( SMDSEntity_Node, nbNodes);
471
472     // read nb of elements
473     Tmed2smeshElemTypeMap::const_iterator me2sme    = med2smeshElemTypeMap().begin();
474     Tmed2smeshElemTypeMap::const_iterator me2smeEnd = med2smeshElemTypeMap().end();
475     for ( ; me2sme != me2smeEnd; ++me2sme )
476     {
477       int nbElems = aMed->GetNbCells( medMeshInfo, entityByGeom(me2sme->first), me2sme->first );
478       if ( nbElems > 0 )
479         setNb( me2sme->second, nbElems );
480     }
481   }
482   return true;
483 }
484
485 //================================================================================
486 /*!
487  * \brief Reads info of groups from the med file
488  */
489 //================================================================================
490
491 void SMESH_PreMeshInfo::readGroupInfo()
492 {
493   if ( _mesh->_mapGroups.empty() ) return;
494
495   // make SMESH_PreMeshInfo of groups
496   map< std::string, SMESH_PreMeshInfo* > name2GroupInfo;
497   map<int, SMESH::SMESH_GroupBase_ptr>::const_iterator i2group = _mesh->_mapGroups.begin();
498   for ( ; i2group != _mesh->_mapGroups.end(); ++i2group )
499   {
500     if ( SMESH_GroupBase_i* group_i =
501          SMESH::DownCast<SMESH_GroupBase_i*>( i2group->second ))
502     {
503       SMESH_PreMeshInfo* info = newInstance();
504       group_i->changePreMeshInfo() = info;
505       if ( SMESHDS_Group* group = dynamic_cast< SMESHDS_Group* >( group_i->GetGroupDS() ))
506       {
507         std::string name = group->GetStoreName();
508         name2GroupInfo.insert( std::make_pair( name, info ));
509         info->_isInfoOk = true;
510       }
511     }
512   }
513
514   map< int, vector< SMESH_PreMeshInfo* > > famId2grInfo;
515
516   MED::PWrapper aMed = MED::CrWrapperR(_medFileName);
517   MED::PMeshInfo medMeshInfo = aMed->CrMeshInfo(3,3,SMESH_Comment( _meshID ));
518
519   // read families to fill in famId2grInfo
520   int nbFams = aMed->GetNbFamilies( medMeshInfo );
521   if ( nbFams <= 1 ) return; // zero family is always present
522   for ( int iF = 0; iF <= nbFams; ++iF )
523   {
524     int nbGroups = aMed->GetNbFamGroup( iF, medMeshInfo );
525     if ( nbGroups < 1 ) continue;
526     MED::PFamilyInfo medFamInfo = aMed->CrFamilyInfo( medMeshInfo, nbGroups, nbGroups );
527     aMed->GetFamilyInfo( iF, medFamInfo ); // read groups of a family
528     vector< SMESH_PreMeshInfo* >& grInfoVec = famId2grInfo[ medFamInfo->GetId() ];
529     for ( int iG = 0; iG < nbGroups; ++iG )
530     {
531       const std::string grName = medFamInfo->GetGroupName( iG );
532       map< std::string, SMESH_PreMeshInfo* >::iterator n2i = name2GroupInfo.find( grName );
533       if ( n2i != name2GroupInfo.end() )
534         grInfoVec.push_back( n2i->second );
535     }
536   }
537
538   // read family numbers of elements
539   Tmed2smeshElemTypeMap::const_iterator me2sme    = med2smeshElemTypeMap().begin();
540   Tmed2smeshElemTypeMap::const_iterator me2smeEnd = med2smeshElemTypeMap().end();
541   MED::PElemInfo medElemInfo = aMed->CrElemInfo( medMeshInfo, 0 );
542   MED::TIntVector& famNums = medElemInfo->myFamNum;
543   for ( ; me2sme != me2smeEnd; ++me2sme ) // loop on elem types
544   {
545     famNums.resize( NbEntities( me2sme->second ));
546     if ( famNums.empty() ) continue;
547     aMed->GetFamilies( medElemInfo, famNums.size(), entityByGeom(me2sme->first), me2sme->first );
548     // distribute elements of a type among groups
549     map< int, vector< SMESH_PreMeshInfo* > >::iterator f2infos = famId2grInfo.begin();
550     for ( size_t i = 0; i < famNums.size(); ++i )
551     {
552       if ( famNums[i] != f2infos->first )
553       {
554         f2infos = famId2grInfo.find( famNums[i] );
555         if ( f2infos == famId2grInfo.end() )
556           f2infos = famId2grInfo.insert
557             ( std::make_pair( famNums[i], vector< SMESH_PreMeshInfo*>())).first;
558       }
559       vector< SMESH_PreMeshInfo* >& infoVec = f2infos->second ;
560       for ( size_t j = 0; j < infoVec.size(); ++j )
561         infoVec[j]->_elemCounter++;
562     }
563     // pass _elemCounter to a real elem type
564     map< std::string, SMESH_PreMeshInfo* >::iterator n2i = name2GroupInfo.begin();
565     for ( ; n2i != name2GroupInfo.end(); ++n2i )
566     {
567       SMESH_PreMeshInfo* info = n2i->second;
568       info->setNb( me2sme->second, info->_elemCounter );
569       info->_elemCounter = 0;
570     }
571   }
572 }
573
574 //================================================================================
575 /*!
576  * \brief Reads info of sub-meshes from hdf file of old study
577  */
578 //================================================================================
579
580 void SMESH_PreMeshInfo::readSubMeshInfo()
581 {
582   if ( _mesh->_mapSubMeshIor.empty() ) return;
583
584   // create SMESH_PreMeshInfo of sub-meshes
585   map<int, SMESH::SMESH_subMesh_ptr>::iterator id2sm = _mesh->_mapSubMeshIor.begin();
586   for ( ; id2sm != _mesh->_mapSubMeshIor.end(); ++id2sm )
587   {
588     if ( SMESH_subMesh_i* sm = SMESH::DownCast<SMESH_subMesh_i*>( id2sm->second ))
589     {
590       sm->changePreMeshInfo() = newInstance();
591       sm->changePreMeshInfo()->_isInfoOk = true;
592     }
593   }
594
595   // try to read 
596   HDFfile* aFile = new HDFfile( (char*) _hdfFileName.c_str() );
597   aFile->OpenOnDisk( HDF_RDONLY );
598
599   char meshGrpName[ 30 ];
600   sprintf( meshGrpName, "Mesh %d", _meshID );
601   if ( aFile->ExistInternalObject( meshGrpName ) )
602   {
603     HDFgroup* aTopGroup = new HDFgroup( meshGrpName, aFile );
604     aTopGroup->OpenOnDisk();
605     if ( aTopGroup->ExistInternalObject( "Submeshes" ))
606     {
607       HDFgroup* aGroup = new HDFgroup( "Submeshes", aTopGroup );
608       aGroup->OpenOnDisk();
609
610       SMESHDS_Mesh* meshDS = _mesh->GetImpl().GetMeshDS();
611       int maxSmId = Max( meshDS->MaxSubMeshIndex(), meshDS->MaxShapeIndex() );
612
613       for ( int isNode = 0; isNode < 2; ++isNode )
614       {
615         std::string aDSName( isNode ? "Node Submeshes" : "Element Submeshes");
616         if ( aGroup->ExistInternalObject( (char*) aDSName.c_str() ))
617         {
618           // read sub-mesh id of all nodes or elems
619           HDFdataset* aDataset = new HDFdataset( (char*) aDSName.c_str(), aGroup );
620           aDataset->OpenOnDisk();
621           int nbElems = aDataset->GetSize();
622           int* smIDs = new int [ nbElems ];
623           aDataset->ReadFromDisk( smIDs );
624           aDataset->CloseOnDisk();
625           // count nb elems in each sub-mesh
626           vector<int> nbBySubmeshId( maxSmId + 1, 0 );
627           for ( int i = 0; i < nbElems; ++i )
628           {
629             const int smID = smIDs[ i ];
630             if ( smID < (int) nbBySubmeshId.size() )
631               nbBySubmeshId[ smID ]++;
632           }
633           delete [] smIDs;
634
635           // store nb elems in SMESH_PreMeshInfo of sub-meshes
636           map<int, SMESH::SMESH_subMesh_ptr>::iterator id2sm = _mesh->_mapSubMeshIor.begin();
637           for ( ; id2sm != _mesh->_mapSubMeshIor.end(); ++id2sm )
638           {
639             if ( SMESH_subMesh_i* sm = SMESH::DownCast<SMESH_subMesh_i*>( id2sm->second ))
640             {
641               SMESH_PreMeshInfo* & info = sm->changePreMeshInfo();
642
643               vector<int> smIds = getSimpleSubMeshIds( meshDS, id2sm->first );
644               for ( size_t i = 0; i < smIds.size(); ++i )
645                 info->_elemCounter += nbBySubmeshId[ smIds[i] ];
646
647               SMDSAbs_EntityType elemType;
648               if ( isNode )
649               {
650                 elemType = SMDSEntity_Node;
651               }
652               else
653               {
654                 bool koElemType = false;
655                 const TopoDS_Shape& shape = meshDS->IndexToShape( smIds[0] );
656                 elemType = getElemType( shape.ShapeType(), info->_elemCounter, koElemType );
657                 info->_isInfoOk = !koElemType;
658               }
659               info->setNb( elemType, info->_elemCounter );
660             }
661           }
662         } // if ( aGroup->ExistInternalObject( aDSName ))
663       } // for ( int isNode = 0; isNode < 2; ++isNode )
664
665       aGroup->CloseOnDisk();
666     } // if ( aTopGroup->ExistInternalObject( "Submeshes" ))
667
668     aTopGroup->CloseOnDisk();
669   } // if ( aFile->ExistInternalObject( meshGrpName ) )
670
671   aFile->CloseOnDisk();
672   delete aFile;
673 }
674
675 //================================================================================
676 /*!
677  * \brief Return type of element for sub-mesh on a shape of given type
678  */
679 //================================================================================
680
681 SMDSAbs_EntityType SMESH_PreMeshInfo::getElemType( const TopAbs_ShapeEnum shapeType,
682                                                    const int              nbElemsInSubMesh,
683                                                    bool&                  isKoType) const
684 {
685   isKoType = false;
686   int type, typeEnd;
687   SMESH_PreMeshInfo* meshInfo = _mesh->changePreMeshInfo();
688
689   switch ( shapeType )
690   {
691   case TopAbs_SOLID:
692     type = SMDSEntity_Tetra;
693     typeEnd = SMDSEntity_Last;
694     isKoType = ( meshInfo->NbVolumes() != nbElemsInSubMesh );
695     break;
696   case TopAbs_FACE:
697   case TopAbs_SHELL:  
698     type = SMDSEntity_Triangle;
699     typeEnd = SMDSEntity_Tetra;
700     isKoType = ( meshInfo->NbFaces() != nbElemsInSubMesh );
701     break;
702   case TopAbs_WIRE:
703   case TopAbs_EDGE:   return SMDSEntity_Edge;
704   case TopAbs_VERTEX: return SMDSEntity_0D;
705   default:            return SMDSEntity_Last;
706   }
707
708   if ( !isKoType )
709   {
710     for ( int t = type; t < typeEnd; ++t )
711       if ( nbElemsInSubMesh == meshInfo->NbEntities( SMDSAbs_EntityType( t )))
712         return SMDSAbs_EntityType( t );
713   }
714   isKoType = true;
715   return SMDSAbs_EntityType( type );
716 }
717
718 //================================================================================
719 /*!
720  * \brief Saves SMESH_PreMeshInfo to the study file
721  */
722 //================================================================================
723
724 void SMESH_PreMeshInfo::SaveToFile( SMESH_Mesh_i* mesh,
725                                     const int     meshID,
726                                     HDFfile*      hdfFile)
727 {
728   // create a HDF group for SMESH_PreMeshInfo of this mesh
729   SMESH_Comment hdfGroupName("SMESH_PreMeshInfo"); hdfGroupName << meshID;
730   HDFgroup* infoHdfGroup = new HDFgroup( hdfGroupName, hdfFile );
731   infoHdfGroup->CreateOnDisk();
732
733   SMESH_TRY;
734
735   // info of mesh
736   meshInfo2hdf( mesh->GetMeshInfo(), "Mesh", infoHdfGroup );
737   
738   // info of groups
739   SMESH_PreMeshInfo incompleteInfo( 0,0,"","");
740   incompleteInfo.setNb( SMDSEntity_Node, GroupOnFilter_OutOfDate );
741   SMESHDS_Mesh* meshDS = mesh->GetImpl().GetMeshDS();
742
743   map<int, SMESH::SMESH_GroupBase_ptr>::const_iterator i2group = mesh->_mapGroups.begin();
744   for ( ; i2group != mesh->_mapGroups.end(); ++i2group )
745   {
746     if ( SMESH_GroupBase_i* group_i = SMESH::DownCast<SMESH_GroupBase_i*>( i2group->second ))
747     {
748       SMESHDS_GroupBase * group = group_i->GetGroupDS();
749       if ( SMESHDS_GroupOnFilter* gof = dynamic_cast<SMESHDS_GroupOnFilter*>(group))
750       {
751         // prevent too long storage time due to applying filter to many elements
752         if ( !gof->IsUpToDate() && meshDS->GetMeshInfo().NbElements( gof->GetType() ) > 1e5 )
753         {
754           meshInfo2hdf( incompleteInfo.GetMeshInfo(),
755                         group->GetStoreName(),
756                         infoHdfGroup);
757           continue;
758         }
759       }
760       meshInfo2hdf( group_i->GetMeshInfo(), group->GetStoreName(), infoHdfGroup);
761     }
762   }
763
764   // info of sub-meshes
765   map<int, SMESH::SMESH_subMesh_ptr>::iterator id2sm = mesh->_mapSubMeshIor.begin();
766   for ( ; id2sm != mesh->_mapSubMeshIor.end(); ++id2sm )
767   {
768     if ( SMESH_subMesh_i* sm = SMESH::DownCast<SMESH_subMesh_i*>( id2sm->second ))
769     {
770       meshInfo2hdf( sm->GetMeshInfo(),
771                     SMESH_Comment( sm->GetId() ),
772                     infoHdfGroup);
773     }
774   }
775
776   SMESH_CATCH( SMESH::doNothing );
777
778   infoHdfGroup->CloseOnDisk();
779 }
780
781 //================================================================================
782 /*!
783  * \brief Reads all data and remove all SMESH_PreMeshInfo fields from objects
784  */
785 //================================================================================
786
787 void SMESH_PreMeshInfo::FullLoadFromFile() const
788 {
789   SignalToGUI signalOnLoading( _mesh );
790
791   SMESH_PreMeshInfo* meshInfo = _mesh->changePreMeshInfo();
792   _mesh->changePreMeshInfo() = NULL; // to allow GUI accessing to real info
793
794   ::SMESH_Mesh&   mesh = _mesh->GetImpl();
795   SMESHDS_Mesh* meshDS = mesh.GetMeshDS();
796
797   SMESH_TRY;
798
799   MYDEBUGOUT( "BEG FullLoadFromFile() " << _meshID );
800
801   // load mesh
802   DriverMED_R_SMESHDS_Mesh myReader;
803   myReader.SetFile( _medFileName.c_str() );
804   myReader.SetMesh( meshDS );
805   myReader.SetMeshId( _meshID );
806   myReader.Perform();
807
808   // load groups
809   const set<SMESHDS_GroupBase*>& groups = meshDS->GetGroups();
810   set<SMESHDS_GroupBase*>::const_iterator groupIt = groups.begin();
811   for ( ; groupIt != groups.end(); ++groupIt )
812     if ( SMESHDS_Group* aGrp = dynamic_cast<SMESHDS_Group*>( *groupIt ))
813       myReader.GetGroup( aGrp );
814
815   // load sub-meshes
816   readSubMeshes( &myReader );
817
818   SMESH_CATCH( SMESH::doNothing );
819
820   _mesh->changePreMeshInfo() = meshInfo;
821
822   ForgetAllData();
823
824   signalOnLoading.sendStop();
825
826   meshDS->Modified();
827
828   // load dependent meshes referring/referred via hypotheses
829   mesh.GetSubMesh( mesh.GetShapeToMesh() )->
830     ComputeStateEngine (SMESH_subMesh::SUBMESH_LOADED);
831
832   MYDEBUGOUT( "END FullLoadFromFile()" );
833 }
834
835 //================================================================================
836 /*!
837  * \brief Reads full data of sub-meshes
838  */
839 //================================================================================
840
841 void SMESH_PreMeshInfo::readSubMeshes(DriverMED_R_SMESHDS_Mesh* reader) const
842 {
843   HDFfile* aFile = new HDFfile( (char*) _hdfFileName.c_str() );
844   aFile->OpenOnDisk( HDF_RDONLY );
845
846   char meshGrpName[ 30 ];
847   sprintf( meshGrpName, "Mesh %d", _meshID );
848   if ( aFile->ExistInternalObject( meshGrpName ) )
849   {
850     HDFgroup* aTopGroup = new HDFgroup( meshGrpName, aFile );
851     aTopGroup->OpenOnDisk();
852
853     SMESHDS_Mesh* meshDS = _mesh->GetImpl().GetMeshDS();
854
855     // issue 0020693. Restore _isModified flag
856     if ( aTopGroup->ExistInternalObject( "_isModified" ))
857     {
858       HDFdataset* aDataset = new HDFdataset( "_isModified", aTopGroup );
859       aDataset->OpenOnDisk();
860       hdf_size size = aDataset->GetSize();
861       int* isModified = new int[ size ];
862       aDataset->ReadFromDisk( isModified );
863       aDataset->CloseOnDisk();
864       _mesh->GetImpl().SetIsModified( bool(*isModified));
865       delete [] isModified;
866     }
867
868     bool submeshesInFamilies = ( ! aTopGroup->ExistInternalObject( "Submeshes" ));
869     if ( submeshesInFamilies ) // from MED
870     {
871       // old way working before fix of PAL 12992
872       reader->CreateAllSubMeshes();
873     }
874     else
875     {
876       // open a group
877       HDFgroup* aGroup = new HDFgroup( "Submeshes", aTopGroup );
878       aGroup->OpenOnDisk();
879
880       int maxID = Max( meshDS->MaxSubMeshIndex(), meshDS->MaxShapeIndex() );
881       vector< SMESHDS_SubMesh * > subMeshes( maxID + 1, (SMESHDS_SubMesh*) 0 );
882       vector< TopAbs_ShapeEnum  > smType   ( maxID + 1, TopAbs_SHAPE );
883
884       PositionCreator aPositionCreator;
885
886       SMDS_NodeIteratorPtr nIt = meshDS->nodesIterator();
887       SMDS_ElemIteratorPtr eIt = meshDS->elementsIterator();
888       for ( int isNode = 0; isNode < 2; ++isNode )
889       {
890         std::string aDSName( isNode ? "Node Submeshes" : "Element Submeshes");
891         if ( aGroup->ExistInternalObject( (char*) aDSName.c_str() ))
892         {
893           HDFdataset* aDataset = new HDFdataset( (char*) aDSName.c_str(), aGroup );
894           aDataset->OpenOnDisk();
895           // read submesh IDs for all elements sorted by ID
896           size_t nbElems = aDataset->GetSize();
897           int* smIDs = new int [ nbElems ];
898           aDataset->ReadFromDisk( smIDs );
899           aDataset->CloseOnDisk();
900
901           // get elements sorted by ID
902           TIDSortedElemSet elemSet;
903           if ( isNode )
904             while ( nIt->more() ) elemSet.insert( elemSet.end(), nIt->next() );
905           else
906             while ( eIt->more() ) elemSet.insert( elemSet.end(), eIt->next() );
907           //ASSERT( elemSet.size() == nbElems ); -- issue 20182
908           // -- Most probably a bad study was saved when there were
909           // not fixed bugs in SMDS_MeshInfo
910           if ( elemSet.size() < nbElems ) {
911 #ifdef _DEBUG_
912             cout << "SMESH_Gen_i::Load(), warning: Node position data is invalid" << endl;
913 #endif
914             nbElems = elemSet.size();
915           }
916           // add elements to sub-meshes
917           TIDSortedElemSet::iterator iE = elemSet.begin();
918           for ( size_t i = 0; i < nbElems; ++i, ++iE )
919           {
920             int smID = smIDs[ i ];
921             if ( smID == 0 ) continue;
922             const SMDS_MeshElement* elem = *iE;
923             if ( smID > maxID ) {
924               // corresponding subshape no longer exists: maybe geom group has been edited
925               if ( _mesh->GetImpl().HasShapeToMesh() )
926                 meshDS->RemoveElement( elem );
927               continue;
928             }
929             // get or create submesh
930             SMESHDS_SubMesh* & sm = subMeshes[ smID ];
931             if ( ! sm ) {
932               sm = meshDS->NewSubMesh( smID );
933               smType[ smID ] = meshDS->IndexToShape( smID ).ShapeType();
934             }
935             // add
936             if ( isNode ) {
937               SMDS_PositionPtr pos = aPositionCreator.MakePosition( smType[ smID ]);
938               SMDS_MeshNode* node = const_cast<SMDS_MeshNode*>( static_cast<const SMDS_MeshNode*>( elem ));
939               node->SetPosition( pos );
940               sm->AddNode( node );
941             } else {
942               sm->AddElement( elem );
943             }
944           }
945           delete [] smIDs;
946         }
947       }
948     } // end reading submeshes
949
950     // Read node positions on sub-shapes (SMDS_Position)
951
952     if ( aTopGroup->ExistInternalObject( "Node Positions" ))
953     {
954       // There are 5 datasets to read:
955       // "Nodes on Edges" - ID of node on edge
956       // "Edge positions" - U parameter on node on edge
957       // "Nodes on Faces" - ID of node on face
958       // "Face U positions" - U parameter of node on face
959       // "Face V positions" - V parameter of node on face
960       const char* aEid_DSName = "Nodes on Edges";
961       const char* aEu_DSName  = "Edge positions";
962       const char* aFu_DSName  = "Face U positions";
963       //char* aFid_DSName = "Nodes on Faces";
964       //char* aFv_DSName  = "Face V positions";
965
966       // data to retrieve
967       int nbEids = 0, nbFids = 0;
968       int *aEids = 0, *aFids  = 0;
969       double *aEpos = 0, *aFupos = 0, *aFvpos = 0;
970
971       // open a group
972       HDFgroup* aGroup = new HDFgroup( "Node Positions", aTopGroup );
973       aGroup->OpenOnDisk();
974
975       // loop on 5 data sets
976       int aNbObjects = aGroup->nInternalObjects();
977       for ( int i = 0; i < aNbObjects; i++ )
978       {
979         // identify dataset
980         char aDSName[ HDF_NAME_MAX_LEN+1 ];
981         aGroup->InternalObjectIndentify( i, aDSName );
982         // read data
983         HDFdataset* aDataset = new HDFdataset( aDSName, aGroup );
984         aDataset->OpenOnDisk();
985         if ( aDataset->GetType() == HDF_FLOAT64 ) // Positions
986         {
987           double* pos = new double [ aDataset->GetSize() ];
988           aDataset->ReadFromDisk( pos );
989           // which one?
990           if ( strncmp( aDSName, aEu_DSName, strlen( aEu_DSName )) == 0 )
991             aEpos = pos;
992           else if ( strncmp( aDSName, aFu_DSName, strlen( aFu_DSName )) == 0 )
993             aFupos = pos;
994           else
995             aFvpos = pos;
996         }
997         else // NODE IDS
998         {
999           int aSize = aDataset->GetSize();
1000
1001           // for reading files, created from 18.07.2005 till 10.10.2005
1002           if (aDataset->GetType() == HDF_STRING)
1003             aSize /= sizeof(int);
1004
1005           int* ids = new int [aSize];
1006           aDataset->ReadFromDisk( ids );
1007           // on face or nodes?
1008           if ( strncmp( aDSName, aEid_DSName, strlen( aEid_DSName )) == 0 ) {
1009             aEids = ids;
1010             nbEids = aSize;
1011           }
1012           else {
1013             aFids = ids;
1014             nbFids = aSize;
1015           }
1016         }
1017         aDataset->CloseOnDisk();
1018       } // loop on 5 datasets
1019
1020       // Set node positions on edges or faces
1021       for ( int onFace = 0; onFace < 2; onFace++ )
1022       {
1023         int nbNodes = ( onFace ? nbFids : nbEids );
1024         if ( nbNodes == 0 ) continue;
1025         int* aNodeIDs = ( onFace ? aFids : aEids );
1026         double* aUPos = ( onFace ? aFupos : aEpos );
1027         double* aVPos = ( onFace ? aFvpos : 0 );
1028         // loop on node IDs
1029         for ( int iNode = 0; iNode < nbNodes; iNode++ )
1030         {
1031           const SMDS_MeshNode* node = meshDS->FindNode( aNodeIDs[ iNode ]);
1032           if ( !node ) continue; // maybe removed while Loading() if geometry changed
1033           SMDS_PositionPtr aPos = node->GetPosition();
1034           ASSERT( aPos );
1035           if ( onFace ) {
1036             // ASSERT( aPos->GetTypeOfPosition() == SMDS_TOP_FACE );-- issue 20182
1037             // -- Most probably a bad study was saved when there were
1038             // not fixed bugs in SMDS_MeshInfo
1039             if ( aPos->GetTypeOfPosition() == SMDS_TOP_FACE ) {
1040               SMDS_FacePosition* fPos = const_cast<SMDS_FacePosition*>
1041                 ( static_cast<const SMDS_FacePosition*>( aPos ));
1042               fPos->SetUParameter( aUPos[ iNode ]);
1043               fPos->SetVParameter( aVPos[ iNode ]);
1044             }
1045           }
1046           else {
1047             // ASSERT( aPos->GetTypeOfPosition() == SMDS_TOP_EDGE );-- issue 20182
1048             if ( aPos->GetTypeOfPosition() == SMDS_TOP_EDGE ) {
1049               SMDS_EdgePosition* fPos = const_cast<SMDS_EdgePosition*>
1050                 ( static_cast<const SMDS_EdgePosition*>( aPos ));
1051               fPos->SetUParameter( aUPos[ iNode ]);
1052             }
1053           }
1054         }
1055       }
1056       if ( aEids ) delete [] aEids;
1057       if ( aFids ) delete [] aFids;
1058       if ( aEpos ) delete [] aEpos;
1059       if ( aFupos ) delete [] aFupos;
1060       if ( aFvpos ) delete [] aFvpos;
1061
1062       aGroup->CloseOnDisk();
1063
1064     } // if ( aTopGroup->ExistInternalObject( "Node Positions" ) )
1065
1066     aTopGroup->CloseOnDisk();
1067   } // if ( aFile->ExistInternalObject( meshGrpName ) )
1068   
1069   aFile->CloseOnDisk();
1070   delete aFile;
1071 }
1072
1073 //================================================================================
1074 /*!
1075  * \brief Remove all SMESH_PreMeshInfo fields from objects w/o data loading
1076  */
1077 //================================================================================
1078
1079 void SMESH_PreMeshInfo::ForgetAllData() const
1080 {
1081   SMESH_TRY;
1082
1083   if ( _mesh->changePreMeshInfo() != this )
1084     return _mesh->changePreMeshInfo()->ForgetAllData();
1085
1086   // remove SMESH_PreMeshInfo from groups
1087   map<int, SMESH::SMESH_GroupBase_ptr>::const_iterator i2group = _mesh->_mapGroups.begin();
1088   for ( ; i2group != _mesh->_mapGroups.end(); ++i2group )
1089   {
1090     if ( SMESH_GroupBase_i* group_i =
1091          SMESH::DownCast<SMESH_GroupBase_i*>( i2group->second ))
1092     {
1093       SMESH_PreMeshInfo* & info = group_i->changePreMeshInfo();
1094       delete info;
1095       info = NULL;
1096     }
1097   }
1098   // remove SMESH_PreMeshInfo from sub-meshes
1099   map<int, SMESH::SMESH_subMesh_ptr>::iterator id2sm = _mesh->_mapSubMeshIor.begin();
1100   for ( ; id2sm != _mesh->_mapSubMeshIor.end(); ++id2sm )
1101   {
1102     if ( SMESH_subMesh_i* sm_i = SMESH::DownCast<SMESH_subMesh_i*>( id2sm->second ))
1103     {
1104       SMESH_PreMeshInfo* & info = sm_i->changePreMeshInfo();
1105       delete info;
1106       info = NULL;
1107     }
1108   }
1109   // remove SMESH_PreMeshInfo from the mesh
1110   _mesh->changePreMeshInfo() = NULL;
1111   delete this;
1112
1113   SMESH_CATCH( SMESH::doNothing );
1114
1115
1116   // Finalize loading
1117
1118   // SMESH_TRY;
1119
1120   // ::SMESH_Mesh& mesh = _mesh->GetImpl();
1121
1122   // // update hyps needing full mesh data restored (issue 20918)
1123   // // map<int, SMESH::SMESH_Hypothesis_ptr>::iterator id2hyp= _mesh->_mapHypo.begin();
1124   // // for ( ; id2hyp != _mesh->_mapHypo.end(); ++id2hyp )
1125   // //   if ( SMESH_Hypothesis_i* hyp = SMESH::DownCast<SMESH_Hypothesis_i*>( id2hyp->second ))
1126   // //     hyp->UpdateAsMeshesRestored();
1127
1128
1129   // SMESH_CATCH( SMESH::doNothing );
1130 }
1131
1132 //================================================================================
1133 /*!
1134  * \brief remove all SMESH_PreMeshInfo fields from mesh and its child objects w/o data loading
1135  */
1136 //================================================================================
1137
1138 void SMESH_PreMeshInfo::ForgetAllData( SMESH_Mesh_i* mesh )
1139 {
1140   if ( mesh && mesh->changePreMeshInfo() )
1141     mesh->changePreMeshInfo()->ForgetAllData();
1142 }
1143
1144 //================================================================================
1145 /*!
1146  * \brief Calls either FullLoadFromFile() or ForgetAllData() depending on preferences
1147  */
1148 //================================================================================
1149
1150 void SMESH_PreMeshInfo::ForgetOrLoad() const
1151 {
1152   if ( SMESH_Gen_i::GetSMESHGen()->ToForgetMeshDataOnHypModif() &&
1153        _mesh->HasShapeToMesh())
1154     ForgetAllData();
1155   else
1156     FullLoadFromFile();
1157 }
1158
1159 //================================================================================
1160 /*!
1161  * \brief Method of SMESH_IDSource interface
1162  */
1163 //================================================================================
1164
1165 SMESH::array_of_ElementType* SMESH_PreMeshInfo::GetTypes() const
1166 {
1167   SMESH::array_of_ElementType_var types = new SMESH::array_of_ElementType;
1168
1169   types->length( 4 );
1170   int nbTypes = 0;
1171   if (NbEdges())      types[nbTypes++] = SMESH::EDGE;
1172   if (NbFaces())      types[nbTypes++] = SMESH::FACE;
1173   if (NbVolumes())    types[nbTypes++] = SMESH::VOLUME;
1174   if (Nb0DElements()) types[nbTypes++] = SMESH::ELEM0D;
1175   if (NbBalls())      types[nbTypes++] = SMESH::BALL;
1176   types->length( nbTypes );
1177
1178   return types._retn();
1179 }
1180
1181 //================================================================================
1182 /*!
1183  * \brief Method of SMESH_IDSource interface returning nb elements by element type
1184  */
1185 //================================================================================
1186
1187 SMESH::long_array* SMESH_PreMeshInfo::GetMeshInfo() const
1188 {
1189   SMESH::long_array_var aRes = new SMESH::long_array();
1190   aRes->length(SMESH::Entity_Last);
1191   for (int i = SMESH::Entity_Node; i < SMESH::Entity_Last; i++)
1192     aRes[i] = 0;
1193
1194   for (int i = SMESH::Entity_Node; i < SMESH::Entity_Last; i++)
1195     aRes[i] = NbEntities((SMDSAbs_EntityType)i);
1196   return aRes._retn();
1197 }
1198
1199 //================================================================================
1200 /*!
1201  * Returns false if GetMeshInfo() returns incorrect information that may
1202  * happen if mesh data is not yet fully loaded from the file of study.
1203  */
1204 //================================================================================
1205
1206 bool SMESH_PreMeshInfo::IsMeshInfoCorrect() const
1207 {
1208   return _isInfoOk;
1209 }
1210
1211 //================================================================================
1212 /*!
1213  * \brief TEMPORARY method to remove study files on closing study;
1214  * RIGHT WAY: study files are remove automatically when meshes are destroyed
1215  */
1216 //================================================================================
1217
1218 void SMESH_PreMeshInfo::RemoveStudyFiles_TMP_METHOD(SALOMEDS::SComponent_ptr smeshComp)
1219 {
1220   if ( theMeshCounter > 0 )
1221   {
1222     SALOMEDS::ChildIterator_wrap itBig = SMESH_Gen_i::getStudyServant()->NewChildIterator( smeshComp );
1223     for ( ; itBig->More(); itBig->Next() ) {
1224       SALOMEDS::SObject_wrap gotBranch = itBig->Value();
1225       CORBA::Object_var       anObject = SMESH_Gen_i::SObjectToObject( gotBranch );
1226       if ( SMESH_Mesh_i* mesh = SMESH::DownCast<SMESH_Mesh_i*>( anObject ))
1227       {
1228         if ( mesh->changePreMeshInfo() )
1229         {
1230           mesh->changePreMeshInfo()->ForgetAllData();
1231         }
1232       }
1233     }
1234   }
1235 }