1 // Copyright (C) 2007-2016 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // File : SMESH_PreMeshInfo.cxx
23 // Created : Fri Feb 10 17:36:39 2012
24 // Author : Edward AGAPOV (eap)
27 #include "SMESH_PreMeshInfo.hxx"
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"
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>
51 #include <TopoDS_Iterator.hxx>
52 #include <TopoDS_Shape.hxx>
54 #include "SMESH_TryCatch.hxx"
56 #include CORBA_SERVER_HEADER(SALOME_Session)
60 #define MYDEBUGOUT(msg) //std::cout << msg << std::endl;
64 enum { GroupOnFilter_OutOfDate = -1 };
66 // a map to count not yet loaded meshes
67 static std::map< int, int > theStudyIDToMeshCounter;
69 //================================================================================
71 * \brief Counts not fully loaded meshes
73 //================================================================================
75 void meshInfoLoaded( SMESH_Mesh_i* mesh )
77 std::map< int, int >::iterator id2counter =
78 theStudyIDToMeshCounter.insert( std::make_pair( (int) mesh->GetStudyId(), 0 )).first;
81 //================================================================================
83 * \brief Removes temporary files if none of meshes needs them
85 //================================================================================
87 void filesNoMoreNeeded(SMESH_Mesh_i* mesh,
91 if ( --theStudyIDToMeshCounter[ (int) mesh->GetStudyId() ] == 0 )
93 std::string tmpDir = SALOMEDS_Tool::GetDirFromPath( hdfFile );
95 SALOMEDS::ListOfFileNames_var aFiles = new SALOMEDS::ListOfFileNames;
97 medFile = SALOMEDS_Tool::GetNameFromPath( medFile ) + ".med";
98 hdfFile = SALOMEDS_Tool::GetNameFromPath( hdfFile ) + ".hdf";
99 aFiles[0] = medFile.c_str();
100 aFiles[1] = hdfFile.c_str();
102 SALOMEDS_Tool::RemoveTemporaryFiles( tmpDir.c_str(), aFiles.in(), true );
106 //=============================================================================
108 * \brief Class sending signals on start and finish of loading
110 //=============================================================================
114 std::string _messagePrefix;
115 SALOME::Session_var _session;
117 SignalToGUI( SMESH_Mesh_i* mesh )
119 SMESH_Gen_i* gen = SMESH_Gen_i::GetSMESHGen();
120 SALOMEDS::Study_var study = gen->GetCurrentStudy();
121 if ( !study->_is_nil() && study->StudyId() == mesh->GetStudyId() )
123 SALOMEDS::SObject_wrap meshSO = gen->ObjectToSObject(study, mesh->_this() );
124 CORBA::Object_var obj = gen->GetNS()->Resolve( "/Kernel/Session" );
125 _session = SALOME::Session::_narrow( obj );
126 if ( !meshSO->_is_nil() && !_session->_is_nil() )
128 CORBA::String_var meshEntry = meshSO->GetID();
129 _messagePrefix = "SMESH/mesh_loading/";
130 _messagePrefix += meshEntry.in();
132 std::string msgToGUI = _messagePrefix + "/";
133 msgToGUI += SMESH_Comment( mesh->NbNodes() );
135 msgToGUI += SMESH_Comment( mesh->NbElements() );
137 _session->emitMessageOneWay( msgToGUI.c_str());
143 if ( !_messagePrefix.empty() )
145 std::string msgToGUI = _messagePrefix + "/stop";
146 _session->emitMessageOneWay( msgToGUI.c_str());
147 _messagePrefix.clear();
150 ~SignalToGUI() { sendStop(); }
153 //=============================================================================
155 * \brief Creates SMDS_Position according to shape type
157 //=============================================================================
159 class PositionCreator {
161 SMDS_PositionPtr MakePosition(const TopAbs_ShapeEnum type) {
162 return (this->*myFuncTable[ type ])();
165 myFuncTable.resize( (size_t) TopAbs_SHAPE, & PositionCreator::defaultPosition );
166 myFuncTable[ TopAbs_SOLID ] = & PositionCreator::volumePosition;
167 myFuncTable[ TopAbs_FACE ] = & PositionCreator::facePosition;
168 myFuncTable[ TopAbs_EDGE ] = & PositionCreator::edgePosition;
169 myFuncTable[ TopAbs_VERTEX ] = & PositionCreator::vertexPosition;
172 SMDS_PositionPtr edgePosition() const { return SMDS_PositionPtr( new SMDS_EdgePosition ); }
173 SMDS_PositionPtr facePosition() const { return SMDS_PositionPtr( new SMDS_FacePosition ); }
174 SMDS_PositionPtr volumePosition() const { return SMDS_PositionPtr( new SMDS_SpacePosition ); }
175 SMDS_PositionPtr vertexPosition() const { return SMDS_PositionPtr( new SMDS_VertexPosition); }
176 SMDS_PositionPtr defaultPosition() const { return SMDS_SpacePosition::originSpacePosition(); }
177 typedef SMDS_PositionPtr (PositionCreator:: * FmakePos)() const;
178 std::vector<FmakePos> myFuncTable;
181 //================================================================================
183 * \brief Returns ids of simple shapes composing a complex one
185 //================================================================================
187 std::vector<int> getSimpleSubMeshIds( SMESHDS_Mesh* meshDS, int shapeId )
189 std::vector<int> ids;
191 std::list<TopoDS_Shape> shapeQueue( 1, meshDS->IndexToShape( shapeId ));
192 std::list<TopoDS_Shape>::iterator shape = shapeQueue.begin();
193 for ( ; shape != shapeQueue.end(); ++shape )
195 if ( shape->IsNull() ) continue;
196 if ( shape->ShapeType() == TopAbs_COMPOUND ||
197 shape->ShapeType() == TopAbs_COMPSOLID )
199 for ( TopoDS_Iterator it( *shape ); it.More(); it.Next() )
200 shapeQueue.push_back( it.Value() );
204 ids.push_back( meshDS->ShapeToIndex( *shape ));
210 //================================================================================
212 * \brief Return EEntiteMaillage by EGeometrieElement
214 //================================================================================
216 MED::EEntiteMaillage entityByGeom(const MED::EGeometrieElement geom )
218 return geom == MED::eBALL ? MED::eSTRUCT_ELEMENT : MED::eMAILLE;
221 //================================================================================
223 * \brief Return a map< EGeometrieElement, SMDSAbs_EntityType >
225 //================================================================================
227 typedef std::map< MED::EGeometrieElement, SMDSAbs_EntityType > Tmed2smeshElemTypeMap;
228 const Tmed2smeshElemTypeMap& med2smeshElemTypeMap()
230 static Tmed2smeshElemTypeMap med2smeshTypes;
231 if ( med2smeshTypes.empty() )
233 for ( int iG = 0; iG < SMDSEntity_Last; ++iG )
235 SMDSAbs_EntityType smdsType = (SMDSAbs_EntityType) iG;
236 MED::EGeometrieElement medType =
237 (MED::EGeometrieElement) DriverMED::GetMedGeoType( smdsType );
238 med2smeshTypes.insert( std::make_pair( medType, smdsType ));
241 return med2smeshTypes;
244 //================================================================================
246 * \brief Writes meshInfo into a HDF file
248 //================================================================================
250 void meshInfo2hdf( SMESH::long_array_var meshInfo,
251 const std::string& name,
254 // we use med identification of element (MED::EGeometrieElement) types
255 // but not enum SMDSAbs_EntityType because values of SMDSAbs_EntityType may
256 // change at insertion of new items in the middle.
257 //const vector<MED::EGeometrieElement>& medTypes = mesh2medElemType();
259 std::vector<int> data;
261 for ( size_t i = 0; i < meshInfo->length(); ++i )
262 if ( meshInfo[i] > 0 )
264 data.push_back( DriverMED::GetMedGeoType( SMDSAbs_EntityType( i ))); //medTypes[ i ] );
265 data.push_back( meshInfo[ i ] );
270 hdf_size datasetSize[] = { data.size() };
271 HDFarray* anArray = new HDFarray(0, HDF_INT32, 1, datasetSize);
272 anArray->CreateOnDisk();
274 HDFdataset* dataset = new HDFdataset( name.c_str(), hdfGroup, HDF_ARRAY, datasetSize, 1 );
275 dataset->SetArrayId(anArray->GetId());
276 dataset->CreateOnDisk();
277 dataset->WriteOnDisk( & data[0] );
278 dataset->CloseOnDisk();
279 anArray->CloseOnDisk();
284 //================================================================================
286 * \brief Reads meshInfo from a HDF file
288 //================================================================================
290 void SMESH_PreMeshInfo::hdf2meshInfo( const std::string& name,
293 if ( hdfGroup->ExistInternalObject( name.c_str()) )
295 HDFdataset* dataset = new HDFdataset( name.c_str(), hdfGroup );
296 dataset->OpenOnDisk();
298 // // hdf_size datasetSize[ 1 ];
299 // // HDFarray *array = new HDFarray(dataset);
300 // // array->GetDim( datasetSize );
301 // int size = dataset->GetSize();
303 std::vector<int> info( SMDSEntity_Last * 2, 0 );
304 dataset->ReadFromDisk( &info[0] );
305 dataset->CloseOnDisk();
307 const Tmed2smeshElemTypeMap& med2smesh = med2smeshElemTypeMap();
308 Tmed2smeshElemTypeMap::const_iterator me2sme, me2smeEnd = med2smesh.end();
309 for ( size_t i = 0; i < info.size(); )
311 int medType = info[i++];
312 int nbElems = info[i++];
313 if ( !nbElems ) break;
314 me2sme = med2smesh.find( (MED::EGeometrieElement) medType );
315 if ( me2sme != me2smeEnd )
316 setNb( me2sme->second, nbElems );
321 if ( NbNodes() == GroupOnFilter_OutOfDate ) // case of !SMESHDS_GroupOnFilter::IsUpToDate()
324 setNb( SMDSEntity_Node, 0 );
328 //================================================================================
330 * \brief Constructor callable by SMESH_PreMeshInfo only
332 //================================================================================
334 SMESH_PreMeshInfo::SMESH_PreMeshInfo(SMESH_Mesh_i* mesh,
336 const std::string& medFile,
337 const std::string& hdfFile)
338 : _medFileName( medFile ),
339 _hdfFileName( hdfFile ),
340 _toRemoveFiles( false ),
348 //================================================================================
350 * \brief Release temporary files
352 //================================================================================
354 SMESH_PreMeshInfo::~SMESH_PreMeshInfo()
356 if ( _toRemoveFiles ) // it can be true only for SMESH_PreMeshInfo of the mesh
357 filesNoMoreNeeded( _mesh, _medFileName, _hdfFileName );
359 _toRemoveFiles = false;
362 //================================================================================
364 * \brief fills SMESH_PreMeshInfo field of all objects of mesh
366 //================================================================================
368 void SMESH_PreMeshInfo::LoadFromFile( SMESH_Mesh_i* mesh,
370 const std::string& medFile,
371 const std::string& hdfFile,
372 const bool toRemoveFiles)
376 SMESH_PreMeshInfo* meshPreInfo = new SMESH_PreMeshInfo( mesh,meshID,medFile,hdfFile );
377 mesh->changePreMeshInfo() = meshPreInfo;
379 meshPreInfo->_toRemoveFiles = toRemoveFiles;
381 meshInfoLoaded( mesh );
383 if ( meshPreInfo->readPreInfoFromHDF() )
384 // all SMESH_PreMeshInfo's are stored in HDF file (written after
385 // implementing SMESH_PreMeshInfo)
388 // try to read SMESH_PreMeshInfo from med file (as study is older than SMESH_PreMeshInfo)
389 if ( meshPreInfo->readMeshInfo() )
391 meshPreInfo->readGroupInfo();
392 meshPreInfo->readSubMeshInfo();
396 meshPreInfo->FullLoadFromFile();
398 SMESH_CATCH( SMESH::doNothing );
401 //================================================================================
403 * \brief Tries to read all SMESH_PreMeshInfo from a HDF file
404 * \retval bool - true if succeeded
406 * This method is symmetrical to SaveToFile()
408 //================================================================================
410 bool SMESH_PreMeshInfo::readPreInfoFromHDF()
412 HDFfile* aFile = new HDFfile( (char*) _hdfFileName.c_str() );
413 aFile->OpenOnDisk( HDF_RDONLY );
415 SMESH_Comment hdfGroupName("SMESH_PreMeshInfo"); hdfGroupName << _meshID;
416 const bool infoAvailable = aFile->ExistInternalObject( hdfGroupName );
419 HDFgroup* infoHdfGroup = new HDFgroup( hdfGroupName, aFile );
420 infoHdfGroup->OpenOnDisk();
422 _mesh->changePreMeshInfo()->hdf2meshInfo( "Mesh", infoHdfGroup );
424 // read SMESH_PreMeshInfo of groups
425 map<int, SMESH::SMESH_GroupBase_ptr>::const_iterator i2group = _mesh->_mapGroups.begin();
426 for ( ; i2group != _mesh->_mapGroups.end(); ++i2group )
428 if ( SMESH_GroupBase_i* group_i =
429 SMESH::DownCast<SMESH_GroupBase_i*>( i2group->second ))
431 group_i->changePreMeshInfo() = newInstance();
432 if ( SMESHDS_GroupBase* group = group_i->GetGroupDS() )
434 const std::string name = group->GetStoreName();
435 group_i->changePreMeshInfo()->hdf2meshInfo( name, infoHdfGroup );
440 // read SMESH_PreMeshInfo of sub-meshes
441 map<int, SMESH::SMESH_subMesh_ptr>::iterator id2sm = _mesh->_mapSubMeshIor.begin();
442 for ( ; id2sm != _mesh->_mapSubMeshIor.end(); ++id2sm )
444 if ( SMESH_subMesh_i* sm = SMESH::DownCast<SMESH_subMesh_i*>( id2sm->second ))
446 sm->changePreMeshInfo() = newInstance();
447 sm->changePreMeshInfo()->hdf2meshInfo( SMESH_Comment( sm->GetId()), infoHdfGroup );
452 aFile->CloseOnDisk();
455 return infoAvailable;
458 //================================================================================
460 * \brief Reads mesh info of mesh from the med file
462 //================================================================================
464 bool SMESH_PreMeshInfo::readMeshInfo()
468 MED::PWrapper aMed = MED::CrWrapper(_medFileName,true);
469 // if ( aMed->GetVersion() != MED::eV2_2 )
472 MED::PMeshInfo medMeshInfo = aMed->CrMeshInfo(3,3,SMESH_Comment( _meshID ));
475 int nbNodes = std::max( 0, aMed->GetNbNodes( medMeshInfo ));
478 setNb( SMDSEntity_Node, nbNodes);
480 // read nb of elements
481 Tmed2smeshElemTypeMap::const_iterator me2sme = med2smeshElemTypeMap().begin();
482 Tmed2smeshElemTypeMap::const_iterator me2smeEnd = med2smeshElemTypeMap().end();
483 for ( ; me2sme != me2smeEnd; ++me2sme )
485 int nbElems = aMed->GetNbCells( medMeshInfo, entityByGeom(me2sme->first), me2sme->first );
487 setNb( me2sme->second, nbElems );
493 //================================================================================
495 * \brief Reads info of groups from the med file
497 //================================================================================
499 void SMESH_PreMeshInfo::readGroupInfo()
501 if ( _mesh->_mapGroups.empty() ) return;
503 // make SMESH_PreMeshInfo of groups
504 map< std::string, SMESH_PreMeshInfo* > name2GroupInfo;
505 map<int, SMESH::SMESH_GroupBase_ptr>::const_iterator i2group = _mesh->_mapGroups.begin();
506 for ( ; i2group != _mesh->_mapGroups.end(); ++i2group )
508 if ( SMESH_GroupBase_i* group_i =
509 SMESH::DownCast<SMESH_GroupBase_i*>( i2group->second ))
511 SMESH_PreMeshInfo* info = newInstance();
512 group_i->changePreMeshInfo() = info;
513 if ( SMESHDS_Group* group = dynamic_cast< SMESHDS_Group* >( group_i->GetGroupDS() ))
515 std::string name = group->GetStoreName();
516 name2GroupInfo.insert( std::make_pair( name, info ));
517 info->_isInfoOk = true;
522 map< int, vector< SMESH_PreMeshInfo* > > famId2grInfo;
524 MED::PWrapper aMed = MED::CrWrapper(_medFileName,false);
525 MED::PMeshInfo medMeshInfo = aMed->CrMeshInfo(3,3,SMESH_Comment( _meshID ));
527 // read families to fill in famId2grInfo
528 int nbFams = aMed->GetNbFamilies( medMeshInfo );
529 if ( nbFams <= 1 ) return; // zero family is always present
530 for ( int iF = 0; iF <= nbFams; ++iF )
532 int nbGroups = aMed->GetNbFamGroup( iF, medMeshInfo );
533 if ( nbGroups < 1 ) continue;
534 MED::PFamilyInfo medFamInfo = aMed->CrFamilyInfo( medMeshInfo, nbGroups, nbGroups );
535 aMed->GetFamilyInfo( iF, medFamInfo ); // read groups of a family
536 vector< SMESH_PreMeshInfo* >& grInfoVec = famId2grInfo[ medFamInfo->GetId() ];
537 for ( int iG = 0; iG < nbGroups; ++iG )
539 const std::string grName = medFamInfo->GetGroupName( iG );
540 map< std::string, SMESH_PreMeshInfo* >::iterator n2i = name2GroupInfo.find( grName );
541 if ( n2i != name2GroupInfo.end() )
542 grInfoVec.push_back( n2i->second );
546 // read family numbers of elements
547 Tmed2smeshElemTypeMap::const_iterator me2sme = med2smeshElemTypeMap().begin();
548 Tmed2smeshElemTypeMap::const_iterator me2smeEnd = med2smeshElemTypeMap().end();
549 MED::PElemInfo medElemInfo = aMed->CrElemInfo( medMeshInfo, 0 );
550 MED::TIntVector& famNums = medElemInfo->myFamNum;
551 for ( ; me2sme != me2smeEnd; ++me2sme ) // loop on elem types
553 famNums.resize( NbEntities( me2sme->second ));
554 if ( famNums.empty() ) continue;
555 aMed->GetFamilies( medElemInfo, famNums.size(), entityByGeom(me2sme->first), me2sme->first );
556 // distribute elements of a type among groups
557 map< int, vector< SMESH_PreMeshInfo* > >::iterator f2infos = famId2grInfo.begin();
558 for ( size_t i = 0; i < famNums.size(); ++i )
560 if ( famNums[i] != f2infos->first )
562 f2infos = famId2grInfo.find( famNums[i] );
563 if ( f2infos == famId2grInfo.end() )
564 f2infos = famId2grInfo.insert
565 ( std::make_pair( famNums[i], vector< SMESH_PreMeshInfo*>())).first;
567 vector< SMESH_PreMeshInfo* >& infoVec = f2infos->second ;
568 for ( size_t j = 0; j < infoVec.size(); ++j )
569 infoVec[j]->_elemCounter++;
571 // pass _elemCounter to a real elem type
572 map< std::string, SMESH_PreMeshInfo* >::iterator n2i = name2GroupInfo.begin();
573 for ( ; n2i != name2GroupInfo.end(); ++n2i )
575 SMESH_PreMeshInfo* info = n2i->second;
576 info->setNb( me2sme->second, info->_elemCounter );
577 info->_elemCounter = 0;
582 //================================================================================
584 * \brief Reads info of sub-meshes from hdf file of old study
586 //================================================================================
588 void SMESH_PreMeshInfo::readSubMeshInfo()
590 if ( _mesh->_mapSubMeshIor.empty() ) return;
592 // create SMESH_PreMeshInfo of sub-meshes
593 map<int, SMESH::SMESH_subMesh_ptr>::iterator id2sm = _mesh->_mapSubMeshIor.begin();
594 for ( ; id2sm != _mesh->_mapSubMeshIor.end(); ++id2sm )
596 if ( SMESH_subMesh_i* sm = SMESH::DownCast<SMESH_subMesh_i*>( id2sm->second ))
598 sm->changePreMeshInfo() = newInstance();
599 sm->changePreMeshInfo()->_isInfoOk = true;
604 HDFfile* aFile = new HDFfile( (char*) _hdfFileName.c_str() );
605 aFile->OpenOnDisk( HDF_RDONLY );
607 char meshGrpName[ 30 ];
608 sprintf( meshGrpName, "Mesh %d", _meshID );
609 if ( aFile->ExistInternalObject( meshGrpName ) )
611 HDFgroup* aTopGroup = new HDFgroup( meshGrpName, aFile );
612 aTopGroup->OpenOnDisk();
613 if ( aTopGroup->ExistInternalObject( "Submeshes" ))
615 HDFgroup* aGroup = new HDFgroup( "Submeshes", aTopGroup );
616 aGroup->OpenOnDisk();
618 SMESHDS_Mesh* meshDS = _mesh->GetImpl().GetMeshDS();
619 int maxSmId = Max( meshDS->MaxSubMeshIndex(), meshDS->MaxShapeIndex() );
621 for ( int isNode = 0; isNode < 2; ++isNode )
623 std::string aDSName( isNode ? "Node Submeshes" : "Element Submeshes");
624 if ( aGroup->ExistInternalObject( (char*) aDSName.c_str() ))
626 // read sub-mesh id of all nodes or elems
627 HDFdataset* aDataset = new HDFdataset( (char*) aDSName.c_str(), aGroup );
628 aDataset->OpenOnDisk();
629 int nbElems = aDataset->GetSize();
630 int* smIDs = new int [ nbElems ];
631 aDataset->ReadFromDisk( smIDs );
632 aDataset->CloseOnDisk();
633 // count nb elems in each sub-mesh
634 vector<int> nbBySubmeshId( maxSmId + 1, 0 );
635 for ( int i = 0; i < nbElems; ++i )
637 const int smID = smIDs[ i ];
638 if ( smID < (int) nbBySubmeshId.size() )
639 nbBySubmeshId[ smID ]++;
643 // store nb elems in SMESH_PreMeshInfo of sub-meshes
644 map<int, SMESH::SMESH_subMesh_ptr>::iterator id2sm = _mesh->_mapSubMeshIor.begin();
645 for ( ; id2sm != _mesh->_mapSubMeshIor.end(); ++id2sm )
647 if ( SMESH_subMesh_i* sm = SMESH::DownCast<SMESH_subMesh_i*>( id2sm->second ))
649 SMESH_PreMeshInfo* & info = sm->changePreMeshInfo();
651 vector<int> smIds = getSimpleSubMeshIds( meshDS, id2sm->first );
652 for ( size_t i = 0; i < smIds.size(); ++i )
653 info->_elemCounter += nbBySubmeshId[ smIds[i] ];
655 SMDSAbs_EntityType elemType;
658 elemType = SMDSEntity_Node;
662 bool koElemType = false;
663 const TopoDS_Shape& shape = meshDS->IndexToShape( smIds[0] );
664 elemType = getElemType( shape.ShapeType(), info->_elemCounter, koElemType );
665 info->_isInfoOk = !koElemType;
667 info->setNb( elemType, info->_elemCounter );
670 } // if ( aGroup->ExistInternalObject( aDSName ))
671 } // for ( int isNode = 0; isNode < 2; ++isNode )
673 aGroup->CloseOnDisk();
674 } // if ( aTopGroup->ExistInternalObject( "Submeshes" ))
676 aTopGroup->CloseOnDisk();
677 } // if ( aFile->ExistInternalObject( meshGrpName ) )
679 aFile->CloseOnDisk();
683 //================================================================================
685 * \brief Return type of element for sub-mesh on a shape of given type
687 //================================================================================
689 SMDSAbs_EntityType SMESH_PreMeshInfo::getElemType( const TopAbs_ShapeEnum shapeType,
690 const int nbElemsInSubMesh,
691 bool& isKoType) const
695 SMESH_PreMeshInfo* meshInfo = _mesh->changePreMeshInfo();
700 type = SMDSEntity_Tetra;
701 typeEnd = SMDSEntity_Last;
702 isKoType = ( meshInfo->NbVolumes() != nbElemsInSubMesh );
706 type = SMDSEntity_Triangle;
707 typeEnd = SMDSEntity_Tetra;
708 isKoType = ( meshInfo->NbFaces() != nbElemsInSubMesh );
711 case TopAbs_EDGE: return SMDSEntity_Edge;
712 case TopAbs_VERTEX: return SMDSEntity_0D;
713 default: return SMDSEntity_Last;
718 for ( int t = type; t < typeEnd; ++t )
719 if ( nbElemsInSubMesh == meshInfo->NbEntities( SMDSAbs_EntityType( t )))
720 return SMDSAbs_EntityType( t );
723 return SMDSAbs_EntityType( type );
726 //================================================================================
728 * \brief Saves SMESH_PreMeshInfo to the study file
730 //================================================================================
732 void SMESH_PreMeshInfo::SaveToFile( SMESH_Mesh_i* mesh,
736 // create a HDF group for SMESH_PreMeshInfo of this mesh
737 SMESH_Comment hdfGroupName("SMESH_PreMeshInfo"); hdfGroupName << meshID;
738 HDFgroup* infoHdfGroup = new HDFgroup( hdfGroupName, hdfFile );
739 infoHdfGroup->CreateOnDisk();
744 meshInfo2hdf( mesh->GetMeshInfo(), "Mesh", infoHdfGroup );
747 SMESH_PreMeshInfo incompleteInfo( 0,0,"","");
748 incompleteInfo.setNb( SMDSEntity_Node, GroupOnFilter_OutOfDate );
749 SMESHDS_Mesh* meshDS = mesh->GetImpl().GetMeshDS();
751 map<int, SMESH::SMESH_GroupBase_ptr>::const_iterator i2group = mesh->_mapGroups.begin();
752 for ( ; i2group != mesh->_mapGroups.end(); ++i2group )
754 if ( SMESH_GroupBase_i* group_i = SMESH::DownCast<SMESH_GroupBase_i*>( i2group->second ))
756 SMESHDS_GroupBase * group = group_i->GetGroupDS();
757 if ( SMESHDS_GroupOnFilter* gof = dynamic_cast<SMESHDS_GroupOnFilter*>(group))
759 // prevent too long storage time due to applying filter to many elements
760 if ( !gof->IsUpToDate() && meshDS->GetMeshInfo().NbElements( gof->GetType() ) > 1e5 )
762 meshInfo2hdf( incompleteInfo.GetMeshInfo(),
763 group->GetStoreName(),
768 meshInfo2hdf( group_i->GetMeshInfo(), group->GetStoreName(), infoHdfGroup);
772 // info of sub-meshes
773 map<int, SMESH::SMESH_subMesh_ptr>::iterator id2sm = mesh->_mapSubMeshIor.begin();
774 for ( ; id2sm != mesh->_mapSubMeshIor.end(); ++id2sm )
776 if ( SMESH_subMesh_i* sm = SMESH::DownCast<SMESH_subMesh_i*>( id2sm->second ))
778 meshInfo2hdf( sm->GetMeshInfo(),
779 SMESH_Comment( sm->GetId() ),
784 SMESH_CATCH( SMESH::doNothing );
786 infoHdfGroup->CloseOnDisk();
789 //================================================================================
791 * \brief Reads all data and remove all SMESH_PreMeshInfo fields from objects
793 //================================================================================
795 void SMESH_PreMeshInfo::FullLoadFromFile() const
797 SignalToGUI signalOnLoading( _mesh );
799 SMESH_PreMeshInfo* meshInfo = _mesh->changePreMeshInfo();
800 _mesh->changePreMeshInfo() = NULL; // to allow GUI accessing to real info
802 ::SMESH_Mesh& mesh = _mesh->GetImpl();
803 SMESHDS_Mesh* meshDS = mesh.GetMeshDS();
807 MYDEBUGOUT( "BEG FullLoadFromFile() " << _meshID );
810 DriverMED_R_SMESHDS_Mesh myReader;
811 myReader.SetFile( _medFileName.c_str() );
812 myReader.SetMesh( meshDS );
813 myReader.SetMeshId( _meshID );
817 const set<SMESHDS_GroupBase*>& groups = meshDS->GetGroups();
818 set<SMESHDS_GroupBase*>::const_iterator groupIt = groups.begin();
819 for ( ; groupIt != groups.end(); ++groupIt )
820 if ( SMESHDS_Group* aGrp = dynamic_cast<SMESHDS_Group*>( *groupIt ))
821 myReader.GetGroup( aGrp );
824 readSubMeshes( &myReader );
826 SMESH_CATCH( SMESH::doNothing );
828 _mesh->changePreMeshInfo() = meshInfo;
832 signalOnLoading.sendStop();
836 // load dependent meshes referring/referred via hypotheses
837 mesh.GetSubMesh( mesh.GetShapeToMesh() )->
838 ComputeStateEngine (SMESH_subMesh::SUBMESH_LOADED);
840 MYDEBUGOUT( "END FullLoadFromFile()" );
843 //================================================================================
845 * \brief Reads full data of sub-meshes
847 //================================================================================
849 void SMESH_PreMeshInfo::readSubMeshes(DriverMED_R_SMESHDS_Mesh* reader) const
851 HDFfile* aFile = new HDFfile( (char*) _hdfFileName.c_str() );
852 aFile->OpenOnDisk( HDF_RDONLY );
854 char meshGrpName[ 30 ];
855 sprintf( meshGrpName, "Mesh %d", _meshID );
856 if ( aFile->ExistInternalObject( meshGrpName ) )
858 HDFgroup* aTopGroup = new HDFgroup( meshGrpName, aFile );
859 aTopGroup->OpenOnDisk();
861 SMESHDS_Mesh* meshDS = _mesh->GetImpl().GetMeshDS();
863 // issue 0020693. Restore _isModified flag
864 if ( aTopGroup->ExistInternalObject( "_isModified" ))
866 HDFdataset* aDataset = new HDFdataset( "_isModified", aTopGroup );
867 aDataset->OpenOnDisk();
868 hdf_size size = aDataset->GetSize();
869 int* isModified = new int[ size ];
870 aDataset->ReadFromDisk( isModified );
871 aDataset->CloseOnDisk();
872 _mesh->GetImpl().SetIsModified( bool(*isModified));
873 delete [] isModified;
876 bool submeshesInFamilies = ( ! aTopGroup->ExistInternalObject( "Submeshes" ));
877 if ( submeshesInFamilies ) // from MED
879 // old way working before fix of PAL 12992
880 reader->CreateAllSubMeshes();
885 HDFgroup* aGroup = new HDFgroup( "Submeshes", aTopGroup );
886 aGroup->OpenOnDisk();
888 int maxID = Max( meshDS->MaxSubMeshIndex(), meshDS->MaxShapeIndex() );
889 vector< SMESHDS_SubMesh * > subMeshes( maxID + 1, (SMESHDS_SubMesh*) 0 );
890 vector< TopAbs_ShapeEnum > smType ( maxID + 1, TopAbs_SHAPE );
892 PositionCreator aPositionCreator;
894 SMDS_NodeIteratorPtr nIt = meshDS->nodesIterator();
895 SMDS_ElemIteratorPtr eIt = meshDS->elementsIterator();
896 for ( int isNode = 0; isNode < 2; ++isNode )
898 std::string aDSName( isNode ? "Node Submeshes" : "Element Submeshes");
899 if ( aGroup->ExistInternalObject( (char*) aDSName.c_str() ))
901 HDFdataset* aDataset = new HDFdataset( (char*) aDSName.c_str(), aGroup );
902 aDataset->OpenOnDisk();
903 // read submesh IDs for all elements sorted by ID
904 size_t nbElems = aDataset->GetSize();
905 int* smIDs = new int [ nbElems ];
906 aDataset->ReadFromDisk( smIDs );
907 aDataset->CloseOnDisk();
909 // get elements sorted by ID
910 TIDSortedElemSet elemSet;
912 while ( nIt->more() ) elemSet.insert( elemSet.end(), nIt->next() );
914 while ( eIt->more() ) elemSet.insert( elemSet.end(), eIt->next() );
915 //ASSERT( elemSet.size() == nbElems ); -- issue 20182
916 // -- Most probably a bad study was saved when there were
917 // not fixed bugs in SMDS_MeshInfo
918 if ( elemSet.size() < nbElems ) {
920 cout << "SMESH_Gen_i::Load(), warning: Node position data is invalid" << endl;
922 nbElems = elemSet.size();
924 // add elements to sub-meshes
925 TIDSortedElemSet::iterator iE = elemSet.begin();
926 for ( size_t i = 0; i < nbElems; ++i, ++iE )
928 int smID = smIDs[ i ];
929 if ( smID == 0 ) continue;
930 const SMDS_MeshElement* elem = *iE;
931 if ( smID > maxID ) {
932 // corresponding subshape no longer exists: maybe geom group has been edited
933 if ( _mesh->GetImpl().HasShapeToMesh() )
934 meshDS->RemoveElement( elem );
937 // get or create submesh
938 SMESHDS_SubMesh* & sm = subMeshes[ smID ];
940 sm = meshDS->NewSubMesh( smID );
941 smType[ smID ] = meshDS->IndexToShape( smID ).ShapeType();
945 SMDS_PositionPtr pos = aPositionCreator.MakePosition( smType[ smID ]);
946 SMDS_MeshNode* node = const_cast<SMDS_MeshNode*>( static_cast<const SMDS_MeshNode*>( elem ));
947 node->SetPosition( pos );
950 sm->AddElement( elem );
956 } // end reading submeshes
958 // Read node positions on sub-shapes (SMDS_Position)
960 if ( aTopGroup->ExistInternalObject( "Node Positions" ))
962 // There are 5 datasets to read:
963 // "Nodes on Edges" - ID of node on edge
964 // "Edge positions" - U parameter on node on edge
965 // "Nodes on Faces" - ID of node on face
966 // "Face U positions" - U parameter of node on face
967 // "Face V positions" - V parameter of node on face
968 const char* aEid_DSName = "Nodes on Edges";
969 const char* aEu_DSName = "Edge positions";
970 const char* aFu_DSName = "Face U positions";
971 //char* aFid_DSName = "Nodes on Faces";
972 //char* aFv_DSName = "Face V positions";
975 int nbEids = 0, nbFids = 0;
976 int *aEids = 0, *aFids = 0;
977 double *aEpos = 0, *aFupos = 0, *aFvpos = 0;
980 HDFgroup* aGroup = new HDFgroup( "Node Positions", aTopGroup );
981 aGroup->OpenOnDisk();
983 // loop on 5 data sets
984 int aNbObjects = aGroup->nInternalObjects();
985 for ( int i = 0; i < aNbObjects; i++ )
988 char aDSName[ HDF_NAME_MAX_LEN+1 ];
989 aGroup->InternalObjectIndentify( i, aDSName );
991 HDFdataset* aDataset = new HDFdataset( aDSName, aGroup );
992 aDataset->OpenOnDisk();
993 if ( aDataset->GetType() == HDF_FLOAT64 ) // Positions
995 double* pos = new double [ aDataset->GetSize() ];
996 aDataset->ReadFromDisk( pos );
998 if ( strncmp( aDSName, aEu_DSName, strlen( aEu_DSName )) == 0 )
1000 else if ( strncmp( aDSName, aFu_DSName, strlen( aFu_DSName )) == 0 )
1007 int aSize = aDataset->GetSize();
1009 // for reading files, created from 18.07.2005 till 10.10.2005
1010 if (aDataset->GetType() == HDF_STRING)
1011 aSize /= sizeof(int);
1013 int* ids = new int [aSize];
1014 aDataset->ReadFromDisk( ids );
1015 // on face or nodes?
1016 if ( strncmp( aDSName, aEid_DSName, strlen( aEid_DSName )) == 0 ) {
1025 aDataset->CloseOnDisk();
1026 } // loop on 5 datasets
1028 // Set node positions on edges or faces
1029 for ( int onFace = 0; onFace < 2; onFace++ )
1031 int nbNodes = ( onFace ? nbFids : nbEids );
1032 if ( nbNodes == 0 ) continue;
1033 int* aNodeIDs = ( onFace ? aFids : aEids );
1034 double* aUPos = ( onFace ? aFupos : aEpos );
1035 double* aVPos = ( onFace ? aFvpos : 0 );
1037 for ( int iNode = 0; iNode < nbNodes; iNode++ )
1039 const SMDS_MeshNode* node = meshDS->FindNode( aNodeIDs[ iNode ]);
1040 if ( !node ) continue; // maybe removed while Loading() if geometry changed
1041 SMDS_PositionPtr aPos = node->GetPosition();
1044 // ASSERT( aPos->GetTypeOfPosition() == SMDS_TOP_FACE );-- issue 20182
1045 // -- Most probably a bad study was saved when there were
1046 // not fixed bugs in SMDS_MeshInfo
1047 if ( aPos->GetTypeOfPosition() == SMDS_TOP_FACE ) {
1048 SMDS_FacePosition* fPos = const_cast<SMDS_FacePosition*>
1049 ( static_cast<const SMDS_FacePosition*>( aPos ));
1050 fPos->SetUParameter( aUPos[ iNode ]);
1051 fPos->SetVParameter( aVPos[ iNode ]);
1055 // ASSERT( aPos->GetTypeOfPosition() == SMDS_TOP_EDGE );-- issue 20182
1056 if ( aPos->GetTypeOfPosition() == SMDS_TOP_EDGE ) {
1057 SMDS_EdgePosition* fPos = const_cast<SMDS_EdgePosition*>
1058 ( static_cast<const SMDS_EdgePosition*>( aPos ));
1059 fPos->SetUParameter( aUPos[ iNode ]);
1064 if ( aEids ) delete [] aEids;
1065 if ( aFids ) delete [] aFids;
1066 if ( aEpos ) delete [] aEpos;
1067 if ( aFupos ) delete [] aFupos;
1068 if ( aFvpos ) delete [] aFvpos;
1070 aGroup->CloseOnDisk();
1072 } // if ( aTopGroup->ExistInternalObject( "Node Positions" ) )
1074 aTopGroup->CloseOnDisk();
1075 } // if ( aFile->ExistInternalObject( meshGrpName ) )
1077 aFile->CloseOnDisk();
1081 //================================================================================
1083 * \brief Remove all SMESH_PreMeshInfo fields from objects w/o data loading
1085 //================================================================================
1087 void SMESH_PreMeshInfo::ForgetAllData() const
1091 if ( _mesh->changePreMeshInfo() != this )
1092 return _mesh->changePreMeshInfo()->ForgetAllData();
1094 // remove SMESH_PreMeshInfo from groups
1095 map<int, SMESH::SMESH_GroupBase_ptr>::const_iterator i2group = _mesh->_mapGroups.begin();
1096 for ( ; i2group != _mesh->_mapGroups.end(); ++i2group )
1098 if ( SMESH_GroupBase_i* group_i =
1099 SMESH::DownCast<SMESH_GroupBase_i*>( i2group->second ))
1101 SMESH_PreMeshInfo* & info = group_i->changePreMeshInfo();
1106 // remove SMESH_PreMeshInfo from sub-meshes
1107 map<int, SMESH::SMESH_subMesh_ptr>::iterator id2sm = _mesh->_mapSubMeshIor.begin();
1108 for ( ; id2sm != _mesh->_mapSubMeshIor.end(); ++id2sm )
1110 if ( SMESH_subMesh_i* sm_i = SMESH::DownCast<SMESH_subMesh_i*>( id2sm->second ))
1112 SMESH_PreMeshInfo* & info = sm_i->changePreMeshInfo();
1117 // remove SMESH_PreMeshInfo from the mesh
1118 _mesh->changePreMeshInfo() = NULL;
1121 SMESH_CATCH( SMESH::doNothing );
1128 // ::SMESH_Mesh& mesh = _mesh->GetImpl();
1130 // // update hyps needing full mesh data restored (issue 20918)
1131 // // map<int, SMESH::SMESH_Hypothesis_ptr>::iterator id2hyp= _mesh->_mapHypo.begin();
1132 // // for ( ; id2hyp != _mesh->_mapHypo.end(); ++id2hyp )
1133 // // if ( SMESH_Hypothesis_i* hyp = SMESH::DownCast<SMESH_Hypothesis_i*>( id2hyp->second ))
1134 // // hyp->UpdateAsMeshesRestored();
1137 // SMESH_CATCH( SMESH::doNothing );
1140 //================================================================================
1142 * \brief remove all SMESH_PreMeshInfo fields from mesh and its child objects w/o data loading
1144 //================================================================================
1146 void SMESH_PreMeshInfo::ForgetAllData( SMESH_Mesh_i* mesh )
1148 if ( mesh && mesh->changePreMeshInfo() )
1149 mesh->changePreMeshInfo()->ForgetAllData();
1152 //================================================================================
1154 * \brief Calls either FullLoadFromFile() or ForgetAllData() depending on preferences
1156 //================================================================================
1158 void SMESH_PreMeshInfo::ForgetOrLoad() const
1160 if ( SMESH_Gen_i::GetSMESHGen()->ToForgetMeshDataOnHypModif() &&
1161 _mesh->HasShapeToMesh())
1167 //================================================================================
1169 * \brief Method of SMESH_IDSource interface
1171 //================================================================================
1173 SMESH::array_of_ElementType* SMESH_PreMeshInfo::GetTypes() const
1175 SMESH::array_of_ElementType_var types = new SMESH::array_of_ElementType;
1179 if (NbEdges()) types[nbTypes++] = SMESH::EDGE;
1180 if (NbFaces()) types[nbTypes++] = SMESH::FACE;
1181 if (NbVolumes()) types[nbTypes++] = SMESH::VOLUME;
1182 if (Nb0DElements()) types[nbTypes++] = SMESH::ELEM0D;
1183 if (NbBalls()) types[nbTypes++] = SMESH::BALL;
1184 types->length( nbTypes );
1186 return types._retn();
1189 //================================================================================
1191 * \brief Method of SMESH_IDSource interface returning nb elements by element type
1193 //================================================================================
1195 SMESH::long_array* SMESH_PreMeshInfo::GetMeshInfo() const
1197 SMESH::long_array_var aRes = new SMESH::long_array();
1198 aRes->length(SMESH::Entity_Last);
1199 for (int i = SMESH::Entity_Node; i < SMESH::Entity_Last; i++)
1202 for (int i = SMESH::Entity_Node; i < SMESH::Entity_Last; i++)
1203 aRes[i] = NbEntities((SMDSAbs_EntityType)i);
1204 return aRes._retn();
1207 //================================================================================
1209 * Returns false if GetMeshInfo() returns incorrect information that may
1210 * happen if mesh data is not yet fully loaded from the file of study.
1212 //================================================================================
1214 bool SMESH_PreMeshInfo::IsMeshInfoCorrect() const
1219 //================================================================================
1221 * \brief TEMPORARY method to remove study files on closing study;
1222 * RIGHT WAY: study files are remove automatically when meshes are destroyed
1224 //================================================================================
1226 void SMESH_PreMeshInfo::RemoveStudyFiles_TMP_METHOD(SALOMEDS::SComponent_ptr smeshComp)
1228 SALOMEDS::Study_var study = smeshComp->GetStudy();
1229 if ( theStudyIDToMeshCounter[ (int) study->StudyId() ] > 0 )
1231 SALOMEDS::ChildIterator_wrap itBig = study->NewChildIterator( smeshComp );
1232 for ( ; itBig->More(); itBig->Next() ) {
1233 SALOMEDS::SObject_wrap gotBranch = itBig->Value();
1234 CORBA::Object_var anObject = SMESH_Gen_i::SObjectToObject( gotBranch );
1235 if ( SMESH_Mesh_i* mesh = SMESH::DownCast<SMESH_Mesh_i*>( anObject ))
1237 if ( mesh->changePreMeshInfo() )
1239 mesh->changePreMeshInfo()->ForgetAllData();