1 // Copyright (C) 2007-2011 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 // SMESH SMESH : implementaion of SMESH idl descriptions
24 // File : SMESH_Mesh.cxx
25 // Author : Paul RASCLE, EDF
28 #include "SMESH_Mesh.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 "DriverMED_W_SMESHDS_Mesh.h"
44 #include "DriverDAT_W_SMDS_Mesh.h"
45 #include "DriverUNV_W_SMDS_Mesh.h"
46 #include "DriverSTL_W_SMDS_Mesh.h"
48 #include "DriverMED_R_SMESHDS_Mesh.h"
49 #include "DriverUNV_R_SMDS_Mesh.h"
50 #include "DriverSTL_R_SMDS_Mesh.h"
52 #include "DriverCGNS_Read.hxx"
53 #include "DriverCGNS_Write.hxx"
56 #undef _Precision_HeaderFile
57 #include <BRepBndLib.hxx>
58 #include <BRepPrimAPI_MakeBox.hxx>
59 #include <Bnd_Box.hxx>
61 #include <TopExp_Explorer.hxx>
62 #include <TopTools_ListIteratorOfListOfShape.hxx>
63 #include <TopTools_ListOfShape.hxx>
64 #include <TopTools_MapOfShape.hxx>
65 #include <TopoDS_Iterator.hxx>
67 #include "Utils_ExceptHandlers.hxx"
69 #include <boost/thread/thread.hpp>
70 #include <boost/bind.hpp>
74 // maximum stored group name length in MED file
75 #define MAX_MED_GROUP_NAME_LENGTH 80
78 static int MYDEBUG = 0;
80 static int MYDEBUG = 0;
83 #define cSMESH_Hyp(h) static_cast<const SMESH_Hypothesis*>(h)
85 typedef SMESH_HypoFilter THypType;
87 //=============================================================================
91 //=============================================================================
93 SMESH_Mesh::SMESH_Mesh(int theLocalId,
96 bool theIsEmbeddedMode,
97 SMESHDS_Document* theDocument):
98 _groupId( 0 ), _nbSubShapes( 0 )
100 MESSAGE("SMESH_Mesh::SMESH_Mesh(int localId)");
102 _studyId = theStudyId;
104 _myDocument = theDocument;
105 _idDoc = theDocument->NewMesh(theIsEmbeddedMode);
106 _myMeshDS = theDocument->GetMesh(_idDoc);
107 _isShapeToMesh = false;
108 _isAutoColor = false;
110 _shapeDiagonal = 0.0;
112 _myMeshDS->ShapeToMesh( PseudoShape() );
115 //================================================================================
117 * \brief Constructor of SMESH_Mesh being a base of some descendant class
119 //================================================================================
121 SMESH_Mesh::SMESH_Mesh():
127 _isShapeToMesh( false ),
131 _isAutoColor( false ),
132 _isModified( false ),
133 _shapeDiagonal( 0.0 ),
140 void deleteMeshDS(SMESHDS_Mesh* meshDS)
142 //cout << "deleteMeshDS( " << meshDS << endl;
147 //=============================================================================
151 //=============================================================================
153 SMESH_Mesh::~SMESH_Mesh()
155 MESSAGE("SMESH_Mesh::~SMESH_Mesh");
157 // issue 0020340: EDF 1022 SMESH : Crash with FindNodeClosestTo in a second new study
158 // Notify event listeners at least that something happens
159 if ( SMESH_subMesh * sm = GetSubMeshContaining(1))
160 sm->ComputeStateEngine( SMESH_subMesh::MESH_ENTITY_REMOVED );
163 map < int, SMESH_Group * >::iterator itg;
164 for (itg = _mapGroup.begin(); itg != _mapGroup.end(); itg++) {
165 SMESH_Group *aGroup = (*itg).second;
171 map <int, SMESH_subMesh*>::iterator sm = _mapSubMesh.begin();
172 for ( ; sm != _mapSubMesh.end(); ++sm )
179 if ( _callUp) delete _callUp;
182 // remove self from studyContext
185 StudyContextStruct * studyContext = _gen->GetStudyContext( _studyId );
186 studyContext->mapMesh.erase( _id );
189 _myDocument->RemoveMesh( _id );
193 // delete _myMeshDS, in a thread in order not to block closing a study with large meshes
194 boost::thread aThread(boost::bind( & deleteMeshDS, _myMeshDS ));
197 //================================================================================
199 * \brief Return true if a mesh with given id exists
201 //================================================================================
203 bool SMESH_Mesh::MeshExists( int meshId ) const
205 return _myDocument ? _myDocument->GetMesh( meshId ) : false;
208 //=============================================================================
210 * \brief Set geometry to be meshed
212 //=============================================================================
214 void SMESH_Mesh::ShapeToMesh(const TopoDS_Shape & aShape)
216 if(MYDEBUG) MESSAGE("SMESH_Mesh::ShapeToMesh");
218 if ( !aShape.IsNull() && _isShapeToMesh ) {
219 if ( aShape.ShapeType() != TopAbs_COMPOUND && // group contents is allowed to change
220 _myMeshDS->ShapeToMesh().ShapeType() != TopAbs_COMPOUND )
221 throw SALOME_Exception(LOCALIZED ("a shape to mesh has already been defined"));
223 // clear current data
224 if ( !_myMeshDS->ShapeToMesh().IsNull() )
226 // removal of a shape to mesh, delete objects referring to sub-shapes:
228 map <int, SMESH_subMesh *>::iterator i_sm = _mapSubMesh.begin();
229 for ( ; i_sm != _mapSubMesh.end(); ++i_sm )
232 // - groups on geometry
233 map <int, SMESH_Group *>::iterator i_gr = _mapGroup.begin();
234 while ( i_gr != _mapGroup.end() ) {
235 if ( dynamic_cast<SMESHDS_GroupOnGeom*>( i_gr->second->GetGroupDS() )) {
236 _myMeshDS->RemoveGroup( i_gr->second->GetGroupDS() );
238 _mapGroup.erase( i_gr++ );
243 _mapAncestors.Clear();
246 TopoDS_Shape aNullShape;
247 _myMeshDS->ShapeToMesh( aNullShape );
249 _shapeDiagonal = 0.0;
252 // set a new geometry
253 if ( !aShape.IsNull() )
255 _myMeshDS->ShapeToMesh(aShape);
256 _isShapeToMesh = true;
257 _nbSubShapes = _myMeshDS->MaxShapeIndex();
259 // fill map of ancestors
260 fillAncestorsMap(aShape);
264 _isShapeToMesh = false;
265 _shapeDiagonal = 0.0;
266 _myMeshDS->ShapeToMesh( PseudoShape() );
271 //=======================================================================
273 * \brief Return geometry to be meshed. (It may be a PseudoShape()!)
275 //=======================================================================
277 TopoDS_Shape SMESH_Mesh::GetShapeToMesh() const
279 return _myMeshDS->ShapeToMesh();
282 //=======================================================================
284 * \brief Return a solid which is returned by GetShapeToMesh() if
285 * a real geometry to be meshed was not set
287 //=======================================================================
289 const TopoDS_Solid& SMESH_Mesh::PseudoShape()
291 static TopoDS_Solid aSolid;
292 if ( aSolid.IsNull() )
294 aSolid = BRepPrimAPI_MakeBox(1,1,1);
299 //=======================================================================
301 * \brief Return diagonal size of bounding box of a shape
303 //=======================================================================
305 double SMESH_Mesh::GetShapeDiagonalSize(const TopoDS_Shape & aShape)
307 if ( !aShape.IsNull() ) {
309 BRepBndLib::Add(aShape, Box);
310 return sqrt( Box.SquareExtent() );
315 //=======================================================================
317 * \brief Return diagonal size of bounding box of shape to mesh
319 //=======================================================================
321 double SMESH_Mesh::GetShapeDiagonalSize() const
323 if ( _shapeDiagonal == 0. && _isShapeToMesh )
324 const_cast<SMESH_Mesh*>(this)->_shapeDiagonal = GetShapeDiagonalSize( GetShapeToMesh() );
326 return _shapeDiagonal;
329 //================================================================================
331 * \brief Load mesh from study file
333 //================================================================================
335 void SMESH_Mesh::Load()
341 //=======================================================================
343 * \brief Remove all nodes and elements
345 //=======================================================================
347 void SMESH_Mesh::Clear()
350 _myMeshDS->ClearMesh();
352 // update compute state of submeshes
353 if ( SMESH_subMesh *sm = GetSubMeshContaining( GetShapeToMesh() ) )
355 sm->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
356 sm->ComputeSubMeshStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
357 sm->ComputeStateEngine( SMESH_subMesh::CLEAN ); // for event listeners (issue 0020918)
358 sm->ComputeSubMeshStateEngine( SMESH_subMesh::CLEAN );
363 //=======================================================================
365 * \brief Remove all nodes and elements of indicated shape
367 //=======================================================================
369 void SMESH_Mesh::ClearSubMesh(const int theShapeId)
371 // clear sub-meshes; get ready to re-compute as a side-effect
372 if ( SMESH_subMesh *sm = GetSubMeshContaining( theShapeId ) )
374 SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(/*includeSelf=*/true,
375 /*complexShapeFirst=*/false);
376 while ( smIt->more() )
379 TopAbs_ShapeEnum shapeType = sm->GetSubShape().ShapeType();
380 if ( shapeType == TopAbs_VERTEX || shapeType < TopAbs_SOLID )
381 // all other shapes depends on vertices so they are already cleaned
382 sm->ComputeStateEngine( SMESH_subMesh::CLEAN );
383 // to recompute even if failed
384 sm->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
389 //=======================================================================
390 //function : UNVToMesh
392 //=======================================================================
394 int SMESH_Mesh::UNVToMesh(const char* theFileName)
396 if(MYDEBUG) MESSAGE("UNVToMesh - theFileName = "<<theFileName);
398 throw SALOME_Exception(LOCALIZED("a shape to mesh has already been defined"));
399 _isShapeToMesh = false;
400 DriverUNV_R_SMDS_Mesh myReader;
401 myReader.SetMesh(_myMeshDS);
402 myReader.SetFile(theFileName);
403 myReader.SetMeshId(-1);
406 MESSAGE("UNVToMesh - _myMeshDS->NbNodes() = "<<_myMeshDS->NbNodes());
407 MESSAGE("UNVToMesh - _myMeshDS->NbEdges() = "<<_myMeshDS->NbEdges());
408 MESSAGE("UNVToMesh - _myMeshDS->NbFaces() = "<<_myMeshDS->NbFaces());
409 MESSAGE("UNVToMesh - _myMeshDS->NbVolumes() = "<<_myMeshDS->NbVolumes());
411 SMDS_MeshGroup* aGroup = (SMDS_MeshGroup*) myReader.GetGroup();
413 TGroupNamesMap aGroupNames = myReader.GetGroupNamesMap();
414 //const TGroupIdMap& aGroupId = myReader.GetGroupIdMap();
415 aGroup->InitSubGroupsIterator();
416 while (aGroup->MoreSubGroups()) {
417 SMDS_MeshGroup* aSubGroup = (SMDS_MeshGroup*) aGroup->NextSubGroup();
418 string aName = aGroupNames[aSubGroup];
421 SMESH_Group* aSMESHGroup = AddGroup( aSubGroup->GetType(), aName.c_str(), aId );
423 if(MYDEBUG) MESSAGE("UNVToMesh - group added: "<<aName);
424 SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>( aSMESHGroup->GetGroupDS() );
426 aGroupDS->SetStoreName(aName.c_str());
427 aSubGroup->InitIterator();
428 const SMDS_MeshElement* aElement = 0;
429 while (aSubGroup->More()) {
430 aElement = aSubGroup->Next();
432 aGroupDS->SMDSGroup().Add(aElement);
436 aGroupDS->SetType(aElement->GetType());
444 //=======================================================================
445 //function : MEDToMesh
447 //=======================================================================
449 int SMESH_Mesh::MEDToMesh(const char* theFileName, const char* theMeshName)
451 if(MYDEBUG) MESSAGE("MEDToMesh - theFileName = "<<theFileName<<", mesh name = "<<theMeshName);
453 throw SALOME_Exception(LOCALIZED("a shape to mesh has already been defined"));
454 _isShapeToMesh = false;
455 DriverMED_R_SMESHDS_Mesh myReader;
456 myReader.SetMesh(_myMeshDS);
457 myReader.SetMeshId(-1);
458 myReader.SetFile(theFileName);
459 myReader.SetMeshName(theMeshName);
460 Driver_Mesh::Status status = myReader.Perform();
462 MESSAGE("MEDToMesh - _myMeshDS->NbNodes() = "<<_myMeshDS->NbNodes());
463 MESSAGE("MEDToMesh - _myMeshDS->NbEdges() = "<<_myMeshDS->NbEdges());
464 MESSAGE("MEDToMesh - _myMeshDS->NbFaces() = "<<_myMeshDS->NbFaces());
465 MESSAGE("MEDToMesh - _myMeshDS->NbVolumes() = "<<_myMeshDS->NbVolumes());
468 // Reading groups (sub-meshes are out of scope of MED import functionality)
469 list<TNameAndType> aGroupNames = myReader.GetGroupNamesAndTypes();
470 if(MYDEBUG) MESSAGE("MEDToMesh - Nb groups = "<<aGroupNames.size());
472 list<TNameAndType>::iterator name_type = aGroupNames.begin();
473 for ( ; name_type != aGroupNames.end(); name_type++ ) {
474 SMESH_Group* aGroup = AddGroup( name_type->second, name_type->first.c_str(), anId );
476 if(MYDEBUG) MESSAGE("MEDToMesh - group added: "<<name_type->first.c_str());
477 SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>( aGroup->GetGroupDS() );
479 aGroupDS->SetStoreName( name_type->first.c_str() );
480 myReader.GetGroup( aGroupDS );
487 //=======================================================================
488 //function : STLToMesh
490 //=======================================================================
492 int SMESH_Mesh::STLToMesh(const char* theFileName)
494 if(MYDEBUG) MESSAGE("STLToMesh - theFileName = "<<theFileName);
496 throw SALOME_Exception(LOCALIZED("a shape to mesh has already been defined"));
497 _isShapeToMesh = false;
498 DriverSTL_R_SMDS_Mesh myReader;
499 myReader.SetMesh(_myMeshDS);
500 myReader.SetFile(theFileName);
501 myReader.SetMeshId(-1);
504 MESSAGE("STLToMesh - _myMeshDS->NbNodes() = "<<_myMeshDS->NbNodes());
505 MESSAGE("STLToMesh - _myMeshDS->NbEdges() = "<<_myMeshDS->NbEdges());
506 MESSAGE("STLToMesh - _myMeshDS->NbFaces() = "<<_myMeshDS->NbFaces());
507 MESSAGE("STLToMesh - _myMeshDS->NbVolumes() = "<<_myMeshDS->NbVolumes());
512 //================================================================================
514 * \brief Reads the given mesh from the CGNS file
515 * \param theFileName - name of the file
516 * \retval int - Driver_Mesh::Status
518 //================================================================================
520 int SMESH_Mesh::CGNSToMesh(const char* theFileName,
521 const int theMeshIndex,
522 std::string& theMeshName)
524 int res = Driver_Mesh::DRS_FAIL;
527 DriverCGNS_Read myReader;
528 myReader.SetMesh(_myMeshDS);
529 myReader.SetFile(theFileName);
530 myReader.SetMeshId(theMeshIndex);
531 res = myReader.Perform();
532 theMeshName = myReader.GetMeshName();
541 //=============================================================================
545 //=============================================================================
547 SMESH_Hypothesis::Hypothesis_Status
548 SMESH_Mesh::AddHypothesis(const TopoDS_Shape & aSubShape,
549 int anHypId ) throw(SALOME_Exception)
551 Unexpect aCatch(SalomeException);
552 if(MYDEBUG) MESSAGE("SMESH_Mesh::AddHypothesis");
554 SMESH_subMesh *subMesh = GetSubMesh(aSubShape);
555 if ( !subMesh || !subMesh->GetId())
556 return SMESH_Hypothesis::HYP_BAD_SUBSHAPE;
558 StudyContextStruct *sc = _gen->GetStudyContext(_studyId);
559 if (sc->mapHypothesis.find(anHypId) == sc->mapHypothesis.end())
561 if(MYDEBUG) MESSAGE("Hypothesis ID does not give an hypothesis");
566 throw SALOME_Exception(LOCALIZED("hypothesis does not exist"));
569 SMESH_Hypothesis *anHyp = sc->mapHypothesis[anHypId];
570 MESSAGE( "SMESH_Mesh::AddHypothesis " << anHyp->GetName() );
572 bool isGlobalHyp = IsMainShape( aSubShape );
574 // NotConformAllowed can be only global
577 // NOTE: this is not a correct way to check a name of hypothesis,
578 // there should be an attribute of hypothesis saying that it can/can't
580 string hypName = anHyp->GetName();
581 if ( hypName == "NotConformAllowed" )
583 if(MYDEBUG) MESSAGE( "Hypotesis <NotConformAllowed> can be only global" );
584 return SMESH_Hypothesis::HYP_INCOMPATIBLE;
590 bool isAlgo = ( !anHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO );
591 int event = isAlgo ? SMESH_subMesh::ADD_ALGO : SMESH_subMesh::ADD_HYP;
593 SMESH_Hypothesis::Hypothesis_Status ret = subMesh->AlgoStateEngine(event, anHyp);
596 if (!SMESH_Hypothesis::IsStatusFatal(ret) &&
597 anHyp->GetDim() <= SMESH_Gen::GetShapeDim(aSubShape)) // is added on father
599 event = isAlgo ? SMESH_subMesh::ADD_FATHER_ALGO : SMESH_subMesh::ADD_FATHER_HYP;
601 SMESH_Hypothesis::Hypothesis_Status ret2 =
602 subMesh->SubMeshesAlgoStateEngine(event, anHyp);
606 // check concurent hypotheses on ancestors
607 if (ret < SMESH_Hypothesis::HYP_CONCURENT && !isGlobalHyp )
609 SMESH_subMeshIteratorPtr smIt = subMesh->getDependsOnIterator(false,false);
610 while ( smIt->more() ) {
611 SMESH_subMesh* sm = smIt->next();
612 if ( sm->IsApplicableHypotesis( anHyp )) {
613 ret2 = sm->CheckConcurentHypothesis( anHyp->GetType() );
622 HasModificationsToDiscard(); // to reset _isModified flag if a mesh becomes empty
624 GetMeshDS()->Modified();
626 if(MYDEBUG) subMesh->DumpAlgoState(true);
627 if(MYDEBUG) SCRUTE(ret);
631 //=============================================================================
635 //=============================================================================
637 SMESH_Hypothesis::Hypothesis_Status
638 SMESH_Mesh::RemoveHypothesis(const TopoDS_Shape & aSubShape,
639 int anHypId)throw(SALOME_Exception)
641 Unexpect aCatch(SalomeException);
642 if(MYDEBUG) MESSAGE("SMESH_Mesh::RemoveHypothesis");
644 StudyContextStruct *sc = _gen->GetStudyContext(_studyId);
645 if (sc->mapHypothesis.find(anHypId) == sc->mapHypothesis.end())
646 throw SALOME_Exception(LOCALIZED("hypothesis does not exist"));
648 SMESH_Hypothesis *anHyp = sc->mapHypothesis[anHypId];
650 int hypType = anHyp->GetType();
656 bool isAlgo = ( !anHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO );
657 int event = isAlgo ? SMESH_subMesh::REMOVE_ALGO : SMESH_subMesh::REMOVE_HYP;
659 SMESH_subMesh *subMesh = GetSubMesh(aSubShape);
661 SMESH_Hypothesis::Hypothesis_Status ret = subMesh->AlgoStateEngine(event, anHyp);
663 // there may appear concurrent hyps that were covered by the removed hyp
664 if (ret < SMESH_Hypothesis::HYP_CONCURENT &&
665 subMesh->IsApplicableHypotesis( anHyp ) &&
666 subMesh->CheckConcurentHypothesis( anHyp->GetType() ) != SMESH_Hypothesis::HYP_OK)
667 ret = SMESH_Hypothesis::HYP_CONCURENT;
670 if (!SMESH_Hypothesis::IsStatusFatal(ret) &&
671 anHyp->GetDim() <= SMESH_Gen::GetShapeDim(aSubShape)) // is removed from father
673 event = isAlgo ? SMESH_subMesh::REMOVE_FATHER_ALGO : SMESH_subMesh::REMOVE_FATHER_HYP;
675 SMESH_Hypothesis::Hypothesis_Status ret2 =
676 subMesh->SubMeshesAlgoStateEngine(event, anHyp);
677 if (ret2 > ret) // more severe
680 // check concurent hypotheses on ancestors
681 if (ret < SMESH_Hypothesis::HYP_CONCURENT && !IsMainShape( aSubShape ) )
683 SMESH_subMeshIteratorPtr smIt = subMesh->getDependsOnIterator(false,false);
684 while ( smIt->more() ) {
685 SMESH_subMesh* sm = smIt->next();
686 if ( sm->IsApplicableHypotesis( anHyp )) {
687 ret2 = sm->CheckConcurentHypothesis( anHyp->GetType() );
697 HasModificationsToDiscard(); // to reset _isModified flag if mesh become empty
699 GetMeshDS()->Modified();
701 if(MYDEBUG) subMesh->DumpAlgoState(true);
702 if(MYDEBUG) SCRUTE(ret);
706 //=============================================================================
710 //=============================================================================
712 const list<const SMESHDS_Hypothesis*>&
713 SMESH_Mesh::GetHypothesisList(const TopoDS_Shape & aSubShape) const
714 throw(SALOME_Exception)
716 Unexpect aCatch(SalomeException);
717 return _myMeshDS->GetHypothesis(aSubShape);
720 //=======================================================================
722 * \brief Return the hypothesis assigned to the shape
723 * \param aSubShape - the shape to check
724 * \param aFilter - the hypothesis filter
725 * \param andAncestors - flag to check hypos assigned to ancestors of the shape
726 * \param assignedTo - to return the shape the found hypo is assigned to
727 * \retval SMESH_Hypothesis* - the first hypo passed through aFilter
729 //=======================================================================
731 const SMESH_Hypothesis * SMESH_Mesh::GetHypothesis(const TopoDS_Shape & aSubShape,
732 const SMESH_HypoFilter& aFilter,
733 const bool andAncestors,
734 TopoDS_Shape* assignedTo) const
737 const list<const SMESHDS_Hypothesis*>& hypList = _myMeshDS->GetHypothesis(aSubShape);
738 list<const SMESHDS_Hypothesis*>::const_iterator hyp = hypList.begin();
739 for ( ; hyp != hypList.end(); hyp++ ) {
740 const SMESH_Hypothesis * h = cSMESH_Hyp( *hyp );
741 if ( aFilter.IsOk( h, aSubShape)) {
742 if ( assignedTo ) *assignedTo = aSubShape;
749 // user sorted submeshes of ancestors, according to stored submesh priority
750 const list<SMESH_subMesh*> smList = getAncestorsSubMeshes( aSubShape );
751 list<SMESH_subMesh*>::const_iterator smIt = smList.begin();
752 for ( ; smIt != smList.end(); smIt++ )
754 const TopoDS_Shape& curSh = (*smIt)->GetSubShape();
755 const list<const SMESHDS_Hypothesis*>& hypList = _myMeshDS->GetHypothesis(curSh);
756 list<const SMESHDS_Hypothesis*>::const_iterator hyp = hypList.begin();
757 for ( ; hyp != hypList.end(); hyp++ ) {
758 const SMESH_Hypothesis * h = cSMESH_Hyp( *hyp );
759 if (aFilter.IsOk( h, curSh )) {
760 if ( assignedTo ) *assignedTo = curSh;
769 //================================================================================
771 * \brief Return hypothesis assigned to the shape
772 * \param aSubShape - the shape to check
773 * \param aFilter - the hypothesis filter
774 * \param aHypList - the list of the found hypotheses
775 * \param andAncestors - flag to check hypos assigned to ancestors of the shape
776 * \retval int - number of unique hypos in aHypList
778 //================================================================================
780 int SMESH_Mesh::GetHypotheses(const TopoDS_Shape & aSubShape,
781 const SMESH_HypoFilter& aFilter,
782 list <const SMESHDS_Hypothesis * >& aHypList,
783 const bool andAncestors) const
785 set<string> hypTypes; // to exclude same type hypos from the result list
788 // only one main hypothesis is allowed
789 bool mainHypFound = false;
792 list<const SMESHDS_Hypothesis*>::const_iterator hyp;
793 for ( hyp = aHypList.begin(); hyp != aHypList.end(); hyp++ ) {
794 if ( hypTypes.insert( (*hyp)->GetName() ).second )
796 if ( !cSMESH_Hyp(*hyp)->IsAuxiliary() )
800 // get hypos from aSubShape
802 const list<const SMESHDS_Hypothesis*>& hypList = _myMeshDS->GetHypothesis(aSubShape);
803 for ( hyp = hypList.begin(); hyp != hypList.end(); hyp++ )
804 if ( aFilter.IsOk (cSMESH_Hyp( *hyp ), aSubShape) &&
805 ( cSMESH_Hyp(*hyp)->IsAuxiliary() || !mainHypFound ) &&
806 hypTypes.insert( (*hyp)->GetName() ).second )
808 aHypList.push_back( *hyp );
810 if ( !cSMESH_Hyp(*hyp)->IsAuxiliary() )
815 // get hypos from ancestors of aSubShape
818 TopTools_MapOfShape map;
820 // user sorted submeshes of ancestors, according to stored submesh priority
821 const list<SMESH_subMesh*> smList = getAncestorsSubMeshes( aSubShape );
822 list<SMESH_subMesh*>::const_iterator smIt = smList.begin();
823 for ( ; smIt != smList.end(); smIt++ )
825 const TopoDS_Shape& curSh = (*smIt)->GetSubShape();
826 if ( !map.Add( curSh ))
828 const list<const SMESHDS_Hypothesis*>& hypList = _myMeshDS->GetHypothesis(curSh);
829 for ( hyp = hypList.begin(); hyp != hypList.end(); hyp++ )
830 if (aFilter.IsOk( cSMESH_Hyp( *hyp ), curSh ) &&
831 ( cSMESH_Hyp(*hyp)->IsAuxiliary() || !mainHypFound ) &&
832 hypTypes.insert( (*hyp)->GetName() ).second )
834 aHypList.push_back( *hyp );
836 if ( !cSMESH_Hyp(*hyp)->IsAuxiliary() )
844 //=============================================================================
848 //=============================================================================
850 const list<SMESHDS_Command*> & SMESH_Mesh::GetLog() throw(SALOME_Exception)
852 Unexpect aCatch(SalomeException);
853 if(MYDEBUG) MESSAGE("SMESH_Mesh::GetLog");
854 return _myMeshDS->GetScript()->GetCommands();
857 //=============================================================================
861 //=============================================================================
862 void SMESH_Mesh::ClearLog() throw(SALOME_Exception)
864 Unexpect aCatch(SalomeException);
865 if(MYDEBUG) MESSAGE("SMESH_Mesh::ClearLog");
866 _myMeshDS->GetScript()->Clear();
869 //=============================================================================
871 * Get or Create the SMESH_subMesh object implementation
873 //=============================================================================
875 SMESH_subMesh *SMESH_Mesh::GetSubMesh(const TopoDS_Shape & aSubShape)
876 throw(SALOME_Exception)
878 Unexpect aCatch(SalomeException);
879 SMESH_subMesh *aSubMesh;
880 int index = _myMeshDS->ShapeToIndex(aSubShape);
882 // for submeshes on GEOM Group
883 if (( !index || index > _nbSubShapes ) && aSubShape.ShapeType() == TopAbs_COMPOUND ) {
884 TopoDS_Iterator it( aSubShape );
887 index = _myMeshDS->AddCompoundSubmesh( aSubShape, it.Value().ShapeType() );
888 if ( index > _nbSubShapes ) _nbSubShapes = index; // not to create sm for this group again
890 // fill map of Ancestors
891 fillAncestorsMap(aSubShape);
895 // return NULL; // neither sub-shape nor a group
897 map <int, SMESH_subMesh *>::iterator i_sm = _mapSubMesh.find(index);
898 if ( i_sm != _mapSubMesh.end())
900 aSubMesh = i_sm->second;
904 aSubMesh = new SMESH_subMesh(index, this, _myMeshDS, aSubShape);
905 _mapSubMesh[index] = aSubMesh;
911 //=============================================================================
913 * Get the SMESH_subMesh object implementation. Dont create it, return null
914 * if it does not exist.
916 //=============================================================================
918 SMESH_subMesh *SMESH_Mesh::GetSubMeshContaining(const TopoDS_Shape & aSubShape) const
919 throw(SALOME_Exception)
921 Unexpect aCatch(SalomeException);
922 SMESH_subMesh *aSubMesh = NULL;
924 int index = _myMeshDS->ShapeToIndex(aSubShape);
926 map <int, SMESH_subMesh *>::const_iterator i_sm = _mapSubMesh.find(index);
927 if ( i_sm != _mapSubMesh.end())
928 aSubMesh = i_sm->second;
932 //=============================================================================
934 * Get the SMESH_subMesh object implementation. Dont create it, return null
935 * if it does not exist.
937 //=============================================================================
939 SMESH_subMesh *SMESH_Mesh::GetSubMeshContaining(const int aShapeID) const
940 throw(SALOME_Exception)
942 Unexpect aCatch(SalomeException);
944 map <int, SMESH_subMesh *>::const_iterator i_sm = _mapSubMesh.find(aShapeID);
945 if (i_sm == _mapSubMesh.end())
949 //================================================================================
951 * \brief Return submeshes of groups containing the given sub-shape
953 //================================================================================
956 SMESH_Mesh::GetGroupSubMeshesContaining(const TopoDS_Shape & aSubShape) const
957 throw(SALOME_Exception)
959 Unexpect aCatch(SalomeException);
960 list<SMESH_subMesh*> found;
962 SMESH_subMesh * subMesh = GetSubMeshContaining(aSubShape);
966 // submeshes of groups have max IDs, so search from the map end
967 map<int, SMESH_subMesh *>::const_reverse_iterator i_sm;
968 for ( i_sm = _mapSubMesh.rbegin(); i_sm != _mapSubMesh.rend(); ++i_sm) {
969 SMESHDS_SubMesh * ds = i_sm->second->GetSubMeshDS();
970 if ( ds && ds->IsComplexSubmesh() ) {
971 TopExp_Explorer exp( i_sm->second->GetSubShape(), aSubShape.ShapeType() );
972 for ( ; exp.More(); exp.Next() ) {
973 if ( aSubShape.IsSame( exp.Current() )) {
974 found.push_back( i_sm->second );
984 //=======================================================================
985 //function : IsUsedHypothesis
986 //purpose : Return True if anHyp is used to mesh aSubShape
987 //=======================================================================
989 bool SMESH_Mesh::IsUsedHypothesis(SMESHDS_Hypothesis * anHyp,
990 const SMESH_subMesh* aSubMesh)
992 SMESH_Hypothesis* hyp = static_cast<SMESH_Hypothesis*>(anHyp);
994 // check if anHyp can be used to mesh aSubMesh
995 if ( !aSubMesh || !aSubMesh->IsApplicableHypotesis( hyp ))
998 const TopoDS_Shape & aSubShape = const_cast<SMESH_subMesh*>( aSubMesh )->GetSubShape();
1000 SMESH_Algo *algo = _gen->GetAlgo(*this, aSubShape );
1003 if (anHyp->GetType() > SMESHDS_Hypothesis::PARAM_ALGO)
1004 return ( anHyp == algo );
1006 // algorithm parameter
1009 // look trough hypotheses used by algo
1010 SMESH_HypoFilter hypoKind;
1011 if ( algo->InitCompatibleHypoFilter( hypoKind, !hyp->IsAuxiliary() )) {
1012 list <const SMESHDS_Hypothesis * > usedHyps;
1013 if ( GetHypotheses( aSubShape, hypoKind, usedHyps, true ))
1014 return ( find( usedHyps.begin(), usedHyps.end(), anHyp ) != usedHyps.end() );
1018 // look through all assigned hypotheses
1019 //SMESH_HypoFilter filter( SMESH_HypoFilter::Is( hyp ));
1020 return false; //GetHypothesis( aSubShape, filter, true );
1023 //=============================================================================
1027 //=============================================================================
1029 const list < SMESH_subMesh * >&
1030 SMESH_Mesh::GetSubMeshUsingHypothesis(SMESHDS_Hypothesis * anHyp)
1031 throw(SALOME_Exception)
1033 Unexpect aCatch(SalomeException);
1034 if(MYDEBUG) MESSAGE("SMESH_Mesh::GetSubMeshUsingHypothesis");
1035 map < int, SMESH_subMesh * >::iterator itsm;
1036 _subMeshesUsingHypothesisList.clear();
1037 for (itsm = _mapSubMesh.begin(); itsm != _mapSubMesh.end(); itsm++)
1039 SMESH_subMesh *aSubMesh = (*itsm).second;
1040 if ( IsUsedHypothesis ( anHyp, aSubMesh ))
1041 _subMeshesUsingHypothesisList.push_back(aSubMesh);
1043 return _subMeshesUsingHypothesisList;
1046 //=======================================================================
1047 //function : NotifySubMeshesHypothesisModification
1048 //purpose : Say all submeshes using theChangedHyp that it has been modified
1049 //=======================================================================
1051 void SMESH_Mesh::NotifySubMeshesHypothesisModification(const SMESH_Hypothesis* hyp)
1053 Unexpect aCatch(SalomeException);
1055 if ( !GetMeshDS()->IsUsedHypothesis( hyp ))
1059 _callUp->HypothesisModified();
1061 const SMESH_Algo *foundAlgo = 0;
1062 SMESH_HypoFilter algoKind, compatibleHypoKind;
1063 list <const SMESHDS_Hypothesis * > usedHyps;
1066 map < int, SMESH_subMesh * >::iterator itsm;
1067 for (itsm = _mapSubMesh.begin(); itsm != _mapSubMesh.end(); itsm++)
1069 SMESH_subMesh *aSubMesh = (*itsm).second;
1070 if ( aSubMesh->IsApplicableHypotesis( hyp ))
1072 const TopoDS_Shape & aSubShape = aSubMesh->GetSubShape();
1074 if ( !foundAlgo ) // init filter for algo search
1075 algoKind.Init( THypType::IsAlgo() ).And( THypType::IsApplicableTo( aSubShape ));
1077 const SMESH_Algo *algo = static_cast<const SMESH_Algo*>
1078 ( GetHypothesis( aSubShape, algoKind, true ));
1082 bool sameAlgo = ( algo == foundAlgo );
1083 if ( !sameAlgo && foundAlgo )
1084 sameAlgo = ( strcmp( algo->GetName(), foundAlgo->GetName() ) == 0);
1086 if ( !sameAlgo ) { // init filter for used hypos search
1087 if ( !algo->InitCompatibleHypoFilter( compatibleHypoKind, !hyp->IsAuxiliary() ))
1088 continue; // algo does not use any hypothesis
1092 // check if hyp is used by algo
1094 if ( GetHypotheses( aSubShape, compatibleHypoKind, usedHyps, true ) &&
1095 find( usedHyps.begin(), usedHyps.end(), hyp ) != usedHyps.end() )
1097 aSubMesh->AlgoStateEngine(SMESH_subMesh::MODIF_HYP,
1098 const_cast< SMESH_Hypothesis*>( hyp ));
1103 HasModificationsToDiscard(); // to reset _isModified flag if mesh becomes empty
1104 GetMeshDS()->Modified();
1107 //=============================================================================
1109 * Auto color functionality
1111 //=============================================================================
1112 void SMESH_Mesh::SetAutoColor(bool theAutoColor) throw(SALOME_Exception)
1114 Unexpect aCatch(SalomeException);
1115 _isAutoColor = theAutoColor;
1118 bool SMESH_Mesh::GetAutoColor() throw(SALOME_Exception)
1120 Unexpect aCatch(SalomeException);
1121 return _isAutoColor;
1124 //=======================================================================
1125 //function : SetIsModified
1126 //purpose : Set the flag meaning that the mesh has been edited "manually"
1127 //=======================================================================
1129 void SMESH_Mesh::SetIsModified(bool isModified)
1131 _isModified = isModified;
1134 // check if mesh becomes empty as result of modification
1135 HasModificationsToDiscard();
1138 //=======================================================================
1139 //function : HasModificationsToDiscard
1140 //purpose : Return true if the mesh has been edited since a total re-compute
1141 // and those modifications may prevent successful partial re-compute.
1142 // As a side effect reset _isModified flag if mesh is empty
1144 //=======================================================================
1146 bool SMESH_Mesh::HasModificationsToDiscard() const
1148 if ( ! _isModified )
1151 // return true if the next Compute() will be partial and
1152 // existing but changed elements may prevent successful re-compute
1153 bool hasComputed = false, hasNotComputed = false;
1154 map <int, SMESH_subMesh*>::const_iterator i_sm = _mapSubMesh.begin();
1155 for ( ; i_sm != _mapSubMesh.end() ; ++i_sm )
1156 switch ( i_sm->second->GetSubShape().ShapeType() )
1161 if ( i_sm->second->IsMeshComputed() )
1164 hasNotComputed = true;
1165 if ( hasComputed && hasNotComputed)
1169 if ( NbNodes() < 1 )
1170 const_cast<SMESH_Mesh*>(this)->_isModified = false;
1175 //================================================================================
1177 * \brief Check if any groups of the same type have equal names
1179 //================================================================================
1181 bool SMESH_Mesh::HasDuplicatedGroupNamesMED()
1183 //set<string> aGroupNames; // Corrected for Mantis issue 0020028
1184 map< SMDSAbs_ElementType, set<string> > aGroupNames;
1185 for ( map<int, SMESH_Group*>::iterator it = _mapGroup.begin(); it != _mapGroup.end(); it++ )
1187 SMESH_Group* aGroup = it->second;
1188 SMDSAbs_ElementType aType = aGroup->GetGroupDS()->GetType();
1189 string aGroupName = aGroup->GetName();
1190 aGroupName.resize(MAX_MED_GROUP_NAME_LENGTH);
1191 if (!aGroupNames[aType].insert(aGroupName).second)
1198 //================================================================================
1200 * \brief Export the mesh to a med file
1202 //================================================================================
1204 void SMESH_Mesh::ExportMED(const char * file,
1205 const char* theMeshName,
1208 const SMESHDS_Mesh* meshPart)
1209 throw(SALOME_Exception)
1211 Unexpect aCatch(SalomeException);
1213 DriverMED_W_SMESHDS_Mesh myWriter;
1214 myWriter.SetFile ( file, MED::EVersion(theVersion) );
1215 myWriter.SetMesh ( meshPart ? (SMESHDS_Mesh*) meshPart : _myMeshDS );
1217 myWriter.SetMeshId ( _idDoc );
1219 myWriter.SetMeshId ( -1 );
1220 myWriter.SetMeshName( theMeshName );
1223 if ( theAutoGroups ) {
1224 myWriter.AddGroupOfNodes();
1225 myWriter.AddGroupOfEdges();
1226 myWriter.AddGroupOfFaces();
1227 myWriter.AddGroupOfVolumes();
1230 // Pass groups to writer. Provide unique group names.
1231 //set<string> aGroupNames; // Corrected for Mantis issue 0020028
1234 map< SMDSAbs_ElementType, set<string> > aGroupNames;
1236 int maxNbIter = 10000; // to guarantee cycle finish
1237 for ( map<int, SMESH_Group*>::iterator it = _mapGroup.begin(); it != _mapGroup.end(); it++ ) {
1238 SMESH_Group* aGroup = it->second;
1239 SMESHDS_GroupBase* aGroupDS = aGroup->GetGroupDS();
1241 SMDSAbs_ElementType aType = aGroupDS->GetType();
1242 string aGroupName0 = aGroup->GetName();
1243 aGroupName0.resize(MAX_MED_GROUP_NAME_LENGTH);
1244 string aGroupName = aGroupName0;
1245 for (int i = 1; !aGroupNames[aType].insert(aGroupName).second && i < maxNbIter; i++) {
1246 sprintf(&aString[0], "GR_%d_%s", i, aGroupName0.c_str());
1247 aGroupName = aString;
1248 aGroupName.resize(MAX_MED_GROUP_NAME_LENGTH);
1250 aGroupDS->SetStoreName( aGroupName.c_str() );
1251 myWriter.AddGroup( aGroupDS );
1259 void SMESH_Mesh::ExportSAUV(const char *file,
1260 const char* theMeshName,
1262 throw(SALOME_Exception)
1264 std::string medfilename(file);
1265 medfilename += ".med";
1268 cmd = "%PYTHONBIN% ";
1273 cmd += "from medutilities import my_remove ; my_remove(r'" + medfilename + "')";
1275 system(cmd.c_str());
1276 ExportMED(medfilename.c_str(), theMeshName, theAutoGroups, 1);
1278 cmd = "%PYTHONBIN% ";
1283 cmd += "from medutilities import convert ; convert(r'" + medfilename + "', 'MED', 'GIBI', 1, r'" + file + "')";
1285 system(cmd.c_str());
1287 cmd = "%PYTHONBIN% ";
1292 cmd += "from medutilities import my_remove ; my_remove(r'" + medfilename + "')";
1294 system(cmd.c_str());
1297 //================================================================================
1299 * \brief Export the mesh to a DAT file
1301 //================================================================================
1303 void SMESH_Mesh::ExportDAT(const char * file,
1304 const SMESHDS_Mesh* meshPart) throw(SALOME_Exception)
1306 Unexpect aCatch(SalomeException);
1307 DriverDAT_W_SMDS_Mesh myWriter;
1308 myWriter.SetFile( file );
1309 myWriter.SetMesh( meshPart ? (SMESHDS_Mesh*) meshPart : _myMeshDS );
1310 myWriter.SetMeshId(_idDoc);
1314 //================================================================================
1316 * \brief Export the mesh to an UNV file
1318 //================================================================================
1320 void SMESH_Mesh::ExportUNV(const char * file,
1321 const SMESHDS_Mesh* meshPart) throw(SALOME_Exception)
1323 Unexpect aCatch(SalomeException);
1324 DriverUNV_W_SMDS_Mesh myWriter;
1325 myWriter.SetFile( file );
1326 myWriter.SetMesh( meshPart ? (SMESHDS_Mesh*) meshPart : _myMeshDS );
1327 myWriter.SetMeshId(_idDoc);
1328 // myWriter.SetGroups(_mapGroup);
1332 for ( map<int, SMESH_Group*>::iterator it = _mapGroup.begin(); it != _mapGroup.end(); it++ ) {
1333 SMESH_Group* aGroup = it->second;
1334 SMESHDS_GroupBase* aGroupDS = aGroup->GetGroupDS();
1336 string aGroupName = aGroup->GetName();
1337 aGroupDS->SetStoreName( aGroupName.c_str() );
1338 myWriter.AddGroup( aGroupDS );
1345 //================================================================================
1347 * \brief Export the mesh to an STL file
1349 //================================================================================
1351 void SMESH_Mesh::ExportSTL(const char * file,
1353 const SMESHDS_Mesh* meshPart) throw(SALOME_Exception)
1355 Unexpect aCatch(SalomeException);
1356 DriverSTL_W_SMDS_Mesh myWriter;
1357 myWriter.SetFile( file );
1358 myWriter.SetIsAscii( isascii );
1359 myWriter.SetMesh( meshPart ? (SMESHDS_Mesh*) meshPart : _myMeshDS);
1360 myWriter.SetMeshId(_idDoc);
1364 //================================================================================
1366 * \brief Export the mesh to the CGNS file
1368 //================================================================================
1370 void SMESH_Mesh::ExportCGNS(const char * file,
1371 const SMESHDS_Mesh* meshDS)
1373 int res = Driver_Mesh::DRS_FAIL;
1375 DriverCGNS_Write myWriter;
1376 myWriter.SetFile( file );
1377 myWriter.SetMesh( const_cast<SMESHDS_Mesh*>( meshDS ));
1378 myWriter.SetMeshName( SMESH_Comment("Mesh_") << meshDS->GetPersistentId());
1379 res = myWriter.Perform();
1381 if ( res != Driver_Mesh::DRS_OK )
1382 throw SALOME_Exception("Export failed");
1385 //================================================================================
1387 * \brief Return number of nodes in the mesh
1389 //================================================================================
1391 int SMESH_Mesh::NbNodes() const throw(SALOME_Exception)
1393 Unexpect aCatch(SalomeException);
1394 return _myMeshDS->NbNodes();
1397 //================================================================================
1399 * \brief Return number of edges of given order in the mesh
1401 //================================================================================
1403 int SMESH_Mesh::Nb0DElements() const throw(SALOME_Exception)
1405 Unexpect aCatch(SalomeException);
1406 return _myMeshDS->GetMeshInfo().Nb0DElements();
1409 //================================================================================
1411 * \brief Return number of edges of given order in the mesh
1413 //================================================================================
1415 int SMESH_Mesh::NbEdges(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1417 Unexpect aCatch(SalomeException);
1418 return _myMeshDS->GetMeshInfo().NbEdges(order);
1421 //================================================================================
1423 * \brief Return number of faces of given order in the mesh
1425 //================================================================================
1427 int SMESH_Mesh::NbFaces(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1429 Unexpect aCatch(SalomeException);
1430 return _myMeshDS->GetMeshInfo().NbFaces(order);
1433 //================================================================================
1435 * \brief Return the number of faces in the mesh
1437 //================================================================================
1439 int SMESH_Mesh::NbTriangles(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1441 Unexpect aCatch(SalomeException);
1442 return _myMeshDS->GetMeshInfo().NbTriangles(order);
1445 //================================================================================
1447 * \brief Return the number nodes faces in the mesh
1449 //================================================================================
1451 int SMESH_Mesh::NbQuadrangles(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1453 Unexpect aCatch(SalomeException);
1454 return _myMeshDS->GetMeshInfo().NbQuadrangles(order);
1457 //================================================================================
1459 * \brief Return number of biquadratic quadrangles in the mesh
1461 //================================================================================
1463 int SMESH_Mesh::NbBiQuadQuadrangles() const throw(SALOME_Exception)
1465 Unexpect aCatch(SalomeException);
1466 return _myMeshDS->GetMeshInfo().NbBiQuadQuadrangles();
1469 //================================================================================
1471 * \brief Return the number of polygonal faces in the mesh
1473 //================================================================================
1475 int SMESH_Mesh::NbPolygons() const throw(SALOME_Exception)
1477 Unexpect aCatch(SalomeException);
1478 return _myMeshDS->GetMeshInfo().NbPolygons();
1481 //================================================================================
1483 * \brief Return number of volumes of given order in the mesh
1485 //================================================================================
1487 int SMESH_Mesh::NbVolumes(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1489 Unexpect aCatch(SalomeException);
1490 return _myMeshDS->GetMeshInfo().NbVolumes(order);
1493 //================================================================================
1495 * \brief Return number of tetrahedrons of given order in the mesh
1497 //================================================================================
1499 int SMESH_Mesh::NbTetras(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1501 Unexpect aCatch(SalomeException);
1502 return _myMeshDS->GetMeshInfo().NbTetras(order);
1505 //================================================================================
1507 * \brief Return number of hexahedrons of given order in the mesh
1509 //================================================================================
1511 int SMESH_Mesh::NbHexas(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1513 Unexpect aCatch(SalomeException);
1514 return _myMeshDS->GetMeshInfo().NbHexas(order);
1517 //================================================================================
1519 * \brief Return number of triquadratic hexahedrons in the mesh
1521 //================================================================================
1523 int SMESH_Mesh::NbTriQuadraticHexas() const throw(SALOME_Exception)
1525 Unexpect aCatch(SalomeException);
1526 return _myMeshDS->GetMeshInfo().NbTriQuadHexas();
1529 //================================================================================
1531 * \brief Return number of pyramids of given order in the mesh
1533 //================================================================================
1535 int SMESH_Mesh::NbPyramids(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1537 Unexpect aCatch(SalomeException);
1538 return _myMeshDS->GetMeshInfo().NbPyramids(order);
1541 //================================================================================
1543 * \brief Return number of prisms (penthahedrons) of given order in the mesh
1545 //================================================================================
1547 int SMESH_Mesh::NbPrisms(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1549 Unexpect aCatch(SalomeException);
1550 return _myMeshDS->GetMeshInfo().NbPrisms(order);
1553 //================================================================================
1555 * \brief Return number of hexagonal prisms in the mesh
1557 //================================================================================
1559 int SMESH_Mesh::NbHexagonalPrisms() const throw(SALOME_Exception)
1561 Unexpect aCatch(SalomeException);
1562 return _myMeshDS->GetMeshInfo().NbHexPrisms();
1565 //================================================================================
1567 * \brief Return number of polyhedrons in the mesh
1569 //================================================================================
1571 int SMESH_Mesh::NbPolyhedrons() const throw(SALOME_Exception)
1573 Unexpect aCatch(SalomeException);
1574 return _myMeshDS->GetMeshInfo().NbPolyhedrons();
1577 //================================================================================
1579 * \brief Return number of submeshes in the mesh
1581 //================================================================================
1583 int SMESH_Mesh::NbSubMesh() const throw(SALOME_Exception)
1585 Unexpect aCatch(SalomeException);
1586 return _myMeshDS->NbSubMesh();
1589 //=======================================================================
1590 //function : IsNotConformAllowed
1591 //purpose : check if a hypothesis alowing notconform mesh is present
1592 //=======================================================================
1594 bool SMESH_Mesh::IsNotConformAllowed() const
1596 if(MYDEBUG) MESSAGE("SMESH_Mesh::IsNotConformAllowed");
1598 static SMESH_HypoFilter filter( SMESH_HypoFilter::HasName( "NotConformAllowed" ));
1599 return GetHypothesis( _myMeshDS->ShapeToMesh(), filter, false );
1602 //=======================================================================
1603 //function : IsMainShape
1605 //=======================================================================
1607 bool SMESH_Mesh::IsMainShape(const TopoDS_Shape& theShape) const
1609 return theShape.IsSame(_myMeshDS->ShapeToMesh() );
1612 //=============================================================================
1616 //=============================================================================
1618 SMESH_Group* SMESH_Mesh::AddGroup (const SMDSAbs_ElementType theType,
1619 const char* theName,
1621 const TopoDS_Shape& theShape,
1622 const SMESH_PredicatePtr& thePredicate)
1624 if (_mapGroup.count(_groupId))
1627 SMESH_Group* aGroup = new SMESH_Group (theId, this, theType, theName, theShape, thePredicate);
1628 GetMeshDS()->AddGroup( aGroup->GetGroupDS() );
1629 _mapGroup[_groupId++] = aGroup;
1633 //================================================================================
1635 * \brief Creates SMESH_Groups for not wrapped SMESHDS_Groups
1636 * \retval bool - true if new SMESH_Groups have been created
1639 //================================================================================
1641 bool SMESH_Mesh::SynchronizeGroups()
1643 int nbGroups = _mapGroup.size();
1644 const set<SMESHDS_GroupBase*>& groups = _myMeshDS->GetGroups();
1645 set<SMESHDS_GroupBase*>::const_iterator gIt = groups.begin();
1646 for ( ; gIt != groups.end(); ++gIt )
1648 SMESHDS_GroupBase* groupDS = (SMESHDS_GroupBase*) *gIt;
1649 _groupId = groupDS->GetID();
1650 if ( !_mapGroup.count( _groupId ))
1651 _mapGroup[_groupId] = new SMESH_Group( groupDS );
1653 if ( !_mapGroup.empty() )
1654 _groupId = _mapGroup.rbegin()->first + 1;
1656 return nbGroups < _mapGroup.size();
1659 //================================================================================
1661 * \brief Return iterator on all existing groups
1663 //================================================================================
1665 SMESH_Mesh::GroupIteratorPtr SMESH_Mesh::GetGroups() const
1667 typedef map <int, SMESH_Group *> TMap;
1668 return GroupIteratorPtr( new SMDS_mapIterator<TMap>( _mapGroup ));
1671 //=============================================================================
1673 * \brief Return a group by ID
1675 //=============================================================================
1677 SMESH_Group* SMESH_Mesh::GetGroup (const int theGroupID)
1679 if (_mapGroup.find(theGroupID) == _mapGroup.end())
1681 return _mapGroup[theGroupID];
1685 //=============================================================================
1687 * \brief Return IDs of all groups
1689 //=============================================================================
1691 list<int> SMESH_Mesh::GetGroupIds() const
1694 for ( map<int, SMESH_Group*>::const_iterator it = _mapGroup.begin(); it != _mapGroup.end(); it++ )
1695 anIds.push_back( it->first );
1700 //================================================================================
1702 * \brief Set a caller of methods at level of CORBA API implementation.
1703 * The set upCaller will be deleted by SMESH_Mesh
1705 //================================================================================
1707 void SMESH_Mesh::SetCallUp( TCallUp* upCaller )
1709 if ( _callUp ) delete _callUp;
1713 //=============================================================================
1717 //=============================================================================
1719 bool SMESH_Mesh::RemoveGroup (const int theGroupID)
1721 if (_mapGroup.find(theGroupID) == _mapGroup.end())
1723 GetMeshDS()->RemoveGroup( _mapGroup[theGroupID]->GetGroupDS() );
1724 delete _mapGroup[theGroupID];
1725 _mapGroup.erase (theGroupID);
1727 _callUp->RemoveGroup( theGroupID );
1731 //=======================================================================
1732 //function : GetAncestors
1733 //purpose : return list of ancestors of theSubShape in the order
1734 // that lower dimention shapes come first.
1735 //=======================================================================
1737 const TopTools_ListOfShape& SMESH_Mesh::GetAncestors(const TopoDS_Shape& theS) const
1739 if ( _mapAncestors.Contains( theS ) )
1740 return _mapAncestors.FindFromKey( theS );
1742 static TopTools_ListOfShape emptyList;
1746 //=======================================================================
1748 //purpose : dumps contents of mesh to stream [ debug purposes ]
1749 //=======================================================================
1751 ostream& SMESH_Mesh::Dump(ostream& save)
1754 save << "========================== Dump contents of mesh ==========================" << endl << endl;
1755 save << ++clause << ") Total number of nodes: \t" << NbNodes() << endl;
1756 save << ++clause << ") Total number of edges: \t" << NbEdges() << endl;
1757 save << ++clause << ") Total number of faces: \t" << NbFaces() << endl;
1758 save << ++clause << ") Total number of polygons:\t" << NbPolygons() << endl;
1759 save << ++clause << ") Total number of volumes:\t" << NbVolumes() << endl;
1760 save << ++clause << ") Total number of polyhedrons:\t" << NbPolyhedrons() << endl << endl;
1761 for ( int isQuadratic = 0; isQuadratic < 2; ++isQuadratic )
1763 string orderStr = isQuadratic ? "quadratic" : "linear";
1764 SMDSAbs_ElementOrder order = isQuadratic ? ORDER_QUADRATIC : ORDER_LINEAR;
1766 save << ++clause << ") Total number of " << orderStr << " edges:\t" << NbEdges(order) << endl;
1767 save << ++clause << ") Total number of " << orderStr << " faces:\t" << NbFaces(order) << endl;
1768 if ( NbFaces(order) > 0 ) {
1769 int nb3 = NbTriangles(order);
1770 int nb4 = NbQuadrangles(order);
1771 save << clause << ".1) Number of " << orderStr << " triangles: \t" << nb3 << endl;
1772 save << clause << ".2) Number of " << orderStr << " quadrangles:\t" << nb4 << endl;
1773 if ( nb3 + nb4 != NbFaces(order) ) {
1774 map<int,int> myFaceMap;
1775 SMDS_FaceIteratorPtr itFaces=_myMeshDS->facesIterator();
1776 while( itFaces->more( ) ) {
1777 int nbNodes = itFaces->next()->NbNodes();
1778 if ( myFaceMap.find( nbNodes ) == myFaceMap.end() )
1779 myFaceMap[ nbNodes ] = 0;
1780 myFaceMap[ nbNodes ] = myFaceMap[ nbNodes ] + 1;
1782 save << clause << ".3) Faces in detail: " << endl;
1783 map <int,int>::iterator itF;
1784 for (itF = myFaceMap.begin(); itF != myFaceMap.end(); itF++)
1785 save << "--> nb nodes: " << itF->first << " - nb elemens:\t" << itF->second << endl;
1788 save << ++clause << ") Total number of " << orderStr << " volumes:\t" << NbVolumes(order) << endl;
1789 if ( NbVolumes(order) > 0 ) {
1790 int nb8 = NbHexas(order);
1791 int nb4 = NbTetras(order);
1792 int nb5 = NbPyramids(order);
1793 int nb6 = NbPrisms(order);
1794 save << clause << ".1) Number of " << orderStr << " hexahedrons:\t" << nb8 << endl;
1795 save << clause << ".2) Number of " << orderStr << " tetrahedrons:\t" << nb4 << endl;
1796 save << clause << ".3) Number of " << orderStr << " prisms: \t" << nb6 << endl;
1797 save << clause << ".4) Number of " << orderStr << " pyramids:\t" << nb5 << endl;
1798 if ( nb8 + nb4 + nb5 + nb6 != NbVolumes(order) ) {
1799 map<int,int> myVolumesMap;
1800 SMDS_VolumeIteratorPtr itVolumes=_myMeshDS->volumesIterator();
1801 while( itVolumes->more( ) ) {
1802 int nbNodes = itVolumes->next()->NbNodes();
1803 if ( myVolumesMap.find( nbNodes ) == myVolumesMap.end() )
1804 myVolumesMap[ nbNodes ] = 0;
1805 myVolumesMap[ nbNodes ] = myVolumesMap[ nbNodes ] + 1;
1807 save << clause << ".5) Volumes in detail: " << endl;
1808 map <int,int>::iterator itV;
1809 for (itV = myVolumesMap.begin(); itV != myVolumesMap.end(); itV++)
1810 save << "--> nb nodes: " << itV->first << " - nb elemens:\t" << itV->second << endl;
1815 save << "===========================================================================" << endl;
1819 //=======================================================================
1820 //function : GetElementType
1821 //purpose : Returns type of mesh element with certain id
1822 //=======================================================================
1824 SMDSAbs_ElementType SMESH_Mesh::GetElementType( const int id, const bool iselem )
1826 return _myMeshDS->GetElementType( id, iselem );
1829 //=============================================================================
1831 * \brief Convert group on geometry into standalone group
1833 //=============================================================================
1835 SMESH_Group* SMESH_Mesh::ConvertToStandalone ( int theGroupID )
1837 SMESH_Group* aGroup = 0;
1838 map < int, SMESH_Group * >::iterator itg = _mapGroup.find( theGroupID );
1839 if ( itg == _mapGroup.end() )
1842 SMESH_Group* anOldGrp = (*itg).second;
1843 SMESHDS_GroupBase* anOldGrpDS = anOldGrp->GetGroupDS();
1844 if ( !anOldGrp || !anOldGrpDS )
1847 // create new standalone group
1848 aGroup = new SMESH_Group (theGroupID, this, anOldGrpDS->GetType(), anOldGrp->GetName() );
1849 _mapGroup[theGroupID] = aGroup;
1851 SMESHDS_Group* aNewGrpDS = dynamic_cast<SMESHDS_Group*>( aGroup->GetGroupDS() );
1852 GetMeshDS()->RemoveGroup( anOldGrpDS );
1853 GetMeshDS()->AddGroup( aNewGrpDS );
1855 // add elements (or nodes) into new created group
1856 SMDS_ElemIteratorPtr anItr = anOldGrpDS->GetElements();
1857 while ( anItr->more() )
1858 aNewGrpDS->Add( (anItr->next())->GetID() );
1861 aNewGrpDS->SetColor( anOldGrpDS->GetColor() );
1869 //=============================================================================
1871 * \brief remove submesh order from Mesh
1873 //=============================================================================
1875 void SMESH_Mesh::ClearMeshOrder()
1877 _mySubMeshOrder.clear();
1880 //=============================================================================
1882 * \brief remove submesh order from Mesh
1884 //=============================================================================
1886 void SMESH_Mesh::SetMeshOrder(const TListOfListOfInt& theOrder )
1888 _mySubMeshOrder = theOrder;
1891 //=============================================================================
1893 * \brief return submesh order if any
1895 //=============================================================================
1897 const TListOfListOfInt& SMESH_Mesh::GetMeshOrder() const
1899 return _mySubMeshOrder;
1902 //=============================================================================
1904 * \brief fill _mapAncestors
1906 //=============================================================================
1908 void SMESH_Mesh::fillAncestorsMap(const TopoDS_Shape& theShape)
1911 int desType, ancType;
1912 if ( !theShape.IsSame( GetShapeToMesh()) && theShape.ShapeType() == TopAbs_COMPOUND )
1914 // a geom group is added. Insert it into lists of ancestors before
1915 // the first ancestor more complex than group members
1916 int memberType = TopoDS_Iterator( theShape ).Value().ShapeType();
1917 for ( desType = TopAbs_VERTEX; desType >= memberType; desType-- )
1918 for (TopExp_Explorer des( theShape, TopAbs_ShapeEnum( desType )); des.More(); des.Next())
1920 if ( !_mapAncestors.Contains( des.Current() )) continue;// issue 0020982
1921 TopTools_ListOfShape& ancList = _mapAncestors.ChangeFromKey( des.Current() );
1922 TopTools_ListIteratorOfListOfShape ancIt (ancList);
1923 while ( ancIt.More() && ancIt.Value().ShapeType() >= memberType )
1926 ancList.InsertBefore( theShape, ancIt );
1930 for ( desType = TopAbs_VERTEX; desType > TopAbs_COMPOUND; desType-- )
1931 for ( ancType = desType - 1; ancType >= TopAbs_COMPOUND; ancType-- )
1932 TopExp::MapShapesAndAncestors ( theShape,
1933 (TopAbs_ShapeEnum) desType,
1934 (TopAbs_ShapeEnum) ancType,
1939 //=============================================================================
1941 * \brief sort submeshes according to stored mesh order
1942 * \param theListToSort in out list to be sorted
1943 * \return FALSE if nothing sorted
1945 //=============================================================================
1947 bool SMESH_Mesh::SortByMeshOrder(list<SMESH_subMesh*>& theListToSort) const
1949 if ( !_mySubMeshOrder.size() || theListToSort.size() < 2)
1953 list<SMESH_subMesh*> onlyOrderedList;
1954 // collect all ordered submeshes in one list as pointers
1955 // and get their positions within theListToSort
1956 typedef list<SMESH_subMesh*>::iterator TPosInList;
1957 map< int, TPosInList > sortedPos;
1958 TPosInList smBeg = theListToSort.begin(), smEnd = theListToSort.end();
1959 TListOfListOfInt::const_iterator listIddIt = _mySubMeshOrder.begin();
1960 for( ; listIddIt != _mySubMeshOrder.end(); listIddIt++) {
1961 const TListOfInt& listOfId = *listIddIt;
1962 TListOfInt::const_iterator idIt = listOfId.begin();
1963 for ( ; idIt != listOfId.end(); idIt++ ) {
1964 if ( SMESH_subMesh * sm = GetSubMeshContaining( *idIt )) {
1965 TPosInList smPos = find( smBeg, smEnd, sm );
1966 if ( smPos != smEnd ) {
1967 onlyOrderedList.push_back( sm );
1968 sortedPos[ distance( smBeg, smPos )] = smPos;
1973 if (onlyOrderedList.size() < 2)
1977 list<SMESH_subMesh*>::iterator onlyBIt = onlyOrderedList.begin();
1978 list<SMESH_subMesh*>::iterator onlyEIt = onlyOrderedList.end();
1980 // iterate on ordered submeshes and insert them in detected positions
1981 map< int, TPosInList >::iterator i_pos = sortedPos.begin();
1982 for ( ; onlyBIt != onlyEIt; ++onlyBIt, ++i_pos )
1983 *(i_pos->second) = *onlyBIt;
1988 //=============================================================================
1990 * \brief sort submeshes according to stored mesh order
1991 * \param theListToSort in out list to be sorted
1992 * \return FALSE if nothing sorted
1994 //=============================================================================
1996 list<SMESH_subMesh*> SMESH_Mesh::getAncestorsSubMeshes
1997 (const TopoDS_Shape& theSubShape) const
1999 list<SMESH_subMesh*> listOfSubMesh;
2000 TopTools_ListIteratorOfListOfShape it( GetAncestors( theSubShape ));
2001 for (; it.More(); it.Next() )
2002 if ( SMESH_subMesh* sm = GetSubMeshContaining( it.Value() ))
2003 listOfSubMesh.push_back(sm);
2005 // sort submeshes according to stored mesh order
2006 SortByMeshOrder( listOfSubMesh );
2008 return listOfSubMesh;