1 // Copyright (C) 2007-2015 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // SMESH SMESH : implementaion of SMESH idl descriptions
24 // File : SMESH_subMesh.cxx
25 // Author : Paul RASCLE, EDF
28 #include "SMESH_subMesh.hxx"
30 #include "SMESH_Algo.hxx"
31 #include "SMESH_Gen.hxx"
32 #include "SMESH_HypoFilter.hxx"
33 #include "SMESH_Hypothesis.hxx"
34 #include "SMESH_Mesh.hxx"
35 #include "SMESH_MesherHelper.hxx"
36 #include "SMESH_subMeshEventListener.hxx"
37 #include "SMESH_Comment.hxx"
38 #include "SMDS_SetIterator.hxx"
39 #include "SMDSAbs_ElementType.hxx"
41 #include <Basics_OCCTVersion.hxx>
43 #include "utilities.h"
45 #include "Basics_Utils.hxx"
47 #include <BRep_Builder.hxx>
48 #include <BRep_Tool.hxx>
50 #include <TopExp_Explorer.hxx>
51 #include <TopTools_IndexedMapOfShape.hxx>
52 #include <TopTools_ListIteratorOfListOfShape.hxx>
53 #include <TopTools_ListOfShape.hxx>
55 #include <TopoDS_Compound.hxx>
56 #include <TopoDS_Iterator.hxx>
59 #include <Standard_OutOfMemory.hxx>
60 #include <Standard_ErrorHandler.hxx>
66 //=============================================================================
68 * \brief Allocate some memory at construction and release it at destruction.
69 * Is used to be able to continue working after mesh generation breaks due to
72 //=============================================================================
77 MemoryReserve(): myBuf( new char[1024*1024*2] ){}
78 ~MemoryReserve() { delete [] myBuf; }
81 //=============================================================================
83 * default constructor:
85 //=============================================================================
87 SMESH_subMesh::SMESH_subMesh(int Id,
89 SMESHDS_Mesh * meshDS,
90 const TopoDS_Shape & aSubShape)
92 _subShape = aSubShape;
93 _subMeshDS = meshDS->MeshElements(_subShape); // may be null ...
96 _dependenceAnalysed = _alwaysComputed = false;
98 if (_subShape.ShapeType() == TopAbs_VERTEX)
101 _computeState = READY_TO_COMPUTE;
105 _algoState = NO_ALGO;
106 _computeState = NOT_READY;
108 _computeCost = 0; // how costly is to compute this sub-mesh
109 _realComputeCost = 0;
112 //=============================================================================
116 //=============================================================================
118 SMESH_subMesh::~SMESH_subMesh()
120 deleteOwnListeners();
123 //=============================================================================
127 //=============================================================================
129 int SMESH_subMesh::GetId() const
131 //MESSAGE("SMESH_subMesh::GetId");
135 //=============================================================================
139 //=============================================================================
141 SMESHDS_SubMesh * SMESH_subMesh::GetSubMeshDS()
143 // submesh appears in DS only when a mesher set nodes and elements on a shape
144 return _subMeshDS ? _subMeshDS : _subMeshDS = _father->GetMeshDS()->MeshElements(_subShape); // may be null
147 //=============================================================================
151 //=============================================================================
153 const SMESHDS_SubMesh * SMESH_subMesh::GetSubMeshDS() const
155 return ((SMESH_subMesh*) this )->GetSubMeshDS();
158 //=============================================================================
162 //=============================================================================
164 SMESHDS_SubMesh* SMESH_subMesh::CreateSubMeshDS()
166 if ( !GetSubMeshDS() ) {
167 SMESHDS_Mesh* meshDS = _father->GetMeshDS();
168 meshDS->NewSubMesh( meshDS->ShapeToIndex( _subShape ) );
170 return GetSubMeshDS();
173 //=============================================================================
177 //=============================================================================
179 SMESH_subMesh *SMESH_subMesh::GetFirstToCompute()
181 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(true,false);
182 while ( smIt->more() ) {
183 SMESH_subMesh *sm = smIt->next();
184 if ( sm->GetComputeState() == READY_TO_COMPUTE )
187 return 0; // nothing to compute
190 //================================================================================
192 * \brief Returns a current algorithm
194 //================================================================================
196 SMESH_Algo* SMESH_subMesh::GetAlgo() const
200 SMESH_subMesh* me = const_cast< SMESH_subMesh* >( this );
201 me->_algo = _father->GetGen()->GetAlgo( me );
206 //================================================================================
208 * \brief Allow algo->Compute() if a sub-shape of lower dim is meshed but
209 * none mesh entity is bound to it (PAL13615, 2nd part)
211 //================================================================================
213 void SMESH_subMesh::SetIsAlwaysComputed(bool isAlCo)
215 _alwaysComputed = isAlCo;
216 if ( _alwaysComputed )
217 _computeState = COMPUTE_OK;
219 ComputeStateEngine( CHECK_COMPUTE_STATE );
222 //=======================================================================
224 * \brief Return true if no mesh entities is bound to the submesh
226 //=======================================================================
228 bool SMESH_subMesh::IsEmpty() const
230 if (SMESHDS_SubMesh * subMeshDS = ((SMESH_subMesh*)this)->GetSubMeshDS())
231 return (!subMeshDS->NbElements() && !subMeshDS->NbNodes());
235 //=======================================================================
236 //function : IsMeshComputed
237 //purpose : check if _subMeshDS contains mesh elements
238 //=======================================================================
240 bool SMESH_subMesh::IsMeshComputed() const
242 if ( _alwaysComputed )
244 // algo may bind a submesh not to _subShape, eg 3D algo
245 // sets nodes on SHELL while _subShape may be SOLID
247 SMESHDS_Mesh* meshDS = _father->GetMeshDS();
248 int dim = SMESH_Gen::GetShapeDim( _subShape );
249 int type = _subShape.ShapeType();
250 for ( ; type <= TopAbs_VERTEX; type++) {
251 if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
253 TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
254 for ( ; exp.More(); exp.Next() )
256 if ( SMESHDS_SubMesh * smDS = meshDS->MeshElements( exp.Current() ))
258 bool computed = (dim > 0) ? smDS->NbElements() : smDS->NbNodes();
271 //=============================================================================
273 * Return true if all sub-meshes have been meshed
275 //=============================================================================
277 bool SMESH_subMesh::SubMeshesComputed(bool * isFailedToCompute/*=0*/) const
279 int myDim = SMESH_Gen::GetShapeDim( _subShape );
280 int dimToCheck = myDim - 1;
281 bool subMeshesComputed = true;
282 if ( isFailedToCompute ) *isFailedToCompute = false;
283 // check subMeshes with upper dimension => reverse iteration
284 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,true);
285 while ( smIt->more() )
287 SMESH_subMesh *sm = smIt->next();
288 if ( sm->_alwaysComputed )
290 const TopoDS_Shape & ss = sm->GetSubShape();
292 // MSV 07.04.2006: restrict checking to myDim-1 only. Ex., there is no sense
293 // in checking of existence of edges if the algo needs only faces. Moreover,
294 // degenerated edges may have no submesh, as after computing NETGEN_2D.
295 if ( !_algo || _algo->NeedDiscreteBoundary() ) {
296 int dim = SMESH_Gen::GetShapeDim( ss );
297 if (dim < dimToCheck)
298 break; // the rest subMeshes are all of less dimension
300 SMESHDS_SubMesh * ds = sm->GetSubMeshDS();
301 bool computeOk = (sm->GetComputeState() == COMPUTE_OK ||
302 (ds && ( dimToCheck ? ds->NbElements() : ds->NbNodes() )));
305 subMeshesComputed = false;
306 if ( isFailedToCompute && !(*isFailedToCompute) )
307 *isFailedToCompute = ( sm->GetComputeState() == FAILED_TO_COMPUTE );
309 // int type = ss.ShapeType();
313 // case TopAbs_COMPOUND:
315 // MESSAGE("The not computed sub mesh is a COMPOUND");
318 // case TopAbs_COMPSOLID:
320 // MESSAGE("The not computed sub mesh is a COMPSOLID");
323 // case TopAbs_SHELL:
325 // MESSAGE("The not computed sub mesh is a SHEL");
330 // MESSAGE("The not computed sub mesh is a WIRE");
333 // case TopAbs_SOLID:
335 // MESSAGE("The not computed sub mesh is a SOLID");
340 // MESSAGE("The not computed sub mesh is a FACE");
345 // MESSAGE("The not computed sub mesh is a EDGE");
350 // MESSAGE("The not computed sub mesh is of unknown type");
355 if ( !isFailedToCompute )
359 return subMeshesComputed;
362 //================================================================================
364 * \brief Return cost of computing this sub-mesh. If hypotheses are not well defined,
366 * \return int - the computation cost in abstract units.
368 //================================================================================
370 int SMESH_subMesh::GetComputeCost() const
372 return _realComputeCost;
375 //================================================================================
377 * \brief Return cost of computing this sub-mesh. The cost depends on the shape type
378 * and number of sub-meshes this one DependsOn().
379 * \return int - the computation cost in abstract units.
381 //================================================================================
383 int SMESH_subMesh::computeCost() const
388 switch ( _subShape.ShapeType() ) {
390 case TopAbs_SHELL: computeCost = 5000; break;
391 case TopAbs_FACE: computeCost = 500; break;
392 case TopAbs_EDGE: computeCost = 2; break;
393 default: computeCost = 1;
395 SMESH_subMeshIteratorPtr childIt = getDependsOnIterator(/*includeSelf=*/false);
396 while ( childIt->more() )
397 computeCost += childIt->next()->computeCost();
399 ((SMESH_subMesh*)this)->_computeCost = computeCost;
404 //=============================================================================
406 * Construct dependence on first level subMeshes. complex shapes (compsolid,
407 * shell, wire) are not analysed the same way as simple shapes (solid, face,
409 * For collection shapes (compsolid, shell, wire) prepare a list of submeshes
410 * with possible multiples occurences. Multiples occurences corresponds to
411 * internal frontiers within shapes of the collection and must not be keeped.
412 * See FinalizeDependence.
414 //=============================================================================
416 const map < int, SMESH_subMesh * >& SMESH_subMesh::DependsOn()
418 if ( _dependenceAnalysed || !_father->HasShapeToMesh() )
421 int type = _subShape.ShapeType();
424 case TopAbs_COMPOUND:
426 list< TopoDS_Shape > compounds( 1, _subShape );
427 list< TopoDS_Shape >::iterator comp = compounds.begin();
428 for ( ; comp != compounds.end(); ++comp )
430 for ( TopoDS_Iterator sub( *comp ); sub.More(); sub.Next() )
431 switch ( sub.Value().ShapeType() )
433 case TopAbs_COMPOUND: compounds.push_back( sub.Value() ); break;
434 case TopAbs_COMPSOLID: insertDependence( sub.Value(), TopAbs_SOLID ); break;
435 case TopAbs_SOLID: insertDependence( sub.Value(), TopAbs_SOLID ); break;
436 case TopAbs_SHELL: insertDependence( sub.Value(), TopAbs_FACE ); break;
437 case TopAbs_FACE: insertDependence( sub.Value(), TopAbs_FACE ); break;
438 case TopAbs_WIRE: insertDependence( sub.Value(), TopAbs_EDGE ); break;
439 case TopAbs_EDGE: insertDependence( sub.Value(), TopAbs_EDGE ); break;
440 case TopAbs_VERTEX: insertDependence( sub.Value(), TopAbs_VERTEX ); break;
446 case TopAbs_COMPSOLID: insertDependence( _subShape, TopAbs_SOLID ); break;
447 case TopAbs_SOLID: insertDependence( _subShape, TopAbs_FACE ); break;
448 case TopAbs_SHELL: insertDependence( _subShape, TopAbs_FACE ); break;
449 case TopAbs_FACE: insertDependence( _subShape, TopAbs_EDGE ); break;
450 case TopAbs_WIRE: insertDependence( _subShape, TopAbs_EDGE ); break;
451 case TopAbs_EDGE: insertDependence( _subShape, TopAbs_VERTEX ); break;
454 _dependenceAnalysed = true;
458 //================================================================================
460 * \brief Return a key for SMESH_subMesh::_mapDepend map
462 //================================================================================
465 int dependsOnMapKey( const SMESH_subMesh* sm )
467 int type = sm->GetSubShape().ShapeType();
468 int ordType = 9 - type; // 2 = Vertex, 8 = CompSolid
469 int cle = sm->GetId();
470 cle += 10000000 * ordType; // sort map by ordType then index
475 //=============================================================================
477 * For simple Shapes (solid, face, edge): add subMesh into dependence list.
479 //=============================================================================
481 void SMESH_subMesh::insertDependence(const TopoDS_Shape aShape,
482 TopAbs_ShapeEnum aSubType)
484 TopExp_Explorer sub( aShape, aSubType );
485 for ( ; sub.More(); sub.Next() )
487 SMESH_subMesh *aSubMesh = _father->GetSubMesh( sub.Current() );
488 int cle = dependsOnMapKey( aSubMesh );
489 if ( _mapDepend.find( cle ) == _mapDepend.end())
491 _mapDepend[cle] = aSubMesh;
492 const map < int, SMESH_subMesh * > & subMap = aSubMesh->DependsOn();
493 _mapDepend.insert( subMap.begin(), subMap.end() );
498 //================================================================================
500 * \brief Return \c true if \a this sub-mesh depends on \a other
502 //================================================================================
504 bool SMESH_subMesh::DependsOn( const SMESH_subMesh* other ) const
506 return other ? _mapDepend.count( dependsOnMapKey( other )) : false;
509 //=============================================================================
511 * Return a shape of \a this sub-mesh
513 //=============================================================================
515 const TopoDS_Shape & SMESH_subMesh::GetSubShape() const
520 //=======================================================================
521 //function : CanAddHypothesis
522 //purpose : return true if theHypothesis can be attached to me:
523 // its dimention is checked
524 //=======================================================================
526 bool SMESH_subMesh::CanAddHypothesis(const SMESH_Hypothesis* theHypothesis) const
528 int aHypDim = theHypothesis->GetDim();
529 int aShapeDim = SMESH_Gen::GetShapeDim(_subShape);
530 // issue 21106. Forbid 3D mesh on the SHELL
531 // if (aHypDim == 3 && aShapeDim == 3) {
532 // // check case of open shell
533 // //if (_subShape.ShapeType() == TopAbs_SHELL && !_subShape.Closed())
534 // if (_subShape.ShapeType() == TopAbs_SHELL && !BRep_Tool::IsClosed(_subShape))
537 if ( aHypDim <= aShapeDim )
543 //=======================================================================
544 //function : IsApplicableHypotesis
546 //=======================================================================
548 bool SMESH_subMesh::IsApplicableHypotesis(const SMESH_Hypothesis* theHypothesis,
549 const TopAbs_ShapeEnum theShapeType)
551 if ( theHypothesis->GetType() > SMESHDS_Hypothesis::PARAM_ALGO)
554 if ( theHypothesis->GetShapeType() & (1<< theShapeType))
555 // issue 21106. Forbid 3D mesh on the SHELL
556 return !( theHypothesis->GetDim() == 3 && theShapeType == TopAbs_SHELL );
562 switch ( theShapeType ) {
567 return SMESH_Gen::GetShapeDim( theShapeType ) == theHypothesis->GetDim();
570 // Special case for algorithms, building 2D mesh on a whole shell.
571 // Before this fix there was a problem after restoring from study,
572 // because in that case algorithm is assigned before hypothesis
573 // (on shell in problem case) and hypothesis is checked on faces
574 // (because it is 2D), where we have NO_ALGO state.
575 // Now 2D hypothesis is also applicable to shells.
576 return (theHypothesis->GetDim() == 2 || theHypothesis->GetDim() == 3);
579 // case TopAbs_COMPSOLID:
580 // case TopAbs_COMPOUND:
586 //================================================================================
588 * \brief Treats modification of hypotheses definition
589 * \param [in] event - what happens
590 * \param [in] anHyp - a hypothesis
591 * \return SMESH_Hypothesis::Hypothesis_Status - a treatment result.
593 * Optional description of a problematic situation (if any) can be retrieved
594 * via GetComputeError().
596 //================================================================================
598 SMESH_Hypothesis::Hypothesis_Status
599 SMESH_subMesh::AlgoStateEngine(int event, SMESH_Hypothesis * anHyp)
601 // **** les retour des evenement shape sont significatifs
602 // (add ou remove fait ou non)
603 // le retour des evenement father n'indiquent pas que add ou remove fait
605 SMESH_Hypothesis::Hypothesis_Status aux_ret, ret = SMESH_Hypothesis::HYP_OK;
607 SMESHDS_Mesh* meshDS =_father->GetMeshDS();
608 SMESH_Algo* algo = 0;
611 if (_subShape.ShapeType() == TopAbs_VERTEX )
613 if ( anHyp->GetDim() != 0) {
614 if (event == ADD_HYP || event == ADD_ALGO)
615 return SMESH_Hypothesis::HYP_BAD_DIM;
617 return SMESH_Hypothesis::HYP_OK;
620 else if ( _algoState == HYP_OK ) {
621 // update default _algoState
622 if ( event != REMOVE_FATHER_ALGO )
624 _algoState = NO_ALGO;
627 _algoState = MISSING_HYP;
628 if ( event == REMOVE_FATHER_HYP ||
629 algo->CheckHypothesis(*_father,_subShape, aux_ret))
636 int oldAlgoState = _algoState;
637 bool modifiedHyp = (event == MODIF_HYP); // if set to true, force event MODIF_ALGO_STATE
638 SMESH_Algo* algoRequiringCleaning = 0;
640 bool isApplicableHyp = IsApplicableHypotesis( anHyp );
642 if (event == ADD_ALGO || event == ADD_FATHER_ALGO)
644 // -------------------------------------------
645 // check if a shape needed by algo is present
646 // -------------------------------------------
647 algo = static_cast< SMESH_Algo* >( anHyp );
648 if ( !_father->HasShapeToMesh() && algo->NeedShape() )
649 return SMESH_Hypothesis::HYP_NEED_SHAPE;
650 // ----------------------
651 // check mesh conformity
652 // ----------------------
653 if (isApplicableHyp && !_father->IsNotConformAllowed() && !IsConform( algo ))
654 return SMESH_Hypothesis::HYP_NOTCONFORM;
656 // check if all-dimensional algo is hidden by other local one
657 if ( event == ADD_ALGO ) {
658 SMESH_HypoFilter filter( SMESH_HypoFilter::HasType( algo->GetType() ));
659 filter.Or( SMESH_HypoFilter::HasType( algo->GetType()+1 ));
660 filter.Or( SMESH_HypoFilter::HasType( algo->GetType()+2 ));
661 if ( SMESH_Algo * curAlgo = (SMESH_Algo*)_father->GetHypothesis( this, filter, true ))
662 if ( !curAlgo->NeedDiscreteBoundary() )
663 algoRequiringCleaning = curAlgo;
667 // ----------------------------------
668 // add a hypothesis to DS if possible
669 // ----------------------------------
670 if (event == ADD_HYP || event == ADD_ALGO)
672 if ( ! CanAddHypothesis( anHyp )) // check dimension
673 return SMESH_Hypothesis::HYP_BAD_DIM;
675 if ( !anHyp->IsAuxiliary() && getSimilarAttached( _subShape, anHyp ) )
676 return SMESH_Hypothesis::HYP_ALREADY_EXIST;
678 if ( !meshDS->AddHypothesis(_subShape, anHyp))
679 return SMESH_Hypothesis::HYP_ALREADY_EXIST;
682 // --------------------------
683 // remove a hypothesis from DS
684 // --------------------------
685 if (event == REMOVE_HYP || event == REMOVE_ALGO)
687 if (!meshDS->RemoveHypothesis(_subShape, anHyp))
688 return SMESH_Hypothesis::HYP_OK; // nothing changes
690 if (event == REMOVE_ALGO)
692 algo = dynamic_cast<SMESH_Algo*> (anHyp);
693 if (!algo->NeedDiscreteBoundary())
694 algoRequiringCleaning = algo;
698 // ------------------
699 // analyse algo state
700 // ------------------
701 if (!isApplicableHyp)
702 return ret; // not applicable hypotheses do not change algo state
704 if (( algo = GetAlgo()))
705 algo->InitComputeError();
710 // ----------------------------------------------------------------------
719 if (algo->CheckHypothesis((*_father),_subShape, aux_ret))
720 setAlgoState(HYP_OK);
721 else if ( algo->IsStatusFatal( aux_ret )) {
722 meshDS->RemoveHypothesis(_subShape, anHyp);
726 setAlgoState(MISSING_HYP);
733 case ADD_FATHER_ALGO: { // Algo just added in father
736 if ( algo == anHyp ) {
737 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret))
738 setAlgoState(HYP_OK);
740 setAlgoState(MISSING_HYP);
744 case REMOVE_FATHER_HYP:
746 case REMOVE_FATHER_ALGO: {
750 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
751 setAlgoState(HYP_OK);
753 setAlgoState(MISSING_HYP);
757 case MODIF_HYP: break;
764 // ----------------------------------------------------------------------
772 if ( algo->CheckHypothesis((*_father),_subShape, ret ))
773 setAlgoState(HYP_OK);
774 if (SMESH_Hypothesis::IsStatusFatal( ret ))
775 meshDS->RemoveHypothesis(_subShape, anHyp);
776 else if (!_father->IsUsedHypothesis( anHyp, this ))
778 meshDS->RemoveHypothesis(_subShape, anHyp);
779 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
783 case ADD_ALGO: { //already existing algo : on father ?
786 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))// ignore hyp status
787 setAlgoState(HYP_OK);
788 else if ( algo->IsStatusFatal( aux_ret )) {
789 meshDS->RemoveHypothesis(_subShape, anHyp);
793 setAlgoState(MISSING_HYP);
798 case REMOVE_ALGO: { // perhaps a father algo applies ?
800 if (algo == NULL) // no more algo applying on sub-shape...
802 setAlgoState(NO_ALGO);
806 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
807 setAlgoState(HYP_OK);
809 setAlgoState(MISSING_HYP);
813 case MODIF_HYP: // assigned hypothesis value may become good
814 case ADD_FATHER_HYP: {
817 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
818 setAlgoState(HYP_OK);
820 setAlgoState(MISSING_HYP);
823 case ADD_FATHER_ALGO: { // new father algo
826 if ( algo == anHyp ) {
827 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
828 setAlgoState(HYP_OK);
830 setAlgoState(MISSING_HYP);
834 case REMOVE_FATHER_HYP: // nothing to do
836 case REMOVE_FATHER_ALGO: {
838 if (algo == NULL) // no more applying algo on father
840 setAlgoState(NO_ALGO);
844 if ( algo->CheckHypothesis((*_father),_subShape , aux_ret ))
845 setAlgoState(HYP_OK);
847 setAlgoState(MISSING_HYP);
857 // ----------------------------------------------------------------------
865 if (!algo->CheckHypothesis((*_father),_subShape, ret ))
867 if ( !SMESH_Hypothesis::IsStatusFatal( ret ))
868 // ret should be fatal: anHyp was not added
869 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
871 else if (!_father->IsUsedHypothesis( anHyp, this ))
872 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
874 if (SMESH_Hypothesis::IsStatusFatal( ret ))
876 MESSAGE("do not add extra hypothesis");
877 meshDS->RemoveHypothesis(_subShape, anHyp);
885 case ADD_ALGO: { //already existing algo : on father ?
887 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
888 // check if algo changes
890 f.Init( SMESH_HypoFilter::IsAlgo() );
891 f.And( SMESH_HypoFilter::IsApplicableTo( _subShape ));
892 f.AndNot( SMESH_HypoFilter::Is( algo ));
893 const SMESH_Hypothesis * prevAlgo = _father->GetHypothesis( this, f, true );
895 string(algo->GetName()) != string(prevAlgo->GetName()) )
899 setAlgoState(MISSING_HYP);
905 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
906 setAlgoState(HYP_OK);
908 setAlgoState(MISSING_HYP);
912 case REMOVE_ALGO: { // perhaps a father algo applies ?
914 if (algo == NULL) // no more algo applying on sub-shape...
916 setAlgoState(NO_ALGO);
920 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
921 // check if algo remains
922 if ( anHyp != algo && strcmp( anHyp->GetName(), algo->GetName()) )
926 setAlgoState(MISSING_HYP);
930 case MODIF_HYP: // hypothesis value may become bad
931 case ADD_FATHER_HYP: { // new father hypothesis ?
934 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
936 if (_father->IsUsedHypothesis( anHyp, this )) // new Hyp
940 setAlgoState(MISSING_HYP);
943 case ADD_FATHER_ALGO: {
945 if ( algo == anHyp ) { // a new algo on father
946 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
947 // check if algo changes
949 f.Init( SMESH_HypoFilter::IsAlgo() );
950 f.And( SMESH_HypoFilter::IsApplicableTo( _subShape ));
951 f.AndNot( SMESH_HypoFilter::Is( algo ));
952 const SMESH_Hypothesis* prevAlgo = _father->GetHypothesis( this, f, true );
954 string(algo->GetName()) != string(prevAlgo->GetName()) )
958 setAlgoState(MISSING_HYP);
962 case REMOVE_FATHER_HYP: {
965 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
966 // is there the same local hyp or maybe a new father algo applied?
967 if ( !getSimilarAttached( _subShape, anHyp ) )
971 setAlgoState(MISSING_HYP);
974 case REMOVE_FATHER_ALGO: {
975 // IPAL21346. Edges not removed when Netgen 1d-2d is removed from a SOLID.
976 // CLEAN was not called at event REMOVE_ALGO because the algo is not applicable to SOLID.
977 algo = dynamic_cast<SMESH_Algo*> (anHyp);
978 if (!algo->NeedDiscreteBoundary())
979 algoRequiringCleaning = algo;
981 if (algo == NULL) // no more applying algo on father
983 setAlgoState(NO_ALGO);
987 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
988 // check if algo changes
989 if ( string(algo->GetName()) != string( anHyp->GetName()) )
993 setAlgoState(MISSING_HYP);
1003 // ----------------------------------------------------------------------
1010 // detect algorithm hiding
1012 if ( ret == SMESH_Hypothesis::HYP_OK &&
1013 ( event == ADD_ALGO || event == ADD_FATHER_ALGO ) &&
1014 algo->GetName() == anHyp->GetName() )
1017 SMESH_Gen* gen = _father->GetGen();
1018 const std::vector< SMESH_subMesh * > & ancestors = GetAncestors();
1019 for ( size_t iA = 0; ( ret == SMESH_Hypothesis::HYP_OK && iA < ancestors.size()); ++iA ) {
1020 if ( SMESH_Algo* upperAlgo = ancestors[ iA ]->GetAlgo() )
1021 if ( !upperAlgo->NeedDiscreteBoundary() && !upperAlgo->SupportSubmeshes())
1022 ret = SMESH_Hypothesis::HYP_HIDDEN_ALGO;
1025 if ( ret == SMESH_Hypothesis::HYP_OK &&
1026 !algo->NeedDiscreteBoundary() &&
1027 !algo->SupportSubmeshes())
1029 TopoDS_Shape algoAssignedTo, otherAssignedTo;
1030 gen->GetAlgo( this, &algoAssignedTo );
1031 map<int, SMESH_subMesh*>::reverse_iterator i_sm = _mapDepend.rbegin();
1032 for ( ; ( ret == SMESH_Hypothesis::HYP_OK && i_sm != _mapDepend.rend()) ; ++i_sm )
1033 if ( gen->GetAlgo( i_sm->second, &otherAssignedTo ) &&
1034 SMESH_MesherHelper::IsSubShape( /*sub=*/otherAssignedTo, /*main=*/algoAssignedTo ))
1035 ret = SMESH_Hypothesis::HYP_HIDING_ALGO;
1039 if ( _algo ) { // get an error description set by _algo->CheckHypothesis()
1040 _computeError = _algo->GetComputeError();
1041 _algo->InitComputeError();
1044 bool stateChange = ( _algoState != oldAlgoState );
1046 if ( stateChange && _algoState == HYP_OK ) // hyp becomes OK
1047 algo->SetEventListener( this );
1049 if ( event == REMOVE_ALGO || event == REMOVE_FATHER_ALGO )
1052 notifyListenersOnEvent( event, ALGO_EVENT, anHyp );
1054 if ( stateChange && oldAlgoState == HYP_OK ) { // hyp becomes KO
1055 deleteOwnListeners();
1056 SetIsAlwaysComputed( false );
1057 if (_subShape.ShapeType() == TopAbs_VERTEX ) {
1058 // restore default states
1059 _algoState = HYP_OK;
1060 _computeState = READY_TO_COMPUTE;
1064 if ( algoRequiringCleaning ) {
1065 // added or removed algo is all-dimensional
1066 ComputeStateEngine( CLEAN );
1067 cleanDependsOn( algoRequiringCleaning );
1068 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1071 if ( stateChange || modifiedHyp )
1072 ComputeStateEngine( MODIF_ALGO_STATE );
1074 _realComputeCost = ( _algoState == HYP_OK ) ? computeCost() : 0;
1079 //=======================================================================
1080 //function : IsConform
1081 //purpose : check if a conform mesh will be produced by the Algo
1082 //=======================================================================
1084 bool SMESH_subMesh::IsConform(const SMESH_Algo* theAlgo)
1086 // MESSAGE( "SMESH_subMesh::IsConform" );
1087 if ( !theAlgo ) return false;
1089 // Suppose that theAlgo is applicable to _subShape, do not check it here
1090 //if ( !IsApplicableHypotesis( theAlgo )) return false;
1092 // check only algo that doesn't NeedDiscreteBoundary(): because mesh made
1093 // on a sub-shape will be ignored by theAlgo
1094 if ( theAlgo->NeedDiscreteBoundary() ||
1095 !theAlgo->OnlyUnaryInput() ) // all adjacent shapes will be meshed by this algo?
1098 SMESH_Gen* gen =_father->GetGen();
1100 // only local algo is to be checked
1101 //if ( gen->IsGlobalHypothesis( theAlgo, *_father ))
1102 if ( _subShape.ShapeType() == _father->GetMeshDS()->ShapeToMesh().ShapeType() )
1105 // check algo attached to adjacent shapes
1107 // loop on one level down sub-meshes
1108 TopoDS_Iterator itsub( _subShape );
1109 for (; itsub.More(); itsub.Next())
1111 // loop on adjacent subShapes
1112 const std::vector< SMESH_subMesh * > & ancestors = GetAncestors();
1113 for ( size_t iA = 0; iA < ancestors.size(); ++iA )
1115 const TopoDS_Shape& adjacent = ancestors[ iA ]->GetSubShape();
1116 if ( _subShape.IsSame( adjacent )) continue;
1117 if ( adjacent.ShapeType() != _subShape.ShapeType())
1120 // check algo attached to smAdjacent
1121 SMESH_Algo * algo = ancestors[ iA ]->GetAlgo();
1123 !algo->NeedDiscreteBoundary() &&
1124 algo->OnlyUnaryInput())
1125 return false; // NOT CONFORM MESH WILL BE PRODUCED
1132 //=============================================================================
1136 //=============================================================================
1138 void SMESH_subMesh::setAlgoState(algo_state state)
1143 //================================================================================
1145 * \brief Send an event to sub-meshes
1146 * \param [in] event - the event
1147 * \param [in] anHyp - an hypothesis
1148 * \param [in] exitOnFatal - to stop iteration on sub-meshes if a sub-mesh
1149 * reports a fatal result
1150 * \return SMESH_Hypothesis::Hypothesis_Status - the worst result
1152 * Optional description of a problematic situation (if any) can be retrieved
1153 * via GetComputeError().
1155 //================================================================================
1157 SMESH_Hypothesis::Hypothesis_Status
1158 SMESH_subMesh::SubMeshesAlgoStateEngine(int event,
1159 SMESH_Hypothesis * anHyp,
1162 SMESH_Hypothesis::Hypothesis_Status ret = SMESH_Hypothesis::HYP_OK;
1163 //EAP: a wire (dim==1) should notify edges (dim==1)
1164 //EAP: int dim = SMESH_Gen::GetShapeDim(_subShape);
1165 //if (_subShape.ShapeType() < TopAbs_EDGE ) // wire,face etc
1167 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1168 while ( smIt->more() ) {
1169 SMESH_subMesh* sm = smIt->next();
1170 SMESH_Hypothesis::Hypothesis_Status ret2 = sm->AlgoStateEngine(event, anHyp);
1174 _computeError = sm->_computeError;
1175 sm->_computeError.reset();
1176 if ( exitOnFatal && SMESH_Hypothesis::IsStatusFatal( ret ))
1184 //================================================================================
1186 * \brief Remove elements from sub-meshes.
1187 * \param algoRequiringCleaning - an all-dimensional algorithm whose presence
1188 * causes the cleaning.
1190 //================================================================================
1192 void SMESH_subMesh::cleanDependsOn( SMESH_Algo* algoRequiringCleaning/*=0*/ )
1194 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,
1195 /*complexShapeFirst=*/true);
1196 if ( _father->NbNodes() == 0 )
1198 while ( smIt->more() )
1199 smIt->next()->ComputeStateEngine(CHECK_COMPUTE_STATE);
1201 else if ( !algoRequiringCleaning || !algoRequiringCleaning->SupportSubmeshes() )
1203 while ( smIt->more() )
1204 smIt->next()->ComputeStateEngine(CLEAN);
1206 else if ( algoRequiringCleaning && algoRequiringCleaning->SupportSubmeshes() )
1208 SMESHDS_Mesh* meshDS = _father->GetMeshDS();
1210 // find sub-meshes to keep elements on
1211 set< SMESH_subMesh* > smToKeep;
1212 TopAbs_ShapeEnum prevShapeType = TopAbs_SHAPE;
1213 bool toKeepPrevShapeType = false;
1214 while ( smIt->more() )
1216 SMESH_subMesh* sm = smIt->next();
1217 sm->ComputeStateEngine(CHECK_COMPUTE_STATE);
1218 if ( !sm->IsEmpty() )
1220 const bool sameShapeType = ( prevShapeType == sm->GetSubShape().ShapeType() );
1221 bool keepSubMeshes = ( sameShapeType && toKeepPrevShapeType );
1222 if ( !sameShapeType )
1224 // check if the algo allows presence of global algos of dimension the algo
1225 // can generate it-self
1226 int shapeDim = SMESH_Gen::GetShapeDim( sm->GetSubShape() );
1227 keepSubMeshes = algoRequiringCleaning->NeedLowerHyps( shapeDim );
1228 prevShapeType = sm->GetSubShape().ShapeType();
1229 toKeepPrevShapeType = keepSubMeshes;
1231 if ( !keepSubMeshes )
1233 // look for an algo assigned to sm
1234 bool algoFound = false;
1235 const list<const SMESHDS_Hypothesis*>& hyps = meshDS->GetHypothesis( sm->_subShape );
1236 list<const SMESHDS_Hypothesis*>::const_iterator h = hyps.begin();
1237 for ( ; ( !algoFound && h != hyps.end() ); ++h )
1238 algoFound = ((*h)->GetType() != SMESHDS_Hypothesis::PARAM_ALGO );
1239 keepSubMeshes = algoFound;
1241 // remember all sub-meshes of sm
1242 if ( keepSubMeshes )
1244 SMESH_subMeshIteratorPtr smIt2 = getDependsOnIterator(false,true);
1245 while ( smIt2->more() )
1246 smToKeep.insert( smIt2->next() );
1251 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,true);
1252 while ( smIt->more() )
1254 SMESH_subMesh* sm = smIt->next();
1255 if ( !smToKeep.count( sm ))
1256 sm->ComputeStateEngine(CLEAN);
1261 //=============================================================================
1265 //=============================================================================
1267 void SMESH_subMesh::DumpAlgoState(bool isMain)
1271 const map < int, SMESH_subMesh * >&subMeshes = DependsOn();
1273 map < int, SMESH_subMesh * >::const_iterator itsub;
1274 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
1276 SMESH_subMesh *sm = (*itsub).second;
1277 sm->DumpAlgoState(false);
1280 MESSAGE("dim = " << SMESH_Gen::GetShapeDim(_subShape) <<
1281 " type of shape " << _subShape.ShapeType());
1284 case NO_ALGO : MESSAGE(" AlgoState = NO_ALGO"); break;
1285 case MISSING_HYP : MESSAGE(" AlgoState = MISSING_HYP"); break;
1286 case HYP_OK : MESSAGE(" AlgoState = HYP_OK");break;
1288 switch (_computeState)
1290 case NOT_READY : MESSAGE(" ComputeState = NOT_READY");break;
1291 case READY_TO_COMPUTE : MESSAGE(" ComputeState = READY_TO_COMPUTE");break;
1292 case COMPUTE_OK : MESSAGE(" ComputeState = COMPUTE_OK");break;
1293 case FAILED_TO_COMPUTE: MESSAGE(" ComputeState = FAILED_TO_COMPUTE");break;
1297 //================================================================================
1299 * \brief Remove nodes and elements bound to submesh
1300 * \param subMesh - submesh containing nodes and elements
1302 //================================================================================
1304 static void cleanSubMesh( SMESH_subMesh * subMesh )
1307 if (SMESHDS_SubMesh * subMeshDS = subMesh->GetSubMeshDS()) {
1308 SMESHDS_Mesh * meshDS = subMesh->GetFather()->GetMeshDS();
1309 SMDS_ElemIteratorPtr ite = subMeshDS->GetElements();
1310 while (ite->more()) {
1311 const SMDS_MeshElement * elt = ite->next();
1312 //MESSAGE( " RM elt: "<<elt->GetID()<<" ( "<<elt->NbNodes()<<" )" );
1313 //meshDS->RemoveElement(elt);
1314 meshDS->RemoveFreeElement(elt, 0);
1317 SMDS_NodeIteratorPtr itn = subMeshDS->GetNodes();
1318 while (itn->more()) {
1319 const SMDS_MeshNode * node = itn->next();
1320 //MESSAGE( " RM node: "<<node->GetID());
1321 if ( node->NbInverseElements() == 0 )
1322 meshDS->RemoveFreeNode(node, 0);
1323 else // for StdMeshers_CompositeSegment_1D: node in one submesh, edge in another
1324 meshDS->RemoveNode(node);
1331 //=============================================================================
1335 //=============================================================================
1337 bool SMESH_subMesh::ComputeStateEngine(int event)
1340 case MODIF_ALGO_STATE:
1342 case COMPUTE_SUBMESH:
1343 //case COMPUTE_CANCELED:
1345 //case SUBMESH_COMPUTED:
1346 //case SUBMESH_RESTORED:
1347 //case SUBMESH_LOADED:
1348 //case MESH_ENTITY_REMOVED:
1349 //case CHECK_COMPUTE_STATE:
1350 _computeError.reset(); break;
1354 if ( event == CLEAN )
1355 _alwaysComputed = false; // Unset 'true' set by MergeNodes() (issue 0022182)
1357 if (_subShape.ShapeType() == TopAbs_VERTEX)
1359 _computeState = READY_TO_COMPUTE;
1360 SMESHDS_SubMesh* smDS = GetSubMeshDS();
1361 if ( smDS && smDS->NbNodes() )
1363 if ( event == CLEAN ) {
1365 cleanSubMesh( this );
1368 _computeState = COMPUTE_OK;
1370 else if (( event == COMPUTE || event == COMPUTE_SUBMESH )
1371 && !_alwaysComputed )
1373 const TopoDS_Vertex & V = TopoDS::Vertex( _subShape );
1374 gp_Pnt P = BRep_Tool::Pnt(V);
1375 if ( SMDS_MeshNode * n = _father->GetMeshDS()->AddNode(P.X(), P.Y(), P.Z()) ) {
1376 _father->GetMeshDS()->SetNodeOnVertex(n,_Id);
1377 _computeState = COMPUTE_OK;
1380 if ( event == MODIF_ALGO_STATE )
1384 SMESH_Gen *gen = _father->GetGen();
1385 SMESH_Algo *algo = 0;
1387 SMESH_Hypothesis::Hypothesis_Status hyp_status;
1388 //algo_state oldAlgoState = (algo_state) GetAlgoState();
1390 switch (_computeState)
1393 // ----------------------------------------------------------------------
1398 case MODIF_ALGO_STATE:
1400 if (algo && !algo->NeedDiscreteBoundary())
1401 cleanDependsOn( algo ); // clean sub-meshes with event CLEAN
1402 if ( _algoState == HYP_OK )
1403 _computeState = READY_TO_COMPUTE;
1405 case COMPUTE: // nothing to do
1406 case COMPUTE_SUBMESH:
1408 case COMPUTE_CANCELED: // nothing to do
1412 removeSubMeshElementsAndNodes();
1414 case SUBMESH_COMPUTED: // nothing to do
1416 case SUBMESH_RESTORED:
1417 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1419 case MESH_ENTITY_REMOVED:
1421 case SUBMESH_LOADED:
1422 loadDependentMeshes();
1423 ComputeSubMeshStateEngine( SUBMESH_LOADED );
1425 case CHECK_COMPUTE_STATE:
1426 if ( IsMeshComputed() )
1427 _computeState = COMPUTE_OK;
1435 // ----------------------------------------------------------------------
1437 case READY_TO_COMPUTE:
1440 case MODIF_ALGO_STATE:
1441 _computeState = NOT_READY;
1445 if (!algo->NeedDiscreteBoundary())
1446 cleanDependsOn( algo ); // clean sub-meshes with event CLEAN
1447 if ( _algoState == HYP_OK )
1448 _computeState = READY_TO_COMPUTE;
1452 case COMPUTE_SUBMESH:
1456 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1459 MESSAGE("***** verify compute state *****");
1460 _computeState = NOT_READY;
1461 setAlgoState(MISSING_HYP);
1464 TopoDS_Shape shape = _subShape;
1465 algo->SubMeshesToCompute().assign( 1, this );
1466 // check submeshes needed
1467 if (_father->HasShapeToMesh() ) {
1468 bool subComputed = false, subFailed = false;
1469 if (!algo->OnlyUnaryInput()) {
1470 if ( event == COMPUTE /*&&
1471 ( algo->NeedDiscreteBoundary() || algo->SupportSubmeshes() )*/)
1472 shape = getCollection( gen, algo, subComputed, subFailed, algo->SubMeshesToCompute());
1474 subComputed = SubMeshesComputed( & subFailed );
1477 subComputed = SubMeshesComputed();
1479 ret = ( algo->NeedDiscreteBoundary() ? subComputed :
1480 algo->SupportSubmeshes() ? !subFailed :
1481 ( !subComputed || _father->IsNotConformAllowed() ));
1484 _computeState = FAILED_TO_COMPUTE;
1485 if ( !algo->NeedDiscreteBoundary() && !subFailed )
1487 SMESH_ComputeError::New(COMPERR_BAD_INPUT_MESH,
1488 "Unexpected computed sub-mesh",algo);
1494 // to restore cout that may be redirected by algo
1495 std::streambuf* coutBuffer = std::cout.rdbuf();
1497 //cleanDependants(); for "UseExisting_*D" algos
1498 //removeSubMeshElementsAndNodes();
1499 loadDependentMeshes();
1501 _computeState = FAILED_TO_COMPUTE;
1502 _computeError = SMESH_ComputeError::New(COMPERR_OK,"",algo);
1506 algo->InitComputeError();
1508 MemoryReserve aMemoryReserve;
1509 SMDS_Mesh::CheckMemory();
1510 Kernel_Utils::Localizer loc;
1511 if ( !_father->HasShapeToMesh() ) // no shape
1513 SMESH_MesherHelper helper( *_father );
1514 helper.SetSubShape( shape );
1515 helper.SetElementsOnShape( true );
1516 ret = algo->Compute(*_father, &helper );
1520 ret = algo->Compute((*_father), shape);
1522 // algo can set _computeError of submesh
1523 _computeError = SMESH_ComputeError::Worst( _computeError, algo->GetComputeError() );
1525 catch ( ::SMESH_ComputeError& comperr ) {
1526 cout << " SMESH_ComputeError caught" << endl;
1527 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1528 *_computeError = comperr;
1530 catch ( std::bad_alloc& exc ) {
1531 MESSAGE("std::bad_alloc thrown inside algo->Compute()");
1532 if ( _computeError ) {
1533 _computeError->myName = COMPERR_MEMORY_PB;
1534 //_computeError->myComment = exc.what();
1536 cleanSubMesh( this );
1539 catch ( Standard_OutOfMemory& exc ) {
1540 MESSAGE("Standard_OutOfMemory thrown inside algo->Compute()");
1541 if ( _computeError ) {
1542 _computeError->myName = COMPERR_MEMORY_PB;
1543 //_computeError->myComment = exc.what();
1545 cleanSubMesh( this );
1546 throw std::bad_alloc();
1548 catch (Standard_Failure& ex) {
1549 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1550 _computeError->myName = COMPERR_OCC_EXCEPTION;
1551 _computeError->myComment += ex.DynamicType()->Name();
1552 if ( ex.GetMessageString() && strlen( ex.GetMessageString() )) {
1553 _computeError->myComment += ": ";
1554 _computeError->myComment += ex.GetMessageString();
1557 catch ( SALOME_Exception& S_ex ) {
1558 const int skipSalomeShift = 7; /* to skip "Salome " of
1559 "Salome Exception" prefix returned
1560 by SALOME_Exception::what() */
1561 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1562 _computeError->myName = COMPERR_SLM_EXCEPTION;
1563 _computeError->myComment = S_ex.what() + skipSalomeShift;
1565 catch ( std::exception& exc ) {
1566 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1567 _computeError->myName = COMPERR_STD_EXCEPTION;
1568 _computeError->myComment = exc.what();
1571 if ( _computeError )
1572 _computeError->myName = COMPERR_EXCEPTION;
1576 std::cout.rdbuf( coutBuffer ); // restore cout that could be redirected by algo
1578 // check if an error reported on any sub-shape
1579 bool isComputeErrorSet = !checkComputeError( algo, ret, shape );
1580 if ( isComputeErrorSet )
1582 // check if anything was built
1583 TopExp_Explorer subS(shape, _subShape.ShapeType());
1586 for (; ret && subS.More(); subS.Next())
1587 if ( !_father->GetSubMesh( subS.Current() )->IsMeshComputed() &&
1588 ( _subShape.ShapeType() != TopAbs_EDGE ||
1589 !algo->isDegenerated( TopoDS::Edge( subS.Current() ))))
1592 // Set _computeError
1593 if (!ret && !isComputeErrorSet)
1595 for (subS.ReInit(); subS.More(); subS.Next())
1597 SMESH_subMesh* sm = _father->GetSubMesh( subS.Current() );
1598 if ( !sm->IsMeshComputed() )
1600 if ( !sm->_computeError )
1601 sm->_computeError = SMESH_ComputeError::New();
1602 if ( sm->_computeError->IsOK() )
1603 sm->_computeError->myName = COMPERR_ALGO_FAILED;
1604 sm->_computeState = FAILED_TO_COMPUTE;
1605 sm->_computeError->myAlgo = algo;
1609 if (ret && _computeError && _computeError->myName != COMPERR_WARNING )
1611 _computeError.reset();
1614 // send event SUBMESH_COMPUTED
1616 if ( !algo->NeedDiscreteBoundary() )
1617 // send SUBMESH_COMPUTED to dependants of all sub-meshes of shape
1618 for (subS.ReInit(); subS.More(); subS.Next())
1620 SMESH_subMesh* sm = _father->GetSubMesh( subS.Current() );
1621 SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(false,false);
1622 while ( smIt->more() ) {
1624 if ( sm->GetSubShape().ShapeType() == TopAbs_VERTEX )
1625 sm->updateDependantsState( SUBMESH_COMPUTED );
1631 updateDependantsState( SUBMESH_COMPUTED );
1635 case COMPUTE_CANCELED: // nothing to do
1639 removeSubMeshElementsAndNodes();
1640 _computeState = NOT_READY;
1644 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1646 _computeState = READY_TO_COMPUTE;
1648 setAlgoState(MISSING_HYP);
1651 case SUBMESH_COMPUTED: // nothing to do
1653 case SUBMESH_RESTORED:
1654 // check if a mesh is already computed that may
1655 // happen after retrieval from a file
1656 ComputeStateEngine( CHECK_COMPUTE_STATE );
1657 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1659 if (algo) algo->SubmeshRestored( this );
1661 case MESH_ENTITY_REMOVED:
1663 case SUBMESH_LOADED:
1664 loadDependentMeshes();
1665 ComputeSubMeshStateEngine( SUBMESH_LOADED );
1667 case CHECK_COMPUTE_STATE:
1668 if ( IsMeshComputed() )
1669 _computeState = COMPUTE_OK;
1670 else if ( _computeError && _computeError->IsKO() )
1671 _computeState = FAILED_TO_COMPUTE;
1679 // ----------------------------------------------------------------------
1684 case MODIF_ALGO_STATE:
1685 ComputeStateEngine( CLEAN );
1687 if (algo && !algo->NeedDiscreteBoundary())
1688 cleanDependsOn( algo ); // clean sub-meshes with event CLEAN
1690 case COMPUTE: // nothing to do
1692 case COMPUTE_CANCELED: // nothing to do
1695 cleanDependants(); // clean sub-meshes, dependant on this one, with event CLEAN
1696 removeSubMeshElementsAndNodes();
1697 _computeState = NOT_READY;
1698 if ( _algoState == HYP_OK )
1699 _computeState = READY_TO_COMPUTE;
1701 case SUBMESH_COMPUTED: // nothing to do
1703 case SUBMESH_RESTORED:
1704 ComputeStateEngine( CHECK_COMPUTE_STATE );
1705 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1707 if (algo) algo->SubmeshRestored( this );
1709 case MESH_ENTITY_REMOVED:
1710 updateDependantsState ( CHECK_COMPUTE_STATE );
1711 ComputeStateEngine ( CHECK_COMPUTE_STATE );
1712 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1714 case CHECK_COMPUTE_STATE:
1715 if ( !IsMeshComputed() ) {
1716 if (_algoState == HYP_OK)
1717 _computeState = READY_TO_COMPUTE;
1719 _computeState = NOT_READY;
1722 case SUBMESH_LOADED:
1723 // already treated event, thanks to which _computeState == COMPUTE_OK
1731 // ----------------------------------------------------------------------
1733 case FAILED_TO_COMPUTE:
1736 case MODIF_ALGO_STATE:
1738 ComputeStateEngine( CLEAN );
1740 if (algo && !algo->NeedDiscreteBoundary())
1741 cleanDependsOn( algo ); // clean sub-meshes with event CLEAN
1742 if (_algoState == HYP_OK)
1743 _computeState = READY_TO_COMPUTE;
1745 _computeState = NOT_READY;
1747 case COMPUTE: // nothing to do
1748 case COMPUTE_SUBMESH:
1750 case COMPUTE_CANCELED:
1753 algo->CancelCompute();
1757 cleanDependants(); // submeshes dependent on me should be cleaned as well
1758 removeSubMeshElementsAndNodes();
1760 case SUBMESH_COMPUTED: // allow retry compute
1761 if ( IsEmpty() ) // 23061
1763 if (_algoState == HYP_OK)
1764 _computeState = READY_TO_COMPUTE;
1766 _computeState = NOT_READY;
1769 case SUBMESH_RESTORED:
1770 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1772 case MESH_ENTITY_REMOVED:
1774 case CHECK_COMPUTE_STATE:
1775 if ( IsMeshComputed() )
1776 _computeState = COMPUTE_OK;
1778 if (_algoState == HYP_OK)
1779 _computeState = READY_TO_COMPUTE;
1781 _computeState = NOT_READY;
1783 // case SUBMESH_LOADED:
1791 // ----------------------------------------------------------------------
1797 notifyListenersOnEvent( event, COMPUTE_EVENT );
1803 //=============================================================================
1807 //=============================================================================
1809 bool SMESH_subMesh::Evaluate(MapShapeNbElems& aResMap)
1811 _computeError.reset();
1815 if (_subShape.ShapeType() == TopAbs_VERTEX) {
1816 vector<int> aVec(SMDSEntity_Last,0);
1817 aVec[SMDSEntity_Node] = 1;
1818 aResMap.insert(make_pair(this,aVec));
1822 //SMESH_Gen *gen = _father->GetGen();
1823 SMESH_Algo *algo = 0;
1824 SMESH_Hypothesis::Hypothesis_Status hyp_status;
1827 if(algo && !aResMap.count(this) )
1829 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1830 if (!ret) return false;
1832 if (_father->HasShapeToMesh() && algo->NeedDiscreteBoundary())
1834 // check submeshes needed
1835 bool subMeshEvaluated = true;
1836 int dimToCheck = SMESH_Gen::GetShapeDim( _subShape ) - 1;
1837 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,/*complexShapeFirst=*/true);
1838 while ( smIt->more() && subMeshEvaluated )
1840 SMESH_subMesh* sm = smIt->next();
1841 int dim = SMESH_Gen::GetShapeDim( sm->GetSubShape() );
1842 if (dim < dimToCheck) break; // the rest subMeshes are all of less dimension
1843 const vector<int> & nbs = aResMap[ sm ];
1844 subMeshEvaluated = (std::accumulate( nbs.begin(), nbs.end(), 0 ) > 0 );
1846 if ( !subMeshEvaluated )
1849 _computeError = SMESH_ComputeError::New(COMPERR_OK,"",algo);
1850 ret = algo->Evaluate((*_father), _subShape, aResMap);
1852 aResMap.insert( make_pair( this,vector<int>(0)));
1859 //=======================================================================
1861 * \brief Update compute_state by _computeError and send proper events to
1862 * dependent submeshes
1863 * \retval bool - true if _computeError is NOT set
1865 //=======================================================================
1867 bool SMESH_subMesh::checkComputeError(SMESH_Algo* theAlgo,
1868 const bool theComputeOK,
1869 const TopoDS_Shape& theShape)
1871 bool noErrors = true;
1873 if ( !theShape.IsNull() )
1875 // Check state of submeshes
1876 if ( !theAlgo->NeedDiscreteBoundary())
1878 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1879 while ( smIt->more() )
1880 if ( !smIt->next()->checkComputeError( theAlgo, theComputeOK ))
1884 // Check state of neighbours
1885 if ( !theAlgo->OnlyUnaryInput() &&
1886 theShape.ShapeType() == TopAbs_COMPOUND &&
1887 !theShape.IsSame( _subShape ))
1889 for (TopoDS_Iterator subIt( theShape ); subIt.More(); subIt.Next()) {
1890 SMESH_subMesh* sm = _father->GetSubMesh( subIt.Value() );
1892 if ( !sm->checkComputeError( theAlgo, theComputeOK, sm->GetSubShape() ))
1894 updateDependantsState( SUBMESH_COMPUTED ); // send event SUBMESH_COMPUTED
1901 // Set my _computeState
1903 if ( !_computeError || _computeError->IsOK() )
1905 // no error description is set to this sub-mesh, check if any mesh is computed
1906 _computeState = IsMeshComputed() ? COMPUTE_OK : FAILED_TO_COMPUTE;
1907 if ( _computeState != COMPUTE_OK )
1909 if ( _subShape.ShapeType() == TopAbs_EDGE &&
1910 SMESH_Algo::isDegenerated( TopoDS::Edge( _subShape )) )
1911 _computeState = COMPUTE_OK;
1912 else if ( theComputeOK )
1913 _computeError = SMESH_ComputeError::New(COMPERR_NO_MESH_ON_SHAPE,"",theAlgo);
1917 if ( _computeError && !_computeError->IsOK() )
1919 if ( !_computeError->myAlgo )
1920 _computeError->myAlgo = theAlgo;
1924 text << theAlgo->GetName() << " failed on sub-shape #" << _Id << " with error ";
1925 if (_computeError->IsCommon() )
1926 text << _computeError->CommonName();
1928 text << _computeError->myName;
1929 if ( _computeError->myComment.size() > 0 )
1930 text << " \"" << _computeError->myComment << "\"";
1934 _computeState = _computeError->IsKO() ? FAILED_TO_COMPUTE : COMPUTE_OK;
1942 //=======================================================================
1943 //function : updateSubMeshState
1945 //=======================================================================
1947 void SMESH_subMesh::updateSubMeshState(const compute_state theState)
1949 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1950 while ( smIt->more() )
1951 smIt->next()->_computeState = theState;
1954 //=======================================================================
1955 //function : ComputeSubMeshStateEngine
1957 //=======================================================================
1959 void SMESH_subMesh::ComputeSubMeshStateEngine(int event, const bool includeSelf)
1961 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(includeSelf,false);
1962 while ( smIt->more() )
1963 smIt->next()->ComputeStateEngine(event);
1966 //=======================================================================
1967 //function : updateDependantsState
1969 //=======================================================================
1971 void SMESH_subMesh::updateDependantsState(const compute_event theEvent)
1973 const std::vector< SMESH_subMesh * > & ancestors = GetAncestors();
1974 for ( size_t iA = 0; iA < ancestors.size(); ++iA )
1976 ancestors[ iA ]->ComputeStateEngine( theEvent );
1980 //=======================================================================
1981 //function : cleanDependants
1983 //=======================================================================
1985 void SMESH_subMesh::cleanDependants()
1987 int dimToClean = SMESH_Gen::GetShapeDim( _subShape ) + 1;
1989 const std::vector< SMESH_subMesh * > & ancestors = GetAncestors();
1990 for ( size_t iA = 0; iA < ancestors.size(); ++iA )
1992 const TopoDS_Shape& ancestor = ancestors[ iA ]->GetSubShape();
1993 if ( SMESH_Gen::GetShapeDim( ancestor ) == dimToClean )
1995 // PAL8021. do not go upper than SOLID, else ComputeStateEngine(CLEAN)
1996 // will erase mesh on other shapes in a compound
1997 if ( ancestor.ShapeType() >= TopAbs_SOLID &&
1998 !ancestors[ iA ]->IsEmpty() ) // prevent infinite CLEAN via event lesteners
1999 ancestors[ iA ]->ComputeStateEngine(CLEAN);
2004 //=======================================================================
2005 //function : removeSubMeshElementsAndNodes
2007 //=======================================================================
2009 void SMESH_subMesh::removeSubMeshElementsAndNodes()
2011 cleanSubMesh( this );
2013 // algo may bind a submesh not to _subShape, eg 3D algo
2014 // sets nodes on SHELL while _subShape may be SOLID
2016 int dim = SMESH_Gen::GetShapeDim( _subShape );
2017 int type = _subShape.ShapeType() + 1;
2018 for ( ; type <= TopAbs_EDGE; type++) {
2019 if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
2021 TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
2022 for ( ; exp.More(); exp.Next() )
2023 cleanSubMesh( _father->GetSubMeshContaining( exp.Current() ));
2030 //=======================================================================
2031 //function : getCollection
2032 //purpose : return a shape containing all sub-shapes of the MainShape that can be
2033 // meshed at once along with _subShape
2034 //=======================================================================
2036 TopoDS_Shape SMESH_subMesh::getCollection(SMESH_Gen * theGen,
2037 SMESH_Algo* theAlgo,
2038 bool & theSubComputed,
2039 bool & theSubFailed,
2040 std::vector<SMESH_subMesh*>& theSubs)
2042 theSubComputed = SubMeshesComputed( & theSubFailed );
2044 TopoDS_Shape mainShape = _father->GetMeshDS()->ShapeToMesh();
2046 if ( mainShape.IsSame( _subShape ))
2049 const bool skipAuxHyps = false;
2050 list<const SMESHDS_Hypothesis*> aUsedHyp =
2051 theAlgo->GetUsedHypothesis( *_father, _subShape, skipAuxHyps ); // copy
2053 // put in a compound all shapes with the same hypothesis assigned
2054 // and a good ComputeState
2056 TopoDS_Compound aCompound;
2057 BRep_Builder aBuilder;
2058 aBuilder.MakeCompound( aCompound );
2062 SMESH_subMeshIteratorPtr smIt = _father->GetSubMesh( mainShape )->getDependsOnIterator(false);
2063 while ( smIt->more() )
2065 SMESH_subMesh* subMesh = smIt->next();
2066 const TopoDS_Shape& S = subMesh->_subShape;
2067 if ( S.ShapeType() != this->_subShape.ShapeType() )
2069 theSubs.push_back( subMesh );
2070 if ( subMesh == this )
2072 aBuilder.Add( aCompound, S );
2074 else if ( subMesh->GetComputeState() == READY_TO_COMPUTE )
2076 SMESH_Algo* anAlgo = subMesh->GetAlgo();
2077 if (( anAlgo->IsSameName( *theAlgo )) && // same algo
2078 ( anAlgo->GetUsedHypothesis( *_father, S, skipAuxHyps ) == aUsedHyp )) // same hyps
2080 aBuilder.Add( aCompound, S );
2081 if ( !subMesh->SubMeshesComputed() )
2082 theSubComputed = false;
2090 //=======================================================================
2091 //function : getSimilarAttached
2092 //purpose : return a hypothesis attached to theShape.
2093 // If theHyp is provided, similar but not same hypotheses
2094 // is returned; else only applicable ones having theHypType
2096 //=======================================================================
2098 const SMESH_Hypothesis* SMESH_subMesh::getSimilarAttached(const TopoDS_Shape& theShape,
2099 const SMESH_Hypothesis * theHyp,
2100 const int theHypType)
2102 SMESH_HypoFilter hypoKind;
2103 hypoKind.Init( hypoKind.HasType( theHyp ? theHyp->GetType() : theHypType ));
2105 hypoKind.And ( hypoKind.HasDim( theHyp->GetDim() ));
2106 hypoKind.AndNot( hypoKind.Is( theHyp ));
2107 if ( theHyp->IsAuxiliary() )
2108 hypoKind.And( hypoKind.HasName( theHyp->GetName() ));
2110 hypoKind.AndNot( hypoKind.IsAuxiliary());
2113 hypoKind.And( hypoKind.IsApplicableTo( theShape ));
2116 return _father->GetHypothesis( theShape, hypoKind, false );
2119 //=======================================================================
2120 //function : CheckConcurentHypothesis
2121 //purpose : check if there are several applicable hypothesis attached to
2123 //=======================================================================
2125 SMESH_Hypothesis::Hypothesis_Status
2126 SMESH_subMesh::CheckConcurentHypothesis (const int theHypType)
2128 MESSAGE ("SMESH_subMesh::CheckConcurentHypothesis");
2130 // is there local hypothesis on me?
2131 if ( getSimilarAttached( _subShape, 0, theHypType ) )
2132 return SMESH_Hypothesis::HYP_OK;
2135 TopoDS_Shape aPrevWithHyp;
2136 const SMESH_Hypothesis* aPrevHyp = 0;
2137 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
2138 for (; it.More(); it.Next())
2140 const TopoDS_Shape& ancestor = it.Value();
2141 const SMESH_Hypothesis* hyp = getSimilarAttached( ancestor, 0, theHypType );
2144 if ( aPrevWithHyp.IsNull() || aPrevWithHyp.IsSame( ancestor ))
2146 aPrevWithHyp = ancestor;
2149 else if ( aPrevWithHyp.ShapeType() == ancestor.ShapeType() && aPrevHyp != hyp )
2150 return SMESH_Hypothesis::HYP_CONCURENT;
2152 return SMESH_Hypothesis::HYP_OK;
2155 return SMESH_Hypothesis::HYP_OK;
2158 //================================================================================
2160 * \brief Constructor of OwnListenerData
2162 //================================================================================
2164 SMESH_subMesh::OwnListenerData::OwnListenerData( SMESH_subMesh* sm, EventListener* el):
2166 myMeshID( sm ? sm->GetFather()->GetId() : -1 ),
2167 mySubMeshID( sm ? sm->GetId() : -1 ),
2172 //================================================================================
2174 * \brief Sets an event listener and its data to a submesh
2175 * \param listener - the listener to store
2176 * \param data - the listener data to store
2177 * \param where - the submesh to store the listener and it's data
2179 * It remembers the submesh where it puts the listener in order to delete
2180 * them when HYP_OK algo_state is lost
2181 * After being set, event listener is notified on each event of where submesh.
2183 //================================================================================
2185 void SMESH_subMesh::SetEventListener(EventListener* listener,
2186 EventListenerData* data,
2187 SMESH_subMesh* where)
2189 if ( listener && where ) {
2190 where->setEventListener( listener, data );
2191 _ownListeners.push_back( OwnListenerData( where, listener ));
2195 //================================================================================
2197 * \brief Sets an event listener and its data to a submesh
2198 * \param listener - the listener to store
2199 * \param data - the listener data to store
2201 * After being set, event listener is notified on each event of a submesh.
2203 //================================================================================
2205 void SMESH_subMesh::setEventListener(EventListener* listener,
2206 EventListenerData* data)
2208 map< EventListener*, EventListenerData* >::iterator l_d =
2209 _eventListeners.find( listener );
2210 if ( l_d != _eventListeners.end() ) {
2211 EventListenerData* curData = l_d->second;
2212 if ( curData && curData != data && curData->IsDeletable() )
2218 for ( l_d = _eventListeners.begin(); l_d != _eventListeners.end(); ++l_d )
2219 if ( listener->GetName() == l_d->first->GetName() )
2221 EventListenerData* curData = l_d->second;
2222 if ( curData && curData != data && curData->IsDeletable() )
2224 if ( l_d->first != listener && l_d->first->IsDeletable() )
2226 _eventListeners.erase( l_d );
2229 _eventListeners.insert( make_pair( listener, data ));
2233 //================================================================================
2235 * \brief Return an event listener data
2236 * \param listener - the listener whose data is
2237 * \param myOwn - if \c true, returns a listener set by this sub-mesh,
2238 * else returns a listener listening to events of this sub-mesh
2239 * \retval EventListenerData* - found data, maybe NULL
2241 //================================================================================
2243 EventListenerData* SMESH_subMesh::GetEventListenerData(EventListener* listener,
2244 const bool myOwn) const
2248 list< OwnListenerData >::const_iterator d;
2249 for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
2251 if ( d->myListener == listener && _father->MeshExists( d->myMeshID ))
2252 return d->mySubMesh->GetEventListenerData( listener, !myOwn );
2257 map< EventListener*, EventListenerData* >::const_iterator l_d =
2258 _eventListeners.find( listener );
2259 if ( l_d != _eventListeners.end() )
2265 //================================================================================
2267 * \brief Return an event listener data
2268 * \param listenerName - the listener name
2269 * \param myOwn - if \c true, returns a listener set by this sub-mesh,
2270 * else returns a listener listening to events of this sub-mesh
2271 * \retval EventListenerData* - found data, maybe NULL
2273 //================================================================================
2275 EventListenerData* SMESH_subMesh::GetEventListenerData(const string& listenerName,
2276 const bool myOwn) const
2280 list< OwnListenerData >::const_iterator d;
2281 for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
2283 if ( _father->MeshExists( d->myMeshID ) && listenerName == d->myListener->GetName())
2284 return d->mySubMesh->GetEventListenerData( listenerName, !myOwn );
2289 map< EventListener*, EventListenerData* >::const_iterator l_d = _eventListeners.begin();
2290 for ( ; l_d != _eventListeners.end(); ++l_d )
2291 if ( listenerName == l_d->first->GetName() )
2297 //================================================================================
2299 * \brief Notify stored event listeners on the occured event
2300 * \param event - algo_event or compute_event itself
2301 * \param eventType - algo_event or compute_event
2302 * \param hyp - hypothesis, if eventType is algo_event
2304 //================================================================================
2306 void SMESH_subMesh::notifyListenersOnEvent( const int event,
2307 const event_type eventType,
2308 SMESH_Hypothesis* hyp)
2310 list< pair< EventListener*, EventListenerData* > > eventListeners( _eventListeners.begin(),
2311 _eventListeners.end());
2312 list< pair< EventListener*, EventListenerData* > >::iterator l_d = eventListeners.begin();
2313 for ( ; l_d != eventListeners.end(); ++l_d )
2315 std::pair< EventListener*, EventListenerData* > li_da = *l_d;
2316 if ( !_eventListeners.count( li_da.first )) continue;
2318 if ( li_da.first->myBusySM.insert( this ).second )
2320 const bool isDeletable = li_da.first->IsDeletable();
2322 li_da.first->ProcessEvent( event, eventType, this, li_da.second, hyp );
2324 if ( !isDeletable || _eventListeners.count( li_da.first ))
2325 li_da.first->myBusySM.erase( this ); // a listener is hopefully not dead
2330 //================================================================================
2332 * \brief Unregister the listener and delete listener's data
2333 * \param listener - the event listener
2335 //================================================================================
2337 void SMESH_subMesh::DeleteEventListener(EventListener* listener)
2339 map< EventListener*, EventListenerData* >::iterator l_d =
2340 _eventListeners.find( listener );
2341 if ( l_d != _eventListeners.end() && l_d->first )
2343 if ( l_d->second && l_d->second->IsDeletable() )
2347 l_d->first->myBusySM.erase( this );
2348 if ( l_d->first->IsDeletable() )
2350 l_d->first->BeforeDelete( this, l_d->second );
2353 _eventListeners.erase( l_d );
2357 //================================================================================
2359 * \brief Delete event listeners depending on algo of this submesh
2361 //================================================================================
2363 void SMESH_subMesh::deleteOwnListeners()
2365 list< OwnListenerData >::iterator d;
2366 for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
2368 SMESH_Mesh* mesh = _father->FindMesh( d->myMeshID );
2369 if ( !mesh || !mesh->GetSubMeshContaining( d->mySubMeshID ))
2371 d->mySubMesh->DeleteEventListener( d->myListener );
2373 _ownListeners.clear();
2376 //=======================================================================
2377 //function : loadDependentMeshes
2378 //purpose : loads dependent meshes on SUBMESH_LOADED event
2379 //=======================================================================
2381 void SMESH_subMesh::loadDependentMeshes()
2383 list< OwnListenerData >::iterator d;
2384 for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
2385 if ( _father != d->mySubMesh->_father )
2386 d->mySubMesh->_father->Load();
2388 // map< EventListener*, EventListenerData* >::iterator l_d = _eventListeners.begin();
2389 // for ( ; l_d != _eventListeners.end(); ++l_d )
2390 // if ( l_d->second )
2392 // const list<SMESH_subMesh*>& smList = l_d->second->mySubMeshes;
2393 // list<SMESH_subMesh*>::const_iterator sm = smList.begin();
2394 // for ( ; sm != smList.end(); ++sm )
2395 // if ( _father != (*sm)->_father )
2396 // (*sm)->_father->Load();
2400 //================================================================================
2402 * \brief Do something on a certain event
2403 * \param event - algo_event or compute_event itself
2404 * \param eventType - algo_event or compute_event
2405 * \param subMesh - the submesh where the event occures
2406 * \param data - listener data stored in the subMesh
2407 * \param hyp - hypothesis, if eventType is algo_event
2409 * The base implementation translates CLEAN event to the subMesh
2410 * stored in listener data. Also it sends SUBMESH_COMPUTED event in case of
2411 * successful COMPUTE event.
2413 //================================================================================
2415 void SMESH_subMeshEventListener::ProcessEvent(const int event,
2416 const int eventType,
2417 SMESH_subMesh* subMesh,
2418 EventListenerData* data,
2419 const SMESH_Hypothesis* /*hyp*/)
2421 if ( data && !data->mySubMeshes.empty() &&
2422 eventType == SMESH_subMesh::COMPUTE_EVENT)
2424 ASSERT( data->mySubMeshes.front() != subMesh );
2425 list<SMESH_subMesh*>::iterator smIt = data->mySubMeshes.begin();
2426 list<SMESH_subMesh*>::iterator smEnd = data->mySubMeshes.end();
2428 case SMESH_subMesh::CLEAN:
2429 for ( ; smIt != smEnd; ++ smIt)
2430 (*smIt)->ComputeStateEngine( event );
2432 case SMESH_subMesh::COMPUTE:
2433 case SMESH_subMesh::COMPUTE_SUBMESH:
2434 if ( subMesh->GetComputeState() == SMESH_subMesh::COMPUTE_OK )
2435 for ( ; smIt != smEnd; ++ smIt)
2436 (*smIt)->ComputeStateEngine( SMESH_subMesh::SUBMESH_COMPUTED );
2445 //================================================================================
2447 * \brief Iterator over submeshes and optionally prepended or appended one
2449 //================================================================================
2451 struct _Iterator : public SMDS_Iterator<SMESH_subMesh*>
2453 _Iterator(SMDS_Iterator<SMESH_subMesh*>* subIt,
2454 SMESH_subMesh* prepend,
2455 SMESH_subMesh* append): myIt(subIt),myAppend(append)
2457 myCur = prepend ? prepend : myIt->more() ? myIt->next() : append;
2458 if ( myCur == append ) append = 0;
2460 /// Return true if and only if there are other object in this iterator
2465 /// Return the current object and step to the next one
2466 virtual SMESH_subMesh* next()
2468 SMESH_subMesh* res = myCur;
2469 if ( myIt->more() ) { myCur = myIt->next(); }
2470 else { myCur = myAppend; myAppend = 0; }
2477 SMESH_subMesh *myAppend, *myCur;
2478 SMDS_Iterator<SMESH_subMesh*> *myIt;
2482 //================================================================================
2484 * \brief Return iterator on the submeshes this one depends on
2485 * \param includeSelf - this submesh to be returned also
2486 * \param reverse - if true, complex shape submeshes go first
2488 //================================================================================
2490 SMESH_subMeshIteratorPtr SMESH_subMesh::getDependsOnIterator(const bool includeSelf,
2491 const bool reverse) const
2493 SMESH_subMesh *me = (SMESH_subMesh*) this;
2494 SMESH_subMesh *prepend=0, *append=0;
2495 if ( includeSelf ) {
2496 if ( reverse ) prepend = me;
2499 typedef map < int, SMESH_subMesh * > TMap;
2502 return SMESH_subMeshIteratorPtr
2503 ( new _Iterator( new SMDS_mapReverseIterator<TMap>( me->DependsOn() ), prepend, append ));
2506 return SMESH_subMeshIteratorPtr
2507 ( new _Iterator( new SMDS_mapIterator<TMap>( me->DependsOn() ), prepend, append ));
2511 //================================================================================
2513 * \brief Returns ancestor sub-meshes. Finds them if not yet found.
2515 //================================================================================
2517 const std::vector< SMESH_subMesh * > & SMESH_subMesh::GetAncestors() const
2519 if ( _ancestors.empty() &&
2520 !_subShape.IsSame( _father->GetShapeToMesh() ))
2522 const TopTools_ListOfShape& ancShapes = _father->GetAncestors( _subShape );
2524 SMESH_subMesh* me = const_cast< SMESH_subMesh* >( this );
2525 me->_ancestors.reserve( ancShapes.Extent() );
2527 TopTools_MapOfShape map;
2529 for ( TopTools_ListIteratorOfListOfShape it( ancShapes ); it.More(); it.Next() )
2530 if ( SMESH_subMesh* sm = _father->GetSubMeshContaining( it.Value() ))
2531 if ( map.Add( it.Value() ))
2532 me->_ancestors.push_back( sm );
2538 //================================================================================
2540 * \brief Clears the vector of ancestor sub-meshes
2542 //================================================================================
2544 void SMESH_subMesh::ClearAncestors()
2549 //================================================================================
2551 * \brief Find common submeshes (based on shared sub-shapes with other
2552 * \param theOther submesh to check
2553 * \param theSetOfCommon set of common submesh
2555 //================================================================================
2557 bool SMESH_subMesh::FindIntersection(const SMESH_subMesh* theOther,
2558 std::set<const SMESH_subMesh*>& theSetOfCommon ) const
2560 int oldNb = theSetOfCommon.size();
2562 // check main submeshes
2563 const map <int, SMESH_subMesh*>::const_iterator otherEnd = theOther->_mapDepend.end();
2564 if ( theOther->_mapDepend.find(this->GetId()) != otherEnd )
2565 theSetOfCommon.insert( this );
2566 if ( _mapDepend.find(theOther->GetId()) != _mapDepend.end() )
2567 theSetOfCommon.insert( theOther );
2569 // check common submeshes
2570 map <int, SMESH_subMesh*>::const_iterator mapIt = _mapDepend.begin();
2571 for( ; mapIt != _mapDepend.end(); mapIt++ )
2572 if ( theOther->_mapDepend.find((*mapIt).first) != otherEnd )
2573 theSetOfCommon.insert( (*mapIt).second );
2574 return oldNb < theSetOfCommon.size();