1 // Copyright (C) 2007-2015 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // SMESH SMESH : implementaion of SMESH idl descriptions
24 // File : SMESH_subMesh.cxx
25 // Author : Paul RASCLE, EDF
28 #include "SMESH_subMesh.hxx"
30 #include "SMESH_Algo.hxx"
31 #include "SMESH_Gen.hxx"
32 #include "SMESH_HypoFilter.hxx"
33 #include "SMESH_Hypothesis.hxx"
34 #include "SMESH_Mesh.hxx"
35 #include "SMESH_MesherHelper.hxx"
36 #include "SMESH_subMeshEventListener.hxx"
37 #include "SMESH_Comment.hxx"
38 #include "SMDS_SetIterator.hxx"
39 #include "SMDSAbs_ElementType.hxx"
41 #include <Basics_OCCTVersion.hxx>
43 #include "utilities.h"
45 #include "Basics_Utils.hxx"
47 #include <BRep_Builder.hxx>
48 #include <BRep_Tool.hxx>
50 #include <TopExp_Explorer.hxx>
51 #include <TopTools_IndexedMapOfShape.hxx>
52 #include <TopTools_ListIteratorOfListOfShape.hxx>
53 #include <TopTools_ListOfShape.hxx>
55 #include <TopoDS_Compound.hxx>
56 #include <TopoDS_Iterator.hxx>
59 #include <Standard_OutOfMemory.hxx>
60 #include <Standard_ErrorHandler.hxx>
66 //=============================================================================
68 * \brief Allocate some memory at construction and release it at destruction.
69 * Is used to be able to continue working after mesh generation breaks due to
72 //=============================================================================
77 MemoryReserve(): myBuf( new char[1024*1024*2] ){}
78 ~MemoryReserve() { delete [] myBuf; }
81 //=============================================================================
83 * default constructor:
85 //=============================================================================
87 SMESH_subMesh::SMESH_subMesh(int Id,
89 SMESHDS_Mesh * meshDS,
90 const TopoDS_Shape & aSubShape)
92 _subShape = aSubShape;
93 _subMeshDS = meshDS->MeshElements(_subShape); // may be null ...
96 _dependenceAnalysed = _alwaysComputed = false;
98 if (_subShape.ShapeType() == TopAbs_VERTEX)
101 _computeState = READY_TO_COMPUTE;
105 _algoState = NO_ALGO;
106 _computeState = NOT_READY;
108 _computeCost = 0; // how costly is to compute this sub-mesh
109 _realComputeCost = 0;
112 //=============================================================================
116 //=============================================================================
118 SMESH_subMesh::~SMESH_subMesh()
120 deleteOwnListeners();
123 //=============================================================================
127 //=============================================================================
129 int SMESH_subMesh::GetId() const
131 //MESSAGE("SMESH_subMesh::GetId");
135 //=============================================================================
139 //=============================================================================
141 SMESHDS_SubMesh * SMESH_subMesh::GetSubMeshDS()
143 // submesh appears in DS only when a mesher set nodes and elements on a shape
144 return _subMeshDS ? _subMeshDS : _subMeshDS = _father->GetMeshDS()->MeshElements(_subShape); // may be null
147 //=============================================================================
151 //=============================================================================
153 const SMESHDS_SubMesh * SMESH_subMesh::GetSubMeshDS() const
155 return ((SMESH_subMesh*) this )->GetSubMeshDS();
158 //=============================================================================
162 //=============================================================================
164 SMESHDS_SubMesh* SMESH_subMesh::CreateSubMeshDS()
166 if ( !GetSubMeshDS() ) {
167 SMESHDS_Mesh* meshDS = _father->GetMeshDS();
168 meshDS->NewSubMesh( meshDS->ShapeToIndex( _subShape ) );
170 return GetSubMeshDS();
173 //=============================================================================
177 //=============================================================================
179 SMESH_subMesh *SMESH_subMesh::GetFirstToCompute()
181 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(true,false);
182 while ( smIt->more() ) {
183 SMESH_subMesh *sm = smIt->next();
184 if ( sm->GetComputeState() == READY_TO_COMPUTE )
187 return 0; // nothing to compute
190 //================================================================================
192 * \brief Returns a current algorithm
194 //================================================================================
196 SMESH_Algo* SMESH_subMesh::GetAlgo() const
200 SMESH_subMesh* me = const_cast< SMESH_subMesh* >( this );
201 me->_algo = _father->GetGen()->GetAlgo( me );
206 //================================================================================
208 * \brief Allow algo->Compute() if a sub-shape of lower dim is meshed but
209 * none mesh entity is bound to it (PAL13615, 2nd part)
211 //================================================================================
213 void SMESH_subMesh::SetIsAlwaysComputed(bool isAlCo)
215 _alwaysComputed = isAlCo;
216 if ( _alwaysComputed )
217 _computeState = COMPUTE_OK;
219 ComputeStateEngine( CHECK_COMPUTE_STATE );
222 //=======================================================================
224 * \brief Return true if no mesh entities is bound to the submesh
226 //=======================================================================
228 bool SMESH_subMesh::IsEmpty() const
230 if (SMESHDS_SubMesh * subMeshDS = ((SMESH_subMesh*)this)->GetSubMeshDS())
231 return (!subMeshDS->NbElements() && !subMeshDS->NbNodes());
235 //=======================================================================
236 //function : IsMeshComputed
237 //purpose : check if _subMeshDS contains mesh elements
238 //=======================================================================
240 bool SMESH_subMesh::IsMeshComputed() const
242 if ( _alwaysComputed )
244 // algo may bind a submesh not to _subShape, eg 3D algo
245 // sets nodes on SHELL while _subShape may be SOLID
247 SMESHDS_Mesh* meshDS = _father->GetMeshDS();
248 int dim = SMESH_Gen::GetShapeDim( _subShape );
249 int type = _subShape.ShapeType();
250 for ( ; type <= TopAbs_VERTEX; type++) {
251 if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
253 TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
254 for ( ; exp.More(); exp.Next() )
256 if ( SMESHDS_SubMesh * smDS = meshDS->MeshElements( exp.Current() ))
258 bool computed = (dim > 0) ? smDS->NbElements() : smDS->NbNodes();
271 //=============================================================================
273 * Return true if all sub-meshes have been meshed
275 //=============================================================================
277 bool SMESH_subMesh::SubMeshesComputed(bool * isFailedToCompute/*=0*/) const
279 int myDim = SMESH_Gen::GetShapeDim( _subShape );
280 int dimToCheck = myDim - 1;
281 bool subMeshesComputed = true;
282 if ( isFailedToCompute ) *isFailedToCompute = false;
283 // check subMeshes with upper dimension => reverse iteration
284 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,true);
285 while ( smIt->more() )
287 SMESH_subMesh *sm = smIt->next();
288 if ( sm->_alwaysComputed )
290 const TopoDS_Shape & ss = sm->GetSubShape();
292 // MSV 07.04.2006: restrict checking to myDim-1 only. Ex., there is no sense
293 // in checking of existence of edges if the algo needs only faces. Moreover,
294 // degenerated edges may have no submesh, as after computing NETGEN_2D.
295 if ( !_algo || _algo->NeedDiscreteBoundary() ) {
296 int dim = SMESH_Gen::GetShapeDim( ss );
297 if (dim < dimToCheck)
298 break; // the rest subMeshes are all of less dimension
300 SMESHDS_SubMesh * ds = sm->GetSubMeshDS();
301 bool computeOk = (sm->GetComputeState() == COMPUTE_OK ||
302 (ds && ( dimToCheck ? ds->NbElements() : ds->NbNodes() )));
305 subMeshesComputed = false;
306 if ( isFailedToCompute && !(*isFailedToCompute) )
307 *isFailedToCompute = ( sm->GetComputeState() == FAILED_TO_COMPUTE );
309 // int type = ss.ShapeType();
313 // case TopAbs_COMPOUND:
315 // MESSAGE("The not computed sub mesh is a COMPOUND");
318 // case TopAbs_COMPSOLID:
320 // MESSAGE("The not computed sub mesh is a COMPSOLID");
323 // case TopAbs_SHELL:
325 // MESSAGE("The not computed sub mesh is a SHEL");
330 // MESSAGE("The not computed sub mesh is a WIRE");
333 // case TopAbs_SOLID:
335 // MESSAGE("The not computed sub mesh is a SOLID");
340 // MESSAGE("The not computed sub mesh is a FACE");
345 // MESSAGE("The not computed sub mesh is a EDGE");
350 // MESSAGE("The not computed sub mesh is of unknown type");
355 if ( !isFailedToCompute )
359 return subMeshesComputed;
362 //================================================================================
364 * \brief Return cost of computing this sub-mesh. If hypotheses are not well defined,
366 * \return int - the computation cost in abstract units.
368 //================================================================================
370 int SMESH_subMesh::GetComputeCost() const
372 return _realComputeCost;
375 //================================================================================
377 * \brief Return cost of computing this sub-mesh. The cost depends on the shape type
378 * and number of sub-meshes this one DependsOn().
379 * \return int - the computation cost in abstract units.
381 //================================================================================
383 int SMESH_subMesh::computeCost() const
388 switch ( _subShape.ShapeType() ) {
390 case TopAbs_SHELL: computeCost = 5000; break;
391 case TopAbs_FACE: computeCost = 500; break;
392 case TopAbs_EDGE: computeCost = 2; break;
393 default: computeCost = 1;
395 SMESH_subMeshIteratorPtr childIt = getDependsOnIterator(/*includeSelf=*/false);
396 while ( childIt->more() )
397 computeCost += childIt->next()->computeCost();
399 ((SMESH_subMesh*)this)->_computeCost = computeCost;
404 //=============================================================================
406 * Returns all sub-meshes this one depend on
408 //=============================================================================
410 const map < int, SMESH_subMesh * >& SMESH_subMesh::DependsOn()
412 if ( _dependenceAnalysed || !_father->HasShapeToMesh() )
415 int type = _subShape.ShapeType();
418 case TopAbs_COMPOUND:
420 list< TopoDS_Shape > compounds( 1, _subShape );
421 list< TopoDS_Shape >::iterator comp = compounds.begin();
422 for ( ; comp != compounds.end(); ++comp )
424 for ( TopoDS_Iterator sub( *comp ); sub.More(); sub.Next() )
425 switch ( sub.Value().ShapeType() )
427 case TopAbs_COMPOUND: compounds.push_back( sub.Value() ); break;
428 case TopAbs_COMPSOLID: insertDependence( sub.Value(), TopAbs_SOLID ); break;
429 case TopAbs_SOLID: insertDependence( sub.Value(), TopAbs_SOLID ); break;
430 case TopAbs_SHELL: insertDependence( sub.Value(), TopAbs_FACE ); break;
431 case TopAbs_FACE: insertDependence( sub.Value(), TopAbs_FACE ); break;
432 case TopAbs_WIRE: insertDependence( sub.Value(), TopAbs_EDGE ); break;
433 case TopAbs_EDGE: insertDependence( sub.Value(), TopAbs_EDGE ); break;
434 case TopAbs_VERTEX: insertDependence( sub.Value(), TopAbs_VERTEX ); break;
440 case TopAbs_COMPSOLID: insertDependence( _subShape, TopAbs_SOLID ); break;
441 case TopAbs_SOLID: insertDependence( _subShape, TopAbs_FACE ); break;
442 case TopAbs_SHELL: insertDependence( _subShape, TopAbs_FACE ); break;
443 case TopAbs_FACE: insertDependence( _subShape, TopAbs_EDGE ); break;
444 case TopAbs_WIRE: insertDependence( _subShape, TopAbs_EDGE ); break;
445 case TopAbs_EDGE: insertDependence( _subShape, TopAbs_VERTEX ); break;
448 _dependenceAnalysed = true;
452 //================================================================================
454 * \brief Return a key for SMESH_subMesh::_mapDepend map
456 //================================================================================
460 int dependsOnMapKey( const SMESH_subMesh* sm )
462 int type = sm->GetSubShape().ShapeType();
463 int ordType = 9 - type; // 2 = Vertex, 8 = CompSolid
464 int cle = sm->GetId();
465 cle += 10000000 * ordType; // sort map by ordType then index
470 //=============================================================================
472 * Add sub-meshes on sub-shapes of a given type into the dependence map.
474 //=============================================================================
476 void SMESH_subMesh::insertDependence(const TopoDS_Shape aShape,
477 TopAbs_ShapeEnum aSubType)
479 TopExp_Explorer sub( aShape, aSubType );
480 for ( ; sub.More(); sub.Next() )
482 SMESH_subMesh *aSubMesh = _father->GetSubMesh( sub.Current() );
483 if ( aSubMesh->GetId() == 0 )
484 continue; // not a sub-shape of the shape to mesh
485 int cle = dependsOnMapKey( aSubMesh );
486 if ( _mapDepend.find( cle ) == _mapDepend.end())
488 _mapDepend[cle] = aSubMesh;
489 const map < int, SMESH_subMesh * > & subMap = aSubMesh->DependsOn();
490 _mapDepend.insert( subMap.begin(), subMap.end() );
495 //================================================================================
497 * \brief Return \c true if \a this sub-mesh depends on \a other
499 //================================================================================
501 bool SMESH_subMesh::DependsOn( const SMESH_subMesh* other ) const
503 return other ? _mapDepend.count( dependsOnMapKey( other )) : false;
506 //=============================================================================
508 * Return a shape of \a this sub-mesh
510 //=============================================================================
512 const TopoDS_Shape & SMESH_subMesh::GetSubShape() const
517 //=======================================================================
518 //function : CanAddHypothesis
519 //purpose : return true if theHypothesis can be attached to me:
520 // its dimention is checked
521 //=======================================================================
523 bool SMESH_subMesh::CanAddHypothesis(const SMESH_Hypothesis* theHypothesis) const
525 int aHypDim = theHypothesis->GetDim();
526 int aShapeDim = SMESH_Gen::GetShapeDim(_subShape);
527 // issue 21106. Forbid 3D mesh on the SHELL
528 // if (aHypDim == 3 && aShapeDim == 3) {
529 // // check case of open shell
530 // //if (_subShape.ShapeType() == TopAbs_SHELL && !_subShape.Closed())
531 // if (_subShape.ShapeType() == TopAbs_SHELL && !BRep_Tool::IsClosed(_subShape))
534 if ( aHypDim <= aShapeDim )
540 //=======================================================================
541 //function : IsApplicableHypotesis
543 //=======================================================================
545 bool SMESH_subMesh::IsApplicableHypotesis(const SMESH_Hypothesis* theHypothesis,
546 const TopAbs_ShapeEnum theShapeType)
548 if ( theHypothesis->GetType() > SMESHDS_Hypothesis::PARAM_ALGO)
551 if ( theHypothesis->GetShapeType() & (1<< theShapeType))
552 // issue 21106. Forbid 3D mesh on the SHELL
553 return !( theHypothesis->GetDim() == 3 && theShapeType == TopAbs_SHELL );
559 switch ( theShapeType ) {
564 return SMESH_Gen::GetShapeDim( theShapeType ) == theHypothesis->GetDim();
567 // Special case for algorithms, building 2D mesh on a whole shell.
568 // Before this fix there was a problem after restoring from study,
569 // because in that case algorithm is assigned before hypothesis
570 // (on shell in problem case) and hypothesis is checked on faces
571 // (because it is 2D), where we have NO_ALGO state.
572 // Now 2D hypothesis is also applicable to shells.
573 return (theHypothesis->GetDim() == 2 || theHypothesis->GetDim() == 3);
576 // case TopAbs_COMPSOLID:
577 // case TopAbs_COMPOUND:
583 //================================================================================
585 * \brief Treats modification of hypotheses definition
586 * \param [in] event - what happens
587 * \param [in] anHyp - a hypothesis
588 * \return SMESH_Hypothesis::Hypothesis_Status - a treatment result.
590 * Optional description of a problematic situation (if any) can be retrieved
591 * via GetComputeError().
593 //================================================================================
595 SMESH_Hypothesis::Hypothesis_Status
596 SMESH_subMesh::AlgoStateEngine(int event, SMESH_Hypothesis * anHyp)
598 // **** les retour des evenement shape sont significatifs
599 // (add ou remove fait ou non)
600 // le retour des evenement father n'indiquent pas que add ou remove fait
602 SMESH_Hypothesis::Hypothesis_Status aux_ret, ret = SMESH_Hypothesis::HYP_OK;
603 if ( _Id == 0 ) return ret; // not a sub-shape of the shape to mesh
605 SMESHDS_Mesh* meshDS =_father->GetMeshDS();
606 SMESH_Algo* algo = 0;
609 if (_subShape.ShapeType() == TopAbs_VERTEX )
611 if ( anHyp->GetDim() != 0) {
612 if (event == ADD_HYP || event == ADD_ALGO)
613 return SMESH_Hypothesis::HYP_BAD_DIM;
615 return SMESH_Hypothesis::HYP_OK;
618 else if ( _algoState == HYP_OK ) {
619 // update default _algoState
620 if ( event != REMOVE_FATHER_ALGO )
622 _algoState = NO_ALGO;
625 _algoState = MISSING_HYP;
626 if ( event == REMOVE_FATHER_HYP ||
627 algo->CheckHypothesis(*_father,_subShape, aux_ret))
634 int oldAlgoState = _algoState;
635 bool modifiedHyp = (event == MODIF_HYP); // if set to true, force event MODIF_ALGO_STATE
636 SMESH_Algo* algoRequiringCleaning = 0;
638 bool isApplicableHyp = IsApplicableHypotesis( anHyp );
640 if (event == ADD_ALGO || event == ADD_FATHER_ALGO)
642 // -------------------------------------------
643 // check if a shape needed by algo is present
644 // -------------------------------------------
645 algo = static_cast< SMESH_Algo* >( anHyp );
646 if ( !_father->HasShapeToMesh() && algo->NeedShape() )
647 return SMESH_Hypothesis::HYP_NEED_SHAPE;
648 // ----------------------
649 // check mesh conformity
650 // ----------------------
651 if (isApplicableHyp && !_father->IsNotConformAllowed() && !IsConform( algo ))
652 return SMESH_Hypothesis::HYP_NOTCONFORM;
654 // check if all-dimensional algo is hidden by other local one
655 if ( event == ADD_ALGO ) {
656 SMESH_HypoFilter filter( SMESH_HypoFilter::HasType( algo->GetType() ));
657 filter.Or( SMESH_HypoFilter::HasType( algo->GetType()+1 ));
658 filter.Or( SMESH_HypoFilter::HasType( algo->GetType()+2 ));
659 if ( SMESH_Algo * curAlgo = (SMESH_Algo*)_father->GetHypothesis( this, filter, true ))
660 if ( !curAlgo->NeedDiscreteBoundary() )
661 algoRequiringCleaning = curAlgo;
665 // ----------------------------------
666 // add a hypothesis to DS if possible
667 // ----------------------------------
668 if (event == ADD_HYP || event == ADD_ALGO)
670 if ( ! CanAddHypothesis( anHyp )) // check dimension
671 return SMESH_Hypothesis::HYP_BAD_DIM;
673 if ( !anHyp->IsAuxiliary() && getSimilarAttached( _subShape, anHyp ) )
674 return SMESH_Hypothesis::HYP_ALREADY_EXIST;
676 if ( !meshDS->AddHypothesis(_subShape, anHyp))
677 return SMESH_Hypothesis::HYP_ALREADY_EXIST;
680 // --------------------------
681 // remove a hypothesis from DS
682 // --------------------------
683 if (event == REMOVE_HYP || event == REMOVE_ALGO)
685 if (!meshDS->RemoveHypothesis(_subShape, anHyp))
686 return SMESH_Hypothesis::HYP_OK; // nothing changes
688 if (event == REMOVE_ALGO)
690 algo = dynamic_cast<SMESH_Algo*> (anHyp);
691 if (!algo->NeedDiscreteBoundary())
692 algoRequiringCleaning = algo;
696 // ------------------
697 // analyse algo state
698 // ------------------
699 if (!isApplicableHyp)
700 return ret; // not applicable hypotheses do not change algo state
702 if (( algo = GetAlgo()))
703 algo->InitComputeError();
708 // ----------------------------------------------------------------------
717 if (algo->CheckHypothesis((*_father),_subShape, aux_ret))
718 setAlgoState(HYP_OK);
719 else if ( algo->IsStatusFatal( aux_ret )) {
720 meshDS->RemoveHypothesis(_subShape, anHyp);
724 setAlgoState(MISSING_HYP);
731 case ADD_FATHER_ALGO: { // Algo just added in father
734 if ( algo == anHyp ) {
735 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret))
736 setAlgoState(HYP_OK);
738 setAlgoState(MISSING_HYP);
742 case REMOVE_FATHER_HYP:
744 case REMOVE_FATHER_ALGO: {
748 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
749 setAlgoState(HYP_OK);
751 setAlgoState(MISSING_HYP);
755 case MODIF_HYP: break;
762 // ----------------------------------------------------------------------
770 if ( algo->CheckHypothesis((*_father),_subShape, ret ))
771 setAlgoState(HYP_OK);
772 if (SMESH_Hypothesis::IsStatusFatal( ret ))
773 meshDS->RemoveHypothesis(_subShape, anHyp);
774 else if (!_father->IsUsedHypothesis( anHyp, this ))
776 meshDS->RemoveHypothesis(_subShape, anHyp);
777 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
781 case ADD_ALGO: { //already existing algo : on father ?
784 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))// ignore hyp status
785 setAlgoState(HYP_OK);
786 else if ( algo->IsStatusFatal( aux_ret )) {
787 meshDS->RemoveHypothesis(_subShape, anHyp);
791 setAlgoState(MISSING_HYP);
796 case REMOVE_ALGO: { // perhaps a father algo applies ?
798 if (algo == NULL) // no more algo applying on sub-shape...
800 setAlgoState(NO_ALGO);
804 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
805 setAlgoState(HYP_OK);
807 setAlgoState(MISSING_HYP);
811 case MODIF_HYP: // assigned hypothesis value may become good
812 case ADD_FATHER_HYP: {
815 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
816 setAlgoState(HYP_OK);
818 setAlgoState(MISSING_HYP);
821 case ADD_FATHER_ALGO: { // new father algo
824 if ( algo == anHyp ) {
825 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
826 setAlgoState(HYP_OK);
828 setAlgoState(MISSING_HYP);
832 case REMOVE_FATHER_HYP: // nothing to do
834 case REMOVE_FATHER_ALGO: {
836 if (algo == NULL) // no more applying algo on father
838 setAlgoState(NO_ALGO);
842 if ( algo->CheckHypothesis((*_father),_subShape , aux_ret ))
843 setAlgoState(HYP_OK);
845 setAlgoState(MISSING_HYP);
855 // ----------------------------------------------------------------------
863 if (!algo->CheckHypothesis((*_father),_subShape, ret ))
865 if ( !SMESH_Hypothesis::IsStatusFatal( ret ))
866 // ret should be fatal: anHyp was not added
867 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
869 else if (!_father->IsUsedHypothesis( anHyp, this ))
870 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
872 if (SMESH_Hypothesis::IsStatusFatal( ret ))
874 MESSAGE("do not add extra hypothesis");
875 meshDS->RemoveHypothesis(_subShape, anHyp);
883 case ADD_ALGO: { //already existing algo : on father ?
885 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
886 // check if algo changes
888 f.Init( SMESH_HypoFilter::IsAlgo() );
889 f.And( SMESH_HypoFilter::IsApplicableTo( _subShape ));
890 f.AndNot( SMESH_HypoFilter::Is( algo ));
891 const SMESH_Hypothesis * prevAlgo = _father->GetHypothesis( this, f, true );
893 string( algo->GetName()) != prevAlgo->GetName())
897 setAlgoState(MISSING_HYP);
903 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
904 setAlgoState(HYP_OK);
906 setAlgoState(MISSING_HYP);
910 case REMOVE_ALGO: { // perhaps a father algo applies ?
912 if (algo == NULL) // no more algo applying on sub-shape...
914 setAlgoState(NO_ALGO);
918 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
919 // check if algo remains
920 if ( anHyp != algo && strcmp( anHyp->GetName(), algo->GetName()) )
924 setAlgoState(MISSING_HYP);
928 case MODIF_HYP: // hypothesis value may become bad
929 case ADD_FATHER_HYP: { // new father hypothesis ?
932 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
934 if (_father->IsUsedHypothesis( anHyp, this )) // new Hyp
938 setAlgoState(MISSING_HYP);
941 case ADD_FATHER_ALGO: {
943 if ( algo == anHyp ) { // a new algo on father
944 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
945 // check if algo changes
947 f.Init( SMESH_HypoFilter::IsAlgo() );
948 f.And( SMESH_HypoFilter::IsApplicableTo( _subShape ));
949 f.AndNot( SMESH_HypoFilter::Is( algo ));
950 const SMESH_Hypothesis* prevAlgo = _father->GetHypothesis( this, f, true );
952 string(algo->GetName()) != string(prevAlgo->GetName()) )
956 setAlgoState(MISSING_HYP);
960 case REMOVE_FATHER_HYP: {
963 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
964 // is there the same local hyp or maybe a new father algo applied?
965 if ( !getSimilarAttached( _subShape, anHyp ) )
969 setAlgoState(MISSING_HYP);
972 case REMOVE_FATHER_ALGO: {
973 // IPAL21346. Edges not removed when Netgen 1d-2d is removed from a SOLID.
974 // CLEAN was not called at event REMOVE_ALGO because the algo is not applicable to SOLID.
975 algo = dynamic_cast<SMESH_Algo*> (anHyp);
976 if (!algo->NeedDiscreteBoundary())
977 algoRequiringCleaning = algo;
979 if (algo == NULL) // no more applying algo on father
981 setAlgoState(NO_ALGO);
985 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
986 // check if algo changes
987 if ( string(algo->GetName()) != string( anHyp->GetName()) )
991 setAlgoState(MISSING_HYP);
1001 // ----------------------------------------------------------------------
1008 // detect algorithm hiding
1010 if ( ret == SMESH_Hypothesis::HYP_OK &&
1011 ( event == ADD_ALGO || event == ADD_FATHER_ALGO ) && algo &&
1012 algo->GetName() == anHyp->GetName() )
1015 SMESH_Gen* gen = _father->GetGen();
1016 const std::vector< SMESH_subMesh * > & ancestors = GetAncestors();
1017 for ( size_t iA = 0; ( ret == SMESH_Hypothesis::HYP_OK && iA < ancestors.size()); ++iA ) {
1018 if ( SMESH_Algo* upperAlgo = ancestors[ iA ]->GetAlgo() )
1019 if ( !upperAlgo->NeedDiscreteBoundary() && !upperAlgo->SupportSubmeshes())
1020 ret = SMESH_Hypothesis::HYP_HIDDEN_ALGO;
1023 if ( ret == SMESH_Hypothesis::HYP_OK &&
1024 !algo->NeedDiscreteBoundary() &&
1025 !algo->SupportSubmeshes())
1027 TopoDS_Shape algoAssignedTo, otherAssignedTo;
1028 gen->GetAlgo( this, &algoAssignedTo );
1029 map<int, SMESH_subMesh*>::reverse_iterator i_sm = _mapDepend.rbegin();
1030 for ( ; ( ret == SMESH_Hypothesis::HYP_OK && i_sm != _mapDepend.rend()) ; ++i_sm )
1031 if ( gen->GetAlgo( i_sm->second, &otherAssignedTo ) &&
1032 SMESH_MesherHelper::IsSubShape( /*sub=*/otherAssignedTo, /*main=*/algoAssignedTo ))
1033 ret = SMESH_Hypothesis::HYP_HIDING_ALGO;
1037 if ( _algo ) { // get an error description set by _algo->CheckHypothesis()
1038 _computeError = _algo->GetComputeError();
1039 _algo->InitComputeError();
1042 bool stateChange = ( _algoState != oldAlgoState );
1044 if ( stateChange && _algoState == HYP_OK ) // hyp becomes OK
1045 algo->SetEventListener( this );
1047 if ( event == REMOVE_ALGO || event == REMOVE_FATHER_ALGO )
1050 notifyListenersOnEvent( event, ALGO_EVENT, anHyp );
1052 if ( stateChange && oldAlgoState == HYP_OK ) { // hyp becomes KO
1053 deleteOwnListeners();
1054 SetIsAlwaysComputed( false );
1055 if (_subShape.ShapeType() == TopAbs_VERTEX ) {
1056 // restore default states
1057 _algoState = HYP_OK;
1058 _computeState = READY_TO_COMPUTE;
1062 if ( algoRequiringCleaning ) {
1063 // added or removed algo is all-dimensional
1064 ComputeStateEngine( CLEAN );
1065 cleanDependsOn( algoRequiringCleaning );
1066 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1069 if ( stateChange || modifiedHyp )
1070 ComputeStateEngine( MODIF_ALGO_STATE );
1072 _realComputeCost = ( _algoState == HYP_OK ) ? computeCost() : 0;
1077 //=======================================================================
1078 //function : IsConform
1079 //purpose : check if a conform mesh will be produced by the Algo
1080 //=======================================================================
1082 bool SMESH_subMesh::IsConform(const SMESH_Algo* theAlgo)
1084 // MESSAGE( "SMESH_subMesh::IsConform" );
1085 if ( !theAlgo ) return false;
1087 // Suppose that theAlgo is applicable to _subShape, do not check it here
1088 //if ( !IsApplicableHypotesis( theAlgo )) return false;
1090 // check only algo that doesn't NeedDiscreteBoundary(): because mesh made
1091 // on a sub-shape will be ignored by theAlgo
1092 if ( theAlgo->NeedDiscreteBoundary() ||
1093 !theAlgo->OnlyUnaryInput() ) // all adjacent shapes will be meshed by this algo?
1096 SMESH_Gen* gen =_father->GetGen();
1098 // only local algo is to be checked
1099 //if ( gen->IsGlobalHypothesis( theAlgo, *_father ))
1100 if ( _subShape.ShapeType() == _father->GetMeshDS()->ShapeToMesh().ShapeType() )
1103 // check algo attached to adjacent shapes
1105 // loop on one level down sub-meshes
1106 TopoDS_Iterator itsub( _subShape );
1107 for (; itsub.More(); itsub.Next())
1109 // loop on adjacent subShapes
1110 const std::vector< SMESH_subMesh * > & ancestors = GetAncestors();
1111 for ( size_t iA = 0; iA < ancestors.size(); ++iA )
1113 const TopoDS_Shape& adjacent = ancestors[ iA ]->GetSubShape();
1114 if ( _subShape.IsSame( adjacent )) continue;
1115 if ( adjacent.ShapeType() != _subShape.ShapeType())
1118 // check algo attached to smAdjacent
1119 SMESH_Algo * algo = ancestors[ iA ]->GetAlgo();
1121 !algo->NeedDiscreteBoundary() &&
1122 algo->OnlyUnaryInput())
1123 return false; // NOT CONFORM MESH WILL BE PRODUCED
1130 //=============================================================================
1134 //=============================================================================
1136 void SMESH_subMesh::setAlgoState(algo_state state)
1141 //================================================================================
1143 * \brief Send an event to sub-meshes
1144 * \param [in] event - the event
1145 * \param [in] anHyp - an hypothesis
1146 * \param [in] exitOnFatal - to stop iteration on sub-meshes if a sub-mesh
1147 * reports a fatal result
1148 * \return SMESH_Hypothesis::Hypothesis_Status - the worst result
1150 * Optional description of a problematic situation (if any) can be retrieved
1151 * via GetComputeError().
1153 //================================================================================
1155 SMESH_Hypothesis::Hypothesis_Status
1156 SMESH_subMesh::SubMeshesAlgoStateEngine(int event,
1157 SMESH_Hypothesis * anHyp,
1160 SMESH_Hypothesis::Hypothesis_Status ret = SMESH_Hypothesis::HYP_OK;
1161 //EAP: a wire (dim==1) should notify edges (dim==1)
1162 //EAP: int dim = SMESH_Gen::GetShapeDim(_subShape);
1163 //if (_subShape.ShapeType() < TopAbs_EDGE ) // wire,face etc
1165 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1166 while ( smIt->more() ) {
1167 SMESH_subMesh* sm = smIt->next();
1168 SMESH_Hypothesis::Hypothesis_Status ret2 = sm->AlgoStateEngine(event, anHyp);
1172 _computeError = sm->_computeError;
1173 sm->_computeError.reset();
1174 if ( exitOnFatal && SMESH_Hypothesis::IsStatusFatal( ret ))
1182 //================================================================================
1184 * \brief Remove elements from sub-meshes.
1185 * \param algoRequiringCleaning - an all-dimensional algorithm whose presence
1186 * causes the cleaning.
1188 //================================================================================
1190 void SMESH_subMesh::cleanDependsOn( SMESH_Algo* algoRequiringCleaning/*=0*/ )
1192 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,
1193 /*complexShapeFirst=*/true);
1194 if ( _father->NbNodes() == 0 )
1196 while ( smIt->more() )
1197 smIt->next()->ComputeStateEngine(CHECK_COMPUTE_STATE);
1199 else if ( !algoRequiringCleaning || !algoRequiringCleaning->SupportSubmeshes() )
1201 while ( smIt->more() )
1202 smIt->next()->ComputeStateEngine(CLEAN);
1204 else if ( algoRequiringCleaning && algoRequiringCleaning->SupportSubmeshes() )
1206 SMESHDS_Mesh* meshDS = _father->GetMeshDS();
1208 // find sub-meshes to keep elements on
1209 set< SMESH_subMesh* > smToKeep;
1210 TopAbs_ShapeEnum prevShapeType = TopAbs_SHAPE;
1211 bool toKeepPrevShapeType = false;
1212 while ( smIt->more() )
1214 SMESH_subMesh* sm = smIt->next();
1215 sm->ComputeStateEngine(CHECK_COMPUTE_STATE);
1216 if ( !sm->IsEmpty() )
1218 const bool sameShapeType = ( prevShapeType == sm->GetSubShape().ShapeType() );
1219 bool keepSubMeshes = ( sameShapeType && toKeepPrevShapeType );
1220 if ( !sameShapeType )
1222 // check if the algo allows presence of global algos of dimension the algo
1223 // can generate it-self
1224 int shapeDim = SMESH_Gen::GetShapeDim( sm->GetSubShape() );
1225 keepSubMeshes = algoRequiringCleaning->NeedLowerHyps( shapeDim );
1226 prevShapeType = sm->GetSubShape().ShapeType();
1227 toKeepPrevShapeType = keepSubMeshes;
1229 if ( !keepSubMeshes )
1231 // look for an algo assigned to sm
1232 bool algoFound = false;
1233 const list<const SMESHDS_Hypothesis*>& hyps = meshDS->GetHypothesis( sm->_subShape );
1234 list<const SMESHDS_Hypothesis*>::const_iterator h = hyps.begin();
1235 for ( ; ( !algoFound && h != hyps.end() ); ++h )
1236 algoFound = ((*h)->GetType() != SMESHDS_Hypothesis::PARAM_ALGO );
1237 keepSubMeshes = algoFound;
1239 // remember all sub-meshes of sm
1240 if ( keepSubMeshes )
1242 SMESH_subMeshIteratorPtr smIt2 = getDependsOnIterator(false);
1243 while ( smIt2->more() )
1244 smToKeep.insert( smIt2->next() );
1249 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,true);
1250 while ( smIt->more() )
1252 SMESH_subMesh* sm = smIt->next();
1253 if ( !smToKeep.count( sm ))
1254 sm->ComputeStateEngine(CLEAN);
1259 //=============================================================================
1263 //=============================================================================
1265 void SMESH_subMesh::DumpAlgoState(bool isMain)
1269 const map < int, SMESH_subMesh * >&subMeshes = DependsOn();
1271 map < int, SMESH_subMesh * >::const_iterator itsub;
1272 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
1274 SMESH_subMesh *sm = (*itsub).second;
1275 sm->DumpAlgoState(false);
1278 MESSAGE("dim = " << SMESH_Gen::GetShapeDim(_subShape) <<
1279 " type of shape " << _subShape.ShapeType());
1282 case NO_ALGO : MESSAGE(" AlgoState = NO_ALGO"); break;
1283 case MISSING_HYP : MESSAGE(" AlgoState = MISSING_HYP"); break;
1284 case HYP_OK : MESSAGE(" AlgoState = HYP_OK");break;
1286 switch (_computeState)
1288 case NOT_READY : MESSAGE(" ComputeState = NOT_READY");break;
1289 case READY_TO_COMPUTE : MESSAGE(" ComputeState = READY_TO_COMPUTE");break;
1290 case COMPUTE_OK : MESSAGE(" ComputeState = COMPUTE_OK");break;
1291 case FAILED_TO_COMPUTE: MESSAGE(" ComputeState = FAILED_TO_COMPUTE");break;
1295 //================================================================================
1297 * \brief Remove nodes and elements bound to submesh
1298 * \param subMesh - submesh containing nodes and elements
1300 //================================================================================
1302 static void cleanSubMesh( SMESH_subMesh * subMesh )
1305 if (SMESHDS_SubMesh * subMeshDS = subMesh->GetSubMeshDS()) {
1306 SMESHDS_Mesh * meshDS = subMesh->GetFather()->GetMeshDS();
1307 SMDS_ElemIteratorPtr ite = subMeshDS->GetElements();
1308 while (ite->more()) {
1309 const SMDS_MeshElement * elt = ite->next();
1310 //MESSAGE( " RM elt: "<<elt->GetID()<<" ( "<<elt->NbNodes()<<" )" );
1311 //meshDS->RemoveElement(elt);
1312 meshDS->RemoveFreeElement(elt, 0);
1315 SMDS_NodeIteratorPtr itn = subMeshDS->GetNodes();
1316 while (itn->more()) {
1317 const SMDS_MeshNode * node = itn->next();
1318 //MESSAGE( " RM node: "<<node->GetID());
1319 if ( node->NbInverseElements() == 0 )
1320 meshDS->RemoveFreeNode(node, 0);
1321 else // for StdMeshers_CompositeSegment_1D: node in one submesh, edge in another
1322 meshDS->RemoveNode(node);
1329 //=============================================================================
1333 //=============================================================================
1335 bool SMESH_subMesh::ComputeStateEngine(int event)
1338 case MODIF_ALGO_STATE:
1340 case COMPUTE_SUBMESH:
1341 //case COMPUTE_CANCELED:
1343 //case SUBMESH_COMPUTED:
1344 //case SUBMESH_RESTORED:
1345 //case SUBMESH_LOADED:
1346 //case MESH_ENTITY_REMOVED:
1347 //case CHECK_COMPUTE_STATE:
1348 _computeError.reset(); break;
1352 if ( event == CLEAN )
1353 _alwaysComputed = false; // Unset 'true' set by MergeNodes() (issue 0022182)
1355 if (_subShape.ShapeType() == TopAbs_VERTEX)
1357 _computeState = READY_TO_COMPUTE;
1358 SMESHDS_SubMesh* smDS = GetSubMeshDS();
1359 if ( smDS && smDS->NbNodes() )
1361 if ( event == CLEAN ) {
1363 cleanSubMesh( this );
1366 _computeState = COMPUTE_OK;
1368 else if (( event == COMPUTE || event == COMPUTE_SUBMESH )
1369 && !_alwaysComputed )
1371 const TopoDS_Vertex & V = TopoDS::Vertex( _subShape );
1372 gp_Pnt P = BRep_Tool::Pnt(V);
1373 if ( SMDS_MeshNode * n = _father->GetMeshDS()->AddNode(P.X(), P.Y(), P.Z()) ) {
1374 _father->GetMeshDS()->SetNodeOnVertex(n,_Id);
1375 _computeState = COMPUTE_OK;
1378 if ( event == MODIF_ALGO_STATE )
1382 SMESH_Gen *gen = _father->GetGen();
1383 SMESH_Algo *algo = 0;
1385 SMESH_Hypothesis::Hypothesis_Status hyp_status;
1386 //algo_state oldAlgoState = (algo_state) GetAlgoState();
1388 switch (_computeState)
1391 // ----------------------------------------------------------------------
1396 case MODIF_ALGO_STATE:
1398 if (algo && !algo->NeedDiscreteBoundary())
1399 cleanDependsOn( algo ); // clean sub-meshes with event CLEAN
1400 if ( _algoState == HYP_OK )
1401 _computeState = READY_TO_COMPUTE;
1403 case COMPUTE: // nothing to do
1404 case COMPUTE_SUBMESH:
1406 case COMPUTE_CANCELED: // nothing to do
1410 removeSubMeshElementsAndNodes();
1412 case SUBMESH_COMPUTED: // nothing to do
1414 case SUBMESH_RESTORED:
1415 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1417 case MESH_ENTITY_REMOVED:
1419 case SUBMESH_LOADED:
1420 loadDependentMeshes();
1421 ComputeSubMeshStateEngine( SUBMESH_LOADED );
1423 case CHECK_COMPUTE_STATE:
1424 if ( IsMeshComputed() )
1425 _computeState = COMPUTE_OK;
1433 // ----------------------------------------------------------------------
1435 case READY_TO_COMPUTE:
1438 case MODIF_ALGO_STATE:
1439 _computeState = NOT_READY;
1443 if (!algo->NeedDiscreteBoundary())
1444 cleanDependsOn( algo ); // clean sub-meshes with event CLEAN
1445 if ( _algoState == HYP_OK )
1446 _computeState = READY_TO_COMPUTE;
1450 case COMPUTE_SUBMESH:
1454 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1457 MESSAGE("***** verify compute state *****");
1458 _computeState = NOT_READY;
1459 setAlgoState(MISSING_HYP);
1462 TopoDS_Shape shape = _subShape;
1463 algo->SubMeshesToCompute().assign( 1, this );
1464 // check submeshes needed
1465 if (_father->HasShapeToMesh() ) {
1466 bool subComputed = false, subFailed = false;
1467 if (!algo->OnlyUnaryInput()) {
1468 if ( event == COMPUTE /*&&
1469 ( algo->NeedDiscreteBoundary() || algo->SupportSubmeshes() )*/)
1470 shape = getCollection( gen, algo, subComputed, subFailed, algo->SubMeshesToCompute());
1472 subComputed = SubMeshesComputed( & subFailed );
1475 subComputed = SubMeshesComputed();
1477 ret = ( algo->NeedDiscreteBoundary() ? subComputed :
1478 algo->SupportSubmeshes() ? !subFailed :
1479 ( !subComputed || _father->IsNotConformAllowed() ));
1482 _computeState = FAILED_TO_COMPUTE;
1483 if ( !algo->NeedDiscreteBoundary() && !subFailed )
1485 SMESH_ComputeError::New(COMPERR_BAD_INPUT_MESH,
1486 "Unexpected computed sub-mesh",algo);
1492 // to restore cout that may be redirected by algo
1493 std::streambuf* coutBuffer = std::cout.rdbuf();
1495 //cleanDependants(); for "UseExisting_*D" algos
1496 //removeSubMeshElementsAndNodes();
1497 loadDependentMeshes();
1499 _computeState = FAILED_TO_COMPUTE;
1500 _computeError = SMESH_ComputeError::New(COMPERR_OK,"",algo);
1504 algo->InitComputeError();
1506 MemoryReserve aMemoryReserve;
1507 SMDS_Mesh::CheckMemory();
1508 Kernel_Utils::Localizer loc;
1509 if ( !_father->HasShapeToMesh() ) // no shape
1511 SMESH_MesherHelper helper( *_father );
1512 helper.SetSubShape( shape );
1513 helper.SetElementsOnShape( true );
1514 ret = algo->Compute(*_father, &helper );
1518 ret = algo->Compute((*_father), shape);
1520 // algo can set _computeError of submesh
1521 _computeError = SMESH_ComputeError::Worst( _computeError, algo->GetComputeError() );
1523 catch ( ::SMESH_ComputeError& comperr ) {
1524 cout << " SMESH_ComputeError caught" << endl;
1525 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1526 *_computeError = comperr;
1528 catch ( std::bad_alloc& exc ) {
1529 MESSAGE("std::bad_alloc thrown inside algo->Compute()");
1530 if ( _computeError ) {
1531 _computeError->myName = COMPERR_MEMORY_PB;
1532 //_computeError->myComment = exc.what();
1534 cleanSubMesh( this );
1537 catch ( Standard_OutOfMemory& exc ) {
1538 MESSAGE("Standard_OutOfMemory thrown inside algo->Compute()");
1539 if ( _computeError ) {
1540 _computeError->myName = COMPERR_MEMORY_PB;
1541 //_computeError->myComment = exc.what();
1543 cleanSubMesh( this );
1544 throw std::bad_alloc();
1546 catch (Standard_Failure& ex) {
1547 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1548 _computeError->myName = COMPERR_OCC_EXCEPTION;
1549 _computeError->myComment += ex.DynamicType()->Name();
1550 if ( ex.GetMessageString() && strlen( ex.GetMessageString() )) {
1551 _computeError->myComment += ": ";
1552 _computeError->myComment += ex.GetMessageString();
1555 catch ( SALOME_Exception& S_ex ) {
1556 const int skipSalomeShift = 7; /* to skip "Salome " of
1557 "Salome Exception" prefix returned
1558 by SALOME_Exception::what() */
1559 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1560 _computeError->myName = COMPERR_SLM_EXCEPTION;
1561 _computeError->myComment = S_ex.what() + skipSalomeShift;
1563 catch ( std::exception& exc ) {
1564 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1565 _computeError->myName = COMPERR_STD_EXCEPTION;
1566 _computeError->myComment = exc.what();
1569 if ( _computeError )
1570 _computeError->myName = COMPERR_EXCEPTION;
1574 std::cout.rdbuf( coutBuffer ); // restore cout that could be redirected by algo
1576 // check if an error reported on any sub-shape
1577 bool isComputeErrorSet = !checkComputeError( algo, ret, shape );
1578 if ( isComputeErrorSet )
1580 // check if anything was built
1581 TopExp_Explorer subS(shape, _subShape.ShapeType());
1584 for (; ret && subS.More(); subS.Next())
1585 if ( !_father->GetSubMesh( subS.Current() )->IsMeshComputed() &&
1586 ( _subShape.ShapeType() != TopAbs_EDGE ||
1587 !algo->isDegenerated( TopoDS::Edge( subS.Current() ))))
1590 // Set _computeError
1591 if (!ret && !isComputeErrorSet)
1593 for (subS.ReInit(); subS.More(); subS.Next())
1595 SMESH_subMesh* sm = _father->GetSubMesh( subS.Current() );
1596 if ( !sm->IsMeshComputed() )
1598 if ( !sm->_computeError )
1599 sm->_computeError = SMESH_ComputeError::New();
1600 if ( sm->_computeError->IsOK() )
1601 sm->_computeError->myName = COMPERR_ALGO_FAILED;
1602 sm->_computeState = FAILED_TO_COMPUTE;
1603 sm->_computeError->myAlgo = algo;
1607 if (ret && _computeError && _computeError->myName != COMPERR_WARNING )
1609 _computeError.reset();
1612 // send event SUBMESH_COMPUTED
1614 if ( !algo->NeedDiscreteBoundary() )
1615 // send SUBMESH_COMPUTED to dependants of all sub-meshes of shape
1616 for (subS.ReInit(); subS.More(); subS.Next())
1618 SMESH_subMesh* sm = _father->GetSubMesh( subS.Current() );
1619 SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(false,false);
1620 while ( smIt->more() ) {
1622 if ( sm->GetSubShape().ShapeType() == TopAbs_VERTEX )
1623 sm->updateDependantsState( SUBMESH_COMPUTED );
1629 updateDependantsState( SUBMESH_COMPUTED );
1633 case COMPUTE_CANCELED: // nothing to do
1637 removeSubMeshElementsAndNodes();
1638 _computeState = NOT_READY;
1642 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1644 _computeState = READY_TO_COMPUTE;
1646 setAlgoState(MISSING_HYP);
1649 case SUBMESH_COMPUTED: // nothing to do
1651 case SUBMESH_RESTORED:
1652 // check if a mesh is already computed that may
1653 // happen after retrieval from a file
1654 ComputeStateEngine( CHECK_COMPUTE_STATE );
1655 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1657 if (algo) algo->SubmeshRestored( this );
1659 case MESH_ENTITY_REMOVED:
1661 case SUBMESH_LOADED:
1662 loadDependentMeshes();
1663 ComputeSubMeshStateEngine( SUBMESH_LOADED );
1665 case CHECK_COMPUTE_STATE:
1666 if ( IsMeshComputed() )
1667 _computeState = COMPUTE_OK;
1668 else if ( _computeError && _computeError->IsKO() )
1669 _computeState = FAILED_TO_COMPUTE;
1677 // ----------------------------------------------------------------------
1682 case MODIF_ALGO_STATE:
1683 ComputeStateEngine( CLEAN );
1685 if (algo && !algo->NeedDiscreteBoundary())
1686 cleanDependsOn( algo ); // clean sub-meshes with event CLEAN
1688 case COMPUTE: // nothing to do
1690 case COMPUTE_CANCELED: // nothing to do
1693 cleanDependants(); // clean sub-meshes, dependant on this one, with event CLEAN
1694 removeSubMeshElementsAndNodes();
1695 _computeState = NOT_READY;
1696 if ( _algoState == HYP_OK )
1697 _computeState = READY_TO_COMPUTE;
1699 case SUBMESH_COMPUTED: // nothing to do
1701 case SUBMESH_RESTORED:
1702 ComputeStateEngine( CHECK_COMPUTE_STATE );
1703 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1705 if (algo) algo->SubmeshRestored( this );
1707 case MESH_ENTITY_REMOVED:
1708 updateDependantsState ( CHECK_COMPUTE_STATE );
1709 ComputeStateEngine ( CHECK_COMPUTE_STATE );
1710 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1712 case CHECK_COMPUTE_STATE:
1713 if ( !IsMeshComputed() ) {
1714 if (_algoState == HYP_OK)
1715 _computeState = READY_TO_COMPUTE;
1717 _computeState = NOT_READY;
1720 case SUBMESH_LOADED:
1721 // already treated event, thanks to which _computeState == COMPUTE_OK
1729 // ----------------------------------------------------------------------
1731 case FAILED_TO_COMPUTE:
1734 case MODIF_ALGO_STATE:
1736 ComputeStateEngine( CLEAN );
1738 if (algo && !algo->NeedDiscreteBoundary())
1739 cleanDependsOn( algo ); // clean sub-meshes with event CLEAN
1740 if (_algoState == HYP_OK)
1741 _computeState = READY_TO_COMPUTE;
1743 _computeState = NOT_READY;
1745 case COMPUTE: // nothing to do
1746 case COMPUTE_SUBMESH:
1748 case COMPUTE_CANCELED:
1751 algo->CancelCompute();
1755 cleanDependants(); // submeshes dependent on me should be cleaned as well
1756 removeSubMeshElementsAndNodes();
1758 case SUBMESH_COMPUTED: // allow retry compute
1759 if ( IsEmpty() ) // 23061
1761 if (_algoState == HYP_OK)
1762 _computeState = READY_TO_COMPUTE;
1764 _computeState = NOT_READY;
1767 case SUBMESH_RESTORED:
1768 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1770 case MESH_ENTITY_REMOVED:
1772 case CHECK_COMPUTE_STATE:
1773 if ( IsMeshComputed() )
1774 _computeState = COMPUTE_OK;
1776 if (_algoState == HYP_OK)
1777 _computeState = READY_TO_COMPUTE;
1779 _computeState = NOT_READY;
1781 // case SUBMESH_LOADED:
1789 // ----------------------------------------------------------------------
1795 notifyListenersOnEvent( event, COMPUTE_EVENT );
1801 //=============================================================================
1805 //=============================================================================
1807 bool SMESH_subMesh::Evaluate(MapShapeNbElems& aResMap)
1809 _computeError.reset();
1813 if (_subShape.ShapeType() == TopAbs_VERTEX) {
1814 vector<int> aVec(SMDSEntity_Last,0);
1815 aVec[SMDSEntity_Node] = 1;
1816 aResMap.insert(make_pair(this,aVec));
1820 //SMESH_Gen *gen = _father->GetGen();
1821 SMESH_Algo *algo = 0;
1822 SMESH_Hypothesis::Hypothesis_Status hyp_status;
1825 if( algo && !aResMap.count( this ))
1827 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1828 if (!ret) return false;
1830 if (_father->HasShapeToMesh() && algo->NeedDiscreteBoundary() )
1832 // check submeshes needed
1833 bool subMeshEvaluated = true;
1834 int dimToCheck = SMESH_Gen::GetShapeDim( _subShape ) - 1;
1835 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,/*complexShapeFirst=*/true);
1836 while ( smIt->more() && subMeshEvaluated )
1838 SMESH_subMesh* sm = smIt->next();
1839 int dim = SMESH_Gen::GetShapeDim( sm->GetSubShape() );
1840 if (dim < dimToCheck) break; // the rest subMeshes are all of less dimension
1841 const vector<int> & nbs = aResMap[ sm ];
1842 subMeshEvaluated = (std::accumulate( nbs.begin(), nbs.end(), 0 ) > 0 );
1844 if ( !subMeshEvaluated )
1847 _computeError = SMESH_ComputeError::New(COMPERR_OK,"",algo);
1849 if ( IsMeshComputed() )
1851 vector<int> & nbEntities = aResMap[ this ];
1852 nbEntities.resize( SMDSEntity_Last, 0 );
1853 if ( SMESHDS_SubMesh* sm = GetSubMeshDS() )
1855 nbEntities[ SMDSEntity_Node ] = sm->NbNodes();
1856 SMDS_ElemIteratorPtr elemIt = sm->GetElements();
1857 while ( elemIt->more() )
1858 nbEntities[ elemIt->next()->GetEntityType() ]++;
1863 ret = algo->Evaluate((*_father), _subShape, aResMap);
1865 aResMap.insert( make_pair( this,vector<int>(0)));
1872 //=======================================================================
1874 * \brief Update compute_state by _computeError and send proper events to
1875 * dependent submeshes
1876 * \retval bool - true if _computeError is NOT set
1878 //=======================================================================
1880 bool SMESH_subMesh::checkComputeError(SMESH_Algo* theAlgo,
1881 const bool theComputeOK,
1882 const TopoDS_Shape& theShape)
1884 bool noErrors = true;
1886 if ( !theShape.IsNull() )
1888 // Check state of submeshes
1889 if ( !theAlgo->NeedDiscreteBoundary())
1891 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1892 while ( smIt->more() )
1893 if ( !smIt->next()->checkComputeError( theAlgo, theComputeOK ))
1897 // Check state of neighbours
1898 if ( !theAlgo->OnlyUnaryInput() &&
1899 theShape.ShapeType() == TopAbs_COMPOUND &&
1900 !theShape.IsSame( _subShape ))
1902 for (TopoDS_Iterator subIt( theShape ); subIt.More(); subIt.Next()) {
1903 SMESH_subMesh* sm = _father->GetSubMesh( subIt.Value() );
1905 if ( !sm->checkComputeError( theAlgo, theComputeOK, sm->GetSubShape() ))
1907 updateDependantsState( SUBMESH_COMPUTED ); // send event SUBMESH_COMPUTED
1914 // Set my _computeState
1916 if ( !_computeError || _computeError->IsOK() )
1918 // no error description is set to this sub-mesh, check if any mesh is computed
1919 _computeState = IsMeshComputed() ? COMPUTE_OK : FAILED_TO_COMPUTE;
1920 if ( _computeState != COMPUTE_OK )
1922 if ( _subShape.ShapeType() == TopAbs_EDGE &&
1923 SMESH_Algo::isDegenerated( TopoDS::Edge( _subShape )) )
1924 _computeState = COMPUTE_OK;
1925 else if ( theComputeOK )
1926 _computeError = SMESH_ComputeError::New(COMPERR_NO_MESH_ON_SHAPE,"",theAlgo);
1930 if ( _computeError && !_computeError->IsOK() )
1932 if ( !_computeError->myAlgo )
1933 _computeError->myAlgo = theAlgo;
1937 text << theAlgo->GetName() << " failed on sub-shape #" << _Id << " with error ";
1938 if (_computeError->IsCommon() )
1939 text << _computeError->CommonName();
1941 text << _computeError->myName;
1942 if ( _computeError->myComment.size() > 0 )
1943 text << " \"" << _computeError->myComment << "\"";
1947 _computeState = _computeError->IsKO() ? FAILED_TO_COMPUTE : COMPUTE_OK;
1955 //=======================================================================
1956 //function : updateSubMeshState
1958 //=======================================================================
1960 void SMESH_subMesh::updateSubMeshState(const compute_state theState)
1962 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1963 while ( smIt->more() )
1964 smIt->next()->_computeState = theState;
1967 //=======================================================================
1968 //function : ComputeSubMeshStateEngine
1970 //=======================================================================
1972 void SMESH_subMesh::ComputeSubMeshStateEngine(int event, const bool includeSelf)
1974 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(includeSelf,false);
1975 while ( smIt->more() )
1976 smIt->next()->ComputeStateEngine(event);
1979 //=======================================================================
1980 //function : updateDependantsState
1982 //=======================================================================
1984 void SMESH_subMesh::updateDependantsState(const compute_event theEvent)
1986 const std::vector< SMESH_subMesh * > & ancestors = GetAncestors();
1987 for ( size_t iA = 0; iA < ancestors.size(); ++iA )
1989 ancestors[ iA ]->ComputeStateEngine( theEvent );
1993 //=======================================================================
1994 //function : cleanDependants
1996 //=======================================================================
1998 void SMESH_subMesh::cleanDependants()
2000 int dimToClean = SMESH_Gen::GetShapeDim( _subShape ) + 1;
2002 const std::vector< SMESH_subMesh * > & ancestors = GetAncestors();
2003 for ( size_t iA = 0; iA < ancestors.size(); ++iA )
2005 const TopoDS_Shape& ancestor = ancestors[ iA ]->GetSubShape();
2006 if ( SMESH_Gen::GetShapeDim( ancestor ) == dimToClean )
2008 // PAL8021. do not go upper than SOLID, else ComputeStateEngine(CLEAN)
2009 // will erase mesh on other shapes in a compound
2010 if ( ancestor.ShapeType() >= TopAbs_SOLID &&
2011 !ancestors[ iA ]->IsEmpty() ) // prevent infinite CLEAN via event lesteners
2012 ancestors[ iA ]->ComputeStateEngine(CLEAN);
2017 //=======================================================================
2018 //function : removeSubMeshElementsAndNodes
2020 //=======================================================================
2022 void SMESH_subMesh::removeSubMeshElementsAndNodes()
2024 cleanSubMesh( this );
2026 // algo may bind a submesh not to _subShape, eg 3D algo
2027 // sets nodes on SHELL while _subShape may be SOLID
2029 int dim = SMESH_Gen::GetShapeDim( _subShape );
2030 int type = _subShape.ShapeType() + 1;
2031 for ( ; type <= TopAbs_EDGE; type++) {
2032 if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
2034 TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
2035 for ( ; exp.More(); exp.Next() )
2036 cleanSubMesh( _father->GetSubMeshContaining( exp.Current() ));
2043 //=======================================================================
2044 //function : getCollection
2045 //purpose : return a shape containing all sub-shapes of the MainShape that can be
2046 // meshed at once along with _subShape
2047 //=======================================================================
2049 TopoDS_Shape SMESH_subMesh::getCollection(SMESH_Gen * theGen,
2050 SMESH_Algo* theAlgo,
2051 bool & theSubComputed,
2052 bool & theSubFailed,
2053 std::vector<SMESH_subMesh*>& theSubs)
2055 theSubComputed = SubMeshesComputed( & theSubFailed );
2057 TopoDS_Shape mainShape = _father->GetMeshDS()->ShapeToMesh();
2059 if ( mainShape.IsSame( _subShape ))
2062 const bool skipAuxHyps = false;
2063 list<const SMESHDS_Hypothesis*> aUsedHyp =
2064 theAlgo->GetUsedHypothesis( *_father, _subShape, skipAuxHyps ); // copy
2066 // put in a compound all shapes with the same hypothesis assigned
2067 // and a good ComputeState
2069 TopoDS_Compound aCompound;
2070 BRep_Builder aBuilder;
2071 aBuilder.MakeCompound( aCompound );
2075 SMESH_subMeshIteratorPtr smIt = _father->GetSubMesh( mainShape )->getDependsOnIterator(false);
2076 while ( smIt->more() )
2078 SMESH_subMesh* subMesh = smIt->next();
2079 const TopoDS_Shape& S = subMesh->_subShape;
2080 if ( S.ShapeType() != this->_subShape.ShapeType() )
2082 theSubs.push_back( subMesh );
2083 if ( subMesh == this )
2085 aBuilder.Add( aCompound, S );
2087 else if ( subMesh->GetComputeState() == READY_TO_COMPUTE )
2089 SMESH_Algo* anAlgo = subMesh->GetAlgo();
2090 if (( anAlgo->IsSameName( *theAlgo )) && // same algo
2091 ( anAlgo->GetUsedHypothesis( *_father, S, skipAuxHyps ) == aUsedHyp )) // same hyps
2093 aBuilder.Add( aCompound, S );
2094 if ( !subMesh->SubMeshesComputed() )
2095 theSubComputed = false;
2103 //=======================================================================
2104 //function : getSimilarAttached
2105 //purpose : return a hypothesis attached to theShape.
2106 // If theHyp is provided, similar but not same hypotheses
2107 // is returned; else only applicable ones having theHypType
2109 //=======================================================================
2111 const SMESH_Hypothesis* SMESH_subMesh::getSimilarAttached(const TopoDS_Shape& theShape,
2112 const SMESH_Hypothesis * theHyp,
2113 const int theHypType)
2115 SMESH_HypoFilter hypoKind;
2116 hypoKind.Init( hypoKind.HasType( theHyp ? theHyp->GetType() : theHypType ));
2118 hypoKind.And ( hypoKind.HasDim( theHyp->GetDim() ));
2119 hypoKind.AndNot( hypoKind.Is( theHyp ));
2120 if ( theHyp->IsAuxiliary() )
2121 hypoKind.And( hypoKind.HasName( theHyp->GetName() ));
2123 hypoKind.AndNot( hypoKind.IsAuxiliary());
2126 hypoKind.And( hypoKind.IsApplicableTo( theShape ));
2129 return _father->GetHypothesis( theShape, hypoKind, false );
2132 //=======================================================================
2133 //function : CheckConcurentHypothesis
2134 //purpose : check if there are several applicable hypothesis attached to
2136 //=======================================================================
2138 SMESH_Hypothesis::Hypothesis_Status
2139 SMESH_subMesh::CheckConcurentHypothesis (const int theHypType)
2141 MESSAGE ("SMESH_subMesh::CheckConcurentHypothesis");
2143 // is there local hypothesis on me?
2144 if ( getSimilarAttached( _subShape, 0, theHypType ) )
2145 return SMESH_Hypothesis::HYP_OK;
2148 TopoDS_Shape aPrevWithHyp;
2149 const SMESH_Hypothesis* aPrevHyp = 0;
2150 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
2151 for (; it.More(); it.Next())
2153 const TopoDS_Shape& ancestor = it.Value();
2154 const SMESH_Hypothesis* hyp = getSimilarAttached( ancestor, 0, theHypType );
2157 if ( aPrevWithHyp.IsNull() || aPrevWithHyp.IsSame( ancestor ))
2159 aPrevWithHyp = ancestor;
2162 else if ( aPrevWithHyp.ShapeType() == ancestor.ShapeType() && aPrevHyp != hyp )
2163 return SMESH_Hypothesis::HYP_CONCURENT;
2165 return SMESH_Hypothesis::HYP_OK;
2168 return SMESH_Hypothesis::HYP_OK;
2171 //================================================================================
2173 * \brief Constructor of OwnListenerData
2175 //================================================================================
2177 SMESH_subMesh::OwnListenerData::OwnListenerData( SMESH_subMesh* sm, EventListener* el):
2179 myMeshID( sm ? sm->GetFather()->GetId() : -1 ),
2180 mySubMeshID( sm ? sm->GetId() : -1 ),
2185 //================================================================================
2187 * \brief Sets an event listener and its data to a submesh
2188 * \param listener - the listener to store
2189 * \param data - the listener data to store
2190 * \param where - the submesh to store the listener and it's data
2192 * It remembers the submesh where it puts the listener in order to delete
2193 * them when HYP_OK algo_state is lost
2194 * After being set, event listener is notified on each event of where submesh.
2196 //================================================================================
2198 void SMESH_subMesh::SetEventListener(EventListener* listener,
2199 EventListenerData* data,
2200 SMESH_subMesh* where)
2202 if ( listener && where ) {
2203 where->setEventListener( listener, data );
2204 _ownListeners.push_back( OwnListenerData( where, listener ));
2208 //================================================================================
2210 * \brief Sets an event listener and its data to a submesh
2211 * \param listener - the listener to store
2212 * \param data - the listener data to store
2214 * After being set, event listener is notified on each event of a submesh.
2216 //================================================================================
2218 void SMESH_subMesh::setEventListener(EventListener* listener,
2219 EventListenerData* data)
2221 map< EventListener*, EventListenerData* >::iterator l_d =
2222 _eventListeners.find( listener );
2223 if ( l_d != _eventListeners.end() ) {
2224 EventListenerData* curData = l_d->second;
2225 if ( curData && curData != data && curData->IsDeletable() )
2231 for ( l_d = _eventListeners.begin(); l_d != _eventListeners.end(); ++l_d )
2232 if ( listener->GetName() == l_d->first->GetName() )
2234 EventListenerData* curData = l_d->second;
2235 if ( curData && curData != data && curData->IsDeletable() )
2237 if ( l_d->first != listener && l_d->first->IsDeletable() )
2239 _eventListeners.erase( l_d );
2242 _eventListeners.insert( make_pair( listener, data ));
2246 //================================================================================
2248 * \brief Return an event listener data
2249 * \param listener - the listener whose data is
2250 * \param myOwn - if \c true, returns a listener set by this sub-mesh,
2251 * else returns a listener listening to events of this sub-mesh
2252 * \retval EventListenerData* - found data, maybe NULL
2254 //================================================================================
2256 EventListenerData* SMESH_subMesh::GetEventListenerData(EventListener* listener,
2257 const bool myOwn) const
2261 list< OwnListenerData >::const_iterator d;
2262 for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
2264 if ( d->myListener == listener && _father->MeshExists( d->myMeshID ))
2265 return d->mySubMesh->GetEventListenerData( listener, !myOwn );
2270 map< EventListener*, EventListenerData* >::const_iterator l_d =
2271 _eventListeners.find( listener );
2272 if ( l_d != _eventListeners.end() )
2278 //================================================================================
2280 * \brief Return an event listener data
2281 * \param listenerName - the listener name
2282 * \param myOwn - if \c true, returns a listener set by this sub-mesh,
2283 * else returns a listener listening to events of this sub-mesh
2284 * \retval EventListenerData* - found data, maybe NULL
2286 //================================================================================
2288 EventListenerData* SMESH_subMesh::GetEventListenerData(const string& listenerName,
2289 const bool myOwn) const
2293 list< OwnListenerData >::const_iterator d;
2294 for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
2296 if ( _father->MeshExists( d->myMeshID ) && listenerName == d->myListener->GetName())
2297 return d->mySubMesh->GetEventListenerData( listenerName, !myOwn );
2302 map< EventListener*, EventListenerData* >::const_iterator l_d = _eventListeners.begin();
2303 for ( ; l_d != _eventListeners.end(); ++l_d )
2304 if ( listenerName == l_d->first->GetName() )
2310 //================================================================================
2312 * \brief Notify stored event listeners on the occured event
2313 * \param event - algo_event or compute_event itself
2314 * \param eventType - algo_event or compute_event
2315 * \param hyp - hypothesis, if eventType is algo_event
2317 //================================================================================
2319 void SMESH_subMesh::notifyListenersOnEvent( const int event,
2320 const event_type eventType,
2321 SMESH_Hypothesis* hyp)
2323 list< pair< EventListener*, EventListenerData* > > eventListeners( _eventListeners.begin(),
2324 _eventListeners.end());
2325 list< pair< EventListener*, EventListenerData* > >::iterator l_d = eventListeners.begin();
2326 for ( ; l_d != eventListeners.end(); ++l_d )
2328 std::pair< EventListener*, EventListenerData* > li_da = *l_d;
2329 if ( !_eventListeners.count( li_da.first )) continue;
2331 if ( li_da.first->myBusySM.insert( this ).second )
2333 const bool isDeletable = li_da.first->IsDeletable();
2335 li_da.first->ProcessEvent( event, eventType, this, li_da.second, hyp );
2337 if ( !isDeletable || _eventListeners.count( li_da.first ))
2338 li_da.first->myBusySM.erase( this ); // a listener is hopefully not dead
2343 //================================================================================
2345 * \brief Unregister the listener and delete listener's data
2346 * \param listener - the event listener
2348 //================================================================================
2350 void SMESH_subMesh::DeleteEventListener(EventListener* listener)
2352 map< EventListener*, EventListenerData* >::iterator l_d =
2353 _eventListeners.find( listener );
2354 if ( l_d != _eventListeners.end() && l_d->first )
2356 if ( l_d->second && l_d->second->IsDeletable() )
2360 l_d->first->myBusySM.erase( this );
2361 if ( l_d->first->IsDeletable() )
2363 l_d->first->BeforeDelete( this, l_d->second );
2366 _eventListeners.erase( l_d );
2370 //================================================================================
2372 * \brief Delete event listeners depending on algo of this submesh
2374 //================================================================================
2376 void SMESH_subMesh::deleteOwnListeners()
2378 list< OwnListenerData >::iterator d;
2379 for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
2381 SMESH_Mesh* mesh = _father->FindMesh( d->myMeshID );
2382 if ( !mesh || !mesh->GetSubMeshContaining( d->mySubMeshID ))
2384 d->mySubMesh->DeleteEventListener( d->myListener );
2386 _ownListeners.clear();
2389 //=======================================================================
2390 //function : loadDependentMeshes
2391 //purpose : loads dependent meshes on SUBMESH_LOADED event
2392 //=======================================================================
2394 void SMESH_subMesh::loadDependentMeshes()
2396 list< OwnListenerData >::iterator d;
2397 for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
2398 if ( _father != d->mySubMesh->_father )
2399 d->mySubMesh->_father->Load();
2401 // map< EventListener*, EventListenerData* >::iterator l_d = _eventListeners.begin();
2402 // for ( ; l_d != _eventListeners.end(); ++l_d )
2403 // if ( l_d->second )
2405 // const list<SMESH_subMesh*>& smList = l_d->second->mySubMeshes;
2406 // list<SMESH_subMesh*>::const_iterator sm = smList.begin();
2407 // for ( ; sm != smList.end(); ++sm )
2408 // if ( _father != (*sm)->_father )
2409 // (*sm)->_father->Load();
2413 //================================================================================
2415 * \brief Do something on a certain event
2416 * \param event - algo_event or compute_event itself
2417 * \param eventType - algo_event or compute_event
2418 * \param subMesh - the submesh where the event occures
2419 * \param data - listener data stored in the subMesh
2420 * \param hyp - hypothesis, if eventType is algo_event
2422 * The base implementation translates CLEAN event to the subMesh
2423 * stored in listener data. Also it sends SUBMESH_COMPUTED event in case of
2424 * successful COMPUTE event.
2426 //================================================================================
2428 void SMESH_subMeshEventListener::ProcessEvent(const int event,
2429 const int eventType,
2430 SMESH_subMesh* subMesh,
2431 EventListenerData* data,
2432 const SMESH_Hypothesis* /*hyp*/)
2434 if ( data && !data->mySubMeshes.empty() &&
2435 eventType == SMESH_subMesh::COMPUTE_EVENT)
2437 ASSERT( data->mySubMeshes.front() != subMesh );
2438 list<SMESH_subMesh*>::iterator smIt = data->mySubMeshes.begin();
2439 list<SMESH_subMesh*>::iterator smEnd = data->mySubMeshes.end();
2441 case SMESH_subMesh::CLEAN:
2442 for ( ; smIt != smEnd; ++ smIt)
2443 (*smIt)->ComputeStateEngine( event );
2445 case SMESH_subMesh::COMPUTE:
2446 case SMESH_subMesh::COMPUTE_SUBMESH:
2447 if ( subMesh->GetComputeState() == SMESH_subMesh::COMPUTE_OK )
2448 for ( ; smIt != smEnd; ++ smIt)
2449 (*smIt)->ComputeStateEngine( SMESH_subMesh::SUBMESH_COMPUTED );
2458 //================================================================================
2460 * \brief Iterator over submeshes and optionally prepended or appended one
2462 //================================================================================
2464 struct _Iterator : public SMDS_Iterator<SMESH_subMesh*>
2466 _Iterator(SMDS_Iterator<SMESH_subMesh*>* subIt,
2467 SMESH_subMesh* prepend,
2468 SMESH_subMesh* append): myIt(subIt),myAppend(append)
2470 myCur = prepend ? prepend : myIt->more() ? myIt->next() : append;
2471 if ( myCur == append ) append = 0;
2473 /// Return true if and only if there are other object in this iterator
2478 /// Return the current object and step to the next one
2479 virtual SMESH_subMesh* next()
2481 SMESH_subMesh* res = myCur;
2482 if ( myIt->more() ) { myCur = myIt->next(); }
2483 else { myCur = myAppend; myAppend = 0; }
2490 SMESH_subMesh *myAppend, *myCur;
2491 SMDS_Iterator<SMESH_subMesh*> *myIt;
2495 //================================================================================
2497 * \brief Return iterator on the submeshes this one depends on
2498 * \param includeSelf - this submesh to be returned also
2499 * \param reverse - if true, complex shape submeshes go first
2501 //================================================================================
2503 SMESH_subMeshIteratorPtr SMESH_subMesh::getDependsOnIterator(const bool includeSelf,
2504 const bool reverse) const
2506 SMESH_subMesh *me = (SMESH_subMesh*) this;
2507 SMESH_subMesh *prepend=0, *append=0;
2508 if ( includeSelf ) {
2509 if ( reverse ) prepend = me;
2512 typedef map < int, SMESH_subMesh * > TMap;
2515 return SMESH_subMeshIteratorPtr
2516 ( new _Iterator( new SMDS_mapReverseIterator<TMap>( me->DependsOn() ), prepend, append ));
2519 return SMESH_subMeshIteratorPtr
2520 ( new _Iterator( new SMDS_mapIterator<TMap>( me->DependsOn() ), prepend, append ));
2524 //================================================================================
2526 * \brief Returns ancestor sub-meshes. Finds them if not yet found.
2528 //================================================================================
2530 const std::vector< SMESH_subMesh * > & SMESH_subMesh::GetAncestors() const
2532 if ( _ancestors.empty() &&
2533 !_subShape.IsSame( _father->GetShapeToMesh() ))
2535 const TopTools_ListOfShape& ancShapes = _father->GetAncestors( _subShape );
2537 SMESH_subMesh* me = const_cast< SMESH_subMesh* >( this );
2538 me->_ancestors.reserve( ancShapes.Extent() );
2540 TopTools_MapOfShape map;
2542 for ( TopTools_ListIteratorOfListOfShape it( ancShapes ); it.More(); it.Next() )
2543 if ( SMESH_subMesh* sm = _father->GetSubMeshContaining( it.Value() ))
2544 if ( map.Add( it.Value() ))
2545 me->_ancestors.push_back( sm );
2551 //================================================================================
2553 * \brief Clears the vector of ancestor sub-meshes
2555 //================================================================================
2557 void SMESH_subMesh::ClearAncestors()
2562 //================================================================================
2564 * \brief Find common submeshes (based on shared sub-shapes with other
2565 * \param theOther submesh to check
2566 * \param theSetOfCommon set of common submesh
2568 //================================================================================
2570 bool SMESH_subMesh::FindIntersection(const SMESH_subMesh* theOther,
2571 std::set<const SMESH_subMesh*>& theSetOfCommon ) const
2573 int oldNb = theSetOfCommon.size();
2575 // check main submeshes
2576 const map <int, SMESH_subMesh*>::const_iterator otherEnd = theOther->_mapDepend.end();
2577 if ( theOther->_mapDepend.find(this->GetId()) != otherEnd )
2578 theSetOfCommon.insert( this );
2579 if ( _mapDepend.find(theOther->GetId()) != _mapDepend.end() )
2580 theSetOfCommon.insert( theOther );
2582 // check common submeshes
2583 map <int, SMESH_subMesh*>::const_iterator mapIt = _mapDepend.begin();
2584 for( ; mapIt != _mapDepend.end(); mapIt++ )
2585 if ( theOther->_mapDepend.find((*mapIt).first) != otherEnd )
2586 theSetOfCommon.insert( (*mapIt).second );
2587 return oldNb < theSetOfCommon.size();