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 );
442 { /*internal EDGE*/ insertDependence( _subShape, TopAbs_EDGE, TopAbs_WIRE ); break; }
443 case TopAbs_SHELL: insertDependence( _subShape, TopAbs_FACE ); break;
444 case TopAbs_FACE: insertDependence( _subShape, TopAbs_EDGE ); break;
445 case TopAbs_WIRE: insertDependence( _subShape, TopAbs_EDGE ); break;
446 case TopAbs_EDGE: insertDependence( _subShape, TopAbs_VERTEX ); break;
449 _dependenceAnalysed = true;
453 //================================================================================
455 * \brief Return a key for SMESH_subMesh::_mapDepend map
457 //================================================================================
461 int dependsOnMapKey( TopAbs_ShapeEnum type, int shapeID )
463 int ordType = 9 - int(type); // 2 = Vertex, 8 = CompSolid
465 cle += 10000000 * ordType; // sort map by ordType then index
468 int dependsOnMapKey( const SMESH_subMesh* sm )
470 return dependsOnMapKey( sm->GetSubShape().ShapeType(), sm->GetId() );
474 //=============================================================================
476 * Add sub-meshes on sub-shapes of a given type into the dependence map.
478 //=============================================================================
480 void SMESH_subMesh::insertDependence(const TopoDS_Shape aShape,
481 TopAbs_ShapeEnum aSubType,
482 TopAbs_ShapeEnum avoidType)
484 TopExp_Explorer sub( aShape, aSubType, avoidType );
485 for ( ; sub.More(); sub.Next() )
487 SMESH_subMesh *aSubMesh = _father->GetSubMesh( sub.Current() );
488 if ( aSubMesh->GetId() == 0 )
489 continue; // not a sub-shape of the shape to mesh
490 int cle = dependsOnMapKey( aSubMesh );
491 if ( _mapDepend.find( cle ) == _mapDepend.end())
493 _mapDepend[cle] = aSubMesh;
494 const map < int, SMESH_subMesh * > & subMap = aSubMesh->DependsOn();
495 _mapDepend.insert( subMap.begin(), subMap.end() );
500 //================================================================================
502 * \brief Return \c true if \a this sub-mesh depends on \a other
504 //================================================================================
506 bool SMESH_subMesh::DependsOn( const SMESH_subMesh* other ) const
508 return other ? _mapDepend.count( dependsOnMapKey( other )) : false;
511 //================================================================================
513 * \brief Return \c true if \a this sub-mesh depends on a \a shape
515 //================================================================================
517 bool SMESH_subMesh::DependsOn( const int shapeID ) const
519 return DependsOn( _father->GetSubMeshContaining( shapeID ));
522 //=============================================================================
524 * Return a shape of \a this sub-mesh
526 //=============================================================================
528 const TopoDS_Shape & SMESH_subMesh::GetSubShape() const
533 //=======================================================================
534 //function : CanAddHypothesis
535 //purpose : return true if theHypothesis can be attached to me:
536 // its dimention is checked
537 //=======================================================================
539 bool SMESH_subMesh::CanAddHypothesis(const SMESH_Hypothesis* theHypothesis) const
541 int aHypDim = theHypothesis->GetDim();
542 int aShapeDim = SMESH_Gen::GetShapeDim(_subShape);
543 // issue 21106. Forbid 3D mesh on the SHELL
544 // if (aHypDim == 3 && aShapeDim == 3) {
545 // // check case of open shell
546 // //if (_subShape.ShapeType() == TopAbs_SHELL && !_subShape.Closed())
547 // if (_subShape.ShapeType() == TopAbs_SHELL && !BRep_Tool::IsClosed(_subShape))
550 if ( aHypDim <= aShapeDim )
556 //=======================================================================
557 //function : IsApplicableHypotesis
559 //=======================================================================
561 bool SMESH_subMesh::IsApplicableHypotesis(const SMESH_Hypothesis* theHypothesis,
562 const TopAbs_ShapeEnum theShapeType)
564 if ( theHypothesis->GetType() > SMESHDS_Hypothesis::PARAM_ALGO)
567 if ( theHypothesis->GetShapeType() & (1<< theShapeType))
568 // issue 21106. Forbid 3D mesh on the SHELL
569 return !( theHypothesis->GetDim() == 3 && theShapeType == TopAbs_SHELL );
575 switch ( theShapeType ) {
580 return SMESH_Gen::GetShapeDim( theShapeType ) == theHypothesis->GetDim();
583 // Special case for algorithms, building 2D mesh on a whole shell.
584 // Before this fix there was a problem after restoring from study,
585 // because in that case algorithm is assigned before hypothesis
586 // (on shell in problem case) and hypothesis is checked on faces
587 // (because it is 2D), where we have NO_ALGO state.
588 // Now 2D hypothesis is also applicable to shells.
589 return (theHypothesis->GetDim() == 2 || theHypothesis->GetDim() == 3);
592 // case TopAbs_COMPSOLID:
593 // case TopAbs_COMPOUND:
599 //================================================================================
601 * \brief Treats modification of hypotheses definition
602 * \param [in] event - what happens
603 * \param [in] anHyp - a hypothesis
604 * \return SMESH_Hypothesis::Hypothesis_Status - a treatment result.
606 * Optional description of a problematic situation (if any) can be retrieved
607 * via GetComputeError().
609 //================================================================================
611 SMESH_Hypothesis::Hypothesis_Status
612 SMESH_subMesh::AlgoStateEngine(int event, SMESH_Hypothesis * anHyp)
614 // **** les retour des evenement shape sont significatifs
615 // (add ou remove fait ou non)
616 // le retour des evenement father n'indiquent pas que add ou remove fait
618 SMESH_Hypothesis::Hypothesis_Status aux_ret, ret = SMESH_Hypothesis::HYP_OK;
619 if ( _Id == 0 ) return ret; // not a sub-shape of the shape to mesh
621 SMESHDS_Mesh* meshDS =_father->GetMeshDS();
622 SMESH_Algo* algo = 0;
625 if (_subShape.ShapeType() == TopAbs_VERTEX )
627 if ( anHyp->GetDim() != 0) {
628 if (event == ADD_HYP || event == ADD_ALGO)
629 return SMESH_Hypothesis::HYP_BAD_DIM;
631 return SMESH_Hypothesis::HYP_OK;
634 else if ( _algoState == HYP_OK ) {
635 // update default _algoState
636 if ( event != REMOVE_FATHER_ALGO )
638 _algoState = NO_ALGO;
641 _algoState = MISSING_HYP;
642 if ( event == REMOVE_FATHER_HYP ||
643 algo->CheckHypothesis(*_father,_subShape, aux_ret))
650 int oldAlgoState = _algoState;
651 bool modifiedHyp = (event == MODIF_HYP); // if set to true, force event MODIF_ALGO_STATE
652 SMESH_Algo* algoRequiringCleaning = 0;
654 bool isApplicableHyp = IsApplicableHypotesis( anHyp );
656 if (event == ADD_ALGO || event == ADD_FATHER_ALGO)
658 // -------------------------------------------
659 // check if a shape needed by algo is present
660 // -------------------------------------------
661 algo = static_cast< SMESH_Algo* >( anHyp );
662 if ( !_father->HasShapeToMesh() && algo->NeedShape() )
663 return SMESH_Hypothesis::HYP_NEED_SHAPE;
664 // ----------------------
665 // check mesh conformity
666 // ----------------------
667 if (isApplicableHyp && !_father->IsNotConformAllowed() && !IsConform( algo ))
668 return SMESH_Hypothesis::HYP_NOTCONFORM;
670 // check if all-dimensional algo is hidden by other local one
671 if ( event == ADD_ALGO ) {
672 SMESH_HypoFilter filter( SMESH_HypoFilter::HasType( algo->GetType() ));
673 filter.Or( SMESH_HypoFilter::HasType( algo->GetType()+1 ));
674 filter.Or( SMESH_HypoFilter::HasType( algo->GetType()+2 ));
675 if ( SMESH_Algo * curAlgo = (SMESH_Algo*)_father->GetHypothesis( this, filter, true ))
676 if ( !curAlgo->NeedDiscreteBoundary() )
677 algoRequiringCleaning = curAlgo;
681 // ----------------------------------
682 // add a hypothesis to DS if possible
683 // ----------------------------------
684 if (event == ADD_HYP || event == ADD_ALGO)
686 if ( ! CanAddHypothesis( anHyp )) // check dimension
687 return SMESH_Hypothesis::HYP_BAD_DIM;
689 if ( !anHyp->IsAuxiliary() && getSimilarAttached( _subShape, anHyp ) )
690 return SMESH_Hypothesis::HYP_ALREADY_EXIST;
692 if ( !meshDS->AddHypothesis(_subShape, anHyp))
693 return SMESH_Hypothesis::HYP_ALREADY_EXIST;
696 // --------------------------
697 // remove a hypothesis from DS
698 // --------------------------
699 if (event == REMOVE_HYP || event == REMOVE_ALGO)
701 if (!meshDS->RemoveHypothesis(_subShape, anHyp))
702 return SMESH_Hypothesis::HYP_OK; // nothing changes
704 if (event == REMOVE_ALGO)
706 algo = dynamic_cast<SMESH_Algo*> (anHyp);
707 if (!algo->NeedDiscreteBoundary())
708 algoRequiringCleaning = algo;
712 // ------------------
713 // analyse algo state
714 // ------------------
715 if (!isApplicableHyp)
716 return ret; // not applicable hypotheses do not change algo state
718 if (( algo = GetAlgo()))
719 algo->InitComputeError();
724 // ----------------------------------------------------------------------
733 if (algo->CheckHypothesis((*_father),_subShape, aux_ret))
734 setAlgoState(HYP_OK);
735 else if ( algo->IsStatusFatal( aux_ret )) {
736 meshDS->RemoveHypothesis(_subShape, anHyp);
740 setAlgoState(MISSING_HYP);
747 case ADD_FATHER_ALGO: { // Algo just added in father
750 if ( algo == anHyp ) {
751 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret))
752 setAlgoState(HYP_OK);
754 setAlgoState(MISSING_HYP);
758 case REMOVE_FATHER_HYP:
760 case REMOVE_FATHER_ALGO: {
764 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
765 setAlgoState(HYP_OK);
767 setAlgoState(MISSING_HYP);
771 case MODIF_HYP: break;
778 // ----------------------------------------------------------------------
786 if ( algo->CheckHypothesis((*_father),_subShape, ret ))
787 setAlgoState(HYP_OK);
788 if (SMESH_Hypothesis::IsStatusFatal( ret ))
789 meshDS->RemoveHypothesis(_subShape, anHyp);
790 else if (!_father->IsUsedHypothesis( anHyp, this ))
792 meshDS->RemoveHypothesis(_subShape, anHyp);
793 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
797 case ADD_ALGO: { //already existing algo : on father ?
800 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))// ignore hyp status
801 setAlgoState(HYP_OK);
802 else if ( algo->IsStatusFatal( aux_ret )) {
803 meshDS->RemoveHypothesis(_subShape, anHyp);
807 setAlgoState(MISSING_HYP);
812 case REMOVE_ALGO: { // perhaps a father algo applies ?
814 if (algo == NULL) // no more algo applying on sub-shape...
816 setAlgoState(NO_ALGO);
820 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
821 setAlgoState(HYP_OK);
823 setAlgoState(MISSING_HYP);
827 case MODIF_HYP: // assigned hypothesis value may become good
828 case ADD_FATHER_HYP: {
831 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
832 setAlgoState(HYP_OK);
834 setAlgoState(MISSING_HYP);
837 case ADD_FATHER_ALGO: { // new father algo
840 if ( algo == anHyp ) {
841 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
842 setAlgoState(HYP_OK);
844 setAlgoState(MISSING_HYP);
848 case REMOVE_FATHER_HYP: // nothing to do
850 case REMOVE_FATHER_ALGO: {
852 if (algo == NULL) // no more applying algo on father
854 setAlgoState(NO_ALGO);
858 if ( algo->CheckHypothesis((*_father),_subShape , aux_ret ))
859 setAlgoState(HYP_OK);
861 setAlgoState(MISSING_HYP);
871 // ----------------------------------------------------------------------
879 if (!algo->CheckHypothesis((*_father),_subShape, ret ))
881 if ( !SMESH_Hypothesis::IsStatusFatal( ret ))
882 // ret should be fatal: anHyp was not added
883 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
885 else if (!_father->IsUsedHypothesis( anHyp, this ))
886 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
888 if (SMESH_Hypothesis::IsStatusFatal( ret ))
890 MESSAGE("do not add extra hypothesis");
891 meshDS->RemoveHypothesis(_subShape, anHyp);
899 case ADD_ALGO: { //already existing algo : on father ?
901 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
902 // check if algo changes
904 f.Init( SMESH_HypoFilter::IsAlgo() );
905 f.And( SMESH_HypoFilter::IsApplicableTo( _subShape ));
906 f.AndNot( SMESH_HypoFilter::Is( algo ));
907 const SMESH_Hypothesis * prevAlgo = _father->GetHypothesis( this, f, true );
909 string( algo->GetName()) != prevAlgo->GetName())
913 setAlgoState(MISSING_HYP);
919 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
920 setAlgoState(HYP_OK);
922 setAlgoState(MISSING_HYP);
926 case REMOVE_ALGO: { // perhaps a father algo applies ?
928 if (algo == NULL) // no more algo applying on sub-shape...
930 setAlgoState(NO_ALGO);
934 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
935 // check if algo remains
936 if ( anHyp != algo && strcmp( anHyp->GetName(), algo->GetName()) )
940 setAlgoState(MISSING_HYP);
944 case MODIF_HYP: // hypothesis value may become bad
945 case ADD_FATHER_HYP: { // new father hypothesis ?
948 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
950 if (_father->IsUsedHypothesis( anHyp, this )) // new Hyp
954 setAlgoState(MISSING_HYP);
957 case ADD_FATHER_ALGO: {
959 if ( algo == anHyp ) { // a new algo on father
960 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
961 // check if algo changes
963 f.Init( SMESH_HypoFilter::IsAlgo() );
964 f.And( SMESH_HypoFilter::IsApplicableTo( _subShape ));
965 f.AndNot( SMESH_HypoFilter::Is( algo ));
966 const SMESH_Hypothesis* prevAlgo = _father->GetHypothesis( this, f, true );
968 string(algo->GetName()) != string(prevAlgo->GetName()) )
972 setAlgoState(MISSING_HYP);
976 case REMOVE_FATHER_HYP: {
979 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
980 // is there the same local hyp or maybe a new father algo applied?
981 if ( !getSimilarAttached( _subShape, anHyp ) )
985 setAlgoState(MISSING_HYP);
988 case REMOVE_FATHER_ALGO: {
989 // IPAL21346. Edges not removed when Netgen 1d-2d is removed from a SOLID.
990 // CLEAN was not called at event REMOVE_ALGO because the algo is not applicable to SOLID.
991 algo = dynamic_cast<SMESH_Algo*> (anHyp);
992 if (!algo->NeedDiscreteBoundary())
993 algoRequiringCleaning = algo;
995 if (algo == NULL) // no more applying algo on father
997 setAlgoState(NO_ALGO);
1001 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
1002 // check if algo changes
1003 if ( string(algo->GetName()) != string( anHyp->GetName()) )
1007 setAlgoState(MISSING_HYP);
1017 // ----------------------------------------------------------------------
1024 // detect algorithm hiding
1026 if ( ret == SMESH_Hypothesis::HYP_OK &&
1027 ( event == ADD_ALGO || event == ADD_FATHER_ALGO ) && algo &&
1028 algo->GetName() == anHyp->GetName() )
1031 SMESH_Gen* gen = _father->GetGen();
1032 const std::vector< SMESH_subMesh * > & ancestors = GetAncestors();
1033 for ( size_t iA = 0; ( ret == SMESH_Hypothesis::HYP_OK && iA < ancestors.size()); ++iA ) {
1034 if ( SMESH_Algo* upperAlgo = ancestors[ iA ]->GetAlgo() )
1035 if ( !upperAlgo->NeedDiscreteBoundary() && !upperAlgo->SupportSubmeshes())
1036 ret = SMESH_Hypothesis::HYP_HIDDEN_ALGO;
1039 if ( ret == SMESH_Hypothesis::HYP_OK &&
1040 !algo->NeedDiscreteBoundary() &&
1041 !algo->SupportSubmeshes())
1043 TopoDS_Shape algoAssignedTo, otherAssignedTo;
1044 gen->GetAlgo( this, &algoAssignedTo );
1045 map<int, SMESH_subMesh*>::reverse_iterator i_sm = _mapDepend.rbegin();
1046 for ( ; ( ret == SMESH_Hypothesis::HYP_OK && i_sm != _mapDepend.rend()) ; ++i_sm )
1047 if ( gen->GetAlgo( i_sm->second, &otherAssignedTo ) &&
1048 SMESH_MesherHelper::IsSubShape( /*sub=*/otherAssignedTo, /*main=*/algoAssignedTo ))
1049 ret = SMESH_Hypothesis::HYP_HIDING_ALGO;
1053 if ( _algo ) { // get an error description set by _algo->CheckHypothesis()
1054 _computeError = _algo->GetComputeError();
1055 _algo->InitComputeError();
1058 bool stateChange = ( _algoState != oldAlgoState );
1060 if ( stateChange && _algoState == HYP_OK ) // hyp becomes OK
1061 algo->SetEventListener( this );
1063 if ( event == REMOVE_ALGO || event == REMOVE_FATHER_ALGO )
1066 notifyListenersOnEvent( event, ALGO_EVENT, anHyp );
1068 if ( stateChange && oldAlgoState == HYP_OK ) { // hyp becomes KO
1069 deleteOwnListeners();
1070 SetIsAlwaysComputed( false );
1071 if (_subShape.ShapeType() == TopAbs_VERTEX ) {
1072 // restore default states
1073 _algoState = HYP_OK;
1074 _computeState = READY_TO_COMPUTE;
1078 if ( algoRequiringCleaning ) {
1079 // added or removed algo is all-dimensional
1080 ComputeStateEngine( CLEAN );
1081 cleanDependsOn( algoRequiringCleaning );
1082 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1085 if ( stateChange || modifiedHyp )
1086 ComputeStateEngine( MODIF_ALGO_STATE );
1088 _realComputeCost = ( _algoState == HYP_OK ) ? computeCost() : 0;
1093 //=======================================================================
1094 //function : IsConform
1095 //purpose : check if a conform mesh will be produced by the Algo
1096 //=======================================================================
1098 bool SMESH_subMesh::IsConform(const SMESH_Algo* theAlgo)
1100 // MESSAGE( "SMESH_subMesh::IsConform" );
1101 if ( !theAlgo ) return false;
1103 // Suppose that theAlgo is applicable to _subShape, do not check it here
1104 //if ( !IsApplicableHypotesis( theAlgo )) return false;
1106 // check only algo that doesn't NeedDiscreteBoundary(): because mesh made
1107 // on a sub-shape will be ignored by theAlgo
1108 if ( theAlgo->NeedDiscreteBoundary() ||
1109 !theAlgo->OnlyUnaryInput() ) // all adjacent shapes will be meshed by this algo?
1112 // only local algo is to be checked
1113 //if ( gen->IsGlobalHypothesis( theAlgo, *_father ))
1114 if ( _subShape.ShapeType() == _father->GetMeshDS()->ShapeToMesh().ShapeType() )
1117 // check algo attached to adjacent shapes
1119 // loop on one level down sub-meshes
1120 TopoDS_Iterator itsub( _subShape );
1121 for (; itsub.More(); itsub.Next())
1123 // loop on adjacent subShapes
1124 const std::vector< SMESH_subMesh * > & ancestors = GetAncestors();
1125 for ( size_t iA = 0; iA < ancestors.size(); ++iA )
1127 const TopoDS_Shape& adjacent = ancestors[ iA ]->GetSubShape();
1128 if ( _subShape.IsSame( adjacent )) continue;
1129 if ( adjacent.ShapeType() != _subShape.ShapeType())
1132 // check algo attached to smAdjacent
1133 SMESH_Algo * algo = ancestors[ iA ]->GetAlgo();
1135 !algo->NeedDiscreteBoundary() &&
1136 algo->OnlyUnaryInput())
1137 return false; // NOT CONFORM MESH WILL BE PRODUCED
1144 //=============================================================================
1148 //=============================================================================
1150 void SMESH_subMesh::setAlgoState(algo_state state)
1155 //================================================================================
1157 * \brief Send an event to sub-meshes
1158 * \param [in] event - the event
1159 * \param [in] anHyp - an hypothesis
1160 * \param [in] exitOnFatal - to stop iteration on sub-meshes if a sub-mesh
1161 * reports a fatal result
1162 * \return SMESH_Hypothesis::Hypothesis_Status - the worst result
1164 * Optional description of a problematic situation (if any) can be retrieved
1165 * via GetComputeError().
1167 //================================================================================
1169 SMESH_Hypothesis::Hypothesis_Status
1170 SMESH_subMesh::SubMeshesAlgoStateEngine(int event,
1171 SMESH_Hypothesis * anHyp,
1174 SMESH_Hypothesis::Hypothesis_Status ret = SMESH_Hypothesis::HYP_OK;
1175 //EAP: a wire (dim==1) should notify edges (dim==1)
1176 //EAP: int dim = SMESH_Gen::GetShapeDim(_subShape);
1177 //if (_subShape.ShapeType() < TopAbs_EDGE ) // wire,face etc
1179 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1180 while ( smIt->more() ) {
1181 SMESH_subMesh* sm = smIt->next();
1182 SMESH_Hypothesis::Hypothesis_Status ret2 = sm->AlgoStateEngine(event, anHyp);
1186 _computeError = sm->_computeError;
1187 sm->_computeError.reset();
1188 if ( exitOnFatal && SMESH_Hypothesis::IsStatusFatal( ret ))
1196 //================================================================================
1198 * \brief Remove elements from sub-meshes.
1199 * \param algoRequiringCleaning - an all-dimensional algorithm whose presence
1200 * causes the cleaning.
1202 //================================================================================
1204 void SMESH_subMesh::cleanDependsOn( SMESH_Algo* algoRequiringCleaning/*=0*/ )
1206 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,
1207 /*complexShapeFirst=*/true);
1208 if ( _father->NbNodes() == 0 )
1210 while ( smIt->more() )
1211 smIt->next()->ComputeStateEngine(CHECK_COMPUTE_STATE);
1213 else if ( !algoRequiringCleaning || !algoRequiringCleaning->SupportSubmeshes() )
1215 while ( smIt->more() )
1216 smIt->next()->ComputeStateEngine(CLEAN);
1218 else if ( algoRequiringCleaning && algoRequiringCleaning->SupportSubmeshes() )
1220 SMESHDS_Mesh* meshDS = _father->GetMeshDS();
1222 // find sub-meshes to keep elements on
1223 set< SMESH_subMesh* > smToKeep;
1224 TopAbs_ShapeEnum prevShapeType = TopAbs_SHAPE;
1225 bool toKeepPrevShapeType = false;
1226 while ( smIt->more() )
1228 SMESH_subMesh* sm = smIt->next();
1229 sm->ComputeStateEngine(CHECK_COMPUTE_STATE);
1230 if ( !sm->IsEmpty() )
1232 const bool sameShapeType = ( prevShapeType == sm->GetSubShape().ShapeType() );
1233 bool keepSubMeshes = ( sameShapeType && toKeepPrevShapeType );
1234 if ( !sameShapeType )
1236 // check if the algo allows presence of global algos of dimension the algo
1237 // can generate it-self
1238 int shapeDim = SMESH_Gen::GetShapeDim( sm->GetSubShape() );
1239 keepSubMeshes = algoRequiringCleaning->NeedLowerHyps( shapeDim );
1240 prevShapeType = sm->GetSubShape().ShapeType();
1241 toKeepPrevShapeType = keepSubMeshes;
1243 if ( !keepSubMeshes )
1245 // look for an algo assigned to sm
1246 bool algoFound = false;
1247 const list<const SMESHDS_Hypothesis*>& hyps = meshDS->GetHypothesis( sm->_subShape );
1248 list<const SMESHDS_Hypothesis*>::const_iterator h = hyps.begin();
1249 for ( ; ( !algoFound && h != hyps.end() ); ++h )
1250 algoFound = ((*h)->GetType() != SMESHDS_Hypothesis::PARAM_ALGO );
1251 keepSubMeshes = algoFound;
1253 // remember all sub-meshes of sm
1254 if ( keepSubMeshes )
1256 SMESH_subMeshIteratorPtr smIt2 = getDependsOnIterator(false);
1257 while ( smIt2->more() )
1258 smToKeep.insert( smIt2->next() );
1263 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,true);
1264 while ( smIt->more() )
1266 SMESH_subMesh* sm = smIt->next();
1267 if ( !smToKeep.count( sm ))
1268 sm->ComputeStateEngine(CLEAN);
1273 //=============================================================================
1277 //=============================================================================
1279 void SMESH_subMesh::DumpAlgoState(bool isMain)
1283 const map < int, SMESH_subMesh * >&subMeshes = DependsOn();
1285 map < int, SMESH_subMesh * >::const_iterator itsub;
1286 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
1288 SMESH_subMesh *sm = (*itsub).second;
1289 sm->DumpAlgoState(false);
1292 MESSAGE("dim = " << SMESH_Gen::GetShapeDim(_subShape) <<
1293 " type of shape " << _subShape.ShapeType());
1296 case NO_ALGO : MESSAGE(" AlgoState = NO_ALGO"); break;
1297 case MISSING_HYP : MESSAGE(" AlgoState = MISSING_HYP"); break;
1298 case HYP_OK : MESSAGE(" AlgoState = HYP_OK");break;
1300 switch (_computeState)
1302 case NOT_READY : MESSAGE(" ComputeState = NOT_READY");break;
1303 case READY_TO_COMPUTE : MESSAGE(" ComputeState = READY_TO_COMPUTE");break;
1304 case COMPUTE_OK : MESSAGE(" ComputeState = COMPUTE_OK");break;
1305 case FAILED_TO_COMPUTE: MESSAGE(" ComputeState = FAILED_TO_COMPUTE");break;
1309 //================================================================================
1311 * \brief Remove nodes and elements bound to submesh
1312 * \param subMesh - submesh containing nodes and elements
1314 //================================================================================
1316 static void cleanSubMesh( SMESH_subMesh * subMesh )
1319 if (SMESHDS_SubMesh * subMeshDS = subMesh->GetSubMeshDS()) {
1320 SMESHDS_Mesh * meshDS = subMesh->GetFather()->GetMeshDS();
1321 SMDS_ElemIteratorPtr ite = subMeshDS->GetElements();
1322 while (ite->more()) {
1323 const SMDS_MeshElement * elt = ite->next();
1324 //MESSAGE( " RM elt: "<<elt->GetID()<<" ( "<<elt->NbNodes()<<" )" );
1325 //meshDS->RemoveElement(elt);
1326 meshDS->RemoveFreeElement(elt, 0);
1329 SMDS_NodeIteratorPtr itn = subMeshDS->GetNodes();
1330 while (itn->more()) {
1331 const SMDS_MeshNode * node = itn->next();
1332 //MESSAGE( " RM node: "<<node->GetID());
1333 if ( node->NbInverseElements() == 0 )
1334 meshDS->RemoveFreeNode(node, 0);
1335 else // for StdMeshers_CompositeSegment_1D: node in one submesh, edge in another
1336 meshDS->RemoveNode(node);
1343 //=============================================================================
1347 //=============================================================================
1349 bool SMESH_subMesh::ComputeStateEngine(int event)
1352 case MODIF_ALGO_STATE:
1354 case COMPUTE_SUBMESH:
1355 //case COMPUTE_CANCELED:
1357 //case SUBMESH_COMPUTED:
1358 //case SUBMESH_RESTORED:
1359 //case SUBMESH_LOADED:
1360 //case MESH_ENTITY_REMOVED:
1361 //case CHECK_COMPUTE_STATE:
1362 _computeError.reset(); break;
1366 if ( event == CLEAN )
1367 _alwaysComputed = false; // Unset 'true' set by MergeNodes() (issue 0022182)
1369 if (_subShape.ShapeType() == TopAbs_VERTEX)
1371 _computeState = READY_TO_COMPUTE;
1372 SMESHDS_SubMesh* smDS = GetSubMeshDS();
1373 if ( smDS && smDS->NbNodes() )
1375 if ( event == CLEAN ) {
1377 cleanSubMesh( this );
1380 _computeState = COMPUTE_OK;
1382 else if (( event == COMPUTE || event == COMPUTE_SUBMESH )
1383 && !_alwaysComputed )
1385 const TopoDS_Vertex & V = TopoDS::Vertex( _subShape );
1386 gp_Pnt P = BRep_Tool::Pnt(V);
1387 if ( SMDS_MeshNode * n = _father->GetMeshDS()->AddNode(P.X(), P.Y(), P.Z()) ) {
1388 _father->GetMeshDS()->SetNodeOnVertex(n,_Id);
1389 _computeState = COMPUTE_OK;
1392 if ( event == MODIF_ALGO_STATE )
1396 SMESH_Gen *gen = _father->GetGen();
1397 SMESH_Algo *algo = 0;
1399 SMESH_Hypothesis::Hypothesis_Status hyp_status;
1400 //algo_state oldAlgoState = (algo_state) GetAlgoState();
1402 switch (_computeState)
1405 // ----------------------------------------------------------------------
1410 case MODIF_ALGO_STATE:
1412 if (algo && !algo->NeedDiscreteBoundary())
1413 cleanDependsOn( algo ); // clean sub-meshes with event CLEAN
1414 if ( _algoState == HYP_OK )
1415 _computeState = READY_TO_COMPUTE;
1417 case COMPUTE: // nothing to do
1418 case COMPUTE_SUBMESH:
1420 case COMPUTE_CANCELED: // nothing to do
1424 removeSubMeshElementsAndNodes();
1426 case SUBMESH_COMPUTED: // nothing to do
1428 case SUBMESH_RESTORED:
1429 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1431 case MESH_ENTITY_REMOVED:
1433 case SUBMESH_LOADED:
1434 loadDependentMeshes();
1435 ComputeSubMeshStateEngine( SUBMESH_LOADED );
1437 case CHECK_COMPUTE_STATE:
1438 if ( IsMeshComputed() )
1439 _computeState = COMPUTE_OK;
1447 // ----------------------------------------------------------------------
1449 case READY_TO_COMPUTE:
1452 case MODIF_ALGO_STATE:
1453 _computeState = NOT_READY;
1457 if (!algo->NeedDiscreteBoundary())
1458 cleanDependsOn( algo ); // clean sub-meshes with event CLEAN
1459 if ( _algoState == HYP_OK )
1460 _computeState = READY_TO_COMPUTE;
1464 case COMPUTE_SUBMESH:
1468 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1471 MESSAGE("***** verify compute state *****");
1472 _computeState = NOT_READY;
1473 setAlgoState(MISSING_HYP);
1476 TopoDS_Shape shape = _subShape;
1477 algo->SubMeshesToCompute().assign( 1, this );
1478 // check submeshes needed
1479 if (_father->HasShapeToMesh() ) {
1480 bool subComputed = false, subFailed = false;
1481 if (!algo->OnlyUnaryInput()) {
1482 if ( event == COMPUTE /*&&
1483 ( algo->NeedDiscreteBoundary() || algo->SupportSubmeshes() )*/)
1484 shape = getCollection( gen, algo, subComputed, subFailed, algo->SubMeshesToCompute());
1486 subComputed = SubMeshesComputed( & subFailed );
1489 subComputed = SubMeshesComputed();
1491 ret = ( algo->NeedDiscreteBoundary() ? subComputed :
1492 algo->SupportSubmeshes() ? !subFailed :
1493 ( !subComputed || _father->IsNotConformAllowed() ));
1496 _computeState = FAILED_TO_COMPUTE;
1497 if ( !algo->NeedDiscreteBoundary() && !subFailed )
1499 SMESH_ComputeError::New(COMPERR_BAD_INPUT_MESH,
1500 "Unexpected computed sub-mesh",algo);
1506 // to restore cout that may be redirected by algo
1507 std::streambuf* coutBuffer = std::cout.rdbuf();
1509 //cleanDependants(); for "UseExisting_*D" algos
1510 //removeSubMeshElementsAndNodes();
1511 loadDependentMeshes();
1513 _computeState = FAILED_TO_COMPUTE;
1514 _computeError = SMESH_ComputeError::New(COMPERR_OK,"",algo);
1518 algo->InitComputeError();
1520 MemoryReserve aMemoryReserve;
1521 SMDS_Mesh::CheckMemory();
1522 Kernel_Utils::Localizer loc;
1523 if ( !_father->HasShapeToMesh() ) // no shape
1525 SMESH_MesherHelper helper( *_father );
1526 helper.SetSubShape( shape );
1527 helper.SetElementsOnShape( true );
1528 ret = algo->Compute(*_father, &helper );
1532 ret = algo->Compute((*_father), shape);
1534 // algo can set _computeError of submesh
1535 _computeError = SMESH_ComputeError::Worst( _computeError, algo->GetComputeError() );
1537 catch ( ::SMESH_ComputeError& comperr ) {
1538 cout << " SMESH_ComputeError caught" << endl;
1539 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1540 *_computeError = comperr;
1542 catch ( std::bad_alloc& exc ) {
1543 MESSAGE("std::bad_alloc thrown inside algo->Compute()");
1544 if ( _computeError ) {
1545 _computeError->myName = COMPERR_MEMORY_PB;
1546 //_computeError->myComment = exc.what();
1548 cleanSubMesh( this );
1551 catch ( Standard_OutOfMemory& exc ) {
1552 MESSAGE("Standard_OutOfMemory thrown inside algo->Compute()");
1553 if ( _computeError ) {
1554 _computeError->myName = COMPERR_MEMORY_PB;
1555 //_computeError->myComment = exc.what();
1557 cleanSubMesh( this );
1558 throw std::bad_alloc();
1560 catch (Standard_Failure& ex) {
1561 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1562 _computeError->myName = COMPERR_OCC_EXCEPTION;
1563 _computeError->myComment += ex.DynamicType()->Name();
1564 if ( ex.GetMessageString() && strlen( ex.GetMessageString() )) {
1565 _computeError->myComment += ": ";
1566 _computeError->myComment += ex.GetMessageString();
1569 catch ( SALOME_Exception& S_ex ) {
1570 const int skipSalomeShift = 7; /* to skip "Salome " of
1571 "Salome Exception" prefix returned
1572 by SALOME_Exception::what() */
1573 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1574 _computeError->myName = COMPERR_SLM_EXCEPTION;
1575 _computeError->myComment = S_ex.what() + skipSalomeShift;
1577 catch ( std::exception& exc ) {
1578 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1579 _computeError->myName = COMPERR_STD_EXCEPTION;
1580 _computeError->myComment = exc.what();
1583 if ( _computeError )
1584 _computeError->myName = COMPERR_EXCEPTION;
1588 std::cout.rdbuf( coutBuffer ); // restore cout that could be redirected by algo
1590 // check if an error reported on any sub-shape
1591 bool isComputeErrorSet = !checkComputeError( algo, ret, shape );
1592 if ( isComputeErrorSet )
1594 // check if anything was built
1595 TopExp_Explorer subS(shape, _subShape.ShapeType());
1598 for (; ret && subS.More(); subS.Next())
1599 if ( !_father->GetSubMesh( subS.Current() )->IsMeshComputed() &&
1600 ( _subShape.ShapeType() != TopAbs_EDGE ||
1601 !algo->isDegenerated( TopoDS::Edge( subS.Current() ))))
1604 // Set _computeError
1605 if (!ret && !isComputeErrorSet)
1607 for (subS.ReInit(); subS.More(); subS.Next())
1609 SMESH_subMesh* sm = _father->GetSubMesh( subS.Current() );
1610 if ( !sm->IsMeshComputed() )
1612 if ( !sm->_computeError )
1613 sm->_computeError = SMESH_ComputeError::New();
1614 if ( sm->_computeError->IsOK() )
1615 sm->_computeError->myName = COMPERR_ALGO_FAILED;
1616 sm->_computeState = FAILED_TO_COMPUTE;
1617 sm->_computeError->myAlgo = algo;
1621 if (ret && _computeError && _computeError->myName != COMPERR_WARNING )
1623 _computeError.reset();
1626 // send event SUBMESH_COMPUTED
1628 if ( !algo->NeedDiscreteBoundary() )
1629 // send SUBMESH_COMPUTED to dependants of all sub-meshes of shape
1630 for (subS.ReInit(); subS.More(); subS.Next())
1632 SMESH_subMesh* sm = _father->GetSubMesh( subS.Current() );
1633 SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(false,false);
1634 while ( smIt->more() ) {
1636 if ( sm->GetSubShape().ShapeType() == TopAbs_VERTEX )
1637 sm->updateDependantsState( SUBMESH_COMPUTED );
1643 updateDependantsState( SUBMESH_COMPUTED );
1647 case COMPUTE_CANCELED: // nothing to do
1651 removeSubMeshElementsAndNodes();
1652 _computeState = NOT_READY;
1656 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1658 _computeState = READY_TO_COMPUTE;
1660 setAlgoState(MISSING_HYP);
1663 case SUBMESH_COMPUTED: // nothing to do
1665 case SUBMESH_RESTORED:
1666 // check if a mesh is already computed that may
1667 // happen after retrieval from a file
1668 ComputeStateEngine( CHECK_COMPUTE_STATE );
1669 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1671 if (algo) algo->SubmeshRestored( this );
1673 case MESH_ENTITY_REMOVED:
1675 case SUBMESH_LOADED:
1676 loadDependentMeshes();
1677 ComputeSubMeshStateEngine( SUBMESH_LOADED );
1679 case CHECK_COMPUTE_STATE:
1680 if ( IsMeshComputed() )
1681 _computeState = COMPUTE_OK;
1682 else if ( _computeError && _computeError->IsKO() )
1683 _computeState = FAILED_TO_COMPUTE;
1691 // ----------------------------------------------------------------------
1696 case MODIF_ALGO_STATE:
1697 ComputeStateEngine( CLEAN );
1699 if (algo && !algo->NeedDiscreteBoundary())
1700 cleanDependsOn( algo ); // clean sub-meshes with event CLEAN
1702 case COMPUTE: // nothing to do
1704 case COMPUTE_CANCELED: // nothing to do
1707 cleanDependants(); // clean sub-meshes, dependant on this one, with event CLEAN
1708 removeSubMeshElementsAndNodes();
1709 _computeState = NOT_READY;
1710 if ( _algoState == HYP_OK )
1711 _computeState = READY_TO_COMPUTE;
1713 case SUBMESH_COMPUTED: // nothing to do
1715 case SUBMESH_RESTORED:
1716 ComputeStateEngine( CHECK_COMPUTE_STATE );
1717 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1719 if (algo) algo->SubmeshRestored( this );
1721 case MESH_ENTITY_REMOVED:
1722 updateDependantsState ( CHECK_COMPUTE_STATE );
1723 ComputeStateEngine ( CHECK_COMPUTE_STATE );
1724 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1726 case CHECK_COMPUTE_STATE:
1727 if ( !IsMeshComputed() ) {
1728 if (_algoState == HYP_OK)
1729 _computeState = READY_TO_COMPUTE;
1731 _computeState = NOT_READY;
1734 case SUBMESH_LOADED:
1735 // already treated event, thanks to which _computeState == COMPUTE_OK
1743 // ----------------------------------------------------------------------
1745 case FAILED_TO_COMPUTE:
1748 case MODIF_ALGO_STATE:
1750 ComputeStateEngine( CLEAN );
1752 if (algo && !algo->NeedDiscreteBoundary())
1753 cleanDependsOn( algo ); // clean sub-meshes with event CLEAN
1754 if (_algoState == HYP_OK)
1755 _computeState = READY_TO_COMPUTE;
1757 _computeState = NOT_READY;
1759 case COMPUTE: // nothing to do
1760 case COMPUTE_SUBMESH:
1762 case COMPUTE_CANCELED:
1765 algo->CancelCompute();
1769 cleanDependants(); // submeshes dependent on me should be cleaned as well
1770 removeSubMeshElementsAndNodes();
1772 case SUBMESH_COMPUTED: // allow retry compute
1773 if ( IsEmpty() ) // 23061
1775 if (_algoState == HYP_OK)
1776 _computeState = READY_TO_COMPUTE;
1778 _computeState = NOT_READY;
1781 case SUBMESH_RESTORED:
1782 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1784 case MESH_ENTITY_REMOVED:
1786 case CHECK_COMPUTE_STATE:
1787 if ( IsMeshComputed() )
1788 _computeState = COMPUTE_OK;
1790 if (_algoState == HYP_OK)
1791 _computeState = READY_TO_COMPUTE;
1793 _computeState = NOT_READY;
1795 // case SUBMESH_LOADED:
1803 // ----------------------------------------------------------------------
1809 notifyListenersOnEvent( event, COMPUTE_EVENT );
1815 //=============================================================================
1819 //=============================================================================
1821 bool SMESH_subMesh::Evaluate(MapShapeNbElems& aResMap)
1823 _computeError.reset();
1827 if (_subShape.ShapeType() == TopAbs_VERTEX) {
1828 vector<int> aVec(SMDSEntity_Last,0);
1829 aVec[SMDSEntity_Node] = 1;
1830 aResMap.insert(make_pair(this,aVec));
1834 //SMESH_Gen *gen = _father->GetGen();
1835 SMESH_Algo *algo = 0;
1836 SMESH_Hypothesis::Hypothesis_Status hyp_status;
1839 if( algo && !aResMap.count( this ))
1841 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1842 if (!ret) return false;
1844 if (_father->HasShapeToMesh() && algo->NeedDiscreteBoundary() )
1846 // check submeshes needed
1847 bool subMeshEvaluated = true;
1848 int dimToCheck = SMESH_Gen::GetShapeDim( _subShape ) - 1;
1849 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,/*complexShapeFirst=*/true);
1850 while ( smIt->more() && subMeshEvaluated )
1852 SMESH_subMesh* sm = smIt->next();
1853 int dim = SMESH_Gen::GetShapeDim( sm->GetSubShape() );
1854 if (dim < dimToCheck) break; // the rest subMeshes are all of less dimension
1855 const vector<int> & nbs = aResMap[ sm ];
1856 subMeshEvaluated = (std::accumulate( nbs.begin(), nbs.end(), 0 ) > 0 );
1858 if ( !subMeshEvaluated )
1861 _computeError = SMESH_ComputeError::New(COMPERR_OK,"",algo);
1863 if ( IsMeshComputed() )
1865 vector<int> & nbEntities = aResMap[ this ];
1866 nbEntities.resize( SMDSEntity_Last, 0 );
1867 if ( SMESHDS_SubMesh* sm = GetSubMeshDS() )
1869 nbEntities[ SMDSEntity_Node ] = sm->NbNodes();
1870 SMDS_ElemIteratorPtr elemIt = sm->GetElements();
1871 while ( elemIt->more() )
1872 nbEntities[ elemIt->next()->GetEntityType() ]++;
1877 ret = algo->Evaluate((*_father), _subShape, aResMap);
1879 aResMap.insert( make_pair( this,vector<int>(0)));
1886 //=======================================================================
1888 * \brief Update compute_state by _computeError and send proper events to
1889 * dependent submeshes
1890 * \retval bool - true if _computeError is NOT set
1892 //=======================================================================
1894 bool SMESH_subMesh::checkComputeError(SMESH_Algo* theAlgo,
1895 const bool theComputeOK,
1896 const TopoDS_Shape& theShape)
1898 bool noErrors = true;
1900 if ( !theShape.IsNull() )
1902 // Check state of submeshes
1903 if ( !theAlgo->NeedDiscreteBoundary())
1905 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1906 while ( smIt->more() )
1907 if ( !smIt->next()->checkComputeError( theAlgo, theComputeOK ))
1911 // Check state of neighbours
1912 if ( !theAlgo->OnlyUnaryInput() &&
1913 theShape.ShapeType() == TopAbs_COMPOUND &&
1914 !theShape.IsSame( _subShape ))
1916 for (TopoDS_Iterator subIt( theShape ); subIt.More(); subIt.Next()) {
1917 SMESH_subMesh* sm = _father->GetSubMesh( subIt.Value() );
1919 if ( !sm->checkComputeError( theAlgo, theComputeOK, sm->GetSubShape() ))
1921 updateDependantsState( SUBMESH_COMPUTED ); // send event SUBMESH_COMPUTED
1928 // Set my _computeState
1930 if ( !_computeError || _computeError->IsOK() )
1932 // no error description is set to this sub-mesh, check if any mesh is computed
1933 _computeState = IsMeshComputed() ? COMPUTE_OK : FAILED_TO_COMPUTE;
1934 if ( _computeState != COMPUTE_OK )
1936 if ( _subShape.ShapeType() == TopAbs_EDGE &&
1937 SMESH_Algo::isDegenerated( TopoDS::Edge( _subShape )) )
1938 _computeState = COMPUTE_OK;
1939 else if ( theComputeOK )
1940 _computeError = SMESH_ComputeError::New(COMPERR_NO_MESH_ON_SHAPE,"",theAlgo);
1944 if ( _computeError && !_computeError->IsOK() )
1946 if ( !_computeError->myAlgo )
1947 _computeError->myAlgo = theAlgo;
1951 text << theAlgo->GetName() << " failed on sub-shape #" << _Id << " with error ";
1952 if (_computeError->IsCommon() )
1953 text << _computeError->CommonName();
1955 text << _computeError->myName;
1956 if ( _computeError->myComment.size() > 0 )
1957 text << " \"" << _computeError->myComment << "\"";
1961 _computeState = _computeError->IsKO() ? FAILED_TO_COMPUTE : COMPUTE_OK;
1969 //=======================================================================
1970 //function : updateSubMeshState
1972 //=======================================================================
1974 void SMESH_subMesh::updateSubMeshState(const compute_state theState)
1976 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1977 while ( smIt->more() )
1978 smIt->next()->_computeState = theState;
1981 //=======================================================================
1982 //function : ComputeSubMeshStateEngine
1984 //=======================================================================
1986 void SMESH_subMesh::ComputeSubMeshStateEngine(int event, const bool includeSelf)
1988 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(includeSelf,false);
1989 while ( smIt->more() )
1990 smIt->next()->ComputeStateEngine(event);
1993 //=======================================================================
1994 //function : updateDependantsState
1996 //=======================================================================
1998 void SMESH_subMesh::updateDependantsState(const compute_event theEvent)
2000 const std::vector< SMESH_subMesh * > & ancestors = GetAncestors();
2001 for ( size_t iA = 0; iA < ancestors.size(); ++iA )
2003 ancestors[ iA ]->ComputeStateEngine( theEvent );
2007 //=======================================================================
2008 //function : cleanDependants
2010 //=======================================================================
2012 void SMESH_subMesh::cleanDependants()
2014 int dimToClean = SMESH_Gen::GetShapeDim( _subShape ) + 1;
2016 const std::vector< SMESH_subMesh * > & ancestors = GetAncestors();
2017 for ( size_t iA = 0; iA < ancestors.size(); ++iA )
2019 const TopoDS_Shape& ancestor = ancestors[ iA ]->GetSubShape();
2020 if ( SMESH_Gen::GetShapeDim( ancestor ) == dimToClean )
2022 // PAL8021. do not go upper than SOLID, else ComputeStateEngine(CLEAN)
2023 // will erase mesh on other shapes in a compound
2024 if ( ancestor.ShapeType() >= TopAbs_SOLID &&
2025 !ancestors[ iA ]->IsEmpty() ) // prevent infinite CLEAN via event lesteners
2026 ancestors[ iA ]->ComputeStateEngine(CLEAN);
2031 //=======================================================================
2032 //function : removeSubMeshElementsAndNodes
2034 //=======================================================================
2036 void SMESH_subMesh::removeSubMeshElementsAndNodes()
2038 cleanSubMesh( this );
2040 // algo may bind a submesh not to _subShape, eg 3D algo
2041 // sets nodes on SHELL while _subShape may be SOLID
2043 int dim = SMESH_Gen::GetShapeDim( _subShape );
2044 int type = _subShape.ShapeType() + 1;
2045 for ( ; type <= TopAbs_EDGE; type++) {
2046 if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
2048 TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
2049 for ( ; exp.More(); exp.Next() )
2050 cleanSubMesh( _father->GetSubMeshContaining( exp.Current() ));
2057 //=======================================================================
2058 //function : getCollection
2059 //purpose : return a shape containing all sub-shapes of the MainShape that can be
2060 // meshed at once along with _subShape
2061 //=======================================================================
2063 TopoDS_Shape SMESH_subMesh::getCollection(SMESH_Gen * theGen,
2064 SMESH_Algo* theAlgo,
2065 bool & theSubComputed,
2066 bool & theSubFailed,
2067 std::vector<SMESH_subMesh*>& theSubs)
2069 theSubComputed = SubMeshesComputed( & theSubFailed );
2071 TopoDS_Shape mainShape = _father->GetMeshDS()->ShapeToMesh();
2073 if ( mainShape.IsSame( _subShape ))
2076 const bool skipAuxHyps = false;
2077 list<const SMESHDS_Hypothesis*> aUsedHyp =
2078 theAlgo->GetUsedHypothesis( *_father, _subShape, skipAuxHyps ); // copy
2080 // put in a compound all shapes with the same hypothesis assigned
2081 // and a good ComputeState
2083 TopoDS_Compound aCompound;
2084 BRep_Builder aBuilder;
2085 aBuilder.MakeCompound( aCompound );
2089 SMESH_subMeshIteratorPtr smIt = _father->GetSubMesh( mainShape )->getDependsOnIterator(false);
2090 while ( smIt->more() )
2092 SMESH_subMesh* subMesh = smIt->next();
2093 const TopoDS_Shape& S = subMesh->_subShape;
2094 if ( S.ShapeType() != this->_subShape.ShapeType() )
2096 theSubs.push_back( subMesh );
2097 if ( subMesh == this )
2099 aBuilder.Add( aCompound, S );
2101 else if ( subMesh->GetComputeState() == READY_TO_COMPUTE )
2103 SMESH_Algo* anAlgo = subMesh->GetAlgo();
2104 if (( anAlgo->IsSameName( *theAlgo )) && // same algo
2105 ( anAlgo->GetUsedHypothesis( *_father, S, skipAuxHyps ) == aUsedHyp )) // same hyps
2107 aBuilder.Add( aCompound, S );
2108 if ( !subMesh->SubMeshesComputed() )
2109 theSubComputed = false;
2117 //=======================================================================
2118 //function : getSimilarAttached
2119 //purpose : return a hypothesis attached to theShape.
2120 // If theHyp is provided, similar but not same hypotheses
2121 // is returned; else only applicable ones having theHypType
2123 //=======================================================================
2125 const SMESH_Hypothesis* SMESH_subMesh::getSimilarAttached(const TopoDS_Shape& theShape,
2126 const SMESH_Hypothesis * theHyp,
2127 const int theHypType)
2129 SMESH_HypoFilter hypoKind;
2130 hypoKind.Init( hypoKind.HasType( theHyp ? theHyp->GetType() : theHypType ));
2132 hypoKind.And ( hypoKind.HasDim( theHyp->GetDim() ));
2133 hypoKind.AndNot( hypoKind.Is( theHyp ));
2134 if ( theHyp->IsAuxiliary() )
2135 hypoKind.And( hypoKind.HasName( theHyp->GetName() ));
2137 hypoKind.AndNot( hypoKind.IsAuxiliary());
2140 hypoKind.And( hypoKind.IsApplicableTo( theShape ));
2143 return _father->GetHypothesis( theShape, hypoKind, false );
2146 //=======================================================================
2147 //function : CheckConcurentHypothesis
2148 //purpose : check if there are several applicable hypothesis attached to
2150 //=======================================================================
2152 SMESH_Hypothesis::Hypothesis_Status
2153 SMESH_subMesh::CheckConcurentHypothesis (const int theHypType)
2155 MESSAGE ("SMESH_subMesh::CheckConcurentHypothesis");
2157 // is there local hypothesis on me?
2158 if ( getSimilarAttached( _subShape, 0, theHypType ) )
2159 return SMESH_Hypothesis::HYP_OK;
2162 TopoDS_Shape aPrevWithHyp;
2163 const SMESH_Hypothesis* aPrevHyp = 0;
2164 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
2165 for (; it.More(); it.Next())
2167 const TopoDS_Shape& ancestor = it.Value();
2168 const SMESH_Hypothesis* hyp = getSimilarAttached( ancestor, 0, theHypType );
2171 if ( aPrevWithHyp.IsNull() || aPrevWithHyp.IsSame( ancestor ))
2173 aPrevWithHyp = ancestor;
2176 else if ( aPrevWithHyp.ShapeType() == ancestor.ShapeType() && aPrevHyp != hyp )
2177 return SMESH_Hypothesis::HYP_CONCURENT;
2179 return SMESH_Hypothesis::HYP_OK;
2182 return SMESH_Hypothesis::HYP_OK;
2185 //================================================================================
2187 * \brief Constructor of OwnListenerData
2189 //================================================================================
2191 SMESH_subMesh::OwnListenerData::OwnListenerData( SMESH_subMesh* sm, EventListener* el):
2193 myMeshID( sm ? sm->GetFather()->GetId() : -1 ),
2194 mySubMeshID( sm ? sm->GetId() : -1 ),
2199 //================================================================================
2201 * \brief Sets an event listener and its data to a submesh
2202 * \param listener - the listener to store
2203 * \param data - the listener data to store
2204 * \param where - the submesh to store the listener and it's data
2206 * It remembers the submesh where it puts the listener in order to delete
2207 * them when HYP_OK algo_state is lost
2208 * After being set, event listener is notified on each event of where submesh.
2210 //================================================================================
2212 void SMESH_subMesh::SetEventListener(EventListener* listener,
2213 EventListenerData* data,
2214 SMESH_subMesh* where)
2216 if ( listener && where ) {
2217 where->setEventListener( listener, data );
2218 _ownListeners.push_back( OwnListenerData( where, listener ));
2222 //================================================================================
2224 * \brief Sets an event listener and its data to a submesh
2225 * \param listener - the listener to store
2226 * \param data - the listener data to store
2228 * After being set, event listener is notified on each event of a submesh.
2230 //================================================================================
2232 void SMESH_subMesh::setEventListener(EventListener* listener,
2233 EventListenerData* data)
2235 map< EventListener*, EventListenerData* >::iterator l_d =
2236 _eventListeners.find( listener );
2237 if ( l_d != _eventListeners.end() ) {
2238 EventListenerData* curData = l_d->second;
2239 if ( curData && curData != data && curData->IsDeletable() )
2245 for ( l_d = _eventListeners.begin(); l_d != _eventListeners.end(); ++l_d )
2246 if ( listener->GetName() == l_d->first->GetName() )
2248 EventListenerData* curData = l_d->second;
2249 if ( curData && curData != data && curData->IsDeletable() )
2251 if ( l_d->first != listener && l_d->first->IsDeletable() )
2253 _eventListeners.erase( l_d );
2256 _eventListeners.insert( make_pair( listener, data ));
2260 //================================================================================
2262 * \brief Return an event listener data
2263 * \param listener - the listener whose data is
2264 * \param myOwn - if \c true, returns a listener set by this sub-mesh,
2265 * else returns a listener listening to events of this sub-mesh
2266 * \retval EventListenerData* - found data, maybe NULL
2268 //================================================================================
2270 EventListenerData* SMESH_subMesh::GetEventListenerData(EventListener* listener,
2271 const bool myOwn) const
2275 list< OwnListenerData >::const_iterator d;
2276 for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
2278 if ( d->myListener == listener && _father->MeshExists( d->myMeshID ))
2279 return d->mySubMesh->GetEventListenerData( listener, !myOwn );
2284 map< EventListener*, EventListenerData* >::const_iterator l_d =
2285 _eventListeners.find( listener );
2286 if ( l_d != _eventListeners.end() )
2292 //================================================================================
2294 * \brief Return an event listener data
2295 * \param listenerName - the listener name
2296 * \param myOwn - if \c true, returns a listener set by this sub-mesh,
2297 * else returns a listener listening to events of this sub-mesh
2298 * \retval EventListenerData* - found data, maybe NULL
2300 //================================================================================
2302 EventListenerData* SMESH_subMesh::GetEventListenerData(const string& listenerName,
2303 const bool myOwn) const
2307 list< OwnListenerData >::const_iterator d;
2308 for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
2310 if ( _father->MeshExists( d->myMeshID ) && listenerName == d->myListener->GetName())
2311 return d->mySubMesh->GetEventListenerData( listenerName, !myOwn );
2316 map< EventListener*, EventListenerData* >::const_iterator l_d = _eventListeners.begin();
2317 for ( ; l_d != _eventListeners.end(); ++l_d )
2318 if ( listenerName == l_d->first->GetName() )
2324 //================================================================================
2326 * \brief Notify stored event listeners on the occured event
2327 * \param event - algo_event or compute_event itself
2328 * \param eventType - algo_event or compute_event
2329 * \param hyp - hypothesis, if eventType is algo_event
2331 //================================================================================
2333 void SMESH_subMesh::notifyListenersOnEvent( const int event,
2334 const event_type eventType,
2335 SMESH_Hypothesis* hyp)
2337 list< pair< EventListener*, EventListenerData* > > eventListeners( _eventListeners.begin(),
2338 _eventListeners.end());
2339 list< pair< EventListener*, EventListenerData* > >::iterator l_d = eventListeners.begin();
2340 for ( ; l_d != eventListeners.end(); ++l_d )
2342 std::pair< EventListener*, EventListenerData* > li_da = *l_d;
2343 if ( !_eventListeners.count( li_da.first )) continue;
2345 if ( li_da.first->myBusySM.insert( this ).second )
2347 const bool isDeletable = li_da.first->IsDeletable();
2349 li_da.first->ProcessEvent( event, eventType, this, li_da.second, hyp );
2351 if ( !isDeletable || _eventListeners.count( li_da.first ))
2352 li_da.first->myBusySM.erase( this ); // a listener is hopefully not dead
2357 //================================================================================
2359 * \brief Unregister the listener and delete listener's data
2360 * \param listener - the event listener
2362 //================================================================================
2364 void SMESH_subMesh::DeleteEventListener(EventListener* listener)
2366 map< EventListener*, EventListenerData* >::iterator l_d =
2367 _eventListeners.find( listener );
2368 if ( l_d != _eventListeners.end() && l_d->first )
2370 if ( l_d->second && l_d->second->IsDeletable() )
2374 l_d->first->myBusySM.erase( this );
2375 if ( l_d->first->IsDeletable() )
2377 l_d->first->BeforeDelete( this, l_d->second );
2380 _eventListeners.erase( l_d );
2384 //================================================================================
2386 * \brief Delete event listeners depending on algo of this submesh
2388 //================================================================================
2390 void SMESH_subMesh::deleteOwnListeners()
2392 list< OwnListenerData >::iterator d;
2393 for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
2395 SMESH_Mesh* mesh = _father->FindMesh( d->myMeshID );
2396 if ( !mesh || !mesh->GetSubMeshContaining( d->mySubMeshID ))
2398 d->mySubMesh->DeleteEventListener( d->myListener );
2400 _ownListeners.clear();
2403 //=======================================================================
2404 //function : loadDependentMeshes
2405 //purpose : loads dependent meshes on SUBMESH_LOADED event
2406 //=======================================================================
2408 void SMESH_subMesh::loadDependentMeshes()
2410 list< OwnListenerData >::iterator d;
2411 for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
2412 if ( _father != d->mySubMesh->_father )
2413 d->mySubMesh->_father->Load();
2415 // map< EventListener*, EventListenerData* >::iterator l_d = _eventListeners.begin();
2416 // for ( ; l_d != _eventListeners.end(); ++l_d )
2417 // if ( l_d->second )
2419 // const list<SMESH_subMesh*>& smList = l_d->second->mySubMeshes;
2420 // list<SMESH_subMesh*>::const_iterator sm = smList.begin();
2421 // for ( ; sm != smList.end(); ++sm )
2422 // if ( _father != (*sm)->_father )
2423 // (*sm)->_father->Load();
2427 //================================================================================
2429 * \brief Do something on a certain event
2430 * \param event - algo_event or compute_event itself
2431 * \param eventType - algo_event or compute_event
2432 * \param subMesh - the submesh where the event occures
2433 * \param data - listener data stored in the subMesh
2434 * \param hyp - hypothesis, if eventType is algo_event
2436 * The base implementation translates CLEAN event to the subMesh
2437 * stored in listener data. Also it sends SUBMESH_COMPUTED event in case of
2438 * successful COMPUTE event.
2440 //================================================================================
2442 void SMESH_subMeshEventListener::ProcessEvent(const int event,
2443 const int eventType,
2444 SMESH_subMesh* subMesh,
2445 EventListenerData* data,
2446 const SMESH_Hypothesis* /*hyp*/)
2448 if ( data && !data->mySubMeshes.empty() &&
2449 eventType == SMESH_subMesh::COMPUTE_EVENT)
2451 ASSERT( data->mySubMeshes.front() != subMesh );
2452 list<SMESH_subMesh*>::iterator smIt = data->mySubMeshes.begin();
2453 list<SMESH_subMesh*>::iterator smEnd = data->mySubMeshes.end();
2455 case SMESH_subMesh::CLEAN:
2456 for ( ; smIt != smEnd; ++ smIt)
2457 (*smIt)->ComputeStateEngine( event );
2459 case SMESH_subMesh::COMPUTE:
2460 case SMESH_subMesh::COMPUTE_SUBMESH:
2461 if ( subMesh->GetComputeState() == SMESH_subMesh::COMPUTE_OK )
2462 for ( ; smIt != smEnd; ++ smIt)
2463 (*smIt)->ComputeStateEngine( SMESH_subMesh::SUBMESH_COMPUTED );
2472 //================================================================================
2474 * \brief Iterator over submeshes and optionally prepended or appended one
2476 //================================================================================
2478 struct _Iterator : public SMDS_Iterator<SMESH_subMesh*>
2480 _Iterator(SMDS_Iterator<SMESH_subMesh*>* subIt,
2481 SMESH_subMesh* prepend,
2482 SMESH_subMesh* append): myAppend(append), myIt(subIt)
2484 myCur = prepend ? prepend : myIt->more() ? myIt->next() : append;
2485 if ( myCur == append ) append = 0;
2487 /// Return true if and only if there are other object in this iterator
2492 /// Return the current object and step to the next one
2493 virtual SMESH_subMesh* next()
2495 SMESH_subMesh* res = myCur;
2496 if ( myIt->more() ) { myCur = myIt->next(); }
2497 else { myCur = myAppend; myAppend = 0; }
2504 SMESH_subMesh *myAppend, *myCur;
2505 SMDS_Iterator<SMESH_subMesh*> *myIt;
2509 //================================================================================
2511 * \brief Return iterator on the submeshes this one depends on
2512 * \param includeSelf - this submesh to be returned also
2513 * \param reverse - if true, complex shape submeshes go first
2515 //================================================================================
2517 SMESH_subMeshIteratorPtr SMESH_subMesh::getDependsOnIterator(const bool includeSelf,
2518 const bool reverse) const
2520 SMESH_subMesh *me = (SMESH_subMesh*) this;
2521 SMESH_subMesh *prepend=0, *append=0;
2522 if ( includeSelf ) {
2523 if ( reverse ) prepend = me;
2526 typedef map < int, SMESH_subMesh * > TMap;
2529 return SMESH_subMeshIteratorPtr
2530 ( new _Iterator( new SMDS_mapReverseIterator<TMap>( me->DependsOn() ), prepend, append ));
2533 return SMESH_subMeshIteratorPtr
2534 ( new _Iterator( new SMDS_mapIterator<TMap>( me->DependsOn() ), prepend, append ));
2538 //================================================================================
2540 * \brief Returns ancestor sub-meshes. Finds them if not yet found.
2542 //================================================================================
2544 const std::vector< SMESH_subMesh * > & SMESH_subMesh::GetAncestors() const
2546 if ( _ancestors.empty() &&
2547 !_subShape.IsSame( _father->GetShapeToMesh() ))
2549 const TopTools_ListOfShape& ancShapes = _father->GetAncestors( _subShape );
2551 SMESH_subMesh* me = const_cast< SMESH_subMesh* >( this );
2552 me->_ancestors.reserve( ancShapes.Extent() );
2554 TopTools_MapOfShape map;
2556 for ( TopTools_ListIteratorOfListOfShape it( ancShapes ); it.More(); it.Next() )
2557 if ( SMESH_subMesh* sm = _father->GetSubMeshContaining( it.Value() ))
2558 if ( map.Add( it.Value() ))
2559 me->_ancestors.push_back( sm );
2565 //================================================================================
2567 * \brief Clears the vector of ancestor sub-meshes
2569 //================================================================================
2571 void SMESH_subMesh::ClearAncestors()
2576 //================================================================================
2578 * \brief Find common submeshes (based on shared sub-shapes with other
2579 * \param theOther submesh to check
2580 * \param theSetOfCommon set of common submesh
2582 //================================================================================
2584 bool SMESH_subMesh::FindIntersection(const SMESH_subMesh* theOther,
2585 std::set<const SMESH_subMesh*>& theSetOfCommon ) const
2587 size_t oldNb = theSetOfCommon.size();
2589 // check main submeshes
2590 const map <int, SMESH_subMesh*>::const_iterator otherEnd = theOther->_mapDepend.end();
2591 if ( theOther->_mapDepend.find(this->GetId()) != otherEnd )
2592 theSetOfCommon.insert( this );
2593 if ( _mapDepend.find(theOther->GetId()) != _mapDepend.end() )
2594 theSetOfCommon.insert( theOther );
2596 // check common submeshes
2597 map <int, SMESH_subMesh*>::const_iterator mapIt = _mapDepend.begin();
2598 for( ; mapIt != _mapDepend.end(); mapIt++ )
2599 if ( theOther->_mapDepend.find((*mapIt).first) != otherEnd )
2600 theSetOfCommon.insert( (*mapIt).second );
2601 return oldNb < theSetOfCommon.size();