1 // Copyright (C) 2007-2021 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>
83 #include <boost/filesystem.hpp>
84 namespace fs=boost::filesystem;
86 // maximum stored group name length in MED file
87 #define MAX_MED_GROUP_NAME_LENGTH 80
90 static int MYDEBUG = 0;
92 static int MYDEBUG = 0;
95 #define cSMESH_Hyp(h) static_cast<const SMESH_Hypothesis*>(h)
97 typedef SMESH_HypoFilter THypType;
99 class SMESH_Mesh::SubMeshHolder : public SMESHDS_TSubMeshHolder< SMESH_subMesh >
103 //=============================================================================
107 //=============================================================================
109 SMESH_Mesh::SMESH_Mesh(int theLocalId,
111 bool theIsEmbeddedMode,
112 SMESHDS_Document* theDocument):
113 _groupId( 0 ), _nbSubShapes( 0 )
115 if(MYDEBUG) MESSAGE("SMESH_Mesh::SMESH_Mesh(int localId)");
118 _document = theDocument;
119 _meshDS = theDocument->NewMesh(theIsEmbeddedMode,theLocalId);
120 _isShapeToMesh = false;
121 _isAutoColor = false;
123 _shapeDiagonal = 0.0;
125 _meshDS->ShapeToMesh( PseudoShape() );
126 _subMeshHolder = new SubMeshHolder;
127 // Temporary folder that will be used by parallel computation
128 tmp_folder = fs::temp_directory_path()/fs::unique_path(fs::path("SMESH_%%%%-%%%%"));
129 fs::create_directories(tmp_folder);
131 // assure unique persistent ID
132 if ( _document->NbMeshes() > 1 )
135 for ( _document->InitMeshesIterator(); _document->MoreMesh(); )
137 SMESHDS_Mesh * meshDS =_document->NextMesh();
138 if ( meshDS != _meshDS )
139 ids.insert( meshDS->GetPersistentId() );
142 if ( ids.count( _meshDS->GetPersistentId() ))
144 int uniqueID = *ids.rbegin() + 1;
145 _meshDS->SetPersistentId( uniqueID );
150 //================================================================================
152 * \brief Constructor of SMESH_Mesh being a base of some descendant class
154 //================================================================================
156 SMESH_Mesh::SMESH_Mesh():
160 _isShapeToMesh( false ),
164 _isAutoColor( false ),
165 _isModified( false ),
166 _shapeDiagonal( 0.0 ),
169 _subMeshHolder = new SubMeshHolder;
175 void deleteMeshDS(SMESHDS_Mesh* meshDS)
177 //cout << "deleteMeshDS( " << meshDS << endl;
181 static void* deleteMeshDS(void* meshDS)
183 //cout << "deleteMeshDS( " << meshDS << endl;
184 SMESHDS_Mesh* m = (SMESHDS_Mesh*)meshDS;
193 //=============================================================================
197 //=============================================================================
199 SMESH_Mesh::~SMESH_Mesh()
201 if(MYDEBUG) MESSAGE("SMESH_Mesh::~SMESH_Mesh");
203 if ( _document ) // avoid destructing _meshDS from ~SMESH_Gen()
204 _document->RemoveMesh( _id );
207 // remove self from studyContext
210 StudyContextStruct * studyContext = _gen->GetStudyContext();
211 studyContext->mapMesh.erase( _id );
214 _meshDS->ClearMesh();
216 // issue 0020340: EDF 1022 SMESH : Crash with FindNodeClosestTo in a second new study
217 // Notify event listeners at least that something happens
218 if ( SMESH_subMesh * sm = GetSubMeshContaining(1))
219 sm->ComputeStateEngine( SMESH_subMesh::MESH_ENTITY_REMOVED );
222 std::map < int, SMESH_Group * >::iterator itg;
223 for (itg = _mapGroup.begin(); itg != _mapGroup.end(); itg++) {
224 SMESH_Group *aGroup = (*itg).second;
230 delete _subMeshHolder;
232 if ( _callUp) delete _callUp;
236 // delete _meshDS, in a thread in order not to block closing a study with large meshes
238 boost::thread aThread(boost::bind( & deleteMeshDS, _meshDS ));
241 int result=pthread_create(&thread, NULL, deleteMeshDS, (void*)_meshDS);
246 //================================================================================
248 * \brief Return true if a mesh with given id exists
250 //================================================================================
252 bool SMESH_Mesh::MeshExists( int meshId ) const
254 return _document ? bool( _document->GetMesh( meshId )) : false;
257 //================================================================================
259 * \brief Return a mesh by id
261 //================================================================================
263 SMESH_Mesh* SMESH_Mesh::FindMesh( int meshId ) const
266 return (SMESH_Mesh*) this;
268 if ( StudyContextStruct *aStudyContext = _gen->GetStudyContext())
270 std::map < int, SMESH_Mesh * >::iterator i_m = aStudyContext->mapMesh.find( meshId );
271 if ( i_m != aStudyContext->mapMesh.end() )
277 //=============================================================================
279 * \brief Set geometry to be meshed
281 //=============================================================================
283 void SMESH_Mesh::ShapeToMesh(const TopoDS_Shape & aShape)
285 if(MYDEBUG) MESSAGE("SMESH_Mesh::ShapeToMesh");
287 if ( !aShape.IsNull() && _isShapeToMesh ) {
288 if ( aShape.ShapeType() != TopAbs_COMPOUND && // group contents is allowed to change
289 _meshDS->ShapeToMesh().ShapeType() != TopAbs_COMPOUND )
290 throw SALOME_Exception(LOCALIZED ("a shape to mesh has already been defined"));
292 // clear current data
293 if ( !_meshDS->ShapeToMesh().IsNull() )
295 // removal of a shape to mesh, delete objects referring to sub-shapes:
297 _subMeshHolder->DeleteAll();
298 // - groups on geometry
299 std::map <int, SMESH_Group *>::iterator i_gr = _mapGroup.begin();
300 while ( i_gr != _mapGroup.end() ) {
301 if ( dynamic_cast<SMESHDS_GroupOnGeom*>( i_gr->second->GetGroupDS() )) {
302 _meshDS->RemoveGroup( i_gr->second->GetGroupDS() );
304 _mapGroup.erase( i_gr++ );
309 _mapAncestors.Clear();
312 TopoDS_Shape aNullShape;
313 _meshDS->ShapeToMesh( aNullShape );
315 _shapeDiagonal = 0.0;
318 // set a new geometry
319 if ( !aShape.IsNull() )
321 _meshDS->ShapeToMesh(aShape);
322 _isShapeToMesh = true;
323 _nbSubShapes = _meshDS->MaxShapeIndex();
325 // fill map of ancestors
326 fillAncestorsMap(aShape);
330 _isShapeToMesh = false;
331 _shapeDiagonal = 0.0;
332 _meshDS->ShapeToMesh( PseudoShape() );
337 //=======================================================================
339 * \brief Return geometry to be meshed. (It may be a PseudoShape()!)
341 //=======================================================================
343 TopoDS_Shape SMESH_Mesh::GetShapeToMesh() const
345 return _meshDS->ShapeToMesh();
348 //=======================================================================
350 * \brief Return a solid which is returned by GetShapeToMesh() if
351 * a real geometry to be meshed was not set
353 //=======================================================================
355 const TopoDS_Solid& SMESH_Mesh::PseudoShape()
357 static TopoDS_Solid aSolid;
358 if ( aSolid.IsNull() )
360 aSolid = BRepPrimAPI_MakeBox(1,1,1);
365 //=======================================================================
367 * \brief Return diagonal size of bounding box of a shape
369 //=======================================================================
371 double SMESH_Mesh::GetShapeDiagonalSize(const TopoDS_Shape & aShape)
373 if ( !aShape.IsNull() ) {
375 // avoid too long waiting on large shapes. PreciseBoundingBox() was added
376 // to assure same result which else depends on presence of triangulation (IPAL52557).
377 const int maxNbFaces = 4000;
379 for ( TopExp_Explorer f( aShape, TopAbs_FACE ); f.More() && nbFaces < maxNbFaces; f.Next() )
381 bool isPrecise = false;
382 if ( nbFaces < maxNbFaces )
385 GEOMUtils::PreciseBoundingBox( aShape, Box );
393 BRepBndLib::Add( aShape, Box );
396 return sqrt( Box.SquareExtent() );
401 //=======================================================================
403 * \brief Return diagonal size of bounding box of shape to mesh
405 //=======================================================================
407 double SMESH_Mesh::GetShapeDiagonalSize() const
409 if ( _shapeDiagonal == 0. && _isShapeToMesh )
410 const_cast<SMESH_Mesh*>(this)->_shapeDiagonal = GetShapeDiagonalSize( GetShapeToMesh() );
412 return _shapeDiagonal;
415 //================================================================================
417 * \brief Load mesh from study file
419 //================================================================================
421 void SMESH_Mesh::Load()
427 //=======================================================================
429 * \brief Remove all nodes and elements
431 //=======================================================================
433 void SMESH_Mesh::Clear()
435 if ( HasShapeToMesh() ) // remove all nodes and elements
438 _meshDS->ClearMesh();
440 // update compute state of submeshes
441 if ( SMESH_subMesh *sm = GetSubMeshContaining( GetShapeToMesh() ) )
443 sm->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
444 sm->ComputeSubMeshStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
445 sm->ComputeStateEngine( SMESH_subMesh::CLEAN ); // for event listeners (issue 0020918)
446 sm->ComputeSubMeshStateEngine( SMESH_subMesh::CLEAN );
449 else // remove only nodes/elements computed by algorithms
451 if ( SMESH_subMesh *sm = GetSubMeshContaining( GetShapeToMesh() ) )
453 sm->ComputeStateEngine( SMESH_subMesh::CLEAN );
454 sm->ComputeSubMeshStateEngine( SMESH_subMesh::CLEAN );
455 sm->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
456 sm->ComputeSubMeshStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
459 GetMeshDS()->Modified();
463 //=======================================================================
465 * \brief Remove all nodes and elements of indicated shape
467 //=======================================================================
469 void SMESH_Mesh::ClearSubMesh(const int theShapeId)
471 // clear sub-meshes; get ready to re-compute as a side-effect
472 if ( SMESH_subMesh *sm = GetSubMeshContaining( theShapeId ) )
474 SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(/*includeSelf=*/true,
475 /*complexShapeFirst=*/false);
476 while ( smIt->more() )
479 TopAbs_ShapeEnum shapeType = sm->GetSubShape().ShapeType();
480 if ( shapeType == TopAbs_VERTEX || shapeType < TopAbs_SOLID )
481 // all other shapes depends on vertices so they are already cleaned
482 sm->ComputeStateEngine( SMESH_subMesh::CLEAN );
483 // to recompute even if failed
484 sm->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
489 //=======================================================================
490 //function : UNVToMesh
492 //=======================================================================
494 int SMESH_Mesh::UNVToMesh(const char* theFileName)
496 if ( _isShapeToMesh )
497 throw SALOME_Exception(LOCALIZED("a shape to mesh has already been defined"));
498 _isShapeToMesh = false;
500 DriverUNV_R_SMDS_Mesh myReader;
501 myReader.SetMesh(_meshDS);
502 myReader.SetFile(theFileName);
503 myReader.SetMeshId(-1);
506 TGroupNamesMap& aGroupNames = myReader.GetGroupNamesMap();
507 TGroupNamesMap::iterator gr2names;
508 int anId = 1 + ( _mapGroup.empty() ? 0 : _mapGroup.rbegin()->first );
509 for ( gr2names = aGroupNames.begin(); gr2names != aGroupNames.end(); ++gr2names )
511 SMDS_MeshGroup* aGroup = gr2names->first;
512 const std::string& aName = gr2names->second;
513 SMESHDS_Group* aGroupDS = new SMESHDS_Group( anId++, _meshDS, aGroup->GetType() );
514 aGroupDS->SMDSGroup() = std::move( *aGroup );
515 aGroupDS->SetStoreName( aName.c_str() );
516 AddGroup( aGroupDS );
522 //=======================================================================
523 //function : MEDToMesh
525 //=======================================================================
527 int SMESH_Mesh::MEDToMesh(const char* theFileName, const char* theMeshName)
529 if ( _isShapeToMesh )
530 throw SALOME_Exception(LOCALIZED("a shape to mesh has already been defined"));
531 _isShapeToMesh = false;
533 DriverMED_R_SMESHDS_Mesh myReader;
534 myReader.SetMesh(_meshDS);
535 myReader.SetMeshId(-1);
536 myReader.SetFile(theFileName);
537 myReader.SetMeshName(theMeshName);
538 Driver_Mesh::Status status = myReader.Perform();
540 SMESH_ComputeErrorPtr er = myReader.GetError();
541 if ( er && !er->IsOK() ) std::cout << er->myComment << std::endl;
544 // Reading groups (sub-meshes are out of scope of MED import functionality)
545 std::list<TNameAndType> aGroupNames = myReader.GetGroupNamesAndTypes();
546 std::list<TNameAndType>::iterator name_type = aGroupNames.begin();
547 for ( ; name_type != aGroupNames.end(); name_type++ )
549 SMESH_Group* aGroup = AddGroup( name_type->second, name_type->first.c_str() );
551 SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>( aGroup->GetGroupDS() );
553 aGroupDS->SetStoreName( name_type->first.c_str() );
554 myReader.GetGroup( aGroupDS );
560 _meshDS->CompactMesh();
565 //=======================================================================
566 //function : STLToMesh
568 //=======================================================================
570 std::string SMESH_Mesh::STLToMesh(const char* theFileName)
573 throw SALOME_Exception(LOCALIZED("a shape to mesh has already been defined"));
574 _isShapeToMesh = false;
576 DriverSTL_R_SMDS_Mesh myReader;
577 myReader.SetMesh(_meshDS);
578 myReader.SetFile(theFileName);
579 myReader.SetMeshId(-1);
582 return myReader.GetName();
585 //================================================================================
587 * \brief Reads the given mesh from the CGNS file
588 * \param theFileName - name of the file
589 * \retval int - Driver_Mesh::Status
591 //================================================================================
593 int SMESH_Mesh::CGNSToMesh(const char* theFileName,
594 const int theMeshIndex,
595 std::string& theMeshName)
597 int res = Driver_Mesh::DRS_FAIL;
600 DriverCGNS_Read myReader;
601 myReader.SetMesh(_meshDS);
602 myReader.SetFile(theFileName);
603 myReader.SetMeshId(theMeshIndex);
604 res = myReader.Perform();
605 theMeshName = myReader.GetMeshName();
614 //================================================================================
616 * \brief Fill its data by reading a GMF file
618 //================================================================================
620 SMESH_ComputeErrorPtr SMESH_Mesh::GMFToMesh(const char* theFileName,
621 bool theMakeRequiredGroups)
623 DriverGMF_Read myReader;
624 myReader.SetMesh(_meshDS);
625 myReader.SetFile(theFileName);
626 myReader.SetMakeRequiredGroups( theMakeRequiredGroups );
628 //theMeshName = myReader.GetMeshName();
633 return myReader.GetError();
636 //=============================================================================
640 //=============================================================================
642 SMESH_Hypothesis::Hypothesis_Status
643 SMESH_Mesh::AddHypothesis(const TopoDS_Shape & aSubShape,
645 std::string* anError )
647 if(MYDEBUG) MESSAGE("SMESH_Mesh::AddHypothesis");
652 SMESH_subMesh *subMesh = GetSubMesh(aSubShape);
653 if ( !subMesh || !subMesh->GetId())
654 return SMESH_Hypothesis::HYP_BAD_SUBSHAPE;
656 SMESH_Hypothesis *anHyp = GetHypothesis( anHypId );
658 throw SALOME_Exception(LOCALIZED("hypothesis does not exist"));
660 bool isGlobalHyp = IsMainShape( aSubShape );
662 // NotConformAllowed can be only global
665 // NOTE: this is not a correct way to check a name of hypothesis,
666 // there should be an attribute of hypothesis saying that it can/can't
668 std::string hypName = anHyp->GetName();
669 if ( hypName == "NotConformAllowed" )
671 if(MYDEBUG) MESSAGE( "Hypothesis <NotConformAllowed> can be only global" );
672 return SMESH_Hypothesis::HYP_INCOMPATIBLE;
678 bool isAlgo = ( anHyp->GetType() != SMESHDS_Hypothesis::PARAM_ALGO );
679 SMESH_subMesh::algo_event event = isAlgo ? SMESH_subMesh::ADD_ALGO : SMESH_subMesh::ADD_HYP;
681 SMESH_Hypothesis::Hypothesis_Status ret = subMesh->AlgoStateEngine(event, anHyp);
683 if ( anError && SMESH_Hypothesis::IsStatusFatal(ret) && subMesh->GetComputeError() )
684 *anError = subMesh->GetComputeError()->myComment;
687 if ( !SMESH_Hypothesis::IsStatusFatal(ret) &&
688 anHyp->GetDim() <= SMESH_Gen::GetShapeDim(aSubShape)) // is added on father
690 event = isAlgo ? SMESH_subMesh::ADD_FATHER_ALGO : SMESH_subMesh::ADD_FATHER_HYP;
692 SMESH_Hypothesis::Hypothesis_Status ret2 =
693 subMesh->SubMeshesAlgoStateEngine(event, anHyp, /*exitOnFatal=*/true);
697 if ( SMESH_Hypothesis::IsStatusFatal( ret ))
699 if ( anError && subMesh->GetComputeError() )
700 *anError = subMesh->GetComputeError()->myComment;
702 event = isAlgo ? SMESH_subMesh::REMOVE_ALGO : SMESH_subMesh::REMOVE_HYP;
703 subMesh->AlgoStateEngine(event, anHyp);
707 // check concurrent hypotheses on ancestors
708 if (ret < SMESH_Hypothesis::HYP_CONCURRENT && !isGlobalHyp )
710 SMESH_subMeshIteratorPtr smIt = subMesh->getDependsOnIterator(false,false);
711 while ( smIt->more() ) {
712 SMESH_subMesh* sm = smIt->next();
713 if ( sm->IsApplicableHypothesis( anHyp )) {
714 ret2 = sm->CheckConcurrentHypothesis( anHyp->GetType() );
723 HasModificationsToDiscard(); // to reset _isModified flag if a mesh becomes empty
724 GetMeshDS()->Modified();
726 if(MYDEBUG) subMesh->DumpAlgoState(true);
727 if(MYDEBUG) SCRUTE(ret);
731 //=============================================================================
735 //=============================================================================
737 SMESH_Hypothesis::Hypothesis_Status
738 SMESH_Mesh::RemoveHypothesis(const TopoDS_Shape & aSubShape,
741 if(MYDEBUG) MESSAGE("SMESH_Mesh::RemoveHypothesis");
743 StudyContextStruct *sc = _gen->GetStudyContext();
744 if (sc->mapHypothesis.find(anHypId) == sc->mapHypothesis.end())
745 throw SALOME_Exception(LOCALIZED("hypothesis does not exist"));
747 SMESH_Hypothesis *anHyp = sc->mapHypothesis[anHypId];
748 if(MYDEBUG) { SCRUTE(anHyp->GetType()); }
752 bool isAlgo = ( !anHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO );
753 SMESH_subMesh::algo_event event = isAlgo ? SMESH_subMesh::REMOVE_ALGO : SMESH_subMesh::REMOVE_HYP;
755 SMESH_subMesh *subMesh = GetSubMesh(aSubShape);
757 SMESH_Hypothesis::Hypothesis_Status ret = subMesh->AlgoStateEngine(event, anHyp);
759 // there may appear concurrent hyps that were covered by the removed hyp
760 if (ret < SMESH_Hypothesis::HYP_CONCURRENT &&
761 subMesh->IsApplicableHypothesis( anHyp ) &&
762 subMesh->CheckConcurrentHypothesis( anHyp->GetType() ) != SMESH_Hypothesis::HYP_OK)
763 ret = SMESH_Hypothesis::HYP_CONCURRENT;
766 if (!SMESH_Hypothesis::IsStatusFatal(ret) &&
767 anHyp->GetDim() <= SMESH_Gen::GetShapeDim(aSubShape)) // is removed from father
769 event = isAlgo ? SMESH_subMesh::REMOVE_FATHER_ALGO : SMESH_subMesh::REMOVE_FATHER_HYP;
771 SMESH_Hypothesis::Hypothesis_Status ret2 =
772 subMesh->SubMeshesAlgoStateEngine(event, anHyp);
773 if (ret2 > ret) // more severe
776 // check concurrent hypotheses on ancestors
777 if (ret < SMESH_Hypothesis::HYP_CONCURRENT && !IsMainShape( aSubShape ) )
779 SMESH_subMeshIteratorPtr smIt = subMesh->getDependsOnIterator(false,false);
780 while ( smIt->more() ) {
781 SMESH_subMesh* sm = smIt->next();
782 if ( sm->IsApplicableHypothesis( anHyp )) {
783 ret2 = sm->CheckConcurrentHypothesis( anHyp->GetType() );
793 HasModificationsToDiscard(); // to reset _isModified flag if mesh become empty
794 GetMeshDS()->Modified();
796 if(MYDEBUG) subMesh->DumpAlgoState(true);
797 if(MYDEBUG) SCRUTE(ret);
801 //=============================================================================
805 //=============================================================================
807 const std::list<const SMESHDS_Hypothesis*>&
808 SMESH_Mesh::GetHypothesisList(const TopoDS_Shape & aSubShape) const
810 return _meshDS->GetHypothesis(aSubShape);
813 //=======================================================================
815 * \brief Return the hypothesis assigned to the shape
816 * \param aSubShape - the shape to check
817 * \param aFilter - the hypothesis filter
818 * \param andAncestors - flag to check hypos assigned to ancestors of the shape
819 * \param assignedTo - to return the shape the found hypo is assigned to
820 * \retval SMESH_Hypothesis* - the first hypo passed through aFilter
822 //=======================================================================
824 const SMESH_Hypothesis * SMESH_Mesh::GetHypothesis(const TopoDS_Shape & aSubShape,
825 const SMESH_HypoFilter& aFilter,
826 const bool andAncestors,
827 TopoDS_Shape* assignedTo) const
829 return GetHypothesis( const_cast< SMESH_Mesh* >(this)->GetSubMesh( aSubShape ),
830 aFilter, andAncestors, assignedTo );
833 //=======================================================================
835 * \brief Return the hypothesis assigned to the shape of a sub-mesh
836 * \param aSubMesh - the sub-mesh to check
837 * \param aFilter - the hypothesis filter
838 * \param andAncestors - flag to check hypos assigned to ancestors of the shape
839 * \param assignedTo - to return the shape the found hypo is assigned to
840 * \retval SMESH_Hypothesis* - the first hypo passed through aFilter
842 //=======================================================================
844 const SMESH_Hypothesis * SMESH_Mesh::GetHypothesis(const SMESH_subMesh * aSubMesh,
845 const SMESH_HypoFilter& aFilter,
846 const bool andAncestors,
847 TopoDS_Shape* assignedTo) const
849 if ( !aSubMesh ) return 0;
852 const TopoDS_Shape & aSubShape = aSubMesh->GetSubShape();
853 const std::list<const SMESHDS_Hypothesis*>& hypList = _meshDS->GetHypothesis(aSubShape);
854 std::list<const SMESHDS_Hypothesis*>::const_iterator hyp = hypList.begin();
855 for ( ; hyp != hypList.end(); hyp++ ) {
856 const SMESH_Hypothesis * h = cSMESH_Hyp( *hyp );
857 if ( aFilter.IsOk( h, aSubShape)) {
858 if ( assignedTo ) *assignedTo = aSubShape;
865 // user sorted submeshes of ancestors, according to stored submesh priority
866 std::vector< SMESH_subMesh * > & ancestors =
867 const_cast< std::vector< SMESH_subMesh * > & > ( aSubMesh->GetAncestors() );
868 SortByMeshOrder( ancestors );
870 std::vector<SMESH_subMesh*>::const_iterator smIt = ancestors.begin();
871 for ( ; smIt != ancestors.end(); smIt++ )
873 const TopoDS_Shape& curSh = (*smIt)->GetSubShape();
874 const std::list<const SMESHDS_Hypothesis*>& hypList = _meshDS->GetHypothesis(curSh);
875 std::list<const SMESHDS_Hypothesis*>::const_iterator hyp = hypList.begin();
876 for ( ; hyp != hypList.end(); hyp++ ) {
877 const SMESH_Hypothesis * h = cSMESH_Hyp( *hyp );
878 if (aFilter.IsOk( h, curSh )) {
879 if ( assignedTo ) *assignedTo = curSh;
888 //================================================================================
890 * \brief Return hypotheses assigned to the shape
891 * \param aSubShape - the shape to check
892 * \param aFilter - the hypothesis filter
893 * \param aHypList - the list of the found hypotheses
894 * \param andAncestors - flag to check hypos assigned to ancestors of the shape
895 * \retval int - number of unique hypos in aHypList
897 //================================================================================
899 int SMESH_Mesh::GetHypotheses(const TopoDS_Shape & aSubShape,
900 const SMESH_HypoFilter& aFilter,
901 std::list <const SMESHDS_Hypothesis * >& aHypList,
902 const bool andAncestors,
903 std::list< TopoDS_Shape > * assignedTo/*=0*/) const
905 return GetHypotheses( const_cast< SMESH_Mesh* >(this)->GetSubMesh( aSubShape ),
906 aFilter, aHypList, andAncestors, assignedTo );
909 //================================================================================
911 * \brief Return hypotheses assigned to the shape of a sub-mesh
912 * \param aSubShape - the sub-mesh to check
913 * \param aFilter - the hypothesis filter
914 * \param aHypList - the list of the found hypotheses
915 * \param andAncestors - flag to check hypos assigned to ancestors of the shape
916 * \retval int - number of unique hypos in aHypList
918 //================================================================================
920 int SMESH_Mesh::GetHypotheses(const SMESH_subMesh * aSubMesh,
921 const SMESH_HypoFilter& aFilter,
922 std::list <const SMESHDS_Hypothesis * >& aHypList,
923 const bool andAncestors,
924 std::list< TopoDS_Shape > * assignedTo/*=0*/) const
926 if ( !aSubMesh ) return 0;
928 std::set< std::string > hypTypes; // to exclude same type hypos from the result list
931 // only one main hypothesis is allowed
932 bool mainHypFound = false;
935 std::list<const SMESHDS_Hypothesis*>::const_iterator hyp;
936 for ( hyp = aHypList.begin(); hyp != aHypList.end(); hyp++ ) {
937 if ( hypTypes.insert( (*hyp)->GetName() ).second )
939 if ( !cSMESH_Hyp(*hyp)->IsAuxiliary() )
943 // get hypos from aSubShape
945 const TopoDS_Shape & aSubShape = aSubMesh->GetSubShape();
946 const std::list<const SMESHDS_Hypothesis*>& hypList = _meshDS->GetHypothesis(aSubShape);
947 for ( hyp = hypList.begin(); hyp != hypList.end(); hyp++ )
949 const SMESH_Hypothesis* h = cSMESH_Hyp( *hyp );
950 if (( aFilter.IsOk( h, aSubShape )) &&
951 ( h->IsAuxiliary() || !mainHypFound ) &&
952 ( h->IsAuxiliary() || hypTypes.insert( h->GetName() ).second ))
954 aHypList.push_back( *hyp );
956 if ( !h->IsAuxiliary() )
958 if ( assignedTo ) assignedTo->push_back( aSubShape );
963 // get hypos from ancestors of aSubShape
966 // user sorted submeshes of ancestors, according to stored submesh priority
967 std::vector< SMESH_subMesh * > & ancestors =
968 const_cast< std::vector< SMESH_subMesh * > & > ( aSubMesh->GetAncestors() );
969 SortByMeshOrder( ancestors );
971 std::vector<SMESH_subMesh*>::const_iterator smIt = ancestors.begin();
972 for ( ; smIt != ancestors.end(); smIt++ )
974 const TopoDS_Shape& curSh = (*smIt)->GetSubShape();
975 const std::list<const SMESHDS_Hypothesis*>& hypList = _meshDS->GetHypothesis(curSh);
976 for ( hyp = hypList.begin(); hyp != hypList.end(); hyp++ )
978 const SMESH_Hypothesis* h = cSMESH_Hyp( *hyp );
979 if (( aFilter.IsOk( h, curSh )) &&
980 ( h->IsAuxiliary() || !mainHypFound ) &&
981 ( h->IsAuxiliary() || hypTypes.insert( h->GetName() ).second ))
983 aHypList.push_back( *hyp );
985 if ( !h->IsAuxiliary() )
987 if ( assignedTo ) assignedTo->push_back( curSh );
995 //================================================================================
997 * \brief Return a hypothesis by its ID
999 //================================================================================
1001 SMESH_Hypothesis * SMESH_Mesh::GetHypothesis(const int anHypId) const
1003 StudyContextStruct *sc = _gen->GetStudyContext();
1004 if (sc->mapHypothesis.find(anHypId) == sc->mapHypothesis.end())
1007 SMESH_Hypothesis *anHyp = sc->mapHypothesis[anHypId];
1011 //=============================================================================
1015 //=============================================================================
1017 const std::list<SMESHDS_Command*> & SMESH_Mesh::GetLog()
1019 return _meshDS->GetScript()->GetCommands();
1022 //=============================================================================
1026 //=============================================================================
1027 void SMESH_Mesh::ClearLog()
1029 _meshDS->GetScript()->Clear();
1032 //=============================================================================
1034 * Get or Create the SMESH_subMesh object implementation
1036 //=============================================================================
1038 SMESH_subMesh * SMESH_Mesh::GetSubMesh(const TopoDS_Shape & aSubShape)
1040 int index = _meshDS->ShapeToIndex(aSubShape);
1041 if ( !index && aSubShape.IsNull() )
1044 // for submeshes on GEOM Group
1045 if (( !index || index > _nbSubShapes ) && aSubShape.ShapeType() == TopAbs_COMPOUND )
1047 TopoDS_Iterator it( aSubShape );
1050 index = _meshDS->AddCompoundSubmesh( aSubShape, it.Value().ShapeType() );
1051 // fill map of Ancestors
1052 while ( _nbSubShapes < index )
1053 fillAncestorsMap( _meshDS->IndexToShape( ++_nbSubShapes ));
1057 // return NULL; // neither sub-shape nor a group
1059 SMESH_subMesh* aSubMesh = _subMeshHolder->Get( index );
1062 aSubMesh = new SMESH_subMesh(index, this, _meshDS, aSubShape);
1063 _subMeshHolder->Add( index, aSubMesh );
1065 // include non-computable sub-meshes in SMESH_subMesh::_ancestors of sub-submeshes
1066 switch ( aSubShape.ShapeType() ) {
1067 case TopAbs_COMPOUND:
1070 for ( TopoDS_Iterator subIt( aSubShape ); subIt.More(); subIt.Next() )
1072 SMESH_subMesh* sm = GetSubMesh( subIt.Value() );
1073 SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(/*inclideSelf=*/true);
1074 while ( smIt->more() )
1075 smIt->next()->ClearAncestors();
1083 //=============================================================================
1085 * Get the SMESH_subMesh object implementation. Don't create it, return null
1086 * if it does not exist.
1088 //=============================================================================
1090 SMESH_subMesh *SMESH_Mesh::GetSubMeshContaining(const TopoDS_Shape & aSubShape) const
1092 int index = _meshDS->ShapeToIndex(aSubShape);
1093 return GetSubMeshContaining( index );
1096 //=============================================================================
1098 * Get the SMESH_subMesh object implementation. Don't create it, return null
1099 * if it does not exist.
1101 //=============================================================================
1103 SMESH_subMesh *SMESH_Mesh::GetSubMeshContaining(const int aShapeID) const
1105 SMESH_subMesh *aSubMesh = _subMeshHolder->Get( aShapeID );
1109 //================================================================================
1111 * \brief Return sub-meshes of groups containing the given sub-shape
1113 //================================================================================
1115 std::list<SMESH_subMesh*>
1116 SMESH_Mesh::GetGroupSubMeshesContaining(const TopoDS_Shape & aSubShape) const
1118 std::list<SMESH_subMesh*> found;
1120 SMESH_subMesh * subMesh = GetSubMeshContaining(aSubShape);
1124 // sub-meshes of groups have max IDs, so search from the map end
1125 SMESH_subMeshIteratorPtr smIt( _subMeshHolder->GetIterator( /*reverse=*/true ) );
1126 while ( smIt->more() ) {
1127 SMESH_subMesh* sm = smIt->next();
1128 SMESHDS_SubMesh * ds = sm->GetSubMeshDS();
1129 if ( ds && ds->IsComplexSubmesh() ) {
1130 if ( SMESH_MesherHelper::IsSubShape( aSubShape, sm->GetSubShape() ))
1132 found.push_back( sm );
1136 break; // the rest sub-meshes are not those of groups
1140 if ( found.empty() ) // maybe the main shape is a COMPOUND (issue 0021530)
1142 if ( SMESH_subMesh * mainSM = GetSubMeshContaining(1) )
1143 if ( mainSM->GetSubShape().ShapeType() == TopAbs_COMPOUND )
1145 TopoDS_Iterator it( mainSM->GetSubShape() );
1146 if ( it.Value().ShapeType() == aSubShape.ShapeType() &&
1147 SMESH_MesherHelper::IsSubShape( aSubShape, mainSM->GetSubShape() ))
1148 found.push_back( mainSM );
1151 else // issue 0023068
1153 if ( SMESH_subMesh * mainSM = GetSubMeshContaining(1) )
1154 if ( mainSM->GetSubShape().ShapeType() == TopAbs_COMPOUND )
1155 found.push_back( mainSM );
1159 //=======================================================================
1160 //function : IsUsedHypothesis
1161 //purpose : Return True if anHyp is used to mesh aSubShape
1162 //=======================================================================
1164 bool SMESH_Mesh::IsUsedHypothesis(SMESHDS_Hypothesis * anHyp,
1165 const SMESH_subMesh* aSubMesh)
1167 SMESH_Hypothesis* hyp = static_cast<SMESH_Hypothesis*>(anHyp);
1169 // check if anHyp can be used to mesh aSubMesh
1170 if ( !aSubMesh || !aSubMesh->IsApplicableHypothesis( hyp ))
1173 SMESH_Algo *algo = aSubMesh->GetAlgo();
1176 if (anHyp->GetType() > SMESHDS_Hypothesis::PARAM_ALGO)
1177 return ( anHyp == algo );
1179 // algorithm parameter
1182 // look through hypotheses used by algo
1183 const SMESH_HypoFilter* hypoKind;
1184 if (( hypoKind = algo->GetCompatibleHypoFilter( !hyp->IsAuxiliary() ))) {
1185 std::list <const SMESHDS_Hypothesis * > usedHyps;
1186 if ( GetHypotheses( aSubMesh, *hypoKind, usedHyps, true ))
1187 return ( find( usedHyps.begin(), usedHyps.end(), anHyp ) != usedHyps.end() );
1194 //=======================================================================
1195 //function : NotifySubMeshesHypothesisModification
1196 //purpose : Say all submeshes using theChangedHyp that it has been modified
1197 //=======================================================================
1199 void SMESH_Mesh::NotifySubMeshesHypothesisModification(const SMESH_Hypothesis* hyp)
1202 if ( !GetMeshDS()->IsUsedHypothesis( hyp ))
1205 smIdType nbEntities = ( _meshDS->NbNodes() + _meshDS->NbElements() );
1206 if ( hyp && _callUp && !_callUp->IsLoaded() ) // for not loaded mesh (#16648)
1208 _callUp->HypothesisModified( hyp->GetID(), /*updateIcons=*/true );
1209 nbEntities = ( _meshDS->NbNodes() + _meshDS->NbElements() ); // after loading mesh
1213 const SMESH_HypoFilter* compatibleHypoKind;
1214 std::list <const SMESHDS_Hypothesis * > usedHyps;
1215 std::vector< SMESH_subMesh* > smToNotify;
1216 bool allMeshedEdgesNotified = true;
1218 SMESH_subMeshIteratorPtr smIt( _subMeshHolder->GetIterator() );
1219 while ( smIt->more() )
1221 SMESH_subMesh* aSubMesh = smIt->next();
1222 bool toNotify = false;
1224 // if aSubMesh meshing depends on hyp,
1225 // we call aSubMesh->AlgoStateEngine( MODIF_HYP, hyp ) that causes either
1226 // 1) clearing already computed aSubMesh or
1227 // 2) changing algo_state from MISSING_HYP to HYP_OK when parameters of hyp becomes valid,
1228 // other possible changes are not interesting. (IPAL0052457 - assigning hyp performance pb)
1229 if ( aSubMesh->GetComputeState() == SMESH_subMesh::COMPUTE_OK ||
1230 aSubMesh->GetComputeState() == SMESH_subMesh::FAILED_TO_COMPUTE ||
1231 aSubMesh->GetAlgoState() == SMESH_subMesh::MISSING_HYP ||
1232 hyp->DataDependOnParams() )
1234 const TopoDS_Shape & aSubShape = aSubMesh->GetSubShape();
1236 if (( aSubMesh->IsApplicableHypothesis( hyp )) &&
1237 ( algo = aSubMesh->GetAlgo() ) &&
1238 ( compatibleHypoKind = algo->GetCompatibleHypoFilter( !hyp->IsAuxiliary() )) &&
1239 ( compatibleHypoKind->IsOk( hyp, aSubShape )))
1241 // check if hyp is used by algo
1243 toNotify = ( GetHypotheses( aSubMesh, *compatibleHypoKind, usedHyps, true ) &&
1244 std::find( usedHyps.begin(), usedHyps.end(), hyp ) != usedHyps.end() );
1249 smToNotify.push_back( aSubMesh );
1250 if ( aSubMesh->GetAlgoState() == SMESH_subMesh::MISSING_HYP )
1251 allMeshedEdgesNotified = false; // update of algo state needed, not mesh clearing
1255 if ( !aSubMesh->IsEmpty() &&
1256 aSubMesh->GetSubShape().ShapeType() == TopAbs_EDGE )
1257 allMeshedEdgesNotified = false;
1260 if ( smToNotify.empty() )
1263 // if all meshed EDGEs will be notified then the notification is equivalent
1264 // to the whole mesh clearing, which is usually faster
1265 if ( allMeshedEdgesNotified && NbNodes() > 0 )
1271 // notify in reverse order to avoid filling the pool of IDs
1272 for ( int i = smToNotify.size()-1; i >= 0; --i )
1273 smToNotify[i]->AlgoStateEngine(SMESH_subMesh::MODIF_HYP,
1274 const_cast< SMESH_Hypothesis*>( hyp ));
1276 HasModificationsToDiscard(); // to reset _isModified flag if mesh becomes empty
1277 GetMeshDS()->Modified();
1279 smIdType newNbEntities = ( _meshDS->NbNodes() + _meshDS->NbElements() );
1280 if ( hyp && _callUp )
1281 _callUp->HypothesisModified( hyp->GetID(), newNbEntities != nbEntities );
1284 //=============================================================================
1286 * Auto color functionality
1288 //=============================================================================
1289 void SMESH_Mesh::SetAutoColor(bool theAutoColor)
1291 _isAutoColor = theAutoColor;
1294 bool SMESH_Mesh::GetAutoColor()
1296 return _isAutoColor;
1299 //=======================================================================
1300 //function : SetIsModified
1301 //purpose : Set the flag meaning that the mesh has been edited "manually"
1302 //=======================================================================
1304 void SMESH_Mesh::SetIsModified(bool isModified)
1306 _isModified = isModified;
1309 // check if mesh becomes empty as result of modification
1310 HasModificationsToDiscard();
1313 //=======================================================================
1314 //function : HasModificationsToDiscard
1315 //purpose : Return true if the mesh has been edited since a total re-compute
1316 // and those modifications may prevent successful partial re-compute.
1317 // As a side effect reset _isModified flag if mesh is empty
1319 //=======================================================================
1321 bool SMESH_Mesh::HasModificationsToDiscard() const
1323 if ( ! _isModified )
1326 // return true if the next Compute() will be partial and
1327 // existing but changed elements may prevent successful re-compute
1328 bool hasComputed = false, hasNotComputed = false;
1329 SMESH_subMeshIteratorPtr smIt( _subMeshHolder->GetIterator() );
1330 while ( smIt->more() )
1332 const SMESH_subMesh* aSubMesh = smIt->next();
1333 switch ( aSubMesh->GetSubShape().ShapeType() )
1338 if ( aSubMesh->IsMeshComputed() )
1341 hasNotComputed = true;
1342 if ( hasComputed && hasNotComputed)
1348 if ( NbNodes() < 1 )
1349 const_cast<SMESH_Mesh*>(this)->_isModified = false;
1354 //=============================================================================
1356 * \brief Return true if all sub-meshes are computed OK - to update an icon
1358 //=============================================================================
1360 bool SMESH_Mesh::IsComputedOK()
1362 if ( NbNodes() == 0 )
1365 // if ( !HasShapeToMesh() )
1368 if ( SMESH_subMesh* mainSM = GetSubMeshContaining( 1 ))
1370 SMESH_subMeshIteratorPtr smIt = mainSM->getDependsOnIterator(/*includeSelf=*/true);
1371 while ( smIt->more() )
1373 const SMESH_subMesh* sm = smIt->next();
1374 if ( !sm->IsAlwaysComputed() )
1375 switch ( sm->GetComputeState() )
1377 case SMESH_subMesh::NOT_READY:
1378 case SMESH_subMesh::COMPUTE_OK:
1380 case SMESH_subMesh::FAILED_TO_COMPUTE:
1381 case SMESH_subMesh::READY_TO_COMPUTE:
1389 //================================================================================
1391 * \brief Check if any groups of the same type have equal names
1393 //================================================================================
1395 bool SMESH_Mesh::HasDuplicatedGroupNamesMED()
1397 // Corrected for Mantis issue 0020028
1398 std::map< SMDSAbs_ElementType, std::set< std::string > > aGroupNames;
1399 for ( std::map<int, SMESH_Group*>::iterator it = _mapGroup.begin(); it != _mapGroup.end(); it++ )
1401 SMESH_Group* aGroup = it->second;
1402 SMDSAbs_ElementType aType = aGroup->GetGroupDS()->GetType();
1403 std::string aGroupName = aGroup->GetName();
1404 aGroupName.resize( MAX_MED_GROUP_NAME_LENGTH );
1405 if ( !aGroupNames[aType].insert(aGroupName).second )
1412 //================================================================================
1414 * \brief Export the mesh to a writer
1416 //================================================================================
1418 void SMESH_Mesh::exportMEDCommmon(DriverMED_W_SMESHDS_Mesh& theWriter,
1419 const char* theMeshName,
1421 const SMESHDS_Mesh* theMeshPart,
1422 bool theAutoDimension,
1423 bool theAddODOnVertices,
1424 double theZTolerance,
1425 bool theSaveNumbers)
1427 Driver_Mesh::Status status = Driver_Mesh::DRS_OK;
1431 theWriter.SetMesh ( theMeshPart ? (SMESHDS_Mesh*) theMeshPart : _meshDS );
1432 theWriter.SetAutoDimension( theAutoDimension );
1433 theWriter.AddODOnVertices ( theAddODOnVertices );
1434 theWriter.SetZTolerance ( theZTolerance );
1435 theWriter.SetSaveNumbers ( theSaveNumbers );
1437 theWriter.SetMeshId ( _id );
1439 theWriter.SetMeshId ( -1 );
1440 theWriter.SetMeshName ( theMeshName );
1443 if ( theAutoGroups ) {
1444 theWriter.AddGroupOfNodes();
1445 theWriter.AddGroupOfEdges();
1446 theWriter.AddGroupOfFaces();
1447 theWriter.AddGroupOfVolumes();
1448 theWriter.AddGroupOf0DElems();
1449 theWriter.AddGroupOfBalls();
1452 // Pass groups to writer. Provide unique group names.
1453 //set<string> aGroupNames; // Corrected for Mantis issue 0020028
1456 std::map< SMDSAbs_ElementType, std::set<std::string> > aGroupNames;
1458 int maxNbIter = 10000; // to guarantee cycle finish
1459 for ( std::map<int, SMESH_Group*>::iterator it = _mapGroup.begin();
1460 it != _mapGroup.end();
1462 SMESH_Group* aGroup = it->second;
1463 SMESHDS_GroupBase* aGroupDS = aGroup->GetGroupDS();
1465 SMDSAbs_ElementType aType = aGroupDS->GetType();
1466 std::string aGroupName0 = aGroup->GetName();
1467 aGroupName0.resize(MAX_MED_GROUP_NAME_LENGTH);
1468 std::string aGroupName = aGroupName0;
1469 for (int i = 1; !aGroupNames[aType].insert(aGroupName).second && i < maxNbIter; i++) {
1470 sprintf(&aString[0], "GR_%d_%s", i, aGroupName0.c_str());
1471 aGroupName = aString;
1472 aGroupName.resize(MAX_MED_GROUP_NAME_LENGTH);
1474 aGroupDS->SetStoreName( aGroupName.c_str() );
1475 theWriter.AddGroup( aGroupDS );
1480 status = theWriter.Perform();
1482 SMESH_CATCH( SMESH::throwSalomeEx );
1484 if ( status == Driver_Mesh::DRS_TOO_LARGE_MESH )
1485 throw TooLargeForExport("MED");
1488 //================================================================================
1490 * Same as SMESH_Mesh::ExportMED except for \a file and \a theVersion
1492 //================================================================================
1494 MEDCoupling::MCAuto<MEDCoupling::DataArrayByte>
1495 SMESH_Mesh::ExportMEDCoupling(const char* theMeshName,
1497 const SMESHDS_Mesh* theMeshPart,
1498 bool theAutoDimension,
1499 bool theAddODOnVertices,
1500 double theZTolerance,
1501 bool theSaveNumbers)
1503 DriverMED_W_SMESHDS_Mesh_Mem writer;
1504 this->exportMEDCommmon( writer, theMeshName, theAutoGroups, theMeshPart, theAutoDimension,
1505 theAddODOnVertices, theZTolerance, theSaveNumbers);
1506 return writer.getData();
1509 //================================================================================
1511 * \brief Export the mesh to a med file
1512 * \param [in] theFile - name of the MED file
1513 * \param [in] theMeshName - name of this mesh
1514 * \param [in] theAutoGroups - boolean parameter for creating/not creating
1515 * the groups Group_On_All_Nodes, Group_On_All_Faces, ... ;
1516 * the typical use is auto_groups=false.
1517 * \param [in] theVersion - define the minor (xy, where version is x.y.z) of MED file format.
1518 * If theVersion is equal to -1, the minor version is not changed (default).
1519 * \param [in] theMeshPart - mesh data to export
1520 * \param [in] theAutoDimension - if \c true, a space dimension of a MED mesh can be either
1521 * - 1D if all mesh nodes lie on OX coordinate axis, or
1522 * - 2D if all mesh nodes lie on XOY coordinate plane, or
1523 * - 3D in the rest cases.
1524 * If \a theAutoDimension is \c false, the space dimension is always 3.
1525 * \param [in] theAddODOnVertices - to create 0D elements on all vertices
1526 * \param [in] theZTolerance - tolerance in Z direction. If Z coordinate of a node is close to zero
1527 * within a given tolerance, the coordinate is set to zero.
1528 * If \a ZTolerance is negative, the node coordinates are kept as is.
1529 * \param [in] theSaveNumbers : enable saving numbers of nodes and cells.
1530 * \return int - mesh index in the file
1532 //================================================================================
1534 void SMESH_Mesh::ExportMED(const char * theFile,
1535 const char* theMeshName,
1538 const SMESHDS_Mesh* theMeshPart,
1539 bool theAutoDimension,
1540 bool theAddODOnVertices,
1541 double theZTolerance,
1542 bool theSaveNumbers)
1544 MESSAGE("MED_VERSION:"<< theVersion);
1545 DriverMED_W_SMESHDS_Mesh writer;
1546 writer.SetFile( theFile, theVersion );
1547 this->exportMEDCommmon( writer, theMeshName, theAutoGroups, theMeshPart, theAutoDimension, theAddODOnVertices, theZTolerance, theSaveNumbers );
1550 //================================================================================
1552 * \brief Export the mesh to a DAT file
1554 //================================================================================
1556 void SMESH_Mesh::ExportDAT(const char * file,
1557 const SMESHDS_Mesh* meshPart,
1558 const bool renumber)
1560 Driver_Mesh::Status status;
1563 DriverDAT_W_SMDS_Mesh writer;
1564 writer.SetFile( file );
1565 writer.SetMesh( meshPart ? (SMESHDS_Mesh*) meshPart : _meshDS );
1566 writer.SetMeshId(_id);
1567 writer.SetRenumber( renumber );
1568 status = writer.Perform();
1570 SMESH_CATCH( SMESH::throwSalomeEx );
1572 if ( status == Driver_Mesh::DRS_TOO_LARGE_MESH )
1573 throw TooLargeForExport("DAT");
1576 //================================================================================
1578 * \brief Export the mesh to an UNV file
1580 //================================================================================
1582 void SMESH_Mesh::ExportUNV(const char * file,
1583 const SMESHDS_Mesh* meshPart,
1584 const bool renumber)
1586 Driver_Mesh::Status status;
1589 DriverUNV_W_SMDS_Mesh writer;
1590 writer.SetFile( file );
1591 writer.SetMesh( meshPart ? (SMESHDS_Mesh*) meshPart : _meshDS );
1592 writer.SetMeshId(_id);
1593 writer.SetRenumber( renumber );
1595 // pass group names to SMESHDS
1598 std::map<int, SMESH_Group*>::iterator it = _mapGroup.begin();
1599 for ( ; it != _mapGroup.end(); it++ ) {
1600 SMESH_Group* aGroup = it->second;
1601 SMESHDS_GroupBase* aGroupDS = aGroup->GetGroupDS();
1603 std::string aGroupName = aGroup->GetName();
1604 aGroupDS->SetStoreName( aGroupName.c_str() );
1605 writer.AddGroup( aGroupDS );
1609 status = writer.Perform();
1611 SMESH_CATCH( SMESH::throwSalomeEx );
1613 if ( status == Driver_Mesh::DRS_TOO_LARGE_MESH )
1614 throw TooLargeForExport("UNV");
1617 //================================================================================
1619 * \brief Export the mesh to an STL file
1621 //================================================================================
1623 void SMESH_Mesh::ExportSTL(const char * file,
1626 const SMESHDS_Mesh* meshPart)
1628 Driver_Mesh::Status status;
1631 DriverSTL_W_SMDS_Mesh writer;
1632 writer.SetFile( file );
1633 writer.SetIsAscii( isascii );
1634 writer.SetMesh( meshPart ? (SMESHDS_Mesh*) meshPart : _meshDS);
1635 writer.SetMeshId(_id);
1636 if ( name ) writer.SetName( name );
1637 status = writer.Perform();
1639 SMESH_CATCH( SMESH::throwSalomeEx );
1641 if ( status == Driver_Mesh::DRS_TOO_LARGE_MESH )
1642 throw TooLargeForExport("STL");
1645 //================================================================================
1647 * \brief Export the mesh to the CGNS file
1649 //================================================================================
1651 void SMESH_Mesh::ExportCGNS(const char * file,
1652 const SMESHDS_Mesh* meshDS,
1653 const char * meshName,
1654 const bool groupElemsByType)
1657 int res = Driver_Mesh::DRS_FAIL;
1660 // pass group names to SMESHDS
1661 std::map<int, SMESH_Group*>::iterator it = _mapGroup.begin();
1662 for ( ; it != _mapGroup.end(); it++ ) {
1663 SMESH_Group* group = it->second;
1664 SMESHDS_GroupBase* groupDS = group->GetGroupDS();
1666 std::string groupName = group->GetName();
1667 groupDS->SetStoreName( groupName.c_str() );
1672 DriverCGNS_Write writer;
1673 writer.SetFile( file );
1674 writer.SetMesh( const_cast<SMESHDS_Mesh*>( meshDS ));
1675 writer.SetMeshName( SMESH_Comment("Mesh_") << meshDS->GetPersistentId());
1676 if ( meshName && meshName[0] )
1677 writer.SetMeshName( meshName );
1678 writer.SetElementsByType( groupElemsByType );
1679 res = writer.Perform();
1680 if ( res != Driver_Mesh::DRS_OK )
1682 SMESH_ComputeErrorPtr err = writer.GetError();
1683 if ( err && !err->IsOK() && !err->myComment.empty() )
1684 throw SALOME_Exception(("Export failed: " + err->myComment ).c_str() );
1688 SMESH_CATCH( SMESH::throwSalomeEx );
1690 if ( res == Driver_Mesh::DRS_TOO_LARGE_MESH )
1691 throw TooLargeForExport("CGNS");
1693 if ( res != Driver_Mesh::DRS_OK )
1694 throw SALOME_Exception("Export failed");
1697 //================================================================================
1699 * \brief Export the mesh to a GMF file
1701 //================================================================================
1703 void SMESH_Mesh::ExportGMF(const char * file,
1704 const SMESHDS_Mesh* meshDS,
1705 bool withRequiredGroups)
1707 Driver_Mesh::Status status;
1710 DriverGMF_Write writer;
1711 writer.SetFile( file );
1712 writer.SetMesh( const_cast<SMESHDS_Mesh*>( meshDS ));
1713 writer.SetExportRequiredGroups( withRequiredGroups );
1715 status = writer.Perform();
1717 SMESH_CATCH( SMESH::throwSalomeEx );
1719 if ( status == Driver_Mesh::DRS_TOO_LARGE_MESH )
1720 throw TooLargeForExport("GMF");
1723 //================================================================================
1725 * \brief Return a ratio of "compute cost" of computed sub-meshes to the whole
1728 //================================================================================
1730 double SMESH_Mesh::GetComputeProgress() const
1732 double totalCost = 1e-100, computedCost = 0;
1733 const SMESH_subMesh* curSM = _gen->GetCurrentSubMesh();
1735 // get progress of a current algo
1736 TColStd_MapOfInteger currentSubIds;
1738 if ( SMESH_Algo* algo = curSM->GetAlgo() )
1740 int algoNotDoneCost = 0, algoDoneCost = 0;
1741 const std::vector<SMESH_subMesh*>& smToCompute = algo->SubMeshesToCompute();
1742 for ( size_t i = 0; i < smToCompute.size(); ++i )
1744 if ( smToCompute[i]->IsEmpty() || smToCompute.size() == 1 )
1745 algoNotDoneCost += smToCompute[i]->GetComputeCost();
1747 algoDoneCost += smToCompute[i]->GetComputeCost();
1748 currentSubIds.Add( smToCompute[i]->GetId() );
1754 rate = algo->GetProgress();
1758 std::cerr << "Exception in " << algo->GetName() << "::GetProgress()" << std::endl;
1761 if ( 0. < rate && rate < 1.001 )
1763 computedCost += rate * ( algoDoneCost + algoNotDoneCost );
1767 rate = algo->GetProgressByTic();
1768 computedCost += algoDoneCost + rate * algoNotDoneCost;
1770 // cout << "rate: "<<rate << " algoNotDoneCost: " << algoNotDoneCost << endl;
1773 // get cost of already treated sub-meshes
1774 if ( SMESH_subMesh* mainSM = GetSubMeshContaining( 1 ))
1776 SMESH_subMeshIteratorPtr smIt = mainSM->getDependsOnIterator(/*includeSelf=*/true);
1777 while ( smIt->more() )
1779 const SMESH_subMesh* sm = smIt->next();
1780 const int smCost = sm->GetComputeCost();
1781 totalCost += smCost;
1782 if ( !currentSubIds.Contains( sm->GetId() ) )
1784 if (( !sm->IsEmpty() ) ||
1785 ( sm->GetComputeState() == SMESH_subMesh::FAILED_TO_COMPUTE &&
1786 !sm->DependsOn( curSM ) ))
1787 computedCost += smCost;
1791 // cout << "Total: " << totalCost
1792 // << " computed: " << computedCost << " progress: " << computedCost / totalCost
1793 // << " nbElems: " << GetMeshDS()->GetMeshInfo().NbElements() << endl;
1794 return computedCost / totalCost;
1797 //================================================================================
1799 * \brief Return number of nodes in the mesh
1801 //================================================================================
1803 smIdType SMESH_Mesh::NbNodes() const
1805 return _meshDS->NbNodes();
1808 //================================================================================
1810 * \brief Return number of edges of given order in the mesh
1812 //================================================================================
1814 smIdType SMESH_Mesh::Nb0DElements() const
1816 return _meshDS->GetMeshInfo().Nb0DElements();
1819 //================================================================================
1821 * \brief Return number of edges of given order in the mesh
1823 //================================================================================
1825 smIdType SMESH_Mesh::NbEdges(SMDSAbs_ElementOrder order) const
1827 return _meshDS->GetMeshInfo().NbEdges(order);
1830 //================================================================================
1832 * \brief Return number of faces of given order in the mesh
1834 //================================================================================
1836 smIdType SMESH_Mesh::NbFaces(SMDSAbs_ElementOrder order) const
1838 return _meshDS->GetMeshInfo().NbFaces(order);
1841 //================================================================================
1843 * \brief Return the number of faces in the mesh
1845 //================================================================================
1847 smIdType SMESH_Mesh::NbTriangles(SMDSAbs_ElementOrder order) const
1849 return _meshDS->GetMeshInfo().NbTriangles(order);
1852 //================================================================================
1854 * \brief Return number of biquadratic triangles in the mesh
1856 //================================================================================
1858 smIdType SMESH_Mesh::NbBiQuadTriangles() const
1860 return _meshDS->GetMeshInfo().NbBiQuadTriangles();
1863 //================================================================================
1865 * \brief Return the number nodes faces in the mesh
1867 //================================================================================
1869 smIdType SMESH_Mesh::NbQuadrangles(SMDSAbs_ElementOrder order) const
1871 return _meshDS->GetMeshInfo().NbQuadrangles(order);
1874 //================================================================================
1876 * \brief Return number of biquadratic quadrangles in the mesh
1878 //================================================================================
1880 smIdType SMESH_Mesh::NbBiQuadQuadrangles() const
1882 return _meshDS->GetMeshInfo().NbBiQuadQuadrangles();
1885 //================================================================================
1887 * \brief Return the number of polygonal faces in the mesh
1889 //================================================================================
1891 smIdType SMESH_Mesh::NbPolygons(SMDSAbs_ElementOrder order) const
1893 return _meshDS->GetMeshInfo().NbPolygons(order);
1896 //================================================================================
1898 * \brief Return number of volumes of given order in the mesh
1900 //================================================================================
1902 smIdType SMESH_Mesh::NbVolumes(SMDSAbs_ElementOrder order) const
1904 return _meshDS->GetMeshInfo().NbVolumes(order);
1907 //================================================================================
1909 * \brief Return number of tetrahedrons of given order in the mesh
1911 //================================================================================
1913 smIdType SMESH_Mesh::NbTetras(SMDSAbs_ElementOrder order) const
1915 return _meshDS->GetMeshInfo().NbTetras(order);
1918 //================================================================================
1920 * \brief Return number of hexahedrons of given order in the mesh
1922 //================================================================================
1924 smIdType SMESH_Mesh::NbHexas(SMDSAbs_ElementOrder order) const
1926 return _meshDS->GetMeshInfo().NbHexas(order);
1929 //================================================================================
1931 * \brief Return number of triquadratic hexahedrons in the mesh
1933 //================================================================================
1935 smIdType SMESH_Mesh::NbTriQuadraticHexas() const
1937 return _meshDS->GetMeshInfo().NbTriQuadHexas();
1940 //================================================================================
1942 * \brief Return number of pyramids of given order in the mesh
1944 //================================================================================
1946 smIdType SMESH_Mesh::NbPyramids(SMDSAbs_ElementOrder order) const
1948 return _meshDS->GetMeshInfo().NbPyramids(order);
1951 //================================================================================
1953 * \brief Return number of prisms (penthahedrons) of given order in the mesh
1955 //================================================================================
1957 smIdType SMESH_Mesh::NbPrisms(SMDSAbs_ElementOrder order) const
1959 return _meshDS->GetMeshInfo().NbPrisms(order);
1962 smIdType SMESH_Mesh::NbQuadPrisms() const
1964 return _meshDS->GetMeshInfo().NbQuadPrisms();
1967 smIdType SMESH_Mesh::NbBiQuadPrisms() const
1969 return _meshDS->GetMeshInfo().NbBiQuadPrisms();
1973 //================================================================================
1975 * \brief Return number of hexagonal prisms in the mesh
1977 //================================================================================
1979 smIdType SMESH_Mesh::NbHexagonalPrisms() const
1981 return _meshDS->GetMeshInfo().NbHexPrisms();
1984 //================================================================================
1986 * \brief Return number of polyhedrons in the mesh
1988 //================================================================================
1990 smIdType SMESH_Mesh::NbPolyhedrons() const
1992 return _meshDS->GetMeshInfo().NbPolyhedrons();
1995 //================================================================================
1997 * \brief Return number of ball elements in the mesh
1999 //================================================================================
2001 smIdType SMESH_Mesh::NbBalls() const
2003 return _meshDS->GetMeshInfo().NbBalls();
2006 //================================================================================
2008 * \brief Return number of submeshes in the mesh
2010 //================================================================================
2012 smIdType SMESH_Mesh::NbSubMesh() const
2014 return _meshDS->NbSubMesh();
2017 //================================================================================
2019 * \brief Returns number of meshes in the Study, that is supposed to be
2020 * equal to SMESHDS_Document::NbMeshes()
2022 //================================================================================
2024 int SMESH_Mesh::NbMeshes() const // nb meshes in the Study
2026 return _document->NbMeshes();
2029 //=======================================================================
2030 //function : IsNotConformAllowed
2031 //purpose : check if a hypothesis allowing notconform mesh is present
2032 //=======================================================================
2034 bool SMESH_Mesh::IsNotConformAllowed() const
2036 if(MYDEBUG) MESSAGE("SMESH_Mesh::IsNotConformAllowed");
2038 static SMESH_HypoFilter filter( SMESH_HypoFilter::HasName( "NotConformAllowed" ));
2039 return GetHypothesis( _meshDS->ShapeToMesh(), filter, false );
2042 //=======================================================================
2043 //function : IsMainShape
2045 //=======================================================================
2047 bool SMESH_Mesh::IsMainShape(const TopoDS_Shape& theShape) const
2049 return theShape.IsSame(_meshDS->ShapeToMesh() );
2052 //=======================================================================
2053 //function : GetShapeByEntry
2054 //purpose : return TopoDS_Shape by its study entry
2055 //=======================================================================
2057 TopoDS_Shape SMESH_Mesh::GetShapeByEntry(const std::string& entry) const
2059 return _callUp ? _callUp->GetShapeByEntry( entry ) : TopoDS_Shape();
2062 //=============================================================================
2066 //=============================================================================
2068 SMESH_Group* SMESH_Mesh::AddGroup (const SMDSAbs_ElementType theType,
2069 const char* theName,
2071 const TopoDS_Shape& theShape,
2072 const SMESH_PredicatePtr& thePredicate)
2074 if ( _mapGroup.count( theId ))
2076 int id = ( theId < 0 ) ? _groupId : theId;
2077 SMESH_Group* aGroup = new SMESH_Group ( id, this, theType, theName, theShape, thePredicate );
2078 GetMeshDS()->AddGroup( aGroup->GetGroupDS() );
2079 _mapGroup[ id ] = aGroup;
2080 _groupId = 1 + _mapGroup.rbegin()->first;
2084 //================================================================================
2086 * \brief Creates a group based on an existing SMESHDS group. Group ID should be unique
2088 //================================================================================
2090 SMESH_Group* SMESH_Mesh::AddGroup (SMESHDS_GroupBase* groupDS)
2093 throw SALOME_Exception(LOCALIZED ("SMESH_Mesh::AddGroup(): NULL SMESHDS_GroupBase"));
2095 std::map <int, SMESH_Group*>::iterator i_g = _mapGroup.find( groupDS->GetID() );
2096 if ( i_g != _mapGroup.end() && i_g->second )
2098 if ( i_g->second->GetGroupDS() == groupDS )
2101 throw SALOME_Exception(LOCALIZED ("SMESH_Mesh::AddGroup() wrong ID of SMESHDS_GroupBase"));
2103 SMESH_Group* aGroup = new SMESH_Group (groupDS);
2104 _mapGroup[ groupDS->GetID() ] = aGroup;
2105 GetMeshDS()->AddGroup( aGroup->GetGroupDS() );
2107 _groupId = 1 + _mapGroup.rbegin()->first;
2113 //================================================================================
2115 * \brief Creates SMESH_Groups for not wrapped SMESHDS_Groups
2116 * \retval bool - true if new SMESH_Groups have been created
2119 //================================================================================
2121 bool SMESH_Mesh::SynchronizeGroups()
2123 const size_t nbGroups = _mapGroup.size();
2124 const std::set<SMESHDS_GroupBase*>& groups = _meshDS->GetGroups();
2125 std::set<SMESHDS_GroupBase*>::const_iterator gIt = groups.begin();
2126 for ( ; gIt != groups.end(); ++gIt )
2128 SMESHDS_GroupBase* groupDS = (SMESHDS_GroupBase*) *gIt;
2129 _groupId = groupDS->GetID();
2130 if ( !_mapGroup.count( _groupId ))
2131 _mapGroup[_groupId] = new SMESH_Group( groupDS );
2133 if ( !_mapGroup.empty() )
2134 _groupId = 1 + _mapGroup.rbegin()->first;
2136 return nbGroups < _mapGroup.size();
2139 //================================================================================
2141 * \brief Return iterator on all existing groups
2143 //================================================================================
2145 SMESH_Mesh::GroupIteratorPtr SMESH_Mesh::GetGroups() const
2147 typedef std::map <int, SMESH_Group *> TMap;
2148 return GroupIteratorPtr( new SMDS_mapIterator<TMap>( _mapGroup ));
2151 //=============================================================================
2153 * \brief Return a group by ID
2155 //=============================================================================
2157 SMESH_Group* SMESH_Mesh::GetGroup (const int theGroupID) const
2159 std::map <int, SMESH_Group*>::const_iterator id_grp = _mapGroup.find( theGroupID );
2160 if ( id_grp == _mapGroup.end() )
2162 return id_grp->second;
2166 //=============================================================================
2168 * \brief Return IDs of all groups
2170 //=============================================================================
2172 std::list<int> SMESH_Mesh::GetGroupIds() const
2174 std::list<int> anIds;
2175 std::map<int, SMESH_Group*>::const_iterator it = _mapGroup.begin();
2176 for ( ; it != _mapGroup.end(); it++ )
2177 anIds.push_back( it->first );
2182 //================================================================================
2184 * \brief Set a caller of methods at level of CORBA API implementation.
2185 * The set upCaller will be deleted by SMESH_Mesh
2187 //================================================================================
2189 void SMESH_Mesh::SetCallUp( TCallUp* upCaller )
2191 if ( _callUp ) delete _callUp;
2195 //=============================================================================
2199 //=============================================================================
2201 bool SMESH_Mesh::RemoveGroup( const int theGroupID )
2203 if (_mapGroup.find(theGroupID) == _mapGroup.end())
2205 GetMeshDS()->RemoveGroup( _mapGroup[theGroupID]->GetGroupDS() );
2206 delete _mapGroup[theGroupID];
2207 _mapGroup.erase (theGroupID);
2209 _callUp->RemoveGroup( theGroupID );
2213 //=======================================================================
2214 //function : GetAncestors
2215 //purpose : return list of ancestors of theSubShape in the order
2216 // that lower dimension shapes come first.
2217 //=======================================================================
2219 const TopTools_ListOfShape& SMESH_Mesh::GetAncestors(const TopoDS_Shape& theS) const
2221 if ( _mapAncestors.Contains( theS ) )
2222 return _mapAncestors.FindFromKey( theS );
2224 static TopTools_ListOfShape emptyList;
2228 //=======================================================================
2230 //purpose : dumps contents of mesh to stream [ debug purposes ]
2231 //=======================================================================
2233 ostream& SMESH_Mesh::Dump(ostream& save)
2236 save << "========================== Dump contents of mesh ==========================" << endl << endl;
2237 save << ++clause << ") Total number of nodes: \t" << NbNodes() << endl;
2238 save << ++clause << ") Total number of edges: \t" << NbEdges() << endl;
2239 save << ++clause << ") Total number of faces: \t" << NbFaces() << endl;
2240 save << ++clause << ") Total number of polygons: \t" << NbPolygons() << endl;
2241 save << ++clause << ") Total number of volumes: \t" << NbVolumes() << endl;
2242 save << ++clause << ") Total number of polyhedrons:\t" << NbPolyhedrons() << endl << endl;
2243 for ( int isQuadratic = 0; isQuadratic < 2; ++isQuadratic )
2245 std::string orderStr = isQuadratic ? "quadratic" : "linear";
2246 SMDSAbs_ElementOrder order = isQuadratic ? ORDER_QUADRATIC : ORDER_LINEAR;
2248 save << ++clause << ") Total number of " << orderStr << " edges:\t" << NbEdges(order) << endl;
2249 save << ++clause << ") Total number of " << orderStr << " faces:\t" << NbFaces(order) << endl;
2250 if ( NbFaces(order) > 0 ) {
2251 smIdType nb3 = NbTriangles(order);
2252 smIdType nb4 = NbQuadrangles(order);
2253 save << clause << ".1) Number of " << orderStr << " triangles: \t" << nb3 << endl;
2254 save << clause << ".2) Number of " << orderStr << " quadrangles:\t" << nb4 << endl;
2255 if ( nb3 + nb4 != NbFaces(order) ) {
2256 std::map<int,int> myFaceMap;
2257 SMDS_FaceIteratorPtr itFaces=_meshDS->facesIterator();
2258 while( itFaces->more( ) ) {
2259 int nbNodes = itFaces->next()->NbNodes();
2260 if ( myFaceMap.find( nbNodes ) == myFaceMap.end() )
2261 myFaceMap[ nbNodes ] = 0;
2262 myFaceMap[ nbNodes ] = myFaceMap[ nbNodes ] + 1;
2264 save << clause << ".3) Faces in detail: " << endl;
2265 std::map <int,int>::iterator itF;
2266 for (itF = myFaceMap.begin(); itF != myFaceMap.end(); itF++)
2267 save << "--> nb nodes: " << itF->first << " - nb elements:\t" << itF->second << endl;
2270 save << ++clause << ") Total number of " << orderStr << " volumes:\t" << NbVolumes(order) << endl;
2271 if ( NbVolumes(order) > 0 ) {
2272 smIdType nb8 = NbHexas(order);
2273 smIdType nb4 = NbTetras(order);
2274 smIdType nb5 = NbPyramids(order);
2275 smIdType nb6 = NbPrisms(order);
2276 save << clause << ".1) Number of " << orderStr << " hexahedrons: \t" << nb8 << endl;
2277 save << clause << ".2) Number of " << orderStr << " tetrahedrons:\t" << nb4 << endl;
2278 save << clause << ".3) Number of " << orderStr << " prisms: \t" << nb6 << endl;
2279 save << clause << ".4) Number of " << orderStr << " pyramids: \t" << nb5 << endl;
2280 if ( nb8 + nb4 + nb5 + nb6 != NbVolumes(order) ) {
2281 std::map<int,int> myVolumesMap;
2282 SMDS_VolumeIteratorPtr itVolumes=_meshDS->volumesIterator();
2283 while( itVolumes->more( ) ) {
2284 int nbNodes = itVolumes->next()->NbNodes();
2285 if ( myVolumesMap.find( nbNodes ) == myVolumesMap.end() )
2286 myVolumesMap[ nbNodes ] = 0;
2287 myVolumesMap[ nbNodes ] = myVolumesMap[ nbNodes ] + 1;
2289 save << clause << ".5) Volumes in detail: " << endl;
2290 std::map <int,int>::iterator itV;
2291 for (itV = myVolumesMap.begin(); itV != myVolumesMap.end(); itV++)
2292 save << "--> nb nodes: " << itV->first << " - nb elements:\t" << itV->second << endl;
2297 save << "===========================================================================" << endl;
2301 //=======================================================================
2302 //function : GetElementType
2303 //purpose : Returns type of mesh element with certain id
2304 //=======================================================================
2306 SMDSAbs_ElementType SMESH_Mesh::GetElementType( const smIdType id, const bool iselem )
2308 return _meshDS->GetElementType( id, iselem );
2311 //=============================================================================
2313 * \brief Convert group on geometry into standalone group
2315 //=============================================================================
2317 SMESH_Group* SMESH_Mesh::ConvertToStandalone ( int theGroupID )
2319 SMESH_Group* aGroup = 0;
2320 std::map < int, SMESH_Group * >::iterator itg = _mapGroup.find( theGroupID );
2321 if ( itg == _mapGroup.end() )
2324 SMESH_Group* anOldGrp = (*itg).second;
2325 if ( !anOldGrp || !anOldGrp->GetGroupDS() )
2327 SMESHDS_GroupBase* anOldGrpDS = anOldGrp->GetGroupDS();
2329 // create new standalone group
2330 aGroup = new SMESH_Group (theGroupID, this, anOldGrpDS->GetType(), anOldGrp->GetName() );
2331 _mapGroup[theGroupID] = aGroup;
2333 SMESHDS_Group* aNewGrpDS = dynamic_cast<SMESHDS_Group*>( aGroup->GetGroupDS() );
2334 GetMeshDS()->RemoveGroup( anOldGrpDS );
2335 GetMeshDS()->AddGroup( aNewGrpDS );
2337 // add elements (or nodes) into new created group
2338 SMDS_ElemIteratorPtr anItr = anOldGrpDS->GetElements();
2339 while ( anItr->more() )
2340 aNewGrpDS->Add( (anItr->next())->GetID() );
2343 aNewGrpDS->SetColor( anOldGrpDS->GetColor() );
2351 //=============================================================================
2353 * \brief remove submesh order from Mesh
2355 //=============================================================================
2357 void SMESH_Mesh::ClearMeshOrder()
2359 _subMeshOrder.clear();
2362 //=============================================================================
2364 * \brief remove submesh order from Mesh
2366 //=============================================================================
2368 void SMESH_Mesh::SetMeshOrder(const TListOfListOfInt& theOrder )
2370 _subMeshOrder = theOrder;
2373 //=============================================================================
2375 * \brief return submesh order if any
2377 //=============================================================================
2379 const TListOfListOfInt& SMESH_Mesh::GetMeshOrder() const
2381 return _subMeshOrder;
2384 //=============================================================================
2386 * \brief fill _mapAncestors
2388 //=============================================================================
2390 void SMESH_Mesh::fillAncestorsMap(const TopoDS_Shape& theShape)
2392 int desType, ancType;
2393 if ( !theShape.IsSame( GetShapeToMesh()) && theShape.ShapeType() == TopAbs_COMPOUND )
2395 // a geom group is added. Insert it into lists of ancestors before
2396 // the first ancestor more complex than group members
2397 TopoDS_Iterator subIt( theShape );
2398 if ( !subIt.More() ) return;
2399 int memberType = subIt.Value().ShapeType();
2400 for ( desType = TopAbs_VERTEX; desType >= memberType; desType-- )
2401 for (TopExp_Explorer des( theShape, TopAbs_ShapeEnum( desType )); des.More(); des.Next())
2403 if ( !_mapAncestors.Contains( des.Current() )) continue;// issue 0020982
2404 TopTools_ListOfShape& ancList = _mapAncestors.ChangeFromKey( des.Current() );
2405 TopTools_ListIteratorOfListOfShape ancIt (ancList);
2406 while ( ancIt.More() && ancIt.Value().ShapeType() >= memberType )
2408 if ( ancIt.More() ) ancList.InsertBefore( theShape, ancIt );
2409 else ancList.Append( theShape );
2412 else // else added for 52457: Addition of hypotheses is 8 time longer than meshing
2414 for ( desType = TopAbs_VERTEX; desType > TopAbs_COMPOUND; desType-- )
2415 for ( ancType = desType - 1; ancType >= TopAbs_COMPOUND; ancType-- )
2416 TopExp::MapShapesAndAncestors ( theShape,
2417 (TopAbs_ShapeEnum) desType,
2418 (TopAbs_ShapeEnum) ancType,
2421 // visit COMPOUNDs inside a COMPOUND that are not reachable by TopExp_Explorer
2422 if ( theShape.ShapeType() == TopAbs_COMPOUND )
2424 TopoDS_Iterator sIt(theShape);
2425 if ( sIt.More() && sIt.Value().ShapeType() == TopAbs_COMPOUND )
2426 for ( ; sIt.More(); sIt.Next() )
2427 if ( sIt.Value().ShapeType() == TopAbs_COMPOUND )
2428 fillAncestorsMap( sIt.Value() );
2432 //=============================================================================
2434 * \brief sort submeshes according to stored mesh order
2435 * \param theListToSort in out list to be sorted
2436 * \return FALSE if nothing sorted
2438 //=============================================================================
2440 bool SMESH_Mesh::SortByMeshOrder(std::vector<SMESH_subMesh*>& theListToSort) const
2442 if ( _subMeshOrder.empty() || theListToSort.size() < 2 )
2446 // collect all ordered sub-meshes in smVec as pointers
2447 // and get their positions within theListToSort
2449 std::vector<SMESH_subMesh*> smVec;
2450 typedef std::vector<SMESH_subMesh*>::iterator TPosInList;
2451 std::map< size_t, size_t > sortedPos; // index in theListToSort to order
2452 TPosInList smBeg = theListToSort.begin(), smEnd = theListToSort.end();
2453 TListOfListOfInt::const_iterator listIdsIt = _subMeshOrder.begin();
2454 bool needSort = false;
2455 for( ; listIdsIt != _subMeshOrder.end(); listIdsIt++)
2457 const TListOfInt& listOfId = *listIdsIt;
2458 // convert sm ids to sm's
2460 TListOfInt::const_iterator idIt = listOfId.begin();
2461 for ( ; idIt != listOfId.end(); idIt++ )
2463 if ( SMESH_subMesh * sm = GetSubMeshContaining( *idIt ))
2465 smVec.push_back( sm );
2466 if ( sm->GetSubMeshDS() && sm->GetSubMeshDS()->IsComplexSubmesh() )
2468 smVec.reserve( smVec.size() + sm->GetSubMeshDS()->NbSubMeshes() );
2469 SMESHDS_SubMeshIteratorPtr smdsIt = sm->GetSubMeshDS()->GetSubMeshIterator();
2470 while ( smdsIt->more() )
2472 const SMESHDS_SubMesh* smDS = smdsIt->next();
2473 if (( sm = GetSubMeshContaining( smDS->GetID() )))
2474 smVec.push_back( sm );
2479 // find smVec items in theListToSort
2480 for ( size_t i = 0; i < smVec.size(); ++i )
2482 TPosInList smPos = find( smBeg, smEnd, smVec[i] ); // position in theListToSort
2483 if ( smPos != smEnd )
2485 size_t posInList = std::distance( smBeg, smPos );
2486 size_t order = sortedPos.size();
2487 sortedPos.insert( std::make_pair( posInList, order ));
2488 if ( posInList != order )
2496 // set sm of sortedPos from theListToSort to front of orderedSM
2497 // and the rest of theListToSort to orderedSM end
2499 std::vector<SMESH_subMesh*> orderedSM;
2500 orderedSM.reserve( theListToSort.size() );
2501 orderedSM.resize( sortedPos.size() );
2504 sortedPos.insert( std::make_pair( theListToSort.size(), sortedPos.size() ));
2505 for ( const auto & pos_order : sortedPos )
2507 const size_t& posInList = pos_order.first;
2508 const size_t& order = pos_order.second;
2509 if ( order < sortedPos.size() - 1 )
2510 orderedSM[ order ] = theListToSort[ posInList ];
2512 if ( iPrev < posInList )
2513 orderedSM.insert( orderedSM.end(),
2514 theListToSort.begin() + iPrev,
2515 theListToSort.begin() + posInList );
2516 iPrev = posInList + 1;
2519 theListToSort.swap( orderedSM );
2524 //================================================================================
2526 * \brief Return true if given order of sub-meshes is OK
2528 //================================================================================
2530 bool SMESH_Mesh::IsOrderOK( const SMESH_subMesh* smBefore,
2531 const SMESH_subMesh* smAfter ) const
2533 TListOfListOfInt::const_iterator listIdsIt = _subMeshOrder.begin();
2534 for( ; listIdsIt != _subMeshOrder.end(); listIdsIt++)
2536 const TListOfInt& listOfId = *listIdsIt;
2537 int iB = -1, iA = -1, i = 0;
2538 for ( TListOfInt::const_iterator id = listOfId.begin(); id != listOfId.end(); ++id, ++i )
2540 if ( *id == smBefore->GetId() )
2546 else if ( *id == smAfter->GetId() )
2554 return true; // no order imposed to given sub-meshes
2557 //=============================================================================
2559 * \brief sort submeshes according to stored mesh order
2560 * \param theListToSort in out list to be sorted
2561 * \return FALSE if nothing sorted
2563 //=============================================================================
2565 void SMESH_Mesh::getAncestorsSubMeshes (const TopoDS_Shape& theSubShape,
2566 std::vector< SMESH_subMesh* >& theSubMeshes) const
2568 theSubMeshes.clear();
2569 TopTools_ListIteratorOfListOfShape it( GetAncestors( theSubShape ));
2570 for (; it.More(); it.Next() )
2571 if ( SMESH_subMesh* sm = GetSubMeshContaining( it.Value() ))
2572 theSubMeshes.push_back(sm);
2574 // sort submeshes according to stored mesh order
2575 SortByMeshOrder( theSubMeshes );