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 * Return true if all sub-meshes have been meshed
279 //=============================================================================
281 bool SMESH_subMesh::SubMeshesComputed(bool * isFailedToCompute/*=0*/) const
283 int myDim = SMESH_Gen::GetShapeDim( _subShape );
284 int dimToCheck = myDim - 1;
285 bool subMeshesComputed = true;
286 if ( isFailedToCompute ) *isFailedToCompute = false;
287 // check sub-meshes with upper dimension => reverse iteration
288 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,true);
289 while ( smIt->more() )
291 SMESH_subMesh *sm = smIt->next();
292 if ( sm->_alwaysComputed )
294 const TopoDS_Shape & ss = sm->GetSubShape();
296 // MSV 07.04.2006: restrict checking to myDim-1 only. Ex., there is no sense
297 // in checking of existence of edges if the algo needs only faces. Moreover,
298 // degenerated edges may have no sub-mesh, as after computing NETGEN_2D.
299 if ( !_algo || _algo->NeedDiscreteBoundary() ) {
300 int dim = SMESH_Gen::GetShapeDim( ss );
301 if (dim < dimToCheck)
302 break; // the rest sub-meshes are all of less dimension
304 SMESHDS_SubMesh * ds = sm->GetSubMeshDS();
305 bool computeOk = ((sm->GetComputeState() == COMPUTE_OK ) ||
306 (ds && ( dimToCheck ? ds->NbElements() : ds->NbNodes() )));
309 subMeshesComputed = false;
311 if ( isFailedToCompute && !(*isFailedToCompute) )
312 *isFailedToCompute = ( sm->GetComputeState() == FAILED_TO_COMPUTE );
314 if ( !isFailedToCompute )
318 return subMeshesComputed;
321 //================================================================================
323 * \brief Return cost of computing this sub-mesh. If hypotheses are not well defined,
325 * \return int - the computation cost in abstract units.
327 //================================================================================
329 int SMESH_subMesh::GetComputeCost() const
331 return _realComputeCost;
334 //================================================================================
336 * \brief Return cost of computing this sub-mesh. The cost depends on the shape type
337 * and number of sub-meshes this one DependsOn().
338 * \return int - the computation cost in abstract units.
340 //================================================================================
342 int SMESH_subMesh::computeCost() const
347 switch ( _subShape.ShapeType() ) {
349 case TopAbs_SHELL: computeCost = 5000; break;
350 case TopAbs_FACE: computeCost = 500; break;
351 case TopAbs_EDGE: computeCost = 2; break;
352 default: computeCost = 1;
354 SMESH_subMeshIteratorPtr childIt = getDependsOnIterator(/*includeSelf=*/false);
355 while ( childIt->more() )
356 computeCost += childIt->next()->computeCost();
358 ((SMESH_subMesh*)this)->_computeCost = computeCost;
363 //=============================================================================
365 * Returns all sub-meshes this one depend on
367 //=============================================================================
369 const std::map < int, SMESH_subMesh * >& SMESH_subMesh::DependsOn()
371 if ( _dependenceAnalysed || !_father->HasShapeToMesh() )
374 int type = _subShape.ShapeType();
377 case TopAbs_COMPOUND:
379 list< TopoDS_Shape > compounds( 1, _subShape );
380 list< TopoDS_Shape >::iterator comp = compounds.begin();
381 for ( ; comp != compounds.end(); ++comp )
383 for ( TopoDS_Iterator sub( *comp ); sub.More(); sub.Next() )
384 switch ( sub.Value().ShapeType() )
386 case TopAbs_COMPOUND: compounds.push_back( sub.Value() ); break;
387 case TopAbs_COMPSOLID: insertDependence( sub.Value(), TopAbs_SOLID ); break;
388 case TopAbs_SOLID: insertDependence( sub.Value(), TopAbs_SOLID ); break;
389 case TopAbs_SHELL: insertDependence( sub.Value(), TopAbs_FACE ); break;
390 case TopAbs_FACE: insertDependence( sub.Value(), TopAbs_FACE ); break;
391 case TopAbs_WIRE: insertDependence( sub.Value(), TopAbs_EDGE ); break;
392 case TopAbs_EDGE: insertDependence( sub.Value(), TopAbs_EDGE ); break;
393 case TopAbs_VERTEX: insertDependence( sub.Value(), TopAbs_VERTEX ); break;
399 case TopAbs_COMPSOLID: insertDependence( _subShape, TopAbs_SOLID ); break;
400 case TopAbs_SOLID: insertDependence( _subShape, TopAbs_FACE );
401 { /*internal EDGE*/ insertDependence( _subShape, TopAbs_EDGE, TopAbs_WIRE ); break; }
402 case TopAbs_SHELL: insertDependence( _subShape, TopAbs_FACE ); break;
403 case TopAbs_FACE: insertDependence( _subShape, TopAbs_EDGE ); break;
404 case TopAbs_WIRE: insertDependence( _subShape, TopAbs_EDGE ); break;
405 case TopAbs_EDGE: insertDependence( _subShape, TopAbs_VERTEX ); break;
408 _dependenceAnalysed = true;
412 //================================================================================
414 * \brief Return a key for SMESH_subMesh::_mapDepend map
416 //================================================================================
420 int dependsOnMapKey( TopAbs_ShapeEnum type, int shapeID )
422 int ordType = 9 - int(type); // 2 = Vertex, 8 = CompSolid
424 cle += 10000000 * ordType; // sort map by ordType then index
427 int dependsOnMapKey( const SMESH_subMesh* sm )
429 return dependsOnMapKey( sm->GetSubShape().ShapeType(), sm->GetId() );
433 //=============================================================================
435 * Add sub-meshes on sub-shapes of a given type into the dependence map.
437 //=============================================================================
439 void SMESH_subMesh::insertDependence(const TopoDS_Shape aShape,
440 TopAbs_ShapeEnum aSubType,
441 TopAbs_ShapeEnum avoidType)
443 TopExp_Explorer sub( aShape, aSubType, avoidType );
444 for ( ; sub.More(); sub.Next() )
446 SMESH_subMesh *aSubMesh = _father->GetSubMesh( sub.Current() );
447 if ( aSubMesh->GetId() == 0 )
448 continue; // not a sub-shape of the shape to mesh
449 int cle = dependsOnMapKey( aSubMesh );
450 if ( _mapDepend.find( cle ) == _mapDepend.end())
452 _mapDepend[cle] = aSubMesh;
453 const map < int, SMESH_subMesh * > & subMap = aSubMesh->DependsOn();
454 _mapDepend.insert( subMap.begin(), subMap.end() );
459 //================================================================================
461 * \brief Return \c true if \a this sub-mesh depends on \a other
463 //================================================================================
465 bool SMESH_subMesh::DependsOn( const SMESH_subMesh* other ) const
467 return other ? _mapDepend.count( dependsOnMapKey( other )) : false;
470 //================================================================================
472 * \brief Return \c true if \a this sub-mesh depends on a \a shape
474 //================================================================================
476 bool SMESH_subMesh::DependsOn( const int shapeID ) const
478 return DependsOn( _father->GetSubMeshContaining( shapeID ));
481 //=============================================================================
483 * Return a shape of \a this sub-mesh
485 //=============================================================================
487 const TopoDS_Shape & SMESH_subMesh::GetSubShape() const
492 //=======================================================================
493 //function : CanAddHypothesis
494 //purpose : return true if theHypothesis can be attached to me:
495 // its dimension is checked
496 //=======================================================================
498 bool SMESH_subMesh::CanAddHypothesis(const SMESH_Hypothesis* theHypothesis) const
500 int aHypDim = theHypothesis->GetDim();
501 int aShapeDim = SMESH_Gen::GetShapeDim(_subShape);
502 // issue 21106. Forbid 3D mesh on the SHELL
503 // if (aHypDim == 3 && aShapeDim == 3) {
504 // // check case of open shell
505 // //if (_subShape.ShapeType() == TopAbs_SHELL && !_subShape.Closed())
506 // if (_subShape.ShapeType() == TopAbs_SHELL && !BRep_Tool::IsClosed(_subShape))
509 if ( aHypDim <= aShapeDim )
515 //=======================================================================
516 //function : IsApplicableHypothesis
517 //purpose : check if this sub-mesh can be computed using a hypothesis
518 //=======================================================================
520 bool SMESH_subMesh::IsApplicableHypothesis(const SMESH_Hypothesis* theHypothesis) const
522 if ( !_father->HasShapeToMesh() && _subShape.ShapeType() == TopAbs_SOLID )
523 return true; // true for the PseudoShape
525 return IsApplicableHypothesis( theHypothesis, _subShape.ShapeType() );
528 //=======================================================================
529 //function : IsApplicableHypothesis
530 //purpose : compare shape type and hypothesis type
531 //=======================================================================
533 bool SMESH_subMesh::IsApplicableHypothesis(const SMESH_Hypothesis* theHypothesis,
534 const TopAbs_ShapeEnum theShapeType)
536 if ( theHypothesis->GetType() > SMESHDS_Hypothesis::PARAM_ALGO)
539 if ( theHypothesis->GetShapeType() & (1<< theShapeType))
540 // issue 21106. Forbid 3D mesh on the SHELL
541 return !( theHypothesis->GetDim() == 3 && theShapeType == TopAbs_SHELL );
547 switch ( theShapeType ) {
552 return SMESH_Gen::GetShapeDim( theShapeType ) == theHypothesis->GetDim();
555 // Special case for algorithms, building 2D mesh on a whole shell.
556 // Before this fix there was a problem after restoring from study,
557 // because in that case algorithm is assigned before hypothesis
558 // (on shell in problem case) and hypothesis is checked on faces
559 // (because it is 2D), where we have NO_ALGO state.
560 // Now 2D hypothesis is also applicable to shells.
561 return (theHypothesis->GetDim() == 2 || theHypothesis->GetDim() == 3);
564 // case TopAbs_COMPSOLID:
565 // case TopAbs_COMPOUND:
571 //================================================================================
573 * \brief Treats modification of hypotheses definition
574 * \param [in] event - what happens
575 * \param [in] anHyp - a hypothesis
576 * \return SMESH_Hypothesis::Hypothesis_Status - a treatment result.
578 * Optional description of a problematic situation (if any) can be retrieved
579 * via GetComputeError().
581 //================================================================================
583 SMESH_Hypothesis::Hypothesis_Status
584 SMESH_subMesh::AlgoStateEngine(algo_event event, SMESH_Hypothesis * anHyp)
586 // **** les retour des evenement shape sont significatifs
587 // (add ou remove fait ou non)
588 // le retour des evenement father n'indiquent pas que add ou remove fait
590 SMESH_Hypothesis::Hypothesis_Status aux_ret, ret = SMESH_Hypothesis::HYP_OK;
591 if ( _Id == 0 ) return ret; // not a sub-shape of the shape to mesh
593 SMESHDS_Mesh* meshDS =_father->GetMeshDS();
594 SMESH_Algo* algo = 0;
597 if (_subShape.ShapeType() == TopAbs_VERTEX )
599 if ( anHyp->GetDim() != 0) {
600 if (event == ADD_HYP || event == ADD_ALGO)
601 return SMESH_Hypothesis::HYP_BAD_DIM;
603 return SMESH_Hypothesis::HYP_OK;
606 else if ( _algoState == HYP_OK ) {
607 // update default _algoState
608 if ( event != REMOVE_FATHER_ALGO )
610 _algoState = NO_ALGO;
613 _algoState = MISSING_HYP;
614 if ( event == REMOVE_FATHER_HYP ||
615 algo->CheckHypothesis(*_father,_subShape, aux_ret))
622 int oldAlgoState = _algoState;
623 bool modifiedHyp = (event == MODIF_HYP); // if set to true, force event MODIF_ALGO_STATE
624 SMESH_Algo* algoRequiringCleaning = 0;
626 bool isApplicableHyp = IsApplicableHypothesis( anHyp );
628 if (event == ADD_ALGO || event == ADD_FATHER_ALGO)
630 // -------------------------------------------
631 // check if a shape needed by algo is present
632 // -------------------------------------------
633 algo = static_cast< SMESH_Algo* >( anHyp );
634 if ( !_father->HasShapeToMesh() && algo->NeedShape() )
635 return SMESH_Hypothesis::HYP_NEED_SHAPE;
636 // ----------------------
637 // check mesh conformity
638 // ----------------------
639 if (isApplicableHyp && !_father->IsNotConformAllowed() && !IsConform( algo ))
640 return SMESH_Hypothesis::HYP_NOTCONFORM;
642 // check if all-dimensional algo is hidden by other local one
643 if ( event == ADD_ALGO ) {
644 SMESH_HypoFilter filter( SMESH_HypoFilter::HasType( algo->GetType() ));
645 filter.Or( SMESH_HypoFilter::HasType( algo->GetType()+1 ));
646 filter.Or( SMESH_HypoFilter::HasType( algo->GetType()+2 ));
647 if ( SMESH_Algo * curAlgo = (SMESH_Algo*)_father->GetHypothesis( this, filter, true ))
648 if ( !curAlgo->NeedDiscreteBoundary() && curAlgo != anHyp )
649 algoRequiringCleaning = curAlgo;
653 // ----------------------------------
654 // add a hypothesis to DS if possible
655 // ----------------------------------
656 if (event == ADD_HYP || event == ADD_ALGO)
658 if ( ! CanAddHypothesis( anHyp )) // check dimension
659 return SMESH_Hypothesis::HYP_BAD_DIM;
661 if ( !anHyp->IsAuxiliary() && getSimilarAttached( _subShape, anHyp ) )
662 return SMESH_Hypothesis::HYP_ALREADY_EXIST;
664 if ( !meshDS->AddHypothesis(_subShape, anHyp))
665 return SMESH_Hypothesis::HYP_ALREADY_EXIST;
668 // --------------------------
669 // remove a hypothesis from DS
670 // --------------------------
671 if (event == REMOVE_HYP || event == REMOVE_ALGO)
673 if (!meshDS->RemoveHypothesis(_subShape, anHyp))
674 return SMESH_Hypothesis::HYP_OK; // nothing changes
676 if (event == REMOVE_ALGO)
678 algo = dynamic_cast<SMESH_Algo*> (anHyp);
679 if (!algo->NeedDiscreteBoundary())
680 algoRequiringCleaning = algo;
684 // ------------------
685 // analyse algo state
686 // ------------------
687 if (!isApplicableHyp)
688 return ret; // not applicable hypotheses do not change algo state
690 if (( algo = GetAlgo()))
691 algo->InitComputeError();
696 // ----------------------------------------------------------------------
705 if (algo->CheckHypothesis((*_father),_subShape, aux_ret))
706 setAlgoState(HYP_OK);
707 else if ( algo->IsStatusFatal( aux_ret )) {
708 meshDS->RemoveHypothesis(_subShape, anHyp);
712 setAlgoState(MISSING_HYP);
719 case ADD_FATHER_ALGO: { // Algo just added in father
722 if ( algo == anHyp ) {
723 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret))
724 setAlgoState(HYP_OK);
726 setAlgoState(MISSING_HYP);
730 case REMOVE_FATHER_HYP:
732 case REMOVE_FATHER_ALGO: {
736 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
737 setAlgoState(HYP_OK);
739 setAlgoState(MISSING_HYP);
743 case MODIF_HYP: break;
750 // ----------------------------------------------------------------------
758 if ( algo->CheckHypothesis((*_father),_subShape, ret ))
759 setAlgoState(HYP_OK);
760 if (SMESH_Hypothesis::IsStatusFatal( ret ))
761 meshDS->RemoveHypothesis(_subShape, anHyp);
762 else if (!_father->IsUsedHypothesis( anHyp, this ))
764 meshDS->RemoveHypothesis(_subShape, anHyp);
765 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
769 case ADD_ALGO: { //already existing algo : on father ?
772 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))// ignore hyp status
773 setAlgoState(HYP_OK);
774 else if ( algo->IsStatusFatal( aux_ret )) {
775 meshDS->RemoveHypothesis(_subShape, anHyp);
779 setAlgoState(MISSING_HYP);
784 case REMOVE_ALGO: { // perhaps a father algo applies ?
786 if (algo == NULL) // no more algo applying on sub-shape...
788 setAlgoState(NO_ALGO);
792 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
793 setAlgoState(HYP_OK);
795 setAlgoState(MISSING_HYP);
799 case MODIF_HYP: // assigned hypothesis value may become good
800 case ADD_FATHER_HYP: {
803 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
804 setAlgoState(HYP_OK);
806 setAlgoState(MISSING_HYP);
809 case ADD_FATHER_ALGO: { // new father algo
812 if ( algo == anHyp ) {
813 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
814 setAlgoState(HYP_OK);
816 setAlgoState(MISSING_HYP);
820 case REMOVE_FATHER_HYP: // nothing to do
822 case REMOVE_FATHER_ALGO: {
824 if (algo == NULL) // no more applying algo on father
826 setAlgoState(NO_ALGO);
830 if ( algo->CheckHypothesis((*_father),_subShape , aux_ret ))
831 setAlgoState(HYP_OK);
833 setAlgoState(MISSING_HYP);
843 // ----------------------------------------------------------------------
851 if (!algo->CheckHypothesis((*_father),_subShape, ret ))
853 if ( !SMESH_Hypothesis::IsStatusFatal( ret ))
854 // ret should be fatal: anHyp was not added
855 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
857 else if (!_father->IsUsedHypothesis( anHyp, this ))
858 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
860 if (SMESH_Hypothesis::IsStatusFatal( ret ))
862 MESSAGE("do not add extra hypothesis");
863 meshDS->RemoveHypothesis(_subShape, anHyp);
871 case ADD_ALGO: { //already existing algo : on father ?
873 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
874 // check if algo changes
876 f.Init( SMESH_HypoFilter::IsAlgo() );
877 f.And( SMESH_HypoFilter::IsApplicableTo( _subShape ));
878 f.AndNot( SMESH_HypoFilter::Is( algo ));
879 const SMESH_Hypothesis * prevAlgo = _father->GetHypothesis( this, f, true );
881 string( algo->GetName()) != prevAlgo->GetName())
883 oldAlgoState = NO_ALGO; // force setting event listener (#16648)
888 setAlgoState(MISSING_HYP);
894 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
895 setAlgoState(HYP_OK);
897 setAlgoState(MISSING_HYP);
901 case REMOVE_ALGO: { // perhaps a father algo applies ?
903 if (algo == NULL) // no more algo applying on sub-shape...
905 setAlgoState(NO_ALGO);
909 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
910 // check if algo remains
911 if ( anHyp != algo && strcmp( anHyp->GetName(), algo->GetName()) )
915 setAlgoState(MISSING_HYP);
919 case MODIF_HYP: // hypothesis value may become bad
920 case ADD_FATHER_HYP: { // new father hypothesis ?
923 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
925 if (_father->IsUsedHypothesis( anHyp, this )) // new Hyp
929 setAlgoState(MISSING_HYP);
932 case ADD_FATHER_ALGO: {
934 if ( algo == anHyp ) { // a new algo on father
935 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
936 // check if algo changes
938 f.Init( SMESH_HypoFilter::IsAlgo() );
939 f.And( SMESH_HypoFilter::IsApplicableTo( _subShape ));
940 f.AndNot( SMESH_HypoFilter::Is( algo ));
941 const SMESH_Hypothesis* prevAlgo = _father->GetHypothesis( this, f, true );
943 string(algo->GetName()) != string(prevAlgo->GetName()) )
947 setAlgoState(MISSING_HYP);
951 case REMOVE_FATHER_HYP: {
954 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
955 // is there the same local hyp or maybe a new father algo applied?
956 if ( !getSimilarAttached( _subShape, anHyp ) )
960 setAlgoState(MISSING_HYP);
963 case REMOVE_FATHER_ALGO: {
964 // IPAL21346. Edges not removed when Netgen 1d-2d is removed from a SOLID.
965 // CLEAN was not called at event REMOVE_ALGO because the algo is not applicable to SOLID.
966 algo = dynamic_cast<SMESH_Algo*> (anHyp);
967 if (!algo->NeedDiscreteBoundary())
968 algoRequiringCleaning = algo;
970 if (algo == NULL) // no more applying algo on father
972 setAlgoState(NO_ALGO);
976 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
977 // check if algo changes
978 if ( string(algo->GetName()) != string( anHyp->GetName()) )
982 setAlgoState(MISSING_HYP);
992 // ----------------------------------------------------------------------
999 // detect algorithm hiding
1001 if ( ret == SMESH_Hypothesis::HYP_OK &&
1002 ( event == ADD_ALGO || event == ADD_FATHER_ALGO ) && algo &&
1003 algo->GetName() == anHyp->GetName() )
1006 SMESH_Gen* gen = _father->GetGen();
1007 const std::vector< SMESH_subMesh * > & ancestors = GetAncestors();
1008 for ( size_t iA = 0; ( ret == SMESH_Hypothesis::HYP_OK && iA < ancestors.size()); ++iA ) {
1009 if ( SMESH_Algo* upperAlgo = ancestors[ iA ]->GetAlgo() )
1010 if ( !upperAlgo->NeedDiscreteBoundary() && !upperAlgo->SupportSubmeshes())
1011 ret = SMESH_Hypothesis::HYP_HIDDEN_ALGO;
1014 if ( ret == SMESH_Hypothesis::HYP_OK &&
1015 !algo->NeedDiscreteBoundary() &&
1016 !algo->SupportSubmeshes())
1018 TopoDS_Shape algoAssignedTo, otherAssignedTo;
1019 gen->GetAlgo( this, &algoAssignedTo );
1020 map<int, SMESH_subMesh*>::reverse_iterator i_sm = _mapDepend.rbegin();
1021 for ( ; ( ret == SMESH_Hypothesis::HYP_OK && i_sm != _mapDepend.rend()) ; ++i_sm )
1022 if ( gen->GetAlgo( i_sm->second, &otherAssignedTo ) &&
1023 SMESH_MesherHelper::IsSubShape( /*sub=*/otherAssignedTo, /*main=*/algoAssignedTo ))
1024 ret = SMESH_Hypothesis::HYP_HIDING_ALGO;
1028 if ( _algo ) { // get an error description set by _algo->CheckHypothesis()
1029 _computeError = _algo->GetComputeError();
1030 _algo->InitComputeError();
1033 bool stateChange = ( _algoState != oldAlgoState );
1035 if ( stateChange && _algoState == HYP_OK ) // hyp becomes OK
1036 algo->SetEventListener( this );
1038 if ( event == REMOVE_ALGO || event == REMOVE_FATHER_ALGO )
1041 notifyListenersOnEvent( event, ALGO_EVENT, anHyp );
1043 if ( stateChange && oldAlgoState == HYP_OK ) { // hyp becomes KO
1044 deleteOwnListeners();
1045 SetIsAlwaysComputed( false );
1046 if (_subShape.ShapeType() == TopAbs_VERTEX ) {
1047 // restore default states
1048 _algoState = HYP_OK;
1049 _computeState = READY_TO_COMPUTE;
1053 if ( algoRequiringCleaning ) {
1054 // added or removed algo is all-dimensional
1055 ComputeStateEngine( CLEAN );
1056 cleanDependsOn( algoRequiringCleaning );
1057 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1060 if ( stateChange || modifiedHyp )
1061 ComputeStateEngine( MODIF_ALGO_STATE );
1063 _realComputeCost = ( _algoState == HYP_OK ) ? computeCost() : 0;
1068 //=======================================================================
1069 //function : IsConform
1070 //purpose : check if a conform mesh will be produced by the Algo
1071 //=======================================================================
1073 bool SMESH_subMesh::IsConform(const SMESH_Algo* theAlgo)
1075 // MESSAGE( "SMESH_subMesh::IsConform" );
1076 if ( !theAlgo ) return false;
1078 // Suppose that theAlgo is applicable to _subShape, do not check it here
1079 //if ( !IsApplicableHypothesis( theAlgo )) return false;
1081 // check only algo that doesn't NeedDiscreteBoundary(): because mesh made
1082 // on a sub-shape will be ignored by theAlgo
1083 if ( theAlgo->NeedDiscreteBoundary() ||
1084 !theAlgo->OnlyUnaryInput() ) // all adjacent shapes will be meshed by this algo?
1087 // only local algo is to be checked
1088 //if ( gen->IsGlobalHypothesis( theAlgo, *_father ))
1089 if ( _subShape.ShapeType() == _father->GetMeshDS()->ShapeToMesh().ShapeType() )
1092 // check algo attached to adjacent shapes
1094 // loop on one level down sub-meshes
1095 TopoDS_Iterator itsub( _subShape );
1096 for (; itsub.More(); itsub.Next())
1098 // loop on adjacent subShapes
1099 const std::vector< SMESH_subMesh * > & ancestors = GetAncestors();
1100 for ( size_t iA = 0; iA < ancestors.size(); ++iA )
1102 const TopoDS_Shape& adjacent = ancestors[ iA ]->GetSubShape();
1103 if ( _subShape.IsSame( adjacent )) continue;
1104 if ( adjacent.ShapeType() != _subShape.ShapeType())
1107 // check algo attached to smAdjacent
1108 SMESH_Algo * algo = ancestors[ iA ]->GetAlgo();
1110 !algo->NeedDiscreteBoundary() &&
1111 algo->OnlyUnaryInput())
1112 return false; // NOT CONFORM MESH WILL BE PRODUCED
1119 //=============================================================================
1123 //=============================================================================
1125 void SMESH_subMesh::setAlgoState(algo_state state)
1130 //================================================================================
1132 * \brief Send an event to sub-meshes
1133 * \param [in] event - the event
1134 * \param [in] anHyp - an hypothesis
1135 * \param [in] exitOnFatal - to stop iteration on sub-meshes if a sub-mesh
1136 * reports a fatal result
1137 * \return SMESH_Hypothesis::Hypothesis_Status - the worst result
1139 * Optional description of a problematic situation (if any) can be retrieved
1140 * via GetComputeError().
1142 //================================================================================
1144 SMESH_Hypothesis::Hypothesis_Status
1145 SMESH_subMesh::SubMeshesAlgoStateEngine(algo_event event,
1146 SMESH_Hypothesis * anHyp,
1149 SMESH_Hypothesis::Hypothesis_Status ret = SMESH_Hypothesis::HYP_OK;
1150 //EAP: a wire (dim==1) should notify edges (dim==1)
1151 //EAP: int dim = SMESH_Gen::GetShapeDim(_subShape);
1152 //if (_subShape.ShapeType() < TopAbs_EDGE ) // wire,face etc
1154 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1155 while ( smIt->more() ) {
1156 SMESH_subMesh* sm = smIt->next();
1157 SMESH_Hypothesis::Hypothesis_Status ret2 = sm->AlgoStateEngine(event, anHyp);
1161 _computeError = sm->_computeError;
1162 sm->_computeError.reset();
1163 if ( exitOnFatal && SMESH_Hypothesis::IsStatusFatal( ret ))
1171 //================================================================================
1173 * \brief Remove elements from sub-meshes.
1174 * \param algoRequiringCleaning - an all-dimensional algorithm whose presence
1175 * causes the cleaning.
1177 //================================================================================
1179 void SMESH_subMesh::cleanDependsOn( SMESH_Algo* algoRequiringCleaning/*=0*/ )
1181 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,
1182 /*complexShapeFirst=*/true);
1183 if ( _father->NbNodes() == 0 )
1185 while ( smIt->more() )
1186 smIt->next()->ComputeStateEngine(CHECK_COMPUTE_STATE);
1188 else if ( !algoRequiringCleaning || !algoRequiringCleaning->SupportSubmeshes() )
1190 while ( smIt->more() )
1191 smIt->next()->ComputeStateEngine(CLEAN);
1193 else if ( algoRequiringCleaning && algoRequiringCleaning->SupportSubmeshes() )
1195 // find sub-meshes to keep elements on
1196 set< SMESH_subMesh* > smToKeep;
1197 TopAbs_ShapeEnum prevShapeType = TopAbs_SHAPE;
1198 bool toKeepPrevShapeType = false;
1199 while ( smIt->more() )
1201 SMESH_subMesh* sm = smIt->next();
1202 sm->ComputeStateEngine(CHECK_COMPUTE_STATE);
1203 if ( !sm->IsEmpty() )
1205 const bool sameShapeType = ( prevShapeType == sm->GetSubShape().ShapeType() );
1206 bool keepSubMeshes = ( sameShapeType && toKeepPrevShapeType );
1207 if ( !sameShapeType )
1209 // check if the algo allows presence of global algos of dimension the algo
1210 // can generate it-self;
1211 // always keep a node on VERTEX, as this node can be shared by segments
1212 // lying on EDGEs not shared by the VERTEX of sm, due to MergeNodes (PAL23068)
1213 int shapeDim = SMESH_Gen::GetShapeDim( sm->GetSubShape() );
1214 keepSubMeshes = ( algoRequiringCleaning->NeedLowerHyps( shapeDim ) || shapeDim == 0 );
1215 prevShapeType = sm->GetSubShape().ShapeType();
1216 toKeepPrevShapeType = keepSubMeshes;
1218 if ( !keepSubMeshes )
1220 // look for a local algo used to mesh sm
1221 TopoDS_Shape algoShape = SMESH_MesherHelper::GetShapeOfHypothesis
1222 ( algoRequiringCleaning, _subShape, _father );
1223 SMESH_HypoFilter moreLocalAlgo;
1224 moreLocalAlgo.Init( SMESH_HypoFilter::IsMoreLocalThan( algoShape, *_father ));
1225 moreLocalAlgo.And ( SMESH_HypoFilter::IsAlgo() );
1226 bool localAlgoFound = _father->GetHypothesis( sm->_subShape, moreLocalAlgo, true );
1227 keepSubMeshes = localAlgoFound;
1229 // remember all sub-meshes of sm
1230 if ( keepSubMeshes )
1232 SMESH_subMeshIteratorPtr smIt2 = sm->getDependsOnIterator(true);
1233 while ( smIt2->more() )
1234 smToKeep.insert( smIt2->next() );
1239 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,true);
1240 while ( smIt->more() )
1242 SMESH_subMesh* sm = smIt->next();
1243 if ( !smToKeep.count( sm ))
1244 sm->ComputeStateEngine(CLEAN);
1249 //=============================================================================
1253 //=============================================================================
1255 void SMESH_subMesh::DumpAlgoState(bool isMain)
1259 const map < int, SMESH_subMesh * >&subMeshes = DependsOn();
1261 map < int, SMESH_subMesh * >::const_iterator itsub;
1262 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
1264 SMESH_subMesh *sm = (*itsub).second;
1265 sm->DumpAlgoState(false);
1268 MESSAGE("dim = " << SMESH_Gen::GetShapeDim(_subShape) <<
1269 " type of shape " << _subShape.ShapeType());
1272 case NO_ALGO : MESSAGE(" AlgoState = NO_ALGO"); break;
1273 case MISSING_HYP : MESSAGE(" AlgoState = MISSING_HYP"); break;
1274 case HYP_OK : MESSAGE(" AlgoState = HYP_OK");break;
1276 switch (_computeState)
1278 case NOT_READY : MESSAGE(" ComputeState = NOT_READY");break;
1279 case READY_TO_COMPUTE : MESSAGE(" ComputeState = READY_TO_COMPUTE");break;
1280 case COMPUTE_OK : MESSAGE(" ComputeState = COMPUTE_OK");break;
1281 case FAILED_TO_COMPUTE: MESSAGE(" ComputeState = FAILED_TO_COMPUTE");break;
1285 //================================================================================
1287 * \brief Remove nodes and elements bound to submesh
1288 * \param subMesh - submesh containing nodes and elements
1290 //================================================================================
1292 static void cleanSubMesh( SMESH_subMesh * subMesh )
1295 if (SMESHDS_SubMesh * subMeshDS = subMesh->GetSubMeshDS())
1297 SMESHDS_Mesh * meshDS = subMesh->GetFather()->GetMeshDS();
1298 int nbElems = subMeshDS->NbElements();
1300 for ( SMDS_ElemIteratorPtr ite = subMeshDS->GetElements(); ite->more(); )
1301 meshDS->RemoveFreeElement( ite->next(), subMeshDS );
1303 int nbNodes = subMeshDS->NbNodes();
1305 for ( SMDS_NodeIteratorPtr itn = subMeshDS->GetNodes(); itn->more() ; )
1307 const SMDS_MeshNode * node = itn->next();
1308 if ( node->NbInverseElements() == 0 )
1309 meshDS->RemoveFreeNode( node, subMeshDS );
1310 else // for StdMeshers_CompositeSegment_1D: node in one submesh, edge in another
1311 meshDS->RemoveNode( node );
1318 //=============================================================================
1322 //=============================================================================
1324 bool SMESH_subMesh::ComputeStateEngine(compute_event event)
1327 case MODIF_ALGO_STATE:
1329 case COMPUTE_SUBMESH:
1330 //case COMPUTE_CANCELED:
1332 //case SUBMESH_COMPUTED:
1333 //case SUBMESH_RESTORED:
1334 //case SUBMESH_LOADED:
1335 //case MESH_ENTITY_REMOVED:
1336 //case CHECK_COMPUTE_STATE:
1337 _computeError.reset(); break;
1341 if ( event == CLEAN )
1342 _alwaysComputed = false; // Unset 'true' set by MergeNodes() (issue 0022182)
1344 if (_subShape.ShapeType() == TopAbs_VERTEX)
1346 _computeState = READY_TO_COMPUTE;
1347 SMESHDS_SubMesh* smDS = GetSubMeshDS();
1348 if ( smDS && smDS->NbNodes() )
1350 if ( event == CLEAN ) {
1352 cleanSubMesh( this );
1355 _computeState = COMPUTE_OK;
1357 else if (( event == COMPUTE || event == COMPUTE_SUBMESH )
1358 && !_alwaysComputed )
1360 const TopoDS_Vertex & V = TopoDS::Vertex( _subShape );
1361 gp_Pnt P = BRep_Tool::Pnt(V);
1362 if ( SMDS_MeshNode * n = _father->GetMeshDS()->AddNode(P.X(), P.Y(), P.Z()) ) {
1363 _father->GetMeshDS()->SetNodeOnVertex(n,_Id);
1364 _computeState = COMPUTE_OK;
1367 if ( event == MODIF_ALGO_STATE )
1371 SMESH_Gen *gen = _father->GetGen();
1372 SMESH_Algo *algo = 0;
1374 SMESH_Hypothesis::Hypothesis_Status hyp_status;
1375 //algo_state oldAlgoState = (algo_state) GetAlgoState();
1377 switch (_computeState)
1380 // ----------------------------------------------------------------------
1385 case MODIF_ALGO_STATE:
1387 if (algo && !algo->NeedDiscreteBoundary())
1388 cleanDependsOn( algo ); // clean sub-meshes with event CLEAN
1389 if ( _algoState == HYP_OK )
1390 _computeState = READY_TO_COMPUTE;
1392 case COMPUTE: // nothing to do
1393 case COMPUTE_SUBMESH:
1395 case COMPUTE_CANCELED: // nothing to do
1399 removeSubMeshElementsAndNodes();
1401 case SUBMESH_COMPUTED: // nothing to do
1403 case SUBMESH_RESTORED:
1404 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1406 case MESH_ENTITY_REMOVED:
1408 case SUBMESH_LOADED:
1409 loadDependentMeshes();
1410 ComputeSubMeshStateEngine( SUBMESH_LOADED );
1412 case CHECK_COMPUTE_STATE:
1413 if ( IsMeshComputed() )
1414 _computeState = COMPUTE_OK;
1422 // ----------------------------------------------------------------------
1424 case READY_TO_COMPUTE:
1427 case MODIF_ALGO_STATE:
1428 _computeState = NOT_READY;
1432 if (!algo->NeedDiscreteBoundary())
1433 cleanDependsOn( algo ); // clean sub-meshes with event CLEAN
1434 if ( _algoState == HYP_OK )
1435 _computeState = READY_TO_COMPUTE;
1439 case COMPUTE_NOGEOM: // no geometry; can be several algos
1440 if ( !_father->HasShapeToMesh() )
1442 algo = GetAlgo(); // current algo
1445 // apply algos in the order of increasing dimension
1446 std::list< const SMESHDS_Hypothesis * > algos = _father->GetHypothesisList( _subShape );
1447 for ( int t = SMESHDS_Hypothesis::ALGO_1D; t <= SMESHDS_Hypothesis::ALGO_3D; ++t )
1449 std::list<const SMESHDS_Hypothesis *>::iterator al = algos.begin();
1450 for ( ; al != algos.end(); ++al )
1451 if ( (*al)->GetType() == t )
1453 _algo = (SMESH_Algo*) *al;
1454 _computeState = READY_TO_COMPUTE;
1455 if ( !ComputeStateEngine( COMPUTE ))
1459 _algo = algo; // restore
1464 case COMPUTE_SUBMESH:
1468 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1471 MESSAGE("***** verify compute state *****");
1472 _computeState = NOT_READY;
1473 setAlgoState(MISSING_HYP);
1476 TopoDS_Shape shape = _subShape;
1477 algo->SubMeshesToCompute().assign( 1, this );
1478 // check submeshes needed
1479 if (_father->HasShapeToMesh() ) {
1480 bool subComputed = false, subFailed = false;
1481 if (!algo->OnlyUnaryInput()) {
1482 // --- commented for bos#22320 to compute all sub-shapes at once if possible;
1483 // --- in case COMPUTE_SUBMESH, set of sub-shapes is limited
1484 // --- by calling SetAllowedSubShapes()
1485 // if ( event == COMPUTE )
1486 // shape = getCollection( gen, algo, subComputed, subFailed, algo->SubMeshesToComput;
1488 // subComputed = SubMeshesComputed( & subFailed );
1489 shape = getCollection( gen, algo, subComputed, subFailed, algo->SubMeshesToCompute());
1492 subComputed = SubMeshesComputed();
1494 ret = ( algo->NeedDiscreteBoundary() ? subComputed :
1495 algo->SupportSubmeshes() ? !subFailed :
1496 ( !subComputed || _father->IsNotConformAllowed() ));
1499 _computeState = FAILED_TO_COMPUTE;
1500 if ( !algo->NeedDiscreteBoundary() && !subFailed )
1502 SMESH_ComputeError::New(COMPERR_BAD_INPUT_MESH,
1503 "Unexpected computed sub-mesh",algo);
1509 // to restore cout that may be redirected by algo
1510 std::streambuf* coutBuffer = std::cout.rdbuf();
1512 //cleanDependants(); for "UseExisting_*D" algos
1513 //removeSubMeshElementsAndNodes();
1514 loadDependentMeshes();
1516 _computeState = FAILED_TO_COMPUTE;
1517 _computeError = SMESH_ComputeError::New(COMPERR_OK,"",algo);
1521 algo->InitComputeError();
1523 MemoryReserve aMemoryReserve;
1524 SMDS_Mesh::CheckMemory();
1525 Kernel_Utils::Localizer loc;
1526 if ( !_father->HasShapeToMesh() ) // no shape
1528 SMESH_MesherHelper helper( *_father );
1529 helper.SetSubShape( shape );
1530 helper.SetElementsOnShape( true );
1531 ret = algo->Compute(*_father, &helper );
1535 ret = algo->Compute((*_father), shape);
1537 // algo can set _computeError of submesh
1538 _computeError = SMESH_ComputeError::Worst( _computeError, algo->GetComputeError() );
1540 catch ( ::SMESH_ComputeError& comperr ) {
1541 cout << " SMESH_ComputeError caught" << endl;
1542 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1543 *_computeError = comperr;
1545 catch ( std::bad_alloc& exc ) {
1546 MESSAGE("std::bad_alloc thrown inside algo->Compute()");
1547 if ( _computeError ) {
1548 _computeError->myName = COMPERR_MEMORY_PB;
1550 cleanSubMesh( this );
1553 catch ( Standard_OutOfMemory& exc ) {
1554 MESSAGE("Standard_OutOfMemory thrown inside algo->Compute()");
1555 if ( _computeError ) {
1556 _computeError->myName = COMPERR_MEMORY_PB;
1558 cleanSubMesh( this );
1559 throw std::bad_alloc();
1561 catch (Standard_Failure& ex) {
1562 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1563 _computeError->myName = COMPERR_OCC_EXCEPTION;
1564 _computeError->myComment += ex.DynamicType()->Name();
1565 if ( ex.GetMessageString() && strlen( ex.GetMessageString() )) {
1566 _computeError->myComment += ": ";
1567 _computeError->myComment += ex.GetMessageString();
1570 catch ( SALOME_Exception& S_ex ) {
1571 const int skipSalomeShift = 7; /* to skip "Salome " of
1572 "Salome Exception" prefix returned
1573 by SALOME_Exception::what() */
1574 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1575 _computeError->myName = COMPERR_SLM_EXCEPTION;
1576 _computeError->myComment = S_ex.what() + skipSalomeShift;
1578 catch ( std::exception& exc ) {
1579 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1580 _computeError->myName = COMPERR_STD_EXCEPTION;
1581 _computeError->myComment = exc.what();
1584 if ( _computeError )
1585 _computeError->myName = COMPERR_EXCEPTION;
1589 std::cout.rdbuf( coutBuffer ); // restore cout that could be redirected by algo
1591 // check if an error reported on any sub-shape
1592 bool isComputeErrorSet = !checkComputeError( algo, ret, shape );
1593 if ( isComputeErrorSet )
1595 // check if anything was built
1596 TopExp_Explorer subS(shape, _subShape.ShapeType());
1599 for (; ret && subS.More(); subS.Next())
1600 if ( !_father->GetSubMesh( subS.Current() )->IsMeshComputed() &&
1601 ( _subShape.ShapeType() != TopAbs_EDGE ||
1602 !algo->isDegenerated( TopoDS::Edge( subS.Current() ))))
1605 #ifdef PRINT_WHO_COMPUTE_WHAT
1606 for (subS.ReInit(); subS.More(); subS.Next())
1608 const std::list <const SMESHDS_Hypothesis *> & hyps =
1609 _algo->GetUsedHypothesis( *_father, _subShape );
1610 SMESH_Comment hypStr;
1611 if ( !hyps.empty() )
1613 hypStr << hyps.front()->GetName() << " ";
1614 ((SMESHDS_Hypothesis*)hyps.front())->SaveTo( hypStr.Stream() );
1617 cout << _father->GetSubMesh( subS.Current() )->GetId()
1618 << " " << ( ret ? "OK" : "FAIL" )
1619 << " " << _algo->GetName()
1620 << " " << hypStr << endl;
1623 // Set _computeError
1624 if ( !ret && !isComputeErrorSet )
1626 for ( subS.ReInit(); subS.More(); subS.Next() )
1628 SMESH_subMesh* sm = _father->GetSubMesh( subS.Current() );
1629 if ( !sm->IsMeshComputed() )
1631 if ( !sm->_computeError )
1632 sm->_computeError = SMESH_ComputeError::New();
1633 if ( sm->_computeError->IsOK() )
1634 sm->_computeError->myName = COMPERR_ALGO_FAILED;
1635 sm->_computeState = FAILED_TO_COMPUTE;
1636 sm->_computeError->myAlgo = algo;
1640 if ( ret && _computeError && _computeError->myName != COMPERR_WARNING )
1642 _computeError.reset();
1645 // transform errors into warnings if it is caused by mesh edition (imp 0023068)
1646 if (!ret && _father->GetIsModified() )
1648 for (subS.ReInit(); subS.More(); subS.Next())
1650 SMESH_subMesh* sm = _father->GetSubMesh( subS.Current() );
1651 if ( !sm->IsMeshComputed() && sm->_computeError )
1653 // check if there is a VERTEX w/o nodes
1654 // with READY_TO_COMPUTE state (after MergeNodes())
1655 SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(false,false);
1656 while ( smIt->more() )
1658 SMESH_subMesh * vertSM = smIt->next();
1659 if ( vertSM->_subShape.ShapeType() != TopAbs_VERTEX ) break;
1660 if ( vertSM->GetComputeState() == READY_TO_COMPUTE )
1662 SMESHDS_SubMesh * ds = vertSM->GetSubMeshDS();
1663 if ( !ds || ds->NbNodes() == 0 )
1665 sm->_computeState = READY_TO_COMPUTE;
1666 sm->_computeError->myName = COMPERR_WARNING;
1675 // send event SUBMESH_COMPUTED
1677 if ( !algo->NeedDiscreteBoundary() )
1678 // send SUBMESH_COMPUTED to dependants of all sub-meshes of shape
1679 for (subS.ReInit(); subS.More(); subS.Next())
1681 SMESH_subMesh* sm = _father->GetSubMesh( subS.Current() );
1682 SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(false,false);
1683 while ( smIt->more() ) {
1685 if ( sm->GetSubShape().ShapeType() == TopAbs_VERTEX )
1686 sm->updateDependantsState( SUBMESH_COMPUTED );
1692 updateDependantsState( SUBMESH_COMPUTED );
1694 // let algo clear its data gathered while algo->Compute()
1695 algo->CheckHypothesis((*_father), _subShape, hyp_status);
1698 case COMPUTE_CANCELED: // nothing to do
1702 removeSubMeshElementsAndNodes();
1703 _computeState = NOT_READY;
1707 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1709 _computeState = READY_TO_COMPUTE;
1711 setAlgoState(MISSING_HYP);
1714 case SUBMESH_COMPUTED: // nothing to do
1716 case SUBMESH_RESTORED:
1717 // check if a mesh is already computed that may
1718 // happen after retrieval from a file
1719 ComputeStateEngine( CHECK_COMPUTE_STATE );
1720 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1722 if (algo) algo->SubmeshRestored( this );
1724 case MESH_ENTITY_REMOVED:
1726 case SUBMESH_LOADED:
1727 loadDependentMeshes();
1728 ComputeSubMeshStateEngine( SUBMESH_LOADED );
1730 case CHECK_COMPUTE_STATE:
1731 if ( IsMeshComputed() )
1732 _computeState = COMPUTE_OK;
1733 else if ( _computeError && _computeError->IsKO() )
1734 _computeState = FAILED_TO_COMPUTE;
1742 // ----------------------------------------------------------------------
1747 case MODIF_ALGO_STATE:
1748 ComputeStateEngine( CLEAN );
1750 if (algo && !algo->NeedDiscreteBoundary())
1751 cleanDependsOn( algo ); // clean sub-meshes with event CLEAN
1753 case COMPUTE: // nothing to do
1755 case COMPUTE_SUBMESH: // nothing to do
1757 case COMPUTE_CANCELED: // nothing to do
1760 cleanDependants(); // clean sub-meshes, dependent on this one, with event CLEAN
1761 removeSubMeshElementsAndNodes();
1762 _computeState = NOT_READY;
1763 if ( _algoState == HYP_OK )
1764 _computeState = READY_TO_COMPUTE;
1766 case SUBMESH_COMPUTED: // nothing to do
1768 case SUBMESH_RESTORED:
1769 ComputeStateEngine( CHECK_COMPUTE_STATE );
1770 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1772 if (algo) algo->SubmeshRestored( this );
1774 case MESH_ENTITY_REMOVED:
1775 updateDependantsState ( CHECK_COMPUTE_STATE );
1776 ComputeStateEngine ( CHECK_COMPUTE_STATE );
1777 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1779 case CHECK_COMPUTE_STATE:
1780 if ( !IsMeshComputed() ) {
1781 if (_algoState == HYP_OK)
1782 _computeState = READY_TO_COMPUTE;
1784 _computeState = NOT_READY;
1787 case SUBMESH_LOADED:
1788 // already treated event, thanks to which _computeState == COMPUTE_OK
1796 // ----------------------------------------------------------------------
1798 case FAILED_TO_COMPUTE:
1801 case MODIF_ALGO_STATE:
1803 ComputeStateEngine( CLEAN );
1805 if (algo && !algo->NeedDiscreteBoundary())
1806 cleanDependsOn( algo ); // clean sub-meshes with event CLEAN
1807 if (_algoState == HYP_OK)
1808 _computeState = READY_TO_COMPUTE;
1810 _computeState = NOT_READY;
1812 case COMPUTE: // nothing to do
1813 case COMPUTE_SUBMESH:
1815 case COMPUTE_CANCELED:
1818 algo->CancelCompute();
1822 cleanDependants(); // submeshes dependent on me should be cleaned as well
1823 removeSubMeshElementsAndNodes();
1825 case SUBMESH_COMPUTED: // allow retry compute
1826 if ( IsEmpty() ) // 23061
1828 if (_algoState == HYP_OK)
1829 _computeState = READY_TO_COMPUTE;
1831 _computeState = NOT_READY;
1834 case SUBMESH_RESTORED:
1835 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1837 case MESH_ENTITY_REMOVED:
1839 case CHECK_COMPUTE_STATE:
1840 if ( IsMeshComputed() )
1841 _computeState = COMPUTE_OK;
1843 if (_algoState == HYP_OK)
1844 _computeState = READY_TO_COMPUTE;
1846 _computeState = NOT_READY;
1848 // case SUBMESH_LOADED:
1856 // ----------------------------------------------------------------------
1862 notifyListenersOnEvent( event, COMPUTE_EVENT );
1868 //=============================================================================
1872 //=============================================================================
1874 bool SMESH_subMesh::Evaluate(MapShapeNbElems& aResMap)
1876 _computeError.reset();
1880 if (_subShape.ShapeType() == TopAbs_VERTEX) {
1881 vector<int> aVec(SMDSEntity_Last,0);
1882 aVec[SMDSEntity_Node] = 1;
1883 aResMap.insert(make_pair(this,aVec));
1887 //SMESH_Gen *gen = _father->GetGen();
1888 SMESH_Algo *algo = 0;
1889 SMESH_Hypothesis::Hypothesis_Status hyp_status;
1892 if( algo && !aResMap.count( this ))
1894 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1895 if (!ret) return false;
1897 if (_father->HasShapeToMesh() && algo->NeedDiscreteBoundary() )
1899 // check submeshes needed
1900 bool subMeshEvaluated = true;
1901 int dimToCheck = SMESH_Gen::GetShapeDim( _subShape ) - 1;
1902 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,/*complexShapeFirst=*/true);
1903 while ( smIt->more() && subMeshEvaluated )
1905 SMESH_subMesh* sm = smIt->next();
1906 int dim = SMESH_Gen::GetShapeDim( sm->GetSubShape() );
1907 if (dim < dimToCheck) break; // the rest subMeshes are all of less dimension
1908 const vector<int> & nbs = aResMap[ sm ];
1909 subMeshEvaluated = (std::accumulate( nbs.begin(), nbs.end(), 0 ) > 0 );
1911 if ( !subMeshEvaluated )
1914 _computeError = SMESH_ComputeError::New(COMPERR_OK,"",algo);
1916 if ( IsMeshComputed() )
1918 vector<int> & nbEntities = aResMap[ this ];
1919 nbEntities.resize( SMDSEntity_Last, 0 );
1920 if ( SMESHDS_SubMesh* sm = GetSubMeshDS() )
1922 nbEntities[ SMDSEntity_Node ] = sm->NbNodes();
1923 SMDS_ElemIteratorPtr elemIt = sm->GetElements();
1924 while ( elemIt->more() )
1925 nbEntities[ elemIt->next()->GetEntityType() ]++;
1930 ret = algo->Evaluate((*_father), _subShape, aResMap);
1932 aResMap.insert( make_pair( this,vector<int>(0)));
1939 //=======================================================================
1941 * \brief Update compute_state by _computeError and send proper events to
1942 * dependent submeshes
1943 * \retval bool - true if _computeError is NOT set
1945 //=======================================================================
1947 bool SMESH_subMesh::checkComputeError(SMESH_Algo* theAlgo,
1948 const bool theComputeOK,
1949 const TopoDS_Shape& theShape)
1951 bool noErrors = true;
1953 if ( !theShape.IsNull() )
1955 // Check state of submeshes
1956 if ( !theAlgo->NeedDiscreteBoundary())
1958 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1959 while ( smIt->more() )
1960 if ( !smIt->next()->checkComputeError( theAlgo, theComputeOK ))
1964 // Check state of neighbours
1965 if ( !theAlgo->OnlyUnaryInput() &&
1966 theShape.ShapeType() == TopAbs_COMPOUND &&
1967 !theShape.IsSame( _subShape ))
1969 for (TopoDS_Iterator subIt( theShape ); subIt.More(); subIt.Next()) {
1970 SMESH_subMesh* sm = _father->GetSubMesh( subIt.Value() );
1972 if ( !sm->checkComputeError( theAlgo, theComputeOK, sm->GetSubShape() ))
1974 updateDependantsState( SUBMESH_COMPUTED ); // send event SUBMESH_COMPUTED
1981 // Set my _computeState
1983 if ( !_computeError || _computeError->IsOK() )
1985 // no error description is set to this sub-mesh, check if any mesh is computed
1986 _computeState = IsMeshComputed() ? COMPUTE_OK : FAILED_TO_COMPUTE;
1987 if ( _computeState != COMPUTE_OK )
1989 if ( _subShape.ShapeType() == TopAbs_EDGE &&
1990 SMESH_Algo::isDegenerated( TopoDS::Edge( _subShape )) )
1991 _computeState = COMPUTE_OK;
1992 else if ( theComputeOK )
1993 _computeError = SMESH_ComputeError::New(COMPERR_NO_MESH_ON_SHAPE,"",theAlgo);
1997 if ( _computeError && !_computeError->IsOK() )
1999 if ( !_computeError->myAlgo )
2000 _computeError->myAlgo = theAlgo;
2004 text << theAlgo->GetName() << " failed on sub-shape #" << _Id << " with error ";
2005 if (_computeError->IsCommon() )
2006 text << _computeError->CommonName();
2008 text << _computeError->myName;
2009 if ( _computeError->myComment.size() > 0 )
2010 text << " \"" << _computeError->myComment << "\"";
2014 _computeState = _computeError->IsKO() ? FAILED_TO_COMPUTE : COMPUTE_OK;
2022 //=======================================================================
2023 //function : updateSubMeshState
2025 //=======================================================================
2027 void SMESH_subMesh::updateSubMeshState(const compute_state theState)
2029 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
2030 while ( smIt->more() )
2031 smIt->next()->_computeState = theState;
2034 //=======================================================================
2035 //function : ComputeSubMeshStateEngine
2037 //=======================================================================
2039 void SMESH_subMesh::ComputeSubMeshStateEngine(compute_event event, const bool includeSelf)
2041 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(includeSelf,false);
2042 while ( smIt->more() )
2043 smIt->next()->ComputeStateEngine(event);
2046 //=======================================================================
2047 //function : updateDependantsState
2049 //=======================================================================
2051 void SMESH_subMesh::updateDependantsState(const compute_event theEvent)
2053 const std::vector< SMESH_subMesh * > & ancestors = GetAncestors();
2054 for ( size_t iA = 0; iA < ancestors.size(); ++iA )
2056 ancestors[ iA ]->ComputeStateEngine( theEvent );
2060 //=======================================================================
2061 //function : cleanDependants
2063 //=======================================================================
2065 void SMESH_subMesh::cleanDependants()
2067 int dimToClean = SMESH_Gen::GetShapeDim( _subShape ) + 1;
2069 const std::vector< SMESH_subMesh * > & ancestors = GetAncestors();
2070 for ( size_t iA = 0; iA < ancestors.size(); ++iA )
2072 const TopoDS_Shape& ancestor = ancestors[ iA ]->GetSubShape();
2073 if ( SMESH_Gen::GetShapeDim( ancestor ) == dimToClean )
2075 // PAL8021. do not go upper than SOLID, else ComputeStateEngine(CLEAN)
2076 // will erase mesh on other shapes in a compound
2077 if ( ancestor.ShapeType() >= TopAbs_SOLID &&
2078 !ancestors[ iA ]->IsEmpty() ) // prevent infinite CLEAN via event lesteners
2079 ancestors[ iA ]->ComputeStateEngine(CLEAN);
2084 //=======================================================================
2085 //function : removeSubMeshElementsAndNodes
2087 //=======================================================================
2089 void SMESH_subMesh::removeSubMeshElementsAndNodes()
2091 cleanSubMesh( this );
2093 // algo may bind a submesh not to _subShape, eg 3D algo
2094 // sets nodes on SHELL while _subShape may be SOLID
2096 int dim = SMESH_Gen::GetShapeDim( _subShape );
2097 int type = _subShape.ShapeType() + 1;
2098 for ( ; type <= TopAbs_EDGE; type++) {
2099 if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
2101 TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
2102 for ( ; exp.More(); exp.Next() )
2103 cleanSubMesh( _father->GetSubMeshContaining( exp.Current() ));
2110 //=======================================================================
2111 //function : getCollection
2112 //purpose : return a shape containing all sub-shapes of the MainShape that can be
2113 // meshed at once along with _subShape
2114 //=======================================================================
2116 TopoDS_Shape SMESH_subMesh::getCollection(SMESH_Gen * theGen,
2117 SMESH_Algo* theAlgo,
2118 bool & theSubComputed,
2119 bool & theSubFailed,
2120 std::vector<SMESH_subMesh*>& theSubs)
2122 theSubComputed = SubMeshesComputed( & theSubFailed );
2124 TopoDS_Shape mainShape = _father->GetMeshDS()->ShapeToMesh();
2126 if ( mainShape.IsSame( _subShape ))
2129 const bool skipAuxHyps = false;
2130 list<const SMESHDS_Hypothesis*> usedHyps =
2131 theAlgo->GetUsedHypothesis( *_father, _subShape, skipAuxHyps ); // copy
2132 std::list < TopoDS_Shape > assiShapes = theAlgo->GetAssignedShapes();
2134 // put in a compound all shapes with the same hypothesis assigned
2135 // and a good ComputeState
2137 TopoDS_Compound aCompound;
2138 BRep_Builder aBuilder;
2139 aBuilder.MakeCompound( aCompound );
2143 SMESH_subMeshIteratorPtr smIt = _father->GetSubMesh( mainShape )->getDependsOnIterator(false);
2144 while ( smIt->more() )
2146 SMESH_subMesh* subMesh = smIt->next();
2147 const TopoDS_Shape& S = subMesh->_subShape;
2148 if ( S.ShapeType() != this->_subShape.ShapeType() )
2150 if ( _allowedSubShapes && !_allowedSubShapes->IsEmpty() && !_allowedSubShapes->Contains( S ))
2152 if ( subMesh == this )
2154 aBuilder.Add( aCompound, S );
2155 theSubs.push_back( subMesh );
2157 else if ( subMesh->GetComputeState() == READY_TO_COMPUTE )
2159 SMESH_Algo* anAlgo = subMesh->GetAlgo();
2160 if (( anAlgo->IsSameName( *theAlgo )) && // same algo
2161 ( anAlgo->GetUsedHypothesis( *_father, S, skipAuxHyps ) == usedHyps ) && // same hyps
2162 ( anAlgo->GetAssignedShapes() == assiShapes ) && // on same sub-shapes
2163 ( _algoShape == subMesh->_algoShape ))
2165 aBuilder.Add( aCompound, S );
2166 if ( !subMesh->SubMeshesComputed() )
2167 theSubComputed = false;
2168 theSubs.push_back( subMesh );
2176 //=======================================================================
2177 //function : getSimilarAttached
2178 //purpose : return a hypothesis attached to theShape.
2179 // If theHyp is provided, similar but not same hypotheses
2180 // is returned; else only applicable ones having theHypType
2182 //=======================================================================
2184 const SMESH_Hypothesis* SMESH_subMesh::getSimilarAttached(const TopoDS_Shape& theShape,
2185 const SMESH_Hypothesis * theHyp,
2186 const int theHypType)
2188 SMESH_HypoFilter hypoKind;
2189 hypoKind.Init( hypoKind.HasType( theHyp ? theHyp->GetType() : theHypType ));
2191 hypoKind.And ( hypoKind.HasDim( theHyp->GetDim() ));
2192 hypoKind.AndNot( hypoKind.Is( theHyp ));
2193 if ( theHyp->IsAuxiliary() )
2194 hypoKind.And( hypoKind.HasName( theHyp->GetName() ));
2196 hypoKind.AndNot( hypoKind.IsAuxiliary());
2199 hypoKind.And( hypoKind.IsApplicableTo( theShape ));
2202 return _father->GetHypothesis( theShape, hypoKind, false );
2205 //=======================================================================
2206 //function : CheckConcurrentHypothesis
2207 //purpose : check if there are several applicable hypothesis attached to
2209 //=======================================================================
2211 SMESH_Hypothesis::Hypothesis_Status
2212 SMESH_subMesh::CheckConcurrentHypothesis (const int theHypType)
2214 // is there local hypothesis on me?
2215 if ( getSimilarAttached( _subShape, 0, theHypType ) )
2216 return SMESH_Hypothesis::HYP_OK;
2219 TopoDS_Shape aPrevWithHyp;
2220 const SMESH_Hypothesis* aPrevHyp = 0;
2221 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
2222 for (; it.More(); it.Next())
2224 const TopoDS_Shape& ancestor = it.Value();
2225 const SMESH_Hypothesis* hyp = getSimilarAttached( ancestor, 0, theHypType );
2228 if ( aPrevWithHyp.IsNull() || aPrevWithHyp.IsSame( ancestor ))
2230 aPrevWithHyp = ancestor;
2233 else if ( aPrevWithHyp.ShapeType() == ancestor.ShapeType() && aPrevHyp != hyp )
2234 return SMESH_Hypothesis::HYP_CONCURRENT;
2236 return SMESH_Hypothesis::HYP_OK;
2239 return SMESH_Hypothesis::HYP_OK;
2242 //================================================================================
2244 * \brief Constructor of OwnListenerData
2246 //================================================================================
2248 SMESH_subMesh::OwnListenerData::OwnListenerData( SMESH_subMesh* sm, EventListener* el):
2250 myMeshID( sm ? sm->GetFather()->GetId() : -1 ),
2251 mySubMeshID( sm ? sm->GetId() : -1 ),
2256 //================================================================================
2258 * \brief Sets an event listener and its data to a submesh
2259 * \param listener - the listener to store
2260 * \param data - the listener data to store
2261 * \param where - the submesh to store the listener and it's data
2263 * It remembers the submesh where it puts the listener in order to delete
2264 * them when HYP_OK algo_state is lost
2265 * After being set, event listener is notified on each event of where submesh.
2267 //================================================================================
2269 void SMESH_subMesh::SetEventListener(EventListener* listener,
2270 EventListenerData* data,
2271 SMESH_subMesh* where)
2273 if ( listener && where ) {
2274 where->setEventListener( listener, data );
2275 _ownListeners.push_back( OwnListenerData( where, listener ));
2279 //================================================================================
2281 * \brief Sets an event listener and its data to a submesh
2282 * \param listener - the listener to store
2283 * \param data - the listener data to store
2285 * After being set, event listener is notified on each event of a submesh.
2287 //================================================================================
2289 void SMESH_subMesh::setEventListener(EventListener* listener,
2290 EventListenerData* data)
2292 map< EventListener*, EventListenerData* >::iterator l_d =
2293 _eventListeners.find( listener );
2294 if ( l_d != _eventListeners.end() ) {
2295 EventListenerData* curData = l_d->second;
2297 if ( curData && curData != data && curData->IsDeletable() )
2302 for ( l_d = _eventListeners.begin(); l_d != _eventListeners.end(); ++l_d )
2303 if ( listener->GetName() == l_d->first->GetName() )
2305 EventListenerData* curData = l_d->second;
2307 if ( curData && curData != data && curData->IsDeletable() )
2309 if ( l_d->first != listener && l_d->first->IsDeletable() )
2311 _eventListeners.erase( l_d );
2314 _eventListeners.insert( make_pair( listener, data ));
2318 //================================================================================
2320 * \brief Return an event listener data
2321 * \param listener - the listener whose data is
2322 * \param myOwn - if \c true, returns a listener set by this sub-mesh,
2323 * else returns a listener listening to events of this sub-mesh
2324 * \retval EventListenerData* - found data, maybe NULL
2326 //================================================================================
2328 EventListenerData* SMESH_subMesh::GetEventListenerData(EventListener* listener,
2329 const bool myOwn) const
2333 list< OwnListenerData >::const_iterator d;
2334 for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
2336 if ( d->myListener == listener && _father->MeshExists( d->myMeshID ))
2337 return d->mySubMesh->GetEventListenerData( listener, !myOwn );
2342 map< EventListener*, EventListenerData* >::const_iterator l_d =
2343 _eventListeners.find( listener );
2344 if ( l_d != _eventListeners.end() )
2350 //================================================================================
2352 * \brief Return an event listener data
2353 * \param listenerName - the listener name
2354 * \param myOwn - if \c true, returns a listener set by this sub-mesh,
2355 * else returns a listener listening to events of this sub-mesh
2356 * \retval EventListenerData* - found data, maybe NULL
2358 //================================================================================
2360 EventListenerData* SMESH_subMesh::GetEventListenerData(const string& listenerName,
2361 const bool myOwn) const
2365 list< OwnListenerData >::const_iterator d;
2366 for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
2368 if ( _father->MeshExists( d->myMeshID ) && listenerName == d->myListener->GetName())
2369 return d->mySubMesh->GetEventListenerData( listenerName, !myOwn );
2374 map< EventListener*, EventListenerData* >::const_iterator l_d = _eventListeners.begin();
2375 for ( ; l_d != _eventListeners.end(); ++l_d )
2376 if ( listenerName == l_d->first->GetName() )
2382 //================================================================================
2384 * \brief Notify stored event listeners on the occurred event
2385 * \param event - algo_event or compute_event itself
2386 * \param eventType - algo_event or compute_event
2387 * \param hyp - hypothesis, if eventType is algo_event
2389 //================================================================================
2391 void SMESH_subMesh::notifyListenersOnEvent( const int event,
2392 const event_type eventType,
2393 SMESH_Hypothesis* hyp)
2395 list< pair< EventListener*, EventListenerData* > > eventListeners( _eventListeners.begin(),
2396 _eventListeners.end());
2397 list< pair< EventListener*, EventListenerData* > >::iterator l_d = eventListeners.begin();
2398 for ( ; l_d != eventListeners.end(); ++l_d )
2400 std::pair< EventListener*, EventListenerData* > li_da = *l_d;
2401 if ( !_eventListeners.count( li_da.first )) continue;
2403 if ( li_da.first->myBusySM.insert( this ).second )
2405 const bool isDeletable = li_da.first->IsDeletable();
2407 li_da.first->ProcessEvent( event, eventType, this, li_da.second, hyp );
2409 if ( !isDeletable || _eventListeners.count( li_da.first ))
2410 li_da.first->myBusySM.erase( this ); // a listener is hopefully not dead
2415 //================================================================================
2417 * \brief Unregister the listener and delete listener's data
2418 * \param listener - the event listener
2420 //================================================================================
2422 void SMESH_subMesh::DeleteEventListener(EventListener* listener)
2424 map< EventListener*, EventListenerData* >::iterator l_d =
2425 _eventListeners.find( listener );
2426 if ( l_d != _eventListeners.end() && l_d->first )
2428 if ( l_d->second && l_d->second->IsDeletable() )
2432 l_d->first->myBusySM.erase( this );
2433 if ( l_d->first->IsDeletable() )
2435 l_d->first->BeforeDelete( this, l_d->second );
2438 _eventListeners.erase( l_d );
2442 //================================================================================
2444 * \brief Delete event listeners depending on algo of this submesh
2446 //================================================================================
2448 void SMESH_subMesh::deleteOwnListeners()
2450 list< OwnListenerData >::iterator d;
2451 for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
2453 SMESH_Mesh* mesh = _father->FindMesh( d->myMeshID );
2454 if ( !mesh || !mesh->GetSubMeshContaining( d->mySubMeshID ))
2456 d->mySubMesh->DeleteEventListener( d->myListener );
2458 _ownListeners.clear();
2461 //=======================================================================
2462 //function : loadDependentMeshes
2463 //purpose : loads dependent meshes on SUBMESH_LOADED event
2464 //=======================================================================
2466 void SMESH_subMesh::loadDependentMeshes()
2468 list< OwnListenerData >::iterator d;
2469 for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
2470 if ( _father != d->mySubMesh->_father &&
2471 _father->FindMesh( d->myMeshID ))
2472 d->mySubMesh->_father->Load();
2474 // map< EventListener*, EventListenerData* >::iterator l_d = _eventListeners.begin();
2475 // for ( ; l_d != _eventListeners.end(); ++l_d )
2476 // if ( l_d->second )
2478 // const list<SMESH_subMesh*>& smList = l_d->second->mySubMeshes;
2479 // list<SMESH_subMesh*>::const_iterator sm = smList.begin();
2480 // for ( ; sm != smList.end(); ++sm )
2481 // if ( _father != (*sm)->_father )
2482 // (*sm)->_father->Load();
2486 //================================================================================
2488 * \brief Do something on a certain event
2489 * \param event - algo_event or compute_event itself
2490 * \param eventType - algo_event or compute_event
2491 * \param subMesh - the submesh where the event occurs
2492 * \param data - listener data stored in the subMesh
2493 * \param hyp - hypothesis, if eventType is algo_event
2495 * The base implementation translates CLEAN event to the subMesh
2496 * stored in listener data. Also it sends SUBMESH_COMPUTED event in case of
2497 * successful COMPUTE event.
2499 //================================================================================
2501 void SMESH_subMeshEventListener::ProcessEvent(const int event,
2502 const int eventType,
2503 SMESH_subMesh* subMesh,
2504 EventListenerData* data,
2505 const SMESH_Hypothesis* /*hyp*/)
2507 if ( data && !data->mySubMeshes.empty() &&
2508 eventType == SMESH_subMesh::COMPUTE_EVENT)
2510 ASSERT( data->mySubMeshes.front() != subMesh );
2511 list<SMESH_subMesh*>::iterator smIt = data->mySubMeshes.begin();
2512 list<SMESH_subMesh*>::iterator smEnd = data->mySubMeshes.end();
2514 case SMESH_subMesh::CLEAN:
2515 for ( ; smIt != smEnd; ++ smIt)
2516 (*smIt)->ComputeStateEngine( SMESH_subMesh::compute_event( event ));
2518 case SMESH_subMesh::COMPUTE:
2519 case SMESH_subMesh::COMPUTE_SUBMESH:
2520 if ( subMesh->GetComputeState() == SMESH_subMesh::COMPUTE_OK )
2521 for ( ; smIt != smEnd; ++ smIt)
2522 (*smIt)->ComputeStateEngine( SMESH_subMesh::SUBMESH_COMPUTED );
2531 //================================================================================
2533 * \brief Iterator over submeshes and optionally prepended or appended one
2535 //================================================================================
2537 struct _Iterator : public SMDS_Iterator<SMESH_subMesh*>
2539 _Iterator(SMDS_Iterator<SMESH_subMesh*>* subIt,
2540 SMESH_subMesh* prepend,
2541 SMESH_subMesh* append): myAppend(append), myIt(subIt)
2543 myCur = prepend ? prepend : myIt->more() ? myIt->next() : append;
2544 if ( myCur == append ) append = 0;
2546 /// Return true if and only if there are other object in this iterator
2551 /// Return the current object and step to the next one
2552 virtual SMESH_subMesh* next()
2554 SMESH_subMesh* res = myCur;
2555 if ( myIt->more() ) { myCur = myIt->next(); }
2556 else { myCur = myAppend; myAppend = 0; }
2563 SMESH_subMesh *myAppend, *myCur;
2564 SMDS_Iterator<SMESH_subMesh*> *myIt;
2568 //================================================================================
2570 * \brief Return iterator on the submeshes this one depends on
2571 * \param includeSelf - this submesh to be returned also
2572 * \param reverse - if true, complex shape submeshes go first
2574 //================================================================================
2576 SMESH_subMeshIteratorPtr SMESH_subMesh::getDependsOnIterator(const bool includeSelf,
2577 const bool reverse) const
2579 SMESH_subMesh *me = (SMESH_subMesh*) this;
2580 SMESH_subMesh *prepend=0, *append=0;
2581 if ( includeSelf ) {
2582 if ( reverse ) prepend = me;
2585 typedef map < int, SMESH_subMesh * > TMap;
2588 return SMESH_subMeshIteratorPtr
2589 ( new _Iterator( new SMDS_mapReverseIterator<TMap>( me->DependsOn() ), prepend, append ));
2592 return SMESH_subMeshIteratorPtr
2593 ( new _Iterator( new SMDS_mapIterator<TMap>( me->DependsOn() ), prepend, append ));
2597 //================================================================================
2599 * \brief Returns ancestor sub-meshes. Finds them if not yet found.
2601 //================================================================================
2603 const std::vector< SMESH_subMesh * > & SMESH_subMesh::GetAncestors() const
2605 if ( _ancestors.empty() &&
2606 !_subShape.IsSame( _father->GetShapeToMesh() ))
2608 const TopTools_ListOfShape& ancShapes = _father->GetAncestors( _subShape );
2610 SMESH_subMesh* me = const_cast< SMESH_subMesh* >( this );
2611 me->_ancestors.reserve( ancShapes.Extent() );
2613 // assure that all sub-meshes exist
2614 TopoDS_Shape mainShape = _father->GetShapeToMesh();
2615 if ( !mainShape.IsNull() )
2616 _father->GetSubMesh( mainShape )->DependsOn();
2618 TopTools_MapOfShape map;
2620 for ( TopTools_ListIteratorOfListOfShape it( ancShapes ); it.More(); it.Next() )
2621 if ( SMESH_subMesh* sm = _father->GetSubMeshContaining( it.Value() ))
2622 if ( map.Add( it.Value() ))
2623 me->_ancestors.push_back( sm );
2629 //================================================================================
2631 * \brief Clears the vector of ancestor sub-meshes
2633 //================================================================================
2635 void SMESH_subMesh::ClearAncestors()
2640 //================================================================================
2642 * \brief Find common submeshes (based on shared sub-shapes with other
2643 * \param theOther submesh to check
2644 * \param theSetOfCommon set of common submesh
2646 //================================================================================
2648 bool SMESH_subMesh::FindIntersection(const SMESH_subMesh* theOther,
2649 std::set<const SMESH_subMesh*>& theSetOfCommon ) const
2651 size_t oldNb = theSetOfCommon.size();
2653 // check main submeshes
2654 const map <int, SMESH_subMesh*>::const_iterator otherEnd = theOther->_mapDepend.end();
2655 if ( theOther->_mapDepend.find(this->GetId()) != otherEnd )
2656 theSetOfCommon.insert( this );
2657 if ( _mapDepend.find(theOther->GetId()) != _mapDepend.end() )
2658 theSetOfCommon.insert( theOther );
2660 // check common submeshes
2661 map <int, SMESH_subMesh*>::const_iterator mapIt = _mapDepend.begin();
2662 for( ; mapIt != _mapDepend.end(); mapIt++ )
2663 if ( theOther->_mapDepend.find((*mapIt).first) != otherEnd )
2664 theSetOfCommon.insert( (*mapIt).second );
2665 return oldNb < theSetOfCommon.size();