1 // Copyright (C) 2007-2021 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"
42 #include "Basics_Utils.hxx"
44 #include <BRep_Builder.hxx>
45 #include <BRep_Tool.hxx>
47 #include <TopExp_Explorer.hxx>
48 #include <TopTools_IndexedMapOfShape.hxx>
49 #include <TopTools_ListIteratorOfListOfShape.hxx>
50 #include <TopTools_ListOfShape.hxx>
52 #include <TopoDS_Compound.hxx>
53 #include <TopoDS_Iterator.hxx>
56 #include <Standard_OutOfMemory.hxx>
57 #include <Standard_ErrorHandler.hxx>
64 // enable printing algo + shape id + hypo used while meshing
65 //#define PRINT_WHO_COMPUTE_WHAT
68 #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 Returns a current algorithm
214 //================================================================================
216 SMESH_Algo* SMESH_subMesh::CopyAlgo() const
218 //SMESH_Algo* algo = (SMESH_Algo*) new StdMeshers_Regular_1D(666, _father->GetGent());
224 //================================================================================
226 * \brief Allow algo->Compute() if a sub-shape of lower dim is meshed but
227 * none mesh entity is bound to it (PAL13615, 2nd part)
229 //================================================================================
231 void SMESH_subMesh::SetIsAlwaysComputed(bool isAlCo)
233 _alwaysComputed = isAlCo;
234 if ( _alwaysComputed )
235 _computeState = COMPUTE_OK;
237 ComputeStateEngine( CHECK_COMPUTE_STATE );
240 //=======================================================================
242 * \brief Return true if no mesh entities is bound to the submesh
244 //=======================================================================
246 bool SMESH_subMesh::IsEmpty() const
248 if (SMESHDS_SubMesh * subMeshDS = ((SMESH_subMesh*)this)->GetSubMeshDS())
249 return (!subMeshDS->NbElements() && !subMeshDS->NbNodes());
253 //=======================================================================
254 //function : IsMeshComputed
255 //purpose : check if _subMeshDS contains mesh elements
256 //=======================================================================
258 bool SMESH_subMesh::IsMeshComputed() const
260 if ( _alwaysComputed )
262 // algo may bind a sub-mesh not to _subShape, eg 3D algo
263 // sets nodes on SHELL while _subShape may be SOLID
265 SMESHDS_Mesh* meshDS = _father->GetMeshDS();
266 int dim = SMESH_Gen::GetShapeDim( _subShape );
267 int type = _subShape.ShapeType();
268 for ( ; type <= TopAbs_VERTEX; type++) {
269 if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
271 TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
272 for ( ; exp.More(); exp.Next() )
274 if ( SMESHDS_SubMesh * smDS = meshDS->MeshElements( exp.Current() ) )
276 bool computed = (dim > 0) ? smDS->NbElements() : smDS->NbNodes();
289 //================================================================================
291 * \brief Check if any upper level sub-shape is not computed.
292 * Used to update a sub-mesh icon
294 //================================================================================
296 bool SMESH_subMesh::IsComputedPartially() const
298 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(/*includeSelf=*/true,
299 /*SolidFirst=*/true);
300 bool allComputed = true;
301 TopAbs_ShapeEnum readyType = TopAbs_VERTEX; // max value
302 while ( smIt->more() && allComputed )
304 SMESH_subMesh* sm = smIt->next();
306 if ( sm->GetSubShape().ShapeType() > readyType )
307 break; // lower dimension -> stop
308 if ( sm->GetComputeState() != SMESH_subMesh::NOT_READY )
309 readyType = sm->GetSubShape().ShapeType();
311 switch ( sm->GetComputeState() )
313 case SMESH_subMesh::READY_TO_COMPUTE:
314 case SMESH_subMesh::FAILED_TO_COMPUTE:
315 allComputed = false;// sm->IsMeshComputed();
317 case SMESH_subMesh::NOT_READY:
318 case SMESH_subMesh::COMPUTE_OK:
325 //=============================================================================
327 * Return true if all sub-meshes have been meshed
329 //=============================================================================
331 bool SMESH_subMesh::SubMeshesComputed(bool * isFailedToCompute/*=0*/) const
333 int myDim = SMESH_Gen::GetShapeDim( _subShape );
334 int dimToCheck = myDim - 1;
335 bool subMeshesComputed = true;
336 if ( isFailedToCompute ) *isFailedToCompute = false;
337 // check sub-meshes with upper dimension => reverse iteration
338 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,true);
339 while ( smIt->more() )
341 SMESH_subMesh *sm = smIt->next();
342 if ( sm->_alwaysComputed )
344 const TopoDS_Shape & ss = sm->GetSubShape();
346 // MSV 07.04.2006: restrict checking to myDim-1 only. Ex., there is no sense
347 // in checking of existence of edges if the algo needs only faces. Moreover,
348 // degenerated edges may have no sub-mesh, as after computing NETGEN_2D.
349 if ( !_algo || _algo->NeedDiscreteBoundary() ) {
350 int dim = SMESH_Gen::GetShapeDim( ss );
351 if (dim < dimToCheck)
352 break; // the rest sub-meshes are all of less dimension
354 SMESHDS_SubMesh * ds = sm->GetSubMeshDS();
355 bool computeOk = ((sm->GetComputeState() == COMPUTE_OK ) ||
356 (ds && ( dimToCheck ? ds->NbElements() : ds->NbNodes() )));
359 subMeshesComputed = false;
361 if ( isFailedToCompute && !(*isFailedToCompute) )
362 *isFailedToCompute = ( sm->GetComputeState() == FAILED_TO_COMPUTE );
364 if ( !isFailedToCompute )
368 return subMeshesComputed;
371 //================================================================================
373 * \brief Return cost of computing this sub-mesh. If hypotheses are not well defined,
375 * \return int - the computation cost in abstract units.
377 //================================================================================
379 int SMESH_subMesh::GetComputeCost() const
381 return _realComputeCost;
384 //================================================================================
386 * \brief Return cost of computing this sub-mesh. The cost depends on the shape type
387 * and number of sub-meshes this one DependsOn().
388 * \return int - the computation cost in abstract units.
390 //================================================================================
392 int SMESH_subMesh::computeCost() const
397 switch ( _subShape.ShapeType() ) {
399 case TopAbs_SHELL: computeCost = 5000; break;
400 case TopAbs_FACE: computeCost = 500; break;
401 case TopAbs_EDGE: computeCost = 2; break;
402 default: computeCost = 1;
404 SMESH_subMeshIteratorPtr childIt = getDependsOnIterator(/*includeSelf=*/false);
405 while ( childIt->more() )
406 computeCost += childIt->next()->computeCost();
408 ((SMESH_subMesh*)this)->_computeCost = computeCost;
413 //=============================================================================
415 * Returns all sub-meshes this one depend on
417 //=============================================================================
419 const std::map < int, SMESH_subMesh * >& SMESH_subMesh::DependsOn()
421 if ( _dependenceAnalysed || !_father->HasShapeToMesh() )
424 int type = _subShape.ShapeType();
427 case TopAbs_COMPOUND:
429 list< TopoDS_Shape > compounds( 1, _subShape );
430 list< TopoDS_Shape >::iterator comp = compounds.begin();
431 for ( ; comp != compounds.end(); ++comp )
433 for ( TopoDS_Iterator sub( *comp ); sub.More(); sub.Next() )
434 switch ( sub.Value().ShapeType() )
436 case TopAbs_COMPOUND: compounds.push_back( sub.Value() ); break;
437 case TopAbs_COMPSOLID: insertDependence( sub.Value(), TopAbs_SOLID ); break;
438 case TopAbs_SOLID: insertDependence( sub.Value(), TopAbs_SOLID ); break;
439 case TopAbs_SHELL: insertDependence( sub.Value(), TopAbs_FACE ); break;
440 case TopAbs_FACE: insertDependence( sub.Value(), TopAbs_FACE ); break;
441 case TopAbs_WIRE: insertDependence( sub.Value(), TopAbs_EDGE ); break;
442 case TopAbs_EDGE: insertDependence( sub.Value(), TopAbs_EDGE ); break;
443 case TopAbs_VERTEX: insertDependence( sub.Value(), TopAbs_VERTEX ); break;
449 case TopAbs_COMPSOLID: insertDependence( _subShape, TopAbs_SOLID ); break;
450 case TopAbs_SOLID: insertDependence( _subShape, TopAbs_FACE );
451 { /*internal EDGE*/ insertDependence( _subShape, TopAbs_EDGE, TopAbs_WIRE ); break; }
452 case TopAbs_SHELL: insertDependence( _subShape, TopAbs_FACE ); break;
453 case TopAbs_FACE: insertDependence( _subShape, TopAbs_EDGE ); break;
454 case TopAbs_WIRE: insertDependence( _subShape, TopAbs_EDGE ); break;
455 case TopAbs_EDGE: insertDependence( _subShape, TopAbs_VERTEX ); break;
458 _dependenceAnalysed = true;
462 //================================================================================
464 * \brief Return a key for SMESH_subMesh::_mapDepend map
466 //================================================================================
470 int dependsOnMapKey( TopAbs_ShapeEnum type, int shapeID )
472 int ordType = 9 - int(type); // 2 = Vertex, 8 = CompSolid
474 cle += 10000000 * ordType; // sort map by ordType then index
477 int dependsOnMapKey( const SMESH_subMesh* sm )
479 return dependsOnMapKey( sm->GetSubShape().ShapeType(), sm->GetId() );
483 //=============================================================================
485 * Add sub-meshes on sub-shapes of a given type into the dependence map.
487 //=============================================================================
489 void SMESH_subMesh::insertDependence(const TopoDS_Shape aShape,
490 TopAbs_ShapeEnum aSubType,
491 TopAbs_ShapeEnum avoidType)
493 TopExp_Explorer sub( aShape, aSubType, avoidType );
494 for ( ; sub.More(); sub.Next() )
496 SMESH_subMesh *aSubMesh = _father->GetSubMesh( sub.Current() );
497 if ( aSubMesh->GetId() == 0 )
498 continue; // not a sub-shape of the shape to mesh
499 int cle = dependsOnMapKey( aSubMesh );
500 if ( _mapDepend.find( cle ) == _mapDepend.end())
502 _mapDepend[cle] = aSubMesh;
503 const map < int, SMESH_subMesh * > & subMap = aSubMesh->DependsOn();
504 _mapDepend.insert( subMap.begin(), subMap.end() );
509 //================================================================================
511 * \brief Return \c true if \a this sub-mesh depends on \a other
513 //================================================================================
515 bool SMESH_subMesh::DependsOn( const SMESH_subMesh* other ) const
517 return other ? _mapDepend.count( dependsOnMapKey( other )) : false;
520 //================================================================================
522 * \brief Return \c true if \a this sub-mesh depends on a \a shape
524 //================================================================================
526 bool SMESH_subMesh::DependsOn( const int shapeID ) const
528 return DependsOn( _father->GetSubMeshContaining( shapeID ));
531 //=============================================================================
533 * Return a shape of \a this sub-mesh
535 //=============================================================================
537 const TopoDS_Shape & SMESH_subMesh::GetSubShape() const
542 //=======================================================================
543 //function : CanAddHypothesis
544 //purpose : return true if theHypothesis can be attached to me:
545 // its dimension is checked
546 //=======================================================================
548 bool SMESH_subMesh::CanAddHypothesis(const SMESH_Hypothesis* theHypothesis) const
550 int aHypDim = theHypothesis->GetDim();
551 int aShapeDim = SMESH_Gen::GetShapeDim(_subShape);
552 // issue 21106. Forbid 3D mesh on the SHELL
553 // if (aHypDim == 3 && aShapeDim == 3) {
554 // // check case of open shell
555 // //if (_subShape.ShapeType() == TopAbs_SHELL && !_subShape.Closed())
556 // if (_subShape.ShapeType() == TopAbs_SHELL && !BRep_Tool::IsClosed(_subShape))
559 if ( aHypDim <= aShapeDim )
565 //=======================================================================
566 //function : IsApplicableHypothesis
567 //purpose : check if this sub-mesh can be computed using a hypothesis
568 //=======================================================================
570 bool SMESH_subMesh::IsApplicableHypothesis(const SMESH_Hypothesis* theHypothesis) const
572 if ( !_father->HasShapeToMesh() && _subShape.ShapeType() == TopAbs_SOLID )
573 return true; // true for the PseudoShape
575 return IsApplicableHypothesis( theHypothesis, _subShape.ShapeType() );
578 //=======================================================================
579 //function : IsApplicableHypothesis
580 //purpose : compare shape type and hypothesis type
581 //=======================================================================
583 bool SMESH_subMesh::IsApplicableHypothesis(const SMESH_Hypothesis* theHypothesis,
584 const TopAbs_ShapeEnum theShapeType)
586 if ( theHypothesis->GetType() > SMESHDS_Hypothesis::PARAM_ALGO)
589 if ( theHypothesis->GetShapeType() & (1<< theShapeType))
590 // issue 21106. Forbid 3D mesh on the SHELL
591 return !( theHypothesis->GetDim() == 3 && theShapeType == TopAbs_SHELL );
597 switch ( theShapeType ) {
602 return SMESH_Gen::GetShapeDim( theShapeType ) == theHypothesis->GetDim();
605 // Special case for algorithms, building 2D mesh on a whole shell.
606 // Before this fix there was a problem after restoring from study,
607 // because in that case algorithm is assigned before hypothesis
608 // (on shell in problem case) and hypothesis is checked on faces
609 // (because it is 2D), where we have NO_ALGO state.
610 // Now 2D hypothesis is also applicable to shells.
611 return (theHypothesis->GetDim() == 2 || theHypothesis->GetDim() == 3);
614 // case TopAbs_COMPSOLID:
615 // case TopAbs_COMPOUND:
621 //================================================================================
623 * \brief Treats modification of hypotheses definition
624 * \param [in] event - what happens
625 * \param [in] anHyp - a hypothesis
626 * \return SMESH_Hypothesis::Hypothesis_Status - a treatment result.
628 * Optional description of a problematic situation (if any) can be retrieved
629 * via GetComputeError().
631 //================================================================================
633 SMESH_Hypothesis::Hypothesis_Status
634 SMESH_subMesh::AlgoStateEngine(algo_event event, SMESH_Hypothesis * anHyp)
636 // **** les retour des evenement shape sont significatifs
637 // (add ou remove fait ou non)
638 // le retour des evenement father n'indiquent pas que add ou remove fait
640 SMESH_Hypothesis::Hypothesis_Status aux_ret, ret = SMESH_Hypothesis::HYP_OK;
641 if ( _Id == 0 ) return ret; // not a sub-shape of the shape to mesh
643 SMESHDS_Mesh* meshDS =_father->GetMeshDS();
644 SMESH_Algo* algo = 0;
647 if (_subShape.ShapeType() == TopAbs_VERTEX )
649 if ( anHyp->GetDim() != 0) {
650 if (event == ADD_HYP || event == ADD_ALGO)
651 return SMESH_Hypothesis::HYP_BAD_DIM;
653 return SMESH_Hypothesis::HYP_OK;
656 else if ( _algoState == HYP_OK ) {
657 // update default _algoState
658 if ( event != REMOVE_FATHER_ALGO )
660 _algoState = NO_ALGO;
663 _algoState = MISSING_HYP;
664 if ( event == REMOVE_FATHER_HYP ||
665 algo->CheckHypothesis(*_father,_subShape, aux_ret))
672 int oldAlgoState = _algoState;
673 bool modifiedHyp = (event == MODIF_HYP); // if set to true, force event MODIF_ALGO_STATE
674 SMESH_Algo* algoRequiringCleaning = 0;
676 bool isApplicableHyp = IsApplicableHypothesis( anHyp );
678 if (event == ADD_ALGO || event == ADD_FATHER_ALGO)
680 // -------------------------------------------
681 // check if a shape needed by algo is present
682 // -------------------------------------------
683 algo = static_cast< SMESH_Algo* >( anHyp );
684 if ( !_father->HasShapeToMesh() && algo->NeedShape() )
685 return SMESH_Hypothesis::HYP_NEED_SHAPE;
686 // ----------------------
687 // check mesh conformity
688 // ----------------------
689 if (isApplicableHyp && !_father->IsNotConformAllowed() && !IsConform( algo ))
690 return SMESH_Hypothesis::HYP_NOTCONFORM;
692 // check if all-dimensional algo is hidden by other local one
693 if ( event == ADD_ALGO ) {
694 SMESH_HypoFilter filter( SMESH_HypoFilter::HasType( algo->GetType() ));
695 filter.Or( SMESH_HypoFilter::HasType( algo->GetType()+1 ));
696 filter.Or( SMESH_HypoFilter::HasType( algo->GetType()+2 ));
697 if ( SMESH_Algo * curAlgo = (SMESH_Algo*)_father->GetHypothesis( this, filter, true ))
698 if ( !curAlgo->NeedDiscreteBoundary() && curAlgo != anHyp )
699 algoRequiringCleaning = curAlgo;
703 // ----------------------------------
704 // add a hypothesis to DS if possible
705 // ----------------------------------
706 if (event == ADD_HYP || event == ADD_ALGO)
708 if ( ! CanAddHypothesis( anHyp )) // check dimension
709 return SMESH_Hypothesis::HYP_BAD_DIM;
711 if ( !anHyp->IsAuxiliary() && getSimilarAttached( _subShape, anHyp ) )
712 return SMESH_Hypothesis::HYP_ALREADY_EXIST;
714 if ( !meshDS->AddHypothesis(_subShape, anHyp))
715 return SMESH_Hypothesis::HYP_ALREADY_EXIST;
718 // --------------------------
719 // remove a hypothesis from DS
720 // --------------------------
721 if (event == REMOVE_HYP || event == REMOVE_ALGO)
723 if (!meshDS->RemoveHypothesis(_subShape, anHyp))
724 return SMESH_Hypothesis::HYP_OK; // nothing changes
726 if (event == REMOVE_ALGO)
728 algo = dynamic_cast<SMESH_Algo*> (anHyp);
729 if (!algo->NeedDiscreteBoundary())
730 algoRequiringCleaning = algo;
734 // ------------------
735 // analyse algo state
736 // ------------------
737 if (!isApplicableHyp)
738 return ret; // not applicable hypotheses do not change algo state
740 if (( algo = GetAlgo()))
741 algo->InitComputeError();
746 // ----------------------------------------------------------------------
755 if (algo->CheckHypothesis((*_father),_subShape, aux_ret))
756 setAlgoState(HYP_OK);
757 else if ( algo->IsStatusFatal( aux_ret )) {
758 meshDS->RemoveHypothesis(_subShape, anHyp);
762 setAlgoState(MISSING_HYP);
769 case ADD_FATHER_ALGO: { // Algo just added in father
772 if ( algo == anHyp ) {
773 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret))
774 setAlgoState(HYP_OK);
776 setAlgoState(MISSING_HYP);
780 case REMOVE_FATHER_HYP:
782 case REMOVE_FATHER_ALGO: {
786 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
787 setAlgoState(HYP_OK);
789 setAlgoState(MISSING_HYP);
793 case MODIF_HYP: break;
800 // ----------------------------------------------------------------------
808 if ( algo->CheckHypothesis((*_father),_subShape, ret ))
809 setAlgoState(HYP_OK);
810 if (SMESH_Hypothesis::IsStatusFatal( ret ))
811 meshDS->RemoveHypothesis(_subShape, anHyp);
812 else if (!_father->IsUsedHypothesis( anHyp, this ))
814 meshDS->RemoveHypothesis(_subShape, anHyp);
815 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
819 case ADD_ALGO: { //already existing algo : on father ?
822 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))// ignore hyp status
823 setAlgoState(HYP_OK);
824 else if ( algo->IsStatusFatal( aux_ret )) {
825 meshDS->RemoveHypothesis(_subShape, anHyp);
829 setAlgoState(MISSING_HYP);
834 case REMOVE_ALGO: { // perhaps a father algo applies ?
836 if (algo == NULL) // no more algo applying on sub-shape...
838 setAlgoState(NO_ALGO);
842 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
843 setAlgoState(HYP_OK);
845 setAlgoState(MISSING_HYP);
849 case MODIF_HYP: // assigned hypothesis value may become good
850 case ADD_FATHER_HYP: {
853 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
854 setAlgoState(HYP_OK);
856 setAlgoState(MISSING_HYP);
859 case ADD_FATHER_ALGO: { // new father algo
862 if ( algo == anHyp ) {
863 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
864 setAlgoState(HYP_OK);
866 setAlgoState(MISSING_HYP);
870 case REMOVE_FATHER_HYP: // nothing to do
872 case REMOVE_FATHER_ALGO: {
874 if (algo == NULL) // no more applying algo on father
876 setAlgoState(NO_ALGO);
880 if ( algo->CheckHypothesis((*_father),_subShape , aux_ret ))
881 setAlgoState(HYP_OK);
883 setAlgoState(MISSING_HYP);
893 // ----------------------------------------------------------------------
901 if (!algo->CheckHypothesis((*_father),_subShape, ret ))
903 if ( !SMESH_Hypothesis::IsStatusFatal( ret ))
904 // ret should be fatal: anHyp was not added
905 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
907 else if (!_father->IsUsedHypothesis( anHyp, this ))
908 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
910 if (SMESH_Hypothesis::IsStatusFatal( ret ))
912 MESSAGE("do not add extra hypothesis");
913 meshDS->RemoveHypothesis(_subShape, anHyp);
921 case ADD_ALGO: { //already existing algo : on father ?
923 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
924 // check if algo changes
926 f.Init( SMESH_HypoFilter::IsAlgo() );
927 f.And( SMESH_HypoFilter::IsApplicableTo( _subShape ));
928 f.AndNot( SMESH_HypoFilter::Is( algo ));
929 const SMESH_Hypothesis * prevAlgo = _father->GetHypothesis( this, f, true );
931 string( algo->GetName()) != prevAlgo->GetName())
933 oldAlgoState = NO_ALGO; // force setting event listener (#16648)
938 setAlgoState(MISSING_HYP);
944 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
945 setAlgoState(HYP_OK);
947 setAlgoState(MISSING_HYP);
951 case REMOVE_ALGO: { // perhaps a father algo applies ?
953 if (algo == NULL) // no more algo applying on sub-shape...
955 setAlgoState(NO_ALGO);
959 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
960 // check if algo remains
961 if ( anHyp != algo && strcmp( anHyp->GetName(), algo->GetName()) )
965 setAlgoState(MISSING_HYP);
969 case MODIF_HYP: // hypothesis value may become bad
970 case ADD_FATHER_HYP: { // new father hypothesis ?
973 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
975 if (_father->IsUsedHypothesis( anHyp, this )) // new Hyp
979 setAlgoState(MISSING_HYP);
982 case ADD_FATHER_ALGO: {
984 if ( algo == anHyp ) { // a new algo on father
985 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
986 // check if algo changes
988 f.Init( SMESH_HypoFilter::IsAlgo() );
989 f.And( SMESH_HypoFilter::IsApplicableTo( _subShape ));
990 f.AndNot( SMESH_HypoFilter::Is( algo ));
991 const SMESH_Hypothesis* prevAlgo = _father->GetHypothesis( this, f, true );
993 string(algo->GetName()) != string(prevAlgo->GetName()) )
997 setAlgoState(MISSING_HYP);
1001 case REMOVE_FATHER_HYP: {
1004 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
1005 // is there the same local hyp or maybe a new father algo applied?
1006 if ( !getSimilarAttached( _subShape, anHyp ) )
1010 setAlgoState(MISSING_HYP);
1013 case REMOVE_FATHER_ALGO: {
1014 // IPAL21346. Edges not removed when Netgen 1d-2d is removed from a SOLID.
1015 // CLEAN was not called at event REMOVE_ALGO because the algo is not applicable to SOLID.
1016 algo = dynamic_cast<SMESH_Algo*> (anHyp);
1017 if (!algo->NeedDiscreteBoundary())
1018 algoRequiringCleaning = algo;
1020 if (algo == NULL) // no more applying algo on father
1022 setAlgoState(NO_ALGO);
1026 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
1027 // check if algo changes
1028 if ( string(algo->GetName()) != string( anHyp->GetName()) )
1032 setAlgoState(MISSING_HYP);
1042 // ----------------------------------------------------------------------
1049 // detect algorithm hiding
1051 if ( ret == SMESH_Hypothesis::HYP_OK &&
1052 ( event == ADD_ALGO || event == ADD_FATHER_ALGO ) && algo &&
1053 algo->GetName() == anHyp->GetName() )
1056 SMESH_Gen* gen = _father->GetGen();
1057 const std::vector< SMESH_subMesh * > & ancestors = GetAncestors();
1058 for ( size_t iA = 0; ( ret == SMESH_Hypothesis::HYP_OK && iA < ancestors.size()); ++iA ) {
1059 if ( SMESH_Algo* upperAlgo = ancestors[ iA ]->GetAlgo() )
1060 if ( !upperAlgo->NeedDiscreteBoundary() && !upperAlgo->SupportSubmeshes())
1061 ret = SMESH_Hypothesis::HYP_HIDDEN_ALGO;
1064 if ( ret == SMESH_Hypothesis::HYP_OK &&
1065 !algo->NeedDiscreteBoundary() &&
1066 !algo->SupportSubmeshes())
1068 TopoDS_Shape algoAssignedTo, otherAssignedTo;
1069 gen->GetAlgo( this, &algoAssignedTo );
1070 map<int, SMESH_subMesh*>::reverse_iterator i_sm = _mapDepend.rbegin();
1071 for ( ; ( ret == SMESH_Hypothesis::HYP_OK && i_sm != _mapDepend.rend()) ; ++i_sm )
1072 if ( gen->GetAlgo( i_sm->second, &otherAssignedTo ) &&
1073 SMESH_MesherHelper::IsSubShape( /*sub=*/otherAssignedTo, /*main=*/algoAssignedTo ))
1074 ret = SMESH_Hypothesis::HYP_HIDING_ALGO;
1078 if ( _algo ) { // get an error description set by _algo->CheckHypothesis()
1079 _computeError = _algo->GetComputeError();
1080 _algo->InitComputeError();
1083 bool stateChange = ( _algoState != oldAlgoState );
1085 if ( stateChange && _algoState == HYP_OK ) // hyp becomes OK
1086 algo->SetEventListener( this );
1088 if ( event == REMOVE_ALGO || event == REMOVE_FATHER_ALGO )
1091 notifyListenersOnEvent( event, ALGO_EVENT, anHyp );
1093 if ( stateChange && oldAlgoState == HYP_OK ) { // hyp becomes KO
1094 deleteOwnListeners();
1095 SetIsAlwaysComputed( false );
1096 if (_subShape.ShapeType() == TopAbs_VERTEX ) {
1097 // restore default states
1098 _algoState = HYP_OK;
1099 _computeState = READY_TO_COMPUTE;
1103 if ( algoRequiringCleaning ) {
1104 // added or removed algo is all-dimensional
1105 ComputeStateEngine( CLEAN );
1106 cleanDependsOn( algoRequiringCleaning );
1107 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1110 if ( stateChange || modifiedHyp )
1111 ComputeStateEngine( MODIF_ALGO_STATE );
1113 _realComputeCost = ( _algoState == HYP_OK ) ? computeCost() : 0;
1118 //=======================================================================
1119 //function : IsConform
1120 //purpose : check if a conform mesh will be produced by the Algo
1121 //=======================================================================
1123 bool SMESH_subMesh::IsConform(const SMESH_Algo* theAlgo)
1125 // MESSAGE( "SMESH_subMesh::IsConform" );
1126 if ( !theAlgo ) return false;
1128 // Suppose that theAlgo is applicable to _subShape, do not check it here
1129 //if ( !IsApplicableHypothesis( theAlgo )) return false;
1131 // check only algo that doesn't NeedDiscreteBoundary(): because mesh made
1132 // on a sub-shape will be ignored by theAlgo
1133 if ( theAlgo->NeedDiscreteBoundary() ||
1134 !theAlgo->OnlyUnaryInput() ) // all adjacent shapes will be meshed by this algo?
1137 // only local algo is to be checked
1138 //if ( gen->IsGlobalHypothesis( theAlgo, *_father ))
1139 if ( _subShape.ShapeType() == _father->GetMeshDS()->ShapeToMesh().ShapeType() )
1142 // check algo attached to adjacent shapes
1144 // loop on one level down sub-meshes
1145 TopoDS_Iterator itsub( _subShape );
1146 for (; itsub.More(); itsub.Next())
1148 // loop on adjacent subShapes
1149 const std::vector< SMESH_subMesh * > & ancestors = GetAncestors();
1150 for ( size_t iA = 0; iA < ancestors.size(); ++iA )
1152 const TopoDS_Shape& adjacent = ancestors[ iA ]->GetSubShape();
1153 if ( _subShape.IsSame( adjacent )) continue;
1154 if ( adjacent.ShapeType() != _subShape.ShapeType())
1157 // check algo attached to smAdjacent
1158 SMESH_Algo * algo = ancestors[ iA ]->GetAlgo();
1160 !algo->NeedDiscreteBoundary() &&
1161 algo->OnlyUnaryInput())
1162 return false; // NOT CONFORM MESH WILL BE PRODUCED
1169 //=============================================================================
1173 //=============================================================================
1175 void SMESH_subMesh::setAlgoState(algo_state state)
1180 //================================================================================
1182 * \brief Send an event to sub-meshes
1183 * \param [in] event - the event
1184 * \param [in] anHyp - an hypothesis
1185 * \param [in] exitOnFatal - to stop iteration on sub-meshes if a sub-mesh
1186 * reports a fatal result
1187 * \return SMESH_Hypothesis::Hypothesis_Status - the worst result
1189 * Optional description of a problematic situation (if any) can be retrieved
1190 * via GetComputeError().
1192 //================================================================================
1194 SMESH_Hypothesis::Hypothesis_Status
1195 SMESH_subMesh::SubMeshesAlgoStateEngine(algo_event event,
1196 SMESH_Hypothesis * anHyp,
1199 SMESH_Hypothesis::Hypothesis_Status ret = SMESH_Hypothesis::HYP_OK;
1200 //EAP: a wire (dim==1) should notify edges (dim==1)
1201 //EAP: int dim = SMESH_Gen::GetShapeDim(_subShape);
1202 //if (_subShape.ShapeType() < TopAbs_EDGE ) // wire,face etc
1204 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1205 while ( smIt->more() ) {
1206 SMESH_subMesh* sm = smIt->next();
1207 SMESH_Hypothesis::Hypothesis_Status ret2 = sm->AlgoStateEngine(event, anHyp);
1211 _computeError = sm->_computeError;
1212 sm->_computeError.reset();
1213 if ( exitOnFatal && SMESH_Hypothesis::IsStatusFatal( ret ))
1221 //================================================================================
1223 * \brief Remove elements from sub-meshes.
1224 * \param algoRequiringCleaning - an all-dimensional algorithm whose presence
1225 * causes the cleaning.
1227 //================================================================================
1229 void SMESH_subMesh::cleanDependsOn( SMESH_Algo* algoRequiringCleaning/*=0*/ )
1231 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,
1232 /*complexShapeFirst=*/true);
1233 if ( _father->NbNodes() == 0 )
1235 while ( smIt->more() )
1236 smIt->next()->ComputeStateEngine(CHECK_COMPUTE_STATE);
1238 else if ( !algoRequiringCleaning || !algoRequiringCleaning->SupportSubmeshes() )
1240 while ( smIt->more() )
1241 smIt->next()->ComputeStateEngine(CLEAN);
1243 else if ( algoRequiringCleaning && algoRequiringCleaning->SupportSubmeshes() )
1245 // find sub-meshes to keep elements on
1246 set< SMESH_subMesh* > smToKeep;
1247 TopAbs_ShapeEnum prevShapeType = TopAbs_SHAPE;
1248 bool toKeepPrevShapeType = false;
1249 while ( smIt->more() )
1251 SMESH_subMesh* sm = smIt->next();
1252 sm->ComputeStateEngine(CHECK_COMPUTE_STATE);
1253 if ( !sm->IsEmpty() )
1255 const bool sameShapeType = ( prevShapeType == sm->GetSubShape().ShapeType() );
1256 bool keepSubMeshes = ( sameShapeType && toKeepPrevShapeType );
1257 if ( !sameShapeType )
1259 // check if the algo allows presence of global algos of dimension the algo
1260 // can generate it-self;
1261 // always keep a node on VERTEX, as this node can be shared by segments
1262 // lying on EDGEs not shared by the VERTEX of sm, due to MergeNodes (PAL23068)
1263 int shapeDim = SMESH_Gen::GetShapeDim( sm->GetSubShape() );
1264 keepSubMeshes = ( algoRequiringCleaning->NeedLowerHyps( shapeDim ) || shapeDim == 0 );
1265 prevShapeType = sm->GetSubShape().ShapeType();
1266 toKeepPrevShapeType = keepSubMeshes;
1268 if ( !keepSubMeshes )
1270 // look for a local algo used to mesh sm
1271 TopoDS_Shape algoShape = SMESH_MesherHelper::GetShapeOfHypothesis
1272 ( algoRequiringCleaning, _subShape, _father );
1273 SMESH_HypoFilter moreLocalAlgo;
1274 moreLocalAlgo.Init( SMESH_HypoFilter::IsMoreLocalThan( algoShape, *_father ));
1275 moreLocalAlgo.And ( SMESH_HypoFilter::IsAlgo() );
1276 bool localAlgoFound = _father->GetHypothesis( sm->_subShape, moreLocalAlgo, true );
1277 keepSubMeshes = localAlgoFound;
1279 // remember all sub-meshes of sm
1280 if ( keepSubMeshes )
1282 SMESH_subMeshIteratorPtr smIt2 = sm->getDependsOnIterator(true);
1283 while ( smIt2->more() )
1284 smToKeep.insert( smIt2->next() );
1289 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,true);
1290 while ( smIt->more() )
1292 SMESH_subMesh* sm = smIt->next();
1293 if ( !smToKeep.count( sm ))
1294 sm->ComputeStateEngine(CLEAN);
1299 //=============================================================================
1303 //=============================================================================
1305 void SMESH_subMesh::DumpAlgoState(bool isMain)
1309 const map < int, SMESH_subMesh * >&subMeshes = DependsOn();
1311 map < int, SMESH_subMesh * >::const_iterator itsub;
1312 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
1314 SMESH_subMesh *sm = (*itsub).second;
1315 sm->DumpAlgoState(false);
1318 MESSAGE("dim = " << SMESH_Gen::GetShapeDim(_subShape) <<
1319 " type of shape " << _subShape.ShapeType());
1322 case NO_ALGO : MESSAGE(" AlgoState = NO_ALGO"); break;
1323 case MISSING_HYP : MESSAGE(" AlgoState = MISSING_HYP"); break;
1324 case HYP_OK : MESSAGE(" AlgoState = HYP_OK");break;
1326 switch (_computeState)
1328 case NOT_READY : MESSAGE(" ComputeState = NOT_READY");break;
1329 case READY_TO_COMPUTE : MESSAGE(" ComputeState = READY_TO_COMPUTE");break;
1330 case COMPUTE_OK : MESSAGE(" ComputeState = COMPUTE_OK");break;
1331 case FAILED_TO_COMPUTE: MESSAGE(" ComputeState = FAILED_TO_COMPUTE");break;
1335 //================================================================================
1337 * \brief Remove nodes and elements bound to submesh
1338 * \param subMesh - submesh containing nodes and elements
1340 //================================================================================
1342 static void cleanSubMesh( SMESH_subMesh * subMesh )
1345 if (SMESHDS_SubMesh * subMeshDS = subMesh->GetSubMeshDS())
1347 SMESHDS_Mesh * meshDS = subMesh->GetFather()->GetMeshDS();
1348 smIdType nbElems = subMeshDS->NbElements();
1350 for ( SMDS_ElemIteratorPtr ite = subMeshDS->GetElements(); ite->more(); )
1351 meshDS->RemoveFreeElement( ite->next(), subMeshDS );
1353 smIdType nbNodes = subMeshDS->NbNodes();
1355 for ( SMDS_NodeIteratorPtr itn = subMeshDS->GetNodes(); itn->more() ; )
1357 const SMDS_MeshNode * node = itn->next();
1358 if ( node->NbInverseElements() == 0 )
1359 meshDS->RemoveFreeNode( node, subMeshDS );
1360 else // for StdMeshers_CompositeSegment_1D: node in one submesh, edge in another
1361 meshDS->RemoveNode( node );
1368 //=============================================================================
1372 //=============================================================================
1374 bool SMESH_subMesh::ComputeStateEngine(compute_event event)
1377 case MODIF_ALGO_STATE:
1379 case COMPUTE_SUBMESH:
1380 //case COMPUTE_CANCELED:
1382 //case SUBMESH_COMPUTED:
1383 //case SUBMESH_RESTORED:
1384 //case SUBMESH_LOADED:
1385 //case MESH_ENTITY_REMOVED:
1386 //case CHECK_COMPUTE_STATE:
1387 _computeError.reset(); break;
1391 if ( event == CLEAN )
1392 _alwaysComputed = false; // Unset 'true' set by MergeNodes() (issue 0022182)
1394 if (_subShape.ShapeType() == TopAbs_VERTEX)
1396 _computeState = READY_TO_COMPUTE;
1397 SMESHDS_SubMesh* smDS = GetSubMeshDS();
1398 if ( smDS && smDS->NbNodes() )
1400 if ( event == CLEAN ) {
1402 cleanSubMesh( this );
1405 _computeState = COMPUTE_OK;
1407 else if (( event == COMPUTE || event == COMPUTE_SUBMESH )
1408 && !_alwaysComputed )
1410 // LOCK: Adding node to mesh
1412 const TopoDS_Vertex & V = TopoDS::Vertex( _subShape );
1413 gp_Pnt P = BRep_Tool::Pnt(V);
1414 if ( SMDS_MeshNode * n = _father->GetMeshDS()->AddNode(P.X(), P.Y(), P.Z()) ) {
1415 _father->GetMeshDS()->SetNodeOnVertex(n,_Id);
1416 _computeState = COMPUTE_OK;
1421 if ( event == MODIF_ALGO_STATE )
1425 SMESH_Gen *gen = _father->GetGen();
1426 SMESH_Algo *algo = 0;
1428 SMESH_Hypothesis::Hypothesis_Status hyp_status;
1429 //algo_state oldAlgoState = (algo_state) GetAlgoState();
1431 switch (_computeState)
1434 // ----------------------------------------------------------------------
1439 case MODIF_ALGO_STATE:
1441 if (algo && !algo->NeedDiscreteBoundary())
1442 cleanDependsOn( algo ); // clean sub-meshes with event CLEAN
1443 if ( _algoState == HYP_OK )
1444 _computeState = READY_TO_COMPUTE;
1446 case COMPUTE: // nothing to do
1447 case COMPUTE_SUBMESH:
1449 case COMPUTE_CANCELED: // nothing to do
1453 removeSubMeshElementsAndNodes();
1455 case SUBMESH_COMPUTED: // nothing to do
1457 case SUBMESH_RESTORED:
1458 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1460 case MESH_ENTITY_REMOVED:
1462 case SUBMESH_LOADED:
1463 loadDependentMeshes();
1464 ComputeSubMeshStateEngine( SUBMESH_LOADED );
1467 case CHECK_COMPUTE_STATE:
1468 if ( IsMeshComputed() )
1469 _computeState = COMPUTE_OK;
1477 // ----------------------------------------------------------------------
1479 case READY_TO_COMPUTE:
1482 case MODIF_ALGO_STATE:
1483 _computeState = NOT_READY;
1487 if (!algo->NeedDiscreteBoundary())
1488 cleanDependsOn( algo ); // clean sub-meshes with event CLEAN
1489 if ( _algoState == HYP_OK )
1490 _computeState = READY_TO_COMPUTE;
1494 case COMPUTE_NOGEOM: // no geometry; can be several algos
1495 if ( !_father->HasShapeToMesh() )
1497 algo = GetAlgo(); // current algo
1500 // apply algos in the order of increasing dimension
1501 std::list< const SMESHDS_Hypothesis * > algos = _father->GetHypothesisList( _subShape );
1502 for ( int t = SMESHDS_Hypothesis::ALGO_1D; t <= SMESHDS_Hypothesis::ALGO_3D; ++t )
1504 std::list<const SMESHDS_Hypothesis *>::iterator al = algos.begin();
1505 for ( ; al != algos.end(); ++al )
1506 if ( (*al)->GetType() == t )
1508 _algo = (SMESH_Algo*) *al;
1509 _computeState = READY_TO_COMPUTE;
1510 if ( !ComputeStateEngine( COMPUTE ))
1514 _algo = algo; // restore
1520 case COMPUTE_SUBMESH:
1523 SMESH_Algo* algo2 = CopyAlgo();
1524 cout << "Algo2" << algo2;
1527 //ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1528 //_father->Unlock();
1531 MESSAGE("***** verify compute state *****");
1532 _computeState = NOT_READY;
1533 setAlgoState(MISSING_HYP);
1536 TopoDS_Shape shape = _subShape;
1537 algo->SubMeshesToCompute().assign( 1, this );
1538 // check submeshes needed
1539 // Forcing to false for parallel run
1540 // TODO: Remove forced false
1541 if (_father->HasShapeToMesh() && false) {
1542 bool subComputed = false, subFailed = false;
1543 if (!algo->OnlyUnaryInput()) {
1544 // --- commented for bos#22320 to compute all sub-shapes at once if possible;
1545 // --- in case COMPUTE_SUBMESH, set of sub-shapes is limited
1546 // --- by calling SetAllowedSubShapes()
1547 // if ( event == COMPUTE )
1548 // shape = getCollection( gen, algo, subComputed, subFailed, algo->SubMeshesToComput;
1550 // subComputed = SubMeshesComputed( & subFailed );
1551 shape = getCollection( gen, algo, subComputed, subFailed, algo->SubMeshesToCompute());
1554 subComputed = SubMeshesComputed();
1556 ret = ( algo->NeedDiscreteBoundary() ? subComputed :
1557 algo->SupportSubmeshes() ? !subFailed :
1558 ( !subComputed || _father->IsNotConformAllowed() ));
1561 _computeState = FAILED_TO_COMPUTE;
1562 if ( !algo->NeedDiscreteBoundary() && !subFailed )
1564 SMESH_ComputeError::New(COMPERR_BAD_INPUT_MESH,
1565 "Unexpected computed sub-mesh",algo);
1571 // to restore cout that may be redirected by algo
1572 std::streambuf* coutBuffer = std::cout.rdbuf();
1574 //cleanDependants(); for "UseExisting_*D" algos
1575 //removeSubMeshElementsAndNodes();
1576 loadDependentMeshes();
1578 _computeState = FAILED_TO_COMPUTE;
1579 _computeError = SMESH_ComputeError::New(COMPERR_OK,"",algo);
1583 algo->InitComputeError();
1585 MemoryReserve aMemoryReserve;
1586 SMDS_Mesh::CheckMemory();
1587 Kernel_Utils::Localizer loc;
1588 if ( !_father->HasShapeToMesh() ) // no shape
1590 SMESH_MesherHelper helper( *_father );
1591 helper.SetSubShape( shape );
1592 helper.SetElementsOnShape( true );
1593 ret = algo->Compute(*_father, &helper );
1597 ret = algo->Compute((*_father), shape);
1599 // algo can set _computeError of submesh
1600 _computeError = SMESH_ComputeError::Worst( _computeError, algo->GetComputeError() );
1602 catch ( ::SMESH_ComputeError& comperr ) {
1603 cout << " SMESH_ComputeError caught" << endl;
1604 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1605 *_computeError = comperr;
1607 catch ( std::bad_alloc& exc ) {
1608 MESSAGE("std::bad_alloc thrown inside algo->Compute()");
1609 if ( _computeError ) {
1610 _computeError->myName = COMPERR_MEMORY_PB;
1612 cleanSubMesh( this );
1615 catch ( Standard_OutOfMemory& exc ) {
1616 MESSAGE("Standard_OutOfMemory thrown inside algo->Compute()");
1617 if ( _computeError ) {
1618 _computeError->myName = COMPERR_MEMORY_PB;
1620 cleanSubMesh( this );
1621 throw std::bad_alloc();
1623 catch (Standard_Failure& ex) {
1624 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1625 _computeError->myName = COMPERR_OCC_EXCEPTION;
1626 _computeError->myComment += ex.DynamicType()->Name();
1627 if ( ex.GetMessageString() && strlen( ex.GetMessageString() )) {
1628 _computeError->myComment += ": ";
1629 _computeError->myComment += ex.GetMessageString();
1632 catch ( SALOME_Exception& S_ex ) {
1633 const int skipSalomeShift = 7; /* to skip "Salome " of
1634 "Salome Exception" prefix returned
1635 by SALOME_Exception::what() */
1636 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1637 _computeError->myName = COMPERR_SLM_EXCEPTION;
1638 _computeError->myComment = S_ex.what() + skipSalomeShift;
1640 catch ( std::exception& exc ) {
1641 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1642 _computeError->myName = COMPERR_STD_EXCEPTION;
1643 _computeError->myComment = exc.what();
1646 if ( _computeError )
1647 _computeError->myName = COMPERR_EXCEPTION;
1651 std::cout.rdbuf( coutBuffer ); // restore cout that could be redirected by algo
1653 // check if an error reported on any sub-shape
1654 bool isComputeErrorSet = !checkComputeError( algo, ret, shape );
1655 if ( isComputeErrorSet )
1657 // check if anything was built
1658 TopExp_Explorer subS(shape, _subShape.ShapeType());
1661 for (; ret && subS.More(); subS.Next())
1662 if ( !_father->GetSubMesh( subS.Current() )->IsMeshComputed() &&
1663 ( _subShape.ShapeType() != TopAbs_EDGE ||
1664 !algo->isDegenerated( TopoDS::Edge( subS.Current() ))))
1667 #ifdef PRINT_WHO_COMPUTE_WHAT
1668 for (subS.ReInit(); subS.More(); subS.Next())
1671 const std::list <const SMESHDS_Hypothesis *> hyps =
1672 _algo->GetUsedHypothesis( *_father, _subShape );
1674 SMESH_Comment hypStr;
1675 if ( !hyps.empty() )
1677 hypStr << hyps.front()->GetName() << " ";
1678 ((SMESHDS_Hypothesis*)hyps.front())->SaveTo( hypStr.Stream() );
1681 cout << _father->GetSubMesh( subS.Current() )->GetId()
1682 << " " << ( ret ? "OK" : "FAIL" )
1683 << " " << _algo->GetName()
1684 << " " << hypStr << endl;
1687 // Set _computeError
1688 if ( !ret && !isComputeErrorSet )
1690 for ( subS.ReInit(); subS.More(); subS.Next() )
1692 SMESH_subMesh* sm = _father->GetSubMesh( subS.Current() );
1693 if ( !sm->IsMeshComputed() )
1695 if ( !sm->_computeError )
1696 sm->_computeError = SMESH_ComputeError::New();
1697 if ( sm->_computeError->IsOK() )
1698 sm->_computeError->myName = COMPERR_ALGO_FAILED;
1699 sm->_computeState = FAILED_TO_COMPUTE;
1700 sm->_computeError->myAlgo = algo;
1704 if ( ret && _computeError && _computeError->myName != COMPERR_WARNING )
1706 _computeError.reset();
1709 // transform errors into warnings if it is caused by mesh edition (imp 0023068)
1710 if (!ret && _father->GetIsModified() )
1712 for (subS.ReInit(); subS.More(); subS.Next())
1714 SMESH_subMesh* sm = _father->GetSubMesh( subS.Current() );
1715 if ( !sm->IsMeshComputed() && sm->_computeError )
1717 // check if there is a VERTEX w/o nodes
1718 // with READY_TO_COMPUTE state (after MergeNodes())
1719 SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(false,false);
1720 while ( smIt->more() )
1722 SMESH_subMesh * vertSM = smIt->next();
1723 if ( vertSM->_subShape.ShapeType() != TopAbs_VERTEX ) break;
1724 if ( vertSM->GetComputeState() == READY_TO_COMPUTE )
1726 SMESHDS_SubMesh * ds = vertSM->GetSubMeshDS();
1727 if ( !ds || ds->NbNodes() == 0 )
1729 sm->_computeState = READY_TO_COMPUTE;
1730 sm->_computeError->myName = COMPERR_WARNING;
1739 // send event SUBMESH_COMPUTED
1741 if ( !algo->NeedDiscreteBoundary() )
1742 // send SUBMESH_COMPUTED to dependants of all sub-meshes of shape
1743 for (subS.ReInit(); subS.More(); subS.Next())
1745 SMESH_subMesh* sm = _father->GetSubMesh( subS.Current() );
1746 SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(false,false);
1747 while ( smIt->more() ) {
1749 if ( sm->GetSubShape().ShapeType() == TopAbs_VERTEX )
1750 sm->updateDependantsState( SUBMESH_COMPUTED );
1756 updateDependantsState( SUBMESH_COMPUTED );
1758 // let algo clear its data gathered while algo->Compute()
1760 //algo->CheckHypothesis((*_father), _subShape, hyp_status);
1761 //_father->Unlock();
1764 case COMPUTE_CANCELED: // nothing to do
1768 removeSubMeshElementsAndNodes();
1769 _computeState = NOT_READY;
1773 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1775 _computeState = READY_TO_COMPUTE;
1777 setAlgoState(MISSING_HYP);
1780 case SUBMESH_COMPUTED: // nothing to do
1782 case SUBMESH_RESTORED:
1783 // check if a mesh is already computed that may
1784 // happen after retrieval from a file
1785 ComputeStateEngine( CHECK_COMPUTE_STATE );
1786 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1788 if (algo) algo->SubmeshRestored( this );
1790 case MESH_ENTITY_REMOVED:
1792 case SUBMESH_LOADED:
1793 loadDependentMeshes();
1794 ComputeSubMeshStateEngine( SUBMESH_LOADED );
1797 case CHECK_COMPUTE_STATE:
1798 if ( IsMeshComputed() )
1799 _computeState = COMPUTE_OK;
1800 else if ( _computeError && _computeError->IsKO() )
1801 _computeState = FAILED_TO_COMPUTE;
1809 // ----------------------------------------------------------------------
1814 case MODIF_ALGO_STATE:
1815 ComputeStateEngine( CLEAN );
1817 if (algo && !algo->NeedDiscreteBoundary())
1818 cleanDependsOn( algo ); // clean sub-meshes with event CLEAN
1820 case COMPUTE: // nothing to do
1822 case COMPUTE_SUBMESH: // nothing to do
1824 case COMPUTE_CANCELED: // nothing to do
1827 cleanDependants(); // clean sub-meshes, dependent on this one, with event CLEAN
1828 removeSubMeshElementsAndNodes();
1829 _computeState = NOT_READY;
1830 if ( _algoState == HYP_OK )
1831 _computeState = READY_TO_COMPUTE;
1833 case SUBMESH_COMPUTED: // nothing to do
1835 case SUBMESH_RESTORED:
1836 ComputeStateEngine( CHECK_COMPUTE_STATE );
1837 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1839 if (algo) algo->SubmeshRestored( this );
1841 case MESH_ENTITY_REMOVED:
1842 updateDependantsState ( CHECK_COMPUTE_STATE );
1843 ComputeStateEngine ( CHECK_COMPUTE_STATE );
1844 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1846 case CHECK_COMPUTE_STATE:
1847 if ( !IsMeshComputed() ) {
1848 if (_algoState == HYP_OK)
1849 _computeState = READY_TO_COMPUTE;
1851 _computeState = NOT_READY;
1854 case SUBMESH_LOADED:
1855 // already treated event, thanks to which _computeState == COMPUTE_OK
1863 // ----------------------------------------------------------------------
1865 case FAILED_TO_COMPUTE:
1868 case MODIF_ALGO_STATE:
1870 ComputeStateEngine( CLEAN );
1872 if (algo && !algo->NeedDiscreteBoundary())
1873 cleanDependsOn( algo ); // clean sub-meshes with event CLEAN
1874 if (_algoState == HYP_OK)
1875 _computeState = READY_TO_COMPUTE;
1877 _computeState = NOT_READY;
1879 case COMPUTE: // nothing to do
1880 case COMPUTE_SUBMESH:
1882 case COMPUTE_CANCELED:
1885 algo->CancelCompute();
1889 cleanDependants(); // submeshes dependent on me should be cleaned as well
1890 removeSubMeshElementsAndNodes();
1892 case SUBMESH_COMPUTED: // allow retry compute
1893 if ( IsEmpty() ) // 23061
1895 if (_algoState == HYP_OK)
1896 _computeState = READY_TO_COMPUTE;
1898 _computeState = NOT_READY;
1901 case SUBMESH_RESTORED:
1902 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1904 case MESH_ENTITY_REMOVED:
1906 case CHECK_COMPUTE_STATE:
1907 if ( IsMeshComputed() )
1908 _computeState = COMPUTE_OK;
1910 if (_algoState == HYP_OK)
1911 _computeState = READY_TO_COMPUTE;
1913 _computeState = NOT_READY;
1915 // case SUBMESH_LOADED:
1923 // ----------------------------------------------------------------------
1929 //notifyListenersOnEvent( event, COMPUTE_EVENT );
1935 //=============================================================================
1939 //=============================================================================
1941 bool SMESH_subMesh::Evaluate(MapShapeNbElems& aResMap)
1943 _computeError.reset();
1947 if (_subShape.ShapeType() == TopAbs_VERTEX) {
1948 vector<smIdType> aVec(SMDSEntity_Last,0);
1949 aVec[SMDSEntity_Node] = 1;
1950 aResMap.insert(make_pair(this,aVec));
1954 //SMESH_Gen *gen = _father->GetGen();
1955 SMESH_Algo *algo = 0;
1956 SMESH_Hypothesis::Hypothesis_Status hyp_status;
1959 if( algo && !aResMap.count( this ))
1961 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1962 if (!ret) return false;
1964 if (_father->HasShapeToMesh() && algo->NeedDiscreteBoundary() )
1966 // check submeshes needed
1967 bool subMeshEvaluated = true;
1968 int dimToCheck = SMESH_Gen::GetShapeDim( _subShape ) - 1;
1969 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,/*complexShapeFirst=*/true);
1970 while ( smIt->more() && subMeshEvaluated )
1972 SMESH_subMesh* sm = smIt->next();
1973 int dim = SMESH_Gen::GetShapeDim( sm->GetSubShape() );
1974 if (dim < dimToCheck) break; // the rest subMeshes are all of less dimension
1975 const vector<smIdType> & nbs = aResMap[ sm ];
1976 subMeshEvaluated = (std::accumulate( nbs.begin(), nbs.end(), 0 ) > 0 );
1978 if ( !subMeshEvaluated )
1981 _computeError = SMESH_ComputeError::New(COMPERR_OK,"",algo);
1983 if ( IsMeshComputed() )
1985 vector<smIdType> & nbEntities = aResMap[ this ];
1986 nbEntities.resize( SMDSEntity_Last, 0 );
1987 if ( SMESHDS_SubMesh* sm = GetSubMeshDS() )
1989 nbEntities[ SMDSEntity_Node ] = sm->NbNodes();
1990 SMDS_ElemIteratorPtr elemIt = sm->GetElements();
1991 while ( elemIt->more() )
1992 nbEntities[ elemIt->next()->GetEntityType() ]++;
1997 ret = algo->Evaluate((*_father), _subShape, aResMap);
1999 aResMap.insert( make_pair( this,vector<smIdType>(0)));
2006 //=======================================================================
2008 * \brief Update compute_state by _computeError and send proper events to
2009 * dependent submeshes
2010 * \retval bool - true if _computeError is NOT set
2012 //=======================================================================
2014 bool SMESH_subMesh::checkComputeError(SMESH_Algo* theAlgo,
2015 const bool theComputeOK,
2016 const TopoDS_Shape& theShape)
2018 bool noErrors = true;
2020 if ( !theShape.IsNull() )
2022 // Check state of submeshes
2023 if ( !theAlgo->NeedDiscreteBoundary())
2025 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
2026 while ( smIt->more() )
2027 if ( !smIt->next()->checkComputeError( theAlgo, theComputeOK ))
2031 // Check state of neighbours
2032 if ( !theAlgo->OnlyUnaryInput() &&
2033 theShape.ShapeType() == TopAbs_COMPOUND &&
2034 !theShape.IsSame( _subShape ))
2036 for (TopoDS_Iterator subIt( theShape ); subIt.More(); subIt.Next()) {
2037 SMESH_subMesh* sm = _father->GetSubMesh( subIt.Value() );
2039 if ( !sm->checkComputeError( theAlgo, theComputeOK, sm->GetSubShape() ))
2041 updateDependantsState( SUBMESH_COMPUTED ); // send event SUBMESH_COMPUTED
2048 // Set my _computeState
2050 if ( !_computeError || _computeError->IsOK() )
2052 // no error description is set to this sub-mesh, check if any mesh is computed
2053 _computeState = IsMeshComputed() ? COMPUTE_OK : FAILED_TO_COMPUTE;
2054 if ( _computeState != COMPUTE_OK )
2056 if ( _subShape.ShapeType() == TopAbs_EDGE &&
2057 SMESH_Algo::isDegenerated( TopoDS::Edge( _subShape )) )
2058 _computeState = COMPUTE_OK;
2059 else if ( theComputeOK )
2060 _computeError = SMESH_ComputeError::New(COMPERR_NO_MESH_ON_SHAPE,"",theAlgo);
2064 if ( _computeError && !_computeError->IsOK() )
2066 if ( !_computeError->myAlgo )
2067 _computeError->myAlgo = theAlgo;
2071 text << theAlgo->GetName() << " failed on sub-shape #" << _Id << " with error ";
2072 if (_computeError->IsCommon() )
2073 text << _computeError->CommonName();
2075 text << _computeError->myName;
2076 if ( _computeError->myComment.size() > 0 )
2077 text << " \"" << _computeError->myComment << "\"";
2081 _computeState = _computeError->IsKO() ? FAILED_TO_COMPUTE : COMPUTE_OK;
2089 //=======================================================================
2090 //function : updateSubMeshState
2092 //=======================================================================
2094 void SMESH_subMesh::updateSubMeshState(const compute_state theState)
2096 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
2097 while ( smIt->more() )
2098 smIt->next()->_computeState = theState;
2101 //=======================================================================
2102 //function : ComputeSubMeshStateEngine
2104 //=======================================================================
2106 void SMESH_subMesh::ComputeSubMeshStateEngine(compute_event event, const bool includeSelf)
2108 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(includeSelf,false);
2109 while ( smIt->more() )
2110 smIt->next()->ComputeStateEngine(event);
2113 //=======================================================================
2114 //function : updateDependantsState
2116 //=======================================================================
2118 void SMESH_subMesh::updateDependantsState(const compute_event theEvent)
2120 const std::vector< SMESH_subMesh * > & ancestors = GetAncestors();
2121 for ( size_t iA = 0; iA < ancestors.size(); ++iA )
2123 ancestors[ iA ]->ComputeStateEngine( theEvent );
2127 //=======================================================================
2128 //function : cleanDependants
2130 //=======================================================================
2132 void SMESH_subMesh::cleanDependants()
2134 int dimToClean = SMESH_Gen::GetShapeDim( _subShape ) + 1;
2136 const std::vector< SMESH_subMesh * > & ancestors = GetAncestors();
2137 for ( size_t iA = 0; iA < ancestors.size(); ++iA )
2139 const TopoDS_Shape& ancestor = ancestors[ iA ]->GetSubShape();
2140 if ( SMESH_Gen::GetShapeDim( ancestor ) == dimToClean )
2142 // PAL8021. do not go upper than SOLID, else ComputeStateEngine(CLEAN)
2143 // will erase mesh on other shapes in a compound
2144 if ( ancestor.ShapeType() >= TopAbs_SOLID &&
2145 !ancestors[ iA ]->IsEmpty() ) // prevent infinite CLEAN via event lesteners
2146 ancestors[ iA ]->ComputeStateEngine(CLEAN);
2151 //=======================================================================
2152 //function : removeSubMeshElementsAndNodes
2154 //=======================================================================
2156 void SMESH_subMesh::removeSubMeshElementsAndNodes()
2158 cleanSubMesh( this );
2160 // algo may bind a submesh not to _subShape, eg 3D algo
2161 // sets nodes on SHELL while _subShape may be SOLID
2163 int dim = SMESH_Gen::GetShapeDim( _subShape );
2164 int type = _subShape.ShapeType() + 1;
2165 for ( ; type <= TopAbs_EDGE; type++) {
2166 if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
2168 TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
2169 for ( ; exp.More(); exp.Next() )
2170 cleanSubMesh( _father->GetSubMeshContaining( exp.Current() ));
2177 //=======================================================================
2178 //function : getCollection
2179 //purpose : return a shape containing all sub-shapes of the MainShape that can be
2180 // meshed at once along with _subShape
2181 //=======================================================================
2183 TopoDS_Shape SMESH_subMesh::getCollection(SMESH_Gen * /*theGen*/,
2184 SMESH_Algo* theAlgo,
2185 bool & theSubComputed,
2186 bool & theSubFailed,
2187 std::vector<SMESH_subMesh*>& theSubs)
2189 theSubComputed = SubMeshesComputed( & theSubFailed );
2191 TopoDS_Shape mainShape = _father->GetMeshDS()->ShapeToMesh();
2193 if ( mainShape.IsSame( _subShape ))
2196 const bool skipAuxHyps = false;
2198 list<const SMESHDS_Hypothesis*> usedHyps =
2199 theAlgo->GetUsedHypothesis( *_father, _subShape, skipAuxHyps ); // copy
2201 std::list < TopoDS_Shape > assiShapes = theAlgo->GetAssignedShapes();
2203 // put in a compound all shapes with the same hypothesis assigned
2204 // and a good ComputeState
2206 TopoDS_Compound aCompound;
2207 BRep_Builder aBuilder;
2208 aBuilder.MakeCompound( aCompound );
2212 SMESH_subMeshIteratorPtr smIt = _father->GetSubMesh( mainShape )->getDependsOnIterator(false);
2213 while ( smIt->more() )
2215 SMESH_subMesh* subMesh = smIt->next();
2216 const TopoDS_Shape& S = subMesh->_subShape;
2217 if ( S.ShapeType() != this->_subShape.ShapeType() )
2219 if ( _allowedSubShapes && !_allowedSubShapes->IsEmpty() && !_allowedSubShapes->Contains( S ))
2221 if ( subMesh == this )
2223 aBuilder.Add( aCompound, S );
2224 theSubs.push_back( subMesh );
2226 else if ( subMesh->GetComputeState() == READY_TO_COMPUTE )
2229 SMESH_Algo* anAlgo = subMesh->GetAlgo();
2230 if (( anAlgo->IsSameName( *theAlgo )) && // same algo
2231 ( anAlgo->GetUsedHypothesis( *_father, S, skipAuxHyps ) == usedHyps ) && // same hyps
2232 ( anAlgo->GetAssignedShapes() == assiShapes ) && // on same sub-shapes
2233 ( _algoShape == subMesh->_algoShape ))
2235 aBuilder.Add( aCompound, S );
2236 if ( !subMesh->SubMeshesComputed() )
2237 theSubComputed = false;
2238 theSubs.push_back( subMesh );
2244 return theSubs.size() == 1 ? theSubs[0]->GetSubShape() : aCompound;
2247 //=======================================================================
2248 //function : getSimilarAttached
2249 //purpose : return a hypothesis attached to theShape.
2250 // If theHyp is provided, similar but not same hypotheses
2251 // is returned; else only applicable ones having theHypType
2253 //=======================================================================
2255 const SMESH_Hypothesis* SMESH_subMesh::getSimilarAttached(const TopoDS_Shape& theShape,
2256 const SMESH_Hypothesis * theHyp,
2257 const int theHypType)
2259 SMESH_HypoFilter hypoKind;
2260 hypoKind.Init( hypoKind.HasType( theHyp ? theHyp->GetType() : theHypType ));
2262 hypoKind.And ( hypoKind.HasDim( theHyp->GetDim() ));
2263 hypoKind.AndNot( hypoKind.Is( theHyp ));
2264 if ( theHyp->IsAuxiliary() )
2265 hypoKind.And( hypoKind.HasName( theHyp->GetName() ));
2267 hypoKind.AndNot( hypoKind.IsAuxiliary());
2270 hypoKind.And( hypoKind.IsApplicableTo( theShape ));
2273 return _father->GetHypothesis( theShape, hypoKind, false );
2276 //=======================================================================
2277 //function : CheckConcurrentHypothesis
2278 //purpose : check if there are several applicable hypothesis attached to
2280 //=======================================================================
2282 SMESH_Hypothesis::Hypothesis_Status
2283 SMESH_subMesh::CheckConcurrentHypothesis (const int theHypType)
2285 // is there local hypothesis on me?
2286 if ( getSimilarAttached( _subShape, 0, theHypType ) )
2287 return SMESH_Hypothesis::HYP_OK;
2290 TopoDS_Shape aPrevWithHyp;
2291 const SMESH_Hypothesis* aPrevHyp = 0;
2292 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
2293 for (; it.More(); it.Next())
2295 const TopoDS_Shape& ancestor = it.Value();
2296 const SMESH_Hypothesis* hyp = getSimilarAttached( ancestor, 0, theHypType );
2299 if ( aPrevWithHyp.IsNull() || aPrevWithHyp.IsSame( ancestor ))
2301 aPrevWithHyp = ancestor;
2304 else if ( aPrevWithHyp.ShapeType() == ancestor.ShapeType() && aPrevHyp != hyp )
2305 return SMESH_Hypothesis::HYP_CONCURRENT;
2307 return SMESH_Hypothesis::HYP_OK;
2310 return SMESH_Hypothesis::HYP_OK;
2313 //================================================================================
2315 * \brief Constructor of OwnListenerData
2317 //================================================================================
2319 SMESH_subMesh::OwnListenerData::OwnListenerData( SMESH_subMesh* sm, EventListener* el):
2321 myMeshID( sm ? sm->GetFather()->GetId() : -1 ),
2322 mySubMeshID( sm ? sm->GetId() : -1 ),
2327 //================================================================================
2329 * \brief Sets an event listener and its data to a submesh
2330 * \param listener - the listener to store
2331 * \param data - the listener data to store
2332 * \param where - the submesh to store the listener and it's data
2334 * It remembers the submesh where it puts the listener in order to delete
2335 * them when HYP_OK algo_state is lost
2336 * After being set, event listener is notified on each event of where submesh.
2338 //================================================================================
2340 void SMESH_subMesh::SetEventListener(EventListener* listener,
2341 EventListenerData* data,
2342 SMESH_subMesh* where)
2344 if ( listener && where ) {
2345 where->setEventListener( listener, data );
2346 _ownListeners.push_back( OwnListenerData( where, listener ));
2350 //================================================================================
2352 * \brief Sets an event listener and its data to a submesh
2353 * \param listener - the listener to store
2354 * \param data - the listener data to store
2356 * After being set, event listener is notified on each event of a submesh.
2358 //================================================================================
2360 void SMESH_subMesh::setEventListener(EventListener* listener,
2361 EventListenerData* data)
2363 map< EventListener*, EventListenerData* >::iterator l_d =
2364 _eventListeners.find( listener );
2365 if ( l_d != _eventListeners.end() ) {
2366 EventListenerData* curData = l_d->second;
2368 if ( curData && curData != data && curData->IsDeletable() )
2373 for ( l_d = _eventListeners.begin(); l_d != _eventListeners.end(); ++l_d )
2374 if ( listener->GetName() == l_d->first->GetName() )
2376 EventListenerData* curData = l_d->second;
2378 if ( curData && curData != data && curData->IsDeletable() )
2380 if ( l_d->first != listener && l_d->first->IsDeletable() )
2382 _eventListeners.erase( l_d );
2385 _eventListeners.insert( make_pair( listener, data ));
2389 //================================================================================
2391 * \brief Return an event listener data
2392 * \param listener - the listener whose data is
2393 * \param myOwn - if \c true, returns a listener set by this sub-mesh,
2394 * else returns a listener listening to events of this sub-mesh
2395 * \retval EventListenerData* - found data, maybe NULL
2397 //================================================================================
2399 EventListenerData* SMESH_subMesh::GetEventListenerData(EventListener* listener,
2400 const bool myOwn) const
2404 list< OwnListenerData >::const_iterator d;
2405 for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
2407 if ( d->myListener == listener && _father->MeshExists( d->myMeshID ))
2408 return d->mySubMesh->GetEventListenerData( listener, !myOwn );
2413 map< EventListener*, EventListenerData* >::const_iterator l_d =
2414 _eventListeners.find( listener );
2415 if ( l_d != _eventListeners.end() )
2421 //================================================================================
2423 * \brief Return an event listener data
2424 * \param listenerName - the listener name
2425 * \param myOwn - if \c true, returns a listener set by this sub-mesh,
2426 * else returns a listener listening to events of this sub-mesh
2427 * \retval EventListenerData* - found data, maybe NULL
2429 //================================================================================
2431 EventListenerData* SMESH_subMesh::GetEventListenerData(const string& listenerName,
2432 const bool myOwn) const
2436 list< OwnListenerData >::const_iterator d;
2437 for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
2439 if ( _father->MeshExists( d->myMeshID ) && listenerName == d->myListener->GetName())
2440 return d->mySubMesh->GetEventListenerData( listenerName, !myOwn );
2445 map< EventListener*, EventListenerData* >::const_iterator l_d = _eventListeners.begin();
2446 for ( ; l_d != _eventListeners.end(); ++l_d )
2447 if ( listenerName == l_d->first->GetName() )
2453 //================================================================================
2455 * \brief Notify stored event listeners on the occurred event
2456 * \param event - algo_event or compute_event itself
2457 * \param eventType - algo_event or compute_event
2458 * \param hyp - hypothesis, if eventType is algo_event
2460 //================================================================================
2462 void SMESH_subMesh::notifyListenersOnEvent( const int event,
2463 const event_type eventType,
2464 SMESH_Hypothesis* hyp)
2466 list< pair< EventListener*, EventListenerData* > > eventListeners( _eventListeners.begin(),
2467 _eventListeners.end());
2468 list< pair< EventListener*, EventListenerData* > >::iterator l_d = eventListeners.begin();
2469 for ( ; l_d != eventListeners.end(); ++l_d )
2471 std::pair< EventListener*, EventListenerData* > li_da = *l_d;
2472 if ( !_eventListeners.count( li_da.first )) continue;
2474 if ( li_da.first->myBusySM.insert( this ).second )
2476 const bool isDeletable = li_da.first->IsDeletable();
2478 li_da.first->ProcessEvent( event, eventType, this, li_da.second, hyp );
2480 if ( !isDeletable || _eventListeners.count( li_da.first ))
2481 li_da.first->myBusySM.erase( this ); // a listener is hopefully not dead
2486 //================================================================================
2488 * \brief Unregister the listener and delete listener's data
2489 * \param listener - the event listener
2491 //================================================================================
2493 void SMESH_subMesh::DeleteEventListener(EventListener* listener)
2495 map< EventListener*, EventListenerData* >::iterator l_d =
2496 _eventListeners.find( listener );
2497 if ( l_d != _eventListeners.end() && l_d->first )
2499 if ( l_d->second && l_d->second->IsDeletable() )
2503 l_d->first->myBusySM.erase( this );
2504 if ( l_d->first->IsDeletable() )
2506 l_d->first->BeforeDelete( this, l_d->second );
2509 _eventListeners.erase( l_d );
2513 //================================================================================
2515 * \brief Delete event listeners depending on algo of this submesh
2517 //================================================================================
2519 void SMESH_subMesh::deleteOwnListeners()
2521 list< OwnListenerData >::iterator d;
2522 for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
2524 SMESH_Mesh* mesh = _father->FindMesh( d->myMeshID );
2525 if ( !mesh || !mesh->GetSubMeshContaining( d->mySubMeshID ))
2527 d->mySubMesh->DeleteEventListener( d->myListener );
2529 _ownListeners.clear();
2532 //=======================================================================
2533 //function : loadDependentMeshes
2534 //purpose : loads dependent meshes on SUBMESH_LOADED event
2535 //=======================================================================
2537 void SMESH_subMesh::loadDependentMeshes()
2539 list< OwnListenerData >::iterator d;
2540 for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
2541 if ( _father != d->mySubMesh->_father &&
2542 _father->FindMesh( d->myMeshID ))
2543 d->mySubMesh->_father->Load();
2545 // map< EventListener*, EventListenerData* >::iterator l_d = _eventListeners.begin();
2546 // for ( ; l_d != _eventListeners.end(); ++l_d )
2547 // if ( l_d->second )
2549 // const list<SMESH_subMesh*>& smList = l_d->second->mySubMeshes;
2550 // list<SMESH_subMesh*>::const_iterator sm = smList.begin();
2551 // for ( ; sm != smList.end(); ++sm )
2552 // if ( _father != (*sm)->_father )
2553 // (*sm)->_father->Load();
2557 //================================================================================
2559 * \brief Do something on a certain event
2560 * \param event - algo_event or compute_event itself
2561 * \param eventType - algo_event or compute_event
2562 * \param subMesh - the submesh where the event occurs
2563 * \param data - listener data stored in the subMesh
2564 * \param hyp - hypothesis, if eventType is algo_event
2566 * The base implementation translates CLEAN event to the subMesh
2567 * stored in listener data. Also it sends SUBMESH_COMPUTED event in case of
2568 * successful COMPUTE event.
2570 //================================================================================
2572 void SMESH_subMeshEventListener::ProcessEvent(const int event,
2573 const int eventType,
2574 SMESH_subMesh* subMesh,
2575 EventListenerData* data,
2576 const SMESH_Hypothesis* /*hyp*/)
2578 if ( data && !data->mySubMeshes.empty() &&
2579 eventType == SMESH_subMesh::COMPUTE_EVENT)
2581 ASSERT( data->mySubMeshes.front() != subMesh );
2582 list<SMESH_subMesh*>::iterator smIt = data->mySubMeshes.begin();
2583 list<SMESH_subMesh*>::iterator smEnd = data->mySubMeshes.end();
2585 case SMESH_subMesh::CLEAN:
2586 for ( ; smIt != smEnd; ++ smIt)
2587 (*smIt)->ComputeStateEngine( SMESH_subMesh::compute_event( event ));
2589 case SMESH_subMesh::COMPUTE:
2590 case SMESH_subMesh::COMPUTE_SUBMESH:
2591 if ( subMesh->GetComputeState() == SMESH_subMesh::COMPUTE_OK )
2592 for ( ; smIt != smEnd; ++ smIt)
2593 (*smIt)->ComputeStateEngine( SMESH_subMesh::SUBMESH_COMPUTED );
2602 //================================================================================
2604 * \brief Iterator over submeshes and optionally prepended or appended one
2606 //================================================================================
2608 struct _Iterator : public SMDS_Iterator<SMESH_subMesh*>
2610 _Iterator(SMDS_Iterator<SMESH_subMesh*>* subIt,
2611 SMESH_subMesh* prepend,
2612 SMESH_subMesh* append): myAppend(append), myIt(subIt)
2614 myCur = prepend ? prepend : myIt->more() ? myIt->next() : append;
2615 if ( myCur == append ) append = 0;
2617 /// Return true if and only if there are other object in this iterator
2622 /// Return the current object and step to the next one
2623 virtual SMESH_subMesh* next()
2625 SMESH_subMesh* res = myCur;
2626 if ( myIt->more() ) { myCur = myIt->next(); }
2627 else { myCur = myAppend; myAppend = 0; }
2634 SMESH_subMesh *myAppend, *myCur;
2635 SMDS_Iterator<SMESH_subMesh*> *myIt;
2639 //================================================================================
2641 * \brief Return iterator on the submeshes this one depends on
2642 * \param includeSelf - this submesh to be returned also
2643 * \param reverse - if true, complex shape submeshes go first
2645 //================================================================================
2647 SMESH_subMeshIteratorPtr SMESH_subMesh::getDependsOnIterator(const bool includeSelf,
2648 const bool reverse) const
2650 SMESH_subMesh *me = (SMESH_subMesh*) this;
2651 SMESH_subMesh *prepend=0, *append=0;
2652 if ( includeSelf ) {
2653 if ( reverse ) prepend = me;
2656 typedef map < int, SMESH_subMesh * > TMap;
2659 return SMESH_subMeshIteratorPtr
2660 ( new _Iterator( new SMDS_mapReverseIterator<TMap>( me->DependsOn() ), prepend, append ));
2663 return SMESH_subMeshIteratorPtr
2664 ( new _Iterator( new SMDS_mapIterator<TMap>( me->DependsOn() ), prepend, append ));
2668 //================================================================================
2670 * \brief Returns ancestor sub-meshes. Finds them if not yet found.
2672 //================================================================================
2674 const std::vector< SMESH_subMesh * > & SMESH_subMesh::GetAncestors() const
2676 if ( _ancestors.empty() &&
2677 !_subShape.IsSame( _father->GetShapeToMesh() ))
2679 const TopTools_ListOfShape& ancShapes = _father->GetAncestors( _subShape );
2681 SMESH_subMesh* me = const_cast< SMESH_subMesh* >( this );
2682 me->_ancestors.reserve( ancShapes.Extent() );
2684 // assure that all sub-meshes exist
2685 TopoDS_Shape mainShape = _father->GetShapeToMesh();
2686 if ( !mainShape.IsNull() )
2687 _father->GetSubMesh( mainShape )->DependsOn();
2689 TopTools_MapOfShape map;
2691 for ( TopTools_ListIteratorOfListOfShape it( ancShapes ); it.More(); it.Next() )
2692 if ( SMESH_subMesh* sm = _father->GetSubMeshContaining( it.Value() ))
2693 if ( map.Add( it.Value() ))
2694 me->_ancestors.push_back( sm );
2700 //================================================================================
2702 * \brief Clears the vector of ancestor sub-meshes
2704 //================================================================================
2706 void SMESH_subMesh::ClearAncestors()
2711 //================================================================================
2713 * \brief Find common submeshes (based on shared sub-shapes with other
2714 * \param theOther submesh to check
2715 * \param theSetOfCommon set of common submesh
2717 //================================================================================
2719 bool SMESH_subMesh::FindIntersection(const SMESH_subMesh* theOther,
2720 std::set<const SMESH_subMesh*>& theSetOfCommon ) const
2722 size_t oldNb = theSetOfCommon.size();
2724 // check main submeshes
2725 const map <int, SMESH_subMesh*>::const_iterator otherEnd = theOther->_mapDepend.end();
2726 if ( theOther->_mapDepend.find(this->GetId()) != otherEnd )
2727 theSetOfCommon.insert( this );
2728 if ( _mapDepend.find(theOther->GetId()) != _mapDepend.end() )
2729 theSetOfCommon.insert( theOther );
2731 // check common submeshes
2732 map <int, SMESH_subMesh*>::const_iterator mapIt = _mapDepend.begin();
2733 for( ; mapIt != _mapDepend.end(); mapIt++ )
2734 if ( theOther->_mapDepend.find((*mapIt).first) != otherEnd )
2735 theSetOfCommon.insert( (*mapIt).second );
2736 return oldNb < theSetOfCommon.size();