1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // File : SMESH_Mesh.cxx
24 // Author : Paul RASCLE, EDF
27 #include "SMESH_Mesh.hxx"
28 #include "SMESH_MesherHelper.hxx"
29 #include "SMESH_subMesh.hxx"
30 #include "SMESH_Gen.hxx"
31 #include "SMESH_Hypothesis.hxx"
32 #include "SMESH_Group.hxx"
33 #include "SMESH_HypoFilter.hxx"
34 #include "SMESHDS_Group.hxx"
35 #include "SMESHDS_Script.hxx"
36 #include "SMESHDS_GroupOnGeom.hxx"
37 #include "SMESHDS_Document.hxx"
38 #include "SMDS_MeshVolume.hxx"
39 #include "SMDS_SetIterator.hxx"
41 #include "utilities.h"
43 #include "DriverDAT_W_SMDS_Mesh.h"
44 #include "DriverGMF_Read.hxx"
45 #include "DriverGMF_Write.hxx"
46 #include "DriverMED_R_SMESHDS_Mesh.h"
47 #include "DriverMED_W_SMESHDS_Mesh.h"
48 #include "DriverSTL_R_SMDS_Mesh.h"
49 #include "DriverSTL_W_SMDS_Mesh.h"
50 #include "DriverUNV_R_SMDS_Mesh.h"
51 #include "DriverUNV_W_SMDS_Mesh.h"
53 #include "DriverCGNS_Read.hxx"
54 #include "DriverCGNS_Write.hxx"
57 #undef _Precision_HeaderFile
58 #include <BRepBndLib.hxx>
59 #include <BRepPrimAPI_MakeBox.hxx>
60 #include <Bnd_Box.hxx>
62 #include <TopExp_Explorer.hxx>
63 #include <TopTools_ListIteratorOfListOfShape.hxx>
64 #include <TopTools_ListOfShape.hxx>
65 #include <TopTools_MapOfShape.hxx>
66 #include <TopoDS_Iterator.hxx>
68 #include "Utils_ExceptHandlers.hxx"
70 #include <boost/thread/thread.hpp>
71 #include <boost/bind.hpp>
75 // maximum stored group name length in MED file
76 #define MAX_MED_GROUP_NAME_LENGTH 80
79 static int MYDEBUG = 0;
81 static int MYDEBUG = 0;
84 #define cSMESH_Hyp(h) static_cast<const SMESH_Hypothesis*>(h)
86 typedef SMESH_HypoFilter THypType;
88 //=============================================================================
92 //=============================================================================
94 SMESH_Mesh::SMESH_Mesh(int theLocalId,
97 bool theIsEmbeddedMode,
98 SMESHDS_Document* theDocument):
99 _groupId( 0 ), _nbSubShapes( 0 )
101 MESSAGE("SMESH_Mesh::SMESH_Mesh(int localId)");
103 _studyId = theStudyId;
105 _myDocument = theDocument;
106 _myMeshDS = theDocument->NewMesh(theIsEmbeddedMode,theLocalId);
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():
126 _isShapeToMesh( false ),
130 _isAutoColor( false ),
131 _isModified( false ),
132 _shapeDiagonal( 0.0 ),
139 void deleteMeshDS(SMESHDS_Mesh* meshDS)
141 //cout << "deleteMeshDS( " << meshDS << endl;
146 //=============================================================================
150 //=============================================================================
152 SMESH_Mesh::~SMESH_Mesh()
154 MESSAGE("SMESH_Mesh::~SMESH_Mesh");
156 // issue 0020340: EDF 1022 SMESH : Crash with FindNodeClosestTo in a second new study
157 // Notify event listeners at least that something happens
158 if ( SMESH_subMesh * sm = GetSubMeshContaining(1))
159 sm->ComputeStateEngine( SMESH_subMesh::MESH_ENTITY_REMOVED );
162 map < int, SMESH_Group * >::iterator itg;
163 for (itg = _mapGroup.begin(); itg != _mapGroup.end(); itg++) {
164 SMESH_Group *aGroup = (*itg).second;
170 map <int, SMESH_subMesh*>::iterator sm = _mapSubMesh.begin();
171 for ( ; sm != _mapSubMesh.end(); ++sm )
178 if ( _callUp) delete _callUp;
181 // remove self from studyContext
184 StudyContextStruct * studyContext = _gen->GetStudyContext( _studyId );
185 studyContext->mapMesh.erase( _id );
188 _myDocument->RemoveMesh( _id );
192 // delete _myMeshDS, in a thread in order not to block closing a study with large meshes
193 boost::thread aThread(boost::bind( & deleteMeshDS, _myMeshDS ));
196 //================================================================================
198 * \brief Return true if a mesh with given id exists
200 //================================================================================
202 bool SMESH_Mesh::MeshExists( int meshId ) const
204 return _myDocument ? _myDocument->GetMesh( meshId ) : false;
207 //=============================================================================
209 * \brief Set geometry to be meshed
211 //=============================================================================
213 void SMESH_Mesh::ShapeToMesh(const TopoDS_Shape & aShape)
215 if(MYDEBUG) MESSAGE("SMESH_Mesh::ShapeToMesh");
217 if ( !aShape.IsNull() && _isShapeToMesh ) {
218 if ( aShape.ShapeType() != TopAbs_COMPOUND && // group contents is allowed to change
219 _myMeshDS->ShapeToMesh().ShapeType() != TopAbs_COMPOUND )
220 throw SALOME_Exception(LOCALIZED ("a shape to mesh has already been defined"));
222 // clear current data
223 if ( !_myMeshDS->ShapeToMesh().IsNull() )
225 // removal of a shape to mesh, delete objects referring to sub-shapes:
227 map <int, SMESH_subMesh *>::iterator i_sm = _mapSubMesh.begin();
228 for ( ; i_sm != _mapSubMesh.end(); ++i_sm )
231 // - groups on geometry
232 map <int, SMESH_Group *>::iterator i_gr = _mapGroup.begin();
233 while ( i_gr != _mapGroup.end() ) {
234 if ( dynamic_cast<SMESHDS_GroupOnGeom*>( i_gr->second->GetGroupDS() )) {
235 _myMeshDS->RemoveGroup( i_gr->second->GetGroupDS() );
237 _mapGroup.erase( i_gr++ );
242 _mapAncestors.Clear();
245 TopoDS_Shape aNullShape;
246 _myMeshDS->ShapeToMesh( aNullShape );
248 _shapeDiagonal = 0.0;
251 // set a new geometry
252 if ( !aShape.IsNull() )
254 _myMeshDS->ShapeToMesh(aShape);
255 _isShapeToMesh = true;
256 _nbSubShapes = _myMeshDS->MaxShapeIndex();
258 // fill map of ancestors
259 fillAncestorsMap(aShape);
263 _isShapeToMesh = false;
264 _shapeDiagonal = 0.0;
265 _myMeshDS->ShapeToMesh( PseudoShape() );
270 //=======================================================================
272 * \brief Return geometry to be meshed. (It may be a PseudoShape()!)
274 //=======================================================================
276 TopoDS_Shape SMESH_Mesh::GetShapeToMesh() const
278 return _myMeshDS->ShapeToMesh();
281 //=======================================================================
283 * \brief Return a solid which is returned by GetShapeToMesh() if
284 * a real geometry to be meshed was not set
286 //=======================================================================
288 const TopoDS_Solid& SMESH_Mesh::PseudoShape()
290 static TopoDS_Solid aSolid;
291 if ( aSolid.IsNull() )
293 aSolid = BRepPrimAPI_MakeBox(1,1,1);
298 //=======================================================================
300 * \brief Return diagonal size of bounding box of a shape
302 //=======================================================================
304 double SMESH_Mesh::GetShapeDiagonalSize(const TopoDS_Shape & aShape)
306 if ( !aShape.IsNull() ) {
308 BRepBndLib::Add(aShape, Box);
309 return sqrt( Box.SquareExtent() );
314 //=======================================================================
316 * \brief Return diagonal size of bounding box of shape to mesh
318 //=======================================================================
320 double SMESH_Mesh::GetShapeDiagonalSize() const
322 if ( _shapeDiagonal == 0. && _isShapeToMesh )
323 const_cast<SMESH_Mesh*>(this)->_shapeDiagonal = GetShapeDiagonalSize( GetShapeToMesh() );
325 return _shapeDiagonal;
328 //================================================================================
330 * \brief Load mesh from study file
332 //================================================================================
334 void SMESH_Mesh::Load()
340 //=======================================================================
342 * \brief Remove all nodes and elements
344 //=======================================================================
346 void SMESH_Mesh::Clear()
348 if ( HasShapeToMesh() ) // remove all nodes and elements
351 _myMeshDS->ClearMesh();
353 // update compute state of submeshes
354 if ( SMESH_subMesh *sm = GetSubMeshContaining( GetShapeToMesh() ) )
356 sm->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
357 sm->ComputeSubMeshStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
358 sm->ComputeStateEngine( SMESH_subMesh::CLEAN ); // for event listeners (issue 0020918)
359 sm->ComputeSubMeshStateEngine( SMESH_subMesh::CLEAN );
362 else // remove only nodes/elements computed by algorithms
364 if ( SMESH_subMesh *sm = GetSubMeshContaining( GetShapeToMesh() ) )
366 SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(/*includeSelf=*/true,
367 /*complexShapeFirst=*/true);
368 while ( smIt->more() )
371 sm->ComputeStateEngine( SMESH_subMesh::CLEAN );
378 //=======================================================================
380 * \brief Remove all nodes and elements of indicated shape
382 //=======================================================================
384 void SMESH_Mesh::ClearSubMesh(const int theShapeId)
386 // clear sub-meshes; get ready to re-compute as a side-effect
387 if ( SMESH_subMesh *sm = GetSubMeshContaining( theShapeId ) )
389 SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(/*includeSelf=*/true,
390 /*complexShapeFirst=*/false);
391 while ( smIt->more() )
394 TopAbs_ShapeEnum shapeType = sm->GetSubShape().ShapeType();
395 if ( shapeType == TopAbs_VERTEX || shapeType < TopAbs_SOLID )
396 // all other shapes depends on vertices so they are already cleaned
397 sm->ComputeStateEngine( SMESH_subMesh::CLEAN );
398 // to recompute even if failed
399 sm->ComputeStateEngine( SMESH_subMesh::CHECK_COMPUTE_STATE );
404 //=======================================================================
405 //function : UNVToMesh
407 //=======================================================================
409 int SMESH_Mesh::UNVToMesh(const char* theFileName)
411 if(MYDEBUG) MESSAGE("UNVToMesh - theFileName = "<<theFileName);
413 throw SALOME_Exception(LOCALIZED("a shape to mesh has already been defined"));
414 _isShapeToMesh = false;
415 DriverUNV_R_SMDS_Mesh myReader;
416 myReader.SetMesh(_myMeshDS);
417 myReader.SetFile(theFileName);
418 myReader.SetMeshId(-1);
421 MESSAGE("UNVToMesh - _myMeshDS->NbNodes() = "<<_myMeshDS->NbNodes());
422 MESSAGE("UNVToMesh - _myMeshDS->NbEdges() = "<<_myMeshDS->NbEdges());
423 MESSAGE("UNVToMesh - _myMeshDS->NbFaces() = "<<_myMeshDS->NbFaces());
424 MESSAGE("UNVToMesh - _myMeshDS->NbVolumes() = "<<_myMeshDS->NbVolumes());
426 SMDS_MeshGroup* aGroup = (SMDS_MeshGroup*) myReader.GetGroup();
428 TGroupNamesMap aGroupNames = myReader.GetGroupNamesMap();
429 //const TGroupIdMap& aGroupId = myReader.GetGroupIdMap();
430 aGroup->InitSubGroupsIterator();
431 while (aGroup->MoreSubGroups()) {
432 SMDS_MeshGroup* aSubGroup = (SMDS_MeshGroup*) aGroup->NextSubGroup();
433 string aName = aGroupNames[aSubGroup];
436 SMESH_Group* aSMESHGroup = AddGroup( aSubGroup->GetType(), aName.c_str(), aId );
438 if(MYDEBUG) MESSAGE("UNVToMesh - group added: "<<aName);
439 SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>( aSMESHGroup->GetGroupDS() );
441 aGroupDS->SetStoreName(aName.c_str());
442 aSubGroup->InitIterator();
443 const SMDS_MeshElement* aElement = 0;
444 while (aSubGroup->More()) {
445 aElement = aSubGroup->Next();
447 aGroupDS->SMDSGroup().Add(aElement);
451 aGroupDS->SetType(aElement->GetType());
459 //=======================================================================
460 //function : MEDToMesh
462 //=======================================================================
464 int SMESH_Mesh::MEDToMesh(const char* theFileName, const char* theMeshName)
466 if(MYDEBUG) MESSAGE("MEDToMesh - theFileName = "<<theFileName<<", mesh name = "<<theMeshName);
468 throw SALOME_Exception(LOCALIZED("a shape to mesh has already been defined"));
469 _isShapeToMesh = false;
470 DriverMED_R_SMESHDS_Mesh myReader;
471 myReader.SetMesh(_myMeshDS);
472 myReader.SetMeshId(-1);
473 myReader.SetFile(theFileName);
474 myReader.SetMeshName(theMeshName);
475 Driver_Mesh::Status status = myReader.Perform();
477 MESSAGE("MEDToMesh - _myMeshDS->NbNodes() = "<<_myMeshDS->NbNodes());
478 MESSAGE("MEDToMesh - _myMeshDS->NbEdges() = "<<_myMeshDS->NbEdges());
479 MESSAGE("MEDToMesh - _myMeshDS->NbFaces() = "<<_myMeshDS->NbFaces());
480 MESSAGE("MEDToMesh - _myMeshDS->NbVolumes() = "<<_myMeshDS->NbVolumes());
483 // Reading groups (sub-meshes are out of scope of MED import functionality)
484 list<TNameAndType> aGroupNames = myReader.GetGroupNamesAndTypes();
485 if(MYDEBUG) MESSAGE("MEDToMesh - Nb groups = "<<aGroupNames.size());
487 list<TNameAndType>::iterator name_type = aGroupNames.begin();
488 for ( ; name_type != aGroupNames.end(); name_type++ ) {
489 SMESH_Group* aGroup = AddGroup( name_type->second, name_type->first.c_str(), anId );
491 if(MYDEBUG) MESSAGE("MEDToMesh - group added: "<<name_type->first.c_str());
492 SMESHDS_Group* aGroupDS = dynamic_cast<SMESHDS_Group*>( aGroup->GetGroupDS() );
494 aGroupDS->SetStoreName( name_type->first.c_str() );
495 myReader.GetGroup( aGroupDS );
502 //=======================================================================
503 //function : STLToMesh
505 //=======================================================================
507 int SMESH_Mesh::STLToMesh(const char* theFileName)
509 if(MYDEBUG) MESSAGE("STLToMesh - theFileName = "<<theFileName);
511 throw SALOME_Exception(LOCALIZED("a shape to mesh has already been defined"));
512 _isShapeToMesh = false;
513 DriverSTL_R_SMDS_Mesh myReader;
514 myReader.SetMesh(_myMeshDS);
515 myReader.SetFile(theFileName);
516 myReader.SetMeshId(-1);
519 MESSAGE("STLToMesh - _myMeshDS->NbNodes() = "<<_myMeshDS->NbNodes());
520 MESSAGE("STLToMesh - _myMeshDS->NbEdges() = "<<_myMeshDS->NbEdges());
521 MESSAGE("STLToMesh - _myMeshDS->NbFaces() = "<<_myMeshDS->NbFaces());
522 MESSAGE("STLToMesh - _myMeshDS->NbVolumes() = "<<_myMeshDS->NbVolumes());
527 //================================================================================
529 * \brief Reads the given mesh from the CGNS file
530 * \param theFileName - name of the file
531 * \retval int - Driver_Mesh::Status
533 //================================================================================
535 int SMESH_Mesh::CGNSToMesh(const char* theFileName,
536 const int theMeshIndex,
537 std::string& theMeshName)
539 int res = Driver_Mesh::DRS_FAIL;
542 DriverCGNS_Read myReader;
543 myReader.SetMesh(_myMeshDS);
544 myReader.SetFile(theFileName);
545 myReader.SetMeshId(theMeshIndex);
546 res = myReader.Perform();
547 theMeshName = myReader.GetMeshName();
556 //================================================================================
558 * \brief Fill its data by reading a GMF file
560 //================================================================================
562 SMESH_ComputeErrorPtr SMESH_Mesh::GMFToMesh(const char* theFileName,
563 bool theMakeRequiredGroups)
565 DriverGMF_Read myReader;
566 myReader.SetMesh(_myMeshDS);
567 myReader.SetFile(theFileName);
568 myReader.SetMakeRequiredGroups( theMakeRequiredGroups );
570 //theMeshName = myReader.GetMeshName();
575 return myReader.GetError();
578 //=============================================================================
582 //=============================================================================
584 SMESH_Hypothesis::Hypothesis_Status
585 SMESH_Mesh::AddHypothesis(const TopoDS_Shape & aSubShape,
586 int anHypId ) throw(SALOME_Exception)
588 Unexpect aCatch(SalomeException);
589 if(MYDEBUG) MESSAGE("SMESH_Mesh::AddHypothesis");
591 SMESH_subMesh *subMesh = GetSubMesh(aSubShape);
592 if ( !subMesh || !subMesh->GetId())
593 return SMESH_Hypothesis::HYP_BAD_SUBSHAPE;
595 StudyContextStruct *sc = _gen->GetStudyContext(_studyId);
596 if (sc->mapHypothesis.find(anHypId) == sc->mapHypothesis.end())
598 if(MYDEBUG) MESSAGE("Hypothesis ID does not give an hypothesis");
603 throw SALOME_Exception(LOCALIZED("hypothesis does not exist"));
606 SMESH_Hypothesis *anHyp = sc->mapHypothesis[anHypId];
607 MESSAGE( "SMESH_Mesh::AddHypothesis " << anHyp->GetName() );
609 bool isGlobalHyp = IsMainShape( aSubShape );
611 // NotConformAllowed can be only global
614 // NOTE: this is not a correct way to check a name of hypothesis,
615 // there should be an attribute of hypothesis saying that it can/can't
617 string hypName = anHyp->GetName();
618 if ( hypName == "NotConformAllowed" )
620 if(MYDEBUG) MESSAGE( "Hypotesis <NotConformAllowed> can be only global" );
621 return SMESH_Hypothesis::HYP_INCOMPATIBLE;
627 bool isAlgo = ( !anHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO );
628 int event = isAlgo ? SMESH_subMesh::ADD_ALGO : SMESH_subMesh::ADD_HYP;
630 SMESH_Hypothesis::Hypothesis_Status ret = subMesh->AlgoStateEngine(event, anHyp);
633 if (!SMESH_Hypothesis::IsStatusFatal(ret) &&
634 anHyp->GetDim() <= SMESH_Gen::GetShapeDim(aSubShape)) // is added on father
636 event = isAlgo ? SMESH_subMesh::ADD_FATHER_ALGO : SMESH_subMesh::ADD_FATHER_HYP;
638 SMESH_Hypothesis::Hypothesis_Status ret2 =
639 subMesh->SubMeshesAlgoStateEngine(event, anHyp);
643 // check concurent hypotheses on ancestors
644 if (ret < SMESH_Hypothesis::HYP_CONCURENT && !isGlobalHyp )
646 SMESH_subMeshIteratorPtr smIt = subMesh->getDependsOnIterator(false,false);
647 while ( smIt->more() ) {
648 SMESH_subMesh* sm = smIt->next();
649 if ( sm->IsApplicableHypotesis( anHyp )) {
650 ret2 = sm->CheckConcurentHypothesis( anHyp->GetType() );
659 HasModificationsToDiscard(); // to reset _isModified flag if a mesh becomes empty
661 if(MYDEBUG) subMesh->DumpAlgoState(true);
662 if(MYDEBUG) SCRUTE(ret);
666 //=============================================================================
670 //=============================================================================
672 SMESH_Hypothesis::Hypothesis_Status
673 SMESH_Mesh::RemoveHypothesis(const TopoDS_Shape & aSubShape,
674 int anHypId)throw(SALOME_Exception)
676 Unexpect aCatch(SalomeException);
677 if(MYDEBUG) MESSAGE("SMESH_Mesh::RemoveHypothesis");
679 StudyContextStruct *sc = _gen->GetStudyContext(_studyId);
680 if (sc->mapHypothesis.find(anHypId) == sc->mapHypothesis.end())
681 throw SALOME_Exception(LOCALIZED("hypothesis does not exist"));
683 SMESH_Hypothesis *anHyp = sc->mapHypothesis[anHypId];
685 int hypType = anHyp->GetType();
691 bool isAlgo = ( !anHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO );
692 int event = isAlgo ? SMESH_subMesh::REMOVE_ALGO : SMESH_subMesh::REMOVE_HYP;
694 SMESH_subMesh *subMesh = GetSubMesh(aSubShape);
696 SMESH_Hypothesis::Hypothesis_Status ret = subMesh->AlgoStateEngine(event, anHyp);
698 // there may appear concurrent hyps that were covered by the removed hyp
699 if (ret < SMESH_Hypothesis::HYP_CONCURENT &&
700 subMesh->IsApplicableHypotesis( anHyp ) &&
701 subMesh->CheckConcurentHypothesis( anHyp->GetType() ) != SMESH_Hypothesis::HYP_OK)
702 ret = SMESH_Hypothesis::HYP_CONCURENT;
705 if (!SMESH_Hypothesis::IsStatusFatal(ret) &&
706 anHyp->GetDim() <= SMESH_Gen::GetShapeDim(aSubShape)) // is removed from father
708 event = isAlgo ? SMESH_subMesh::REMOVE_FATHER_ALGO : SMESH_subMesh::REMOVE_FATHER_HYP;
710 SMESH_Hypothesis::Hypothesis_Status ret2 =
711 subMesh->SubMeshesAlgoStateEngine(event, anHyp);
712 if (ret2 > ret) // more severe
715 // check concurent hypotheses on ancestors
716 if (ret < SMESH_Hypothesis::HYP_CONCURENT && !IsMainShape( aSubShape ) )
718 SMESH_subMeshIteratorPtr smIt = subMesh->getDependsOnIterator(false,false);
719 while ( smIt->more() ) {
720 SMESH_subMesh* sm = smIt->next();
721 if ( sm->IsApplicableHypotesis( anHyp )) {
722 ret2 = sm->CheckConcurentHypothesis( anHyp->GetType() );
732 HasModificationsToDiscard(); // to reset _isModified flag if mesh become empty
734 if(MYDEBUG) subMesh->DumpAlgoState(true);
735 if(MYDEBUG) SCRUTE(ret);
739 //=============================================================================
743 //=============================================================================
745 const list<const SMESHDS_Hypothesis*>&
746 SMESH_Mesh::GetHypothesisList(const TopoDS_Shape & aSubShape) const
747 throw(SALOME_Exception)
749 Unexpect aCatch(SalomeException);
750 return _myMeshDS->GetHypothesis(aSubShape);
753 //=======================================================================
755 * \brief Return the hypothesis assigned to the shape
756 * \param aSubShape - the shape to check
757 * \param aFilter - the hypothesis filter
758 * \param andAncestors - flag to check hypos assigned to ancestors of the shape
759 * \param assignedTo - to return the shape the found hypo is assigned to
760 * \retval SMESH_Hypothesis* - the first hypo passed through aFilter
762 //=======================================================================
764 const SMESH_Hypothesis * SMESH_Mesh::GetHypothesis(const TopoDS_Shape & aSubShape,
765 const SMESH_HypoFilter& aFilter,
766 const bool andAncestors,
767 TopoDS_Shape* assignedTo) const
770 const list<const SMESHDS_Hypothesis*>& hypList = _myMeshDS->GetHypothesis(aSubShape);
771 list<const SMESHDS_Hypothesis*>::const_iterator hyp = hypList.begin();
772 for ( ; hyp != hypList.end(); hyp++ ) {
773 const SMESH_Hypothesis * h = cSMESH_Hyp( *hyp );
774 if ( aFilter.IsOk( h, aSubShape)) {
775 if ( assignedTo ) *assignedTo = aSubShape;
782 // user sorted submeshes of ancestors, according to stored submesh priority
783 const list<SMESH_subMesh*> smList = getAncestorsSubMeshes( aSubShape );
784 list<SMESH_subMesh*>::const_iterator smIt = smList.begin();
785 for ( ; smIt != smList.end(); smIt++ )
787 const TopoDS_Shape& curSh = (*smIt)->GetSubShape();
788 const list<const SMESHDS_Hypothesis*>& hypList = _myMeshDS->GetHypothesis(curSh);
789 list<const SMESHDS_Hypothesis*>::const_iterator hyp = hypList.begin();
790 for ( ; hyp != hypList.end(); hyp++ ) {
791 const SMESH_Hypothesis * h = cSMESH_Hyp( *hyp );
792 if (aFilter.IsOk( h, curSh )) {
793 if ( assignedTo ) *assignedTo = curSh;
802 //================================================================================
804 * \brief Return hypothesis assigned to the shape
805 * \param aSubShape - the shape to check
806 * \param aFilter - the hypothesis filter
807 * \param aHypList - the list of the found hypotheses
808 * \param andAncestors - flag to check hypos assigned to ancestors of the shape
809 * \retval int - number of unique hypos in aHypList
811 //================================================================================
813 int SMESH_Mesh::GetHypotheses(const TopoDS_Shape & aSubShape,
814 const SMESH_HypoFilter& aFilter,
815 list <const SMESHDS_Hypothesis * >& aHypList,
816 const bool andAncestors) const
818 set<string> hypTypes; // to exclude same type hypos from the result list
821 // only one main hypothesis is allowed
822 bool mainHypFound = false;
825 list<const SMESHDS_Hypothesis*>::const_iterator hyp;
826 for ( hyp = aHypList.begin(); hyp != aHypList.end(); hyp++ ) {
827 if ( hypTypes.insert( (*hyp)->GetName() ).second )
829 if ( !cSMESH_Hyp(*hyp)->IsAuxiliary() )
833 // get hypos from aSubShape
835 const list<const SMESHDS_Hypothesis*>& hypList = _myMeshDS->GetHypothesis(aSubShape);
836 for ( hyp = hypList.begin(); hyp != hypList.end(); hyp++ )
837 if ( aFilter.IsOk (cSMESH_Hyp( *hyp ), aSubShape) &&
838 ( cSMESH_Hyp(*hyp)->IsAuxiliary() || !mainHypFound ) &&
839 hypTypes.insert( (*hyp)->GetName() ).second )
841 aHypList.push_back( *hyp );
843 if ( !cSMESH_Hyp(*hyp)->IsAuxiliary() )
848 // get hypos from ancestors of aSubShape
851 TopTools_MapOfShape map;
853 // user sorted submeshes of ancestors, according to stored submesh priority
854 const list<SMESH_subMesh*> smList = getAncestorsSubMeshes( aSubShape );
855 list<SMESH_subMesh*>::const_iterator smIt = smList.begin();
856 for ( ; smIt != smList.end(); smIt++ )
858 const TopoDS_Shape& curSh = (*smIt)->GetSubShape();
859 if ( !map.Add( curSh ))
861 const list<const SMESHDS_Hypothesis*>& hypList = _myMeshDS->GetHypothesis(curSh);
862 for ( hyp = hypList.begin(); hyp != hypList.end(); hyp++ )
863 if (aFilter.IsOk( cSMESH_Hyp( *hyp ), curSh ) &&
864 ( cSMESH_Hyp(*hyp)->IsAuxiliary() || !mainHypFound ) &&
865 hypTypes.insert( (*hyp)->GetName() ).second )
867 aHypList.push_back( *hyp );
869 if ( !cSMESH_Hyp(*hyp)->IsAuxiliary() )
877 //=============================================================================
881 //=============================================================================
883 const list<SMESHDS_Command*> & SMESH_Mesh::GetLog() throw(SALOME_Exception)
885 Unexpect aCatch(SalomeException);
886 if(MYDEBUG) MESSAGE("SMESH_Mesh::GetLog");
887 return _myMeshDS->GetScript()->GetCommands();
890 //=============================================================================
894 //=============================================================================
895 void SMESH_Mesh::ClearLog() throw(SALOME_Exception)
897 Unexpect aCatch(SalomeException);
898 if(MYDEBUG) MESSAGE("SMESH_Mesh::ClearLog");
899 _myMeshDS->GetScript()->Clear();
902 //=============================================================================
904 * Get or Create the SMESH_subMesh object implementation
906 //=============================================================================
908 SMESH_subMesh *SMESH_Mesh::GetSubMesh(const TopoDS_Shape & aSubShape)
909 throw(SALOME_Exception)
911 Unexpect aCatch(SalomeException);
912 SMESH_subMesh *aSubMesh;
913 int index = _myMeshDS->ShapeToIndex(aSubShape);
915 // for submeshes on GEOM Group
916 if (( !index || index > _nbSubShapes ) && aSubShape.ShapeType() == TopAbs_COMPOUND ) {
917 TopoDS_Iterator it( aSubShape );
920 index = _myMeshDS->AddCompoundSubmesh( aSubShape, it.Value().ShapeType() );
921 // fill map of Ancestors
922 while ( _nbSubShapes < index )
923 fillAncestorsMap( _myMeshDS->IndexToShape( ++_nbSubShapes ));
927 // return NULL; // neither sub-shape nor a group
929 map <int, SMESH_subMesh *>::iterator i_sm = _mapSubMesh.find(index);
930 if ( i_sm != _mapSubMesh.end())
932 aSubMesh = i_sm->second;
936 aSubMesh = new SMESH_subMesh(index, this, _myMeshDS, aSubShape);
937 _mapSubMesh[index] = aSubMesh;
942 //=============================================================================
944 * Get the SMESH_subMesh object implementation. Dont create it, return null
945 * if it does not exist.
947 //=============================================================================
949 SMESH_subMesh *SMESH_Mesh::GetSubMeshContaining(const TopoDS_Shape & aSubShape) const
950 throw(SALOME_Exception)
952 Unexpect aCatch(SalomeException);
953 SMESH_subMesh *aSubMesh = NULL;
955 int index = _myMeshDS->ShapeToIndex(aSubShape);
957 map <int, SMESH_subMesh *>::const_iterator i_sm = _mapSubMesh.find(index);
958 if ( i_sm != _mapSubMesh.end())
959 aSubMesh = i_sm->second;
963 //=============================================================================
965 * Get the SMESH_subMesh object implementation. Dont create it, return null
966 * if it does not exist.
968 //=============================================================================
970 SMESH_subMesh *SMESH_Mesh::GetSubMeshContaining(const int aShapeID) const
971 throw(SALOME_Exception)
973 Unexpect aCatch(SalomeException);
975 map <int, SMESH_subMesh *>::const_iterator i_sm = _mapSubMesh.find(aShapeID);
976 if (i_sm == _mapSubMesh.end())
980 //================================================================================
982 * \brief Return submeshes of groups containing the given sub-shape
984 //================================================================================
987 SMESH_Mesh::GetGroupSubMeshesContaining(const TopoDS_Shape & aSubShape) const
988 throw(SALOME_Exception)
990 Unexpect aCatch(SalomeException);
991 list<SMESH_subMesh*> found;
993 SMESH_subMesh * subMesh = GetSubMeshContaining(aSubShape);
997 // submeshes of groups have max IDs, so search from the map end
998 map<int, SMESH_subMesh *>::const_reverse_iterator i_sm;
999 for ( i_sm = _mapSubMesh.rbegin(); i_sm != _mapSubMesh.rend(); ++i_sm) {
1000 SMESHDS_SubMesh * ds = i_sm->second->GetSubMeshDS();
1001 if ( ds && ds->IsComplexSubmesh() ) {
1002 if ( SMESH_MesherHelper::IsSubShape( aSubShape, i_sm->second->GetSubShape() ))
1004 found.push_back( i_sm->second );
1008 break; // the rest sub-meshes are not those of groups
1012 if ( found.empty() ) // maybe the main shape is a COMPOUND (issue 0021530)
1014 if ( SMESH_subMesh * mainSM = GetSubMeshContaining(1))
1015 if ( mainSM->GetSubShape().ShapeType() == TopAbs_COMPOUND )
1017 TopoDS_Iterator it( mainSM->GetSubShape() );
1018 if ( it.Value().ShapeType() == aSubShape.ShapeType() &&
1019 SMESH_MesherHelper::IsSubShape( aSubShape, mainSM->GetSubShape() ))
1020 found.push_back( mainSM );
1025 //=======================================================================
1026 //function : IsUsedHypothesis
1027 //purpose : Return True if anHyp is used to mesh aSubShape
1028 //=======================================================================
1030 bool SMESH_Mesh::IsUsedHypothesis(SMESHDS_Hypothesis * anHyp,
1031 const SMESH_subMesh* aSubMesh)
1033 SMESH_Hypothesis* hyp = static_cast<SMESH_Hypothesis*>(anHyp);
1035 // check if anHyp can be used to mesh aSubMesh
1036 if ( !aSubMesh || !aSubMesh->IsApplicableHypotesis( hyp ))
1039 const TopoDS_Shape & aSubShape = const_cast<SMESH_subMesh*>( aSubMesh )->GetSubShape();
1041 SMESH_Algo *algo = _gen->GetAlgo(*this, aSubShape );
1044 if (anHyp->GetType() > SMESHDS_Hypothesis::PARAM_ALGO)
1045 return ( anHyp == algo );
1047 // algorithm parameter
1050 // look trough hypotheses used by algo
1051 SMESH_HypoFilter hypoKind;
1052 if ( algo->InitCompatibleHypoFilter( hypoKind, !hyp->IsAuxiliary() )) {
1053 list <const SMESHDS_Hypothesis * > usedHyps;
1054 if ( GetHypotheses( aSubShape, hypoKind, usedHyps, true ))
1055 return ( find( usedHyps.begin(), usedHyps.end(), anHyp ) != usedHyps.end() );
1059 // look through all assigned hypotheses
1060 //SMESH_HypoFilter filter( SMESH_HypoFilter::Is( hyp ));
1061 return false; //GetHypothesis( aSubShape, filter, true );
1064 //=============================================================================
1068 //=============================================================================
1070 const list < SMESH_subMesh * >&
1071 SMESH_Mesh::GetSubMeshUsingHypothesis(SMESHDS_Hypothesis * anHyp)
1072 throw(SALOME_Exception)
1074 Unexpect aCatch(SalomeException);
1075 if(MYDEBUG) MESSAGE("SMESH_Mesh::GetSubMeshUsingHypothesis");
1076 map < int, SMESH_subMesh * >::iterator itsm;
1077 _subMeshesUsingHypothesisList.clear();
1078 for (itsm = _mapSubMesh.begin(); itsm != _mapSubMesh.end(); itsm++)
1080 SMESH_subMesh *aSubMesh = (*itsm).second;
1081 if ( IsUsedHypothesis ( anHyp, aSubMesh ))
1082 _subMeshesUsingHypothesisList.push_back(aSubMesh);
1084 return _subMeshesUsingHypothesisList;
1087 //=======================================================================
1088 //function : NotifySubMeshesHypothesisModification
1089 //purpose : Say all submeshes using theChangedHyp that it has been modified
1090 //=======================================================================
1092 void SMESH_Mesh::NotifySubMeshesHypothesisModification(const SMESH_Hypothesis* hyp)
1094 Unexpect aCatch(SalomeException);
1096 if ( !GetMeshDS()->IsUsedHypothesis( hyp ))
1100 _callUp->HypothesisModified();
1102 const SMESH_Algo *foundAlgo = 0;
1103 SMESH_HypoFilter algoKind, compatibleHypoKind;
1104 list <const SMESHDS_Hypothesis * > usedHyps;
1107 map < int, SMESH_subMesh * >::iterator itsm;
1108 for (itsm = _mapSubMesh.begin(); itsm != _mapSubMesh.end(); itsm++)
1110 SMESH_subMesh *aSubMesh = (*itsm).second;
1111 if ( aSubMesh->IsApplicableHypotesis( hyp ))
1113 const TopoDS_Shape & aSubShape = aSubMesh->GetSubShape();
1115 if ( !foundAlgo ) // init filter for algo search
1116 algoKind.Init( THypType::IsAlgo() ).And( THypType::IsApplicableTo( aSubShape ));
1118 const SMESH_Algo *algo = static_cast<const SMESH_Algo*>
1119 ( GetHypothesis( aSubShape, algoKind, true ));
1123 bool sameAlgo = ( algo == foundAlgo );
1124 if ( !sameAlgo && foundAlgo )
1125 sameAlgo = ( strcmp( algo->GetName(), foundAlgo->GetName() ) == 0);
1127 if ( !sameAlgo ) { // init filter for used hypos search
1128 if ( !algo->InitCompatibleHypoFilter( compatibleHypoKind, !hyp->IsAuxiliary() ))
1129 continue; // algo does not use any hypothesis
1133 // check if hyp is used by algo
1135 if ( GetHypotheses( aSubShape, compatibleHypoKind, usedHyps, true ) &&
1136 find( usedHyps.begin(), usedHyps.end(), hyp ) != usedHyps.end() )
1138 aSubMesh->AlgoStateEngine(SMESH_subMesh::MODIF_HYP,
1139 const_cast< SMESH_Hypothesis*>( hyp ));
1144 HasModificationsToDiscard(); // to reset _isModified flag if mesh becomes empty
1145 GetMeshDS()->Modified();
1148 //=============================================================================
1150 * Auto color functionality
1152 //=============================================================================
1153 void SMESH_Mesh::SetAutoColor(bool theAutoColor) throw(SALOME_Exception)
1155 Unexpect aCatch(SalomeException);
1156 _isAutoColor = theAutoColor;
1159 bool SMESH_Mesh::GetAutoColor() throw(SALOME_Exception)
1161 Unexpect aCatch(SalomeException);
1162 return _isAutoColor;
1165 //=======================================================================
1166 //function : SetIsModified
1167 //purpose : Set the flag meaning that the mesh has been edited "manually"
1168 //=======================================================================
1170 void SMESH_Mesh::SetIsModified(bool isModified)
1172 _isModified = isModified;
1175 // check if mesh becomes empty as result of modification
1176 HasModificationsToDiscard();
1179 //=======================================================================
1180 //function : HasModificationsToDiscard
1181 //purpose : Return true if the mesh has been edited since a total re-compute
1182 // and those modifications may prevent successful partial re-compute.
1183 // As a side effect reset _isModified flag if mesh is empty
1185 //=======================================================================
1187 bool SMESH_Mesh::HasModificationsToDiscard() const
1189 if ( ! _isModified )
1192 // return true if the next Compute() will be partial and
1193 // existing but changed elements may prevent successful re-compute
1194 bool hasComputed = false, hasNotComputed = false;
1195 map <int, SMESH_subMesh*>::const_iterator i_sm = _mapSubMesh.begin();
1196 for ( ; i_sm != _mapSubMesh.end() ; ++i_sm )
1197 switch ( i_sm->second->GetSubShape().ShapeType() )
1202 if ( i_sm->second->IsMeshComputed() )
1205 hasNotComputed = true;
1206 if ( hasComputed && hasNotComputed)
1210 if ( NbNodes() < 1 )
1211 const_cast<SMESH_Mesh*>(this)->_isModified = false;
1216 //================================================================================
1218 * \brief Check if any groups of the same type have equal names
1220 //================================================================================
1222 bool SMESH_Mesh::HasDuplicatedGroupNamesMED()
1224 //set<string> aGroupNames; // Corrected for Mantis issue 0020028
1225 map< SMDSAbs_ElementType, set<string> > aGroupNames;
1226 for ( map<int, SMESH_Group*>::iterator it = _mapGroup.begin(); it != _mapGroup.end(); it++ )
1228 SMESH_Group* aGroup = it->second;
1229 SMDSAbs_ElementType aType = aGroup->GetGroupDS()->GetType();
1230 string aGroupName = aGroup->GetName();
1231 aGroupName.resize(MAX_MED_GROUP_NAME_LENGTH);
1232 if (!aGroupNames[aType].insert(aGroupName).second)
1239 //================================================================================
1241 * \brief Export the mesh to a med file
1243 //================================================================================
1245 void SMESH_Mesh::ExportMED(const char * file,
1246 const char* theMeshName,
1249 const SMESHDS_Mesh* meshPart)
1250 throw(SALOME_Exception)
1252 Unexpect aCatch(SalomeException);
1254 DriverMED_W_SMESHDS_Mesh myWriter;
1255 myWriter.SetFile ( file, MED::EVersion(theVersion) );
1256 myWriter.SetMesh ( meshPart ? (SMESHDS_Mesh*) meshPart : _myMeshDS );
1258 myWriter.SetMeshId ( _id );
1260 myWriter.SetMeshId ( -1 );
1261 myWriter.SetMeshName( theMeshName );
1264 if ( theAutoGroups ) {
1265 myWriter.AddGroupOfNodes();
1266 myWriter.AddGroupOfEdges();
1267 myWriter.AddGroupOfFaces();
1268 myWriter.AddGroupOfVolumes();
1271 // Pass groups to writer. Provide unique group names.
1272 //set<string> aGroupNames; // Corrected for Mantis issue 0020028
1275 map< SMDSAbs_ElementType, set<string> > aGroupNames;
1277 int maxNbIter = 10000; // to guarantee cycle finish
1278 for ( map<int, SMESH_Group*>::iterator it = _mapGroup.begin(); it != _mapGroup.end(); it++ ) {
1279 SMESH_Group* aGroup = it->second;
1280 SMESHDS_GroupBase* aGroupDS = aGroup->GetGroupDS();
1282 SMDSAbs_ElementType aType = aGroupDS->GetType();
1283 string aGroupName0 = aGroup->GetName();
1284 aGroupName0.resize(MAX_MED_GROUP_NAME_LENGTH);
1285 string aGroupName = aGroupName0;
1286 for (int i = 1; !aGroupNames[aType].insert(aGroupName).second && i < maxNbIter; i++) {
1287 sprintf(&aString[0], "GR_%d_%s", i, aGroupName0.c_str());
1288 aGroupName = aString;
1289 aGroupName.resize(MAX_MED_GROUP_NAME_LENGTH);
1291 aGroupDS->SetStoreName( aGroupName.c_str() );
1292 myWriter.AddGroup( aGroupDS );
1300 void SMESH_Mesh::ExportSAUV(const char *file,
1301 const char* theMeshName,
1303 throw(SALOME_Exception)
1305 std::string medfilename(file);
1306 medfilename += ".med";
1309 cmd = "%PYTHONBIN% ";
1314 cmd += "from medutilities import my_remove ; my_remove(r'" + medfilename + "')";
1316 system(cmd.c_str());
1317 ExportMED(medfilename.c_str(), theMeshName, theAutoGroups, 1);
1319 cmd = "%PYTHONBIN% ";
1324 cmd += "from medutilities import convert ; convert(r'" + medfilename + "', 'MED', 'GIBI', 1, r'" + file + "')";
1326 system(cmd.c_str());
1328 cmd = "%PYTHONBIN% ";
1333 cmd += "from medutilities import my_remove ; my_remove(r'" + medfilename + "')";
1335 system(cmd.c_str());
1338 //================================================================================
1340 * \brief Export the mesh to a DAT file
1342 //================================================================================
1344 void SMESH_Mesh::ExportDAT(const char * file,
1345 const SMESHDS_Mesh* meshPart) throw(SALOME_Exception)
1347 Unexpect aCatch(SalomeException);
1348 DriverDAT_W_SMDS_Mesh myWriter;
1349 myWriter.SetFile( file );
1350 myWriter.SetMesh( meshPart ? (SMESHDS_Mesh*) meshPart : _myMeshDS );
1351 myWriter.SetMeshId(_id);
1355 //================================================================================
1357 * \brief Export the mesh to an UNV file
1359 //================================================================================
1361 void SMESH_Mesh::ExportUNV(const char * file,
1362 const SMESHDS_Mesh* meshPart) throw(SALOME_Exception)
1364 Unexpect aCatch(SalomeException);
1365 DriverUNV_W_SMDS_Mesh myWriter;
1366 myWriter.SetFile( file );
1367 myWriter.SetMesh( meshPart ? (SMESHDS_Mesh*) meshPart : _myMeshDS );
1368 myWriter.SetMeshId(_id);
1369 // myWriter.SetGroups(_mapGroup);
1373 for ( map<int, SMESH_Group*>::iterator it = _mapGroup.begin(); it != _mapGroup.end(); it++ ) {
1374 SMESH_Group* aGroup = it->second;
1375 SMESHDS_GroupBase* aGroupDS = aGroup->GetGroupDS();
1377 string aGroupName = aGroup->GetName();
1378 aGroupDS->SetStoreName( aGroupName.c_str() );
1379 myWriter.AddGroup( aGroupDS );
1386 //================================================================================
1388 * \brief Export the mesh to an STL file
1390 //================================================================================
1392 void SMESH_Mesh::ExportSTL(const char * file,
1394 const SMESHDS_Mesh* meshPart) throw(SALOME_Exception)
1396 Unexpect aCatch(SalomeException);
1397 DriverSTL_W_SMDS_Mesh myWriter;
1398 myWriter.SetFile( file );
1399 myWriter.SetIsAscii( isascii );
1400 myWriter.SetMesh( meshPart ? (SMESHDS_Mesh*) meshPart : _myMeshDS);
1401 myWriter.SetMeshId(_id);
1405 //================================================================================
1407 * \brief Export the mesh to the CGNS file
1409 //================================================================================
1411 void SMESH_Mesh::ExportCGNS(const char * file,
1412 const SMESHDS_Mesh* meshDS)
1414 int res = Driver_Mesh::DRS_FAIL;
1416 DriverCGNS_Write myWriter;
1417 myWriter.SetFile( file );
1418 myWriter.SetMesh( const_cast<SMESHDS_Mesh*>( meshDS ));
1419 myWriter.SetMeshName( SMESH_Comment("Mesh_") << meshDS->GetPersistentId());
1420 res = myWriter.Perform();
1422 if ( res != Driver_Mesh::DRS_OK )
1423 throw SALOME_Exception("Export failed");
1426 //================================================================================
1428 * \brief Export the mesh to a GMF file
1430 //================================================================================
1432 void SMESH_Mesh::ExportGMF(const char * file,
1433 const SMESHDS_Mesh* meshDS,
1434 bool withRequiredGroups)
1436 DriverGMF_Write myWriter;
1437 myWriter.SetFile( file );
1438 myWriter.SetMesh( const_cast<SMESHDS_Mesh*>( meshDS ));
1439 myWriter.SetExportRequiredGroups( withRequiredGroups );
1444 //================================================================================
1446 * \brief Return number of nodes in the mesh
1448 //================================================================================
1450 int SMESH_Mesh::NbNodes() const throw(SALOME_Exception)
1452 Unexpect aCatch(SalomeException);
1453 return _myMeshDS->NbNodes();
1456 //================================================================================
1458 * \brief Return number of edges of given order in the mesh
1460 //================================================================================
1462 int SMESH_Mesh::Nb0DElements() const throw(SALOME_Exception)
1464 Unexpect aCatch(SalomeException);
1465 return _myMeshDS->GetMeshInfo().Nb0DElements();
1468 //================================================================================
1470 * \brief Return number of edges of given order in the mesh
1472 //================================================================================
1474 int SMESH_Mesh::NbEdges(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1476 Unexpect aCatch(SalomeException);
1477 return _myMeshDS->GetMeshInfo().NbEdges(order);
1480 //================================================================================
1482 * \brief Return number of faces of given order in the mesh
1484 //================================================================================
1486 int SMESH_Mesh::NbFaces(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1488 Unexpect aCatch(SalomeException);
1489 return _myMeshDS->GetMeshInfo().NbFaces(order);
1492 //================================================================================
1494 * \brief Return the number of faces in the mesh
1496 //================================================================================
1498 int SMESH_Mesh::NbTriangles(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1500 Unexpect aCatch(SalomeException);
1501 return _myMeshDS->GetMeshInfo().NbTriangles(order);
1504 //================================================================================
1506 * \brief Return number of biquadratic triangles in the mesh
1508 //================================================================================
1510 int SMESH_Mesh::NbBiQuadTriangles() const throw(SALOME_Exception)
1512 Unexpect aCatch(SalomeException);
1513 return _myMeshDS->GetMeshInfo().NbBiQuadTriangles();
1516 //================================================================================
1518 * \brief Return the number nodes faces in the mesh
1520 //================================================================================
1522 int SMESH_Mesh::NbQuadrangles(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1524 Unexpect aCatch(SalomeException);
1525 return _myMeshDS->GetMeshInfo().NbQuadrangles(order);
1528 //================================================================================
1530 * \brief Return number of biquadratic quadrangles in the mesh
1532 //================================================================================
1534 int SMESH_Mesh::NbBiQuadQuadrangles() const throw(SALOME_Exception)
1536 Unexpect aCatch(SalomeException);
1537 return _myMeshDS->GetMeshInfo().NbBiQuadQuadrangles();
1540 //================================================================================
1542 * \brief Return the number of polygonal faces in the mesh
1544 //================================================================================
1546 int SMESH_Mesh::NbPolygons() const throw(SALOME_Exception)
1548 Unexpect aCatch(SalomeException);
1549 return _myMeshDS->GetMeshInfo().NbPolygons();
1552 //================================================================================
1554 * \brief Return number of volumes of given order in the mesh
1556 //================================================================================
1558 int SMESH_Mesh::NbVolumes(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1560 Unexpect aCatch(SalomeException);
1561 return _myMeshDS->GetMeshInfo().NbVolumes(order);
1564 //================================================================================
1566 * \brief Return number of tetrahedrons of given order in the mesh
1568 //================================================================================
1570 int SMESH_Mesh::NbTetras(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1572 Unexpect aCatch(SalomeException);
1573 return _myMeshDS->GetMeshInfo().NbTetras(order);
1576 //================================================================================
1578 * \brief Return number of hexahedrons of given order in the mesh
1580 //================================================================================
1582 int SMESH_Mesh::NbHexas(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1584 Unexpect aCatch(SalomeException);
1585 return _myMeshDS->GetMeshInfo().NbHexas(order);
1588 //================================================================================
1590 * \brief Return number of triquadratic hexahedrons in the mesh
1592 //================================================================================
1594 int SMESH_Mesh::NbTriQuadraticHexas() const throw(SALOME_Exception)
1596 Unexpect aCatch(SalomeException);
1597 return _myMeshDS->GetMeshInfo().NbTriQuadHexas();
1600 //================================================================================
1602 * \brief Return number of pyramids of given order in the mesh
1604 //================================================================================
1606 int SMESH_Mesh::NbPyramids(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1608 Unexpect aCatch(SalomeException);
1609 return _myMeshDS->GetMeshInfo().NbPyramids(order);
1612 //================================================================================
1614 * \brief Return number of prisms (penthahedrons) of given order in the mesh
1616 //================================================================================
1618 int SMESH_Mesh::NbPrisms(SMDSAbs_ElementOrder order) const throw(SALOME_Exception)
1620 Unexpect aCatch(SalomeException);
1621 return _myMeshDS->GetMeshInfo().NbPrisms(order);
1624 //================================================================================
1626 * \brief Return number of hexagonal prisms in the mesh
1628 //================================================================================
1630 int SMESH_Mesh::NbHexagonalPrisms() const throw(SALOME_Exception)
1632 Unexpect aCatch(SalomeException);
1633 return _myMeshDS->GetMeshInfo().NbHexPrisms();
1636 //================================================================================
1638 * \brief Return number of polyhedrons in the mesh
1640 //================================================================================
1642 int SMESH_Mesh::NbPolyhedrons() const throw(SALOME_Exception)
1644 Unexpect aCatch(SalomeException);
1645 return _myMeshDS->GetMeshInfo().NbPolyhedrons();
1648 //================================================================================
1650 * \brief Return number of ball elements in the mesh
1652 //================================================================================
1654 int SMESH_Mesh::NbBalls() const throw(SALOME_Exception)
1656 Unexpect aCatch(SalomeException);
1657 return _myMeshDS->GetMeshInfo().NbBalls();
1660 //================================================================================
1662 * \brief Return number of submeshes in the mesh
1664 //================================================================================
1666 int SMESH_Mesh::NbSubMesh() const throw(SALOME_Exception)
1668 Unexpect aCatch(SalomeException);
1669 return _myMeshDS->NbSubMesh();
1672 //=======================================================================
1673 //function : IsNotConformAllowed
1674 //purpose : check if a hypothesis alowing notconform mesh is present
1675 //=======================================================================
1677 bool SMESH_Mesh::IsNotConformAllowed() const
1679 if(MYDEBUG) MESSAGE("SMESH_Mesh::IsNotConformAllowed");
1681 static SMESH_HypoFilter filter( SMESH_HypoFilter::HasName( "NotConformAllowed" ));
1682 return GetHypothesis( _myMeshDS->ShapeToMesh(), filter, false );
1685 //=======================================================================
1686 //function : IsMainShape
1688 //=======================================================================
1690 bool SMESH_Mesh::IsMainShape(const TopoDS_Shape& theShape) const
1692 return theShape.IsSame(_myMeshDS->ShapeToMesh() );
1695 //=============================================================================
1699 //=============================================================================
1701 SMESH_Group* SMESH_Mesh::AddGroup (const SMDSAbs_ElementType theType,
1702 const char* theName,
1704 const TopoDS_Shape& theShape,
1705 const SMESH_PredicatePtr& thePredicate)
1707 if (_mapGroup.count(_groupId))
1710 SMESH_Group* aGroup = new SMESH_Group (theId, this, theType, theName, theShape, thePredicate);
1711 GetMeshDS()->AddGroup( aGroup->GetGroupDS() );
1712 _mapGroup[_groupId++] = aGroup;
1716 //================================================================================
1718 * \brief Creates a group based on an existing SMESHDS group. Group ID should be unique
1720 //================================================================================
1722 SMESH_Group* SMESH_Mesh::AddGroup (SMESHDS_GroupBase* groupDS) throw(SALOME_Exception)
1725 throw SALOME_Exception(LOCALIZED ("SMESH_Mesh::AddGroup(): NULL SMESHDS_GroupBase"));
1727 map <int, SMESH_Group*>::iterator i_g = _mapGroup.find( groupDS->GetID() );
1728 if ( i_g != _mapGroup.end() && i_g->second )
1730 if ( i_g->second->GetGroupDS() == groupDS )
1733 throw SALOME_Exception(LOCALIZED ("SMESH_Mesh::AddGroup() wrong ID of SMESHDS_GroupBase"));
1735 SMESH_Group* aGroup = new SMESH_Group (groupDS);
1736 _mapGroup[ groupDS->GetID() ] = aGroup;
1737 GetMeshDS()->AddGroup( aGroup->GetGroupDS() );
1739 _groupId = 1 + _mapGroup.rbegin()->first;
1745 //================================================================================
1747 * \brief Creates SMESH_Groups for not wrapped SMESHDS_Groups
1748 * \retval bool - true if new SMESH_Groups have been created
1751 //================================================================================
1753 bool SMESH_Mesh::SynchronizeGroups()
1755 int nbGroups = _mapGroup.size();
1756 const set<SMESHDS_GroupBase*>& groups = _myMeshDS->GetGroups();
1757 set<SMESHDS_GroupBase*>::const_iterator gIt = groups.begin();
1758 for ( ; gIt != groups.end(); ++gIt )
1760 SMESHDS_GroupBase* groupDS = (SMESHDS_GroupBase*) *gIt;
1761 _groupId = groupDS->GetID();
1762 if ( !_mapGroup.count( _groupId ))
1763 _mapGroup[_groupId] = new SMESH_Group( groupDS );
1765 if ( !_mapGroup.empty() )
1766 _groupId = _mapGroup.rbegin()->first + 1;
1768 return nbGroups < _mapGroup.size();
1771 //================================================================================
1773 * \brief Return iterator on all existing groups
1775 //================================================================================
1777 SMESH_Mesh::GroupIteratorPtr SMESH_Mesh::GetGroups() const
1779 typedef map <int, SMESH_Group *> TMap;
1780 return GroupIteratorPtr( new SMDS_mapIterator<TMap>( _mapGroup ));
1783 //=============================================================================
1785 * \brief Return a group by ID
1787 //=============================================================================
1789 SMESH_Group* SMESH_Mesh::GetGroup (const int theGroupID)
1791 if (_mapGroup.find(theGroupID) == _mapGroup.end())
1793 return _mapGroup[theGroupID];
1797 //=============================================================================
1799 * \brief Return IDs of all groups
1801 //=============================================================================
1803 list<int> SMESH_Mesh::GetGroupIds() const
1806 for ( map<int, SMESH_Group*>::const_iterator it = _mapGroup.begin(); it != _mapGroup.end(); it++ )
1807 anIds.push_back( it->first );
1812 //================================================================================
1814 * \brief Set a caller of methods at level of CORBA API implementation.
1815 * The set upCaller will be deleted by SMESH_Mesh
1817 //================================================================================
1819 void SMESH_Mesh::SetCallUp( TCallUp* upCaller )
1821 if ( _callUp ) delete _callUp;
1825 //=============================================================================
1829 //=============================================================================
1831 bool SMESH_Mesh::RemoveGroup (const int theGroupID)
1833 if (_mapGroup.find(theGroupID) == _mapGroup.end())
1835 GetMeshDS()->RemoveGroup( _mapGroup[theGroupID]->GetGroupDS() );
1836 delete _mapGroup[theGroupID];
1837 _mapGroup.erase (theGroupID);
1839 _callUp->RemoveGroup( theGroupID );
1843 //=======================================================================
1844 //function : GetAncestors
1845 //purpose : return list of ancestors of theSubShape in the order
1846 // that lower dimention shapes come first.
1847 //=======================================================================
1849 const TopTools_ListOfShape& SMESH_Mesh::GetAncestors(const TopoDS_Shape& theS) const
1851 if ( _mapAncestors.Contains( theS ) )
1852 return _mapAncestors.FindFromKey( theS );
1854 static TopTools_ListOfShape emptyList;
1858 //=======================================================================
1860 //purpose : dumps contents of mesh to stream [ debug purposes ]
1861 //=======================================================================
1863 ostream& SMESH_Mesh::Dump(ostream& save)
1866 save << "========================== Dump contents of mesh ==========================" << endl << endl;
1867 save << ++clause << ") Total number of nodes: \t" << NbNodes() << endl;
1868 save << ++clause << ") Total number of edges: \t" << NbEdges() << endl;
1869 save << ++clause << ") Total number of faces: \t" << NbFaces() << endl;
1870 save << ++clause << ") Total number of polygons:\t" << NbPolygons() << endl;
1871 save << ++clause << ") Total number of volumes:\t" << NbVolumes() << endl;
1872 save << ++clause << ") Total number of polyhedrons:\t" << NbPolyhedrons() << endl << endl;
1873 for ( int isQuadratic = 0; isQuadratic < 2; ++isQuadratic )
1875 string orderStr = isQuadratic ? "quadratic" : "linear";
1876 SMDSAbs_ElementOrder order = isQuadratic ? ORDER_QUADRATIC : ORDER_LINEAR;
1878 save << ++clause << ") Total number of " << orderStr << " edges:\t" << NbEdges(order) << endl;
1879 save << ++clause << ") Total number of " << orderStr << " faces:\t" << NbFaces(order) << endl;
1880 if ( NbFaces(order) > 0 ) {
1881 int nb3 = NbTriangles(order);
1882 int nb4 = NbQuadrangles(order);
1883 save << clause << ".1) Number of " << orderStr << " triangles: \t" << nb3 << endl;
1884 save << clause << ".2) Number of " << orderStr << " quadrangles:\t" << nb4 << endl;
1885 if ( nb3 + nb4 != NbFaces(order) ) {
1886 map<int,int> myFaceMap;
1887 SMDS_FaceIteratorPtr itFaces=_myMeshDS->facesIterator();
1888 while( itFaces->more( ) ) {
1889 int nbNodes = itFaces->next()->NbNodes();
1890 if ( myFaceMap.find( nbNodes ) == myFaceMap.end() )
1891 myFaceMap[ nbNodes ] = 0;
1892 myFaceMap[ nbNodes ] = myFaceMap[ nbNodes ] + 1;
1894 save << clause << ".3) Faces in detail: " << endl;
1895 map <int,int>::iterator itF;
1896 for (itF = myFaceMap.begin(); itF != myFaceMap.end(); itF++)
1897 save << "--> nb nodes: " << itF->first << " - nb elemens:\t" << itF->second << endl;
1900 save << ++clause << ") Total number of " << orderStr << " volumes:\t" << NbVolumes(order) << endl;
1901 if ( NbVolumes(order) > 0 ) {
1902 int nb8 = NbHexas(order);
1903 int nb4 = NbTetras(order);
1904 int nb5 = NbPyramids(order);
1905 int nb6 = NbPrisms(order);
1906 save << clause << ".1) Number of " << orderStr << " hexahedrons:\t" << nb8 << endl;
1907 save << clause << ".2) Number of " << orderStr << " tetrahedrons:\t" << nb4 << endl;
1908 save << clause << ".3) Number of " << orderStr << " prisms: \t" << nb6 << endl;
1909 save << clause << ".4) Number of " << orderStr << " pyramids:\t" << nb5 << endl;
1910 if ( nb8 + nb4 + nb5 + nb6 != NbVolumes(order) ) {
1911 map<int,int> myVolumesMap;
1912 SMDS_VolumeIteratorPtr itVolumes=_myMeshDS->volumesIterator();
1913 while( itVolumes->more( ) ) {
1914 int nbNodes = itVolumes->next()->NbNodes();
1915 if ( myVolumesMap.find( nbNodes ) == myVolumesMap.end() )
1916 myVolumesMap[ nbNodes ] = 0;
1917 myVolumesMap[ nbNodes ] = myVolumesMap[ nbNodes ] + 1;
1919 save << clause << ".5) Volumes in detail: " << endl;
1920 map <int,int>::iterator itV;
1921 for (itV = myVolumesMap.begin(); itV != myVolumesMap.end(); itV++)
1922 save << "--> nb nodes: " << itV->first << " - nb elemens:\t" << itV->second << endl;
1927 save << "===========================================================================" << endl;
1931 //=======================================================================
1932 //function : GetElementType
1933 //purpose : Returns type of mesh element with certain id
1934 //=======================================================================
1936 SMDSAbs_ElementType SMESH_Mesh::GetElementType( const int id, const bool iselem )
1938 return _myMeshDS->GetElementType( id, iselem );
1941 //=============================================================================
1943 * \brief Convert group on geometry into standalone group
1945 //=============================================================================
1947 SMESH_Group* SMESH_Mesh::ConvertToStandalone ( int theGroupID )
1949 SMESH_Group* aGroup = 0;
1950 map < int, SMESH_Group * >::iterator itg = _mapGroup.find( theGroupID );
1951 if ( itg == _mapGroup.end() )
1954 SMESH_Group* anOldGrp = (*itg).second;
1955 SMESHDS_GroupBase* anOldGrpDS = anOldGrp->GetGroupDS();
1956 if ( !anOldGrp || !anOldGrpDS )
1959 // create new standalone group
1960 aGroup = new SMESH_Group (theGroupID, this, anOldGrpDS->GetType(), anOldGrp->GetName() );
1961 _mapGroup[theGroupID] = aGroup;
1963 SMESHDS_Group* aNewGrpDS = dynamic_cast<SMESHDS_Group*>( aGroup->GetGroupDS() );
1964 GetMeshDS()->RemoveGroup( anOldGrpDS );
1965 GetMeshDS()->AddGroup( aNewGrpDS );
1967 // add elements (or nodes) into new created group
1968 SMDS_ElemIteratorPtr anItr = anOldGrpDS->GetElements();
1969 while ( anItr->more() )
1970 aNewGrpDS->Add( (anItr->next())->GetID() );
1973 aNewGrpDS->SetColor( anOldGrpDS->GetColor() );
1981 //=============================================================================
1983 * \brief remove submesh order from Mesh
1985 //=============================================================================
1987 void SMESH_Mesh::ClearMeshOrder()
1989 _mySubMeshOrder.clear();
1992 //=============================================================================
1994 * \brief remove submesh order from Mesh
1996 //=============================================================================
1998 void SMESH_Mesh::SetMeshOrder(const TListOfListOfInt& theOrder )
2000 _mySubMeshOrder = theOrder;
2003 //=============================================================================
2005 * \brief return submesh order if any
2007 //=============================================================================
2009 const TListOfListOfInt& SMESH_Mesh::GetMeshOrder() const
2011 return _mySubMeshOrder;
2014 //=============================================================================
2016 * \brief fill _mapAncestors
2018 //=============================================================================
2020 void SMESH_Mesh::fillAncestorsMap(const TopoDS_Shape& theShape)
2023 int desType, ancType;
2024 if ( !theShape.IsSame( GetShapeToMesh()) && theShape.ShapeType() == TopAbs_COMPOUND )
2026 // a geom group is added. Insert it into lists of ancestors before
2027 // the first ancestor more complex than group members
2028 TopoDS_Iterator subIt( theShape );
2029 if ( !subIt.More() ) return;
2030 int memberType = subIt.Value().ShapeType();
2031 for ( desType = TopAbs_VERTEX; desType >= memberType; desType-- )
2032 for (TopExp_Explorer des( theShape, TopAbs_ShapeEnum( desType )); des.More(); des.Next())
2034 if ( !_mapAncestors.Contains( des.Current() )) continue;// issue 0020982
2035 TopTools_ListOfShape& ancList = _mapAncestors.ChangeFromKey( des.Current() );
2036 TopTools_ListIteratorOfListOfShape ancIt (ancList);
2037 while ( ancIt.More() && ancIt.Value().ShapeType() >= memberType )
2040 ancList.InsertBefore( theShape, ancIt );
2044 for ( desType = TopAbs_VERTEX; desType > TopAbs_COMPOUND; desType-- )
2045 for ( ancType = desType - 1; ancType >= TopAbs_COMPOUND; ancType-- )
2046 TopExp::MapShapesAndAncestors ( theShape,
2047 (TopAbs_ShapeEnum) desType,
2048 (TopAbs_ShapeEnum) ancType,
2051 // visit COMPOUNDs inside a COMPOUND that are not reachable by TopExp_Explorer
2052 if ( theShape.ShapeType() == TopAbs_COMPOUND )
2054 for ( TopoDS_Iterator sIt(theShape); sIt.More(); sIt.Next() )
2055 if ( sIt.Value().ShapeType() == TopAbs_COMPOUND )
2056 fillAncestorsMap( sIt.Value() );
2060 //=============================================================================
2062 * \brief sort submeshes according to stored mesh order
2063 * \param theListToSort in out list to be sorted
2064 * \return FALSE if nothing sorted
2066 //=============================================================================
2068 bool SMESH_Mesh::SortByMeshOrder(list<SMESH_subMesh*>& theListToSort) const
2070 if ( !_mySubMeshOrder.size() || theListToSort.size() < 2)
2074 list<SMESH_subMesh*> onlyOrderedList;
2075 // collect all ordered submeshes in one list as pointers
2076 // and get their positions within theListToSort
2077 typedef list<SMESH_subMesh*>::iterator TPosInList;
2078 map< int, TPosInList > sortedPos;
2079 TPosInList smBeg = theListToSort.begin(), smEnd = theListToSort.end();
2080 TListOfListOfInt::const_iterator listIdsIt = _mySubMeshOrder.begin();
2081 for( ; listIdsIt != _mySubMeshOrder.end(); listIdsIt++) {
2082 const TListOfInt& listOfId = *listIdsIt;
2083 TListOfInt::const_iterator idIt = listOfId.begin();
2084 for ( ; idIt != listOfId.end(); idIt++ ) {
2085 if ( SMESH_subMesh * sm = GetSubMeshContaining( *idIt )) {
2086 TPosInList smPos = find( smBeg, smEnd, sm );
2087 if ( smPos != smEnd ) {
2088 onlyOrderedList.push_back( sm );
2089 sortedPos[ distance( smBeg, smPos )] = smPos;
2094 if (onlyOrderedList.size() < 2)
2098 list<SMESH_subMesh*>::iterator onlyBIt = onlyOrderedList.begin();
2099 list<SMESH_subMesh*>::iterator onlyEIt = onlyOrderedList.end();
2101 // iterate on ordered submeshes and insert them in detected positions
2102 map< int, TPosInList >::iterator i_pos = sortedPos.begin();
2103 for ( ; onlyBIt != onlyEIt; ++onlyBIt, ++i_pos )
2104 *(i_pos->second) = *onlyBIt;
2109 //================================================================================
2111 * \brief Return true if given order of sub-meshes is OK
2113 //================================================================================
2115 bool SMESH_Mesh::IsOrderOK( const SMESH_subMesh* smBefore,
2116 const SMESH_subMesh* smAfter ) const
2118 TListOfListOfInt::const_iterator listIdsIt = _mySubMeshOrder.begin();
2119 TListOfInt::const_iterator idBef, idAft;
2120 for( ; listIdsIt != _mySubMeshOrder.end(); listIdsIt++)
2122 const TListOfInt& listOfId = *listIdsIt;
2123 idBef = std::find( listOfId.begin(), listOfId.end(), smBefore->GetId() );
2124 if ( idBef != listOfId.end() )
2125 idAft = std::find( listOfId.begin(), listOfId.end(), smAfter->GetId() );
2126 if ( idAft != listOfId.end () )
2127 return ( std::distance( listOfId.begin(), idBef ) <
2128 std::distance( listOfId.begin(), idAft ) );
2130 return true; // no order imposed to given submeshes
2133 //=============================================================================
2135 * \brief sort submeshes according to stored mesh order
2136 * \param theListToSort in out list to be sorted
2137 * \return FALSE if nothing sorted
2139 //=============================================================================
2141 list<SMESH_subMesh*>
2142 SMESH_Mesh::getAncestorsSubMeshes (const TopoDS_Shape& theSubShape) const
2144 list<SMESH_subMesh*> listOfSubMesh;
2145 TopTools_ListIteratorOfListOfShape it( GetAncestors( theSubShape ));
2146 for (; it.More(); it.Next() )
2147 if ( SMESH_subMesh* sm = GetSubMeshContaining( it.Value() ))
2148 listOfSubMesh.push_back(sm);
2150 // sort submeshes according to stored mesh order
2151 SortByMeshOrder( listOfSubMesh );
2153 return listOfSubMesh;