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>
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);
244 fs::remove_all(tmp_folder);
249 //================================================================================
251 * \brief Return true if a mesh with given id exists
253 //================================================================================
255 bool SMESH_Mesh::MeshExists( int meshId ) const
257 return _document ? bool( _document->GetMesh( meshId )) : false;
260 //================================================================================
262 * \brief Return a mesh by id
264 //================================================================================
266 SMESH_Mesh* SMESH_Mesh::FindMesh( int meshId ) const
269 return (SMESH_Mesh*) this;
271 if ( StudyContextStruct *aStudyContext = _gen->GetStudyContext())
273 std::map < int, SMESH_Mesh * >::iterator i_m = aStudyContext->mapMesh.find( meshId );
274 if ( i_m != aStudyContext->mapMesh.end() )
280 //=============================================================================
282 * \brief Set geometry to be meshed
284 //=============================================================================
286 void SMESH_Mesh::ShapeToMesh(const TopoDS_Shape & aShape)
288 if(MYDEBUG) MESSAGE("SMESH_Mesh::ShapeToMesh");
290 if ( !aShape.IsNull() && _isShapeToMesh ) {
291 if ( aShape.ShapeType() != TopAbs_COMPOUND && // group contents is allowed to change
292 _meshDS->ShapeToMesh().ShapeType() != TopAbs_COMPOUND )
293 throw SALOME_Exception(LOCALIZED ("a shape to mesh has already been defined"));
295 // clear current data
296 if ( !_meshDS->ShapeToMesh().IsNull() )
298 // removal of a shape to mesh, delete objects referring to sub-shapes:
300 _subMeshHolder->DeleteAll();
301 // - groups on geometry
302 std::map <int, SMESH_Group *>::iterator i_gr = _mapGroup.begin();
303 while ( i_gr != _mapGroup.end() ) {
304 if ( dynamic_cast<SMESHDS_GroupOnGeom*>( i_gr->second->GetGroupDS() )) {
305 _meshDS->RemoveGroup( i_gr->second->GetGroupDS() );
307 _mapGroup.erase( i_gr++ );
312 _mapAncestors.Clear();
315 TopoDS_Shape aNullShape;
316 _meshDS->ShapeToMesh( aNullShape );
318 _shapeDiagonal = 0.0;
321 // set a new geometry
322 if ( !aShape.IsNull() )
324 _meshDS->ShapeToMesh(aShape);
325 _isShapeToMesh = true;
326 _nbSubShapes = _meshDS->MaxShapeIndex();
328 // fill map of ancestors
329 fillAncestorsMap(aShape);
333 _isShapeToMesh = false;
334 _shapeDiagonal = 0.0;
335 _meshDS->ShapeToMesh( PseudoShape() );
340 //=======================================================================
342 * \brief Return geometry to be meshed. (It may be a PseudoShape()!)
344 //=======================================================================
346 TopoDS_Shape SMESH_Mesh::GetShapeToMesh() const
348 return _meshDS->ShapeToMesh();
351 //=======================================================================
353 * \brief Return a solid which is returned by GetShapeToMesh() if
354 * a real geometry to be meshed was not set
356 //=======================================================================
358 const TopoDS_Solid& SMESH_Mesh::PseudoShape()
360 static TopoDS_Solid aSolid;
361 if ( aSolid.IsNull() )
363 aSolid = BRepPrimAPI_MakeBox(1,1,1);
368 //=======================================================================
370 * \brief Return diagonal size of bounding box of a shape
372 //=======================================================================
374 double SMESH_Mesh::GetShapeDiagonalSize(const TopoDS_Shape & aShape)
376 if ( !aShape.IsNull() ) {
378 // avoid too long waiting on large shapes. PreciseBoundingBox() was added
379 // to assure same result which else depends on presence of triangulation (IPAL52557).
380 const int maxNbFaces = 4000;
382 for ( TopExp_Explorer f( aShape, TopAbs_FACE ); f.More() && nbFaces < maxNbFaces; f.Next() )
384 bool isPrecise = false;
385 if ( nbFaces < maxNbFaces )
388 GEOMUtils::PreciseBoundingBox( aShape, Box );
396 BRepBndLib::Add( aShape, Box );
399 return sqrt( Box.SquareExtent() );
404 //=======================================================================
406 * \brief Return diagonal size of bounding box of shape to mesh
408 //=======================================================================
410 double SMESH_Mesh::GetShapeDiagonalSize() const
412 if ( _shapeDiagonal == 0. && _isShapeToMesh )
413 const_cast<SMESH_Mesh*>(this)->_shapeDiagonal = GetShapeDiagonalSize( GetShapeToMesh() );
415 return _shapeDiagonal;
418 //================================================================================
420 * \brief Load mesh from study file
422 //================================================================================
424 void SMESH_Mesh::Load()
430 //=======================================================================
432 * \brief Remove all nodes and elements
434 //=======================================================================
436 void SMESH_Mesh::Clear()
438 if ( HasShapeToMesh() ) // remove all nodes and elements
441 _meshDS->ClearMesh();
443 // update compute state of submeshes
444 if ( SMESH_subMesh *sm = GetSubMeshContaining( GetShapeToMesh() ) )
446 sm->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
447 sm->ComputeSubMeshStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
448 sm->ComputeStateEngine( SMESH_subMesh::CLEAN ); // for event listeners (issue 0020918)
449 sm->ComputeSubMeshStateEngine( SMESH_subMesh::CLEAN );
452 else // remove only nodes/elements computed by algorithms
454 if ( SMESH_subMesh *sm = GetSubMeshContaining( GetShapeToMesh() ) )
456 sm->ComputeStateEngine( SMESH_subMesh::CLEAN );
457 sm->ComputeSubMeshStateEngine( SMESH_subMesh::CLEAN );
458 sm->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
459 sm->ComputeSubMeshStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
462 GetMeshDS()->Modified();
466 //=======================================================================
468 * \brief Remove all nodes and elements of indicated shape
470 //=======================================================================
472 void SMESH_Mesh::ClearSubMesh(const int theShapeId)
474 // clear sub-meshes; get ready to re-compute as a side-effect
475 if ( SMESH_subMesh *sm = GetSubMeshContaining( theShapeId ) )
477 SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(/*includeSelf=*/true,
478 /*complexShapeFirst=*/false);
479 while ( smIt->more() )
482 TopAbs_ShapeEnum shapeType = sm->GetSubShape().ShapeType();
483 if ( shapeType == TopAbs_VERTEX || shapeType < TopAbs_SOLID )
484 // all other shapes depends on vertices so they are already cleaned
485 sm->ComputeStateEngine( SMESH_subMesh::CLEAN );
486 // to recompute even if failed
487 sm->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
492 //=======================================================================
493 //function : UNVToMesh
495 //=======================================================================
497 int SMESH_Mesh::UNVToMesh(const char* theFileName)
499 if ( _isShapeToMesh )
500 throw SALOME_Exception(LOCALIZED("a shape to mesh has already been defined"));
501 _isShapeToMesh = false;
503 DriverUNV_R_SMDS_Mesh myReader;
504 myReader.SetMesh(_meshDS);
505 myReader.SetFile(theFileName);
506 myReader.SetMeshId(-1);
509 TGroupNamesMap& aGroupNames = myReader.GetGroupNamesMap();
510 TGroupNamesMap::iterator gr2names;
511 int anId = 1 + ( _mapGroup.empty() ? 0 : _mapGroup.rbegin()->first );
512 for ( gr2names = aGroupNames.begin(); gr2names != aGroupNames.end(); ++gr2names )
514 SMDS_MeshGroup* aGroup = gr2names->first;
515 const std::string& aName = gr2names->second;
516 SMESHDS_Group* aGroupDS = new SMESHDS_Group( anId++, _meshDS, aGroup->GetType() );
517 aGroupDS->SMDSGroup() = std::move( *aGroup );
518 aGroupDS->SetStoreName( aName.c_str() );
519 AddGroup( aGroupDS );
525 //=======================================================================
526 //function : MEDToMesh
528 //=======================================================================
530 int SMESH_Mesh::MEDToMesh(const char* theFileName, const char* theMeshName)
532 if ( _isShapeToMesh )
533 throw SALOME_Exception(LOCALIZED("a shape to mesh has already been defined"));
534 _isShapeToMesh = false;
536 DriverMED_R_SMESHDS_Mesh myReader;
537 myReader.SetMesh(_meshDS);
538 myReader.SetMeshId(-1);
539 myReader.SetFile(theFileName);
540 myReader.SetMeshName(theMeshName);
541 Driver_Mesh::Status status = myReader.Perform();
543 SMESH_ComputeErrorPtr er = myReader.GetError();
544 if ( er && !er->IsOK() ) std::cout << er->myComment << std::endl;
547 // Reading groups (sub-meshes are out of scope of MED import functionality)
548 std::list<TNameAndType> aGroupNames = myReader.GetGroupNamesAndTypes();
549 std::list<TNameAndType>::iterator name_type = aGroupNames.begin();
550 for ( ; name_type != aGroupNames.end(); name_type++ )
552 SMESH_Group* aGroup = AddGroup( name_type->second, name_type->first.c_str() );
554 SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>( aGroup->GetGroupDS() );
556 aGroupDS->SetStoreName( name_type->first.c_str() );
557 myReader.GetGroup( aGroupDS );
563 _meshDS->CompactMesh();
568 //=======================================================================
569 //function : STLToMesh
571 //=======================================================================
573 std::string SMESH_Mesh::STLToMesh(const char* theFileName)
576 throw SALOME_Exception(LOCALIZED("a shape to mesh has already been defined"));
577 _isShapeToMesh = false;
579 DriverSTL_R_SMDS_Mesh myReader;
580 myReader.SetMesh(_meshDS);
581 myReader.SetFile(theFileName);
582 myReader.SetMeshId(-1);
585 return myReader.GetName();
588 //================================================================================
590 * \brief Reads the given mesh from the CGNS file
591 * \param theFileName - name of the file
592 * \retval int - Driver_Mesh::Status
594 //================================================================================
596 int SMESH_Mesh::CGNSToMesh(const char* theFileName,
597 const int theMeshIndex,
598 std::string& theMeshName)
600 int res = Driver_Mesh::DRS_FAIL;
603 DriverCGNS_Read myReader;
604 myReader.SetMesh(_meshDS);
605 myReader.SetFile(theFileName);
606 myReader.SetMeshId(theMeshIndex);
607 res = myReader.Perform();
608 theMeshName = myReader.GetMeshName();
617 //================================================================================
619 * \brief Fill its data by reading a GMF file
621 //================================================================================
623 SMESH_ComputeErrorPtr SMESH_Mesh::GMFToMesh(const char* theFileName,
624 bool theMakeRequiredGroups)
626 DriverGMF_Read myReader;
627 myReader.SetMesh(_meshDS);
628 myReader.SetFile(theFileName);
629 myReader.SetMakeRequiredGroups( theMakeRequiredGroups );
631 //theMeshName = myReader.GetMeshName();
636 return myReader.GetError();
639 //=============================================================================
643 //=============================================================================
645 SMESH_Hypothesis::Hypothesis_Status
646 SMESH_Mesh::AddHypothesis(const TopoDS_Shape & aSubShape,
648 std::string* anError )
650 if(MYDEBUG) MESSAGE("SMESH_Mesh::AddHypothesis");
655 SMESH_subMesh *subMesh = GetSubMesh(aSubShape);
656 if ( !subMesh || !subMesh->GetId())
657 return SMESH_Hypothesis::HYP_BAD_SUBSHAPE;
659 SMESH_Hypothesis *anHyp = GetHypothesis( anHypId );
661 throw SALOME_Exception(LOCALIZED("hypothesis does not exist"));
663 bool isGlobalHyp = IsMainShape( aSubShape );
665 // NotConformAllowed can be only global
668 // NOTE: this is not a correct way to check a name of hypothesis,
669 // there should be an attribute of hypothesis saying that it can/can't
671 std::string hypName = anHyp->GetName();
672 if ( hypName == "NotConformAllowed" )
674 if(MYDEBUG) MESSAGE( "Hypothesis <NotConformAllowed> can be only global" );
675 return SMESH_Hypothesis::HYP_INCOMPATIBLE;
681 bool isAlgo = ( anHyp->GetType() != SMESHDS_Hypothesis::PARAM_ALGO );
682 SMESH_subMesh::algo_event event = isAlgo ? SMESH_subMesh::ADD_ALGO : SMESH_subMesh::ADD_HYP;
684 SMESH_Hypothesis::Hypothesis_Status ret = subMesh->AlgoStateEngine(event, anHyp);
686 if ( anError && SMESH_Hypothesis::IsStatusFatal(ret) && subMesh->GetComputeError() )
687 *anError = subMesh->GetComputeError()->myComment;
690 if ( !SMESH_Hypothesis::IsStatusFatal(ret) &&
691 anHyp->GetDim() <= SMESH_Gen::GetShapeDim(aSubShape)) // is added on father
693 event = isAlgo ? SMESH_subMesh::ADD_FATHER_ALGO : SMESH_subMesh::ADD_FATHER_HYP;
695 SMESH_Hypothesis::Hypothesis_Status ret2 =
696 subMesh->SubMeshesAlgoStateEngine(event, anHyp, /*exitOnFatal=*/true);
700 if ( SMESH_Hypothesis::IsStatusFatal( ret ))
702 if ( anError && subMesh->GetComputeError() )
703 *anError = subMesh->GetComputeError()->myComment;
705 event = isAlgo ? SMESH_subMesh::REMOVE_ALGO : SMESH_subMesh::REMOVE_HYP;
706 subMesh->AlgoStateEngine(event, anHyp);
710 // check concurrent hypotheses on ancestors
711 if (ret < SMESH_Hypothesis::HYP_CONCURRENT && !isGlobalHyp )
713 SMESH_subMeshIteratorPtr smIt = subMesh->getDependsOnIterator(false,false);
714 while ( smIt->more() ) {
715 SMESH_subMesh* sm = smIt->next();
716 if ( sm->IsApplicableHypothesis( anHyp )) {
717 ret2 = sm->CheckConcurrentHypothesis( anHyp );
726 HasModificationsToDiscard(); // to reset _isModified flag if a mesh becomes empty
727 GetMeshDS()->Modified();
729 if(MYDEBUG) subMesh->DumpAlgoState(true);
730 if(MYDEBUG) SCRUTE(ret);
734 //=============================================================================
738 //=============================================================================
740 SMESH_Hypothesis::Hypothesis_Status
741 SMESH_Mesh::RemoveHypothesis(const TopoDS_Shape & aSubShape,
744 if(MYDEBUG) MESSAGE("SMESH_Mesh::RemoveHypothesis");
746 StudyContextStruct *sc = _gen->GetStudyContext();
747 if (sc->mapHypothesis.find(anHypId) == sc->mapHypothesis.end())
748 throw SALOME_Exception(LOCALIZED("hypothesis does not exist"));
750 SMESH_Hypothesis *anHyp = sc->mapHypothesis[anHypId];
751 if(MYDEBUG) { SCRUTE(anHyp->GetType()); }
755 bool isAlgo = ( !anHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO );
756 SMESH_subMesh::algo_event event = isAlgo ? SMESH_subMesh::REMOVE_ALGO : SMESH_subMesh::REMOVE_HYP;
758 SMESH_subMesh *subMesh = GetSubMesh(aSubShape);
760 SMESH_Hypothesis::Hypothesis_Status ret = subMesh->AlgoStateEngine(event, anHyp);
762 // there may appear concurrent hyps that were covered by the removed hyp
763 if (ret < SMESH_Hypothesis::HYP_CONCURRENT &&
764 subMesh->IsApplicableHypothesis( anHyp ) &&
765 subMesh->CheckConcurrentHypothesis( anHyp ) != SMESH_Hypothesis::HYP_OK)
766 ret = SMESH_Hypothesis::HYP_CONCURRENT;
769 if (!SMESH_Hypothesis::IsStatusFatal(ret) &&
770 anHyp->GetDim() <= SMESH_Gen::GetShapeDim(aSubShape)) // is removed from father
772 event = isAlgo ? SMESH_subMesh::REMOVE_FATHER_ALGO : SMESH_subMesh::REMOVE_FATHER_HYP;
774 SMESH_Hypothesis::Hypothesis_Status ret2 =
775 subMesh->SubMeshesAlgoStateEngine(event, anHyp);
776 if (ret2 > ret) // more severe
779 // check concurrent hypotheses on ancestors
780 if (ret < SMESH_Hypothesis::HYP_CONCURRENT && !IsMainShape( aSubShape ) )
782 SMESH_subMeshIteratorPtr smIt = subMesh->getDependsOnIterator(false,false);
783 while ( smIt->more() ) {
784 SMESH_subMesh* sm = smIt->next();
785 if ( sm->IsApplicableHypothesis( anHyp )) {
786 ret2 = sm->CheckConcurrentHypothesis( anHyp );
796 HasModificationsToDiscard(); // to reset _isModified flag if mesh become empty
797 GetMeshDS()->Modified();
799 if(MYDEBUG) subMesh->DumpAlgoState(true);
800 if(MYDEBUG) SCRUTE(ret);
804 //=============================================================================
808 //=============================================================================
810 const std::list<const SMESHDS_Hypothesis*>&
811 SMESH_Mesh::GetHypothesisList(const TopoDS_Shape & aSubShape) const
813 return _meshDS->GetHypothesis(aSubShape);
816 //=======================================================================
818 * \brief Return the hypothesis assigned to the shape
819 * \param aSubShape - the shape to check
820 * \param aFilter - the hypothesis filter
821 * \param andAncestors - flag to check hypos assigned to ancestors of the shape
822 * \param assignedTo - to return the shape the found hypo is assigned to
823 * \retval SMESH_Hypothesis* - the first hypo passed through aFilter
825 //=======================================================================
827 const SMESH_Hypothesis * SMESH_Mesh::GetHypothesis(const TopoDS_Shape & aSubShape,
828 const SMESH_HypoFilter& aFilter,
829 const bool andAncestors,
830 TopoDS_Shape* assignedTo) const
832 return GetHypothesis( const_cast< SMESH_Mesh* >(this)->GetSubMesh( aSubShape ),
833 aFilter, andAncestors, assignedTo );
836 //=======================================================================
838 * \brief Return the hypothesis assigned to the shape of a sub-mesh
839 * \param aSubMesh - the sub-mesh to check
840 * \param aFilter - the hypothesis filter
841 * \param andAncestors - flag to check hypos assigned to ancestors of the shape
842 * \param assignedTo - to return the shape the found hypo is assigned to
843 * \retval SMESH_Hypothesis* - the first hypo passed through aFilter
845 //=======================================================================
847 const SMESH_Hypothesis * SMESH_Mesh::GetHypothesis(const SMESH_subMesh * aSubMesh,
848 const SMESH_HypoFilter& aFilter,
849 const bool andAncestors,
850 TopoDS_Shape* assignedTo) const
852 if ( !aSubMesh ) return 0;
855 const TopoDS_Shape & aSubShape = aSubMesh->GetSubShape();
856 const std::list<const SMESHDS_Hypothesis*>& hypList = _meshDS->GetHypothesis(aSubShape);
857 std::list<const SMESHDS_Hypothesis*>::const_iterator hyp = hypList.begin();
858 for ( ; hyp != hypList.end(); hyp++ ) {
859 const SMESH_Hypothesis * h = cSMESH_Hyp( *hyp );
860 if ( aFilter.IsOk( h, aSubShape)) {
861 if ( assignedTo ) *assignedTo = aSubShape;
868 // user sorted submeshes of ancestors, according to stored submesh priority
869 std::vector< SMESH_subMesh * > & ancestors =
870 const_cast< std::vector< SMESH_subMesh * > & > ( aSubMesh->GetAncestors() );
871 SortByMeshOrder( ancestors );
873 std::vector<SMESH_subMesh*>::const_iterator smIt = ancestors.begin();
874 for ( ; smIt != ancestors.end(); smIt++ )
876 const TopoDS_Shape& curSh = (*smIt)->GetSubShape();
877 const std::list<const SMESHDS_Hypothesis*>& hypList = _meshDS->GetHypothesis(curSh);
878 std::list<const SMESHDS_Hypothesis*>::const_iterator hyp = hypList.begin();
879 for ( ; hyp != hypList.end(); hyp++ ) {
880 const SMESH_Hypothesis * h = cSMESH_Hyp( *hyp );
881 if (aFilter.IsOk( h, curSh )) {
882 if ( assignedTo ) *assignedTo = curSh;
891 //================================================================================
893 * \brief Return hypotheses assigned to the shape
894 * \param aSubShape - the shape to check
895 * \param aFilter - the hypothesis filter
896 * \param aHypList - the list of the found hypotheses
897 * \param andAncestors - flag to check hypos assigned to ancestors of the shape
898 * \retval int - number of unique hypos in aHypList
900 //================================================================================
902 int SMESH_Mesh::GetHypotheses(const TopoDS_Shape & aSubShape,
903 const SMESH_HypoFilter& aFilter,
904 std::list <const SMESHDS_Hypothesis * >& aHypList,
905 const bool andAncestors,
906 std::list< TopoDS_Shape > * assignedTo/*=0*/) const
908 return GetHypotheses( const_cast< SMESH_Mesh* >(this)->GetSubMesh( aSubShape ),
909 aFilter, aHypList, andAncestors, assignedTo );
912 //================================================================================
914 * \brief Return hypotheses assigned to the shape of a sub-mesh
915 * \param aSubShape - the sub-mesh to check
916 * \param aFilter - the hypothesis filter
917 * \param aHypList - the list of the found hypotheses
918 * \param andAncestors - flag to check hypos assigned to ancestors of the shape
919 * \retval int - number of unique hypos in aHypList
921 //================================================================================
923 int SMESH_Mesh::GetHypotheses(const SMESH_subMesh * aSubMesh,
924 const SMESH_HypoFilter& aFilter,
925 std::list <const SMESHDS_Hypothesis * >& aHypList,
926 const bool andAncestors,
927 std::list< TopoDS_Shape > * assignedTo/*=0*/) const
929 if ( !aSubMesh ) return 0;
931 std::set< std::string > hypTypes; // to exclude same type hypos from the result list
934 // only one main hypothesis is allowed
935 bool mainHypFound = false;
938 std::list<const SMESHDS_Hypothesis*>::const_iterator hyp;
939 for ( hyp = aHypList.begin(); hyp != aHypList.end(); hyp++ ) {
940 if ( hypTypes.insert( (*hyp)->GetName() ).second )
942 if ( !cSMESH_Hyp(*hyp)->IsAuxiliary() )
946 // get hypos from aSubShape
948 const TopoDS_Shape & aSubShape = aSubMesh->GetSubShape();
949 const std::list<const SMESHDS_Hypothesis*>& hypList = _meshDS->GetHypothesis(aSubShape);
950 for ( hyp = hypList.begin(); hyp != hypList.end(); hyp++ )
952 const SMESH_Hypothesis* h = cSMESH_Hyp( *hyp );
953 if (( aFilter.IsOk( h, aSubShape )) &&
954 ( h->IsAuxiliary() || !mainHypFound ) &&
955 ( h->IsAuxiliary() || hypTypes.insert( h->GetName() ).second ))
957 aHypList.push_back( *hyp );
959 if ( !h->IsAuxiliary() )
961 if ( assignedTo ) assignedTo->push_back( aSubShape );
966 // get hypos from ancestors of aSubShape
969 // user sorted submeshes of ancestors, according to stored submesh priority
970 std::vector< SMESH_subMesh * > & ancestors =
971 const_cast< std::vector< SMESH_subMesh * > & > ( aSubMesh->GetAncestors() );
972 SortByMeshOrder( ancestors );
974 std::vector<SMESH_subMesh*>::const_iterator smIt = ancestors.begin();
975 for ( ; smIt != ancestors.end(); smIt++ )
977 const TopoDS_Shape& curSh = (*smIt)->GetSubShape();
978 const std::list<const SMESHDS_Hypothesis*>& hypList = _meshDS->GetHypothesis(curSh);
979 for ( hyp = hypList.begin(); hyp != hypList.end(); hyp++ )
981 const SMESH_Hypothesis* h = cSMESH_Hyp( *hyp );
982 if (( aFilter.IsOk( h, curSh )) &&
983 ( h->IsAuxiliary() || !mainHypFound ) &&
984 ( h->IsAuxiliary() || hypTypes.insert( h->GetName() ).second ))
986 aHypList.push_back( *hyp );
988 if ( !h->IsAuxiliary() )
990 if ( assignedTo ) assignedTo->push_back( curSh );
998 //================================================================================
1000 * \brief Return a hypothesis by its ID
1002 //================================================================================
1004 SMESH_Hypothesis * SMESH_Mesh::GetHypothesis(const int anHypId) const
1006 StudyContextStruct *sc = _gen->GetStudyContext();
1007 if (sc->mapHypothesis.find(anHypId) == sc->mapHypothesis.end())
1010 SMESH_Hypothesis *anHyp = sc->mapHypothesis[anHypId];
1014 //=============================================================================
1018 //=============================================================================
1020 const std::list<SMESHDS_Command*> & SMESH_Mesh::GetLog()
1022 return _meshDS->GetScript()->GetCommands();
1025 //=============================================================================
1029 //=============================================================================
1030 void SMESH_Mesh::ClearLog()
1032 _meshDS->GetScript()->Clear();
1035 //=============================================================================
1037 * Get or Create the SMESH_subMesh object implementation
1039 //=============================================================================
1041 SMESH_subMesh * SMESH_Mesh::GetSubMesh(const TopoDS_Shape & aSubShape)
1043 int index = _meshDS->ShapeToIndex(aSubShape);
1044 if ( !index && aSubShape.IsNull() )
1047 // for submeshes on GEOM Group
1048 if (( !index || index > _nbSubShapes ) && aSubShape.ShapeType() == TopAbs_COMPOUND )
1050 TopoDS_Iterator it( aSubShape );
1053 index = _meshDS->AddCompoundSubmesh( aSubShape, it.Value().ShapeType() );
1054 // fill map of Ancestors
1055 while ( _nbSubShapes < index )
1056 fillAncestorsMap( _meshDS->IndexToShape( ++_nbSubShapes ));
1060 // return NULL; // neither sub-shape nor a group
1062 SMESH_subMesh* aSubMesh = _subMeshHolder->Get( index );
1065 aSubMesh = new SMESH_subMesh(index, this, _meshDS, aSubShape);
1066 _subMeshHolder->Add( index, aSubMesh );
1068 // include non-computable sub-meshes in SMESH_subMesh::_ancestors of sub-submeshes
1069 switch ( aSubShape.ShapeType() ) {
1070 case TopAbs_COMPOUND:
1073 for ( TopoDS_Iterator subIt( aSubShape ); subIt.More(); subIt.Next() )
1075 SMESH_subMesh* sm = GetSubMesh( subIt.Value() );
1076 SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(/*inclideSelf=*/true);
1077 while ( smIt->more() )
1078 smIt->next()->ClearAncestors();
1086 //=============================================================================
1088 * Get the SMESH_subMesh object implementation. Don't create it, return null
1089 * if it does not exist.
1091 //=============================================================================
1093 SMESH_subMesh *SMESH_Mesh::GetSubMeshContaining(const TopoDS_Shape & aSubShape) const
1095 int index = _meshDS->ShapeToIndex(aSubShape);
1096 return GetSubMeshContaining( index );
1099 //=============================================================================
1101 * Get the SMESH_subMesh object implementation. Don't create it, return null
1102 * if it does not exist.
1104 //=============================================================================
1106 SMESH_subMesh *SMESH_Mesh::GetSubMeshContaining(const int aShapeID) const
1108 SMESH_subMesh *aSubMesh = _subMeshHolder->Get( aShapeID );
1112 //================================================================================
1114 * \brief Return sub-meshes of groups containing the given sub-shape
1116 //================================================================================
1118 std::list<SMESH_subMesh*>
1119 SMESH_Mesh::GetGroupSubMeshesContaining(const TopoDS_Shape & aSubShape) const
1121 std::list<SMESH_subMesh*> found;
1123 SMESH_subMesh * subMesh = GetSubMeshContaining(aSubShape);
1127 // sub-meshes of groups have max IDs, so search from the map end
1128 SMESH_subMeshIteratorPtr smIt( _subMeshHolder->GetIterator( /*reverse=*/true ) );
1129 while ( smIt->more() ) {
1130 SMESH_subMesh* sm = smIt->next();
1131 SMESHDS_SubMesh * ds = sm->GetSubMeshDS();
1132 if ( ds && ds->IsComplexSubmesh() ) {
1133 if ( SMESH_MesherHelper::IsSubShape( aSubShape, sm->GetSubShape() ))
1135 found.push_back( sm );
1139 break; // the rest sub-meshes are not those of groups
1143 if ( found.empty() ) // maybe the main shape is a COMPOUND (issue 0021530)
1145 if ( SMESH_subMesh * mainSM = GetSubMeshContaining(1) )
1146 if ( mainSM->GetSubShape().ShapeType() == TopAbs_COMPOUND )
1148 TopoDS_Iterator it( mainSM->GetSubShape() );
1149 if ( it.Value().ShapeType() == aSubShape.ShapeType() &&
1150 SMESH_MesherHelper::IsSubShape( aSubShape, mainSM->GetSubShape() ))
1151 found.push_back( mainSM );
1154 else // issue 0023068
1156 if ( SMESH_subMesh * mainSM = GetSubMeshContaining(1) )
1157 if ( mainSM->GetSubShape().ShapeType() == TopAbs_COMPOUND )
1158 found.push_back( mainSM );
1162 //=======================================================================
1163 //function : IsUsedHypothesis
1164 //purpose : Return True if anHyp is used to mesh aSubShape
1165 //=======================================================================
1167 bool SMESH_Mesh::IsUsedHypothesis(SMESHDS_Hypothesis * anHyp,
1168 const SMESH_subMesh* aSubMesh)
1170 SMESH_Hypothesis* hyp = static_cast<SMESH_Hypothesis*>(anHyp);
1172 // check if anHyp can be used to mesh aSubMesh
1173 if ( !aSubMesh || !aSubMesh->IsApplicableHypothesis( hyp ))
1176 SMESH_Algo *algo = aSubMesh->GetAlgo();
1179 if (anHyp->GetType() > SMESHDS_Hypothesis::PARAM_ALGO)
1180 return ( anHyp == algo );
1182 // algorithm parameter
1185 // look through hypotheses used by algo
1186 const SMESH_HypoFilter* hypoKind;
1187 if (( hypoKind = algo->GetCompatibleHypoFilter( !hyp->IsAuxiliary() ))) {
1188 std::list <const SMESHDS_Hypothesis * > usedHyps;
1189 if ( GetHypotheses( aSubMesh, *hypoKind, usedHyps, true ))
1190 return ( find( usedHyps.begin(), usedHyps.end(), anHyp ) != usedHyps.end() );
1197 //=======================================================================
1198 //function : NotifySubMeshesHypothesisModification
1199 //purpose : Say all submeshes using theChangedHyp that it has been modified
1200 //=======================================================================
1202 void SMESH_Mesh::NotifySubMeshesHypothesisModification(const SMESH_Hypothesis* hyp)
1205 if ( !GetMeshDS()->IsUsedHypothesis( hyp ))
1208 smIdType nbEntities = ( _meshDS->NbNodes() + _meshDS->NbElements() );
1209 if ( hyp && _callUp && !_callUp->IsLoaded() ) // for not loaded mesh (#16648)
1211 _callUp->HypothesisModified( hyp->GetID(), /*updateIcons=*/true );
1212 nbEntities = ( _meshDS->NbNodes() + _meshDS->NbElements() ); // after loading mesh
1216 const SMESH_HypoFilter* compatibleHypoKind;
1217 std::list <const SMESHDS_Hypothesis * > usedHyps;
1218 std::vector< SMESH_subMesh* > smToNotify;
1219 bool allMeshedEdgesNotified = true;
1221 SMESH_subMeshIteratorPtr smIt( _subMeshHolder->GetIterator() );
1222 while ( smIt->more() )
1224 SMESH_subMesh* aSubMesh = smIt->next();
1225 bool toNotify = false;
1227 // if aSubMesh meshing depends on hyp,
1228 // we call aSubMesh->AlgoStateEngine( MODIF_HYP, hyp ) that causes either
1229 // 1) clearing already computed aSubMesh or
1230 // 2) changing algo_state from MISSING_HYP to HYP_OK when parameters of hyp becomes valid,
1231 // other possible changes are not interesting. (IPAL0052457 - assigning hyp performance pb)
1232 if ( aSubMesh->GetComputeState() == SMESH_subMesh::COMPUTE_OK ||
1233 aSubMesh->GetComputeState() == SMESH_subMesh::FAILED_TO_COMPUTE ||
1234 aSubMesh->GetAlgoState() == SMESH_subMesh::MISSING_HYP ||
1235 hyp->DataDependOnParams() )
1237 const TopoDS_Shape & aSubShape = aSubMesh->GetSubShape();
1239 if (( aSubMesh->IsApplicableHypothesis( hyp )) &&
1240 ( algo = aSubMesh->GetAlgo() ) &&
1241 ( compatibleHypoKind = algo->GetCompatibleHypoFilter( !hyp->IsAuxiliary() )) &&
1242 ( compatibleHypoKind->IsOk( hyp, aSubShape )))
1244 // check if hyp is used by algo
1246 toNotify = ( GetHypotheses( aSubMesh, *compatibleHypoKind, usedHyps, true ) &&
1247 std::find( usedHyps.begin(), usedHyps.end(), hyp ) != usedHyps.end() );
1252 smToNotify.push_back( aSubMesh );
1253 if ( aSubMesh->GetAlgoState() == SMESH_subMesh::MISSING_HYP )
1254 allMeshedEdgesNotified = false; // update of algo state needed, not mesh clearing
1258 if ( !aSubMesh->IsEmpty() &&
1259 aSubMesh->GetSubShape().ShapeType() == TopAbs_EDGE )
1260 allMeshedEdgesNotified = false;
1263 if ( smToNotify.empty() )
1266 // if all meshed EDGEs will be notified then the notification is equivalent
1267 // to the whole mesh clearing, which is usually faster
1268 if ( allMeshedEdgesNotified && NbNodes() > 0 )
1274 // notify in reverse order to avoid filling the pool of IDs
1275 for ( int i = smToNotify.size()-1; i >= 0; --i )
1276 smToNotify[i]->AlgoStateEngine(SMESH_subMesh::MODIF_HYP,
1277 const_cast< SMESH_Hypothesis*>( hyp ));
1279 HasModificationsToDiscard(); // to reset _isModified flag if mesh becomes empty
1280 GetMeshDS()->Modified();
1282 smIdType newNbEntities = ( _meshDS->NbNodes() + _meshDS->NbElements() );
1283 if ( hyp && _callUp )
1284 _callUp->HypothesisModified( hyp->GetID(), newNbEntities != nbEntities );
1287 //=============================================================================
1289 * Auto color functionality
1291 //=============================================================================
1292 void SMESH_Mesh::SetAutoColor(bool theAutoColor)
1294 _isAutoColor = theAutoColor;
1297 bool SMESH_Mesh::GetAutoColor()
1299 return _isAutoColor;
1302 //=======================================================================
1303 //function : SetIsModified
1304 //purpose : Set the flag meaning that the mesh has been edited "manually"
1305 //=======================================================================
1307 void SMESH_Mesh::SetIsModified(bool isModified)
1309 _isModified = isModified;
1312 // check if mesh becomes empty as result of modification
1313 HasModificationsToDiscard();
1316 //=======================================================================
1317 //function : HasModificationsToDiscard
1318 //purpose : Return true if the mesh has been edited since a total re-compute
1319 // and those modifications may prevent successful partial re-compute.
1320 // As a side effect reset _isModified flag if mesh is empty
1322 //=======================================================================
1324 bool SMESH_Mesh::HasModificationsToDiscard() const
1326 if ( ! _isModified )
1329 // return true if the next Compute() will be partial and
1330 // existing but changed elements may prevent successful re-compute
1331 bool hasComputed = false, hasNotComputed = false;
1332 SMESH_subMeshIteratorPtr smIt( _subMeshHolder->GetIterator() );
1333 while ( smIt->more() )
1335 const SMESH_subMesh* aSubMesh = smIt->next();
1336 switch ( aSubMesh->GetSubShape().ShapeType() )
1341 if ( aSubMesh->IsMeshComputed() )
1344 hasNotComputed = true;
1345 if ( hasComputed && hasNotComputed)
1351 if ( NbNodes() < 1 )
1352 const_cast<SMESH_Mesh*>(this)->_isModified = false;
1357 //=============================================================================
1359 * \brief Return true if all sub-meshes are computed OK - to update an icon
1361 //=============================================================================
1363 bool SMESH_Mesh::IsComputedOK()
1365 if ( NbNodes() == 0 )
1368 // if ( !HasShapeToMesh() )
1371 if ( SMESH_subMesh* mainSM = GetSubMeshContaining( 1 ))
1373 SMESH_subMeshIteratorPtr smIt = mainSM->getDependsOnIterator(/*includeSelf=*/true);
1374 while ( smIt->more() )
1376 const SMESH_subMesh* sm = smIt->next();
1377 if ( !sm->IsAlwaysComputed() )
1378 switch ( sm->GetComputeState() )
1380 case SMESH_subMesh::NOT_READY:
1381 case SMESH_subMesh::COMPUTE_OK:
1383 case SMESH_subMesh::FAILED_TO_COMPUTE:
1384 case SMESH_subMesh::READY_TO_COMPUTE:
1392 //================================================================================
1394 * \brief Check if any groups of the same type have equal names
1396 //================================================================================
1398 bool SMESH_Mesh::HasDuplicatedGroupNamesMED()
1400 // Corrected for Mantis issue 0020028
1401 std::map< SMDSAbs_ElementType, std::set< std::string > > aGroupNames;
1402 for ( std::map<int, SMESH_Group*>::iterator it = _mapGroup.begin(); it != _mapGroup.end(); it++ )
1404 SMESH_Group* aGroup = it->second;
1405 SMDSAbs_ElementType aType = aGroup->GetGroupDS()->GetType();
1406 std::string aGroupName = aGroup->GetName();
1407 aGroupName.resize( MAX_MED_GROUP_NAME_LENGTH );
1408 if ( !aGroupNames[aType].insert(aGroupName).second )
1415 //================================================================================
1417 * \brief Export the mesh to a writer
1419 //================================================================================
1421 void SMESH_Mesh::exportMEDCommmon(DriverMED_W_SMESHDS_Mesh& theWriter,
1422 const char* theMeshName,
1424 const SMESHDS_Mesh* theMeshPart,
1425 bool theAutoDimension,
1426 bool theAddODOnVertices,
1427 double theZTolerance,
1428 bool theSaveNumbers)
1430 Driver_Mesh::Status status = Driver_Mesh::DRS_OK;
1434 theWriter.SetMesh ( theMeshPart ? (SMESHDS_Mesh*) theMeshPart : _meshDS );
1435 theWriter.SetAutoDimension( theAutoDimension );
1436 theWriter.AddODOnVertices ( theAddODOnVertices );
1437 theWriter.SetZTolerance ( theZTolerance );
1438 theWriter.SetSaveNumbers ( theSaveNumbers );
1440 theWriter.SetMeshId ( _id );
1442 theWriter.SetMeshId ( -1 );
1443 theWriter.SetMeshName ( theMeshName );
1446 if ( theAutoGroups ) {
1447 theWriter.AddGroupOfNodes();
1448 theWriter.AddGroupOfEdges();
1449 theWriter.AddGroupOfFaces();
1450 theWriter.AddGroupOfVolumes();
1451 theWriter.AddGroupOf0DElems();
1452 theWriter.AddGroupOfBalls();
1455 // Pass groups to writer. Provide unique group names.
1456 //set<string> aGroupNames; // Corrected for Mantis issue 0020028
1459 std::map< SMDSAbs_ElementType, std::set<std::string> > aGroupNames;
1461 int maxNbIter = 10000; // to guarantee cycle finish
1462 for ( std::map<int, SMESH_Group*>::iterator it = _mapGroup.begin();
1463 it != _mapGroup.end();
1465 SMESH_Group* aGroup = it->second;
1466 SMESHDS_GroupBase* aGroupDS = aGroup->GetGroupDS();
1468 SMDSAbs_ElementType aType = aGroupDS->GetType();
1469 std::string aGroupName0 = aGroup->GetName();
1470 aGroupName0.resize(MAX_MED_GROUP_NAME_LENGTH);
1471 std::string aGroupName = aGroupName0;
1472 for (int i = 1; !aGroupNames[aType].insert(aGroupName).second && i < maxNbIter; i++) {
1473 sprintf(&aString[0], "GR_%d_%s", i, aGroupName0.c_str());
1474 aGroupName = aString;
1475 aGroupName.resize(MAX_MED_GROUP_NAME_LENGTH);
1477 aGroupDS->SetStoreName( aGroupName.c_str() );
1478 theWriter.AddGroup( aGroupDS );
1483 status = theWriter.Perform();
1485 SMESH_CATCH( SMESH::throwSalomeEx );
1487 if ( status == Driver_Mesh::DRS_TOO_LARGE_MESH )
1488 throw TooLargeForExport("MED");
1491 //================================================================================
1493 * Same as SMESH_Mesh::ExportMED except for \a file and \a theVersion
1495 //================================================================================
1497 MEDCoupling::MCAuto<MEDCoupling::DataArrayByte>
1498 SMESH_Mesh::ExportMEDCoupling(const char* theMeshName,
1500 const SMESHDS_Mesh* theMeshPart,
1501 bool theAutoDimension,
1502 bool theAddODOnVertices,
1503 double theZTolerance,
1504 bool theSaveNumbers)
1506 DriverMED_W_SMESHDS_Mesh_Mem writer;
1507 this->exportMEDCommmon( writer, theMeshName, theAutoGroups, theMeshPart, theAutoDimension,
1508 theAddODOnVertices, theZTolerance, theSaveNumbers);
1509 return writer.getData();
1512 //================================================================================
1514 * \brief Export the mesh to a med file
1515 * \param [in] theFile - name of the MED file
1516 * \param [in] theMeshName - name of this mesh
1517 * \param [in] theAutoGroups - boolean parameter for creating/not creating
1518 * the groups Group_On_All_Nodes, Group_On_All_Faces, ... ;
1519 * the typical use is auto_groups=false.
1520 * \param [in] theVersion - define the minor (xy, where version is x.y.z) of MED file format.
1521 * If theVersion is equal to -1, the minor version is not changed (default).
1522 * \param [in] theMeshPart - mesh data to export
1523 * \param [in] theAutoDimension - if \c true, a space dimension of a MED mesh can be either
1524 * - 1D if all mesh nodes lie on OX coordinate axis, or
1525 * - 2D if all mesh nodes lie on XOY coordinate plane, or
1526 * - 3D in the rest cases.
1527 * If \a theAutoDimension is \c false, the space dimension is always 3.
1528 * \param [in] theAddODOnVertices - to create 0D elements on all vertices
1529 * \param [in] theZTolerance - tolerance in Z direction. If Z coordinate of a node is close to zero
1530 * within a given tolerance, the coordinate is set to zero.
1531 * If \a ZTolerance is negative, the node coordinates are kept as is.
1532 * \param [in] theSaveNumbers : enable saving numbers of nodes and cells.
1533 * \return int - mesh index in the file
1535 //================================================================================
1537 void SMESH_Mesh::ExportMED(const char * theFile,
1538 const char* theMeshName,
1541 const SMESHDS_Mesh* theMeshPart,
1542 bool theAutoDimension,
1543 bool theAddODOnVertices,
1544 double theZTolerance,
1545 bool theSaveNumbers)
1547 MESSAGE("MED_VERSION:"<< theVersion);
1548 DriverMED_W_SMESHDS_Mesh writer;
1549 writer.SetFile( theFile, theVersion );
1550 this->exportMEDCommmon( writer, theMeshName, theAutoGroups, theMeshPart, theAutoDimension, theAddODOnVertices, theZTolerance, theSaveNumbers );
1553 //================================================================================
1555 * \brief Export the mesh to a DAT file
1557 //================================================================================
1559 void SMESH_Mesh::ExportDAT(const char * file,
1560 const SMESHDS_Mesh* meshPart,
1561 const bool renumber)
1563 Driver_Mesh::Status status;
1566 DriverDAT_W_SMDS_Mesh writer;
1567 writer.SetFile( file );
1568 writer.SetMesh( meshPart ? (SMESHDS_Mesh*) meshPart : _meshDS );
1569 writer.SetMeshId(_id);
1570 writer.SetRenumber( renumber );
1571 status = writer.Perform();
1573 SMESH_CATCH( SMESH::throwSalomeEx );
1575 if ( status == Driver_Mesh::DRS_TOO_LARGE_MESH )
1576 throw TooLargeForExport("DAT");
1579 //================================================================================
1581 * \brief Export the mesh to an UNV file
1583 //================================================================================
1585 void SMESH_Mesh::ExportUNV(const char * file,
1586 const SMESHDS_Mesh* meshPart,
1587 const bool renumber)
1589 Driver_Mesh::Status status;
1592 DriverUNV_W_SMDS_Mesh writer;
1593 writer.SetFile( file );
1594 writer.SetMesh( meshPart ? (SMESHDS_Mesh*) meshPart : _meshDS );
1595 writer.SetMeshId(_id);
1596 writer.SetRenumber( renumber );
1598 // pass group names to SMESHDS
1601 std::map<int, SMESH_Group*>::iterator it = _mapGroup.begin();
1602 for ( ; it != _mapGroup.end(); it++ ) {
1603 SMESH_Group* aGroup = it->second;
1604 SMESHDS_GroupBase* aGroupDS = aGroup->GetGroupDS();
1606 std::string aGroupName = aGroup->GetName();
1607 aGroupDS->SetStoreName( aGroupName.c_str() );
1608 writer.AddGroup( aGroupDS );
1612 status = writer.Perform();
1614 SMESH_CATCH( SMESH::throwSalomeEx );
1616 if ( status == Driver_Mesh::DRS_TOO_LARGE_MESH )
1617 throw TooLargeForExport("UNV");
1620 //================================================================================
1622 * \brief Export the mesh to an STL file
1624 //================================================================================
1626 void SMESH_Mesh::ExportSTL(const char * file,
1629 const SMESHDS_Mesh* meshPart)
1631 Driver_Mesh::Status status;
1634 DriverSTL_W_SMDS_Mesh writer;
1635 writer.SetFile( file );
1636 writer.SetIsAscii( isascii );
1637 writer.SetMesh( meshPart ? (SMESHDS_Mesh*) meshPart : _meshDS);
1638 writer.SetMeshId(_id);
1639 if ( name ) writer.SetName( name );
1640 status = writer.Perform();
1642 SMESH_CATCH( SMESH::throwSalomeEx );
1644 if ( status == Driver_Mesh::DRS_TOO_LARGE_MESH )
1645 throw TooLargeForExport("STL");
1648 //================================================================================
1650 * \brief Export the mesh to the CGNS file
1652 //================================================================================
1654 void SMESH_Mesh::ExportCGNS(const char * file,
1655 const SMESHDS_Mesh* meshDS,
1656 const char * meshName,
1657 const bool groupElemsByType)
1660 int res = Driver_Mesh::DRS_FAIL;
1663 // pass group names to SMESHDS
1664 std::map<int, SMESH_Group*>::iterator it = _mapGroup.begin();
1665 for ( ; it != _mapGroup.end(); it++ ) {
1666 SMESH_Group* group = it->second;
1667 SMESHDS_GroupBase* groupDS = group->GetGroupDS();
1669 std::string groupName = group->GetName();
1670 groupDS->SetStoreName( groupName.c_str() );
1675 DriverCGNS_Write writer;
1676 writer.SetFile( file );
1677 writer.SetMesh( const_cast<SMESHDS_Mesh*>( meshDS ));
1678 writer.SetMeshName( SMESH_Comment("Mesh_") << meshDS->GetPersistentId());
1679 if ( meshName && meshName[0] )
1680 writer.SetMeshName( meshName );
1681 writer.SetElementsByType( groupElemsByType );
1682 res = writer.Perform();
1683 if ( res != Driver_Mesh::DRS_OK )
1685 SMESH_ComputeErrorPtr err = writer.GetError();
1686 if ( err && !err->IsOK() && !err->myComment.empty() )
1687 throw SALOME_Exception(("Export failed: " + err->myComment ).c_str() );
1691 SMESH_CATCH( SMESH::throwSalomeEx );
1693 if ( res == Driver_Mesh::DRS_TOO_LARGE_MESH )
1694 throw TooLargeForExport("CGNS");
1696 if ( res != Driver_Mesh::DRS_OK )
1697 throw SALOME_Exception("Export failed");
1700 //================================================================================
1702 * \brief Export the mesh to a GMF file
1704 //================================================================================
1706 void SMESH_Mesh::ExportGMF(const char * file,
1707 const SMESHDS_Mesh* meshDS,
1708 bool withRequiredGroups)
1710 Driver_Mesh::Status status;
1713 DriverGMF_Write writer;
1714 writer.SetFile( file );
1715 writer.SetMesh( const_cast<SMESHDS_Mesh*>( meshDS ));
1716 writer.SetExportRequiredGroups( withRequiredGroups );
1718 status = writer.Perform();
1720 SMESH_CATCH( SMESH::throwSalomeEx );
1722 if ( status == Driver_Mesh::DRS_TOO_LARGE_MESH )
1723 throw TooLargeForExport("GMF");
1726 //================================================================================
1728 * \brief Return a ratio of "compute cost" of computed sub-meshes to the whole
1731 //================================================================================
1733 double SMESH_Mesh::GetComputeProgress() const
1735 double totalCost = 1e-100, computedCost = 0;
1736 const SMESH_subMesh* curSM = _gen->GetCurrentSubMesh();
1738 // get progress of a current algo
1739 TColStd_MapOfInteger currentSubIds;
1741 if ( SMESH_Algo* algo = curSM->GetAlgo() )
1743 int algoNotDoneCost = 0, algoDoneCost = 0;
1744 const std::vector<SMESH_subMesh*>& smToCompute = algo->SubMeshesToCompute();
1745 for ( size_t i = 0; i < smToCompute.size(); ++i )
1747 if ( smToCompute[i]->IsEmpty() || smToCompute.size() == 1 )
1748 algoNotDoneCost += smToCompute[i]->GetComputeCost();
1750 algoDoneCost += smToCompute[i]->GetComputeCost();
1751 currentSubIds.Add( smToCompute[i]->GetId() );
1757 rate = algo->GetProgress();
1761 std::cerr << "Exception in " << algo->GetName() << "::GetProgress()" << std::endl;
1764 if ( 0. < rate && rate < 1.001 )
1766 computedCost += rate * ( algoDoneCost + algoNotDoneCost );
1770 rate = algo->GetProgressByTic();
1771 computedCost += algoDoneCost + rate * algoNotDoneCost;
1773 // cout << "rate: "<<rate << " algoNotDoneCost: " << algoNotDoneCost << endl;
1776 // get cost of already treated sub-meshes
1777 if ( SMESH_subMesh* mainSM = GetSubMeshContaining( 1 ))
1779 SMESH_subMeshIteratorPtr smIt = mainSM->getDependsOnIterator(/*includeSelf=*/true);
1780 while ( smIt->more() )
1782 const SMESH_subMesh* sm = smIt->next();
1783 const int smCost = sm->GetComputeCost();
1784 totalCost += smCost;
1785 if ( !currentSubIds.Contains( sm->GetId() ) )
1787 if (( !sm->IsEmpty() ) ||
1788 ( sm->GetComputeState() == SMESH_subMesh::FAILED_TO_COMPUTE &&
1789 !sm->DependsOn( curSM ) ))
1790 computedCost += smCost;
1794 // cout << "Total: " << totalCost
1795 // << " computed: " << computedCost << " progress: " << computedCost / totalCost
1796 // << " nbElems: " << GetMeshDS()->GetMeshInfo().NbElements() << endl;
1797 return computedCost / totalCost;
1800 //================================================================================
1802 * \brief Return number of nodes in the mesh
1804 //================================================================================
1806 smIdType SMESH_Mesh::NbNodes() const
1808 return _meshDS->NbNodes();
1811 //================================================================================
1813 * \brief Return number of edges of given order in the mesh
1815 //================================================================================
1817 smIdType SMESH_Mesh::Nb0DElements() const
1819 return _meshDS->GetMeshInfo().Nb0DElements();
1822 //================================================================================
1824 * \brief Return number of edges of given order in the mesh
1826 //================================================================================
1828 smIdType SMESH_Mesh::NbEdges(SMDSAbs_ElementOrder order) const
1830 return _meshDS->GetMeshInfo().NbEdges(order);
1833 //================================================================================
1835 * \brief Return number of faces of given order in the mesh
1837 //================================================================================
1839 smIdType SMESH_Mesh::NbFaces(SMDSAbs_ElementOrder order) const
1841 return _meshDS->GetMeshInfo().NbFaces(order);
1844 //================================================================================
1846 * \brief Return the number of faces in the mesh
1848 //================================================================================
1850 smIdType SMESH_Mesh::NbTriangles(SMDSAbs_ElementOrder order) const
1852 return _meshDS->GetMeshInfo().NbTriangles(order);
1855 //================================================================================
1857 * \brief Return number of biquadratic triangles in the mesh
1859 //================================================================================
1861 smIdType SMESH_Mesh::NbBiQuadTriangles() const
1863 return _meshDS->GetMeshInfo().NbBiQuadTriangles();
1866 //================================================================================
1868 * \brief Return the number nodes faces in the mesh
1870 //================================================================================
1872 smIdType SMESH_Mesh::NbQuadrangles(SMDSAbs_ElementOrder order) const
1874 return _meshDS->GetMeshInfo().NbQuadrangles(order);
1877 //================================================================================
1879 * \brief Return number of biquadratic quadrangles in the mesh
1881 //================================================================================
1883 smIdType SMESH_Mesh::NbBiQuadQuadrangles() const
1885 return _meshDS->GetMeshInfo().NbBiQuadQuadrangles();
1888 //================================================================================
1890 * \brief Return the number of polygonal faces in the mesh
1892 //================================================================================
1894 smIdType SMESH_Mesh::NbPolygons(SMDSAbs_ElementOrder order) const
1896 return _meshDS->GetMeshInfo().NbPolygons(order);
1899 //================================================================================
1901 * \brief Return number of volumes of given order in the mesh
1903 //================================================================================
1905 smIdType SMESH_Mesh::NbVolumes(SMDSAbs_ElementOrder order) const
1907 return _meshDS->GetMeshInfo().NbVolumes(order);
1910 //================================================================================
1912 * \brief Return number of tetrahedrons of given order in the mesh
1914 //================================================================================
1916 smIdType SMESH_Mesh::NbTetras(SMDSAbs_ElementOrder order) const
1918 return _meshDS->GetMeshInfo().NbTetras(order);
1921 //================================================================================
1923 * \brief Return number of hexahedrons of given order in the mesh
1925 //================================================================================
1927 smIdType SMESH_Mesh::NbHexas(SMDSAbs_ElementOrder order) const
1929 return _meshDS->GetMeshInfo().NbHexas(order);
1932 //================================================================================
1934 * \brief Return number of triquadratic hexahedrons in the mesh
1936 //================================================================================
1938 smIdType SMESH_Mesh::NbTriQuadraticHexas() const
1940 return _meshDS->GetMeshInfo().NbTriQuadHexas();
1943 //================================================================================
1945 * \brief Return number of pyramids of given order in the mesh
1947 //================================================================================
1949 smIdType SMESH_Mesh::NbPyramids(SMDSAbs_ElementOrder order) const
1951 return _meshDS->GetMeshInfo().NbPyramids(order);
1954 //================================================================================
1956 * \brief Return number of prisms (penthahedrons) of given order in the mesh
1958 //================================================================================
1960 smIdType SMESH_Mesh::NbPrisms(SMDSAbs_ElementOrder order) const
1962 return _meshDS->GetMeshInfo().NbPrisms(order);
1965 smIdType SMESH_Mesh::NbQuadPrisms() const
1967 return _meshDS->GetMeshInfo().NbQuadPrisms();
1970 smIdType SMESH_Mesh::NbBiQuadPrisms() const
1972 return _meshDS->GetMeshInfo().NbBiQuadPrisms();
1976 //================================================================================
1978 * \brief Return number of hexagonal prisms in the mesh
1980 //================================================================================
1982 smIdType SMESH_Mesh::NbHexagonalPrisms() const
1984 return _meshDS->GetMeshInfo().NbHexPrisms();
1987 //================================================================================
1989 * \brief Return number of polyhedrons in the mesh
1991 //================================================================================
1993 smIdType SMESH_Mesh::NbPolyhedrons() const
1995 return _meshDS->GetMeshInfo().NbPolyhedrons();
1998 //================================================================================
2000 * \brief Return number of ball elements in the mesh
2002 //================================================================================
2004 smIdType SMESH_Mesh::NbBalls() const
2006 return _meshDS->GetMeshInfo().NbBalls();
2009 //================================================================================
2011 * \brief Return number of submeshes in the mesh
2013 //================================================================================
2015 smIdType SMESH_Mesh::NbSubMesh() const
2017 return _meshDS->NbSubMesh();
2020 //================================================================================
2022 * \brief Returns number of meshes in the Study, that is supposed to be
2023 * equal to SMESHDS_Document::NbMeshes()
2025 //================================================================================
2027 int SMESH_Mesh::NbMeshes() const // nb meshes in the Study
2029 return _document->NbMeshes();
2032 //=======================================================================
2033 //function : IsNotConformAllowed
2034 //purpose : check if a hypothesis allowing notconform mesh is present
2035 //=======================================================================
2037 bool SMESH_Mesh::IsNotConformAllowed() const
2039 if(MYDEBUG) MESSAGE("SMESH_Mesh::IsNotConformAllowed");
2041 static SMESH_HypoFilter filter( SMESH_HypoFilter::HasName( "NotConformAllowed" ));
2042 return GetHypothesis( _meshDS->ShapeToMesh(), filter, false );
2045 //=======================================================================
2046 //function : IsMainShape
2048 //=======================================================================
2050 bool SMESH_Mesh::IsMainShape(const TopoDS_Shape& theShape) const
2052 return theShape.IsSame(_meshDS->ShapeToMesh() );
2055 //=======================================================================
2056 //function : GetShapeByEntry
2057 //purpose : return TopoDS_Shape by its study entry
2058 //=======================================================================
2060 TopoDS_Shape SMESH_Mesh::GetShapeByEntry(const std::string& entry) const
2062 return _callUp ? _callUp->GetShapeByEntry( entry ) : TopoDS_Shape();
2065 //=============================================================================
2069 //=============================================================================
2071 SMESH_Group* SMESH_Mesh::AddGroup (const SMDSAbs_ElementType theType,
2072 const char* theName,
2074 const TopoDS_Shape& theShape,
2075 const SMESH_PredicatePtr& thePredicate)
2077 if ( _mapGroup.count( theId ))
2079 int id = ( theId < 0 ) ? _groupId : theId;
2080 SMESH_Group* aGroup = new SMESH_Group ( id, this, theType, theName, theShape, thePredicate );
2081 GetMeshDS()->AddGroup( aGroup->GetGroupDS() );
2082 _mapGroup[ id ] = aGroup;
2083 _groupId = 1 + _mapGroup.rbegin()->first;
2087 //================================================================================
2089 * \brief Creates a group based on an existing SMESHDS group. Group ID should be unique
2091 //================================================================================
2093 SMESH_Group* SMESH_Mesh::AddGroup (SMESHDS_GroupBase* groupDS)
2096 throw SALOME_Exception(LOCALIZED ("SMESH_Mesh::AddGroup(): NULL SMESHDS_GroupBase"));
2098 std::map <int, SMESH_Group*>::iterator i_g = _mapGroup.find( groupDS->GetID() );
2099 if ( i_g != _mapGroup.end() && i_g->second )
2101 if ( i_g->second->GetGroupDS() == groupDS )
2104 throw SALOME_Exception(LOCALIZED ("SMESH_Mesh::AddGroup() wrong ID of SMESHDS_GroupBase"));
2106 SMESH_Group* aGroup = new SMESH_Group (groupDS);
2107 _mapGroup[ groupDS->GetID() ] = aGroup;
2108 GetMeshDS()->AddGroup( aGroup->GetGroupDS() );
2110 _groupId = 1 + _mapGroup.rbegin()->first;
2116 //================================================================================
2118 * \brief Creates SMESH_Groups for not wrapped SMESHDS_Groups
2119 * \retval bool - true if new SMESH_Groups have been created
2122 //================================================================================
2124 bool SMESH_Mesh::SynchronizeGroups()
2126 const size_t nbGroups = _mapGroup.size();
2127 const std::set<SMESHDS_GroupBase*>& groups = _meshDS->GetGroups();
2128 std::set<SMESHDS_GroupBase*>::const_iterator gIt = groups.begin();
2129 for ( ; gIt != groups.end(); ++gIt )
2131 SMESHDS_GroupBase* groupDS = (SMESHDS_GroupBase*) *gIt;
2132 _groupId = groupDS->GetID();
2133 if ( !_mapGroup.count( _groupId ))
2134 _mapGroup[_groupId] = new SMESH_Group( groupDS );
2136 if ( !_mapGroup.empty() )
2137 _groupId = 1 + _mapGroup.rbegin()->first;
2139 return nbGroups < _mapGroup.size();
2142 //================================================================================
2144 * \brief Return iterator on all existing groups
2146 //================================================================================
2148 SMESH_Mesh::GroupIteratorPtr SMESH_Mesh::GetGroups() const
2150 typedef std::map <int, SMESH_Group *> TMap;
2151 return GroupIteratorPtr( new SMDS_mapIterator<TMap>( _mapGroup ));
2154 //=============================================================================
2156 * \brief Return a group by ID
2158 //=============================================================================
2160 SMESH_Group* SMESH_Mesh::GetGroup (const int theGroupID) const
2162 std::map <int, SMESH_Group*>::const_iterator id_grp = _mapGroup.find( theGroupID );
2163 if ( id_grp == _mapGroup.end() )
2165 return id_grp->second;
2169 //=============================================================================
2171 * \brief Return IDs of all groups
2173 //=============================================================================
2175 std::list<int> SMESH_Mesh::GetGroupIds() const
2177 std::list<int> anIds;
2178 std::map<int, SMESH_Group*>::const_iterator it = _mapGroup.begin();
2179 for ( ; it != _mapGroup.end(); it++ )
2180 anIds.push_back( it->first );
2185 //================================================================================
2187 * \brief Set a caller of methods at level of CORBA API implementation.
2188 * The set upCaller will be deleted by SMESH_Mesh
2190 //================================================================================
2192 void SMESH_Mesh::SetCallUp( TCallUp* upCaller )
2194 if ( _callUp ) delete _callUp;
2198 //=============================================================================
2202 //=============================================================================
2204 bool SMESH_Mesh::RemoveGroup( const int theGroupID )
2206 if (_mapGroup.find(theGroupID) == _mapGroup.end())
2208 GetMeshDS()->RemoveGroup( _mapGroup[theGroupID]->GetGroupDS() );
2209 delete _mapGroup[theGroupID];
2210 _mapGroup.erase (theGroupID);
2212 _callUp->RemoveGroup( theGroupID );
2216 //=======================================================================
2217 //function : GetAncestors
2218 //purpose : return list of ancestors of theSubShape in the order
2219 // that lower dimension shapes come first.
2220 //=======================================================================
2222 const TopTools_ListOfShape& SMESH_Mesh::GetAncestors(const TopoDS_Shape& theS) const
2224 if ( _mapAncestors.Contains( theS ) )
2225 return _mapAncestors.FindFromKey( theS );
2227 static TopTools_ListOfShape emptyList;
2231 //=======================================================================
2233 //purpose : dumps contents of mesh to stream [ debug purposes ]
2234 //=======================================================================
2236 ostream& SMESH_Mesh::Dump(ostream& save)
2239 save << "========================== Dump contents of mesh ==========================" << endl << endl;
2240 save << ++clause << ") Total number of nodes: \t" << NbNodes() << endl;
2241 save << ++clause << ") Total number of edges: \t" << NbEdges() << endl;
2242 save << ++clause << ") Total number of faces: \t" << NbFaces() << endl;
2243 save << ++clause << ") Total number of polygons: \t" << NbPolygons() << endl;
2244 save << ++clause << ") Total number of volumes: \t" << NbVolumes() << endl;
2245 save << ++clause << ") Total number of polyhedrons:\t" << NbPolyhedrons() << endl << endl;
2246 for ( int isQuadratic = 0; isQuadratic < 2; ++isQuadratic )
2248 std::string orderStr = isQuadratic ? "quadratic" : "linear";
2249 SMDSAbs_ElementOrder order = isQuadratic ? ORDER_QUADRATIC : ORDER_LINEAR;
2251 save << ++clause << ") Total number of " << orderStr << " edges:\t" << NbEdges(order) << endl;
2252 save << ++clause << ") Total number of " << orderStr << " faces:\t" << NbFaces(order) << endl;
2253 if ( NbFaces(order) > 0 ) {
2254 smIdType nb3 = NbTriangles(order);
2255 smIdType nb4 = NbQuadrangles(order);
2256 save << clause << ".1) Number of " << orderStr << " triangles: \t" << nb3 << endl;
2257 save << clause << ".2) Number of " << orderStr << " quadrangles:\t" << nb4 << endl;
2258 if ( nb3 + nb4 != NbFaces(order) ) {
2259 std::map<int,int> myFaceMap;
2260 SMDS_FaceIteratorPtr itFaces=_meshDS->facesIterator();
2261 while( itFaces->more( ) ) {
2262 int nbNodes = itFaces->next()->NbNodes();
2263 if ( myFaceMap.find( nbNodes ) == myFaceMap.end() )
2264 myFaceMap[ nbNodes ] = 0;
2265 myFaceMap[ nbNodes ] = myFaceMap[ nbNodes ] + 1;
2267 save << clause << ".3) Faces in detail: " << endl;
2268 std::map <int,int>::iterator itF;
2269 for (itF = myFaceMap.begin(); itF != myFaceMap.end(); itF++)
2270 save << "--> nb nodes: " << itF->first << " - nb elements:\t" << itF->second << endl;
2273 save << ++clause << ") Total number of " << orderStr << " volumes:\t" << NbVolumes(order) << endl;
2274 if ( NbVolumes(order) > 0 ) {
2275 smIdType nb8 = NbHexas(order);
2276 smIdType nb4 = NbTetras(order);
2277 smIdType nb5 = NbPyramids(order);
2278 smIdType nb6 = NbPrisms(order);
2279 save << clause << ".1) Number of " << orderStr << " hexahedrons: \t" << nb8 << endl;
2280 save << clause << ".2) Number of " << orderStr << " tetrahedrons:\t" << nb4 << endl;
2281 save << clause << ".3) Number of " << orderStr << " prisms: \t" << nb6 << endl;
2282 save << clause << ".4) Number of " << orderStr << " pyramids: \t" << nb5 << endl;
2283 if ( nb8 + nb4 + nb5 + nb6 != NbVolumes(order) ) {
2284 std::map<int,int> myVolumesMap;
2285 SMDS_VolumeIteratorPtr itVolumes=_meshDS->volumesIterator();
2286 while( itVolumes->more( ) ) {
2287 int nbNodes = itVolumes->next()->NbNodes();
2288 if ( myVolumesMap.find( nbNodes ) == myVolumesMap.end() )
2289 myVolumesMap[ nbNodes ] = 0;
2290 myVolumesMap[ nbNodes ] = myVolumesMap[ nbNodes ] + 1;
2292 save << clause << ".5) Volumes in detail: " << endl;
2293 std::map <int,int>::iterator itV;
2294 for (itV = myVolumesMap.begin(); itV != myVolumesMap.end(); itV++)
2295 save << "--> nb nodes: " << itV->first << " - nb elements:\t" << itV->second << endl;
2300 save << "===========================================================================" << endl;
2304 //=======================================================================
2305 //function : GetElementType
2306 //purpose : Returns type of mesh element with certain id
2307 //=======================================================================
2309 SMDSAbs_ElementType SMESH_Mesh::GetElementType( const smIdType id, const bool iselem )
2311 return _meshDS->GetElementType( id, iselem );
2314 //=============================================================================
2316 * \brief Convert group on geometry into standalone group
2318 //=============================================================================
2320 SMESH_Group* SMESH_Mesh::ConvertToStandalone ( int theGroupID )
2322 SMESH_Group* aGroup = 0;
2323 std::map < int, SMESH_Group * >::iterator itg = _mapGroup.find( theGroupID );
2324 if ( itg == _mapGroup.end() )
2327 SMESH_Group* anOldGrp = (*itg).second;
2328 if ( !anOldGrp || !anOldGrp->GetGroupDS() )
2330 SMESHDS_GroupBase* anOldGrpDS = anOldGrp->GetGroupDS();
2332 // create new standalone group
2333 aGroup = new SMESH_Group (theGroupID, this, anOldGrpDS->GetType(), anOldGrp->GetName() );
2334 _mapGroup[theGroupID] = aGroup;
2336 SMESHDS_Group* aNewGrpDS = dynamic_cast<SMESHDS_Group*>( aGroup->GetGroupDS() );
2337 GetMeshDS()->RemoveGroup( anOldGrpDS );
2338 GetMeshDS()->AddGroup( aNewGrpDS );
2340 // add elements (or nodes) into new created group
2341 SMDS_ElemIteratorPtr anItr = anOldGrpDS->GetElements();
2342 while ( anItr->more() )
2343 aNewGrpDS->Add( (anItr->next())->GetID() );
2346 aNewGrpDS->SetColor( anOldGrpDS->GetColor() );
2354 //=============================================================================
2356 * \brief remove submesh order from Mesh
2358 //=============================================================================
2360 void SMESH_Mesh::ClearMeshOrder()
2362 _subMeshOrder.clear();
2365 //=============================================================================
2367 * \brief remove submesh order from Mesh
2369 //=============================================================================
2371 void SMESH_Mesh::SetMeshOrder(const TListOfListOfInt& theOrder )
2373 _subMeshOrder = theOrder;
2376 //=============================================================================
2378 * \brief return submesh order if any
2380 //=============================================================================
2382 const TListOfListOfInt& SMESH_Mesh::GetMeshOrder() const
2384 return _subMeshOrder;
2387 //=============================================================================
2389 * \brief fill _mapAncestors
2391 //=============================================================================
2393 void SMESH_Mesh::fillAncestorsMap(const TopoDS_Shape& theShape)
2395 int desType, ancType;
2396 if ( !theShape.IsSame( GetShapeToMesh()) && theShape.ShapeType() == TopAbs_COMPOUND )
2398 // a geom group is added. Insert it into lists of ancestors before
2399 // the first ancestor more complex than group members
2400 TopoDS_Iterator subIt( theShape );
2401 if ( !subIt.More() ) return;
2402 int memberType = subIt.Value().ShapeType();
2403 for ( desType = TopAbs_VERTEX; desType >= memberType; desType-- )
2404 for (TopExp_Explorer des( theShape, TopAbs_ShapeEnum( desType )); des.More(); des.Next())
2406 if ( !_mapAncestors.Contains( des.Current() )) continue;// issue 0020982
2407 TopTools_ListOfShape& ancList = _mapAncestors.ChangeFromKey( des.Current() );
2408 TopTools_ListIteratorOfListOfShape ancIt (ancList);
2409 while ( ancIt.More() && ancIt.Value().ShapeType() >= memberType )
2411 if ( ancIt.More() ) ancList.InsertBefore( theShape, ancIt );
2412 else ancList.Append( theShape );
2415 else // else added for 52457: Addition of hypotheses is 8 time longer than meshing
2417 for ( desType = TopAbs_VERTEX; desType > TopAbs_COMPOUND; desType-- )
2418 for ( ancType = desType - 1; ancType >= TopAbs_COMPOUND; ancType-- )
2419 TopExp::MapShapesAndAncestors ( theShape,
2420 (TopAbs_ShapeEnum) desType,
2421 (TopAbs_ShapeEnum) ancType,
2424 // visit COMPOUNDs inside a COMPOUND that are not reachable by TopExp_Explorer
2425 if ( theShape.ShapeType() == TopAbs_COMPOUND )
2427 TopoDS_Iterator sIt(theShape);
2428 if ( sIt.More() && sIt.Value().ShapeType() == TopAbs_COMPOUND )
2429 for ( ; sIt.More(); sIt.Next() )
2430 if ( sIt.Value().ShapeType() == TopAbs_COMPOUND )
2431 fillAncestorsMap( sIt.Value() );
2435 //=============================================================================
2437 * \brief sort submeshes according to stored mesh order
2438 * \param theListToSort in out list to be sorted
2439 * \return FALSE if nothing sorted
2441 //=============================================================================
2443 bool SMESH_Mesh::SortByMeshOrder(std::vector<SMESH_subMesh*>& theListToSort) const
2445 if ( _subMeshOrder.empty() || theListToSort.size() < 2 )
2449 // collect all ordered sub-meshes in smVec as pointers
2450 // and get their positions within theListToSort
2452 std::vector<SMESH_subMesh*> smVec;
2453 typedef std::vector<SMESH_subMesh*>::iterator TPosInList;
2454 std::map< size_t, size_t > sortedPos; // index in theListToSort to order
2455 TPosInList smBeg = theListToSort.begin(), smEnd = theListToSort.end();
2456 TListOfListOfInt::const_iterator listIdsIt = _subMeshOrder.begin();
2457 bool needSort = false;
2458 for( ; listIdsIt != _subMeshOrder.end(); listIdsIt++)
2460 const TListOfInt& listOfId = *listIdsIt;
2461 // convert sm ids to sm's
2463 TListOfInt::const_iterator idIt = listOfId.begin();
2464 for ( ; idIt != listOfId.end(); idIt++ )
2466 if ( SMESH_subMesh * sm = GetSubMeshContaining( *idIt ))
2468 smVec.push_back( sm );
2469 if ( sm->GetSubMeshDS() && sm->GetSubMeshDS()->IsComplexSubmesh() )
2471 smVec.reserve( smVec.size() + sm->GetSubMeshDS()->NbSubMeshes() );
2472 SMESHDS_SubMeshIteratorPtr smdsIt = sm->GetSubMeshDS()->GetSubMeshIterator();
2473 while ( smdsIt->more() )
2475 const SMESHDS_SubMesh* smDS = smdsIt->next();
2476 if (( sm = GetSubMeshContaining( smDS->GetID() )))
2477 smVec.push_back( sm );
2482 // find smVec items in theListToSort
2483 for ( size_t i = 0; i < smVec.size(); ++i )
2485 TPosInList smPos = find( smBeg, smEnd, smVec[i] ); // position in theListToSort
2486 if ( smPos != smEnd )
2488 size_t posInList = std::distance( smBeg, smPos );
2489 size_t order = sortedPos.size();
2490 sortedPos.insert( std::make_pair( posInList, order ));
2491 if ( posInList != order )
2499 // set sm of sortedPos from theListToSort to front of orderedSM
2500 // and the rest of theListToSort to orderedSM end
2502 std::vector<SMESH_subMesh*> orderedSM;
2503 orderedSM.reserve( theListToSort.size() );
2504 orderedSM.resize( sortedPos.size() );
2507 sortedPos.insert( std::make_pair( theListToSort.size(), sortedPos.size() ));
2508 for ( const auto & pos_order : sortedPos )
2510 const size_t& posInList = pos_order.first;
2511 const size_t& order = pos_order.second;
2512 if ( order < sortedPos.size() - 1 )
2513 orderedSM[ order ] = theListToSort[ posInList ];
2515 if ( iPrev < posInList )
2516 orderedSM.insert( orderedSM.end(),
2517 theListToSort.begin() + iPrev,
2518 theListToSort.begin() + posInList );
2519 iPrev = posInList + 1;
2522 theListToSort.swap( orderedSM );
2527 //================================================================================
2529 * \brief Return true if given order of sub-meshes is OK
2531 //================================================================================
2533 bool SMESH_Mesh::IsOrderOK( const SMESH_subMesh* smBefore,
2534 const SMESH_subMesh* smAfter ) const
2536 TListOfListOfInt::const_iterator listIdsIt = _subMeshOrder.begin();
2537 for( ; listIdsIt != _subMeshOrder.end(); listIdsIt++)
2539 const TListOfInt& listOfId = *listIdsIt;
2540 int iB = -1, iA = -1, i = 0;
2541 for ( TListOfInt::const_iterator id = listOfId.begin(); id != listOfId.end(); ++id, ++i )
2543 if ( *id == smBefore->GetId() )
2549 else if ( *id == smAfter->GetId() )
2557 return true; // no order imposed to given sub-meshes
2560 //=============================================================================
2562 * \brief sort submeshes according to stored mesh order
2563 * \param theListToSort in out list to be sorted
2564 * \return FALSE if nothing sorted
2566 //=============================================================================
2568 void SMESH_Mesh::getAncestorsSubMeshes (const TopoDS_Shape& theSubShape,
2569 std::vector< SMESH_subMesh* >& theSubMeshes) const
2571 theSubMeshes.clear();
2572 TopTools_ListIteratorOfListOfShape it( GetAncestors( theSubShape ));
2573 for (; it.More(); it.Next() )
2574 if ( SMESH_subMesh* sm = GetSubMeshContaining( it.Value() ))
2575 theSubMeshes.push_back(sm);
2577 // sort submeshes according to stored mesh order
2578 SortByMeshOrder( theSubMeshes );