Salome HOME
f395b7986c23b00d64c18f8984cb50ca2634aed0
[modules/smesh.git] / src / SMESH / SMESH_subMesh.cxx
1 // Copyright (C) 2007-2015  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  SMESH SMESH : implementaion of SMESH idl descriptions
24 //  File   : SMESH_subMesh.cxx
25 //  Author : Paul RASCLE, EDF
26 //  Module : SMESH
27
28 #include "SMESH_subMesh.hxx"
29
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"
40
41 #include <Basics_OCCTVersion.hxx>
42
43 #include "utilities.h"
44 #include "OpUtil.hxx"
45 #include "Basics_Utils.hxx"
46
47 #include <BRep_Builder.hxx>
48 #include <BRep_Tool.hxx>
49 #include <TopExp.hxx>
50 #include <TopExp_Explorer.hxx>
51 #include <TopTools_IndexedMapOfShape.hxx>
52 #include <TopTools_ListIteratorOfListOfShape.hxx>
53 #include <TopTools_ListOfShape.hxx>
54 #include <TopoDS.hxx>
55 #include <TopoDS_Compound.hxx>
56 #include <TopoDS_Iterator.hxx>
57 #include <gp_Pnt.hxx>
58
59 #include <Standard_OutOfMemory.hxx>
60 #include <Standard_ErrorHandler.hxx>
61
62 #include <numeric>
63
64 using namespace std;
65
66 //=============================================================================
67 /*!
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
70  * lack of memory
71  */
72 //=============================================================================
73
74 struct MemoryReserve
75 {
76   char* myBuf;
77   MemoryReserve(): myBuf( new char[1024*1024*2] ){}
78   ~MemoryReserve() { delete [] myBuf; }
79 };
80
81 //=============================================================================
82 /*!
83  *  default constructor:
84  */
85 //=============================================================================
86
87 SMESH_subMesh::SMESH_subMesh(int                  Id,
88                              SMESH_Mesh *         father,
89                              SMESHDS_Mesh *       meshDS,
90                              const TopoDS_Shape & aSubShape)
91 {
92   _subShape           = aSubShape;
93   _subMeshDS          = meshDS->MeshElements(_subShape);   // may be null ...
94   _father             = father;
95   _Id                 = Id;
96   _dependenceAnalysed = _alwaysComputed = false;
97   _algo               = 0;
98   if (_subShape.ShapeType() == TopAbs_VERTEX)
99   {
100     _algoState = HYP_OK;
101     _computeState = READY_TO_COMPUTE;
102   }
103   else
104   {
105     _algoState = NO_ALGO;
106     _computeState = NOT_READY;
107   }
108   _computeCost = 0; // how costly is to compute this sub-mesh
109   _realComputeCost = 0;
110 }
111
112 //=============================================================================
113 /*!
114  *
115  */
116 //=============================================================================
117
118 SMESH_subMesh::~SMESH_subMesh()
119 {
120   deleteOwnListeners();
121 }
122
123 //=============================================================================
124 /*!
125  *
126  */
127 //=============================================================================
128
129 int SMESH_subMesh::GetId() const
130 {
131   //MESSAGE("SMESH_subMesh::GetId");
132   return _Id;
133 }
134
135 //=============================================================================
136 /*!
137  *
138  */
139 //=============================================================================
140
141 SMESHDS_SubMesh * SMESH_subMesh::GetSubMeshDS()
142 {
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
145 }
146
147 //=============================================================================
148 /*!
149  *
150  */
151 //=============================================================================
152
153 const SMESHDS_SubMesh * SMESH_subMesh::GetSubMeshDS() const
154 {
155   return ((SMESH_subMesh*) this )->GetSubMeshDS();
156 }
157
158 //=============================================================================
159 /*!
160  *
161  */
162 //=============================================================================
163
164 SMESHDS_SubMesh* SMESH_subMesh::CreateSubMeshDS()
165 {
166   if ( !GetSubMeshDS() ) {
167     SMESHDS_Mesh* meshDS = _father->GetMeshDS();
168     meshDS->NewSubMesh( meshDS->ShapeToIndex( _subShape ) );
169   }
170   return GetSubMeshDS();
171 }
172
173 //=============================================================================
174 /*!
175  *
176  */
177 //=============================================================================
178
179 SMESH_subMesh *SMESH_subMesh::GetFirstToCompute()
180 {
181   SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(true,false);
182   while ( smIt->more() ) {
183     SMESH_subMesh *sm = smIt->next();
184     if ( sm->GetComputeState() == READY_TO_COMPUTE )
185       return sm;
186   }
187   return 0;                     // nothing to compute
188 }
189
190 //================================================================================
191 /*!
192  * \brief Returns a current algorithm
193  */
194 //================================================================================
195
196 SMESH_Algo* SMESH_subMesh::GetAlgo() const
197 {
198   if ( !_algo )
199   {
200     SMESH_subMesh* me = const_cast< SMESH_subMesh* >( this );
201     me->_algo = _father->GetGen()->GetAlgo( me );
202   }
203   return _algo;
204 }
205
206 //================================================================================
207 /*!
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)
210  */
211 //================================================================================
212
213 void SMESH_subMesh::SetIsAlwaysComputed(bool isAlCo)
214 {
215   _alwaysComputed = isAlCo;
216   if ( _alwaysComputed )
217     _computeState = COMPUTE_OK;
218   else
219     ComputeStateEngine( CHECK_COMPUTE_STATE );
220 }
221
222 //=======================================================================
223 /*!
224  * \brief Return true if no mesh entities is bound to the submesh
225  */
226 //=======================================================================
227
228 bool SMESH_subMesh::IsEmpty() const
229 {
230   if (SMESHDS_SubMesh * subMeshDS = ((SMESH_subMesh*)this)->GetSubMeshDS())
231     return (!subMeshDS->NbElements() && !subMeshDS->NbNodes());
232   return true;
233 }
234
235 //=======================================================================
236 //function : IsMeshComputed
237 //purpose  : check if _subMeshDS contains mesh elements
238 //=======================================================================
239
240 bool SMESH_subMesh::IsMeshComputed() const
241 {
242   if ( _alwaysComputed )
243     return true;
244   // algo may bind a submesh not to _subShape, eg 3D algo
245   // sets nodes on SHELL while _subShape may be SOLID
246
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 ))
252     {
253       TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
254       for ( ; exp.More(); exp.Next() )
255       {
256         if ( SMESHDS_SubMesh * smDS = meshDS->MeshElements( exp.Current() ))
257         {
258           bool computed = (dim > 0) ? smDS->NbElements() : smDS->NbNodes();
259           if ( computed )
260             return true;
261         }
262       }
263     }
264     else
265       break;
266   }
267
268   return false;
269 }
270
271 //=============================================================================
272 /*!
273  * Return true if all sub-meshes have been meshed
274  */
275 //=============================================================================
276
277 bool SMESH_subMesh::SubMeshesComputed(bool * isFailedToCompute/*=0*/) const
278 {
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() )
286   {
287     SMESH_subMesh *sm = smIt->next();
288     if ( sm->_alwaysComputed )
289       continue;
290     const TopoDS_Shape & ss = sm->GetSubShape();
291
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
299     }
300     SMESHDS_SubMesh * ds = sm->GetSubMeshDS();
301     bool computeOk = (sm->GetComputeState() == COMPUTE_OK ||
302                       (ds && ( dimToCheck ? ds->NbElements() : ds->NbNodes()  )));
303     if (!computeOk)
304     {
305       subMeshesComputed = false;
306       if ( isFailedToCompute && !(*isFailedToCompute) )
307         *isFailedToCompute = ( sm->GetComputeState() == FAILED_TO_COMPUTE );
308
309       // int type = ss.ShapeType();
310
311       // switch (type)
312       // {
313       // case TopAbs_COMPOUND:
314       //   {
315       //     MESSAGE("The not computed sub mesh is a COMPOUND");
316       //     break;
317       //   }
318       // case TopAbs_COMPSOLID:
319       //   {
320       //     MESSAGE("The not computed sub mesh is a COMPSOLID");
321       //     break;
322       //   }
323       // case TopAbs_SHELL:
324       //   {
325       //     MESSAGE("The not computed sub mesh is a SHEL");
326       //     break;
327       //   }
328       // case TopAbs_WIRE:
329       //   {
330       //     MESSAGE("The not computed sub mesh is a WIRE");
331       //     break;
332       //   }
333       // case TopAbs_SOLID:
334       //   {
335       //     MESSAGE("The not computed sub mesh is a SOLID");
336       //     break;
337       //   }
338       // case TopAbs_FACE:
339       //   {
340       //     MESSAGE("The not computed sub mesh is a FACE");
341       //     break;
342       //   }
343       // case TopAbs_EDGE:
344       //   {
345       //     MESSAGE("The not computed sub mesh is a EDGE");
346       //     break;
347       //   }
348       // default:
349       //   {
350       //     MESSAGE("The not computed sub mesh is of unknown type");
351       //     break;
352       //   }
353       // }
354
355       if ( !isFailedToCompute )
356         break;
357     }
358   }
359   return subMeshesComputed;
360 }
361
362 //================================================================================
363 /*!
364  * \brief Return cost of computing this sub-mesh. If hypotheses are not well defined,
365  *        zero is returned
366  *  \return int - the computation cost in abstract units.
367  */
368 //================================================================================
369
370 int SMESH_subMesh::GetComputeCost() const
371 {
372   return _realComputeCost;
373 }
374
375 //================================================================================
376 /*!
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.
380  */
381 //================================================================================
382
383 int SMESH_subMesh::computeCost() const
384 {
385   if ( !_computeCost )
386   {
387     int computeCost;
388     switch ( _subShape.ShapeType() ) {
389     case TopAbs_SOLID:
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;
394     }
395     SMESH_subMeshIteratorPtr childIt = getDependsOnIterator(/*includeSelf=*/false);
396     while ( childIt->more() )
397       computeCost += childIt->next()->computeCost();
398
399     ((SMESH_subMesh*)this)->_computeCost = computeCost;
400   }
401   return _computeCost;
402 }
403
404 //=============================================================================
405 /*!
406  * Returns all sub-meshes this one depend on
407  */
408 //=============================================================================
409
410 const map < int, SMESH_subMesh * >& SMESH_subMesh::DependsOn()
411 {
412   if ( _dependenceAnalysed || !_father->HasShapeToMesh() )
413     return _mapDepend;
414
415   int type = _subShape.ShapeType();
416   switch (type)
417   {
418   case TopAbs_COMPOUND:
419   {
420     list< TopoDS_Shape > compounds( 1, _subShape );
421     list< TopoDS_Shape >::iterator comp = compounds.begin();
422     for ( ; comp != compounds.end(); ++comp )
423     {
424       for ( TopoDS_Iterator sub( *comp ); sub.More(); sub.Next() )
425         switch ( sub.Value().ShapeType() )
426         {
427         case TopAbs_COMPOUND:  compounds.push_back( sub.Value() ); break;
428         case TopAbs_COMPSOLID: insertDependence( sub.Value(), TopAbs_SOLID ); break;
429         case TopAbs_SOLID:     insertDependence( sub.Value(), TopAbs_SOLID ); break;
430         case TopAbs_SHELL:     insertDependence( sub.Value(), TopAbs_FACE ); break;
431         case TopAbs_FACE:      insertDependence( sub.Value(), TopAbs_FACE ); break;
432         case TopAbs_WIRE:      insertDependence( sub.Value(), TopAbs_EDGE ); break;
433         case TopAbs_EDGE:      insertDependence( sub.Value(), TopAbs_EDGE ); break;
434         case TopAbs_VERTEX:    insertDependence( sub.Value(), TopAbs_VERTEX ); break;
435         default:;
436         }
437     }
438   }
439   break;
440   case TopAbs_COMPSOLID: insertDependence( _subShape, TopAbs_SOLID ); break;
441   case TopAbs_SOLID:     insertDependence( _subShape, TopAbs_FACE );
442   { /*internal EDGE*/    insertDependence( _subShape, TopAbs_EDGE, TopAbs_WIRE ); break; }
443   case TopAbs_SHELL:     insertDependence( _subShape, TopAbs_FACE ); break;
444   case TopAbs_FACE:      insertDependence( _subShape, TopAbs_EDGE ); break;
445   case TopAbs_WIRE:      insertDependence( _subShape, TopAbs_EDGE ); break;
446   case TopAbs_EDGE:      insertDependence( _subShape, TopAbs_VERTEX ); break;
447   default:;
448   }
449   _dependenceAnalysed = true;
450   return _mapDepend;
451 }
452
453 //================================================================================
454 /*!
455  * \brief Return a key for SMESH_subMesh::_mapDepend map
456  */
457 //================================================================================
458
459 namespace
460 {
461   int dependsOnMapKey( TopAbs_ShapeEnum type, int shapeID )
462   {
463     int ordType = 9 - int(type);               // 2 = Vertex, 8 = CompSolid
464     int     cle = shapeID;
465     cle += 10000000 * ordType;    // sort map by ordType then index
466     return cle;
467   }
468   int dependsOnMapKey( const SMESH_subMesh* sm )
469   {
470     return dependsOnMapKey( sm->GetSubShape().ShapeType(), sm->GetId() );
471   }
472 }
473
474 //=============================================================================
475 /*!
476  * Add sub-meshes on sub-shapes of a given type into the dependence map.
477  */
478 //=============================================================================
479
480 void SMESH_subMesh::insertDependence(const TopoDS_Shape aShape,
481                                      TopAbs_ShapeEnum   aSubType,
482                                      TopAbs_ShapeEnum   avoidType)
483 {
484   TopExp_Explorer sub( aShape, aSubType, avoidType );
485   for ( ; sub.More(); sub.Next() )
486   {
487     SMESH_subMesh *aSubMesh = _father->GetSubMesh( sub.Current() );
488     if ( aSubMesh->GetId() == 0 )
489       continue;  // not a sub-shape of the shape to mesh
490     int cle = dependsOnMapKey( aSubMesh );
491     if ( _mapDepend.find( cle ) == _mapDepend.end())
492     {
493       _mapDepend[cle] = aSubMesh;
494       const map < int, SMESH_subMesh * > & subMap = aSubMesh->DependsOn();
495       _mapDepend.insert( subMap.begin(), subMap.end() );
496     }
497   }
498 }
499
500 //================================================================================
501 /*!
502  * \brief Return \c true if \a this sub-mesh depends on \a other
503  */
504 //================================================================================
505
506 bool SMESH_subMesh::DependsOn( const SMESH_subMesh* other ) const
507 {
508   return other ? _mapDepend.count( dependsOnMapKey( other )) : false;
509 }
510
511 //================================================================================
512 /*!
513  * \brief Return \c true if \a this sub-mesh depends on a \a shape
514  */
515 //================================================================================
516
517 bool SMESH_subMesh::DependsOn( const int shapeID ) const
518 {
519   return DependsOn( _father->GetSubMeshContaining( shapeID ));
520 }
521
522 //=============================================================================
523 /*!
524  * Return a shape of \a this sub-mesh
525  */
526 //=============================================================================
527
528 const TopoDS_Shape & SMESH_subMesh::GetSubShape() const
529 {
530   return _subShape;
531 }
532
533 //=======================================================================
534 //function : CanAddHypothesis
535 //purpose  : return true if theHypothesis can be attached to me:
536 //           its dimention is checked
537 //=======================================================================
538
539 bool SMESH_subMesh::CanAddHypothesis(const SMESH_Hypothesis* theHypothesis) const
540 {
541   int aHypDim   = theHypothesis->GetDim();
542   int aShapeDim = SMESH_Gen::GetShapeDim(_subShape);
543   // issue 21106. Forbid 3D mesh on the SHELL
544   // if (aHypDim == 3 && aShapeDim == 3) {
545   //   // check case of open shell
546   //   //if (_subShape.ShapeType() == TopAbs_SHELL && !_subShape.Closed())
547   //   if (_subShape.ShapeType() == TopAbs_SHELL && !BRep_Tool::IsClosed(_subShape))
548   //     return false;
549   // }
550   if ( aHypDim <= aShapeDim )
551     return true;
552
553   return false;
554 }
555
556 //=======================================================================
557 //function : IsApplicableHypotesis
558 //purpose  :
559 //=======================================================================
560
561 bool SMESH_subMesh::IsApplicableHypotesis(const SMESH_Hypothesis* theHypothesis,
562                                           const TopAbs_ShapeEnum  theShapeType)
563 {
564   if ( theHypothesis->GetType() > SMESHDS_Hypothesis::PARAM_ALGO)
565   {
566     // algorithm
567     if ( theHypothesis->GetShapeType() & (1<< theShapeType))
568       // issue 21106. Forbid 3D mesh on the SHELL
569       return !( theHypothesis->GetDim() == 3 && theShapeType == TopAbs_SHELL );
570     else
571       return false;
572   }
573
574   // hypothesis
575   switch ( theShapeType ) {
576   case TopAbs_VERTEX:
577   case TopAbs_EDGE:
578   case TopAbs_FACE:
579   case TopAbs_SOLID:
580     return SMESH_Gen::GetShapeDim( theShapeType ) == theHypothesis->GetDim();
581
582   case TopAbs_SHELL:
583     // Special case for algorithms, building 2D mesh on a whole shell.
584     // Before this fix there was a problem after restoring from study,
585     // because in that case algorithm is assigned before hypothesis
586     // (on shell in problem case) and hypothesis is checked on faces
587     // (because it is 2D), where we have NO_ALGO state.
588     // Now 2D hypothesis is also applicable to shells.
589     return (theHypothesis->GetDim() == 2 || theHypothesis->GetDim() == 3);
590
591 //   case TopAbs_WIRE:
592 //   case TopAbs_COMPSOLID:
593 //   case TopAbs_COMPOUND:
594   default:;
595   }
596   return false;
597 }
598
599 //================================================================================
600 /*!
601  * \brief Treats modification of hypotheses definition
602  *  \param [in] event - what happens
603  *  \param [in] anHyp - a hypothesis
604  *  \return SMESH_Hypothesis::Hypothesis_Status - a treatment result.
605  * 
606  * Optional description of a problematic situation (if any) can be retrieved
607  * via GetComputeError().
608  */
609 //================================================================================
610
611 SMESH_Hypothesis::Hypothesis_Status
612   SMESH_subMesh::AlgoStateEngine(int event, SMESH_Hypothesis * anHyp)
613 {
614   // **** les retour des evenement shape sont significatifs
615   // (add ou remove fait ou non)
616   // le retour des evenement father n'indiquent pas que add ou remove fait
617
618   SMESH_Hypothesis::Hypothesis_Status aux_ret, ret = SMESH_Hypothesis::HYP_OK;
619   if ( _Id == 0 ) return ret; // not a sub-shape of the shape to mesh
620
621   SMESHDS_Mesh* meshDS =_father->GetMeshDS();
622   SMESH_Algo*   algo   = 0;
623   _algo = 0;
624
625   if (_subShape.ShapeType() == TopAbs_VERTEX )
626   {
627     if ( anHyp->GetDim() != 0) {
628       if (event == ADD_HYP || event == ADD_ALGO)
629         return SMESH_Hypothesis::HYP_BAD_DIM;
630       else
631         return SMESH_Hypothesis::HYP_OK;
632     }
633     // 0D hypothesis
634     else if ( _algoState == HYP_OK ) {
635       // update default _algoState
636       if ( event != REMOVE_FATHER_ALGO )
637       {
638         _algoState = NO_ALGO;
639         algo = GetAlgo();
640         if ( algo ) {
641           _algoState = MISSING_HYP;
642           if ( event == REMOVE_FATHER_HYP ||
643                algo->CheckHypothesis(*_father,_subShape, aux_ret))
644             _algoState = HYP_OK;
645         }
646       }
647     }
648   }
649
650   int oldAlgoState = _algoState;
651   bool modifiedHyp = (event == MODIF_HYP);  // if set to true, force event MODIF_ALGO_STATE
652   SMESH_Algo* algoRequiringCleaning = 0;
653
654   bool isApplicableHyp = IsApplicableHypotesis( anHyp );
655
656   if (event == ADD_ALGO || event == ADD_FATHER_ALGO)
657   {
658     // -------------------------------------------
659     // check if a shape needed by algo is present
660     // -------------------------------------------
661     algo = static_cast< SMESH_Algo* >( anHyp );
662     if ( !_father->HasShapeToMesh() && algo->NeedShape() )
663       return SMESH_Hypothesis::HYP_NEED_SHAPE;
664     // ----------------------
665     // check mesh conformity
666     // ----------------------
667     if (isApplicableHyp && !_father->IsNotConformAllowed() && !IsConform( algo ))
668       return SMESH_Hypothesis::HYP_NOTCONFORM;
669
670     // check if all-dimensional algo is hidden by other local one
671     if ( event == ADD_ALGO ) {
672       SMESH_HypoFilter filter( SMESH_HypoFilter::HasType( algo->GetType() ));
673       filter.Or( SMESH_HypoFilter::HasType( algo->GetType()+1 ));
674       filter.Or( SMESH_HypoFilter::HasType( algo->GetType()+2 ));
675       if ( SMESH_Algo * curAlgo = (SMESH_Algo*)_father->GetHypothesis( this, filter, true ))
676         if ( !curAlgo->NeedDiscreteBoundary() )
677           algoRequiringCleaning = curAlgo;
678     }
679   }
680
681   // ----------------------------------
682   // add a hypothesis to DS if possible
683   // ----------------------------------
684   if (event == ADD_HYP || event == ADD_ALGO)
685   {
686     if ( ! CanAddHypothesis( anHyp )) // check dimension
687       return SMESH_Hypothesis::HYP_BAD_DIM;
688
689     if ( !anHyp->IsAuxiliary() && getSimilarAttached( _subShape, anHyp ) )
690       return SMESH_Hypothesis::HYP_ALREADY_EXIST;
691
692     if ( !meshDS->AddHypothesis(_subShape, anHyp))
693       return SMESH_Hypothesis::HYP_ALREADY_EXIST;
694   }
695
696   // --------------------------
697   // remove a hypothesis from DS
698   // --------------------------
699   if (event == REMOVE_HYP || event == REMOVE_ALGO)
700   {
701     if (!meshDS->RemoveHypothesis(_subShape, anHyp))
702       return SMESH_Hypothesis::HYP_OK; // nothing changes
703
704     if (event == REMOVE_ALGO)
705     {
706       algo = dynamic_cast<SMESH_Algo*> (anHyp);
707       if (!algo->NeedDiscreteBoundary())
708         algoRequiringCleaning = algo;
709     }
710   }
711
712   // ------------------
713   // analyse algo state
714   // ------------------
715   if (!isApplicableHyp)
716     return ret; // not applicable hypotheses do not change algo state
717
718   if (( algo = GetAlgo()))
719     algo->InitComputeError();
720
721   switch (_algoState)
722   {
723
724     // ----------------------------------------------------------------------
725
726   case NO_ALGO:
727     switch (event) {
728     case ADD_HYP:
729       break;
730     case ADD_ALGO: {
731       algo = GetAlgo();
732       ASSERT(algo);
733       if (algo->CheckHypothesis((*_father),_subShape, aux_ret))
734         setAlgoState(HYP_OK);
735       else if ( algo->IsStatusFatal( aux_ret )) {
736         meshDS->RemoveHypothesis(_subShape, anHyp);
737         ret = aux_ret;
738       }
739       else
740         setAlgoState(MISSING_HYP);
741       break;
742     }
743     case REMOVE_HYP:
744     case REMOVE_ALGO:
745     case ADD_FATHER_HYP:
746       break;
747     case ADD_FATHER_ALGO: {    // Algo just added in father
748       algo = GetAlgo();
749       ASSERT(algo);
750       if ( algo == anHyp ) {
751         if ( algo->CheckHypothesis((*_father),_subShape, aux_ret))
752           setAlgoState(HYP_OK);
753         else
754           setAlgoState(MISSING_HYP);
755       }
756       break;
757     }
758     case REMOVE_FATHER_HYP:
759       break;
760     case REMOVE_FATHER_ALGO: {
761       algo = GetAlgo();
762       if (algo)
763       {
764         if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
765             setAlgoState(HYP_OK);
766         else
767           setAlgoState(MISSING_HYP);
768       }
769       break;
770     }
771     case MODIF_HYP: break;
772     default:
773       ASSERT(0);
774       break;
775     }
776     break;
777
778     // ----------------------------------------------------------------------
779
780   case MISSING_HYP:
781     switch (event)
782     {
783     case ADD_HYP: {
784       algo = GetAlgo();
785       ASSERT(algo);
786       if ( algo->CheckHypothesis((*_father),_subShape, ret ))
787         setAlgoState(HYP_OK);
788       if (SMESH_Hypothesis::IsStatusFatal( ret ))
789         meshDS->RemoveHypothesis(_subShape, anHyp);
790       else if (!_father->IsUsedHypothesis( anHyp, this ))
791       {
792         meshDS->RemoveHypothesis(_subShape, anHyp);
793         ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
794       }
795       break;
796     }
797     case ADD_ALGO: {           //already existing algo : on father ?
798       algo = GetAlgo();
799       ASSERT(algo);
800       if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))// ignore hyp status
801         setAlgoState(HYP_OK);
802       else if ( algo->IsStatusFatal( aux_ret )) {
803         meshDS->RemoveHypothesis(_subShape, anHyp);
804         ret = aux_ret;
805       }
806       else
807         setAlgoState(MISSING_HYP);
808       break;
809     }
810     case REMOVE_HYP:
811       break;
812     case REMOVE_ALGO: {        // perhaps a father algo applies ?
813       algo = GetAlgo();
814       if (algo == NULL)  // no more algo applying on sub-shape...
815       {
816         setAlgoState(NO_ALGO);
817       }
818       else
819       {
820         if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
821           setAlgoState(HYP_OK);
822         else
823           setAlgoState(MISSING_HYP);
824       }
825       break;
826     }
827     case MODIF_HYP: // assigned hypothesis value may become good
828     case ADD_FATHER_HYP: {
829       algo = GetAlgo();
830       ASSERT(algo);
831       if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
832         setAlgoState(HYP_OK);
833       else
834         setAlgoState(MISSING_HYP);
835       break;
836     }
837     case ADD_FATHER_ALGO: { // new father algo
838       algo = GetAlgo();
839       ASSERT( algo );
840       if ( algo == anHyp ) {
841         if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
842           setAlgoState(HYP_OK);
843         else
844           setAlgoState(MISSING_HYP);
845       }
846       break;
847     }
848     case REMOVE_FATHER_HYP:    // nothing to do
849       break;
850     case REMOVE_FATHER_ALGO: {
851       algo = GetAlgo();
852       if (algo == NULL)  // no more applying algo on father
853       {
854         setAlgoState(NO_ALGO);
855       }
856       else
857       {
858         if ( algo->CheckHypothesis((*_father),_subShape , aux_ret ))
859           setAlgoState(HYP_OK);
860         else
861           setAlgoState(MISSING_HYP);
862       }
863       break;
864     }
865     default:
866       ASSERT(0);
867       break;
868     }
869     break;
870
871     // ----------------------------------------------------------------------
872
873   case HYP_OK:
874     switch (event)
875     {
876     case ADD_HYP: {
877       algo = GetAlgo();
878       ASSERT(algo);
879       if (!algo->CheckHypothesis((*_father),_subShape, ret ))
880       {
881         if ( !SMESH_Hypothesis::IsStatusFatal( ret ))
882           // ret should be fatal: anHyp was not added
883           ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
884       }
885       else if (!_father->IsUsedHypothesis( anHyp, this ))
886         ret = SMESH_Hypothesis::HYP_INCOMPATIBLE;
887
888       if (SMESH_Hypothesis::IsStatusFatal( ret ))
889       {
890         MESSAGE("do not add extra hypothesis");
891         meshDS->RemoveHypothesis(_subShape, anHyp);
892       }
893       else
894       {
895         modifiedHyp = true;
896       }
897       break;
898     }
899     case ADD_ALGO: {           //already existing algo : on father ?
900       algo = GetAlgo();
901       if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
902         // check if algo changes
903         SMESH_HypoFilter f;
904         f.Init(   SMESH_HypoFilter::IsAlgo() );
905         f.And(    SMESH_HypoFilter::IsApplicableTo( _subShape ));
906         f.AndNot( SMESH_HypoFilter::Is( algo ));
907         const SMESH_Hypothesis * prevAlgo = _father->GetHypothesis( this, f, true );
908         if (prevAlgo &&
909             string( algo->GetName()) != prevAlgo->GetName())
910           modifiedHyp = true;
911       }
912       else
913         setAlgoState(MISSING_HYP);
914       break;
915     }
916     case REMOVE_HYP: {
917       algo = GetAlgo();
918       ASSERT(algo);
919       if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
920         setAlgoState(HYP_OK);
921       else
922         setAlgoState(MISSING_HYP);
923       modifiedHyp = true;
924       break;
925     }
926     case REMOVE_ALGO: {         // perhaps a father algo applies ?
927       algo = GetAlgo();
928       if (algo == NULL)   // no more algo applying on sub-shape...
929       {
930         setAlgoState(NO_ALGO);
931       }
932       else
933       {
934         if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
935           // check if algo remains
936           if ( anHyp != algo && strcmp( anHyp->GetName(), algo->GetName()) )
937             modifiedHyp = true;
938         }
939         else
940           setAlgoState(MISSING_HYP);
941       }
942       break;
943     }
944     case MODIF_HYP: // hypothesis value may become bad
945     case ADD_FATHER_HYP: {  // new father hypothesis ?
946       algo = GetAlgo();
947       ASSERT(algo);
948       if ( algo->CheckHypothesis((*_father),_subShape, aux_ret ))
949       {
950         if (_father->IsUsedHypothesis( anHyp, this )) // new Hyp
951           modifiedHyp = true;
952       }
953       else
954         setAlgoState(MISSING_HYP);
955       break;
956     }
957     case ADD_FATHER_ALGO: {
958       algo = GetAlgo();
959       if ( algo == anHyp ) { // a new algo on father
960         if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
961           // check if algo changes
962           SMESH_HypoFilter f;
963           f.Init(   SMESH_HypoFilter::IsAlgo() );
964           f.And(    SMESH_HypoFilter::IsApplicableTo( _subShape ));
965           f.AndNot( SMESH_HypoFilter::Is( algo ));
966           const SMESH_Hypothesis* prevAlgo = _father->GetHypothesis( this, f, true );
967           if (prevAlgo &&
968               string(algo->GetName()) != string(prevAlgo->GetName()) )
969             modifiedHyp = true;
970         }
971         else
972           setAlgoState(MISSING_HYP);
973       }
974       break;
975     }
976     case REMOVE_FATHER_HYP: {
977       algo = GetAlgo();
978       ASSERT(algo);
979       if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
980         // is there the same local hyp or maybe a new father algo applied?
981         if ( !getSimilarAttached( _subShape, anHyp ) )
982           modifiedHyp = true;
983       }
984       else
985         setAlgoState(MISSING_HYP);
986       break;
987     }
988     case REMOVE_FATHER_ALGO: {
989       // IPAL21346. Edges not removed when Netgen 1d-2d is removed from a SOLID.
990       // CLEAN was not called at event REMOVE_ALGO because the algo is not applicable to SOLID.
991       algo = dynamic_cast<SMESH_Algo*> (anHyp);
992       if (!algo->NeedDiscreteBoundary())
993         algoRequiringCleaning = algo;
994       algo = GetAlgo();
995       if (algo == NULL)  // no more applying algo on father
996       {
997         setAlgoState(NO_ALGO);
998       }
999       else
1000       {
1001         if ( algo->CheckHypothesis((*_father),_subShape, aux_ret )) {
1002           // check if algo changes
1003           if ( string(algo->GetName()) != string( anHyp->GetName()) )
1004             modifiedHyp = true;
1005         }
1006         else
1007           setAlgoState(MISSING_HYP);
1008       }
1009       break;
1010     }
1011     default:
1012       ASSERT(0);
1013       break;
1014     }
1015     break;
1016
1017     // ----------------------------------------------------------------------
1018
1019   default:
1020     ASSERT(0);
1021     break;
1022   }
1023
1024   // detect algorithm hiding
1025   //
1026   if ( ret == SMESH_Hypothesis::HYP_OK && 
1027        ( event == ADD_ALGO || event == ADD_FATHER_ALGO ) && algo && 
1028        algo->GetName() == anHyp->GetName() )
1029   {
1030     // is algo hidden?
1031     SMESH_Gen* gen = _father->GetGen();
1032     const std::vector< SMESH_subMesh * > & ancestors = GetAncestors();
1033     for ( size_t iA = 0; ( ret == SMESH_Hypothesis::HYP_OK && iA < ancestors.size()); ++iA ) {
1034       if ( SMESH_Algo* upperAlgo = ancestors[ iA ]->GetAlgo() )
1035         if ( !upperAlgo->NeedDiscreteBoundary() && !upperAlgo->SupportSubmeshes())
1036           ret = SMESH_Hypothesis::HYP_HIDDEN_ALGO;
1037     }
1038     // is algo hiding?
1039     if ( ret == SMESH_Hypothesis::HYP_OK &&
1040          !algo->NeedDiscreteBoundary()    &&
1041          !algo->SupportSubmeshes())
1042     {
1043       TopoDS_Shape algoAssignedTo, otherAssignedTo;
1044       gen->GetAlgo( this, &algoAssignedTo );
1045       map<int, SMESH_subMesh*>::reverse_iterator i_sm = _mapDepend.rbegin();
1046       for ( ; ( ret == SMESH_Hypothesis::HYP_OK && i_sm != _mapDepend.rend()) ; ++i_sm )
1047         if ( gen->GetAlgo( i_sm->second, &otherAssignedTo ) &&
1048              SMESH_MesherHelper::IsSubShape( /*sub=*/otherAssignedTo, /*main=*/algoAssignedTo ))
1049           ret = SMESH_Hypothesis::HYP_HIDING_ALGO;
1050     }
1051   }
1052
1053   if ( _algo ) { // get an error description set by _algo->CheckHypothesis()
1054     _computeError = _algo->GetComputeError();
1055     _algo->InitComputeError();
1056   }
1057
1058   bool stateChange = ( _algoState != oldAlgoState );
1059
1060   if ( stateChange && _algoState == HYP_OK ) // hyp becomes OK
1061     algo->SetEventListener( this );
1062
1063   if ( event == REMOVE_ALGO || event == REMOVE_FATHER_ALGO )
1064     _algo = 0;
1065
1066   notifyListenersOnEvent( event, ALGO_EVENT, anHyp );
1067
1068   if ( stateChange && oldAlgoState == HYP_OK ) { // hyp becomes KO
1069     deleteOwnListeners();
1070     SetIsAlwaysComputed( false );
1071     if (_subShape.ShapeType() == TopAbs_VERTEX ) {
1072       // restore default states
1073       _algoState = HYP_OK;
1074       _computeState = READY_TO_COMPUTE;
1075     }
1076   }
1077
1078   if ( algoRequiringCleaning ) {
1079     // added or removed algo is all-dimensional
1080     ComputeStateEngine( CLEAN );
1081     cleanDependsOn( algoRequiringCleaning );
1082     ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1083   }
1084
1085   if ( stateChange || modifiedHyp )
1086     ComputeStateEngine( MODIF_ALGO_STATE );
1087
1088   _realComputeCost = ( _algoState == HYP_OK ) ? computeCost() : 0;
1089
1090   return ret;
1091 }
1092
1093 //=======================================================================
1094 //function : IsConform
1095 //purpose  : check if a conform mesh will be produced by the Algo
1096 //=======================================================================
1097
1098 bool SMESH_subMesh::IsConform(const SMESH_Algo* theAlgo)
1099 {
1100 //  MESSAGE( "SMESH_subMesh::IsConform" );
1101   if ( !theAlgo ) return false;
1102
1103   // Suppose that theAlgo is applicable to _subShape, do not check it here
1104   //if ( !IsApplicableHypotesis( theAlgo )) return false;
1105
1106   // check only algo that doesn't NeedDiscreteBoundary(): because mesh made
1107   // on a sub-shape will be ignored by theAlgo
1108   if ( theAlgo->NeedDiscreteBoundary() ||
1109        !theAlgo->OnlyUnaryInput() ) // all adjacent shapes will be meshed by this algo?
1110     return true;
1111
1112   // only local algo is to be checked
1113   //if ( gen->IsGlobalHypothesis( theAlgo, *_father ))
1114   if ( _subShape.ShapeType() == _father->GetMeshDS()->ShapeToMesh().ShapeType() )
1115     return true;
1116
1117   // check algo attached to adjacent shapes
1118
1119   // loop on one level down sub-meshes
1120   TopoDS_Iterator itsub( _subShape );
1121   for (; itsub.More(); itsub.Next())
1122   {
1123     // loop on adjacent subShapes
1124     const std::vector< SMESH_subMesh * > & ancestors = GetAncestors();
1125     for ( size_t iA = 0; iA < ancestors.size(); ++iA )
1126     {
1127       const TopoDS_Shape& adjacent = ancestors[ iA ]->GetSubShape();
1128       if ( _subShape.IsSame( adjacent )) continue;
1129       if ( adjacent.ShapeType() != _subShape.ShapeType())
1130         break;
1131
1132       // check algo attached to smAdjacent
1133       SMESH_Algo * algo = ancestors[ iA ]->GetAlgo();
1134       if (algo &&
1135           !algo->NeedDiscreteBoundary() &&
1136           algo->OnlyUnaryInput())
1137         return false; // NOT CONFORM MESH WILL BE PRODUCED
1138     }
1139   }
1140
1141   return true;
1142 }
1143
1144 //=============================================================================
1145 /*!
1146  *
1147  */
1148 //=============================================================================
1149
1150 void SMESH_subMesh::setAlgoState(algo_state state)
1151 {
1152   _algoState = state;
1153 }
1154
1155 //================================================================================
1156 /*!
1157  * \brief Send an event to sub-meshes
1158  *  \param [in] event - the event
1159  *  \param [in] anHyp - an hypothesis
1160  *  \param [in] exitOnFatal - to stop iteration on sub-meshes if a sub-mesh
1161  *              reports a fatal result
1162  *  \return SMESH_Hypothesis::Hypothesis_Status - the worst result
1163  *
1164  * Optional description of a problematic situation (if any) can be retrieved
1165  * via GetComputeError().
1166  */
1167 //================================================================================
1168
1169 SMESH_Hypothesis::Hypothesis_Status
1170   SMESH_subMesh::SubMeshesAlgoStateEngine(int                event,
1171                                           SMESH_Hypothesis * anHyp,
1172                                           bool               exitOnFatal)
1173 {
1174   SMESH_Hypothesis::Hypothesis_Status ret = SMESH_Hypothesis::HYP_OK;
1175   //EAP: a wire (dim==1) should notify edges (dim==1)
1176   //EAP: int dim = SMESH_Gen::GetShapeDim(_subShape);
1177   //if (_subShape.ShapeType() < TopAbs_EDGE ) // wire,face etc
1178   {
1179     SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1180     while ( smIt->more() ) {
1181       SMESH_subMesh* sm = smIt->next();
1182       SMESH_Hypothesis::Hypothesis_Status ret2 = sm->AlgoStateEngine(event, anHyp);
1183       if ( ret2 > ret )
1184       {
1185         ret = ret2;
1186         _computeError = sm->_computeError;
1187         sm->_computeError.reset();
1188         if ( exitOnFatal && SMESH_Hypothesis::IsStatusFatal( ret ))
1189           break;
1190       }
1191     }
1192   }
1193   return ret;
1194 }
1195
1196 //================================================================================
1197 /*!
1198  * \brief Remove elements from sub-meshes.
1199  *  \param algoRequiringCleaning - an all-dimensional algorithm whose presence
1200  *         causes the cleaning.
1201  */
1202 //================================================================================
1203
1204 void SMESH_subMesh::cleanDependsOn( SMESH_Algo* algoRequiringCleaning/*=0*/ )
1205 {
1206   SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,
1207                                                        /*complexShapeFirst=*/true);
1208   if ( _father->NbNodes() == 0 )
1209   {
1210     while ( smIt->more() )
1211       smIt->next()->ComputeStateEngine(CHECK_COMPUTE_STATE);
1212   }
1213   else if ( !algoRequiringCleaning || !algoRequiringCleaning->SupportSubmeshes() )
1214   {
1215     while ( smIt->more() )
1216       smIt->next()->ComputeStateEngine(CLEAN);
1217   }
1218   else if ( algoRequiringCleaning && algoRequiringCleaning->SupportSubmeshes() )
1219   {
1220     // find sub-meshes to keep elements on
1221     set< SMESH_subMesh* > smToKeep;
1222     TopAbs_ShapeEnum prevShapeType = TopAbs_SHAPE;
1223     bool toKeepPrevShapeType = false;
1224     while ( smIt->more() )
1225     {
1226       SMESH_subMesh* sm = smIt->next();
1227       sm->ComputeStateEngine(CHECK_COMPUTE_STATE);
1228       if ( !sm->IsEmpty() )
1229       {
1230         const bool sameShapeType = ( prevShapeType == sm->GetSubShape().ShapeType() );
1231         bool       keepSubMeshes = ( sameShapeType && toKeepPrevShapeType );
1232         if ( !sameShapeType )
1233         {
1234           // check if the algo allows presence of global algos of dimension the algo
1235           // can generate it-self
1236           int  shapeDim = SMESH_Gen::GetShapeDim( sm->GetSubShape() );
1237           keepSubMeshes = algoRequiringCleaning->NeedLowerHyps( shapeDim );
1238           prevShapeType = sm->GetSubShape().ShapeType();
1239           toKeepPrevShapeType = keepSubMeshes;
1240         }
1241         if ( !keepSubMeshes )
1242         {
1243           // look for a local algo used to mesh sm
1244           TopoDS_Shape algoShape = SMESH_MesherHelper::GetShapeOfHypothesis
1245             ( algoRequiringCleaning, _subShape, _father );
1246           SMESH_HypoFilter moreLocalAlgo;
1247           moreLocalAlgo.Init( SMESH_HypoFilter::IsMoreLocalThan( algoShape, *_father ));
1248           moreLocalAlgo.And ( SMESH_HypoFilter::IsAlgo() );
1249           bool localAlgoFound = _father->GetHypothesis( sm->_subShape, moreLocalAlgo, true );
1250           keepSubMeshes = localAlgoFound;
1251         }
1252         // remember all sub-meshes of sm
1253         if ( keepSubMeshes )
1254         {
1255           SMESH_subMeshIteratorPtr smIt2 = getDependsOnIterator(false);
1256           while ( smIt2->more() )
1257             smToKeep.insert( smIt2->next() );
1258         }
1259       }
1260     }
1261     // remove elements
1262     SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,true);
1263     while ( smIt->more() )
1264     {
1265       SMESH_subMesh* sm = smIt->next();
1266       if ( !smToKeep.count( sm ))
1267         sm->ComputeStateEngine(CLEAN);
1268     }
1269   }
1270 }
1271
1272 //=============================================================================
1273 /*!
1274  *
1275  */
1276 //=============================================================================
1277
1278 void SMESH_subMesh::DumpAlgoState(bool isMain)
1279 {
1280   if (isMain)
1281   {
1282     const map < int, SMESH_subMesh * >&subMeshes = DependsOn();
1283
1284     map < int, SMESH_subMesh * >::const_iterator itsub;
1285     for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
1286     {
1287       SMESH_subMesh *sm = (*itsub).second;
1288       sm->DumpAlgoState(false);
1289     }
1290   }
1291   MESSAGE("dim = " << SMESH_Gen::GetShapeDim(_subShape) <<
1292           " type of shape " << _subShape.ShapeType());
1293   switch (_algoState)
1294   {
1295   case NO_ALGO          : MESSAGE(" AlgoState = NO_ALGO"); break;
1296   case MISSING_HYP      : MESSAGE(" AlgoState = MISSING_HYP"); break;
1297   case HYP_OK           : MESSAGE(" AlgoState = HYP_OK");break;
1298   }
1299   switch (_computeState)
1300   {
1301   case NOT_READY        : MESSAGE(" ComputeState = NOT_READY");break;
1302   case READY_TO_COMPUTE : MESSAGE(" ComputeState = READY_TO_COMPUTE");break;
1303   case COMPUTE_OK       : MESSAGE(" ComputeState = COMPUTE_OK");break;
1304   case FAILED_TO_COMPUTE: MESSAGE(" ComputeState = FAILED_TO_COMPUTE");break;
1305   }
1306 }
1307
1308 //================================================================================
1309 /*!
1310  * \brief Remove nodes and elements bound to submesh
1311   * \param subMesh - submesh containing nodes and elements
1312  */
1313 //================================================================================
1314
1315 static void cleanSubMesh( SMESH_subMesh * subMesh )
1316 {
1317   if (subMesh) {
1318     if (SMESHDS_SubMesh * subMeshDS = subMesh->GetSubMeshDS()) {
1319       SMESHDS_Mesh * meshDS = subMesh->GetFather()->GetMeshDS();
1320       SMDS_ElemIteratorPtr ite = subMeshDS->GetElements();
1321       while (ite->more()) {
1322         const SMDS_MeshElement * elt = ite->next();
1323         //MESSAGE( " RM elt: "<<elt->GetID()<<" ( "<<elt->NbNodes()<<" )" );
1324         //meshDS->RemoveElement(elt);
1325         meshDS->RemoveFreeElement(elt, 0);
1326       }
1327
1328       SMDS_NodeIteratorPtr itn = subMeshDS->GetNodes();
1329       while (itn->more()) {
1330         const SMDS_MeshNode * node = itn->next();
1331         //MESSAGE( " RM node: "<<node->GetID());
1332         if ( node->NbInverseElements() == 0 )
1333           meshDS->RemoveFreeNode(node, 0);
1334         else // for StdMeshers_CompositeSegment_1D: node in one submesh, edge in another
1335           meshDS->RemoveNode(node);
1336       }
1337       subMeshDS->Clear();
1338     }
1339   }
1340 }
1341
1342 //=============================================================================
1343 /*!
1344  *
1345  */
1346 //=============================================================================
1347
1348 bool SMESH_subMesh::ComputeStateEngine(int event)
1349 {
1350   switch ( event ) {
1351   case MODIF_ALGO_STATE:
1352   case COMPUTE:
1353   case COMPUTE_SUBMESH:
1354     //case COMPUTE_CANCELED:
1355   case CLEAN:
1356     //case SUBMESH_COMPUTED:
1357     //case SUBMESH_RESTORED:
1358     //case SUBMESH_LOADED:
1359     //case MESH_ENTITY_REMOVED:
1360     //case CHECK_COMPUTE_STATE:
1361     _computeError.reset(); break;
1362   default:;
1363   }
1364
1365   if ( event == CLEAN )
1366     _alwaysComputed = false; // Unset 'true' set by MergeNodes() (issue 0022182)
1367
1368   if (_subShape.ShapeType() == TopAbs_VERTEX)
1369   {
1370     _computeState = READY_TO_COMPUTE;
1371     SMESHDS_SubMesh* smDS = GetSubMeshDS();
1372     if ( smDS && smDS->NbNodes() )
1373     {
1374       if ( event == CLEAN ) {
1375         cleanDependants();
1376         cleanSubMesh( this );
1377       }
1378       else
1379         _computeState = COMPUTE_OK;
1380     }
1381     else if (( event == COMPUTE || event == COMPUTE_SUBMESH )
1382              && !_alwaysComputed )
1383     {
1384       const TopoDS_Vertex & V = TopoDS::Vertex( _subShape );
1385       gp_Pnt P = BRep_Tool::Pnt(V);
1386       if ( SMDS_MeshNode * n = _father->GetMeshDS()->AddNode(P.X(), P.Y(), P.Z()) ) {
1387         _father->GetMeshDS()->SetNodeOnVertex(n,_Id);
1388         _computeState = COMPUTE_OK;
1389       }
1390     }
1391     if ( event == MODIF_ALGO_STATE )
1392       cleanDependants();
1393     return true;
1394   }
1395   SMESH_Gen *gen = _father->GetGen();
1396   SMESH_Algo *algo = 0;
1397   bool ret = true;
1398   SMESH_Hypothesis::Hypothesis_Status hyp_status;
1399   //algo_state oldAlgoState = (algo_state) GetAlgoState();
1400
1401   switch (_computeState)
1402   {
1403
1404     // ----------------------------------------------------------------------
1405
1406   case NOT_READY:
1407     switch (event)
1408     {
1409     case MODIF_ALGO_STATE:
1410       algo = GetAlgo();
1411       if (algo && !algo->NeedDiscreteBoundary())
1412         cleanDependsOn( algo ); // clean sub-meshes with event CLEAN
1413       if ( _algoState == HYP_OK )
1414         _computeState = READY_TO_COMPUTE;
1415       break;
1416     case COMPUTE:               // nothing to do
1417     case COMPUTE_SUBMESH:
1418       break;
1419     case COMPUTE_CANCELED:      // nothing to do
1420       break;
1421     case CLEAN:
1422       cleanDependants();
1423       removeSubMeshElementsAndNodes();
1424       break;
1425     case SUBMESH_COMPUTED:      // nothing to do
1426       break;
1427     case SUBMESH_RESTORED:
1428       ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1429       break;
1430     case MESH_ENTITY_REMOVED:
1431       break;
1432     case SUBMESH_LOADED:
1433       loadDependentMeshes();
1434       ComputeSubMeshStateEngine( SUBMESH_LOADED );
1435       //break;
1436     case CHECK_COMPUTE_STATE:
1437       if ( IsMeshComputed() )
1438         _computeState = COMPUTE_OK;
1439       break;
1440     default:
1441       ASSERT(0);
1442       break;
1443     }
1444     break;
1445
1446     // ----------------------------------------------------------------------
1447
1448   case READY_TO_COMPUTE:
1449     switch (event)
1450     {
1451     case MODIF_ALGO_STATE:
1452       _computeState = NOT_READY;
1453       algo = GetAlgo();
1454       if (algo)
1455       {
1456         if (!algo->NeedDiscreteBoundary())
1457           cleanDependsOn( algo ); // clean sub-meshes with event CLEAN
1458         if ( _algoState == HYP_OK )
1459           _computeState = READY_TO_COMPUTE;
1460       }
1461       break;
1462     case COMPUTE:
1463     case COMPUTE_SUBMESH:
1464       {
1465         algo = GetAlgo();
1466         ASSERT(algo);
1467         ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1468         if (!ret)
1469         {
1470           MESSAGE("***** verify compute state *****");
1471           _computeState = NOT_READY;
1472           setAlgoState(MISSING_HYP);
1473           break;
1474         }
1475         TopoDS_Shape shape = _subShape;
1476         algo->SubMeshesToCompute().assign( 1, this );
1477         // check submeshes needed
1478         if (_father->HasShapeToMesh() ) {
1479           bool subComputed = false, subFailed = false;
1480           if (!algo->OnlyUnaryInput()) {
1481             if ( event == COMPUTE /*&&
1482                  ( algo->NeedDiscreteBoundary() || algo->SupportSubmeshes() )*/)
1483               shape = getCollection( gen, algo, subComputed, subFailed, algo->SubMeshesToCompute());
1484             else
1485               subComputed = SubMeshesComputed( & subFailed );
1486           }
1487           else {
1488             subComputed = SubMeshesComputed();
1489           }
1490           ret = ( algo->NeedDiscreteBoundary() ? subComputed :
1491                   algo->SupportSubmeshes() ? !subFailed :
1492                   ( !subComputed || _father->IsNotConformAllowed() ));
1493           if (!ret)
1494           {
1495             _computeState = FAILED_TO_COMPUTE;
1496             if ( !algo->NeedDiscreteBoundary() && !subFailed )
1497               _computeError =
1498                 SMESH_ComputeError::New(COMPERR_BAD_INPUT_MESH,
1499                                         "Unexpected computed sub-mesh",algo);
1500             break; // goto exit
1501           }
1502         }
1503         // Compute
1504
1505         // to restore cout that may be redirected by algo
1506         std::streambuf* coutBuffer = std::cout.rdbuf();
1507
1508         //cleanDependants(); for "UseExisting_*D" algos
1509         //removeSubMeshElementsAndNodes();
1510         loadDependentMeshes();
1511         ret = false;
1512         _computeState = FAILED_TO_COMPUTE;
1513         _computeError = SMESH_ComputeError::New(COMPERR_OK,"",algo);
1514         try {
1515           OCC_CATCH_SIGNALS;
1516
1517           algo->InitComputeError();
1518
1519           MemoryReserve aMemoryReserve;
1520           SMDS_Mesh::CheckMemory();
1521           Kernel_Utils::Localizer loc;
1522           if ( !_father->HasShapeToMesh() ) // no shape
1523           {
1524             SMESH_MesherHelper helper( *_father );
1525             helper.SetSubShape( shape );
1526             helper.SetElementsOnShape( true );
1527             ret = algo->Compute(*_father, &helper );
1528           }
1529           else
1530           {
1531             ret = algo->Compute((*_father), shape);
1532           }
1533           // algo can set _computeError of submesh
1534           _computeError = SMESH_ComputeError::Worst( _computeError, algo->GetComputeError() );
1535         }
1536         catch ( ::SMESH_ComputeError& comperr ) {
1537           cout << " SMESH_ComputeError caught" << endl;
1538           if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1539           *_computeError = comperr;
1540         }
1541         catch ( std::bad_alloc& exc ) {
1542           MESSAGE("std::bad_alloc thrown inside algo->Compute()");
1543           if ( _computeError ) {
1544             _computeError->myName = COMPERR_MEMORY_PB;
1545             //_computeError->myComment = exc.what();
1546           }
1547           cleanSubMesh( this );
1548           throw exc;
1549         }
1550         catch ( Standard_OutOfMemory& exc ) {
1551           MESSAGE("Standard_OutOfMemory thrown inside algo->Compute()");
1552           if ( _computeError ) {
1553             _computeError->myName = COMPERR_MEMORY_PB;
1554             //_computeError->myComment = exc.what();
1555           }
1556           cleanSubMesh( this );
1557           throw std::bad_alloc();
1558         }
1559         catch (Standard_Failure& ex) {
1560           if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1561           _computeError->myName    = COMPERR_OCC_EXCEPTION;
1562           _computeError->myComment += ex.DynamicType()->Name();
1563           if ( ex.GetMessageString() && strlen( ex.GetMessageString() )) {
1564             _computeError->myComment += ": ";
1565             _computeError->myComment += ex.GetMessageString();
1566           }
1567         }
1568         catch ( SALOME_Exception& S_ex ) {
1569           const int skipSalomeShift = 7; /* to skip "Salome " of
1570                                             "Salome Exception" prefix returned
1571                                             by SALOME_Exception::what() */
1572           if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1573           _computeError->myName    = COMPERR_SLM_EXCEPTION;
1574           _computeError->myComment = S_ex.what() + skipSalomeShift;
1575         }
1576         catch ( std::exception& exc ) {
1577           if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1578           _computeError->myName    = COMPERR_STD_EXCEPTION;
1579           _computeError->myComment = exc.what();
1580         }
1581         catch ( ... ) {
1582           if ( _computeError )
1583             _computeError->myName = COMPERR_EXCEPTION;
1584           else
1585             ret = false;
1586         }
1587         std::cout.rdbuf( coutBuffer ); // restore cout that could be redirected by algo
1588
1589         // check if an error reported on any sub-shape
1590         bool isComputeErrorSet = !checkComputeError( algo, ret, shape );
1591         if ( isComputeErrorSet )
1592           ret = false;
1593         // check if anything was built
1594         TopExp_Explorer subS(shape, _subShape.ShapeType());
1595         if (ret)
1596         {
1597           for (; ret && subS.More(); subS.Next())
1598             if ( !_father->GetSubMesh( subS.Current() )->IsMeshComputed() &&
1599                  ( _subShape.ShapeType() != TopAbs_EDGE ||
1600                    !algo->isDegenerated( TopoDS::Edge( subS.Current() ))))
1601               ret = false;
1602         }
1603         // Set _computeError
1604         if (!ret && !isComputeErrorSet)
1605         {
1606           for (subS.ReInit(); subS.More(); subS.Next())
1607           {
1608             SMESH_subMesh* sm = _father->GetSubMesh( subS.Current() );
1609             if ( !sm->IsMeshComputed() )
1610             {
1611               if ( !sm->_computeError )
1612                 sm->_computeError = SMESH_ComputeError::New();
1613               if ( sm->_computeError->IsOK() )
1614                 sm->_computeError->myName = COMPERR_ALGO_FAILED;
1615               sm->_computeState = FAILED_TO_COMPUTE;
1616               sm->_computeError->myAlgo = algo;
1617             }
1618           }
1619         }
1620         if (ret && _computeError && _computeError->myName != COMPERR_WARNING )
1621         {
1622           _computeError.reset();
1623         }
1624
1625         // send event SUBMESH_COMPUTED
1626         if ( ret ) {
1627           if ( !algo->NeedDiscreteBoundary() )
1628             // send SUBMESH_COMPUTED to dependants of all sub-meshes of shape
1629             for (subS.ReInit(); subS.More(); subS.Next())
1630             {
1631               SMESH_subMesh* sm = _father->GetSubMesh( subS.Current() );
1632               SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(false,false);
1633               while ( smIt->more() ) {
1634                 sm = smIt->next();
1635                 if ( sm->GetSubShape().ShapeType() == TopAbs_VERTEX )
1636                   sm->updateDependantsState( SUBMESH_COMPUTED );
1637                 else
1638                   break;
1639               }
1640             }
1641           else
1642             updateDependantsState( SUBMESH_COMPUTED );
1643         }
1644       }
1645       break;
1646     case COMPUTE_CANCELED:               // nothing to do
1647       break;
1648     case CLEAN:
1649       cleanDependants();
1650       removeSubMeshElementsAndNodes();
1651       _computeState = NOT_READY;
1652       algo = GetAlgo();
1653       if (algo)
1654       {
1655         ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1656         if (ret)
1657           _computeState = READY_TO_COMPUTE;
1658         else
1659           setAlgoState(MISSING_HYP);
1660       }
1661       break;
1662     case SUBMESH_COMPUTED:      // nothing to do
1663       break;
1664     case SUBMESH_RESTORED:
1665       // check if a mesh is already computed that may
1666       // happen after retrieval from a file
1667       ComputeStateEngine( CHECK_COMPUTE_STATE );
1668       ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1669       algo = GetAlgo();
1670       if (algo) algo->SubmeshRestored( this );
1671       break;
1672     case MESH_ENTITY_REMOVED:
1673       break;
1674     case SUBMESH_LOADED:
1675       loadDependentMeshes();
1676       ComputeSubMeshStateEngine( SUBMESH_LOADED );
1677       //break;
1678     case CHECK_COMPUTE_STATE:
1679       if ( IsMeshComputed() )
1680         _computeState = COMPUTE_OK;
1681       else if ( _computeError && _computeError->IsKO() )
1682         _computeState = FAILED_TO_COMPUTE;
1683       break;
1684     default:
1685       ASSERT(0);
1686       break;
1687     }
1688     break;
1689
1690     // ----------------------------------------------------------------------
1691
1692   case COMPUTE_OK:
1693     switch (event)
1694     {
1695     case MODIF_ALGO_STATE:
1696       ComputeStateEngine( CLEAN );
1697       algo = GetAlgo();
1698       if (algo && !algo->NeedDiscreteBoundary())
1699         cleanDependsOn( algo ); // clean sub-meshes with event CLEAN
1700       break;
1701     case COMPUTE:               // nothing to do
1702       break;
1703     case COMPUTE_CANCELED:      // nothing to do
1704       break;
1705     case CLEAN:
1706       cleanDependants();  // clean sub-meshes, dependant on this one, with event CLEAN
1707       removeSubMeshElementsAndNodes();
1708       _computeState = NOT_READY;
1709       if ( _algoState == HYP_OK )
1710         _computeState = READY_TO_COMPUTE;
1711       break;
1712     case SUBMESH_COMPUTED:      // nothing to do
1713       break;
1714     case SUBMESH_RESTORED:
1715       ComputeStateEngine( CHECK_COMPUTE_STATE );
1716       ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1717       algo = GetAlgo();
1718       if (algo) algo->SubmeshRestored( this );
1719       break;
1720     case MESH_ENTITY_REMOVED:
1721       updateDependantsState    ( CHECK_COMPUTE_STATE );
1722       ComputeStateEngine       ( CHECK_COMPUTE_STATE );
1723       ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1724       break;
1725     case CHECK_COMPUTE_STATE:
1726       if ( !IsMeshComputed() ) {
1727         if (_algoState == HYP_OK)
1728           _computeState = READY_TO_COMPUTE;
1729         else
1730           _computeState = NOT_READY;
1731       }
1732       break;
1733     case SUBMESH_LOADED:
1734       // already treated event, thanks to which _computeState == COMPUTE_OK
1735       break;
1736     default:
1737       ASSERT(0);
1738       break;
1739     }
1740     break;
1741
1742     // ----------------------------------------------------------------------
1743
1744   case FAILED_TO_COMPUTE:
1745     switch (event)
1746     {
1747     case MODIF_ALGO_STATE:
1748       if ( !IsEmpty() )
1749         ComputeStateEngine( CLEAN );
1750       algo = GetAlgo();
1751       if (algo && !algo->NeedDiscreteBoundary())
1752         cleanDependsOn( algo ); // clean sub-meshes with event CLEAN
1753       if (_algoState == HYP_OK)
1754         _computeState = READY_TO_COMPUTE;
1755       else
1756         _computeState = NOT_READY;
1757       break;
1758     case COMPUTE:        // nothing to do
1759     case COMPUTE_SUBMESH:
1760       break;
1761     case COMPUTE_CANCELED:
1762       {
1763         algo = GetAlgo();
1764         algo->CancelCompute();
1765       }
1766       break;
1767     case CLEAN:
1768       cleanDependants(); // submeshes dependent on me should be cleaned as well
1769       removeSubMeshElementsAndNodes();
1770       break;
1771     case SUBMESH_COMPUTED:      // allow retry compute
1772       if ( IsEmpty() ) // 23061
1773       {
1774         if (_algoState == HYP_OK)
1775           _computeState = READY_TO_COMPUTE;
1776         else
1777           _computeState = NOT_READY;
1778       }
1779       break;
1780     case SUBMESH_RESTORED:
1781       ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1782       break;
1783     case MESH_ENTITY_REMOVED:
1784       break;
1785     case CHECK_COMPUTE_STATE:
1786       if ( IsMeshComputed() )
1787         _computeState = COMPUTE_OK;
1788       else
1789         if (_algoState == HYP_OK)
1790           _computeState = READY_TO_COMPUTE;
1791         else
1792           _computeState = NOT_READY;
1793       break;
1794     // case SUBMESH_LOADED:
1795     //   break;
1796     default:
1797       ASSERT(0);
1798       break;
1799     }
1800     break;
1801
1802     // ----------------------------------------------------------------------
1803   default:
1804     ASSERT(0);
1805     break;
1806   }
1807
1808   notifyListenersOnEvent( event, COMPUTE_EVENT );
1809
1810   return ret;
1811 }
1812
1813
1814 //=============================================================================
1815 /*!
1816  *
1817  */
1818 //=============================================================================
1819
1820 bool SMESH_subMesh::Evaluate(MapShapeNbElems& aResMap)
1821 {
1822   _computeError.reset();
1823
1824   bool ret = true;
1825
1826   if (_subShape.ShapeType() == TopAbs_VERTEX) {
1827     vector<int> aVec(SMDSEntity_Last,0);
1828     aVec[SMDSEntity_Node] = 1;
1829     aResMap.insert(make_pair(this,aVec));
1830     return ret;
1831   }
1832
1833   //SMESH_Gen *gen = _father->GetGen();
1834   SMESH_Algo *algo = 0;
1835   SMESH_Hypothesis::Hypothesis_Status hyp_status;
1836
1837   algo = GetAlgo();
1838   if( algo && !aResMap.count( this ))
1839   {
1840     ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1841     if (!ret) return false;
1842
1843     if (_father->HasShapeToMesh() && algo->NeedDiscreteBoundary() )
1844     {
1845       // check submeshes needed
1846       bool subMeshEvaluated = true;
1847       int dimToCheck = SMESH_Gen::GetShapeDim( _subShape ) - 1;
1848       SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,/*complexShapeFirst=*/true);
1849       while ( smIt->more() && subMeshEvaluated )
1850       {
1851         SMESH_subMesh* sm = smIt->next();
1852         int dim = SMESH_Gen::GetShapeDim( sm->GetSubShape() );
1853         if (dim < dimToCheck) break; // the rest subMeshes are all of less dimension
1854         const vector<int> & nbs = aResMap[ sm ];
1855         subMeshEvaluated = (std::accumulate( nbs.begin(), nbs.end(), 0 ) > 0 );
1856       }
1857       if ( !subMeshEvaluated )
1858         return false;
1859     }
1860     _computeError = SMESH_ComputeError::New(COMPERR_OK,"",algo);
1861
1862     if ( IsMeshComputed() )
1863     {
1864       vector<int> & nbEntities = aResMap[ this ];
1865       nbEntities.resize( SMDSEntity_Last, 0 );
1866       if ( SMESHDS_SubMesh* sm = GetSubMeshDS() )
1867       {
1868         nbEntities[ SMDSEntity_Node ] = sm->NbNodes();
1869         SMDS_ElemIteratorPtr   elemIt = sm->GetElements();
1870         while ( elemIt->more() )
1871           nbEntities[ elemIt->next()->GetEntityType() ]++;
1872       }
1873     }
1874     else
1875     {
1876       ret = algo->Evaluate((*_father), _subShape, aResMap);
1877     }
1878     aResMap.insert( make_pair( this,vector<int>(0)));
1879   }
1880
1881   return ret;
1882 }
1883
1884
1885 //=======================================================================
1886 /*!
1887  * \brief Update compute_state by _computeError and send proper events to
1888  * dependent submeshes
1889   * \retval bool - true if _computeError is NOT set
1890  */
1891 //=======================================================================
1892
1893 bool SMESH_subMesh::checkComputeError(SMESH_Algo*         theAlgo,
1894                                       const bool          theComputeOK,
1895                                       const TopoDS_Shape& theShape)
1896 {
1897   bool noErrors = true;
1898
1899   if ( !theShape.IsNull() )
1900   {
1901     // Check state of submeshes
1902     if ( !theAlgo->NeedDiscreteBoundary())
1903     {
1904       SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1905       while ( smIt->more() )
1906         if ( !smIt->next()->checkComputeError( theAlgo, theComputeOK ))
1907           noErrors = false;
1908     }
1909
1910     // Check state of neighbours
1911     if ( !theAlgo->OnlyUnaryInput() &&
1912          theShape.ShapeType() == TopAbs_COMPOUND &&
1913          !theShape.IsSame( _subShape ))
1914     {
1915       for (TopoDS_Iterator subIt( theShape ); subIt.More(); subIt.Next()) {
1916         SMESH_subMesh* sm = _father->GetSubMesh( subIt.Value() );
1917         if ( sm != this ) {
1918           if ( !sm->checkComputeError( theAlgo, theComputeOK, sm->GetSubShape() ))
1919             noErrors = false;
1920           updateDependantsState( SUBMESH_COMPUTED ); // send event SUBMESH_COMPUTED
1921         }
1922       }
1923     }
1924   }
1925   {
1926
1927     // Set my _computeState
1928
1929     if ( !_computeError || _computeError->IsOK() )
1930     {
1931       // no error description is set to this sub-mesh, check if any mesh is computed
1932       _computeState = IsMeshComputed() ? COMPUTE_OK : FAILED_TO_COMPUTE;
1933       if ( _computeState != COMPUTE_OK )
1934       {
1935         if ( _subShape.ShapeType() == TopAbs_EDGE &&
1936              SMESH_Algo::isDegenerated( TopoDS::Edge( _subShape )) )
1937           _computeState = COMPUTE_OK;
1938         else if ( theComputeOK )
1939           _computeError = SMESH_ComputeError::New(COMPERR_NO_MESH_ON_SHAPE,"",theAlgo);
1940       }
1941     }
1942
1943     if ( _computeError && !_computeError->IsOK() )
1944     {
1945       if ( !_computeError->myAlgo )
1946         _computeError->myAlgo = theAlgo;
1947
1948       // Show error
1949       SMESH_Comment text;
1950       text << theAlgo->GetName() << " failed on sub-shape #" << _Id << " with error ";
1951       if (_computeError->IsCommon() )
1952         text << _computeError->CommonName();
1953       else
1954         text << _computeError->myName;
1955       if ( _computeError->myComment.size() > 0 )
1956         text << " \"" << _computeError->myComment << "\"";
1957
1958       INFOS( text );
1959
1960       _computeState = _computeError->IsKO() ? FAILED_TO_COMPUTE : COMPUTE_OK;
1961
1962       noErrors = false;
1963     }
1964   }
1965   return noErrors;
1966 }
1967
1968 //=======================================================================
1969 //function : updateSubMeshState
1970 //purpose  :
1971 //=======================================================================
1972
1973 void SMESH_subMesh::updateSubMeshState(const compute_state theState)
1974 {
1975   SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1976   while ( smIt->more() )
1977     smIt->next()->_computeState = theState;
1978 }
1979
1980 //=======================================================================
1981 //function : ComputeSubMeshStateEngine
1982 //purpose  :
1983 //=======================================================================
1984
1985 void SMESH_subMesh::ComputeSubMeshStateEngine(int event, const bool includeSelf)
1986 {
1987   SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(includeSelf,false);
1988   while ( smIt->more() )
1989     smIt->next()->ComputeStateEngine(event);
1990 }
1991
1992 //=======================================================================
1993 //function : updateDependantsState
1994 //purpose  :
1995 //=======================================================================
1996
1997 void SMESH_subMesh::updateDependantsState(const compute_event theEvent)
1998 {
1999   const std::vector< SMESH_subMesh * > & ancestors = GetAncestors();
2000   for ( size_t iA = 0; iA < ancestors.size(); ++iA )
2001   {
2002     ancestors[ iA ]->ComputeStateEngine( theEvent );
2003   }
2004 }
2005
2006 //=======================================================================
2007 //function : cleanDependants
2008 //purpose  : 
2009 //=======================================================================
2010
2011 void SMESH_subMesh::cleanDependants()
2012 {
2013   int dimToClean = SMESH_Gen::GetShapeDim( _subShape ) + 1;
2014
2015   const std::vector< SMESH_subMesh * > & ancestors = GetAncestors();
2016   for ( size_t iA = 0; iA < ancestors.size(); ++iA )
2017   {
2018     const TopoDS_Shape& ancestor = ancestors[ iA ]->GetSubShape();
2019     if ( SMESH_Gen::GetShapeDim( ancestor ) == dimToClean )
2020     {
2021       // PAL8021. do not go upper than SOLID, else ComputeStateEngine(CLEAN)
2022       // will erase mesh on other shapes in a compound
2023       if ( ancestor.ShapeType() >= TopAbs_SOLID &&
2024            !ancestors[ iA ]->IsEmpty() )  // prevent infinite CLEAN via event lesteners
2025         ancestors[ iA ]->ComputeStateEngine(CLEAN);
2026     }
2027   }
2028 }
2029
2030 //=======================================================================
2031 //function : removeSubMeshElementsAndNodes
2032 //purpose  : 
2033 //=======================================================================
2034
2035 void SMESH_subMesh::removeSubMeshElementsAndNodes()
2036 {
2037   cleanSubMesh( this );
2038
2039   // algo may bind a submesh not to _subShape, eg 3D algo
2040   // sets nodes on SHELL while _subShape may be SOLID
2041
2042   int dim = SMESH_Gen::GetShapeDim( _subShape );
2043   int type = _subShape.ShapeType() + 1;
2044   for ( ; type <= TopAbs_EDGE; type++) {
2045     if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
2046     {
2047       TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
2048       for ( ; exp.More(); exp.Next() )
2049         cleanSubMesh( _father->GetSubMeshContaining( exp.Current() ));
2050     }
2051     else
2052       break;
2053   }
2054 }
2055
2056 //=======================================================================
2057 //function : getCollection
2058 //purpose  : return a shape containing all sub-shapes of the MainShape that can be
2059 //           meshed at once along with _subShape
2060 //=======================================================================
2061
2062 TopoDS_Shape SMESH_subMesh::getCollection(SMESH_Gen * theGen,
2063                                           SMESH_Algo* theAlgo,
2064                                           bool &      theSubComputed,
2065                                           bool &      theSubFailed,
2066                                           std::vector<SMESH_subMesh*>& theSubs)
2067 {
2068   theSubComputed = SubMeshesComputed( & theSubFailed );
2069
2070   TopoDS_Shape mainShape = _father->GetMeshDS()->ShapeToMesh();
2071
2072   if ( mainShape.IsSame( _subShape ))
2073     return _subShape;
2074
2075   const bool skipAuxHyps = false;
2076   list<const SMESHDS_Hypothesis*> aUsedHyp =
2077     theAlgo->GetUsedHypothesis( *_father, _subShape, skipAuxHyps ); // copy
2078
2079   // put in a compound all shapes with the same hypothesis assigned
2080   // and a good ComputeState
2081
2082   TopoDS_Compound aCompound;
2083   BRep_Builder aBuilder;
2084   aBuilder.MakeCompound( aCompound );
2085
2086   theSubs.clear();
2087
2088   SMESH_subMeshIteratorPtr smIt = _father->GetSubMesh( mainShape )->getDependsOnIterator(false);
2089   while ( smIt->more() )
2090   {
2091     SMESH_subMesh* subMesh = smIt->next();
2092     const TopoDS_Shape&  S = subMesh->_subShape;
2093     if ( S.ShapeType() != this->_subShape.ShapeType() )
2094       continue;
2095     theSubs.push_back( subMesh );
2096     if ( subMesh == this )
2097     {
2098       aBuilder.Add( aCompound, S );
2099     }
2100     else if ( subMesh->GetComputeState() == READY_TO_COMPUTE )
2101     {
2102       SMESH_Algo* anAlgo = subMesh->GetAlgo();
2103       if (( anAlgo->IsSameName( *theAlgo )) && // same algo
2104           ( anAlgo->GetUsedHypothesis( *_father, S, skipAuxHyps ) == aUsedHyp )) // same hyps
2105       {
2106         aBuilder.Add( aCompound, S );
2107         if ( !subMesh->SubMeshesComputed() )
2108           theSubComputed = false;
2109       }
2110     }
2111   }
2112
2113   return aCompound;
2114 }
2115
2116 //=======================================================================
2117 //function : getSimilarAttached
2118 //purpose  : return a hypothesis attached to theShape.
2119 //           If theHyp is provided, similar but not same hypotheses
2120 //           is returned; else only applicable ones having theHypType
2121 //           is returned
2122 //=======================================================================
2123
2124 const SMESH_Hypothesis* SMESH_subMesh::getSimilarAttached(const TopoDS_Shape&      theShape,
2125                                                           const SMESH_Hypothesis * theHyp,
2126                                                           const int                theHypType)
2127 {
2128   SMESH_HypoFilter hypoKind;
2129   hypoKind.Init( hypoKind.HasType( theHyp ? theHyp->GetType() : theHypType ));
2130   if ( theHyp ) {
2131     hypoKind.And   ( hypoKind.HasDim( theHyp->GetDim() ));
2132     hypoKind.AndNot( hypoKind.Is( theHyp ));
2133     if ( theHyp->IsAuxiliary() )
2134       hypoKind.And( hypoKind.HasName( theHyp->GetName() ));
2135     else
2136       hypoKind.AndNot( hypoKind.IsAuxiliary());
2137   }
2138   else {
2139     hypoKind.And( hypoKind.IsApplicableTo( theShape ));
2140   }
2141
2142   return _father->GetHypothesis( theShape, hypoKind, false );
2143 }
2144
2145 //=======================================================================
2146 //function : CheckConcurentHypothesis
2147 //purpose  : check if there are several applicable hypothesis attached to
2148 //           ancestors
2149 //=======================================================================
2150
2151 SMESH_Hypothesis::Hypothesis_Status
2152   SMESH_subMesh::CheckConcurentHypothesis (const int theHypType)
2153 {
2154   MESSAGE ("SMESH_subMesh::CheckConcurentHypothesis");
2155
2156   // is there local hypothesis on me?
2157   if ( getSimilarAttached( _subShape, 0, theHypType ) )
2158     return SMESH_Hypothesis::HYP_OK;
2159
2160
2161   TopoDS_Shape aPrevWithHyp;
2162   const SMESH_Hypothesis* aPrevHyp = 0;
2163   TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
2164   for (; it.More(); it.Next())
2165   {
2166     const TopoDS_Shape& ancestor = it.Value();
2167     const SMESH_Hypothesis* hyp = getSimilarAttached( ancestor, 0, theHypType );
2168     if ( hyp )
2169     {
2170       if ( aPrevWithHyp.IsNull() || aPrevWithHyp.IsSame( ancestor ))
2171       {
2172         aPrevWithHyp = ancestor;
2173         aPrevHyp     = hyp;
2174       }
2175       else if ( aPrevWithHyp.ShapeType() == ancestor.ShapeType() && aPrevHyp != hyp )
2176         return SMESH_Hypothesis::HYP_CONCURENT;
2177       else
2178         return SMESH_Hypothesis::HYP_OK;
2179     }
2180   }
2181   return SMESH_Hypothesis::HYP_OK;
2182 }
2183
2184 //================================================================================
2185 /*!
2186  * \brief Constructor of OwnListenerData
2187  */
2188 //================================================================================
2189
2190 SMESH_subMesh::OwnListenerData::OwnListenerData( SMESH_subMesh* sm, EventListener* el):
2191   mySubMesh( sm ),
2192   myMeshID( sm ? sm->GetFather()->GetId() : -1 ),
2193   mySubMeshID( sm ? sm->GetId() : -1 ),
2194   myListener( el )
2195 {
2196 }
2197
2198 //================================================================================
2199 /*!
2200  * \brief Sets an event listener and its data to a submesh
2201  * \param listener - the listener to store
2202  * \param data - the listener data to store
2203  * \param where - the submesh to store the listener and it's data
2204  * 
2205  * It remembers the submesh where it puts the listener in order to delete
2206  * them when HYP_OK algo_state is lost
2207  * After being set, event listener is notified on each event of where submesh.
2208  */
2209 //================================================================================
2210
2211 void SMESH_subMesh::SetEventListener(EventListener*     listener,
2212                                      EventListenerData* data,
2213                                      SMESH_subMesh*     where)
2214 {
2215   if ( listener && where ) {
2216     where->setEventListener( listener, data );
2217     _ownListeners.push_back( OwnListenerData( where, listener ));
2218   }
2219 }
2220
2221 //================================================================================
2222 /*!
2223  * \brief Sets an event listener and its data to a submesh
2224  * \param listener - the listener to store
2225  * \param data - the listener data to store
2226  * 
2227  * After being set, event listener is notified on each event of a submesh.
2228  */
2229 //================================================================================
2230
2231 void SMESH_subMesh::setEventListener(EventListener*     listener,
2232                                      EventListenerData* data)
2233 {
2234   map< EventListener*, EventListenerData* >::iterator l_d =
2235     _eventListeners.find( listener );
2236   if ( l_d != _eventListeners.end() ) {
2237     EventListenerData* curData = l_d->second;
2238     if ( curData && curData != data && curData->IsDeletable() )
2239       delete curData;
2240     l_d->second = data;
2241   }
2242   else
2243   {
2244     for ( l_d = _eventListeners.begin(); l_d != _eventListeners.end(); ++l_d )
2245       if ( listener->GetName() == l_d->first->GetName() )
2246       {
2247         EventListenerData* curData = l_d->second;
2248         if ( curData && curData != data && curData->IsDeletable() )
2249           delete curData;
2250         if ( l_d->first != listener && l_d->first->IsDeletable() )
2251           delete l_d->first;
2252         _eventListeners.erase( l_d );
2253         break;
2254       }
2255     _eventListeners.insert( make_pair( listener, data ));
2256   }
2257 }
2258
2259 //================================================================================
2260 /*!
2261  * \brief Return an event listener data
2262  * \param listener - the listener whose data is
2263  * \param myOwn - if \c true, returns a listener set by this sub-mesh,
2264  *        else returns a listener listening to events of this sub-mesh
2265  * \retval EventListenerData* - found data, maybe NULL
2266  */
2267 //================================================================================
2268
2269 EventListenerData* SMESH_subMesh::GetEventListenerData(EventListener* listener,
2270                                                        const bool     myOwn) const
2271 {
2272   if ( myOwn )
2273   {
2274     list< OwnListenerData >::const_iterator d;
2275     for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
2276     {
2277       if ( d->myListener == listener && _father->MeshExists( d->myMeshID ))
2278         return d->mySubMesh->GetEventListenerData( listener, !myOwn );
2279     }
2280   }
2281   else
2282   {
2283     map< EventListener*, EventListenerData* >::const_iterator l_d =
2284       _eventListeners.find( listener );
2285     if ( l_d != _eventListeners.end() )
2286       return l_d->second;
2287   }
2288   return 0;
2289 }
2290
2291 //================================================================================
2292 /*!
2293  * \brief Return an event listener data
2294  * \param listenerName - the listener name
2295  * \param myOwn - if \c true, returns a listener set by this sub-mesh,
2296  *        else returns a listener listening to events of this sub-mesh
2297  * \retval EventListenerData* - found data, maybe NULL
2298  */
2299 //================================================================================
2300
2301 EventListenerData* SMESH_subMesh::GetEventListenerData(const string& listenerName,
2302                                                        const bool    myOwn) const
2303 {
2304   if ( myOwn )
2305   {
2306     list< OwnListenerData >::const_iterator d;
2307     for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
2308     {
2309       if ( _father->MeshExists( d->myMeshID ) && listenerName == d->myListener->GetName())
2310         return d->mySubMesh->GetEventListenerData( listenerName, !myOwn );
2311     }
2312   }
2313   else
2314   {
2315     map< EventListener*, EventListenerData* >::const_iterator l_d = _eventListeners.begin();
2316     for ( ; l_d != _eventListeners.end(); ++l_d )
2317       if ( listenerName == l_d->first->GetName() )
2318         return l_d->second;
2319   }
2320   return 0;
2321 }
2322
2323 //================================================================================
2324 /*!
2325  * \brief Notify stored event listeners on the occured event
2326  * \param event - algo_event or compute_event itself
2327  * \param eventType - algo_event or compute_event
2328  * \param hyp - hypothesis, if eventType is algo_event
2329  */
2330 //================================================================================
2331
2332 void SMESH_subMesh::notifyListenersOnEvent( const int         event,
2333                                             const event_type  eventType,
2334                                             SMESH_Hypothesis* hyp)
2335 {
2336   list< pair< EventListener*, EventListenerData* > > eventListeners( _eventListeners.begin(),
2337                                                                      _eventListeners.end());
2338   list< pair< EventListener*, EventListenerData* > >::iterator l_d = eventListeners.begin();
2339   for ( ; l_d != eventListeners.end(); ++l_d )
2340   {
2341     std::pair< EventListener*, EventListenerData* > li_da = *l_d;
2342     if ( !_eventListeners.count( li_da.first )) continue;
2343
2344     if ( li_da.first->myBusySM.insert( this ).second )
2345     {
2346       const bool isDeletable = li_da.first->IsDeletable();
2347
2348       li_da.first->ProcessEvent( event, eventType, this, li_da.second, hyp );
2349
2350       if ( !isDeletable || _eventListeners.count( li_da.first ))
2351         li_da.first->myBusySM.erase( this ); // a listener is hopefully not dead
2352     }
2353   }
2354 }
2355
2356 //================================================================================
2357 /*!
2358  * \brief Unregister the listener and delete listener's data
2359  * \param listener - the event listener
2360  */
2361 //================================================================================
2362
2363 void SMESH_subMesh::DeleteEventListener(EventListener* listener)
2364 {
2365   map< EventListener*, EventListenerData* >::iterator l_d =
2366     _eventListeners.find( listener );
2367   if ( l_d != _eventListeners.end() && l_d->first )
2368   {
2369     if ( l_d->second && l_d->second->IsDeletable() )
2370     {
2371       delete l_d->second;
2372     }
2373     l_d->first->myBusySM.erase( this );
2374     if ( l_d->first->IsDeletable() )
2375     {
2376       l_d->first->BeforeDelete( this, l_d->second );
2377       delete l_d->first;
2378     }
2379     _eventListeners.erase( l_d );
2380   }
2381 }
2382
2383 //================================================================================
2384 /*!
2385  * \brief Delete event listeners depending on algo of this submesh
2386  */
2387 //================================================================================
2388
2389 void SMESH_subMesh::deleteOwnListeners()
2390 {
2391   list< OwnListenerData >::iterator d;
2392   for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
2393   {
2394     SMESH_Mesh* mesh = _father->FindMesh( d->myMeshID );
2395     if ( !mesh || !mesh->GetSubMeshContaining( d->mySubMeshID ))
2396       continue;
2397     d->mySubMesh->DeleteEventListener( d->myListener );
2398   }
2399   _ownListeners.clear();
2400 }
2401
2402 //=======================================================================
2403 //function : loadDependentMeshes
2404 //purpose  : loads dependent meshes on SUBMESH_LOADED event
2405 //=======================================================================
2406
2407 void SMESH_subMesh::loadDependentMeshes()
2408 {
2409   list< OwnListenerData >::iterator d;
2410   for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
2411     if ( _father != d->mySubMesh->_father )
2412       d->mySubMesh->_father->Load();
2413
2414   // map< EventListener*, EventListenerData* >::iterator l_d = _eventListeners.begin();
2415   // for ( ; l_d != _eventListeners.end(); ++l_d )
2416   //   if ( l_d->second )
2417   //   {
2418   //     const list<SMESH_subMesh*>& smList = l_d->second->mySubMeshes;
2419   //     list<SMESH_subMesh*>::const_iterator sm = smList.begin();
2420   //     for ( ; sm != smList.end(); ++sm )
2421   //       if ( _father != (*sm)->_father )
2422   //         (*sm)->_father->Load();
2423   //   }
2424 }
2425
2426 //================================================================================
2427 /*!
2428  * \brief Do something on a certain event
2429  * \param event - algo_event or compute_event itself
2430  * \param eventType - algo_event or compute_event
2431  * \param subMesh - the submesh where the event occures
2432  * \param data - listener data stored in the subMesh
2433  * \param hyp - hypothesis, if eventType is algo_event
2434  * 
2435  * The base implementation translates CLEAN event to the subMesh
2436  * stored in listener data. Also it sends SUBMESH_COMPUTED event in case of
2437  * successful COMPUTE event.
2438  */
2439 //================================================================================
2440
2441 void SMESH_subMeshEventListener::ProcessEvent(const int          event,
2442                                               const int          eventType,
2443                                               SMESH_subMesh*     subMesh,
2444                                               EventListenerData* data,
2445                                               const SMESH_Hypothesis*  /*hyp*/)
2446 {
2447   if ( data && !data->mySubMeshes.empty() &&
2448        eventType == SMESH_subMesh::COMPUTE_EVENT)
2449   {
2450     ASSERT( data->mySubMeshes.front() != subMesh );
2451     list<SMESH_subMesh*>::iterator smIt = data->mySubMeshes.begin();
2452     list<SMESH_subMesh*>::iterator smEnd = data->mySubMeshes.end();
2453     switch ( event ) {
2454     case SMESH_subMesh::CLEAN:
2455       for ( ; smIt != smEnd; ++ smIt)
2456         (*smIt)->ComputeStateEngine( event );
2457       break;
2458     case SMESH_subMesh::COMPUTE:
2459     case SMESH_subMesh::COMPUTE_SUBMESH:
2460       if ( subMesh->GetComputeState() == SMESH_subMesh::COMPUTE_OK )
2461         for ( ; smIt != smEnd; ++ smIt)
2462           (*smIt)->ComputeStateEngine( SMESH_subMesh::SUBMESH_COMPUTED );
2463       break;
2464     default:;
2465     }
2466   }
2467 }
2468
2469 namespace {
2470
2471   //================================================================================
2472   /*!
2473    * \brief Iterator over submeshes and optionally prepended or appended one
2474    */
2475   //================================================================================
2476
2477   struct _Iterator : public SMDS_Iterator<SMESH_subMesh*>
2478   {
2479     _Iterator(SMDS_Iterator<SMESH_subMesh*>* subIt,
2480               SMESH_subMesh*                 prepend,
2481               SMESH_subMesh*                 append): myAppend(append), myIt(subIt)
2482     {
2483       myCur = prepend ? prepend : myIt->more() ? myIt->next() : append;
2484       if ( myCur == append ) append = 0;
2485     }
2486     /// Return true if and only if there are other object in this iterator
2487     virtual bool more()
2488     {
2489       return myCur;
2490     }
2491     /// Return the current object and step to the next one
2492     virtual SMESH_subMesh* next()
2493     {
2494       SMESH_subMesh* res = myCur;
2495       if ( myIt->more() ) { myCur = myIt->next(); }
2496       else                { myCur = myAppend; myAppend = 0; }
2497       return res;
2498     }
2499     /// ~
2500     ~_Iterator()
2501     { delete myIt; }
2502     ///
2503     SMESH_subMesh                 *myAppend, *myCur;
2504     SMDS_Iterator<SMESH_subMesh*> *myIt;
2505   };
2506 }
2507
2508 //================================================================================
2509 /*!
2510  * \brief  Return iterator on the submeshes this one depends on
2511   * \param includeSelf - this submesh to be returned also
2512   * \param reverse - if true, complex shape submeshes go first
2513  */
2514 //================================================================================
2515
2516 SMESH_subMeshIteratorPtr SMESH_subMesh::getDependsOnIterator(const bool includeSelf,
2517                                                              const bool reverse) const
2518 {
2519   SMESH_subMesh *me = (SMESH_subMesh*) this;
2520   SMESH_subMesh *prepend=0, *append=0;
2521   if ( includeSelf ) {
2522     if ( reverse ) prepend = me;
2523     else            append = me;
2524   }
2525   typedef map < int, SMESH_subMesh * > TMap;
2526   if ( reverse )
2527   {
2528     return SMESH_subMeshIteratorPtr
2529       ( new _Iterator( new SMDS_mapReverseIterator<TMap>( me->DependsOn() ), prepend, append ));
2530   }
2531   {
2532     return SMESH_subMeshIteratorPtr
2533       ( new _Iterator( new SMDS_mapIterator<TMap>( me->DependsOn() ), prepend, append ));
2534   }
2535 }
2536
2537 //================================================================================
2538 /*!
2539  * \brief Returns ancestor sub-meshes. Finds them if not yet found.
2540  */
2541 //================================================================================
2542
2543 const std::vector< SMESH_subMesh * > & SMESH_subMesh::GetAncestors() const
2544 {
2545   if ( _ancestors.empty() &&
2546        !_subShape.IsSame( _father->GetShapeToMesh() ))
2547   {
2548     const TopTools_ListOfShape& ancShapes = _father->GetAncestors( _subShape );
2549
2550     SMESH_subMesh* me = const_cast< SMESH_subMesh* >( this );
2551     me->_ancestors.reserve( ancShapes.Extent() );
2552
2553     TopTools_MapOfShape map;
2554    
2555     for ( TopTools_ListIteratorOfListOfShape it( ancShapes ); it.More(); it.Next() )
2556       if ( SMESH_subMesh* sm = _father->GetSubMeshContaining( it.Value() ))
2557         if ( map.Add( it.Value() ))
2558           me->_ancestors.push_back( sm );
2559   }
2560
2561   return _ancestors;
2562 }
2563
2564 //================================================================================
2565 /*!
2566  * \brief Clears the vector of ancestor sub-meshes
2567  */
2568 //================================================================================
2569
2570 void SMESH_subMesh::ClearAncestors()
2571 {
2572   _ancestors.clear();
2573 }
2574
2575 //================================================================================
2576 /*!
2577  * \brief  Find common submeshes (based on shared sub-shapes with other
2578   * \param theOther submesh to check
2579   * \param theSetOfCommon set of common submesh
2580  */
2581 //================================================================================
2582
2583 bool SMESH_subMesh::FindIntersection(const SMESH_subMesh*            theOther,
2584                                      std::set<const SMESH_subMesh*>& theSetOfCommon ) const
2585 {
2586   size_t oldNb = theSetOfCommon.size();
2587
2588   // check main submeshes
2589   const map <int, SMESH_subMesh*>::const_iterator otherEnd = theOther->_mapDepend.end();
2590   if ( theOther->_mapDepend.find(this->GetId()) != otherEnd )
2591     theSetOfCommon.insert( this );
2592   if ( _mapDepend.find(theOther->GetId()) != _mapDepend.end() )
2593     theSetOfCommon.insert( theOther );
2594
2595   // check common submeshes
2596   map <int, SMESH_subMesh*>::const_iterator mapIt = _mapDepend.begin();
2597   for( ; mapIt != _mapDepend.end(); mapIt++ )
2598     if ( theOther->_mapDepend.find((*mapIt).first) != otherEnd )
2599       theSetOfCommon.insert( (*mapIt).second );
2600   return oldNb < theSetOfCommon.size();
2601 }