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 // count not yet loaded meshes
67 static int theMeshCounter = 0;
69 //================================================================================
71 * \brief Counts not fully loaded meshes
73 //================================================================================
75 void meshInfoLoaded( SMESH_Mesh_i* mesh )
79 //================================================================================
81 * \brief Removes temporary files if none of meshes needs them
83 //================================================================================
85 void filesNoMoreNeeded(SMESH_Mesh_i* mesh,
89 if ( --theMeshCounter == 0 )
91 std::string tmpDir = SALOMEDS_Tool::GetDirFromPath( hdfFile );
93 SALOMEDS_Tool::ListOfFiles aFiles;
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());
100 SALOMEDS_Tool::RemoveTemporaryFiles( tmpDir.c_str(), aFiles, true );
104 //=============================================================================
106 * \brief Class sending signals on start and finish of loading
108 //=============================================================================
112 std::string _messagePrefix;
113 SALOME::Session_var _session;
115 SignalToGUI( SMESH_Mesh_i* mesh )
117 SMESH_Gen_i* gen = SMESH_Gen_i::GetSMESHGen();
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() )
124 CORBA::String_var meshEntry = meshSO->GetID();
125 _messagePrefix = "SMESH/mesh_loading/";
126 _messagePrefix += meshEntry.in();
128 std::string msgToGUI = _messagePrefix + "/";
129 msgToGUI += SMESH_Comment( mesh->NbNodes() );
131 msgToGUI += SMESH_Comment( mesh->NbElements() );
133 _session->emitMessageOneWay( msgToGUI.c_str());
138 if ( !_messagePrefix.empty() )
140 std::string msgToGUI = _messagePrefix + "/stop";
141 _session->emitMessageOneWay( msgToGUI.c_str());
142 _messagePrefix.clear();
145 ~SignalToGUI() { sendStop(); }
148 //=============================================================================
150 * \brief Creates SMDS_Position according to shape type
152 //=============================================================================
154 class PositionCreator {
156 SMDS_PositionPtr MakePosition(const TopAbs_ShapeEnum type) {
157 return (this->*myFuncTable[ type ])();
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;
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;
176 //================================================================================
178 * \brief Returns ids of simple shapes composing a complex one
180 //================================================================================
182 std::vector<int> getSimpleSubMeshIds( SMESHDS_Mesh* meshDS, int shapeId )
184 std::vector<int> ids;
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 )
190 if ( shape->IsNull() ) continue;
191 if ( shape->ShapeType() == TopAbs_COMPOUND ||
192 shape->ShapeType() == TopAbs_COMPSOLID )
194 for ( TopoDS_Iterator it( *shape ); it.More(); it.Next() )
195 shapeQueue.push_back( it.Value() );
199 ids.push_back( meshDS->ShapeToIndex( *shape ));
205 //================================================================================
207 * \brief Return EEntiteMaillage by EGeometrieElement
209 //================================================================================
211 MED::EEntiteMaillage entityByGeom(const MED::EGeometrieElement geom )
213 return geom == MED::eBALL ? MED::eSTRUCT_ELEMENT : MED::eMAILLE;
216 //================================================================================
218 * \brief Return a map< EGeometrieElement, SMDSAbs_EntityType >
220 //================================================================================
222 typedef std::map< MED::EGeometrieElement, SMDSAbs_EntityType > Tmed2smeshElemTypeMap;
223 const Tmed2smeshElemTypeMap& med2smeshElemTypeMap()
225 static Tmed2smeshElemTypeMap med2smeshTypes;
226 if ( med2smeshTypes.empty() )
228 for ( int iG = 0; iG < SMDSEntity_Last; ++iG )
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 ));
236 return med2smeshTypes;
239 //================================================================================
241 * \brief Writes meshInfo into a HDF file
243 //================================================================================
245 void meshInfo2hdf( SMESH::long_array_var meshInfo,
246 const std::string& name,
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();
254 std::vector<int> data;
256 for ( size_t i = 0; i < meshInfo->length(); ++i )
257 if ( meshInfo[i] > 0 )
259 data.push_back( DriverMED::GetMedGeoType( SMDSAbs_EntityType( i ))); //medTypes[ i ] );
260 data.push_back( meshInfo[ i ] );
265 hdf_size datasetSize[] = { data.size() };
266 HDFarray* anArray = new HDFarray(0, HDF_INT32, 1, datasetSize);
267 anArray->CreateOnDisk();
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();
279 //================================================================================
281 * \brief Reads meshInfo from a HDF file
283 //================================================================================
285 void SMESH_PreMeshInfo::hdf2meshInfo( const std::string& name,
288 if ( hdfGroup->ExistInternalObject( name.c_str()) )
290 HDFdataset* dataset = new HDFdataset( name.c_str(), hdfGroup );
291 dataset->OpenOnDisk();
293 // // hdf_size datasetSize[ 1 ];
294 // // HDFarray *array = new HDFarray(dataset);
295 // // array->GetDim( datasetSize );
296 // int size = dataset->GetSize();
298 std::vector<int> info( SMDSEntity_Last * 2, 0 );
299 dataset->ReadFromDisk( &info[0] );
300 dataset->CloseOnDisk();
302 const Tmed2smeshElemTypeMap& med2smesh = med2smeshElemTypeMap();
303 Tmed2smeshElemTypeMap::const_iterator me2sme, me2smeEnd = med2smesh.end();
304 for ( size_t i = 0; i < info.size(); )
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 );
316 if ( NbNodes() == GroupOnFilter_OutOfDate ) // case of !SMESHDS_GroupOnFilter::IsUpToDate()
319 setNb( SMDSEntity_Node, 0 );
323 //================================================================================
325 * \brief Constructor callable by SMESH_PreMeshInfo only
327 //================================================================================
329 SMESH_PreMeshInfo::SMESH_PreMeshInfo(SMESH_Mesh_i* mesh,
331 const std::string& medFile,
332 const std::string& hdfFile)
333 : _medFileName( medFile ),
334 _hdfFileName( hdfFile ),
335 _toRemoveFiles( false ),
343 //================================================================================
345 * \brief Release temporary files
347 //================================================================================
349 SMESH_PreMeshInfo::~SMESH_PreMeshInfo()
351 if ( _toRemoveFiles ) // it can be true only for SMESH_PreMeshInfo of the mesh
352 filesNoMoreNeeded( _mesh, _medFileName, _hdfFileName );
354 _toRemoveFiles = false;
357 //================================================================================
359 * \brief fills SMESH_PreMeshInfo field of all objects of mesh
361 //================================================================================
363 void SMESH_PreMeshInfo::LoadFromFile( SMESH_Mesh_i* mesh,
365 const std::string& medFile,
366 const std::string& hdfFile,
367 const bool toRemoveFiles)
371 SMESH_PreMeshInfo* meshPreInfo = new SMESH_PreMeshInfo( mesh,meshID,medFile,hdfFile );
372 mesh->changePreMeshInfo() = meshPreInfo;
374 meshPreInfo->_toRemoveFiles = toRemoveFiles;
376 meshInfoLoaded( mesh );
378 if ( meshPreInfo->readPreInfoFromHDF() )
379 // all SMESH_PreMeshInfo's are stored in HDF file (written after
380 // implementing SMESH_PreMeshInfo)
383 // try to read SMESH_PreMeshInfo from med file (as study is older than SMESH_PreMeshInfo)
384 if ( meshPreInfo->readMeshInfo() )
386 meshPreInfo->readGroupInfo();
387 meshPreInfo->readSubMeshInfo();
391 meshPreInfo->FullLoadFromFile();
393 SMESH_CATCH( SMESH::doNothing );
396 //================================================================================
398 * \brief Tries to read all SMESH_PreMeshInfo from a HDF file
399 * \retval bool - true if succeeded
401 * This method is symmetrical to SaveToFile()
403 //================================================================================
405 bool SMESH_PreMeshInfo::readPreInfoFromHDF()
407 HDFfile* aFile = new HDFfile( (char*) _hdfFileName.c_str() );
408 aFile->OpenOnDisk( HDF_RDONLY );
410 SMESH_Comment hdfGroupName("SMESH_PreMeshInfo"); hdfGroupName << _meshID;
411 const bool infoAvailable = aFile->ExistInternalObject( hdfGroupName );
414 HDFgroup* infoHdfGroup = new HDFgroup( hdfGroupName, aFile );
415 infoHdfGroup->OpenOnDisk();
417 _mesh->changePreMeshInfo()->hdf2meshInfo( "Mesh", infoHdfGroup );
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 )
423 if ( SMESH_GroupBase_i* group_i =
424 SMESH::DownCast<SMESH_GroupBase_i*>( i2group->second ))
426 group_i->changePreMeshInfo() = newInstance();
427 if ( SMESHDS_GroupBase* group = group_i->GetGroupDS() )
429 const std::string name = group->GetStoreName();
430 group_i->changePreMeshInfo()->hdf2meshInfo( name, infoHdfGroup );
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 )
439 if ( SMESH_subMesh_i* sm = SMESH::DownCast<SMESH_subMesh_i*>( id2sm->second ))
441 sm->changePreMeshInfo() = newInstance();
442 sm->changePreMeshInfo()->hdf2meshInfo( SMESH_Comment( sm->GetId()), infoHdfGroup );
447 aFile->CloseOnDisk();
450 return infoAvailable;
453 //================================================================================
455 * \brief Reads mesh info of mesh from the med file
457 //================================================================================
459 bool SMESH_PreMeshInfo::readMeshInfo()
463 MED::PWrapper aMed = MED::CrWrapper(_medFileName,true);
464 // if ( aMed->GetVersion() != MED::eV2_2 )
467 MED::PMeshInfo medMeshInfo = aMed->CrMeshInfo(3,3,SMESH_Comment( _meshID ));
470 int nbNodes = std::max( 0, aMed->GetNbNodes( medMeshInfo ));
473 setNb( SMDSEntity_Node, nbNodes);
475 // read nb of elements
476 Tmed2smeshElemTypeMap::const_iterator me2sme = med2smeshElemTypeMap().begin();
477 Tmed2smeshElemTypeMap::const_iterator me2smeEnd = med2smeshElemTypeMap().end();
478 for ( ; me2sme != me2smeEnd; ++me2sme )
480 int nbElems = aMed->GetNbCells( medMeshInfo, entityByGeom(me2sme->first), me2sme->first );
482 setNb( me2sme->second, nbElems );
488 //================================================================================
490 * \brief Reads info of groups from the med file
492 //================================================================================
494 void SMESH_PreMeshInfo::readGroupInfo()
496 if ( _mesh->_mapGroups.empty() ) return;
498 // make SMESH_PreMeshInfo of groups
499 map< std::string, SMESH_PreMeshInfo* > name2GroupInfo;
500 map<int, SMESH::SMESH_GroupBase_ptr>::const_iterator i2group = _mesh->_mapGroups.begin();
501 for ( ; i2group != _mesh->_mapGroups.end(); ++i2group )
503 if ( SMESH_GroupBase_i* group_i =
504 SMESH::DownCast<SMESH_GroupBase_i*>( i2group->second ))
506 SMESH_PreMeshInfo* info = newInstance();
507 group_i->changePreMeshInfo() = info;
508 if ( SMESHDS_Group* group = dynamic_cast< SMESHDS_Group* >( group_i->GetGroupDS() ))
510 std::string name = group->GetStoreName();
511 name2GroupInfo.insert( std::make_pair( name, info ));
512 info->_isInfoOk = true;
517 map< int, vector< SMESH_PreMeshInfo* > > famId2grInfo;
519 MED::PWrapper aMed = MED::CrWrapper(_medFileName,false);
520 MED::PMeshInfo medMeshInfo = aMed->CrMeshInfo(3,3,SMESH_Comment( _meshID ));
522 // read families to fill in famId2grInfo
523 int nbFams = aMed->GetNbFamilies( medMeshInfo );
524 if ( nbFams <= 1 ) return; // zero family is always present
525 for ( int iF = 0; iF <= nbFams; ++iF )
527 int nbGroups = aMed->GetNbFamGroup( iF, medMeshInfo );
528 if ( nbGroups < 1 ) continue;
529 MED::PFamilyInfo medFamInfo = aMed->CrFamilyInfo( medMeshInfo, nbGroups, nbGroups );
530 aMed->GetFamilyInfo( iF, medFamInfo ); // read groups of a family
531 vector< SMESH_PreMeshInfo* >& grInfoVec = famId2grInfo[ medFamInfo->GetId() ];
532 for ( int iG = 0; iG < nbGroups; ++iG )
534 const std::string grName = medFamInfo->GetGroupName( iG );
535 map< std::string, SMESH_PreMeshInfo* >::iterator n2i = name2GroupInfo.find( grName );
536 if ( n2i != name2GroupInfo.end() )
537 grInfoVec.push_back( n2i->second );
541 // read family numbers of elements
542 Tmed2smeshElemTypeMap::const_iterator me2sme = med2smeshElemTypeMap().begin();
543 Tmed2smeshElemTypeMap::const_iterator me2smeEnd = med2smeshElemTypeMap().end();
544 MED::PElemInfo medElemInfo = aMed->CrElemInfo( medMeshInfo, 0 );
545 MED::TIntVector& famNums = medElemInfo->myFamNum;
546 for ( ; me2sme != me2smeEnd; ++me2sme ) // loop on elem types
548 famNums.resize( NbEntities( me2sme->second ));
549 if ( famNums.empty() ) continue;
550 aMed->GetFamilies( medElemInfo, famNums.size(), entityByGeom(me2sme->first), me2sme->first );
551 // distribute elements of a type among groups
552 map< int, vector< SMESH_PreMeshInfo* > >::iterator f2infos = famId2grInfo.begin();
553 for ( size_t i = 0; i < famNums.size(); ++i )
555 if ( famNums[i] != f2infos->first )
557 f2infos = famId2grInfo.find( famNums[i] );
558 if ( f2infos == famId2grInfo.end() )
559 f2infos = famId2grInfo.insert
560 ( std::make_pair( famNums[i], vector< SMESH_PreMeshInfo*>())).first;
562 vector< SMESH_PreMeshInfo* >& infoVec = f2infos->second ;
563 for ( size_t j = 0; j < infoVec.size(); ++j )
564 infoVec[j]->_elemCounter++;
566 // pass _elemCounter to a real elem type
567 map< std::string, SMESH_PreMeshInfo* >::iterator n2i = name2GroupInfo.begin();
568 for ( ; n2i != name2GroupInfo.end(); ++n2i )
570 SMESH_PreMeshInfo* info = n2i->second;
571 info->setNb( me2sme->second, info->_elemCounter );
572 info->_elemCounter = 0;
577 //================================================================================
579 * \brief Reads info of sub-meshes from hdf file of old study
581 //================================================================================
583 void SMESH_PreMeshInfo::readSubMeshInfo()
585 if ( _mesh->_mapSubMeshIor.empty() ) return;
587 // create SMESH_PreMeshInfo of sub-meshes
588 map<int, SMESH::SMESH_subMesh_ptr>::iterator id2sm = _mesh->_mapSubMeshIor.begin();
589 for ( ; id2sm != _mesh->_mapSubMeshIor.end(); ++id2sm )
591 if ( SMESH_subMesh_i* sm = SMESH::DownCast<SMESH_subMesh_i*>( id2sm->second ))
593 sm->changePreMeshInfo() = newInstance();
594 sm->changePreMeshInfo()->_isInfoOk = true;
599 HDFfile* aFile = new HDFfile( (char*) _hdfFileName.c_str() );
600 aFile->OpenOnDisk( HDF_RDONLY );
602 char meshGrpName[ 30 ];
603 sprintf( meshGrpName, "Mesh %d", _meshID );
604 if ( aFile->ExistInternalObject( meshGrpName ) )
606 HDFgroup* aTopGroup = new HDFgroup( meshGrpName, aFile );
607 aTopGroup->OpenOnDisk();
608 if ( aTopGroup->ExistInternalObject( "Submeshes" ))
610 HDFgroup* aGroup = new HDFgroup( "Submeshes", aTopGroup );
611 aGroup->OpenOnDisk();
613 SMESHDS_Mesh* meshDS = _mesh->GetImpl().GetMeshDS();
614 int maxSmId = Max( meshDS->MaxSubMeshIndex(), meshDS->MaxShapeIndex() );
616 for ( int isNode = 0; isNode < 2; ++isNode )
618 std::string aDSName( isNode ? "Node Submeshes" : "Element Submeshes");
619 if ( aGroup->ExistInternalObject( (char*) aDSName.c_str() ))
621 // read sub-mesh id of all nodes or elems
622 HDFdataset* aDataset = new HDFdataset( (char*) aDSName.c_str(), aGroup );
623 aDataset->OpenOnDisk();
624 int nbElems = aDataset->GetSize();
625 int* smIDs = new int [ nbElems ];
626 aDataset->ReadFromDisk( smIDs );
627 aDataset->CloseOnDisk();
628 // count nb elems in each sub-mesh
629 vector<int> nbBySubmeshId( maxSmId + 1, 0 );
630 for ( int i = 0; i < nbElems; ++i )
632 const int smID = smIDs[ i ];
633 if ( smID < (int) nbBySubmeshId.size() )
634 nbBySubmeshId[ smID ]++;
638 // store nb elems in SMESH_PreMeshInfo of sub-meshes
639 map<int, SMESH::SMESH_subMesh_ptr>::iterator id2sm = _mesh->_mapSubMeshIor.begin();
640 for ( ; id2sm != _mesh->_mapSubMeshIor.end(); ++id2sm )
642 if ( SMESH_subMesh_i* sm = SMESH::DownCast<SMESH_subMesh_i*>( id2sm->second ))
644 SMESH_PreMeshInfo* & info = sm->changePreMeshInfo();
646 vector<int> smIds = getSimpleSubMeshIds( meshDS, id2sm->first );
647 for ( size_t i = 0; i < smIds.size(); ++i )
648 info->_elemCounter += nbBySubmeshId[ smIds[i] ];
650 SMDSAbs_EntityType elemType;
653 elemType = SMDSEntity_Node;
657 bool koElemType = false;
658 const TopoDS_Shape& shape = meshDS->IndexToShape( smIds[0] );
659 elemType = getElemType( shape.ShapeType(), info->_elemCounter, koElemType );
660 info->_isInfoOk = !koElemType;
662 info->setNb( elemType, info->_elemCounter );
665 } // if ( aGroup->ExistInternalObject( aDSName ))
666 } // for ( int isNode = 0; isNode < 2; ++isNode )
668 aGroup->CloseOnDisk();
669 } // if ( aTopGroup->ExistInternalObject( "Submeshes" ))
671 aTopGroup->CloseOnDisk();
672 } // if ( aFile->ExistInternalObject( meshGrpName ) )
674 aFile->CloseOnDisk();
678 //================================================================================
680 * \brief Return type of element for sub-mesh on a shape of given type
682 //================================================================================
684 SMDSAbs_EntityType SMESH_PreMeshInfo::getElemType( const TopAbs_ShapeEnum shapeType,
685 const int nbElemsInSubMesh,
686 bool& isKoType) const
690 SMESH_PreMeshInfo* meshInfo = _mesh->changePreMeshInfo();
695 type = SMDSEntity_Tetra;
696 typeEnd = SMDSEntity_Last;
697 isKoType = ( meshInfo->NbVolumes() != nbElemsInSubMesh );
701 type = SMDSEntity_Triangle;
702 typeEnd = SMDSEntity_Tetra;
703 isKoType = ( meshInfo->NbFaces() != nbElemsInSubMesh );
706 case TopAbs_EDGE: return SMDSEntity_Edge;
707 case TopAbs_VERTEX: return SMDSEntity_0D;
708 default: return SMDSEntity_Last;
713 for ( int t = type; t < typeEnd; ++t )
714 if ( nbElemsInSubMesh == meshInfo->NbEntities( SMDSAbs_EntityType( t )))
715 return SMDSAbs_EntityType( t );
718 return SMDSAbs_EntityType( type );
721 //================================================================================
723 * \brief Saves SMESH_PreMeshInfo to the study file
725 //================================================================================
727 void SMESH_PreMeshInfo::SaveToFile( SMESH_Mesh_i* mesh,
731 // create a HDF group for SMESH_PreMeshInfo of this mesh
732 SMESH_Comment hdfGroupName("SMESH_PreMeshInfo"); hdfGroupName << meshID;
733 HDFgroup* infoHdfGroup = new HDFgroup( hdfGroupName, hdfFile );
734 infoHdfGroup->CreateOnDisk();
739 meshInfo2hdf( mesh->GetMeshInfo(), "Mesh", infoHdfGroup );
742 SMESH_PreMeshInfo incompleteInfo( 0,0,"","");
743 incompleteInfo.setNb( SMDSEntity_Node, GroupOnFilter_OutOfDate );
744 SMESHDS_Mesh* meshDS = mesh->GetImpl().GetMeshDS();
746 map<int, SMESH::SMESH_GroupBase_ptr>::const_iterator i2group = mesh->_mapGroups.begin();
747 for ( ; i2group != mesh->_mapGroups.end(); ++i2group )
749 if ( SMESH_GroupBase_i* group_i = SMESH::DownCast<SMESH_GroupBase_i*>( i2group->second ))
751 SMESHDS_GroupBase * group = group_i->GetGroupDS();
752 if ( SMESHDS_GroupOnFilter* gof = dynamic_cast<SMESHDS_GroupOnFilter*>(group))
754 // prevent too long storage time due to applying filter to many elements
755 if ( !gof->IsUpToDate() && meshDS->GetMeshInfo().NbElements( gof->GetType() ) > 1e5 )
757 meshInfo2hdf( incompleteInfo.GetMeshInfo(),
758 group->GetStoreName(),
763 meshInfo2hdf( group_i->GetMeshInfo(), group->GetStoreName(), infoHdfGroup);
767 // info of sub-meshes
768 map<int, SMESH::SMESH_subMesh_ptr>::iterator id2sm = mesh->_mapSubMeshIor.begin();
769 for ( ; id2sm != mesh->_mapSubMeshIor.end(); ++id2sm )
771 if ( SMESH_subMesh_i* sm = SMESH::DownCast<SMESH_subMesh_i*>( id2sm->second ))
773 meshInfo2hdf( sm->GetMeshInfo(),
774 SMESH_Comment( sm->GetId() ),
779 SMESH_CATCH( SMESH::doNothing );
781 infoHdfGroup->CloseOnDisk();
784 //================================================================================
786 * \brief Reads all data and remove all SMESH_PreMeshInfo fields from objects
788 //================================================================================
790 void SMESH_PreMeshInfo::FullLoadFromFile() const
792 SignalToGUI signalOnLoading( _mesh );
794 SMESH_PreMeshInfo* meshInfo = _mesh->changePreMeshInfo();
795 _mesh->changePreMeshInfo() = NULL; // to allow GUI accessing to real info
797 ::SMESH_Mesh& mesh = _mesh->GetImpl();
798 SMESHDS_Mesh* meshDS = mesh.GetMeshDS();
802 MYDEBUGOUT( "BEG FullLoadFromFile() " << _meshID );
805 DriverMED_R_SMESHDS_Mesh myReader;
806 myReader.SetFile( _medFileName.c_str() );
807 myReader.SetMesh( meshDS );
808 myReader.SetMeshId( _meshID );
812 const set<SMESHDS_GroupBase*>& groups = meshDS->GetGroups();
813 set<SMESHDS_GroupBase*>::const_iterator groupIt = groups.begin();
814 for ( ; groupIt != groups.end(); ++groupIt )
815 if ( SMESHDS_Group* aGrp = dynamic_cast<SMESHDS_Group*>( *groupIt ))
816 myReader.GetGroup( aGrp );
819 readSubMeshes( &myReader );
821 SMESH_CATCH( SMESH::doNothing );
823 _mesh->changePreMeshInfo() = meshInfo;
827 signalOnLoading.sendStop();
831 // load dependent meshes referring/referred via hypotheses
832 mesh.GetSubMesh( mesh.GetShapeToMesh() )->
833 ComputeStateEngine (SMESH_subMesh::SUBMESH_LOADED);
835 MYDEBUGOUT( "END FullLoadFromFile()" );
838 //================================================================================
840 * \brief Reads full data of sub-meshes
842 //================================================================================
844 void SMESH_PreMeshInfo::readSubMeshes(DriverMED_R_SMESHDS_Mesh* reader) const
846 HDFfile* aFile = new HDFfile( (char*) _hdfFileName.c_str() );
847 aFile->OpenOnDisk( HDF_RDONLY );
849 char meshGrpName[ 30 ];
850 sprintf( meshGrpName, "Mesh %d", _meshID );
851 if ( aFile->ExistInternalObject( meshGrpName ) )
853 HDFgroup* aTopGroup = new HDFgroup( meshGrpName, aFile );
854 aTopGroup->OpenOnDisk();
856 SMESHDS_Mesh* meshDS = _mesh->GetImpl().GetMeshDS();
858 // issue 0020693. Restore _isModified flag
859 if ( aTopGroup->ExistInternalObject( "_isModified" ))
861 HDFdataset* aDataset = new HDFdataset( "_isModified", aTopGroup );
862 aDataset->OpenOnDisk();
863 hdf_size size = aDataset->GetSize();
864 int* isModified = new int[ size ];
865 aDataset->ReadFromDisk( isModified );
866 aDataset->CloseOnDisk();
867 _mesh->GetImpl().SetIsModified( bool(*isModified));
868 delete [] isModified;
871 bool submeshesInFamilies = ( ! aTopGroup->ExistInternalObject( "Submeshes" ));
872 if ( submeshesInFamilies ) // from MED
874 // old way working before fix of PAL 12992
875 reader->CreateAllSubMeshes();
880 HDFgroup* aGroup = new HDFgroup( "Submeshes", aTopGroup );
881 aGroup->OpenOnDisk();
883 int maxID = Max( meshDS->MaxSubMeshIndex(), meshDS->MaxShapeIndex() );
884 vector< SMESHDS_SubMesh * > subMeshes( maxID + 1, (SMESHDS_SubMesh*) 0 );
885 vector< TopAbs_ShapeEnum > smType ( maxID + 1, TopAbs_SHAPE );
887 PositionCreator aPositionCreator;
889 SMDS_NodeIteratorPtr nIt = meshDS->nodesIterator();
890 SMDS_ElemIteratorPtr eIt = meshDS->elementsIterator();
891 for ( int isNode = 0; isNode < 2; ++isNode )
893 std::string aDSName( isNode ? "Node Submeshes" : "Element Submeshes");
894 if ( aGroup->ExistInternalObject( (char*) aDSName.c_str() ))
896 HDFdataset* aDataset = new HDFdataset( (char*) aDSName.c_str(), aGroup );
897 aDataset->OpenOnDisk();
898 // read submesh IDs for all elements sorted by ID
899 size_t nbElems = aDataset->GetSize();
900 int* smIDs = new int [ nbElems ];
901 aDataset->ReadFromDisk( smIDs );
902 aDataset->CloseOnDisk();
904 // get elements sorted by ID
905 TIDSortedElemSet elemSet;
907 while ( nIt->more() ) elemSet.insert( elemSet.end(), nIt->next() );
909 while ( eIt->more() ) elemSet.insert( elemSet.end(), eIt->next() );
910 //ASSERT( elemSet.size() == nbElems ); -- issue 20182
911 // -- Most probably a bad study was saved when there were
912 // not fixed bugs in SMDS_MeshInfo
913 if ( elemSet.size() < nbElems ) {
915 cout << "SMESH_Gen_i::Load(), warning: Node position data is invalid" << endl;
917 nbElems = elemSet.size();
919 // add elements to sub-meshes
920 TIDSortedElemSet::iterator iE = elemSet.begin();
921 for ( size_t i = 0; i < nbElems; ++i, ++iE )
923 int smID = smIDs[ i ];
924 if ( smID == 0 ) continue;
925 const SMDS_MeshElement* elem = *iE;
926 if ( smID > maxID ) {
927 // corresponding subshape no longer exists: maybe geom group has been edited
928 if ( _mesh->GetImpl().HasShapeToMesh() )
929 meshDS->RemoveElement( elem );
932 // get or create submesh
933 SMESHDS_SubMesh* & sm = subMeshes[ smID ];
935 sm = meshDS->NewSubMesh( smID );
936 smType[ smID ] = meshDS->IndexToShape( smID ).ShapeType();
940 SMDS_PositionPtr pos = aPositionCreator.MakePosition( smType[ smID ]);
941 SMDS_MeshNode* node = const_cast<SMDS_MeshNode*>( static_cast<const SMDS_MeshNode*>( elem ));
942 node->SetPosition( pos );
945 sm->AddElement( elem );
951 } // end reading submeshes
953 // Read node positions on sub-shapes (SMDS_Position)
955 if ( aTopGroup->ExistInternalObject( "Node Positions" ))
957 // There are 5 datasets to read:
958 // "Nodes on Edges" - ID of node on edge
959 // "Edge positions" - U parameter on node on edge
960 // "Nodes on Faces" - ID of node on face
961 // "Face U positions" - U parameter of node on face
962 // "Face V positions" - V parameter of node on face
963 const char* aEid_DSName = "Nodes on Edges";
964 const char* aEu_DSName = "Edge positions";
965 const char* aFu_DSName = "Face U positions";
966 //char* aFid_DSName = "Nodes on Faces";
967 //char* aFv_DSName = "Face V positions";
970 int nbEids = 0, nbFids = 0;
971 int *aEids = 0, *aFids = 0;
972 double *aEpos = 0, *aFupos = 0, *aFvpos = 0;
975 HDFgroup* aGroup = new HDFgroup( "Node Positions", aTopGroup );
976 aGroup->OpenOnDisk();
978 // loop on 5 data sets
979 int aNbObjects = aGroup->nInternalObjects();
980 for ( int i = 0; i < aNbObjects; i++ )
983 char aDSName[ HDF_NAME_MAX_LEN+1 ];
984 aGroup->InternalObjectIndentify( i, aDSName );
986 HDFdataset* aDataset = new HDFdataset( aDSName, aGroup );
987 aDataset->OpenOnDisk();
988 if ( aDataset->GetType() == HDF_FLOAT64 ) // Positions
990 double* pos = new double [ aDataset->GetSize() ];
991 aDataset->ReadFromDisk( pos );
993 if ( strncmp( aDSName, aEu_DSName, strlen( aEu_DSName )) == 0 )
995 else if ( strncmp( aDSName, aFu_DSName, strlen( aFu_DSName )) == 0 )
1002 int aSize = aDataset->GetSize();
1004 // for reading files, created from 18.07.2005 till 10.10.2005
1005 if (aDataset->GetType() == HDF_STRING)
1006 aSize /= sizeof(int);
1008 int* ids = new int [aSize];
1009 aDataset->ReadFromDisk( ids );
1010 // on face or nodes?
1011 if ( strncmp( aDSName, aEid_DSName, strlen( aEid_DSName )) == 0 ) {
1020 aDataset->CloseOnDisk();
1021 } // loop on 5 datasets
1023 // Set node positions on edges or faces
1024 for ( int onFace = 0; onFace < 2; onFace++ )
1026 int nbNodes = ( onFace ? nbFids : nbEids );
1027 if ( nbNodes == 0 ) continue;
1028 int* aNodeIDs = ( onFace ? aFids : aEids );
1029 double* aUPos = ( onFace ? aFupos : aEpos );
1030 double* aVPos = ( onFace ? aFvpos : 0 );
1032 for ( int iNode = 0; iNode < nbNodes; iNode++ )
1034 const SMDS_MeshNode* node = meshDS->FindNode( aNodeIDs[ iNode ]);
1035 if ( !node ) continue; // maybe removed while Loading() if geometry changed
1036 SMDS_PositionPtr aPos = node->GetPosition();
1039 // ASSERT( aPos->GetTypeOfPosition() == SMDS_TOP_FACE );-- issue 20182
1040 // -- Most probably a bad study was saved when there were
1041 // not fixed bugs in SMDS_MeshInfo
1042 if ( aPos->GetTypeOfPosition() == SMDS_TOP_FACE ) {
1043 SMDS_FacePosition* fPos = const_cast<SMDS_FacePosition*>
1044 ( static_cast<const SMDS_FacePosition*>( aPos ));
1045 fPos->SetUParameter( aUPos[ iNode ]);
1046 fPos->SetVParameter( aVPos[ iNode ]);
1050 // ASSERT( aPos->GetTypeOfPosition() == SMDS_TOP_EDGE );-- issue 20182
1051 if ( aPos->GetTypeOfPosition() == SMDS_TOP_EDGE ) {
1052 SMDS_EdgePosition* fPos = const_cast<SMDS_EdgePosition*>
1053 ( static_cast<const SMDS_EdgePosition*>( aPos ));
1054 fPos->SetUParameter( aUPos[ iNode ]);
1059 if ( aEids ) delete [] aEids;
1060 if ( aFids ) delete [] aFids;
1061 if ( aEpos ) delete [] aEpos;
1062 if ( aFupos ) delete [] aFupos;
1063 if ( aFvpos ) delete [] aFvpos;
1065 aGroup->CloseOnDisk();
1067 } // if ( aTopGroup->ExistInternalObject( "Node Positions" ) )
1069 aTopGroup->CloseOnDisk();
1070 } // if ( aFile->ExistInternalObject( meshGrpName ) )
1072 aFile->CloseOnDisk();
1076 //================================================================================
1078 * \brief Remove all SMESH_PreMeshInfo fields from objects w/o data loading
1080 //================================================================================
1082 void SMESH_PreMeshInfo::ForgetAllData() const
1086 if ( _mesh->changePreMeshInfo() != this )
1087 return _mesh->changePreMeshInfo()->ForgetAllData();
1089 // remove SMESH_PreMeshInfo from groups
1090 map<int, SMESH::SMESH_GroupBase_ptr>::const_iterator i2group = _mesh->_mapGroups.begin();
1091 for ( ; i2group != _mesh->_mapGroups.end(); ++i2group )
1093 if ( SMESH_GroupBase_i* group_i =
1094 SMESH::DownCast<SMESH_GroupBase_i*>( i2group->second ))
1096 SMESH_PreMeshInfo* & info = group_i->changePreMeshInfo();
1101 // remove SMESH_PreMeshInfo from sub-meshes
1102 map<int, SMESH::SMESH_subMesh_ptr>::iterator id2sm = _mesh->_mapSubMeshIor.begin();
1103 for ( ; id2sm != _mesh->_mapSubMeshIor.end(); ++id2sm )
1105 if ( SMESH_subMesh_i* sm_i = SMESH::DownCast<SMESH_subMesh_i*>( id2sm->second ))
1107 SMESH_PreMeshInfo* & info = sm_i->changePreMeshInfo();
1112 // remove SMESH_PreMeshInfo from the mesh
1113 _mesh->changePreMeshInfo() = NULL;
1116 SMESH_CATCH( SMESH::doNothing );
1123 // ::SMESH_Mesh& mesh = _mesh->GetImpl();
1125 // // update hyps needing full mesh data restored (issue 20918)
1126 // // map<int, SMESH::SMESH_Hypothesis_ptr>::iterator id2hyp= _mesh->_mapHypo.begin();
1127 // // for ( ; id2hyp != _mesh->_mapHypo.end(); ++id2hyp )
1128 // // if ( SMESH_Hypothesis_i* hyp = SMESH::DownCast<SMESH_Hypothesis_i*>( id2hyp->second ))
1129 // // hyp->UpdateAsMeshesRestored();
1132 // SMESH_CATCH( SMESH::doNothing );
1135 //================================================================================
1137 * \brief remove all SMESH_PreMeshInfo fields from mesh and its child objects w/o data loading
1139 //================================================================================
1141 void SMESH_PreMeshInfo::ForgetAllData( SMESH_Mesh_i* mesh )
1143 if ( mesh && mesh->changePreMeshInfo() )
1144 mesh->changePreMeshInfo()->ForgetAllData();
1147 //================================================================================
1149 * \brief Calls either FullLoadFromFile() or ForgetAllData() depending on preferences
1151 //================================================================================
1153 void SMESH_PreMeshInfo::ForgetOrLoad() const
1155 if ( SMESH_Gen_i::GetSMESHGen()->ToForgetMeshDataOnHypModif() &&
1156 _mesh->HasShapeToMesh())
1162 //================================================================================
1164 * \brief Method of SMESH_IDSource interface
1166 //================================================================================
1168 SMESH::array_of_ElementType* SMESH_PreMeshInfo::GetTypes() const
1170 SMESH::array_of_ElementType_var types = new SMESH::array_of_ElementType;
1174 if (NbEdges()) types[nbTypes++] = SMESH::EDGE;
1175 if (NbFaces()) types[nbTypes++] = SMESH::FACE;
1176 if (NbVolumes()) types[nbTypes++] = SMESH::VOLUME;
1177 if (Nb0DElements()) types[nbTypes++] = SMESH::ELEM0D;
1178 if (NbBalls()) types[nbTypes++] = SMESH::BALL;
1179 types->length( nbTypes );
1181 return types._retn();
1184 //================================================================================
1186 * \brief Method of SMESH_IDSource interface returning nb elements by element type
1188 //================================================================================
1190 SMESH::long_array* SMESH_PreMeshInfo::GetMeshInfo() const
1192 SMESH::long_array_var aRes = new SMESH::long_array();
1193 aRes->length(SMESH::Entity_Last);
1194 for (int i = SMESH::Entity_Node; i < SMESH::Entity_Last; i++)
1197 for (int i = SMESH::Entity_Node; i < SMESH::Entity_Last; i++)
1198 aRes[i] = NbEntities((SMDSAbs_EntityType)i);
1199 return aRes._retn();
1202 //================================================================================
1204 * Returns false if GetMeshInfo() returns incorrect information that may
1205 * happen if mesh data is not yet fully loaded from the file of study.
1207 //================================================================================
1209 bool SMESH_PreMeshInfo::IsMeshInfoCorrect() const
1214 //================================================================================
1216 * \brief TEMPORARY method to remove study files on closing study;
1217 * RIGHT WAY: study files are remove automatically when meshes are destroyed
1219 //================================================================================
1221 void SMESH_PreMeshInfo::RemoveStudyFiles_TMP_METHOD(SALOMEDS::SComponent_ptr smeshComp)
1223 if ( theMeshCounter > 0 )
1225 SALOMEDS::ChildIterator_wrap itBig = SMESH_Gen_i::getStudyServant()->NewChildIterator( smeshComp );
1226 for ( ; itBig->More(); itBig->Next() ) {
1227 SALOMEDS::SObject_wrap gotBranch = itBig->Value();
1228 CORBA::Object_var anObject = SMESH_Gen_i::SObjectToObject( gotBranch );
1229 if ( SMESH_Mesh_i* mesh = SMESH::DownCast<SMESH_Mesh_i*>( anObject ))
1231 if ( mesh->changePreMeshInfo() )
1233 mesh->changePreMeshInfo()->ForgetAllData();