1 // Copyright (C) 2007-2012 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.
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 "SMESH_subMesh.hxx"
30 #include "SMESH_Gen.hxx"
31 #include "SMESH_Hypothesis.hxx"
32 #include "SMESH_Group.hxx"
33 #include "SMESH_HypoFilter.hxx"
34 #include "SMESHDS_Group.hxx"
35 #include "SMESHDS_Script.hxx"
36 #include "SMESHDS_GroupOnGeom.hxx"
37 #include "SMESHDS_Document.hxx"
38 #include "SMDS_MeshVolume.hxx"
39 #include "SMDS_SetIterator.hxx"
41 #include "utilities.h"
43 #include "DriverDAT_W_SMDS_Mesh.h"
44 #include "DriverGMF_Read.hxx"
45 #include "DriverGMF_Write.hxx"
46 #include "DriverMED_R_SMESHDS_Mesh.h"
47 #include "DriverMED_W_SMESHDS_Mesh.h"
48 #include "DriverSTL_R_SMDS_Mesh.h"
49 #include "DriverSTL_W_SMDS_Mesh.h"
50 #include "DriverUNV_R_SMDS_Mesh.h"
51 #include "DriverUNV_W_SMDS_Mesh.h"
53 #include "DriverCGNS_Read.hxx"
54 #include "DriverCGNS_Write.hxx"
57 #undef _Precision_HeaderFile
58 #include <BRepBndLib.hxx>
59 #include <BRepPrimAPI_MakeBox.hxx>
60 #include <Bnd_Box.hxx>
62 #include <TopExp_Explorer.hxx>
63 #include <TopTools_ListIteratorOfListOfShape.hxx>
64 #include <TopTools_ListOfShape.hxx>
65 #include <TopTools_MapOfShape.hxx>
66 #include <TopoDS_Iterator.hxx>
68 #include "Utils_ExceptHandlers.hxx"
70 #include <boost/thread/thread.hpp>
71 #include <boost/bind.hpp>
75 // maximum stored group name length in MED file
76 #define MAX_MED_GROUP_NAME_LENGTH 80
79 static int MYDEBUG = 0;
81 static int MYDEBUG = 0;
84 #define cSMESH_Hyp(h) static_cast<const SMESH_Hypothesis*>(h)
86 typedef SMESH_HypoFilter THypType;
88 //=============================================================================
92 //=============================================================================
94 SMESH_Mesh::SMESH_Mesh(int theLocalId,
97 bool theIsEmbeddedMode,
98 SMESHDS_Document* theDocument):
99 _groupId( 0 ), _nbSubShapes( 0 )
101 MESSAGE("SMESH_Mesh::SMESH_Mesh(int localId)");
103 _studyId = theStudyId;
105 _myDocument = theDocument;
106 _idDoc = theDocument->NewMesh(theIsEmbeddedMode);
107 _myMeshDS = theDocument->GetMesh(_idDoc);
108 _isShapeToMesh = false;
109 _isAutoColor = false;
111 _shapeDiagonal = 0.0;
113 _myMeshDS->ShapeToMesh( PseudoShape() );
116 //================================================================================
118 * \brief Constructor of SMESH_Mesh being a base of some descendant class
120 //================================================================================
122 SMESH_Mesh::SMESH_Mesh():
128 _isShapeToMesh( false ),
132 _isAutoColor( false ),
133 _isModified( false ),
134 _shapeDiagonal( 0.0 ),
141 void deleteMeshDS(SMESHDS_Mesh* meshDS)
143 //cout << "deleteMeshDS( " << meshDS << endl;
148 //=============================================================================
152 //=============================================================================
154 SMESH_Mesh::~SMESH_Mesh()
156 MESSAGE("SMESH_Mesh::~SMESH_Mesh");
158 // issue 0020340: EDF 1022 SMESH : Crash with FindNodeClosestTo in a second new study
159 // Notify event listeners at least that something happens
160 if ( SMESH_subMesh * sm = GetSubMeshContaining(1))
161 sm->ComputeStateEngine( SMESH_subMesh::MESH_ENTITY_REMOVED );
164 map < int, SMESH_Group * >::iterator itg;
165 for (itg = _mapGroup.begin(); itg != _mapGroup.end(); itg++) {
166 SMESH_Group *aGroup = (*itg).second;
172 map <int, SMESH_subMesh*>::iterator sm = _mapSubMesh.begin();
173 for ( ; sm != _mapSubMesh.end(); ++sm )
180 if ( _callUp) delete _callUp;
183 // remove self from studyContext
186 StudyContextStruct * studyContext = _gen->GetStudyContext( _studyId );
187 studyContext->mapMesh.erase( _id );
190 _myDocument->RemoveMesh( _id );
194 // delete _myMeshDS, in a thread in order not to block closing a study with large meshes
195 boost::thread aThread(boost::bind( & deleteMeshDS, _myMeshDS ));
198 //================================================================================
200 * \brief Return true if a mesh with given id exists
202 //================================================================================
204 bool SMESH_Mesh::MeshExists( int meshId ) const
206 return _myDocument ? _myDocument->GetMesh( meshId ) : false;
209 //=============================================================================
211 * \brief Set geometry to be meshed
213 //=============================================================================
215 void SMESH_Mesh::ShapeToMesh(const TopoDS_Shape & aShape)
217 if(MYDEBUG) MESSAGE("SMESH_Mesh::ShapeToMesh");
219 if ( !aShape.IsNull() && _isShapeToMesh ) {
220 if ( aShape.ShapeType() != TopAbs_COMPOUND && // group contents is allowed to change
221 _myMeshDS->ShapeToMesh().ShapeType() != TopAbs_COMPOUND )
222 throw SALOME_Exception(LOCALIZED ("a shape to mesh has already been defined"));
224 // clear current data
225 if ( !_myMeshDS->ShapeToMesh().IsNull() )
227 // removal of a shape to mesh, delete objects referring to sub-shapes:
229 map <int, SMESH_subMesh *>::iterator i_sm = _mapSubMesh.begin();
230 for ( ; i_sm != _mapSubMesh.end(); ++i_sm )
233 // - groups on geometry
234 map <int, SMESH_Group *>::iterator i_gr = _mapGroup.begin();
235 while ( i_gr != _mapGroup.end() ) {
236 if ( dynamic_cast<SMESHDS_GroupOnGeom*>( i_gr->second->GetGroupDS() )) {
237 _myMeshDS->RemoveGroup( i_gr->second->GetGroupDS() );
239 _mapGroup.erase( i_gr++ );
244 _mapAncestors.Clear();
247 TopoDS_Shape aNullShape;
248 _myMeshDS->ShapeToMesh( aNullShape );
250 _shapeDiagonal = 0.0;
253 // set a new geometry
254 if ( !aShape.IsNull() )
256 _myMeshDS->ShapeToMesh(aShape);
257 _isShapeToMesh = true;
258 _nbSubShapes = _myMeshDS->MaxShapeIndex();
260 // fill map of ancestors
261 fillAncestorsMap(aShape);
265 _isShapeToMesh = false;
266 _shapeDiagonal = 0.0;
267 _myMeshDS->ShapeToMesh( PseudoShape() );
272 //=======================================================================
274 * \brief Return geometry to be meshed. (It may be a PseudoShape()!)
276 //=======================================================================
278 TopoDS_Shape SMESH_Mesh::GetShapeToMesh() const
280 return _myMeshDS->ShapeToMesh();
283 //=======================================================================
285 * \brief Return a solid which is returned by GetShapeToMesh() if
286 * a real geometry to be meshed was not set
288 //=======================================================================
290 const TopoDS_Solid& SMESH_Mesh::PseudoShape()
292 static TopoDS_Solid aSolid;
293 if ( aSolid.IsNull() )
295 aSolid = BRepPrimAPI_MakeBox(1,1,1);
300 //=======================================================================
302 * \brief Return diagonal size of bounding box of a shape
304 //=======================================================================
306 double SMESH_Mesh::GetShapeDiagonalSize(const TopoDS_Shape & aShape)
308 if ( !aShape.IsNull() ) {
310 BRepBndLib::Add(aShape, Box);
311 return sqrt( Box.SquareExtent() );
316 //=======================================================================
318 * \brief Return diagonal size of bounding box of shape to mesh
320 //=======================================================================
322 double SMESH_Mesh::GetShapeDiagonalSize() const
324 if ( _shapeDiagonal == 0. && _isShapeToMesh )
325 const_cast<SMESH_Mesh*>(this)->_shapeDiagonal = GetShapeDiagonalSize( GetShapeToMesh() );
327 return _shapeDiagonal;
330 //================================================================================
332 * \brief Load mesh from study file
334 //================================================================================
336 void SMESH_Mesh::Load()
342 //=======================================================================
344 * \brief Remove all nodes and elements
346 //=======================================================================
348 void SMESH_Mesh::Clear()
351 _myMeshDS->ClearMesh();
353 // update compute state of submeshes
354 if ( SMESH_subMesh *sm = GetSubMeshContaining( GetShapeToMesh() ) )
356 sm->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
357 sm->ComputeSubMeshStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
358 sm->ComputeStateEngine( SMESH_subMesh::CLEAN ); // for event listeners (issue 0020918)
359 sm->ComputeSubMeshStateEngine( SMESH_subMesh::CLEAN );
364 //=======================================================================
366 * \brief Remove all nodes and elements of indicated shape
368 //=======================================================================
370 void SMESH_Mesh::ClearSubMesh(const int theShapeId)
372 // clear sub-meshes; get ready to re-compute as a side-effect
373 if ( SMESH_subMesh *sm = GetSubMeshContaining( theShapeId ) )
375 SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(/*includeSelf=*/true,
376 /*complexShapeFirst=*/false);
377 while ( smIt->more() )
380 TopAbs_ShapeEnum shapeType = sm->GetSubShape().ShapeType();
381 if ( shapeType == TopAbs_VERTEX || shapeType < TopAbs_SOLID )
382 // all other shapes depends on vertices so they are already cleaned
383 sm->ComputeStateEngine( SMESH_subMesh::CLEAN );
384 // to recompute even if failed
385 sm->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
390 //=======================================================================
391 //function : UNVToMesh
393 //=======================================================================
395 int SMESH_Mesh::UNVToMesh(const char* theFileName)
397 if(MYDEBUG) MESSAGE("UNVToMesh - theFileName = "<<theFileName);
399 throw SALOME_Exception(LOCALIZED("a shape to mesh has already been defined"));
400 _isShapeToMesh = false;
401 DriverUNV_R_SMDS_Mesh myReader;
402 myReader.SetMesh(_myMeshDS);
403 myReader.SetFile(theFileName);
404 myReader.SetMeshId(-1);
407 MESSAGE("UNVToMesh - _myMeshDS->NbNodes() = "<<_myMeshDS->NbNodes());
408 MESSAGE("UNVToMesh - _myMeshDS->NbEdges() = "<<_myMeshDS->NbEdges());
409 MESSAGE("UNVToMesh - _myMeshDS->NbFaces() = "<<_myMeshDS->NbFaces());
410 MESSAGE("UNVToMesh - _myMeshDS->NbVolumes() = "<<_myMeshDS->NbVolumes());
412 SMDS_MeshGroup* aGroup = (SMDS_MeshGroup*) myReader.GetGroup();
414 TGroupNamesMap aGroupNames = myReader.GetGroupNamesMap();
415 //const TGroupIdMap& aGroupId = myReader.GetGroupIdMap();
416 aGroup->InitSubGroupsIterator();
417 while (aGroup->MoreSubGroups()) {
418 SMDS_MeshGroup* aSubGroup = (SMDS_MeshGroup*) aGroup->NextSubGroup();
419 string aName = aGroupNames[aSubGroup];
422 SMESH_Group* aSMESHGroup = AddGroup( aSubGroup->GetType(), aName.c_str(), aId );
424 if(MYDEBUG) MESSAGE("UNVToMesh - group added: "<<aName);
425 SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>( aSMESHGroup->GetGroupDS() );
427 aGroupDS->SetStoreName(aName.c_str());
428 aSubGroup->InitIterator();
429 const SMDS_MeshElement* aElement = 0;
430 while (aSubGroup->More()) {
431 aElement = aSubGroup->Next();
433 aGroupDS->SMDSGroup().Add(aElement);
437 aGroupDS->SetType(aElement->GetType());
445 //=======================================================================
446 //function : MEDToMesh
448 //=======================================================================
450 int SMESH_Mesh::MEDToMesh(const char* theFileName, const char* theMeshName)
452 if(MYDEBUG) MESSAGE("MEDToMesh - theFileName = "<<theFileName<<", mesh name = "<<theMeshName);
454 throw SALOME_Exception(LOCALIZED("a shape to mesh has already been defined"));
455 _isShapeToMesh = false;
456 DriverMED_R_SMESHDS_Mesh myReader;
457 myReader.SetMesh(_myMeshDS);
458 myReader.SetMeshId(-1);
459 myReader.SetFile(theFileName);
460 myReader.SetMeshName(theMeshName);
461 Driver_Mesh::Status status = myReader.Perform();
463 MESSAGE("MEDToMesh - _myMeshDS->NbNodes() = "<<_myMeshDS->NbNodes());
464 MESSAGE("MEDToMesh - _myMeshDS->NbEdges() = "<<_myMeshDS->NbEdges());
465 MESSAGE("MEDToMesh - _myMeshDS->NbFaces() = "<<_myMeshDS->NbFaces());
466 MESSAGE("MEDToMesh - _myMeshDS->NbVolumes() = "<<_myMeshDS->NbVolumes());
469 // Reading groups (sub-meshes are out of scope of MED import functionality)
470 list<TNameAndType> aGroupNames = myReader.GetGroupNamesAndTypes();
471 if(MYDEBUG) MESSAGE("MEDToMesh - Nb groups = "<<aGroupNames.size());
473 list<TNameAndType>::iterator name_type = aGroupNames.begin();
474 for ( ; name_type != aGroupNames.end(); name_type++ ) {
475 SMESH_Group* aGroup = AddGroup( name_type->second, name_type->first.c_str(), anId );
477 if(MYDEBUG) MESSAGE("MEDToMesh - group added: "<<name_type->first.c_str());
478 SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>( aGroup->GetGroupDS() );
480 aGroupDS->SetStoreName( name_type->first.c_str() );
481 myReader.GetGroup( aGroupDS );
488 //=======================================================================
489 //function : STLToMesh
491 //=======================================================================
493 int SMESH_Mesh::STLToMesh(const char* theFileName)
495 if(MYDEBUG) MESSAGE("STLToMesh - theFileName = "<<theFileName);
497 throw SALOME_Exception(LOCALIZED("a shape to mesh has already been defined"));
498 _isShapeToMesh = false;
499 DriverSTL_R_SMDS_Mesh myReader;
500 myReader.SetMesh(_myMeshDS);
501 myReader.SetFile(theFileName);
502 myReader.SetMeshId(-1);
505 MESSAGE("STLToMesh - _myMeshDS->NbNodes() = "<<_myMeshDS->NbNodes());
506 MESSAGE("STLToMesh - _myMeshDS->NbEdges() = "<<_myMeshDS->NbEdges());
507 MESSAGE("STLToMesh - _myMeshDS->NbFaces() = "<<_myMeshDS->NbFaces());
508 MESSAGE("STLToMesh - _myMeshDS->NbVolumes() = "<<_myMeshDS->NbVolumes());
513 //================================================================================
515 * \brief Reads the given mesh from the CGNS file
516 * \param theFileName - name of the file
517 * \retval int - Driver_Mesh::Status
519 //================================================================================
521 int SMESH_Mesh::CGNSToMesh(const char* theFileName,
522 const int theMeshIndex,
523 std::string& theMeshName)
525 int res = Driver_Mesh::DRS_FAIL;
528 DriverCGNS_Read myReader;
529 myReader.SetMesh(_myMeshDS);
530 myReader.SetFile(theFileName);
531 myReader.SetMeshId(theMeshIndex);
532 res = myReader.Perform();
533 theMeshName = myReader.GetMeshName();
542 //================================================================================
544 * \brief Fill its data by reading a GMF file
546 //================================================================================
548 SMESH_ComputeErrorPtr SMESH_Mesh::GMFToMesh(const char* theFileName)
550 DriverGMF_Read myReader;
551 myReader.SetMesh(_myMeshDS);
552 myReader.SetFile(theFileName);
554 //theMeshName = myReader.GetMeshName();
559 return myReader.GetError();
562 //=============================================================================
566 //=============================================================================
568 SMESH_Hypothesis::Hypothesis_Status
569 SMESH_Mesh::AddHypothesis(const TopoDS_Shape & aSubShape,
570 int anHypId ) throw(SALOME_Exception)
572 Unexpect aCatch(SalomeException);
573 if(MYDEBUG) MESSAGE("SMESH_Mesh::AddHypothesis");
575 SMESH_subMesh *subMesh = GetSubMesh(aSubShape);
576 if ( !subMesh || !subMesh->GetId())
577 return SMESH_Hypothesis::HYP_BAD_SUBSHAPE;
579 StudyContextStruct *sc = _gen->GetStudyContext(_studyId);
580 if (sc->mapHypothesis.find(anHypId) == sc->mapHypothesis.end())
582 if(MYDEBUG) MESSAGE("Hypothesis ID does not give an hypothesis");
587 throw SALOME_Exception(LOCALIZED("hypothesis does not exist"));
590 SMESH_Hypothesis *anHyp = sc->mapHypothesis[anHypId];
591 MESSAGE( "SMESH_Mesh::AddHypothesis " << anHyp->GetName() );
593 bool isGlobalHyp = IsMainShape( aSubShape );
595 // NotConformAllowed can be only global
598 // NOTE: this is not a correct way to check a name of hypothesis,
599 // there should be an attribute of hypothesis saying that it can/can't
601 string hypName = anHyp->GetName();
602 if ( hypName == "NotConformAllowed" )
604 if(MYDEBUG) MESSAGE( "Hypotesis <NotConformAllowed> can be only global" );
605 return SMESH_Hypothesis::HYP_INCOMPATIBLE;
611 bool isAlgo = ( !anHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO );
612 int event = isAlgo ? SMESH_subMesh::ADD_ALGO : SMESH_subMesh::ADD_HYP;
614 SMESH_Hypothesis::Hypothesis_Status ret = subMesh->AlgoStateEngine(event, anHyp);
617 if (!SMESH_Hypothesis::IsStatusFatal(ret) &&
618 anHyp->GetDim() <= SMESH_Gen::GetShapeDim(aSubShape)) // is added on father
620 event = isAlgo ? SMESH_subMesh::ADD_FATHER_ALGO : SMESH_subMesh::ADD_FATHER_HYP;
622 SMESH_Hypothesis::Hypothesis_Status ret2 =
623 subMesh->SubMeshesAlgoStateEngine(event, anHyp);
627 // check concurent hypotheses on ancestors
628 if (ret < SMESH_Hypothesis::HYP_CONCURENT && !isGlobalHyp )
630 SMESH_subMeshIteratorPtr smIt = subMesh->getDependsOnIterator(false,false);
631 while ( smIt->more() ) {
632 SMESH_subMesh* sm = smIt->next();
633 if ( sm->IsApplicableHypotesis( anHyp )) {
634 ret2 = sm->CheckConcurentHypothesis( anHyp->GetType() );
643 HasModificationsToDiscard(); // to reset _isModified flag if a mesh becomes empty
645 GetMeshDS()->Modified();
647 if(MYDEBUG) subMesh->DumpAlgoState(true);
648 if(MYDEBUG) SCRUTE(ret);
652 //=============================================================================
656 //=============================================================================
658 SMESH_Hypothesis::Hypothesis_Status
659 SMESH_Mesh::RemoveHypothesis(const TopoDS_Shape & aSubShape,
660 int anHypId)throw(SALOME_Exception)
662 Unexpect aCatch(SalomeException);
663 if(MYDEBUG) MESSAGE("SMESH_Mesh::RemoveHypothesis");
665 StudyContextStruct *sc = _gen->GetStudyContext(_studyId);
666 if (sc->mapHypothesis.find(anHypId) == sc->mapHypothesis.end())
667 throw SALOME_Exception(LOCALIZED("hypothesis does not exist"));
669 SMESH_Hypothesis *anHyp = sc->mapHypothesis[anHypId];
671 int hypType = anHyp->GetType();
677 bool isAlgo = ( !anHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO );
678 int event = isAlgo ? SMESH_subMesh::REMOVE_ALGO : SMESH_subMesh::REMOVE_HYP;
680 SMESH_subMesh *subMesh = GetSubMesh(aSubShape);
682 SMESH_Hypothesis::Hypothesis_Status ret = subMesh->AlgoStateEngine(event, anHyp);
684 // there may appear concurrent hyps that were covered by the removed hyp
685 if (ret < SMESH_Hypothesis::HYP_CONCURENT &&
686 subMesh->IsApplicableHypotesis( anHyp ) &&
687 subMesh->CheckConcurentHypothesis( anHyp->GetType() ) != SMESH_Hypothesis::HYP_OK)
688 ret = SMESH_Hypothesis::HYP_CONCURENT;
691 if (!SMESH_Hypothesis::IsStatusFatal(ret) &&
692 anHyp->GetDim() <= SMESH_Gen::GetShapeDim(aSubShape)) // is removed from father
694 event = isAlgo ? SMESH_subMesh::REMOVE_FATHER_ALGO : SMESH_subMesh::REMOVE_FATHER_HYP;
696 SMESH_Hypothesis::Hypothesis_Status ret2 =
697 subMesh->SubMeshesAlgoStateEngine(event, anHyp);
698 if (ret2 > ret) // more severe
701 // check concurent hypotheses on ancestors
702 if (ret < SMESH_Hypothesis::HYP_CONCURENT && !IsMainShape( aSubShape ) )
704 SMESH_subMeshIteratorPtr smIt = subMesh->getDependsOnIterator(false,false);
705 while ( smIt->more() ) {
706 SMESH_subMesh* sm = smIt->next();
707 if ( sm->IsApplicableHypotesis( anHyp )) {
708 ret2 = sm->CheckConcurentHypothesis( anHyp->GetType() );
718 HasModificationsToDiscard(); // to reset _isModified flag if mesh become empty
720 GetMeshDS()->Modified();
722 if(MYDEBUG) subMesh->DumpAlgoState(true);
723 if(MYDEBUG) SCRUTE(ret);
727 //=============================================================================
731 //=============================================================================
733 const list<const SMESHDS_Hypothesis*>&
734 SMESH_Mesh::GetHypothesisList(const TopoDS_Shape & aSubShape) const
735 throw(SALOME_Exception)
737 Unexpect aCatch(SalomeException);
738 return _myMeshDS->GetHypothesis(aSubShape);
741 //=======================================================================
743 * \brief Return the hypothesis assigned to the shape
744 * \param aSubShape - the shape to check
745 * \param aFilter - the hypothesis filter
746 * \param andAncestors - flag to check hypos assigned to ancestors of the shape
747 * \param assignedTo - to return the shape the found hypo is assigned to
748 * \retval SMESH_Hypothesis* - the first hypo passed through aFilter
750 //=======================================================================
752 const SMESH_Hypothesis * SMESH_Mesh::GetHypothesis(const TopoDS_Shape & aSubShape,
753 const SMESH_HypoFilter& aFilter,
754 const bool andAncestors,
755 TopoDS_Shape* assignedTo) const
758 const list<const SMESHDS_Hypothesis*>& hypList = _myMeshDS->GetHypothesis(aSubShape);
759 list<const SMESHDS_Hypothesis*>::const_iterator hyp = hypList.begin();
760 for ( ; hyp != hypList.end(); hyp++ ) {
761 const SMESH_Hypothesis * h = cSMESH_Hyp( *hyp );
762 if ( aFilter.IsOk( h, aSubShape)) {
763 if ( assignedTo ) *assignedTo = aSubShape;
770 // user sorted submeshes of ancestors, according to stored submesh priority
771 const list<SMESH_subMesh*> smList = getAncestorsSubMeshes( aSubShape );
772 list<SMESH_subMesh*>::const_iterator smIt = smList.begin();
773 for ( ; smIt != smList.end(); smIt++ )
775 const TopoDS_Shape& curSh = (*smIt)->GetSubShape();
776 const list<const SMESHDS_Hypothesis*>& hypList = _myMeshDS->GetHypothesis(curSh);
777 list<const SMESHDS_Hypothesis*>::const_iterator hyp = hypList.begin();
778 for ( ; hyp != hypList.end(); hyp++ ) {
779 const SMESH_Hypothesis * h = cSMESH_Hyp( *hyp );
780 if (aFilter.IsOk( h, curSh )) {
781 if ( assignedTo ) *assignedTo = curSh;
790 //================================================================================
792 * \brief Return hypothesis assigned to the shape
793 * \param aSubShape - the shape to check
794 * \param aFilter - the hypothesis filter
795 * \param aHypList - the list of the found hypotheses
796 * \param andAncestors - flag to check hypos assigned to ancestors of the shape
797 * \retval int - number of unique hypos in aHypList
799 //================================================================================
801 int SMESH_Mesh::GetHypotheses(const TopoDS_Shape & aSubShape,
802 const SMESH_HypoFilter& aFilter,
803 list <const SMESHDS_Hypothesis * >& aHypList,
804 const bool andAncestors) const
806 set<string> hypTypes; // to exclude same type hypos from the result list
809 // only one main hypothesis is allowed
810 bool mainHypFound = false;
813 list<const SMESHDS_Hypothesis*>::const_iterator hyp;
814 for ( hyp = aHypList.begin(); hyp != aHypList.end(); hyp++ ) {
815 if ( hypTypes.insert( (*hyp)->GetName() ).second )
817 if ( !cSMESH_Hyp(*hyp)->IsAuxiliary() )
821 // get hypos from aSubShape
823 const list<const SMESHDS_Hypothesis*>& hypList = _myMeshDS->GetHypothesis(aSubShape);
824 for ( hyp = hypList.begin(); hyp != hypList.end(); hyp++ )
825 if ( aFilter.IsOk (cSMESH_Hyp( *hyp ), aSubShape) &&
826 ( cSMESH_Hyp(*hyp)->IsAuxiliary() || !mainHypFound ) &&
827 hypTypes.insert( (*hyp)->GetName() ).second )
829 aHypList.push_back( *hyp );
831 if ( !cSMESH_Hyp(*hyp)->IsAuxiliary() )
836 // get hypos from ancestors of aSubShape
839 TopTools_MapOfShape map;
841 // user sorted submeshes of ancestors, according to stored submesh priority
842 const list<SMESH_subMesh*> smList = getAncestorsSubMeshes( aSubShape );
843 list<SMESH_subMesh*>::const_iterator smIt = smList.begin();
844 for ( ; smIt != smList.end(); smIt++ )
846 const TopoDS_Shape& curSh = (*smIt)->GetSubShape();
847 if ( !map.Add( curSh ))
849 const list<const SMESHDS_Hypothesis*>& hypList = _myMeshDS->GetHypothesis(curSh);
850 for ( hyp = hypList.begin(); hyp != hypList.end(); hyp++ )
851 if (aFilter.IsOk( cSMESH_Hyp( *hyp ), curSh ) &&
852 ( cSMESH_Hyp(*hyp)->IsAuxiliary() || !mainHypFound ) &&
853 hypTypes.insert( (*hyp)->GetName() ).second )
855 aHypList.push_back( *hyp );
857 if ( !cSMESH_Hyp(*hyp)->IsAuxiliary() )
865 //=============================================================================
869 //=============================================================================
871 const list<SMESHDS_Command*> & SMESH_Mesh::GetLog() throw(SALOME_Exception)
873 Unexpect aCatch(SalomeException);
874 if(MYDEBUG) MESSAGE("SMESH_Mesh::GetLog");
875 return _myMeshDS->GetScript()->GetCommands();
878 //=============================================================================
882 //=============================================================================
883 void SMESH_Mesh::ClearLog() throw(SALOME_Exception)
885 Unexpect aCatch(SalomeException);
886 if(MYDEBUG) MESSAGE("SMESH_Mesh::ClearLog");
887 _myMeshDS->GetScript()->Clear();
890 //=============================================================================
892 * Get or Create the SMESH_subMesh object implementation
894 //=============================================================================
896 SMESH_subMesh *SMESH_Mesh::GetSubMesh(const TopoDS_Shape & aSubShape)
897 throw(SALOME_Exception)
899 Unexpect aCatch(SalomeException);
900 SMESH_subMesh *aSubMesh;
901 int index = _myMeshDS->ShapeToIndex(aSubShape);
903 // for submeshes on GEOM Group
904 if (( !index || index > _nbSubShapes ) && aSubShape.ShapeType() == TopAbs_COMPOUND ) {
905 TopoDS_Iterator it( aSubShape );
908 index = _myMeshDS->AddCompoundSubmesh( aSubShape, it.Value().ShapeType() );
909 if ( index > _nbSubShapes ) _nbSubShapes = index; // not to create sm for this group again
911 // fill map of Ancestors
912 fillAncestorsMap(aSubShape);
916 // return NULL; // neither sub-shape nor a group
918 map <int, SMESH_subMesh *>::iterator i_sm = _mapSubMesh.find(index);
919 if ( i_sm != _mapSubMesh.end())
921 aSubMesh = i_sm->second;
925 aSubMesh = new SMESH_subMesh(index, this, _myMeshDS, aSubShape);
926 _mapSubMesh[index] = aSubMesh;
931 //=============================================================================
933 * Get the SMESH_subMesh object implementation. Dont create it, return null
934 * if it does not exist.
936 //=============================================================================
938 SMESH_subMesh *SMESH_Mesh::GetSubMeshContaining(const TopoDS_Shape & aSubShape) const
939 throw(SALOME_Exception)
941 Unexpect aCatch(SalomeException);
942 SMESH_subMesh *aSubMesh = NULL;
944 int index = _myMeshDS->ShapeToIndex(aSubShape);
946 map <int, SMESH_subMesh *>::const_iterator i_sm = _mapSubMesh.find(index);
947 if ( i_sm != _mapSubMesh.end())
948 aSubMesh = i_sm->second;
952 //=============================================================================
954 * Get the SMESH_subMesh object implementation. Dont create it, return null
955 * if it does not exist.
957 //=============================================================================
959 SMESH_subMesh *SMESH_Mesh::GetSubMeshContaining(const int aShapeID) const
960 throw(SALOME_Exception)
962 Unexpect aCatch(SalomeException);
964 map <int, SMESH_subMesh *>::const_iterator i_sm = _mapSubMesh.find(aShapeID);
965 if (i_sm == _mapSubMesh.end())
969 //================================================================================
971 * \brief Return submeshes of groups containing the given sub-shape
973 //================================================================================
976 SMESH_Mesh::GetGroupSubMeshesContaining(const TopoDS_Shape & aSubShape) const
977 throw(SALOME_Exception)
979 Unexpect aCatch(SalomeException);
980 list<SMESH_subMesh*> found;
982 SMESH_subMesh * subMesh = GetSubMeshContaining(aSubShape);
986 // submeshes of groups have max IDs, so search from the map end
987 map<int, SMESH_subMesh *>::const_reverse_iterator i_sm;
988 for ( i_sm = _mapSubMesh.rbegin(); i_sm != _mapSubMesh.rend(); ++i_sm) {
989 SMESHDS_SubMesh * ds = i_sm->second->GetSubMeshDS();
990 if ( ds && ds->IsComplexSubmesh() ) {
991 if ( SMESH_MesherHelper::IsSubShape( aSubShape, i_sm->second->GetSubShape() ))
993 found.push_back( i_sm->second );
997 break; // the rest sub-meshes are not those of groups
1001 if ( found.empty() ) // maybe the main shape is a COMPOUND (issue 0021530)
1003 if ( SMESH_subMesh * mainSM = GetSubMeshContaining(1))
1004 if ( mainSM->GetSubShape().ShapeType() == TopAbs_COMPOUND )
1006 TopoDS_Iterator it( mainSM->GetSubShape() );
1007 if ( it.Value().ShapeType() == aSubShape.ShapeType() &&
1008 SMESH_MesherHelper::IsSubShape( aSubShape, mainSM->GetSubShape() ))
1009 found.push_back( mainSM );
1014 //=======================================================================
1015 //function : IsUsedHypothesis
1016 //purpose : Return True if anHyp is used to mesh aSubShape
1017 //=======================================================================
1019 bool SMESH_Mesh::IsUsedHypothesis(SMESHDS_Hypothesis * anHyp,
1020 const SMESH_subMesh* aSubMesh)
1022 SMESH_Hypothesis* hyp = static_cast<SMESH_Hypothesis*>(anHyp);
1024 // check if anHyp can be used to mesh aSubMesh
1025 if ( !aSubMesh || !aSubMesh->IsApplicableHypotesis( hyp ))
1028 const TopoDS_Shape & aSubShape = const_cast<SMESH_subMesh*>( aSubMesh )->GetSubShape();
1030 SMESH_Algo *algo = _gen->GetAlgo(*this, aSubShape );
1033 if (anHyp->GetType() > SMESHDS_Hypothesis::PARAM_ALGO)
1034 return ( anHyp == algo );
1036 // algorithm parameter
1039 // look trough hypotheses used by algo
1040 SMESH_HypoFilter hypoKind;
1041 if ( algo->InitCompatibleHypoFilter( hypoKind, !hyp->IsAuxiliary() )) {
1042 list <const SMESHDS_Hypothesis * > usedHyps;
1043 if ( GetHypotheses( aSubShape, hypoKind, usedHyps, true ))
1044 return ( find( usedHyps.begin(), usedHyps.end(), anHyp ) != usedHyps.end() );
1048 // look through all assigned hypotheses
1049 //SMESH_HypoFilter filter( SMESH_HypoFilter::Is( hyp ));
1050 return false; //GetHypothesis( aSubShape, filter, true );
1053 //=============================================================================
1057 //=============================================================================
1059 const list < SMESH_subMesh * >&
1060 SMESH_Mesh::GetSubMeshUsingHypothesis(SMESHDS_Hypothesis * anHyp)
1061 throw(SALOME_Exception)
1063 Unexpect aCatch(SalomeException);
1064 if(MYDEBUG) MESSAGE("SMESH_Mesh::GetSubMeshUsingHypothesis");
1065 map < int, SMESH_subMesh * >::iterator itsm;
1066 _subMeshesUsingHypothesisList.clear();
1067 for (itsm = _mapSubMesh.begin(); itsm != _mapSubMesh.end(); itsm++)
1069 SMESH_subMesh *aSubMesh = (*itsm).second;
1070 if ( IsUsedHypothesis ( anHyp, aSubMesh ))
1071 _subMeshesUsingHypothesisList.push_back(aSubMesh);
1073 return _subMeshesUsingHypothesisList;
1076 //=======================================================================
1077 //function : NotifySubMeshesHypothesisModification
1078 //purpose : Say all submeshes using theChangedHyp that it has been modified
1079 //=======================================================================
1081 void SMESH_Mesh::NotifySubMeshesHypothesisModification(const SMESH_Hypothesis* hyp)
1083 Unexpect aCatch(SalomeException);
1085 if ( !GetMeshDS()->IsUsedHypothesis( hyp ))
1089 _callUp->HypothesisModified();
1091 const SMESH_Algo *foundAlgo = 0;
1092 SMESH_HypoFilter algoKind, compatibleHypoKind;
1093 list <const SMESHDS_Hypothesis * > usedHyps;
1096 map < int, SMESH_subMesh * >::iterator itsm;
1097 for (itsm = _mapSubMesh.begin(); itsm != _mapSubMesh.end(); itsm++)
1099 SMESH_subMesh *aSubMesh = (*itsm).second;
1100 if ( aSubMesh->IsApplicableHypotesis( hyp ))
1102 const TopoDS_Shape & aSubShape = aSubMesh->GetSubShape();
1104 if ( !foundAlgo ) // init filter for algo search
1105 algoKind.Init( THypType::IsAlgo() ).And( THypType::IsApplicableTo( aSubShape ));
1107 const SMESH_Algo *algo = static_cast<const SMESH_Algo*>
1108 ( GetHypothesis( aSubShape, algoKind, true ));
1112 bool sameAlgo = ( algo == foundAlgo );
1113 if ( !sameAlgo && foundAlgo )
1114 sameAlgo = ( strcmp( algo->GetName(), foundAlgo->GetName() ) == 0);
1116 if ( !sameAlgo ) { // init filter for used hypos search
1117 if ( !algo->InitCompatibleHypoFilter( compatibleHypoKind, !hyp->IsAuxiliary() ))
1118 continue; // algo does not use any hypothesis
1122 // check if hyp is used by algo
1124 if ( GetHypotheses( aSubShape, compatibleHypoKind, usedHyps, true ) &&
1125 find( usedHyps.begin(), usedHyps.end(), hyp ) != usedHyps.end() )
1127 aSubMesh->AlgoStateEngine(SMESH_subMesh::MODIF_HYP,
1128 const_cast< SMESH_Hypothesis*>( hyp ));
1133 HasModificationsToDiscard(); // to reset _isModified flag if mesh becomes empty
1134 GetMeshDS()->Modified();
1137 //=============================================================================
1139 * Auto color functionality
1141 //=============================================================================
1142 void SMESH_Mesh::SetAutoColor(bool theAutoColor) throw(SALOME_Exception)
1144 Unexpect aCatch(SalomeException);
1145 _isAutoColor = theAutoColor;
1148 bool SMESH_Mesh::GetAutoColor() throw(SALOME_Exception)
1150 Unexpect aCatch(SalomeException);
1151 return _isAutoColor;
1154 //=======================================================================
1155 //function : SetIsModified
1156 //purpose : Set the flag meaning that the mesh has been edited "manually"
1157 //=======================================================================
1159 void SMESH_Mesh::SetIsModified(bool isModified)
1161 _isModified = isModified;
1164 // check if mesh becomes empty as result of modification
1165 HasModificationsToDiscard();
1168 //=======================================================================
1169 //function : HasModificationsToDiscard
1170 //purpose : Return true if the mesh has been edited since a total re-compute
1171 // and those modifications may prevent successful partial re-compute.
1172 // As a side effect reset _isModified flag if mesh is empty
1174 //=======================================================================
1176 bool SMESH_Mesh::HasModificationsToDiscard() const
1178 if ( ! _isModified )
1181 // return true if the next Compute() will be partial and
1182 // existing but changed elements may prevent successful re-compute
1183 bool hasComputed = false, hasNotComputed = false;
1184 map <int, SMESH_subMesh*>::const_iterator i_sm = _mapSubMesh.begin();
1185 for ( ; i_sm != _mapSubMesh.end() ; ++i_sm )
1186 switch ( i_sm->second->GetSubShape().ShapeType() )
1191 if ( i_sm->second->IsMeshComputed() )
1194 hasNotComputed = true;
1195 if ( hasComputed && hasNotComputed)
1199 if ( NbNodes() < 1 )
1200 const_cast<SMESH_Mesh*>(this)->_isModified = false;
1205 //================================================================================
1207 * \brief Check if any groups of the same type have equal names
1209 //================================================================================
1211 bool SMESH_Mesh::HasDuplicatedGroupNamesMED()
1213 //set<string> aGroupNames; // Corrected for Mantis issue 0020028
1214 map< SMDSAbs_ElementType, set<string> > aGroupNames;
1215 for ( map<int, SMESH_Group*>::iterator it = _mapGroup.begin(); it != _mapGroup.end(); it++ )
1217 SMESH_Group* aGroup = it->second;
1218 SMDSAbs_ElementType aType = aGroup->GetGroupDS()->GetType();
1219 string aGroupName = aGroup->GetName();
1220 aGroupName.resize(MAX_MED_GROUP_NAME_LENGTH);
1221 if (!aGroupNames[aType].insert(aGroupName).second)
1228 //================================================================================
1230 * \brief Export the mesh to a med file
1232 //================================================================================
1234 void SMESH_Mesh::ExportMED(const char * file,
1235 const char* theMeshName,
1238 const SMESHDS_Mesh* meshPart)
1239 throw(SALOME_Exception)
1241 Unexpect aCatch(SalomeException);
1243 DriverMED_W_SMESHDS_Mesh myWriter;
1244 myWriter.SetFile ( file, MED::EVersion(theVersion) );
1245 myWriter.SetMesh ( meshPart ? (SMESHDS_Mesh*) meshPart : _myMeshDS );
1247 myWriter.SetMeshId ( _idDoc );
1249 myWriter.SetMeshId ( -1 );
1250 myWriter.SetMeshName( theMeshName );
1253 if ( theAutoGroups ) {
1254 myWriter.AddGroupOfNodes();
1255 myWriter.AddGroupOfEdges();
1256 myWriter.AddGroupOfFaces();
1257 myWriter.AddGroupOfVolumes();
1260 // Pass groups to writer. Provide unique group names.
1261 //set<string> aGroupNames; // Corrected for Mantis issue 0020028
1264 map< SMDSAbs_ElementType, set<string> > aGroupNames;
1266 int maxNbIter = 10000; // to guarantee cycle finish
1267 for ( map<int, SMESH_Group*>::iterator it = _mapGroup.begin(); it != _mapGroup.end(); it++ ) {
1268 SMESH_Group* aGroup = it->second;
1269 SMESHDS_GroupBase* aGroupDS = aGroup->GetGroupDS();
1271 SMDSAbs_ElementType aType = aGroupDS->GetType();
1272 string aGroupName0 = aGroup->GetName();
1273 aGroupName0.resize(MAX_MED_GROUP_NAME_LENGTH);
1274 string aGroupName = aGroupName0;
1275 for (int i = 1; !aGroupNames[aType].insert(aGroupName).second && i < maxNbIter; i++) {
1276 sprintf(&aString[0], "GR_%d_%s", i, aGroupName0.c_str());
1277 aGroupName = aString;
1278 aGroupName.resize(MAX_MED_GROUP_NAME_LENGTH);
1280 aGroupDS->SetStoreName( aGroupName.c_str() );
1281 myWriter.AddGroup( aGroupDS );
1289 void SMESH_Mesh::ExportSAUV(const char *file,
1290 const char* theMeshName,
1292 throw(SALOME_Exception)
1294 std::string medfilename(file);
1295 medfilename += ".med";
1298 cmd = "%PYTHONBIN% ";
1303 cmd += "from medutilities import my_remove ; my_remove(r'" + medfilename + "')";
1305 system(cmd.c_str());
1306 ExportMED(medfilename.c_str(), theMeshName, theAutoGroups, 1);
1308 cmd = "%PYTHONBIN% ";
1313 cmd += "from medutilities import convert ; convert(r'" + medfilename + "', 'MED', 'GIBI', 1, r'" + file + "')";
1315 system(cmd.c_str());
1317 cmd = "%PYTHONBIN% ";
1322 cmd += "from medutilities import my_remove ; my_remove(r'" + medfilename + "')";
1324 system(cmd.c_str());
1327 //================================================================================
1329 * \brief Export the mesh to a DAT file
1331 //================================================================================
1333 void SMESH_Mesh::ExportDAT(const char * file,
1334 const SMESHDS_Mesh* meshPart) throw(SALOME_Exception)
1336 Unexpect aCatch(SalomeException);
1337 DriverDAT_W_SMDS_Mesh myWriter;
1338 myWriter.SetFile( file );
1339 myWriter.SetMesh( meshPart ? (SMESHDS_Mesh*) meshPart : _myMeshDS );
1340 myWriter.SetMeshId(_idDoc);
1344 //================================================================================
1346 * \brief Export the mesh to an UNV file
1348 //================================================================================
1350 void SMESH_Mesh::ExportUNV(const char * file,
1351 const SMESHDS_Mesh* meshPart) throw(SALOME_Exception)
1353 Unexpect aCatch(SalomeException);
1354 DriverUNV_W_SMDS_Mesh myWriter;
1355 myWriter.SetFile( file );
1356 myWriter.SetMesh( meshPart ? (SMESHDS_Mesh*) meshPart : _myMeshDS );
1357 myWriter.SetMeshId(_idDoc);
1358 // myWriter.SetGroups(_mapGroup);
1362 for ( map<int, SMESH_Group*>::iterator it = _mapGroup.begin(); it != _mapGroup.end(); it++ ) {
1363 SMESH_Group* aGroup = it->second;
1364 SMESHDS_GroupBase* aGroupDS = aGroup->GetGroupDS();
1366 string aGroupName = aGroup->GetName();
1367 aGroupDS->SetStoreName( aGroupName.c_str() );
1368 myWriter.AddGroup( aGroupDS );
1375 //================================================================================
1377 * \brief Export the mesh to an STL file
1379 //================================================================================
1381 void SMESH_Mesh::ExportSTL(const char * file,
1383 const SMESHDS_Mesh* meshPart) throw(SALOME_Exception)
1385 Unexpect aCatch(SalomeException);
1386 DriverSTL_W_SMDS_Mesh myWriter;
1387 myWriter.SetFile( file );
1388 myWriter.SetIsAscii( isascii );
1389 myWriter.SetMesh( meshPart ? (SMESHDS_Mesh*) meshPart : _myMeshDS);
1390 myWriter.SetMeshId(_idDoc);
1394 //================================================================================
1396 * \brief Export the mesh to the CGNS file
1398 //================================================================================
1400 void SMESH_Mesh::ExportCGNS(const char * file,
1401 const SMESHDS_Mesh* meshDS)
1403 int res = Driver_Mesh::DRS_FAIL;
1405 DriverCGNS_Write myWriter;
1406 myWriter.SetFile( file );
1407 myWriter.SetMesh( const_cast<SMESHDS_Mesh*>( meshDS ));
1408 myWriter.SetMeshName( SMESH_Comment("Mesh_") << meshDS->GetPersistentId());
1409 res = myWriter.Perform();
1411 if ( res != Driver_Mesh::DRS_OK )
1412 throw SALOME_Exception("Export failed");
1415 //================================================================================
1417 * \brief Export the mesh to a GMF file
1419 //================================================================================
1421 void SMESH_Mesh::ExportGMF(const char * file,
1422 const SMESHDS_Mesh* meshDS)
1424 DriverGMF_Write myWriter;
1425 myWriter.SetFile( file );
1426 myWriter.SetMesh( const_cast<SMESHDS_Mesh*>( meshDS ));
1430 //================================================================================
1432 * \brief Return number of nodes in the mesh
1434 //================================================================================
1436 int SMESH_Mesh::NbNodes() const throw(SALOME_Exception)
1438 Unexpect aCatch(SalomeException);
1439 return _myMeshDS->NbNodes();
1442 //================================================================================
1444 * \brief Return number of edges of given order in the mesh
1446 //================================================================================
1448 int SMESH_Mesh::Nb0DElements() const throw(SALOME_Exception)
1450 Unexpect aCatch(SalomeException);
1451 return _myMeshDS->GetMeshInfo().Nb0DElements();
1454 //================================================================================
1456 * \brief Return number of edges of given order in the mesh
1458 //================================================================================
1460 int SMESH_Mesh::NbEdges(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1462 Unexpect aCatch(SalomeException);
1463 return _myMeshDS->GetMeshInfo().NbEdges(order);
1466 //================================================================================
1468 * \brief Return number of faces of given order in the mesh
1470 //================================================================================
1472 int SMESH_Mesh::NbFaces(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1474 Unexpect aCatch(SalomeException);
1475 return _myMeshDS->GetMeshInfo().NbFaces(order);
1478 //================================================================================
1480 * \brief Return the number of faces in the mesh
1482 //================================================================================
1484 int SMESH_Mesh::NbTriangles(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1486 Unexpect aCatch(SalomeException);
1487 return _myMeshDS->GetMeshInfo().NbTriangles(order);
1490 //================================================================================
1492 * \brief Return the number nodes faces in the mesh
1494 //================================================================================
1496 int SMESH_Mesh::NbQuadrangles(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1498 Unexpect aCatch(SalomeException);
1499 return _myMeshDS->GetMeshInfo().NbQuadrangles(order);
1502 //================================================================================
1504 * \brief Return number of biquadratic quadrangles in the mesh
1506 //================================================================================
1508 int SMESH_Mesh::NbBiQuadQuadrangles() const throw(SALOME_Exception)
1510 Unexpect aCatch(SalomeException);
1511 return _myMeshDS->GetMeshInfo().NbBiQuadQuadrangles();
1514 //================================================================================
1516 * \brief Return the number of polygonal faces in the mesh
1518 //================================================================================
1520 int SMESH_Mesh::NbPolygons() const throw(SALOME_Exception)
1522 Unexpect aCatch(SalomeException);
1523 return _myMeshDS->GetMeshInfo().NbPolygons();
1526 //================================================================================
1528 * \brief Return number of volumes of given order in the mesh
1530 //================================================================================
1532 int SMESH_Mesh::NbVolumes(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1534 Unexpect aCatch(SalomeException);
1535 return _myMeshDS->GetMeshInfo().NbVolumes(order);
1538 //================================================================================
1540 * \brief Return number of tetrahedrons of given order in the mesh
1542 //================================================================================
1544 int SMESH_Mesh::NbTetras(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1546 Unexpect aCatch(SalomeException);
1547 return _myMeshDS->GetMeshInfo().NbTetras(order);
1550 //================================================================================
1552 * \brief Return number of hexahedrons of given order in the mesh
1554 //================================================================================
1556 int SMESH_Mesh::NbHexas(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1558 Unexpect aCatch(SalomeException);
1559 return _myMeshDS->GetMeshInfo().NbHexas(order);
1562 //================================================================================
1564 * \brief Return number of triquadratic hexahedrons in the mesh
1566 //================================================================================
1568 int SMESH_Mesh::NbTriQuadraticHexas() const throw(SALOME_Exception)
1570 Unexpect aCatch(SalomeException);
1571 return _myMeshDS->GetMeshInfo().NbTriQuadHexas();
1574 //================================================================================
1576 * \brief Return number of pyramids of given order in the mesh
1578 //================================================================================
1580 int SMESH_Mesh::NbPyramids(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1582 Unexpect aCatch(SalomeException);
1583 return _myMeshDS->GetMeshInfo().NbPyramids(order);
1586 //================================================================================
1588 * \brief Return number of prisms (penthahedrons) of given order in the mesh
1590 //================================================================================
1592 int SMESH_Mesh::NbPrisms(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1594 Unexpect aCatch(SalomeException);
1595 return _myMeshDS->GetMeshInfo().NbPrisms(order);
1598 //================================================================================
1600 * \brief Return number of hexagonal prisms in the mesh
1602 //================================================================================
1604 int SMESH_Mesh::NbHexagonalPrisms() const throw(SALOME_Exception)
1606 Unexpect aCatch(SalomeException);
1607 return _myMeshDS->GetMeshInfo().NbHexPrisms();
1610 //================================================================================
1612 * \brief Return number of polyhedrons in the mesh
1614 //================================================================================
1616 int SMESH_Mesh::NbPolyhedrons() const throw(SALOME_Exception)
1618 Unexpect aCatch(SalomeException);
1619 return _myMeshDS->GetMeshInfo().NbPolyhedrons();
1622 //================================================================================
1624 * \brief Return number of ball elements in the mesh
1626 //================================================================================
1628 int SMESH_Mesh::NbBalls() const throw(SALOME_Exception)
1630 Unexpect aCatch(SalomeException);
1631 return _myMeshDS->GetMeshInfo().NbBalls();
1634 //================================================================================
1636 * \brief Return number of submeshes in the mesh
1638 //================================================================================
1640 int SMESH_Mesh::NbSubMesh() const throw(SALOME_Exception)
1642 Unexpect aCatch(SalomeException);
1643 return _myMeshDS->NbSubMesh();
1646 //=======================================================================
1647 //function : IsNotConformAllowed
1648 //purpose : check if a hypothesis alowing notconform mesh is present
1649 //=======================================================================
1651 bool SMESH_Mesh::IsNotConformAllowed() const
1653 if(MYDEBUG) MESSAGE("SMESH_Mesh::IsNotConformAllowed");
1655 static SMESH_HypoFilter filter( SMESH_HypoFilter::HasName( "NotConformAllowed" ));
1656 return GetHypothesis( _myMeshDS->ShapeToMesh(), filter, false );
1659 //=======================================================================
1660 //function : IsMainShape
1662 //=======================================================================
1664 bool SMESH_Mesh::IsMainShape(const TopoDS_Shape& theShape) const
1666 return theShape.IsSame(_myMeshDS->ShapeToMesh() );
1669 //=============================================================================
1673 //=============================================================================
1675 SMESH_Group* SMESH_Mesh::AddGroup (const SMDSAbs_ElementType theType,
1676 const char* theName,
1678 const TopoDS_Shape& theShape,
1679 const SMESH_PredicatePtr& thePredicate)
1681 if (_mapGroup.count(_groupId))
1684 SMESH_Group* aGroup = new SMESH_Group (theId, this, theType, theName, theShape, thePredicate);
1685 GetMeshDS()->AddGroup( aGroup->GetGroupDS() );
1686 _mapGroup[_groupId++] = aGroup;
1690 //================================================================================
1692 * \brief Creates a group based on an existing SMESHDS group. Group ID should be unique
1694 //================================================================================
1696 SMESH_Group* SMESH_Mesh::AddGroup (SMESHDS_GroupBase* groupDS) throw(SALOME_Exception)
1699 throw SALOME_Exception(LOCALIZED ("SMESH_Mesh::AddGroup(): NULL SMESHDS_GroupBase"));
1701 map <int, SMESH_Group*>::iterator i_g = _mapGroup.find( groupDS->GetID() );
1702 if ( i_g != _mapGroup.end() && i_g->second )
1704 if ( i_g->second->GetGroupDS() == groupDS )
1707 throw SALOME_Exception(LOCALIZED ("SMESH_Mesh::AddGroup() wrong ID of SMESHDS_GroupBase"));
1709 SMESH_Group* aGroup = new SMESH_Group (groupDS);
1710 _mapGroup[ groupDS->GetID() ] = aGroup;
1711 GetMeshDS()->AddGroup( aGroup->GetGroupDS() );
1713 _groupId = 1 + _mapGroup.rbegin()->first;
1719 //================================================================================
1721 * \brief Creates SMESH_Groups for not wrapped SMESHDS_Groups
1722 * \retval bool - true if new SMESH_Groups have been created
1725 //================================================================================
1727 bool SMESH_Mesh::SynchronizeGroups()
1729 int nbGroups = _mapGroup.size();
1730 const set<SMESHDS_GroupBase*>& groups = _myMeshDS->GetGroups();
1731 set<SMESHDS_GroupBase*>::const_iterator gIt = groups.begin();
1732 for ( ; gIt != groups.end(); ++gIt )
1734 SMESHDS_GroupBase* groupDS = (SMESHDS_GroupBase*) *gIt;
1735 _groupId = groupDS->GetID();
1736 if ( !_mapGroup.count( _groupId ))
1737 _mapGroup[_groupId] = new SMESH_Group( groupDS );
1739 if ( !_mapGroup.empty() )
1740 _groupId = _mapGroup.rbegin()->first + 1;
1742 return nbGroups < _mapGroup.size();
1745 //================================================================================
1747 * \brief Return iterator on all existing groups
1749 //================================================================================
1751 SMESH_Mesh::GroupIteratorPtr SMESH_Mesh::GetGroups() const
1753 typedef map <int, SMESH_Group *> TMap;
1754 return GroupIteratorPtr( new SMDS_mapIterator<TMap>( _mapGroup ));
1757 //=============================================================================
1759 * \brief Return a group by ID
1761 //=============================================================================
1763 SMESH_Group* SMESH_Mesh::GetGroup (const int theGroupID)
1765 if (_mapGroup.find(theGroupID) == _mapGroup.end())
1767 return _mapGroup[theGroupID];
1771 //=============================================================================
1773 * \brief Return IDs of all groups
1775 //=============================================================================
1777 list<int> SMESH_Mesh::GetGroupIds() const
1780 for ( map<int, SMESH_Group*>::const_iterator it = _mapGroup.begin(); it != _mapGroup.end(); it++ )
1781 anIds.push_back( it->first );
1786 //================================================================================
1788 * \brief Set a caller of methods at level of CORBA API implementation.
1789 * The set upCaller will be deleted by SMESH_Mesh
1791 //================================================================================
1793 void SMESH_Mesh::SetCallUp( TCallUp* upCaller )
1795 if ( _callUp ) delete _callUp;
1799 //=============================================================================
1803 //=============================================================================
1805 bool SMESH_Mesh::RemoveGroup (const int theGroupID)
1807 if (_mapGroup.find(theGroupID) == _mapGroup.end())
1809 GetMeshDS()->RemoveGroup( _mapGroup[theGroupID]->GetGroupDS() );
1810 delete _mapGroup[theGroupID];
1811 _mapGroup.erase (theGroupID);
1813 _callUp->RemoveGroup( theGroupID );
1817 //=======================================================================
1818 //function : GetAncestors
1819 //purpose : return list of ancestors of theSubShape in the order
1820 // that lower dimention shapes come first.
1821 //=======================================================================
1823 const TopTools_ListOfShape& SMESH_Mesh::GetAncestors(const TopoDS_Shape& theS) const
1825 if ( _mapAncestors.Contains( theS ) )
1826 return _mapAncestors.FindFromKey( theS );
1828 static TopTools_ListOfShape emptyList;
1832 //=======================================================================
1834 //purpose : dumps contents of mesh to stream [ debug purposes ]
1835 //=======================================================================
1837 ostream& SMESH_Mesh::Dump(ostream& save)
1840 save << "========================== Dump contents of mesh ==========================" << endl << endl;
1841 save << ++clause << ") Total number of nodes: \t" << NbNodes() << endl;
1842 save << ++clause << ") Total number of edges: \t" << NbEdges() << endl;
1843 save << ++clause << ") Total number of faces: \t" << NbFaces() << endl;
1844 save << ++clause << ") Total number of polygons:\t" << NbPolygons() << endl;
1845 save << ++clause << ") Total number of volumes:\t" << NbVolumes() << endl;
1846 save << ++clause << ") Total number of polyhedrons:\t" << NbPolyhedrons() << endl << endl;
1847 for ( int isQuadratic = 0; isQuadratic < 2; ++isQuadratic )
1849 string orderStr = isQuadratic ? "quadratic" : "linear";
1850 SMDSAbs_ElementOrder order = isQuadratic ? ORDER_QUADRATIC : ORDER_LINEAR;
1852 save << ++clause << ") Total number of " << orderStr << " edges:\t" << NbEdges(order) << endl;
1853 save << ++clause << ") Total number of " << orderStr << " faces:\t" << NbFaces(order) << endl;
1854 if ( NbFaces(order) > 0 ) {
1855 int nb3 = NbTriangles(order);
1856 int nb4 = NbQuadrangles(order);
1857 save << clause << ".1) Number of " << orderStr << " triangles: \t" << nb3 << endl;
1858 save << clause << ".2) Number of " << orderStr << " quadrangles:\t" << nb4 << endl;
1859 if ( nb3 + nb4 != NbFaces(order) ) {
1860 map<int,int> myFaceMap;
1861 SMDS_FaceIteratorPtr itFaces=_myMeshDS->facesIterator();
1862 while( itFaces->more( ) ) {
1863 int nbNodes = itFaces->next()->NbNodes();
1864 if ( myFaceMap.find( nbNodes ) == myFaceMap.end() )
1865 myFaceMap[ nbNodes ] = 0;
1866 myFaceMap[ nbNodes ] = myFaceMap[ nbNodes ] + 1;
1868 save << clause << ".3) Faces in detail: " << endl;
1869 map <int,int>::iterator itF;
1870 for (itF = myFaceMap.begin(); itF != myFaceMap.end(); itF++)
1871 save << "--> nb nodes: " << itF->first << " - nb elemens:\t" << itF->second << endl;
1874 save << ++clause << ") Total number of " << orderStr << " volumes:\t" << NbVolumes(order) << endl;
1875 if ( NbVolumes(order) > 0 ) {
1876 int nb8 = NbHexas(order);
1877 int nb4 = NbTetras(order);
1878 int nb5 = NbPyramids(order);
1879 int nb6 = NbPrisms(order);
1880 save << clause << ".1) Number of " << orderStr << " hexahedrons:\t" << nb8 << endl;
1881 save << clause << ".2) Number of " << orderStr << " tetrahedrons:\t" << nb4 << endl;
1882 save << clause << ".3) Number of " << orderStr << " prisms: \t" << nb6 << endl;
1883 save << clause << ".4) Number of " << orderStr << " pyramids:\t" << nb5 << endl;
1884 if ( nb8 + nb4 + nb5 + nb6 != NbVolumes(order) ) {
1885 map<int,int> myVolumesMap;
1886 SMDS_VolumeIteratorPtr itVolumes=_myMeshDS->volumesIterator();
1887 while( itVolumes->more( ) ) {
1888 int nbNodes = itVolumes->next()->NbNodes();
1889 if ( myVolumesMap.find( nbNodes ) == myVolumesMap.end() )
1890 myVolumesMap[ nbNodes ] = 0;
1891 myVolumesMap[ nbNodes ] = myVolumesMap[ nbNodes ] + 1;
1893 save << clause << ".5) Volumes in detail: " << endl;
1894 map <int,int>::iterator itV;
1895 for (itV = myVolumesMap.begin(); itV != myVolumesMap.end(); itV++)
1896 save << "--> nb nodes: " << itV->first << " - nb elemens:\t" << itV->second << endl;
1901 save << "===========================================================================" << endl;
1905 //=======================================================================
1906 //function : GetElementType
1907 //purpose : Returns type of mesh element with certain id
1908 //=======================================================================
1910 SMDSAbs_ElementType SMESH_Mesh::GetElementType( const int id, const bool iselem )
1912 return _myMeshDS->GetElementType( id, iselem );
1915 //=============================================================================
1917 * \brief Convert group on geometry into standalone group
1919 //=============================================================================
1921 SMESH_Group* SMESH_Mesh::ConvertToStandalone ( int theGroupID )
1923 SMESH_Group* aGroup = 0;
1924 map < int, SMESH_Group * >::iterator itg = _mapGroup.find( theGroupID );
1925 if ( itg == _mapGroup.end() )
1928 SMESH_Group* anOldGrp = (*itg).second;
1929 SMESHDS_GroupBase* anOldGrpDS = anOldGrp->GetGroupDS();
1930 if ( !anOldGrp || !anOldGrpDS )
1933 // create new standalone group
1934 aGroup = new SMESH_Group (theGroupID, this, anOldGrpDS->GetType(), anOldGrp->GetName() );
1935 _mapGroup[theGroupID] = aGroup;
1937 SMESHDS_Group* aNewGrpDS = dynamic_cast<SMESHDS_Group*>( aGroup->GetGroupDS() );
1938 GetMeshDS()->RemoveGroup( anOldGrpDS );
1939 GetMeshDS()->AddGroup( aNewGrpDS );
1941 // add elements (or nodes) into new created group
1942 SMDS_ElemIteratorPtr anItr = anOldGrpDS->GetElements();
1943 while ( anItr->more() )
1944 aNewGrpDS->Add( (anItr->next())->GetID() );
1947 aNewGrpDS->SetColor( anOldGrpDS->GetColor() );
1955 //=============================================================================
1957 * \brief remove submesh order from Mesh
1959 //=============================================================================
1961 void SMESH_Mesh::ClearMeshOrder()
1963 _mySubMeshOrder.clear();
1966 //=============================================================================
1968 * \brief remove submesh order from Mesh
1970 //=============================================================================
1972 void SMESH_Mesh::SetMeshOrder(const TListOfListOfInt& theOrder )
1974 _mySubMeshOrder = theOrder;
1977 //=============================================================================
1979 * \brief return submesh order if any
1981 //=============================================================================
1983 const TListOfListOfInt& SMESH_Mesh::GetMeshOrder() const
1985 return _mySubMeshOrder;
1988 //=============================================================================
1990 * \brief fill _mapAncestors
1992 //=============================================================================
1994 void SMESH_Mesh::fillAncestorsMap(const TopoDS_Shape& theShape)
1997 int desType, ancType;
1998 if ( !theShape.IsSame( GetShapeToMesh()) && theShape.ShapeType() == TopAbs_COMPOUND )
2000 // a geom group is added. Insert it into lists of ancestors before
2001 // the first ancestor more complex than group members
2002 int memberType = TopoDS_Iterator( theShape ).Value().ShapeType();
2003 for ( desType = TopAbs_VERTEX; desType >= memberType; desType-- )
2004 for (TopExp_Explorer des( theShape, TopAbs_ShapeEnum( desType )); des.More(); des.Next())
2006 if ( !_mapAncestors.Contains( des.Current() )) continue;// issue 0020982
2007 TopTools_ListOfShape& ancList = _mapAncestors.ChangeFromKey( des.Current() );
2008 TopTools_ListIteratorOfListOfShape ancIt (ancList);
2009 while ( ancIt.More() && ancIt.Value().ShapeType() >= memberType )
2012 ancList.InsertBefore( theShape, ancIt );
2016 for ( desType = TopAbs_VERTEX; desType > TopAbs_COMPOUND; desType-- )
2017 for ( ancType = desType - 1; ancType >= TopAbs_COMPOUND; ancType-- )
2018 TopExp::MapShapesAndAncestors ( theShape,
2019 (TopAbs_ShapeEnum) desType,
2020 (TopAbs_ShapeEnum) ancType,
2023 // visit COMPOUNDs inside a COMPOUND that are not reachable by TopExp_Explorer
2024 if ( theShape.ShapeType() == TopAbs_COMPOUND )
2026 for ( TopoDS_Iterator sIt(theShape); sIt.More(); sIt.Next() )
2027 if ( sIt.Value().ShapeType() == TopAbs_COMPOUND )
2028 fillAncestorsMap( sIt.Value() );
2032 //=============================================================================
2034 * \brief sort submeshes according to stored mesh order
2035 * \param theListToSort in out list to be sorted
2036 * \return FALSE if nothing sorted
2038 //=============================================================================
2040 bool SMESH_Mesh::SortByMeshOrder(list<SMESH_subMesh*>& theListToSort) const
2042 if ( !_mySubMeshOrder.size() || theListToSort.size() < 2)
2046 list<SMESH_subMesh*> onlyOrderedList;
2047 // collect all ordered submeshes in one list as pointers
2048 // and get their positions within theListToSort
2049 typedef list<SMESH_subMesh*>::iterator TPosInList;
2050 map< int, TPosInList > sortedPos;
2051 TPosInList smBeg = theListToSort.begin(), smEnd = theListToSort.end();
2052 TListOfListOfInt::const_iterator listIdsIt = _mySubMeshOrder.begin();
2053 for( ; listIdsIt != _mySubMeshOrder.end(); listIdsIt++) {
2054 const TListOfInt& listOfId = *listIdsIt;
2055 TListOfInt::const_iterator idIt = listOfId.begin();
2056 for ( ; idIt != listOfId.end(); idIt++ ) {
2057 if ( SMESH_subMesh * sm = GetSubMeshContaining( *idIt )) {
2058 TPosInList smPos = find( smBeg, smEnd, sm );
2059 if ( smPos != smEnd ) {
2060 onlyOrderedList.push_back( sm );
2061 sortedPos[ distance( smBeg, smPos )] = smPos;
2066 if (onlyOrderedList.size() < 2)
2070 list<SMESH_subMesh*>::iterator onlyBIt = onlyOrderedList.begin();
2071 list<SMESH_subMesh*>::iterator onlyEIt = onlyOrderedList.end();
2073 // iterate on ordered submeshes and insert them in detected positions
2074 map< int, TPosInList >::iterator i_pos = sortedPos.begin();
2075 for ( ; onlyBIt != onlyEIt; ++onlyBIt, ++i_pos )
2076 *(i_pos->second) = *onlyBIt;
2081 //================================================================================
2083 * \brief Return true if given order of sub-meshes is OK
2085 //================================================================================
2087 bool SMESH_Mesh::IsOrderOK( const SMESH_subMesh* smBefore,
2088 const SMESH_subMesh* smAfter ) const
2090 TListOfListOfInt::const_iterator listIdsIt = _mySubMeshOrder.begin();
2091 TListOfInt::const_iterator idBef, idAft;
2092 for( ; listIdsIt != _mySubMeshOrder.end(); listIdsIt++)
2094 const TListOfInt& listOfId = *listIdsIt;
2095 idBef = std::find( listOfId.begin(), listOfId.end(), smBefore->GetId() );
2096 if ( idBef != listOfId.end() )
2097 idAft = std::find( listOfId.begin(), listOfId.end(), smAfter->GetId() );
2098 if ( idAft != listOfId.end () )
2099 return ( std::distance( listOfId.begin(), idBef ) <
2100 std::distance( listOfId.begin(), idAft ) );
2102 return true; // no order imposed to given submeshes
2105 //=============================================================================
2107 * \brief sort submeshes according to stored mesh order
2108 * \param theListToSort in out list to be sorted
2109 * \return FALSE if nothing sorted
2111 //=============================================================================
2113 list<SMESH_subMesh*>
2114 SMESH_Mesh::getAncestorsSubMeshes (const TopoDS_Shape& theSubShape) const
2116 list<SMESH_subMesh*> listOfSubMesh;
2117 TopTools_ListIteratorOfListOfShape it( GetAncestors( theSubShape ));
2118 for (; it.More(); it.Next() )
2119 if ( SMESH_subMesh* sm = GetSubMeshContaining( it.Value() ))
2120 listOfSubMesh.push_back(sm);
2122 // sort submeshes according to stored mesh order
2123 SortByMeshOrder( listOfSubMesh );
2125 return listOfSubMesh;