1 // Copyright (C) 2007-2016 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 "SMDS_SetIterator.hxx"
31 #include "SMESHDS_Mesh.hxx"
32 #include "SMESH_Algo.hxx"
33 #include "SMESH_Comment.hxx"
34 #include "SMESH_Gen.hxx"
35 #include "SMESH_HypoFilter.hxx"
36 #include "SMESH_Hypothesis.hxx"
37 #include "SMESH_Mesh.hxx"
38 #include "SMESH_MesherHelper.hxx"
39 #include "SMESH_subMeshEventListener.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 sub-mesh 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 sub-meshes 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 sub-mesh, 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 sub-meshes 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;
307 if ( isFailedToCompute && !(*isFailedToCompute) )
308 *isFailedToCompute = ( sm->GetComputeState() == FAILED_TO_COMPUTE );
310 if ( !isFailedToCompute )
314 return subMeshesComputed;
317 //================================================================================
319 * \brief Return cost of computing this sub-mesh. If hypotheses are not well defined,
321 * \return int - the computation cost in abstract units.
323 //================================================================================
325 int SMESH_subMesh::GetComputeCost() const
327 return _realComputeCost;
330 //================================================================================
332 * \brief Return cost of computing this sub-mesh. The cost depends on the shape type
333 * and number of sub-meshes this one DependsOn().
334 * \return int - the computation cost in abstract units.
336 //================================================================================
338 int SMESH_subMesh::computeCost() const
343 switch ( _subShape.ShapeType() ) {
345 case TopAbs_SHELL: computeCost = 5000; break;
346 case TopAbs_FACE: computeCost = 500; break;
347 case TopAbs_EDGE: computeCost = 2; break;
348 default: computeCost = 1;
350 SMESH_subMeshIteratorPtr childIt = getDependsOnIterator(/*includeSelf=*/false);
351 while ( childIt->more() )
352 computeCost += childIt->next()->computeCost();
354 ((SMESH_subMesh*)this)->_computeCost = computeCost;
359 //=============================================================================
361 * Returns all sub-meshes this one depend on
363 //=============================================================================
365 const map < int, SMESH_subMesh * >& SMESH_subMesh::DependsOn()
367 if ( _dependenceAnalysed || !_father->HasShapeToMesh() )
370 int type = _subShape.ShapeType();
373 case TopAbs_COMPOUND:
375 list< TopoDS_Shape > compounds( 1, _subShape );
376 list< TopoDS_Shape >::iterator comp = compounds.begin();
377 for ( ; comp != compounds.end(); ++comp )
379 for ( TopoDS_Iterator sub( *comp ); sub.More(); sub.Next() )
380 switch ( sub.Value().ShapeType() )
382 case TopAbs_COMPOUND: compounds.push_back( sub.Value() ); break;
383 case TopAbs_COMPSOLID: insertDependence( sub.Value(), TopAbs_SOLID ); break;
384 case TopAbs_SOLID: insertDependence( sub.Value(), TopAbs_SOLID ); break;
385 case TopAbs_SHELL: insertDependence( sub.Value(), TopAbs_FACE ); break;
386 case TopAbs_FACE: insertDependence( sub.Value(), TopAbs_FACE ); break;
387 case TopAbs_WIRE: insertDependence( sub.Value(), TopAbs_EDGE ); break;
388 case TopAbs_EDGE: insertDependence( sub.Value(), TopAbs_EDGE ); break;
389 case TopAbs_VERTEX: insertDependence( sub.Value(), TopAbs_VERTEX ); break;
395 case TopAbs_COMPSOLID: insertDependence( _subShape, TopAbs_SOLID ); break;
396 case TopAbs_SOLID: insertDependence( _subShape, TopAbs_FACE );
397 { /*internal EDGE*/ insertDependence( _subShape, TopAbs_EDGE, TopAbs_WIRE ); break; }
398 case TopAbs_SHELL: insertDependence( _subShape, TopAbs_FACE ); break;
399 case TopAbs_FACE: insertDependence( _subShape, TopAbs_EDGE ); break;
400 case TopAbs_WIRE: insertDependence( _subShape, TopAbs_EDGE ); break;
401 case TopAbs_EDGE: insertDependence( _subShape, TopAbs_VERTEX ); break;
404 _dependenceAnalysed = true;
408 //================================================================================
410 * \brief Return a key for SMESH_subMesh::_mapDepend map
412 //================================================================================
416 int dependsOnMapKey( TopAbs_ShapeEnum type, int shapeID )
418 int ordType = 9 - int(type); // 2 = Vertex, 8 = CompSolid
420 cle += 10000000 * ordType; // sort map by ordType then index
423 int dependsOnMapKey( const SMESH_subMesh* sm )
425 return dependsOnMapKey( sm->GetSubShape().ShapeType(), sm->GetId() );
429 //=============================================================================
431 * Add sub-meshes on sub-shapes of a given type into the dependence map.
433 //=============================================================================
435 void SMESH_subMesh::insertDependence(const TopoDS_Shape aShape,
436 TopAbs_ShapeEnum aSubType,
437 TopAbs_ShapeEnum avoidType)
439 TopExp_Explorer sub( aShape, aSubType, avoidType );
440 for ( ; sub.More(); sub.Next() )
442 SMESH_subMesh *aSubMesh = _father->GetSubMesh( sub.Current() );
443 if ( aSubMesh->GetId() == 0 )
444 continue; // not a sub-shape of the shape to mesh
445 int cle = dependsOnMapKey( aSubMesh );
446 if ( _mapDepend.find( cle ) == _mapDepend.end())
448 _mapDepend[cle] = aSubMesh;
449 const map < int, SMESH_subMesh * > & subMap = aSubMesh->DependsOn();
450 _mapDepend.insert( subMap.begin(), subMap.end() );
455 //================================================================================
457 * \brief Return \c true if \a this sub-mesh depends on \a other
459 //================================================================================
461 bool SMESH_subMesh::DependsOn( const SMESH_subMesh* other ) const
463 return other ? _mapDepend.count( dependsOnMapKey( other )) : false;
466 //================================================================================
468 * \brief Return \c true if \a this sub-mesh depends on a \a shape
470 //================================================================================
472 bool SMESH_subMesh::DependsOn( const int shapeID ) const
474 return DependsOn( _father->GetSubMeshContaining( shapeID ));
477 //=============================================================================
479 * Return a shape of \a this sub-mesh
481 //=============================================================================
483 const TopoDS_Shape & SMESH_subMesh::GetSubShape() const
488 //=======================================================================
489 //function : CanAddHypothesis
490 //purpose : return true if theHypothesis can be attached to me:
491 // its dimention is checked
492 //=======================================================================
494 bool SMESH_subMesh::CanAddHypothesis(const SMESH_Hypothesis* theHypothesis) const
496 int aHypDim = theHypothesis->GetDim();
497 int aShapeDim = SMESH_Gen::GetShapeDim(_subShape);
498 // issue 21106. Forbid 3D mesh on the SHELL
499 // if (aHypDim == 3 && aShapeDim == 3) {
500 // // check case of open shell
501 // //if (_subShape.ShapeType() == TopAbs_SHELL && !_subShape.Closed())
502 // if (_subShape.ShapeType() == TopAbs_SHELL && !BRep_Tool::IsClosed(_subShape))
505 if ( aHypDim <= aShapeDim )
511 //=======================================================================
512 //function : IsApplicableHypotesis
514 //=======================================================================
516 bool SMESH_subMesh::IsApplicableHypotesis(const SMESH_Hypothesis* theHypothesis,
517 const TopAbs_ShapeEnum theShapeType)
519 if ( theHypothesis->GetType() > SMESHDS_Hypothesis::PARAM_ALGO)
522 if ( theHypothesis->GetShapeType() & (1<< theShapeType))
523 // issue 21106. Forbid 3D mesh on the SHELL
524 return !( theHypothesis->GetDim() == 3 && theShapeType == TopAbs_SHELL );
530 switch ( theShapeType ) {
535 return SMESH_Gen::GetShapeDim( theShapeType ) == theHypothesis->GetDim();
538 // Special case for algorithms, building 2D mesh on a whole shell.
539 // Before this fix there was a problem after restoring from study,
540 // because in that case algorithm is assigned before hypothesis
541 // (on shell in problem case) and hypothesis is checked on faces
542 // (because it is 2D), where we have NO_ALGO state.
543 // Now 2D hypothesis is also applicable to shells.
544 return (theHypothesis->GetDim() == 2 || theHypothesis->GetDim() == 3);
547 // case TopAbs_COMPSOLID:
548 // case TopAbs_COMPOUND:
554 //================================================================================
556 * \brief Treats modification of hypotheses definition
557 * \param [in] event - what happens
558 * \param [in] anHyp - a hypothesis
559 * \return SMESH_Hypothesis::Hypothesis_Status - a treatment result.
561 * Optional description of a problematic situation (if any) can be retrieved
562 * via GetComputeError().
564 //================================================================================
566 SMESH_Hypothesis::Hypothesis_Status
567 SMESH_subMesh::AlgoStateEngine(algo_event event, SMESH_Hypothesis * anHyp)
569 // **** les retour des evenement shape sont significatifs
570 // (add ou remove fait ou non)
571 // le retour des evenement father n'indiquent pas que add ou remove fait
573 SMESH_Hypothesis::Hypothesis_Status aux_ret, ret = SMESH_Hypothesis::HYP_OK;
574 if ( _Id == 0 ) return ret; // not a sub-shape of the shape to mesh
576 SMESHDS_Mesh* meshDS =_father->GetMeshDS();
577 SMESH_Algo* algo = 0;
580 if (_subShape.ShapeType() == TopAbs_VERTEX )
582 if ( anHyp->GetDim() != 0) {
583 if (event == ADD_HYP || event == ADD_ALGO)
584 return SMESH_Hypothesis::HYP_BAD_DIM;
586 return SMESH_Hypothesis::HYP_OK;
589 else if ( _algoState == HYP_OK ) {
590 // update default _algoState
591 if ( event != REMOVE_FATHER_ALGO )
593 _algoState = NO_ALGO;
596 _algoState = MISSING_HYP;
597 if ( event == REMOVE_FATHER_HYP ||
598 algo->CheckHypothesis(*_father,_subShape, aux_ret))
605 int oldAlgoState = _algoState;
606 bool modifiedHyp = (event == MODIF_HYP); // if set to true, force event MODIF_ALGO_STATE
607 SMESH_Algo* algoRequiringCleaning = 0;
609 bool isApplicableHyp = IsApplicableHypotesis( anHyp );
611 if (event == ADD_ALGO || event == ADD_FATHER_ALGO)
613 // -------------------------------------------
614 // check if a shape needed by algo is present
615 // -------------------------------------------
616 algo = static_cast< SMESH_Algo* >( anHyp );
617 if ( !_father->HasShapeToMesh() && algo->NeedShape() )
618 return SMESH_Hypothesis::HYP_NEED_SHAPE;
619 // ----------------------
620 // check mesh conformity
621 // ----------------------
622 if (isApplicableHyp && !_father->IsNotConformAllowed() && !IsConform( algo ))
623 return SMESH_Hypothesis::HYP_NOTCONFORM;
625 // check if all-dimensional algo is hidden by other local one
626 if ( event == ADD_ALGO ) {
627 SMESH_HypoFilter filter( SMESH_HypoFilter::HasType( algo->GetType() ));
628 filter.Or( SMESH_HypoFilter::HasType( algo->GetType()+1 ));
629 filter.Or( SMESH_HypoFilter::HasType( algo->GetType()+2 ));
630 if ( SMESH_Algo * curAlgo = (SMESH_Algo*)_father->GetHypothesis( this, filter, true ))
631 if ( !curAlgo->NeedDiscreteBoundary() )
632 algoRequiringCleaning = curAlgo;
636 // ----------------------------------
637 // add a hypothesis to DS if possible
638 // ----------------------------------
639 if (event == ADD_HYP || event == ADD_ALGO)
641 if ( ! CanAddHypothesis( anHyp )) // check dimension
642 return SMESH_Hypothesis::HYP_BAD_DIM;
644 if ( !anHyp->IsAuxiliary() && getSimilarAttached( _subShape, anHyp ) )
645 return SMESH_Hypothesis::HYP_ALREADY_EXIST;
647 if ( !meshDS->AddHypothesis(_subShape, anHyp))
648 return SMESH_Hypothesis::HYP_ALREADY_EXIST;
651 // --------------------------
652 // remove a hypothesis from DS
653 // --------------------------
654 if (event == REMOVE_HYP || event == REMOVE_ALGO)
656 if (!meshDS->RemoveHypothesis(_subShape, anHyp))
657 return SMESH_Hypothesis::HYP_OK; // nothing changes
659 if (event == REMOVE_ALGO)
661 algo = dynamic_cast<SMESH_Algo*> (anHyp);
662 if (!algo->NeedDiscreteBoundary())
663 algoRequiringCleaning = algo;
667 // ------------------
668 // analyse algo state
669 // ------------------
670 if (!isApplicableHyp)
671 return ret; // not applicable hypotheses do not change algo state
673 if (( algo = GetAlgo()))
674 algo->InitComputeError();
679 // ----------------------------------------------------------------------
688 if (algo->CheckHypothesis((*_father),_subShape, aux_ret))
689 setAlgoState(HYP_OK);
690 else if ( algo->IsStatusFatal( aux_ret )) {
691 meshDS->RemoveHypothesis(_subShape, anHyp);
695 setAlgoState(MISSING_HYP);
702 case ADD_FATHER_ALGO: { // Algo just added in father
705 if ( algo == anHyp ) {
706 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret))
707 setAlgoState(HYP_OK);
709 setAlgoState(MISSING_HYP);
713 case REMOVE_FATHER_HYP:
715 case REMOVE_FATHER_ALGO: {
719 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
720 setAlgoState(HYP_OK);
722 setAlgoState(MISSING_HYP);
726 case MODIF_HYP: break;
733 // ----------------------------------------------------------------------
741 if ( algo->CheckHypothesis((*_father),_subShape, ret ))
742 setAlgoState(HYP_OK);
743 if (SMESH_Hypothesis::IsStatusFatal( ret ))
744 meshDS->RemoveHypothesis(_subShape, anHyp);
745 else if (!_father->IsUsedHypothesis( anHyp, this ))
747 meshDS->RemoveHypothesis(_subShape, anHyp);
748 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
752 case ADD_ALGO: { //already existing algo : on father ?
755 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))// ignore hyp status
756 setAlgoState(HYP_OK);
757 else if ( algo->IsStatusFatal( aux_ret )) {
758 meshDS->RemoveHypothesis(_subShape, anHyp);
762 setAlgoState(MISSING_HYP);
767 case REMOVE_ALGO: { // perhaps a father algo applies ?
769 if (algo == NULL) // no more algo applying on sub-shape...
771 setAlgoState(NO_ALGO);
775 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
776 setAlgoState(HYP_OK);
778 setAlgoState(MISSING_HYP);
782 case MODIF_HYP: // assigned hypothesis value may become good
783 case ADD_FATHER_HYP: {
786 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
787 setAlgoState(HYP_OK);
789 setAlgoState(MISSING_HYP);
792 case ADD_FATHER_ALGO: { // new father algo
795 if ( algo == anHyp ) {
796 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
797 setAlgoState(HYP_OK);
799 setAlgoState(MISSING_HYP);
803 case REMOVE_FATHER_HYP: // nothing to do
805 case REMOVE_FATHER_ALGO: {
807 if (algo == NULL) // no more applying algo on father
809 setAlgoState(NO_ALGO);
813 if ( algo->CheckHypothesis((*_father),_subShape , aux_ret ))
814 setAlgoState(HYP_OK);
816 setAlgoState(MISSING_HYP);
826 // ----------------------------------------------------------------------
834 if (!algo->CheckHypothesis((*_father),_subShape, ret ))
836 if ( !SMESH_Hypothesis::IsStatusFatal( ret ))
837 // ret should be fatal: anHyp was not added
838 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
840 else if (!_father->IsUsedHypothesis( anHyp, this ))
841 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
843 if (SMESH_Hypothesis::IsStatusFatal( ret ))
845 MESSAGE("do not add extra hypothesis");
846 meshDS->RemoveHypothesis(_subShape, anHyp);
854 case ADD_ALGO: { //already existing algo : on father ?
856 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
857 // check if algo changes
859 f.Init( SMESH_HypoFilter::IsAlgo() );
860 f.And( SMESH_HypoFilter::IsApplicableTo( _subShape ));
861 f.AndNot( SMESH_HypoFilter::Is( algo ));
862 const SMESH_Hypothesis * prevAlgo = _father->GetHypothesis( this, f, true );
864 string( algo->GetName()) != prevAlgo->GetName())
868 setAlgoState(MISSING_HYP);
874 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
875 setAlgoState(HYP_OK);
877 setAlgoState(MISSING_HYP);
881 case REMOVE_ALGO: { // perhaps a father algo applies ?
883 if (algo == NULL) // no more algo applying on sub-shape...
885 setAlgoState(NO_ALGO);
889 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
890 // check if algo remains
891 if ( anHyp != algo && strcmp( anHyp->GetName(), algo->GetName()) )
895 setAlgoState(MISSING_HYP);
899 case MODIF_HYP: // hypothesis value may become bad
900 case ADD_FATHER_HYP: { // new father hypothesis ?
903 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
905 if (_father->IsUsedHypothesis( anHyp, this )) // new Hyp
909 setAlgoState(MISSING_HYP);
912 case ADD_FATHER_ALGO: {
914 if ( algo == anHyp ) { // a new algo on father
915 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
916 // check if algo changes
918 f.Init( SMESH_HypoFilter::IsAlgo() );
919 f.And( SMESH_HypoFilter::IsApplicableTo( _subShape ));
920 f.AndNot( SMESH_HypoFilter::Is( algo ));
921 const SMESH_Hypothesis* prevAlgo = _father->GetHypothesis( this, f, true );
923 string(algo->GetName()) != string(prevAlgo->GetName()) )
927 setAlgoState(MISSING_HYP);
931 case REMOVE_FATHER_HYP: {
934 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
935 // is there the same local hyp or maybe a new father algo applied?
936 if ( !getSimilarAttached( _subShape, anHyp ) )
940 setAlgoState(MISSING_HYP);
943 case REMOVE_FATHER_ALGO: {
944 // IPAL21346. Edges not removed when Netgen 1d-2d is removed from a SOLID.
945 // CLEAN was not called at event REMOVE_ALGO because the algo is not applicable to SOLID.
946 algo = dynamic_cast<SMESH_Algo*> (anHyp);
947 if (!algo->NeedDiscreteBoundary())
948 algoRequiringCleaning = algo;
950 if (algo == NULL) // no more applying algo on father
952 setAlgoState(NO_ALGO);
956 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
957 // check if algo changes
958 if ( string(algo->GetName()) != string( anHyp->GetName()) )
962 setAlgoState(MISSING_HYP);
972 // ----------------------------------------------------------------------
979 // detect algorithm hiding
981 if ( ret == SMESH_Hypothesis::HYP_OK &&
982 ( event == ADD_ALGO || event == ADD_FATHER_ALGO ) && algo &&
983 algo->GetName() == anHyp->GetName() )
986 SMESH_Gen* gen = _father->GetGen();
987 const std::vector< SMESH_subMesh * > & ancestors = GetAncestors();
988 for ( size_t iA = 0; ( ret == SMESH_Hypothesis::HYP_OK && iA < ancestors.size()); ++iA ) {
989 if ( SMESH_Algo* upperAlgo = ancestors[ iA ]->GetAlgo() )
990 if ( !upperAlgo->NeedDiscreteBoundary() && !upperAlgo->SupportSubmeshes())
991 ret = SMESH_Hypothesis::HYP_HIDDEN_ALGO;
994 if ( ret == SMESH_Hypothesis::HYP_OK &&
995 !algo->NeedDiscreteBoundary() &&
996 !algo->SupportSubmeshes())
998 TopoDS_Shape algoAssignedTo, otherAssignedTo;
999 gen->GetAlgo( this, &algoAssignedTo );
1000 map<int, SMESH_subMesh*>::reverse_iterator i_sm = _mapDepend.rbegin();
1001 for ( ; ( ret == SMESH_Hypothesis::HYP_OK && i_sm != _mapDepend.rend()) ; ++i_sm )
1002 if ( gen->GetAlgo( i_sm->second, &otherAssignedTo ) &&
1003 SMESH_MesherHelper::IsSubShape( /*sub=*/otherAssignedTo, /*main=*/algoAssignedTo ))
1004 ret = SMESH_Hypothesis::HYP_HIDING_ALGO;
1008 if ( _algo ) { // get an error description set by _algo->CheckHypothesis()
1009 _computeError = _algo->GetComputeError();
1010 _algo->InitComputeError();
1013 bool stateChange = ( _algoState != oldAlgoState );
1015 if ( stateChange && _algoState == HYP_OK ) // hyp becomes OK
1016 algo->SetEventListener( this );
1018 if ( event == REMOVE_ALGO || event == REMOVE_FATHER_ALGO )
1021 notifyListenersOnEvent( event, ALGO_EVENT, anHyp );
1023 if ( stateChange && oldAlgoState == HYP_OK ) { // hyp becomes KO
1024 deleteOwnListeners();
1025 SetIsAlwaysComputed( false );
1026 if (_subShape.ShapeType() == TopAbs_VERTEX ) {
1027 // restore default states
1028 _algoState = HYP_OK;
1029 _computeState = READY_TO_COMPUTE;
1033 if ( algoRequiringCleaning ) {
1034 // added or removed algo is all-dimensional
1035 ComputeStateEngine( CLEAN );
1036 cleanDependsOn( algoRequiringCleaning );
1037 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1040 if ( stateChange || modifiedHyp )
1041 ComputeStateEngine( MODIF_ALGO_STATE );
1043 _realComputeCost = ( _algoState == HYP_OK ) ? computeCost() : 0;
1048 //=======================================================================
1049 //function : IsConform
1050 //purpose : check if a conform mesh will be produced by the Algo
1051 //=======================================================================
1053 bool SMESH_subMesh::IsConform(const SMESH_Algo* theAlgo)
1055 // MESSAGE( "SMESH_subMesh::IsConform" );
1056 if ( !theAlgo ) return false;
1058 // Suppose that theAlgo is applicable to _subShape, do not check it here
1059 //if ( !IsApplicableHypotesis( theAlgo )) return false;
1061 // check only algo that doesn't NeedDiscreteBoundary(): because mesh made
1062 // on a sub-shape will be ignored by theAlgo
1063 if ( theAlgo->NeedDiscreteBoundary() ||
1064 !theAlgo->OnlyUnaryInput() ) // all adjacent shapes will be meshed by this algo?
1067 // only local algo is to be checked
1068 //if ( gen->IsGlobalHypothesis( theAlgo, *_father ))
1069 if ( _subShape.ShapeType() == _father->GetMeshDS()->ShapeToMesh().ShapeType() )
1072 // check algo attached to adjacent shapes
1074 // loop on one level down sub-meshes
1075 TopoDS_Iterator itsub( _subShape );
1076 for (; itsub.More(); itsub.Next())
1078 // loop on adjacent subShapes
1079 const std::vector< SMESH_subMesh * > & ancestors = GetAncestors();
1080 for ( size_t iA = 0; iA < ancestors.size(); ++iA )
1082 const TopoDS_Shape& adjacent = ancestors[ iA ]->GetSubShape();
1083 if ( _subShape.IsSame( adjacent )) continue;
1084 if ( adjacent.ShapeType() != _subShape.ShapeType())
1087 // check algo attached to smAdjacent
1088 SMESH_Algo * algo = ancestors[ iA ]->GetAlgo();
1090 !algo->NeedDiscreteBoundary() &&
1091 algo->OnlyUnaryInput())
1092 return false; // NOT CONFORM MESH WILL BE PRODUCED
1099 //=============================================================================
1103 //=============================================================================
1105 void SMESH_subMesh::setAlgoState(algo_state state)
1110 //================================================================================
1112 * \brief Send an event to sub-meshes
1113 * \param [in] event - the event
1114 * \param [in] anHyp - an hypothesis
1115 * \param [in] exitOnFatal - to stop iteration on sub-meshes if a sub-mesh
1116 * reports a fatal result
1117 * \return SMESH_Hypothesis::Hypothesis_Status - the worst result
1119 * Optional description of a problematic situation (if any) can be retrieved
1120 * via GetComputeError().
1122 //================================================================================
1124 SMESH_Hypothesis::Hypothesis_Status
1125 SMESH_subMesh::SubMeshesAlgoStateEngine(algo_event event,
1126 SMESH_Hypothesis * anHyp,
1129 SMESH_Hypothesis::Hypothesis_Status ret = SMESH_Hypothesis::HYP_OK;
1130 //EAP: a wire (dim==1) should notify edges (dim==1)
1131 //EAP: int dim = SMESH_Gen::GetShapeDim(_subShape);
1132 //if (_subShape.ShapeType() < TopAbs_EDGE ) // wire,face etc
1134 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1135 while ( smIt->more() ) {
1136 SMESH_subMesh* sm = smIt->next();
1137 SMESH_Hypothesis::Hypothesis_Status ret2 = sm->AlgoStateEngine(event, anHyp);
1141 _computeError = sm->_computeError;
1142 sm->_computeError.reset();
1143 if ( exitOnFatal && SMESH_Hypothesis::IsStatusFatal( ret ))
1151 //================================================================================
1153 * \brief Remove elements from sub-meshes.
1154 * \param algoRequiringCleaning - an all-dimensional algorithm whose presence
1155 * causes the cleaning.
1157 //================================================================================
1159 void SMESH_subMesh::cleanDependsOn( SMESH_Algo* algoRequiringCleaning/*=0*/ )
1161 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,
1162 /*complexShapeFirst=*/true);
1163 if ( _father->NbNodes() == 0 )
1165 while ( smIt->more() )
1166 smIt->next()->ComputeStateEngine(CHECK_COMPUTE_STATE);
1168 else if ( !algoRequiringCleaning || !algoRequiringCleaning->SupportSubmeshes() )
1170 while ( smIt->more() )
1171 smIt->next()->ComputeStateEngine(CLEAN);
1173 else if ( algoRequiringCleaning && algoRequiringCleaning->SupportSubmeshes() )
1175 // find sub-meshes to keep elements on
1176 set< SMESH_subMesh* > smToKeep;
1177 TopAbs_ShapeEnum prevShapeType = TopAbs_SHAPE;
1178 bool toKeepPrevShapeType = false;
1179 while ( smIt->more() )
1181 SMESH_subMesh* sm = smIt->next();
1182 sm->ComputeStateEngine(CHECK_COMPUTE_STATE);
1183 if ( !sm->IsEmpty() )
1185 const bool sameShapeType = ( prevShapeType == sm->GetSubShape().ShapeType() );
1186 bool keepSubMeshes = ( sameShapeType && toKeepPrevShapeType );
1187 if ( !sameShapeType )
1189 // check if the algo allows presence of global algos of dimension the algo
1190 // can generate it-self;
1191 // always keep a node on VERTEX, as this node can be shared by segments
1192 // lying on EDGEs not shared by the VERTEX of sm, due to MergeNodes (PAL23068)
1193 int shapeDim = SMESH_Gen::GetShapeDim( sm->GetSubShape() );
1194 keepSubMeshes = ( algoRequiringCleaning->NeedLowerHyps( shapeDim ) || shapeDim == 0 );
1195 prevShapeType = sm->GetSubShape().ShapeType();
1196 toKeepPrevShapeType = keepSubMeshes;
1198 if ( !keepSubMeshes )
1200 // look for a local algo used to mesh sm
1201 TopoDS_Shape algoShape = SMESH_MesherHelper::GetShapeOfHypothesis
1202 ( algoRequiringCleaning, _subShape, _father );
1203 SMESH_HypoFilter moreLocalAlgo;
1204 moreLocalAlgo.Init( SMESH_HypoFilter::IsMoreLocalThan( algoShape, *_father ));
1205 moreLocalAlgo.And ( SMESH_HypoFilter::IsAlgo() );
1206 bool localAlgoFound = _father->GetHypothesis( sm->_subShape, moreLocalAlgo, true );
1207 keepSubMeshes = localAlgoFound;
1209 // remember all sub-meshes of sm
1210 if ( keepSubMeshes )
1212 SMESH_subMeshIteratorPtr smIt2 = sm->getDependsOnIterator(true);
1213 while ( smIt2->more() )
1214 smToKeep.insert( smIt2->next() );
1219 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,true);
1220 while ( smIt->more() )
1222 SMESH_subMesh* sm = smIt->next();
1223 if ( !smToKeep.count( sm ))
1224 sm->ComputeStateEngine(CLEAN);
1229 //=============================================================================
1233 //=============================================================================
1235 void SMESH_subMesh::DumpAlgoState(bool isMain)
1239 const map < int, SMESH_subMesh * >&subMeshes = DependsOn();
1241 map < int, SMESH_subMesh * >::const_iterator itsub;
1242 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
1244 SMESH_subMesh *sm = (*itsub).second;
1245 sm->DumpAlgoState(false);
1248 MESSAGE("dim = " << SMESH_Gen::GetShapeDim(_subShape) <<
1249 " type of shape " << _subShape.ShapeType());
1252 case NO_ALGO : MESSAGE(" AlgoState = NO_ALGO"); break;
1253 case MISSING_HYP : MESSAGE(" AlgoState = MISSING_HYP"); break;
1254 case HYP_OK : MESSAGE(" AlgoState = HYP_OK");break;
1256 switch (_computeState)
1258 case NOT_READY : MESSAGE(" ComputeState = NOT_READY");break;
1259 case READY_TO_COMPUTE : MESSAGE(" ComputeState = READY_TO_COMPUTE");break;
1260 case COMPUTE_OK : MESSAGE(" ComputeState = COMPUTE_OK");break;
1261 case FAILED_TO_COMPUTE: MESSAGE(" ComputeState = FAILED_TO_COMPUTE");break;
1265 //================================================================================
1267 * \brief Remove nodes and elements bound to submesh
1268 * \param subMesh - submesh containing nodes and elements
1270 //================================================================================
1272 static void cleanSubMesh( SMESH_subMesh * subMesh )
1275 if (SMESHDS_SubMesh * subMeshDS = subMesh->GetSubMeshDS())
1277 SMESHDS_Mesh * meshDS = subMesh->GetFather()->GetMeshDS();
1278 int nbElems = subMeshDS->NbElements();
1281 // start from elem with max ID to avoid filling the pool of IDs
1283 SMDS_ElemIteratorPtr ite = subMeshDS->GetElements( rev );
1284 const SMDS_MeshElement * lastElem = ite->next();
1285 rev = ( lastElem->GetID() == meshDS->MaxElementID() );
1287 ite = subMeshDS->GetElements( rev );
1289 meshDS->RemoveFreeElement( lastElem, subMeshDS );
1290 while (ite->more()) {
1291 const SMDS_MeshElement * elt = ite->next();
1292 meshDS->RemoveFreeElement( elt, subMeshDS );
1295 int nbNodes = subMeshDS->NbNodes();
1299 SMDS_NodeIteratorPtr itn = subMeshDS->GetNodes( rev );
1300 const SMDS_MeshNode * lastNode = itn->next();
1301 rev = ( lastNode->GetID() == meshDS->MaxNodeID() );
1303 itn = subMeshDS->GetNodes( rev );
1305 meshDS->RemoveNode( lastNode );
1306 while (itn->more()) {
1307 const SMDS_MeshNode * node = itn->next();
1308 if ( node->NbInverseElements() == 0 )
1309 meshDS->RemoveFreeNode( node, subMeshDS );
1310 else // for StdMeshers_CompositeSegment_1D: node in one submesh, edge in another
1311 meshDS->RemoveNode(node);
1319 //=============================================================================
1323 //=============================================================================
1325 bool SMESH_subMesh::ComputeStateEngine(compute_event event)
1328 case MODIF_ALGO_STATE:
1330 case COMPUTE_SUBMESH:
1331 //case COMPUTE_CANCELED:
1333 //case SUBMESH_COMPUTED:
1334 //case SUBMESH_RESTORED:
1335 //case SUBMESH_LOADED:
1336 //case MESH_ENTITY_REMOVED:
1337 //case CHECK_COMPUTE_STATE:
1338 _computeError.reset(); break;
1342 if ( event == CLEAN )
1343 _alwaysComputed = false; // Unset 'true' set by MergeNodes() (issue 0022182)
1345 if (_subShape.ShapeType() == TopAbs_VERTEX)
1347 _computeState = READY_TO_COMPUTE;
1348 SMESHDS_SubMesh* smDS = GetSubMeshDS();
1349 if ( smDS && smDS->NbNodes() )
1351 if ( event == CLEAN ) {
1353 cleanSubMesh( this );
1356 _computeState = COMPUTE_OK;
1358 else if (( event == COMPUTE || event == COMPUTE_SUBMESH )
1359 && !_alwaysComputed )
1361 const TopoDS_Vertex & V = TopoDS::Vertex( _subShape );
1362 gp_Pnt P = BRep_Tool::Pnt(V);
1363 if ( SMDS_MeshNode * n = _father->GetMeshDS()->AddNode(P.X(), P.Y(), P.Z()) ) {
1364 _father->GetMeshDS()->SetNodeOnVertex(n,_Id);
1365 _computeState = COMPUTE_OK;
1368 if ( event == MODIF_ALGO_STATE )
1372 SMESH_Gen *gen = _father->GetGen();
1373 SMESH_Algo *algo = 0;
1375 SMESH_Hypothesis::Hypothesis_Status hyp_status;
1376 //algo_state oldAlgoState = (algo_state) GetAlgoState();
1378 switch (_computeState)
1381 // ----------------------------------------------------------------------
1386 case MODIF_ALGO_STATE:
1388 if (algo && !algo->NeedDiscreteBoundary())
1389 cleanDependsOn( algo ); // clean sub-meshes with event CLEAN
1390 if ( _algoState == HYP_OK )
1391 _computeState = READY_TO_COMPUTE;
1393 case COMPUTE: // nothing to do
1394 case COMPUTE_SUBMESH:
1396 case COMPUTE_CANCELED: // nothing to do
1400 removeSubMeshElementsAndNodes();
1402 case SUBMESH_COMPUTED: // nothing to do
1404 case SUBMESH_RESTORED:
1405 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1407 case MESH_ENTITY_REMOVED:
1409 case SUBMESH_LOADED:
1410 loadDependentMeshes();
1411 ComputeSubMeshStateEngine( SUBMESH_LOADED );
1413 case CHECK_COMPUTE_STATE:
1414 if ( IsMeshComputed() )
1415 _computeState = COMPUTE_OK;
1423 // ----------------------------------------------------------------------
1425 case READY_TO_COMPUTE:
1428 case MODIF_ALGO_STATE:
1429 _computeState = NOT_READY;
1433 if (!algo->NeedDiscreteBoundary())
1434 cleanDependsOn( algo ); // clean sub-meshes with event CLEAN
1435 if ( _algoState == HYP_OK )
1436 _computeState = READY_TO_COMPUTE;
1440 case COMPUTE_SUBMESH:
1444 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1447 MESSAGE("***** verify compute state *****");
1448 _computeState = NOT_READY;
1449 setAlgoState(MISSING_HYP);
1452 TopoDS_Shape shape = _subShape;
1453 algo->SubMeshesToCompute().assign( 1, this );
1454 // check submeshes needed
1455 if (_father->HasShapeToMesh() ) {
1456 bool subComputed = false, subFailed = false;
1457 if (!algo->OnlyUnaryInput()) {
1458 if ( event == COMPUTE /*&&
1459 ( algo->NeedDiscreteBoundary() || algo->SupportSubmeshes() )*/)
1460 shape = getCollection( gen, algo, subComputed, subFailed, algo->SubMeshesToCompute());
1462 subComputed = SubMeshesComputed( & subFailed );
1465 subComputed = SubMeshesComputed();
1467 ret = ( algo->NeedDiscreteBoundary() ? subComputed :
1468 algo->SupportSubmeshes() ? !subFailed :
1469 ( !subComputed || _father->IsNotConformAllowed() ));
1472 _computeState = FAILED_TO_COMPUTE;
1473 if ( !algo->NeedDiscreteBoundary() && !subFailed )
1475 SMESH_ComputeError::New(COMPERR_BAD_INPUT_MESH,
1476 "Unexpected computed sub-mesh",algo);
1482 // to restore cout that may be redirected by algo
1483 std::streambuf* coutBuffer = std::cout.rdbuf();
1485 //cleanDependants(); for "UseExisting_*D" algos
1486 //removeSubMeshElementsAndNodes();
1487 loadDependentMeshes();
1489 _computeState = FAILED_TO_COMPUTE;
1490 _computeError = SMESH_ComputeError::New(COMPERR_OK,"",algo);
1494 algo->InitComputeError();
1496 MemoryReserve aMemoryReserve;
1497 SMDS_Mesh::CheckMemory();
1498 Kernel_Utils::Localizer loc;
1499 if ( !_father->HasShapeToMesh() ) // no shape
1501 SMESH_MesherHelper helper( *_father );
1502 helper.SetSubShape( shape );
1503 helper.SetElementsOnShape( true );
1504 ret = algo->Compute(*_father, &helper );
1508 ret = algo->Compute((*_father), shape);
1510 // algo can set _computeError of submesh
1511 _computeError = SMESH_ComputeError::Worst( _computeError, algo->GetComputeError() );
1513 catch ( ::SMESH_ComputeError& comperr ) {
1514 cout << " SMESH_ComputeError caught" << endl;
1515 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1516 *_computeError = comperr;
1518 catch ( std::bad_alloc& exc ) {
1519 MESSAGE("std::bad_alloc thrown inside algo->Compute()");
1520 if ( _computeError ) {
1521 _computeError->myName = COMPERR_MEMORY_PB;
1523 cleanSubMesh( this );
1526 catch ( Standard_OutOfMemory& exc ) {
1527 MESSAGE("Standard_OutOfMemory thrown inside algo->Compute()");
1528 if ( _computeError ) {
1529 _computeError->myName = COMPERR_MEMORY_PB;
1531 cleanSubMesh( this );
1532 throw std::bad_alloc();
1534 catch (Standard_Failure& ex) {
1535 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1536 _computeError->myName = COMPERR_OCC_EXCEPTION;
1537 _computeError->myComment += ex.DynamicType()->Name();
1538 if ( ex.GetMessageString() && strlen( ex.GetMessageString() )) {
1539 _computeError->myComment += ": ";
1540 _computeError->myComment += ex.GetMessageString();
1543 catch ( SALOME_Exception& S_ex ) {
1544 const int skipSalomeShift = 7; /* to skip "Salome " of
1545 "Salome Exception" prefix returned
1546 by SALOME_Exception::what() */
1547 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1548 _computeError->myName = COMPERR_SLM_EXCEPTION;
1549 _computeError->myComment = S_ex.what() + skipSalomeShift;
1551 catch ( std::exception& exc ) {
1552 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1553 _computeError->myName = COMPERR_STD_EXCEPTION;
1554 _computeError->myComment = exc.what();
1557 if ( _computeError )
1558 _computeError->myName = COMPERR_EXCEPTION;
1562 std::cout.rdbuf( coutBuffer ); // restore cout that could be redirected by algo
1564 // check if an error reported on any sub-shape
1565 bool isComputeErrorSet = !checkComputeError( algo, ret, shape );
1566 if ( isComputeErrorSet )
1568 // check if anything was built
1569 TopExp_Explorer subS(shape, _subShape.ShapeType());
1572 for (; ret && subS.More(); subS.Next())
1573 if ( !_father->GetSubMesh( subS.Current() )->IsMeshComputed() &&
1574 ( _subShape.ShapeType() != TopAbs_EDGE ||
1575 !algo->isDegenerated( TopoDS::Edge( subS.Current() ))))
1578 // Set _computeError
1579 if ( !ret && !isComputeErrorSet )
1581 for ( subS.ReInit(); subS.More(); subS.Next() )
1583 SMESH_subMesh* sm = _father->GetSubMesh( subS.Current() );
1584 if ( !sm->IsMeshComputed() )
1586 if ( !sm->_computeError )
1587 sm->_computeError = SMESH_ComputeError::New();
1588 if ( sm->_computeError->IsOK() )
1589 sm->_computeError->myName = COMPERR_ALGO_FAILED;
1590 sm->_computeState = FAILED_TO_COMPUTE;
1591 sm->_computeError->myAlgo = algo;
1595 if ( ret && _computeError && _computeError->myName != COMPERR_WARNING )
1597 _computeError.reset();
1600 // transform errors into warnings if it is caused by mesh edition (imp 0023068)
1601 if (!ret && _father->GetIsModified() )
1603 for (subS.ReInit(); subS.More(); subS.Next())
1605 SMESH_subMesh* sm = _father->GetSubMesh( subS.Current() );
1606 if ( !sm->IsMeshComputed() && sm->_computeError )
1608 // check if there is a VERTEX w/o nodes
1609 // with READY_TO_COMPUTE state (after MergeNodes())
1610 SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(false,false);
1611 while ( smIt->more() )
1613 SMESH_subMesh * vertSM = smIt->next();
1614 if ( vertSM->_subShape.ShapeType() != TopAbs_VERTEX ) break;
1615 if ( vertSM->GetComputeState() == READY_TO_COMPUTE )
1617 SMESHDS_SubMesh * ds = vertSM->GetSubMeshDS();
1618 if ( !ds || ds->NbNodes() == 0 )
1620 sm->_computeState = READY_TO_COMPUTE;
1621 sm->_computeError->myName = COMPERR_WARNING;
1630 // send event SUBMESH_COMPUTED
1632 if ( !algo->NeedDiscreteBoundary() )
1633 // send SUBMESH_COMPUTED to dependants of all sub-meshes of shape
1634 for (subS.ReInit(); subS.More(); subS.Next())
1636 SMESH_subMesh* sm = _father->GetSubMesh( subS.Current() );
1637 SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(false,false);
1638 while ( smIt->more() ) {
1640 if ( sm->GetSubShape().ShapeType() == TopAbs_VERTEX )
1641 sm->updateDependantsState( SUBMESH_COMPUTED );
1647 updateDependantsState( SUBMESH_COMPUTED );
1651 case COMPUTE_CANCELED: // nothing to do
1655 removeSubMeshElementsAndNodes();
1656 _computeState = NOT_READY;
1660 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1662 _computeState = READY_TO_COMPUTE;
1664 setAlgoState(MISSING_HYP);
1667 case SUBMESH_COMPUTED: // nothing to do
1669 case SUBMESH_RESTORED:
1670 // check if a mesh is already computed that may
1671 // happen after retrieval from a file
1672 ComputeStateEngine( CHECK_COMPUTE_STATE );
1673 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1675 if (algo) algo->SubmeshRestored( this );
1677 case MESH_ENTITY_REMOVED:
1679 case SUBMESH_LOADED:
1680 loadDependentMeshes();
1681 ComputeSubMeshStateEngine( SUBMESH_LOADED );
1683 case CHECK_COMPUTE_STATE:
1684 if ( IsMeshComputed() )
1685 _computeState = COMPUTE_OK;
1686 else if ( _computeError && _computeError->IsKO() )
1687 _computeState = FAILED_TO_COMPUTE;
1695 // ----------------------------------------------------------------------
1700 case MODIF_ALGO_STATE:
1701 ComputeStateEngine( CLEAN );
1703 if (algo && !algo->NeedDiscreteBoundary())
1704 cleanDependsOn( algo ); // clean sub-meshes with event CLEAN
1706 case COMPUTE: // nothing to do
1708 case COMPUTE_CANCELED: // nothing to do
1711 cleanDependants(); // clean sub-meshes, dependant on this one, with event CLEAN
1712 removeSubMeshElementsAndNodes();
1713 _computeState = NOT_READY;
1714 if ( _algoState == HYP_OK )
1715 _computeState = READY_TO_COMPUTE;
1717 case SUBMESH_COMPUTED: // nothing to do
1719 case SUBMESH_RESTORED:
1720 ComputeStateEngine( CHECK_COMPUTE_STATE );
1721 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1723 if (algo) algo->SubmeshRestored( this );
1725 case MESH_ENTITY_REMOVED:
1726 updateDependantsState ( CHECK_COMPUTE_STATE );
1727 ComputeStateEngine ( CHECK_COMPUTE_STATE );
1728 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1730 case CHECK_COMPUTE_STATE:
1731 if ( !IsMeshComputed() ) {
1732 if (_algoState == HYP_OK)
1733 _computeState = READY_TO_COMPUTE;
1735 _computeState = NOT_READY;
1738 case SUBMESH_LOADED:
1739 // already treated event, thanks to which _computeState == COMPUTE_OK
1747 // ----------------------------------------------------------------------
1749 case FAILED_TO_COMPUTE:
1752 case MODIF_ALGO_STATE:
1754 ComputeStateEngine( CLEAN );
1756 if (algo && !algo->NeedDiscreteBoundary())
1757 cleanDependsOn( algo ); // clean sub-meshes with event CLEAN
1758 if (_algoState == HYP_OK)
1759 _computeState = READY_TO_COMPUTE;
1761 _computeState = NOT_READY;
1763 case COMPUTE: // nothing to do
1764 case COMPUTE_SUBMESH:
1766 case COMPUTE_CANCELED:
1769 algo->CancelCompute();
1773 cleanDependants(); // submeshes dependent on me should be cleaned as well
1774 removeSubMeshElementsAndNodes();
1776 case SUBMESH_COMPUTED: // allow retry compute
1777 if ( IsEmpty() ) // 23061
1779 if (_algoState == HYP_OK)
1780 _computeState = READY_TO_COMPUTE;
1782 _computeState = NOT_READY;
1785 case SUBMESH_RESTORED:
1786 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1788 case MESH_ENTITY_REMOVED:
1790 case CHECK_COMPUTE_STATE:
1791 if ( IsMeshComputed() )
1792 _computeState = COMPUTE_OK;
1794 if (_algoState == HYP_OK)
1795 _computeState = READY_TO_COMPUTE;
1797 _computeState = NOT_READY;
1799 // case SUBMESH_LOADED:
1807 // ----------------------------------------------------------------------
1813 notifyListenersOnEvent( event, COMPUTE_EVENT );
1819 //=============================================================================
1823 //=============================================================================
1825 bool SMESH_subMesh::Evaluate(MapShapeNbElems& aResMap)
1827 _computeError.reset();
1831 if (_subShape.ShapeType() == TopAbs_VERTEX) {
1832 vector<int> aVec(SMDSEntity_Last,0);
1833 aVec[SMDSEntity_Node] = 1;
1834 aResMap.insert(make_pair(this,aVec));
1838 //SMESH_Gen *gen = _father->GetGen();
1839 SMESH_Algo *algo = 0;
1840 SMESH_Hypothesis::Hypothesis_Status hyp_status;
1843 if( algo && !aResMap.count( this ))
1845 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1846 if (!ret) return false;
1848 if (_father->HasShapeToMesh() && algo->NeedDiscreteBoundary() )
1850 // check submeshes needed
1851 bool subMeshEvaluated = true;
1852 int dimToCheck = SMESH_Gen::GetShapeDim( _subShape ) - 1;
1853 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,/*complexShapeFirst=*/true);
1854 while ( smIt->more() && subMeshEvaluated )
1856 SMESH_subMesh* sm = smIt->next();
1857 int dim = SMESH_Gen::GetShapeDim( sm->GetSubShape() );
1858 if (dim < dimToCheck) break; // the rest subMeshes are all of less dimension
1859 const vector<int> & nbs = aResMap[ sm ];
1860 subMeshEvaluated = (std::accumulate( nbs.begin(), nbs.end(), 0 ) > 0 );
1862 if ( !subMeshEvaluated )
1865 _computeError = SMESH_ComputeError::New(COMPERR_OK,"",algo);
1867 if ( IsMeshComputed() )
1869 vector<int> & nbEntities = aResMap[ this ];
1870 nbEntities.resize( SMDSEntity_Last, 0 );
1871 if ( SMESHDS_SubMesh* sm = GetSubMeshDS() )
1873 nbEntities[ SMDSEntity_Node ] = sm->NbNodes();
1874 SMDS_ElemIteratorPtr elemIt = sm->GetElements();
1875 while ( elemIt->more() )
1876 nbEntities[ elemIt->next()->GetEntityType() ]++;
1881 ret = algo->Evaluate((*_father), _subShape, aResMap);
1883 aResMap.insert( make_pair( this,vector<int>(0)));
1890 //=======================================================================
1892 * \brief Update compute_state by _computeError and send proper events to
1893 * dependent submeshes
1894 * \retval bool - true if _computeError is NOT set
1896 //=======================================================================
1898 bool SMESH_subMesh::checkComputeError(SMESH_Algo* theAlgo,
1899 const bool theComputeOK,
1900 const TopoDS_Shape& theShape)
1902 bool noErrors = true;
1904 if ( !theShape.IsNull() )
1906 // Check state of submeshes
1907 if ( !theAlgo->NeedDiscreteBoundary())
1909 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1910 while ( smIt->more() )
1911 if ( !smIt->next()->checkComputeError( theAlgo, theComputeOK ))
1915 // Check state of neighbours
1916 if ( !theAlgo->OnlyUnaryInput() &&
1917 theShape.ShapeType() == TopAbs_COMPOUND &&
1918 !theShape.IsSame( _subShape ))
1920 for (TopoDS_Iterator subIt( theShape ); subIt.More(); subIt.Next()) {
1921 SMESH_subMesh* sm = _father->GetSubMesh( subIt.Value() );
1923 if ( !sm->checkComputeError( theAlgo, theComputeOK, sm->GetSubShape() ))
1925 updateDependantsState( SUBMESH_COMPUTED ); // send event SUBMESH_COMPUTED
1932 // Set my _computeState
1934 if ( !_computeError || _computeError->IsOK() )
1936 // no error description is set to this sub-mesh, check if any mesh is computed
1937 _computeState = IsMeshComputed() ? COMPUTE_OK : FAILED_TO_COMPUTE;
1938 if ( _computeState != COMPUTE_OK )
1940 if ( _subShape.ShapeType() == TopAbs_EDGE &&
1941 SMESH_Algo::isDegenerated( TopoDS::Edge( _subShape )) )
1942 _computeState = COMPUTE_OK;
1943 else if ( theComputeOK )
1944 _computeError = SMESH_ComputeError::New(COMPERR_NO_MESH_ON_SHAPE,"",theAlgo);
1948 if ( _computeError && !_computeError->IsOK() )
1950 if ( !_computeError->myAlgo )
1951 _computeError->myAlgo = theAlgo;
1955 text << theAlgo->GetName() << " failed on sub-shape #" << _Id << " with error ";
1956 if (_computeError->IsCommon() )
1957 text << _computeError->CommonName();
1959 text << _computeError->myName;
1960 if ( _computeError->myComment.size() > 0 )
1961 text << " \"" << _computeError->myComment << "\"";
1965 _computeState = _computeError->IsKO() ? FAILED_TO_COMPUTE : COMPUTE_OK;
1973 //=======================================================================
1974 //function : updateSubMeshState
1976 //=======================================================================
1978 void SMESH_subMesh::updateSubMeshState(const compute_state theState)
1980 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1981 while ( smIt->more() )
1982 smIt->next()->_computeState = theState;
1985 //=======================================================================
1986 //function : ComputeSubMeshStateEngine
1988 //=======================================================================
1990 void SMESH_subMesh::ComputeSubMeshStateEngine(compute_event event, const bool includeSelf)
1992 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(includeSelf,false);
1993 while ( smIt->more() )
1994 smIt->next()->ComputeStateEngine(event);
1997 //=======================================================================
1998 //function : updateDependantsState
2000 //=======================================================================
2002 void SMESH_subMesh::updateDependantsState(const compute_event theEvent)
2004 const std::vector< SMESH_subMesh * > & ancestors = GetAncestors();
2005 for ( size_t iA = 0; iA < ancestors.size(); ++iA )
2007 ancestors[ iA ]->ComputeStateEngine( theEvent );
2011 //=======================================================================
2012 //function : cleanDependants
2014 //=======================================================================
2016 void SMESH_subMesh::cleanDependants()
2018 int dimToClean = SMESH_Gen::GetShapeDim( _subShape ) + 1;
2020 const std::vector< SMESH_subMesh * > & ancestors = GetAncestors();
2021 for ( size_t iA = 0; iA < ancestors.size(); ++iA )
2023 const TopoDS_Shape& ancestor = ancestors[ iA ]->GetSubShape();
2024 if ( SMESH_Gen::GetShapeDim( ancestor ) == dimToClean )
2026 // PAL8021. do not go upper than SOLID, else ComputeStateEngine(CLEAN)
2027 // will erase mesh on other shapes in a compound
2028 if ( ancestor.ShapeType() >= TopAbs_SOLID &&
2029 !ancestors[ iA ]->IsEmpty() ) // prevent infinite CLEAN via event lesteners
2030 ancestors[ iA ]->ComputeStateEngine(CLEAN);
2035 //=======================================================================
2036 //function : removeSubMeshElementsAndNodes
2038 //=======================================================================
2040 void SMESH_subMesh::removeSubMeshElementsAndNodes()
2042 cleanSubMesh( this );
2044 // algo may bind a submesh not to _subShape, eg 3D algo
2045 // sets nodes on SHELL while _subShape may be SOLID
2047 int dim = SMESH_Gen::GetShapeDim( _subShape );
2048 int type = _subShape.ShapeType() + 1;
2049 for ( ; type <= TopAbs_EDGE; type++) {
2050 if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
2052 TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
2053 for ( ; exp.More(); exp.Next() )
2054 cleanSubMesh( _father->GetSubMeshContaining( exp.Current() ));
2061 //=======================================================================
2062 //function : getCollection
2063 //purpose : return a shape containing all sub-shapes of the MainShape that can be
2064 // meshed at once along with _subShape
2065 //=======================================================================
2067 TopoDS_Shape SMESH_subMesh::getCollection(SMESH_Gen * theGen,
2068 SMESH_Algo* theAlgo,
2069 bool & theSubComputed,
2070 bool & theSubFailed,
2071 std::vector<SMESH_subMesh*>& theSubs)
2073 theSubComputed = SubMeshesComputed( & theSubFailed );
2075 TopoDS_Shape mainShape = _father->GetMeshDS()->ShapeToMesh();
2077 if ( mainShape.IsSame( _subShape ))
2080 const bool skipAuxHyps = false;
2081 list<const SMESHDS_Hypothesis*> aUsedHyp =
2082 theAlgo->GetUsedHypothesis( *_father, _subShape, skipAuxHyps ); // copy
2084 // put in a compound all shapes with the same hypothesis assigned
2085 // and a good ComputeState
2087 TopoDS_Compound aCompound;
2088 BRep_Builder aBuilder;
2089 aBuilder.MakeCompound( aCompound );
2093 SMESH_subMeshIteratorPtr smIt = _father->GetSubMesh( mainShape )->getDependsOnIterator(false);
2094 while ( smIt->more() )
2096 SMESH_subMesh* subMesh = smIt->next();
2097 const TopoDS_Shape& S = subMesh->_subShape;
2098 if ( S.ShapeType() != this->_subShape.ShapeType() )
2100 if ( subMesh == this )
2102 aBuilder.Add( aCompound, S );
2103 theSubs.push_back( subMesh );
2105 else if ( subMesh->GetComputeState() == READY_TO_COMPUTE )
2107 SMESH_Algo* anAlgo = subMesh->GetAlgo();
2108 if (( anAlgo->IsSameName( *theAlgo )) && // same algo
2109 ( anAlgo->GetUsedHypothesis( *_father, S, skipAuxHyps ) == aUsedHyp )) // same hyps
2111 aBuilder.Add( aCompound, S );
2112 if ( !subMesh->SubMeshesComputed() )
2113 theSubComputed = false;
2114 theSubs.push_back( subMesh );
2122 //=======================================================================
2123 //function : getSimilarAttached
2124 //purpose : return a hypothesis attached to theShape.
2125 // If theHyp is provided, similar but not same hypotheses
2126 // is returned; else only applicable ones having theHypType
2128 //=======================================================================
2130 const SMESH_Hypothesis* SMESH_subMesh::getSimilarAttached(const TopoDS_Shape& theShape,
2131 const SMESH_Hypothesis * theHyp,
2132 const int theHypType)
2134 SMESH_HypoFilter hypoKind;
2135 hypoKind.Init( hypoKind.HasType( theHyp ? theHyp->GetType() : theHypType ));
2137 hypoKind.And ( hypoKind.HasDim( theHyp->GetDim() ));
2138 hypoKind.AndNot( hypoKind.Is( theHyp ));
2139 if ( theHyp->IsAuxiliary() )
2140 hypoKind.And( hypoKind.HasName( theHyp->GetName() ));
2142 hypoKind.AndNot( hypoKind.IsAuxiliary());
2145 hypoKind.And( hypoKind.IsApplicableTo( theShape ));
2148 return _father->GetHypothesis( theShape, hypoKind, false );
2151 //=======================================================================
2152 //function : CheckConcurentHypothesis
2153 //purpose : check if there are several applicable hypothesis attached to
2155 //=======================================================================
2157 SMESH_Hypothesis::Hypothesis_Status
2158 SMESH_subMesh::CheckConcurentHypothesis (const int theHypType)
2160 MESSAGE ("SMESH_subMesh::CheckConcurentHypothesis");
2162 // is there local hypothesis on me?
2163 if ( getSimilarAttached( _subShape, 0, theHypType ) )
2164 return SMESH_Hypothesis::HYP_OK;
2167 TopoDS_Shape aPrevWithHyp;
2168 const SMESH_Hypothesis* aPrevHyp = 0;
2169 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
2170 for (; it.More(); it.Next())
2172 const TopoDS_Shape& ancestor = it.Value();
2173 const SMESH_Hypothesis* hyp = getSimilarAttached( ancestor, 0, theHypType );
2176 if ( aPrevWithHyp.IsNull() || aPrevWithHyp.IsSame( ancestor ))
2178 aPrevWithHyp = ancestor;
2181 else if ( aPrevWithHyp.ShapeType() == ancestor.ShapeType() && aPrevHyp != hyp )
2182 return SMESH_Hypothesis::HYP_CONCURENT;
2184 return SMESH_Hypothesis::HYP_OK;
2187 return SMESH_Hypothesis::HYP_OK;
2190 //================================================================================
2192 * \brief Constructor of OwnListenerData
2194 //================================================================================
2196 SMESH_subMesh::OwnListenerData::OwnListenerData( SMESH_subMesh* sm, EventListener* el):
2198 myMeshID( sm ? sm->GetFather()->GetId() : -1 ),
2199 mySubMeshID( sm ? sm->GetId() : -1 ),
2204 //================================================================================
2206 * \brief Sets an event listener and its data to a submesh
2207 * \param listener - the listener to store
2208 * \param data - the listener data to store
2209 * \param where - the submesh to store the listener and it's data
2211 * It remembers the submesh where it puts the listener in order to delete
2212 * them when HYP_OK algo_state is lost
2213 * After being set, event listener is notified on each event of where submesh.
2215 //================================================================================
2217 void SMESH_subMesh::SetEventListener(EventListener* listener,
2218 EventListenerData* data,
2219 SMESH_subMesh* where)
2221 if ( listener && where ) {
2222 where->setEventListener( listener, data );
2223 _ownListeners.push_back( OwnListenerData( where, listener ));
2227 //================================================================================
2229 * \brief Sets an event listener and its data to a submesh
2230 * \param listener - the listener to store
2231 * \param data - the listener data to store
2233 * After being set, event listener is notified on each event of a submesh.
2235 //================================================================================
2237 void SMESH_subMesh::setEventListener(EventListener* listener,
2238 EventListenerData* data)
2240 map< EventListener*, EventListenerData* >::iterator l_d =
2241 _eventListeners.find( listener );
2242 if ( l_d != _eventListeners.end() ) {
2243 EventListenerData* curData = l_d->second;
2244 if ( curData && curData != data && curData->IsDeletable() )
2250 for ( l_d = _eventListeners.begin(); l_d != _eventListeners.end(); ++l_d )
2251 if ( listener->GetName() == l_d->first->GetName() )
2253 EventListenerData* curData = l_d->second;
2254 if ( curData && curData != data && curData->IsDeletable() )
2256 if ( l_d->first != listener && l_d->first->IsDeletable() )
2258 _eventListeners.erase( l_d );
2261 _eventListeners.insert( make_pair( listener, data ));
2265 //================================================================================
2267 * \brief Return an event listener data
2268 * \param listener - the listener whose data is
2269 * \param myOwn - if \c true, returns a listener set by this sub-mesh,
2270 * else returns a listener listening to events of this sub-mesh
2271 * \retval EventListenerData* - found data, maybe NULL
2273 //================================================================================
2275 EventListenerData* SMESH_subMesh::GetEventListenerData(EventListener* listener,
2276 const bool myOwn) const
2280 list< OwnListenerData >::const_iterator d;
2281 for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
2283 if ( d->myListener == listener && _father->MeshExists( d->myMeshID ))
2284 return d->mySubMesh->GetEventListenerData( listener, !myOwn );
2289 map< EventListener*, EventListenerData* >::const_iterator l_d =
2290 _eventListeners.find( listener );
2291 if ( l_d != _eventListeners.end() )
2297 //================================================================================
2299 * \brief Return an event listener data
2300 * \param listenerName - the listener name
2301 * \param myOwn - if \c true, returns a listener set by this sub-mesh,
2302 * else returns a listener listening to events of this sub-mesh
2303 * \retval EventListenerData* - found data, maybe NULL
2305 //================================================================================
2307 EventListenerData* SMESH_subMesh::GetEventListenerData(const string& listenerName,
2308 const bool myOwn) const
2312 list< OwnListenerData >::const_iterator d;
2313 for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
2315 if ( _father->MeshExists( d->myMeshID ) && listenerName == d->myListener->GetName())
2316 return d->mySubMesh->GetEventListenerData( listenerName, !myOwn );
2321 map< EventListener*, EventListenerData* >::const_iterator l_d = _eventListeners.begin();
2322 for ( ; l_d != _eventListeners.end(); ++l_d )
2323 if ( listenerName == l_d->first->GetName() )
2329 //================================================================================
2331 * \brief Notify stored event listeners on the occured event
2332 * \param event - algo_event or compute_event itself
2333 * \param eventType - algo_event or compute_event
2334 * \param hyp - hypothesis, if eventType is algo_event
2336 //================================================================================
2338 void SMESH_subMesh::notifyListenersOnEvent( const int event,
2339 const event_type eventType,
2340 SMESH_Hypothesis* hyp)
2342 list< pair< EventListener*, EventListenerData* > > eventListeners( _eventListeners.begin(),
2343 _eventListeners.end());
2344 list< pair< EventListener*, EventListenerData* > >::iterator l_d = eventListeners.begin();
2345 for ( ; l_d != eventListeners.end(); ++l_d )
2347 std::pair< EventListener*, EventListenerData* > li_da = *l_d;
2348 if ( !_eventListeners.count( li_da.first )) continue;
2350 if ( li_da.first->myBusySM.insert( this ).second )
2352 const bool isDeletable = li_da.first->IsDeletable();
2354 li_da.first->ProcessEvent( event, eventType, this, li_da.second, hyp );
2356 if ( !isDeletable || _eventListeners.count( li_da.first ))
2357 li_da.first->myBusySM.erase( this ); // a listener is hopefully not dead
2362 //================================================================================
2364 * \brief Unregister the listener and delete listener's data
2365 * \param listener - the event listener
2367 //================================================================================
2369 void SMESH_subMesh::DeleteEventListener(EventListener* listener)
2371 map< EventListener*, EventListenerData* >::iterator l_d =
2372 _eventListeners.find( listener );
2373 if ( l_d != _eventListeners.end() && l_d->first )
2375 if ( l_d->second && l_d->second->IsDeletable() )
2379 l_d->first->myBusySM.erase( this );
2380 if ( l_d->first->IsDeletable() )
2382 l_d->first->BeforeDelete( this, l_d->second );
2385 _eventListeners.erase( l_d );
2389 //================================================================================
2391 * \brief Delete event listeners depending on algo of this submesh
2393 //================================================================================
2395 void SMESH_subMesh::deleteOwnListeners()
2397 list< OwnListenerData >::iterator d;
2398 for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
2400 SMESH_Mesh* mesh = _father->FindMesh( d->myMeshID );
2401 if ( !mesh || !mesh->GetSubMeshContaining( d->mySubMeshID ))
2403 d->mySubMesh->DeleteEventListener( d->myListener );
2405 _ownListeners.clear();
2408 //=======================================================================
2409 //function : loadDependentMeshes
2410 //purpose : loads dependent meshes on SUBMESH_LOADED event
2411 //=======================================================================
2413 void SMESH_subMesh::loadDependentMeshes()
2415 list< OwnListenerData >::iterator d;
2416 for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
2417 if ( _father != d->mySubMesh->_father )
2418 d->mySubMesh->_father->Load();
2420 // map< EventListener*, EventListenerData* >::iterator l_d = _eventListeners.begin();
2421 // for ( ; l_d != _eventListeners.end(); ++l_d )
2422 // if ( l_d->second )
2424 // const list<SMESH_subMesh*>& smList = l_d->second->mySubMeshes;
2425 // list<SMESH_subMesh*>::const_iterator sm = smList.begin();
2426 // for ( ; sm != smList.end(); ++sm )
2427 // if ( _father != (*sm)->_father )
2428 // (*sm)->_father->Load();
2432 //================================================================================
2434 * \brief Do something on a certain event
2435 * \param event - algo_event or compute_event itself
2436 * \param eventType - algo_event or compute_event
2437 * \param subMesh - the submesh where the event occures
2438 * \param data - listener data stored in the subMesh
2439 * \param hyp - hypothesis, if eventType is algo_event
2441 * The base implementation translates CLEAN event to the subMesh
2442 * stored in listener data. Also it sends SUBMESH_COMPUTED event in case of
2443 * successful COMPUTE event.
2445 //================================================================================
2447 void SMESH_subMeshEventListener::ProcessEvent(const int event,
2448 const int eventType,
2449 SMESH_subMesh* subMesh,
2450 EventListenerData* data,
2451 const SMESH_Hypothesis* /*hyp*/)
2453 if ( data && !data->mySubMeshes.empty() &&
2454 eventType == SMESH_subMesh::COMPUTE_EVENT)
2456 ASSERT( data->mySubMeshes.front() != subMesh );
2457 list<SMESH_subMesh*>::iterator smIt = data->mySubMeshes.begin();
2458 list<SMESH_subMesh*>::iterator smEnd = data->mySubMeshes.end();
2460 case SMESH_subMesh::CLEAN:
2461 for ( ; smIt != smEnd; ++ smIt)
2462 (*smIt)->ComputeStateEngine( SMESH_subMesh::compute_event( event ));
2464 case SMESH_subMesh::COMPUTE:
2465 case SMESH_subMesh::COMPUTE_SUBMESH:
2466 if ( subMesh->GetComputeState() == SMESH_subMesh::COMPUTE_OK )
2467 for ( ; smIt != smEnd; ++ smIt)
2468 (*smIt)->ComputeStateEngine( SMESH_subMesh::SUBMESH_COMPUTED );
2477 //================================================================================
2479 * \brief Iterator over submeshes and optionally prepended or appended one
2481 //================================================================================
2483 struct _Iterator : public SMDS_Iterator<SMESH_subMesh*>
2485 _Iterator(SMDS_Iterator<SMESH_subMesh*>* subIt,
2486 SMESH_subMesh* prepend,
2487 SMESH_subMesh* append): myAppend(append), myIt(subIt)
2489 myCur = prepend ? prepend : myIt->more() ? myIt->next() : append;
2490 if ( myCur == append ) append = 0;
2492 /// Return true if and only if there are other object in this iterator
2497 /// Return the current object and step to the next one
2498 virtual SMESH_subMesh* next()
2500 SMESH_subMesh* res = myCur;
2501 if ( myIt->more() ) { myCur = myIt->next(); }
2502 else { myCur = myAppend; myAppend = 0; }
2509 SMESH_subMesh *myAppend, *myCur;
2510 SMDS_Iterator<SMESH_subMesh*> *myIt;
2514 //================================================================================
2516 * \brief Return iterator on the submeshes this one depends on
2517 * \param includeSelf - this submesh to be returned also
2518 * \param reverse - if true, complex shape submeshes go first
2520 //================================================================================
2522 SMESH_subMeshIteratorPtr SMESH_subMesh::getDependsOnIterator(const bool includeSelf,
2523 const bool reverse) const
2525 SMESH_subMesh *me = (SMESH_subMesh*) this;
2526 SMESH_subMesh *prepend=0, *append=0;
2527 if ( includeSelf ) {
2528 if ( reverse ) prepend = me;
2531 typedef map < int, SMESH_subMesh * > TMap;
2534 return SMESH_subMeshIteratorPtr
2535 ( new _Iterator( new SMDS_mapReverseIterator<TMap>( me->DependsOn() ), prepend, append ));
2538 return SMESH_subMeshIteratorPtr
2539 ( new _Iterator( new SMDS_mapIterator<TMap>( me->DependsOn() ), prepend, append ));
2543 //================================================================================
2545 * \brief Returns ancestor sub-meshes. Finds them if not yet found.
2547 //================================================================================
2549 const std::vector< SMESH_subMesh * > & SMESH_subMesh::GetAncestors() const
2551 if ( _ancestors.empty() &&
2552 !_subShape.IsSame( _father->GetShapeToMesh() ))
2554 const TopTools_ListOfShape& ancShapes = _father->GetAncestors( _subShape );
2556 SMESH_subMesh* me = const_cast< SMESH_subMesh* >( this );
2557 me->_ancestors.reserve( ancShapes.Extent() );
2559 TopTools_MapOfShape map;
2561 for ( TopTools_ListIteratorOfListOfShape it( ancShapes ); it.More(); it.Next() )
2562 if ( SMESH_subMesh* sm = _father->GetSubMeshContaining( it.Value() ))
2563 if ( map.Add( it.Value() ))
2564 me->_ancestors.push_back( sm );
2570 //================================================================================
2572 * \brief Clears the vector of ancestor sub-meshes
2574 //================================================================================
2576 void SMESH_subMesh::ClearAncestors()
2581 //================================================================================
2583 * \brief Find common submeshes (based on shared sub-shapes with other
2584 * \param theOther submesh to check
2585 * \param theSetOfCommon set of common submesh
2587 //================================================================================
2589 bool SMESH_subMesh::FindIntersection(const SMESH_subMesh* theOther,
2590 std::set<const SMESH_subMesh*>& theSetOfCommon ) const
2592 size_t oldNb = theSetOfCommon.size();
2594 // check main submeshes
2595 const map <int, SMESH_subMesh*>::const_iterator otherEnd = theOther->_mapDepend.end();
2596 if ( theOther->_mapDepend.find(this->GetId()) != otherEnd )
2597 theSetOfCommon.insert( this );
2598 if ( _mapDepend.find(theOther->GetId()) != _mapDepend.end() )
2599 theSetOfCommon.insert( theOther );
2601 // check common submeshes
2602 map <int, SMESH_subMesh*>::const_iterator mapIt = _mapDepend.begin();
2603 for( ; mapIt != _mapDepend.end(); mapIt++ )
2604 if ( theOther->_mapDepend.find((*mapIt).first) != otherEnd )
2605 theSetOfCommon.insert( (*mapIt).second );
2606 return oldNb < theSetOfCommon.size();