1 // Copyright (C) 2007-2014 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 //=============================================================================
408 //=============================================================================
410 // bool SMESH_subMesh::SubMeshesReady()
412 // bool subMeshesReady = true;
413 // SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,true);
414 // while ( smIt->more() ) {
415 // SMESH_subMesh *sm = smIt->next();
416 // bool computeOk = (sm->GetComputeState() == COMPUTE_OK ||
417 // sm->GetComputeState() == READY_TO_COMPUTE);
420 // subMeshesReady = false;
421 // SCRUTE(sm->GetId());
425 // return subMeshesReady;
428 //=============================================================================
430 * Construct dependence on first level subMeshes. complex shapes (compsolid,
431 * shell, wire) are not analysed the same way as simple shapes (solid, face,
433 * For collection shapes (compsolid, shell, wire) prepare a list of submeshes
434 * with possible multiples occurences. Multiples occurences corresponds to
435 * internal frontiers within shapes of the collection and must not be keeped.
436 * See FinalizeDependence.
438 //=============================================================================
440 const map < int, SMESH_subMesh * >& SMESH_subMesh::DependsOn()
442 if (_dependenceAnalysed)
445 //MESSAGE("SMESH_subMesh::DependsOn");
447 int type = _subShape.ShapeType();
451 case TopAbs_COMPOUND:
453 //MESSAGE("compound");
454 for (TopExp_Explorer exp(_subShape, TopAbs_SOLID); exp.More();exp.Next())
456 insertDependence(exp.Current());
458 for (TopExp_Explorer exp(_subShape, TopAbs_SHELL, TopAbs_SOLID); exp.More(); exp.Next())
460 if ( BRep_Tool::IsClosed(exp.Current() ))
461 insertDependence(exp.Current()); //only shell not in solid
463 for (TopExp_Explorer expF(exp.Current(), TopAbs_FACE); expF.More();expF.Next())
464 insertDependence(expF.Current()); // issue 0020959: HEXA_3D fails on shell
467 for (TopExp_Explorer exp(_subShape, TopAbs_FACE, TopAbs_SHELL); exp.More();exp.Next())
469 insertDependence(exp.Current());
471 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE, TopAbs_FACE); exp.More();exp.Next())
473 insertDependence(exp.Current());
475 for (TopExp_Explorer exp(_subShape, TopAbs_VERTEX, TopAbs_EDGE); exp.More();exp.Next())
477 insertDependence(exp.Current());
481 case TopAbs_COMPSOLID:
483 //MESSAGE("compsolid");
484 for (TopExp_Explorer exp(_subShape, TopAbs_SOLID); exp.More(); exp.Next())
486 insertDependence(exp.Current());
493 for (TopExp_Explorer exp(_subShape, TopAbs_FACE); exp.More(); exp.Next())
495 insertDependence(exp.Current());
502 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE); exp.More(); exp.Next())
504 insertDependence(exp.Current());
511 if(_father->HasShapeToMesh()) {
512 for (TopExp_Explorer exp(_subShape, TopAbs_FACE); exp.More();exp.Next())
514 insertDependence(exp.Current());
522 for (TopExp_Explorer exp(_subShape, TopAbs_EDGE); exp.More();exp.Next())
524 insertDependence(exp.Current());
531 for (TopExp_Explorer exp(_subShape, TopAbs_VERTEX); exp.More(); exp.Next())
533 insertDependence(exp.Current());
546 _dependenceAnalysed = true;
550 //================================================================================
552 * \brief Return a key for SMESH_subMesh::_mapDepend map
554 //================================================================================
557 int dependsOnMapKey( const SMESH_subMesh* sm )
559 int type = sm->GetSubShape().ShapeType();
560 int ordType = 9 - type; // 2 = Vertex, 8 = CompSolid
561 int cle = sm->GetId();
562 cle += 10000000 * ordType; // sort map by ordType then index
567 //=============================================================================
569 * For simple Shapes (solid, face, edge): add subMesh into dependence list.
571 //=============================================================================
573 void SMESH_subMesh::insertDependence(const TopoDS_Shape aSubShape)
575 SMESH_subMesh *aSubMesh = _father->GetSubMesh(aSubShape);
576 int cle = dependsOnMapKey( aSubMesh );
577 if ( _mapDepend.find( cle ) == _mapDepend.end())
579 _mapDepend[cle] = aSubMesh;
580 const map < int, SMESH_subMesh * > & subMap = aSubMesh->DependsOn();
581 _mapDepend.insert( subMap.begin(), subMap.end() );
585 //================================================================================
587 * \brief Return \c true if \a this sub-mesh depends on \a other
589 //================================================================================
591 bool SMESH_subMesh::DependsOn( const SMESH_subMesh* other ) const
593 return other ? _mapDepend.count( dependsOnMapKey( other )) : false;
596 //=============================================================================
598 * Return a shape of \a this sub-mesh
600 //=============================================================================
602 const TopoDS_Shape & SMESH_subMesh::GetSubShape() const
607 //=======================================================================
608 //function : CanAddHypothesis
609 //purpose : return true if theHypothesis can be attached to me:
610 // its dimention is checked
611 //=======================================================================
613 bool SMESH_subMesh::CanAddHypothesis(const SMESH_Hypothesis* theHypothesis) const
615 int aHypDim = theHypothesis->GetDim();
616 int aShapeDim = SMESH_Gen::GetShapeDim(_subShape);
617 // issue 21106. Forbid 3D mesh on the SHELL
618 // if (aHypDim == 3 && aShapeDim == 3) {
619 // // check case of open shell
620 // //if (_subShape.ShapeType() == TopAbs_SHELL && !_subShape.Closed())
621 // if (_subShape.ShapeType() == TopAbs_SHELL && !BRep_Tool::IsClosed(_subShape))
624 if ( aHypDim <= aShapeDim )
630 //=======================================================================
631 //function : IsApplicableHypotesis
633 //=======================================================================
635 bool SMESH_subMesh::IsApplicableHypotesis(const SMESH_Hypothesis* theHypothesis,
636 const TopAbs_ShapeEnum theShapeType)
638 if ( theHypothesis->GetType() > SMESHDS_Hypothesis::PARAM_ALGO)
641 if ( theHypothesis->GetShapeType() & (1<< theShapeType))
642 // issue 21106. Forbid 3D mesh on the SHELL
643 return !( theHypothesis->GetDim() == 3 && theShapeType == TopAbs_SHELL );
649 switch ( theShapeType ) {
654 return SMESH_Gen::GetShapeDim( theShapeType ) == theHypothesis->GetDim();
657 // Special case for algorithms, building 2D mesh on a whole shell.
658 // Before this fix there was a problem after restoring from study,
659 // because in that case algorithm is assigned before hypothesis
660 // (on shell in problem case) and hypothesis is checked on faces
661 // (because it is 2D), where we have NO_ALGO state.
662 // Now 2D hypothesis is also applicable to shells.
663 return (theHypothesis->GetDim() == 2 || theHypothesis->GetDim() == 3);
666 // case TopAbs_COMPSOLID:
667 // case TopAbs_COMPOUND:
673 //=============================================================================
677 //=============================================================================
679 SMESH_Hypothesis::Hypothesis_Status
680 SMESH_subMesh::AlgoStateEngine(int event, SMESH_Hypothesis * anHyp)
682 // **** les retour des evenement shape sont significatifs
683 // (add ou remove fait ou non)
684 // le retour des evenement father n'indiquent pas que add ou remove fait
686 SMESH_Hypothesis::Hypothesis_Status aux_ret, ret = SMESH_Hypothesis::HYP_OK;
688 SMESHDS_Mesh* meshDS =_father->GetMeshDS();
689 SMESH_Algo* algo = 0;
692 if (_subShape.ShapeType() == TopAbs_VERTEX )
694 if ( anHyp->GetDim() != 0) {
695 if (event == ADD_HYP || event == ADD_ALGO)
696 return SMESH_Hypothesis::HYP_BAD_DIM;
698 return SMESH_Hypothesis::HYP_OK;
701 else if ( _algoState == HYP_OK ) {
702 // update default _algoState
703 if ( event != REMOVE_FATHER_ALGO )
705 _algoState = NO_ALGO;
708 _algoState = MISSING_HYP;
709 if ( event == REMOVE_FATHER_HYP ||
710 algo->CheckHypothesis(*_father,_subShape, aux_ret))
717 int oldAlgoState = _algoState;
718 bool modifiedHyp = (event == MODIF_HYP); // if set to true, force event MODIF_ALGO_STATE
719 SMESH_Algo* algoRequiringCleaning = 0;
721 bool isApplicableHyp = IsApplicableHypotesis( anHyp );
723 if (event == ADD_ALGO || event == ADD_FATHER_ALGO)
725 // -------------------------------------------
726 // check if a shape needed by algo is present
727 // -------------------------------------------
728 algo = static_cast< SMESH_Algo* >( anHyp );
729 if ( !_father->HasShapeToMesh() && algo->NeedShape() )
730 return SMESH_Hypothesis::HYP_NEED_SHAPE;
731 // ----------------------
732 // check mesh conformity
733 // ----------------------
734 if (isApplicableHyp && !_father->IsNotConformAllowed() && !IsConform( algo ))
735 return SMESH_Hypothesis::HYP_NOTCONFORM;
737 // check if all-dimensional algo is hidden by other local one
738 if ( event == ADD_ALGO ) {
739 SMESH_HypoFilter filter( SMESH_HypoFilter::HasType( algo->GetType() ));
740 filter.Or( SMESH_HypoFilter::HasType( algo->GetType()+1 ));
741 filter.Or( SMESH_HypoFilter::HasType( algo->GetType()+2 ));
742 if ( SMESH_Algo * curAlgo = (SMESH_Algo*)_father->GetHypothesis( this, filter, true ))
743 if ( !curAlgo->NeedDiscreteBoundary() )
744 algoRequiringCleaning = curAlgo;
748 // ----------------------------------
749 // add a hypothesis to DS if possible
750 // ----------------------------------
751 if (event == ADD_HYP || event == ADD_ALGO)
753 if ( ! CanAddHypothesis( anHyp )) // check dimension
754 return SMESH_Hypothesis::HYP_BAD_DIM;
756 if ( /*!anHyp->IsAuxiliary() &&*/ getSimilarAttached( _subShape, anHyp ) )
757 return SMESH_Hypothesis::HYP_ALREADY_EXIST;
759 if ( !meshDS->AddHypothesis(_subShape, anHyp))
760 return SMESH_Hypothesis::HYP_ALREADY_EXIST;
763 // --------------------------
764 // remove a hypothesis from DS
765 // --------------------------
766 if (event == REMOVE_HYP || event == REMOVE_ALGO)
768 if (!meshDS->RemoveHypothesis(_subShape, anHyp))
769 return SMESH_Hypothesis::HYP_OK; // nothing changes
771 if (event == REMOVE_ALGO)
773 algo = dynamic_cast<SMESH_Algo*> (anHyp);
774 if (!algo->NeedDiscreteBoundary())
775 algoRequiringCleaning = algo;
779 // ------------------
780 // analyse algo state
781 // ------------------
782 if (!isApplicableHyp)
783 return ret; // not applicable hypotheses do not change algo state
788 // ----------------------------------------------------------------------
797 if (algo->CheckHypothesis((*_father),_subShape, aux_ret))
798 setAlgoState(HYP_OK);
799 else if ( algo->IsStatusFatal( aux_ret )) {
800 meshDS->RemoveHypothesis(_subShape, anHyp);
804 setAlgoState(MISSING_HYP);
811 case ADD_FATHER_ALGO: { // Algo just added in father
814 if ( algo == anHyp ) {
815 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret))
816 setAlgoState(HYP_OK);
818 setAlgoState(MISSING_HYP);
822 case REMOVE_FATHER_HYP:
824 case REMOVE_FATHER_ALGO: {
828 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
829 setAlgoState(HYP_OK);
831 setAlgoState(MISSING_HYP);
835 case MODIF_HYP: break;
842 // ----------------------------------------------------------------------
850 if ( algo->CheckHypothesis((*_father),_subShape, ret ))
851 setAlgoState(HYP_OK);
852 if (SMESH_Hypothesis::IsStatusFatal( ret ))
853 meshDS->RemoveHypothesis(_subShape, anHyp);
854 else if (!_father->IsUsedHypothesis( anHyp, this ))
856 meshDS->RemoveHypothesis(_subShape, anHyp);
857 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
861 case ADD_ALGO: { //already existing algo : on father ?
864 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))// ignore hyp status
865 setAlgoState(HYP_OK);
866 else if ( algo->IsStatusFatal( aux_ret )) {
867 meshDS->RemoveHypothesis(_subShape, anHyp);
871 setAlgoState(MISSING_HYP);
876 case REMOVE_ALGO: { // perhaps a father algo applies ?
878 if (algo == NULL) // no more algo applying on sub-shape...
880 setAlgoState(NO_ALGO);
884 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
885 setAlgoState(HYP_OK);
887 setAlgoState(MISSING_HYP);
891 case MODIF_HYP: // assigned hypothesis value may become good
892 case ADD_FATHER_HYP: {
895 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
896 setAlgoState(HYP_OK);
898 setAlgoState(MISSING_HYP);
901 case ADD_FATHER_ALGO: { // new father algo
904 if ( algo == anHyp ) {
905 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
906 setAlgoState(HYP_OK);
908 setAlgoState(MISSING_HYP);
912 case REMOVE_FATHER_HYP: // nothing to do
914 case REMOVE_FATHER_ALGO: {
916 if (algo == NULL) // no more applying algo on father
918 setAlgoState(NO_ALGO);
922 if ( algo->CheckHypothesis((*_father),_subShape , aux_ret ))
923 setAlgoState(HYP_OK);
925 setAlgoState(MISSING_HYP);
935 // ----------------------------------------------------------------------
943 if (!algo->CheckHypothesis((*_father),_subShape, ret ))
945 if ( !SMESH_Hypothesis::IsStatusFatal( ret ))
946 // ret should be fatal: anHyp was not added
947 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
949 else if (!_father->IsUsedHypothesis( anHyp, this ))
950 ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
952 if (SMESH_Hypothesis::IsStatusFatal( ret ))
954 MESSAGE("do not add extra hypothesis");
955 meshDS->RemoveHypothesis(_subShape, anHyp);
963 case ADD_ALGO: { //already existing algo : on father ?
965 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
966 // check if algo changes
968 f.Init( SMESH_HypoFilter::IsAlgo() );
969 f.And( SMESH_HypoFilter::IsApplicableTo( _subShape ));
970 f.AndNot( SMESH_HypoFilter::Is( algo ));
971 const SMESH_Hypothesis * prevAlgo = _father->GetHypothesis( this, f, true );
973 string(algo->GetName()) != string(prevAlgo->GetName()) )
977 setAlgoState(MISSING_HYP);
983 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
984 setAlgoState(HYP_OK);
986 setAlgoState(MISSING_HYP);
990 case REMOVE_ALGO: { // perhaps a father algo applies ?
992 if (algo == NULL) // no more algo applying on sub-shape...
994 setAlgoState(NO_ALGO);
998 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
999 // check if algo remains
1000 if ( anHyp != algo && strcmp( anHyp->GetName(), algo->GetName()) )
1004 setAlgoState(MISSING_HYP);
1008 case MODIF_HYP: // hypothesis value may become bad
1009 case ADD_FATHER_HYP: { // new father hypothesis ?
1012 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
1014 if (_father->IsUsedHypothesis( anHyp, this )) // new Hyp
1018 setAlgoState(MISSING_HYP);
1021 case ADD_FATHER_ALGO: {
1023 if ( algo == anHyp ) { // a new algo on father
1024 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
1025 // check if algo changes
1027 f.Init( SMESH_HypoFilter::IsAlgo() );
1028 f.And( SMESH_HypoFilter::IsApplicableTo( _subShape ));
1029 f.AndNot( SMESH_HypoFilter::Is( algo ));
1030 const SMESH_Hypothesis* prevAlgo = _father->GetHypothesis( this, f, true );
1032 string(algo->GetName()) != string(prevAlgo->GetName()) )
1036 setAlgoState(MISSING_HYP);
1040 case REMOVE_FATHER_HYP: {
1043 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
1044 // is there the same local hyp or maybe a new father algo applied?
1045 if ( !getSimilarAttached( _subShape, anHyp ) )
1049 setAlgoState(MISSING_HYP);
1052 case REMOVE_FATHER_ALGO: {
1053 // IPAL21346. Edges not removed when Netgen 1d-2d is removed from a SOLID.
1054 // CLEAN was not called at event REMOVE_ALGO because the algo is not applicable to SOLID.
1055 algo = dynamic_cast<SMESH_Algo*> (anHyp);
1056 if (!algo->NeedDiscreteBoundary())
1057 algoRequiringCleaning = algo;
1059 if (algo == NULL) // no more applying algo on father
1061 setAlgoState(NO_ALGO);
1065 if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
1066 // check if algo changes
1067 if ( string(algo->GetName()) != string( anHyp->GetName()) )
1071 setAlgoState(MISSING_HYP);
1081 // ----------------------------------------------------------------------
1088 // detect algorithm hiding
1090 if ( ret == SMESH_Hypothesis::HYP_OK &&
1091 ( event == ADD_ALGO || event == ADD_FATHER_ALGO ) &&
1092 algo->GetName() == anHyp->GetName() )
1095 SMESH_Gen* gen = _father->GetGen();
1096 const std::vector< SMESH_subMesh * > & ancestors = GetAncestors();
1097 for ( size_t iA = 0; ( ret == SMESH_Hypothesis::HYP_OK && iA < ancestors.size()); ++iA ) {
1098 if ( SMESH_Algo* upperAlgo = ancestors[ iA ]->GetAlgo() )
1099 if ( !upperAlgo->NeedDiscreteBoundary() && !upperAlgo->SupportSubmeshes())
1100 ret = SMESH_Hypothesis::HYP_HIDDEN_ALGO;
1103 if ( ret == SMESH_Hypothesis::HYP_OK &&
1104 !algo->NeedDiscreteBoundary() &&
1105 !algo->SupportSubmeshes())
1107 TopoDS_Shape algoAssignedTo, otherAssignedTo;
1108 gen->GetAlgo( this, &algoAssignedTo );
1109 map<int, SMESH_subMesh*>::reverse_iterator i_sm = _mapDepend.rbegin();
1110 for ( ; ( ret == SMESH_Hypothesis::HYP_OK && i_sm != _mapDepend.rend()) ; ++i_sm )
1111 if ( gen->GetAlgo( i_sm->second, &otherAssignedTo ) &&
1112 SMESH_MesherHelper::IsSubShape( /*sub=*/otherAssignedTo, /*main=*/algoAssignedTo ))
1113 ret = SMESH_Hypothesis::HYP_HIDING_ALGO;
1117 bool stateChange = ( _algoState != oldAlgoState );
1119 if ( stateChange && _algoState == HYP_OK ) // hyp becomes OK
1120 algo->SetEventListener( this );
1122 if ( event == REMOVE_ALGO || event == REMOVE_FATHER_ALGO )
1125 notifyListenersOnEvent( event, ALGO_EVENT, anHyp );
1127 if ( stateChange && oldAlgoState == HYP_OK ) { // hyp becomes KO
1128 deleteOwnListeners();
1129 SetIsAlwaysComputed( false );
1130 if (_subShape.ShapeType() == TopAbs_VERTEX ) {
1131 // restore default states
1132 _algoState = HYP_OK;
1133 _computeState = READY_TO_COMPUTE;
1137 if ( algoRequiringCleaning ) {
1138 // added or removed algo is all-dimensional
1139 ComputeStateEngine( CLEAN );
1140 cleanDependsOn( algoRequiringCleaning );
1141 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1144 if (stateChange || modifiedHyp)
1145 ComputeStateEngine(MODIF_ALGO_STATE);
1147 _realComputeCost = ( _algoState == HYP_OK ) ? computeCost() : 0;
1152 //=======================================================================
1153 //function : IsConform
1154 //purpose : check if a conform mesh will be produced by the Algo
1155 //=======================================================================
1157 bool SMESH_subMesh::IsConform(const SMESH_Algo* theAlgo)
1159 // MESSAGE( "SMESH_subMesh::IsConform" );
1160 if ( !theAlgo ) return false;
1162 // Suppose that theAlgo is applicable to _subShape, do not check it here
1163 //if ( !IsApplicableHypotesis( theAlgo )) return false;
1165 // check only algo that doesn't NeedDiscreteBoundary(): because mesh made
1166 // on a sub-shape will be ignored by theAlgo
1167 if ( theAlgo->NeedDiscreteBoundary() ||
1168 !theAlgo->OnlyUnaryInput() ) // all adjacent shapes will be meshed by this algo?
1171 SMESH_Gen* gen =_father->GetGen();
1173 // only local algo is to be checked
1174 //if ( gen->IsGlobalHypothesis( theAlgo, *_father ))
1175 if ( _subShape.ShapeType() == _father->GetMeshDS()->ShapeToMesh().ShapeType() )
1178 // check algo attached to adjacent shapes
1180 // loop on one level down sub-meshes
1181 TopoDS_Iterator itsub( _subShape );
1182 for (; itsub.More(); itsub.Next())
1184 // loop on adjacent subShapes
1185 const std::vector< SMESH_subMesh * > & ancestors = GetAncestors();
1186 for ( size_t iA = 0; iA < ancestors.size(); ++iA )
1188 const TopoDS_Shape& adjacent = ancestors[ iA ]->GetSubShape();
1189 if ( _subShape.IsSame( adjacent )) continue;
1190 if ( adjacent.ShapeType() != _subShape.ShapeType())
1193 // check algo attached to smAdjacent
1194 SMESH_Algo * algo = ancestors[ iA ]->GetAlgo();
1196 !algo->NeedDiscreteBoundary() &&
1197 algo->OnlyUnaryInput())
1198 return false; // NOT CONFORM MESH WILL BE PRODUCED
1205 //=============================================================================
1209 //=============================================================================
1211 void SMESH_subMesh::setAlgoState(algo_state state)
1216 //=============================================================================
1220 //=============================================================================
1221 SMESH_Hypothesis::Hypothesis_Status
1222 SMESH_subMesh::SubMeshesAlgoStateEngine(int event,
1223 SMESH_Hypothesis * anHyp)
1225 SMESH_Hypothesis::Hypothesis_Status ret = SMESH_Hypothesis::HYP_OK;
1226 //EAP: a wire (dim==1) should notify edges (dim==1)
1227 //EAP: int dim = SMESH_Gen::GetShapeDim(_subShape);
1228 //if (_subShape.ShapeType() < TopAbs_EDGE ) // wire,face etc
1230 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1231 while ( smIt->more() ) {
1232 SMESH_Hypothesis::Hypothesis_Status ret2 =
1233 smIt->next()->AlgoStateEngine(event, anHyp);
1241 //================================================================================
1243 * \brief Remove elements from sub-meshes.
1244 * \param algoRequiringCleaning - an all-dimensional algorithm whose presence
1245 * causes the cleaning.
1247 //================================================================================
1249 void SMESH_subMesh::cleanDependsOn( SMESH_Algo* algoRequiringCleaning/*=0*/ )
1251 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,
1252 /*complexShapeFirst=*/true);
1253 if ( _father->NbNodes() == 0 )
1255 while ( smIt->more() )
1256 smIt->next()->ComputeStateEngine(CHECK_COMPUTE_STATE);
1258 else if ( !algoRequiringCleaning || !algoRequiringCleaning->SupportSubmeshes() )
1260 while ( smIt->more() )
1261 smIt->next()->ComputeStateEngine(CLEAN);
1263 else if ( algoRequiringCleaning && algoRequiringCleaning->SupportSubmeshes() )
1265 SMESHDS_Mesh* meshDS = _father->GetMeshDS();
1267 // find sub-meshes to keep elements on
1268 set< SMESH_subMesh* > smToKeep;
1269 TopAbs_ShapeEnum prevShapeType = TopAbs_SHAPE;
1270 bool toKeepPrevShapeType = false;
1271 while ( smIt->more() )
1273 SMESH_subMesh* sm = smIt->next();
1274 sm->ComputeStateEngine(CHECK_COMPUTE_STATE);
1275 if ( !sm->IsEmpty() )
1277 const bool sameShapeType = ( prevShapeType == sm->GetSubShape().ShapeType() );
1278 bool keepSubMeshes = ( sameShapeType && toKeepPrevShapeType );
1279 if ( !sameShapeType )
1281 // check if the algo allows presence of global algos of dimension the algo
1282 // can generate it-self
1283 int shapeDim = SMESH_Gen::GetShapeDim( sm->GetSubShape() );
1284 keepSubMeshes = algoRequiringCleaning->NeedLowerHyps( shapeDim );
1285 prevShapeType = sm->GetSubShape().ShapeType();
1286 toKeepPrevShapeType = keepSubMeshes;
1288 if ( !keepSubMeshes )
1290 // look for an algo assigned to sm
1291 bool algoFound = false;
1292 const list<const SMESHDS_Hypothesis*>& hyps = meshDS->GetHypothesis( sm->_subShape );
1293 list<const SMESHDS_Hypothesis*>::const_iterator h = hyps.begin();
1294 for ( ; ( !algoFound && h != hyps.end() ); ++h )
1295 algoFound = ((*h)->GetType() != SMESHDS_Hypothesis::PARAM_ALGO );
1296 keepSubMeshes = algoFound;
1298 // remember all sub-meshes of sm
1299 if ( keepSubMeshes )
1301 SMESH_subMeshIteratorPtr smIt2 = getDependsOnIterator(false,true);
1302 while ( smIt2->more() )
1303 smToKeep.insert( smIt2->next() );
1308 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,true);
1309 while ( smIt->more() )
1311 SMESH_subMesh* sm = smIt->next();
1312 if ( !smToKeep.count( sm ))
1313 sm->ComputeStateEngine(CLEAN);
1318 //=============================================================================
1322 //=============================================================================
1324 void SMESH_subMesh::DumpAlgoState(bool isMain)
1326 // if (dim < 1) return;
1329 const map < int, SMESH_subMesh * >&subMeshes = DependsOn();
1331 map < int, SMESH_subMesh * >::const_iterator itsub;
1332 for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
1334 SMESH_subMesh *sm = (*itsub).second;
1335 sm->DumpAlgoState(false);
1338 //int type = _subShape.ShapeType();
1339 MESSAGE("dim = " << SMESH_Gen::GetShapeDim(_subShape) <<
1340 " type of shape " << _subShape.ShapeType());
1344 MESSAGE(" AlgoState = NO_ALGO");
1347 MESSAGE(" AlgoState = MISSING_HYP");
1350 MESSAGE(" AlgoState = HYP_OK");
1353 switch (_computeState)
1356 MESSAGE(" ComputeState = NOT_READY");
1358 case READY_TO_COMPUTE:
1359 MESSAGE(" ComputeState = READY_TO_COMPUTE");
1362 MESSAGE(" ComputeState = COMPUTE_OK");
1364 case FAILED_TO_COMPUTE:
1365 MESSAGE(" ComputeState = FAILED_TO_COMPUTE");
1370 //================================================================================
1372 * \brief Remove nodes and elements bound to submesh
1373 * \param subMesh - submesh containing nodes and elements
1375 //================================================================================
1377 static void cleanSubMesh( SMESH_subMesh * subMesh )
1380 if (SMESHDS_SubMesh * subMeshDS = subMesh->GetSubMeshDS()) {
1381 SMESHDS_Mesh * meshDS = subMesh->GetFather()->GetMeshDS();
1382 SMDS_ElemIteratorPtr ite = subMeshDS->GetElements();
1383 while (ite->more()) {
1384 const SMDS_MeshElement * elt = ite->next();
1385 //MESSAGE( " RM elt: "<<elt->GetID()<<" ( "<<elt->NbNodes()<<" )" );
1386 //meshDS->RemoveElement(elt);
1387 meshDS->RemoveFreeElement(elt, 0);
1390 SMDS_NodeIteratorPtr itn = subMeshDS->GetNodes();
1391 while (itn->more()) {
1392 const SMDS_MeshNode * node = itn->next();
1393 //MESSAGE( " RM node: "<<node->GetID());
1394 if ( node->NbInverseElements() == 0 )
1395 meshDS->RemoveFreeNode(node, 0);
1396 else // for StdMeshers_CompositeSegment_1D: node in one submesh, edge in another
1397 meshDS->RemoveNode(node);
1404 //=============================================================================
1408 //=============================================================================
1410 bool SMESH_subMesh::ComputeStateEngine(int event)
1413 case MODIF_ALGO_STATE:
1415 case COMPUTE_SUBMESH:
1416 //case COMPUTE_CANCELED:
1418 //case SUBMESH_COMPUTED:
1419 //case SUBMESH_RESTORED:
1420 //case SUBMESH_LOADED:
1421 //case MESH_ENTITY_REMOVED:
1422 //case CHECK_COMPUTE_STATE:
1423 _computeError.reset(); break;
1427 if ( event == CLEAN )
1428 _alwaysComputed = false; // Unset 'true' set by MergeNodes() (issue 0022182)
1430 if (_subShape.ShapeType() == TopAbs_VERTEX)
1432 _computeState = READY_TO_COMPUTE;
1433 SMESHDS_SubMesh* smDS = GetSubMeshDS();
1434 if ( smDS && smDS->NbNodes() )
1436 if ( event == CLEAN ) {
1438 cleanSubMesh( this );
1441 _computeState = COMPUTE_OK;
1443 else if (( event == COMPUTE || event == COMPUTE_SUBMESH )
1444 && !_alwaysComputed )
1446 const TopoDS_Vertex & V = TopoDS::Vertex( _subShape );
1447 gp_Pnt P = BRep_Tool::Pnt(V);
1448 if ( SMDS_MeshNode * n = _father->GetMeshDS()->AddNode(P.X(), P.Y(), P.Z()) ) {
1449 _father->GetMeshDS()->SetNodeOnVertex(n,_Id);
1450 _computeState = COMPUTE_OK;
1453 if ( event == MODIF_ALGO_STATE )
1457 SMESH_Gen *gen = _father->GetGen();
1458 SMESH_Algo *algo = 0;
1460 SMESH_Hypothesis::Hypothesis_Status hyp_status;
1461 //algo_state oldAlgoState = (algo_state) GetAlgoState();
1463 switch (_computeState)
1466 // ----------------------------------------------------------------------
1471 case MODIF_ALGO_STATE:
1473 if (algo && !algo->NeedDiscreteBoundary())
1474 cleanDependsOn( algo ); // clean sub-meshes with event CLEAN
1475 if ( _algoState == HYP_OK )
1476 _computeState = READY_TO_COMPUTE;
1478 case COMPUTE: // nothing to do
1479 case COMPUTE_SUBMESH:
1481 case COMPUTE_CANCELED: // nothing to do
1485 removeSubMeshElementsAndNodes();
1487 case SUBMESH_COMPUTED: // nothing to do
1489 case SUBMESH_RESTORED:
1490 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1492 case MESH_ENTITY_REMOVED:
1494 case SUBMESH_LOADED:
1495 loadDependentMeshes();
1496 ComputeSubMeshStateEngine( SUBMESH_LOADED );
1498 case CHECK_COMPUTE_STATE:
1499 if ( IsMeshComputed() )
1500 _computeState = COMPUTE_OK;
1508 // ----------------------------------------------------------------------
1510 case READY_TO_COMPUTE:
1513 case MODIF_ALGO_STATE:
1514 _computeState = NOT_READY;
1518 if (!algo->NeedDiscreteBoundary())
1519 cleanDependsOn( algo ); // clean sub-meshes with event CLEAN
1520 if ( _algoState == HYP_OK )
1521 _computeState = READY_TO_COMPUTE;
1525 case COMPUTE_SUBMESH:
1529 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1532 MESSAGE("***** verify compute state *****");
1533 _computeState = NOT_READY;
1534 setAlgoState(MISSING_HYP);
1537 TopoDS_Shape shape = _subShape;
1538 algo->SubMeshesToCompute().assign( 1, this );
1539 // check submeshes needed
1540 if (_father->HasShapeToMesh() ) {
1541 bool subComputed = false, subFailed = false;
1542 if (!algo->OnlyUnaryInput()) {
1543 if ( event == COMPUTE /*&&
1544 ( algo->NeedDiscreteBoundary() || algo->SupportSubmeshes() )*/)
1545 shape = getCollection( gen, algo, subComputed, subFailed, algo->SubMeshesToCompute());
1547 subComputed = SubMeshesComputed( & subFailed );
1550 subComputed = SubMeshesComputed();
1552 ret = ( algo->NeedDiscreteBoundary() ? subComputed :
1553 algo->SupportSubmeshes() ? !subFailed :
1554 ( !subComputed || _father->IsNotConformAllowed() ));
1557 _computeState = FAILED_TO_COMPUTE;
1558 if ( !algo->NeedDiscreteBoundary() && !subFailed )
1560 SMESH_ComputeError::New(COMPERR_BAD_INPUT_MESH,
1561 "Unexpected computed sub-mesh",algo);
1567 // to restore cout that may be redirected by algo
1568 std::streambuf* coutBuffer = std::cout.rdbuf();
1570 //cleanDependants(); for "UseExisting_*D" algos
1571 //removeSubMeshElementsAndNodes();
1572 loadDependentMeshes();
1574 _computeState = FAILED_TO_COMPUTE;
1575 _computeError = SMESH_ComputeError::New(COMPERR_OK,"",algo);
1579 algo->InitComputeError();
1581 MemoryReserve aMemoryReserve;
1582 SMDS_Mesh::CheckMemory();
1583 Kernel_Utils::Localizer loc;
1584 if ( !_father->HasShapeToMesh() ) // no shape
1586 SMESH_MesherHelper helper( *_father );
1587 helper.SetSubShape( shape );
1588 helper.SetElementsOnShape( true );
1589 ret = algo->Compute(*_father, &helper );
1593 ret = algo->Compute((*_father), shape);
1595 // algo can set _computeError of submesh
1596 _computeError = SMESH_ComputeError::Worst( _computeError, algo->GetComputeError() );
1598 catch ( ::SMESH_ComputeError& comperr ) {
1599 cout << " SMESH_ComputeError caught" << endl;
1600 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1601 *_computeError = comperr;
1603 catch ( std::bad_alloc& exc ) {
1604 MESSAGE("std::bad_alloc thrown inside algo->Compute()");
1605 if ( _computeError ) {
1606 _computeError->myName = COMPERR_MEMORY_PB;
1607 //_computeError->myComment = exc.what();
1609 cleanSubMesh( this );
1612 catch ( Standard_OutOfMemory& exc ) {
1613 MESSAGE("Standard_OutOfMemory thrown inside algo->Compute()");
1614 if ( _computeError ) {
1615 _computeError->myName = COMPERR_MEMORY_PB;
1616 //_computeError->myComment = exc.what();
1618 cleanSubMesh( this );
1619 throw std::bad_alloc();
1621 catch (Standard_Failure& ex) {
1622 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1623 _computeError->myName = COMPERR_OCC_EXCEPTION;
1624 _computeError->myComment += ex.DynamicType()->Name();
1625 if ( ex.GetMessageString() && strlen( ex.GetMessageString() )) {
1626 _computeError->myComment += ": ";
1627 _computeError->myComment += ex.GetMessageString();
1630 catch ( SALOME_Exception& S_ex ) {
1631 const int skipSalomeShift = 7; /* to skip "Salome " of
1632 "Salome Exception" prefix returned
1633 by SALOME_Exception::what() */
1634 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1635 _computeError->myName = COMPERR_SLM_EXCEPTION;
1636 _computeError->myComment = S_ex.what() + skipSalomeShift;
1638 catch ( std::exception& exc ) {
1639 if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1640 _computeError->myName = COMPERR_STD_EXCEPTION;
1641 _computeError->myComment = exc.what();
1644 if ( _computeError )
1645 _computeError->myName = COMPERR_EXCEPTION;
1649 std::cout.rdbuf( coutBuffer ); // restore cout that could be redirected by algo
1651 // check if an error reported on any sub-shape
1652 bool isComputeErrorSet = !checkComputeError( algo, ret, shape );
1653 if ( isComputeErrorSet )
1655 // check if anything was built
1656 TopExp_Explorer subS(shape, _subShape.ShapeType());
1659 for (; ret && subS.More(); subS.Next())
1660 if ( !_father->GetSubMesh( subS.Current() )->IsMeshComputed() &&
1661 ( _subShape.ShapeType() != TopAbs_EDGE ||
1662 !algo->isDegenerated( TopoDS::Edge( subS.Current() ))))
1665 // Set _computeError
1666 if (!ret && !isComputeErrorSet)
1668 for (subS.ReInit(); subS.More(); subS.Next())
1670 SMESH_subMesh* sm = _father->GetSubMesh( subS.Current() );
1671 if ( !sm->IsMeshComputed() )
1673 if ( !sm->_computeError )
1674 sm->_computeError = SMESH_ComputeError::New();
1675 if ( sm->_computeError->IsOK() )
1676 sm->_computeError->myName = COMPERR_ALGO_FAILED;
1677 sm->_computeState = FAILED_TO_COMPUTE;
1678 sm->_computeError->myAlgo = algo;
1682 if (ret && _computeError && _computeError->myName != COMPERR_WARNING )
1684 _computeError.reset();
1687 // send event SUBMESH_COMPUTED
1689 if ( !algo->NeedDiscreteBoundary() )
1690 // send SUBMESH_COMPUTED to dependants of all sub-meshes of shape
1691 for (subS.ReInit(); subS.More(); subS.Next())
1693 SMESH_subMesh* sm = _father->GetSubMesh( subS.Current() );
1694 SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(false,false);
1695 while ( smIt->more() ) {
1697 if ( sm->GetSubShape().ShapeType() == TopAbs_VERTEX )
1698 sm->updateDependantsState( SUBMESH_COMPUTED );
1704 updateDependantsState( SUBMESH_COMPUTED );
1708 case COMPUTE_CANCELED: // nothing to do
1712 removeSubMeshElementsAndNodes();
1713 _computeState = NOT_READY;
1717 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1719 _computeState = READY_TO_COMPUTE;
1721 setAlgoState(MISSING_HYP);
1724 case SUBMESH_COMPUTED: // nothing to do
1726 case SUBMESH_RESTORED:
1727 // check if a mesh is already computed that may
1728 // happen after retrieval from a file
1729 ComputeStateEngine( CHECK_COMPUTE_STATE );
1730 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1732 if (algo) algo->SubmeshRestored( this );
1734 case MESH_ENTITY_REMOVED:
1736 case SUBMESH_LOADED:
1737 loadDependentMeshes();
1738 ComputeSubMeshStateEngine( SUBMESH_LOADED );
1740 case CHECK_COMPUTE_STATE:
1741 if ( IsMeshComputed() )
1742 _computeState = COMPUTE_OK;
1743 else if ( _computeError && _computeError->IsKO() )
1744 _computeState = FAILED_TO_COMPUTE;
1752 // ----------------------------------------------------------------------
1757 case MODIF_ALGO_STATE:
1758 ComputeStateEngine( CLEAN );
1760 if (algo && !algo->NeedDiscreteBoundary())
1761 cleanDependsOn( algo ); // clean sub-meshes with event CLEAN
1763 case COMPUTE: // nothing to do
1765 case COMPUTE_CANCELED: // nothing to do
1768 cleanDependants(); // clean sub-meshes, dependant on this one, with event CLEAN
1769 removeSubMeshElementsAndNodes();
1770 _computeState = NOT_READY;
1771 if ( _algoState == HYP_OK )
1772 _computeState = READY_TO_COMPUTE;
1774 case SUBMESH_COMPUTED: // nothing to do
1776 case SUBMESH_RESTORED:
1777 ComputeStateEngine( CHECK_COMPUTE_STATE );
1778 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1780 if (algo) algo->SubmeshRestored( this );
1782 case MESH_ENTITY_REMOVED:
1783 updateDependantsState ( CHECK_COMPUTE_STATE );
1784 ComputeStateEngine ( CHECK_COMPUTE_STATE );
1785 ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1787 case CHECK_COMPUTE_STATE:
1788 if ( !IsMeshComputed() ) {
1789 if (_algoState == HYP_OK)
1790 _computeState = READY_TO_COMPUTE;
1792 _computeState = NOT_READY;
1795 case SUBMESH_LOADED:
1796 // already treated event, thanks to which _computeState == COMPUTE_OK
1804 // ----------------------------------------------------------------------
1806 case FAILED_TO_COMPUTE:
1809 case MODIF_ALGO_STATE:
1811 ComputeStateEngine( CLEAN );
1813 if (algo && !algo->NeedDiscreteBoundary())
1814 cleanDependsOn( algo ); // clean sub-meshes with event CLEAN
1815 if (_algoState == HYP_OK)
1816 _computeState = READY_TO_COMPUTE;
1818 _computeState = NOT_READY;
1820 case COMPUTE: // nothing to do
1821 case COMPUTE_SUBMESH:
1823 case COMPUTE_CANCELED:
1826 algo->CancelCompute();
1830 cleanDependants(); // submeshes dependent on me should be cleaned as well
1831 removeSubMeshElementsAndNodes();
1833 case SUBMESH_COMPUTED: // allow retry compute
1834 if (_algoState == HYP_OK)
1835 _computeState = READY_TO_COMPUTE;
1837 _computeState = NOT_READY;
1839 case SUBMESH_RESTORED:
1840 ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1842 case MESH_ENTITY_REMOVED:
1844 case CHECK_COMPUTE_STATE:
1845 if ( IsMeshComputed() )
1846 _computeState = COMPUTE_OK;
1848 if (_algoState == HYP_OK)
1849 _computeState = READY_TO_COMPUTE;
1851 _computeState = NOT_READY;
1853 // case SUBMESH_LOADED:
1861 // ----------------------------------------------------------------------
1867 notifyListenersOnEvent( event, COMPUTE_EVENT );
1873 //=============================================================================
1877 //=============================================================================
1879 bool SMESH_subMesh::Evaluate(MapShapeNbElems& aResMap)
1881 _computeError.reset();
1885 if (_subShape.ShapeType() == TopAbs_VERTEX) {
1886 vector<int> aVec(SMDSEntity_Last,0);
1887 aVec[SMDSEntity_Node] = 1;
1888 aResMap.insert(make_pair(this,aVec));
1892 //SMESH_Gen *gen = _father->GetGen();
1893 SMESH_Algo *algo = 0;
1894 SMESH_Hypothesis::Hypothesis_Status hyp_status;
1897 if(algo && !aResMap.count(this) )
1899 ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1900 if (!ret) return false;
1902 if (_father->HasShapeToMesh() && algo->NeedDiscreteBoundary())
1904 // check submeshes needed
1905 bool subMeshEvaluated = true;
1906 int dimToCheck = SMESH_Gen::GetShapeDim( _subShape ) - 1;
1907 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,/*complexShapeFirst=*/true);
1908 while ( smIt->more() && subMeshEvaluated )
1910 SMESH_subMesh* sm = smIt->next();
1911 int dim = SMESH_Gen::GetShapeDim( sm->GetSubShape() );
1912 if (dim < dimToCheck) break; // the rest subMeshes are all of less dimension
1913 const vector<int> & nbs = aResMap[ sm ];
1914 subMeshEvaluated = (std::accumulate( nbs.begin(), nbs.end(), 0 ) > 0 );
1916 if ( !subMeshEvaluated )
1919 _computeError = SMESH_ComputeError::New(COMPERR_OK,"",algo);
1920 ret = algo->Evaluate((*_father), _subShape, aResMap);
1922 aResMap.insert( make_pair( this,vector<int>(0)));
1929 //=======================================================================
1931 * \brief Update compute_state by _computeError and send proper events to
1932 * dependent submeshes
1933 * \retval bool - true if _computeError is NOT set
1935 //=======================================================================
1937 bool SMESH_subMesh::checkComputeError(SMESH_Algo* theAlgo,
1938 const bool theComputeOK,
1939 const TopoDS_Shape& theShape)
1941 bool noErrors = true;
1943 if ( !theShape.IsNull() )
1945 // Check state of submeshes
1946 if ( !theAlgo->NeedDiscreteBoundary())
1948 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1949 while ( smIt->more() )
1950 if ( !smIt->next()->checkComputeError( theAlgo, theComputeOK ))
1954 // Check state of neighbours
1955 if ( !theAlgo->OnlyUnaryInput() &&
1956 theShape.ShapeType() == TopAbs_COMPOUND &&
1957 !theShape.IsSame( _subShape ))
1959 for (TopoDS_Iterator subIt( theShape ); subIt.More(); subIt.Next()) {
1960 SMESH_subMesh* sm = _father->GetSubMesh( subIt.Value() );
1962 if ( !sm->checkComputeError( theAlgo, theComputeOK, sm->GetSubShape() ))
1964 updateDependantsState( SUBMESH_COMPUTED ); // send event SUBMESH_COMPUTED
1971 // Set my _computeState
1973 if ( !_computeError || _computeError->IsOK() )
1975 // no error description is set to this sub-mesh, check if any mesh is computed
1976 _computeState = IsMeshComputed() ? COMPUTE_OK : FAILED_TO_COMPUTE;
1977 if ( _computeState != COMPUTE_OK )
1979 if ( _subShape.ShapeType() == TopAbs_EDGE &&
1980 SMESH_Algo::isDegenerated( TopoDS::Edge( _subShape )) )
1981 _computeState = COMPUTE_OK;
1982 else if ( theComputeOK )
1983 _computeError = SMESH_ComputeError::New(COMPERR_NO_MESH_ON_SHAPE,"",theAlgo);
1987 if ( _computeError && !_computeError->IsOK() )
1989 if ( !_computeError->myAlgo )
1990 _computeError->myAlgo = theAlgo;
1994 text << theAlgo->GetName() << " failed on sub-shape #" << _Id << " with error ";
1995 if (_computeError->IsCommon() )
1996 text << _computeError->CommonName();
1998 text << _computeError->myName;
1999 if ( _computeError->myComment.size() > 0 )
2000 text << " \"" << _computeError->myComment << "\"";
2004 _computeState = _computeError->IsKO() ? FAILED_TO_COMPUTE : COMPUTE_OK;
2012 //=======================================================================
2013 //function : updateSubMeshState
2015 //=======================================================================
2017 void SMESH_subMesh::updateSubMeshState(const compute_state theState)
2019 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
2020 while ( smIt->more() )
2021 smIt->next()->_computeState = theState;
2024 //=======================================================================
2025 //function : ComputeSubMeshStateEngine
2027 //=======================================================================
2029 void SMESH_subMesh::ComputeSubMeshStateEngine(int event, const bool includeSelf)
2031 SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(includeSelf,false);
2032 while ( smIt->more() )
2033 smIt->next()->ComputeStateEngine(event);
2036 //=======================================================================
2037 //function : updateDependantsState
2039 //=======================================================================
2041 void SMESH_subMesh::updateDependantsState(const compute_event theEvent)
2043 const std::vector< SMESH_subMesh * > & ancestors = GetAncestors();
2044 for ( size_t iA = 0; iA < ancestors.size(); ++iA )
2046 ancestors[ iA ]->ComputeStateEngine( theEvent );
2050 //=============================================================================
2054 //=============================================================================
2056 void SMESH_subMesh::cleanDependants()
2058 int dimToClean = SMESH_Gen::GetShapeDim( _subShape ) + 1;
2060 const std::vector< SMESH_subMesh * > & ancestors = GetAncestors();
2061 for ( size_t iA = 0; iA < ancestors.size(); ++iA )
2063 const TopoDS_Shape& ancestor = ancestors[ iA ]->GetSubShape();
2064 if ( SMESH_Gen::GetShapeDim( ancestor ) == dimToClean )
2066 // PAL8021. do not go upper than SOLID, else ComputeStateEngine(CLEAN)
2067 // will erase mesh on other shapes in a compound
2068 if ( ancestor.ShapeType() >= TopAbs_SOLID &&
2069 !ancestors[ iA ]->IsEmpty() ) // prevent infinite CLEAN via event lesteners
2070 ancestors[ iA ]->ComputeStateEngine(CLEAN);
2075 //=============================================================================
2079 //=============================================================================
2081 void SMESH_subMesh::removeSubMeshElementsAndNodes()
2083 cleanSubMesh( this );
2085 // algo may bind a submesh not to _subShape, eg 3D algo
2086 // sets nodes on SHELL while _subShape may be SOLID
2088 int dim = SMESH_Gen::GetShapeDim( _subShape );
2089 int type = _subShape.ShapeType() + 1;
2090 for ( ; type <= TopAbs_EDGE; type++) {
2091 if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
2093 TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
2094 for ( ; exp.More(); exp.Next() )
2095 cleanSubMesh( _father->GetSubMeshContaining( exp.Current() ));
2102 //=======================================================================
2103 //function : getCollection
2104 //purpose : return a shape containing all sub-shapes of the MainShape that can be
2105 // meshed at once along with _subShape
2106 //=======================================================================
2108 TopoDS_Shape SMESH_subMesh::getCollection(SMESH_Gen * theGen,
2109 SMESH_Algo* theAlgo,
2110 bool & theSubComputed,
2111 bool & theSubFailed,
2112 std::vector<SMESH_subMesh*>& theSubs)
2114 theSubComputed = SubMeshesComputed( & theSubFailed );
2116 TopoDS_Shape mainShape = _father->GetMeshDS()->ShapeToMesh();
2118 if ( mainShape.IsSame( _subShape ))
2121 const bool ignoreAuxiliaryHyps = false;
2122 list<const SMESHDS_Hypothesis*> aUsedHyp =
2123 theAlgo->GetUsedHypothesis( *_father, _subShape, ignoreAuxiliaryHyps ); // copy
2125 // put in a compound all shapes with the same hypothesis assigned
2126 // and a good ComputeState
2128 TopoDS_Compound aCompound;
2129 BRep_Builder aBuilder;
2130 aBuilder.MakeCompound( aCompound );
2134 TopExp_Explorer anExplorer( mainShape, _subShape.ShapeType() );
2135 for ( ; anExplorer.More(); anExplorer.Next() )
2137 const TopoDS_Shape& S = anExplorer.Current();
2138 SMESH_subMesh* subMesh = _father->GetSubMesh( S );
2139 theSubs.push_back( subMesh );
2140 if ( subMesh == this )
2142 aBuilder.Add( aCompound, S );
2144 else if ( subMesh->GetComputeState() == READY_TO_COMPUTE )
2146 SMESH_Algo* anAlgo = theGen->GetAlgo( subMesh );
2147 if (strcmp( anAlgo->GetName(), theAlgo->GetName()) == 0 && // same algo
2148 anAlgo->GetUsedHypothesis( *_father, S, ignoreAuxiliaryHyps ) == aUsedHyp) // same hyps
2149 aBuilder.Add( aCompound, S );
2150 if ( !subMesh->SubMeshesComputed() )
2151 theSubComputed = false;
2158 //=======================================================================
2159 //function : getSimilarAttached
2160 //purpose : return a hypothesis attached to theShape.
2161 // If theHyp is provided, similar but not same hypotheses
2162 // is returned; else only applicable ones having theHypType
2164 //=======================================================================
2166 const SMESH_Hypothesis* SMESH_subMesh::getSimilarAttached(const TopoDS_Shape& theShape,
2167 const SMESH_Hypothesis * theHyp,
2168 const int theHypType)
2170 SMESH_HypoFilter hypoKind;
2171 hypoKind.Init( hypoKind.HasType( theHyp ? theHyp->GetType() : theHypType ));
2173 hypoKind.And ( hypoKind.HasDim( theHyp->GetDim() ));
2174 hypoKind.AndNot( hypoKind.Is( theHyp ));
2175 if ( theHyp->IsAuxiliary() )
2176 hypoKind.And( hypoKind.HasName( theHyp->GetName() ));
2178 hypoKind.AndNot( hypoKind.IsAuxiliary());
2181 hypoKind.And( hypoKind.IsApplicableTo( theShape ));
2184 return _father->GetHypothesis( theShape, hypoKind, false );
2187 //=======================================================================
2188 //function : CheckConcurentHypothesis
2189 //purpose : check if there are several applicable hypothesis attached to
2191 //=======================================================================
2193 SMESH_Hypothesis::Hypothesis_Status
2194 SMESH_subMesh::CheckConcurentHypothesis (const int theHypType)
2196 MESSAGE ("SMESH_subMesh::CheckConcurentHypothesis");
2198 // is there local hypothesis on me?
2199 if ( getSimilarAttached( _subShape, 0, theHypType ) )
2200 return SMESH_Hypothesis::HYP_OK;
2203 TopoDS_Shape aPrevWithHyp;
2204 const SMESH_Hypothesis* aPrevHyp = 0;
2205 TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
2206 for (; it.More(); it.Next())
2208 const TopoDS_Shape& ancestor = it.Value();
2209 const SMESH_Hypothesis* hyp = getSimilarAttached( ancestor, 0, theHypType );
2212 if ( aPrevWithHyp.IsNull() || aPrevWithHyp.IsSame( ancestor ))
2214 aPrevWithHyp = ancestor;
2217 else if ( aPrevWithHyp.ShapeType() == ancestor.ShapeType() && aPrevHyp != hyp )
2218 return SMESH_Hypothesis::HYP_CONCURENT;
2220 return SMESH_Hypothesis::HYP_OK;
2223 return SMESH_Hypothesis::HYP_OK;
2226 //================================================================================
2228 * \brief Constructor of OwnListenerData
2230 //================================================================================
2232 SMESH_subMesh::OwnListenerData::OwnListenerData( SMESH_subMesh* sm, EventListener* el):
2234 myMeshID( sm ? sm->GetFather()->GetId() : -1 ),
2235 mySubMeshID( sm ? sm->GetId() : -1 ),
2240 //================================================================================
2242 * \brief Sets an event listener and its data to a submesh
2243 * \param listener - the listener to store
2244 * \param data - the listener data to store
2245 * \param where - the submesh to store the listener and it's data
2247 * It remembers the submesh where it puts the listener in order to delete
2248 * them when HYP_OK algo_state is lost
2249 * After being set, event listener is notified on each event of where submesh.
2251 //================================================================================
2253 void SMESH_subMesh::SetEventListener(EventListener* listener,
2254 EventListenerData* data,
2255 SMESH_subMesh* where)
2257 if ( listener && where ) {
2258 where->setEventListener( listener, data );
2259 _ownListeners.push_back( OwnListenerData( where, listener ));
2263 //================================================================================
2265 * \brief Sets an event listener and its data to a submesh
2266 * \param listener - the listener to store
2267 * \param data - the listener data to store
2269 * After being set, event listener is notified on each event of a submesh.
2271 //================================================================================
2273 void SMESH_subMesh::setEventListener(EventListener* listener,
2274 EventListenerData* data)
2276 map< EventListener*, EventListenerData* >::iterator l_d =
2277 _eventListeners.find( listener );
2278 if ( l_d != _eventListeners.end() ) {
2279 EventListenerData* curData = l_d->second;
2280 if ( curData && curData != data && curData->IsDeletable() )
2286 for ( l_d = _eventListeners.begin(); l_d != _eventListeners.end(); ++l_d )
2287 if ( listener->GetName() == l_d->first->GetName() )
2289 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 occured 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 if ( !_father->MeshExists( d->myMeshID ))
2438 if ( _father->GetId() == d->myMeshID && !_father->GetSubMeshContaining( d->mySubMeshID ))
2440 d->mySubMesh->DeleteEventListener( d->myListener );
2442 _ownListeners.clear();
2445 //=======================================================================
2446 //function : loadDependentMeshes
2447 //purpose : loads dependent meshes on SUBMESH_LOADED event
2448 //=======================================================================
2450 void SMESH_subMesh::loadDependentMeshes()
2452 list< OwnListenerData >::iterator d;
2453 for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
2454 if ( _father != d->mySubMesh->_father )
2455 d->mySubMesh->_father->Load();
2457 // map< EventListener*, EventListenerData* >::iterator l_d = _eventListeners.begin();
2458 // for ( ; l_d != _eventListeners.end(); ++l_d )
2459 // if ( l_d->second )
2461 // const list<SMESH_subMesh*>& smList = l_d->second->mySubMeshes;
2462 // list<SMESH_subMesh*>::const_iterator sm = smList.begin();
2463 // for ( ; sm != smList.end(); ++sm )
2464 // if ( _father != (*sm)->_father )
2465 // (*sm)->_father->Load();
2469 //================================================================================
2471 * \brief Do something on a certain event
2472 * \param event - algo_event or compute_event itself
2473 * \param eventType - algo_event or compute_event
2474 * \param subMesh - the submesh where the event occures
2475 * \param data - listener data stored in the subMesh
2476 * \param hyp - hypothesis, if eventType is algo_event
2478 * The base implementation translates CLEAN event to the subMesh
2479 * stored in listener data. Also it sends SUBMESH_COMPUTED event in case of
2480 * successful COMPUTE event.
2482 //================================================================================
2484 void SMESH_subMeshEventListener::ProcessEvent(const int event,
2485 const int eventType,
2486 SMESH_subMesh* subMesh,
2487 EventListenerData* data,
2488 const SMESH_Hypothesis* /*hyp*/)
2490 if ( data && !data->mySubMeshes.empty() &&
2491 eventType == SMESH_subMesh::COMPUTE_EVENT)
2493 ASSERT( data->mySubMeshes.front() != subMesh );
2494 list<SMESH_subMesh*>::iterator smIt = data->mySubMeshes.begin();
2495 list<SMESH_subMesh*>::iterator smEnd = data->mySubMeshes.end();
2497 case SMESH_subMesh::CLEAN:
2498 for ( ; smIt != smEnd; ++ smIt)
2499 (*smIt)->ComputeStateEngine( event );
2501 case SMESH_subMesh::COMPUTE:
2502 case SMESH_subMesh::COMPUTE_SUBMESH:
2503 if ( subMesh->GetComputeState() == SMESH_subMesh::COMPUTE_OK )
2504 for ( ; smIt != smEnd; ++ smIt)
2505 (*smIt)->ComputeStateEngine( SMESH_subMesh::SUBMESH_COMPUTED );
2514 //================================================================================
2516 * \brief Iterator over submeshes and optionally prepended or appended one
2518 //================================================================================
2520 struct _Iterator : public SMDS_Iterator<SMESH_subMesh*>
2522 _Iterator(SMDS_Iterator<SMESH_subMesh*>* subIt,
2523 SMESH_subMesh* prepend,
2524 SMESH_subMesh* append): myIt(subIt),myAppend(append)
2526 myCur = prepend ? prepend : myIt->more() ? myIt->next() : append;
2527 if ( myCur == append ) append = 0;
2529 /// Return true if and only if there are other object in this iterator
2534 /// Return the current object and step to the next one
2535 virtual SMESH_subMesh* next()
2537 SMESH_subMesh* res = myCur;
2538 if ( myIt->more() ) { myCur = myIt->next(); }
2539 else { myCur = myAppend; myAppend = 0; }
2546 SMESH_subMesh *myAppend, *myCur;
2547 SMDS_Iterator<SMESH_subMesh*> *myIt;
2551 //================================================================================
2553 * \brief Return iterator on the submeshes this one depends on
2554 * \param includeSelf - this submesh to be returned also
2555 * \param reverse - if true, complex shape submeshes go first
2557 //================================================================================
2559 SMESH_subMeshIteratorPtr SMESH_subMesh::getDependsOnIterator(const bool includeSelf,
2560 const bool reverse) const
2562 SMESH_subMesh *me = (SMESH_subMesh*) this;
2563 SMESH_subMesh *prepend=0, *append=0;
2564 if ( includeSelf ) {
2565 if ( reverse ) prepend = me;
2568 typedef map < int, SMESH_subMesh * > TMap;
2571 return SMESH_subMeshIteratorPtr
2572 ( new _Iterator( new SMDS_mapReverseIterator<TMap>( me->DependsOn() ), prepend, append ));
2575 return SMESH_subMeshIteratorPtr
2576 ( new _Iterator( new SMDS_mapIterator<TMap>( me->DependsOn() ), prepend, append ));
2580 //================================================================================
2582 * \brief Returns ancestor sub-meshes. Finds them if not yet found.
2584 //================================================================================
2586 const std::vector< SMESH_subMesh * > & SMESH_subMesh::GetAncestors() const
2588 if ( _ancestors.empty() &&
2589 !_subShape.IsSame( _father->GetShapeToMesh() ))
2591 const TopTools_ListOfShape& ancShapes = _father->GetAncestors( _subShape );
2593 SMESH_subMesh* me = const_cast< SMESH_subMesh* >( this );
2594 me->_ancestors.reserve( ancShapes.Extent() );
2596 TopTools_MapOfShape map;
2598 for ( TopTools_ListIteratorOfListOfShape it( ancShapes ); it.More(); it.Next() )
2599 if ( SMESH_subMesh* sm = _father->GetSubMeshContaining( it.Value() ))
2600 if ( map.Add( it.Value() ))
2601 me->_ancestors.push_back( sm );
2607 //================================================================================
2609 * \brief Clears the vector of ancestor sub-meshes
2611 //================================================================================
2613 void SMESH_subMesh::ClearAncestors()
2618 //================================================================================
2620 * \brief Find common submeshes (based on shared sub-shapes with other
2621 * \param theOther submesh to check
2622 * \param theSetOfCommon set of common submesh
2624 //================================================================================
2626 bool SMESH_subMesh::FindIntersection(const SMESH_subMesh* theOther,
2627 std::set<const SMESH_subMesh*>& theSetOfCommon ) const
2629 int oldNb = theSetOfCommon.size();
2630 // check main submeshes
2631 const map <int, SMESH_subMesh*>::const_iterator otherEnd = theOther->_mapDepend.end();
2632 if ( theOther->_mapDepend.find(this->GetId()) != otherEnd )
2633 theSetOfCommon.insert( this );
2634 if ( _mapDepend.find(theOther->GetId()) != _mapDepend.end() )
2635 theSetOfCommon.insert( theOther );
2636 // check common submeshes
2637 map <int, SMESH_subMesh*>::const_iterator mapIt = _mapDepend.begin();
2638 for( ; mapIt != _mapDepend.end(); mapIt++ )
2639 if ( theOther->_mapDepend.find((*mapIt).first) != otherEnd )
2640 theSetOfCommon.insert( (*mapIt).second );
2641 return oldNb < theSetOfCommon.size();