1 // Copyright (C) 2007-2016 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;
115 //=============================================================================
119 //=============================================================================
121 SMESH_subMesh::~SMESH_subMesh()
123 deleteOwnListeners();
126 //=============================================================================
130 //=============================================================================
132 int SMESH_subMesh::GetId() const
134 //MESSAGE("SMESH_subMesh::GetId");
138 //=============================================================================
142 //=============================================================================
144 SMESHDS_SubMesh * SMESH_subMesh::GetSubMeshDS()
146 // submesh appears in DS only when a mesher set nodes and elements on a shape
147 return _subMeshDS ? _subMeshDS : _subMeshDS = _father->GetMeshDS()->MeshElements(_subShape); // may be null
150 //=============================================================================
154 //=============================================================================
156 const SMESHDS_SubMesh * SMESH_subMesh::GetSubMeshDS() const
158 return ((SMESH_subMesh*) this )->GetSubMeshDS();
161 //=============================================================================
165 //=============================================================================
167 SMESHDS_SubMesh* SMESH_subMesh::CreateSubMeshDS()
169 if ( !GetSubMeshDS() ) {
170 SMESHDS_Mesh* meshDS = _father->GetMeshDS();
171 meshDS->NewSubMesh( meshDS->ShapeToIndex( _subShape ) );
173 return GetSubMeshDS();
176 //=============================================================================
180 //=============================================================================
182 SMESH_subMesh *SMESH_subMesh::GetFirstToCompute()
184 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(true,false);
185 while ( smIt->more() ) {
186 SMESH_subMesh *sm = smIt->next();
187 if ( sm->GetComputeState() == READY_TO_COMPUTE )
190 return 0; // nothing to compute
193 //================================================================================
195 * \brief Returns a current algorithm
197 //================================================================================
199 SMESH_Algo* SMESH_subMesh::GetAlgo() const
203 SMESH_subMesh* me = const_cast< SMESH_subMesh* >( this );
204 me->_algo = _father->GetGen()->GetAlgo( me );
209 //================================================================================
211 * \brief Allow algo->Compute() if a sub-shape of lower dim is meshed but
212 * none mesh entity is bound to it (PAL13615, 2nd part)
214 //================================================================================
216 void SMESH_subMesh::SetIsAlwaysComputed(bool isAlCo)
218 _alwaysComputed = isAlCo;
219 if ( _alwaysComputed )
220 _computeState = COMPUTE_OK;
222 ComputeStateEngine( CHECK_COMPUTE_STATE );
225 //=======================================================================
227 * \brief Return true if no mesh entities is bound to the submesh
229 //=======================================================================
231 bool SMESH_subMesh::IsEmpty() const
233 if (SMESHDS_SubMesh * subMeshDS = ((SMESH_subMesh*)this)->GetSubMeshDS())
234 return (!subMeshDS->NbElements() && !subMeshDS->NbNodes());
238 //=======================================================================
239 //function : IsMeshComputed
240 //purpose : check if _subMeshDS contains mesh elements
241 //=======================================================================
243 bool SMESH_subMesh::IsMeshComputed() const
245 if ( _alwaysComputed )
247 // algo may bind a sub-mesh not to _subShape, eg 3D algo
248 // sets nodes on SHELL while _subShape may be SOLID
250 SMESHDS_Mesh* meshDS = _father->GetMeshDS();
251 int dim = SMESH_Gen::GetShapeDim( _subShape );
252 int type = _subShape.ShapeType();
253 for ( ; type <= TopAbs_VERTEX; type++) {
254 if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
256 TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
257 for ( ; exp.More(); exp.Next() )
259 if ( SMESHDS_SubMesh * smDS = meshDS->MeshElements( exp.Current() ))
261 bool computed = (dim > 0) ? smDS->NbElements() : smDS->NbNodes();
274 //=============================================================================
276 * Return true if all sub-meshes have been meshed
278 //=============================================================================
280 bool SMESH_subMesh::SubMeshesComputed(bool * isFailedToCompute/*=0*/) const
282 int myDim = SMESH_Gen::GetShapeDim( _subShape );
283 int dimToCheck = myDim - 1;
284 bool subMeshesComputed = true;
285 if ( isFailedToCompute ) *isFailedToCompute = false;
286 // check sub-meshes with upper dimension => reverse iteration
287 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,true);
288 while ( smIt->more() )
290 SMESH_subMesh *sm = smIt->next();
291 if ( sm->_alwaysComputed )
293 const TopoDS_Shape & ss = sm->GetSubShape();
295 // MSV 07.04.2006: restrict checking to myDim-1 only. Ex., there is no sense
296 // in checking of existence of edges if the algo needs only faces. Moreover,
297 // degenerated edges may have no sub-mesh, as after computing NETGEN_2D.
298 if ( !_algo || _algo->NeedDiscreteBoundary() ) {
299 int dim = SMESH_Gen::GetShapeDim( ss );
300 if (dim < dimToCheck)
301 break; // the rest sub-meshes are all of less dimension
303 SMESHDS_SubMesh * ds = sm->GetSubMeshDS();
304 bool computeOk = ((sm->GetComputeState() == COMPUTE_OK ) ||
305 (ds && ( dimToCheck ? ds->NbElements() : ds->NbNodes() )));
308 subMeshesComputed = false;
310 if ( isFailedToCompute && !(*isFailedToCompute) )
311 *isFailedToCompute = ( sm->GetComputeState() == FAILED_TO_COMPUTE );
313 if ( !isFailedToCompute )
317 return subMeshesComputed;
320 //================================================================================
322 * \brief Return cost of computing this sub-mesh. If hypotheses are not well defined,
324 * \return int - the computation cost in abstract units.
326 //================================================================================
328 int SMESH_subMesh::GetComputeCost() const
330 return _realComputeCost;
333 //================================================================================
335 * \brief Return cost of computing this sub-mesh. The cost depends on the shape type
336 * and number of sub-meshes this one DependsOn().
337 * \return int - the computation cost in abstract units.
339 //================================================================================
341 int SMESH_subMesh::computeCost() const
346 switch ( _subShape.ShapeType() ) {
348 case TopAbs_SHELL: computeCost = 5000; break;
349 case TopAbs_FACE: computeCost = 500; break;
350 case TopAbs_EDGE: computeCost = 2; break;
351 default: computeCost = 1;
353 SMESH_subMeshIteratorPtr childIt = getDependsOnIterator(/*includeSelf=*/false);
354 while ( childIt->more() )
355 computeCost += childIt->next()->computeCost();
357 ((SMESH_subMesh*)this)->_computeCost = computeCost;
362 //=============================================================================
364 * Returns all sub-meshes this one depend on
366 //=============================================================================
368 const map < int, SMESH_subMesh * >& SMESH_subMesh::DependsOn()
370 if ( _dependenceAnalysed || !_father->HasShapeToMesh() )
373 int type = _subShape.ShapeType();
376 case TopAbs_COMPOUND:
378 list< TopoDS_Shape > compounds( 1, _subShape );
379 list< TopoDS_Shape >::iterator comp = compounds.begin();
380 for ( ; comp != compounds.end(); ++comp )
382 for ( TopoDS_Iterator sub( *comp ); sub.More(); sub.Next() )
383 switch ( sub.Value().ShapeType() )
385 case TopAbs_COMPOUND: compounds.push_back( sub.Value() ); break;
386 case TopAbs_COMPSOLID: insertDependence( sub.Value(), TopAbs_SOLID ); break;
387 case TopAbs_SOLID: insertDependence( sub.Value(), TopAbs_SOLID ); break;
388 case TopAbs_SHELL: insertDependence( sub.Value(), TopAbs_FACE ); break;
389 case TopAbs_FACE: insertDependence( sub.Value(), TopAbs_FACE ); break;
390 case TopAbs_WIRE: insertDependence( sub.Value(), TopAbs_EDGE ); break;
391 case TopAbs_EDGE: insertDependence( sub.Value(), TopAbs_EDGE ); break;
392 case TopAbs_VERTEX: insertDependence( sub.Value(), TopAbs_VERTEX ); break;
398 case TopAbs_COMPSOLID: insertDependence( _subShape, TopAbs_SOLID ); break;
399 case TopAbs_SOLID: insertDependence( _subShape, TopAbs_FACE );
400 { /*internal EDGE*/ insertDependence( _subShape, TopAbs_EDGE, TopAbs_WIRE ); break; }
401 case TopAbs_SHELL: insertDependence( _subShape, TopAbs_FACE ); break;
402 case TopAbs_FACE: insertDependence( _subShape, TopAbs_EDGE ); break;
403 case TopAbs_WIRE: insertDependence( _subShape, TopAbs_EDGE ); break;
404 case TopAbs_EDGE: insertDependence( _subShape, TopAbs_VERTEX ); break;
407 _dependenceAnalysed = true;
411 //================================================================================
413 * \brief Return a key for SMESH_subMesh::_mapDepend map
415 //================================================================================
419 int dependsOnMapKey( TopAbs_ShapeEnum type, int shapeID )
421 int ordType = 9 - int(type); // 2 = Vertex, 8 = CompSolid
423 cle += 10000000 * ordType; // sort map by ordType then index
426 int dependsOnMapKey( const SMESH_subMesh* sm )
428 return dependsOnMapKey( sm->GetSubShape().ShapeType(), sm->GetId() );
432 //=============================================================================
434 * Add sub-meshes on sub-shapes of a given type into the dependence map.
436 //=============================================================================
438 void SMESH_subMesh::insertDependence(const TopoDS_Shape aShape,
439 TopAbs_ShapeEnum aSubType,
440 TopAbs_ShapeEnum avoidType)
442 TopExp_Explorer sub( aShape, aSubType, avoidType );
443 for ( ; sub.More(); sub.Next() )
445 SMESH_subMesh *aSubMesh = _father->GetSubMesh( sub.Current() );
446 if ( aSubMesh->GetId() == 0 )
447 continue; // not a sub-shape of the shape to mesh
448 int cle = dependsOnMapKey( aSubMesh );
449 if ( _mapDepend.find( cle ) == _mapDepend.end())
451 _mapDepend[cle] = aSubMesh;
452 const map < int, SMESH_subMesh * > & subMap = aSubMesh->DependsOn();
453 _mapDepend.insert( subMap.begin(), subMap.end() );
458 //================================================================================
460 * \brief Return \c true if \a this sub-mesh depends on \a other
462 //================================================================================
464 bool SMESH_subMesh::DependsOn( const SMESH_subMesh* other ) const
466 return other ? _mapDepend.count( dependsOnMapKey( other )) : false;
469 //================================================================================
471 * \brief Return \c true if \a this sub-mesh depends on a \a shape
473 //================================================================================
475 bool SMESH_subMesh::DependsOn( const int shapeID ) const
477 return DependsOn( _father->GetSubMeshContaining( shapeID ));
480 //=============================================================================
482 * Return a shape of \a this sub-mesh
484 //=============================================================================
486 const TopoDS_Shape & SMESH_subMesh::GetSubShape() const
491 //=======================================================================
492 //function : CanAddHypothesis
493 //purpose : return true if theHypothesis can be attached to me:
494 // its dimension is checked
495 //=======================================================================
497 bool SMESH_subMesh::CanAddHypothesis(const SMESH_Hypothesis* theHypothesis) const
499 int aHypDim = theHypothesis->GetDim();
500 int aShapeDim = SMESH_Gen::GetShapeDim(_subShape);
501 // issue 21106. Forbid 3D mesh on the SHELL
502 // if (aHypDim == 3 && aShapeDim == 3) {
503 // // check case of open shell
504 // //if (_subShape.ShapeType() == TopAbs_SHELL && !_subShape.Closed())
505 // if (_subShape.ShapeType() == TopAbs_SHELL && !BRep_Tool::IsClosed(_subShape))
508 if ( aHypDim <= aShapeDim )
514 //=======================================================================
515 //function : IsApplicableHypothesis
516 //purpose : check if this sub-mesh can be computed using a hypothesis
517 //=======================================================================
519 bool SMESH_subMesh::IsApplicableHypothesis(const SMESH_Hypothesis* theHypothesis) const
521 if ( !_father->HasShapeToMesh() && _subShape.ShapeType() == TopAbs_SOLID )
522 return true; // true for the PseudoShape
524 return IsApplicableHypothesis( theHypothesis, _subShape.ShapeType() );
527 //=======================================================================
528 //function : IsApplicableHypothesis
529 //purpose : compare shape type and hypothesis type
530 //=======================================================================
532 bool SMESH_subMesh::IsApplicableHypothesis(const SMESH_Hypothesis* theHypothesis,
533 const TopAbs_ShapeEnum theShapeType)
535 if ( theHypothesis->GetType() > SMESHDS_Hypothesis::PARAM_ALGO)
538 if ( theHypothesis->GetShapeType() & (1<< theShapeType))
539 // issue 21106. Forbid 3D mesh on the SHELL
540 return !( theHypothesis->GetDim() == 3 && theShapeType == TopAbs_SHELL );
546 switch ( theShapeType ) {
551 return SMESH_Gen::GetShapeDim( theShapeType ) == theHypothesis->GetDim();
554 // Special case for algorithms, building 2D mesh on a whole shell.
555 // Before this fix there was a problem after restoring from study,
556 // because in that case algorithm is assigned before hypothesis
557 // (on shell in problem case) and hypothesis is checked on faces
558 // (because it is 2D), where we have NO_ALGO state.
559 // Now 2D hypothesis is also applicable to shells.
560 return (theHypothesis->GetDim() == 2 || theHypothesis->GetDim() == 3);
563 // case TopAbs_COMPSOLID:
564 // case TopAbs_COMPOUND:
570 //================================================================================
572 * \brief Treats modification of hypotheses definition
573 * \param [in] event - what happens
574 * \param [in] anHyp - a hypothesis
575 * \return SMESH_Hypothesis::Hypothesis_Status - a treatment result.
577 * Optional description of a problematic situation (if any) can be retrieved
578 * via GetComputeError().
580 //================================================================================
582 SMESH_Hypothesis::Hypothesis_Status
583 SMESH_subMesh::AlgoStateEngine(algo_event event, SMESH_Hypothesis * anHyp)
585 // **** les retour des evenement shape sont significatifs
586 // (add ou remove fait ou non)
587 // le retour des evenement father n'indiquent pas que add ou remove fait
589 SMESH_Hypothesis::Hypothesis_Status aux_ret, ret = SMESH_Hypothesis::HYP_OK;
590 if ( _Id == 0 ) return ret; // not a sub-shape of the shape to mesh
592 SMESHDS_Mesh* meshDS =_father->GetMeshDS();
593 SMESH_Algo* algo = 0;
596 if (_subShape.ShapeType() == TopAbs_VERTEX )
598 if ( anHyp->GetDim() != 0) {
599 if (event == ADD_HYP || event == ADD_ALGO)
600 return SMESH_Hypothesis::HYP_BAD_DIM;
602 return SMESH_Hypothesis::HYP_OK;
605 else if ( _algoState == HYP_OK ) {
606 // update default _algoState
607 if ( event != REMOVE_FATHER_ALGO )
609 _algoState = NO_ALGO;
612 _algoState = MISSING_HYP;
613 if ( event == REMOVE_FATHER_HYP ||
614 algo->CheckHypothesis(*_father,_subShape, aux_ret))
621 int oldAlgoState = _algoState;
622 bool modifiedHyp = (event == MODIF_HYP); // if set to true, force event MODIF_ALGO_STATE
623 SMESH_Algo* algoRequiringCleaning = 0;
625 bool isApplicableHyp = IsApplicableHypothesis( anHyp );
627 if (event == ADD_ALGO || event == ADD_FATHER_ALGO)
629 // -------------------------------------------
630 // check if a shape needed by algo is present
631 // -------------------------------------------
632 algo = static_cast< SMESH_Algo* >( anHyp );
633 if ( !_father->HasShapeToMesh() && algo->NeedShape() )
634 return SMESH_Hypothesis::HYP_NEED_SHAPE;
635 // ----------------------
636 // check mesh conformity
637 // ----------------------
638 if (isApplicableHyp && !_father->IsNotConformAllowed() && !IsConform( algo ))
639 return SMESH_Hypothesis::HYP_NOTCONFORM;
641 // check if all-dimensional algo is hidden by other local one
642 if ( event == ADD_ALGO ) {
643 SMESH_HypoFilter filter( SMESH_HypoFilter::HasType( algo->GetType() ));
644 filter.Or( SMESH_HypoFilter::HasType( algo->GetType()+1 ));
645 filter.Or( SMESH_HypoFilter::HasType( algo->GetType()+2 ));
646 if ( SMESH_Algo * curAlgo = (SMESH_Algo*)_father->GetHypothesis( this, filter, true ))
647 if ( !curAlgo->NeedDiscreteBoundary() && curAlgo != anHyp )
648 algoRequiringCleaning = curAlgo;
652 // ----------------------------------
653 // add a hypothesis to DS if possible
654 // ----------------------------------
655 if (event == ADD_HYP || event == ADD_ALGO)
657 if ( ! CanAddHypothesis( anHyp )) // check dimension
658 return SMESH_Hypothesis::HYP_BAD_DIM;
660 if ( !anHyp->IsAuxiliary() && getSimilarAttached( _subShape, anHyp ) )
661 return SMESH_Hypothesis::HYP_ALREADY_EXIST;
663 if ( !meshDS->AddHypothesis(_subShape, anHyp))
664 return SMESH_Hypothesis::HYP_ALREADY_EXIST;
667 // --------------------------
668 // remove a hypothesis from DS
669 // --------------------------
670 if (event == REMOVE_HYP || event == REMOVE_ALGO)
672 if (!meshDS->RemoveHypothesis(_subShape, anHyp))
673 return SMESH_Hypothesis::HYP_OK; // nothing changes
675 if (event == REMOVE_ALGO)
677 algo = dynamic_cast<SMESH_Algo*> (anHyp);
678 if (!algo->NeedDiscreteBoundary())
679 algoRequiringCleaning = algo;
683 // ------------------
684 // analyse algo state
685 // ------------------
686 if (!isApplicableHyp)
687 return ret; // not applicable hypotheses do not change algo state
689 if (( algo = GetAlgo()))
690 algo->InitComputeError();
695 // ----------------------------------------------------------------------
704 if (algo->CheckHypothesis((*_father),_subShape, aux_ret))
705 setAlgoState(HYP_OK);
706 else if ( algo->IsStatusFatal( aux_ret )) {
707 meshDS->RemoveHypothesis(_subShape, anHyp);
711 setAlgoState(MISSING_HYP);
718 case ADD_FATHER_ALGO: { // Algo just added in father
721 if ( algo == anHyp ) {
722 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret))
723 setAlgoState(HYP_OK);
725 setAlgoState(MISSING_HYP);
729 case REMOVE_FATHER_HYP:
731 case REMOVE_FATHER_ALGO: {
735 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
736 setAlgoState(HYP_OK);
738 setAlgoState(MISSING_HYP);
742 case MODIF_HYP: break;
749 // ----------------------------------------------------------------------
757 if ( algo->CheckHypothesis((*_father),_subShape, ret ))
758 setAlgoState(HYP_OK);
759 if (SMESH_Hypothesis::IsStatusFatal( ret ))
760 meshDS->RemoveHypothesis(_subShape, anHyp);
761 else if (!_father->IsUsedHypothesis( anHyp, this ))
763 meshDS->RemoveHypothesis(_subShape, anHyp);
764 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
768 case ADD_ALGO: { //already existing algo : on father ?
771 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))// ignore hyp status
772 setAlgoState(HYP_OK);
773 else if ( algo->IsStatusFatal( aux_ret )) {
774 meshDS->RemoveHypothesis(_subShape, anHyp);
778 setAlgoState(MISSING_HYP);
783 case REMOVE_ALGO: { // perhaps a father algo applies ?
785 if (algo == NULL) // no more algo applying on sub-shape...
787 setAlgoState(NO_ALGO);
791 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
792 setAlgoState(HYP_OK);
794 setAlgoState(MISSING_HYP);
798 case MODIF_HYP: // assigned hypothesis value may become good
799 case ADD_FATHER_HYP: {
802 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
803 setAlgoState(HYP_OK);
805 setAlgoState(MISSING_HYP);
808 case ADD_FATHER_ALGO: { // new father algo
811 if ( algo == anHyp ) {
812 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
813 setAlgoState(HYP_OK);
815 setAlgoState(MISSING_HYP);
819 case REMOVE_FATHER_HYP: // nothing to do
821 case REMOVE_FATHER_ALGO: {
823 if (algo == NULL) // no more applying algo on father
825 setAlgoState(NO_ALGO);
829 if ( algo->CheckHypothesis((*_father),_subShape , aux_ret ))
830 setAlgoState(HYP_OK);
832 setAlgoState(MISSING_HYP);
842 // ----------------------------------------------------------------------
850 if (!algo->CheckHypothesis((*_father),_subShape, ret ))
852 if ( !SMESH_Hypothesis::IsStatusFatal( ret ))
853 // ret should be fatal: anHyp was not added
854 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
856 else if (!_father->IsUsedHypothesis( anHyp, this ))
857 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
859 if (SMESH_Hypothesis::IsStatusFatal( ret ))
861 MESSAGE("do not add extra hypothesis");
862 meshDS->RemoveHypothesis(_subShape, anHyp);
870 case ADD_ALGO: { //already existing algo : on father ?
872 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
873 // check if algo changes
875 f.Init( SMESH_HypoFilter::IsAlgo() );
876 f.And( SMESH_HypoFilter::IsApplicableTo( _subShape ));
877 f.AndNot( SMESH_HypoFilter::Is( algo ));
878 const SMESH_Hypothesis * prevAlgo = _father->GetHypothesis( this, f, true );
880 string( algo->GetName()) != prevAlgo->GetName())
884 setAlgoState(MISSING_HYP);
890 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
891 setAlgoState(HYP_OK);
893 setAlgoState(MISSING_HYP);
897 case REMOVE_ALGO: { // perhaps a father algo applies ?
899 if (algo == NULL) // no more algo applying on sub-shape...
901 setAlgoState(NO_ALGO);
905 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
906 // check if algo remains
907 if ( anHyp != algo && strcmp( anHyp->GetName(), algo->GetName()) )
911 setAlgoState(MISSING_HYP);
915 case MODIF_HYP: // hypothesis value may become bad
916 case ADD_FATHER_HYP: { // new father hypothesis ?
919 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
921 if (_father->IsUsedHypothesis( anHyp, this )) // new Hyp
925 setAlgoState(MISSING_HYP);
928 case ADD_FATHER_ALGO: {
930 if ( algo == anHyp ) { // a new algo on father
931 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
932 // check if algo changes
934 f.Init( SMESH_HypoFilter::IsAlgo() );
935 f.And( SMESH_HypoFilter::IsApplicableTo( _subShape ));
936 f.AndNot( SMESH_HypoFilter::Is( algo ));
937 const SMESH_Hypothesis* prevAlgo = _father->GetHypothesis( this, f, true );
939 string(algo->GetName()) != string(prevAlgo->GetName()) )
943 setAlgoState(MISSING_HYP);
947 case REMOVE_FATHER_HYP: {
950 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
951 // is there the same local hyp or maybe a new father algo applied?
952 if ( !getSimilarAttached( _subShape, anHyp ) )
956 setAlgoState(MISSING_HYP);
959 case REMOVE_FATHER_ALGO: {
960 // IPAL21346. Edges not removed when Netgen 1d-2d is removed from a SOLID.
961 // CLEAN was not called at event REMOVE_ALGO because the algo is not applicable to SOLID.
962 algo = dynamic_cast<SMESH_Algo*> (anHyp);
963 if (!algo->NeedDiscreteBoundary())
964 algoRequiringCleaning = algo;
966 if (algo == NULL) // no more applying algo on father
968 setAlgoState(NO_ALGO);
972 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
973 // check if algo changes
974 if ( string(algo->GetName()) != string( anHyp->GetName()) )
978 setAlgoState(MISSING_HYP);
988 // ----------------------------------------------------------------------
995 // detect algorithm hiding
997 if ( ret == SMESH_Hypothesis::HYP_OK &&
998 ( event == ADD_ALGO || event == ADD_FATHER_ALGO ) && algo &&
999 algo->GetName() == anHyp->GetName() )
1002 SMESH_Gen* gen = _father->GetGen();
1003 const std::vector< SMESH_subMesh * > & ancestors = GetAncestors();
1004 for ( size_t iA = 0; ( ret == SMESH_Hypothesis::HYP_OK && iA < ancestors.size()); ++iA ) {
1005 if ( SMESH_Algo* upperAlgo = ancestors[ iA ]->GetAlgo() )
1006 if ( !upperAlgo->NeedDiscreteBoundary() && !upperAlgo->SupportSubmeshes())
1007 ret = SMESH_Hypothesis::HYP_HIDDEN_ALGO;
1010 if ( ret == SMESH_Hypothesis::HYP_OK &&
1011 !algo->NeedDiscreteBoundary() &&
1012 !algo->SupportSubmeshes())
1014 TopoDS_Shape algoAssignedTo, otherAssignedTo;
1015 gen->GetAlgo( this, &algoAssignedTo );
1016 map<int, SMESH_subMesh*>::reverse_iterator i_sm = _mapDepend.rbegin();
1017 for ( ; ( ret == SMESH_Hypothesis::HYP_OK && i_sm != _mapDepend.rend()) ; ++i_sm )
1018 if ( gen->GetAlgo( i_sm->second, &otherAssignedTo ) &&
1019 SMESH_MesherHelper::IsSubShape( /*sub=*/otherAssignedTo, /*main=*/algoAssignedTo ))
1020 ret = SMESH_Hypothesis::HYP_HIDING_ALGO;
1024 if ( _algo ) { // get an error description set by _algo->CheckHypothesis()
1025 _computeError = _algo->GetComputeError();
1026 _algo->InitComputeError();
1029 bool stateChange = ( _algoState != oldAlgoState );
1031 if ( stateChange && _algoState == HYP_OK ) // hyp becomes OK
1032 algo->SetEventListener( this );
1034 if ( event == REMOVE_ALGO || event == REMOVE_FATHER_ALGO )
1037 notifyListenersOnEvent( event, ALGO_EVENT, anHyp );
1039 if ( stateChange && oldAlgoState == HYP_OK ) { // hyp becomes KO
1040 deleteOwnListeners();
1041 SetIsAlwaysComputed( false );
1042 if (_subShape.ShapeType() == TopAbs_VERTEX ) {
1043 // restore default states
1044 _algoState = HYP_OK;
1045 _computeState = READY_TO_COMPUTE;
1049 if ( algoRequiringCleaning ) {
1050 // added or removed algo is all-dimensional
1051 ComputeStateEngine( CLEAN );
1052 cleanDependsOn( algoRequiringCleaning );
1053 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1056 if ( stateChange || modifiedHyp )
1057 ComputeStateEngine( MODIF_ALGO_STATE );
1059 _realComputeCost = ( _algoState == HYP_OK ) ? computeCost() : 0;
1064 //=======================================================================
1065 //function : IsConform
1066 //purpose : check if a conform mesh will be produced by the Algo
1067 //=======================================================================
1069 bool SMESH_subMesh::IsConform(const SMESH_Algo* theAlgo)
1071 // MESSAGE( "SMESH_subMesh::IsConform" );
1072 if ( !theAlgo ) return false;
1074 // Suppose that theAlgo is applicable to _subShape, do not check it here
1075 //if ( !IsApplicableHypothesis( theAlgo )) return false;
1077 // check only algo that doesn't NeedDiscreteBoundary(): because mesh made
1078 // on a sub-shape will be ignored by theAlgo
1079 if ( theAlgo->NeedDiscreteBoundary() ||
1080 !theAlgo->OnlyUnaryInput() ) // all adjacent shapes will be meshed by this algo?
1083 // only local algo is to be checked
1084 //if ( gen->IsGlobalHypothesis( theAlgo, *_father ))
1085 if ( _subShape.ShapeType() == _father->GetMeshDS()->ShapeToMesh().ShapeType() )
1088 // check algo attached to adjacent shapes
1090 // loop on one level down sub-meshes
1091 TopoDS_Iterator itsub( _subShape );
1092 for (; itsub.More(); itsub.Next())
1094 // loop on adjacent subShapes
1095 const std::vector< SMESH_subMesh * > & ancestors = GetAncestors();
1096 for ( size_t iA = 0; iA < ancestors.size(); ++iA )
1098 const TopoDS_Shape& adjacent = ancestors[ iA ]->GetSubShape();
1099 if ( _subShape.IsSame( adjacent )) continue;
1100 if ( adjacent.ShapeType() != _subShape.ShapeType())
1103 // check algo attached to smAdjacent
1104 SMESH_Algo * algo = ancestors[ iA ]->GetAlgo();
1106 !algo->NeedDiscreteBoundary() &&
1107 algo->OnlyUnaryInput())
1108 return false; // NOT CONFORM MESH WILL BE PRODUCED
1115 //=============================================================================
1119 //=============================================================================
1121 void SMESH_subMesh::setAlgoState(algo_state state)
1126 //================================================================================
1128 * \brief Send an event to sub-meshes
1129 * \param [in] event - the event
1130 * \param [in] anHyp - an hypothesis
1131 * \param [in] exitOnFatal - to stop iteration on sub-meshes if a sub-mesh
1132 * reports a fatal result
1133 * \return SMESH_Hypothesis::Hypothesis_Status - the worst result
1135 * Optional description of a problematic situation (if any) can be retrieved
1136 * via GetComputeError().
1138 //================================================================================
1140 SMESH_Hypothesis::Hypothesis_Status
1141 SMESH_subMesh::SubMeshesAlgoStateEngine(algo_event event,
1142 SMESH_Hypothesis * anHyp,
1145 SMESH_Hypothesis::Hypothesis_Status ret = SMESH_Hypothesis::HYP_OK;
1146 //EAP: a wire (dim==1) should notify edges (dim==1)
1147 //EAP: int dim = SMESH_Gen::GetShapeDim(_subShape);
1148 //if (_subShape.ShapeType() < TopAbs_EDGE ) // wire,face etc
1150 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1151 while ( smIt->more() ) {
1152 SMESH_subMesh* sm = smIt->next();
1153 SMESH_Hypothesis::Hypothesis_Status ret2 = sm->AlgoStateEngine(event, anHyp);
1157 _computeError = sm->_computeError;
1158 sm->_computeError.reset();
1159 if ( exitOnFatal && SMESH_Hypothesis::IsStatusFatal( ret ))
1167 //================================================================================
1169 * \brief Remove elements from sub-meshes.
1170 * \param algoRequiringCleaning - an all-dimensional algorithm whose presence
1171 * causes the cleaning.
1173 //================================================================================
1175 void SMESH_subMesh::cleanDependsOn( SMESH_Algo* algoRequiringCleaning/*=0*/ )
1177 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,
1178 /*complexShapeFirst=*/true);
1179 if ( _father->NbNodes() == 0 )
1181 while ( smIt->more() )
1182 smIt->next()->ComputeStateEngine(CHECK_COMPUTE_STATE);
1184 else if ( !algoRequiringCleaning || !algoRequiringCleaning->SupportSubmeshes() )
1186 while ( smIt->more() )
1187 smIt->next()->ComputeStateEngine(CLEAN);
1189 else if ( algoRequiringCleaning && algoRequiringCleaning->SupportSubmeshes() )
1191 // find sub-meshes to keep elements on
1192 set< SMESH_subMesh* > smToKeep;
1193 TopAbs_ShapeEnum prevShapeType = TopAbs_SHAPE;
1194 bool toKeepPrevShapeType = false;
1195 while ( smIt->more() )
1197 SMESH_subMesh* sm = smIt->next();
1198 sm->ComputeStateEngine(CHECK_COMPUTE_STATE);
1199 if ( !sm->IsEmpty() )
1201 const bool sameShapeType = ( prevShapeType == sm->GetSubShape().ShapeType() );
1202 bool keepSubMeshes = ( sameShapeType && toKeepPrevShapeType );
1203 if ( !sameShapeType )
1205 // check if the algo allows presence of global algos of dimension the algo
1206 // can generate it-self;
1207 // always keep a node on VERTEX, as this node can be shared by segments
1208 // lying on EDGEs not shared by the VERTEX of sm, due to MergeNodes (PAL23068)
1209 int shapeDim = SMESH_Gen::GetShapeDim( sm->GetSubShape() );
1210 keepSubMeshes = ( algoRequiringCleaning->NeedLowerHyps( shapeDim ) || shapeDim == 0 );
1211 prevShapeType = sm->GetSubShape().ShapeType();
1212 toKeepPrevShapeType = keepSubMeshes;
1214 if ( !keepSubMeshes )
1216 // look for a local algo used to mesh sm
1217 TopoDS_Shape algoShape = SMESH_MesherHelper::GetShapeOfHypothesis
1218 ( algoRequiringCleaning, _subShape, _father );
1219 SMESH_HypoFilter moreLocalAlgo;
1220 moreLocalAlgo.Init( SMESH_HypoFilter::IsMoreLocalThan( algoShape, *_father ));
1221 moreLocalAlgo.And ( SMESH_HypoFilter::IsAlgo() );
1222 bool localAlgoFound = _father->GetHypothesis( sm->_subShape, moreLocalAlgo, true );
1223 keepSubMeshes = localAlgoFound;
1225 // remember all sub-meshes of sm
1226 if ( keepSubMeshes )
1228 SMESH_subMeshIteratorPtr smIt2 = sm->getDependsOnIterator(true);
1229 while ( smIt2->more() )
1230 smToKeep.insert( smIt2->next() );
1235 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,true);
1236 while ( smIt->more() )
1238 SMESH_subMesh* sm = smIt->next();
1239 if ( !smToKeep.count( sm ))
1240 sm->ComputeStateEngine(CLEAN);
1245 //=============================================================================
1249 //=============================================================================
1251 void SMESH_subMesh::DumpAlgoState(bool isMain)
1255 const map < int, SMESH_subMesh * >&subMeshes = DependsOn();
1257 map < int, SMESH_subMesh * >::const_iterator itsub;
1258 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
1260 SMESH_subMesh *sm = (*itsub).second;
1261 sm->DumpAlgoState(false);
1264 MESSAGE("dim = " << SMESH_Gen::GetShapeDim(_subShape) <<
1265 " type of shape " << _subShape.ShapeType());
1268 case NO_ALGO : MESSAGE(" AlgoState = NO_ALGO"); break;
1269 case MISSING_HYP : MESSAGE(" AlgoState = MISSING_HYP"); break;
1270 case HYP_OK : MESSAGE(" AlgoState = HYP_OK");break;
1272 switch (_computeState)
1274 case NOT_READY : MESSAGE(" ComputeState = NOT_READY");break;
1275 case READY_TO_COMPUTE : MESSAGE(" ComputeState = READY_TO_COMPUTE");break;
1276 case COMPUTE_OK : MESSAGE(" ComputeState = COMPUTE_OK");break;
1277 case FAILED_TO_COMPUTE: MESSAGE(" ComputeState = FAILED_TO_COMPUTE");break;
1281 //================================================================================
1283 * \brief Remove nodes and elements bound to submesh
1284 * \param subMesh - submesh containing nodes and elements
1286 //================================================================================
1288 static void cleanSubMesh( SMESH_subMesh * subMesh )
1291 if (SMESHDS_SubMesh * subMeshDS = subMesh->GetSubMeshDS())
1293 SMESHDS_Mesh * meshDS = subMesh->GetFather()->GetMeshDS();
1294 int nbElems = subMeshDS->NbElements();
1296 for ( SMDS_ElemIteratorPtr ite = subMeshDS->GetElements(); ite->more(); )
1297 meshDS->RemoveFreeElement( ite->next(), subMeshDS );
1299 int nbNodes = subMeshDS->NbNodes();
1301 for ( SMDS_NodeIteratorPtr itn = subMeshDS->GetNodes(); itn->more() ; )
1303 const SMDS_MeshNode * node = itn->next();
1304 if ( node->NbInverseElements() == 0 )
1305 meshDS->RemoveFreeNode( node, subMeshDS );
1306 else // for StdMeshers_CompositeSegment_1D: node in one submesh, edge in another
1307 meshDS->RemoveNode( node );
1314 //=============================================================================
1318 //=============================================================================
1320 bool SMESH_subMesh::ComputeStateEngine(compute_event event)
1323 case MODIF_ALGO_STATE:
1325 case COMPUTE_SUBMESH:
1326 //case COMPUTE_CANCELED:
1328 //case SUBMESH_COMPUTED:
1329 //case SUBMESH_RESTORED:
1330 //case SUBMESH_LOADED:
1331 //case MESH_ENTITY_REMOVED:
1332 //case CHECK_COMPUTE_STATE:
1333 _computeError.reset(); break;
1337 if ( event == CLEAN )
1338 _alwaysComputed = false; // Unset 'true' set by MergeNodes() (issue 0022182)
1340 if (_subShape.ShapeType() == TopAbs_VERTEX)
1342 _computeState = READY_TO_COMPUTE;
1343 SMESHDS_SubMesh* smDS = GetSubMeshDS();
1344 if ( smDS && smDS->NbNodes() )
1346 if ( event == CLEAN ) {
1348 cleanSubMesh( this );
1351 _computeState = COMPUTE_OK;
1353 else if (( event == COMPUTE || event == COMPUTE_SUBMESH )
1354 && !_alwaysComputed )
1356 const TopoDS_Vertex & V = TopoDS::Vertex( _subShape );
1357 gp_Pnt P = BRep_Tool::Pnt(V);
1358 if ( SMDS_MeshNode * n = _father->GetMeshDS()->AddNode(P.X(), P.Y(), P.Z()) ) {
1359 _father->GetMeshDS()->SetNodeOnVertex(n,_Id);
1360 _computeState = COMPUTE_OK;
1363 if ( event == MODIF_ALGO_STATE )
1367 SMESH_Gen *gen = _father->GetGen();
1368 SMESH_Algo *algo = 0;
1370 SMESH_Hypothesis::Hypothesis_Status hyp_status;
1371 //algo_state oldAlgoState = (algo_state) GetAlgoState();
1373 switch (_computeState)
1376 // ----------------------------------------------------------------------
1381 case MODIF_ALGO_STATE:
1383 if (algo && !algo->NeedDiscreteBoundary())
1384 cleanDependsOn( algo ); // clean sub-meshes with event CLEAN
1385 if ( _algoState == HYP_OK )
1386 _computeState = READY_TO_COMPUTE;
1388 case COMPUTE: // nothing to do
1389 case COMPUTE_SUBMESH:
1391 case COMPUTE_CANCELED: // nothing to do
1395 removeSubMeshElementsAndNodes();
1397 case SUBMESH_COMPUTED: // nothing to do
1399 case SUBMESH_RESTORED:
1400 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1402 case MESH_ENTITY_REMOVED:
1404 case SUBMESH_LOADED:
1405 loadDependentMeshes();
1406 ComputeSubMeshStateEngine( SUBMESH_LOADED );
1408 case CHECK_COMPUTE_STATE:
1409 if ( IsMeshComputed() )
1410 _computeState = COMPUTE_OK;
1418 // ----------------------------------------------------------------------
1420 case READY_TO_COMPUTE:
1423 case MODIF_ALGO_STATE:
1424 _computeState = NOT_READY;
1428 if (!algo->NeedDiscreteBoundary())
1429 cleanDependsOn( algo ); // clean sub-meshes with event CLEAN
1430 if ( _algoState == HYP_OK )
1431 _computeState = READY_TO_COMPUTE;
1435 case COMPUTE_NOGEOM: // no geometry; can be several algos
1436 if ( !_father->HasShapeToMesh() )
1438 algo = GetAlgo(); // current algo
1441 // apply algos in the order of increasing dimension
1442 std::list< const SMESHDS_Hypothesis * > algos = _father->GetHypothesisList( _subShape );
1443 for ( int t = SMESHDS_Hypothesis::ALGO_1D; t <= SMESHDS_Hypothesis::ALGO_3D; ++t )
1445 std::list<const SMESHDS_Hypothesis *>::iterator al = algos.begin();
1446 for ( ; al != algos.end(); ++al )
1447 if ( (*al)->GetType() == t )
1449 _algo = (SMESH_Algo*) *al;
1450 _computeState = READY_TO_COMPUTE;
1451 if ( !ComputeStateEngine( COMPUTE ))
1455 _algo = algo; // restore
1460 case COMPUTE_SUBMESH:
1464 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1467 MESSAGE("***** verify compute state *****");
1468 _computeState = NOT_READY;
1469 setAlgoState(MISSING_HYP);
1472 TopoDS_Shape shape = _subShape;
1473 algo->SubMeshesToCompute().assign( 1, this );
1474 // check submeshes needed
1475 if (_father->HasShapeToMesh() ) {
1476 bool subComputed = false, subFailed = false;
1477 if (!algo->OnlyUnaryInput()) {
1478 if ( event == COMPUTE /*&&
1479 ( algo->NeedDiscreteBoundary() || algo->SupportSubmeshes() )*/)
1480 shape = getCollection( gen, algo, subComputed, subFailed, algo->SubMeshesToCompute());
1482 subComputed = SubMeshesComputed( & subFailed );
1485 subComputed = SubMeshesComputed();
1487 ret = ( algo->NeedDiscreteBoundary() ? subComputed :
1488 algo->SupportSubmeshes() ? !subFailed :
1489 ( !subComputed || _father->IsNotConformAllowed() ));
1492 _computeState = FAILED_TO_COMPUTE;
1493 if ( !algo->NeedDiscreteBoundary() && !subFailed )
1495 SMESH_ComputeError::New(COMPERR_BAD_INPUT_MESH,
1496 "Unexpected computed sub-mesh",algo);
1502 // to restore cout that may be redirected by algo
1503 std::streambuf* coutBuffer = std::cout.rdbuf();
1505 //cleanDependants(); for "UseExisting_*D" algos
1506 //removeSubMeshElementsAndNodes();
1507 loadDependentMeshes();
1509 _computeState = FAILED_TO_COMPUTE;
1510 _computeError = SMESH_ComputeError::New(COMPERR_OK,"",algo);
1514 algo->InitComputeError();
1516 MemoryReserve aMemoryReserve;
1517 SMDS_Mesh::CheckMemory();
1518 Kernel_Utils::Localizer loc;
1519 if ( !_father->HasShapeToMesh() ) // no shape
1521 SMESH_MesherHelper helper( *_father );
1522 helper.SetSubShape( shape );
1523 helper.SetElementsOnShape( true );
1524 ret = algo->Compute(*_father, &helper );
1528 ret = algo->Compute((*_father), shape);
1530 // algo can set _computeError of submesh
1531 _computeError = SMESH_ComputeError::Worst( _computeError, algo->GetComputeError() );
1533 catch ( ::SMESH_ComputeError& comperr ) {
1534 cout << " SMESH_ComputeError caught" << endl;
1535 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1536 *_computeError = comperr;
1538 catch ( std::bad_alloc& exc ) {
1539 MESSAGE("std::bad_alloc thrown inside algo->Compute()");
1540 if ( _computeError ) {
1541 _computeError->myName = COMPERR_MEMORY_PB;
1543 cleanSubMesh( this );
1546 catch ( Standard_OutOfMemory& exc ) {
1547 MESSAGE("Standard_OutOfMemory thrown inside algo->Compute()");
1548 if ( _computeError ) {
1549 _computeError->myName = COMPERR_MEMORY_PB;
1551 cleanSubMesh( this );
1552 throw std::bad_alloc();
1554 catch (Standard_Failure& ex) {
1555 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1556 _computeError->myName = COMPERR_OCC_EXCEPTION;
1557 _computeError->myComment += ex.DynamicType()->Name();
1558 if ( ex.GetMessageString() && strlen( ex.GetMessageString() )) {
1559 _computeError->myComment += ": ";
1560 _computeError->myComment += ex.GetMessageString();
1563 catch ( SALOME_Exception& S_ex ) {
1564 const int skipSalomeShift = 7; /* to skip "Salome " of
1565 "Salome Exception" prefix returned
1566 by SALOME_Exception::what() */
1567 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1568 _computeError->myName = COMPERR_SLM_EXCEPTION;
1569 _computeError->myComment = S_ex.what() + skipSalomeShift;
1571 catch ( std::exception& exc ) {
1572 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1573 _computeError->myName = COMPERR_STD_EXCEPTION;
1574 _computeError->myComment = exc.what();
1577 if ( _computeError )
1578 _computeError->myName = COMPERR_EXCEPTION;
1582 std::cout.rdbuf( coutBuffer ); // restore cout that could be redirected by algo
1584 // check if an error reported on any sub-shape
1585 bool isComputeErrorSet = !checkComputeError( algo, ret, shape );
1586 if ( isComputeErrorSet )
1588 // check if anything was built
1589 TopExp_Explorer subS(shape, _subShape.ShapeType());
1592 for (; ret && subS.More(); subS.Next())
1593 if ( !_father->GetSubMesh( subS.Current() )->IsMeshComputed() &&
1594 ( _subShape.ShapeType() != TopAbs_EDGE ||
1595 !algo->isDegenerated( TopoDS::Edge( subS.Current() ))))
1598 #ifdef PRINT_WHO_COMPUTE_WHAT
1599 for (subS.ReInit(); subS.More(); subS.Next())
1601 const std::list <const SMESHDS_Hypothesis *> & hyps =
1602 _algo->GetUsedHypothesis( *_father, _subShape );
1603 SMESH_Comment hypStr;
1604 if ( !hyps.empty() )
1606 hypStr << hyps.front()->GetName() << " ";
1607 ((SMESHDS_Hypothesis*)hyps.front())->SaveTo( hypStr.Stream() );
1610 cout << _algo->GetName()
1611 << " " << _father->GetSubMesh( subS.Current() )->GetId()
1612 << " " << hypStr << endl;
1615 // Set _computeError
1616 if ( !ret && !isComputeErrorSet )
1618 for ( subS.ReInit(); subS.More(); subS.Next() )
1620 SMESH_subMesh* sm = _father->GetSubMesh( subS.Current() );
1621 if ( !sm->IsMeshComputed() )
1623 if ( !sm->_computeError )
1624 sm->_computeError = SMESH_ComputeError::New();
1625 if ( sm->_computeError->IsOK() )
1626 sm->_computeError->myName = COMPERR_ALGO_FAILED;
1627 sm->_computeState = FAILED_TO_COMPUTE;
1628 sm->_computeError->myAlgo = algo;
1632 if ( ret && _computeError && _computeError->myName != COMPERR_WARNING )
1634 _computeError.reset();
1637 // transform errors into warnings if it is caused by mesh edition (imp 0023068)
1638 if (!ret && _father->GetIsModified() )
1640 for (subS.ReInit(); subS.More(); subS.Next())
1642 SMESH_subMesh* sm = _father->GetSubMesh( subS.Current() );
1643 if ( !sm->IsMeshComputed() && sm->_computeError )
1645 // check if there is a VERTEX w/o nodes
1646 // with READY_TO_COMPUTE state (after MergeNodes())
1647 SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(false,false);
1648 while ( smIt->more() )
1650 SMESH_subMesh * vertSM = smIt->next();
1651 if ( vertSM->_subShape.ShapeType() != TopAbs_VERTEX ) break;
1652 if ( vertSM->GetComputeState() == READY_TO_COMPUTE )
1654 SMESHDS_SubMesh * ds = vertSM->GetSubMeshDS();
1655 if ( !ds || ds->NbNodes() == 0 )
1657 sm->_computeState = READY_TO_COMPUTE;
1658 sm->_computeError->myName = COMPERR_WARNING;
1667 // send event SUBMESH_COMPUTED
1669 if ( !algo->NeedDiscreteBoundary() )
1670 // send SUBMESH_COMPUTED to dependants of all sub-meshes of shape
1671 for (subS.ReInit(); subS.More(); subS.Next())
1673 SMESH_subMesh* sm = _father->GetSubMesh( subS.Current() );
1674 SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(false,false);
1675 while ( smIt->more() ) {
1677 if ( sm->GetSubShape().ShapeType() == TopAbs_VERTEX )
1678 sm->updateDependantsState( SUBMESH_COMPUTED );
1684 updateDependantsState( SUBMESH_COMPUTED );
1688 case COMPUTE_CANCELED: // nothing to do
1692 removeSubMeshElementsAndNodes();
1693 _computeState = NOT_READY;
1697 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1699 _computeState = READY_TO_COMPUTE;
1701 setAlgoState(MISSING_HYP);
1704 case SUBMESH_COMPUTED: // nothing to do
1706 case SUBMESH_RESTORED:
1707 // check if a mesh is already computed that may
1708 // happen after retrieval from a file
1709 ComputeStateEngine( CHECK_COMPUTE_STATE );
1710 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1712 if (algo) algo->SubmeshRestored( this );
1714 case MESH_ENTITY_REMOVED:
1716 case SUBMESH_LOADED:
1717 loadDependentMeshes();
1718 ComputeSubMeshStateEngine( SUBMESH_LOADED );
1720 case CHECK_COMPUTE_STATE:
1721 if ( IsMeshComputed() )
1722 _computeState = COMPUTE_OK;
1723 else if ( _computeError && _computeError->IsKO() )
1724 _computeState = FAILED_TO_COMPUTE;
1732 // ----------------------------------------------------------------------
1737 case MODIF_ALGO_STATE:
1738 ComputeStateEngine( CLEAN );
1740 if (algo && !algo->NeedDiscreteBoundary())
1741 cleanDependsOn( algo ); // clean sub-meshes with event CLEAN
1743 case COMPUTE: // nothing to do
1745 case COMPUTE_CANCELED: // nothing to do
1748 cleanDependants(); // clean sub-meshes, dependent on this one, with event CLEAN
1749 removeSubMeshElementsAndNodes();
1750 _computeState = NOT_READY;
1751 if ( _algoState == HYP_OK )
1752 _computeState = READY_TO_COMPUTE;
1754 case SUBMESH_COMPUTED: // nothing to do
1756 case SUBMESH_RESTORED:
1757 ComputeStateEngine( CHECK_COMPUTE_STATE );
1758 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1760 if (algo) algo->SubmeshRestored( this );
1762 case MESH_ENTITY_REMOVED:
1763 updateDependantsState ( CHECK_COMPUTE_STATE );
1764 ComputeStateEngine ( CHECK_COMPUTE_STATE );
1765 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1767 case CHECK_COMPUTE_STATE:
1768 if ( !IsMeshComputed() ) {
1769 if (_algoState == HYP_OK)
1770 _computeState = READY_TO_COMPUTE;
1772 _computeState = NOT_READY;
1775 case SUBMESH_LOADED:
1776 // already treated event, thanks to which _computeState == COMPUTE_OK
1784 // ----------------------------------------------------------------------
1786 case FAILED_TO_COMPUTE:
1789 case MODIF_ALGO_STATE:
1791 ComputeStateEngine( CLEAN );
1793 if (algo && !algo->NeedDiscreteBoundary())
1794 cleanDependsOn( algo ); // clean sub-meshes with event CLEAN
1795 if (_algoState == HYP_OK)
1796 _computeState = READY_TO_COMPUTE;
1798 _computeState = NOT_READY;
1800 case COMPUTE: // nothing to do
1801 case COMPUTE_SUBMESH:
1803 case COMPUTE_CANCELED:
1806 algo->CancelCompute();
1810 cleanDependants(); // submeshes dependent on me should be cleaned as well
1811 removeSubMeshElementsAndNodes();
1813 case SUBMESH_COMPUTED: // allow retry compute
1814 if ( IsEmpty() ) // 23061
1816 if (_algoState == HYP_OK)
1817 _computeState = READY_TO_COMPUTE;
1819 _computeState = NOT_READY;
1822 case SUBMESH_RESTORED:
1823 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1825 case MESH_ENTITY_REMOVED:
1827 case CHECK_COMPUTE_STATE:
1828 if ( IsMeshComputed() )
1829 _computeState = COMPUTE_OK;
1831 if (_algoState == HYP_OK)
1832 _computeState = READY_TO_COMPUTE;
1834 _computeState = NOT_READY;
1836 // case SUBMESH_LOADED:
1844 // ----------------------------------------------------------------------
1850 notifyListenersOnEvent( event, COMPUTE_EVENT );
1856 //=============================================================================
1860 //=============================================================================
1862 bool SMESH_subMesh::Evaluate(MapShapeNbElems& aResMap)
1864 _computeError.reset();
1868 if (_subShape.ShapeType() == TopAbs_VERTEX) {
1869 vector<int> aVec(SMDSEntity_Last,0);
1870 aVec[SMDSEntity_Node] = 1;
1871 aResMap.insert(make_pair(this,aVec));
1875 //SMESH_Gen *gen = _father->GetGen();
1876 SMESH_Algo *algo = 0;
1877 SMESH_Hypothesis::Hypothesis_Status hyp_status;
1880 if( algo && !aResMap.count( this ))
1882 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1883 if (!ret) return false;
1885 if (_father->HasShapeToMesh() && algo->NeedDiscreteBoundary() )
1887 // check submeshes needed
1888 bool subMeshEvaluated = true;
1889 int dimToCheck = SMESH_Gen::GetShapeDim( _subShape ) - 1;
1890 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,/*complexShapeFirst=*/true);
1891 while ( smIt->more() && subMeshEvaluated )
1893 SMESH_subMesh* sm = smIt->next();
1894 int dim = SMESH_Gen::GetShapeDim( sm->GetSubShape() );
1895 if (dim < dimToCheck) break; // the rest subMeshes are all of less dimension
1896 const vector<int> & nbs = aResMap[ sm ];
1897 subMeshEvaluated = (std::accumulate( nbs.begin(), nbs.end(), 0 ) > 0 );
1899 if ( !subMeshEvaluated )
1902 _computeError = SMESH_ComputeError::New(COMPERR_OK,"",algo);
1904 if ( IsMeshComputed() )
1906 vector<int> & nbEntities = aResMap[ this ];
1907 nbEntities.resize( SMDSEntity_Last, 0 );
1908 if ( SMESHDS_SubMesh* sm = GetSubMeshDS() )
1910 nbEntities[ SMDSEntity_Node ] = sm->NbNodes();
1911 SMDS_ElemIteratorPtr elemIt = sm->GetElements();
1912 while ( elemIt->more() )
1913 nbEntities[ elemIt->next()->GetEntityType() ]++;
1918 ret = algo->Evaluate((*_father), _subShape, aResMap);
1920 aResMap.insert( make_pair( this,vector<int>(0)));
1927 //=======================================================================
1929 * \brief Update compute_state by _computeError and send proper events to
1930 * dependent submeshes
1931 * \retval bool - true if _computeError is NOT set
1933 //=======================================================================
1935 bool SMESH_subMesh::checkComputeError(SMESH_Algo* theAlgo,
1936 const bool theComputeOK,
1937 const TopoDS_Shape& theShape)
1939 bool noErrors = true;
1941 if ( !theShape.IsNull() )
1943 // Check state of submeshes
1944 if ( !theAlgo->NeedDiscreteBoundary())
1946 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1947 while ( smIt->more() )
1948 if ( !smIt->next()->checkComputeError( theAlgo, theComputeOK ))
1952 // Check state of neighbours
1953 if ( !theAlgo->OnlyUnaryInput() &&
1954 theShape.ShapeType() == TopAbs_COMPOUND &&
1955 !theShape.IsSame( _subShape ))
1957 for (TopoDS_Iterator subIt( theShape ); subIt.More(); subIt.Next()) {
1958 SMESH_subMesh* sm = _father->GetSubMesh( subIt.Value() );
1960 if ( !sm->checkComputeError( theAlgo, theComputeOK, sm->GetSubShape() ))
1962 updateDependantsState( SUBMESH_COMPUTED ); // send event SUBMESH_COMPUTED
1969 // Set my _computeState
1971 if ( !_computeError || _computeError->IsOK() )
1973 // no error description is set to this sub-mesh, check if any mesh is computed
1974 _computeState = IsMeshComputed() ? COMPUTE_OK : FAILED_TO_COMPUTE;
1975 if ( _computeState != COMPUTE_OK )
1977 if ( _subShape.ShapeType() == TopAbs_EDGE &&
1978 SMESH_Algo::isDegenerated( TopoDS::Edge( _subShape )) )
1979 _computeState = COMPUTE_OK;
1980 else if ( theComputeOK )
1981 _computeError = SMESH_ComputeError::New(COMPERR_NO_MESH_ON_SHAPE,"",theAlgo);
1985 if ( _computeError && !_computeError->IsOK() )
1987 if ( !_computeError->myAlgo )
1988 _computeError->myAlgo = theAlgo;
1992 text << theAlgo->GetName() << " failed on sub-shape #" << _Id << " with error ";
1993 if (_computeError->IsCommon() )
1994 text << _computeError->CommonName();
1996 text << _computeError->myName;
1997 if ( _computeError->myComment.size() > 0 )
1998 text << " \"" << _computeError->myComment << "\"";
2002 _computeState = _computeError->IsKO() ? FAILED_TO_COMPUTE : COMPUTE_OK;
2010 //=======================================================================
2011 //function : updateSubMeshState
2013 //=======================================================================
2015 void SMESH_subMesh::updateSubMeshState(const compute_state theState)
2017 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
2018 while ( smIt->more() )
2019 smIt->next()->_computeState = theState;
2022 //=======================================================================
2023 //function : ComputeSubMeshStateEngine
2025 //=======================================================================
2027 void SMESH_subMesh::ComputeSubMeshStateEngine(compute_event event, const bool includeSelf)
2029 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(includeSelf,false);
2030 while ( smIt->more() )
2031 smIt->next()->ComputeStateEngine(event);
2034 //=======================================================================
2035 //function : updateDependantsState
2037 //=======================================================================
2039 void SMESH_subMesh::updateDependantsState(const compute_event theEvent)
2041 const std::vector< SMESH_subMesh * > & ancestors = GetAncestors();
2042 for ( size_t iA = 0; iA < ancestors.size(); ++iA )
2044 ancestors[ iA ]->ComputeStateEngine( theEvent );
2048 //=======================================================================
2049 //function : cleanDependants
2051 //=======================================================================
2053 void SMESH_subMesh::cleanDependants()
2055 int dimToClean = SMESH_Gen::GetShapeDim( _subShape ) + 1;
2057 const std::vector< SMESH_subMesh * > & ancestors = GetAncestors();
2058 for ( size_t iA = 0; iA < ancestors.size(); ++iA )
2060 const TopoDS_Shape& ancestor = ancestors[ iA ]->GetSubShape();
2061 if ( SMESH_Gen::GetShapeDim( ancestor ) == dimToClean )
2063 // PAL8021. do not go upper than SOLID, else ComputeStateEngine(CLEAN)
2064 // will erase mesh on other shapes in a compound
2065 if ( ancestor.ShapeType() >= TopAbs_SOLID &&
2066 !ancestors[ iA ]->IsEmpty() ) // prevent infinite CLEAN via event lesteners
2067 ancestors[ iA ]->ComputeStateEngine(CLEAN);
2072 //=======================================================================
2073 //function : removeSubMeshElementsAndNodes
2075 //=======================================================================
2077 void SMESH_subMesh::removeSubMeshElementsAndNodes()
2079 cleanSubMesh( this );
2081 // algo may bind a submesh not to _subShape, eg 3D algo
2082 // sets nodes on SHELL while _subShape may be SOLID
2084 int dim = SMESH_Gen::GetShapeDim( _subShape );
2085 int type = _subShape.ShapeType() + 1;
2086 for ( ; type <= TopAbs_EDGE; type++) {
2087 if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
2089 TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
2090 for ( ; exp.More(); exp.Next() )
2091 cleanSubMesh( _father->GetSubMeshContaining( exp.Current() ));
2098 //=======================================================================
2099 //function : getCollection
2100 //purpose : return a shape containing all sub-shapes of the MainShape that can be
2101 // meshed at once along with _subShape
2102 //=======================================================================
2104 TopoDS_Shape SMESH_subMesh::getCollection(SMESH_Gen * theGen,
2105 SMESH_Algo* theAlgo,
2106 bool & theSubComputed,
2107 bool & theSubFailed,
2108 std::vector<SMESH_subMesh*>& theSubs)
2110 theSubComputed = SubMeshesComputed( & theSubFailed );
2112 TopoDS_Shape mainShape = _father->GetMeshDS()->ShapeToMesh();
2114 if ( mainShape.IsSame( _subShape ))
2117 const bool skipAuxHyps = false;
2118 list<const SMESHDS_Hypothesis*> aUsedHyp =
2119 theAlgo->GetUsedHypothesis( *_father, _subShape, skipAuxHyps ); // copy
2121 // put in a compound all shapes with the same hypothesis assigned
2122 // and a good ComputeState
2124 TopoDS_Compound aCompound;
2125 BRep_Builder aBuilder;
2126 aBuilder.MakeCompound( aCompound );
2130 SMESH_subMeshIteratorPtr smIt = _father->GetSubMesh( mainShape )->getDependsOnIterator(false);
2131 while ( smIt->more() )
2133 SMESH_subMesh* subMesh = smIt->next();
2134 const TopoDS_Shape& S = subMesh->_subShape;
2135 if ( S.ShapeType() != this->_subShape.ShapeType() )
2137 if ( subMesh == this )
2139 aBuilder.Add( aCompound, S );
2140 theSubs.push_back( subMesh );
2142 else if ( subMesh->GetComputeState() == READY_TO_COMPUTE )
2144 SMESH_Algo* anAlgo = subMesh->GetAlgo();
2145 if (( anAlgo->IsSameName( *theAlgo )) && // same algo
2146 ( anAlgo->GetUsedHypothesis( *_father, S, skipAuxHyps ) == aUsedHyp )) // same hyps
2148 aBuilder.Add( aCompound, S );
2149 if ( !subMesh->SubMeshesComputed() )
2150 theSubComputed = false;
2151 theSubs.push_back( subMesh );
2159 //=======================================================================
2160 //function : getSimilarAttached
2161 //purpose : return a hypothesis attached to theShape.
2162 // If theHyp is provided, similar but not same hypotheses
2163 // is returned; else only applicable ones having theHypType
2165 //=======================================================================
2167 const SMESH_Hypothesis* SMESH_subMesh::getSimilarAttached(const TopoDS_Shape& theShape,
2168 const SMESH_Hypothesis * theHyp,
2169 const int theHypType)
2171 SMESH_HypoFilter hypoKind;
2172 hypoKind.Init( hypoKind.HasType( theHyp ? theHyp->GetType() : theHypType ));
2174 hypoKind.And ( hypoKind.HasDim( theHyp->GetDim() ));
2175 hypoKind.AndNot( hypoKind.Is( theHyp ));
2176 if ( theHyp->IsAuxiliary() )
2177 hypoKind.And( hypoKind.HasName( theHyp->GetName() ));
2179 hypoKind.AndNot( hypoKind.IsAuxiliary());
2182 hypoKind.And( hypoKind.IsApplicableTo( theShape ));
2185 return _father->GetHypothesis( theShape, hypoKind, false );
2188 //=======================================================================
2189 //function : CheckConcurrentHypothesis
2190 //purpose : check if there are several applicable hypothesis attached to
2192 //=======================================================================
2194 SMESH_Hypothesis::Hypothesis_Status
2195 SMESH_subMesh::CheckConcurrentHypothesis (const int theHypType)
2197 // is there local hypothesis on me?
2198 if ( getSimilarAttached( _subShape, 0, theHypType ) )
2199 return SMESH_Hypothesis::HYP_OK;
2202 TopoDS_Shape aPrevWithHyp;
2203 const SMESH_Hypothesis* aPrevHyp = 0;
2204 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
2205 for (; it.More(); it.Next())
2207 const TopoDS_Shape& ancestor = it.Value();
2208 const SMESH_Hypothesis* hyp = getSimilarAttached( ancestor, 0, theHypType );
2211 if ( aPrevWithHyp.IsNull() || aPrevWithHyp.IsSame( ancestor ))
2213 aPrevWithHyp = ancestor;
2216 else if ( aPrevWithHyp.ShapeType() == ancestor.ShapeType() && aPrevHyp != hyp )
2217 return SMESH_Hypothesis::HYP_CONCURRENT;
2219 return SMESH_Hypothesis::HYP_OK;
2222 return SMESH_Hypothesis::HYP_OK;
2225 //================================================================================
2227 * \brief Constructor of OwnListenerData
2229 //================================================================================
2231 SMESH_subMesh::OwnListenerData::OwnListenerData( SMESH_subMesh* sm, EventListener* el):
2233 myMeshID( sm ? sm->GetFather()->GetId() : -1 ),
2234 mySubMeshID( sm ? sm->GetId() : -1 ),
2239 //================================================================================
2241 * \brief Sets an event listener and its data to a submesh
2242 * \param listener - the listener to store
2243 * \param data - the listener data to store
2244 * \param where - the submesh to store the listener and it's data
2246 * It remembers the submesh where it puts the listener in order to delete
2247 * them when HYP_OK algo_state is lost
2248 * After being set, event listener is notified on each event of where submesh.
2250 //================================================================================
2252 void SMESH_subMesh::SetEventListener(EventListener* listener,
2253 EventListenerData* data,
2254 SMESH_subMesh* where)
2256 if ( listener && where ) {
2257 where->setEventListener( listener, data );
2258 _ownListeners.push_back( OwnListenerData( where, listener ));
2262 //================================================================================
2264 * \brief Sets an event listener and its data to a submesh
2265 * \param listener - the listener to store
2266 * \param data - the listener data to store
2268 * After being set, event listener is notified on each event of a submesh.
2270 //================================================================================
2272 void SMESH_subMesh::setEventListener(EventListener* listener,
2273 EventListenerData* data)
2275 map< EventListener*, EventListenerData* >::iterator l_d =
2276 _eventListeners.find( listener );
2277 if ( l_d != _eventListeners.end() ) {
2278 EventListenerData* curData = l_d->second;
2280 if ( curData && curData != data && curData->IsDeletable() )
2285 for ( l_d = _eventListeners.begin(); l_d != _eventListeners.end(); ++l_d )
2286 if ( listener->GetName() == l_d->first->GetName() )
2288 EventListenerData* curData = l_d->second;
2290 if ( curData && curData != data && curData->IsDeletable() )
2292 if ( l_d->first != listener && l_d->first->IsDeletable() )
2294 _eventListeners.erase( l_d );
2297 _eventListeners.insert( make_pair( listener, data ));
2301 //================================================================================
2303 * \brief Return an event listener data
2304 * \param listener - the listener whose data is
2305 * \param myOwn - if \c true, returns a listener set by this sub-mesh,
2306 * else returns a listener listening to events of this sub-mesh
2307 * \retval EventListenerData* - found data, maybe NULL
2309 //================================================================================
2311 EventListenerData* SMESH_subMesh::GetEventListenerData(EventListener* listener,
2312 const bool myOwn) const
2316 list< OwnListenerData >::const_iterator d;
2317 for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
2319 if ( d->myListener == listener && _father->MeshExists( d->myMeshID ))
2320 return d->mySubMesh->GetEventListenerData( listener, !myOwn );
2325 map< EventListener*, EventListenerData* >::const_iterator l_d =
2326 _eventListeners.find( listener );
2327 if ( l_d != _eventListeners.end() )
2333 //================================================================================
2335 * \brief Return an event listener data
2336 * \param listenerName - the listener name
2337 * \param myOwn - if \c true, returns a listener set by this sub-mesh,
2338 * else returns a listener listening to events of this sub-mesh
2339 * \retval EventListenerData* - found data, maybe NULL
2341 //================================================================================
2343 EventListenerData* SMESH_subMesh::GetEventListenerData(const string& listenerName,
2344 const bool myOwn) const
2348 list< OwnListenerData >::const_iterator d;
2349 for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
2351 if ( _father->MeshExists( d->myMeshID ) && listenerName == d->myListener->GetName())
2352 return d->mySubMesh->GetEventListenerData( listenerName, !myOwn );
2357 map< EventListener*, EventListenerData* >::const_iterator l_d = _eventListeners.begin();
2358 for ( ; l_d != _eventListeners.end(); ++l_d )
2359 if ( listenerName == l_d->first->GetName() )
2365 //================================================================================
2367 * \brief Notify stored event listeners on the occurred event
2368 * \param event - algo_event or compute_event itself
2369 * \param eventType - algo_event or compute_event
2370 * \param hyp - hypothesis, if eventType is algo_event
2372 //================================================================================
2374 void SMESH_subMesh::notifyListenersOnEvent( const int event,
2375 const event_type eventType,
2376 SMESH_Hypothesis* hyp)
2378 list< pair< EventListener*, EventListenerData* > > eventListeners( _eventListeners.begin(),
2379 _eventListeners.end());
2380 list< pair< EventListener*, EventListenerData* > >::iterator l_d = eventListeners.begin();
2381 for ( ; l_d != eventListeners.end(); ++l_d )
2383 std::pair< EventListener*, EventListenerData* > li_da = *l_d;
2384 if ( !_eventListeners.count( li_da.first )) continue;
2386 if ( li_da.first->myBusySM.insert( this ).second )
2388 const bool isDeletable = li_da.first->IsDeletable();
2390 li_da.first->ProcessEvent( event, eventType, this, li_da.second, hyp );
2392 if ( !isDeletable || _eventListeners.count( li_da.first ))
2393 li_da.first->myBusySM.erase( this ); // a listener is hopefully not dead
2398 //================================================================================
2400 * \brief Unregister the listener and delete listener's data
2401 * \param listener - the event listener
2403 //================================================================================
2405 void SMESH_subMesh::DeleteEventListener(EventListener* listener)
2407 map< EventListener*, EventListenerData* >::iterator l_d =
2408 _eventListeners.find( listener );
2409 if ( l_d != _eventListeners.end() && l_d->first )
2411 if ( l_d->second && l_d->second->IsDeletable() )
2415 l_d->first->myBusySM.erase( this );
2416 if ( l_d->first->IsDeletable() )
2418 l_d->first->BeforeDelete( this, l_d->second );
2421 _eventListeners.erase( l_d );
2425 //================================================================================
2427 * \brief Delete event listeners depending on algo of this submesh
2429 //================================================================================
2431 void SMESH_subMesh::deleteOwnListeners()
2433 list< OwnListenerData >::iterator d;
2434 for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
2436 SMESH_Mesh* mesh = _father->FindMesh( d->myMeshID );
2437 if ( !mesh || !mesh->GetSubMeshContaining( d->mySubMeshID ))
2439 d->mySubMesh->DeleteEventListener( d->myListener );
2441 _ownListeners.clear();
2444 //=======================================================================
2445 //function : loadDependentMeshes
2446 //purpose : loads dependent meshes on SUBMESH_LOADED event
2447 //=======================================================================
2449 void SMESH_subMesh::loadDependentMeshes()
2451 list< OwnListenerData >::iterator d;
2452 for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
2453 if ( _father != d->mySubMesh->_father )
2454 d->mySubMesh->_father->Load();
2456 // map< EventListener*, EventListenerData* >::iterator l_d = _eventListeners.begin();
2457 // for ( ; l_d != _eventListeners.end(); ++l_d )
2458 // if ( l_d->second )
2460 // const list<SMESH_subMesh*>& smList = l_d->second->mySubMeshes;
2461 // list<SMESH_subMesh*>::const_iterator sm = smList.begin();
2462 // for ( ; sm != smList.end(); ++sm )
2463 // if ( _father != (*sm)->_father )
2464 // (*sm)->_father->Load();
2468 //================================================================================
2470 * \brief Do something on a certain event
2471 * \param event - algo_event or compute_event itself
2472 * \param eventType - algo_event or compute_event
2473 * \param subMesh - the submesh where the event occurs
2474 * \param data - listener data stored in the subMesh
2475 * \param hyp - hypothesis, if eventType is algo_event
2477 * The base implementation translates CLEAN event to the subMesh
2478 * stored in listener data. Also it sends SUBMESH_COMPUTED event in case of
2479 * successful COMPUTE event.
2481 //================================================================================
2483 void SMESH_subMeshEventListener::ProcessEvent(const int event,
2484 const int eventType,
2485 SMESH_subMesh* subMesh,
2486 EventListenerData* data,
2487 const SMESH_Hypothesis* /*hyp*/)
2489 if ( data && !data->mySubMeshes.empty() &&
2490 eventType == SMESH_subMesh::COMPUTE_EVENT)
2492 ASSERT( data->mySubMeshes.front() != subMesh );
2493 list<SMESH_subMesh*>::iterator smIt = data->mySubMeshes.begin();
2494 list<SMESH_subMesh*>::iterator smEnd = data->mySubMeshes.end();
2496 case SMESH_subMesh::CLEAN:
2497 for ( ; smIt != smEnd; ++ smIt)
2498 (*smIt)->ComputeStateEngine( SMESH_subMesh::compute_event( event ));
2500 case SMESH_subMesh::COMPUTE:
2501 case SMESH_subMesh::COMPUTE_SUBMESH:
2502 if ( subMesh->GetComputeState() == SMESH_subMesh::COMPUTE_OK )
2503 for ( ; smIt != smEnd; ++ smIt)
2504 (*smIt)->ComputeStateEngine( SMESH_subMesh::SUBMESH_COMPUTED );
2513 //================================================================================
2515 * \brief Iterator over submeshes and optionally prepended or appended one
2517 //================================================================================
2519 struct _Iterator : public SMDS_Iterator<SMESH_subMesh*>
2521 _Iterator(SMDS_Iterator<SMESH_subMesh*>* subIt,
2522 SMESH_subMesh* prepend,
2523 SMESH_subMesh* append): myAppend(append), myIt(subIt)
2525 myCur = prepend ? prepend : myIt->more() ? myIt->next() : append;
2526 if ( myCur == append ) append = 0;
2528 /// Return true if and only if there are other object in this iterator
2533 /// Return the current object and step to the next one
2534 virtual SMESH_subMesh* next()
2536 SMESH_subMesh* res = myCur;
2537 if ( myIt->more() ) { myCur = myIt->next(); }
2538 else { myCur = myAppend; myAppend = 0; }
2545 SMESH_subMesh *myAppend, *myCur;
2546 SMDS_Iterator<SMESH_subMesh*> *myIt;
2550 //================================================================================
2552 * \brief Return iterator on the submeshes this one depends on
2553 * \param includeSelf - this submesh to be returned also
2554 * \param reverse - if true, complex shape submeshes go first
2556 //================================================================================
2558 SMESH_subMeshIteratorPtr SMESH_subMesh::getDependsOnIterator(const bool includeSelf,
2559 const bool reverse) const
2561 SMESH_subMesh *me = (SMESH_subMesh*) this;
2562 SMESH_subMesh *prepend=0, *append=0;
2563 if ( includeSelf ) {
2564 if ( reverse ) prepend = me;
2567 typedef map < int, SMESH_subMesh * > TMap;
2570 return SMESH_subMeshIteratorPtr
2571 ( new _Iterator( new SMDS_mapReverseIterator<TMap>( me->DependsOn() ), prepend, append ));
2574 return SMESH_subMeshIteratorPtr
2575 ( new _Iterator( new SMDS_mapIterator<TMap>( me->DependsOn() ), prepend, append ));
2579 //================================================================================
2581 * \brief Returns ancestor sub-meshes. Finds them if not yet found.
2583 //================================================================================
2585 const std::vector< SMESH_subMesh * > & SMESH_subMesh::GetAncestors() const
2587 if ( _ancestors.empty() &&
2588 !_subShape.IsSame( _father->GetShapeToMesh() ))
2590 const TopTools_ListOfShape& ancShapes = _father->GetAncestors( _subShape );
2592 SMESH_subMesh* me = const_cast< SMESH_subMesh* >( this );
2593 me->_ancestors.reserve( ancShapes.Extent() );
2595 TopTools_MapOfShape map;
2597 // assure that all sub-meshes exist
2598 _father->GetSubMesh( _father->GetShapeToMesh() )->DependsOn();
2600 for ( TopTools_ListIteratorOfListOfShape it( ancShapes ); it.More(); it.Next() )
2601 if ( SMESH_subMesh* sm = _father->GetSubMeshContaining( it.Value() ))
2602 if ( map.Add( it.Value() ))
2603 me->_ancestors.push_back( sm );
2609 //================================================================================
2611 * \brief Clears the vector of ancestor sub-meshes
2613 //================================================================================
2615 void SMESH_subMesh::ClearAncestors()
2620 //================================================================================
2622 * \brief Find common submeshes (based on shared sub-shapes with other
2623 * \param theOther submesh to check
2624 * \param theSetOfCommon set of common submesh
2626 //================================================================================
2628 bool SMESH_subMesh::FindIntersection(const SMESH_subMesh* theOther,
2629 std::set<const SMESH_subMesh*>& theSetOfCommon ) const
2631 size_t oldNb = theSetOfCommon.size();
2633 // check main submeshes
2634 const map <int, SMESH_subMesh*>::const_iterator otherEnd = theOther->_mapDepend.end();
2635 if ( theOther->_mapDepend.find(this->GetId()) != otherEnd )
2636 theSetOfCommon.insert( this );
2637 if ( _mapDepend.find(theOther->GetId()) != _mapDepend.end() )
2638 theSetOfCommon.insert( theOther );
2640 // check common submeshes
2641 map <int, SMESH_subMesh*>::const_iterator mapIt = _mapDepend.begin();
2642 for( ; mapIt != _mapDepend.end(); mapIt++ )
2643 if ( theOther->_mapDepend.find((*mapIt).first) != otherEnd )
2644 theSetOfCommon.insert( (*mapIt).second );
2645 return oldNb < theSetOfCommon.size();