1 // Copyright (C) 2007-2015 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // SMESH SMESH : implementaion of SMESH idl descriptions
24 // File : SMESH_subMesh.cxx
25 // Author : Paul RASCLE, EDF
28 #include "SMESH_subMesh.hxx"
30 #include "SMESH_Algo.hxx"
31 #include "SMESH_Gen.hxx"
32 #include "SMESH_HypoFilter.hxx"
33 #include "SMESH_Hypothesis.hxx"
34 #include "SMESH_Mesh.hxx"
35 #include "SMESH_MesherHelper.hxx"
36 #include "SMESH_subMeshEventListener.hxx"
37 #include "SMESH_Comment.hxx"
38 #include "SMDS_SetIterator.hxx"
39 #include "SMDSAbs_ElementType.hxx"
41 #include <Basics_OCCTVersion.hxx>
43 #include "utilities.h"
45 #include "Basics_Utils.hxx"
47 #include <BRep_Builder.hxx>
48 #include <BRep_Tool.hxx>
50 #include <TopExp_Explorer.hxx>
51 #include <TopTools_IndexedMapOfShape.hxx>
52 #include <TopTools_ListIteratorOfListOfShape.hxx>
53 #include <TopTools_ListOfShape.hxx>
55 #include <TopoDS_Compound.hxx>
56 #include <TopoDS_Iterator.hxx>
59 #include <Standard_OutOfMemory.hxx>
60 #include <Standard_ErrorHandler.hxx>
66 //=============================================================================
68 * \brief Allocate some memory at construction and release it at destruction.
69 * Is used to be able to continue working after mesh generation breaks due to
72 //=============================================================================
77 MemoryReserve(): myBuf( new char[1024*1024*2] ){}
78 ~MemoryReserve() { delete [] myBuf; }
81 //=============================================================================
83 * default constructor:
85 //=============================================================================
87 SMESH_subMesh::SMESH_subMesh(int Id,
89 SMESHDS_Mesh * meshDS,
90 const TopoDS_Shape & aSubShape)
92 _subShape = aSubShape;
93 _subMeshDS = meshDS->MeshElements(_subShape); // may be null ...
96 _dependenceAnalysed = _alwaysComputed = false;
98 if (_subShape.ShapeType() == TopAbs_VERTEX)
101 _computeState = READY_TO_COMPUTE;
105 _algoState = NO_ALGO;
106 _computeState = NOT_READY;
108 _computeCost = 0; // how costly is to compute this sub-mesh
109 _realComputeCost = 0;
112 //=============================================================================
116 //=============================================================================
118 SMESH_subMesh::~SMESH_subMesh()
120 deleteOwnListeners();
123 //=============================================================================
127 //=============================================================================
129 int SMESH_subMesh::GetId() const
131 //MESSAGE("SMESH_subMesh::GetId");
135 //=============================================================================
139 //=============================================================================
141 SMESHDS_SubMesh * SMESH_subMesh::GetSubMeshDS()
143 // submesh appears in DS only when a mesher set nodes and elements on a shape
144 return _subMeshDS ? _subMeshDS : _subMeshDS = _father->GetMeshDS()->MeshElements(_subShape); // may be null
147 //=============================================================================
151 //=============================================================================
153 const SMESHDS_SubMesh * SMESH_subMesh::GetSubMeshDS() const
155 return ((SMESH_subMesh*) this )->GetSubMeshDS();
158 //=============================================================================
162 //=============================================================================
164 SMESHDS_SubMesh* SMESH_subMesh::CreateSubMeshDS()
166 if ( !GetSubMeshDS() ) {
167 SMESHDS_Mesh* meshDS = _father->GetMeshDS();
168 meshDS->NewSubMesh( meshDS->ShapeToIndex( _subShape ) );
170 return GetSubMeshDS();
173 //=============================================================================
177 //=============================================================================
179 SMESH_subMesh *SMESH_subMesh::GetFirstToCompute()
181 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(true,false);
182 while ( smIt->more() ) {
183 SMESH_subMesh *sm = smIt->next();
184 if ( sm->GetComputeState() == READY_TO_COMPUTE )
187 return 0; // nothing to compute
190 //================================================================================
192 * \brief Returns a current algorithm
194 //================================================================================
196 SMESH_Algo* SMESH_subMesh::GetAlgo() const
200 SMESH_subMesh* me = const_cast< SMESH_subMesh* >( this );
201 me->_algo = _father->GetGen()->GetAlgo( me );
206 //================================================================================
208 * \brief Allow algo->Compute() if a sub-shape of lower dim is meshed but
209 * none mesh entity is bound to it (PAL13615, 2nd part)
211 //================================================================================
213 void SMESH_subMesh::SetIsAlwaysComputed(bool isAlCo)
215 _alwaysComputed = isAlCo;
216 if ( _alwaysComputed )
217 _computeState = COMPUTE_OK;
219 ComputeStateEngine( CHECK_COMPUTE_STATE );
222 //=======================================================================
224 * \brief Return true if no mesh entities is bound to the submesh
226 //=======================================================================
228 bool SMESH_subMesh::IsEmpty() const
230 if (SMESHDS_SubMesh * subMeshDS = ((SMESH_subMesh*)this)->GetSubMeshDS())
231 return (!subMeshDS->NbElements() && !subMeshDS->NbNodes());
235 //=======================================================================
236 //function : IsMeshComputed
237 //purpose : check if _subMeshDS contains mesh elements
238 //=======================================================================
240 bool SMESH_subMesh::IsMeshComputed() const
242 if ( _alwaysComputed )
244 // algo may bind a submesh not to _subShape, eg 3D algo
245 // sets nodes on SHELL while _subShape may be SOLID
247 SMESHDS_Mesh* meshDS = _father->GetMeshDS();
248 int dim = SMESH_Gen::GetShapeDim( _subShape );
249 int type = _subShape.ShapeType();
250 for ( ; type <= TopAbs_VERTEX; type++) {
251 if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
253 TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
254 for ( ; exp.More(); exp.Next() )
256 if ( SMESHDS_SubMesh * smDS = meshDS->MeshElements( exp.Current() ))
258 bool computed = (dim > 0) ? smDS->NbElements() : smDS->NbNodes();
271 //=============================================================================
273 * Return true if all sub-meshes have been meshed
275 //=============================================================================
277 bool SMESH_subMesh::SubMeshesComputed(bool * isFailedToCompute/*=0*/) const
279 int myDim = SMESH_Gen::GetShapeDim( _subShape );
280 int dimToCheck = myDim - 1;
281 bool subMeshesComputed = true;
282 if ( isFailedToCompute ) *isFailedToCompute = false;
283 // check subMeshes with upper dimension => reverse iteration
284 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,true);
285 while ( smIt->more() )
287 SMESH_subMesh *sm = smIt->next();
288 if ( sm->_alwaysComputed )
290 const TopoDS_Shape & ss = sm->GetSubShape();
292 // MSV 07.04.2006: restrict checking to myDim-1 only. Ex., there is no sense
293 // in checking of existence of edges if the algo needs only faces. Moreover,
294 // degenerated edges may have no submesh, as after computing NETGEN_2D.
295 if ( !_algo || _algo->NeedDiscreteBoundary() ) {
296 int dim = SMESH_Gen::GetShapeDim( ss );
297 if (dim < dimToCheck)
298 break; // the rest subMeshes are all of less dimension
300 SMESHDS_SubMesh * ds = sm->GetSubMeshDS();
301 bool computeOk = (sm->GetComputeState() == COMPUTE_OK ||
302 (ds && ( dimToCheck ? ds->NbElements() : ds->NbNodes() )));
305 subMeshesComputed = false;
306 if ( isFailedToCompute && !(*isFailedToCompute) )
307 *isFailedToCompute = ( sm->GetComputeState() == FAILED_TO_COMPUTE );
309 // int type = ss.ShapeType();
313 // case TopAbs_COMPOUND:
315 // MESSAGE("The not computed sub mesh is a COMPOUND");
318 // case TopAbs_COMPSOLID:
320 // MESSAGE("The not computed sub mesh is a COMPSOLID");
323 // case TopAbs_SHELL:
325 // MESSAGE("The not computed sub mesh is a SHEL");
330 // MESSAGE("The not computed sub mesh is a WIRE");
333 // case TopAbs_SOLID:
335 // MESSAGE("The not computed sub mesh is a SOLID");
340 // MESSAGE("The not computed sub mesh is a FACE");
345 // MESSAGE("The not computed sub mesh is a EDGE");
350 // MESSAGE("The not computed sub mesh is of unknown type");
355 if ( !isFailedToCompute )
359 return subMeshesComputed;
362 //================================================================================
364 * \brief Return cost of computing this sub-mesh. If hypotheses are not well defined,
366 * \return int - the computation cost in abstract units.
368 //================================================================================
370 int SMESH_subMesh::GetComputeCost() const
372 return _realComputeCost;
375 //================================================================================
377 * \brief Return cost of computing this sub-mesh. The cost depends on the shape type
378 * and number of sub-meshes this one DependsOn().
379 * \return int - the computation cost in abstract units.
381 //================================================================================
383 int SMESH_subMesh::computeCost() const
388 switch ( _subShape.ShapeType() ) {
390 case TopAbs_SHELL: computeCost = 5000; break;
391 case TopAbs_FACE: computeCost = 500; break;
392 case TopAbs_EDGE: computeCost = 2; break;
393 default: computeCost = 1;
395 SMESH_subMeshIteratorPtr childIt = getDependsOnIterator(/*includeSelf=*/false);
396 while ( childIt->more() )
397 computeCost += childIt->next()->computeCost();
399 ((SMESH_subMesh*)this)->_computeCost = computeCost;
404 //=============================================================================
406 * Returns all sub-meshes this one depend on
408 //=============================================================================
410 const map < int, SMESH_subMesh * >& SMESH_subMesh::DependsOn()
412 if ( _dependenceAnalysed || !_father->HasShapeToMesh() )
415 int type = _subShape.ShapeType();
418 case TopAbs_COMPOUND:
420 list< TopoDS_Shape > compounds( 1, _subShape );
421 list< TopoDS_Shape >::iterator comp = compounds.begin();
422 for ( ; comp != compounds.end(); ++comp )
424 for ( TopoDS_Iterator sub( *comp ); sub.More(); sub.Next() )
425 switch ( sub.Value().ShapeType() )
427 case TopAbs_COMPOUND: compounds.push_back( sub.Value() ); break;
428 case TopAbs_COMPSOLID: insertDependence( sub.Value(), TopAbs_SOLID ); break;
429 case TopAbs_SOLID: insertDependence( sub.Value(), TopAbs_SOLID ); break;
430 case TopAbs_SHELL: insertDependence( sub.Value(), TopAbs_FACE ); break;
431 case TopAbs_FACE: insertDependence( sub.Value(), TopAbs_FACE ); break;
432 case TopAbs_WIRE: insertDependence( sub.Value(), TopAbs_EDGE ); break;
433 case TopAbs_EDGE: insertDependence( sub.Value(), TopAbs_EDGE ); break;
434 case TopAbs_VERTEX: insertDependence( sub.Value(), TopAbs_VERTEX ); break;
440 case TopAbs_COMPSOLID: insertDependence( _subShape, TopAbs_SOLID ); break;
441 case TopAbs_SOLID: insertDependence( _subShape, TopAbs_FACE ); break;
442 case TopAbs_SHELL: insertDependence( _subShape, TopAbs_FACE ); break;
443 case TopAbs_FACE: insertDependence( _subShape, TopAbs_EDGE ); break;
444 case TopAbs_WIRE: insertDependence( _subShape, TopAbs_EDGE ); break;
445 case TopAbs_EDGE: insertDependence( _subShape, TopAbs_VERTEX ); break;
448 _dependenceAnalysed = true;
452 //================================================================================
454 * \brief Return a key for SMESH_subMesh::_mapDepend map
456 //================================================================================
460 int dependsOnMapKey( TopAbs_ShapeEnum type, int shapeID )
462 int ordType = 9 - int(type); // 2 = Vertex, 8 = CompSolid
464 cle += 10000000 * ordType; // sort map by ordType then index
467 int dependsOnMapKey( const SMESH_subMesh* sm )
469 return dependsOnMapKey( sm->GetSubShape().ShapeType(), sm->GetId() );
473 //=============================================================================
475 * Add sub-meshes on sub-shapes of a given type into the dependence map.
477 //=============================================================================
479 void SMESH_subMesh::insertDependence(const TopoDS_Shape aShape,
480 TopAbs_ShapeEnum aSubType)
482 TopExp_Explorer sub( aShape, aSubType );
483 for ( ; sub.More(); sub.Next() )
485 SMESH_subMesh *aSubMesh = _father->GetSubMesh( sub.Current() );
486 if ( aSubMesh->GetId() == 0 )
487 continue; // not a sub-shape of the shape to mesh
488 int cle = dependsOnMapKey( aSubMesh );
489 if ( _mapDepend.find( cle ) == _mapDepend.end())
491 _mapDepend[cle] = aSubMesh;
492 const map < int, SMESH_subMesh * > & subMap = aSubMesh->DependsOn();
493 _mapDepend.insert( subMap.begin(), subMap.end() );
498 //================================================================================
500 * \brief Return \c true if \a this sub-mesh depends on \a other
502 //================================================================================
504 bool SMESH_subMesh::DependsOn( const SMESH_subMesh* other ) const
506 return other ? _mapDepend.count( dependsOnMapKey( other )) : false;
509 //================================================================================
511 * \brief Return \c true if \a this sub-mesh depends on a \a shape
513 //================================================================================
515 bool SMESH_subMesh::DependsOn( const int shapeID ) const
517 return DependsOn( _father->GetSubMeshContaining( shapeID ));
520 //=============================================================================
522 * Return a shape of \a this sub-mesh
524 //=============================================================================
526 const TopoDS_Shape & SMESH_subMesh::GetSubShape() const
531 //=======================================================================
532 //function : CanAddHypothesis
533 //purpose : return true if theHypothesis can be attached to me:
534 // its dimention is checked
535 //=======================================================================
537 bool SMESH_subMesh::CanAddHypothesis(const SMESH_Hypothesis* theHypothesis) const
539 int aHypDim = theHypothesis->GetDim();
540 int aShapeDim = SMESH_Gen::GetShapeDim(_subShape);
541 // issue 21106. Forbid 3D mesh on the SHELL
542 // if (aHypDim == 3 && aShapeDim == 3) {
543 // // check case of open shell
544 // //if (_subShape.ShapeType() == TopAbs_SHELL && !_subShape.Closed())
545 // if (_subShape.ShapeType() == TopAbs_SHELL && !BRep_Tool::IsClosed(_subShape))
548 if ( aHypDim <= aShapeDim )
554 //=======================================================================
555 //function : IsApplicableHypotesis
557 //=======================================================================
559 bool SMESH_subMesh::IsApplicableHypotesis(const SMESH_Hypothesis* theHypothesis,
560 const TopAbs_ShapeEnum theShapeType)
562 if ( theHypothesis->GetType() > SMESHDS_Hypothesis::PARAM_ALGO)
565 if ( theHypothesis->GetShapeType() & (1<< theShapeType))
566 // issue 21106. Forbid 3D mesh on the SHELL
567 return !( theHypothesis->GetDim() == 3 && theShapeType == TopAbs_SHELL );
573 switch ( theShapeType ) {
578 return SMESH_Gen::GetShapeDim( theShapeType ) == theHypothesis->GetDim();
581 // Special case for algorithms, building 2D mesh on a whole shell.
582 // Before this fix there was a problem after restoring from study,
583 // because in that case algorithm is assigned before hypothesis
584 // (on shell in problem case) and hypothesis is checked on faces
585 // (because it is 2D), where we have NO_ALGO state.
586 // Now 2D hypothesis is also applicable to shells.
587 return (theHypothesis->GetDim() == 2 || theHypothesis->GetDim() == 3);
590 // case TopAbs_COMPSOLID:
591 // case TopAbs_COMPOUND:
597 //================================================================================
599 * \brief Treats modification of hypotheses definition
600 * \param [in] event - what happens
601 * \param [in] anHyp - a hypothesis
602 * \return SMESH_Hypothesis::Hypothesis_Status - a treatment result.
604 * Optional description of a problematic situation (if any) can be retrieved
605 * via GetComputeError().
607 //================================================================================
609 SMESH_Hypothesis::Hypothesis_Status
610 SMESH_subMesh::AlgoStateEngine(int event, SMESH_Hypothesis * anHyp)
612 // **** les retour des evenement shape sont significatifs
613 // (add ou remove fait ou non)
614 // le retour des evenement father n'indiquent pas que add ou remove fait
616 SMESH_Hypothesis::Hypothesis_Status aux_ret, ret = SMESH_Hypothesis::HYP_OK;
617 if ( _Id == 0 ) return ret; // not a sub-shape of the shape to mesh
619 SMESHDS_Mesh* meshDS =_father->GetMeshDS();
620 SMESH_Algo* algo = 0;
623 if (_subShape.ShapeType() == TopAbs_VERTEX )
625 if ( anHyp->GetDim() != 0) {
626 if (event == ADD_HYP || event == ADD_ALGO)
627 return SMESH_Hypothesis::HYP_BAD_DIM;
629 return SMESH_Hypothesis::HYP_OK;
632 else if ( _algoState == HYP_OK ) {
633 // update default _algoState
634 if ( event != REMOVE_FATHER_ALGO )
636 _algoState = NO_ALGO;
639 _algoState = MISSING_HYP;
640 if ( event == REMOVE_FATHER_HYP ||
641 algo->CheckHypothesis(*_father,_subShape, aux_ret))
648 int oldAlgoState = _algoState;
649 bool modifiedHyp = (event == MODIF_HYP); // if set to true, force event MODIF_ALGO_STATE
650 SMESH_Algo* algoRequiringCleaning = 0;
652 bool isApplicableHyp = IsApplicableHypotesis( anHyp );
654 if (event == ADD_ALGO || event == ADD_FATHER_ALGO)
656 // -------------------------------------------
657 // check if a shape needed by algo is present
658 // -------------------------------------------
659 algo = static_cast< SMESH_Algo* >( anHyp );
660 if ( !_father->HasShapeToMesh() && algo->NeedShape() )
661 return SMESH_Hypothesis::HYP_NEED_SHAPE;
662 // ----------------------
663 // check mesh conformity
664 // ----------------------
665 if (isApplicableHyp && !_father->IsNotConformAllowed() && !IsConform( algo ))
666 return SMESH_Hypothesis::HYP_NOTCONFORM;
668 // check if all-dimensional algo is hidden by other local one
669 if ( event == ADD_ALGO ) {
670 SMESH_HypoFilter filter( SMESH_HypoFilter::HasType( algo->GetType() ));
671 filter.Or( SMESH_HypoFilter::HasType( algo->GetType()+1 ));
672 filter.Or( SMESH_HypoFilter::HasType( algo->GetType()+2 ));
673 if ( SMESH_Algo * curAlgo = (SMESH_Algo*)_father->GetHypothesis( this, filter, true ))
674 if ( !curAlgo->NeedDiscreteBoundary() )
675 algoRequiringCleaning = curAlgo;
679 // ----------------------------------
680 // add a hypothesis to DS if possible
681 // ----------------------------------
682 if (event == ADD_HYP || event == ADD_ALGO)
684 if ( ! CanAddHypothesis( anHyp )) // check dimension
685 return SMESH_Hypothesis::HYP_BAD_DIM;
687 if ( !anHyp->IsAuxiliary() && getSimilarAttached( _subShape, anHyp ) )
688 return SMESH_Hypothesis::HYP_ALREADY_EXIST;
690 if ( !meshDS->AddHypothesis(_subShape, anHyp))
691 return SMESH_Hypothesis::HYP_ALREADY_EXIST;
694 // --------------------------
695 // remove a hypothesis from DS
696 // --------------------------
697 if (event == REMOVE_HYP || event == REMOVE_ALGO)
699 if (!meshDS->RemoveHypothesis(_subShape, anHyp))
700 return SMESH_Hypothesis::HYP_OK; // nothing changes
702 if (event == REMOVE_ALGO)
704 algo = dynamic_cast<SMESH_Algo*> (anHyp);
705 if (!algo->NeedDiscreteBoundary())
706 algoRequiringCleaning = algo;
710 // ------------------
711 // analyse algo state
712 // ------------------
713 if (!isApplicableHyp)
714 return ret; // not applicable hypotheses do not change algo state
716 if (( algo = GetAlgo()))
717 algo->InitComputeError();
722 // ----------------------------------------------------------------------
731 if (algo->CheckHypothesis((*_father),_subShape, aux_ret))
732 setAlgoState(HYP_OK);
733 else if ( algo->IsStatusFatal( aux_ret )) {
734 meshDS->RemoveHypothesis(_subShape, anHyp);
738 setAlgoState(MISSING_HYP);
745 case ADD_FATHER_ALGO: { // Algo just added in father
748 if ( algo == anHyp ) {
749 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret))
750 setAlgoState(HYP_OK);
752 setAlgoState(MISSING_HYP);
756 case REMOVE_FATHER_HYP:
758 case REMOVE_FATHER_ALGO: {
762 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
763 setAlgoState(HYP_OK);
765 setAlgoState(MISSING_HYP);
769 case MODIF_HYP: break;
776 // ----------------------------------------------------------------------
784 if ( algo->CheckHypothesis((*_father),_subShape, ret ))
785 setAlgoState(HYP_OK);
786 if (SMESH_Hypothesis::IsStatusFatal( ret ))
787 meshDS->RemoveHypothesis(_subShape, anHyp);
788 else if (!_father->IsUsedHypothesis( anHyp, this ))
790 meshDS->RemoveHypothesis(_subShape, anHyp);
791 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
795 case ADD_ALGO: { //already existing algo : on father ?
798 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))// ignore hyp status
799 setAlgoState(HYP_OK);
800 else if ( algo->IsStatusFatal( aux_ret )) {
801 meshDS->RemoveHypothesis(_subShape, anHyp);
805 setAlgoState(MISSING_HYP);
810 case REMOVE_ALGO: { // perhaps a father algo applies ?
812 if (algo == NULL) // no more algo applying on sub-shape...
814 setAlgoState(NO_ALGO);
818 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
819 setAlgoState(HYP_OK);
821 setAlgoState(MISSING_HYP);
825 case MODIF_HYP: // assigned hypothesis value may become good
826 case ADD_FATHER_HYP: {
829 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
830 setAlgoState(HYP_OK);
832 setAlgoState(MISSING_HYP);
835 case ADD_FATHER_ALGO: { // new father algo
838 if ( algo == anHyp ) {
839 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
840 setAlgoState(HYP_OK);
842 setAlgoState(MISSING_HYP);
846 case REMOVE_FATHER_HYP: // nothing to do
848 case REMOVE_FATHER_ALGO: {
850 if (algo == NULL) // no more applying algo on father
852 setAlgoState(NO_ALGO);
856 if ( algo->CheckHypothesis((*_father),_subShape , aux_ret ))
857 setAlgoState(HYP_OK);
859 setAlgoState(MISSING_HYP);
869 // ----------------------------------------------------------------------
877 if (!algo->CheckHypothesis((*_father),_subShape, ret ))
879 if ( !SMESH_Hypothesis::IsStatusFatal( ret ))
880 // ret should be fatal: anHyp was not added
881 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
883 else if (!_father->IsUsedHypothesis( anHyp, this ))
884 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
886 if (SMESH_Hypothesis::IsStatusFatal( ret ))
888 MESSAGE("do not add extra hypothesis");
889 meshDS->RemoveHypothesis(_subShape, anHyp);
897 case ADD_ALGO: { //already existing algo : on father ?
899 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
900 // check if algo changes
902 f.Init( SMESH_HypoFilter::IsAlgo() );
903 f.And( SMESH_HypoFilter::IsApplicableTo( _subShape ));
904 f.AndNot( SMESH_HypoFilter::Is( algo ));
905 const SMESH_Hypothesis * prevAlgo = _father->GetHypothesis( this, f, true );
907 string( algo->GetName()) != prevAlgo->GetName())
911 setAlgoState(MISSING_HYP);
917 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
918 setAlgoState(HYP_OK);
920 setAlgoState(MISSING_HYP);
924 case REMOVE_ALGO: { // perhaps a father algo applies ?
926 if (algo == NULL) // no more algo applying on sub-shape...
928 setAlgoState(NO_ALGO);
932 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
933 // check if algo remains
934 if ( anHyp != algo && strcmp( anHyp->GetName(), algo->GetName()) )
938 setAlgoState(MISSING_HYP);
942 case MODIF_HYP: // hypothesis value may become bad
943 case ADD_FATHER_HYP: { // new father hypothesis ?
946 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
948 if (_father->IsUsedHypothesis( anHyp, this )) // new Hyp
952 setAlgoState(MISSING_HYP);
955 case ADD_FATHER_ALGO: {
957 if ( algo == anHyp ) { // a new algo on father
958 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
959 // check if algo changes
961 f.Init( SMESH_HypoFilter::IsAlgo() );
962 f.And( SMESH_HypoFilter::IsApplicableTo( _subShape ));
963 f.AndNot( SMESH_HypoFilter::Is( algo ));
964 const SMESH_Hypothesis* prevAlgo = _father->GetHypothesis( this, f, true );
966 string(algo->GetName()) != string(prevAlgo->GetName()) )
970 setAlgoState(MISSING_HYP);
974 case REMOVE_FATHER_HYP: {
977 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
978 // is there the same local hyp or maybe a new father algo applied?
979 if ( !getSimilarAttached( _subShape, anHyp ) )
983 setAlgoState(MISSING_HYP);
986 case REMOVE_FATHER_ALGO: {
987 // IPAL21346. Edges not removed when Netgen 1d-2d is removed from a SOLID.
988 // CLEAN was not called at event REMOVE_ALGO because the algo is not applicable to SOLID.
989 algo = dynamic_cast<SMESH_Algo*> (anHyp);
990 if (!algo->NeedDiscreteBoundary())
991 algoRequiringCleaning = algo;
993 if (algo == NULL) // no more applying algo on father
995 setAlgoState(NO_ALGO);
999 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
1000 // check if algo changes
1001 if ( string(algo->GetName()) != string( anHyp->GetName()) )
1005 setAlgoState(MISSING_HYP);
1015 // ----------------------------------------------------------------------
1022 // detect algorithm hiding
1024 if ( ret == SMESH_Hypothesis::HYP_OK &&
1025 ( event == ADD_ALGO || event == ADD_FATHER_ALGO ) && algo &&
1026 algo->GetName() == anHyp->GetName() )
1029 SMESH_Gen* gen = _father->GetGen();
1030 const std::vector< SMESH_subMesh * > & ancestors = GetAncestors();
1031 for ( size_t iA = 0; ( ret == SMESH_Hypothesis::HYP_OK && iA < ancestors.size()); ++iA ) {
1032 if ( SMESH_Algo* upperAlgo = ancestors[ iA ]->GetAlgo() )
1033 if ( !upperAlgo->NeedDiscreteBoundary() && !upperAlgo->SupportSubmeshes())
1034 ret = SMESH_Hypothesis::HYP_HIDDEN_ALGO;
1037 if ( ret == SMESH_Hypothesis::HYP_OK &&
1038 !algo->NeedDiscreteBoundary() &&
1039 !algo->SupportSubmeshes())
1041 TopoDS_Shape algoAssignedTo, otherAssignedTo;
1042 gen->GetAlgo( this, &algoAssignedTo );
1043 map<int, SMESH_subMesh*>::reverse_iterator i_sm = _mapDepend.rbegin();
1044 for ( ; ( ret == SMESH_Hypothesis::HYP_OK && i_sm != _mapDepend.rend()) ; ++i_sm )
1045 if ( gen->GetAlgo( i_sm->second, &otherAssignedTo ) &&
1046 SMESH_MesherHelper::IsSubShape( /*sub=*/otherAssignedTo, /*main=*/algoAssignedTo ))
1047 ret = SMESH_Hypothesis::HYP_HIDING_ALGO;
1051 if ( _algo ) { // get an error description set by _algo->CheckHypothesis()
1052 _computeError = _algo->GetComputeError();
1053 _algo->InitComputeError();
1056 bool stateChange = ( _algoState != oldAlgoState );
1058 if ( stateChange && _algoState == HYP_OK ) // hyp becomes OK
1059 algo->SetEventListener( this );
1061 if ( event == REMOVE_ALGO || event == REMOVE_FATHER_ALGO )
1064 notifyListenersOnEvent( event, ALGO_EVENT, anHyp );
1066 if ( stateChange && oldAlgoState == HYP_OK ) { // hyp becomes KO
1067 deleteOwnListeners();
1068 SetIsAlwaysComputed( false );
1069 if (_subShape.ShapeType() == TopAbs_VERTEX ) {
1070 // restore default states
1071 _algoState = HYP_OK;
1072 _computeState = READY_TO_COMPUTE;
1076 if ( algoRequiringCleaning ) {
1077 // added or removed algo is all-dimensional
1078 ComputeStateEngine( CLEAN );
1079 cleanDependsOn( algoRequiringCleaning );
1080 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1083 if ( stateChange || modifiedHyp )
1084 ComputeStateEngine( MODIF_ALGO_STATE );
1086 _realComputeCost = ( _algoState == HYP_OK ) ? computeCost() : 0;
1091 //=======================================================================
1092 //function : IsConform
1093 //purpose : check if a conform mesh will be produced by the Algo
1094 //=======================================================================
1096 bool SMESH_subMesh::IsConform(const SMESH_Algo* theAlgo)
1098 // MESSAGE( "SMESH_subMesh::IsConform" );
1099 if ( !theAlgo ) return false;
1101 // Suppose that theAlgo is applicable to _subShape, do not check it here
1102 //if ( !IsApplicableHypotesis( theAlgo )) return false;
1104 // check only algo that doesn't NeedDiscreteBoundary(): because mesh made
1105 // on a sub-shape will be ignored by theAlgo
1106 if ( theAlgo->NeedDiscreteBoundary() ||
1107 !theAlgo->OnlyUnaryInput() ) // all adjacent shapes will be meshed by this algo?
1110 // only local algo is to be checked
1111 //if ( gen->IsGlobalHypothesis( theAlgo, *_father ))
1112 if ( _subShape.ShapeType() == _father->GetMeshDS()->ShapeToMesh().ShapeType() )
1115 // check algo attached to adjacent shapes
1117 // loop on one level down sub-meshes
1118 TopoDS_Iterator itsub( _subShape );
1119 for (; itsub.More(); itsub.Next())
1121 // loop on adjacent subShapes
1122 const std::vector< SMESH_subMesh * > & ancestors = GetAncestors();
1123 for ( size_t iA = 0; iA < ancestors.size(); ++iA )
1125 const TopoDS_Shape& adjacent = ancestors[ iA ]->GetSubShape();
1126 if ( _subShape.IsSame( adjacent )) continue;
1127 if ( adjacent.ShapeType() != _subShape.ShapeType())
1130 // check algo attached to smAdjacent
1131 SMESH_Algo * algo = ancestors[ iA ]->GetAlgo();
1133 !algo->NeedDiscreteBoundary() &&
1134 algo->OnlyUnaryInput())
1135 return false; // NOT CONFORM MESH WILL BE PRODUCED
1142 //=============================================================================
1146 //=============================================================================
1148 void SMESH_subMesh::setAlgoState(algo_state state)
1153 //================================================================================
1155 * \brief Send an event to sub-meshes
1156 * \param [in] event - the event
1157 * \param [in] anHyp - an hypothesis
1158 * \param [in] exitOnFatal - to stop iteration on sub-meshes if a sub-mesh
1159 * reports a fatal result
1160 * \return SMESH_Hypothesis::Hypothesis_Status - the worst result
1162 * Optional description of a problematic situation (if any) can be retrieved
1163 * via GetComputeError().
1165 //================================================================================
1167 SMESH_Hypothesis::Hypothesis_Status
1168 SMESH_subMesh::SubMeshesAlgoStateEngine(int event,
1169 SMESH_Hypothesis * anHyp,
1172 SMESH_Hypothesis::Hypothesis_Status ret = SMESH_Hypothesis::HYP_OK;
1173 //EAP: a wire (dim==1) should notify edges (dim==1)
1174 //EAP: int dim = SMESH_Gen::GetShapeDim(_subShape);
1175 //if (_subShape.ShapeType() < TopAbs_EDGE ) // wire,face etc
1177 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1178 while ( smIt->more() ) {
1179 SMESH_subMesh* sm = smIt->next();
1180 SMESH_Hypothesis::Hypothesis_Status ret2 = sm->AlgoStateEngine(event, anHyp);
1184 _computeError = sm->_computeError;
1185 sm->_computeError.reset();
1186 if ( exitOnFatal && SMESH_Hypothesis::IsStatusFatal( ret ))
1194 //================================================================================
1196 * \brief Remove elements from sub-meshes.
1197 * \param algoRequiringCleaning - an all-dimensional algorithm whose presence
1198 * causes the cleaning.
1200 //================================================================================
1202 void SMESH_subMesh::cleanDependsOn( SMESH_Algo* algoRequiringCleaning/*=0*/ )
1204 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,
1205 /*complexShapeFirst=*/true);
1206 if ( _father->NbNodes() == 0 )
1208 while ( smIt->more() )
1209 smIt->next()->ComputeStateEngine(CHECK_COMPUTE_STATE);
1211 else if ( !algoRequiringCleaning || !algoRequiringCleaning->SupportSubmeshes() )
1213 while ( smIt->more() )
1214 smIt->next()->ComputeStateEngine(CLEAN);
1216 else if ( algoRequiringCleaning && algoRequiringCleaning->SupportSubmeshes() )
1218 SMESHDS_Mesh* meshDS = _father->GetMeshDS();
1220 // find sub-meshes to keep elements on
1221 set< SMESH_subMesh* > smToKeep;
1222 TopAbs_ShapeEnum prevShapeType = TopAbs_SHAPE;
1223 bool toKeepPrevShapeType = false;
1224 while ( smIt->more() )
1226 SMESH_subMesh* sm = smIt->next();
1227 sm->ComputeStateEngine(CHECK_COMPUTE_STATE);
1228 if ( !sm->IsEmpty() )
1230 const bool sameShapeType = ( prevShapeType == sm->GetSubShape().ShapeType() );
1231 bool keepSubMeshes = ( sameShapeType && toKeepPrevShapeType );
1232 if ( !sameShapeType )
1234 // check if the algo allows presence of global algos of dimension the algo
1235 // can generate it-self
1236 int shapeDim = SMESH_Gen::GetShapeDim( sm->GetSubShape() );
1237 keepSubMeshes = algoRequiringCleaning->NeedLowerHyps( shapeDim );
1238 prevShapeType = sm->GetSubShape().ShapeType();
1239 toKeepPrevShapeType = keepSubMeshes;
1241 if ( !keepSubMeshes )
1243 // look for an algo assigned to sm
1244 bool algoFound = false;
1245 const list<const SMESHDS_Hypothesis*>& hyps = meshDS->GetHypothesis( sm->_subShape );
1246 list<const SMESHDS_Hypothesis*>::const_iterator h = hyps.begin();
1247 for ( ; ( !algoFound && h != hyps.end() ); ++h )
1248 algoFound = ((*h)->GetType() != SMESHDS_Hypothesis::PARAM_ALGO );
1249 keepSubMeshes = algoFound;
1251 // remember all sub-meshes of sm
1252 if ( keepSubMeshes )
1254 SMESH_subMeshIteratorPtr smIt2 = getDependsOnIterator(false);
1255 while ( smIt2->more() )
1256 smToKeep.insert( smIt2->next() );
1261 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,true);
1262 while ( smIt->more() )
1264 SMESH_subMesh* sm = smIt->next();
1265 if ( !smToKeep.count( sm ))
1266 sm->ComputeStateEngine(CLEAN);
1271 //=============================================================================
1275 //=============================================================================
1277 void SMESH_subMesh::DumpAlgoState(bool isMain)
1281 const map < int, SMESH_subMesh * >&subMeshes = DependsOn();
1283 map < int, SMESH_subMesh * >::const_iterator itsub;
1284 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
1286 SMESH_subMesh *sm = (*itsub).second;
1287 sm->DumpAlgoState(false);
1290 MESSAGE("dim = " << SMESH_Gen::GetShapeDim(_subShape) <<
1291 " type of shape " << _subShape.ShapeType());
1294 case NO_ALGO : MESSAGE(" AlgoState = NO_ALGO"); break;
1295 case MISSING_HYP : MESSAGE(" AlgoState = MISSING_HYP"); break;
1296 case HYP_OK : MESSAGE(" AlgoState = HYP_OK");break;
1298 switch (_computeState)
1300 case NOT_READY : MESSAGE(" ComputeState = NOT_READY");break;
1301 case READY_TO_COMPUTE : MESSAGE(" ComputeState = READY_TO_COMPUTE");break;
1302 case COMPUTE_OK : MESSAGE(" ComputeState = COMPUTE_OK");break;
1303 case FAILED_TO_COMPUTE: MESSAGE(" ComputeState = FAILED_TO_COMPUTE");break;
1307 //================================================================================
1309 * \brief Remove nodes and elements bound to submesh
1310 * \param subMesh - submesh containing nodes and elements
1312 //================================================================================
1314 static void cleanSubMesh( SMESH_subMesh * subMesh )
1317 if (SMESHDS_SubMesh * subMeshDS = subMesh->GetSubMeshDS()) {
1318 SMESHDS_Mesh * meshDS = subMesh->GetFather()->GetMeshDS();
1319 SMDS_ElemIteratorPtr ite = subMeshDS->GetElements();
1320 while (ite->more()) {
1321 const SMDS_MeshElement * elt = ite->next();
1322 //MESSAGE( " RM elt: "<<elt->GetID()<<" ( "<<elt->NbNodes()<<" )" );
1323 //meshDS->RemoveElement(elt);
1324 meshDS->RemoveFreeElement(elt, 0);
1327 SMDS_NodeIteratorPtr itn = subMeshDS->GetNodes();
1328 while (itn->more()) {
1329 const SMDS_MeshNode * node = itn->next();
1330 //MESSAGE( " RM node: "<<node->GetID());
1331 if ( node->NbInverseElements() == 0 )
1332 meshDS->RemoveFreeNode(node, 0);
1333 else // for StdMeshers_CompositeSegment_1D: node in one submesh, edge in another
1334 meshDS->RemoveNode(node);
1341 //=============================================================================
1345 //=============================================================================
1347 bool SMESH_subMesh::ComputeStateEngine(int event)
1350 case MODIF_ALGO_STATE:
1352 case COMPUTE_SUBMESH:
1353 //case COMPUTE_CANCELED:
1355 //case SUBMESH_COMPUTED:
1356 //case SUBMESH_RESTORED:
1357 //case SUBMESH_LOADED:
1358 //case MESH_ENTITY_REMOVED:
1359 //case CHECK_COMPUTE_STATE:
1360 _computeError.reset(); break;
1364 if ( event == CLEAN )
1365 _alwaysComputed = false; // Unset 'true' set by MergeNodes() (issue 0022182)
1367 if (_subShape.ShapeType() == TopAbs_VERTEX)
1369 _computeState = READY_TO_COMPUTE;
1370 SMESHDS_SubMesh* smDS = GetSubMeshDS();
1371 if ( smDS && smDS->NbNodes() )
1373 if ( event == CLEAN ) {
1375 cleanSubMesh( this );
1378 _computeState = COMPUTE_OK;
1380 else if (( event == COMPUTE || event == COMPUTE_SUBMESH )
1381 && !_alwaysComputed )
1383 const TopoDS_Vertex & V = TopoDS::Vertex( _subShape );
1384 gp_Pnt P = BRep_Tool::Pnt(V);
1385 if ( SMDS_MeshNode * n = _father->GetMeshDS()->AddNode(P.X(), P.Y(), P.Z()) ) {
1386 _father->GetMeshDS()->SetNodeOnVertex(n,_Id);
1387 _computeState = COMPUTE_OK;
1390 if ( event == MODIF_ALGO_STATE )
1394 SMESH_Gen *gen = _father->GetGen();
1395 SMESH_Algo *algo = 0;
1397 SMESH_Hypothesis::Hypothesis_Status hyp_status;
1398 //algo_state oldAlgoState = (algo_state) GetAlgoState();
1400 switch (_computeState)
1403 // ----------------------------------------------------------------------
1408 case MODIF_ALGO_STATE:
1410 if (algo && !algo->NeedDiscreteBoundary())
1411 cleanDependsOn( algo ); // clean sub-meshes with event CLEAN
1412 if ( _algoState == HYP_OK )
1413 _computeState = READY_TO_COMPUTE;
1415 case COMPUTE: // nothing to do
1416 case COMPUTE_SUBMESH:
1418 case COMPUTE_CANCELED: // nothing to do
1422 removeSubMeshElementsAndNodes();
1424 case SUBMESH_COMPUTED: // nothing to do
1426 case SUBMESH_RESTORED:
1427 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1429 case MESH_ENTITY_REMOVED:
1431 case SUBMESH_LOADED:
1432 loadDependentMeshes();
1433 ComputeSubMeshStateEngine( SUBMESH_LOADED );
1435 case CHECK_COMPUTE_STATE:
1436 if ( IsMeshComputed() )
1437 _computeState = COMPUTE_OK;
1445 // ----------------------------------------------------------------------
1447 case READY_TO_COMPUTE:
1450 case MODIF_ALGO_STATE:
1451 _computeState = NOT_READY;
1455 if (!algo->NeedDiscreteBoundary())
1456 cleanDependsOn( algo ); // clean sub-meshes with event CLEAN
1457 if ( _algoState == HYP_OK )
1458 _computeState = READY_TO_COMPUTE;
1462 case COMPUTE_SUBMESH:
1466 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1469 MESSAGE("***** verify compute state *****");
1470 _computeState = NOT_READY;
1471 setAlgoState(MISSING_HYP);
1474 TopoDS_Shape shape = _subShape;
1475 algo->SubMeshesToCompute().assign( 1, this );
1476 // check submeshes needed
1477 if (_father->HasShapeToMesh() ) {
1478 bool subComputed = false, subFailed = false;
1479 if (!algo->OnlyUnaryInput()) {
1480 if ( event == COMPUTE /*&&
1481 ( algo->NeedDiscreteBoundary() || algo->SupportSubmeshes() )*/)
1482 shape = getCollection( gen, algo, subComputed, subFailed, algo->SubMeshesToCompute());
1484 subComputed = SubMeshesComputed( & subFailed );
1487 subComputed = SubMeshesComputed();
1489 ret = ( algo->NeedDiscreteBoundary() ? subComputed :
1490 algo->SupportSubmeshes() ? !subFailed :
1491 ( !subComputed || _father->IsNotConformAllowed() ));
1494 _computeState = FAILED_TO_COMPUTE;
1495 if ( !algo->NeedDiscreteBoundary() && !subFailed )
1497 SMESH_ComputeError::New(COMPERR_BAD_INPUT_MESH,
1498 "Unexpected computed sub-mesh",algo);
1504 // to restore cout that may be redirected by algo
1505 std::streambuf* coutBuffer = std::cout.rdbuf();
1507 //cleanDependants(); for "UseExisting_*D" algos
1508 //removeSubMeshElementsAndNodes();
1509 loadDependentMeshes();
1511 _computeState = FAILED_TO_COMPUTE;
1512 _computeError = SMESH_ComputeError::New(COMPERR_OK,"",algo);
1516 algo->InitComputeError();
1518 MemoryReserve aMemoryReserve;
1519 SMDS_Mesh::CheckMemory();
1520 Kernel_Utils::Localizer loc;
1521 if ( !_father->HasShapeToMesh() ) // no shape
1523 SMESH_MesherHelper helper( *_father );
1524 helper.SetSubShape( shape );
1525 helper.SetElementsOnShape( true );
1526 ret = algo->Compute(*_father, &helper );
1530 ret = algo->Compute((*_father), shape);
1532 // algo can set _computeError of submesh
1533 _computeError = SMESH_ComputeError::Worst( _computeError, algo->GetComputeError() );
1535 catch ( ::SMESH_ComputeError& comperr ) {
1536 cout << " SMESH_ComputeError caught" << endl;
1537 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1538 *_computeError = comperr;
1540 catch ( std::bad_alloc& exc ) {
1541 MESSAGE("std::bad_alloc thrown inside algo->Compute()");
1542 if ( _computeError ) {
1543 _computeError->myName = COMPERR_MEMORY_PB;
1544 //_computeError->myComment = exc.what();
1546 cleanSubMesh( this );
1549 catch ( Standard_OutOfMemory& exc ) {
1550 MESSAGE("Standard_OutOfMemory thrown inside algo->Compute()");
1551 if ( _computeError ) {
1552 _computeError->myName = COMPERR_MEMORY_PB;
1553 //_computeError->myComment = exc.what();
1555 cleanSubMesh( this );
1556 throw std::bad_alloc();
1558 catch (Standard_Failure& ex) {
1559 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1560 _computeError->myName = COMPERR_OCC_EXCEPTION;
1561 _computeError->myComment += ex.DynamicType()->Name();
1562 if ( ex.GetMessageString() && strlen( ex.GetMessageString() )) {
1563 _computeError->myComment += ": ";
1564 _computeError->myComment += ex.GetMessageString();
1567 catch ( SALOME_Exception& S_ex ) {
1568 const int skipSalomeShift = 7; /* to skip "Salome " of
1569 "Salome Exception" prefix returned
1570 by SALOME_Exception::what() */
1571 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1572 _computeError->myName = COMPERR_SLM_EXCEPTION;
1573 _computeError->myComment = S_ex.what() + skipSalomeShift;
1575 catch ( std::exception& exc ) {
1576 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1577 _computeError->myName = COMPERR_STD_EXCEPTION;
1578 _computeError->myComment = exc.what();
1581 if ( _computeError )
1582 _computeError->myName = COMPERR_EXCEPTION;
1586 std::cout.rdbuf( coutBuffer ); // restore cout that could be redirected by algo
1588 // check if an error reported on any sub-shape
1589 bool isComputeErrorSet = !checkComputeError( algo, ret, shape );
1590 if ( isComputeErrorSet )
1592 // check if anything was built
1593 TopExp_Explorer subS(shape, _subShape.ShapeType());
1596 for (; ret && subS.More(); subS.Next())
1597 if ( !_father->GetSubMesh( subS.Current() )->IsMeshComputed() &&
1598 ( _subShape.ShapeType() != TopAbs_EDGE ||
1599 !algo->isDegenerated( TopoDS::Edge( subS.Current() ))))
1602 // Set _computeError
1603 if (!ret && !isComputeErrorSet)
1605 for (subS.ReInit(); subS.More(); subS.Next())
1607 SMESH_subMesh* sm = _father->GetSubMesh( subS.Current() );
1608 if ( !sm->IsMeshComputed() )
1610 if ( !sm->_computeError )
1611 sm->_computeError = SMESH_ComputeError::New();
1612 if ( sm->_computeError->IsOK() )
1613 sm->_computeError->myName = COMPERR_ALGO_FAILED;
1614 sm->_computeState = FAILED_TO_COMPUTE;
1615 sm->_computeError->myAlgo = algo;
1619 if (ret && _computeError && _computeError->myName != COMPERR_WARNING )
1621 _computeError.reset();
1624 // send event SUBMESH_COMPUTED
1626 if ( !algo->NeedDiscreteBoundary() )
1627 // send SUBMESH_COMPUTED to dependants of all sub-meshes of shape
1628 for (subS.ReInit(); subS.More(); subS.Next())
1630 SMESH_subMesh* sm = _father->GetSubMesh( subS.Current() );
1631 SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(false,false);
1632 while ( smIt->more() ) {
1634 if ( sm->GetSubShape().ShapeType() == TopAbs_VERTEX )
1635 sm->updateDependantsState( SUBMESH_COMPUTED );
1641 updateDependantsState( SUBMESH_COMPUTED );
1645 case COMPUTE_CANCELED: // nothing to do
1649 removeSubMeshElementsAndNodes();
1650 _computeState = NOT_READY;
1654 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1656 _computeState = READY_TO_COMPUTE;
1658 setAlgoState(MISSING_HYP);
1661 case SUBMESH_COMPUTED: // nothing to do
1663 case SUBMESH_RESTORED:
1664 // check if a mesh is already computed that may
1665 // happen after retrieval from a file
1666 ComputeStateEngine( CHECK_COMPUTE_STATE );
1667 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1669 if (algo) algo->SubmeshRestored( this );
1671 case MESH_ENTITY_REMOVED:
1673 case SUBMESH_LOADED:
1674 loadDependentMeshes();
1675 ComputeSubMeshStateEngine( SUBMESH_LOADED );
1677 case CHECK_COMPUTE_STATE:
1678 if ( IsMeshComputed() )
1679 _computeState = COMPUTE_OK;
1680 else if ( _computeError && _computeError->IsKO() )
1681 _computeState = FAILED_TO_COMPUTE;
1689 // ----------------------------------------------------------------------
1694 case MODIF_ALGO_STATE:
1695 ComputeStateEngine( CLEAN );
1697 if (algo && !algo->NeedDiscreteBoundary())
1698 cleanDependsOn( algo ); // clean sub-meshes with event CLEAN
1700 case COMPUTE: // nothing to do
1702 case COMPUTE_CANCELED: // nothing to do
1705 cleanDependants(); // clean sub-meshes, dependant on this one, with event CLEAN
1706 removeSubMeshElementsAndNodes();
1707 _computeState = NOT_READY;
1708 if ( _algoState == HYP_OK )
1709 _computeState = READY_TO_COMPUTE;
1711 case SUBMESH_COMPUTED: // nothing to do
1713 case SUBMESH_RESTORED:
1714 ComputeStateEngine( CHECK_COMPUTE_STATE );
1715 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1717 if (algo) algo->SubmeshRestored( this );
1719 case MESH_ENTITY_REMOVED:
1720 updateDependantsState ( CHECK_COMPUTE_STATE );
1721 ComputeStateEngine ( CHECK_COMPUTE_STATE );
1722 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1724 case CHECK_COMPUTE_STATE:
1725 if ( !IsMeshComputed() ) {
1726 if (_algoState == HYP_OK)
1727 _computeState = READY_TO_COMPUTE;
1729 _computeState = NOT_READY;
1732 case SUBMESH_LOADED:
1733 // already treated event, thanks to which _computeState == COMPUTE_OK
1741 // ----------------------------------------------------------------------
1743 case FAILED_TO_COMPUTE:
1746 case MODIF_ALGO_STATE:
1748 ComputeStateEngine( CLEAN );
1750 if (algo && !algo->NeedDiscreteBoundary())
1751 cleanDependsOn( algo ); // clean sub-meshes with event CLEAN
1752 if (_algoState == HYP_OK)
1753 _computeState = READY_TO_COMPUTE;
1755 _computeState = NOT_READY;
1757 case COMPUTE: // nothing to do
1758 case COMPUTE_SUBMESH:
1760 case COMPUTE_CANCELED:
1763 algo->CancelCompute();
1767 cleanDependants(); // submeshes dependent on me should be cleaned as well
1768 removeSubMeshElementsAndNodes();
1770 case SUBMESH_COMPUTED: // allow retry compute
1771 if ( IsEmpty() ) // 23061
1773 if (_algoState == HYP_OK)
1774 _computeState = READY_TO_COMPUTE;
1776 _computeState = NOT_READY;
1779 case SUBMESH_RESTORED:
1780 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1782 case MESH_ENTITY_REMOVED:
1784 case CHECK_COMPUTE_STATE:
1785 if ( IsMeshComputed() )
1786 _computeState = COMPUTE_OK;
1788 if (_algoState == HYP_OK)
1789 _computeState = READY_TO_COMPUTE;
1791 _computeState = NOT_READY;
1793 // case SUBMESH_LOADED:
1801 // ----------------------------------------------------------------------
1807 notifyListenersOnEvent( event, COMPUTE_EVENT );
1813 //=============================================================================
1817 //=============================================================================
1819 bool SMESH_subMesh::Evaluate(MapShapeNbElems& aResMap)
1821 _computeError.reset();
1825 if (_subShape.ShapeType() == TopAbs_VERTEX) {
1826 vector<int> aVec(SMDSEntity_Last,0);
1827 aVec[SMDSEntity_Node] = 1;
1828 aResMap.insert(make_pair(this,aVec));
1832 //SMESH_Gen *gen = _father->GetGen();
1833 SMESH_Algo *algo = 0;
1834 SMESH_Hypothesis::Hypothesis_Status hyp_status;
1837 if( algo && !aResMap.count( this ))
1839 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1840 if (!ret) return false;
1842 if (_father->HasShapeToMesh() && algo->NeedDiscreteBoundary() )
1844 // check submeshes needed
1845 bool subMeshEvaluated = true;
1846 int dimToCheck = SMESH_Gen::GetShapeDim( _subShape ) - 1;
1847 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,/*complexShapeFirst=*/true);
1848 while ( smIt->more() && subMeshEvaluated )
1850 SMESH_subMesh* sm = smIt->next();
1851 int dim = SMESH_Gen::GetShapeDim( sm->GetSubShape() );
1852 if (dim < dimToCheck) break; // the rest subMeshes are all of less dimension
1853 const vector<int> & nbs = aResMap[ sm ];
1854 subMeshEvaluated = (std::accumulate( nbs.begin(), nbs.end(), 0 ) > 0 );
1856 if ( !subMeshEvaluated )
1859 _computeError = SMESH_ComputeError::New(COMPERR_OK,"",algo);
1861 if ( IsMeshComputed() )
1863 vector<int> & nbEntities = aResMap[ this ];
1864 nbEntities.resize( SMDSEntity_Last, 0 );
1865 if ( SMESHDS_SubMesh* sm = GetSubMeshDS() )
1867 nbEntities[ SMDSEntity_Node ] = sm->NbNodes();
1868 SMDS_ElemIteratorPtr elemIt = sm->GetElements();
1869 while ( elemIt->more() )
1870 nbEntities[ elemIt->next()->GetEntityType() ]++;
1875 ret = algo->Evaluate((*_father), _subShape, aResMap);
1877 aResMap.insert( make_pair( this,vector<int>(0)));
1884 //=======================================================================
1886 * \brief Update compute_state by _computeError and send proper events to
1887 * dependent submeshes
1888 * \retval bool - true if _computeError is NOT set
1890 //=======================================================================
1892 bool SMESH_subMesh::checkComputeError(SMESH_Algo* theAlgo,
1893 const bool theComputeOK,
1894 const TopoDS_Shape& theShape)
1896 bool noErrors = true;
1898 if ( !theShape.IsNull() )
1900 // Check state of submeshes
1901 if ( !theAlgo->NeedDiscreteBoundary())
1903 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1904 while ( smIt->more() )
1905 if ( !smIt->next()->checkComputeError( theAlgo, theComputeOK ))
1909 // Check state of neighbours
1910 if ( !theAlgo->OnlyUnaryInput() &&
1911 theShape.ShapeType() == TopAbs_COMPOUND &&
1912 !theShape.IsSame( _subShape ))
1914 for (TopoDS_Iterator subIt( theShape ); subIt.More(); subIt.Next()) {
1915 SMESH_subMesh* sm = _father->GetSubMesh( subIt.Value() );
1917 if ( !sm->checkComputeError( theAlgo, theComputeOK, sm->GetSubShape() ))
1919 updateDependantsState( SUBMESH_COMPUTED ); // send event SUBMESH_COMPUTED
1926 // Set my _computeState
1928 if ( !_computeError || _computeError->IsOK() )
1930 // no error description is set to this sub-mesh, check if any mesh is computed
1931 _computeState = IsMeshComputed() ? COMPUTE_OK : FAILED_TO_COMPUTE;
1932 if ( _computeState != COMPUTE_OK )
1934 if ( _subShape.ShapeType() == TopAbs_EDGE &&
1935 SMESH_Algo::isDegenerated( TopoDS::Edge( _subShape )) )
1936 _computeState = COMPUTE_OK;
1937 else if ( theComputeOK )
1938 _computeError = SMESH_ComputeError::New(COMPERR_NO_MESH_ON_SHAPE,"",theAlgo);
1942 if ( _computeError && !_computeError->IsOK() )
1944 if ( !_computeError->myAlgo )
1945 _computeError->myAlgo = theAlgo;
1949 text << theAlgo->GetName() << " failed on sub-shape #" << _Id << " with error ";
1950 if (_computeError->IsCommon() )
1951 text << _computeError->CommonName();
1953 text << _computeError->myName;
1954 if ( _computeError->myComment.size() > 0 )
1955 text << " \"" << _computeError->myComment << "\"";
1959 _computeState = _computeError->IsKO() ? FAILED_TO_COMPUTE : COMPUTE_OK;
1967 //=======================================================================
1968 //function : updateSubMeshState
1970 //=======================================================================
1972 void SMESH_subMesh::updateSubMeshState(const compute_state theState)
1974 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1975 while ( smIt->more() )
1976 smIt->next()->_computeState = theState;
1979 //=======================================================================
1980 //function : ComputeSubMeshStateEngine
1982 //=======================================================================
1984 void SMESH_subMesh::ComputeSubMeshStateEngine(int event, const bool includeSelf)
1986 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(includeSelf,false);
1987 while ( smIt->more() )
1988 smIt->next()->ComputeStateEngine(event);
1991 //=======================================================================
1992 //function : updateDependantsState
1994 //=======================================================================
1996 void SMESH_subMesh::updateDependantsState(const compute_event theEvent)
1998 const std::vector< SMESH_subMesh * > & ancestors = GetAncestors();
1999 for ( size_t iA = 0; iA < ancestors.size(); ++iA )
2001 ancestors[ iA ]->ComputeStateEngine( theEvent );
2005 //=======================================================================
2006 //function : cleanDependants
2008 //=======================================================================
2010 void SMESH_subMesh::cleanDependants()
2012 int dimToClean = SMESH_Gen::GetShapeDim( _subShape ) + 1;
2014 const std::vector< SMESH_subMesh * > & ancestors = GetAncestors();
2015 for ( size_t iA = 0; iA < ancestors.size(); ++iA )
2017 const TopoDS_Shape& ancestor = ancestors[ iA ]->GetSubShape();
2018 if ( SMESH_Gen::GetShapeDim( ancestor ) == dimToClean )
2020 // PAL8021. do not go upper than SOLID, else ComputeStateEngine(CLEAN)
2021 // will erase mesh on other shapes in a compound
2022 if ( ancestor.ShapeType() >= TopAbs_SOLID &&
2023 !ancestors[ iA ]->IsEmpty() ) // prevent infinite CLEAN via event lesteners
2024 ancestors[ iA ]->ComputeStateEngine(CLEAN);
2029 //=======================================================================
2030 //function : removeSubMeshElementsAndNodes
2032 //=======================================================================
2034 void SMESH_subMesh::removeSubMeshElementsAndNodes()
2036 cleanSubMesh( this );
2038 // algo may bind a submesh not to _subShape, eg 3D algo
2039 // sets nodes on SHELL while _subShape may be SOLID
2041 int dim = SMESH_Gen::GetShapeDim( _subShape );
2042 int type = _subShape.ShapeType() + 1;
2043 for ( ; type <= TopAbs_EDGE; type++) {
2044 if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
2046 TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
2047 for ( ; exp.More(); exp.Next() )
2048 cleanSubMesh( _father->GetSubMeshContaining( exp.Current() ));
2055 //=======================================================================
2056 //function : getCollection
2057 //purpose : return a shape containing all sub-shapes of the MainShape that can be
2058 // meshed at once along with _subShape
2059 //=======================================================================
2061 TopoDS_Shape SMESH_subMesh::getCollection(SMESH_Gen * theGen,
2062 SMESH_Algo* theAlgo,
2063 bool & theSubComputed,
2064 bool & theSubFailed,
2065 std::vector<SMESH_subMesh*>& theSubs)
2067 theSubComputed = SubMeshesComputed( & theSubFailed );
2069 TopoDS_Shape mainShape = _father->GetMeshDS()->ShapeToMesh();
2071 if ( mainShape.IsSame( _subShape ))
2074 const bool skipAuxHyps = false;
2075 list<const SMESHDS_Hypothesis*> aUsedHyp =
2076 theAlgo->GetUsedHypothesis( *_father, _subShape, skipAuxHyps ); // copy
2078 // put in a compound all shapes with the same hypothesis assigned
2079 // and a good ComputeState
2081 TopoDS_Compound aCompound;
2082 BRep_Builder aBuilder;
2083 aBuilder.MakeCompound( aCompound );
2087 SMESH_subMeshIteratorPtr smIt = _father->GetSubMesh( mainShape )->getDependsOnIterator(false);
2088 while ( smIt->more() )
2090 SMESH_subMesh* subMesh = smIt->next();
2091 const TopoDS_Shape& S = subMesh->_subShape;
2092 if ( S.ShapeType() != this->_subShape.ShapeType() )
2094 theSubs.push_back( subMesh );
2095 if ( subMesh == this )
2097 aBuilder.Add( aCompound, S );
2099 else if ( subMesh->GetComputeState() == READY_TO_COMPUTE )
2101 SMESH_Algo* anAlgo = subMesh->GetAlgo();
2102 if (( anAlgo->IsSameName( *theAlgo )) && // same algo
2103 ( anAlgo->GetUsedHypothesis( *_father, S, skipAuxHyps ) == aUsedHyp )) // same hyps
2105 aBuilder.Add( aCompound, S );
2106 if ( !subMesh->SubMeshesComputed() )
2107 theSubComputed = false;
2115 //=======================================================================
2116 //function : getSimilarAttached
2117 //purpose : return a hypothesis attached to theShape.
2118 // If theHyp is provided, similar but not same hypotheses
2119 // is returned; else only applicable ones having theHypType
2121 //=======================================================================
2123 const SMESH_Hypothesis* SMESH_subMesh::getSimilarAttached(const TopoDS_Shape& theShape,
2124 const SMESH_Hypothesis * theHyp,
2125 const int theHypType)
2127 SMESH_HypoFilter hypoKind;
2128 hypoKind.Init( hypoKind.HasType( theHyp ? theHyp->GetType() : theHypType ));
2130 hypoKind.And ( hypoKind.HasDim( theHyp->GetDim() ));
2131 hypoKind.AndNot( hypoKind.Is( theHyp ));
2132 if ( theHyp->IsAuxiliary() )
2133 hypoKind.And( hypoKind.HasName( theHyp->GetName() ));
2135 hypoKind.AndNot( hypoKind.IsAuxiliary());
2138 hypoKind.And( hypoKind.IsApplicableTo( theShape ));
2141 return _father->GetHypothesis( theShape, hypoKind, false );
2144 //=======================================================================
2145 //function : CheckConcurentHypothesis
2146 //purpose : check if there are several applicable hypothesis attached to
2148 //=======================================================================
2150 SMESH_Hypothesis::Hypothesis_Status
2151 SMESH_subMesh::CheckConcurentHypothesis (const int theHypType)
2153 MESSAGE ("SMESH_subMesh::CheckConcurentHypothesis");
2155 // is there local hypothesis on me?
2156 if ( getSimilarAttached( _subShape, 0, theHypType ) )
2157 return SMESH_Hypothesis::HYP_OK;
2160 TopoDS_Shape aPrevWithHyp;
2161 const SMESH_Hypothesis* aPrevHyp = 0;
2162 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
2163 for (; it.More(); it.Next())
2165 const TopoDS_Shape& ancestor = it.Value();
2166 const SMESH_Hypothesis* hyp = getSimilarAttached( ancestor, 0, theHypType );
2169 if ( aPrevWithHyp.IsNull() || aPrevWithHyp.IsSame( ancestor ))
2171 aPrevWithHyp = ancestor;
2174 else if ( aPrevWithHyp.ShapeType() == ancestor.ShapeType() && aPrevHyp != hyp )
2175 return SMESH_Hypothesis::HYP_CONCURENT;
2177 return SMESH_Hypothesis::HYP_OK;
2180 return SMESH_Hypothesis::HYP_OK;
2183 //================================================================================
2185 * \brief Constructor of OwnListenerData
2187 //================================================================================
2189 SMESH_subMesh::OwnListenerData::OwnListenerData( SMESH_subMesh* sm, EventListener* el):
2191 myMeshID( sm ? sm->GetFather()->GetId() : -1 ),
2192 mySubMeshID( sm ? sm->GetId() : -1 ),
2197 //================================================================================
2199 * \brief Sets an event listener and its data to a submesh
2200 * \param listener - the listener to store
2201 * \param data - the listener data to store
2202 * \param where - the submesh to store the listener and it's data
2204 * It remembers the submesh where it puts the listener in order to delete
2205 * them when HYP_OK algo_state is lost
2206 * After being set, event listener is notified on each event of where submesh.
2208 //================================================================================
2210 void SMESH_subMesh::SetEventListener(EventListener* listener,
2211 EventListenerData* data,
2212 SMESH_subMesh* where)
2214 if ( listener && where ) {
2215 where->setEventListener( listener, data );
2216 _ownListeners.push_back( OwnListenerData( where, listener ));
2220 //================================================================================
2222 * \brief Sets an event listener and its data to a submesh
2223 * \param listener - the listener to store
2224 * \param data - the listener data to store
2226 * After being set, event listener is notified on each event of a submesh.
2228 //================================================================================
2230 void SMESH_subMesh::setEventListener(EventListener* listener,
2231 EventListenerData* data)
2233 map< EventListener*, EventListenerData* >::iterator l_d =
2234 _eventListeners.find( listener );
2235 if ( l_d != _eventListeners.end() ) {
2236 EventListenerData* curData = l_d->second;
2237 if ( curData && curData != data && curData->IsDeletable() )
2243 for ( l_d = _eventListeners.begin(); l_d != _eventListeners.end(); ++l_d )
2244 if ( listener->GetName() == l_d->first->GetName() )
2246 EventListenerData* curData = l_d->second;
2247 if ( curData && curData != data && curData->IsDeletable() )
2249 if ( l_d->first != listener && l_d->first->IsDeletable() )
2251 _eventListeners.erase( l_d );
2254 _eventListeners.insert( make_pair( listener, data ));
2258 //================================================================================
2260 * \brief Return an event listener data
2261 * \param listener - the listener whose data is
2262 * \param myOwn - if \c true, returns a listener set by this sub-mesh,
2263 * else returns a listener listening to events of this sub-mesh
2264 * \retval EventListenerData* - found data, maybe NULL
2266 //================================================================================
2268 EventListenerData* SMESH_subMesh::GetEventListenerData(EventListener* listener,
2269 const bool myOwn) const
2273 list< OwnListenerData >::const_iterator d;
2274 for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
2276 if ( d->myListener == listener && _father->MeshExists( d->myMeshID ))
2277 return d->mySubMesh->GetEventListenerData( listener, !myOwn );
2282 map< EventListener*, EventListenerData* >::const_iterator l_d =
2283 _eventListeners.find( listener );
2284 if ( l_d != _eventListeners.end() )
2290 //================================================================================
2292 * \brief Return an event listener data
2293 * \param listenerName - the listener name
2294 * \param myOwn - if \c true, returns a listener set by this sub-mesh,
2295 * else returns a listener listening to events of this sub-mesh
2296 * \retval EventListenerData* - found data, maybe NULL
2298 //================================================================================
2300 EventListenerData* SMESH_subMesh::GetEventListenerData(const string& listenerName,
2301 const bool myOwn) const
2305 list< OwnListenerData >::const_iterator d;
2306 for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
2308 if ( _father->MeshExists( d->myMeshID ) && listenerName == d->myListener->GetName())
2309 return d->mySubMesh->GetEventListenerData( listenerName, !myOwn );
2314 map< EventListener*, EventListenerData* >::const_iterator l_d = _eventListeners.begin();
2315 for ( ; l_d != _eventListeners.end(); ++l_d )
2316 if ( listenerName == l_d->first->GetName() )
2322 //================================================================================
2324 * \brief Notify stored event listeners on the occured event
2325 * \param event - algo_event or compute_event itself
2326 * \param eventType - algo_event or compute_event
2327 * \param hyp - hypothesis, if eventType is algo_event
2329 //================================================================================
2331 void SMESH_subMesh::notifyListenersOnEvent( const int event,
2332 const event_type eventType,
2333 SMESH_Hypothesis* hyp)
2335 list< pair< EventListener*, EventListenerData* > > eventListeners( _eventListeners.begin(),
2336 _eventListeners.end());
2337 list< pair< EventListener*, EventListenerData* > >::iterator l_d = eventListeners.begin();
2338 for ( ; l_d != eventListeners.end(); ++l_d )
2340 std::pair< EventListener*, EventListenerData* > li_da = *l_d;
2341 if ( !_eventListeners.count( li_da.first )) continue;
2343 if ( li_da.first->myBusySM.insert( this ).second )
2345 const bool isDeletable = li_da.first->IsDeletable();
2347 li_da.first->ProcessEvent( event, eventType, this, li_da.second, hyp );
2349 if ( !isDeletable || _eventListeners.count( li_da.first ))
2350 li_da.first->myBusySM.erase( this ); // a listener is hopefully not dead
2355 //================================================================================
2357 * \brief Unregister the listener and delete listener's data
2358 * \param listener - the event listener
2360 //================================================================================
2362 void SMESH_subMesh::DeleteEventListener(EventListener* listener)
2364 map< EventListener*, EventListenerData* >::iterator l_d =
2365 _eventListeners.find( listener );
2366 if ( l_d != _eventListeners.end() && l_d->first )
2368 if ( l_d->second && l_d->second->IsDeletable() )
2372 l_d->first->myBusySM.erase( this );
2373 if ( l_d->first->IsDeletable() )
2375 l_d->first->BeforeDelete( this, l_d->second );
2378 _eventListeners.erase( l_d );
2382 //================================================================================
2384 * \brief Delete event listeners depending on algo of this submesh
2386 //================================================================================
2388 void SMESH_subMesh::deleteOwnListeners()
2390 list< OwnListenerData >::iterator d;
2391 for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
2393 SMESH_Mesh* mesh = _father->FindMesh( d->myMeshID );
2394 if ( !mesh || !mesh->GetSubMeshContaining( d->mySubMeshID ))
2396 d->mySubMesh->DeleteEventListener( d->myListener );
2398 _ownListeners.clear();
2401 //=======================================================================
2402 //function : loadDependentMeshes
2403 //purpose : loads dependent meshes on SUBMESH_LOADED event
2404 //=======================================================================
2406 void SMESH_subMesh::loadDependentMeshes()
2408 list< OwnListenerData >::iterator d;
2409 for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
2410 if ( _father != d->mySubMesh->_father )
2411 d->mySubMesh->_father->Load();
2413 // map< EventListener*, EventListenerData* >::iterator l_d = _eventListeners.begin();
2414 // for ( ; l_d != _eventListeners.end(); ++l_d )
2415 // if ( l_d->second )
2417 // const list<SMESH_subMesh*>& smList = l_d->second->mySubMeshes;
2418 // list<SMESH_subMesh*>::const_iterator sm = smList.begin();
2419 // for ( ; sm != smList.end(); ++sm )
2420 // if ( _father != (*sm)->_father )
2421 // (*sm)->_father->Load();
2425 //================================================================================
2427 * \brief Do something on a certain event
2428 * \param event - algo_event or compute_event itself
2429 * \param eventType - algo_event or compute_event
2430 * \param subMesh - the submesh where the event occures
2431 * \param data - listener data stored in the subMesh
2432 * \param hyp - hypothesis, if eventType is algo_event
2434 * The base implementation translates CLEAN event to the subMesh
2435 * stored in listener data. Also it sends SUBMESH_COMPUTED event in case of
2436 * successful COMPUTE event.
2438 //================================================================================
2440 void SMESH_subMeshEventListener::ProcessEvent(const int event,
2441 const int eventType,
2442 SMESH_subMesh* subMesh,
2443 EventListenerData* data,
2444 const SMESH_Hypothesis* /*hyp*/)
2446 if ( data && !data->mySubMeshes.empty() &&
2447 eventType == SMESH_subMesh::COMPUTE_EVENT)
2449 ASSERT( data->mySubMeshes.front() != subMesh );
2450 list<SMESH_subMesh*>::iterator smIt = data->mySubMeshes.begin();
2451 list<SMESH_subMesh*>::iterator smEnd = data->mySubMeshes.end();
2453 case SMESH_subMesh::CLEAN:
2454 for ( ; smIt != smEnd; ++ smIt)
2455 (*smIt)->ComputeStateEngine( event );
2457 case SMESH_subMesh::COMPUTE:
2458 case SMESH_subMesh::COMPUTE_SUBMESH:
2459 if ( subMesh->GetComputeState() == SMESH_subMesh::COMPUTE_OK )
2460 for ( ; smIt != smEnd; ++ smIt)
2461 (*smIt)->ComputeStateEngine( SMESH_subMesh::SUBMESH_COMPUTED );
2470 //================================================================================
2472 * \brief Iterator over submeshes and optionally prepended or appended one
2474 //================================================================================
2476 struct _Iterator : public SMDS_Iterator<SMESH_subMesh*>
2478 _Iterator(SMDS_Iterator<SMESH_subMesh*>* subIt,
2479 SMESH_subMesh* prepend,
2480 SMESH_subMesh* append): myAppend(append), myIt(subIt)
2482 myCur = prepend ? prepend : myIt->more() ? myIt->next() : append;
2483 if ( myCur == append ) append = 0;
2485 /// Return true if and only if there are other object in this iterator
2490 /// Return the current object and step to the next one
2491 virtual SMESH_subMesh* next()
2493 SMESH_subMesh* res = myCur;
2494 if ( myIt->more() ) { myCur = myIt->next(); }
2495 else { myCur = myAppend; myAppend = 0; }
2502 SMESH_subMesh *myAppend, *myCur;
2503 SMDS_Iterator<SMESH_subMesh*> *myIt;
2507 //================================================================================
2509 * \brief Return iterator on the submeshes this one depends on
2510 * \param includeSelf - this submesh to be returned also
2511 * \param reverse - if true, complex shape submeshes go first
2513 //================================================================================
2515 SMESH_subMeshIteratorPtr SMESH_subMesh::getDependsOnIterator(const bool includeSelf,
2516 const bool reverse) const
2518 SMESH_subMesh *me = (SMESH_subMesh*) this;
2519 SMESH_subMesh *prepend=0, *append=0;
2520 if ( includeSelf ) {
2521 if ( reverse ) prepend = me;
2524 typedef map < int, SMESH_subMesh * > TMap;
2527 return SMESH_subMeshIteratorPtr
2528 ( new _Iterator( new SMDS_mapReverseIterator<TMap>( me->DependsOn() ), prepend, append ));
2531 return SMESH_subMeshIteratorPtr
2532 ( new _Iterator( new SMDS_mapIterator<TMap>( me->DependsOn() ), prepend, append ));
2536 //================================================================================
2538 * \brief Returns ancestor sub-meshes. Finds them if not yet found.
2540 //================================================================================
2542 const std::vector< SMESH_subMesh * > & SMESH_subMesh::GetAncestors() const
2544 if ( _ancestors.empty() &&
2545 !_subShape.IsSame( _father->GetShapeToMesh() ))
2547 const TopTools_ListOfShape& ancShapes = _father->GetAncestors( _subShape );
2549 SMESH_subMesh* me = const_cast< SMESH_subMesh* >( this );
2550 me->_ancestors.reserve( ancShapes.Extent() );
2552 TopTools_MapOfShape map;
2554 for ( TopTools_ListIteratorOfListOfShape it( ancShapes ); it.More(); it.Next() )
2555 if ( SMESH_subMesh* sm = _father->GetSubMeshContaining( it.Value() ))
2556 if ( map.Add( it.Value() ))
2557 me->_ancestors.push_back( sm );
2563 //================================================================================
2565 * \brief Clears the vector of ancestor sub-meshes
2567 //================================================================================
2569 void SMESH_subMesh::ClearAncestors()
2574 //================================================================================
2576 * \brief Find common submeshes (based on shared sub-shapes with other
2577 * \param theOther submesh to check
2578 * \param theSetOfCommon set of common submesh
2580 //================================================================================
2582 bool SMESH_subMesh::FindIntersection(const SMESH_subMesh* theOther,
2583 std::set<const SMESH_subMesh*>& theSetOfCommon ) const
2585 size_t oldNb = theSetOfCommon.size();
2587 // check main submeshes
2588 const map <int, SMESH_subMesh*>::const_iterator otherEnd = theOther->_mapDepend.end();
2589 if ( theOther->_mapDepend.find(this->GetId()) != otherEnd )
2590 theSetOfCommon.insert( this );
2591 if ( _mapDepend.find(theOther->GetId()) != _mapDepend.end() )
2592 theSetOfCommon.insert( theOther );
2594 // check common submeshes
2595 map <int, SMESH_subMesh*>::const_iterator mapIt = _mapDepend.begin();
2596 for( ; mapIt != _mapDepend.end(); mapIt++ )
2597 if ( theOther->_mapDepend.find((*mapIt).first) != otherEnd )
2598 theSetOfCommon.insert( (*mapIt).second );
2599 return oldNb < theSetOfCommon.size();