1 // Copyright (C) 2007-2013 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.
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_R_SMESHDS_Mesh.h"
30 #include "SMDS_EdgePosition.hxx"
31 #include "SMDS_FacePosition.hxx"
32 #include "SMDS_SpacePosition.hxx"
33 #include "SMDS_VertexPosition.hxx"
34 #include "SMESHDS_Group.hxx"
35 #include "SMESHDS_GroupOnFilter.hxx"
36 #include "SMESH_Gen_i.hxx"
37 #include "SMESH_Group_i.hxx"
38 #include "SMESH_Mesh_i.hxx"
39 #include "SMESH_subMesh_i.hxx"
41 #include <MED_Factory.hxx>
43 #include <HDFarray.hxx>
44 #include <HDFdataset.hxx>
45 #include <HDFfile.hxx>
46 #include <HDFgroup.hxx>
47 #include <SALOMEDS_Tool.hxx>
48 #include <SALOMEDS_wrap.hxx>
50 #include <TopoDS_Iterator.hxx>
51 #include <TopoDS_Shape.hxx>
53 #include "SMESH_TryCatch.hxx"
55 #include CORBA_SERVER_HEADER(SALOME_Session)
58 #define MYDEBUGOUT(msg) //std::cout << msg << std::endl;
62 enum { GroupOnFilter_OutOfDate = -1 };
64 // a map to count not yet loaded meshes
65 static map< int, int > theStudyIDToMeshCounter;
67 //================================================================================
69 * \brief Counts not fully loaded meshes
71 //================================================================================
73 void meshInfoLoaded( SMESH_Mesh_i* mesh )
75 map< int, int >::iterator id2counter =
76 theStudyIDToMeshCounter.insert( make_pair( (int) mesh->GetStudyId(), 0 )).first;
79 //================================================================================
81 * \brief Removes temporary files if none of meshes needs them
83 //================================================================================
85 void filesNoMoreNeeded(SMESH_Mesh_i* mesh,
89 if ( --theStudyIDToMeshCounter[ (int) mesh->GetStudyId() ] == 0 )
91 string tmpDir = SALOMEDS_Tool::GetDirFromPath( hdfFile );
93 SALOMEDS::ListOfFileNames_var aFiles = new SALOMEDS::ListOfFileNames;
95 medFile = SALOMEDS_Tool::GetNameFromPath( medFile ) + ".med";
96 hdfFile = SALOMEDS_Tool::GetNameFromPath( hdfFile ) + ".hdf";
97 aFiles[0] = medFile.c_str();
98 aFiles[1] = hdfFile.c_str();
100 SALOMEDS_Tool::RemoveTemporaryFiles( tmpDir.c_str(), aFiles.in(), true );
104 //=============================================================================
106 * \brief Class sending signals on start and finish of loading
108 //=============================================================================
112 string _messagePrefix;
113 SALOME::Session_var _session;
115 SignalToGUI( SMESH_Mesh_i* mesh )
117 SMESH_Gen_i* gen = SMESH_Gen_i::GetSMESHGen();
118 SALOMEDS::Study_var study = gen->GetCurrentStudy();
119 if ( !study->_is_nil() && study->StudyId() == mesh->GetStudyId() )
121 SALOMEDS::SObject_wrap meshSO = gen->ObjectToSObject(study, mesh->_this() );
122 CORBA::Object_var obj = gen->GetNS()->Resolve( "/Kernel/Session" );
123 _session = SALOME::Session::_narrow( obj );
124 if ( !meshSO->_is_nil() && !_session->_is_nil() )
126 CORBA::String_var meshEntry = meshSO->GetID();
127 _messagePrefix = "SMESH/mesh_loading/";
128 _messagePrefix += meshEntry.in();
130 string msgToGUI = _messagePrefix + "/";
131 msgToGUI += SMESH_Comment( mesh->NbNodes() );
133 msgToGUI += SMESH_Comment( mesh->NbElements() );
135 _session->emitMessageOneWay( msgToGUI.c_str());
141 if ( !_messagePrefix.empty() )
143 string msgToGUI = _messagePrefix + "/stop";
144 _session->emitMessageOneWay( msgToGUI.c_str());
145 _messagePrefix.clear();
148 ~SignalToGUI() { sendStop(); }
151 //=============================================================================
153 * \brief Creates SMDS_Position according to shape type
155 //=============================================================================
157 class PositionCreator {
159 SMDS_PositionPtr MakePosition(const TopAbs_ShapeEnum type) {
160 return (this->*myFuncTable[ type ])();
163 myFuncTable.resize( (size_t) TopAbs_SHAPE, & PositionCreator::defaultPosition );
164 myFuncTable[ TopAbs_SOLID ] = & PositionCreator::volumePosition;
165 myFuncTable[ TopAbs_FACE ] = & PositionCreator::facePosition;
166 myFuncTable[ TopAbs_EDGE ] = & PositionCreator::edgePosition;
167 myFuncTable[ TopAbs_VERTEX ] = & PositionCreator::vertexPosition;
170 SMDS_PositionPtr edgePosition() const { return SMDS_PositionPtr( new SMDS_EdgePosition ); }
171 SMDS_PositionPtr facePosition() const { return SMDS_PositionPtr( new SMDS_FacePosition ); }
172 SMDS_PositionPtr volumePosition() const { return SMDS_PositionPtr( new SMDS_SpacePosition ); }
173 SMDS_PositionPtr vertexPosition() const { return SMDS_PositionPtr( new SMDS_VertexPosition); }
174 SMDS_PositionPtr defaultPosition() const { return SMDS_SpacePosition::originSpacePosition(); }
175 typedef SMDS_PositionPtr (PositionCreator:: * FmakePos)() const;
176 vector<FmakePos> myFuncTable;
179 //================================================================================
181 * \brief Returns ids of simple shapes composing a complex one
183 //================================================================================
185 vector<int> getSimpleSubMeshIds( SMESHDS_Mesh* meshDS, int shapeId )
189 list<TopoDS_Shape> shapeQueue( 1, meshDS->IndexToShape( shapeId ));
190 list<TopoDS_Shape>::iterator shape = shapeQueue.begin();
191 for ( ; shape != shapeQueue.end(); ++shape )
193 if ( shape->IsNull() ) continue;
194 if ( shape->ShapeType() == TopAbs_COMPOUND ||
195 shape->ShapeType() == TopAbs_COMPSOLID )
197 for ( TopoDS_Iterator it( *shape ); it.More(); it.Next() )
198 shapeQueue.push_back( it.Value() );
202 ids.push_back( meshDS->ShapeToIndex( *shape ));
208 //================================================================================
210 * \brief Return EEntiteMaillage by EGeometrieElement
212 //================================================================================
214 MED::EEntiteMaillage entityByGeom(const MED::EGeometrieElement geom )
216 return geom == MED::eBALL ? MED::eSTRUCT_ELEMENT : MED::eMAILLE;
219 //================================================================================
221 * \brief Return a map< EGeometrieElement, SMDSAbs_EntityType >
223 //================================================================================
225 typedef map< MED::EGeometrieElement, SMDSAbs_EntityType > Tmed2smeshElemTypeMap;
226 const Tmed2smeshElemTypeMap& med2smeshElemTypeMap()
228 static map< MED::EGeometrieElement, SMDSAbs_EntityType> med2smeshTypes;
229 if ( med2smeshTypes.empty() )
231 med2smeshTypes[ MED::ePOINT1 ] = SMDSEntity_0D ;
232 med2smeshTypes[ MED::eSEG2 ] = SMDSEntity_Edge ;
233 med2smeshTypes[ MED::eSEG3 ] = SMDSEntity_Quad_Edge ;
234 med2smeshTypes[ MED::eTRIA3 ] = SMDSEntity_Triangle ;
235 med2smeshTypes[ MED::eTRIA6 ] = SMDSEntity_Quad_Triangle ;
236 med2smeshTypes[ MED::eQUAD4 ] = SMDSEntity_Quadrangle ;
237 med2smeshTypes[ MED::eQUAD8 ] = SMDSEntity_Quad_Quadrangle ;
238 med2smeshTypes[ MED::eQUAD9 ] = SMDSEntity_BiQuad_Quadrangle ;
239 med2smeshTypes[ MED::eTETRA4 ] = SMDSEntity_Tetra ;
240 med2smeshTypes[ MED::ePYRA5 ] = SMDSEntity_Pyramid ;
241 med2smeshTypes[ MED::ePENTA6 ] = SMDSEntity_Penta ;
242 med2smeshTypes[ MED::eHEXA8 ] = SMDSEntity_Hexa ;
243 med2smeshTypes[ MED::eOCTA12 ] = SMDSEntity_Hexagonal_Prism ;
244 med2smeshTypes[ MED::eTETRA10 ] = SMDSEntity_Quad_Tetra ;
245 med2smeshTypes[ MED::ePYRA13 ] = SMDSEntity_Quad_Pyramid ;
246 med2smeshTypes[ MED::ePENTA15 ] = SMDSEntity_Quad_Penta ;
247 med2smeshTypes[ MED::eHEXA20 ] = SMDSEntity_Quad_Hexa ;
248 med2smeshTypes[ MED::eHEXA27 ] = SMDSEntity_TriQuad_Hexa ;
249 med2smeshTypes[ MED::ePOLYGONE ] = SMDSEntity_Polygon ;
250 med2smeshTypes[ MED::ePOLYEDRE ] = SMDSEntity_Polyhedra ;
251 med2smeshTypes[ MED::eNONE ] = SMDSEntity_Node ;
252 med2smeshTypes[ MED::eBALL ] = SMDSEntity_Ball ;
254 return med2smeshTypes;
257 //================================================================================
259 * \brief Return a vector<MED::EGeometrieElement> intended to retrieve
260 * MED::EGeometrieElement by SMDSAbs_EntityType
262 //================================================================================
264 const vector<MED::EGeometrieElement>& mesh2medElemType()
266 static vector<MED::EGeometrieElement> mesh2medElemTypes;
267 if ( mesh2medElemTypes.empty() )
269 mesh2medElemTypes.resize( SMDSEntity_Last + 1 );
270 Tmed2smeshElemTypeMap::const_iterator me2sme = med2smeshElemTypeMap().begin();
271 Tmed2smeshElemTypeMap::const_iterator me2smeEnd = med2smeshElemTypeMap().end();
272 for ( ; me2sme != me2smeEnd; ++me2sme )
273 mesh2medElemTypes[ me2sme->second ] = me2sme->first;
275 return mesh2medElemTypes;
278 //================================================================================
280 * \brief Writes meshInfo into a HDF file
282 //================================================================================
284 void meshInfo2hdf( SMESH::long_array_var meshInfo,
285 const std::string& name,
288 // we use med identification of element (MED::EGeometrieElement>) types
289 // but not enum SMDSAbs_EntityType because values of SMDSAbs_EntityType may
290 // change at insertion of new items in the middle.
291 const vector<MED::EGeometrieElement>& medTypes = mesh2medElemType();
295 for ( size_t i = 0; i < meshInfo->length(); ++i )
296 if ( meshInfo[i] > 0 )
298 data.push_back( medTypes[ i ] );
299 data.push_back( meshInfo[ i ] );
304 hdf_size datasetSize[] = { data.size() };
305 HDFarray* anArray = new HDFarray(0, HDF_INT32, 1, datasetSize);
306 anArray->CreateOnDisk();
308 HDFdataset* dataset = new HDFdataset( name.c_str(), hdfGroup, HDF_ARRAY, datasetSize, 1 );
309 dataset->SetArrayId(anArray->GetId());
310 dataset->CreateOnDisk();
311 dataset->WriteOnDisk( & data[0] );
312 dataset->CloseOnDisk();
313 anArray->CloseOnDisk();
318 //================================================================================
320 * \brief Reads meshInfo from a HDF file
322 //================================================================================
324 void SMESH_PreMeshInfo::hdf2meshInfo( const std::string& name,
327 if ( hdfGroup->ExistInternalObject( name.c_str()) )
329 HDFdataset* dataset = new HDFdataset( name.c_str(), hdfGroup );
330 dataset->OpenOnDisk();
332 // // hdf_size datasetSize[ 1 ];
333 // // HDFarray *array = new HDFarray(dataset);
334 // // array->GetDim( datasetSize );
335 // int size = dataset->GetSize();
337 vector<int> info( SMDSEntity_Last * 2, 0 );
338 dataset->ReadFromDisk( &info[0] );
339 dataset->CloseOnDisk();
341 const Tmed2smeshElemTypeMap& med2smesh = med2smeshElemTypeMap();
342 Tmed2smeshElemTypeMap::const_iterator me2sme, me2smeEnd = med2smesh.end();
343 for ( size_t i = 0; i < info.size(); )
345 int medType = info[i++];
346 int nbElems = info[i++];
347 if ( !nbElems ) break;
348 me2sme = med2smesh.find( (MED::EGeometrieElement) medType );
349 if ( me2sme != me2smeEnd )
350 setNb( me2sme->second, nbElems );
355 if ( NbNodes() == GroupOnFilter_OutOfDate ) // case of !SMESHDS_GroupOnFilter::IsUpToDate()
358 setNb( SMDSEntity_Node, 0 );
362 //================================================================================
364 * \brief Constructor callable by SMESH_PreMeshInfo only
366 //================================================================================
368 SMESH_PreMeshInfo::SMESH_PreMeshInfo(SMESH_Mesh_i* mesh,
370 const std::string& medFile,
371 const std::string& hdfFile)
372 : _medFileName( medFile ),
373 _hdfFileName( hdfFile ),
374 _toRemoveFiles( false ),
382 //================================================================================
384 * \brief Release temporary files
386 //================================================================================
388 SMESH_PreMeshInfo::~SMESH_PreMeshInfo()
390 if ( _toRemoveFiles ) // it can be true only for SMESH_PreMeshInfo of the mesh
391 filesNoMoreNeeded( _mesh, _medFileName, _hdfFileName );
393 _toRemoveFiles = false;
396 //================================================================================
398 * \brief fills SMESH_PreMeshInfo field of all objects of mesh
400 //================================================================================
402 void SMESH_PreMeshInfo::LoadFromFile( SMESH_Mesh_i* mesh,
404 const std::string& medFile,
405 const std::string& hdfFile,
406 const bool toRemoveFiles)
410 SMESH_PreMeshInfo* meshPreInfo = new SMESH_PreMeshInfo( mesh,meshID,medFile,hdfFile );
411 mesh->changePreMeshInfo() = meshPreInfo;
413 meshPreInfo->_toRemoveFiles = toRemoveFiles;
415 meshInfoLoaded( mesh );
417 if ( meshPreInfo->readPreInfoFromHDF() )
418 // all SMESH_PreMeshInfo's are stored in HDF file (written after
419 // implementing SMESH_PreMeshInfo)
422 // try to read SMESH_PreMeshInfo from med file (as study is older than SMESH_PreMeshInfo)
423 if ( meshPreInfo->readMeshInfo() )
425 meshPreInfo->readGroupInfo();
426 meshPreInfo->readSubMeshInfo();
430 meshPreInfo->FullLoadFromFile();
432 SMESH_CATCH( SMESH::doNothing );
435 //================================================================================
437 * \brief Tries to read all SMESH_PreMeshInfo from a HDF file
438 * \retval bool - true if succeeded
440 * This method is symmetrical to SaveToFile()
442 //================================================================================
444 bool SMESH_PreMeshInfo::readPreInfoFromHDF()
446 HDFfile* aFile = new HDFfile( (char*) _hdfFileName.c_str() );
447 aFile->OpenOnDisk( HDF_RDONLY );
449 SMESH_Comment hdfGroupName("SMESH_PreMeshInfo"); hdfGroupName << _meshID;
450 const bool infoAvailable = aFile->ExistInternalObject( hdfGroupName );
453 HDFgroup* infoHdfGroup = new HDFgroup( hdfGroupName, aFile );
454 infoHdfGroup->OpenOnDisk();
456 _mesh->changePreMeshInfo()->hdf2meshInfo( "Mesh", infoHdfGroup );
458 // read SMESH_PreMeshInfo of groups
459 map<int, SMESH::SMESH_GroupBase_ptr>::const_iterator i2group = _mesh->_mapGroups.begin();
460 for ( ; i2group != _mesh->_mapGroups.end(); ++i2group )
462 if ( SMESH_GroupBase_i* group_i =
463 SMESH::DownCast<SMESH_GroupBase_i*>( i2group->second ))
465 group_i->changePreMeshInfo() = newInstance();
466 if ( SMESHDS_GroupBase* group = group_i->GetGroupDS() )
468 const string name = group->GetStoreName();
469 group_i->changePreMeshInfo()->hdf2meshInfo( name, infoHdfGroup );
474 // read SMESH_PreMeshInfo of sub-meshes
475 map<int, SMESH::SMESH_subMesh_ptr>::iterator id2sm = _mesh->_mapSubMeshIor.begin();
476 for ( ; id2sm != _mesh->_mapSubMeshIor.end(); ++id2sm )
478 if ( SMESH_subMesh_i* sm = SMESH::DownCast<SMESH_subMesh_i*>( id2sm->second ))
480 sm->changePreMeshInfo() = newInstance();
481 sm->changePreMeshInfo()->hdf2meshInfo( SMESH_Comment( sm->GetId()), infoHdfGroup );
486 aFile->CloseOnDisk();
489 return infoAvailable;
492 //================================================================================
494 * \brief Reads mesh info of mesh from the med file
496 //================================================================================
498 bool SMESH_PreMeshInfo::readMeshInfo()
502 MED::PWrapper aMed = MED::CrWrapper(_medFileName,true);
503 // if ( aMed->GetVersion() != MED::eV2_2 )
506 MED::PMeshInfo medMeshInfo = aMed->CrMeshInfo(3,3,SMESH_Comment( _meshID ));
509 int nbNodes = std::max( 0, aMed->GetNbNodes( medMeshInfo ));
512 setNb( SMDSEntity_Node, nbNodes);
514 // read nb of elements
515 Tmed2smeshElemTypeMap::const_iterator me2sme = med2smeshElemTypeMap().begin();
516 Tmed2smeshElemTypeMap::const_iterator me2smeEnd = med2smeshElemTypeMap().end();
517 for ( ; me2sme != me2smeEnd; ++me2sme )
519 int nbElems = aMed->GetNbCells( medMeshInfo, entityByGeom(me2sme->first), me2sme->first );
521 setNb( me2sme->second, nbElems );
527 //================================================================================
529 * \brief Reads info of groups from the med file
531 //================================================================================
533 void SMESH_PreMeshInfo::readGroupInfo()
535 if ( _mesh->_mapGroups.empty() ) return;
537 // make SMESH_PreMeshInfo of groups
538 map< string, SMESH_PreMeshInfo* > name2GroupInfo;
539 map<int, SMESH::SMESH_GroupBase_ptr>::const_iterator i2group = _mesh->_mapGroups.begin();
540 for ( ; i2group != _mesh->_mapGroups.end(); ++i2group )
542 if ( SMESH_GroupBase_i* group_i =
543 SMESH::DownCast<SMESH_GroupBase_i*>( i2group->second ))
545 SMESH_PreMeshInfo* info = newInstance();
546 group_i->changePreMeshInfo() = info;
547 if ( SMESHDS_Group* group = dynamic_cast< SMESHDS_Group* >( group_i->GetGroupDS() ))
549 string name = group->GetStoreName();
550 name2GroupInfo.insert( make_pair( name, info ));
551 info->_isInfoOk = true;
556 map< int, vector< SMESH_PreMeshInfo* > > famId2grInfo;
558 MED::PWrapper aMed = MED::CrWrapper(_medFileName,false);
559 MED::PMeshInfo medMeshInfo = aMed->CrMeshInfo(3,3,SMESH_Comment( _meshID ));
561 // read families to fill in famId2grInfo
562 int nbFams = aMed->GetNbFamilies( medMeshInfo );
563 if ( nbFams <= 1 ) return; // zero family is always present
564 for ( int iF = 0; iF <= nbFams; ++iF )
566 int nbGroups = aMed->GetNbFamGroup( iF, medMeshInfo );
567 if ( nbGroups < 1 ) continue;
568 MED::PFamilyInfo medFamInfo = aMed->CrFamilyInfo( medMeshInfo, nbGroups, nbGroups );
569 aMed->GetFamilyInfo( iF, medFamInfo ); // read groups of a family
570 vector< SMESH_PreMeshInfo* >& grInfoVec = famId2grInfo[ medFamInfo->GetId() ];
571 for ( int iG = 0; iG < nbGroups; ++iG )
573 const string grName = medFamInfo->GetGroupName( iG );
574 map< string, SMESH_PreMeshInfo* >::iterator n2i = name2GroupInfo.find( grName );
575 if ( n2i != name2GroupInfo.end() )
576 grInfoVec.push_back( n2i->second );
580 // read family numbers of elements
581 Tmed2smeshElemTypeMap::const_iterator me2sme = med2smeshElemTypeMap().begin();
582 Tmed2smeshElemTypeMap::const_iterator me2smeEnd = med2smeshElemTypeMap().end();
583 MED::PElemInfo medElemInfo = aMed->CrElemInfo( medMeshInfo, 0 );
584 MED::TIntVector& famNums = medElemInfo->myFamNum;
585 for ( ; me2sme != me2smeEnd; ++me2sme ) // loop on elem types
587 famNums.resize( NbEntities( me2sme->second ));
588 if ( famNums.empty() ) continue;
589 aMed->GetFamilies( medElemInfo, famNums.size(), entityByGeom(me2sme->first), me2sme->first );
590 // distribute elements of a type among groups
591 map< int, vector< SMESH_PreMeshInfo* > >::iterator f2infos = famId2grInfo.begin();
592 for ( size_t i = 0; i < famNums.size(); ++i )
594 if ( famNums[i] != f2infos->first )
596 f2infos = famId2grInfo.find( famNums[i] );
597 if ( f2infos == famId2grInfo.end() )
598 f2infos = famId2grInfo.insert
599 ( make_pair( famNums[i], vector< SMESH_PreMeshInfo*>())).first;
601 vector< SMESH_PreMeshInfo* >& infoVec = f2infos->second ;
602 for ( size_t j = 0; j < infoVec.size(); ++j )
603 infoVec[j]->_elemCounter++;
605 // pass _elemCounter to a real elem type
606 map< string, SMESH_PreMeshInfo* >::iterator n2i = name2GroupInfo.begin();
607 for ( ; n2i != name2GroupInfo.end(); ++n2i )
609 SMESH_PreMeshInfo* info = n2i->second;
610 info->setNb( me2sme->second, info->_elemCounter );
611 info->_elemCounter = 0;
616 //================================================================================
618 * \brief Reads info of sub-meshes from hdf file of old study
620 //================================================================================
622 void SMESH_PreMeshInfo::readSubMeshInfo()
624 if ( _mesh->_mapSubMeshIor.empty() ) return;
626 // create SMESH_PreMeshInfo of sub-meshes
627 map<int, SMESH::SMESH_subMesh_ptr>::iterator id2sm = _mesh->_mapSubMeshIor.begin();
628 for ( ; id2sm != _mesh->_mapSubMeshIor.end(); ++id2sm )
630 if ( SMESH_subMesh_i* sm = SMESH::DownCast<SMESH_subMesh_i*>( id2sm->second ))
632 sm->changePreMeshInfo() = newInstance();
633 sm->changePreMeshInfo()->_isInfoOk = true;
638 HDFfile* aFile = new HDFfile( (char*) _hdfFileName.c_str() );
639 aFile->OpenOnDisk( HDF_RDONLY );
641 char meshGrpName[ 30 ];
642 sprintf( meshGrpName, "Mesh %d", _meshID );
643 if ( aFile->ExistInternalObject( meshGrpName ) )
645 HDFgroup* aTopGroup = new HDFgroup( meshGrpName, aFile );
646 aTopGroup->OpenOnDisk();
647 if ( aTopGroup->ExistInternalObject( "Submeshes" ))
649 HDFgroup* aGroup = new HDFgroup( "Submeshes", aTopGroup );
650 aGroup->OpenOnDisk();
652 SMESHDS_Mesh* meshDS = _mesh->GetImpl().GetMeshDS();
653 int maxSmId = Max( meshDS->MaxSubMeshIndex(), meshDS->MaxShapeIndex() );
655 for ( int isNode = 0; isNode < 2; ++isNode )
657 string aDSName( isNode ? "Node Submeshes" : "Element Submeshes");
658 if ( aGroup->ExistInternalObject( (char*) aDSName.c_str() ))
660 // read sub-mesh id of all nodes or elems
661 HDFdataset* aDataset = new HDFdataset( (char*) aDSName.c_str(), aGroup );
662 aDataset->OpenOnDisk();
663 int nbElems = aDataset->GetSize();
664 int* smIDs = new int [ nbElems ];
665 aDataset->ReadFromDisk( smIDs );
666 aDataset->CloseOnDisk();
667 // count nb elems in each sub-mesh
668 vector<int> nbBySubmeshId( maxSmId + 1, 0 );
669 for ( int i = 0; i < nbElems; ++i )
671 const int smID = smIDs[ i ];
672 if ( smID < (int) nbBySubmeshId.size() )
673 nbBySubmeshId[ smID ]++;
677 // store nb elems in SMESH_PreMeshInfo of sub-meshes
678 map<int, SMESH::SMESH_subMesh_ptr>::iterator id2sm = _mesh->_mapSubMeshIor.begin();
679 for ( ; id2sm != _mesh->_mapSubMeshIor.end(); ++id2sm )
681 if ( SMESH_subMesh_i* sm = SMESH::DownCast<SMESH_subMesh_i*>( id2sm->second ))
683 SMESH_PreMeshInfo* & info = sm->changePreMeshInfo();
685 vector<int> smIds = getSimpleSubMeshIds( meshDS, id2sm->first );
686 for ( size_t i = 0; i < smIds.size(); ++i )
687 info->_elemCounter += nbBySubmeshId[ smIds[i] ];
689 SMDSAbs_EntityType elemType;
692 elemType = SMDSEntity_Node;
696 bool koElemType = false;
697 const TopoDS_Shape& shape = meshDS->IndexToShape( smIds[0] );
698 elemType = getElemType( shape.ShapeType(), info->_elemCounter, koElemType );
699 info->_isInfoOk = !koElemType;
701 info->setNb( elemType, info->_elemCounter );
704 } // if ( aGroup->ExistInternalObject( aDSName ))
705 } // for ( int isNode = 0; isNode < 2; ++isNode )
707 aGroup->CloseOnDisk();
708 } // if ( aTopGroup->ExistInternalObject( "Submeshes" ))
710 aTopGroup->CloseOnDisk();
711 } // if ( aFile->ExistInternalObject( meshGrpName ) )
713 aFile->CloseOnDisk();
717 //================================================================================
719 * \brief Return type of element for sub-mesh on a shape of given type
721 //================================================================================
723 SMDSAbs_EntityType SMESH_PreMeshInfo::getElemType( const TopAbs_ShapeEnum shapeType,
724 const int nbElemsInSubMesh,
725 bool& isKoType) const
729 SMESH_PreMeshInfo* meshInfo = _mesh->changePreMeshInfo();
734 type = SMDSEntity_Tetra;
735 typeEnd = SMDSEntity_Last;
736 isKoType = ( meshInfo->NbVolumes() != nbElemsInSubMesh );
740 type = SMDSEntity_Triangle;
741 typeEnd = SMDSEntity_Tetra;
742 isKoType = ( meshInfo->NbFaces() != nbElemsInSubMesh );
745 case TopAbs_EDGE: return SMDSEntity_Edge;
746 case TopAbs_VERTEX: return SMDSEntity_0D;
747 default: return SMDSEntity_Last;
752 for ( int t = type; t < typeEnd; ++t )
753 if ( nbElemsInSubMesh == meshInfo->NbEntities( SMDSAbs_EntityType( t )))
754 return SMDSAbs_EntityType( t );
757 return SMDSAbs_EntityType( type );
760 //================================================================================
762 * \brief Saves SMESH_PreMeshInfo to the study file
764 //================================================================================
766 void SMESH_PreMeshInfo::SaveToFile( SMESH_Mesh_i* mesh,
770 // create a HDF group for SMESH_PreMeshInfo of this mesh
771 SMESH_Comment hdfGroupName("SMESH_PreMeshInfo"); hdfGroupName << meshID;
772 HDFgroup* infoHdfGroup = new HDFgroup( hdfGroupName, hdfFile );
773 infoHdfGroup->CreateOnDisk();
778 meshInfo2hdf( mesh->GetMeshInfo(), "Mesh", infoHdfGroup );
781 SMESH_PreMeshInfo incompleteInfo( 0,0,"","");
782 incompleteInfo.setNb( SMDSEntity_Node, GroupOnFilter_OutOfDate );
783 SMESHDS_Mesh* meshDS = mesh->GetImpl().GetMeshDS();
785 map<int, SMESH::SMESH_GroupBase_ptr>::const_iterator i2group = mesh->_mapGroups.begin();
786 for ( ; i2group != mesh->_mapGroups.end(); ++i2group )
788 if ( SMESH_GroupBase_i* group_i = SMESH::DownCast<SMESH_GroupBase_i*>( i2group->second ))
790 SMESHDS_GroupBase * group = group_i->GetGroupDS();
791 if ( SMESHDS_GroupOnFilter* gof = dynamic_cast<SMESHDS_GroupOnFilter*>(group))
793 // prevent too long storage time due to applying filter to many elements
794 if ( !gof->IsUpToDate() && meshDS->GetMeshInfo().NbElements( gof->GetType() ) > 1e5 )
796 meshInfo2hdf( incompleteInfo.GetMeshInfo(),
797 group->GetStoreName(),
802 meshInfo2hdf( group_i->GetMeshInfo(), group->GetStoreName(), infoHdfGroup);
806 // info of sub-meshes
807 map<int, SMESH::SMESH_subMesh_ptr>::iterator id2sm = mesh->_mapSubMeshIor.begin();
808 for ( ; id2sm != mesh->_mapSubMeshIor.end(); ++id2sm )
810 if ( SMESH_subMesh_i* sm = SMESH::DownCast<SMESH_subMesh_i*>( id2sm->second ))
812 meshInfo2hdf( sm->GetMeshInfo(),
813 SMESH_Comment( sm->GetId() ),
818 SMESH_CATCH( SMESH::doNothing );
820 infoHdfGroup->CloseOnDisk();
823 //================================================================================
825 * \brief Reads all data and remove all SMESH_PreMeshInfo fields from objects
827 //================================================================================
829 void SMESH_PreMeshInfo::FullLoadFromFile() const
831 SignalToGUI signalOnLoading( _mesh );
833 SMESH_PreMeshInfo* meshInfo = _mesh->changePreMeshInfo();
834 _mesh->changePreMeshInfo() = NULL; // to allow GUI accessing to real info
836 ::SMESH_Mesh& mesh = _mesh->GetImpl();
837 SMESHDS_Mesh* meshDS = mesh.GetMeshDS();
841 MYDEBUGOUT( "BEG FullLoadFromFile() " << _meshID );
844 DriverMED_R_SMESHDS_Mesh myReader;
845 myReader.SetFile( _medFileName.c_str() );
846 myReader.SetMesh( meshDS );
847 myReader.SetMeshId( _meshID );
851 const set<SMESHDS_GroupBase*>& groups = meshDS->GetGroups();
852 set<SMESHDS_GroupBase*>::const_iterator groupIt = groups.begin();
853 for ( ; groupIt != groups.end(); ++groupIt )
854 if ( SMESHDS_Group* aGrp = dynamic_cast<SMESHDS_Group*>( *groupIt ))
855 myReader.GetGroup( aGrp );
858 readSubMeshes( &myReader );
860 SMESH_CATCH( SMESH::doNothing );
862 _mesh->changePreMeshInfo() = meshInfo;
866 signalOnLoading.sendStop();
870 // load dependent meshes referring/referred via hypotheses
871 mesh.GetSubMesh( mesh.GetShapeToMesh() )->
872 ComputeStateEngine (SMESH_subMesh::SUBMESH_LOADED);
874 MYDEBUGOUT( "END FullLoadFromFile()" );
877 //================================================================================
879 * \brief Reads full data of sub-meshes
881 //================================================================================
883 void SMESH_PreMeshInfo::readSubMeshes(DriverMED_R_SMESHDS_Mesh* reader) const
885 HDFfile* aFile = new HDFfile( (char*) _hdfFileName.c_str() );
886 aFile->OpenOnDisk( HDF_RDONLY );
888 char meshGrpName[ 30 ];
889 sprintf( meshGrpName, "Mesh %d", _meshID );
890 if ( aFile->ExistInternalObject( meshGrpName ) )
892 HDFgroup* aTopGroup = new HDFgroup( meshGrpName, aFile );
893 aTopGroup->OpenOnDisk();
895 SMESHDS_Mesh* meshDS = _mesh->GetImpl().GetMeshDS();
897 // issue 0020693. Restore _isModified flag
898 if ( aTopGroup->ExistInternalObject( "_isModified" ))
900 HDFdataset* aDataset = new HDFdataset( "_isModified", aTopGroup );
901 aDataset->OpenOnDisk();
902 hdf_size size = aDataset->GetSize();
903 int* isModified = new int[ size ];
904 aDataset->ReadFromDisk( isModified );
905 aDataset->CloseOnDisk();
906 _mesh->GetImpl().SetIsModified( bool(*isModified));
907 delete [] isModified;
910 bool submeshesInFamilies = ( ! aTopGroup->ExistInternalObject( "Submeshes" ));
911 if ( submeshesInFamilies ) // from MED
913 // old way working before fix of PAL 12992
914 reader->CreateAllSubMeshes();
919 HDFgroup* aGroup = new HDFgroup( "Submeshes", aTopGroup );
920 aGroup->OpenOnDisk();
922 int maxID = Max( meshDS->MaxSubMeshIndex(), meshDS->MaxShapeIndex() );
923 vector< SMESHDS_SubMesh * > subMeshes( maxID + 1, (SMESHDS_SubMesh*) 0 );
924 vector< TopAbs_ShapeEnum > smType ( maxID + 1, TopAbs_SHAPE );
926 PositionCreator aPositionCreator;
928 SMDS_NodeIteratorPtr nIt = meshDS->nodesIterator();
929 SMDS_ElemIteratorPtr eIt = meshDS->elementsIterator();
930 for ( int isNode = 0; isNode < 2; ++isNode )
932 string aDSName( isNode ? "Node Submeshes" : "Element Submeshes");
933 if ( aGroup->ExistInternalObject( (char*) aDSName.c_str() ))
935 HDFdataset* aDataset = new HDFdataset( (char*) aDSName.c_str(), aGroup );
936 aDataset->OpenOnDisk();
937 // read submesh IDs for all elements sorted by ID
938 int nbElems = aDataset->GetSize();
939 int* smIDs = new int [ nbElems ];
940 aDataset->ReadFromDisk( smIDs );
941 aDataset->CloseOnDisk();
943 // get elements sorted by ID
944 TIDSortedElemSet elemSet;
946 while ( nIt->more() ) elemSet.insert( elemSet.end(), nIt->next() );
948 while ( eIt->more() ) elemSet.insert( elemSet.end(), eIt->next() );
949 //ASSERT( elemSet.size() == nbElems ); -- issue 20182
950 // -- Most probably a bad study was saved when there were
951 // not fixed bugs in SMDS_MeshInfo
952 if ( elemSet.size() < nbElems ) {
954 cout << "SMESH_Gen_i::Load(), warning: Node position data is invalid" << endl;
956 nbElems = elemSet.size();
958 // add elements to submeshes
959 TIDSortedElemSet::iterator iE = elemSet.begin();
960 for ( int i = 0; i < nbElems; ++i, ++iE )
962 int smID = smIDs[ i ];
963 if ( smID == 0 ) continue;
964 const SMDS_MeshElement* elem = *iE;
965 if ( smID > maxID ) {
966 // corresponding subshape no longer exists: maybe geom group has been edited
967 if ( _mesh->GetImpl().HasShapeToMesh() )
968 meshDS->RemoveElement( elem );
971 // get or create submesh
972 SMESHDS_SubMesh* & sm = subMeshes[ smID ];
974 sm = meshDS->NewSubMesh( smID );
975 smType[ smID ] = meshDS->IndexToShape( smID ).ShapeType();
979 SMDS_PositionPtr pos = aPositionCreator.MakePosition( smType[ smID ]);
980 SMDS_MeshNode* node = const_cast<SMDS_MeshNode*>( static_cast<const SMDS_MeshNode*>( elem ));
981 node->SetPosition( pos );
984 sm->AddElement( elem );
990 } // end reading submeshes
992 // Read node positions on sub-shapes (SMDS_Position)
994 if ( aTopGroup->ExistInternalObject( "Node Positions" ))
996 // There are 5 datasets to read:
997 // "Nodes on Edges" - ID of node on edge
998 // "Edge positions" - U parameter on node on edge
999 // "Nodes on Faces" - ID of node on face
1000 // "Face U positions" - U parameter of node on face
1001 // "Face V positions" - V parameter of node on face
1002 const char* aEid_DSName = "Nodes on Edges";
1003 const char* aEu_DSName = "Edge positions";
1004 const char* aFu_DSName = "Face U positions";
1005 //char* aFid_DSName = "Nodes on Faces";
1006 //char* aFv_DSName = "Face V positions";
1009 int nbEids = 0, nbFids = 0;
1010 int *aEids = 0, *aFids = 0;
1011 double *aEpos = 0, *aFupos = 0, *aFvpos = 0;
1014 HDFgroup* aGroup = new HDFgroup( "Node Positions", aTopGroup );
1015 aGroup->OpenOnDisk();
1017 // loop on 5 data sets
1018 int aNbObjects = aGroup->nInternalObjects();
1019 for ( int i = 0; i < aNbObjects; i++ )
1022 char aDSName[ HDF_NAME_MAX_LEN+1 ];
1023 aGroup->InternalObjectIndentify( i, aDSName );
1025 HDFdataset* aDataset = new HDFdataset( aDSName, aGroup );
1026 aDataset->OpenOnDisk();
1027 if ( aDataset->GetType() == HDF_FLOAT64 ) // Positions
1029 double* pos = new double [ aDataset->GetSize() ];
1030 aDataset->ReadFromDisk( pos );
1032 if ( strncmp( aDSName, aEu_DSName, strlen( aEu_DSName )) == 0 )
1034 else if ( strncmp( aDSName, aFu_DSName, strlen( aFu_DSName )) == 0 )
1041 int aSize = aDataset->GetSize();
1043 // for reading files, created from 18.07.2005 till 10.10.2005
1044 if (aDataset->GetType() == HDF_STRING)
1045 aSize /= sizeof(int);
1047 int* ids = new int [aSize];
1048 aDataset->ReadFromDisk( ids );
1049 // on face or nodes?
1050 if ( strncmp( aDSName, aEid_DSName, strlen( aEid_DSName )) == 0 ) {
1059 aDataset->CloseOnDisk();
1060 } // loop on 5 datasets
1062 // Set node positions on edges or faces
1063 for ( int onFace = 0; onFace < 2; onFace++ )
1065 int nbNodes = ( onFace ? nbFids : nbEids );
1066 if ( nbNodes == 0 ) continue;
1067 int* aNodeIDs = ( onFace ? aFids : aEids );
1068 double* aUPos = ( onFace ? aFupos : aEpos );
1069 double* aVPos = ( onFace ? aFvpos : 0 );
1071 for ( int iNode = 0; iNode < nbNodes; iNode++ )
1073 const SMDS_MeshNode* node = meshDS->FindNode( aNodeIDs[ iNode ]);
1074 if ( !node ) continue; // maybe removed while Loading() if geometry changed
1075 SMDS_PositionPtr aPos = node->GetPosition();
1078 // ASSERT( aPos->GetTypeOfPosition() == SMDS_TOP_FACE );-- issue 20182
1079 // -- Most probably a bad study was saved when there were
1080 // not fixed bugs in SMDS_MeshInfo
1081 if ( aPos->GetTypeOfPosition() == SMDS_TOP_FACE ) {
1082 SMDS_FacePosition* fPos = const_cast<SMDS_FacePosition*>
1083 ( static_cast<const SMDS_FacePosition*>( aPos ));
1084 fPos->SetUParameter( aUPos[ iNode ]);
1085 fPos->SetVParameter( aVPos[ iNode ]);
1089 // ASSERT( aPos->GetTypeOfPosition() == SMDS_TOP_EDGE );-- issue 20182
1090 if ( aPos->GetTypeOfPosition() == SMDS_TOP_EDGE ) {
1091 SMDS_EdgePosition* fPos = const_cast<SMDS_EdgePosition*>
1092 ( static_cast<const SMDS_EdgePosition*>( aPos ));
1093 fPos->SetUParameter( aUPos[ iNode ]);
1098 if ( aEids ) delete [] aEids;
1099 if ( aFids ) delete [] aFids;
1100 if ( aEpos ) delete [] aEpos;
1101 if ( aFupos ) delete [] aFupos;
1102 if ( aFvpos ) delete [] aFvpos;
1104 aGroup->CloseOnDisk();
1106 } // if ( aTopGroup->ExistInternalObject( "Node Positions" ) )
1108 aTopGroup->CloseOnDisk();
1109 } // if ( aFile->ExistInternalObject( meshGrpName ) )
1111 aFile->CloseOnDisk();
1115 //================================================================================
1117 * \brief Remove all SMESH_PreMeshInfo fields from objects w/o data loading
1119 //================================================================================
1121 void SMESH_PreMeshInfo::ForgetAllData() const
1125 if ( _mesh->changePreMeshInfo() != this )
1126 return _mesh->changePreMeshInfo()->ForgetAllData();
1128 // remove SMESH_PreMeshInfo from groups
1129 map<int, SMESH::SMESH_GroupBase_ptr>::const_iterator i2group = _mesh->_mapGroups.begin();
1130 for ( ; i2group != _mesh->_mapGroups.end(); ++i2group )
1132 if ( SMESH_GroupBase_i* group_i =
1133 SMESH::DownCast<SMESH_GroupBase_i*>( i2group->second ))
1135 SMESH_PreMeshInfo* & info = group_i->changePreMeshInfo();
1140 // remove SMESH_PreMeshInfo from sub-meshes
1141 map<int, SMESH::SMESH_subMesh_ptr>::iterator id2sm = _mesh->_mapSubMeshIor.begin();
1142 for ( ; id2sm != _mesh->_mapSubMeshIor.end(); ++id2sm )
1144 if ( SMESH_subMesh_i* sm_i = SMESH::DownCast<SMESH_subMesh_i*>( id2sm->second ))
1146 SMESH_PreMeshInfo* & info = sm_i->changePreMeshInfo();
1151 // remove SMESH_PreMeshInfo from the mesh
1152 _mesh->changePreMeshInfo() = NULL;
1155 SMESH_CATCH( SMESH::doNothing );
1162 // ::SMESH_Mesh& mesh = _mesh->GetImpl();
1164 // // update hyps needing full mesh data restored (issue 20918)
1165 // // map<int, SMESH::SMESH_Hypothesis_ptr>::iterator id2hyp= _mesh->_mapHypo.begin();
1166 // // for ( ; id2hyp != _mesh->_mapHypo.end(); ++id2hyp )
1167 // // if ( SMESH_Hypothesis_i* hyp = SMESH::DownCast<SMESH_Hypothesis_i*>( id2hyp->second ))
1168 // // hyp->UpdateAsMeshesRestored();
1171 // SMESH_CATCH( SMESH::doNothing );
1174 //================================================================================
1176 * \brief remove all SMESH_PreMeshInfo fields from mesh and its child objects w/o data loading
1178 //================================================================================
1180 void SMESH_PreMeshInfo::ForgetAllData( SMESH_Mesh_i* mesh )
1182 if ( mesh && mesh->changePreMeshInfo() )
1183 mesh->changePreMeshInfo()->ForgetAllData();
1186 //================================================================================
1188 * \brief Calls either FullLoadFromFile() or ForgetAllData() depending on preferences
1190 //================================================================================
1192 void SMESH_PreMeshInfo::ForgetOrLoad() const
1194 if ( SMESH_Gen_i::GetSMESHGen()->ToForgetMeshDataOnHypModif() &&
1195 _mesh->HasShapeToMesh())
1201 //================================================================================
1203 * \brief Method of SMESH_IDSource interface
1205 //================================================================================
1207 SMESH::array_of_ElementType* SMESH_PreMeshInfo::GetTypes() const
1209 SMESH::array_of_ElementType_var types = new SMESH::array_of_ElementType;
1213 if (NbEdges()) types[nbTypes++] = SMESH::EDGE;
1214 if (NbFaces()) types[nbTypes++] = SMESH::FACE;
1215 if (NbVolumes()) types[nbTypes++] = SMESH::VOLUME;
1216 if (Nb0DElements()) types[nbTypes++] = SMESH::ELEM0D;
1217 if (NbBalls()) types[nbTypes++] = SMESH::BALL;
1218 types->length( nbTypes );
1220 return types._retn();
1223 //================================================================================
1225 * \brief Method of SMESH_IDSource interface returning nb elements by element type
1227 //================================================================================
1229 SMESH::long_array* SMESH_PreMeshInfo::GetMeshInfo() const
1231 SMESH::long_array_var aRes = new SMESH::long_array();
1232 aRes->length(SMESH::Entity_Last);
1233 for (int i = SMESH::Entity_Node; i < SMESH::Entity_Last; i++)
1236 for (int i = SMESH::Entity_Node; i < SMESH::Entity_Last; i++)
1237 aRes[i] = NbEntities((SMDSAbs_EntityType)i);
1238 return aRes._retn();
1241 //================================================================================
1243 * Returns false if GetMeshInfo() returns incorrect information that may
1244 * happen if mesh data is not yet fully loaded from the file of study.
1246 //================================================================================
1248 bool SMESH_PreMeshInfo::IsMeshInfoCorrect() const
1253 //================================================================================
1255 * \brief TEMPORARY method to remove study files on closing study;
1256 * RIGHT WAY: study files are remove automatically when meshes are destroyed
1258 //================================================================================
1260 void SMESH_PreMeshInfo::RemoveStudyFiles_TMP_METHOD(SALOMEDS::SComponent_ptr smeshComp)
1262 SALOMEDS::Study_var study = smeshComp->GetStudy();
1263 if ( theStudyIDToMeshCounter[ (int) study->StudyId() ] > 0 )
1265 SALOMEDS::ChildIterator_wrap itBig = study->NewChildIterator( smeshComp );
1266 for ( ; itBig->More(); itBig->Next() ) {
1267 SALOMEDS::SObject_wrap gotBranch = itBig->Value();
1268 CORBA::Object_var anObject = SMESH_Gen_i::SObjectToObject( gotBranch );
1269 if ( SMESH_Mesh_i* mesh = SMESH::DownCast<SMESH_Mesh_i*>( anObject ))
1271 if ( mesh->changePreMeshInfo() )
1273 mesh->changePreMeshInfo()->ForgetAllData();