1 // Copyright (C) 2007-2022 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
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);
239 //================================================================================
241 * \brief Return true if a mesh with given id exists
243 //================================================================================
245 bool SMESH_Mesh::MeshExists( int meshId ) const
247 return _document ? bool( _document->GetMesh( meshId )) : false;
250 //================================================================================
252 * \brief Return a mesh by id
254 //================================================================================
256 SMESH_Mesh* SMESH_Mesh::FindMesh( int meshId ) const
259 return (SMESH_Mesh*) this;
261 if ( StudyContextStruct *aStudyContext = _gen->GetStudyContext())
263 std::map < int, SMESH_Mesh * >::iterator i_m = aStudyContext->mapMesh.find( meshId );
264 if ( i_m != aStudyContext->mapMesh.end() )
270 //=============================================================================
272 * \brief Set geometry to be meshed
274 //=============================================================================
276 void SMESH_Mesh::ShapeToMesh(const TopoDS_Shape & aShape)
278 MESSAGE("SMESH_Mesh::ShapeToMesh");
280 if ( !aShape.IsNull() && _isShapeToMesh ) {
281 if ( aShape.ShapeType() != TopAbs_COMPOUND && // group contents is allowed to change
282 _meshDS->ShapeToMesh().ShapeType() != TopAbs_COMPOUND )
283 throw SALOME_Exception(LOCALIZED ("a shape to mesh has already been defined"));
285 // clear current data
286 if ( !_meshDS->ShapeToMesh().IsNull() )
288 // removal of a shape to mesh, delete objects referring to sub-shapes:
290 _subMeshHolder->DeleteAll();
291 // - groups on geometry
292 std::map <int, SMESH_Group *>::iterator i_gr = _mapGroup.begin();
293 while ( i_gr != _mapGroup.end() ) {
294 if ( dynamic_cast<SMESHDS_GroupOnGeom*>( i_gr->second->GetGroupDS() )) {
295 _meshDS->RemoveGroup( i_gr->second->GetGroupDS() );
297 _mapGroup.erase( i_gr++ );
302 _mapAncestors.Clear();
305 TopoDS_Shape aNullShape;
306 _meshDS->ShapeToMesh( aNullShape );
308 _shapeDiagonal = 0.0;
311 // set a new geometry
312 if ( !aShape.IsNull() )
314 _meshDS->ShapeToMesh(aShape);
315 _isShapeToMesh = true;
316 _nbSubShapes = _meshDS->MaxShapeIndex();
318 // fill map of ancestors
319 fillAncestorsMap(aShape);
323 _isShapeToMesh = false;
324 _shapeDiagonal = 0.0;
325 _meshDS->ShapeToMesh( PseudoShape() );
330 //=======================================================================
332 * \brief Return geometry to be meshed. (It may be a PseudoShape()!)
334 //=======================================================================
336 TopoDS_Shape SMESH_Mesh::GetShapeToMesh() const
338 return _meshDS->ShapeToMesh();
341 //=======================================================================
343 * \brief Return a solid which is returned by GetShapeToMesh() if
344 * a real geometry to be meshed was not set
346 //=======================================================================
348 const TopoDS_Solid& SMESH_Mesh::PseudoShape()
350 static TopoDS_Solid aSolid;
351 if ( aSolid.IsNull() )
353 aSolid = BRepPrimAPI_MakeBox(1,1,1);
358 //=======================================================================
360 * \brief Return diagonal size of bounding box of a shape
362 //=======================================================================
364 double SMESH_Mesh::GetShapeDiagonalSize(const TopoDS_Shape & aShape)
366 if ( !aShape.IsNull() ) {
368 // avoid too long waiting on large shapes. PreciseBoundingBox() was added
369 // to assure same result which else depends on presence of triangulation (IPAL52557).
370 const int maxNbFaces = 4000;
372 for ( TopExp_Explorer f( aShape, TopAbs_FACE ); f.More() && nbFaces < maxNbFaces; f.Next() )
374 bool isPrecise = false;
375 if ( nbFaces < maxNbFaces )
378 GEOMUtils::PreciseBoundingBox( aShape, Box );
386 BRepBndLib::Add( aShape, Box );
389 return sqrt( Box.SquareExtent() );
394 //=======================================================================
396 * \brief Return diagonal size of bounding box of shape to mesh
398 //=======================================================================
400 double SMESH_Mesh::GetShapeDiagonalSize() const
402 if ( _shapeDiagonal == 0. && _isShapeToMesh )
403 const_cast<SMESH_Mesh*>(this)->_shapeDiagonal = GetShapeDiagonalSize( GetShapeToMesh() );
405 return _shapeDiagonal;
408 //================================================================================
410 * \brief Load mesh from study file
412 //================================================================================
414 void SMESH_Mesh::Load()
420 //=======================================================================
422 * \brief Remove all nodes and elements
424 //=======================================================================
426 void SMESH_Mesh::Clear()
428 if ( HasShapeToMesh() ) // remove all nodes and elements
431 _meshDS->ClearMesh();
433 // update compute state of submeshes
434 if ( SMESH_subMesh *sm = GetSubMeshContaining( GetShapeToMesh() ) )
436 sm->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
437 sm->ComputeSubMeshStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
438 sm->ComputeStateEngine( SMESH_subMesh::CLEAN ); // for event listeners (issue 0020918)
439 sm->ComputeSubMeshStateEngine( SMESH_subMesh::CLEAN );
442 else // remove only nodes/elements computed by algorithms
444 if ( SMESH_subMesh *sm = GetSubMeshContaining( GetShapeToMesh() ) )
446 sm->ComputeStateEngine( SMESH_subMesh::CLEAN );
447 sm->ComputeSubMeshStateEngine( SMESH_subMesh::CLEAN );
448 sm->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
449 sm->ComputeSubMeshStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
452 GetMeshDS()->Modified();
456 //=======================================================================
458 * \brief Remove all nodes and elements of indicated shape
460 //=======================================================================
462 void SMESH_Mesh::ClearSubMesh(const int theShapeId)
464 // clear sub-meshes; get ready to re-compute as a side-effect
465 if ( SMESH_subMesh *sm = GetSubMeshContaining( theShapeId ) )
467 SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(/*includeSelf=*/true,
468 /*complexShapeFirst=*/false);
469 while ( smIt->more() )
472 TopAbs_ShapeEnum shapeType = sm->GetSubShape().ShapeType();
473 if ( shapeType == TopAbs_VERTEX || shapeType < TopAbs_SOLID )
474 // all other shapes depends on vertices so they are already cleaned
475 sm->ComputeStateEngine( SMESH_subMesh::CLEAN );
476 // to recompute even if failed
477 sm->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
482 //=======================================================================
483 //function : UNVToMesh
485 //=======================================================================
487 int SMESH_Mesh::UNVToMesh(const char* theFileName)
489 if ( _isShapeToMesh )
490 throw SALOME_Exception(LOCALIZED("a shape to mesh has already been defined"));
491 _isShapeToMesh = false;
493 DriverUNV_R_SMDS_Mesh myReader;
494 myReader.SetMesh(_meshDS);
495 myReader.SetFile(theFileName);
496 myReader.SetMeshId(-1);
499 TGroupNamesMap& aGroupNames = myReader.GetGroupNamesMap();
500 TGroupNamesMap::iterator gr2names;
501 int anId = 1 + ( _mapGroup.empty() ? 0 : _mapGroup.rbegin()->first );
502 for ( gr2names = aGroupNames.begin(); gr2names != aGroupNames.end(); ++gr2names )
504 SMDS_MeshGroup* aGroup = gr2names->first;
505 const std::string& aName = gr2names->second;
506 SMESHDS_Group* aGroupDS = new SMESHDS_Group( anId++, _meshDS, aGroup->GetType() );
507 aGroupDS->SMDSGroup() = std::move( *aGroup );
508 aGroupDS->SetStoreName( aName.c_str() );
509 AddGroup( aGroupDS );
515 //=======================================================================
516 //function : MEDToMesh
518 //=======================================================================
520 int SMESH_Mesh::MEDToMesh(const char* theFileName, const char* theMeshName)
522 if ( _isShapeToMesh )
523 throw SALOME_Exception(LOCALIZED("a shape to mesh has already been defined"));
524 _isShapeToMesh = false;
526 DriverMED_R_SMESHDS_Mesh myReader;
527 myReader.SetMesh(_meshDS);
528 myReader.SetMeshId(-1);
529 myReader.SetFile(theFileName);
530 myReader.SetMeshName(theMeshName);
531 Driver_Mesh::Status status = myReader.Perform();
533 if (SALOME::VerbosityActivated())
535 SMESH_ComputeErrorPtr er = myReader.GetError();
536 if ( er && !er->IsOK() ) std::cout << er->myComment << std::endl;
539 // Reading groups (sub-meshes are out of scope of MED import functionality)
540 std::list<TNameAndType> aGroupNames = myReader.GetGroupNamesAndTypes();
541 std::list<TNameAndType>::iterator name_type = aGroupNames.begin();
542 for ( ; name_type != aGroupNames.end(); name_type++ )
544 SMESH_Group* aGroup = AddGroup( name_type->second, name_type->first.c_str() );
546 SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>( aGroup->GetGroupDS() );
548 aGroupDS->SetStoreName( name_type->first.c_str() );
549 myReader.GetGroup( aGroupDS );
555 _meshDS->CompactMesh();
560 //=======================================================================
561 //function : STLToMesh
563 //=======================================================================
565 std::string SMESH_Mesh::STLToMesh(const char* theFileName)
568 throw SALOME_Exception(LOCALIZED("a shape to mesh has already been defined"));
569 _isShapeToMesh = false;
571 DriverSTL_R_SMDS_Mesh myReader;
572 myReader.SetMesh(_meshDS);
573 myReader.SetFile(theFileName);
574 myReader.SetMeshId(-1);
577 return myReader.GetName();
580 //================================================================================
582 * \brief Reads the given mesh from the CGNS file
583 * \param theFileName - name of the file
584 * \retval int - Driver_Mesh::Status
586 //================================================================================
588 int SMESH_Mesh::CGNSToMesh(const char* theFileName,
589 const int theMeshIndex,
590 std::string& theMeshName)
592 int res = Driver_Mesh::DRS_FAIL;
595 DriverCGNS_Read myReader;
596 myReader.SetMesh(_meshDS);
597 myReader.SetFile(theFileName);
598 myReader.SetMeshId(theMeshIndex);
599 res = myReader.Perform();
600 theMeshName = myReader.GetMeshName();
609 //================================================================================
611 * \brief Fill its data by reading a GMF file
613 //================================================================================
615 SMESH_ComputeErrorPtr SMESH_Mesh::GMFToMesh(const char* theFileName,
616 bool theMakeRequiredGroups)
618 DriverGMF_Read myReader;
619 myReader.SetMesh(_meshDS);
620 myReader.SetFile(theFileName);
621 myReader.SetMakeRequiredGroups( theMakeRequiredGroups );
623 //theMeshName = myReader.GetMeshName();
628 return myReader.GetError();
631 //=============================================================================
635 //=============================================================================
637 SMESH_Hypothesis::Hypothesis_Status
638 SMESH_Mesh::AddHypothesis(const TopoDS_Shape & aSubShape,
640 std::string* anError )
642 MESSAGE("SMESH_Mesh::AddHypothesis");
647 SMESH_subMesh *subMesh = GetSubMesh(aSubShape);
648 if ( !subMesh || !subMesh->GetId())
649 return SMESH_Hypothesis::HYP_BAD_SUBSHAPE;
651 SMESH_Hypothesis *anHyp = GetHypothesis( anHypId );
653 throw SALOME_Exception(LOCALIZED("hypothesis does not exist"));
655 bool isGlobalHyp = IsMainShape( aSubShape );
657 // NotConformAllowed can be only global
660 // NOTE: this is not a correct way to check a name of hypothesis,
661 // there should be an attribute of hypothesis saying that it can/can't
663 std::string hypName = anHyp->GetName();
664 if ( hypName == "NotConformAllowed" )
666 MESSAGE( "Hypothesis <NotConformAllowed> can be only global" );
667 return SMESH_Hypothesis::HYP_INCOMPATIBLE;
673 bool isAlgo = ( anHyp->GetType() != SMESHDS_Hypothesis::PARAM_ALGO );
674 SMESH_subMesh::algo_event event = isAlgo ? SMESH_subMesh::ADD_ALGO : SMESH_subMesh::ADD_HYP;
676 SMESH_Hypothesis::Hypothesis_Status ret = subMesh->AlgoStateEngine(event, anHyp);
678 if ( anError && SMESH_Hypothesis::IsStatusFatal(ret) && subMesh->GetComputeError() )
679 *anError = subMesh->GetComputeError()->myComment;
682 if ( !SMESH_Hypothesis::IsStatusFatal(ret) &&
683 anHyp->GetDim() <= SMESH_Gen::GetShapeDim(aSubShape)) // is added on father
685 event = isAlgo ? SMESH_subMesh::ADD_FATHER_ALGO : SMESH_subMesh::ADD_FATHER_HYP;
687 SMESH_Hypothesis::Hypothesis_Status ret2 =
688 subMesh->SubMeshesAlgoStateEngine(event, anHyp, /*exitOnFatal=*/true);
692 if ( SMESH_Hypothesis::IsStatusFatal( ret ))
694 if ( anError && subMesh->GetComputeError() )
695 *anError = subMesh->GetComputeError()->myComment;
697 event = isAlgo ? SMESH_subMesh::REMOVE_ALGO : SMESH_subMesh::REMOVE_HYP;
698 subMesh->AlgoStateEngine(event, anHyp);
702 // check concurrent hypotheses on ancestors
703 if (ret < SMESH_Hypothesis::HYP_CONCURRENT && !isGlobalHyp )
705 SMESH_subMeshIteratorPtr smIt = subMesh->getDependsOnIterator(false,false);
706 while ( smIt->more() ) {
707 SMESH_subMesh* sm = smIt->next();
708 if ( sm->IsApplicableHypothesis( anHyp )) {
709 ret2 = sm->CheckConcurrentHypothesis( anHyp );
718 HasModificationsToDiscard(); // to reset _isModified flag if a mesh becomes empty
719 GetMeshDS()->Modified();
721 if(SALOME::VerbosityActivated()) subMesh->DumpAlgoState(true);
726 //=============================================================================
730 //=============================================================================
732 SMESH_Hypothesis::Hypothesis_Status
733 SMESH_Mesh::RemoveHypothesis(const TopoDS_Shape & aSubShape,
736 MESSAGE("SMESH_Mesh::RemoveHypothesis");
738 StudyContextStruct *sc = _gen->GetStudyContext();
739 if (sc->mapHypothesis.find(anHypId) == sc->mapHypothesis.end())
740 throw SALOME_Exception(LOCALIZED("hypothesis does not exist"));
742 SMESH_Hypothesis *anHyp = sc->mapHypothesis[anHypId];
743 SCRUTE(anHyp->GetType());
747 bool isAlgo = ( !anHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO );
748 SMESH_subMesh::algo_event event = isAlgo ? SMESH_subMesh::REMOVE_ALGO : SMESH_subMesh::REMOVE_HYP;
750 SMESH_subMesh *subMesh = GetSubMesh(aSubShape);
752 SMESH_Hypothesis::Hypothesis_Status ret = subMesh->AlgoStateEngine(event, anHyp);
754 // there may appear concurrent hyps that were covered by the removed hyp
755 if (ret < SMESH_Hypothesis::HYP_CONCURRENT &&
756 subMesh->IsApplicableHypothesis( anHyp ) &&
757 subMesh->CheckConcurrentHypothesis( anHyp ) != SMESH_Hypothesis::HYP_OK)
758 ret = SMESH_Hypothesis::HYP_CONCURRENT;
761 if (!SMESH_Hypothesis::IsStatusFatal(ret) &&
762 anHyp->GetDim() <= SMESH_Gen::GetShapeDim(aSubShape)) // is removed from father
764 event = isAlgo ? SMESH_subMesh::REMOVE_FATHER_ALGO : SMESH_subMesh::REMOVE_FATHER_HYP;
766 SMESH_Hypothesis::Hypothesis_Status ret2 =
767 subMesh->SubMeshesAlgoStateEngine(event, anHyp);
768 if (ret2 > ret) // more severe
771 // check concurrent hypotheses on ancestors
772 if (ret < SMESH_Hypothesis::HYP_CONCURRENT && !IsMainShape( aSubShape ) )
774 SMESH_subMeshIteratorPtr smIt = subMesh->getDependsOnIterator(false,false);
775 while ( smIt->more() ) {
776 SMESH_subMesh* sm = smIt->next();
777 if ( sm->IsApplicableHypothesis( anHyp )) {
778 ret2 = sm->CheckConcurrentHypothesis( anHyp );
788 HasModificationsToDiscard(); // to reset _isModified flag if mesh become empty
789 GetMeshDS()->Modified();
791 if(SALOME::VerbosityActivated()) subMesh->DumpAlgoState(true);
796 //=============================================================================
800 //=============================================================================
802 const std::list<const SMESHDS_Hypothesis*>&
803 SMESH_Mesh::GetHypothesisList(const TopoDS_Shape & aSubShape) const
805 return _meshDS->GetHypothesis(aSubShape);
808 //=======================================================================
810 * \brief Return the hypothesis assigned to the shape
811 * \param aSubShape - the shape to check
812 * \param aFilter - the hypothesis filter
813 * \param andAncestors - flag to check hypos assigned to ancestors of the shape
814 * \param assignedTo - to return the shape the found hypo is assigned to
815 * \retval SMESH_Hypothesis* - the first hypo passed through aFilter
817 //=======================================================================
819 const SMESH_Hypothesis * SMESH_Mesh::GetHypothesis(const TopoDS_Shape & aSubShape,
820 const SMESH_HypoFilter& aFilter,
821 const bool andAncestors,
822 TopoDS_Shape* assignedTo) const
824 return GetHypothesis( const_cast< SMESH_Mesh* >(this)->GetSubMesh( aSubShape ),
825 aFilter, andAncestors, assignedTo );
828 //=======================================================================
830 * \brief Return the hypothesis assigned to the shape of a sub-mesh
831 * \param aSubMesh - the sub-mesh to check
832 * \param aFilter - the hypothesis filter
833 * \param andAncestors - flag to check hypos assigned to ancestors of the shape
834 * \param assignedTo - to return the shape the found hypo is assigned to
835 * \retval SMESH_Hypothesis* - the first hypo passed through aFilter
837 //=======================================================================
839 const SMESH_Hypothesis * SMESH_Mesh::GetHypothesis(const SMESH_subMesh * aSubMesh,
840 const SMESH_HypoFilter& aFilter,
841 const bool andAncestors,
842 TopoDS_Shape* assignedTo) const
844 if ( !aSubMesh ) return 0;
847 const TopoDS_Shape & aSubShape = aSubMesh->GetSubShape();
848 const std::list<const SMESHDS_Hypothesis*>& hypList = _meshDS->GetHypothesis(aSubShape);
849 std::list<const SMESHDS_Hypothesis*>::const_iterator hyp = hypList.begin();
850 for ( ; hyp != hypList.end(); hyp++ ) {
851 const SMESH_Hypothesis * h = cSMESH_Hyp( *hyp );
852 if ( aFilter.IsOk( h, aSubShape)) {
853 if ( assignedTo ) *assignedTo = aSubShape;
860 // user sorted submeshes of ancestors, according to stored submesh priority
861 std::vector< SMESH_subMesh * > & ancestors =
862 const_cast< std::vector< SMESH_subMesh * > & > ( aSubMesh->GetAncestors() );
863 SortByMeshOrder( ancestors );
865 std::vector<SMESH_subMesh*>::const_iterator smIt = ancestors.begin();
866 for ( ; smIt != ancestors.end(); smIt++ )
868 const TopoDS_Shape& curSh = (*smIt)->GetSubShape();
869 const std::list<const SMESHDS_Hypothesis*>& hypList = _meshDS->GetHypothesis(curSh);
870 std::list<const SMESHDS_Hypothesis*>::const_iterator hyp = hypList.begin();
871 for ( ; hyp != hypList.end(); hyp++ ) {
872 const SMESH_Hypothesis * h = cSMESH_Hyp( *hyp );
873 if (aFilter.IsOk( h, curSh )) {
874 if ( assignedTo ) *assignedTo = curSh;
883 //================================================================================
885 * \brief Return hypotheses assigned to the shape
886 * \param aSubShape - the shape to check
887 * \param aFilter - the hypothesis filter
888 * \param aHypList - the list of the found hypotheses
889 * \param andAncestors - flag to check hypos assigned to ancestors of the shape
890 * \retval int - number of unique hypos in aHypList
892 //================================================================================
894 int SMESH_Mesh::GetHypotheses(const TopoDS_Shape & aSubShape,
895 const SMESH_HypoFilter& aFilter,
896 std::list <const SMESHDS_Hypothesis * >& aHypList,
897 const bool andAncestors,
898 std::list< TopoDS_Shape > * assignedTo/*=0*/) const
900 return GetHypotheses( const_cast< SMESH_Mesh* >(this)->GetSubMesh( aSubShape ),
901 aFilter, aHypList, andAncestors, assignedTo );
904 //================================================================================
906 * \brief Return hypotheses assigned to the shape of a sub-mesh
907 * \param aSubShape - the sub-mesh to check
908 * \param aFilter - the hypothesis filter
909 * \param aHypList - the list of the found hypotheses
910 * \param andAncestors - flag to check hypos assigned to ancestors of the shape
911 * \retval int - number of unique hypos in aHypList
913 //================================================================================
915 int SMESH_Mesh::GetHypotheses(const SMESH_subMesh * aSubMesh,
916 const SMESH_HypoFilter& aFilter,
917 std::list <const SMESHDS_Hypothesis * >& aHypList,
918 const bool andAncestors,
919 std::list< TopoDS_Shape > * assignedTo/*=0*/) const
921 if ( !aSubMesh ) return 0;
923 std::set< std::string > hypTypes; // to exclude same type hypos from the result list
926 // only one main hypothesis is allowed
927 bool mainHypFound = false;
930 std::list<const SMESHDS_Hypothesis*>::const_iterator hyp;
931 for ( hyp = aHypList.begin(); hyp != aHypList.end(); hyp++ ) {
932 if ( hypTypes.insert( (*hyp)->GetName() ).second )
934 if ( !cSMESH_Hyp(*hyp)->IsAuxiliary() )
938 // get hypos from aSubShape
940 const TopoDS_Shape & aSubShape = aSubMesh->GetSubShape();
941 const std::list<const SMESHDS_Hypothesis*>& hypList = _meshDS->GetHypothesis(aSubShape);
942 for ( hyp = hypList.begin(); hyp != hypList.end(); hyp++ )
944 const SMESH_Hypothesis* h = cSMESH_Hyp( *hyp );
945 if (( aFilter.IsOk( h, aSubShape )) &&
946 ( h->IsAuxiliary() || !mainHypFound ) &&
947 ( h->IsAuxiliary() || hypTypes.insert( h->GetName() ).second ))
949 aHypList.push_back( *hyp );
951 if ( !h->IsAuxiliary() )
953 if ( assignedTo ) assignedTo->push_back( aSubShape );
958 // get hypos from ancestors of aSubShape
961 // user sorted submeshes of ancestors, according to stored submesh priority
962 std::vector< SMESH_subMesh * > & ancestors =
963 const_cast< std::vector< SMESH_subMesh * > & > ( aSubMesh->GetAncestors() );
964 SortByMeshOrder( ancestors );
966 std::vector<SMESH_subMesh*>::const_iterator smIt = ancestors.begin();
967 for ( ; smIt != ancestors.end(); smIt++ )
969 const TopoDS_Shape& curSh = (*smIt)->GetSubShape();
970 const std::list<const SMESHDS_Hypothesis*>& hypList = _meshDS->GetHypothesis(curSh);
971 for ( hyp = hypList.begin(); hyp != hypList.end(); hyp++ )
973 const SMESH_Hypothesis* h = cSMESH_Hyp( *hyp );
974 if (( aFilter.IsOk( h, curSh )) &&
975 ( h->IsAuxiliary() || !mainHypFound ) &&
976 ( h->IsAuxiliary() || hypTypes.insert( h->GetName() ).second ))
978 aHypList.push_back( *hyp );
980 if ( !h->IsAuxiliary() )
982 if ( assignedTo ) assignedTo->push_back( curSh );
990 //================================================================================
992 * \brief Return a hypothesis by its ID
994 //================================================================================
996 SMESH_Hypothesis * SMESH_Mesh::GetHypothesis(const int anHypId) const
998 StudyContextStruct *sc = _gen->GetStudyContext();
999 if (sc->mapHypothesis.find(anHypId) == sc->mapHypothesis.end())
1002 SMESH_Hypothesis *anHyp = sc->mapHypothesis[anHypId];
1006 //=============================================================================
1010 //=============================================================================
1012 const std::list<SMESHDS_Command*> & SMESH_Mesh::GetLog()
1014 return _meshDS->GetScript()->GetCommands();
1017 //=============================================================================
1021 //=============================================================================
1022 void SMESH_Mesh::ClearLog()
1024 _meshDS->GetScript()->Clear();
1027 //=============================================================================
1029 * Get or Create the SMESH_subMesh object implementation
1031 //=============================================================================
1033 SMESH_subMesh * SMESH_Mesh::GetSubMesh(const TopoDS_Shape & aSubShape)
1035 int index = _meshDS->ShapeToIndex(aSubShape);
1036 if ( !index && aSubShape.IsNull() )
1039 // for submeshes on GEOM Group
1040 if (( !index || index > _nbSubShapes ) && aSubShape.ShapeType() == TopAbs_COMPOUND )
1042 TopoDS_Iterator it( aSubShape );
1045 index = _meshDS->AddCompoundSubmesh( aSubShape, it.Value().ShapeType() );
1046 // fill map of Ancestors
1047 while ( _nbSubShapes < index )
1048 fillAncestorsMap( _meshDS->IndexToShape( ++_nbSubShapes ));
1052 // return NULL; // neither sub-shape nor a group
1054 SMESH_subMesh* aSubMesh = _subMeshHolder->Get( index );
1057 aSubMesh = new SMESH_subMesh(index, this, _meshDS, aSubShape);
1058 _subMeshHolder->Add( index, aSubMesh );
1060 // include non-computable sub-meshes in SMESH_subMesh::_ancestors of sub-submeshes
1061 switch ( aSubShape.ShapeType() ) {
1062 case TopAbs_COMPOUND:
1065 for ( TopoDS_Iterator subIt( aSubShape ); subIt.More(); subIt.Next() )
1067 SMESH_subMesh* sm = GetSubMesh( subIt.Value() );
1068 SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(/*inclideSelf=*/true);
1069 while ( smIt->more() )
1070 smIt->next()->ClearAncestors();
1078 //=============================================================================
1080 * Get the SMESH_subMesh object implementation. Don't create it, return null
1081 * if it does not exist.
1083 //=============================================================================
1085 SMESH_subMesh *SMESH_Mesh::GetSubMeshContaining(const TopoDS_Shape & aSubShape) const
1087 int index = _meshDS->ShapeToIndex(aSubShape);
1088 return GetSubMeshContaining( index );
1091 //=============================================================================
1093 * Get the SMESH_subMesh object implementation. Don't create it, return null
1094 * if it does not exist.
1096 //=============================================================================
1098 SMESH_subMesh *SMESH_Mesh::GetSubMeshContaining(const int aShapeID) const
1100 SMESH_subMesh *aSubMesh = _subMeshHolder->Get( aShapeID );
1104 //================================================================================
1106 * \brief Return sub-meshes of groups containing the given sub-shape
1108 //================================================================================
1110 std::list<SMESH_subMesh*>
1111 SMESH_Mesh::GetGroupSubMeshesContaining(const TopoDS_Shape & aSubShape) const
1113 std::list<SMESH_subMesh*> found;
1115 SMESH_subMesh * subMesh = GetSubMeshContaining(aSubShape);
1119 // sub-meshes of groups have max IDs, so search from the map end
1120 SMESH_subMeshIteratorPtr smIt( _subMeshHolder->GetIterator( /*reverse=*/true ) );
1121 while ( smIt->more() ) {
1122 SMESH_subMesh* sm = smIt->next();
1123 SMESHDS_SubMesh * ds = sm->GetSubMeshDS();
1124 if ( ds && ds->IsComplexSubmesh() ) {
1125 if ( SMESH_MesherHelper::IsSubShape( aSubShape, sm->GetSubShape() ))
1127 found.push_back( sm );
1131 break; // the rest sub-meshes are not those of groups
1135 if ( found.empty() ) // maybe the main shape is a COMPOUND (issue 0021530)
1137 if ( SMESH_subMesh * mainSM = GetSubMeshContaining(1) )
1138 if ( mainSM->GetSubShape().ShapeType() == TopAbs_COMPOUND )
1140 TopoDS_Iterator it( mainSM->GetSubShape() );
1141 if ( it.Value().ShapeType() == aSubShape.ShapeType() &&
1142 SMESH_MesherHelper::IsSubShape( aSubShape, mainSM->GetSubShape() ))
1143 found.push_back( mainSM );
1146 else // issue 0023068
1148 if ( SMESH_subMesh * mainSM = GetSubMeshContaining(1) )
1149 if ( mainSM->GetSubShape().ShapeType() == TopAbs_COMPOUND )
1150 found.push_back( mainSM );
1154 //=======================================================================
1155 //function : IsUsedHypothesis
1156 //purpose : Return True if anHyp is used to mesh aSubShape
1157 //=======================================================================
1159 bool SMESH_Mesh::IsUsedHypothesis(SMESHDS_Hypothesis * anHyp,
1160 const SMESH_subMesh* aSubMesh)
1162 SMESH_Hypothesis* hyp = static_cast<SMESH_Hypothesis*>(anHyp);
1164 // check if anHyp can be used to mesh aSubMesh
1165 if ( !aSubMesh || !aSubMesh->IsApplicableHypothesis( hyp ))
1168 SMESH_Algo *algo = aSubMesh->GetAlgo();
1171 if (anHyp->GetType() > SMESHDS_Hypothesis::PARAM_ALGO)
1172 return ( anHyp == algo );
1174 // algorithm parameter
1177 // look through hypotheses used by algo
1178 const SMESH_HypoFilter* hypoKind;
1179 if (( hypoKind = algo->GetCompatibleHypoFilter( !hyp->IsAuxiliary() ))) {
1180 std::list <const SMESHDS_Hypothesis * > usedHyps;
1181 if ( GetHypotheses( aSubMesh, *hypoKind, usedHyps, true ))
1182 return ( find( usedHyps.begin(), usedHyps.end(), anHyp ) != usedHyps.end() );
1189 //=======================================================================
1190 //function : NotifySubMeshesHypothesisModification
1191 //purpose : Say all submeshes using theChangedHyp that it has been modified
1192 //=======================================================================
1194 void SMESH_Mesh::NotifySubMeshesHypothesisModification(const SMESH_Hypothesis* hyp)
1197 if ( !GetMeshDS()->IsUsedHypothesis( hyp ))
1200 smIdType nbEntities = ( _meshDS->NbNodes() + _meshDS->NbElements() );
1201 if ( hyp && _callUp && !_callUp->IsLoaded() ) // for not loaded mesh (#16648)
1203 _callUp->HypothesisModified( hyp->GetID(), /*updateIcons=*/true );
1204 nbEntities = ( _meshDS->NbNodes() + _meshDS->NbElements() ); // after loading mesh
1208 const SMESH_HypoFilter* compatibleHypoKind;
1209 std::list <const SMESHDS_Hypothesis * > usedHyps;
1210 std::vector< SMESH_subMesh* > smToNotify;
1211 bool allMeshedEdgesNotified = true;
1213 SMESH_subMeshIteratorPtr smIt( _subMeshHolder->GetIterator() );
1214 while ( smIt->more() )
1216 SMESH_subMesh* aSubMesh = smIt->next();
1217 bool toNotify = false;
1219 // if aSubMesh meshing depends on hyp,
1220 // we call aSubMesh->AlgoStateEngine( MODIF_HYP, hyp ) that causes either
1221 // 1) clearing already computed aSubMesh or
1222 // 2) changing algo_state from MISSING_HYP to HYP_OK when parameters of hyp becomes valid,
1223 // other possible changes are not interesting. (IPAL0052457 - assigning hyp performance pb)
1224 if ( aSubMesh->GetComputeState() == SMESH_subMesh::COMPUTE_OK ||
1225 aSubMesh->GetComputeState() == SMESH_subMesh::FAILED_TO_COMPUTE ||
1226 aSubMesh->GetAlgoState() == SMESH_subMesh::MISSING_HYP ||
1227 hyp->DataDependOnParams() )
1229 const TopoDS_Shape & aSubShape = aSubMesh->GetSubShape();
1231 if (( aSubMesh->IsApplicableHypothesis( hyp )) &&
1232 ( algo = aSubMesh->GetAlgo() ) &&
1233 ( compatibleHypoKind = algo->GetCompatibleHypoFilter( !hyp->IsAuxiliary() )) &&
1234 ( compatibleHypoKind->IsOk( hyp, aSubShape )))
1236 // check if hyp is used by algo
1238 toNotify = ( GetHypotheses( aSubMesh, *compatibleHypoKind, usedHyps, true ) &&
1239 std::find( usedHyps.begin(), usedHyps.end(), hyp ) != usedHyps.end() );
1244 smToNotify.push_back( aSubMesh );
1245 if ( aSubMesh->GetAlgoState() == SMESH_subMesh::MISSING_HYP )
1246 allMeshedEdgesNotified = false; // update of algo state needed, not mesh clearing
1250 if ( !aSubMesh->IsEmpty() &&
1251 aSubMesh->GetSubShape().ShapeType() == TopAbs_EDGE )
1252 allMeshedEdgesNotified = false;
1255 if ( smToNotify.empty() )
1258 // if all meshed EDGEs will be notified then the notification is equivalent
1259 // to the whole mesh clearing, which is usually faster
1260 if ( allMeshedEdgesNotified && NbNodes() > 0 )
1266 // notify in reverse order to avoid filling the pool of IDs
1267 for ( int i = smToNotify.size()-1; i >= 0; --i )
1268 smToNotify[i]->AlgoStateEngine(SMESH_subMesh::MODIF_HYP,
1269 const_cast< SMESH_Hypothesis*>( hyp ));
1271 HasModificationsToDiscard(); // to reset _isModified flag if mesh becomes empty
1272 GetMeshDS()->Modified();
1274 smIdType newNbEntities = ( _meshDS->NbNodes() + _meshDS->NbElements() );
1275 if ( hyp && _callUp )
1276 _callUp->HypothesisModified( hyp->GetID(), newNbEntities != nbEntities );
1279 //=============================================================================
1281 * Auto color functionality
1283 //=============================================================================
1284 void SMESH_Mesh::SetAutoColor(bool theAutoColor)
1286 _isAutoColor = theAutoColor;
1289 bool SMESH_Mesh::GetAutoColor()
1291 return _isAutoColor;
1294 //=======================================================================
1295 //function : SetIsModified
1296 //purpose : Set the flag meaning that the mesh has been edited "manually"
1297 //=======================================================================
1299 void SMESH_Mesh::SetIsModified(bool isModified)
1301 _isModified = isModified;
1304 // check if mesh becomes empty as result of modification
1305 HasModificationsToDiscard();
1308 //=======================================================================
1309 //function : HasModificationsToDiscard
1310 //purpose : Return true if the mesh has been edited since a total re-compute
1311 // and those modifications may prevent successful partial re-compute.
1312 // As a side effect reset _isModified flag if mesh is empty
1314 //=======================================================================
1316 bool SMESH_Mesh::HasModificationsToDiscard() const
1318 if ( ! _isModified )
1321 // return true if the next Compute() will be partial and
1322 // existing but changed elements may prevent successful re-compute
1323 bool hasComputed = false, hasNotComputed = false;
1324 SMESH_subMeshIteratorPtr smIt( _subMeshHolder->GetIterator() );
1325 while ( smIt->more() )
1327 const SMESH_subMesh* aSubMesh = smIt->next();
1328 switch ( aSubMesh->GetSubShape().ShapeType() )
1333 if ( aSubMesh->IsMeshComputed() )
1336 hasNotComputed = true;
1337 if ( hasComputed && hasNotComputed)
1343 if ( NbNodes() < 1 )
1344 const_cast<SMESH_Mesh*>(this)->_isModified = false;
1349 //=============================================================================
1351 * \brief Return true if all sub-meshes are computed OK - to update an icon
1353 //=============================================================================
1355 bool SMESH_Mesh::IsComputedOK()
1357 if ( NbNodes() == 0 )
1360 // if ( !HasShapeToMesh() )
1363 if ( SMESH_subMesh* mainSM = GetSubMeshContaining( 1 ))
1365 SMESH_subMeshIteratorPtr smIt = mainSM->getDependsOnIterator(/*includeSelf=*/true);
1366 while ( smIt->more() )
1368 const SMESH_subMesh* sm = smIt->next();
1369 if ( !sm->IsAlwaysComputed() )
1370 switch ( sm->GetComputeState() )
1372 case SMESH_subMesh::NOT_READY:
1373 case SMESH_subMesh::COMPUTE_OK:
1375 case SMESH_subMesh::FAILED_TO_COMPUTE:
1376 case SMESH_subMesh::READY_TO_COMPUTE:
1384 //================================================================================
1386 * \brief Check if any groups of the same type have equal names
1388 //================================================================================
1390 bool SMESH_Mesh::HasDuplicatedGroupNamesMED()
1392 // Corrected for Mantis issue 0020028
1393 std::map< SMDSAbs_ElementType, std::set< std::string > > aGroupNames;
1394 for ( std::map<int, SMESH_Group*>::iterator it = _mapGroup.begin(); it != _mapGroup.end(); it++ )
1396 SMESH_Group* aGroup = it->second;
1397 SMDSAbs_ElementType aType = aGroup->GetGroupDS()->GetType();
1398 std::string aGroupName = aGroup->GetName();
1399 aGroupName.resize( MAX_MED_GROUP_NAME_LENGTH );
1400 if ( !aGroupNames[aType].insert(aGroupName).second )
1407 //================================================================================
1409 * \brief Export the mesh to a writer
1411 //================================================================================
1413 void SMESH_Mesh::exportMEDCommmon(DriverMED_W_SMESHDS_Mesh& theWriter,
1414 const char* theMeshName,
1416 const SMESHDS_Mesh* theMeshPart,
1417 bool theAutoDimension,
1418 bool theAddODOnVertices,
1419 double theZTolerance,
1420 bool theSaveNumbers)
1422 Driver_Mesh::Status status = Driver_Mesh::DRS_OK;
1426 theWriter.SetMesh ( theMeshPart ? (SMESHDS_Mesh*) theMeshPart : _meshDS );
1427 theWriter.SetAutoDimension( theAutoDimension );
1428 theWriter.AddODOnVertices ( theAddODOnVertices );
1429 theWriter.SetZTolerance ( theZTolerance );
1430 theWriter.SetSaveNumbers ( theSaveNumbers );
1432 theWriter.SetMeshId ( _id );
1434 theWriter.SetMeshId ( -1 );
1435 theWriter.SetMeshName ( theMeshName );
1438 if ( theAutoGroups ) {
1439 theWriter.AddGroupOfNodes();
1440 theWriter.AddGroupOfEdges();
1441 theWriter.AddGroupOfFaces();
1442 theWriter.AddGroupOfVolumes();
1443 theWriter.AddGroupOf0DElems();
1444 theWriter.AddGroupOfBalls();
1447 // Pass groups to writer. Provide unique group names.
1448 //set<string> aGroupNames; // Corrected for Mantis issue 0020028
1451 std::map< SMDSAbs_ElementType, std::set<std::string> > aGroupNames;
1453 int maxNbIter = 10000; // to guarantee cycle finish
1454 for ( std::map<int, SMESH_Group*>::iterator it = _mapGroup.begin();
1455 it != _mapGroup.end();
1457 SMESH_Group* aGroup = it->second;
1458 SMESHDS_GroupBase* aGroupDS = aGroup->GetGroupDS();
1460 SMDSAbs_ElementType aType = aGroupDS->GetType();
1461 std::string aGroupName0 = aGroup->GetName();
1462 aGroupName0.resize(MAX_MED_GROUP_NAME_LENGTH);
1463 std::string aGroupName = aGroupName0;
1464 for (int i = 1; !aGroupNames[aType].insert(aGroupName).second && i < maxNbIter; i++) {
1465 sprintf(&aString[0], "GR_%d_%s", i, aGroupName0.c_str());
1466 aGroupName = aString;
1467 aGroupName.resize(MAX_MED_GROUP_NAME_LENGTH);
1469 aGroupDS->SetStoreName( aGroupName.c_str() );
1470 theWriter.AddGroup( aGroupDS );
1475 status = theWriter.Perform();
1477 SMESH_CATCH( SMESH::throwSalomeEx );
1479 if ( status == Driver_Mesh::DRS_TOO_LARGE_MESH )
1480 throw TooLargeForExport("MED");
1483 //================================================================================
1485 * Same as SMESH_Mesh::ExportMED except for \a file and \a theVersion
1487 //================================================================================
1489 MEDCoupling::MCAuto<MEDCoupling::DataArrayByte>
1490 SMESH_Mesh::ExportMEDCoupling(const char* theMeshName,
1492 const SMESHDS_Mesh* theMeshPart,
1493 bool theAutoDimension,
1494 bool theAddODOnVertices,
1495 double theZTolerance,
1496 bool theSaveNumbers)
1498 DriverMED_W_SMESHDS_Mesh_Mem writer;
1499 this->exportMEDCommmon( writer, theMeshName, theAutoGroups, theMeshPart, theAutoDimension,
1500 theAddODOnVertices, theZTolerance, theSaveNumbers);
1501 return writer.getData();
1504 //================================================================================
1506 * \brief Export the mesh to a med file
1507 * \param [in] theFile - name of the MED file
1508 * \param [in] theMeshName - name of this mesh
1509 * \param [in] theAutoGroups - boolean parameter for creating/not creating
1510 * the groups Group_On_All_Nodes, Group_On_All_Faces, ... ;
1511 * the typical use is auto_groups=false.
1512 * \param [in] theVersion - define the minor (xy, where version is x.y.z) of MED file format.
1513 * If theVersion is equal to -1, the minor version is not changed (default).
1514 * \param [in] theMeshPart - mesh data to export
1515 * \param [in] theAutoDimension - if \c true, a space dimension of a MED mesh can be either
1516 * - 1D if all mesh nodes lie on OX coordinate axis, or
1517 * - 2D if all mesh nodes lie on XOY coordinate plane, or
1518 * - 3D in the rest cases.
1519 * If \a theAutoDimension is \c false, the space dimension is always 3.
1520 * \param [in] theAddODOnVertices - to create 0D elements on all vertices
1521 * \param [in] theZTolerance - tolerance in Z direction. If Z coordinate of a node is close to zero
1522 * within a given tolerance, the coordinate is set to zero.
1523 * If \a ZTolerance is negative, the node coordinates are kept as is.
1524 * \param [in] theSaveNumbers : enable saving numbers of nodes and cells.
1525 * \return int - mesh index in the file
1527 //================================================================================
1529 void SMESH_Mesh::ExportMED(const char * theFile,
1530 const char* theMeshName,
1533 const SMESHDS_Mesh* theMeshPart,
1534 bool theAutoDimension,
1535 bool theAddODOnVertices,
1536 double theZTolerance,
1537 bool theSaveNumbers)
1539 MESSAGE("MED_VERSION:"<< theVersion);
1540 DriverMED_W_SMESHDS_Mesh writer;
1541 writer.SetFile( theFile, theVersion );
1542 this->exportMEDCommmon( writer, theMeshName, theAutoGroups, theMeshPart, theAutoDimension, theAddODOnVertices, theZTolerance, theSaveNumbers );
1545 //================================================================================
1547 * \brief Export the mesh to a DAT file
1549 //================================================================================
1551 void SMESH_Mesh::ExportDAT(const char * file,
1552 const SMESHDS_Mesh* meshPart,
1553 const bool renumber)
1555 Driver_Mesh::Status status;
1558 DriverDAT_W_SMDS_Mesh writer;
1559 writer.SetFile( file );
1560 writer.SetMesh( meshPart ? (SMESHDS_Mesh*) meshPart : _meshDS );
1561 writer.SetMeshId(_id);
1562 writer.SetRenumber( renumber );
1563 status = writer.Perform();
1565 SMESH_CATCH( SMESH::throwSalomeEx );
1567 if ( status == Driver_Mesh::DRS_TOO_LARGE_MESH )
1568 throw TooLargeForExport("DAT");
1571 //================================================================================
1573 * \brief Export the mesh to an UNV file
1575 //================================================================================
1577 void SMESH_Mesh::ExportUNV(const char * file,
1578 const SMESHDS_Mesh* meshPart,
1579 const bool renumber)
1581 Driver_Mesh::Status status;
1584 DriverUNV_W_SMDS_Mesh writer;
1585 writer.SetFile( file );
1586 writer.SetMesh( meshPart ? (SMESHDS_Mesh*) meshPart : _meshDS );
1587 writer.SetMeshId(_id);
1588 writer.SetRenumber( renumber );
1590 // pass group names to SMESHDS
1593 std::map<int, SMESH_Group*>::iterator it = _mapGroup.begin();
1594 for ( ; it != _mapGroup.end(); it++ ) {
1595 SMESH_Group* aGroup = it->second;
1596 SMESHDS_GroupBase* aGroupDS = aGroup->GetGroupDS();
1598 std::string aGroupName = aGroup->GetName();
1599 aGroupDS->SetStoreName( aGroupName.c_str() );
1600 writer.AddGroup( aGroupDS );
1604 status = writer.Perform();
1606 SMESH_CATCH( SMESH::throwSalomeEx );
1608 if ( status == Driver_Mesh::DRS_TOO_LARGE_MESH )
1609 throw TooLargeForExport("UNV");
1612 //================================================================================
1614 * \brief Export the mesh to an STL file
1616 //================================================================================
1618 void SMESH_Mesh::ExportSTL(const char * file,
1621 const SMESHDS_Mesh* meshPart)
1623 Driver_Mesh::Status status;
1626 DriverSTL_W_SMDS_Mesh writer;
1627 writer.SetFile( file );
1628 writer.SetIsAscii( isascii );
1629 writer.SetMesh( meshPart ? (SMESHDS_Mesh*) meshPart : _meshDS);
1630 writer.SetMeshId(_id);
1631 if ( name ) writer.SetName( name );
1632 status = writer.Perform();
1634 SMESH_CATCH( SMESH::throwSalomeEx );
1636 if ( status == Driver_Mesh::DRS_TOO_LARGE_MESH )
1637 throw TooLargeForExport("STL");
1640 //================================================================================
1642 * \brief Export the mesh to the CGNS file
1644 //================================================================================
1646 void SMESH_Mesh::ExportCGNS(const char * file,
1647 const SMESHDS_Mesh* meshDS,
1648 const char * meshName,
1649 const bool groupElemsByType)
1652 int res = Driver_Mesh::DRS_FAIL;
1655 // pass group names to SMESHDS
1656 std::map<int, SMESH_Group*>::iterator it = _mapGroup.begin();
1657 for ( ; it != _mapGroup.end(); it++ ) {
1658 SMESH_Group* group = it->second;
1659 SMESHDS_GroupBase* groupDS = group->GetGroupDS();
1661 std::string groupName = group->GetName();
1662 groupDS->SetStoreName( groupName.c_str() );
1667 DriverCGNS_Write writer;
1668 writer.SetFile( file );
1669 writer.SetMesh( const_cast<SMESHDS_Mesh*>( meshDS ));
1670 writer.SetMeshName( SMESH_Comment("Mesh_") << meshDS->GetPersistentId());
1671 if ( meshName && meshName[0] )
1672 writer.SetMeshName( meshName );
1673 writer.SetElementsByType( groupElemsByType );
1674 res = writer.Perform();
1675 if ( res != Driver_Mesh::DRS_OK )
1677 SMESH_ComputeErrorPtr err = writer.GetError();
1678 if ( err && !err->IsOK() && !err->myComment.empty() )
1679 throw SALOME_Exception(("Export failed: " + err->myComment ).c_str() );
1683 SMESH_CATCH( SMESH::throwSalomeEx );
1685 if ( res == Driver_Mesh::DRS_TOO_LARGE_MESH )
1686 throw TooLargeForExport("CGNS");
1688 if ( res != Driver_Mesh::DRS_OK )
1689 throw SALOME_Exception("Export failed");
1692 //================================================================================
1694 * \brief Export the mesh to a GMF file
1696 //================================================================================
1698 void SMESH_Mesh::ExportGMF(const char * file,
1699 const SMESHDS_Mesh* meshDS,
1700 bool withRequiredGroups)
1702 Driver_Mesh::Status status;
1705 DriverGMF_Write writer;
1706 writer.SetFile( file );
1707 writer.SetMesh( const_cast<SMESHDS_Mesh*>( meshDS ));
1708 writer.SetExportRequiredGroups( withRequiredGroups );
1710 status = writer.Perform();
1712 SMESH_CATCH( SMESH::throwSalomeEx );
1714 if ( status == Driver_Mesh::DRS_TOO_LARGE_MESH )
1715 throw TooLargeForExport("GMF");
1718 //================================================================================
1720 * \brief Return a ratio of "compute cost" of computed sub-meshes to the whole
1723 //================================================================================
1725 double SMESH_Mesh::GetComputeProgress() const
1727 double totalCost = 1e-100, computedCost = 0;
1728 const SMESH_subMesh* curSM = _gen->GetCurrentSubMesh();
1730 // get progress of a current algo
1731 TColStd_MapOfInteger currentSubIds;
1733 if ( SMESH_Algo* algo = curSM->GetAlgo() )
1735 int algoNotDoneCost = 0, algoDoneCost = 0;
1736 const std::vector<SMESH_subMesh*>& smToCompute = algo->SubMeshesToCompute();
1737 for ( size_t i = 0; i < smToCompute.size(); ++i )
1739 if ( smToCompute[i]->IsEmpty() || smToCompute.size() == 1 )
1740 algoNotDoneCost += smToCompute[i]->GetComputeCost();
1742 algoDoneCost += smToCompute[i]->GetComputeCost();
1743 currentSubIds.Add( smToCompute[i]->GetId() );
1749 rate = algo->GetProgress();
1752 if (SALOME::VerbosityActivated())
1753 std::cerr << "Exception in " << algo->GetName() << "::GetProgress()" << std::endl;
1755 if ( 0. < rate && rate < 1.001 )
1757 computedCost += rate * ( algoDoneCost + algoNotDoneCost );
1761 rate = algo->GetProgressByTic();
1762 computedCost += algoDoneCost + rate * algoNotDoneCost;
1766 // get cost of already treated sub-meshes
1767 if ( SMESH_subMesh* mainSM = GetSubMeshContaining( 1 ))
1769 SMESH_subMeshIteratorPtr smIt = mainSM->getDependsOnIterator(/*includeSelf=*/true);
1770 while ( smIt->more() )
1772 const SMESH_subMesh* sm = smIt->next();
1773 const int smCost = sm->GetComputeCost();
1774 totalCost += smCost;
1775 if ( !currentSubIds.Contains( sm->GetId() ) )
1777 if (( !sm->IsEmpty() ) ||
1778 ( sm->GetComputeState() == SMESH_subMesh::FAILED_TO_COMPUTE &&
1779 !sm->DependsOn( curSM ) ))
1780 computedCost += smCost;
1784 return computedCost / totalCost;
1787 //================================================================================
1789 * \brief Return number of nodes in the mesh
1791 //================================================================================
1793 smIdType SMESH_Mesh::NbNodes() const
1795 return _meshDS->NbNodes();
1798 //================================================================================
1800 * \brief Return number of edges of given order in the mesh
1802 //================================================================================
1804 smIdType SMESH_Mesh::Nb0DElements() const
1806 return _meshDS->GetMeshInfo().Nb0DElements();
1809 //================================================================================
1811 * \brief Return number of edges of given order in the mesh
1813 //================================================================================
1815 smIdType SMESH_Mesh::NbEdges(SMDSAbs_ElementOrder order) const
1817 return _meshDS->GetMeshInfo().NbEdges(order);
1820 //================================================================================
1822 * \brief Return number of faces of given order in the mesh
1824 //================================================================================
1826 smIdType SMESH_Mesh::NbFaces(SMDSAbs_ElementOrder order) const
1828 return _meshDS->GetMeshInfo().NbFaces(order);
1831 //================================================================================
1833 * \brief Return the number of faces in the mesh
1835 //================================================================================
1837 smIdType SMESH_Mesh::NbTriangles(SMDSAbs_ElementOrder order) const
1839 return _meshDS->GetMeshInfo().NbTriangles(order);
1842 //================================================================================
1844 * \brief Return number of biquadratic triangles in the mesh
1846 //================================================================================
1848 smIdType SMESH_Mesh::NbBiQuadTriangles() const
1850 return _meshDS->GetMeshInfo().NbBiQuadTriangles();
1853 //================================================================================
1855 * \brief Return the number nodes faces in the mesh
1857 //================================================================================
1859 smIdType SMESH_Mesh::NbQuadrangles(SMDSAbs_ElementOrder order) const
1861 return _meshDS->GetMeshInfo().NbQuadrangles(order);
1864 //================================================================================
1866 * \brief Return number of biquadratic quadrangles in the mesh
1868 //================================================================================
1870 smIdType SMESH_Mesh::NbBiQuadQuadrangles() const
1872 return _meshDS->GetMeshInfo().NbBiQuadQuadrangles();
1875 //================================================================================
1877 * \brief Return the number of polygonal faces in the mesh
1879 //================================================================================
1881 smIdType SMESH_Mesh::NbPolygons(SMDSAbs_ElementOrder order) const
1883 return _meshDS->GetMeshInfo().NbPolygons(order);
1886 //================================================================================
1888 * \brief Return number of volumes of given order in the mesh
1890 //================================================================================
1892 smIdType SMESH_Mesh::NbVolumes(SMDSAbs_ElementOrder order) const
1894 return _meshDS->GetMeshInfo().NbVolumes(order);
1897 //================================================================================
1899 * \brief Return number of tetrahedrons of given order in the mesh
1901 //================================================================================
1903 smIdType SMESH_Mesh::NbTetras(SMDSAbs_ElementOrder order) const
1905 return _meshDS->GetMeshInfo().NbTetras(order);
1908 //================================================================================
1910 * \brief Return number of hexahedrons of given order in the mesh
1912 //================================================================================
1914 smIdType SMESH_Mesh::NbHexas(SMDSAbs_ElementOrder order) const
1916 return _meshDS->GetMeshInfo().NbHexas(order);
1919 //================================================================================
1921 * \brief Return number of triquadratic hexahedrons in the mesh
1923 //================================================================================
1925 smIdType SMESH_Mesh::NbTriQuadraticHexas() const
1927 return _meshDS->GetMeshInfo().NbTriQuadHexas();
1930 //================================================================================
1932 * \brief Return number of pyramids of given order in the mesh
1934 //================================================================================
1936 smIdType SMESH_Mesh::NbPyramids(SMDSAbs_ElementOrder order) const
1938 return _meshDS->GetMeshInfo().NbPyramids(order);
1941 //================================================================================
1943 * \brief Return number of prisms (penthahedrons) of given order in the mesh
1945 //================================================================================
1947 smIdType SMESH_Mesh::NbPrisms(SMDSAbs_ElementOrder order) const
1949 return _meshDS->GetMeshInfo().NbPrisms(order);
1952 smIdType SMESH_Mesh::NbQuadPrisms() const
1954 return _meshDS->GetMeshInfo().NbQuadPrisms();
1957 smIdType SMESH_Mesh::NbBiQuadPrisms() const
1959 return _meshDS->GetMeshInfo().NbBiQuadPrisms();
1963 //================================================================================
1965 * \brief Return number of hexagonal prisms in the mesh
1967 //================================================================================
1969 smIdType SMESH_Mesh::NbHexagonalPrisms() const
1971 return _meshDS->GetMeshInfo().NbHexPrisms();
1974 //================================================================================
1976 * \brief Return number of polyhedrons in the mesh
1978 //================================================================================
1980 smIdType SMESH_Mesh::NbPolyhedrons() const
1982 return _meshDS->GetMeshInfo().NbPolyhedrons();
1985 //================================================================================
1987 * \brief Return number of ball elements in the mesh
1989 //================================================================================
1991 smIdType SMESH_Mesh::NbBalls() const
1993 return _meshDS->GetMeshInfo().NbBalls();
1996 //================================================================================
1998 * \brief Return number of submeshes in the mesh
2000 //================================================================================
2002 smIdType SMESH_Mesh::NbSubMesh() const
2004 return _meshDS->NbSubMesh();
2007 //================================================================================
2009 * \brief Returns number of meshes in the Study, that is supposed to be
2010 * equal to SMESHDS_Document::NbMeshes()
2012 //================================================================================
2014 int SMESH_Mesh::NbMeshes() const // nb meshes in the Study
2016 return _document->NbMeshes();
2019 //=======================================================================
2020 //function : IsNotConformAllowed
2021 //purpose : check if a hypothesis allowing notconform mesh is present
2022 //=======================================================================
2024 bool SMESH_Mesh::IsNotConformAllowed() const
2026 MESSAGE("SMESH_Mesh::IsNotConformAllowed");
2028 static SMESH_HypoFilter filter( SMESH_HypoFilter::HasName( "NotConformAllowed" ));
2029 return GetHypothesis( _meshDS->ShapeToMesh(), filter, false );
2032 //=======================================================================
2033 //function : IsMainShape
2035 //=======================================================================
2037 bool SMESH_Mesh::IsMainShape(const TopoDS_Shape& theShape) const
2039 return theShape.IsSame(_meshDS->ShapeToMesh() );
2042 //=======================================================================
2043 //function : GetShapeByEntry
2044 //purpose : return TopoDS_Shape by its study entry
2045 //=======================================================================
2047 TopoDS_Shape SMESH_Mesh::GetShapeByEntry(const std::string& entry) const
2049 return _callUp ? _callUp->GetShapeByEntry( entry ) : TopoDS_Shape();
2052 //=============================================================================
2056 //=============================================================================
2058 SMESH_Group* SMESH_Mesh::AddGroup (const SMDSAbs_ElementType theType,
2059 const char* theName,
2061 const TopoDS_Shape& theShape,
2062 const SMESH_PredicatePtr& thePredicate)
2064 if ( _mapGroup.count( theId ))
2066 int id = ( theId < 0 ) ? _groupId : theId;
2067 SMESH_Group* aGroup = new SMESH_Group ( id, this, theType, theName, theShape, thePredicate );
2068 GetMeshDS()->AddGroup( aGroup->GetGroupDS() );
2069 _mapGroup[ id ] = aGroup;
2070 _groupId = 1 + _mapGroup.rbegin()->first;
2074 //================================================================================
2076 * \brief Creates a group based on an existing SMESHDS group. Group ID should be unique
2078 //================================================================================
2080 SMESH_Group* SMESH_Mesh::AddGroup (SMESHDS_GroupBase* groupDS)
2083 throw SALOME_Exception(LOCALIZED ("SMESH_Mesh::AddGroup(): NULL SMESHDS_GroupBase"));
2085 std::map <int, SMESH_Group*>::iterator i_g = _mapGroup.find( groupDS->GetID() );
2086 if ( i_g != _mapGroup.end() && i_g->second )
2088 if ( i_g->second->GetGroupDS() == groupDS )
2091 throw SALOME_Exception(LOCALIZED ("SMESH_Mesh::AddGroup() wrong ID of SMESHDS_GroupBase"));
2093 SMESH_Group* aGroup = new SMESH_Group (groupDS);
2094 _mapGroup[ groupDS->GetID() ] = aGroup;
2095 GetMeshDS()->AddGroup( aGroup->GetGroupDS() );
2097 _groupId = 1 + _mapGroup.rbegin()->first;
2103 //================================================================================
2105 * \brief Creates SMESH_Groups for not wrapped SMESHDS_Groups
2106 * \retval bool - true if new SMESH_Groups have been created
2109 //================================================================================
2111 bool SMESH_Mesh::SynchronizeGroups()
2113 const size_t nbGroups = _mapGroup.size();
2114 const std::set<SMESHDS_GroupBase*>& groups = _meshDS->GetGroups();
2115 std::set<SMESHDS_GroupBase*>::const_iterator gIt = groups.begin();
2116 for ( ; gIt != groups.end(); ++gIt )
2118 SMESHDS_GroupBase* groupDS = (SMESHDS_GroupBase*) *gIt;
2119 _groupId = groupDS->GetID();
2120 if ( !_mapGroup.count( _groupId ))
2121 _mapGroup[_groupId] = new SMESH_Group( groupDS );
2123 if ( !_mapGroup.empty() )
2124 _groupId = 1 + _mapGroup.rbegin()->first;
2126 return nbGroups < _mapGroup.size();
2129 //================================================================================
2131 * \brief Return iterator on all existing groups
2133 //================================================================================
2135 SMESH_Mesh::GroupIteratorPtr SMESH_Mesh::GetGroups() const
2137 typedef std::map <int, SMESH_Group *> TMap;
2138 return GroupIteratorPtr( new SMDS_mapIterator<TMap>( _mapGroup ));
2141 //=============================================================================
2143 * \brief Return a group by ID
2145 //=============================================================================
2147 SMESH_Group* SMESH_Mesh::GetGroup (const int theGroupID) const
2149 std::map <int, SMESH_Group*>::const_iterator id_grp = _mapGroup.find( theGroupID );
2150 if ( id_grp == _mapGroup.end() )
2152 return id_grp->second;
2156 //=============================================================================
2158 * \brief Return IDs of all groups
2160 //=============================================================================
2162 std::list<int> SMESH_Mesh::GetGroupIds() const
2164 std::list<int> anIds;
2165 std::map<int, SMESH_Group*>::const_iterator it = _mapGroup.begin();
2166 for ( ; it != _mapGroup.end(); it++ )
2167 anIds.push_back( it->first );
2172 //================================================================================
2174 * \brief Set a caller of methods at level of CORBA API implementation.
2175 * The set upCaller will be deleted by SMESH_Mesh
2177 //================================================================================
2179 void SMESH_Mesh::SetCallUp( TCallUp* upCaller )
2181 if ( _callUp ) delete _callUp;
2185 //=============================================================================
2189 //=============================================================================
2191 bool SMESH_Mesh::RemoveGroup( const int theGroupID )
2193 if (_mapGroup.find(theGroupID) == _mapGroup.end())
2195 GetMeshDS()->RemoveGroup( _mapGroup[theGroupID]->GetGroupDS() );
2196 delete _mapGroup[theGroupID];
2197 _mapGroup.erase (theGroupID);
2199 _callUp->RemoveGroup( theGroupID );
2203 //=======================================================================
2204 //function : GetAncestors
2205 //purpose : return list of ancestors of theSubShape in the order
2206 // that lower dimension shapes come first.
2207 //=======================================================================
2209 const TopTools_ListOfShape& SMESH_Mesh::GetAncestors(const TopoDS_Shape& theS) const
2211 if ( _mapAncestors.Contains( theS ) )
2212 return _mapAncestors.FindFromKey( theS );
2214 static TopTools_ListOfShape emptyList;
2218 //=======================================================================
2220 //purpose : dumps contents of mesh to stream [ debug purposes ]
2221 //=======================================================================
2223 ostream& SMESH_Mesh::Dump(ostream& save)
2226 save << "========================== Dump contents of mesh ==========================" << endl << endl;
2227 save << ++clause << ") Total number of nodes: \t" << NbNodes() << endl;
2228 save << ++clause << ") Total number of edges: \t" << NbEdges() << endl;
2229 save << ++clause << ") Total number of faces: \t" << NbFaces() << endl;
2230 save << ++clause << ") Total number of polygons: \t" << NbPolygons() << endl;
2231 save << ++clause << ") Total number of volumes: \t" << NbVolumes() << endl;
2232 save << ++clause << ") Total number of polyhedrons:\t" << NbPolyhedrons() << endl << endl;
2233 for ( int isQuadratic = 0; isQuadratic < 2; ++isQuadratic )
2235 std::string orderStr = isQuadratic ? "quadratic" : "linear";
2236 SMDSAbs_ElementOrder order = isQuadratic ? ORDER_QUADRATIC : ORDER_LINEAR;
2238 save << ++clause << ") Total number of " << orderStr << " edges:\t" << NbEdges(order) << endl;
2239 save << ++clause << ") Total number of " << orderStr << " faces:\t" << NbFaces(order) << endl;
2240 if ( NbFaces(order) > 0 ) {
2241 smIdType nb3 = NbTriangles(order);
2242 smIdType nb4 = NbQuadrangles(order);
2243 save << clause << ".1) Number of " << orderStr << " triangles: \t" << nb3 << endl;
2244 save << clause << ".2) Number of " << orderStr << " quadrangles:\t" << nb4 << endl;
2245 if ( nb3 + nb4 != NbFaces(order) ) {
2246 std::map<int,int> myFaceMap;
2247 SMDS_FaceIteratorPtr itFaces=_meshDS->facesIterator();
2248 while( itFaces->more( ) ) {
2249 int nbNodes = itFaces->next()->NbNodes();
2250 if ( myFaceMap.find( nbNodes ) == myFaceMap.end() )
2251 myFaceMap[ nbNodes ] = 0;
2252 myFaceMap[ nbNodes ] = myFaceMap[ nbNodes ] + 1;
2254 save << clause << ".3) Faces in detail: " << endl;
2255 std::map <int,int>::iterator itF;
2256 for (itF = myFaceMap.begin(); itF != myFaceMap.end(); itF++)
2257 save << "--> nb nodes: " << itF->first << " - nb elements:\t" << itF->second << endl;
2260 save << ++clause << ") Total number of " << orderStr << " volumes:\t" << NbVolumes(order) << endl;
2261 if ( NbVolumes(order) > 0 ) {
2262 smIdType nb8 = NbHexas(order);
2263 smIdType nb4 = NbTetras(order);
2264 smIdType nb5 = NbPyramids(order);
2265 smIdType nb6 = NbPrisms(order);
2266 save << clause << ".1) Number of " << orderStr << " hexahedrons: \t" << nb8 << endl;
2267 save << clause << ".2) Number of " << orderStr << " tetrahedrons:\t" << nb4 << endl;
2268 save << clause << ".3) Number of " << orderStr << " prisms: \t" << nb6 << endl;
2269 save << clause << ".4) Number of " << orderStr << " pyramids: \t" << nb5 << endl;
2270 if ( nb8 + nb4 + nb5 + nb6 != NbVolumes(order) ) {
2271 std::map<int,int> myVolumesMap;
2272 SMDS_VolumeIteratorPtr itVolumes=_meshDS->volumesIterator();
2273 while( itVolumes->more( ) ) {
2274 int nbNodes = itVolumes->next()->NbNodes();
2275 if ( myVolumesMap.find( nbNodes ) == myVolumesMap.end() )
2276 myVolumesMap[ nbNodes ] = 0;
2277 myVolumesMap[ nbNodes ] = myVolumesMap[ nbNodes ] + 1;
2279 save << clause << ".5) Volumes in detail: " << endl;
2280 std::map <int,int>::iterator itV;
2281 for (itV = myVolumesMap.begin(); itV != myVolumesMap.end(); itV++)
2282 save << "--> nb nodes: " << itV->first << " - nb elements:\t" << itV->second << endl;
2287 save << "===========================================================================" << endl;
2291 //=======================================================================
2292 //function : GetElementType
2293 //purpose : Returns type of mesh element with certain id
2294 //=======================================================================
2296 SMDSAbs_ElementType SMESH_Mesh::GetElementType( const smIdType id, const bool iselem )
2298 return _meshDS->GetElementType( id, iselem );
2301 //=============================================================================
2303 * \brief Convert group on geometry into standalone group
2305 //=============================================================================
2307 SMESH_Group* SMESH_Mesh::ConvertToStandalone ( int theGroupID )
2309 SMESH_Group* aGroup = 0;
2310 std::map < int, SMESH_Group * >::iterator itg = _mapGroup.find( theGroupID );
2311 if ( itg == _mapGroup.end() )
2314 SMESH_Group* anOldGrp = (*itg).second;
2315 if ( !anOldGrp || !anOldGrp->GetGroupDS() )
2317 SMESHDS_GroupBase* anOldGrpDS = anOldGrp->GetGroupDS();
2319 // create new standalone group
2320 aGroup = new SMESH_Group (theGroupID, this, anOldGrpDS->GetType(), anOldGrp->GetName() );
2321 _mapGroup[theGroupID] = aGroup;
2323 SMESHDS_Group* aNewGrpDS = dynamic_cast<SMESHDS_Group*>( aGroup->GetGroupDS() );
2324 GetMeshDS()->RemoveGroup( anOldGrpDS );
2325 GetMeshDS()->AddGroup( aNewGrpDS );
2327 // add elements (or nodes) into new created group
2328 SMDS_ElemIteratorPtr anItr = anOldGrpDS->GetElements();
2329 while ( anItr->more() )
2330 aNewGrpDS->Add( (anItr->next())->GetID() );
2333 aNewGrpDS->SetColor( anOldGrpDS->GetColor() );
2341 //=============================================================================
2343 * \brief remove submesh order from Mesh
2345 //=============================================================================
2347 void SMESH_Mesh::ClearMeshOrder()
2349 _subMeshOrder.clear();
2352 //=============================================================================
2354 * \brief remove submesh order from Mesh
2356 //=============================================================================
2358 void SMESH_Mesh::SetMeshOrder(const TListOfListOfInt& theOrder )
2360 _subMeshOrder = theOrder;
2363 //=============================================================================
2365 * \brief return submesh order if any
2367 //=============================================================================
2369 const TListOfListOfInt& SMESH_Mesh::GetMeshOrder() const
2371 return _subMeshOrder;
2374 //=============================================================================
2376 * \brief fill _mapAncestors
2378 //=============================================================================
2380 void SMESH_Mesh::fillAncestorsMap(const TopoDS_Shape& theShape)
2382 int desType, ancType;
2383 if ( !theShape.IsSame( GetShapeToMesh()) && theShape.ShapeType() == TopAbs_COMPOUND )
2385 // a geom group is added. Insert it into lists of ancestors before
2386 // the first ancestor more complex than group members
2387 TopoDS_Iterator subIt( theShape );
2388 if ( !subIt.More() ) return;
2389 int memberType = subIt.Value().ShapeType();
2390 for ( desType = TopAbs_VERTEX; desType >= memberType; desType-- )
2391 for (TopExp_Explorer des( theShape, TopAbs_ShapeEnum( desType )); des.More(); des.Next())
2393 if ( !_mapAncestors.Contains( des.Current() )) continue;// issue 0020982
2394 TopTools_ListOfShape& ancList = _mapAncestors.ChangeFromKey( des.Current() );
2395 TopTools_ListIteratorOfListOfShape ancIt (ancList);
2396 while ( ancIt.More() && ancIt.Value().ShapeType() >= memberType )
2398 if ( ancIt.More() ) ancList.InsertBefore( theShape, ancIt );
2399 else ancList.Append( theShape );
2402 else // else added for 52457: Addition of hypotheses is 8 time longer than meshing
2404 for ( desType = TopAbs_VERTEX; desType > TopAbs_COMPOUND; desType-- )
2405 for ( ancType = desType - 1; ancType >= TopAbs_COMPOUND; ancType-- )
2406 TopExp::MapShapesAndAncestors ( theShape,
2407 (TopAbs_ShapeEnum) desType,
2408 (TopAbs_ShapeEnum) ancType,
2411 // visit COMPOUNDs inside a COMPOUND that are not reachable by TopExp_Explorer
2412 if ( theShape.ShapeType() == TopAbs_COMPOUND )
2414 TopoDS_Iterator sIt(theShape);
2415 if ( sIt.More() && sIt.Value().ShapeType() == TopAbs_COMPOUND )
2416 for ( ; sIt.More(); sIt.Next() )
2417 if ( sIt.Value().ShapeType() == TopAbs_COMPOUND )
2418 fillAncestorsMap( sIt.Value() );
2422 //=============================================================================
2424 * \brief sort submeshes according to stored mesh order
2425 * \param theListToSort in out list to be sorted
2426 * \return FALSE if nothing sorted
2428 //=============================================================================
2430 bool SMESH_Mesh::SortByMeshOrder(std::vector<SMESH_subMesh*>& theListToSort) const
2432 if ( _subMeshOrder.empty() || theListToSort.size() < 2 )
2436 // collect all ordered sub-meshes in smVec as pointers
2437 // and get their positions within theListToSort
2439 std::vector<SMESH_subMesh*> smVec;
2440 typedef std::vector<SMESH_subMesh*>::iterator TPosInList;
2441 std::map< size_t, size_t > sortedPos; // index in theListToSort to order
2442 TPosInList smBeg = theListToSort.begin(), smEnd = theListToSort.end();
2443 TListOfListOfInt::const_iterator listIdsIt = _subMeshOrder.begin();
2444 bool needSort = false;
2445 for( ; listIdsIt != _subMeshOrder.end(); listIdsIt++)
2447 const TListOfInt& listOfId = *listIdsIt;
2448 // convert sm ids to sm's
2450 TListOfInt::const_iterator idIt = listOfId.begin();
2451 for ( ; idIt != listOfId.end(); idIt++ )
2453 if ( SMESH_subMesh * sm = GetSubMeshContaining( *idIt ))
2455 smVec.push_back( sm );
2456 if ( sm->GetSubMeshDS() && sm->GetSubMeshDS()->IsComplexSubmesh() )
2458 smVec.reserve( smVec.size() + sm->GetSubMeshDS()->NbSubMeshes() );
2459 SMESHDS_SubMeshIteratorPtr smdsIt = sm->GetSubMeshDS()->GetSubMeshIterator();
2460 while ( smdsIt->more() )
2462 const SMESHDS_SubMesh* smDS = smdsIt->next();
2463 if (( sm = GetSubMeshContaining( smDS->GetID() )))
2464 smVec.push_back( sm );
2469 // find smVec items in theListToSort
2470 for ( size_t i = 0; i < smVec.size(); ++i )
2472 TPosInList smPos = find( smBeg, smEnd, smVec[i] ); // position in theListToSort
2473 if ( smPos != smEnd )
2475 size_t posInList = std::distance( smBeg, smPos );
2476 size_t order = sortedPos.size();
2477 sortedPos.insert( std::make_pair( posInList, order ));
2478 if ( posInList != order )
2486 // set sm of sortedPos from theListToSort to front of orderedSM
2487 // and the rest of theListToSort to orderedSM end
2489 std::vector<SMESH_subMesh*> orderedSM;
2490 orderedSM.reserve( theListToSort.size() );
2491 orderedSM.resize( sortedPos.size() );
2494 sortedPos.insert( std::make_pair( theListToSort.size(), sortedPos.size() ));
2495 for ( const auto & pos_order : sortedPos )
2497 const size_t& posInList = pos_order.first;
2498 const size_t& order = pos_order.second;
2499 if ( order < sortedPos.size() - 1 )
2500 orderedSM[ order ] = theListToSort[ posInList ];
2502 if ( iPrev < posInList )
2503 orderedSM.insert( orderedSM.end(),
2504 theListToSort.begin() + iPrev,
2505 theListToSort.begin() + posInList );
2506 iPrev = posInList + 1;
2509 theListToSort.swap( orderedSM );
2514 //================================================================================
2516 * \brief Return true if given order of sub-meshes is OK
2518 //================================================================================
2520 bool SMESH_Mesh::IsOrderOK( const SMESH_subMesh* smBefore,
2521 const SMESH_subMesh* smAfter ) const
2523 TListOfListOfInt::const_iterator listIdsIt = _subMeshOrder.begin();
2524 for( ; listIdsIt != _subMeshOrder.end(); listIdsIt++)
2526 const TListOfInt& listOfId = *listIdsIt;
2527 int iB = -1, iA = -1, i = 0;
2528 for ( TListOfInt::const_iterator id = listOfId.begin(); id != listOfId.end(); ++id, ++i )
2530 if ( *id == smBefore->GetId() )
2536 else if ( *id == smAfter->GetId() )
2544 return true; // no order imposed to given sub-meshes
2547 //=============================================================================
2549 * \brief sort submeshes according to stored mesh order
2550 * \param theListToSort in out list to be sorted
2551 * \return FALSE if nothing sorted
2553 //=============================================================================
2555 void SMESH_Mesh::getAncestorsSubMeshes (const TopoDS_Shape& theSubShape,
2556 std::vector< SMESH_subMesh* >& theSubMeshes) const
2558 theSubMeshes.clear();
2559 TopTools_ListIteratorOfListOfShape it( GetAncestors( theSubShape ));
2560 for (; it.More(); it.Next() )
2561 if ( SMESH_subMesh* sm = GetSubMeshContaining( it.Value() ))
2562 theSubMeshes.push_back(sm);
2564 // sort submeshes according to stored mesh order
2565 SortByMeshOrder( theSubMeshes );
2569 //=============================================================================
2571 * \brief Build folder for parallel computation
2573 //=============================================================================
2574 void SMESH_Mesh::CreateTmpFolder()
2577 // Temporary folder that will be used by parallel computation
2578 tmp_folder = fs::temp_directory_path()/fs::unique_path(fs::path("SMESH_%%%%-%%%%"));
2579 fs::create_directories(tmp_folder);
2583 //=============================================================================
2585 * \brief Delete temporary folder used for parallel computation
2587 //=============================================================================
2588 void SMESH_Mesh::DeleteTmpFolder()
2591 fs::remove_all(tmp_folder);