1 // Copyright (C) 2007-2020 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 // maximum stored group name length in MED file
84 #define MAX_MED_GROUP_NAME_LENGTH 80
87 static int MYDEBUG = 0;
89 static int MYDEBUG = 0;
92 #define cSMESH_Hyp(h) static_cast<const SMESH_Hypothesis*>(h)
94 typedef SMESH_HypoFilter THypType;
96 class SMESH_Mesh::SubMeshHolder : public SMESHDS_TSubMeshHolder< SMESH_subMesh >
100 //=============================================================================
104 //=============================================================================
106 SMESH_Mesh::SMESH_Mesh(int theLocalId,
108 bool theIsEmbeddedMode,
109 SMESHDS_Document* theDocument):
110 _groupId( 0 ), _nbSubShapes( 0 )
112 if(MYDEBUG) MESSAGE("SMESH_Mesh::SMESH_Mesh(int localId)");
115 _myDocument = theDocument;
116 _myMeshDS = theDocument->NewMesh(theIsEmbeddedMode,theLocalId);
117 _isShapeToMesh = false;
118 _isAutoColor = false;
120 _shapeDiagonal = 0.0;
122 _myMeshDS->ShapeToMesh( PseudoShape() );
123 _subMeshHolder = new SubMeshHolder;
126 //================================================================================
128 * \brief Constructor of SMESH_Mesh being a base of some descendant class
130 //================================================================================
132 SMESH_Mesh::SMESH_Mesh():
136 _isShapeToMesh( false ),
140 _isAutoColor( false ),
141 _isModified( false ),
142 _shapeDiagonal( 0.0 ),
145 _subMeshHolder = new SubMeshHolder;
151 void deleteMeshDS(SMESHDS_Mesh* meshDS)
153 //cout << "deleteMeshDS( " << meshDS << endl;
157 static void* deleteMeshDS(void* meshDS)
159 //cout << "deleteMeshDS( " << meshDS << endl;
160 SMESHDS_Mesh* m = (SMESHDS_Mesh*)meshDS;
169 //=============================================================================
173 //=============================================================================
175 SMESH_Mesh::~SMESH_Mesh()
177 if(MYDEBUG) MESSAGE("SMESH_Mesh::~SMESH_Mesh");
179 if ( _myDocument ) // avoid destructing _myMeshDS from ~SMESH_Gen()
180 _myDocument->RemoveMesh( _id );
183 // remove self from studyContext
186 StudyContextStruct * studyContext = _gen->GetStudyContext();
187 studyContext->mapMesh.erase( _id );
190 _myMeshDS->ClearMesh();
192 // issue 0020340: EDF 1022 SMESH : Crash with FindNodeClosestTo in a second new study
193 // Notify event listeners at least that something happens
194 if ( SMESH_subMesh * sm = GetSubMeshContaining(1))
195 sm->ComputeStateEngine( SMESH_subMesh::MESH_ENTITY_REMOVED );
198 std::map < int, SMESH_Group * >::iterator itg;
199 for (itg = _mapGroup.begin(); itg != _mapGroup.end(); itg++) {
200 SMESH_Group *aGroup = (*itg).second;
206 delete _subMeshHolder;
208 if ( _callUp) delete _callUp;
212 // delete _myMeshDS, in a thread in order not to block closing a study with large meshes
214 boost::thread aThread(boost::bind( & deleteMeshDS, _myMeshDS ));
217 int result=pthread_create(&thread, NULL, deleteMeshDS, (void*)_myMeshDS);
222 //================================================================================
224 * \brief Return true if a mesh with given id exists
226 //================================================================================
228 bool SMESH_Mesh::MeshExists( int meshId ) const
230 return _myDocument ? bool( _myDocument->GetMesh( meshId )) : false;
233 //================================================================================
235 * \brief Return a mesh by id
237 //================================================================================
239 SMESH_Mesh* SMESH_Mesh::FindMesh( int meshId ) const
242 return (SMESH_Mesh*) this;
244 if ( StudyContextStruct *aStudyContext = _gen->GetStudyContext())
246 std::map < int, SMESH_Mesh * >::iterator i_m = aStudyContext->mapMesh.find( meshId );
247 if ( i_m != aStudyContext->mapMesh.end() )
253 //=============================================================================
255 * \brief Set geometry to be meshed
257 //=============================================================================
259 void SMESH_Mesh::ShapeToMesh(const TopoDS_Shape & aShape)
261 if(MYDEBUG) MESSAGE("SMESH_Mesh::ShapeToMesh");
263 if ( !aShape.IsNull() && _isShapeToMesh ) {
264 if ( aShape.ShapeType() != TopAbs_COMPOUND && // group contents is allowed to change
265 _myMeshDS->ShapeToMesh().ShapeType() != TopAbs_COMPOUND )
266 throw SALOME_Exception(LOCALIZED ("a shape to mesh has already been defined"));
268 // clear current data
269 if ( !_myMeshDS->ShapeToMesh().IsNull() )
271 // removal of a shape to mesh, delete objects referring to sub-shapes:
273 _subMeshHolder->DeleteAll();
274 // - groups on geometry
275 std::map <int, SMESH_Group *>::iterator i_gr = _mapGroup.begin();
276 while ( i_gr != _mapGroup.end() ) {
277 if ( dynamic_cast<SMESHDS_GroupOnGeom*>( i_gr->second->GetGroupDS() )) {
278 _myMeshDS->RemoveGroup( i_gr->second->GetGroupDS() );
280 _mapGroup.erase( i_gr++ );
285 _mapAncestors.Clear();
288 TopoDS_Shape aNullShape;
289 _myMeshDS->ShapeToMesh( aNullShape );
291 _shapeDiagonal = 0.0;
294 // set a new geometry
295 if ( !aShape.IsNull() )
297 _myMeshDS->ShapeToMesh(aShape);
298 _isShapeToMesh = true;
299 _nbSubShapes = _myMeshDS->MaxShapeIndex();
301 // fill map of ancestors
302 fillAncestorsMap(aShape);
306 _isShapeToMesh = false;
307 _shapeDiagonal = 0.0;
308 _myMeshDS->ShapeToMesh( PseudoShape() );
313 //=======================================================================
315 * \brief Return geometry to be meshed. (It may be a PseudoShape()!)
317 //=======================================================================
319 TopoDS_Shape SMESH_Mesh::GetShapeToMesh() const
321 return _myMeshDS->ShapeToMesh();
324 //=======================================================================
326 * \brief Return a solid which is returned by GetShapeToMesh() if
327 * a real geometry to be meshed was not set
329 //=======================================================================
331 const TopoDS_Solid& SMESH_Mesh::PseudoShape()
333 static TopoDS_Solid aSolid;
334 if ( aSolid.IsNull() )
336 aSolid = BRepPrimAPI_MakeBox(1,1,1);
341 //=======================================================================
343 * \brief Return diagonal size of bounding box of a shape
345 //=======================================================================
347 double SMESH_Mesh::GetShapeDiagonalSize(const TopoDS_Shape & aShape)
349 if ( !aShape.IsNull() ) {
351 // avoid too long waiting on large shapes. PreciseBoundingBox() was added
352 // to assure same result which else depends on presence of triangulation (IPAL52557).
353 const int maxNbFaces = 4000;
355 for ( TopExp_Explorer f( aShape, TopAbs_FACE ); f.More() && nbFaces < maxNbFaces; f.Next() )
357 bool isPrecise = false;
358 if ( nbFaces < maxNbFaces )
361 GEOMUtils::PreciseBoundingBox( aShape, Box );
369 BRepBndLib::Add( aShape, Box );
372 return sqrt( Box.SquareExtent() );
377 //=======================================================================
379 * \brief Return diagonal size of bounding box of shape to mesh
381 //=======================================================================
383 double SMESH_Mesh::GetShapeDiagonalSize() const
385 if ( _shapeDiagonal == 0. && _isShapeToMesh )
386 const_cast<SMESH_Mesh*>(this)->_shapeDiagonal = GetShapeDiagonalSize( GetShapeToMesh() );
388 return _shapeDiagonal;
391 //================================================================================
393 * \brief Load mesh from study file
395 //================================================================================
397 void SMESH_Mesh::Load()
403 //=======================================================================
405 * \brief Remove all nodes and elements
407 //=======================================================================
409 void SMESH_Mesh::Clear()
411 if ( HasShapeToMesh() ) // remove all nodes and elements
414 _myMeshDS->ClearMesh();
416 // update compute state of submeshes
417 if ( SMESH_subMesh *sm = GetSubMeshContaining( GetShapeToMesh() ) )
419 sm->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
420 sm->ComputeSubMeshStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
421 sm->ComputeStateEngine( SMESH_subMesh::CLEAN ); // for event listeners (issue 0020918)
422 sm->ComputeSubMeshStateEngine( SMESH_subMesh::CLEAN );
425 else // remove only nodes/elements computed by algorithms
427 if ( SMESH_subMesh *sm = GetSubMeshContaining( GetShapeToMesh() ) )
429 sm->ComputeStateEngine( SMESH_subMesh::CLEAN );
430 sm->ComputeSubMeshStateEngine( SMESH_subMesh::CLEAN );
431 sm->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
432 sm->ComputeSubMeshStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
435 GetMeshDS()->Modified();
439 //=======================================================================
441 * \brief Remove all nodes and elements of indicated shape
443 //=======================================================================
445 void SMESH_Mesh::ClearSubMesh(const int theShapeId)
447 // clear sub-meshes; get ready to re-compute as a side-effect
448 if ( SMESH_subMesh *sm = GetSubMeshContaining( theShapeId ) )
450 SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(/*includeSelf=*/true,
451 /*complexShapeFirst=*/false);
452 while ( smIt->more() )
455 TopAbs_ShapeEnum shapeType = sm->GetSubShape().ShapeType();
456 if ( shapeType == TopAbs_VERTEX || shapeType < TopAbs_SOLID )
457 // all other shapes depends on vertices so they are already cleaned
458 sm->ComputeStateEngine( SMESH_subMesh::CLEAN );
459 // to recompute even if failed
460 sm->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
465 //=======================================================================
466 //function : UNVToMesh
468 //=======================================================================
470 int SMESH_Mesh::UNVToMesh(const char* theFileName)
472 if ( _isShapeToMesh )
473 throw SALOME_Exception(LOCALIZED("a shape to mesh has already been defined"));
474 _isShapeToMesh = false;
476 DriverUNV_R_SMDS_Mesh myReader;
477 myReader.SetMesh(_myMeshDS);
478 myReader.SetFile(theFileName);
479 myReader.SetMeshId(-1);
482 TGroupNamesMap& aGroupNames = myReader.GetGroupNamesMap();
483 TGroupNamesMap::iterator gr2names;
484 int anId = 1 + ( _mapGroup.empty() ? 0 : _mapGroup.rbegin()->first );
485 for ( gr2names = aGroupNames.begin(); gr2names != aGroupNames.end(); ++gr2names )
487 SMDS_MeshGroup* aGroup = gr2names->first;
488 const std::string& aName = gr2names->second;
489 SMESHDS_Group* aGroupDS = new SMESHDS_Group( anId++, _myMeshDS, aGroup->GetType() );
490 aGroupDS->SMDSGroup() = std::move( *aGroup );
491 aGroupDS->SetStoreName( aName.c_str() );
492 AddGroup( aGroupDS );
498 //=======================================================================
499 //function : MEDToMesh
501 //=======================================================================
503 int SMESH_Mesh::MEDToMesh(const char* theFileName, const char* theMeshName)
505 if ( _isShapeToMesh )
506 throw SALOME_Exception(LOCALIZED("a shape to mesh has already been defined"));
507 _isShapeToMesh = false;
509 DriverMED_R_SMESHDS_Mesh myReader;
510 myReader.SetMesh(_myMeshDS);
511 myReader.SetMeshId(-1);
512 myReader.SetFile(theFileName);
513 myReader.SetMeshName(theMeshName);
514 Driver_Mesh::Status status = myReader.Perform();
516 SMESH_ComputeErrorPtr er = myReader.GetError();
517 if ( er && !er->IsOK() ) std::cout << er->myComment << std::endl;
520 // Reading groups (sub-meshes are out of scope of MED import functionality)
521 std::list<TNameAndType> aGroupNames = myReader.GetGroupNamesAndTypes();
522 std::list<TNameAndType>::iterator name_type = aGroupNames.begin();
523 for ( ; name_type != aGroupNames.end(); name_type++ )
525 SMESH_Group* aGroup = AddGroup( name_type->second, name_type->first.c_str() );
527 SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>( aGroup->GetGroupDS() );
529 aGroupDS->SetStoreName( name_type->first.c_str() );
530 myReader.GetGroup( aGroupDS );
535 _myMeshDS->Modified();
536 _myMeshDS->CompactMesh();
541 //=======================================================================
542 //function : STLToMesh
544 //=======================================================================
546 std::string SMESH_Mesh::STLToMesh(const char* theFileName)
549 throw SALOME_Exception(LOCALIZED("a shape to mesh has already been defined"));
550 _isShapeToMesh = false;
552 DriverSTL_R_SMDS_Mesh myReader;
553 myReader.SetMesh(_myMeshDS);
554 myReader.SetFile(theFileName);
555 myReader.SetMeshId(-1);
558 return myReader.GetName();
561 //================================================================================
563 * \brief Reads the given mesh from the CGNS file
564 * \param theFileName - name of the file
565 * \retval int - Driver_Mesh::Status
567 //================================================================================
569 int SMESH_Mesh::CGNSToMesh(const char* theFileName,
570 const int theMeshIndex,
571 std::string& theMeshName)
573 int res = Driver_Mesh::DRS_FAIL;
576 DriverCGNS_Read myReader;
577 myReader.SetMesh(_myMeshDS);
578 myReader.SetFile(theFileName);
579 myReader.SetMeshId(theMeshIndex);
580 res = myReader.Perform();
581 theMeshName = myReader.GetMeshName();
590 //================================================================================
592 * \brief Fill its data by reading a GMF file
594 //================================================================================
596 SMESH_ComputeErrorPtr SMESH_Mesh::GMFToMesh(const char* theFileName,
597 bool theMakeRequiredGroups)
599 DriverGMF_Read myReader;
600 myReader.SetMesh(_myMeshDS);
601 myReader.SetFile(theFileName);
602 myReader.SetMakeRequiredGroups( theMakeRequiredGroups );
604 //theMeshName = myReader.GetMeshName();
609 return myReader.GetError();
612 //=============================================================================
616 //=============================================================================
618 SMESH_Hypothesis::Hypothesis_Status
619 SMESH_Mesh::AddHypothesis(const TopoDS_Shape & aSubShape,
621 std::string* anError ) throw(SALOME_Exception)
623 Unexpect aCatch(SalomeException);
624 if(MYDEBUG) MESSAGE("SMESH_Mesh::AddHypothesis");
629 SMESH_subMesh *subMesh = GetSubMesh(aSubShape);
630 if ( !subMesh || !subMesh->GetId())
631 return SMESH_Hypothesis::HYP_BAD_SUBSHAPE;
633 SMESH_Hypothesis *anHyp = GetHypothesis( anHypId );
635 throw SALOME_Exception(LOCALIZED("hypothesis does not exist"));
637 bool isGlobalHyp = IsMainShape( aSubShape );
639 // NotConformAllowed can be only global
642 // NOTE: this is not a correct way to check a name of hypothesis,
643 // there should be an attribute of hypothesis saying that it can/can't
645 std::string hypName = anHyp->GetName();
646 if ( hypName == "NotConformAllowed" )
648 if(MYDEBUG) MESSAGE( "Hypothesis <NotConformAllowed> can be only global" );
649 return SMESH_Hypothesis::HYP_INCOMPATIBLE;
655 bool isAlgo = ( anHyp->GetType() != SMESHDS_Hypothesis::PARAM_ALGO );
656 SMESH_subMesh::algo_event event = isAlgo ? SMESH_subMesh::ADD_ALGO : SMESH_subMesh::ADD_HYP;
658 SMESH_Hypothesis::Hypothesis_Status ret = subMesh->AlgoStateEngine(event, anHyp);
660 if ( anError && SMESH_Hypothesis::IsStatusFatal(ret) && subMesh->GetComputeError() )
661 *anError = subMesh->GetComputeError()->myComment;
664 if ( !SMESH_Hypothesis::IsStatusFatal(ret) &&
665 anHyp->GetDim() <= SMESH_Gen::GetShapeDim(aSubShape)) // is added on father
667 event = isAlgo ? SMESH_subMesh::ADD_FATHER_ALGO : SMESH_subMesh::ADD_FATHER_HYP;
669 SMESH_Hypothesis::Hypothesis_Status ret2 =
670 subMesh->SubMeshesAlgoStateEngine(event, anHyp, /*exitOnFatal=*/true);
674 if ( SMESH_Hypothesis::IsStatusFatal( ret ))
676 if ( anError && subMesh->GetComputeError() )
677 *anError = subMesh->GetComputeError()->myComment;
679 event = isAlgo ? SMESH_subMesh::REMOVE_ALGO : SMESH_subMesh::REMOVE_HYP;
680 subMesh->AlgoStateEngine(event, anHyp);
684 // check concurrent hypotheses on ancestors
685 if (ret < SMESH_Hypothesis::HYP_CONCURRENT && !isGlobalHyp )
687 SMESH_subMeshIteratorPtr smIt = subMesh->getDependsOnIterator(false,false);
688 while ( smIt->more() ) {
689 SMESH_subMesh* sm = smIt->next();
690 if ( sm->IsApplicableHypothesis( anHyp )) {
691 ret2 = sm->CheckConcurrentHypothesis( anHyp->GetType() );
700 HasModificationsToDiscard(); // to reset _isModified flag if a mesh becomes empty
701 GetMeshDS()->Modified();
703 if(MYDEBUG) subMesh->DumpAlgoState(true);
704 if(MYDEBUG) SCRUTE(ret);
708 //=============================================================================
712 //=============================================================================
714 SMESH_Hypothesis::Hypothesis_Status
715 SMESH_Mesh::RemoveHypothesis(const TopoDS_Shape & aSubShape,
716 int anHypId) throw( SALOME_Exception )
718 Unexpect aCatch(SalomeException);
719 if(MYDEBUG) MESSAGE("SMESH_Mesh::RemoveHypothesis");
721 StudyContextStruct *sc = _gen->GetStudyContext();
722 if (sc->mapHypothesis.find(anHypId) == sc->mapHypothesis.end())
723 throw SALOME_Exception(LOCALIZED("hypothesis does not exist"));
725 SMESH_Hypothesis *anHyp = sc->mapHypothesis[anHypId];
726 if(MYDEBUG) { SCRUTE(anHyp->GetType()); }
730 bool isAlgo = ( !anHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO );
731 SMESH_subMesh::algo_event event = isAlgo ? SMESH_subMesh::REMOVE_ALGO : SMESH_subMesh::REMOVE_HYP;
733 SMESH_subMesh *subMesh = GetSubMesh(aSubShape);
735 SMESH_Hypothesis::Hypothesis_Status ret = subMesh->AlgoStateEngine(event, anHyp);
737 // there may appear concurrent hyps that were covered by the removed hyp
738 if (ret < SMESH_Hypothesis::HYP_CONCURRENT &&
739 subMesh->IsApplicableHypothesis( anHyp ) &&
740 subMesh->CheckConcurrentHypothesis( anHyp->GetType() ) != SMESH_Hypothesis::HYP_OK)
741 ret = SMESH_Hypothesis::HYP_CONCURRENT;
744 if (!SMESH_Hypothesis::IsStatusFatal(ret) &&
745 anHyp->GetDim() <= SMESH_Gen::GetShapeDim(aSubShape)) // is removed from father
747 event = isAlgo ? SMESH_subMesh::REMOVE_FATHER_ALGO : SMESH_subMesh::REMOVE_FATHER_HYP;
749 SMESH_Hypothesis::Hypothesis_Status ret2 =
750 subMesh->SubMeshesAlgoStateEngine(event, anHyp);
751 if (ret2 > ret) // more severe
754 // check concurrent hypotheses on ancestors
755 if (ret < SMESH_Hypothesis::HYP_CONCURRENT && !IsMainShape( aSubShape ) )
757 SMESH_subMeshIteratorPtr smIt = subMesh->getDependsOnIterator(false,false);
758 while ( smIt->more() ) {
759 SMESH_subMesh* sm = smIt->next();
760 if ( sm->IsApplicableHypothesis( anHyp )) {
761 ret2 = sm->CheckConcurrentHypothesis( anHyp->GetType() );
771 HasModificationsToDiscard(); // to reset _isModified flag if mesh become empty
772 GetMeshDS()->Modified();
774 if(MYDEBUG) subMesh->DumpAlgoState(true);
775 if(MYDEBUG) SCRUTE(ret);
779 //=============================================================================
783 //=============================================================================
785 const std::list<const SMESHDS_Hypothesis*>&
786 SMESH_Mesh::GetHypothesisList(const TopoDS_Shape & aSubShape) const
787 throw(SALOME_Exception)
789 return _myMeshDS->GetHypothesis(aSubShape);
792 //=======================================================================
794 * \brief Return the hypothesis assigned to the shape
795 * \param aSubShape - the shape to check
796 * \param aFilter - the hypothesis filter
797 * \param andAncestors - flag to check hypos assigned to ancestors of the shape
798 * \param assignedTo - to return the shape the found hypo is assigned to
799 * \retval SMESH_Hypothesis* - the first hypo passed through aFilter
801 //=======================================================================
803 const SMESH_Hypothesis * SMESH_Mesh::GetHypothesis(const TopoDS_Shape & aSubShape,
804 const SMESH_HypoFilter& aFilter,
805 const bool andAncestors,
806 TopoDS_Shape* assignedTo) const
808 return GetHypothesis( const_cast< SMESH_Mesh* >(this)->GetSubMesh( aSubShape ),
809 aFilter, andAncestors, assignedTo );
812 //=======================================================================
814 * \brief Return the hypothesis assigned to the shape of a sub-mesh
815 * \param aSubMesh - the sub-mesh to check
816 * \param aFilter - the hypothesis filter
817 * \param andAncestors - flag to check hypos assigned to ancestors of the shape
818 * \param assignedTo - to return the shape the found hypo is assigned to
819 * \retval SMESH_Hypothesis* - the first hypo passed through aFilter
821 //=======================================================================
823 const SMESH_Hypothesis * SMESH_Mesh::GetHypothesis(const SMESH_subMesh * aSubMesh,
824 const SMESH_HypoFilter& aFilter,
825 const bool andAncestors,
826 TopoDS_Shape* assignedTo) const
828 if ( !aSubMesh ) return 0;
831 const TopoDS_Shape & aSubShape = aSubMesh->GetSubShape();
832 const std::list<const SMESHDS_Hypothesis*>& hypList = _myMeshDS->GetHypothesis(aSubShape);
833 std::list<const SMESHDS_Hypothesis*>::const_iterator hyp = hypList.begin();
834 for ( ; hyp != hypList.end(); hyp++ ) {
835 const SMESH_Hypothesis * h = cSMESH_Hyp( *hyp );
836 if ( aFilter.IsOk( h, aSubShape)) {
837 if ( assignedTo ) *assignedTo = aSubShape;
844 // user sorted submeshes of ancestors, according to stored submesh priority
845 std::vector< SMESH_subMesh * > & ancestors =
846 const_cast< std::vector< SMESH_subMesh * > & > ( aSubMesh->GetAncestors() );
847 SortByMeshOrder( ancestors );
849 std::vector<SMESH_subMesh*>::const_iterator smIt = ancestors.begin();
850 for ( ; smIt != ancestors.end(); smIt++ )
852 const TopoDS_Shape& curSh = (*smIt)->GetSubShape();
853 const std::list<const SMESHDS_Hypothesis*>& hypList = _myMeshDS->GetHypothesis(curSh);
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, curSh )) {
858 if ( assignedTo ) *assignedTo = curSh;
867 //================================================================================
869 * \brief Return hypotheses assigned to the shape
870 * \param aSubShape - the shape to check
871 * \param aFilter - the hypothesis filter
872 * \param aHypList - the list of the found hypotheses
873 * \param andAncestors - flag to check hypos assigned to ancestors of the shape
874 * \retval int - number of unique hypos in aHypList
876 //================================================================================
878 int SMESH_Mesh::GetHypotheses(const TopoDS_Shape & aSubShape,
879 const SMESH_HypoFilter& aFilter,
880 std::list <const SMESHDS_Hypothesis * >& aHypList,
881 const bool andAncestors,
882 std::list< TopoDS_Shape > * assignedTo/*=0*/) const
884 return GetHypotheses( const_cast< SMESH_Mesh* >(this)->GetSubMesh( aSubShape ),
885 aFilter, aHypList, andAncestors, assignedTo );
888 //================================================================================
890 * \brief Return hypotheses assigned to the shape of a sub-mesh
891 * \param aSubShape - the sub-mesh 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 SMESH_subMesh * aSubMesh,
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 if ( !aSubMesh ) return 0;
907 std::set< std::string > hypTypes; // to exclude same type hypos from the result list
910 // only one main hypothesis is allowed
911 bool mainHypFound = false;
914 std::list<const SMESHDS_Hypothesis*>::const_iterator hyp;
915 for ( hyp = aHypList.begin(); hyp != aHypList.end(); hyp++ ) {
916 if ( hypTypes.insert( (*hyp)->GetName() ).second )
918 if ( !cSMESH_Hyp(*hyp)->IsAuxiliary() )
922 // get hypos from aSubShape
924 const TopoDS_Shape & aSubShape = aSubMesh->GetSubShape();
925 const std::list<const SMESHDS_Hypothesis*>& hypList = _myMeshDS->GetHypothesis(aSubShape);
926 for ( hyp = hypList.begin(); hyp != hypList.end(); hyp++ )
928 const SMESH_Hypothesis* h = cSMESH_Hyp( *hyp );
929 if (( aFilter.IsOk( h, aSubShape )) &&
930 ( h->IsAuxiliary() || !mainHypFound ) &&
931 ( h->IsAuxiliary() || hypTypes.insert( h->GetName() ).second ))
933 aHypList.push_back( *hyp );
935 if ( !h->IsAuxiliary() )
937 if ( assignedTo ) assignedTo->push_back( aSubShape );
942 // get hypos from ancestors of aSubShape
945 // user sorted submeshes of ancestors, according to stored submesh priority
946 std::vector< SMESH_subMesh * > & ancestors =
947 const_cast< std::vector< SMESH_subMesh * > & > ( aSubMesh->GetAncestors() );
948 SortByMeshOrder( ancestors );
950 std::vector<SMESH_subMesh*>::const_iterator smIt = ancestors.begin();
951 for ( ; smIt != ancestors.end(); smIt++ )
953 const TopoDS_Shape& curSh = (*smIt)->GetSubShape();
954 const std::list<const SMESHDS_Hypothesis*>& hypList = _myMeshDS->GetHypothesis(curSh);
955 for ( hyp = hypList.begin(); hyp != hypList.end(); hyp++ )
957 const SMESH_Hypothesis* h = cSMESH_Hyp( *hyp );
958 if (( aFilter.IsOk( h, curSh )) &&
959 ( h->IsAuxiliary() || !mainHypFound ) &&
960 ( h->IsAuxiliary() || hypTypes.insert( h->GetName() ).second ))
962 aHypList.push_back( *hyp );
964 if ( !h->IsAuxiliary() )
966 if ( assignedTo ) assignedTo->push_back( curSh );
974 //================================================================================
976 * \brief Return a hypothesis by its ID
978 //================================================================================
980 SMESH_Hypothesis * SMESH_Mesh::GetHypothesis(const int anHypId) const
982 StudyContextStruct *sc = _gen->GetStudyContext();
983 if (sc->mapHypothesis.find(anHypId) == sc->mapHypothesis.end())
986 SMESH_Hypothesis *anHyp = sc->mapHypothesis[anHypId];
990 //=============================================================================
994 //=============================================================================
996 const std::list<SMESHDS_Command*> & SMESH_Mesh::GetLog() throw(SALOME_Exception)
998 Unexpect aCatch(SalomeException);
999 return _myMeshDS->GetScript()->GetCommands();
1002 //=============================================================================
1006 //=============================================================================
1007 void SMESH_Mesh::ClearLog() throw(SALOME_Exception)
1009 Unexpect aCatch(SalomeException);
1010 _myMeshDS->GetScript()->Clear();
1013 //=============================================================================
1015 * Get or Create the SMESH_subMesh object implementation
1017 //=============================================================================
1019 SMESH_subMesh * SMESH_Mesh::GetSubMesh(const TopoDS_Shape & aSubShape)
1020 throw(SALOME_Exception)
1022 int index = _myMeshDS->ShapeToIndex(aSubShape);
1023 if ( !index && aSubShape.IsNull() )
1026 // for submeshes on GEOM Group
1027 if (( !index || index > _nbSubShapes ) && aSubShape.ShapeType() == TopAbs_COMPOUND )
1029 TopoDS_Iterator it( aSubShape );
1032 index = _myMeshDS->AddCompoundSubmesh( aSubShape, it.Value().ShapeType() );
1033 // fill map of Ancestors
1034 while ( _nbSubShapes < index )
1035 fillAncestorsMap( _myMeshDS->IndexToShape( ++_nbSubShapes ));
1039 // return NULL; // neither sub-shape nor a group
1041 SMESH_subMesh* aSubMesh = _subMeshHolder->Get( index );
1044 aSubMesh = new SMESH_subMesh(index, this, _myMeshDS, aSubShape);
1045 _subMeshHolder->Add( index, aSubMesh );
1047 // include non-computable sub-meshes in SMESH_subMesh::_ancestors of sub-submeshes
1048 switch ( aSubShape.ShapeType() ) {
1049 case TopAbs_COMPOUND:
1052 for ( TopoDS_Iterator subIt( aSubShape ); subIt.More(); subIt.Next() )
1054 SMESH_subMesh* sm = GetSubMesh( subIt.Value() );
1055 SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(/*inclideSelf=*/true);
1056 while ( smIt->more() )
1057 smIt->next()->ClearAncestors();
1065 //=============================================================================
1067 * Get the SMESH_subMesh object implementation. Don't create it, return null
1068 * if it does not exist.
1070 //=============================================================================
1072 SMESH_subMesh *SMESH_Mesh::GetSubMeshContaining(const TopoDS_Shape & aSubShape) const
1073 throw(SALOME_Exception)
1075 int index = _myMeshDS->ShapeToIndex(aSubShape);
1076 return GetSubMeshContaining( index );
1079 //=============================================================================
1081 * Get the SMESH_subMesh object implementation. Don't create it, return null
1082 * if it does not exist.
1084 //=============================================================================
1086 SMESH_subMesh *SMESH_Mesh::GetSubMeshContaining(const int aShapeID) const
1087 throw(SALOME_Exception)
1089 SMESH_subMesh *aSubMesh = _subMeshHolder->Get( aShapeID );
1094 //================================================================================
1096 * \brief Return sub-meshes of groups containing the given sub-shape
1098 //================================================================================
1100 std::list<SMESH_subMesh*>
1101 SMESH_Mesh::GetGroupSubMeshesContaining(const TopoDS_Shape & aSubShape) const
1102 throw(SALOME_Exception)
1104 std::list<SMESH_subMesh*> found;
1106 SMESH_subMesh * subMesh = GetSubMeshContaining(aSubShape);
1110 // sub-meshes of groups have max IDs, so search from the map end
1111 SMESH_subMeshIteratorPtr smIt( _subMeshHolder->GetIterator( /*reverse=*/true ) );
1112 while ( smIt->more() ) {
1113 SMESH_subMesh* sm = smIt->next();
1114 SMESHDS_SubMesh * ds = sm->GetSubMeshDS();
1115 if ( ds && ds->IsComplexSubmesh() ) {
1116 if ( SMESH_MesherHelper::IsSubShape( aSubShape, sm->GetSubShape() ))
1118 found.push_back( sm );
1122 break; // the rest sub-meshes are not those of groups
1126 if ( found.empty() ) // maybe the main shape is a COMPOUND (issue 0021530)
1128 if ( SMESH_subMesh * mainSM = GetSubMeshContaining(1) )
1129 if ( mainSM->GetSubShape().ShapeType() == TopAbs_COMPOUND )
1131 TopoDS_Iterator it( mainSM->GetSubShape() );
1132 if ( it.Value().ShapeType() == aSubShape.ShapeType() &&
1133 SMESH_MesherHelper::IsSubShape( aSubShape, mainSM->GetSubShape() ))
1134 found.push_back( mainSM );
1137 else // issue 0023068
1139 if ( SMESH_subMesh * mainSM = GetSubMeshContaining(1) )
1140 if ( mainSM->GetSubShape().ShapeType() == TopAbs_COMPOUND )
1141 found.push_back( mainSM );
1145 //=======================================================================
1146 //function : IsUsedHypothesis
1147 //purpose : Return True if anHyp is used to mesh aSubShape
1148 //=======================================================================
1150 bool SMESH_Mesh::IsUsedHypothesis(SMESHDS_Hypothesis * anHyp,
1151 const SMESH_subMesh* aSubMesh)
1153 SMESH_Hypothesis* hyp = static_cast<SMESH_Hypothesis*>(anHyp);
1155 // check if anHyp can be used to mesh aSubMesh
1156 if ( !aSubMesh || !aSubMesh->IsApplicableHypothesis( hyp ))
1159 SMESH_Algo *algo = aSubMesh->GetAlgo();
1162 if (anHyp->GetType() > SMESHDS_Hypothesis::PARAM_ALGO)
1163 return ( anHyp == algo );
1165 // algorithm parameter
1168 // look trough hypotheses used by algo
1169 const SMESH_HypoFilter* hypoKind;
1170 if (( hypoKind = algo->GetCompatibleHypoFilter( !hyp->IsAuxiliary() ))) {
1171 std::list <const SMESHDS_Hypothesis * > usedHyps;
1172 if ( GetHypotheses( aSubMesh, *hypoKind, usedHyps, true ))
1173 return ( find( usedHyps.begin(), usedHyps.end(), anHyp ) != usedHyps.end() );
1180 //=======================================================================
1181 //function : NotifySubMeshesHypothesisModification
1182 //purpose : Say all submeshes using theChangedHyp that it has been modified
1183 //=======================================================================
1185 void SMESH_Mesh::NotifySubMeshesHypothesisModification(const SMESH_Hypothesis* hyp)
1187 Unexpect aCatch(SalomeException);
1189 if ( !GetMeshDS()->IsUsedHypothesis( hyp ))
1192 int nbEntities = ( _myMeshDS->NbNodes() + _myMeshDS->NbElements() );
1193 if ( hyp && _callUp && !_callUp->IsLoaded() ) // for not loaded mesh (#16648)
1195 _callUp->HypothesisModified( hyp->GetID(), /*updateIcons=*/true );
1196 nbEntities = ( _myMeshDS->NbNodes() + _myMeshDS->NbElements() ); // after loading mesh
1200 const SMESH_HypoFilter* compatibleHypoKind;
1201 std::list <const SMESHDS_Hypothesis * > usedHyps;
1202 std::vector< SMESH_subMesh* > smToNotify;
1203 bool allMeshedEdgesNotified = true;
1205 SMESH_subMeshIteratorPtr smIt( _subMeshHolder->GetIterator() );
1206 while ( smIt->more() )
1208 SMESH_subMesh* aSubMesh = smIt->next();
1209 bool toNotify = false;
1211 // if aSubMesh meshing depends on hyp,
1212 // we call aSubMesh->AlgoStateEngine( MODIF_HYP, hyp ) that causes either
1213 // 1) clearing already computed aSubMesh or
1214 // 2) changing algo_state from MISSING_HYP to HYP_OK when parameters of hyp becomes valid,
1215 // other possible changes are not interesting. (IPAL0052457 - assigning hyp performance pb)
1216 if ( aSubMesh->GetComputeState() == SMESH_subMesh::COMPUTE_OK ||
1217 aSubMesh->GetComputeState() == SMESH_subMesh::FAILED_TO_COMPUTE ||
1218 aSubMesh->GetAlgoState() == SMESH_subMesh::MISSING_HYP ||
1219 hyp->DataDependOnParams() )
1221 const TopoDS_Shape & aSubShape = aSubMesh->GetSubShape();
1223 if (( aSubMesh->IsApplicableHypothesis( hyp )) &&
1224 ( algo = aSubMesh->GetAlgo() ) &&
1225 ( compatibleHypoKind = algo->GetCompatibleHypoFilter( !hyp->IsAuxiliary() )) &&
1226 ( compatibleHypoKind->IsOk( hyp, aSubShape )))
1228 // check if hyp is used by algo
1230 toNotify = ( GetHypotheses( aSubMesh, *compatibleHypoKind, usedHyps, true ) &&
1231 std::find( usedHyps.begin(), usedHyps.end(), hyp ) != usedHyps.end() );
1236 smToNotify.push_back( aSubMesh );
1237 if ( aSubMesh->GetAlgoState() == SMESH_subMesh::MISSING_HYP )
1238 allMeshedEdgesNotified = false; // update of algo state needed, not mesh clearing
1242 if ( !aSubMesh->IsEmpty() &&
1243 aSubMesh->GetSubShape().ShapeType() == TopAbs_EDGE )
1244 allMeshedEdgesNotified = false;
1247 if ( smToNotify.empty() )
1250 // if all meshed EDGEs will be notified then the notification is equivalent
1251 // to the whole mesh clearing, which is usually faster
1252 if ( allMeshedEdgesNotified && NbNodes() > 0 )
1258 // notify in reverse order to avoid filling the pool of IDs
1259 for ( int i = smToNotify.size()-1; i >= 0; --i )
1260 smToNotify[i]->AlgoStateEngine(SMESH_subMesh::MODIF_HYP,
1261 const_cast< SMESH_Hypothesis*>( hyp ));
1263 HasModificationsToDiscard(); // to reset _isModified flag if mesh becomes empty
1264 GetMeshDS()->Modified();
1266 int newNbEntities = ( _myMeshDS->NbNodes() + _myMeshDS->NbElements() );
1267 if ( hyp && _callUp )
1268 _callUp->HypothesisModified( hyp->GetID(), newNbEntities != nbEntities );
1271 //=============================================================================
1273 * Auto color functionality
1275 //=============================================================================
1276 void SMESH_Mesh::SetAutoColor(bool theAutoColor) throw(SALOME_Exception)
1278 Unexpect aCatch(SalomeException);
1279 _isAutoColor = theAutoColor;
1282 bool SMESH_Mesh::GetAutoColor() throw(SALOME_Exception)
1284 Unexpect aCatch(SalomeException);
1285 return _isAutoColor;
1288 //=======================================================================
1289 //function : SetIsModified
1290 //purpose : Set the flag meaning that the mesh has been edited "manually"
1291 //=======================================================================
1293 void SMESH_Mesh::SetIsModified(bool isModified)
1295 _isModified = isModified;
1298 // check if mesh becomes empty as result of modification
1299 HasModificationsToDiscard();
1302 //=======================================================================
1303 //function : HasModificationsToDiscard
1304 //purpose : Return true if the mesh has been edited since a total re-compute
1305 // and those modifications may prevent successful partial re-compute.
1306 // As a side effect reset _isModified flag if mesh is empty
1308 //=======================================================================
1310 bool SMESH_Mesh::HasModificationsToDiscard() const
1312 if ( ! _isModified )
1315 // return true if the next Compute() will be partial and
1316 // existing but changed elements may prevent successful re-compute
1317 bool hasComputed = false, hasNotComputed = false;
1318 SMESH_subMeshIteratorPtr smIt( _subMeshHolder->GetIterator() );
1319 while ( smIt->more() )
1321 const SMESH_subMesh* aSubMesh = smIt->next();
1322 switch ( aSubMesh->GetSubShape().ShapeType() )
1327 if ( aSubMesh->IsMeshComputed() )
1330 hasNotComputed = true;
1331 if ( hasComputed && hasNotComputed)
1337 if ( NbNodes() < 1 )
1338 const_cast<SMESH_Mesh*>(this)->_isModified = false;
1343 //=============================================================================
1345 * \brief Return true if all sub-meshes are computed OK - to update an icon
1347 //=============================================================================
1349 bool SMESH_Mesh::IsComputedOK()
1351 if ( NbNodes() == 0 )
1354 // if ( !HasShapeToMesh() )
1357 if ( SMESH_subMesh* mainSM = GetSubMeshContaining( 1 ))
1359 SMESH_subMeshIteratorPtr smIt = mainSM->getDependsOnIterator(/*includeSelf=*/true);
1360 while ( smIt->more() )
1362 const SMESH_subMesh* sm = smIt->next();
1363 if ( !sm->IsAlwaysComputed() )
1364 switch ( sm->GetComputeState() )
1366 case SMESH_subMesh::NOT_READY:
1367 case SMESH_subMesh::COMPUTE_OK:
1369 case SMESH_subMesh::FAILED_TO_COMPUTE:
1370 case SMESH_subMesh::READY_TO_COMPUTE:
1378 //================================================================================
1380 * \brief Check if any groups of the same type have equal names
1382 //================================================================================
1384 bool SMESH_Mesh::HasDuplicatedGroupNamesMED()
1386 // Corrected for Mantis issue 0020028
1387 std::map< SMDSAbs_ElementType, std::set< std::string > > aGroupNames;
1388 for ( std::map<int, SMESH_Group*>::iterator it = _mapGroup.begin(); it != _mapGroup.end(); it++ )
1390 SMESH_Group* aGroup = it->second;
1391 SMDSAbs_ElementType aType = aGroup->GetGroupDS()->GetType();
1392 std::string aGroupName = aGroup->GetName();
1393 aGroupName.resize( MAX_MED_GROUP_NAME_LENGTH );
1394 if ( !aGroupNames[aType].insert(aGroupName).second )
1401 //================================================================================
1403 * \brief Export the mesh to a med file
1404 * \param [in] file - name of the MED file
1405 * \param [in] theMeshName - name of this mesh
1406 * \param [in] theAutoGroups - boolean parameter for creating/not creating
1407 * the groups Group_On_All_Nodes, Group_On_All_Faces, ... ;
1408 * the typical use is auto_groups=false.
1409 * \param [in] theMinor - define the minor version (y, where version is x.y.z) of MED file format.
1410 * The theMinor must be between 0 and the current minor version of MED file library.
1411 * If theMinor is equal to -1, the minor version is not changed (default).
1412 * The major version (x, where version is x.y.z) cannot be changed.
1413 * \param [in] meshPart - mesh data to export
1414 * \param [in] theAutoDimension - if \c true, a space dimension of a MED mesh can be either
1415 * - 1D if all mesh nodes lie on OX coordinate axis, or
1416 * - 2D if all mesh nodes lie on XOY coordinate plane, or
1417 * - 3D in the rest cases.
1418 * If \a theAutoDimension is \c false, the space dimension is always 3.
1419 * \param [in] theAddODOnVertices - to create 0D elements on all vertices
1420 * \param [in] theAllElemsToGroup - to make every element to belong to any group (PAL23413)
1421 * \param [in] ZTolerance - tolerance in Z direction. If Z coordinate of a node is close to zero
1422 * within a given tolerance, the coordinate is set to zero.
1423 * If \a ZTolerance is negative, the node coordinates are kept as is.
1424 * \return int - mesh index in the file
1426 //================================================================================
1428 void SMESH_Mesh::ExportMED(const char * file,
1429 const char* theMeshName,
1432 const SMESHDS_Mesh* meshPart,
1433 bool theAutoDimension,
1434 bool theAddODOnVertices,
1435 double theZTolerance,
1436 bool theAllElemsToGroup)
1437 throw(SALOME_Exception)
1439 MESSAGE("MED_VERSION:"<< theVersion);
1442 DriverMED_W_SMESHDS_Mesh myWriter;
1443 myWriter.SetFile ( file , theVersion);
1444 myWriter.SetMesh ( meshPart ? (SMESHDS_Mesh*) meshPart : _myMeshDS );
1445 myWriter.SetAutoDimension( theAutoDimension );
1446 myWriter.AddODOnVertices ( theAddODOnVertices );
1447 myWriter.SetZTolerance ( theZTolerance );
1449 myWriter.SetMeshId ( _id );
1451 myWriter.SetMeshId ( -1 );
1452 myWriter.SetMeshName ( theMeshName );
1455 if ( theAutoGroups ) {
1456 myWriter.AddGroupOfNodes();
1457 myWriter.AddGroupOfEdges();
1458 myWriter.AddGroupOfFaces();
1459 myWriter.AddGroupOfVolumes();
1460 myWriter.AddGroupOf0DElems();
1461 myWriter.AddGroupOfBalls();
1463 if ( theAllElemsToGroup )
1464 myWriter.AddAllToGroup();
1466 // Pass groups to writer. Provide unique group names.
1467 //set<string> aGroupNames; // Corrected for Mantis issue 0020028
1470 std::map< SMDSAbs_ElementType, std::set<std::string> > aGroupNames;
1472 int maxNbIter = 10000; // to guarantee cycle finish
1473 for ( std::map<int, SMESH_Group*>::iterator it = _mapGroup.begin();
1474 it != _mapGroup.end();
1476 SMESH_Group* aGroup = it->second;
1477 SMESHDS_GroupBase* aGroupDS = aGroup->GetGroupDS();
1479 SMDSAbs_ElementType aType = aGroupDS->GetType();
1480 std::string aGroupName0 = aGroup->GetName();
1481 aGroupName0.resize(MAX_MED_GROUP_NAME_LENGTH);
1482 std::string aGroupName = aGroupName0;
1483 for (int i = 1; !aGroupNames[aType].insert(aGroupName).second && i < maxNbIter; i++) {
1484 sprintf(&aString[0], "GR_%d_%s", i, aGroupName0.c_str());
1485 aGroupName = aString;
1486 aGroupName.resize(MAX_MED_GROUP_NAME_LENGTH);
1488 aGroupDS->SetStoreName( aGroupName.c_str() );
1489 myWriter.AddGroup( aGroupDS );
1496 SMESH_CATCH( SMESH::throwSalomeEx );
1499 //================================================================================
1501 * \brief Export the mesh to a SAUV file
1503 //================================================================================
1505 void SMESH_Mesh::ExportSAUV(const char *file,
1506 const char* theMeshName,
1508 throw(SALOME_Exception)
1510 std::string medfilename(file);
1511 medfilename += ".med";
1514 cmd = "%PYTHONBIN% ";
1519 cmd += "from medutilities import my_remove ; my_remove(r'" + medfilename + "')";
1521 system(cmd.c_str());
1522 ExportMED(medfilename.c_str(), theMeshName, theAutoGroups, /*minor=*/-1,
1523 /*meshPart=*/NULL, /*theAutoDimension=*/false, /*theAddODOnVertices=*/false,
1524 /*zTol=*/-1, /*theAllElemsToGroup=*/true ); // theAllElemsToGroup is for PAL0023413
1526 cmd = "%PYTHONBIN% ";
1531 cmd += "from medutilities import convert ; convert(r'" + medfilename + "', 'MED', 'GIBI', 1, r'" + file + "')";
1533 system(cmd.c_str());
1535 cmd = "%PYTHONBIN% ";
1540 cmd += "from medutilities import my_remove ; my_remove(r'" + medfilename + "')";
1542 system(cmd.c_str());
1545 //================================================================================
1547 * \brief Export the mesh to a DAT file
1549 //================================================================================
1551 void SMESH_Mesh::ExportDAT(const char * file,
1552 const SMESHDS_Mesh* meshPart) throw(SALOME_Exception)
1554 Unexpect aCatch(SalomeException);
1555 DriverDAT_W_SMDS_Mesh myWriter;
1556 myWriter.SetFile( file );
1557 myWriter.SetMesh( meshPart ? (SMESHDS_Mesh*) meshPart : _myMeshDS );
1558 myWriter.SetMeshId(_id);
1562 //================================================================================
1564 * \brief Export the mesh to an UNV file
1566 //================================================================================
1568 void SMESH_Mesh::ExportUNV(const char * file,
1569 const SMESHDS_Mesh* meshPart) throw(SALOME_Exception)
1571 Unexpect aCatch(SalomeException);
1572 DriverUNV_W_SMDS_Mesh myWriter;
1573 myWriter.SetFile( file );
1574 myWriter.SetMesh( meshPart ? (SMESHDS_Mesh*) meshPart : _myMeshDS );
1575 myWriter.SetMeshId(_id);
1576 // myWriter.SetGroups(_mapGroup);
1578 // pass group names to SMESHDS
1581 std::map<int, SMESH_Group*>::iterator it = _mapGroup.begin();
1582 for ( ; it != _mapGroup.end(); it++ ) {
1583 SMESH_Group* aGroup = it->second;
1584 SMESHDS_GroupBase* aGroupDS = aGroup->GetGroupDS();
1586 std::string aGroupName = aGroup->GetName();
1587 aGroupDS->SetStoreName( aGroupName.c_str() );
1588 myWriter.AddGroup( aGroupDS );
1595 //================================================================================
1597 * \brief Export the mesh to an STL file
1599 //================================================================================
1601 void SMESH_Mesh::ExportSTL(const char * file,
1604 const SMESHDS_Mesh* meshPart) throw(SALOME_Exception)
1606 Unexpect aCatch(SalomeException);
1607 DriverSTL_W_SMDS_Mesh myWriter;
1608 myWriter.SetFile( file );
1609 myWriter.SetIsAscii( isascii );
1610 myWriter.SetMesh( meshPart ? (SMESHDS_Mesh*) meshPart : _myMeshDS);
1611 myWriter.SetMeshId(_id);
1612 if ( name ) myWriter.SetName( name );
1616 //================================================================================
1618 * \brief Export the mesh to the CGNS file
1620 //================================================================================
1622 void SMESH_Mesh::ExportCGNS(const char * file,
1623 const SMESHDS_Mesh* meshDS,
1624 const char * meshName,
1625 const bool groupElemsByType)
1627 int res = Driver_Mesh::DRS_FAIL;
1629 // pass group names to SMESHDS
1630 std::map<int, SMESH_Group*>::iterator it = _mapGroup.begin();
1631 for ( ; it != _mapGroup.end(); it++ ) {
1632 SMESH_Group* group = it->second;
1633 SMESHDS_GroupBase* groupDS = group->GetGroupDS();
1635 std::string groupName = group->GetName();
1636 groupDS->SetStoreName( groupName.c_str() );
1641 DriverCGNS_Write myWriter;
1642 myWriter.SetFile( file );
1643 myWriter.SetMesh( const_cast<SMESHDS_Mesh*>( meshDS ));
1644 myWriter.SetMeshName( SMESH_Comment("Mesh_") << meshDS->GetPersistentId());
1645 if ( meshName && meshName[0] )
1646 myWriter.SetMeshName( meshName );
1647 myWriter.SetElementsByType( groupElemsByType );
1648 res = myWriter.Perform();
1649 if ( res != Driver_Mesh::DRS_OK )
1651 SMESH_ComputeErrorPtr err = myWriter.GetError();
1652 if ( err && !err->IsOK() && !err->myComment.empty() )
1653 throw SALOME_Exception(("Export failed: " + err->myComment ).c_str() );
1657 if ( res != Driver_Mesh::DRS_OK )
1658 throw SALOME_Exception("Export failed");
1661 //================================================================================
1663 * \brief Export the mesh to a GMF file
1665 //================================================================================
1667 void SMESH_Mesh::ExportGMF(const char * file,
1668 const SMESHDS_Mesh* meshDS,
1669 bool withRequiredGroups)
1671 DriverGMF_Write myWriter;
1672 myWriter.SetFile( file );
1673 myWriter.SetMesh( const_cast<SMESHDS_Mesh*>( meshDS ));
1674 myWriter.SetExportRequiredGroups( withRequiredGroups );
1679 //================================================================================
1681 * \brief Return a ratio of "compute cost" of computed sub-meshes to the whole
1684 //================================================================================
1686 double SMESH_Mesh::GetComputeProgress() const
1688 double totalCost = 1e-100, computedCost = 0;
1689 const SMESH_subMesh* curSM = _gen->GetCurrentSubMesh();
1691 // get progress of a current algo
1692 TColStd_MapOfInteger currentSubIds;
1694 if ( SMESH_Algo* algo = curSM->GetAlgo() )
1696 int algoNotDoneCost = 0, algoDoneCost = 0;
1697 const std::vector<SMESH_subMesh*>& smToCompute = algo->SubMeshesToCompute();
1698 for ( size_t i = 0; i < smToCompute.size(); ++i )
1700 if ( smToCompute[i]->IsEmpty() || smToCompute.size() == 1 )
1701 algoNotDoneCost += smToCompute[i]->GetComputeCost();
1703 algoDoneCost += smToCompute[i]->GetComputeCost();
1704 currentSubIds.Add( smToCompute[i]->GetId() );
1710 rate = algo->GetProgress();
1714 std::cerr << "Exception in " << algo->GetName() << "::GetProgress()" << std::endl;
1717 if ( 0. < rate && rate < 1.001 )
1719 computedCost += rate * ( algoDoneCost + algoNotDoneCost );
1723 rate = algo->GetProgressByTic();
1724 computedCost += algoDoneCost + rate * algoNotDoneCost;
1726 // cout << "rate: "<<rate << " algoNotDoneCost: " << algoNotDoneCost << endl;
1729 // get cost of already treated sub-meshes
1730 if ( SMESH_subMesh* mainSM = GetSubMeshContaining( 1 ))
1732 SMESH_subMeshIteratorPtr smIt = mainSM->getDependsOnIterator(/*includeSelf=*/true);
1733 while ( smIt->more() )
1735 const SMESH_subMesh* sm = smIt->next();
1736 const int smCost = sm->GetComputeCost();
1737 totalCost += smCost;
1738 if ( !currentSubIds.Contains( sm->GetId() ) )
1740 if (( !sm->IsEmpty() ) ||
1741 ( sm->GetComputeState() == SMESH_subMesh::FAILED_TO_COMPUTE &&
1742 !sm->DependsOn( curSM ) ))
1743 computedCost += smCost;
1747 // cout << "Total: " << totalCost
1748 // << " computed: " << computedCost << " progress: " << computedCost / totalCost
1749 // << " nbElems: " << GetMeshDS()->GetMeshInfo().NbElements() << endl;
1750 return computedCost / totalCost;
1753 //================================================================================
1755 * \brief Return number of nodes in the mesh
1757 //================================================================================
1759 int SMESH_Mesh::NbNodes() const throw(SALOME_Exception)
1761 Unexpect aCatch(SalomeException);
1762 return _myMeshDS->NbNodes();
1765 //================================================================================
1767 * \brief Return number of edges of given order in the mesh
1769 //================================================================================
1771 int SMESH_Mesh::Nb0DElements() const throw(SALOME_Exception)
1773 Unexpect aCatch(SalomeException);
1774 return _myMeshDS->GetMeshInfo().Nb0DElements();
1777 //================================================================================
1779 * \brief Return number of edges of given order in the mesh
1781 //================================================================================
1783 int SMESH_Mesh::NbEdges(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1785 Unexpect aCatch(SalomeException);
1786 return _myMeshDS->GetMeshInfo().NbEdges(order);
1789 //================================================================================
1791 * \brief Return number of faces of given order in the mesh
1793 //================================================================================
1795 int SMESH_Mesh::NbFaces(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1797 Unexpect aCatch(SalomeException);
1798 return _myMeshDS->GetMeshInfo().NbFaces(order);
1801 //================================================================================
1803 * \brief Return the number of faces in the mesh
1805 //================================================================================
1807 int SMESH_Mesh::NbTriangles(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1809 Unexpect aCatch(SalomeException);
1810 return _myMeshDS->GetMeshInfo().NbTriangles(order);
1813 //================================================================================
1815 * \brief Return number of biquadratic triangles in the mesh
1817 //================================================================================
1819 int SMESH_Mesh::NbBiQuadTriangles() const throw(SALOME_Exception)
1821 Unexpect aCatch(SalomeException);
1822 return _myMeshDS->GetMeshInfo().NbBiQuadTriangles();
1825 //================================================================================
1827 * \brief Return the number nodes faces in the mesh
1829 //================================================================================
1831 int SMESH_Mesh::NbQuadrangles(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1833 Unexpect aCatch(SalomeException);
1834 return _myMeshDS->GetMeshInfo().NbQuadrangles(order);
1837 //================================================================================
1839 * \brief Return number of biquadratic quadrangles in the mesh
1841 //================================================================================
1843 int SMESH_Mesh::NbBiQuadQuadrangles() const throw(SALOME_Exception)
1845 Unexpect aCatch(SalomeException);
1846 return _myMeshDS->GetMeshInfo().NbBiQuadQuadrangles();
1849 //================================================================================
1851 * \brief Return the number of polygonal faces in the mesh
1853 //================================================================================
1855 int SMESH_Mesh::NbPolygons(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1857 Unexpect aCatch(SalomeException);
1858 return _myMeshDS->GetMeshInfo().NbPolygons(order);
1861 //================================================================================
1863 * \brief Return number of volumes of given order in the mesh
1865 //================================================================================
1867 int SMESH_Mesh::NbVolumes(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1869 Unexpect aCatch(SalomeException);
1870 return _myMeshDS->GetMeshInfo().NbVolumes(order);
1873 //================================================================================
1875 * \brief Return number of tetrahedrons of given order in the mesh
1877 //================================================================================
1879 int SMESH_Mesh::NbTetras(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1881 Unexpect aCatch(SalomeException);
1882 return _myMeshDS->GetMeshInfo().NbTetras(order);
1885 //================================================================================
1887 * \brief Return number of hexahedrons of given order in the mesh
1889 //================================================================================
1891 int SMESH_Mesh::NbHexas(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1893 Unexpect aCatch(SalomeException);
1894 return _myMeshDS->GetMeshInfo().NbHexas(order);
1897 //================================================================================
1899 * \brief Return number of triquadratic hexahedrons in the mesh
1901 //================================================================================
1903 int SMESH_Mesh::NbTriQuadraticHexas() const throw(SALOME_Exception)
1905 Unexpect aCatch(SalomeException);
1906 return _myMeshDS->GetMeshInfo().NbTriQuadHexas();
1909 //================================================================================
1911 * \brief Return number of pyramids of given order in the mesh
1913 //================================================================================
1915 int SMESH_Mesh::NbPyramids(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1917 Unexpect aCatch(SalomeException);
1918 return _myMeshDS->GetMeshInfo().NbPyramids(order);
1921 //================================================================================
1923 * \brief Return number of prisms (penthahedrons) of given order in the mesh
1925 //================================================================================
1927 int SMESH_Mesh::NbPrisms(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1929 Unexpect aCatch(SalomeException);
1930 return _myMeshDS->GetMeshInfo().NbPrisms(order);
1933 int SMESH_Mesh::NbQuadPrisms() const throw (SALOME_Exception)
1935 Unexpect aCatch(SalomeException);
1936 return _myMeshDS->GetMeshInfo().NbQuadPrisms();
1939 int SMESH_Mesh::NbBiQuadPrisms() const throw (SALOME_Exception)
1941 Unexpect aCatch(SalomeException);
1942 return _myMeshDS->GetMeshInfo().NbBiQuadPrisms();
1946 //================================================================================
1948 * \brief Return number of hexagonal prisms in the mesh
1950 //================================================================================
1952 int SMESH_Mesh::NbHexagonalPrisms() const throw(SALOME_Exception)
1954 Unexpect aCatch(SalomeException);
1955 return _myMeshDS->GetMeshInfo().NbHexPrisms();
1958 //================================================================================
1960 * \brief Return number of polyhedrons in the mesh
1962 //================================================================================
1964 int SMESH_Mesh::NbPolyhedrons() const throw(SALOME_Exception)
1966 Unexpect aCatch(SalomeException);
1967 return _myMeshDS->GetMeshInfo().NbPolyhedrons();
1970 //================================================================================
1972 * \brief Return number of ball elements in the mesh
1974 //================================================================================
1976 int SMESH_Mesh::NbBalls() const throw(SALOME_Exception)
1978 Unexpect aCatch(SalomeException);
1979 return _myMeshDS->GetMeshInfo().NbBalls();
1982 //================================================================================
1984 * \brief Return number of submeshes in the mesh
1986 //================================================================================
1988 int SMESH_Mesh::NbSubMesh() const throw(SALOME_Exception)
1990 Unexpect aCatch(SalomeException);
1991 return _myMeshDS->NbSubMesh();
1994 //================================================================================
1996 * \brief Returns number of meshes in the Study, that is supposed to be
1997 * equal to SMESHDS_Document::NbMeshes()
1999 //================================================================================
2001 int SMESH_Mesh::NbMeshes() const // nb meshes in the Study
2003 return _myDocument->NbMeshes();
2006 //=======================================================================
2007 //function : IsNotConformAllowed
2008 //purpose : check if a hypothesis allowing notconform mesh is present
2009 //=======================================================================
2011 bool SMESH_Mesh::IsNotConformAllowed() const
2013 if(MYDEBUG) MESSAGE("SMESH_Mesh::IsNotConformAllowed");
2015 static SMESH_HypoFilter filter( SMESH_HypoFilter::HasName( "NotConformAllowed" ));
2016 return GetHypothesis( _myMeshDS->ShapeToMesh(), filter, false );
2019 //=======================================================================
2020 //function : IsMainShape
2022 //=======================================================================
2024 bool SMESH_Mesh::IsMainShape(const TopoDS_Shape& theShape) const
2026 return theShape.IsSame(_myMeshDS->ShapeToMesh() );
2029 //=============================================================================
2033 //=============================================================================
2035 SMESH_Group* SMESH_Mesh::AddGroup (const SMDSAbs_ElementType theType,
2036 const char* theName,
2038 const TopoDS_Shape& theShape,
2039 const SMESH_PredicatePtr& thePredicate)
2041 if ( _mapGroup.count( theId ))
2043 int id = ( theId < 0 ) ? _groupId : theId;
2044 SMESH_Group* aGroup = new SMESH_Group ( id, this, theType, theName, theShape, thePredicate );
2045 GetMeshDS()->AddGroup( aGroup->GetGroupDS() );
2046 _mapGroup[ id ] = aGroup;
2047 _groupId = 1 + _mapGroup.rbegin()->first;
2051 //================================================================================
2053 * \brief Creates a group based on an existing SMESHDS group. Group ID should be unique
2055 //================================================================================
2057 SMESH_Group* SMESH_Mesh::AddGroup (SMESHDS_GroupBase* groupDS) throw(SALOME_Exception)
2060 throw SALOME_Exception(LOCALIZED ("SMESH_Mesh::AddGroup(): NULL SMESHDS_GroupBase"));
2062 std::map <int, SMESH_Group*>::iterator i_g = _mapGroup.find( groupDS->GetID() );
2063 if ( i_g != _mapGroup.end() && i_g->second )
2065 if ( i_g->second->GetGroupDS() == groupDS )
2068 throw SALOME_Exception(LOCALIZED ("SMESH_Mesh::AddGroup() wrong ID of SMESHDS_GroupBase"));
2070 SMESH_Group* aGroup = new SMESH_Group (groupDS);
2071 _mapGroup[ groupDS->GetID() ] = aGroup;
2072 GetMeshDS()->AddGroup( aGroup->GetGroupDS() );
2074 _groupId = 1 + _mapGroup.rbegin()->first;
2080 //================================================================================
2082 * \brief Creates SMESH_Groups for not wrapped SMESHDS_Groups
2083 * \retval bool - true if new SMESH_Groups have been created
2086 //================================================================================
2088 bool SMESH_Mesh::SynchronizeGroups()
2090 const size_t nbGroups = _mapGroup.size();
2091 const std::set<SMESHDS_GroupBase*>& groups = _myMeshDS->GetGroups();
2092 std::set<SMESHDS_GroupBase*>::const_iterator gIt = groups.begin();
2093 for ( ; gIt != groups.end(); ++gIt )
2095 SMESHDS_GroupBase* groupDS = (SMESHDS_GroupBase*) *gIt;
2096 _groupId = groupDS->GetID();
2097 if ( !_mapGroup.count( _groupId ))
2098 _mapGroup[_groupId] = new SMESH_Group( groupDS );
2100 if ( !_mapGroup.empty() )
2101 _groupId = 1 + _mapGroup.rbegin()->first;
2103 return nbGroups < _mapGroup.size();
2106 //================================================================================
2108 * \brief Return iterator on all existing groups
2110 //================================================================================
2112 SMESH_Mesh::GroupIteratorPtr SMESH_Mesh::GetGroups() const
2114 typedef std::map <int, SMESH_Group *> TMap;
2115 return GroupIteratorPtr( new SMDS_mapIterator<TMap>( _mapGroup ));
2118 //=============================================================================
2120 * \brief Return a group by ID
2122 //=============================================================================
2124 SMESH_Group* SMESH_Mesh::GetGroup (const int theGroupID) const
2126 std::map <int, SMESH_Group*>::const_iterator id_grp = _mapGroup.find( theGroupID );
2127 if ( id_grp == _mapGroup.end() )
2129 return id_grp->second;
2133 //=============================================================================
2135 * \brief Return IDs of all groups
2137 //=============================================================================
2139 std::list<int> SMESH_Mesh::GetGroupIds() const
2141 std::list<int> anIds;
2142 std::map<int, SMESH_Group*>::const_iterator it = _mapGroup.begin();
2143 for ( ; it != _mapGroup.end(); it++ )
2144 anIds.push_back( it->first );
2149 //================================================================================
2151 * \brief Set a caller of methods at level of CORBA API implementation.
2152 * The set upCaller will be deleted by SMESH_Mesh
2154 //================================================================================
2156 void SMESH_Mesh::SetCallUp( TCallUp* upCaller )
2158 if ( _callUp ) delete _callUp;
2162 //=============================================================================
2166 //=============================================================================
2168 bool SMESH_Mesh::RemoveGroup( const int theGroupID )
2170 if (_mapGroup.find(theGroupID) == _mapGroup.end())
2172 GetMeshDS()->RemoveGroup( _mapGroup[theGroupID]->GetGroupDS() );
2173 delete _mapGroup[theGroupID];
2174 _mapGroup.erase (theGroupID);
2176 _callUp->RemoveGroup( theGroupID );
2180 //=======================================================================
2181 //function : GetAncestors
2182 //purpose : return list of ancestors of theSubShape in the order
2183 // that lower dimension shapes come first.
2184 //=======================================================================
2186 const TopTools_ListOfShape& SMESH_Mesh::GetAncestors(const TopoDS_Shape& theS) const
2188 if ( _mapAncestors.Contains( theS ) )
2189 return _mapAncestors.FindFromKey( theS );
2191 static TopTools_ListOfShape emptyList;
2195 //=======================================================================
2197 //purpose : dumps contents of mesh to stream [ debug purposes ]
2198 //=======================================================================
2200 ostream& SMESH_Mesh::Dump(ostream& save)
2203 save << "========================== Dump contents of mesh ==========================" << endl << endl;
2204 save << ++clause << ") Total number of nodes: \t" << NbNodes() << endl;
2205 save << ++clause << ") Total number of edges: \t" << NbEdges() << endl;
2206 save << ++clause << ") Total number of faces: \t" << NbFaces() << endl;
2207 save << ++clause << ") Total number of polygons: \t" << NbPolygons() << endl;
2208 save << ++clause << ") Total number of volumes: \t" << NbVolumes() << endl;
2209 save << ++clause << ") Total number of polyhedrons:\t" << NbPolyhedrons() << endl << endl;
2210 for ( int isQuadratic = 0; isQuadratic < 2; ++isQuadratic )
2212 std::string orderStr = isQuadratic ? "quadratic" : "linear";
2213 SMDSAbs_ElementOrder order = isQuadratic ? ORDER_QUADRATIC : ORDER_LINEAR;
2215 save << ++clause << ") Total number of " << orderStr << " edges:\t" << NbEdges(order) << endl;
2216 save << ++clause << ") Total number of " << orderStr << " faces:\t" << NbFaces(order) << endl;
2217 if ( NbFaces(order) > 0 ) {
2218 int nb3 = NbTriangles(order);
2219 int nb4 = NbQuadrangles(order);
2220 save << clause << ".1) Number of " << orderStr << " triangles: \t" << nb3 << endl;
2221 save << clause << ".2) Number of " << orderStr << " quadrangles:\t" << nb4 << endl;
2222 if ( nb3 + nb4 != NbFaces(order) ) {
2223 std::map<int,int> myFaceMap;
2224 SMDS_FaceIteratorPtr itFaces=_myMeshDS->facesIterator();
2225 while( itFaces->more( ) ) {
2226 int nbNodes = itFaces->next()->NbNodes();
2227 if ( myFaceMap.find( nbNodes ) == myFaceMap.end() )
2228 myFaceMap[ nbNodes ] = 0;
2229 myFaceMap[ nbNodes ] = myFaceMap[ nbNodes ] + 1;
2231 save << clause << ".3) Faces in detail: " << endl;
2232 std::map <int,int>::iterator itF;
2233 for (itF = myFaceMap.begin(); itF != myFaceMap.end(); itF++)
2234 save << "--> nb nodes: " << itF->first << " - nb elements:\t" << itF->second << endl;
2237 save << ++clause << ") Total number of " << orderStr << " volumes:\t" << NbVolumes(order) << endl;
2238 if ( NbVolumes(order) > 0 ) {
2239 int nb8 = NbHexas(order);
2240 int nb4 = NbTetras(order);
2241 int nb5 = NbPyramids(order);
2242 int nb6 = NbPrisms(order);
2243 save << clause << ".1) Number of " << orderStr << " hexahedrons: \t" << nb8 << endl;
2244 save << clause << ".2) Number of " << orderStr << " tetrahedrons:\t" << nb4 << endl;
2245 save << clause << ".3) Number of " << orderStr << " prisms: \t" << nb6 << endl;
2246 save << clause << ".4) Number of " << orderStr << " pyramids: \t" << nb5 << endl;
2247 if ( nb8 + nb4 + nb5 + nb6 != NbVolumes(order) ) {
2248 std::map<int,int> myVolumesMap;
2249 SMDS_VolumeIteratorPtr itVolumes=_myMeshDS->volumesIterator();
2250 while( itVolumes->more( ) ) {
2251 int nbNodes = itVolumes->next()->NbNodes();
2252 if ( myVolumesMap.find( nbNodes ) == myVolumesMap.end() )
2253 myVolumesMap[ nbNodes ] = 0;
2254 myVolumesMap[ nbNodes ] = myVolumesMap[ nbNodes ] + 1;
2256 save << clause << ".5) Volumes in detail: " << endl;
2257 std::map <int,int>::iterator itV;
2258 for (itV = myVolumesMap.begin(); itV != myVolumesMap.end(); itV++)
2259 save << "--> nb nodes: " << itV->first << " - nb elements:\t" << itV->second << endl;
2264 save << "===========================================================================" << endl;
2268 //=======================================================================
2269 //function : GetElementType
2270 //purpose : Returns type of mesh element with certain id
2271 //=======================================================================
2273 SMDSAbs_ElementType SMESH_Mesh::GetElementType( const int id, const bool iselem )
2275 return _myMeshDS->GetElementType( id, iselem );
2278 //=============================================================================
2280 * \brief Convert group on geometry into standalone group
2282 //=============================================================================
2284 SMESH_Group* SMESH_Mesh::ConvertToStandalone ( int theGroupID )
2286 SMESH_Group* aGroup = 0;
2287 std::map < int, SMESH_Group * >::iterator itg = _mapGroup.find( theGroupID );
2288 if ( itg == _mapGroup.end() )
2291 SMESH_Group* anOldGrp = (*itg).second;
2292 if ( !anOldGrp || !anOldGrp->GetGroupDS() )
2294 SMESHDS_GroupBase* anOldGrpDS = anOldGrp->GetGroupDS();
2296 // create new standalone group
2297 aGroup = new SMESH_Group (theGroupID, this, anOldGrpDS->GetType(), anOldGrp->GetName() );
2298 _mapGroup[theGroupID] = aGroup;
2300 SMESHDS_Group* aNewGrpDS = dynamic_cast<SMESHDS_Group*>( aGroup->GetGroupDS() );
2301 GetMeshDS()->RemoveGroup( anOldGrpDS );
2302 GetMeshDS()->AddGroup( aNewGrpDS );
2304 // add elements (or nodes) into new created group
2305 SMDS_ElemIteratorPtr anItr = anOldGrpDS->GetElements();
2306 while ( anItr->more() )
2307 aNewGrpDS->Add( (anItr->next())->GetID() );
2310 aNewGrpDS->SetColor( anOldGrpDS->GetColor() );
2318 //=============================================================================
2320 * \brief remove submesh order from Mesh
2322 //=============================================================================
2324 void SMESH_Mesh::ClearMeshOrder()
2326 _mySubMeshOrder.clear();
2329 //=============================================================================
2331 * \brief remove submesh order from Mesh
2333 //=============================================================================
2335 void SMESH_Mesh::SetMeshOrder(const TListOfListOfInt& theOrder )
2337 _mySubMeshOrder = theOrder;
2340 //=============================================================================
2342 * \brief return submesh order if any
2344 //=============================================================================
2346 const TListOfListOfInt& SMESH_Mesh::GetMeshOrder() const
2348 return _mySubMeshOrder;
2351 //=============================================================================
2353 * \brief fill _mapAncestors
2355 //=============================================================================
2357 void SMESH_Mesh::fillAncestorsMap(const TopoDS_Shape& theShape)
2359 int desType, ancType;
2360 if ( !theShape.IsSame( GetShapeToMesh()) && theShape.ShapeType() == TopAbs_COMPOUND )
2362 // a geom group is added. Insert it into lists of ancestors before
2363 // the first ancestor more complex than group members
2364 TopoDS_Iterator subIt( theShape );
2365 if ( !subIt.More() ) return;
2366 int memberType = subIt.Value().ShapeType();
2367 for ( desType = TopAbs_VERTEX; desType >= memberType; desType-- )
2368 for (TopExp_Explorer des( theShape, TopAbs_ShapeEnum( desType )); des.More(); des.Next())
2370 if ( !_mapAncestors.Contains( des.Current() )) continue;// issue 0020982
2371 TopTools_ListOfShape& ancList = _mapAncestors.ChangeFromKey( des.Current() );
2372 TopTools_ListIteratorOfListOfShape ancIt (ancList);
2373 while ( ancIt.More() && ancIt.Value().ShapeType() >= memberType )
2375 if ( ancIt.More() ) ancList.InsertBefore( theShape, ancIt );
2376 else ancList.Append( theShape );
2379 else // else added for 52457: Addition of hypotheses is 8 time longer than meshing
2381 for ( desType = TopAbs_VERTEX; desType > TopAbs_COMPOUND; desType-- )
2382 for ( ancType = desType - 1; ancType >= TopAbs_COMPOUND; ancType-- )
2383 TopExp::MapShapesAndAncestors ( theShape,
2384 (TopAbs_ShapeEnum) desType,
2385 (TopAbs_ShapeEnum) ancType,
2388 // visit COMPOUNDs inside a COMPOUND that are not reachable by TopExp_Explorer
2389 if ( theShape.ShapeType() == TopAbs_COMPOUND )
2391 TopoDS_Iterator sIt(theShape);
2392 if ( sIt.More() && sIt.Value().ShapeType() == TopAbs_COMPOUND )
2393 for ( ; sIt.More(); sIt.Next() )
2394 if ( sIt.Value().ShapeType() == TopAbs_COMPOUND )
2395 fillAncestorsMap( sIt.Value() );
2399 //=============================================================================
2401 * \brief sort submeshes according to stored mesh order
2402 * \param theListToSort in out list to be sorted
2403 * \return FALSE if nothing sorted
2405 //=============================================================================
2407 bool SMESH_Mesh::SortByMeshOrder(std::vector<SMESH_subMesh*>& theListToSort) const
2409 if ( !_mySubMeshOrder.size() || theListToSort.size() < 2)
2413 std::vector<SMESH_subMesh*> onlyOrderedList, smVec;
2415 // collect all ordered submeshes in one list as pointers
2416 // and get their positions within theListToSort
2417 typedef std::vector<SMESH_subMesh*>::iterator TPosInList;
2418 std::map< int, TPosInList > sortedPos;
2419 TPosInList smBeg = theListToSort.begin(), smEnd = theListToSort.end();
2420 TListOfListOfInt::const_iterator listIdsIt = _mySubMeshOrder.begin();
2421 for( ; listIdsIt != _mySubMeshOrder.end(); listIdsIt++)
2423 const TListOfInt& listOfId = *listIdsIt;
2424 // convert sm ids to sm's
2426 TListOfInt::const_iterator idIt = listOfId.begin();
2427 for ( ; idIt != listOfId.end(); idIt++ )
2429 if ( SMESH_subMesh * sm = GetSubMeshContaining( *idIt ))
2431 smVec.push_back( sm );
2432 if ( sm->GetSubMeshDS() && sm->GetSubMeshDS()->IsComplexSubmesh() )
2434 SMESHDS_SubMeshIteratorPtr smdsIt = sm->GetSubMeshDS()->GetSubMeshIterator();
2435 while ( smdsIt->more() )
2437 const SMESHDS_SubMesh* smDS = smdsIt->next();
2438 if (( sm = GetSubMeshContaining( smDS->GetID() )))
2439 smVec.push_back( sm );
2444 // find smVec items in theListToSort
2445 for ( size_t i = 0; i < smVec.size(); ++i )
2447 TPosInList smPos = find( smBeg, smEnd, smVec[i] );
2448 if ( smPos != smEnd ) {
2449 sortedPos[ std::distance( smBeg, smPos )] = smPos;
2450 if ( sortedPos.size() > onlyOrderedList.size() )
2451 onlyOrderedList.push_back( smVec[i] );
2455 if (onlyOrderedList.size() < 2)
2459 std::vector<SMESH_subMesh*>::iterator onlyBIt = onlyOrderedList.begin();
2460 std::vector<SMESH_subMesh*>::iterator onlyEIt = onlyOrderedList.end();
2462 // iterate on ordered sub-meshes and insert them in detected positions
2463 std::map< int, TPosInList >::iterator i_pos = sortedPos.begin();
2464 for ( ; onlyBIt != onlyEIt; ++onlyBIt, ++i_pos )
2465 *(i_pos->second) = *onlyBIt;
2470 //================================================================================
2472 * \brief Return true if given order of sub-meshes is OK
2474 //================================================================================
2476 bool SMESH_Mesh::IsOrderOK( const SMESH_subMesh* smBefore,
2477 const SMESH_subMesh* smAfter ) const
2479 TListOfListOfInt::const_iterator listIdsIt = _mySubMeshOrder.begin();
2480 for( ; listIdsIt != _mySubMeshOrder.end(); listIdsIt++)
2482 const TListOfInt& listOfId = *listIdsIt;
2483 int iB = -1, iA = -1, i = 0;
2484 for ( TListOfInt::const_iterator id = listOfId.begin(); id != listOfId.end(); ++id, ++i )
2486 if ( *id == smBefore->GetId() )
2492 else if ( *id == smAfter->GetId() )
2500 return true; // no order imposed to given sub-meshes
2503 //=============================================================================
2505 * \brief sort submeshes according to stored mesh order
2506 * \param theListToSort in out list to be sorted
2507 * \return FALSE if nothing sorted
2509 //=============================================================================
2511 void SMESH_Mesh::getAncestorsSubMeshes (const TopoDS_Shape& theSubShape,
2512 std::vector< SMESH_subMesh* >& theSubMeshes) const
2514 theSubMeshes.clear();
2515 TopTools_ListIteratorOfListOfShape it( GetAncestors( theSubShape ));
2516 for (; it.More(); it.Next() )
2517 if ( SMESH_subMesh* sm = GetSubMeshContaining( it.Value() ))
2518 theSubMeshes.push_back(sm);
2520 // sort submeshes according to stored mesh order
2521 SortByMeshOrder( theSubMeshes );