1 // Copyright (C) 2007-2020 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 : implementation of SMESH idl descriptions
24 // File : SMESH_subMesh.cxx
25 // Author : Paul RASCLE, EDF
28 #include "SMESH_subMesh.hxx"
30 #include "SMDS_SetIterator.hxx"
31 #include "SMESHDS_Mesh.hxx"
32 #include "SMESH_Algo.hxx"
33 #include "SMESH_Comment.hxx"
34 #include "SMESH_Gen.hxx"
35 #include "SMESH_HypoFilter.hxx"
36 #include "SMESH_Hypothesis.hxx"
37 #include "SMESH_Mesh.hxx"
38 #include "SMESH_MesherHelper.hxx"
39 #include "SMESH_subMeshEventListener.hxx"
41 #include "utilities.h"
43 #include "Basics_Utils.hxx"
45 #include <BRep_Builder.hxx>
46 #include <BRep_Tool.hxx>
48 #include <TopExp_Explorer.hxx>
49 #include <TopTools_IndexedMapOfShape.hxx>
50 #include <TopTools_ListIteratorOfListOfShape.hxx>
51 #include <TopTools_ListOfShape.hxx>
53 #include <TopoDS_Compound.hxx>
54 #include <TopoDS_Iterator.hxx>
57 #include <Standard_OutOfMemory.hxx>
58 #include <Standard_ErrorHandler.hxx>
65 // enable printing algo + shape id + hypo used while meshing
66 //#define PRINT_WHO_COMPUTE_WHAT
69 //=============================================================================
71 * \brief Allocate some memory at construction and release it at destruction.
72 * Is used to be able to continue working after mesh generation breaks due to
75 //=============================================================================
80 MemoryReserve(): myBuf( new char[1024*1024*2] ){}
81 ~MemoryReserve() { delete [] myBuf; }
84 //=============================================================================
86 * default constructor:
88 //=============================================================================
90 SMESH_subMesh::SMESH_subMesh(int Id,
92 SMESHDS_Mesh * meshDS,
93 const TopoDS_Shape & aSubShape)
95 _subShape = aSubShape;
96 _subMeshDS = meshDS->MeshElements(_subShape); // may be null ...
99 _dependenceAnalysed = _alwaysComputed = false;
101 if (_subShape.ShapeType() == TopAbs_VERTEX)
104 _computeState = READY_TO_COMPUTE;
108 _algoState = NO_ALGO;
109 _computeState = NOT_READY;
111 _computeCost = 0; // how costly is to compute this sub-mesh
112 _realComputeCost = 0;
113 _allowedSubShapes = nullptr;
116 //=============================================================================
120 //=============================================================================
122 SMESH_subMesh::~SMESH_subMesh()
124 deleteOwnListeners();
127 //=============================================================================
131 //=============================================================================
133 int SMESH_subMesh::GetId() const
135 //MESSAGE("SMESH_subMesh::GetId");
139 //=============================================================================
143 //=============================================================================
145 SMESHDS_SubMesh * SMESH_subMesh::GetSubMeshDS()
147 // submesh appears in DS only when a mesher set nodes and elements on a shape
148 return _subMeshDS ? _subMeshDS : _subMeshDS = _father->GetMeshDS()->MeshElements(_subShape); // may be null
151 //=============================================================================
155 //=============================================================================
157 const SMESHDS_SubMesh * SMESH_subMesh::GetSubMeshDS() const
159 return ((SMESH_subMesh*) this )->GetSubMeshDS();
162 //=============================================================================
166 //=============================================================================
168 SMESHDS_SubMesh* SMESH_subMesh::CreateSubMeshDS()
170 if ( !GetSubMeshDS() ) {
171 SMESHDS_Mesh* meshDS = _father->GetMeshDS();
172 meshDS->NewSubMesh( meshDS->ShapeToIndex( _subShape ) );
174 return GetSubMeshDS();
177 //=============================================================================
181 //=============================================================================
183 SMESH_subMesh *SMESH_subMesh::GetFirstToCompute()
185 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(true,false);
186 while ( smIt->more() ) {
187 SMESH_subMesh *sm = smIt->next();
188 if ( sm->GetComputeState() == READY_TO_COMPUTE )
191 return 0; // nothing to compute
194 //================================================================================
196 * \brief Returns a current algorithm
198 //================================================================================
200 SMESH_Algo* SMESH_subMesh::GetAlgo() const
204 SMESH_subMesh* me = const_cast< SMESH_subMesh* >( this );
205 me->_algo = _father->GetGen()->GetAlgo( me, & me->_algoShape );
210 //================================================================================
212 * \brief Allow algo->Compute() if a sub-shape of lower dim is meshed but
213 * none mesh entity is bound to it (PAL13615, 2nd part)
215 //================================================================================
217 void SMESH_subMesh::SetIsAlwaysComputed(bool isAlCo)
219 _alwaysComputed = isAlCo;
220 if ( _alwaysComputed )
221 _computeState = COMPUTE_OK;
223 ComputeStateEngine( CHECK_COMPUTE_STATE );
226 //=======================================================================
228 * \brief Return true if no mesh entities is bound to the submesh
230 //=======================================================================
232 bool SMESH_subMesh::IsEmpty() const
234 if (SMESHDS_SubMesh * subMeshDS = ((SMESH_subMesh*)this)->GetSubMeshDS())
235 return (!subMeshDS->NbElements() && !subMeshDS->NbNodes());
239 //=======================================================================
240 //function : IsMeshComputed
241 //purpose : check if _subMeshDS contains mesh elements
242 //=======================================================================
244 bool SMESH_subMesh::IsMeshComputed() const
246 if ( _alwaysComputed )
248 // algo may bind a sub-mesh not to _subShape, eg 3D algo
249 // sets nodes on SHELL while _subShape may be SOLID
251 SMESHDS_Mesh* meshDS = _father->GetMeshDS();
252 int dim = SMESH_Gen::GetShapeDim( _subShape );
253 int type = _subShape.ShapeType();
254 for ( ; type <= TopAbs_VERTEX; type++) {
255 if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
257 TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
258 for ( ; exp.More(); exp.Next() )
260 if ( SMESHDS_SubMesh * smDS = meshDS->MeshElements( exp.Current() ))
262 bool computed = (dim > 0) ? smDS->NbElements() : smDS->NbNodes();
275 //================================================================================
277 * \brief Check if any upper level sub-shape is not computed.
278 * Used to update a sub-mesh icon
280 //================================================================================
282 bool SMESH_subMesh::IsComputedPartially() const
284 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(/*includeSelf=*/true,
285 /*SolidFirst=*/true);
286 bool allComputed = true;
287 TopAbs_ShapeEnum readyType = TopAbs_VERTEX; // max value
288 while ( smIt->more() && allComputed )
290 SMESH_subMesh* sm = smIt->next();
292 if ( sm->GetSubShape().ShapeType() > readyType )
293 break; // lower dimension -> stop
294 if ( sm->GetComputeState() != SMESH_subMesh::NOT_READY )
295 readyType = sm->GetSubShape().ShapeType();
297 switch ( sm->GetComputeState() )
299 case SMESH_subMesh::READY_TO_COMPUTE:
300 case SMESH_subMesh::FAILED_TO_COMPUTE:
301 allComputed = false;// sm->IsMeshComputed();
303 case SMESH_subMesh::NOT_READY:
304 case SMESH_subMesh::COMPUTE_OK:
311 //=============================================================================
313 * Return true if all sub-meshes have been meshed
315 //=============================================================================
317 bool SMESH_subMesh::SubMeshesComputed(bool * isFailedToCompute/*=0*/) const
319 int myDim = SMESH_Gen::GetShapeDim( _subShape );
320 int dimToCheck = myDim - 1;
321 bool subMeshesComputed = true;
322 if ( isFailedToCompute ) *isFailedToCompute = false;
323 // check sub-meshes with upper dimension => reverse iteration
324 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,true);
325 while ( smIt->more() )
327 SMESH_subMesh *sm = smIt->next();
328 if ( sm->_alwaysComputed )
330 const TopoDS_Shape & ss = sm->GetSubShape();
332 // MSV 07.04.2006: restrict checking to myDim-1 only. Ex., there is no sense
333 // in checking of existence of edges if the algo needs only faces. Moreover,
334 // degenerated edges may have no sub-mesh, as after computing NETGEN_2D.
335 if ( !_algo || _algo->NeedDiscreteBoundary() ) {
336 int dim = SMESH_Gen::GetShapeDim( ss );
337 if (dim < dimToCheck)
338 break; // the rest sub-meshes are all of less dimension
340 SMESHDS_SubMesh * ds = sm->GetSubMeshDS();
341 bool computeOk = ((sm->GetComputeState() == COMPUTE_OK ) ||
342 (ds && ( dimToCheck ? ds->NbElements() : ds->NbNodes() )));
345 subMeshesComputed = false;
347 if ( isFailedToCompute && !(*isFailedToCompute) )
348 *isFailedToCompute = ( sm->GetComputeState() == FAILED_TO_COMPUTE );
350 if ( !isFailedToCompute )
354 return subMeshesComputed;
357 //================================================================================
359 * \brief Return cost of computing this sub-mesh. If hypotheses are not well defined,
361 * \return int - the computation cost in abstract units.
363 //================================================================================
365 int SMESH_subMesh::GetComputeCost() const
367 return _realComputeCost;
370 //================================================================================
372 * \brief Return cost of computing this sub-mesh. The cost depends on the shape type
373 * and number of sub-meshes this one DependsOn().
374 * \return int - the computation cost in abstract units.
376 //================================================================================
378 int SMESH_subMesh::computeCost() const
383 switch ( _subShape.ShapeType() ) {
385 case TopAbs_SHELL: computeCost = 5000; break;
386 case TopAbs_FACE: computeCost = 500; break;
387 case TopAbs_EDGE: computeCost = 2; break;
388 default: computeCost = 1;
390 SMESH_subMeshIteratorPtr childIt = getDependsOnIterator(/*includeSelf=*/false);
391 while ( childIt->more() )
392 computeCost += childIt->next()->computeCost();
394 ((SMESH_subMesh*)this)->_computeCost = computeCost;
399 //=============================================================================
401 * Returns all sub-meshes this one depend on
403 //=============================================================================
405 const std::map < int, SMESH_subMesh * >& SMESH_subMesh::DependsOn()
407 if ( _dependenceAnalysed || !_father->HasShapeToMesh() )
410 int type = _subShape.ShapeType();
413 case TopAbs_COMPOUND:
415 list< TopoDS_Shape > compounds( 1, _subShape );
416 list< TopoDS_Shape >::iterator comp = compounds.begin();
417 for ( ; comp != compounds.end(); ++comp )
419 for ( TopoDS_Iterator sub( *comp ); sub.More(); sub.Next() )
420 switch ( sub.Value().ShapeType() )
422 case TopAbs_COMPOUND: compounds.push_back( sub.Value() ); break;
423 case TopAbs_COMPSOLID: insertDependence( sub.Value(), TopAbs_SOLID ); break;
424 case TopAbs_SOLID: insertDependence( sub.Value(), TopAbs_SOLID ); break;
425 case TopAbs_SHELL: insertDependence( sub.Value(), TopAbs_FACE ); break;
426 case TopAbs_FACE: insertDependence( sub.Value(), TopAbs_FACE ); break;
427 case TopAbs_WIRE: insertDependence( sub.Value(), TopAbs_EDGE ); break;
428 case TopAbs_EDGE: insertDependence( sub.Value(), TopAbs_EDGE ); break;
429 case TopAbs_VERTEX: insertDependence( sub.Value(), TopAbs_VERTEX ); break;
435 case TopAbs_COMPSOLID: insertDependence( _subShape, TopAbs_SOLID ); break;
436 case TopAbs_SOLID: insertDependence( _subShape, TopAbs_FACE );
437 { /*internal EDGE*/ insertDependence( _subShape, TopAbs_EDGE, TopAbs_WIRE ); break; }
438 case TopAbs_SHELL: insertDependence( _subShape, TopAbs_FACE ); break;
439 case TopAbs_FACE: insertDependence( _subShape, TopAbs_EDGE ); break;
440 case TopAbs_WIRE: insertDependence( _subShape, TopAbs_EDGE ); break;
441 case TopAbs_EDGE: insertDependence( _subShape, TopAbs_VERTEX ); break;
444 _dependenceAnalysed = true;
448 //================================================================================
450 * \brief Return a key for SMESH_subMesh::_mapDepend map
452 //================================================================================
456 int dependsOnMapKey( TopAbs_ShapeEnum type, int shapeID )
458 int ordType = 9 - int(type); // 2 = Vertex, 8 = CompSolid
460 cle += 10000000 * ordType; // sort map by ordType then index
463 int dependsOnMapKey( const SMESH_subMesh* sm )
465 return dependsOnMapKey( sm->GetSubShape().ShapeType(), sm->GetId() );
469 //=============================================================================
471 * Add sub-meshes on sub-shapes of a given type into the dependence map.
473 //=============================================================================
475 void SMESH_subMesh::insertDependence(const TopoDS_Shape aShape,
476 TopAbs_ShapeEnum aSubType,
477 TopAbs_ShapeEnum avoidType)
479 TopExp_Explorer sub( aShape, aSubType, avoidType );
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 * \brief Return \c true if \a this sub-mesh depends on a \a shape
510 //================================================================================
512 bool SMESH_subMesh::DependsOn( const int shapeID ) const
514 return DependsOn( _father->GetSubMeshContaining( shapeID ));
517 //=============================================================================
519 * Return a shape of \a this sub-mesh
521 //=============================================================================
523 const TopoDS_Shape & SMESH_subMesh::GetSubShape() const
528 //=======================================================================
529 //function : CanAddHypothesis
530 //purpose : return true if theHypothesis can be attached to me:
531 // its dimension is checked
532 //=======================================================================
534 bool SMESH_subMesh::CanAddHypothesis(const SMESH_Hypothesis* theHypothesis) const
536 int aHypDim = theHypothesis->GetDim();
537 int aShapeDim = SMESH_Gen::GetShapeDim(_subShape);
538 // issue 21106. Forbid 3D mesh on the SHELL
539 // if (aHypDim == 3 && aShapeDim == 3) {
540 // // check case of open shell
541 // //if (_subShape.ShapeType() == TopAbs_SHELL && !_subShape.Closed())
542 // if (_subShape.ShapeType() == TopAbs_SHELL && !BRep_Tool::IsClosed(_subShape))
545 if ( aHypDim <= aShapeDim )
551 //=======================================================================
552 //function : IsApplicableHypothesis
553 //purpose : check if this sub-mesh can be computed using a hypothesis
554 //=======================================================================
556 bool SMESH_subMesh::IsApplicableHypothesis(const SMESH_Hypothesis* theHypothesis) const
558 if ( !_father->HasShapeToMesh() && _subShape.ShapeType() == TopAbs_SOLID )
559 return true; // true for the PseudoShape
561 return IsApplicableHypothesis( theHypothesis, _subShape.ShapeType() );
564 //=======================================================================
565 //function : IsApplicableHypothesis
566 //purpose : compare shape type and hypothesis type
567 //=======================================================================
569 bool SMESH_subMesh::IsApplicableHypothesis(const SMESH_Hypothesis* theHypothesis,
570 const TopAbs_ShapeEnum theShapeType)
572 if ( theHypothesis->GetType() > SMESHDS_Hypothesis::PARAM_ALGO)
575 if ( theHypothesis->GetShapeType() & (1<< theShapeType))
576 // issue 21106. Forbid 3D mesh on the SHELL
577 return !( theHypothesis->GetDim() == 3 && theShapeType == TopAbs_SHELL );
583 switch ( theShapeType ) {
588 return SMESH_Gen::GetShapeDim( theShapeType ) == theHypothesis->GetDim();
591 // Special case for algorithms, building 2D mesh on a whole shell.
592 // Before this fix there was a problem after restoring from study,
593 // because in that case algorithm is assigned before hypothesis
594 // (on shell in problem case) and hypothesis is checked on faces
595 // (because it is 2D), where we have NO_ALGO state.
596 // Now 2D hypothesis is also applicable to shells.
597 return (theHypothesis->GetDim() == 2 || theHypothesis->GetDim() == 3);
600 // case TopAbs_COMPSOLID:
601 // case TopAbs_COMPOUND:
607 //================================================================================
609 * \brief Treats modification of hypotheses definition
610 * \param [in] event - what happens
611 * \param [in] anHyp - a hypothesis
612 * \return SMESH_Hypothesis::Hypothesis_Status - a treatment result.
614 * Optional description of a problematic situation (if any) can be retrieved
615 * via GetComputeError().
617 //================================================================================
619 SMESH_Hypothesis::Hypothesis_Status
620 SMESH_subMesh::AlgoStateEngine(algo_event event, SMESH_Hypothesis * anHyp)
622 // **** les retour des evenement shape sont significatifs
623 // (add ou remove fait ou non)
624 // le retour des evenement father n'indiquent pas que add ou remove fait
626 SMESH_Hypothesis::Hypothesis_Status aux_ret, ret = SMESH_Hypothesis::HYP_OK;
627 if ( _Id == 0 ) return ret; // not a sub-shape of the shape to mesh
629 SMESHDS_Mesh* meshDS =_father->GetMeshDS();
630 SMESH_Algo* algo = 0;
633 if (_subShape.ShapeType() == TopAbs_VERTEX )
635 if ( anHyp->GetDim() != 0) {
636 if (event == ADD_HYP || event == ADD_ALGO)
637 return SMESH_Hypothesis::HYP_BAD_DIM;
639 return SMESH_Hypothesis::HYP_OK;
642 else if ( _algoState == HYP_OK ) {
643 // update default _algoState
644 if ( event != REMOVE_FATHER_ALGO )
646 _algoState = NO_ALGO;
649 _algoState = MISSING_HYP;
650 if ( event == REMOVE_FATHER_HYP ||
651 algo->CheckHypothesis(*_father,_subShape, aux_ret))
658 int oldAlgoState = _algoState;
659 bool modifiedHyp = (event == MODIF_HYP); // if set to true, force event MODIF_ALGO_STATE
660 SMESH_Algo* algoRequiringCleaning = 0;
662 bool isApplicableHyp = IsApplicableHypothesis( anHyp );
664 if (event == ADD_ALGO || event == ADD_FATHER_ALGO)
666 // -------------------------------------------
667 // check if a shape needed by algo is present
668 // -------------------------------------------
669 algo = static_cast< SMESH_Algo* >( anHyp );
670 if ( !_father->HasShapeToMesh() && algo->NeedShape() )
671 return SMESH_Hypothesis::HYP_NEED_SHAPE;
672 // ----------------------
673 // check mesh conformity
674 // ----------------------
675 if (isApplicableHyp && !_father->IsNotConformAllowed() && !IsConform( algo ))
676 return SMESH_Hypothesis::HYP_NOTCONFORM;
678 // check if all-dimensional algo is hidden by other local one
679 if ( event == ADD_ALGO ) {
680 SMESH_HypoFilter filter( SMESH_HypoFilter::HasType( algo->GetType() ));
681 filter.Or( SMESH_HypoFilter::HasType( algo->GetType()+1 ));
682 filter.Or( SMESH_HypoFilter::HasType( algo->GetType()+2 ));
683 if ( SMESH_Algo * curAlgo = (SMESH_Algo*)_father->GetHypothesis( this, filter, true ))
684 if ( !curAlgo->NeedDiscreteBoundary() && curAlgo != anHyp )
685 algoRequiringCleaning = curAlgo;
689 // ----------------------------------
690 // add a hypothesis to DS if possible
691 // ----------------------------------
692 if (event == ADD_HYP || event == ADD_ALGO)
694 if ( ! CanAddHypothesis( anHyp )) // check dimension
695 return SMESH_Hypothesis::HYP_BAD_DIM;
697 if ( !anHyp->IsAuxiliary() && getSimilarAttached( _subShape, anHyp ) )
698 return SMESH_Hypothesis::HYP_ALREADY_EXIST;
700 if ( !meshDS->AddHypothesis(_subShape, anHyp))
701 return SMESH_Hypothesis::HYP_ALREADY_EXIST;
704 // --------------------------
705 // remove a hypothesis from DS
706 // --------------------------
707 if (event == REMOVE_HYP || event == REMOVE_ALGO)
709 if (!meshDS->RemoveHypothesis(_subShape, anHyp))
710 return SMESH_Hypothesis::HYP_OK; // nothing changes
712 if (event == REMOVE_ALGO)
714 algo = dynamic_cast<SMESH_Algo*> (anHyp);
715 if (!algo->NeedDiscreteBoundary())
716 algoRequiringCleaning = algo;
720 // ------------------
721 // analyse algo state
722 // ------------------
723 if (!isApplicableHyp)
724 return ret; // not applicable hypotheses do not change algo state
726 if (( algo = GetAlgo()))
727 algo->InitComputeError();
732 // ----------------------------------------------------------------------
741 if (algo->CheckHypothesis((*_father),_subShape, aux_ret))
742 setAlgoState(HYP_OK);
743 else if ( algo->IsStatusFatal( aux_ret )) {
744 meshDS->RemoveHypothesis(_subShape, anHyp);
748 setAlgoState(MISSING_HYP);
755 case ADD_FATHER_ALGO: { // Algo just added in father
758 if ( algo == anHyp ) {
759 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret))
760 setAlgoState(HYP_OK);
762 setAlgoState(MISSING_HYP);
766 case REMOVE_FATHER_HYP:
768 case REMOVE_FATHER_ALGO: {
772 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
773 setAlgoState(HYP_OK);
775 setAlgoState(MISSING_HYP);
779 case MODIF_HYP: break;
786 // ----------------------------------------------------------------------
794 if ( algo->CheckHypothesis((*_father),_subShape, ret ))
795 setAlgoState(HYP_OK);
796 if (SMESH_Hypothesis::IsStatusFatal( ret ))
797 meshDS->RemoveHypothesis(_subShape, anHyp);
798 else if (!_father->IsUsedHypothesis( anHyp, this ))
800 meshDS->RemoveHypothesis(_subShape, anHyp);
801 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
805 case ADD_ALGO: { //already existing algo : on father ?
808 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))// ignore hyp status
809 setAlgoState(HYP_OK);
810 else if ( algo->IsStatusFatal( aux_ret )) {
811 meshDS->RemoveHypothesis(_subShape, anHyp);
815 setAlgoState(MISSING_HYP);
820 case REMOVE_ALGO: { // perhaps a father algo applies ?
822 if (algo == NULL) // no more algo applying on sub-shape...
824 setAlgoState(NO_ALGO);
828 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
829 setAlgoState(HYP_OK);
831 setAlgoState(MISSING_HYP);
835 case MODIF_HYP: // assigned hypothesis value may become good
836 case ADD_FATHER_HYP: {
839 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
840 setAlgoState(HYP_OK);
842 setAlgoState(MISSING_HYP);
845 case ADD_FATHER_ALGO: { // new father algo
848 if ( algo == anHyp ) {
849 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
850 setAlgoState(HYP_OK);
852 setAlgoState(MISSING_HYP);
856 case REMOVE_FATHER_HYP: // nothing to do
858 case REMOVE_FATHER_ALGO: {
860 if (algo == NULL) // no more applying algo on father
862 setAlgoState(NO_ALGO);
866 if ( algo->CheckHypothesis((*_father),_subShape , aux_ret ))
867 setAlgoState(HYP_OK);
869 setAlgoState(MISSING_HYP);
879 // ----------------------------------------------------------------------
887 if (!algo->CheckHypothesis((*_father),_subShape, ret ))
889 if ( !SMESH_Hypothesis::IsStatusFatal( ret ))
890 // ret should be fatal: anHyp was not added
891 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
893 else if (!_father->IsUsedHypothesis( anHyp, this ))
894 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
896 if (SMESH_Hypothesis::IsStatusFatal( ret ))
898 MESSAGE("do not add extra hypothesis");
899 meshDS->RemoveHypothesis(_subShape, anHyp);
907 case ADD_ALGO: { //already existing algo : on father ?
909 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
910 // check if algo changes
912 f.Init( SMESH_HypoFilter::IsAlgo() );
913 f.And( SMESH_HypoFilter::IsApplicableTo( _subShape ));
914 f.AndNot( SMESH_HypoFilter::Is( algo ));
915 const SMESH_Hypothesis * prevAlgo = _father->GetHypothesis( this, f, true );
917 string( algo->GetName()) != prevAlgo->GetName())
919 oldAlgoState = NO_ALGO; // force setting event listener (#16648)
924 setAlgoState(MISSING_HYP);
930 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
931 setAlgoState(HYP_OK);
933 setAlgoState(MISSING_HYP);
937 case REMOVE_ALGO: { // perhaps a father algo applies ?
939 if (algo == NULL) // no more algo applying on sub-shape...
941 setAlgoState(NO_ALGO);
945 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
946 // check if algo remains
947 if ( anHyp != algo && strcmp( anHyp->GetName(), algo->GetName()) )
951 setAlgoState(MISSING_HYP);
955 case MODIF_HYP: // hypothesis value may become bad
956 case ADD_FATHER_HYP: { // new father hypothesis ?
959 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
961 if (_father->IsUsedHypothesis( anHyp, this )) // new Hyp
965 setAlgoState(MISSING_HYP);
968 case ADD_FATHER_ALGO: {
970 if ( algo == anHyp ) { // a new algo on father
971 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
972 // check if algo changes
974 f.Init( SMESH_HypoFilter::IsAlgo() );
975 f.And( SMESH_HypoFilter::IsApplicableTo( _subShape ));
976 f.AndNot( SMESH_HypoFilter::Is( algo ));
977 const SMESH_Hypothesis* prevAlgo = _father->GetHypothesis( this, f, true );
979 string(algo->GetName()) != string(prevAlgo->GetName()) )
983 setAlgoState(MISSING_HYP);
987 case REMOVE_FATHER_HYP: {
990 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
991 // is there the same local hyp or maybe a new father algo applied?
992 if ( !getSimilarAttached( _subShape, anHyp ) )
996 setAlgoState(MISSING_HYP);
999 case REMOVE_FATHER_ALGO: {
1000 // IPAL21346. Edges not removed when Netgen 1d-2d is removed from a SOLID.
1001 // CLEAN was not called at event REMOVE_ALGO because the algo is not applicable to SOLID.
1002 algo = dynamic_cast<SMESH_Algo*> (anHyp);
1003 if (!algo->NeedDiscreteBoundary())
1004 algoRequiringCleaning = algo;
1006 if (algo == NULL) // no more applying algo on father
1008 setAlgoState(NO_ALGO);
1012 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
1013 // check if algo changes
1014 if ( string(algo->GetName()) != string( anHyp->GetName()) )
1018 setAlgoState(MISSING_HYP);
1028 // ----------------------------------------------------------------------
1035 // detect algorithm hiding
1037 if ( ret == SMESH_Hypothesis::HYP_OK &&
1038 ( event == ADD_ALGO || event == ADD_FATHER_ALGO ) && algo &&
1039 algo->GetName() == anHyp->GetName() )
1042 SMESH_Gen* gen = _father->GetGen();
1043 const std::vector< SMESH_subMesh * > & ancestors = GetAncestors();
1044 for ( size_t iA = 0; ( ret == SMESH_Hypothesis::HYP_OK && iA < ancestors.size()); ++iA ) {
1045 if ( SMESH_Algo* upperAlgo = ancestors[ iA ]->GetAlgo() )
1046 if ( !upperAlgo->NeedDiscreteBoundary() && !upperAlgo->SupportSubmeshes())
1047 ret = SMESH_Hypothesis::HYP_HIDDEN_ALGO;
1050 if ( ret == SMESH_Hypothesis::HYP_OK &&
1051 !algo->NeedDiscreteBoundary() &&
1052 !algo->SupportSubmeshes())
1054 TopoDS_Shape algoAssignedTo, otherAssignedTo;
1055 gen->GetAlgo( this, &algoAssignedTo );
1056 map<int, SMESH_subMesh*>::reverse_iterator i_sm = _mapDepend.rbegin();
1057 for ( ; ( ret == SMESH_Hypothesis::HYP_OK && i_sm != _mapDepend.rend()) ; ++i_sm )
1058 if ( gen->GetAlgo( i_sm->second, &otherAssignedTo ) &&
1059 SMESH_MesherHelper::IsSubShape( /*sub=*/otherAssignedTo, /*main=*/algoAssignedTo ))
1060 ret = SMESH_Hypothesis::HYP_HIDING_ALGO;
1064 if ( _algo ) { // get an error description set by _algo->CheckHypothesis()
1065 _computeError = _algo->GetComputeError();
1066 _algo->InitComputeError();
1069 bool stateChange = ( _algoState != oldAlgoState );
1071 if ( stateChange && _algoState == HYP_OK ) // hyp becomes OK
1072 algo->SetEventListener( this );
1074 if ( event == REMOVE_ALGO || event == REMOVE_FATHER_ALGO )
1077 notifyListenersOnEvent( event, ALGO_EVENT, anHyp );
1079 if ( stateChange && oldAlgoState == HYP_OK ) { // hyp becomes KO
1080 deleteOwnListeners();
1081 SetIsAlwaysComputed( false );
1082 if (_subShape.ShapeType() == TopAbs_VERTEX ) {
1083 // restore default states
1084 _algoState = HYP_OK;
1085 _computeState = READY_TO_COMPUTE;
1089 if ( algoRequiringCleaning ) {
1090 // added or removed algo is all-dimensional
1091 ComputeStateEngine( CLEAN );
1092 cleanDependsOn( algoRequiringCleaning );
1093 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1096 if ( stateChange || modifiedHyp )
1097 ComputeStateEngine( MODIF_ALGO_STATE );
1099 _realComputeCost = ( _algoState == HYP_OK ) ? computeCost() : 0;
1104 //=======================================================================
1105 //function : IsConform
1106 //purpose : check if a conform mesh will be produced by the Algo
1107 //=======================================================================
1109 bool SMESH_subMesh::IsConform(const SMESH_Algo* theAlgo)
1111 // MESSAGE( "SMESH_subMesh::IsConform" );
1112 if ( !theAlgo ) return false;
1114 // Suppose that theAlgo is applicable to _subShape, do not check it here
1115 //if ( !IsApplicableHypothesis( theAlgo )) return false;
1117 // check only algo that doesn't NeedDiscreteBoundary(): because mesh made
1118 // on a sub-shape will be ignored by theAlgo
1119 if ( theAlgo->NeedDiscreteBoundary() ||
1120 !theAlgo->OnlyUnaryInput() ) // all adjacent shapes will be meshed by this algo?
1123 // only local algo is to be checked
1124 //if ( gen->IsGlobalHypothesis( theAlgo, *_father ))
1125 if ( _subShape.ShapeType() == _father->GetMeshDS()->ShapeToMesh().ShapeType() )
1128 // check algo attached to adjacent shapes
1130 // loop on one level down sub-meshes
1131 TopoDS_Iterator itsub( _subShape );
1132 for (; itsub.More(); itsub.Next())
1134 // loop on adjacent subShapes
1135 const std::vector< SMESH_subMesh * > & ancestors = GetAncestors();
1136 for ( size_t iA = 0; iA < ancestors.size(); ++iA )
1138 const TopoDS_Shape& adjacent = ancestors[ iA ]->GetSubShape();
1139 if ( _subShape.IsSame( adjacent )) continue;
1140 if ( adjacent.ShapeType() != _subShape.ShapeType())
1143 // check algo attached to smAdjacent
1144 SMESH_Algo * algo = ancestors[ iA ]->GetAlgo();
1146 !algo->NeedDiscreteBoundary() &&
1147 algo->OnlyUnaryInput())
1148 return false; // NOT CONFORM MESH WILL BE PRODUCED
1155 //=============================================================================
1159 //=============================================================================
1161 void SMESH_subMesh::setAlgoState(algo_state state)
1166 //================================================================================
1168 * \brief Send an event to sub-meshes
1169 * \param [in] event - the event
1170 * \param [in] anHyp - an hypothesis
1171 * \param [in] exitOnFatal - to stop iteration on sub-meshes if a sub-mesh
1172 * reports a fatal result
1173 * \return SMESH_Hypothesis::Hypothesis_Status - the worst result
1175 * Optional description of a problematic situation (if any) can be retrieved
1176 * via GetComputeError().
1178 //================================================================================
1180 SMESH_Hypothesis::Hypothesis_Status
1181 SMESH_subMesh::SubMeshesAlgoStateEngine(algo_event event,
1182 SMESH_Hypothesis * anHyp,
1185 SMESH_Hypothesis::Hypothesis_Status ret = SMESH_Hypothesis::HYP_OK;
1186 //EAP: a wire (dim==1) should notify edges (dim==1)
1187 //EAP: int dim = SMESH_Gen::GetShapeDim(_subShape);
1188 //if (_subShape.ShapeType() < TopAbs_EDGE ) // wire,face etc
1190 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1191 while ( smIt->more() ) {
1192 SMESH_subMesh* sm = smIt->next();
1193 SMESH_Hypothesis::Hypothesis_Status ret2 = sm->AlgoStateEngine(event, anHyp);
1197 _computeError = sm->_computeError;
1198 sm->_computeError.reset();
1199 if ( exitOnFatal && SMESH_Hypothesis::IsStatusFatal( ret ))
1207 //================================================================================
1209 * \brief Remove elements from sub-meshes.
1210 * \param algoRequiringCleaning - an all-dimensional algorithm whose presence
1211 * causes the cleaning.
1213 //================================================================================
1215 void SMESH_subMesh::cleanDependsOn( SMESH_Algo* algoRequiringCleaning/*=0*/ )
1217 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,
1218 /*complexShapeFirst=*/true);
1219 if ( _father->NbNodes() == 0 )
1221 while ( smIt->more() )
1222 smIt->next()->ComputeStateEngine(CHECK_COMPUTE_STATE);
1224 else if ( !algoRequiringCleaning || !algoRequiringCleaning->SupportSubmeshes() )
1226 while ( smIt->more() )
1227 smIt->next()->ComputeStateEngine(CLEAN);
1229 else if ( algoRequiringCleaning && algoRequiringCleaning->SupportSubmeshes() )
1231 // find sub-meshes to keep elements on
1232 set< SMESH_subMesh* > smToKeep;
1233 TopAbs_ShapeEnum prevShapeType = TopAbs_SHAPE;
1234 bool toKeepPrevShapeType = false;
1235 while ( smIt->more() )
1237 SMESH_subMesh* sm = smIt->next();
1238 sm->ComputeStateEngine(CHECK_COMPUTE_STATE);
1239 if ( !sm->IsEmpty() )
1241 const bool sameShapeType = ( prevShapeType == sm->GetSubShape().ShapeType() );
1242 bool keepSubMeshes = ( sameShapeType && toKeepPrevShapeType );
1243 if ( !sameShapeType )
1245 // check if the algo allows presence of global algos of dimension the algo
1246 // can generate it-self;
1247 // always keep a node on VERTEX, as this node can be shared by segments
1248 // lying on EDGEs not shared by the VERTEX of sm, due to MergeNodes (PAL23068)
1249 int shapeDim = SMESH_Gen::GetShapeDim( sm->GetSubShape() );
1250 keepSubMeshes = ( algoRequiringCleaning->NeedLowerHyps( shapeDim ) || shapeDim == 0 );
1251 prevShapeType = sm->GetSubShape().ShapeType();
1252 toKeepPrevShapeType = keepSubMeshes;
1254 if ( !keepSubMeshes )
1256 // look for a local algo used to mesh sm
1257 TopoDS_Shape algoShape = SMESH_MesherHelper::GetShapeOfHypothesis
1258 ( algoRequiringCleaning, _subShape, _father );
1259 SMESH_HypoFilter moreLocalAlgo;
1260 moreLocalAlgo.Init( SMESH_HypoFilter::IsMoreLocalThan( algoShape, *_father ));
1261 moreLocalAlgo.And ( SMESH_HypoFilter::IsAlgo() );
1262 bool localAlgoFound = _father->GetHypothesis( sm->_subShape, moreLocalAlgo, true );
1263 keepSubMeshes = localAlgoFound;
1265 // remember all sub-meshes of sm
1266 if ( keepSubMeshes )
1268 SMESH_subMeshIteratorPtr smIt2 = sm->getDependsOnIterator(true);
1269 while ( smIt2->more() )
1270 smToKeep.insert( smIt2->next() );
1275 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,true);
1276 while ( smIt->more() )
1278 SMESH_subMesh* sm = smIt->next();
1279 if ( !smToKeep.count( sm ))
1280 sm->ComputeStateEngine(CLEAN);
1285 //=============================================================================
1289 //=============================================================================
1291 void SMESH_subMesh::DumpAlgoState(bool isMain)
1295 const map < int, SMESH_subMesh * >&subMeshes = DependsOn();
1297 map < int, SMESH_subMesh * >::const_iterator itsub;
1298 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
1300 SMESH_subMesh *sm = (*itsub).second;
1301 sm->DumpAlgoState(false);
1304 MESSAGE("dim = " << SMESH_Gen::GetShapeDim(_subShape) <<
1305 " type of shape " << _subShape.ShapeType());
1308 case NO_ALGO : MESSAGE(" AlgoState = NO_ALGO"); break;
1309 case MISSING_HYP : MESSAGE(" AlgoState = MISSING_HYP"); break;
1310 case HYP_OK : MESSAGE(" AlgoState = HYP_OK");break;
1312 switch (_computeState)
1314 case NOT_READY : MESSAGE(" ComputeState = NOT_READY");break;
1315 case READY_TO_COMPUTE : MESSAGE(" ComputeState = READY_TO_COMPUTE");break;
1316 case COMPUTE_OK : MESSAGE(" ComputeState = COMPUTE_OK");break;
1317 case FAILED_TO_COMPUTE: MESSAGE(" ComputeState = FAILED_TO_COMPUTE");break;
1321 //================================================================================
1323 * \brief Remove nodes and elements bound to submesh
1324 * \param subMesh - submesh containing nodes and elements
1326 //================================================================================
1328 static void cleanSubMesh( SMESH_subMesh * subMesh )
1331 if (SMESHDS_SubMesh * subMeshDS = subMesh->GetSubMeshDS())
1333 SMESHDS_Mesh * meshDS = subMesh->GetFather()->GetMeshDS();
1334 int nbElems = subMeshDS->NbElements();
1336 for ( SMDS_ElemIteratorPtr ite = subMeshDS->GetElements(); ite->more(); )
1337 meshDS->RemoveFreeElement( ite->next(), subMeshDS );
1339 int nbNodes = subMeshDS->NbNodes();
1341 for ( SMDS_NodeIteratorPtr itn = subMeshDS->GetNodes(); itn->more() ; )
1343 const SMDS_MeshNode * node = itn->next();
1344 if ( node->NbInverseElements() == 0 )
1345 meshDS->RemoveFreeNode( node, subMeshDS );
1346 else // for StdMeshers_CompositeSegment_1D: node in one submesh, edge in another
1347 meshDS->RemoveNode( node );
1354 //=============================================================================
1358 //=============================================================================
1360 bool SMESH_subMesh::ComputeStateEngine(compute_event event)
1363 case MODIF_ALGO_STATE:
1365 case COMPUTE_SUBMESH:
1366 //case COMPUTE_CANCELED:
1368 //case SUBMESH_COMPUTED:
1369 //case SUBMESH_RESTORED:
1370 //case SUBMESH_LOADED:
1371 //case MESH_ENTITY_REMOVED:
1372 //case CHECK_COMPUTE_STATE:
1373 _computeError.reset(); break;
1377 if ( event == CLEAN )
1378 _alwaysComputed = false; // Unset 'true' set by MergeNodes() (issue 0022182)
1380 if (_subShape.ShapeType() == TopAbs_VERTEX)
1382 _computeState = READY_TO_COMPUTE;
1383 SMESHDS_SubMesh* smDS = GetSubMeshDS();
1384 if ( smDS && smDS->NbNodes() )
1386 if ( event == CLEAN ) {
1388 cleanSubMesh( this );
1391 _computeState = COMPUTE_OK;
1393 else if (( event == COMPUTE || event == COMPUTE_SUBMESH )
1394 && !_alwaysComputed )
1396 const TopoDS_Vertex & V = TopoDS::Vertex( _subShape );
1397 gp_Pnt P = BRep_Tool::Pnt(V);
1398 if ( SMDS_MeshNode * n = _father->GetMeshDS()->AddNode(P.X(), P.Y(), P.Z()) ) {
1399 _father->GetMeshDS()->SetNodeOnVertex(n,_Id);
1400 _computeState = COMPUTE_OK;
1403 if ( event == MODIF_ALGO_STATE )
1407 SMESH_Gen *gen = _father->GetGen();
1408 SMESH_Algo *algo = 0;
1410 SMESH_Hypothesis::Hypothesis_Status hyp_status;
1411 //algo_state oldAlgoState = (algo_state) GetAlgoState();
1413 switch (_computeState)
1416 // ----------------------------------------------------------------------
1421 case MODIF_ALGO_STATE:
1423 if (algo && !algo->NeedDiscreteBoundary())
1424 cleanDependsOn( algo ); // clean sub-meshes with event CLEAN
1425 if ( _algoState == HYP_OK )
1426 _computeState = READY_TO_COMPUTE;
1428 case COMPUTE: // nothing to do
1429 case COMPUTE_SUBMESH:
1431 case COMPUTE_CANCELED: // nothing to do
1435 removeSubMeshElementsAndNodes();
1437 case SUBMESH_COMPUTED: // nothing to do
1439 case SUBMESH_RESTORED:
1440 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1442 case MESH_ENTITY_REMOVED:
1444 case SUBMESH_LOADED:
1445 loadDependentMeshes();
1446 ComputeSubMeshStateEngine( SUBMESH_LOADED );
1448 case CHECK_COMPUTE_STATE:
1449 if ( IsMeshComputed() )
1450 _computeState = COMPUTE_OK;
1458 // ----------------------------------------------------------------------
1460 case READY_TO_COMPUTE:
1463 case MODIF_ALGO_STATE:
1464 _computeState = NOT_READY;
1468 if (!algo->NeedDiscreteBoundary())
1469 cleanDependsOn( algo ); // clean sub-meshes with event CLEAN
1470 if ( _algoState == HYP_OK )
1471 _computeState = READY_TO_COMPUTE;
1475 case COMPUTE_NOGEOM: // no geometry; can be several algos
1476 if ( !_father->HasShapeToMesh() )
1478 algo = GetAlgo(); // current algo
1481 // apply algos in the order of increasing dimension
1482 std::list< const SMESHDS_Hypothesis * > algos = _father->GetHypothesisList( _subShape );
1483 for ( int t = SMESHDS_Hypothesis::ALGO_1D; t <= SMESHDS_Hypothesis::ALGO_3D; ++t )
1485 std::list<const SMESHDS_Hypothesis *>::iterator al = algos.begin();
1486 for ( ; al != algos.end(); ++al )
1487 if ( (*al)->GetType() == t )
1489 _algo = (SMESH_Algo*) *al;
1490 _computeState = READY_TO_COMPUTE;
1491 if ( !ComputeStateEngine( COMPUTE ))
1495 _algo = algo; // restore
1500 case COMPUTE_SUBMESH:
1504 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1507 MESSAGE("***** verify compute state *****");
1508 _computeState = NOT_READY;
1509 setAlgoState(MISSING_HYP);
1512 TopoDS_Shape shape = _subShape;
1513 algo->SubMeshesToCompute().assign( 1, this );
1514 // check submeshes needed
1515 if (_father->HasShapeToMesh() ) {
1516 bool subComputed = false, subFailed = false;
1517 if (!algo->OnlyUnaryInput()) {
1518 // --- commented for bos#22320 to compute all sub-shapes at once if possible;
1519 // --- in case COMPUTE_SUBMESH, set of sub-shapes is limited
1520 // --- by calling SetAllowedSubShapes()
1521 // if ( event == COMPUTE )
1522 // shape = getCollection( gen, algo, subComputed, subFailed, algo->SubMeshesToComput;
1524 // subComputed = SubMeshesComputed( & subFailed );
1525 shape = getCollection( gen, algo, subComputed, subFailed, algo->SubMeshesToCompute());
1528 subComputed = SubMeshesComputed();
1530 ret = ( algo->NeedDiscreteBoundary() ? subComputed :
1531 algo->SupportSubmeshes() ? !subFailed :
1532 ( !subComputed || _father->IsNotConformAllowed() ));
1535 _computeState = FAILED_TO_COMPUTE;
1536 if ( !algo->NeedDiscreteBoundary() && !subFailed )
1538 SMESH_ComputeError::New(COMPERR_BAD_INPUT_MESH,
1539 "Unexpected computed sub-mesh",algo);
1545 // to restore cout that may be redirected by algo
1546 std::streambuf* coutBuffer = std::cout.rdbuf();
1548 //cleanDependants(); for "UseExisting_*D" algos
1549 //removeSubMeshElementsAndNodes();
1550 loadDependentMeshes();
1552 _computeState = FAILED_TO_COMPUTE;
1553 _computeError = SMESH_ComputeError::New(COMPERR_OK,"",algo);
1557 algo->InitComputeError();
1559 MemoryReserve aMemoryReserve;
1560 SMDS_Mesh::CheckMemory();
1561 Kernel_Utils::Localizer loc;
1562 if ( !_father->HasShapeToMesh() ) // no shape
1564 SMESH_MesherHelper helper( *_father );
1565 helper.SetSubShape( shape );
1566 helper.SetElementsOnShape( true );
1567 ret = algo->Compute(*_father, &helper );
1571 ret = algo->Compute((*_father), shape);
1573 // algo can set _computeError of submesh
1574 _computeError = SMESH_ComputeError::Worst( _computeError, algo->GetComputeError() );
1576 catch ( ::SMESH_ComputeError& comperr ) {
1577 cout << " SMESH_ComputeError caught" << endl;
1578 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1579 *_computeError = comperr;
1581 catch ( std::bad_alloc& exc ) {
1582 MESSAGE("std::bad_alloc thrown inside algo->Compute()");
1583 if ( _computeError ) {
1584 _computeError->myName = COMPERR_MEMORY_PB;
1586 cleanSubMesh( this );
1589 catch ( Standard_OutOfMemory& exc ) {
1590 MESSAGE("Standard_OutOfMemory thrown inside algo->Compute()");
1591 if ( _computeError ) {
1592 _computeError->myName = COMPERR_MEMORY_PB;
1594 cleanSubMesh( this );
1595 throw std::bad_alloc();
1597 catch (Standard_Failure& ex) {
1598 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1599 _computeError->myName = COMPERR_OCC_EXCEPTION;
1600 _computeError->myComment += ex.DynamicType()->Name();
1601 if ( ex.GetMessageString() && strlen( ex.GetMessageString() )) {
1602 _computeError->myComment += ": ";
1603 _computeError->myComment += ex.GetMessageString();
1606 catch ( SALOME_Exception& S_ex ) {
1607 const int skipSalomeShift = 7; /* to skip "Salome " of
1608 "Salome Exception" prefix returned
1609 by SALOME_Exception::what() */
1610 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1611 _computeError->myName = COMPERR_SLM_EXCEPTION;
1612 _computeError->myComment = S_ex.what() + skipSalomeShift;
1614 catch ( std::exception& exc ) {
1615 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1616 _computeError->myName = COMPERR_STD_EXCEPTION;
1617 _computeError->myComment = exc.what();
1620 if ( _computeError )
1621 _computeError->myName = COMPERR_EXCEPTION;
1625 std::cout.rdbuf( coutBuffer ); // restore cout that could be redirected by algo
1627 // check if an error reported on any sub-shape
1628 bool isComputeErrorSet = !checkComputeError( algo, ret, shape );
1629 if ( isComputeErrorSet )
1631 // check if anything was built
1632 TopExp_Explorer subS(shape, _subShape.ShapeType());
1635 for (; ret && subS.More(); subS.Next())
1636 if ( !_father->GetSubMesh( subS.Current() )->IsMeshComputed() &&
1637 ( _subShape.ShapeType() != TopAbs_EDGE ||
1638 !algo->isDegenerated( TopoDS::Edge( subS.Current() ))))
1641 #ifdef PRINT_WHO_COMPUTE_WHAT
1642 for (subS.ReInit(); subS.More(); subS.Next())
1644 const std::list <const SMESHDS_Hypothesis *> & hyps =
1645 _algo->GetUsedHypothesis( *_father, _subShape );
1646 SMESH_Comment hypStr;
1647 if ( !hyps.empty() )
1649 hypStr << hyps.front()->GetName() << " ";
1650 ((SMESHDS_Hypothesis*)hyps.front())->SaveTo( hypStr.Stream() );
1653 cout << _father->GetSubMesh( subS.Current() )->GetId()
1654 << " " << ( ret ? "OK" : "FAIL" )
1655 << " " << _algo->GetName()
1656 << " " << hypStr << endl;
1659 // Set _computeError
1660 if ( !ret && !isComputeErrorSet )
1662 for ( subS.ReInit(); subS.More(); subS.Next() )
1664 SMESH_subMesh* sm = _father->GetSubMesh( subS.Current() );
1665 if ( !sm->IsMeshComputed() )
1667 if ( !sm->_computeError )
1668 sm->_computeError = SMESH_ComputeError::New();
1669 if ( sm->_computeError->IsOK() )
1670 sm->_computeError->myName = COMPERR_ALGO_FAILED;
1671 sm->_computeState = FAILED_TO_COMPUTE;
1672 sm->_computeError->myAlgo = algo;
1676 if ( ret && _computeError && _computeError->myName != COMPERR_WARNING )
1678 _computeError.reset();
1681 // transform errors into warnings if it is caused by mesh edition (imp 0023068)
1682 if (!ret && _father->GetIsModified() )
1684 for (subS.ReInit(); subS.More(); subS.Next())
1686 SMESH_subMesh* sm = _father->GetSubMesh( subS.Current() );
1687 if ( !sm->IsMeshComputed() && sm->_computeError )
1689 // check if there is a VERTEX w/o nodes
1690 // with READY_TO_COMPUTE state (after MergeNodes())
1691 SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(false,false);
1692 while ( smIt->more() )
1694 SMESH_subMesh * vertSM = smIt->next();
1695 if ( vertSM->_subShape.ShapeType() != TopAbs_VERTEX ) break;
1696 if ( vertSM->GetComputeState() == READY_TO_COMPUTE )
1698 SMESHDS_SubMesh * ds = vertSM->GetSubMeshDS();
1699 if ( !ds || ds->NbNodes() == 0 )
1701 sm->_computeState = READY_TO_COMPUTE;
1702 sm->_computeError->myName = COMPERR_WARNING;
1711 // send event SUBMESH_COMPUTED
1713 if ( !algo->NeedDiscreteBoundary() )
1714 // send SUBMESH_COMPUTED to dependants of all sub-meshes of shape
1715 for (subS.ReInit(); subS.More(); subS.Next())
1717 SMESH_subMesh* sm = _father->GetSubMesh( subS.Current() );
1718 SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(false,false);
1719 while ( smIt->more() ) {
1721 if ( sm->GetSubShape().ShapeType() == TopAbs_VERTEX )
1722 sm->updateDependantsState( SUBMESH_COMPUTED );
1728 updateDependantsState( SUBMESH_COMPUTED );
1730 // let algo clear its data gathered while algo->Compute()
1731 algo->CheckHypothesis((*_father), _subShape, hyp_status);
1734 case COMPUTE_CANCELED: // nothing to do
1738 removeSubMeshElementsAndNodes();
1739 _computeState = NOT_READY;
1743 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1745 _computeState = READY_TO_COMPUTE;
1747 setAlgoState(MISSING_HYP);
1750 case SUBMESH_COMPUTED: // nothing to do
1752 case SUBMESH_RESTORED:
1753 // check if a mesh is already computed that may
1754 // happen after retrieval from a file
1755 ComputeStateEngine( CHECK_COMPUTE_STATE );
1756 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1758 if (algo) algo->SubmeshRestored( this );
1760 case MESH_ENTITY_REMOVED:
1762 case SUBMESH_LOADED:
1763 loadDependentMeshes();
1764 ComputeSubMeshStateEngine( SUBMESH_LOADED );
1766 case CHECK_COMPUTE_STATE:
1767 if ( IsMeshComputed() )
1768 _computeState = COMPUTE_OK;
1769 else if ( _computeError && _computeError->IsKO() )
1770 _computeState = FAILED_TO_COMPUTE;
1778 // ----------------------------------------------------------------------
1783 case MODIF_ALGO_STATE:
1784 ComputeStateEngine( CLEAN );
1786 if (algo && !algo->NeedDiscreteBoundary())
1787 cleanDependsOn( algo ); // clean sub-meshes with event CLEAN
1789 case COMPUTE: // nothing to do
1791 case COMPUTE_SUBMESH: // nothing to do
1793 case COMPUTE_CANCELED: // nothing to do
1796 cleanDependants(); // clean sub-meshes, dependent on this one, with event CLEAN
1797 removeSubMeshElementsAndNodes();
1798 _computeState = NOT_READY;
1799 if ( _algoState == HYP_OK )
1800 _computeState = READY_TO_COMPUTE;
1802 case SUBMESH_COMPUTED: // nothing to do
1804 case SUBMESH_RESTORED:
1805 ComputeStateEngine( CHECK_COMPUTE_STATE );
1806 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1808 if (algo) algo->SubmeshRestored( this );
1810 case MESH_ENTITY_REMOVED:
1811 updateDependantsState ( CHECK_COMPUTE_STATE );
1812 ComputeStateEngine ( CHECK_COMPUTE_STATE );
1813 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1815 case CHECK_COMPUTE_STATE:
1816 if ( !IsMeshComputed() ) {
1817 if (_algoState == HYP_OK)
1818 _computeState = READY_TO_COMPUTE;
1820 _computeState = NOT_READY;
1823 case SUBMESH_LOADED:
1824 // already treated event, thanks to which _computeState == COMPUTE_OK
1832 // ----------------------------------------------------------------------
1834 case FAILED_TO_COMPUTE:
1837 case MODIF_ALGO_STATE:
1839 ComputeStateEngine( CLEAN );
1841 if (algo && !algo->NeedDiscreteBoundary())
1842 cleanDependsOn( algo ); // clean sub-meshes with event CLEAN
1843 if (_algoState == HYP_OK)
1844 _computeState = READY_TO_COMPUTE;
1846 _computeState = NOT_READY;
1848 case COMPUTE: // nothing to do
1849 case COMPUTE_SUBMESH:
1851 case COMPUTE_CANCELED:
1854 algo->CancelCompute();
1858 cleanDependants(); // submeshes dependent on me should be cleaned as well
1859 removeSubMeshElementsAndNodes();
1861 case SUBMESH_COMPUTED: // allow retry compute
1862 if ( IsEmpty() ) // 23061
1864 if (_algoState == HYP_OK)
1865 _computeState = READY_TO_COMPUTE;
1867 _computeState = NOT_READY;
1870 case SUBMESH_RESTORED:
1871 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1873 case MESH_ENTITY_REMOVED:
1875 case CHECK_COMPUTE_STATE:
1876 if ( IsMeshComputed() )
1877 _computeState = COMPUTE_OK;
1879 if (_algoState == HYP_OK)
1880 _computeState = READY_TO_COMPUTE;
1882 _computeState = NOT_READY;
1884 // case SUBMESH_LOADED:
1892 // ----------------------------------------------------------------------
1898 notifyListenersOnEvent( event, COMPUTE_EVENT );
1904 //=============================================================================
1908 //=============================================================================
1910 bool SMESH_subMesh::Evaluate(MapShapeNbElems& aResMap)
1912 _computeError.reset();
1916 if (_subShape.ShapeType() == TopAbs_VERTEX) {
1917 vector<int> aVec(SMDSEntity_Last,0);
1918 aVec[SMDSEntity_Node] = 1;
1919 aResMap.insert(make_pair(this,aVec));
1923 //SMESH_Gen *gen = _father->GetGen();
1924 SMESH_Algo *algo = 0;
1925 SMESH_Hypothesis::Hypothesis_Status hyp_status;
1928 if( algo && !aResMap.count( this ))
1930 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1931 if (!ret) return false;
1933 if (_father->HasShapeToMesh() && algo->NeedDiscreteBoundary() )
1935 // check submeshes needed
1936 bool subMeshEvaluated = true;
1937 int dimToCheck = SMESH_Gen::GetShapeDim( _subShape ) - 1;
1938 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,/*complexShapeFirst=*/true);
1939 while ( smIt->more() && subMeshEvaluated )
1941 SMESH_subMesh* sm = smIt->next();
1942 int dim = SMESH_Gen::GetShapeDim( sm->GetSubShape() );
1943 if (dim < dimToCheck) break; // the rest subMeshes are all of less dimension
1944 const vector<int> & nbs = aResMap[ sm ];
1945 subMeshEvaluated = (std::accumulate( nbs.begin(), nbs.end(), 0 ) > 0 );
1947 if ( !subMeshEvaluated )
1950 _computeError = SMESH_ComputeError::New(COMPERR_OK,"",algo);
1952 if ( IsMeshComputed() )
1954 vector<int> & nbEntities = aResMap[ this ];
1955 nbEntities.resize( SMDSEntity_Last, 0 );
1956 if ( SMESHDS_SubMesh* sm = GetSubMeshDS() )
1958 nbEntities[ SMDSEntity_Node ] = sm->NbNodes();
1959 SMDS_ElemIteratorPtr elemIt = sm->GetElements();
1960 while ( elemIt->more() )
1961 nbEntities[ elemIt->next()->GetEntityType() ]++;
1966 ret = algo->Evaluate((*_father), _subShape, aResMap);
1968 aResMap.insert( make_pair( this,vector<int>(0)));
1975 //=======================================================================
1977 * \brief Update compute_state by _computeError and send proper events to
1978 * dependent submeshes
1979 * \retval bool - true if _computeError is NOT set
1981 //=======================================================================
1983 bool SMESH_subMesh::checkComputeError(SMESH_Algo* theAlgo,
1984 const bool theComputeOK,
1985 const TopoDS_Shape& theShape)
1987 bool noErrors = true;
1989 if ( !theShape.IsNull() )
1991 // Check state of submeshes
1992 if ( !theAlgo->NeedDiscreteBoundary())
1994 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1995 while ( smIt->more() )
1996 if ( !smIt->next()->checkComputeError( theAlgo, theComputeOK ))
2000 // Check state of neighbours
2001 if ( !theAlgo->OnlyUnaryInput() &&
2002 theShape.ShapeType() == TopAbs_COMPOUND &&
2003 !theShape.IsSame( _subShape ))
2005 for (TopoDS_Iterator subIt( theShape ); subIt.More(); subIt.Next()) {
2006 SMESH_subMesh* sm = _father->GetSubMesh( subIt.Value() );
2008 if ( !sm->checkComputeError( theAlgo, theComputeOK, sm->GetSubShape() ))
2010 updateDependantsState( SUBMESH_COMPUTED ); // send event SUBMESH_COMPUTED
2017 // Set my _computeState
2019 if ( !_computeError || _computeError->IsOK() )
2021 // no error description is set to this sub-mesh, check if any mesh is computed
2022 _computeState = IsMeshComputed() ? COMPUTE_OK : FAILED_TO_COMPUTE;
2023 if ( _computeState != COMPUTE_OK )
2025 if ( _subShape.ShapeType() == TopAbs_EDGE &&
2026 SMESH_Algo::isDegenerated( TopoDS::Edge( _subShape )) )
2027 _computeState = COMPUTE_OK;
2028 else if ( theComputeOK )
2029 _computeError = SMESH_ComputeError::New(COMPERR_NO_MESH_ON_SHAPE,"",theAlgo);
2033 if ( _computeError && !_computeError->IsOK() )
2035 if ( !_computeError->myAlgo )
2036 _computeError->myAlgo = theAlgo;
2040 text << theAlgo->GetName() << " failed on sub-shape #" << _Id << " with error ";
2041 if (_computeError->IsCommon() )
2042 text << _computeError->CommonName();
2044 text << _computeError->myName;
2045 if ( _computeError->myComment.size() > 0 )
2046 text << " \"" << _computeError->myComment << "\"";
2050 _computeState = _computeError->IsKO() ? FAILED_TO_COMPUTE : COMPUTE_OK;
2058 //=======================================================================
2059 //function : updateSubMeshState
2061 //=======================================================================
2063 void SMESH_subMesh::updateSubMeshState(const compute_state theState)
2065 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
2066 while ( smIt->more() )
2067 smIt->next()->_computeState = theState;
2070 //=======================================================================
2071 //function : ComputeSubMeshStateEngine
2073 //=======================================================================
2075 void SMESH_subMesh::ComputeSubMeshStateEngine(compute_event event, const bool includeSelf)
2077 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(includeSelf,false);
2078 while ( smIt->more() )
2079 smIt->next()->ComputeStateEngine(event);
2082 //=======================================================================
2083 //function : updateDependantsState
2085 //=======================================================================
2087 void SMESH_subMesh::updateDependantsState(const compute_event theEvent)
2089 const std::vector< SMESH_subMesh * > & ancestors = GetAncestors();
2090 for ( size_t iA = 0; iA < ancestors.size(); ++iA )
2092 ancestors[ iA ]->ComputeStateEngine( theEvent );
2096 //=======================================================================
2097 //function : cleanDependants
2099 //=======================================================================
2101 void SMESH_subMesh::cleanDependants()
2103 int dimToClean = SMESH_Gen::GetShapeDim( _subShape ) + 1;
2105 const std::vector< SMESH_subMesh * > & ancestors = GetAncestors();
2106 for ( size_t iA = 0; iA < ancestors.size(); ++iA )
2108 const TopoDS_Shape& ancestor = ancestors[ iA ]->GetSubShape();
2109 if ( SMESH_Gen::GetShapeDim( ancestor ) == dimToClean )
2111 // PAL8021. do not go upper than SOLID, else ComputeStateEngine(CLEAN)
2112 // will erase mesh on other shapes in a compound
2113 if ( ancestor.ShapeType() >= TopAbs_SOLID &&
2114 !ancestors[ iA ]->IsEmpty() ) // prevent infinite CLEAN via event lesteners
2115 ancestors[ iA ]->ComputeStateEngine(CLEAN);
2120 //=======================================================================
2121 //function : removeSubMeshElementsAndNodes
2123 //=======================================================================
2125 void SMESH_subMesh::removeSubMeshElementsAndNodes()
2127 cleanSubMesh( this );
2129 // algo may bind a submesh not to _subShape, eg 3D algo
2130 // sets nodes on SHELL while _subShape may be SOLID
2132 int dim = SMESH_Gen::GetShapeDim( _subShape );
2133 int type = _subShape.ShapeType() + 1;
2134 for ( ; type <= TopAbs_EDGE; type++) {
2135 if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
2137 TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
2138 for ( ; exp.More(); exp.Next() )
2139 cleanSubMesh( _father->GetSubMeshContaining( exp.Current() ));
2146 //=======================================================================
2147 //function : getCollection
2148 //purpose : return a shape containing all sub-shapes of the MainShape that can be
2149 // meshed at once along with _subShape
2150 //=======================================================================
2152 TopoDS_Shape SMESH_subMesh::getCollection(SMESH_Gen * theGen,
2153 SMESH_Algo* theAlgo,
2154 bool & theSubComputed,
2155 bool & theSubFailed,
2156 std::vector<SMESH_subMesh*>& theSubs)
2158 theSubComputed = SubMeshesComputed( & theSubFailed );
2160 TopoDS_Shape mainShape = _father->GetMeshDS()->ShapeToMesh();
2162 if ( mainShape.IsSame( _subShape ))
2165 const bool skipAuxHyps = false;
2166 list<const SMESHDS_Hypothesis*> usedHyps =
2167 theAlgo->GetUsedHypothesis( *_father, _subShape, skipAuxHyps ); // copy
2168 std::list < TopoDS_Shape > assiShapes = theAlgo->GetAssignedShapes();
2170 // put in a compound all shapes with the same hypothesis assigned
2171 // and a good ComputeState
2173 TopoDS_Compound aCompound;
2174 BRep_Builder aBuilder;
2175 aBuilder.MakeCompound( aCompound );
2179 SMESH_subMeshIteratorPtr smIt = _father->GetSubMesh( mainShape )->getDependsOnIterator(false);
2180 while ( smIt->more() )
2182 SMESH_subMesh* subMesh = smIt->next();
2183 const TopoDS_Shape& S = subMesh->_subShape;
2184 if ( S.ShapeType() != this->_subShape.ShapeType() )
2186 if ( _allowedSubShapes && !_allowedSubShapes->IsEmpty() && !_allowedSubShapes->Contains( S ))
2188 if ( subMesh == this )
2190 aBuilder.Add( aCompound, S );
2191 theSubs.push_back( subMesh );
2193 else if ( subMesh->GetComputeState() == READY_TO_COMPUTE )
2195 SMESH_Algo* anAlgo = subMesh->GetAlgo();
2196 if (( anAlgo->IsSameName( *theAlgo )) && // same algo
2197 ( anAlgo->GetUsedHypothesis( *_father, S, skipAuxHyps ) == usedHyps ) && // same hyps
2198 ( anAlgo->GetAssignedShapes() == assiShapes ) && // on same sub-shapes
2199 ( _algoShape == subMesh->_algoShape ))
2201 aBuilder.Add( aCompound, S );
2202 if ( !subMesh->SubMeshesComputed() )
2203 theSubComputed = false;
2204 theSubs.push_back( subMesh );
2212 //=======================================================================
2213 //function : getSimilarAttached
2214 //purpose : return a hypothesis attached to theShape.
2215 // If theHyp is provided, similar but not same hypotheses
2216 // is returned; else only applicable ones having theHypType
2218 //=======================================================================
2220 const SMESH_Hypothesis* SMESH_subMesh::getSimilarAttached(const TopoDS_Shape& theShape,
2221 const SMESH_Hypothesis * theHyp,
2222 const int theHypType)
2224 SMESH_HypoFilter hypoKind;
2225 hypoKind.Init( hypoKind.HasType( theHyp ? theHyp->GetType() : theHypType ));
2227 hypoKind.And ( hypoKind.HasDim( theHyp->GetDim() ));
2228 hypoKind.AndNot( hypoKind.Is( theHyp ));
2229 if ( theHyp->IsAuxiliary() )
2230 hypoKind.And( hypoKind.HasName( theHyp->GetName() ));
2232 hypoKind.AndNot( hypoKind.IsAuxiliary());
2235 hypoKind.And( hypoKind.IsApplicableTo( theShape ));
2238 return _father->GetHypothesis( theShape, hypoKind, false );
2241 //=======================================================================
2242 //function : CheckConcurrentHypothesis
2243 //purpose : check if there are several applicable hypothesis attached to
2245 //=======================================================================
2247 SMESH_Hypothesis::Hypothesis_Status
2248 SMESH_subMesh::CheckConcurrentHypothesis (const int theHypType)
2250 // is there local hypothesis on me?
2251 if ( getSimilarAttached( _subShape, 0, theHypType ) )
2252 return SMESH_Hypothesis::HYP_OK;
2255 TopoDS_Shape aPrevWithHyp;
2256 const SMESH_Hypothesis* aPrevHyp = 0;
2257 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
2258 for (; it.More(); it.Next())
2260 const TopoDS_Shape& ancestor = it.Value();
2261 const SMESH_Hypothesis* hyp = getSimilarAttached( ancestor, 0, theHypType );
2264 if ( aPrevWithHyp.IsNull() || aPrevWithHyp.IsSame( ancestor ))
2266 aPrevWithHyp = ancestor;
2269 else if ( aPrevWithHyp.ShapeType() == ancestor.ShapeType() && aPrevHyp != hyp )
2270 return SMESH_Hypothesis::HYP_CONCURRENT;
2272 return SMESH_Hypothesis::HYP_OK;
2275 return SMESH_Hypothesis::HYP_OK;
2278 //================================================================================
2280 * \brief Constructor of OwnListenerData
2282 //================================================================================
2284 SMESH_subMesh::OwnListenerData::OwnListenerData( SMESH_subMesh* sm, EventListener* el):
2286 myMeshID( sm ? sm->GetFather()->GetId() : -1 ),
2287 mySubMeshID( sm ? sm->GetId() : -1 ),
2292 //================================================================================
2294 * \brief Sets an event listener and its data to a submesh
2295 * \param listener - the listener to store
2296 * \param data - the listener data to store
2297 * \param where - the submesh to store the listener and it's data
2299 * It remembers the submesh where it puts the listener in order to delete
2300 * them when HYP_OK algo_state is lost
2301 * After being set, event listener is notified on each event of where submesh.
2303 //================================================================================
2305 void SMESH_subMesh::SetEventListener(EventListener* listener,
2306 EventListenerData* data,
2307 SMESH_subMesh* where)
2309 if ( listener && where ) {
2310 where->setEventListener( listener, data );
2311 _ownListeners.push_back( OwnListenerData( where, listener ));
2315 //================================================================================
2317 * \brief Sets an event listener and its data to a submesh
2318 * \param listener - the listener to store
2319 * \param data - the listener data to store
2321 * After being set, event listener is notified on each event of a submesh.
2323 //================================================================================
2325 void SMESH_subMesh::setEventListener(EventListener* listener,
2326 EventListenerData* data)
2328 map< EventListener*, EventListenerData* >::iterator l_d =
2329 _eventListeners.find( listener );
2330 if ( l_d != _eventListeners.end() ) {
2331 EventListenerData* curData = l_d->second;
2333 if ( curData && curData != data && curData->IsDeletable() )
2338 for ( l_d = _eventListeners.begin(); l_d != _eventListeners.end(); ++l_d )
2339 if ( listener->GetName() == l_d->first->GetName() )
2341 EventListenerData* curData = l_d->second;
2343 if ( curData && curData != data && curData->IsDeletable() )
2345 if ( l_d->first != listener && l_d->first->IsDeletable() )
2347 _eventListeners.erase( l_d );
2350 _eventListeners.insert( make_pair( listener, data ));
2354 //================================================================================
2356 * \brief Return an event listener data
2357 * \param listener - the listener whose data is
2358 * \param myOwn - if \c true, returns a listener set by this sub-mesh,
2359 * else returns a listener listening to events of this sub-mesh
2360 * \retval EventListenerData* - found data, maybe NULL
2362 //================================================================================
2364 EventListenerData* SMESH_subMesh::GetEventListenerData(EventListener* listener,
2365 const bool myOwn) const
2369 list< OwnListenerData >::const_iterator d;
2370 for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
2372 if ( d->myListener == listener && _father->MeshExists( d->myMeshID ))
2373 return d->mySubMesh->GetEventListenerData( listener, !myOwn );
2378 map< EventListener*, EventListenerData* >::const_iterator l_d =
2379 _eventListeners.find( listener );
2380 if ( l_d != _eventListeners.end() )
2386 //================================================================================
2388 * \brief Return an event listener data
2389 * \param listenerName - the listener name
2390 * \param myOwn - if \c true, returns a listener set by this sub-mesh,
2391 * else returns a listener listening to events of this sub-mesh
2392 * \retval EventListenerData* - found data, maybe NULL
2394 //================================================================================
2396 EventListenerData* SMESH_subMesh::GetEventListenerData(const string& listenerName,
2397 const bool myOwn) const
2401 list< OwnListenerData >::const_iterator d;
2402 for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
2404 if ( _father->MeshExists( d->myMeshID ) && listenerName == d->myListener->GetName())
2405 return d->mySubMesh->GetEventListenerData( listenerName, !myOwn );
2410 map< EventListener*, EventListenerData* >::const_iterator l_d = _eventListeners.begin();
2411 for ( ; l_d != _eventListeners.end(); ++l_d )
2412 if ( listenerName == l_d->first->GetName() )
2418 //================================================================================
2420 * \brief Notify stored event listeners on the occurred event
2421 * \param event - algo_event or compute_event itself
2422 * \param eventType - algo_event or compute_event
2423 * \param hyp - hypothesis, if eventType is algo_event
2425 //================================================================================
2427 void SMESH_subMesh::notifyListenersOnEvent( const int event,
2428 const event_type eventType,
2429 SMESH_Hypothesis* hyp)
2431 list< pair< EventListener*, EventListenerData* > > eventListeners( _eventListeners.begin(),
2432 _eventListeners.end());
2433 list< pair< EventListener*, EventListenerData* > >::iterator l_d = eventListeners.begin();
2434 for ( ; l_d != eventListeners.end(); ++l_d )
2436 std::pair< EventListener*, EventListenerData* > li_da = *l_d;
2437 if ( !_eventListeners.count( li_da.first )) continue;
2439 if ( li_da.first->myBusySM.insert( this ).second )
2441 const bool isDeletable = li_da.first->IsDeletable();
2443 li_da.first->ProcessEvent( event, eventType, this, li_da.second, hyp );
2445 if ( !isDeletable || _eventListeners.count( li_da.first ))
2446 li_da.first->myBusySM.erase( this ); // a listener is hopefully not dead
2451 //================================================================================
2453 * \brief Unregister the listener and delete listener's data
2454 * \param listener - the event listener
2456 //================================================================================
2458 void SMESH_subMesh::DeleteEventListener(EventListener* listener)
2460 map< EventListener*, EventListenerData* >::iterator l_d =
2461 _eventListeners.find( listener );
2462 if ( l_d != _eventListeners.end() && l_d->first )
2464 if ( l_d->second && l_d->second->IsDeletable() )
2468 l_d->first->myBusySM.erase( this );
2469 if ( l_d->first->IsDeletable() )
2471 l_d->first->BeforeDelete( this, l_d->second );
2474 _eventListeners.erase( l_d );
2478 //================================================================================
2480 * \brief Delete event listeners depending on algo of this submesh
2482 //================================================================================
2484 void SMESH_subMesh::deleteOwnListeners()
2486 list< OwnListenerData >::iterator d;
2487 for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
2489 SMESH_Mesh* mesh = _father->FindMesh( d->myMeshID );
2490 if ( !mesh || !mesh->GetSubMeshContaining( d->mySubMeshID ))
2492 d->mySubMesh->DeleteEventListener( d->myListener );
2494 _ownListeners.clear();
2497 //=======================================================================
2498 //function : loadDependentMeshes
2499 //purpose : loads dependent meshes on SUBMESH_LOADED event
2500 //=======================================================================
2502 void SMESH_subMesh::loadDependentMeshes()
2504 list< OwnListenerData >::iterator d;
2505 for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
2506 if ( _father != d->mySubMesh->_father &&
2507 _father->FindMesh( d->myMeshID ))
2508 d->mySubMesh->_father->Load();
2510 // map< EventListener*, EventListenerData* >::iterator l_d = _eventListeners.begin();
2511 // for ( ; l_d != _eventListeners.end(); ++l_d )
2512 // if ( l_d->second )
2514 // const list<SMESH_subMesh*>& smList = l_d->second->mySubMeshes;
2515 // list<SMESH_subMesh*>::const_iterator sm = smList.begin();
2516 // for ( ; sm != smList.end(); ++sm )
2517 // if ( _father != (*sm)->_father )
2518 // (*sm)->_father->Load();
2522 //================================================================================
2524 * \brief Do something on a certain event
2525 * \param event - algo_event or compute_event itself
2526 * \param eventType - algo_event or compute_event
2527 * \param subMesh - the submesh where the event occurs
2528 * \param data - listener data stored in the subMesh
2529 * \param hyp - hypothesis, if eventType is algo_event
2531 * The base implementation translates CLEAN event to the subMesh
2532 * stored in listener data. Also it sends SUBMESH_COMPUTED event in case of
2533 * successful COMPUTE event.
2535 //================================================================================
2537 void SMESH_subMeshEventListener::ProcessEvent(const int event,
2538 const int eventType,
2539 SMESH_subMesh* subMesh,
2540 EventListenerData* data,
2541 const SMESH_Hypothesis* /*hyp*/)
2543 if ( data && !data->mySubMeshes.empty() &&
2544 eventType == SMESH_subMesh::COMPUTE_EVENT)
2546 ASSERT( data->mySubMeshes.front() != subMesh );
2547 list<SMESH_subMesh*>::iterator smIt = data->mySubMeshes.begin();
2548 list<SMESH_subMesh*>::iterator smEnd = data->mySubMeshes.end();
2550 case SMESH_subMesh::CLEAN:
2551 for ( ; smIt != smEnd; ++ smIt)
2552 (*smIt)->ComputeStateEngine( SMESH_subMesh::compute_event( event ));
2554 case SMESH_subMesh::COMPUTE:
2555 case SMESH_subMesh::COMPUTE_SUBMESH:
2556 if ( subMesh->GetComputeState() == SMESH_subMesh::COMPUTE_OK )
2557 for ( ; smIt != smEnd; ++ smIt)
2558 (*smIt)->ComputeStateEngine( SMESH_subMesh::SUBMESH_COMPUTED );
2567 //================================================================================
2569 * \brief Iterator over submeshes and optionally prepended or appended one
2571 //================================================================================
2573 struct _Iterator : public SMDS_Iterator<SMESH_subMesh*>
2575 _Iterator(SMDS_Iterator<SMESH_subMesh*>* subIt,
2576 SMESH_subMesh* prepend,
2577 SMESH_subMesh* append): myAppend(append), myIt(subIt)
2579 myCur = prepend ? prepend : myIt->more() ? myIt->next() : append;
2580 if ( myCur == append ) append = 0;
2582 /// Return true if and only if there are other object in this iterator
2587 /// Return the current object and step to the next one
2588 virtual SMESH_subMesh* next()
2590 SMESH_subMesh* res = myCur;
2591 if ( myIt->more() ) { myCur = myIt->next(); }
2592 else { myCur = myAppend; myAppend = 0; }
2599 SMESH_subMesh *myAppend, *myCur;
2600 SMDS_Iterator<SMESH_subMesh*> *myIt;
2604 //================================================================================
2606 * \brief Return iterator on the submeshes this one depends on
2607 * \param includeSelf - this submesh to be returned also
2608 * \param reverse - if true, complex shape submeshes go first
2610 //================================================================================
2612 SMESH_subMeshIteratorPtr SMESH_subMesh::getDependsOnIterator(const bool includeSelf,
2613 const bool reverse) const
2615 SMESH_subMesh *me = (SMESH_subMesh*) this;
2616 SMESH_subMesh *prepend=0, *append=0;
2617 if ( includeSelf ) {
2618 if ( reverse ) prepend = me;
2621 typedef map < int, SMESH_subMesh * > TMap;
2624 return SMESH_subMeshIteratorPtr
2625 ( new _Iterator( new SMDS_mapReverseIterator<TMap>( me->DependsOn() ), prepend, append ));
2628 return SMESH_subMeshIteratorPtr
2629 ( new _Iterator( new SMDS_mapIterator<TMap>( me->DependsOn() ), prepend, append ));
2633 //================================================================================
2635 * \brief Returns ancestor sub-meshes. Finds them if not yet found.
2637 //================================================================================
2639 const std::vector< SMESH_subMesh * > & SMESH_subMesh::GetAncestors() const
2641 if ( _ancestors.empty() &&
2642 !_subShape.IsSame( _father->GetShapeToMesh() ))
2644 const TopTools_ListOfShape& ancShapes = _father->GetAncestors( _subShape );
2646 SMESH_subMesh* me = const_cast< SMESH_subMesh* >( this );
2647 me->_ancestors.reserve( ancShapes.Extent() );
2649 // assure that all sub-meshes exist
2650 TopoDS_Shape mainShape = _father->GetShapeToMesh();
2651 if ( !mainShape.IsNull() )
2652 _father->GetSubMesh( mainShape )->DependsOn();
2654 TopTools_MapOfShape map;
2656 for ( TopTools_ListIteratorOfListOfShape it( ancShapes ); it.More(); it.Next() )
2657 if ( SMESH_subMesh* sm = _father->GetSubMeshContaining( it.Value() ))
2658 if ( map.Add( it.Value() ))
2659 me->_ancestors.push_back( sm );
2665 //================================================================================
2667 * \brief Clears the vector of ancestor sub-meshes
2669 //================================================================================
2671 void SMESH_subMesh::ClearAncestors()
2676 //================================================================================
2678 * \brief Find common submeshes (based on shared sub-shapes with other
2679 * \param theOther submesh to check
2680 * \param theSetOfCommon set of common submesh
2682 //================================================================================
2684 bool SMESH_subMesh::FindIntersection(const SMESH_subMesh* theOther,
2685 std::set<const SMESH_subMesh*>& theSetOfCommon ) const
2687 size_t oldNb = theSetOfCommon.size();
2689 // check main submeshes
2690 const map <int, SMESH_subMesh*>::const_iterator otherEnd = theOther->_mapDepend.end();
2691 if ( theOther->_mapDepend.find(this->GetId()) != otherEnd )
2692 theSetOfCommon.insert( this );
2693 if ( _mapDepend.find(theOther->GetId()) != _mapDepend.end() )
2694 theSetOfCommon.insert( theOther );
2696 // check common submeshes
2697 map <int, SMESH_subMesh*>::const_iterator mapIt = _mapDepend.begin();
2698 for( ; mapIt != _mapDepend.end(); mapIt++ )
2699 if ( theOther->_mapDepend.find((*mapIt).first) != otherEnd )
2700 theSetOfCommon.insert( (*mapIt).second );
2701 return oldNb < theSetOfCommon.size();