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 "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;
910 //=============================================================================
912 * Get the SMESH_subMesh object implementation. Dont create it, return null
913 * if it does not exist.
915 //=============================================================================
917 SMESH_subMesh *SMESH_Mesh::GetSubMeshContaining(const TopoDS_Shape & aSubShape) const
918 throw(SALOME_Exception)
920 Unexpect aCatch(SalomeException);
921 SMESH_subMesh *aSubMesh = NULL;
923 int index = _myMeshDS->ShapeToIndex(aSubShape);
925 map <int, SMESH_subMesh *>::const_iterator i_sm = _mapSubMesh.find(index);
926 if ( i_sm != _mapSubMesh.end())
927 aSubMesh = i_sm->second;
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 int aShapeID) const
939 throw(SALOME_Exception)
941 Unexpect aCatch(SalomeException);
943 map <int, SMESH_subMesh *>::const_iterator i_sm = _mapSubMesh.find(aShapeID);
944 if (i_sm == _mapSubMesh.end())
948 //================================================================================
950 * \brief Return submeshes of groups containing the given sub-shape
952 //================================================================================
955 SMESH_Mesh::GetGroupSubMeshesContaining(const TopoDS_Shape & aSubShape) const
956 throw(SALOME_Exception)
958 Unexpect aCatch(SalomeException);
959 list<SMESH_subMesh*> found;
961 SMESH_subMesh * subMesh = GetSubMeshContaining(aSubShape);
965 // submeshes of groups have max IDs, so search from the map end
966 map<int, SMESH_subMesh *>::const_reverse_iterator i_sm;
967 for ( i_sm = _mapSubMesh.rbegin(); i_sm != _mapSubMesh.rend(); ++i_sm) {
968 SMESHDS_SubMesh * ds = i_sm->second->GetSubMeshDS();
969 if ( ds && ds->IsComplexSubmesh() ) {
970 if ( SMESH_MesherHelper::IsSubShape( aSubShape, i_sm->second->GetSubShape() ))
972 found.push_back( i_sm->second );
976 break; // the rest sub-meshes are not those of groups
980 if ( found.empty() ) // maybe the main shape is a COMPOUND (issue 0021530)
982 if ( SMESH_subMesh * mainSM = GetSubMeshContaining(1))
983 if ( mainSM->GetSubShape().ShapeType() == TopAbs_COMPOUND )
985 TopoDS_Iterator it( mainSM->GetSubShape() );
986 if ( it.Value().ShapeType() == aSubShape.ShapeType() &&
987 SMESH_MesherHelper::IsSubShape( aSubShape, mainSM->GetSubShape() ))
988 found.push_back( mainSM );
993 //=======================================================================
994 //function : IsUsedHypothesis
995 //purpose : Return True if anHyp is used to mesh aSubShape
996 //=======================================================================
998 bool SMESH_Mesh::IsUsedHypothesis(SMESHDS_Hypothesis * anHyp,
999 const SMESH_subMesh* aSubMesh)
1001 SMESH_Hypothesis* hyp = static_cast<SMESH_Hypothesis*>(anHyp);
1003 // check if anHyp can be used to mesh aSubMesh
1004 if ( !aSubMesh || !aSubMesh->IsApplicableHypotesis( hyp ))
1007 const TopoDS_Shape & aSubShape = const_cast<SMESH_subMesh*>( aSubMesh )->GetSubShape();
1009 SMESH_Algo *algo = _gen->GetAlgo(*this, aSubShape );
1012 if (anHyp->GetType() > SMESHDS_Hypothesis::PARAM_ALGO)
1013 return ( anHyp == algo );
1015 // algorithm parameter
1018 // look trough hypotheses used by algo
1019 SMESH_HypoFilter hypoKind;
1020 if ( algo->InitCompatibleHypoFilter( hypoKind, !hyp->IsAuxiliary() )) {
1021 list <const SMESHDS_Hypothesis * > usedHyps;
1022 if ( GetHypotheses( aSubShape, hypoKind, usedHyps, true ))
1023 return ( find( usedHyps.begin(), usedHyps.end(), anHyp ) != usedHyps.end() );
1027 // look through all assigned hypotheses
1028 //SMESH_HypoFilter filter( SMESH_HypoFilter::Is( hyp ));
1029 return false; //GetHypothesis( aSubShape, filter, true );
1032 //=============================================================================
1036 //=============================================================================
1038 const list < SMESH_subMesh * >&
1039 SMESH_Mesh::GetSubMeshUsingHypothesis(SMESHDS_Hypothesis * anHyp)
1040 throw(SALOME_Exception)
1042 Unexpect aCatch(SalomeException);
1043 if(MYDEBUG) MESSAGE("SMESH_Mesh::GetSubMeshUsingHypothesis");
1044 map < int, SMESH_subMesh * >::iterator itsm;
1045 _subMeshesUsingHypothesisList.clear();
1046 for (itsm = _mapSubMesh.begin(); itsm != _mapSubMesh.end(); itsm++)
1048 SMESH_subMesh *aSubMesh = (*itsm).second;
1049 if ( IsUsedHypothesis ( anHyp, aSubMesh ))
1050 _subMeshesUsingHypothesisList.push_back(aSubMesh);
1052 return _subMeshesUsingHypothesisList;
1055 //=======================================================================
1056 //function : NotifySubMeshesHypothesisModification
1057 //purpose : Say all submeshes using theChangedHyp that it has been modified
1058 //=======================================================================
1060 void SMESH_Mesh::NotifySubMeshesHypothesisModification(const SMESH_Hypothesis* hyp)
1062 Unexpect aCatch(SalomeException);
1064 if ( !GetMeshDS()->IsUsedHypothesis( hyp ))
1068 _callUp->HypothesisModified();
1070 const SMESH_Algo *foundAlgo = 0;
1071 SMESH_HypoFilter algoKind, compatibleHypoKind;
1072 list <const SMESHDS_Hypothesis * > usedHyps;
1075 map < int, SMESH_subMesh * >::iterator itsm;
1076 for (itsm = _mapSubMesh.begin(); itsm != _mapSubMesh.end(); itsm++)
1078 SMESH_subMesh *aSubMesh = (*itsm).second;
1079 if ( aSubMesh->IsApplicableHypotesis( hyp ))
1081 const TopoDS_Shape & aSubShape = aSubMesh->GetSubShape();
1083 if ( !foundAlgo ) // init filter for algo search
1084 algoKind.Init( THypType::IsAlgo() ).And( THypType::IsApplicableTo( aSubShape ));
1086 const SMESH_Algo *algo = static_cast<const SMESH_Algo*>
1087 ( GetHypothesis( aSubShape, algoKind, true ));
1091 bool sameAlgo = ( algo == foundAlgo );
1092 if ( !sameAlgo && foundAlgo )
1093 sameAlgo = ( strcmp( algo->GetName(), foundAlgo->GetName() ) == 0);
1095 if ( !sameAlgo ) { // init filter for used hypos search
1096 if ( !algo->InitCompatibleHypoFilter( compatibleHypoKind, !hyp->IsAuxiliary() ))
1097 continue; // algo does not use any hypothesis
1101 // check if hyp is used by algo
1103 if ( GetHypotheses( aSubShape, compatibleHypoKind, usedHyps, true ) &&
1104 find( usedHyps.begin(), usedHyps.end(), hyp ) != usedHyps.end() )
1106 aSubMesh->AlgoStateEngine(SMESH_subMesh::MODIF_HYP,
1107 const_cast< SMESH_Hypothesis*>( hyp ));
1112 HasModificationsToDiscard(); // to reset _isModified flag if mesh becomes empty
1113 GetMeshDS()->Modified();
1116 //=============================================================================
1118 * Auto color functionality
1120 //=============================================================================
1121 void SMESH_Mesh::SetAutoColor(bool theAutoColor) throw(SALOME_Exception)
1123 Unexpect aCatch(SalomeException);
1124 _isAutoColor = theAutoColor;
1127 bool SMESH_Mesh::GetAutoColor() throw(SALOME_Exception)
1129 Unexpect aCatch(SalomeException);
1130 return _isAutoColor;
1133 //=======================================================================
1134 //function : SetIsModified
1135 //purpose : Set the flag meaning that the mesh has been edited "manually"
1136 //=======================================================================
1138 void SMESH_Mesh::SetIsModified(bool isModified)
1140 _isModified = isModified;
1143 // check if mesh becomes empty as result of modification
1144 HasModificationsToDiscard();
1147 //=======================================================================
1148 //function : HasModificationsToDiscard
1149 //purpose : Return true if the mesh has been edited since a total re-compute
1150 // and those modifications may prevent successful partial re-compute.
1151 // As a side effect reset _isModified flag if mesh is empty
1153 //=======================================================================
1155 bool SMESH_Mesh::HasModificationsToDiscard() const
1157 if ( ! _isModified )
1160 // return true if the next Compute() will be partial and
1161 // existing but changed elements may prevent successful re-compute
1162 bool hasComputed = false, hasNotComputed = false;
1163 map <int, SMESH_subMesh*>::const_iterator i_sm = _mapSubMesh.begin();
1164 for ( ; i_sm != _mapSubMesh.end() ; ++i_sm )
1165 switch ( i_sm->second->GetSubShape().ShapeType() )
1170 if ( i_sm->second->IsMeshComputed() )
1173 hasNotComputed = true;
1174 if ( hasComputed && hasNotComputed)
1178 if ( NbNodes() < 1 )
1179 const_cast<SMESH_Mesh*>(this)->_isModified = false;
1184 //================================================================================
1186 * \brief Check if any groups of the same type have equal names
1188 //================================================================================
1190 bool SMESH_Mesh::HasDuplicatedGroupNamesMED()
1192 //set<string> aGroupNames; // Corrected for Mantis issue 0020028
1193 map< SMDSAbs_ElementType, set<string> > aGroupNames;
1194 for ( map<int, SMESH_Group*>::iterator it = _mapGroup.begin(); it != _mapGroup.end(); it++ )
1196 SMESH_Group* aGroup = it->second;
1197 SMDSAbs_ElementType aType = aGroup->GetGroupDS()->GetType();
1198 string aGroupName = aGroup->GetName();
1199 aGroupName.resize(MAX_MED_GROUP_NAME_LENGTH);
1200 if (!aGroupNames[aType].insert(aGroupName).second)
1207 //================================================================================
1209 * \brief Export the mesh to a med file
1211 //================================================================================
1213 void SMESH_Mesh::ExportMED(const char * file,
1214 const char* theMeshName,
1217 const SMESHDS_Mesh* meshPart)
1218 throw(SALOME_Exception)
1220 Unexpect aCatch(SalomeException);
1222 DriverMED_W_SMESHDS_Mesh myWriter;
1223 myWriter.SetFile ( file, MED::EVersion(theVersion) );
1224 myWriter.SetMesh ( meshPart ? (SMESHDS_Mesh*) meshPart : _myMeshDS );
1226 myWriter.SetMeshId ( _idDoc );
1228 myWriter.SetMeshId ( -1 );
1229 myWriter.SetMeshName( theMeshName );
1232 if ( theAutoGroups ) {
1233 myWriter.AddGroupOfNodes();
1234 myWriter.AddGroupOfEdges();
1235 myWriter.AddGroupOfFaces();
1236 myWriter.AddGroupOfVolumes();
1239 // Pass groups to writer. Provide unique group names.
1240 //set<string> aGroupNames; // Corrected for Mantis issue 0020028
1243 map< SMDSAbs_ElementType, set<string> > aGroupNames;
1245 int maxNbIter = 10000; // to guarantee cycle finish
1246 for ( map<int, SMESH_Group*>::iterator it = _mapGroup.begin(); it != _mapGroup.end(); it++ ) {
1247 SMESH_Group* aGroup = it->second;
1248 SMESHDS_GroupBase* aGroupDS = aGroup->GetGroupDS();
1250 SMDSAbs_ElementType aType = aGroupDS->GetType();
1251 string aGroupName0 = aGroup->GetName();
1252 aGroupName0.resize(MAX_MED_GROUP_NAME_LENGTH);
1253 string aGroupName = aGroupName0;
1254 for (int i = 1; !aGroupNames[aType].insert(aGroupName).second && i < maxNbIter; i++) {
1255 sprintf(&aString[0], "GR_%d_%s", i, aGroupName0.c_str());
1256 aGroupName = aString;
1257 aGroupName.resize(MAX_MED_GROUP_NAME_LENGTH);
1259 aGroupDS->SetStoreName( aGroupName.c_str() );
1260 myWriter.AddGroup( aGroupDS );
1268 void SMESH_Mesh::ExportSAUV(const char *file,
1269 const char* theMeshName,
1271 throw(SALOME_Exception)
1273 std::string medfilename(file);
1274 medfilename += ".med";
1277 cmd = "%PYTHONBIN% ";
1282 cmd += "from medutilities import my_remove ; my_remove(r'" + medfilename + "')";
1284 system(cmd.c_str());
1285 ExportMED(medfilename.c_str(), theMeshName, theAutoGroups, 1);
1287 cmd = "%PYTHONBIN% ";
1292 cmd += "from medutilities import convert ; convert(r'" + medfilename + "', 'MED', 'GIBI', 1, r'" + file + "')";
1294 system(cmd.c_str());
1296 cmd = "%PYTHONBIN% ";
1301 cmd += "from medutilities import my_remove ; my_remove(r'" + medfilename + "')";
1303 system(cmd.c_str());
1306 //================================================================================
1308 * \brief Export the mesh to a DAT file
1310 //================================================================================
1312 void SMESH_Mesh::ExportDAT(const char * file,
1313 const SMESHDS_Mesh* meshPart) throw(SALOME_Exception)
1315 Unexpect aCatch(SalomeException);
1316 DriverDAT_W_SMDS_Mesh myWriter;
1317 myWriter.SetFile( file );
1318 myWriter.SetMesh( meshPart ? (SMESHDS_Mesh*) meshPart : _myMeshDS );
1319 myWriter.SetMeshId(_idDoc);
1323 //================================================================================
1325 * \brief Export the mesh to an UNV file
1327 //================================================================================
1329 void SMESH_Mesh::ExportUNV(const char * file,
1330 const SMESHDS_Mesh* meshPart) throw(SALOME_Exception)
1332 Unexpect aCatch(SalomeException);
1333 DriverUNV_W_SMDS_Mesh myWriter;
1334 myWriter.SetFile( file );
1335 myWriter.SetMesh( meshPart ? (SMESHDS_Mesh*) meshPart : _myMeshDS );
1336 myWriter.SetMeshId(_idDoc);
1337 // myWriter.SetGroups(_mapGroup);
1341 for ( map<int, SMESH_Group*>::iterator it = _mapGroup.begin(); it != _mapGroup.end(); it++ ) {
1342 SMESH_Group* aGroup = it->second;
1343 SMESHDS_GroupBase* aGroupDS = aGroup->GetGroupDS();
1345 string aGroupName = aGroup->GetName();
1346 aGroupDS->SetStoreName( aGroupName.c_str() );
1347 myWriter.AddGroup( aGroupDS );
1354 //================================================================================
1356 * \brief Export the mesh to an STL file
1358 //================================================================================
1360 void SMESH_Mesh::ExportSTL(const char * file,
1362 const SMESHDS_Mesh* meshPart) throw(SALOME_Exception)
1364 Unexpect aCatch(SalomeException);
1365 DriverSTL_W_SMDS_Mesh myWriter;
1366 myWriter.SetFile( file );
1367 myWriter.SetIsAscii( isascii );
1368 myWriter.SetMesh( meshPart ? (SMESHDS_Mesh*) meshPart : _myMeshDS);
1369 myWriter.SetMeshId(_idDoc);
1373 //================================================================================
1375 * \brief Export the mesh to the CGNS file
1377 //================================================================================
1379 void SMESH_Mesh::ExportCGNS(const char * file,
1380 const SMESHDS_Mesh* meshDS)
1382 int res = Driver_Mesh::DRS_FAIL;
1384 DriverCGNS_Write myWriter;
1385 myWriter.SetFile( file );
1386 myWriter.SetMesh( const_cast<SMESHDS_Mesh*>( meshDS ));
1387 myWriter.SetMeshName( SMESH_Comment("Mesh_") << meshDS->GetPersistentId());
1388 res = myWriter.Perform();
1390 if ( res != Driver_Mesh::DRS_OK )
1391 throw SALOME_Exception("Export failed");
1394 //================================================================================
1396 * \brief Return number of nodes in the mesh
1398 //================================================================================
1400 int SMESH_Mesh::NbNodes() const throw(SALOME_Exception)
1402 Unexpect aCatch(SalomeException);
1403 return _myMeshDS->NbNodes();
1406 //================================================================================
1408 * \brief Return number of edges of given order in the mesh
1410 //================================================================================
1412 int SMESH_Mesh::Nb0DElements() const throw(SALOME_Exception)
1414 Unexpect aCatch(SalomeException);
1415 return _myMeshDS->GetMeshInfo().Nb0DElements();
1418 //================================================================================
1420 * \brief Return number of edges of given order in the mesh
1422 //================================================================================
1424 int SMESH_Mesh::NbEdges(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1426 Unexpect aCatch(SalomeException);
1427 return _myMeshDS->GetMeshInfo().NbEdges(order);
1430 //================================================================================
1432 * \brief Return number of faces of given order in the mesh
1434 //================================================================================
1436 int SMESH_Mesh::NbFaces(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1438 Unexpect aCatch(SalomeException);
1439 return _myMeshDS->GetMeshInfo().NbFaces(order);
1442 //================================================================================
1444 * \brief Return the number of faces in the mesh
1446 //================================================================================
1448 int SMESH_Mesh::NbTriangles(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1450 Unexpect aCatch(SalomeException);
1451 return _myMeshDS->GetMeshInfo().NbTriangles(order);
1454 //================================================================================
1456 * \brief Return the number nodes faces in the mesh
1458 //================================================================================
1460 int SMESH_Mesh::NbQuadrangles(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1462 Unexpect aCatch(SalomeException);
1463 return _myMeshDS->GetMeshInfo().NbQuadrangles(order);
1466 //================================================================================
1468 * \brief Return number of biquadratic quadrangles in the mesh
1470 //================================================================================
1472 int SMESH_Mesh::NbBiQuadQuadrangles() const throw(SALOME_Exception)
1474 Unexpect aCatch(SalomeException);
1475 return _myMeshDS->GetMeshInfo().NbBiQuadQuadrangles();
1478 //================================================================================
1480 * \brief Return the number of polygonal faces in the mesh
1482 //================================================================================
1484 int SMESH_Mesh::NbPolygons() const throw(SALOME_Exception)
1486 Unexpect aCatch(SalomeException);
1487 return _myMeshDS->GetMeshInfo().NbPolygons();
1490 //================================================================================
1492 * \brief Return number of volumes of given order in the mesh
1494 //================================================================================
1496 int SMESH_Mesh::NbVolumes(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1498 Unexpect aCatch(SalomeException);
1499 return _myMeshDS->GetMeshInfo().NbVolumes(order);
1502 //================================================================================
1504 * \brief Return number of tetrahedrons of given order in the mesh
1506 //================================================================================
1508 int SMESH_Mesh::NbTetras(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1510 Unexpect aCatch(SalomeException);
1511 return _myMeshDS->GetMeshInfo().NbTetras(order);
1514 //================================================================================
1516 * \brief Return number of hexahedrons of given order in the mesh
1518 //================================================================================
1520 int SMESH_Mesh::NbHexas(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1522 Unexpect aCatch(SalomeException);
1523 return _myMeshDS->GetMeshInfo().NbHexas(order);
1526 //================================================================================
1528 * \brief Return number of triquadratic hexahedrons in the mesh
1530 //================================================================================
1532 int SMESH_Mesh::NbTriQuadraticHexas() const throw(SALOME_Exception)
1534 Unexpect aCatch(SalomeException);
1535 return _myMeshDS->GetMeshInfo().NbTriQuadHexas();
1538 //================================================================================
1540 * \brief Return number of pyramids of given order in the mesh
1542 //================================================================================
1544 int SMESH_Mesh::NbPyramids(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1546 Unexpect aCatch(SalomeException);
1547 return _myMeshDS->GetMeshInfo().NbPyramids(order);
1550 //================================================================================
1552 * \brief Return number of prisms (penthahedrons) of given order in the mesh
1554 //================================================================================
1556 int SMESH_Mesh::NbPrisms(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1558 Unexpect aCatch(SalomeException);
1559 return _myMeshDS->GetMeshInfo().NbPrisms(order);
1562 //================================================================================
1564 * \brief Return number of hexagonal prisms in the mesh
1566 //================================================================================
1568 int SMESH_Mesh::NbHexagonalPrisms() const throw(SALOME_Exception)
1570 Unexpect aCatch(SalomeException);
1571 return _myMeshDS->GetMeshInfo().NbHexPrisms();
1574 //================================================================================
1576 * \brief Return number of polyhedrons in the mesh
1578 //================================================================================
1580 int SMESH_Mesh::NbPolyhedrons() const throw(SALOME_Exception)
1582 Unexpect aCatch(SalomeException);
1583 return _myMeshDS->GetMeshInfo().NbPolyhedrons();
1586 //================================================================================
1588 * \brief Return number of ball elements in the mesh
1590 //================================================================================
1592 int SMESH_Mesh::NbBalls() const throw(SALOME_Exception)
1594 Unexpect aCatch(SalomeException);
1595 return _myMeshDS->GetMeshInfo().NbBalls();
1598 //================================================================================
1600 * \brief Return number of submeshes in the mesh
1602 //================================================================================
1604 int SMESH_Mesh::NbSubMesh() const throw(SALOME_Exception)
1606 Unexpect aCatch(SalomeException);
1607 return _myMeshDS->NbSubMesh();
1610 //=======================================================================
1611 //function : IsNotConformAllowed
1612 //purpose : check if a hypothesis alowing notconform mesh is present
1613 //=======================================================================
1615 bool SMESH_Mesh::IsNotConformAllowed() const
1617 if(MYDEBUG) MESSAGE("SMESH_Mesh::IsNotConformAllowed");
1619 static SMESH_HypoFilter filter( SMESH_HypoFilter::HasName( "NotConformAllowed" ));
1620 return GetHypothesis( _myMeshDS->ShapeToMesh(), filter, false );
1623 //=======================================================================
1624 //function : IsMainShape
1626 //=======================================================================
1628 bool SMESH_Mesh::IsMainShape(const TopoDS_Shape& theShape) const
1630 return theShape.IsSame(_myMeshDS->ShapeToMesh() );
1633 //=============================================================================
1637 //=============================================================================
1639 SMESH_Group* SMESH_Mesh::AddGroup (const SMDSAbs_ElementType theType,
1640 const char* theName,
1642 const TopoDS_Shape& theShape,
1643 const SMESH_PredicatePtr& thePredicate)
1645 if (_mapGroup.count(_groupId))
1648 SMESH_Group* aGroup = new SMESH_Group (theId, this, theType, theName, theShape, thePredicate);
1649 GetMeshDS()->AddGroup( aGroup->GetGroupDS() );
1650 _mapGroup[_groupId++] = aGroup;
1654 //================================================================================
1656 * \brief Creates a group based on an existing SMESHDS group. Group ID should be unique
1658 //================================================================================
1660 SMESH_Group* SMESH_Mesh::AddGroup (SMESHDS_GroupBase* groupDS) throw(SALOME_Exception)
1663 throw SALOME_Exception(LOCALIZED ("SMESH_Mesh::AddGroup(): NULL SMESHDS_GroupBase"));
1665 map <int, SMESH_Group*>::iterator i_g = _mapGroup.find( groupDS->GetID() );
1666 if ( i_g != _mapGroup.end() && i_g->second )
1668 if ( i_g->second->GetGroupDS() == groupDS )
1671 throw SALOME_Exception(LOCALIZED ("SMESH_Mesh::AddGroup() wrong ID of SMESHDS_GroupBase"));
1673 SMESH_Group* aGroup = new SMESH_Group (groupDS);
1674 _mapGroup[ groupDS->GetID() ] = aGroup;
1675 GetMeshDS()->AddGroup( aGroup->GetGroupDS() );
1677 _groupId = 1 + _mapGroup.rbegin()->first;
1683 //================================================================================
1685 * \brief Creates SMESH_Groups for not wrapped SMESHDS_Groups
1686 * \retval bool - true if new SMESH_Groups have been created
1689 //================================================================================
1691 bool SMESH_Mesh::SynchronizeGroups()
1693 int nbGroups = _mapGroup.size();
1694 const set<SMESHDS_GroupBase*>& groups = _myMeshDS->GetGroups();
1695 set<SMESHDS_GroupBase*>::const_iterator gIt = groups.begin();
1696 for ( ; gIt != groups.end(); ++gIt )
1698 SMESHDS_GroupBase* groupDS = (SMESHDS_GroupBase*) *gIt;
1699 _groupId = groupDS->GetID();
1700 if ( !_mapGroup.count( _groupId ))
1701 _mapGroup[_groupId] = new SMESH_Group( groupDS );
1703 if ( !_mapGroup.empty() )
1704 _groupId = _mapGroup.rbegin()->first + 1;
1706 return nbGroups < _mapGroup.size();
1709 //================================================================================
1711 * \brief Return iterator on all existing groups
1713 //================================================================================
1715 SMESH_Mesh::GroupIteratorPtr SMESH_Mesh::GetGroups() const
1717 typedef map <int, SMESH_Group *> TMap;
1718 return GroupIteratorPtr( new SMDS_mapIterator<TMap>( _mapGroup ));
1721 //=============================================================================
1723 * \brief Return a group by ID
1725 //=============================================================================
1727 SMESH_Group* SMESH_Mesh::GetGroup (const int theGroupID)
1729 if (_mapGroup.find(theGroupID) == _mapGroup.end())
1731 return _mapGroup[theGroupID];
1735 //=============================================================================
1737 * \brief Return IDs of all groups
1739 //=============================================================================
1741 list<int> SMESH_Mesh::GetGroupIds() const
1744 for ( map<int, SMESH_Group*>::const_iterator it = _mapGroup.begin(); it != _mapGroup.end(); it++ )
1745 anIds.push_back( it->first );
1750 //================================================================================
1752 * \brief Set a caller of methods at level of CORBA API implementation.
1753 * The set upCaller will be deleted by SMESH_Mesh
1755 //================================================================================
1757 void SMESH_Mesh::SetCallUp( TCallUp* upCaller )
1759 if ( _callUp ) delete _callUp;
1763 //=============================================================================
1767 //=============================================================================
1769 bool SMESH_Mesh::RemoveGroup (const int theGroupID)
1771 if (_mapGroup.find(theGroupID) == _mapGroup.end())
1773 GetMeshDS()->RemoveGroup( _mapGroup[theGroupID]->GetGroupDS() );
1774 delete _mapGroup[theGroupID];
1775 _mapGroup.erase (theGroupID);
1777 _callUp->RemoveGroup( theGroupID );
1781 //=======================================================================
1782 //function : GetAncestors
1783 //purpose : return list of ancestors of theSubShape in the order
1784 // that lower dimention shapes come first.
1785 //=======================================================================
1787 const TopTools_ListOfShape& SMESH_Mesh::GetAncestors(const TopoDS_Shape& theS) const
1789 if ( _mapAncestors.Contains( theS ) )
1790 return _mapAncestors.FindFromKey( theS );
1792 static TopTools_ListOfShape emptyList;
1796 //=======================================================================
1798 //purpose : dumps contents of mesh to stream [ debug purposes ]
1799 //=======================================================================
1801 ostream& SMESH_Mesh::Dump(ostream& save)
1804 save << "========================== Dump contents of mesh ==========================" << endl << endl;
1805 save << ++clause << ") Total number of nodes: \t" << NbNodes() << endl;
1806 save << ++clause << ") Total number of edges: \t" << NbEdges() << endl;
1807 save << ++clause << ") Total number of faces: \t" << NbFaces() << endl;
1808 save << ++clause << ") Total number of polygons:\t" << NbPolygons() << endl;
1809 save << ++clause << ") Total number of volumes:\t" << NbVolumes() << endl;
1810 save << ++clause << ") Total number of polyhedrons:\t" << NbPolyhedrons() << endl << endl;
1811 for ( int isQuadratic = 0; isQuadratic < 2; ++isQuadratic )
1813 string orderStr = isQuadratic ? "quadratic" : "linear";
1814 SMDSAbs_ElementOrder order = isQuadratic ? ORDER_QUADRATIC : ORDER_LINEAR;
1816 save << ++clause << ") Total number of " << orderStr << " edges:\t" << NbEdges(order) << endl;
1817 save << ++clause << ") Total number of " << orderStr << " faces:\t" << NbFaces(order) << endl;
1818 if ( NbFaces(order) > 0 ) {
1819 int nb3 = NbTriangles(order);
1820 int nb4 = NbQuadrangles(order);
1821 save << clause << ".1) Number of " << orderStr << " triangles: \t" << nb3 << endl;
1822 save << clause << ".2) Number of " << orderStr << " quadrangles:\t" << nb4 << endl;
1823 if ( nb3 + nb4 != NbFaces(order) ) {
1824 map<int,int> myFaceMap;
1825 SMDS_FaceIteratorPtr itFaces=_myMeshDS->facesIterator();
1826 while( itFaces->more( ) ) {
1827 int nbNodes = itFaces->next()->NbNodes();
1828 if ( myFaceMap.find( nbNodes ) == myFaceMap.end() )
1829 myFaceMap[ nbNodes ] = 0;
1830 myFaceMap[ nbNodes ] = myFaceMap[ nbNodes ] + 1;
1832 save << clause << ".3) Faces in detail: " << endl;
1833 map <int,int>::iterator itF;
1834 for (itF = myFaceMap.begin(); itF != myFaceMap.end(); itF++)
1835 save << "--> nb nodes: " << itF->first << " - nb elemens:\t" << itF->second << endl;
1838 save << ++clause << ") Total number of " << orderStr << " volumes:\t" << NbVolumes(order) << endl;
1839 if ( NbVolumes(order) > 0 ) {
1840 int nb8 = NbHexas(order);
1841 int nb4 = NbTetras(order);
1842 int nb5 = NbPyramids(order);
1843 int nb6 = NbPrisms(order);
1844 save << clause << ".1) Number of " << orderStr << " hexahedrons:\t" << nb8 << endl;
1845 save << clause << ".2) Number of " << orderStr << " tetrahedrons:\t" << nb4 << endl;
1846 save << clause << ".3) Number of " << orderStr << " prisms: \t" << nb6 << endl;
1847 save << clause << ".4) Number of " << orderStr << " pyramids:\t" << nb5 << endl;
1848 if ( nb8 + nb4 + nb5 + nb6 != NbVolumes(order) ) {
1849 map<int,int> myVolumesMap;
1850 SMDS_VolumeIteratorPtr itVolumes=_myMeshDS->volumesIterator();
1851 while( itVolumes->more( ) ) {
1852 int nbNodes = itVolumes->next()->NbNodes();
1853 if ( myVolumesMap.find( nbNodes ) == myVolumesMap.end() )
1854 myVolumesMap[ nbNodes ] = 0;
1855 myVolumesMap[ nbNodes ] = myVolumesMap[ nbNodes ] + 1;
1857 save << clause << ".5) Volumes in detail: " << endl;
1858 map <int,int>::iterator itV;
1859 for (itV = myVolumesMap.begin(); itV != myVolumesMap.end(); itV++)
1860 save << "--> nb nodes: " << itV->first << " - nb elemens:\t" << itV->second << endl;
1865 save << "===========================================================================" << endl;
1869 //=======================================================================
1870 //function : GetElementType
1871 //purpose : Returns type of mesh element with certain id
1872 //=======================================================================
1874 SMDSAbs_ElementType SMESH_Mesh::GetElementType( const int id, const bool iselem )
1876 return _myMeshDS->GetElementType( id, iselem );
1879 //=============================================================================
1881 * \brief Convert group on geometry into standalone group
1883 //=============================================================================
1885 SMESH_Group* SMESH_Mesh::ConvertToStandalone ( int theGroupID )
1887 SMESH_Group* aGroup = 0;
1888 map < int, SMESH_Group * >::iterator itg = _mapGroup.find( theGroupID );
1889 if ( itg == _mapGroup.end() )
1892 SMESH_Group* anOldGrp = (*itg).second;
1893 SMESHDS_GroupBase* anOldGrpDS = anOldGrp->GetGroupDS();
1894 if ( !anOldGrp || !anOldGrpDS )
1897 // create new standalone group
1898 aGroup = new SMESH_Group (theGroupID, this, anOldGrpDS->GetType(), anOldGrp->GetName() );
1899 _mapGroup[theGroupID] = aGroup;
1901 SMESHDS_Group* aNewGrpDS = dynamic_cast<SMESHDS_Group*>( aGroup->GetGroupDS() );
1902 GetMeshDS()->RemoveGroup( anOldGrpDS );
1903 GetMeshDS()->AddGroup( aNewGrpDS );
1905 // add elements (or nodes) into new created group
1906 SMDS_ElemIteratorPtr anItr = anOldGrpDS->GetElements();
1907 while ( anItr->more() )
1908 aNewGrpDS->Add( (anItr->next())->GetID() );
1911 aNewGrpDS->SetColor( anOldGrpDS->GetColor() );
1919 //=============================================================================
1921 * \brief remove submesh order from Mesh
1923 //=============================================================================
1925 void SMESH_Mesh::ClearMeshOrder()
1927 _mySubMeshOrder.clear();
1930 //=============================================================================
1932 * \brief remove submesh order from Mesh
1934 //=============================================================================
1936 void SMESH_Mesh::SetMeshOrder(const TListOfListOfInt& theOrder )
1938 _mySubMeshOrder = theOrder;
1941 //=============================================================================
1943 * \brief return submesh order if any
1945 //=============================================================================
1947 const TListOfListOfInt& SMESH_Mesh::GetMeshOrder() const
1949 return _mySubMeshOrder;
1952 //=============================================================================
1954 * \brief fill _mapAncestors
1956 //=============================================================================
1958 void SMESH_Mesh::fillAncestorsMap(const TopoDS_Shape& theShape)
1961 int desType, ancType;
1962 if ( !theShape.IsSame( GetShapeToMesh()) && theShape.ShapeType() == TopAbs_COMPOUND )
1964 // a geom group is added. Insert it into lists of ancestors before
1965 // the first ancestor more complex than group members
1966 int memberType = TopoDS_Iterator( theShape ).Value().ShapeType();
1967 for ( desType = TopAbs_VERTEX; desType >= memberType; desType-- )
1968 for (TopExp_Explorer des( theShape, TopAbs_ShapeEnum( desType )); des.More(); des.Next())
1970 if ( !_mapAncestors.Contains( des.Current() )) continue;// issue 0020982
1971 TopTools_ListOfShape& ancList = _mapAncestors.ChangeFromKey( des.Current() );
1972 TopTools_ListIteratorOfListOfShape ancIt (ancList);
1973 while ( ancIt.More() && ancIt.Value().ShapeType() >= memberType )
1976 ancList.InsertBefore( theShape, ancIt );
1980 for ( desType = TopAbs_VERTEX; desType > TopAbs_COMPOUND; desType-- )
1981 for ( ancType = desType - 1; ancType >= TopAbs_COMPOUND; ancType-- )
1982 TopExp::MapShapesAndAncestors ( theShape,
1983 (TopAbs_ShapeEnum) desType,
1984 (TopAbs_ShapeEnum) ancType,
1989 //=============================================================================
1991 * \brief sort submeshes according to stored mesh order
1992 * \param theListToSort in out list to be sorted
1993 * \return FALSE if nothing sorted
1995 //=============================================================================
1997 bool SMESH_Mesh::SortByMeshOrder(list<SMESH_subMesh*>& theListToSort) const
1999 if ( !_mySubMeshOrder.size() || theListToSort.size() < 2)
2003 list<SMESH_subMesh*> onlyOrderedList;
2004 // collect all ordered submeshes in one list as pointers
2005 // and get their positions within theListToSort
2006 typedef list<SMESH_subMesh*>::iterator TPosInList;
2007 map< int, TPosInList > sortedPos;
2008 TPosInList smBeg = theListToSort.begin(), smEnd = theListToSort.end();
2009 TListOfListOfInt::const_iterator listIddIt = _mySubMeshOrder.begin();
2010 for( ; listIddIt != _mySubMeshOrder.end(); listIddIt++) {
2011 const TListOfInt& listOfId = *listIddIt;
2012 TListOfInt::const_iterator idIt = listOfId.begin();
2013 for ( ; idIt != listOfId.end(); idIt++ ) {
2014 if ( SMESH_subMesh * sm = GetSubMeshContaining( *idIt )) {
2015 TPosInList smPos = find( smBeg, smEnd, sm );
2016 if ( smPos != smEnd ) {
2017 onlyOrderedList.push_back( sm );
2018 sortedPos[ distance( smBeg, smPos )] = smPos;
2023 if (onlyOrderedList.size() < 2)
2027 list<SMESH_subMesh*>::iterator onlyBIt = onlyOrderedList.begin();
2028 list<SMESH_subMesh*>::iterator onlyEIt = onlyOrderedList.end();
2030 // iterate on ordered submeshes and insert them in detected positions
2031 map< int, TPosInList >::iterator i_pos = sortedPos.begin();
2032 for ( ; onlyBIt != onlyEIt; ++onlyBIt, ++i_pos )
2033 *(i_pos->second) = *onlyBIt;
2038 //=============================================================================
2040 * \brief sort submeshes according to stored mesh order
2041 * \param theListToSort in out list to be sorted
2042 * \return FALSE if nothing sorted
2044 //=============================================================================
2046 list<SMESH_subMesh*> SMESH_Mesh::getAncestorsSubMeshes
2047 (const TopoDS_Shape& theSubShape) const
2049 list<SMESH_subMesh*> listOfSubMesh;
2050 TopTools_ListIteratorOfListOfShape it( GetAncestors( theSubShape ));
2051 for (; it.More(); it.Next() )
2052 if ( SMESH_subMesh* sm = GetSubMeshContaining( it.Value() ))
2053 listOfSubMesh.push_back(sm);
2055 // sort submeshes according to stored mesh order
2056 SortByMeshOrder( listOfSubMesh );
2058 return listOfSubMesh;