1 // Copyright (C) 2007-2023 CEA, EDF, 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
23 // File : SMESH_Mesh.cxx
24 // Author : Paul RASCLE, EDF
27 #include "SMESH_Mesh.hxx"
28 #include "SMESH_MesherHelper.hxx"
29 #include "SMDS_MeshVolume.hxx"
30 #include "SMDS_SetIterator.hxx"
31 #include "SMESHDS_Document.hxx"
32 #include "SMESHDS_Group.hxx"
33 #include "SMESHDS_GroupOnGeom.hxx"
34 #include "SMESHDS_Script.hxx"
35 #include "SMESHDS_TSubMeshHolder.hxx"
36 #include "SMESH_Gen.hxx"
37 #include "SMESH_Group.hxx"
38 #include "SMESH_HypoFilter.hxx"
39 #include "SMESH_Hypothesis.hxx"
40 #include "SMESH_subMesh.hxx"
42 #include "utilities.h"
44 #include "DriverDAT_W_SMDS_Mesh.h"
45 #include "DriverGMF_Read.hxx"
46 #include "DriverGMF_Write.hxx"
47 #include "DriverMED_R_SMESHDS_Mesh.h"
48 #include "DriverMED_W_SMESHDS_Mesh.h"
49 #include "DriverSTL_R_SMDS_Mesh.h"
50 #include "DriverSTL_W_SMDS_Mesh.h"
51 #include "DriverUNV_R_SMDS_Mesh.h"
52 #include "DriverUNV_W_SMDS_Mesh.h"
54 #include "DriverCGNS_Read.hxx"
55 #include "DriverCGNS_Write.hxx"
58 #include <GEOMUtils.hxx>
60 //#undef _Precision_HeaderFile
61 #include <BRepBndLib.hxx>
62 #include <BRepPrimAPI_MakeBox.hxx>
63 #include <Bnd_Box.hxx>
64 #include <TColStd_MapOfInteger.hxx>
66 #include <TopExp_Explorer.hxx>
67 #include <TopTools_ListIteratorOfListOfShape.hxx>
68 #include <TopTools_ListOfShape.hxx>
69 #include <TopTools_MapOfShape.hxx>
70 #include <TopoDS_Iterator.hxx>
72 #include "SMESH_TryCatch.hxx" // include after OCCT headers!
74 #include "Utils_ExceptHandlers.hxx"
77 #include <boost/thread/thread.hpp>
78 #include <boost/bind.hpp>
84 #include <boost/filesystem.hpp>
85 namespace fs=boost::filesystem;
88 // maximum stored group name length in MED file
89 #define MAX_MED_GROUP_NAME_LENGTH 80
91 #define cSMESH_Hyp(h) static_cast<const SMESH_Hypothesis*>(h)
93 typedef SMESH_HypoFilter THypType;
95 class SMESH_Mesh::SubMeshHolder : public SMESHDS_TSubMeshHolder< SMESH_subMesh >
99 //=============================================================================
103 //=============================================================================
105 SMESH_Mesh::SMESH_Mesh(int theLocalId,
107 bool theIsEmbeddedMode,
108 SMESHDS_Document* theDocument):
109 _groupId( 0 ), _nbSubShapes( 0 )
111 MESSAGE("SMESH_Mesh::SMESH_Mesh(int localId)");
114 _document = theDocument;
115 _meshDS = theDocument->NewMesh(theIsEmbeddedMode,theLocalId);
116 _isShapeToMesh = false;
117 _isAutoColor = false;
119 _shapeDiagonal = 0.0;
121 _meshDS->ShapeToMesh( PseudoShape() );
122 _subMeshHolder = new SubMeshHolder;
124 // assure unique persistent ID
125 if ( _document->NbMeshes() > 1 )
128 for ( _document->InitMeshesIterator(); _document->MoreMesh(); )
130 SMESHDS_Mesh * meshDS =_document->NextMesh();
131 if ( meshDS != _meshDS )
132 ids.insert( meshDS->GetPersistentId() );
135 if ( ids.count( _meshDS->GetPersistentId() ))
137 int uniqueID = *ids.rbegin() + 1;
138 _meshDS->SetPersistentId( uniqueID );
143 //================================================================================
145 * \brief Constructor of SMESH_Mesh being a base of some descendant class
147 //================================================================================
149 SMESH_Mesh::SMESH_Mesh():
153 _isShapeToMesh( false ),
157 _isAutoColor( false ),
158 _isModified( false ),
159 _shapeDiagonal( 0.0 ),
162 _subMeshHolder = new SubMeshHolder;
168 void deleteMeshDS(SMESHDS_Mesh* meshDS)
173 static void* deleteMeshDS(void* meshDS)
175 SMESHDS_Mesh* m = (SMESHDS_Mesh*)meshDS;
184 //=============================================================================
188 //=============================================================================
190 SMESH_Mesh::~SMESH_Mesh()
192 MESSAGE("SMESH_Mesh::~SMESH_Mesh");
194 if ( _document ) // avoid destructing _meshDS from ~SMESH_Gen()
195 _document->RemoveMesh( _id );
198 // remove self from studyContext
201 StudyContextStruct * studyContext = _gen->GetStudyContext();
202 studyContext->mapMesh.erase( _id );
205 _meshDS->ClearMesh();
207 // issue 0020340: EDF 1022 SMESH : Crash with FindNodeClosestTo in a second new study
208 // Notify event listeners at least that something happens
209 if ( SMESH_subMesh * sm = GetSubMeshContaining(1))
210 sm->ComputeStateEngine( SMESH_subMesh::MESH_ENTITY_REMOVED );
213 std::map < int, SMESH_Group * >::iterator itg;
214 for (itg = _mapGroup.begin(); itg != _mapGroup.end(); itg++) {
215 SMESH_Group *aGroup = (*itg).second;
221 delete _subMeshHolder;
223 if ( _callUp) delete _callUp;
227 // delete _meshDS, in a thread in order not to block closing a study with large meshes
229 boost::thread aThread(boost::bind( & deleteMeshDS, _meshDS ));
232 int result=pthread_create(&thread, NULL, deleteMeshDS, (void*)_meshDS);
237 //================================================================================
239 * \brief Return true if a mesh with given id exists
241 //================================================================================
243 bool SMESH_Mesh::MeshExists( int meshId ) const
245 return _document ? bool( _document->GetMesh( meshId )) : false;
248 //================================================================================
250 * \brief Return a mesh by id
252 //================================================================================
254 SMESH_Mesh* SMESH_Mesh::FindMesh( int meshId ) const
257 return (SMESH_Mesh*) this;
259 if ( StudyContextStruct *aStudyContext = _gen->GetStudyContext())
261 std::map < int, SMESH_Mesh * >::iterator i_m = aStudyContext->mapMesh.find( meshId );
262 if ( i_m != aStudyContext->mapMesh.end() )
268 //=============================================================================
270 * \brief Set geometry to be meshed
272 //=============================================================================
274 void SMESH_Mesh::ShapeToMesh(const TopoDS_Shape & aShape)
276 MESSAGE("SMESH_Mesh::ShapeToMesh");
278 if ( !aShape.IsNull() && _isShapeToMesh ) {
279 if ( aShape.ShapeType() != TopAbs_COMPOUND && // group contents is allowed to change
280 _meshDS->ShapeToMesh().ShapeType() != TopAbs_COMPOUND )
281 throw SALOME_Exception(LOCALIZED ("a shape to mesh has already been defined"));
283 // clear current data
284 if ( !_meshDS->ShapeToMesh().IsNull() )
286 // removal of a shape to mesh, delete objects referring to sub-shapes:
288 _subMeshHolder->DeleteAll();
289 // - groups on geometry
290 std::map <int, SMESH_Group *>::iterator i_gr = _mapGroup.begin();
291 while ( i_gr != _mapGroup.end() ) {
292 if ( dynamic_cast<SMESHDS_GroupOnGeom*>( i_gr->second->GetGroupDS() )) {
293 _meshDS->RemoveGroup( i_gr->second->GetGroupDS() );
295 _mapGroup.erase( i_gr++ );
300 _mapAncestors.Clear();
303 TopoDS_Shape aNullShape;
304 _meshDS->ShapeToMesh( aNullShape );
306 _shapeDiagonal = 0.0;
309 // set a new geometry
310 if ( !aShape.IsNull() )
312 _meshDS->ShapeToMesh(aShape);
313 _isShapeToMesh = true;
314 _nbSubShapes = _meshDS->MaxShapeIndex();
316 // fill map of ancestors
317 fillAncestorsMap(aShape);
321 _isShapeToMesh = false;
322 _shapeDiagonal = 0.0;
323 _meshDS->ShapeToMesh( PseudoShape() );
328 //=======================================================================
330 * \brief Return geometry to be meshed. (It may be a PseudoShape()!)
332 //=======================================================================
334 TopoDS_Shape SMESH_Mesh::GetShapeToMesh() const
336 return _meshDS->ShapeToMesh();
339 //=======================================================================
341 * \brief Return a solid which is returned by GetShapeToMesh() if
342 * a real geometry to be meshed was not set
344 //=======================================================================
346 const TopoDS_Solid& SMESH_Mesh::PseudoShape()
348 static TopoDS_Solid aSolid;
349 if ( aSolid.IsNull() )
351 aSolid = BRepPrimAPI_MakeBox(1,1,1);
356 //=======================================================================
358 * \brief Return diagonal size of bounding box of a shape
360 //=======================================================================
362 double SMESH_Mesh::GetShapeDiagonalSize(const TopoDS_Shape & aShape)
364 if ( !aShape.IsNull() ) {
366 // avoid too long waiting on large shapes. PreciseBoundingBox() was added
367 // to assure same result which else depends on presence of triangulation (IPAL52557).
368 const int maxNbFaces = 4000;
370 for ( TopExp_Explorer f( aShape, TopAbs_FACE ); f.More() && nbFaces < maxNbFaces; f.Next() )
372 bool isPrecise = false;
373 if ( nbFaces < maxNbFaces )
376 GEOMUtils::PreciseBoundingBox( aShape, Box );
384 BRepBndLib::Add( aShape, Box );
387 return sqrt( Box.SquareExtent() );
392 //=======================================================================
394 * \brief Return diagonal size of bounding box of shape to mesh
396 //=======================================================================
398 double SMESH_Mesh::GetShapeDiagonalSize() const
400 if ( _shapeDiagonal == 0. && _isShapeToMesh )
401 const_cast<SMESH_Mesh*>(this)->_shapeDiagonal = GetShapeDiagonalSize( GetShapeToMesh() );
403 return _shapeDiagonal;
406 //================================================================================
408 * \brief Load mesh from study file
410 //================================================================================
412 void SMESH_Mesh::Load()
418 //=======================================================================
420 * \brief Remove all nodes and elements
422 //=======================================================================
424 void SMESH_Mesh::Clear()
426 if ( HasShapeToMesh() ) // remove all nodes and elements
429 _meshDS->ClearMesh();
431 // update compute state of submeshes
432 if ( SMESH_subMesh *sm = GetSubMeshContaining( GetShapeToMesh() ) )
434 sm->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
435 sm->ComputeSubMeshStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
436 sm->ComputeStateEngine( SMESH_subMesh::CLEAN ); // for event listeners (issue 0020918)
437 sm->ComputeSubMeshStateEngine( SMESH_subMesh::CLEAN );
440 else // remove only nodes/elements computed by algorithms
442 if ( SMESH_subMesh *sm = GetSubMeshContaining( GetShapeToMesh() ) )
444 sm->ComputeStateEngine( SMESH_subMesh::CLEAN );
445 sm->ComputeSubMeshStateEngine( SMESH_subMesh::CLEAN );
446 sm->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
447 sm->ComputeSubMeshStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
450 GetMeshDS()->Modified();
454 //=======================================================================
456 * \brief Remove all nodes and elements of indicated shape
458 //=======================================================================
460 void SMESH_Mesh::ClearSubMesh(const int theShapeId)
462 // clear sub-meshes; get ready to re-compute as a side-effect
463 if ( SMESH_subMesh *sm = GetSubMeshContaining( theShapeId ) )
465 SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(/*includeSelf=*/true,
466 /*complexShapeFirst=*/false);
467 while ( smIt->more() )
470 TopAbs_ShapeEnum shapeType = sm->GetSubShape().ShapeType();
471 if ( shapeType == TopAbs_VERTEX || shapeType < TopAbs_SOLID )
472 // all other shapes depends on vertices so they are already cleaned
473 sm->ComputeStateEngine( SMESH_subMesh::CLEAN );
474 // to recompute even if failed
475 sm->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
480 //=======================================================================
481 //function : UNVToMesh
483 //=======================================================================
485 int SMESH_Mesh::UNVToMesh(const char* theFileName)
487 if ( _isShapeToMesh )
488 throw SALOME_Exception(LOCALIZED("a shape to mesh has already been defined"));
489 _isShapeToMesh = false;
491 DriverUNV_R_SMDS_Mesh myReader;
492 myReader.SetMesh(_meshDS);
493 myReader.SetFile(theFileName);
494 myReader.SetMeshId(-1);
497 TGroupNamesMap& aGroupNames = myReader.GetGroupNamesMap();
498 TGroupNamesMap::iterator gr2names;
499 int anId = 1 + ( _mapGroup.empty() ? 0 : _mapGroup.rbegin()->first );
500 for ( gr2names = aGroupNames.begin(); gr2names != aGroupNames.end(); ++gr2names )
502 SMDS_MeshGroup* aGroup = gr2names->first;
503 const std::string& aName = gr2names->second;
504 SMESHDS_Group* aGroupDS = new SMESHDS_Group( anId++, _meshDS, aGroup->GetType() );
505 aGroupDS->SMDSGroup() = std::move( *aGroup );
506 aGroupDS->SetStoreName( aName.c_str() );
507 AddGroup( aGroupDS );
513 //=======================================================================
514 //function : MEDToMesh
516 //=======================================================================
518 int SMESH_Mesh::MEDToMesh(const char* theFileName, const char* theMeshName)
520 if ( _isShapeToMesh )
521 throw SALOME_Exception(LOCALIZED("a shape to mesh has already been defined"));
522 _isShapeToMesh = false;
524 DriverMED_R_SMESHDS_Mesh myReader;
525 myReader.SetMesh(_meshDS);
526 myReader.SetMeshId(-1);
527 myReader.SetFile(theFileName);
528 myReader.SetMeshName(theMeshName);
529 Driver_Mesh::Status status = myReader.Perform();
531 if (SALOME::VerbosityActivated())
533 SMESH_ComputeErrorPtr er = myReader.GetError();
534 if ( er && !er->IsOK() ) std::cout << er->myComment << std::endl;
537 // Reading groups (sub-meshes are out of scope of MED import functionality)
538 std::list<TNameAndType> aGroupNames = myReader.GetGroupNamesAndTypes();
539 std::list<TNameAndType>::iterator name_type = aGroupNames.begin();
540 for ( ; name_type != aGroupNames.end(); name_type++ )
542 SMESH_Group* aGroup = AddGroup( name_type->second, name_type->first.c_str() );
544 SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>( aGroup->GetGroupDS() );
546 aGroupDS->SetStoreName( name_type->first.c_str() );
547 myReader.GetGroup( aGroupDS );
553 _meshDS->CompactMesh();
558 //=======================================================================
559 //function : STLToMesh
561 //=======================================================================
563 std::string SMESH_Mesh::STLToMesh(const char* theFileName)
566 throw SALOME_Exception(LOCALIZED("a shape to mesh has already been defined"));
567 _isShapeToMesh = false;
569 DriverSTL_R_SMDS_Mesh myReader;
570 myReader.SetMesh(_meshDS);
571 myReader.SetFile(theFileName);
572 myReader.SetMeshId(-1);
575 return myReader.GetName();
578 //================================================================================
580 * \brief Reads the given mesh from the CGNS file
581 * \param theFileName - name of the file
582 * \retval int - Driver_Mesh::Status
584 //================================================================================
586 int SMESH_Mesh::CGNSToMesh(const char* theFileName,
587 const int theMeshIndex,
588 std::string& theMeshName)
590 int res = Driver_Mesh::DRS_FAIL;
593 DriverCGNS_Read myReader;
594 myReader.SetMesh(_meshDS);
595 myReader.SetFile(theFileName);
596 myReader.SetMeshId(theMeshIndex);
597 res = myReader.Perform();
598 theMeshName = myReader.GetMeshName();
607 //================================================================================
609 * \brief Fill its data by reading a GMF file
611 //================================================================================
613 SMESH_ComputeErrorPtr SMESH_Mesh::GMFToMesh(const char* theFileName,
614 bool theMakeRequiredGroups)
616 DriverGMF_Read myReader;
617 myReader.SetMesh(_meshDS);
618 myReader.SetFile(theFileName);
619 myReader.SetMakeRequiredGroups( theMakeRequiredGroups );
621 //theMeshName = myReader.GetMeshName();
626 return myReader.GetError();
629 //=============================================================================
633 //=============================================================================
635 SMESH_Hypothesis::Hypothesis_Status
636 SMESH_Mesh::AddHypothesis(const TopoDS_Shape & aSubShape,
638 std::string* anError )
640 MESSAGE("SMESH_Mesh::AddHypothesis");
645 SMESH_subMesh *subMesh = GetSubMesh(aSubShape);
646 if ( !subMesh || !subMesh->GetId())
647 return SMESH_Hypothesis::HYP_BAD_SUBSHAPE;
649 SMESH_Hypothesis *anHyp = GetHypothesis( anHypId );
651 throw SALOME_Exception(LOCALIZED("hypothesis does not exist"));
653 bool isGlobalHyp = IsMainShape( aSubShape );
655 // NotConformAllowed can be only global
658 // NOTE: this is not a correct way to check a name of hypothesis,
659 // there should be an attribute of hypothesis saying that it can/can't
661 std::string hypName = anHyp->GetName();
662 if ( hypName == "NotConformAllowed" )
664 MESSAGE( "Hypothesis <NotConformAllowed> can be only global" );
665 return SMESH_Hypothesis::HYP_INCOMPATIBLE;
671 bool isAlgo = ( anHyp->GetType() != SMESHDS_Hypothesis::PARAM_ALGO );
672 SMESH_subMesh::algo_event event = isAlgo ? SMESH_subMesh::ADD_ALGO : SMESH_subMesh::ADD_HYP;
674 SMESH_Hypothesis::Hypothesis_Status ret = subMesh->AlgoStateEngine(event, anHyp);
676 if ( anError && SMESH_Hypothesis::IsStatusFatal(ret) && subMesh->GetComputeError() )
677 *anError = subMesh->GetComputeError()->myComment;
680 if ( !SMESH_Hypothesis::IsStatusFatal(ret) &&
681 anHyp->GetDim() <= SMESH_Gen::GetShapeDim(aSubShape)) // is added on father
683 event = isAlgo ? SMESH_subMesh::ADD_FATHER_ALGO : SMESH_subMesh::ADD_FATHER_HYP;
685 SMESH_Hypothesis::Hypothesis_Status ret2 =
686 subMesh->SubMeshesAlgoStateEngine(event, anHyp, /*exitOnFatal=*/true);
690 if ( SMESH_Hypothesis::IsStatusFatal( ret ))
692 if ( anError && subMesh->GetComputeError() )
693 *anError = subMesh->GetComputeError()->myComment;
695 event = isAlgo ? SMESH_subMesh::REMOVE_ALGO : SMESH_subMesh::REMOVE_HYP;
696 subMesh->AlgoStateEngine(event, anHyp);
700 // check concurrent hypotheses on ancestors
701 if (ret < SMESH_Hypothesis::HYP_CONCURRENT && !isGlobalHyp )
703 SMESH_subMeshIteratorPtr smIt = subMesh->getDependsOnIterator(false,false);
704 while ( smIt->more() ) {
705 SMESH_subMesh* sm = smIt->next();
706 if ( sm->IsApplicableHypothesis( anHyp )) {
707 ret2 = sm->CheckConcurrentHypothesis( anHyp );
716 HasModificationsToDiscard(); // to reset _isModified flag if a mesh becomes empty
717 GetMeshDS()->Modified();
719 if(SALOME::VerbosityActivated()) subMesh->DumpAlgoState(true);
724 //=============================================================================
728 //=============================================================================
730 SMESH_Hypothesis::Hypothesis_Status
731 SMESH_Mesh::RemoveHypothesis(const TopoDS_Shape & aSubShape,
734 MESSAGE("SMESH_Mesh::RemoveHypothesis");
736 StudyContextStruct *sc = _gen->GetStudyContext();
737 if (sc->mapHypothesis.find(anHypId) == sc->mapHypothesis.end())
738 throw SALOME_Exception(LOCALIZED("hypothesis does not exist"));
740 SMESH_Hypothesis *anHyp = sc->mapHypothesis[anHypId];
741 SCRUTE(anHyp->GetType());
745 bool isAlgo = ( !anHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO );
746 SMESH_subMesh::algo_event event = isAlgo ? SMESH_subMesh::REMOVE_ALGO : SMESH_subMesh::REMOVE_HYP;
748 SMESH_subMesh *subMesh = GetSubMesh(aSubShape);
750 SMESH_Hypothesis::Hypothesis_Status ret = subMesh->AlgoStateEngine(event, anHyp);
752 // there may appear concurrent hyps that were covered by the removed hyp
753 if (ret < SMESH_Hypothesis::HYP_CONCURRENT &&
754 subMesh->IsApplicableHypothesis( anHyp ) &&
755 subMesh->CheckConcurrentHypothesis( anHyp ) != SMESH_Hypothesis::HYP_OK)
756 ret = SMESH_Hypothesis::HYP_CONCURRENT;
759 if (!SMESH_Hypothesis::IsStatusFatal(ret) &&
760 anHyp->GetDim() <= SMESH_Gen::GetShapeDim(aSubShape)) // is removed from father
762 event = isAlgo ? SMESH_subMesh::REMOVE_FATHER_ALGO : SMESH_subMesh::REMOVE_FATHER_HYP;
764 SMESH_Hypothesis::Hypothesis_Status ret2 =
765 subMesh->SubMeshesAlgoStateEngine(event, anHyp);
766 if (ret2 > ret) // more severe
769 // check concurrent hypotheses on ancestors
770 if (ret < SMESH_Hypothesis::HYP_CONCURRENT && !IsMainShape( aSubShape ) )
772 SMESH_subMeshIteratorPtr smIt = subMesh->getDependsOnIterator(false,false);
773 while ( smIt->more() ) {
774 SMESH_subMesh* sm = smIt->next();
775 if ( sm->IsApplicableHypothesis( anHyp )) {
776 ret2 = sm->CheckConcurrentHypothesis( anHyp );
786 HasModificationsToDiscard(); // to reset _isModified flag if mesh become empty
787 GetMeshDS()->Modified();
789 if(SALOME::VerbosityActivated()) subMesh->DumpAlgoState(true);
794 //=============================================================================
798 //=============================================================================
800 const std::list<const SMESHDS_Hypothesis*>&
801 SMESH_Mesh::GetHypothesisList(const TopoDS_Shape & aSubShape) const
803 return _meshDS->GetHypothesis(aSubShape);
806 //=======================================================================
808 * \brief Return the hypothesis assigned to the shape
809 * \param aSubShape - the shape to check
810 * \param aFilter - the hypothesis filter
811 * \param andAncestors - flag to check hypos assigned to ancestors of the shape
812 * \param assignedTo - to return the shape the found hypo is assigned to
813 * \retval SMESH_Hypothesis* - the first hypo passed through aFilter
815 //=======================================================================
817 const SMESH_Hypothesis * SMESH_Mesh::GetHypothesis(const TopoDS_Shape & aSubShape,
818 const SMESH_HypoFilter& aFilter,
819 const bool andAncestors,
820 TopoDS_Shape* assignedTo) const
822 return GetHypothesis( const_cast< SMESH_Mesh* >(this)->GetSubMesh( aSubShape ),
823 aFilter, andAncestors, assignedTo );
826 //=======================================================================
828 * \brief Return the hypothesis assigned to the shape of a sub-mesh
829 * \param aSubMesh - the sub-mesh to check
830 * \param aFilter - the hypothesis filter
831 * \param andAncestors - flag to check hypos assigned to ancestors of the shape
832 * \param assignedTo - to return the shape the found hypo is assigned to
833 * \retval SMESH_Hypothesis* - the first hypo passed through aFilter
835 //=======================================================================
837 const SMESH_Hypothesis * SMESH_Mesh::GetHypothesis(const SMESH_subMesh * aSubMesh,
838 const SMESH_HypoFilter& aFilter,
839 const bool andAncestors,
840 TopoDS_Shape* assignedTo) const
842 if ( !aSubMesh ) return 0;
845 const TopoDS_Shape & aSubShape = aSubMesh->GetSubShape();
846 const std::list<const SMESHDS_Hypothesis*>& hypList = _meshDS->GetHypothesis(aSubShape);
847 std::list<const SMESHDS_Hypothesis*>::const_iterator hyp = hypList.begin();
848 for ( ; hyp != hypList.end(); hyp++ ) {
849 const SMESH_Hypothesis * h = cSMESH_Hyp( *hyp );
850 if ( aFilter.IsOk( h, aSubShape)) {
851 if ( assignedTo ) *assignedTo = aSubShape;
858 // user sorted submeshes of ancestors, according to stored submesh priority
859 std::vector< SMESH_subMesh * > & ancestors =
860 const_cast< std::vector< SMESH_subMesh * > & > ( aSubMesh->GetAncestors() );
861 SortByMeshOrder( ancestors );
863 std::vector<SMESH_subMesh*>::const_iterator smIt = ancestors.begin();
864 for ( ; smIt != ancestors.end(); smIt++ )
866 const TopoDS_Shape& curSh = (*smIt)->GetSubShape();
867 const std::list<const SMESHDS_Hypothesis*>& hypList = _meshDS->GetHypothesis(curSh);
868 std::list<const SMESHDS_Hypothesis*>::const_iterator hyp = hypList.begin();
869 for ( ; hyp != hypList.end(); hyp++ ) {
870 const SMESH_Hypothesis * h = cSMESH_Hyp( *hyp );
871 if (aFilter.IsOk( h, curSh )) {
872 if ( assignedTo ) *assignedTo = curSh;
881 //================================================================================
883 * \brief Return hypotheses assigned to the shape
884 * \param aSubShape - the shape to check
885 * \param aFilter - the hypothesis filter
886 * \param aHypList - the list of the found hypotheses
887 * \param andAncestors - flag to check hypos assigned to ancestors of the shape
888 * \retval int - number of unique hypos in aHypList
890 //================================================================================
892 int SMESH_Mesh::GetHypotheses(const TopoDS_Shape & aSubShape,
893 const SMESH_HypoFilter& aFilter,
894 std::list <const SMESHDS_Hypothesis * >& aHypList,
895 const bool andAncestors,
896 std::list< TopoDS_Shape > * assignedTo/*=0*/) const
898 return GetHypotheses( const_cast< SMESH_Mesh* >(this)->GetSubMesh( aSubShape ),
899 aFilter, aHypList, andAncestors, assignedTo );
902 //================================================================================
904 * \brief Return hypotheses assigned to the shape of a sub-mesh
905 * \param aSubShape - the sub-mesh to check
906 * \param aFilter - the hypothesis filter
907 * \param aHypList - the list of the found hypotheses
908 * \param andAncestors - flag to check hypos assigned to ancestors of the shape
909 * \retval int - number of unique hypos in aHypList
911 //================================================================================
913 int SMESH_Mesh::GetHypotheses(const SMESH_subMesh * aSubMesh,
914 const SMESH_HypoFilter& aFilter,
915 std::list <const SMESHDS_Hypothesis * >& aHypList,
916 const bool andAncestors,
917 std::list< TopoDS_Shape > * assignedTo/*=0*/) const
919 if ( !aSubMesh ) return 0;
921 std::set< std::string > hypTypes; // to exclude same type hypos from the result list
924 // only one main hypothesis is allowed
925 bool mainHypFound = false;
928 std::list<const SMESHDS_Hypothesis*>::const_iterator hyp;
929 for ( hyp = aHypList.begin(); hyp != aHypList.end(); hyp++ ) {
930 if ( hypTypes.insert( (*hyp)->GetName() ).second )
932 if ( !cSMESH_Hyp(*hyp)->IsAuxiliary() )
936 // get hypos from aSubShape
938 const TopoDS_Shape & aSubShape = aSubMesh->GetSubShape();
939 const std::list<const SMESHDS_Hypothesis*>& hypList = _meshDS->GetHypothesis(aSubShape);
940 for ( hyp = hypList.begin(); hyp != hypList.end(); hyp++ )
942 const SMESH_Hypothesis* h = cSMESH_Hyp( *hyp );
943 if (( aFilter.IsOk( h, aSubShape )) &&
944 ( h->IsAuxiliary() || !mainHypFound ) &&
945 ( h->IsAuxiliary() || hypTypes.insert( h->GetName() ).second ))
947 aHypList.push_back( *hyp );
949 if ( !h->IsAuxiliary() )
951 if ( assignedTo ) assignedTo->push_back( aSubShape );
956 // get hypos from ancestors of aSubShape
959 // user sorted submeshes of ancestors, according to stored submesh priority
960 std::vector< SMESH_subMesh * > & ancestors =
961 const_cast< std::vector< SMESH_subMesh * > & > ( aSubMesh->GetAncestors() );
962 SortByMeshOrder( ancestors );
964 std::vector<SMESH_subMesh*>::const_iterator smIt = ancestors.begin();
965 for ( ; smIt != ancestors.end(); smIt++ )
967 const TopoDS_Shape& curSh = (*smIt)->GetSubShape();
968 const std::list<const SMESHDS_Hypothesis*>& hypList = _meshDS->GetHypothesis(curSh);
969 for ( hyp = hypList.begin(); hyp != hypList.end(); hyp++ )
971 const SMESH_Hypothesis* h = cSMESH_Hyp( *hyp );
972 if (( aFilter.IsOk( h, curSh )) &&
973 ( h->IsAuxiliary() || !mainHypFound ) &&
974 ( h->IsAuxiliary() || hypTypes.insert( h->GetName() ).second ))
976 aHypList.push_back( *hyp );
978 if ( !h->IsAuxiliary() )
980 if ( assignedTo ) assignedTo->push_back( curSh );
988 //================================================================================
990 * \brief Return a hypothesis by its ID
992 //================================================================================
994 SMESH_Hypothesis * SMESH_Mesh::GetHypothesis(const int anHypId) const
996 StudyContextStruct *sc = _gen->GetStudyContext();
997 if (sc->mapHypothesis.find(anHypId) == sc->mapHypothesis.end())
1000 SMESH_Hypothesis *anHyp = sc->mapHypothesis[anHypId];
1004 //=============================================================================
1008 //=============================================================================
1010 const std::list<SMESHDS_Command*> & SMESH_Mesh::GetLog()
1012 return _meshDS->GetScript()->GetCommands();
1015 //=============================================================================
1019 //=============================================================================
1020 void SMESH_Mesh::ClearLog()
1022 _meshDS->GetScript()->Clear();
1025 //=============================================================================
1027 * Get or Create the SMESH_subMesh object implementation
1029 //=============================================================================
1031 SMESH_subMesh * SMESH_Mesh::GetSubMesh(const TopoDS_Shape & aSubShape)
1033 int index = _meshDS->ShapeToIndex(aSubShape);
1034 if ( !index && aSubShape.IsNull() )
1037 // for submeshes on GEOM Group
1038 if (( !index || index > _nbSubShapes ) && aSubShape.ShapeType() == TopAbs_COMPOUND )
1040 TopoDS_Iterator it( aSubShape );
1043 index = _meshDS->AddCompoundSubmesh( aSubShape, it.Value().ShapeType() );
1044 // fill map of Ancestors
1045 while ( _nbSubShapes < index )
1046 fillAncestorsMap( _meshDS->IndexToShape( ++_nbSubShapes ));
1050 // return NULL; // neither sub-shape nor a group
1052 SMESH_subMesh* aSubMesh = _subMeshHolder->Get( index );
1055 aSubMesh = new SMESH_subMesh(index, this, _meshDS, aSubShape);
1056 _subMeshHolder->Add( index, aSubMesh );
1058 // include non-computable sub-meshes in SMESH_subMesh::_ancestors of sub-submeshes
1059 switch ( aSubShape.ShapeType() ) {
1060 case TopAbs_COMPOUND:
1063 for ( TopoDS_Iterator subIt( aSubShape ); subIt.More(); subIt.Next() )
1065 SMESH_subMesh* sm = GetSubMesh( subIt.Value() );
1066 SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(/*inclideSelf=*/true);
1067 while ( smIt->more() )
1068 smIt->next()->ClearAncestors();
1076 //=============================================================================
1078 * Get the SMESH_subMesh object implementation. Don't create it, return null
1079 * if it does not exist.
1081 //=============================================================================
1083 SMESH_subMesh *SMESH_Mesh::GetSubMeshContaining(const TopoDS_Shape & aSubShape) const
1085 int index = _meshDS->ShapeToIndex(aSubShape);
1086 return GetSubMeshContaining( index );
1089 //=============================================================================
1091 * Get the SMESH_subMesh object implementation. Don't create it, return null
1092 * if it does not exist.
1094 //=============================================================================
1096 SMESH_subMesh *SMESH_Mesh::GetSubMeshContaining(const int aShapeID) const
1098 SMESH_subMesh *aSubMesh = _subMeshHolder->Get( aShapeID );
1102 //================================================================================
1104 * \brief Return sub-meshes of groups containing the given sub-shape
1106 //================================================================================
1108 std::list<SMESH_subMesh*>
1109 SMESH_Mesh::GetGroupSubMeshesContaining(const TopoDS_Shape & aSubShape) const
1111 std::list<SMESH_subMesh*> found;
1113 SMESH_subMesh * subMesh = GetSubMeshContaining(aSubShape);
1117 // sub-meshes of groups have max IDs, so search from the map end
1118 SMESH_subMeshIteratorPtr smIt( _subMeshHolder->GetIterator( /*reverse=*/true ) );
1119 while ( smIt->more() ) {
1120 SMESH_subMesh* sm = smIt->next();
1121 SMESHDS_SubMesh * ds = sm->GetSubMeshDS();
1122 if ( ds && ds->IsComplexSubmesh() ) {
1123 if ( SMESH_MesherHelper::IsSubShape( aSubShape, sm->GetSubShape() ))
1125 found.push_back( sm );
1129 break; // the rest sub-meshes are not those of groups
1133 if ( found.empty() ) // maybe the main shape is a COMPOUND (issue 0021530)
1135 if ( SMESH_subMesh * mainSM = GetSubMeshContaining(1) )
1136 if ( mainSM->GetSubShape().ShapeType() == TopAbs_COMPOUND )
1138 TopoDS_Iterator it( mainSM->GetSubShape() );
1139 if ( it.Value().ShapeType() == aSubShape.ShapeType() &&
1140 SMESH_MesherHelper::IsSubShape( aSubShape, mainSM->GetSubShape() ))
1141 found.push_back( mainSM );
1144 else // issue 0023068
1146 if ( SMESH_subMesh * mainSM = GetSubMeshContaining(1) )
1147 if ( mainSM->GetSubShape().ShapeType() == TopAbs_COMPOUND )
1148 found.push_back( mainSM );
1152 //=======================================================================
1153 //function : IsUsedHypothesis
1154 //purpose : Return True if anHyp is used to mesh aSubShape
1155 //=======================================================================
1157 bool SMESH_Mesh::IsUsedHypothesis(SMESHDS_Hypothesis * anHyp,
1158 const SMESH_subMesh* aSubMesh)
1160 SMESH_Hypothesis* hyp = static_cast<SMESH_Hypothesis*>(anHyp);
1162 // check if anHyp can be used to mesh aSubMesh
1163 if ( !aSubMesh || !aSubMesh->IsApplicableHypothesis( hyp ))
1166 SMESH_Algo *algo = aSubMesh->GetAlgo();
1169 if (anHyp->GetType() > SMESHDS_Hypothesis::PARAM_ALGO)
1170 return ( anHyp == algo );
1172 // algorithm parameter
1175 // look through hypotheses used by algo
1176 const SMESH_HypoFilter* hypoKind;
1177 if (( hypoKind = algo->GetCompatibleHypoFilter( !hyp->IsAuxiliary() ))) {
1178 std::list <const SMESHDS_Hypothesis * > usedHyps;
1179 if ( GetHypotheses( aSubMesh, *hypoKind, usedHyps, true ))
1180 return ( find( usedHyps.begin(), usedHyps.end(), anHyp ) != usedHyps.end() );
1187 //=======================================================================
1188 //function : NotifySubMeshesHypothesisModification
1189 //purpose : Say all submeshes using theChangedHyp that it has been modified
1190 //=======================================================================
1192 void SMESH_Mesh::NotifySubMeshesHypothesisModification(const SMESH_Hypothesis* hyp)
1195 if ( !GetMeshDS()->IsUsedHypothesis( hyp ))
1198 smIdType nbEntities = ( _meshDS->NbNodes() + _meshDS->NbElements() );
1199 if ( hyp && _callUp && !_callUp->IsLoaded() ) // for not loaded mesh (#16648)
1201 _callUp->HypothesisModified( hyp->GetID(), /*updateIcons=*/true );
1202 nbEntities = ( _meshDS->NbNodes() + _meshDS->NbElements() ); // after loading mesh
1206 const SMESH_HypoFilter* compatibleHypoKind;
1207 std::list <const SMESHDS_Hypothesis * > usedHyps;
1208 std::vector< SMESH_subMesh* > smToNotify;
1209 bool allMeshedEdgesNotified = true;
1211 SMESH_subMeshIteratorPtr smIt( _subMeshHolder->GetIterator() );
1212 while ( smIt->more() )
1214 SMESH_subMesh* aSubMesh = smIt->next();
1215 bool toNotify = false;
1217 // if aSubMesh meshing depends on hyp,
1218 // we call aSubMesh->AlgoStateEngine( MODIF_HYP, hyp ) that causes either
1219 // 1) clearing already computed aSubMesh or
1220 // 2) changing algo_state from MISSING_HYP to HYP_OK when parameters of hyp becomes valid,
1221 // other possible changes are not interesting. (IPAL0052457 - assigning hyp performance pb)
1222 if ( aSubMesh->GetComputeState() == SMESH_subMesh::COMPUTE_OK ||
1223 aSubMesh->GetComputeState() == SMESH_subMesh::FAILED_TO_COMPUTE ||
1224 aSubMesh->GetAlgoState() == SMESH_subMesh::MISSING_HYP ||
1225 hyp->DataDependOnParams() )
1227 const TopoDS_Shape & aSubShape = aSubMesh->GetSubShape();
1229 if (( aSubMesh->IsApplicableHypothesis( hyp )) &&
1230 ( algo = aSubMesh->GetAlgo() ) &&
1231 ( compatibleHypoKind = algo->GetCompatibleHypoFilter( !hyp->IsAuxiliary() )) &&
1232 ( compatibleHypoKind->IsOk( hyp, aSubShape )))
1234 // check if hyp is used by algo
1236 toNotify = ( GetHypotheses( aSubMesh, *compatibleHypoKind, usedHyps, true ) &&
1237 std::find( usedHyps.begin(), usedHyps.end(), hyp ) != usedHyps.end() );
1242 smToNotify.push_back( aSubMesh );
1243 if ( aSubMesh->GetAlgoState() == SMESH_subMesh::MISSING_HYP )
1244 allMeshedEdgesNotified = false; // update of algo state needed, not mesh clearing
1248 if ( !aSubMesh->IsEmpty() &&
1249 aSubMesh->GetSubShape().ShapeType() == TopAbs_EDGE )
1250 allMeshedEdgesNotified = false;
1253 if ( smToNotify.empty() )
1256 // if all meshed EDGEs will be notified then the notification is equivalent
1257 // to the whole mesh clearing, which is usually faster
1258 if ( allMeshedEdgesNotified && NbNodes() > 0 )
1264 // notify in reverse order to avoid filling the pool of IDs
1265 for ( int i = smToNotify.size()-1; i >= 0; --i )
1266 smToNotify[i]->AlgoStateEngine(SMESH_subMesh::MODIF_HYP,
1267 const_cast< SMESH_Hypothesis*>( hyp ));
1269 HasModificationsToDiscard(); // to reset _isModified flag if mesh becomes empty
1270 GetMeshDS()->Modified();
1272 smIdType newNbEntities = ( _meshDS->NbNodes() + _meshDS->NbElements() );
1273 if ( hyp && _callUp )
1274 _callUp->HypothesisModified( hyp->GetID(), newNbEntities != nbEntities );
1277 //=============================================================================
1279 * Auto color functionality
1281 //=============================================================================
1282 void SMESH_Mesh::SetAutoColor(bool theAutoColor)
1284 _isAutoColor = theAutoColor;
1287 bool SMESH_Mesh::GetAutoColor()
1289 return _isAutoColor;
1292 //=======================================================================
1293 //function : SetIsModified
1294 //purpose : Set the flag meaning that the mesh has been edited "manually"
1295 //=======================================================================
1297 void SMESH_Mesh::SetIsModified(bool isModified)
1299 _isModified = isModified;
1302 // check if mesh becomes empty as result of modification
1303 HasModificationsToDiscard();
1306 //=======================================================================
1307 //function : HasModificationsToDiscard
1308 //purpose : Return true if the mesh has been edited since a total re-compute
1309 // and those modifications may prevent successful partial re-compute.
1310 // As a side effect reset _isModified flag if mesh is empty
1312 //=======================================================================
1314 bool SMESH_Mesh::HasModificationsToDiscard() const
1316 if ( ! _isModified )
1319 // return true if the next Compute() will be partial and
1320 // existing but changed elements may prevent successful re-compute
1321 bool hasComputed = false, hasNotComputed = false;
1322 SMESH_subMeshIteratorPtr smIt( _subMeshHolder->GetIterator() );
1323 while ( smIt->more() )
1325 const SMESH_subMesh* aSubMesh = smIt->next();
1326 switch ( aSubMesh->GetSubShape().ShapeType() )
1331 if ( aSubMesh->IsMeshComputed() )
1334 hasNotComputed = true;
1335 if ( hasComputed && hasNotComputed)
1341 if ( NbNodes() < 1 )
1342 const_cast<SMESH_Mesh*>(this)->_isModified = false;
1347 //=============================================================================
1349 * \brief Return true if all sub-meshes are computed OK - to update an icon
1351 //=============================================================================
1353 bool SMESH_Mesh::IsComputedOK()
1355 if ( NbNodes() == 0 )
1358 // if ( !HasShapeToMesh() )
1361 if ( SMESH_subMesh* mainSM = GetSubMeshContaining( 1 ))
1363 SMESH_subMeshIteratorPtr smIt = mainSM->getDependsOnIterator(/*includeSelf=*/true);
1364 while ( smIt->more() )
1366 const SMESH_subMesh* sm = smIt->next();
1367 if ( !sm->IsAlwaysComputed() )
1368 switch ( sm->GetComputeState() )
1370 case SMESH_subMesh::NOT_READY:
1371 case SMESH_subMesh::COMPUTE_OK:
1373 case SMESH_subMesh::FAILED_TO_COMPUTE:
1374 case SMESH_subMesh::READY_TO_COMPUTE:
1382 //================================================================================
1384 * \brief Check if any groups of the same type have equal names
1386 //================================================================================
1388 bool SMESH_Mesh::HasDuplicatedGroupNamesMED()
1390 // Corrected for Mantis issue 0020028
1391 std::map< SMDSAbs_ElementType, std::set< std::string > > aGroupNames;
1392 for ( std::map<int, SMESH_Group*>::iterator it = _mapGroup.begin(); it != _mapGroup.end(); it++ )
1394 SMESH_Group* aGroup = it->second;
1395 SMDSAbs_ElementType aType = aGroup->GetGroupDS()->GetType();
1396 std::string aGroupName = aGroup->GetName();
1397 aGroupName.resize( MAX_MED_GROUP_NAME_LENGTH );
1398 if ( !aGroupNames[aType].insert(aGroupName).second )
1405 //================================================================================
1407 * \brief Export the mesh to a writer
1409 //================================================================================
1411 void SMESH_Mesh::exportMEDCommmon(DriverMED_W_SMESHDS_Mesh& theWriter,
1412 const char* theMeshName,
1414 const SMESHDS_Mesh* theMeshPart,
1415 bool theAutoDimension,
1416 bool theAddODOnVertices,
1417 double theZTolerance,
1418 bool theSaveNumbers)
1420 Driver_Mesh::Status status = Driver_Mesh::DRS_OK;
1424 theWriter.SetMesh ( theMeshPart ? (SMESHDS_Mesh*) theMeshPart : _meshDS );
1425 theWriter.SetAutoDimension( theAutoDimension );
1426 theWriter.AddODOnVertices ( theAddODOnVertices );
1427 theWriter.SetZTolerance ( theZTolerance );
1428 theWriter.SetSaveNumbers ( theSaveNumbers );
1430 theWriter.SetMeshId ( _id );
1432 theWriter.SetMeshId ( -1 );
1433 theWriter.SetMeshName ( theMeshName );
1436 if ( theAutoGroups ) {
1437 theWriter.AddGroupOfNodes();
1438 theWriter.AddGroupOfEdges();
1439 theWriter.AddGroupOfFaces();
1440 theWriter.AddGroupOfVolumes();
1441 theWriter.AddGroupOf0DElems();
1442 theWriter.AddGroupOfBalls();
1445 // Pass groups to writer. Provide unique group names.
1446 //set<string> aGroupNames; // Corrected for Mantis issue 0020028
1449 std::map< SMDSAbs_ElementType, std::set<std::string> > aGroupNames;
1451 int maxNbIter = 10000; // to guarantee cycle finish
1452 for ( std::map<int, SMESH_Group*>::iterator it = _mapGroup.begin();
1453 it != _mapGroup.end();
1455 SMESH_Group* aGroup = it->second;
1456 SMESHDS_GroupBase* aGroupDS = aGroup->GetGroupDS();
1458 SMDSAbs_ElementType aType = aGroupDS->GetType();
1459 std::string aGroupName0 = aGroup->GetName();
1460 aGroupName0.resize(MAX_MED_GROUP_NAME_LENGTH);
1461 std::string aGroupName = aGroupName0;
1462 for (int i = 1; !aGroupNames[aType].insert(aGroupName).second && i < maxNbIter; i++) {
1463 sprintf(&aString[0], "GR_%d_%s", i, aGroupName0.c_str());
1464 aGroupName = aString;
1465 aGroupName.resize(MAX_MED_GROUP_NAME_LENGTH);
1467 aGroupDS->SetStoreName( aGroupName.c_str() );
1468 theWriter.AddGroup( aGroupDS );
1473 status = theWriter.Perform();
1475 SMESH_CATCH( SMESH::throwSalomeEx );
1477 if ( status == Driver_Mesh::DRS_TOO_LARGE_MESH )
1478 throw TooLargeForExport("MED");
1481 //================================================================================
1483 * Same as SMESH_Mesh::ExportMED except for \a file and \a theVersion
1485 //================================================================================
1487 MEDCoupling::MCAuto<MEDCoupling::DataArrayByte>
1488 SMESH_Mesh::ExportMEDCoupling(const char* theMeshName,
1490 const SMESHDS_Mesh* theMeshPart,
1491 bool theAutoDimension,
1492 bool theAddODOnVertices,
1493 double theZTolerance,
1494 bool theSaveNumbers)
1496 DriverMED_W_SMESHDS_Mesh_Mem writer;
1497 this->exportMEDCommmon( writer, theMeshName, theAutoGroups, theMeshPart, theAutoDimension,
1498 theAddODOnVertices, theZTolerance, theSaveNumbers);
1499 return writer.getData();
1502 //================================================================================
1504 * \brief Export the mesh to a med file
1505 * \param [in] theFile - name of the MED file
1506 * \param [in] theMeshName - name of this mesh
1507 * \param [in] theAutoGroups - boolean parameter for creating/not creating
1508 * the groups Group_On_All_Nodes, Group_On_All_Faces, ... ;
1509 * the typical use is auto_groups=false.
1510 * \param [in] theVersion - define the minor (xy, where version is x.y.z) of MED file format.
1511 * If theVersion is equal to -1, the minor version is not changed (default).
1512 * \param [in] theMeshPart - mesh data to export
1513 * \param [in] theAutoDimension - if \c true, a space dimension of a MED mesh can be either
1514 * - 1D if all mesh nodes lie on OX coordinate axis, or
1515 * - 2D if all mesh nodes lie on XOY coordinate plane, or
1516 * - 3D in the rest cases.
1517 * If \a theAutoDimension is \c false, the space dimension is always 3.
1518 * \param [in] theAddODOnVertices - to create 0D elements on all vertices
1519 * \param [in] theZTolerance - tolerance in Z direction. If Z coordinate of a node is close to zero
1520 * within a given tolerance, the coordinate is set to zero.
1521 * If \a ZTolerance is negative, the node coordinates are kept as is.
1522 * \param [in] theSaveNumbers : enable saving numbers of nodes and cells.
1523 * \return int - mesh index in the file
1525 //================================================================================
1527 void SMESH_Mesh::ExportMED(const char * theFile,
1528 const char* theMeshName,
1531 const SMESHDS_Mesh* theMeshPart,
1532 bool theAutoDimension,
1533 bool theAddODOnVertices,
1534 double theZTolerance,
1535 bool theSaveNumbers)
1537 MESSAGE("MED_VERSION:"<< theVersion);
1538 DriverMED_W_SMESHDS_Mesh writer;
1539 writer.SetFile( theFile, theVersion );
1540 this->exportMEDCommmon( writer, theMeshName, theAutoGroups, theMeshPart, theAutoDimension, theAddODOnVertices, theZTolerance, theSaveNumbers );
1543 //================================================================================
1545 * \brief Export the mesh to a DAT file
1547 //================================================================================
1549 void SMESH_Mesh::ExportDAT(const char * file,
1550 const SMESHDS_Mesh* meshPart,
1551 const bool renumber)
1553 Driver_Mesh::Status status;
1556 DriverDAT_W_SMDS_Mesh writer;
1557 writer.SetFile( file );
1558 writer.SetMesh( meshPart ? (SMESHDS_Mesh*) meshPart : _meshDS );
1559 writer.SetMeshId(_id);
1560 writer.SetRenumber( renumber );
1561 status = writer.Perform();
1563 SMESH_CATCH( SMESH::throwSalomeEx );
1565 if ( status == Driver_Mesh::DRS_TOO_LARGE_MESH )
1566 throw TooLargeForExport("DAT");
1569 //================================================================================
1571 * \brief Export the mesh to an UNV file
1573 //================================================================================
1575 void SMESH_Mesh::ExportUNV(const char * file,
1576 const SMESHDS_Mesh* meshPart,
1577 const bool renumber)
1579 Driver_Mesh::Status status;
1582 DriverUNV_W_SMDS_Mesh writer;
1583 writer.SetFile( file );
1584 writer.SetMesh( meshPart ? (SMESHDS_Mesh*) meshPart : _meshDS );
1585 writer.SetMeshId(_id);
1586 writer.SetRenumber( renumber );
1588 // pass group names to SMESHDS
1591 std::map<int, SMESH_Group*>::iterator it = _mapGroup.begin();
1592 for ( ; it != _mapGroup.end(); it++ ) {
1593 SMESH_Group* aGroup = it->second;
1594 SMESHDS_GroupBase* aGroupDS = aGroup->GetGroupDS();
1596 std::string aGroupName = aGroup->GetName();
1597 aGroupDS->SetStoreName( aGroupName.c_str() );
1598 writer.AddGroup( aGroupDS );
1602 status = writer.Perform();
1604 SMESH_CATCH( SMESH::throwSalomeEx );
1606 if ( status == Driver_Mesh::DRS_TOO_LARGE_MESH )
1607 throw TooLargeForExport("UNV");
1610 //================================================================================
1612 * \brief Export the mesh to an STL file
1614 //================================================================================
1616 void SMESH_Mesh::ExportSTL(const char * file,
1619 const SMESHDS_Mesh* meshPart)
1621 Driver_Mesh::Status status;
1624 DriverSTL_W_SMDS_Mesh writer;
1625 writer.SetFile( file );
1626 writer.SetIsAscii( isascii );
1627 writer.SetMesh( meshPart ? (SMESHDS_Mesh*) meshPart : _meshDS);
1628 writer.SetMeshId(_id);
1629 if ( name ) writer.SetName( name );
1630 status = writer.Perform();
1632 SMESH_CATCH( SMESH::throwSalomeEx );
1634 if ( status == Driver_Mesh::DRS_TOO_LARGE_MESH )
1635 throw TooLargeForExport("STL");
1638 //================================================================================
1640 * \brief Export the mesh to the CGNS file
1642 //================================================================================
1644 void SMESH_Mesh::ExportCGNS(const char * file,
1645 const SMESHDS_Mesh* meshDS,
1646 const char * meshName,
1647 const bool groupElemsByType)
1650 int res = Driver_Mesh::DRS_FAIL;
1653 // pass group names to SMESHDS
1654 std::map<int, SMESH_Group*>::iterator it = _mapGroup.begin();
1655 for ( ; it != _mapGroup.end(); it++ ) {
1656 SMESH_Group* group = it->second;
1657 SMESHDS_GroupBase* groupDS = group->GetGroupDS();
1659 std::string groupName = group->GetName();
1660 groupDS->SetStoreName( groupName.c_str() );
1665 DriverCGNS_Write writer;
1666 writer.SetFile( file );
1667 writer.SetMesh( const_cast<SMESHDS_Mesh*>( meshDS ));
1668 writer.SetMeshName( SMESH_Comment("Mesh_") << meshDS->GetPersistentId());
1669 if ( meshName && meshName[0] )
1670 writer.SetMeshName( meshName );
1671 writer.SetElementsByType( groupElemsByType );
1672 res = writer.Perform();
1673 if ( res != Driver_Mesh::DRS_OK )
1675 SMESH_ComputeErrorPtr err = writer.GetError();
1676 if ( err && !err->IsOK() && !err->myComment.empty() )
1677 throw SALOME_Exception(("Export failed: " + err->myComment ).c_str() );
1681 SMESH_CATCH( SMESH::throwSalomeEx );
1683 if ( res == Driver_Mesh::DRS_TOO_LARGE_MESH )
1684 throw TooLargeForExport("CGNS");
1686 if ( res != Driver_Mesh::DRS_OK )
1687 throw SALOME_Exception("Export failed");
1690 //================================================================================
1692 * \brief Export the mesh to a GMF file
1694 //================================================================================
1696 void SMESH_Mesh::ExportGMF(const char * file,
1697 const SMESHDS_Mesh* meshDS,
1698 bool withRequiredGroups)
1700 Driver_Mesh::Status status;
1703 DriverGMF_Write writer;
1704 writer.SetFile( file );
1705 writer.SetMesh( const_cast<SMESHDS_Mesh*>( meshDS ));
1706 writer.SetExportRequiredGroups( withRequiredGroups );
1708 status = writer.Perform();
1710 SMESH_CATCH( SMESH::throwSalomeEx );
1712 if ( status == Driver_Mesh::DRS_TOO_LARGE_MESH )
1713 throw TooLargeForExport("GMF");
1716 //================================================================================
1718 * \brief Return a ratio of "compute cost" of computed sub-meshes to the whole
1721 //================================================================================
1723 double SMESH_Mesh::GetComputeProgress() const
1725 double totalCost = 1e-100, computedCost = 0;
1726 const SMESH_subMesh* curSM = _gen->GetCurrentSubMesh();
1728 // get progress of a current algo
1729 TColStd_MapOfInteger currentSubIds;
1731 if ( SMESH_Algo* algo = curSM->GetAlgo() )
1733 int algoNotDoneCost = 0, algoDoneCost = 0;
1734 const std::vector<SMESH_subMesh*>& smToCompute = algo->SubMeshesToCompute();
1735 for ( size_t i = 0; i < smToCompute.size(); ++i )
1737 if ( smToCompute[i]->IsEmpty() || smToCompute.size() == 1 )
1738 algoNotDoneCost += smToCompute[i]->GetComputeCost();
1740 algoDoneCost += smToCompute[i]->GetComputeCost();
1741 currentSubIds.Add( smToCompute[i]->GetId() );
1747 rate = algo->GetProgress();
1750 if (SALOME::VerbosityActivated())
1751 std::cerr << "Exception in " << algo->GetName() << "::GetProgress()" << std::endl;
1753 if ( 0. < rate && rate < 1.001 )
1755 computedCost += rate * ( algoDoneCost + algoNotDoneCost );
1759 rate = algo->GetProgressByTic();
1760 computedCost += algoDoneCost + rate * algoNotDoneCost;
1764 // get cost of already treated sub-meshes
1765 if ( SMESH_subMesh* mainSM = GetSubMeshContaining( 1 ))
1767 SMESH_subMeshIteratorPtr smIt = mainSM->getDependsOnIterator(/*includeSelf=*/true);
1768 while ( smIt->more() )
1770 const SMESH_subMesh* sm = smIt->next();
1771 const int smCost = sm->GetComputeCost();
1772 totalCost += smCost;
1773 if ( !currentSubIds.Contains( sm->GetId() ) )
1775 if (( !sm->IsEmpty() ) ||
1776 ( sm->GetComputeState() == SMESH_subMesh::FAILED_TO_COMPUTE &&
1777 !sm->DependsOn( curSM ) ))
1778 computedCost += smCost;
1782 return computedCost / totalCost;
1785 //================================================================================
1787 * \brief Return number of nodes in the mesh
1789 //================================================================================
1791 smIdType SMESH_Mesh::NbNodes() const
1793 return _meshDS->NbNodes();
1796 //================================================================================
1798 * \brief Return number of edges of given order in the mesh
1800 //================================================================================
1802 smIdType SMESH_Mesh::Nb0DElements() const
1804 return _meshDS->GetMeshInfo().Nb0DElements();
1807 //================================================================================
1809 * \brief Return number of edges of given order in the mesh
1811 //================================================================================
1813 smIdType SMESH_Mesh::NbEdges(SMDSAbs_ElementOrder order) const
1815 return _meshDS->GetMeshInfo().NbEdges(order);
1818 //================================================================================
1820 * \brief Return number of faces of given order in the mesh
1822 //================================================================================
1824 smIdType SMESH_Mesh::NbFaces(SMDSAbs_ElementOrder order) const
1826 return _meshDS->GetMeshInfo().NbFaces(order);
1829 //================================================================================
1831 * \brief Return the number of faces in the mesh
1833 //================================================================================
1835 smIdType SMESH_Mesh::NbTriangles(SMDSAbs_ElementOrder order) const
1837 return _meshDS->GetMeshInfo().NbTriangles(order);
1840 //================================================================================
1842 * \brief Return number of biquadratic triangles in the mesh
1844 //================================================================================
1846 smIdType SMESH_Mesh::NbBiQuadTriangles() const
1848 return _meshDS->GetMeshInfo().NbBiQuadTriangles();
1851 //================================================================================
1853 * \brief Return the number nodes faces in the mesh
1855 //================================================================================
1857 smIdType SMESH_Mesh::NbQuadrangles(SMDSAbs_ElementOrder order) const
1859 return _meshDS->GetMeshInfo().NbQuadrangles(order);
1862 //================================================================================
1864 * \brief Return number of biquadratic quadrangles in the mesh
1866 //================================================================================
1868 smIdType SMESH_Mesh::NbBiQuadQuadrangles() const
1870 return _meshDS->GetMeshInfo().NbBiQuadQuadrangles();
1873 //================================================================================
1875 * \brief Return the number of polygonal faces in the mesh
1877 //================================================================================
1879 smIdType SMESH_Mesh::NbPolygons(SMDSAbs_ElementOrder order) const
1881 return _meshDS->GetMeshInfo().NbPolygons(order);
1884 //================================================================================
1886 * \brief Return number of volumes of given order in the mesh
1888 //================================================================================
1890 smIdType SMESH_Mesh::NbVolumes(SMDSAbs_ElementOrder order) const
1892 return _meshDS->GetMeshInfo().NbVolumes(order);
1895 //================================================================================
1897 * \brief Return number of tetrahedrons of given order in the mesh
1899 //================================================================================
1901 smIdType SMESH_Mesh::NbTetras(SMDSAbs_ElementOrder order) const
1903 return _meshDS->GetMeshInfo().NbTetras(order);
1906 //================================================================================
1908 * \brief Return number of hexahedrons of given order in the mesh
1910 //================================================================================
1912 smIdType SMESH_Mesh::NbHexas(SMDSAbs_ElementOrder order) const
1914 return _meshDS->GetMeshInfo().NbHexas(order);
1917 //================================================================================
1919 * \brief Return number of triquadratic hexahedrons in the mesh
1921 //================================================================================
1923 smIdType SMESH_Mesh::NbTriQuadraticHexas() const
1925 return _meshDS->GetMeshInfo().NbTriQuadHexas();
1928 //================================================================================
1930 * \brief Return number of pyramids of given order in the mesh
1932 //================================================================================
1934 smIdType SMESH_Mesh::NbPyramids(SMDSAbs_ElementOrder order) const
1936 return _meshDS->GetMeshInfo().NbPyramids(order);
1939 //================================================================================
1941 * \brief Return number of prisms (penthahedrons) of given order in the mesh
1943 //================================================================================
1945 smIdType SMESH_Mesh::NbPrisms(SMDSAbs_ElementOrder order) const
1947 return _meshDS->GetMeshInfo().NbPrisms(order);
1950 smIdType SMESH_Mesh::NbQuadPrisms() const
1952 return _meshDS->GetMeshInfo().NbQuadPrisms();
1955 smIdType SMESH_Mesh::NbBiQuadPrisms() const
1957 return _meshDS->GetMeshInfo().NbBiQuadPrisms();
1961 //================================================================================
1963 * \brief Return number of hexagonal prisms in the mesh
1965 //================================================================================
1967 smIdType SMESH_Mesh::NbHexagonalPrisms() const
1969 return _meshDS->GetMeshInfo().NbHexPrisms();
1972 //================================================================================
1974 * \brief Return number of polyhedrons in the mesh
1976 //================================================================================
1978 smIdType SMESH_Mesh::NbPolyhedrons() const
1980 return _meshDS->GetMeshInfo().NbPolyhedrons();
1983 //================================================================================
1985 * \brief Return number of ball elements in the mesh
1987 //================================================================================
1989 smIdType SMESH_Mesh::NbBalls() const
1991 return _meshDS->GetMeshInfo().NbBalls();
1994 //================================================================================
1996 * \brief Return number of submeshes in the mesh
1998 //================================================================================
2000 smIdType SMESH_Mesh::NbSubMesh() const
2002 return _meshDS->NbSubMesh();
2005 //================================================================================
2007 * \brief Returns number of meshes in the Study, that is supposed to be
2008 * equal to SMESHDS_Document::NbMeshes()
2010 //================================================================================
2012 int SMESH_Mesh::NbMeshes() const // nb meshes in the Study
2014 return _document->NbMeshes();
2017 //=======================================================================
2018 //function : IsNotConformAllowed
2019 //purpose : check if a hypothesis allowing notconform mesh is present
2020 //=======================================================================
2022 bool SMESH_Mesh::IsNotConformAllowed() const
2024 MESSAGE("SMESH_Mesh::IsNotConformAllowed");
2026 static SMESH_HypoFilter filter( SMESH_HypoFilter::HasName( "NotConformAllowed" ));
2027 return GetHypothesis( _meshDS->ShapeToMesh(), filter, false );
2030 //=======================================================================
2031 //function : IsMainShape
2033 //=======================================================================
2035 bool SMESH_Mesh::IsMainShape(const TopoDS_Shape& theShape) const
2037 return theShape.IsSame(_meshDS->ShapeToMesh() );
2040 //=======================================================================
2041 //function : GetShapeByEntry
2042 //purpose : return TopoDS_Shape by its study entry
2043 //=======================================================================
2045 TopoDS_Shape SMESH_Mesh::GetShapeByEntry(const std::string& entry) const
2047 return _callUp ? _callUp->GetShapeByEntry( entry ) : TopoDS_Shape();
2050 //=============================================================================
2054 //=============================================================================
2056 SMESH_Group* SMESH_Mesh::AddGroup (const SMDSAbs_ElementType theType,
2057 const char* theName,
2059 const TopoDS_Shape& theShape,
2060 const SMESH_PredicatePtr& thePredicate)
2062 if ( _mapGroup.count( theId ))
2064 int id = ( theId < 0 ) ? _groupId : theId;
2065 SMESH_Group* aGroup = new SMESH_Group ( id, this, theType, theName, theShape, thePredicate );
2066 GetMeshDS()->AddGroup( aGroup->GetGroupDS() );
2067 _mapGroup[ id ] = aGroup;
2068 _groupId = 1 + _mapGroup.rbegin()->first;
2072 //================================================================================
2074 * \brief Creates a group based on an existing SMESHDS group. Group ID should be unique
2076 //================================================================================
2078 SMESH_Group* SMESH_Mesh::AddGroup (SMESHDS_GroupBase* groupDS)
2081 throw SALOME_Exception(LOCALIZED ("SMESH_Mesh::AddGroup(): NULL SMESHDS_GroupBase"));
2083 std::map <int, SMESH_Group*>::iterator i_g = _mapGroup.find( groupDS->GetID() );
2084 if ( i_g != _mapGroup.end() && i_g->second )
2086 if ( i_g->second->GetGroupDS() == groupDS )
2089 throw SALOME_Exception(LOCALIZED ("SMESH_Mesh::AddGroup() wrong ID of SMESHDS_GroupBase"));
2091 SMESH_Group* aGroup = new SMESH_Group (groupDS);
2092 _mapGroup[ groupDS->GetID() ] = aGroup;
2093 GetMeshDS()->AddGroup( aGroup->GetGroupDS() );
2095 _groupId = 1 + _mapGroup.rbegin()->first;
2101 //================================================================================
2103 * \brief Creates SMESH_Groups for not wrapped SMESHDS_Groups
2104 * \retval bool - true if new SMESH_Groups have been created
2107 //================================================================================
2109 bool SMESH_Mesh::SynchronizeGroups()
2111 const size_t nbGroups = _mapGroup.size();
2112 const std::set<SMESHDS_GroupBase*>& groups = _meshDS->GetGroups();
2113 std::set<SMESHDS_GroupBase*>::const_iterator gIt = groups.begin();
2114 for ( ; gIt != groups.end(); ++gIt )
2116 SMESHDS_GroupBase* groupDS = (SMESHDS_GroupBase*) *gIt;
2117 _groupId = groupDS->GetID();
2118 if ( !_mapGroup.count( _groupId ))
2119 _mapGroup[_groupId] = new SMESH_Group( groupDS );
2121 if ( !_mapGroup.empty() )
2122 _groupId = 1 + _mapGroup.rbegin()->first;
2124 return nbGroups < _mapGroup.size();
2127 //================================================================================
2129 * \brief Return iterator on all existing groups
2131 //================================================================================
2133 SMESH_Mesh::GroupIteratorPtr SMESH_Mesh::GetGroups() const
2135 typedef std::map <int, SMESH_Group *> TMap;
2136 return GroupIteratorPtr( new SMDS_mapIterator<TMap>( _mapGroup ));
2139 //=============================================================================
2141 * \brief Return a group by ID
2143 //=============================================================================
2145 SMESH_Group* SMESH_Mesh::GetGroup (const int theGroupID) const
2147 std::map <int, SMESH_Group*>::const_iterator id_grp = _mapGroup.find( theGroupID );
2148 if ( id_grp == _mapGroup.end() )
2150 return id_grp->second;
2154 //=============================================================================
2156 * \brief Return IDs of all groups
2158 //=============================================================================
2160 std::list<int> SMESH_Mesh::GetGroupIds() const
2162 std::list<int> anIds;
2163 std::map<int, SMESH_Group*>::const_iterator it = _mapGroup.begin();
2164 for ( ; it != _mapGroup.end(); it++ )
2165 anIds.push_back( it->first );
2170 //================================================================================
2172 * \brief Set a caller of methods at level of CORBA API implementation.
2173 * The set upCaller will be deleted by SMESH_Mesh
2175 //================================================================================
2177 void SMESH_Mesh::SetCallUp( TCallUp* upCaller )
2179 if ( _callUp ) delete _callUp;
2183 //=============================================================================
2187 //=============================================================================
2189 bool SMESH_Mesh::RemoveGroup( const int theGroupID )
2191 if (_mapGroup.find(theGroupID) == _mapGroup.end())
2193 GetMeshDS()->RemoveGroup( _mapGroup[theGroupID]->GetGroupDS() );
2194 delete _mapGroup[theGroupID];
2195 _mapGroup.erase (theGroupID);
2197 _callUp->RemoveGroup( theGroupID );
2201 //=======================================================================
2202 //function : GetAncestors
2203 //purpose : return list of ancestors of theSubShape in the order
2204 // that lower dimension shapes come first.
2205 //=======================================================================
2207 const TopTools_ListOfShape& SMESH_Mesh::GetAncestors(const TopoDS_Shape& theS) const
2209 if ( _mapAncestors.Contains( theS ) )
2210 return _mapAncestors.FindFromKey( theS );
2212 static TopTools_ListOfShape emptyList;
2216 //=======================================================================
2218 //purpose : dumps contents of mesh to stream [ debug purposes ]
2219 //=======================================================================
2221 ostream& SMESH_Mesh::Dump(ostream& save)
2224 save << "========================== Dump contents of mesh ==========================" << endl << endl;
2225 save << ++clause << ") Total number of nodes: \t" << NbNodes() << endl;
2226 save << ++clause << ") Total number of edges: \t" << NbEdges() << endl;
2227 save << ++clause << ") Total number of faces: \t" << NbFaces() << endl;
2228 save << ++clause << ") Total number of polygons: \t" << NbPolygons() << endl;
2229 save << ++clause << ") Total number of volumes: \t" << NbVolumes() << endl;
2230 save << ++clause << ") Total number of polyhedrons:\t" << NbPolyhedrons() << endl << endl;
2231 for ( int isQuadratic = 0; isQuadratic < 2; ++isQuadratic )
2233 std::string orderStr = isQuadratic ? "quadratic" : "linear";
2234 SMDSAbs_ElementOrder order = isQuadratic ? ORDER_QUADRATIC : ORDER_LINEAR;
2236 save << ++clause << ") Total number of " << orderStr << " edges:\t" << NbEdges(order) << endl;
2237 save << ++clause << ") Total number of " << orderStr << " faces:\t" << NbFaces(order) << endl;
2238 if ( NbFaces(order) > 0 ) {
2239 smIdType nb3 = NbTriangles(order);
2240 smIdType nb4 = NbQuadrangles(order);
2241 save << clause << ".1) Number of " << orderStr << " triangles: \t" << nb3 << endl;
2242 save << clause << ".2) Number of " << orderStr << " quadrangles:\t" << nb4 << endl;
2243 if ( nb3 + nb4 != NbFaces(order) ) {
2244 std::map<int,int> myFaceMap;
2245 SMDS_FaceIteratorPtr itFaces=_meshDS->facesIterator();
2246 while( itFaces->more( ) ) {
2247 int nbNodes = itFaces->next()->NbNodes();
2248 if ( myFaceMap.find( nbNodes ) == myFaceMap.end() )
2249 myFaceMap[ nbNodes ] = 0;
2250 myFaceMap[ nbNodes ] = myFaceMap[ nbNodes ] + 1;
2252 save << clause << ".3) Faces in detail: " << endl;
2253 std::map <int,int>::iterator itF;
2254 for (itF = myFaceMap.begin(); itF != myFaceMap.end(); itF++)
2255 save << "--> nb nodes: " << itF->first << " - nb elements:\t" << itF->second << endl;
2258 save << ++clause << ") Total number of " << orderStr << " volumes:\t" << NbVolumes(order) << endl;
2259 if ( NbVolumes(order) > 0 ) {
2260 smIdType nb8 = NbHexas(order);
2261 smIdType nb4 = NbTetras(order);
2262 smIdType nb5 = NbPyramids(order);
2263 smIdType nb6 = NbPrisms(order);
2264 save << clause << ".1) Number of " << orderStr << " hexahedrons: \t" << nb8 << endl;
2265 save << clause << ".2) Number of " << orderStr << " tetrahedrons:\t" << nb4 << endl;
2266 save << clause << ".3) Number of " << orderStr << " prisms: \t" << nb6 << endl;
2267 save << clause << ".4) Number of " << orderStr << " pyramids: \t" << nb5 << endl;
2268 if ( nb8 + nb4 + nb5 + nb6 != NbVolumes(order) ) {
2269 std::map<int,int> myVolumesMap;
2270 SMDS_VolumeIteratorPtr itVolumes=_meshDS->volumesIterator();
2271 while( itVolumes->more( ) ) {
2272 int nbNodes = itVolumes->next()->NbNodes();
2273 if ( myVolumesMap.find( nbNodes ) == myVolumesMap.end() )
2274 myVolumesMap[ nbNodes ] = 0;
2275 myVolumesMap[ nbNodes ] = myVolumesMap[ nbNodes ] + 1;
2277 save << clause << ".5) Volumes in detail: " << endl;
2278 std::map <int,int>::iterator itV;
2279 for (itV = myVolumesMap.begin(); itV != myVolumesMap.end(); itV++)
2280 save << "--> nb nodes: " << itV->first << " - nb elements:\t" << itV->second << endl;
2285 save << "===========================================================================" << endl;
2289 //=======================================================================
2290 //function : GetElementType
2291 //purpose : Returns type of mesh element with certain id
2292 //=======================================================================
2294 SMDSAbs_ElementType SMESH_Mesh::GetElementType( const smIdType id, const bool iselem )
2296 return _meshDS->GetElementType( id, iselem );
2299 //=============================================================================
2301 * \brief Convert group on geometry into standalone group
2303 //=============================================================================
2305 SMESH_Group* SMESH_Mesh::ConvertToStandalone ( int theGroupID )
2307 SMESH_Group* aGroup = 0;
2308 std::map < int, SMESH_Group * >::iterator itg = _mapGroup.find( theGroupID );
2309 if ( itg == _mapGroup.end() )
2312 SMESH_Group* anOldGrp = (*itg).second;
2313 if ( !anOldGrp || !anOldGrp->GetGroupDS() )
2315 SMESHDS_GroupBase* anOldGrpDS = anOldGrp->GetGroupDS();
2317 // create new standalone group
2318 aGroup = new SMESH_Group (theGroupID, this, anOldGrpDS->GetType(), anOldGrp->GetName() );
2319 _mapGroup[theGroupID] = aGroup;
2321 SMESHDS_Group* aNewGrpDS = dynamic_cast<SMESHDS_Group*>( aGroup->GetGroupDS() );
2322 GetMeshDS()->RemoveGroup( anOldGrpDS );
2323 GetMeshDS()->AddGroup( aNewGrpDS );
2325 // add elements (or nodes) into new created group
2326 SMDS_ElemIteratorPtr anItr = anOldGrpDS->GetElements();
2327 while ( anItr->more() )
2328 aNewGrpDS->Add( (anItr->next())->GetID() );
2331 aNewGrpDS->SetColor( anOldGrpDS->GetColor() );
2339 //=============================================================================
2341 * \brief remove submesh order from Mesh
2343 //=============================================================================
2345 void SMESH_Mesh::ClearMeshOrder()
2347 _subMeshOrder.clear();
2350 //=============================================================================
2352 * \brief remove submesh order from Mesh
2354 //=============================================================================
2356 void SMESH_Mesh::SetMeshOrder(const TListOfListOfInt& theOrder )
2358 _subMeshOrder = theOrder;
2361 //=============================================================================
2363 * \brief return submesh order if any
2365 //=============================================================================
2367 const TListOfListOfInt& SMESH_Mesh::GetMeshOrder() const
2369 return _subMeshOrder;
2372 //=============================================================================
2374 * \brief fill _mapAncestors
2376 //=============================================================================
2378 void SMESH_Mesh::fillAncestorsMap(const TopoDS_Shape& theShape)
2380 int desType, ancType;
2381 if ( !theShape.IsSame( GetShapeToMesh()) && theShape.ShapeType() == TopAbs_COMPOUND )
2383 // a geom group is added. Insert it into lists of ancestors before
2384 // the first ancestor more complex than group members
2385 TopoDS_Iterator subIt( theShape );
2386 if ( !subIt.More() ) return;
2387 int memberType = subIt.Value().ShapeType();
2388 for ( desType = TopAbs_VERTEX; desType >= memberType; desType-- )
2389 for (TopExp_Explorer des( theShape, TopAbs_ShapeEnum( desType )); des.More(); des.Next())
2391 if ( !_mapAncestors.Contains( des.Current() )) continue;// issue 0020982
2392 TopTools_ListOfShape& ancList = _mapAncestors.ChangeFromKey( des.Current() );
2393 TopTools_ListIteratorOfListOfShape ancIt (ancList);
2394 while ( ancIt.More() && ancIt.Value().ShapeType() >= memberType )
2396 if ( ancIt.More() ) ancList.InsertBefore( theShape, ancIt );
2397 else ancList.Append( theShape );
2400 else // else added for 52457: Addition of hypotheses is 8 time longer than meshing
2402 for ( desType = TopAbs_VERTEX; desType > TopAbs_COMPOUND; desType-- )
2403 for ( ancType = desType - 1; ancType >= TopAbs_COMPOUND; ancType-- )
2404 TopExp::MapShapesAndAncestors ( theShape,
2405 (TopAbs_ShapeEnum) desType,
2406 (TopAbs_ShapeEnum) ancType,
2409 // visit COMPOUNDs inside a COMPOUND that are not reachable by TopExp_Explorer
2410 if ( theShape.ShapeType() == TopAbs_COMPOUND )
2412 TopoDS_Iterator sIt(theShape);
2413 if ( sIt.More() && sIt.Value().ShapeType() == TopAbs_COMPOUND )
2414 for ( ; sIt.More(); sIt.Next() )
2415 if ( sIt.Value().ShapeType() == TopAbs_COMPOUND )
2416 fillAncestorsMap( sIt.Value() );
2420 //=============================================================================
2422 * \brief sort submeshes according to stored mesh order
2423 * \param theListToSort in out list to be sorted
2424 * \return FALSE if nothing sorted
2426 //=============================================================================
2428 bool SMESH_Mesh::SortByMeshOrder(std::vector<SMESH_subMesh*>& theListToSort) const
2430 if ( _subMeshOrder.empty() || theListToSort.size() < 2 )
2434 // collect all ordered sub-meshes in smVec as pointers
2435 // and get their positions within theListToSort
2437 std::vector<SMESH_subMesh*> smVec;
2438 typedef std::vector<SMESH_subMesh*>::iterator TPosInList;
2439 std::map< size_t, size_t > sortedPos; // index in theListToSort to order
2440 TPosInList smBeg = theListToSort.begin(), smEnd = theListToSort.end();
2441 TListOfListOfInt::const_iterator listIdsIt = _subMeshOrder.begin();
2442 bool needSort = false;
2443 for( ; listIdsIt != _subMeshOrder.end(); listIdsIt++)
2445 const TListOfInt& listOfId = *listIdsIt;
2446 // convert sm ids to sm's
2448 TListOfInt::const_iterator idIt = listOfId.begin();
2449 for ( ; idIt != listOfId.end(); idIt++ )
2451 if ( SMESH_subMesh * sm = GetSubMeshContaining( *idIt ))
2453 smVec.push_back( sm );
2454 if ( sm->GetSubMeshDS() && sm->GetSubMeshDS()->IsComplexSubmesh() )
2456 smVec.reserve( smVec.size() + sm->GetSubMeshDS()->NbSubMeshes() );
2457 SMESHDS_SubMeshIteratorPtr smdsIt = sm->GetSubMeshDS()->GetSubMeshIterator();
2458 while ( smdsIt->more() )
2460 const SMESHDS_SubMesh* smDS = smdsIt->next();
2461 if (( sm = GetSubMeshContaining( smDS->GetID() )))
2462 smVec.push_back( sm );
2467 // find smVec items in theListToSort
2468 for ( size_t i = 0; i < smVec.size(); ++i )
2470 TPosInList smPos = find( smBeg, smEnd, smVec[i] ); // position in theListToSort
2471 if ( smPos != smEnd )
2473 size_t posInList = std::distance( smBeg, smPos );
2474 size_t order = sortedPos.size();
2475 sortedPos.insert( std::make_pair( posInList, order ));
2476 if ( posInList != order )
2484 // set sm of sortedPos from theListToSort to front of orderedSM
2485 // and the rest of theListToSort to orderedSM end
2487 std::vector<SMESH_subMesh*> orderedSM;
2488 orderedSM.reserve( theListToSort.size() );
2489 orderedSM.resize( sortedPos.size() );
2492 sortedPos.insert( std::make_pair( theListToSort.size(), sortedPos.size() ));
2493 for ( const auto & pos_order : sortedPos )
2495 const size_t& posInList = pos_order.first;
2496 const size_t& order = pos_order.second;
2497 if ( order < sortedPos.size() - 1 )
2498 orderedSM[ order ] = theListToSort[ posInList ];
2500 if ( iPrev < posInList )
2501 orderedSM.insert( orderedSM.end(),
2502 theListToSort.begin() + iPrev,
2503 theListToSort.begin() + posInList );
2504 iPrev = posInList + 1;
2507 theListToSort.swap( orderedSM );
2512 //================================================================================
2514 * \brief Return true if given order of sub-meshes is OK
2516 //================================================================================
2518 bool SMESH_Mesh::IsOrderOK( const SMESH_subMesh* smBefore,
2519 const SMESH_subMesh* smAfter ) const
2521 TListOfListOfInt::const_iterator listIdsIt = _subMeshOrder.begin();
2522 for( ; listIdsIt != _subMeshOrder.end(); listIdsIt++)
2524 const TListOfInt& listOfId = *listIdsIt;
2525 int iB = -1, iA = -1, i = 0;
2526 for ( TListOfInt::const_iterator id = listOfId.begin(); id != listOfId.end(); ++id, ++i )
2528 if ( *id == smBefore->GetId() )
2534 else if ( *id == smAfter->GetId() )
2542 return true; // no order imposed to given sub-meshes
2545 //=============================================================================
2547 * \brief sort submeshes according to stored mesh order
2548 * \param theListToSort in out list to be sorted
2549 * \return FALSE if nothing sorted
2551 //=============================================================================
2553 void SMESH_Mesh::getAncestorsSubMeshes (const TopoDS_Shape& theSubShape,
2554 std::vector< SMESH_subMesh* >& theSubMeshes) const
2556 theSubMeshes.clear();
2557 TopTools_ListIteratorOfListOfShape it( GetAncestors( theSubShape ));
2558 for (; it.More(); it.Next() )
2559 if ( SMESH_subMesh* sm = GetSubMeshContaining( it.Value() ))
2560 theSubMeshes.push_back(sm);
2562 // sort submeshes according to stored mesh order
2563 SortByMeshOrder( theSubMeshes );