Salome HOME
38a44570802e1d6ee81e7a912518f4ff68123752
[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     SMESHDS_Mesh* meshDS = _father->GetMeshDS();
1221
1222     // find sub-meshes to keep elements on
1223     set< SMESH_subMesh* > smToKeep;
1224     TopAbs_ShapeEnum prevShapeType = TopAbs_SHAPE;
1225     bool toKeepPrevShapeType = false;
1226     while ( smIt->more() )
1227     {
1228       SMESH_subMesh* sm = smIt->next();
1229       sm->ComputeStateEngine(CHECK_COMPUTE_STATE);
1230       if ( !sm->IsEmpty() )
1231       {
1232         const bool sameShapeType = ( prevShapeType == sm->GetSubShape().ShapeType() );
1233         bool       keepSubMeshes = ( sameShapeType && toKeepPrevShapeType );
1234         if ( !sameShapeType )
1235         {
1236           // check if the algo allows presence of global algos of dimension the algo
1237           // can generate it-self
1238           int  shapeDim = SMESH_Gen::GetShapeDim( sm->GetSubShape() );
1239           keepSubMeshes = algoRequiringCleaning->NeedLowerHyps( shapeDim );
1240           prevShapeType = sm->GetSubShape().ShapeType();
1241           toKeepPrevShapeType = keepSubMeshes;
1242         }
1243         if ( !keepSubMeshes )
1244         {
1245           // look for an algo assigned to sm
1246           bool algoFound = false;
1247           const list<const SMESHDS_Hypothesis*>& hyps = meshDS->GetHypothesis( sm->_subShape );
1248           list<const SMESHDS_Hypothesis*>::const_iterator h = hyps.begin();
1249           for ( ; ( !algoFound && h != hyps.end() ); ++h )
1250             algoFound = ((*h)->GetType() != SMESHDS_Hypothesis::PARAM_ALGO );
1251           keepSubMeshes = algoFound;
1252         }
1253         // remember all sub-meshes of sm
1254         if ( keepSubMeshes )
1255         {
1256           SMESH_subMeshIteratorPtr smIt2 = getDependsOnIterator(false);
1257           while ( smIt2->more() )
1258             smToKeep.insert( smIt2->next() );
1259         }
1260       }
1261     }
1262     // remove elements
1263     SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,true);
1264     while ( smIt->more() )
1265     {
1266       SMESH_subMesh* sm = smIt->next();
1267       if ( !smToKeep.count( sm ))
1268         sm->ComputeStateEngine(CLEAN);
1269     }
1270   }
1271 }
1272
1273 //=============================================================================
1274 /*!
1275  *
1276  */
1277 //=============================================================================
1278
1279 void SMESH_subMesh::DumpAlgoState(bool isMain)
1280 {
1281   if (isMain)
1282   {
1283     const map < int, SMESH_subMesh * >&subMeshes = DependsOn();
1284
1285     map < int, SMESH_subMesh * >::const_iterator itsub;
1286     for (itsub = subMeshes.begin(); itsub != subMeshes.end(); itsub++)
1287     {
1288       SMESH_subMesh *sm = (*itsub).second;
1289       sm->DumpAlgoState(false);
1290     }
1291   }
1292   MESSAGE("dim = " << SMESH_Gen::GetShapeDim(_subShape) <<
1293           " type of shape " << _subShape.ShapeType());
1294   switch (_algoState)
1295   {
1296   case NO_ALGO          : MESSAGE(" AlgoState = NO_ALGO"); break;
1297   case MISSING_HYP      : MESSAGE(" AlgoState = MISSING_HYP"); break;
1298   case HYP_OK           : MESSAGE(" AlgoState = HYP_OK");break;
1299   }
1300   switch (_computeState)
1301   {
1302   case NOT_READY        : MESSAGE(" ComputeState = NOT_READY");break;
1303   case READY_TO_COMPUTE : MESSAGE(" ComputeState = READY_TO_COMPUTE");break;
1304   case COMPUTE_OK       : MESSAGE(" ComputeState = COMPUTE_OK");break;
1305   case FAILED_TO_COMPUTE: MESSAGE(" ComputeState = FAILED_TO_COMPUTE");break;
1306   }
1307 }
1308
1309 //================================================================================
1310 /*!
1311  * \brief Remove nodes and elements bound to submesh
1312   * \param subMesh - submesh containing nodes and elements
1313  */
1314 //================================================================================
1315
1316 static void cleanSubMesh( SMESH_subMesh * subMesh )
1317 {
1318   if (subMesh) {
1319     if (SMESHDS_SubMesh * subMeshDS = subMesh->GetSubMeshDS()) {
1320       SMESHDS_Mesh * meshDS = subMesh->GetFather()->GetMeshDS();
1321       SMDS_ElemIteratorPtr ite = subMeshDS->GetElements();
1322       while (ite->more()) {
1323         const SMDS_MeshElement * elt = ite->next();
1324         //MESSAGE( " RM elt: "<<elt->GetID()<<" ( "<<elt->NbNodes()<<" )" );
1325         //meshDS->RemoveElement(elt);
1326         meshDS->RemoveFreeElement(elt, 0);
1327       }
1328
1329       SMDS_NodeIteratorPtr itn = subMeshDS->GetNodes();
1330       while (itn->more()) {
1331         const SMDS_MeshNode * node = itn->next();
1332         //MESSAGE( " RM node: "<<node->GetID());
1333         if ( node->NbInverseElements() == 0 )
1334           meshDS->RemoveFreeNode(node, 0);
1335         else // for StdMeshers_CompositeSegment_1D: node in one submesh, edge in another
1336           meshDS->RemoveNode(node);
1337       }
1338       subMeshDS->Clear();
1339     }
1340   }
1341 }
1342
1343 //=============================================================================
1344 /*!
1345  *
1346  */
1347 //=============================================================================
1348
1349 bool SMESH_subMesh::ComputeStateEngine(int event)
1350 {
1351   switch ( event ) {
1352   case MODIF_ALGO_STATE:
1353   case COMPUTE:
1354   case COMPUTE_SUBMESH:
1355     //case COMPUTE_CANCELED:
1356   case CLEAN:
1357     //case SUBMESH_COMPUTED:
1358     //case SUBMESH_RESTORED:
1359     //case SUBMESH_LOADED:
1360     //case MESH_ENTITY_REMOVED:
1361     //case CHECK_COMPUTE_STATE:
1362     _computeError.reset(); break;
1363   default:;
1364   }
1365
1366   if ( event == CLEAN )
1367     _alwaysComputed = false; // Unset 'true' set by MergeNodes() (issue 0022182)
1368
1369   if (_subShape.ShapeType() == TopAbs_VERTEX)
1370   {
1371     _computeState = READY_TO_COMPUTE;
1372     SMESHDS_SubMesh* smDS = GetSubMeshDS();
1373     if ( smDS && smDS->NbNodes() )
1374     {
1375       if ( event == CLEAN ) {
1376         cleanDependants();
1377         cleanSubMesh( this );
1378       }
1379       else
1380         _computeState = COMPUTE_OK;
1381     }
1382     else if (( event == COMPUTE || event == COMPUTE_SUBMESH )
1383              && !_alwaysComputed )
1384     {
1385       const TopoDS_Vertex & V = TopoDS::Vertex( _subShape );
1386       gp_Pnt P = BRep_Tool::Pnt(V);
1387       if ( SMDS_MeshNode * n = _father->GetMeshDS()->AddNode(P.X(), P.Y(), P.Z()) ) {
1388         _father->GetMeshDS()->SetNodeOnVertex(n,_Id);
1389         _computeState = COMPUTE_OK;
1390       }
1391     }
1392     if ( event == MODIF_ALGO_STATE )
1393       cleanDependants();
1394     return true;
1395   }
1396   SMESH_Gen *gen = _father->GetGen();
1397   SMESH_Algo *algo = 0;
1398   bool ret = true;
1399   SMESH_Hypothesis::Hypothesis_Status hyp_status;
1400   //algo_state oldAlgoState = (algo_state) GetAlgoState();
1401
1402   switch (_computeState)
1403   {
1404
1405     // ----------------------------------------------------------------------
1406
1407   case NOT_READY:
1408     switch (event)
1409     {
1410     case MODIF_ALGO_STATE:
1411       algo = GetAlgo();
1412       if (algo && !algo->NeedDiscreteBoundary())
1413         cleanDependsOn( algo ); // clean sub-meshes with event CLEAN
1414       if ( _algoState == HYP_OK )
1415         _computeState = READY_TO_COMPUTE;
1416       break;
1417     case COMPUTE:               // nothing to do
1418     case COMPUTE_SUBMESH:
1419       break;
1420     case COMPUTE_CANCELED:      // nothing to do
1421       break;
1422     case CLEAN:
1423       cleanDependants();
1424       removeSubMeshElementsAndNodes();
1425       break;
1426     case SUBMESH_COMPUTED:      // nothing to do
1427       break;
1428     case SUBMESH_RESTORED:
1429       ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1430       break;
1431     case MESH_ENTITY_REMOVED:
1432       break;
1433     case SUBMESH_LOADED:
1434       loadDependentMeshes();
1435       ComputeSubMeshStateEngine( SUBMESH_LOADED );
1436       //break;
1437     case CHECK_COMPUTE_STATE:
1438       if ( IsMeshComputed() )
1439         _computeState = COMPUTE_OK;
1440       break;
1441     default:
1442       ASSERT(0);
1443       break;
1444     }
1445     break;
1446
1447     // ----------------------------------------------------------------------
1448
1449   case READY_TO_COMPUTE:
1450     switch (event)
1451     {
1452     case MODIF_ALGO_STATE:
1453       _computeState = NOT_READY;
1454       algo = GetAlgo();
1455       if (algo)
1456       {
1457         if (!algo->NeedDiscreteBoundary())
1458           cleanDependsOn( algo ); // clean sub-meshes with event CLEAN
1459         if ( _algoState == HYP_OK )
1460           _computeState = READY_TO_COMPUTE;
1461       }
1462       break;
1463     case COMPUTE:
1464     case COMPUTE_SUBMESH:
1465       {
1466         algo = GetAlgo();
1467         ASSERT(algo);
1468         ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1469         if (!ret)
1470         {
1471           MESSAGE("***** verify compute state *****");
1472           _computeState = NOT_READY;
1473           setAlgoState(MISSING_HYP);
1474           break;
1475         }
1476         TopoDS_Shape shape = _subShape;
1477         algo->SubMeshesToCompute().assign( 1, this );
1478         // check submeshes needed
1479         if (_father->HasShapeToMesh() ) {
1480           bool subComputed = false, subFailed = false;
1481           if (!algo->OnlyUnaryInput()) {
1482             if ( event == COMPUTE /*&&
1483                  ( algo->NeedDiscreteBoundary() || algo->SupportSubmeshes() )*/)
1484               shape = getCollection( gen, algo, subComputed, subFailed, algo->SubMeshesToCompute());
1485             else
1486               subComputed = SubMeshesComputed( & subFailed );
1487           }
1488           else {
1489             subComputed = SubMeshesComputed();
1490           }
1491           ret = ( algo->NeedDiscreteBoundary() ? subComputed :
1492                   algo->SupportSubmeshes() ? !subFailed :
1493                   ( !subComputed || _father->IsNotConformAllowed() ));
1494           if (!ret)
1495           {
1496             _computeState = FAILED_TO_COMPUTE;
1497             if ( !algo->NeedDiscreteBoundary() && !subFailed )
1498               _computeError =
1499                 SMESH_ComputeError::New(COMPERR_BAD_INPUT_MESH,
1500                                         "Unexpected computed sub-mesh",algo);
1501             break; // goto exit
1502           }
1503         }
1504         // Compute
1505
1506         // to restore cout that may be redirected by algo
1507         std::streambuf* coutBuffer = std::cout.rdbuf();
1508
1509         //cleanDependants(); for "UseExisting_*D" algos
1510         //removeSubMeshElementsAndNodes();
1511         loadDependentMeshes();
1512         ret = false;
1513         _computeState = FAILED_TO_COMPUTE;
1514         _computeError = SMESH_ComputeError::New(COMPERR_OK,"",algo);
1515         try {
1516           OCC_CATCH_SIGNALS;
1517
1518           algo->InitComputeError();
1519
1520           MemoryReserve aMemoryReserve;
1521           SMDS_Mesh::CheckMemory();
1522           Kernel_Utils::Localizer loc;
1523           if ( !_father->HasShapeToMesh() ) // no shape
1524           {
1525             SMESH_MesherHelper helper( *_father );
1526             helper.SetSubShape( shape );
1527             helper.SetElementsOnShape( true );
1528             ret = algo->Compute(*_father, &helper );
1529           }
1530           else
1531           {
1532             ret = algo->Compute((*_father), shape);
1533           }
1534           // algo can set _computeError of submesh
1535           _computeError = SMESH_ComputeError::Worst( _computeError, algo->GetComputeError() );
1536         }
1537         catch ( ::SMESH_ComputeError& comperr ) {
1538           cout << " SMESH_ComputeError caught" << endl;
1539           if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1540           *_computeError = comperr;
1541         }
1542         catch ( std::bad_alloc& exc ) {
1543           MESSAGE("std::bad_alloc thrown inside algo->Compute()");
1544           if ( _computeError ) {
1545             _computeError->myName = COMPERR_MEMORY_PB;
1546             //_computeError->myComment = exc.what();
1547           }
1548           cleanSubMesh( this );
1549           throw exc;
1550         }
1551         catch ( Standard_OutOfMemory& exc ) {
1552           MESSAGE("Standard_OutOfMemory thrown inside algo->Compute()");
1553           if ( _computeError ) {
1554             _computeError->myName = COMPERR_MEMORY_PB;
1555             //_computeError->myComment = exc.what();
1556           }
1557           cleanSubMesh( this );
1558           throw std::bad_alloc();
1559         }
1560         catch (Standard_Failure& ex) {
1561           if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1562           _computeError->myName    = COMPERR_OCC_EXCEPTION;
1563           _computeError->myComment += ex.DynamicType()->Name();
1564           if ( ex.GetMessageString() && strlen( ex.GetMessageString() )) {
1565             _computeError->myComment += ": ";
1566             _computeError->myComment += ex.GetMessageString();
1567           }
1568         }
1569         catch ( SALOME_Exception& S_ex ) {
1570           const int skipSalomeShift = 7; /* to skip "Salome " of
1571                                             "Salome Exception" prefix returned
1572                                             by SALOME_Exception::what() */
1573           if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1574           _computeError->myName    = COMPERR_SLM_EXCEPTION;
1575           _computeError->myComment = S_ex.what() + skipSalomeShift;
1576         }
1577         catch ( std::exception& exc ) {
1578           if ( !_computeError ) _computeError = SMESH_ComputeError::New();
1579           _computeError->myName    = COMPERR_STD_EXCEPTION;
1580           _computeError->myComment = exc.what();
1581         }
1582         catch ( ... ) {
1583           if ( _computeError )
1584             _computeError->myName = COMPERR_EXCEPTION;
1585           else
1586             ret = false;
1587         }
1588         std::cout.rdbuf( coutBuffer ); // restore cout that could be redirected by algo
1589
1590         // check if an error reported on any sub-shape
1591         bool isComputeErrorSet = !checkComputeError( algo, ret, shape );
1592         if ( isComputeErrorSet )
1593           ret = false;
1594         // check if anything was built
1595         TopExp_Explorer subS(shape, _subShape.ShapeType());
1596         if (ret)
1597         {
1598           for (; ret && subS.More(); subS.Next())
1599             if ( !_father->GetSubMesh( subS.Current() )->IsMeshComputed() &&
1600                  ( _subShape.ShapeType() != TopAbs_EDGE ||
1601                    !algo->isDegenerated( TopoDS::Edge( subS.Current() ))))
1602               ret = false;
1603         }
1604         // Set _computeError
1605         if (!ret && !isComputeErrorSet)
1606         {
1607           for (subS.ReInit(); subS.More(); subS.Next())
1608           {
1609             SMESH_subMesh* sm = _father->GetSubMesh( subS.Current() );
1610             if ( !sm->IsMeshComputed() )
1611             {
1612               if ( !sm->_computeError )
1613                 sm->_computeError = SMESH_ComputeError::New();
1614               if ( sm->_computeError->IsOK() )
1615                 sm->_computeError->myName = COMPERR_ALGO_FAILED;
1616               sm->_computeState = FAILED_TO_COMPUTE;
1617               sm->_computeError->myAlgo = algo;
1618             }
1619           }
1620         }
1621         if (ret && _computeError && _computeError->myName != COMPERR_WARNING )
1622         {
1623           _computeError.reset();
1624         }
1625
1626         // send event SUBMESH_COMPUTED
1627         if ( ret ) {
1628           if ( !algo->NeedDiscreteBoundary() )
1629             // send SUBMESH_COMPUTED to dependants of all sub-meshes of shape
1630             for (subS.ReInit(); subS.More(); subS.Next())
1631             {
1632               SMESH_subMesh* sm = _father->GetSubMesh( subS.Current() );
1633               SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(false,false);
1634               while ( smIt->more() ) {
1635                 sm = smIt->next();
1636                 if ( sm->GetSubShape().ShapeType() == TopAbs_VERTEX )
1637                   sm->updateDependantsState( SUBMESH_COMPUTED );
1638                 else
1639                   break;
1640               }
1641             }
1642           else
1643             updateDependantsState( SUBMESH_COMPUTED );
1644         }
1645       }
1646       break;
1647     case COMPUTE_CANCELED:               // nothing to do
1648       break;
1649     case CLEAN:
1650       cleanDependants();
1651       removeSubMeshElementsAndNodes();
1652       _computeState = NOT_READY;
1653       algo = GetAlgo();
1654       if (algo)
1655       {
1656         ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1657         if (ret)
1658           _computeState = READY_TO_COMPUTE;
1659         else
1660           setAlgoState(MISSING_HYP);
1661       }
1662       break;
1663     case SUBMESH_COMPUTED:      // nothing to do
1664       break;
1665     case SUBMESH_RESTORED:
1666       // check if a mesh is already computed that may
1667       // happen after retrieval from a file
1668       ComputeStateEngine( CHECK_COMPUTE_STATE );
1669       ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1670       algo = GetAlgo();
1671       if (algo) algo->SubmeshRestored( this );
1672       break;
1673     case MESH_ENTITY_REMOVED:
1674       break;
1675     case SUBMESH_LOADED:
1676       loadDependentMeshes();
1677       ComputeSubMeshStateEngine( SUBMESH_LOADED );
1678       //break;
1679     case CHECK_COMPUTE_STATE:
1680       if ( IsMeshComputed() )
1681         _computeState = COMPUTE_OK;
1682       else if ( _computeError && _computeError->IsKO() )
1683         _computeState = FAILED_TO_COMPUTE;
1684       break;
1685     default:
1686       ASSERT(0);
1687       break;
1688     }
1689     break;
1690
1691     // ----------------------------------------------------------------------
1692
1693   case COMPUTE_OK:
1694     switch (event)
1695     {
1696     case MODIF_ALGO_STATE:
1697       ComputeStateEngine( CLEAN );
1698       algo = GetAlgo();
1699       if (algo && !algo->NeedDiscreteBoundary())
1700         cleanDependsOn( algo ); // clean sub-meshes with event CLEAN
1701       break;
1702     case COMPUTE:               // nothing to do
1703       break;
1704     case COMPUTE_CANCELED:      // nothing to do
1705       break;
1706     case CLEAN:
1707       cleanDependants();  // clean sub-meshes, dependant on this one, with event CLEAN
1708       removeSubMeshElementsAndNodes();
1709       _computeState = NOT_READY;
1710       if ( _algoState == HYP_OK )
1711         _computeState = READY_TO_COMPUTE;
1712       break;
1713     case SUBMESH_COMPUTED:      // nothing to do
1714       break;
1715     case SUBMESH_RESTORED:
1716       ComputeStateEngine( CHECK_COMPUTE_STATE );
1717       ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1718       algo = GetAlgo();
1719       if (algo) algo->SubmeshRestored( this );
1720       break;
1721     case MESH_ENTITY_REMOVED:
1722       updateDependantsState    ( CHECK_COMPUTE_STATE );
1723       ComputeStateEngine       ( CHECK_COMPUTE_STATE );
1724       ComputeSubMeshStateEngine( CHECK_COMPUTE_STATE );
1725       break;
1726     case CHECK_COMPUTE_STATE:
1727       if ( !IsMeshComputed() ) {
1728         if (_algoState == HYP_OK)
1729           _computeState = READY_TO_COMPUTE;
1730         else
1731           _computeState = NOT_READY;
1732       }
1733       break;
1734     case SUBMESH_LOADED:
1735       // already treated event, thanks to which _computeState == COMPUTE_OK
1736       break;
1737     default:
1738       ASSERT(0);
1739       break;
1740     }
1741     break;
1742
1743     // ----------------------------------------------------------------------
1744
1745   case FAILED_TO_COMPUTE:
1746     switch (event)
1747     {
1748     case MODIF_ALGO_STATE:
1749       if ( !IsEmpty() )
1750         ComputeStateEngine( CLEAN );
1751       algo = GetAlgo();
1752       if (algo && !algo->NeedDiscreteBoundary())
1753         cleanDependsOn( algo ); // clean sub-meshes with event CLEAN
1754       if (_algoState == HYP_OK)
1755         _computeState = READY_TO_COMPUTE;
1756       else
1757         _computeState = NOT_READY;
1758       break;
1759     case COMPUTE:        // nothing to do
1760     case COMPUTE_SUBMESH:
1761       break;
1762     case COMPUTE_CANCELED:
1763       {
1764         algo = GetAlgo();
1765         algo->CancelCompute();
1766       }
1767       break;
1768     case CLEAN:
1769       cleanDependants(); // submeshes dependent on me should be cleaned as well
1770       removeSubMeshElementsAndNodes();
1771       break;
1772     case SUBMESH_COMPUTED:      // allow retry compute
1773       if ( IsEmpty() ) // 23061
1774       {
1775         if (_algoState == HYP_OK)
1776           _computeState = READY_TO_COMPUTE;
1777         else
1778           _computeState = NOT_READY;
1779       }
1780       break;
1781     case SUBMESH_RESTORED:
1782       ComputeSubMeshStateEngine( SUBMESH_RESTORED );
1783       break;
1784     case MESH_ENTITY_REMOVED:
1785       break;
1786     case CHECK_COMPUTE_STATE:
1787       if ( IsMeshComputed() )
1788         _computeState = COMPUTE_OK;
1789       else
1790         if (_algoState == HYP_OK)
1791           _computeState = READY_TO_COMPUTE;
1792         else
1793           _computeState = NOT_READY;
1794       break;
1795     // case SUBMESH_LOADED:
1796     //   break;
1797     default:
1798       ASSERT(0);
1799       break;
1800     }
1801     break;
1802
1803     // ----------------------------------------------------------------------
1804   default:
1805     ASSERT(0);
1806     break;
1807   }
1808
1809   notifyListenersOnEvent( event, COMPUTE_EVENT );
1810
1811   return ret;
1812 }
1813
1814
1815 //=============================================================================
1816 /*!
1817  *
1818  */
1819 //=============================================================================
1820
1821 bool SMESH_subMesh::Evaluate(MapShapeNbElems& aResMap)
1822 {
1823   _computeError.reset();
1824
1825   bool ret = true;
1826
1827   if (_subShape.ShapeType() == TopAbs_VERTEX) {
1828     vector<int> aVec(SMDSEntity_Last,0);
1829     aVec[SMDSEntity_Node] = 1;
1830     aResMap.insert(make_pair(this,aVec));
1831     return ret;
1832   }
1833
1834   //SMESH_Gen *gen = _father->GetGen();
1835   SMESH_Algo *algo = 0;
1836   SMESH_Hypothesis::Hypothesis_Status hyp_status;
1837
1838   algo = GetAlgo();
1839   if( algo && !aResMap.count( this ))
1840   {
1841     ret = algo->CheckHypothesis((*_father), _subShape, hyp_status);
1842     if (!ret) return false;
1843
1844     if (_father->HasShapeToMesh() && algo->NeedDiscreteBoundary() )
1845     {
1846       // check submeshes needed
1847       bool subMeshEvaluated = true;
1848       int dimToCheck = SMESH_Gen::GetShapeDim( _subShape ) - 1;
1849       SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,/*complexShapeFirst=*/true);
1850       while ( smIt->more() && subMeshEvaluated )
1851       {
1852         SMESH_subMesh* sm = smIt->next();
1853         int dim = SMESH_Gen::GetShapeDim( sm->GetSubShape() );
1854         if (dim < dimToCheck) break; // the rest subMeshes are all of less dimension
1855         const vector<int> & nbs = aResMap[ sm ];
1856         subMeshEvaluated = (std::accumulate( nbs.begin(), nbs.end(), 0 ) > 0 );
1857       }
1858       if ( !subMeshEvaluated )
1859         return false;
1860     }
1861     _computeError = SMESH_ComputeError::New(COMPERR_OK,"",algo);
1862
1863     if ( IsMeshComputed() )
1864     {
1865       vector<int> & nbEntities = aResMap[ this ];
1866       nbEntities.resize( SMDSEntity_Last, 0 );
1867       if ( SMESHDS_SubMesh* sm = GetSubMeshDS() )
1868       {
1869         nbEntities[ SMDSEntity_Node ] = sm->NbNodes();
1870         SMDS_ElemIteratorPtr   elemIt = sm->GetElements();
1871         while ( elemIt->more() )
1872           nbEntities[ elemIt->next()->GetEntityType() ]++;
1873       }
1874     }
1875     else
1876     {
1877       ret = algo->Evaluate((*_father), _subShape, aResMap);
1878     }
1879     aResMap.insert( make_pair( this,vector<int>(0)));
1880   }
1881
1882   return ret;
1883 }
1884
1885
1886 //=======================================================================
1887 /*!
1888  * \brief Update compute_state by _computeError and send proper events to
1889  * dependent submeshes
1890   * \retval bool - true if _computeError is NOT set
1891  */
1892 //=======================================================================
1893
1894 bool SMESH_subMesh::checkComputeError(SMESH_Algo*         theAlgo,
1895                                       const bool          theComputeOK,
1896                                       const TopoDS_Shape& theShape)
1897 {
1898   bool noErrors = true;
1899
1900   if ( !theShape.IsNull() )
1901   {
1902     // Check state of submeshes
1903     if ( !theAlgo->NeedDiscreteBoundary())
1904     {
1905       SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1906       while ( smIt->more() )
1907         if ( !smIt->next()->checkComputeError( theAlgo, theComputeOK ))
1908           noErrors = false;
1909     }
1910
1911     // Check state of neighbours
1912     if ( !theAlgo->OnlyUnaryInput() &&
1913          theShape.ShapeType() == TopAbs_COMPOUND &&
1914          !theShape.IsSame( _subShape ))
1915     {
1916       for (TopoDS_Iterator subIt( theShape ); subIt.More(); subIt.Next()) {
1917         SMESH_subMesh* sm = _father->GetSubMesh( subIt.Value() );
1918         if ( sm != this ) {
1919           if ( !sm->checkComputeError( theAlgo, theComputeOK, sm->GetSubShape() ))
1920             noErrors = false;
1921           updateDependantsState( SUBMESH_COMPUTED ); // send event SUBMESH_COMPUTED
1922         }
1923       }
1924     }
1925   }
1926   {
1927
1928     // Set my _computeState
1929
1930     if ( !_computeError || _computeError->IsOK() )
1931     {
1932       // no error description is set to this sub-mesh, check if any mesh is computed
1933       _computeState = IsMeshComputed() ? COMPUTE_OK : FAILED_TO_COMPUTE;
1934       if ( _computeState != COMPUTE_OK )
1935       {
1936         if ( _subShape.ShapeType() == TopAbs_EDGE &&
1937              SMESH_Algo::isDegenerated( TopoDS::Edge( _subShape )) )
1938           _computeState = COMPUTE_OK;
1939         else if ( theComputeOK )
1940           _computeError = SMESH_ComputeError::New(COMPERR_NO_MESH_ON_SHAPE,"",theAlgo);
1941       }
1942     }
1943
1944     if ( _computeError && !_computeError->IsOK() )
1945     {
1946       if ( !_computeError->myAlgo )
1947         _computeError->myAlgo = theAlgo;
1948
1949       // Show error
1950       SMESH_Comment text;
1951       text << theAlgo->GetName() << " failed on sub-shape #" << _Id << " with error ";
1952       if (_computeError->IsCommon() )
1953         text << _computeError->CommonName();
1954       else
1955         text << _computeError->myName;
1956       if ( _computeError->myComment.size() > 0 )
1957         text << " \"" << _computeError->myComment << "\"";
1958
1959       INFOS( text );
1960
1961       _computeState = _computeError->IsKO() ? FAILED_TO_COMPUTE : COMPUTE_OK;
1962
1963       noErrors = false;
1964     }
1965   }
1966   return noErrors;
1967 }
1968
1969 //=======================================================================
1970 //function : updateSubMeshState
1971 //purpose  :
1972 //=======================================================================
1973
1974 void SMESH_subMesh::updateSubMeshState(const compute_state theState)
1975 {
1976   SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(false,false);
1977   while ( smIt->more() )
1978     smIt->next()->_computeState = theState;
1979 }
1980
1981 //=======================================================================
1982 //function : ComputeSubMeshStateEngine
1983 //purpose  :
1984 //=======================================================================
1985
1986 void SMESH_subMesh::ComputeSubMeshStateEngine(int event, const bool includeSelf)
1987 {
1988   SMESH_subMeshIteratorPtr smIt = getDependsOnIterator(includeSelf,false);
1989   while ( smIt->more() )
1990     smIt->next()->ComputeStateEngine(event);
1991 }
1992
1993 //=======================================================================
1994 //function : updateDependantsState
1995 //purpose  :
1996 //=======================================================================
1997
1998 void SMESH_subMesh::updateDependantsState(const compute_event theEvent)
1999 {
2000   const std::vector< SMESH_subMesh * > & ancestors = GetAncestors();
2001   for ( size_t iA = 0; iA < ancestors.size(); ++iA )
2002   {
2003     ancestors[ iA ]->ComputeStateEngine( theEvent );
2004   }
2005 }
2006
2007 //=======================================================================
2008 //function : cleanDependants
2009 //purpose  : 
2010 //=======================================================================
2011
2012 void SMESH_subMesh::cleanDependants()
2013 {
2014   int dimToClean = SMESH_Gen::GetShapeDim( _subShape ) + 1;
2015
2016   const std::vector< SMESH_subMesh * > & ancestors = GetAncestors();
2017   for ( size_t iA = 0; iA < ancestors.size(); ++iA )
2018   {
2019     const TopoDS_Shape& ancestor = ancestors[ iA ]->GetSubShape();
2020     if ( SMESH_Gen::GetShapeDim( ancestor ) == dimToClean )
2021     {
2022       // PAL8021. do not go upper than SOLID, else ComputeStateEngine(CLEAN)
2023       // will erase mesh on other shapes in a compound
2024       if ( ancestor.ShapeType() >= TopAbs_SOLID &&
2025            !ancestors[ iA ]->IsEmpty() )  // prevent infinite CLEAN via event lesteners
2026         ancestors[ iA ]->ComputeStateEngine(CLEAN);
2027     }
2028   }
2029 }
2030
2031 //=======================================================================
2032 //function : removeSubMeshElementsAndNodes
2033 //purpose  : 
2034 //=======================================================================
2035
2036 void SMESH_subMesh::removeSubMeshElementsAndNodes()
2037 {
2038   cleanSubMesh( this );
2039
2040   // algo may bind a submesh not to _subShape, eg 3D algo
2041   // sets nodes on SHELL while _subShape may be SOLID
2042
2043   int dim = SMESH_Gen::GetShapeDim( _subShape );
2044   int type = _subShape.ShapeType() + 1;
2045   for ( ; type <= TopAbs_EDGE; type++) {
2046     if ( dim == SMESH_Gen::GetShapeDim( (TopAbs_ShapeEnum) type ))
2047     {
2048       TopExp_Explorer exp( _subShape, (TopAbs_ShapeEnum) type );
2049       for ( ; exp.More(); exp.Next() )
2050         cleanSubMesh( _father->GetSubMeshContaining( exp.Current() ));
2051     }
2052     else
2053       break;
2054   }
2055 }
2056
2057 //=======================================================================
2058 //function : getCollection
2059 //purpose  : return a shape containing all sub-shapes of the MainShape that can be
2060 //           meshed at once along with _subShape
2061 //=======================================================================
2062
2063 TopoDS_Shape SMESH_subMesh::getCollection(SMESH_Gen * theGen,
2064                                           SMESH_Algo* theAlgo,
2065                                           bool &      theSubComputed,
2066                                           bool &      theSubFailed,
2067                                           std::vector<SMESH_subMesh*>& theSubs)
2068 {
2069   theSubComputed = SubMeshesComputed( & theSubFailed );
2070
2071   TopoDS_Shape mainShape = _father->GetMeshDS()->ShapeToMesh();
2072
2073   if ( mainShape.IsSame( _subShape ))
2074     return _subShape;
2075
2076   const bool skipAuxHyps = false;
2077   list<const SMESHDS_Hypothesis*> aUsedHyp =
2078     theAlgo->GetUsedHypothesis( *_father, _subShape, skipAuxHyps ); // copy
2079
2080   // put in a compound all shapes with the same hypothesis assigned
2081   // and a good ComputeState
2082
2083   TopoDS_Compound aCompound;
2084   BRep_Builder aBuilder;
2085   aBuilder.MakeCompound( aCompound );
2086
2087   theSubs.clear();
2088
2089   SMESH_subMeshIteratorPtr smIt = _father->GetSubMesh( mainShape )->getDependsOnIterator(false);
2090   while ( smIt->more() )
2091   {
2092     SMESH_subMesh* subMesh = smIt->next();
2093     const TopoDS_Shape&  S = subMesh->_subShape;
2094     if ( S.ShapeType() != this->_subShape.ShapeType() )
2095       continue;
2096     theSubs.push_back( subMesh );
2097     if ( subMesh == this )
2098     {
2099       aBuilder.Add( aCompound, S );
2100     }
2101     else if ( subMesh->GetComputeState() == READY_TO_COMPUTE )
2102     {
2103       SMESH_Algo* anAlgo = subMesh->GetAlgo();
2104       if (( anAlgo->IsSameName( *theAlgo )) && // same algo
2105           ( anAlgo->GetUsedHypothesis( *_father, S, skipAuxHyps ) == aUsedHyp )) // same hyps
2106       {
2107         aBuilder.Add( aCompound, S );
2108         if ( !subMesh->SubMeshesComputed() )
2109           theSubComputed = false;
2110       }
2111     }
2112   }
2113
2114   return aCompound;
2115 }
2116
2117 //=======================================================================
2118 //function : getSimilarAttached
2119 //purpose  : return a hypothesis attached to theShape.
2120 //           If theHyp is provided, similar but not same hypotheses
2121 //           is returned; else only applicable ones having theHypType
2122 //           is returned
2123 //=======================================================================
2124
2125 const SMESH_Hypothesis* SMESH_subMesh::getSimilarAttached(const TopoDS_Shape&      theShape,
2126                                                           const SMESH_Hypothesis * theHyp,
2127                                                           const int                theHypType)
2128 {
2129   SMESH_HypoFilter hypoKind;
2130   hypoKind.Init( hypoKind.HasType( theHyp ? theHyp->GetType() : theHypType ));
2131   if ( theHyp ) {
2132     hypoKind.And   ( hypoKind.HasDim( theHyp->GetDim() ));
2133     hypoKind.AndNot( hypoKind.Is( theHyp ));
2134     if ( theHyp->IsAuxiliary() )
2135       hypoKind.And( hypoKind.HasName( theHyp->GetName() ));
2136     else
2137       hypoKind.AndNot( hypoKind.IsAuxiliary());
2138   }
2139   else {
2140     hypoKind.And( hypoKind.IsApplicableTo( theShape ));
2141   }
2142
2143   return _father->GetHypothesis( theShape, hypoKind, false );
2144 }
2145
2146 //=======================================================================
2147 //function : CheckConcurentHypothesis
2148 //purpose  : check if there are several applicable hypothesis attached to
2149 //           ancestors
2150 //=======================================================================
2151
2152 SMESH_Hypothesis::Hypothesis_Status
2153   SMESH_subMesh::CheckConcurentHypothesis (const int theHypType)
2154 {
2155   MESSAGE ("SMESH_subMesh::CheckConcurentHypothesis");
2156
2157   // is there local hypothesis on me?
2158   if ( getSimilarAttached( _subShape, 0, theHypType ) )
2159     return SMESH_Hypothesis::HYP_OK;
2160
2161
2162   TopoDS_Shape aPrevWithHyp;
2163   const SMESH_Hypothesis* aPrevHyp = 0;
2164   TopTools_ListIteratorOfListOfShape it( _father->GetAncestors( _subShape ));
2165   for (; it.More(); it.Next())
2166   {
2167     const TopoDS_Shape& ancestor = it.Value();
2168     const SMESH_Hypothesis* hyp = getSimilarAttached( ancestor, 0, theHypType );
2169     if ( hyp )
2170     {
2171       if ( aPrevWithHyp.IsNull() || aPrevWithHyp.IsSame( ancestor ))
2172       {
2173         aPrevWithHyp = ancestor;
2174         aPrevHyp     = hyp;
2175       }
2176       else if ( aPrevWithHyp.ShapeType() == ancestor.ShapeType() && aPrevHyp != hyp )
2177         return SMESH_Hypothesis::HYP_CONCURENT;
2178       else
2179         return SMESH_Hypothesis::HYP_OK;
2180     }
2181   }
2182   return SMESH_Hypothesis::HYP_OK;
2183 }
2184
2185 //================================================================================
2186 /*!
2187  * \brief Constructor of OwnListenerData
2188  */
2189 //================================================================================
2190
2191 SMESH_subMesh::OwnListenerData::OwnListenerData( SMESH_subMesh* sm, EventListener* el):
2192   mySubMesh( sm ),
2193   myMeshID( sm ? sm->GetFather()->GetId() : -1 ),
2194   mySubMeshID( sm ? sm->GetId() : -1 ),
2195   myListener( el )
2196 {
2197 }
2198
2199 //================================================================================
2200 /*!
2201  * \brief Sets an event listener and its data to a submesh
2202  * \param listener - the listener to store
2203  * \param data - the listener data to store
2204  * \param where - the submesh to store the listener and it's data
2205  * 
2206  * It remembers the submesh where it puts the listener in order to delete
2207  * them when HYP_OK algo_state is lost
2208  * After being set, event listener is notified on each event of where submesh.
2209  */
2210 //================================================================================
2211
2212 void SMESH_subMesh::SetEventListener(EventListener*     listener,
2213                                      EventListenerData* data,
2214                                      SMESH_subMesh*     where)
2215 {
2216   if ( listener && where ) {
2217     where->setEventListener( listener, data );
2218     _ownListeners.push_back( OwnListenerData( where, listener ));
2219   }
2220 }
2221
2222 //================================================================================
2223 /*!
2224  * \brief Sets an event listener and its data to a submesh
2225  * \param listener - the listener to store
2226  * \param data - the listener data to store
2227  * 
2228  * After being set, event listener is notified on each event of a submesh.
2229  */
2230 //================================================================================
2231
2232 void SMESH_subMesh::setEventListener(EventListener*     listener,
2233                                      EventListenerData* data)
2234 {
2235   map< EventListener*, EventListenerData* >::iterator l_d =
2236     _eventListeners.find( listener );
2237   if ( l_d != _eventListeners.end() ) {
2238     EventListenerData* curData = l_d->second;
2239     if ( curData && curData != data && curData->IsDeletable() )
2240       delete curData;
2241     l_d->second = data;
2242   }
2243   else
2244   {
2245     for ( l_d = _eventListeners.begin(); l_d != _eventListeners.end(); ++l_d )
2246       if ( listener->GetName() == l_d->first->GetName() )
2247       {
2248         EventListenerData* curData = l_d->second;
2249         if ( curData && curData != data && curData->IsDeletable() )
2250           delete curData;
2251         if ( l_d->first != listener && l_d->first->IsDeletable() )
2252           delete l_d->first;
2253         _eventListeners.erase( l_d );
2254         break;
2255       }
2256     _eventListeners.insert( make_pair( listener, data ));
2257   }
2258 }
2259
2260 //================================================================================
2261 /*!
2262  * \brief Return an event listener data
2263  * \param listener - the listener whose data is
2264  * \param myOwn - if \c true, returns a listener set by this sub-mesh,
2265  *        else returns a listener listening to events of this sub-mesh
2266  * \retval EventListenerData* - found data, maybe NULL
2267  */
2268 //================================================================================
2269
2270 EventListenerData* SMESH_subMesh::GetEventListenerData(EventListener* listener,
2271                                                        const bool     myOwn) const
2272 {
2273   if ( myOwn )
2274   {
2275     list< OwnListenerData >::const_iterator d;
2276     for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
2277     {
2278       if ( d->myListener == listener && _father->MeshExists( d->myMeshID ))
2279         return d->mySubMesh->GetEventListenerData( listener, !myOwn );
2280     }
2281   }
2282   else
2283   {
2284     map< EventListener*, EventListenerData* >::const_iterator l_d =
2285       _eventListeners.find( listener );
2286     if ( l_d != _eventListeners.end() )
2287       return l_d->second;
2288   }
2289   return 0;
2290 }
2291
2292 //================================================================================
2293 /*!
2294  * \brief Return an event listener data
2295  * \param listenerName - the listener name
2296  * \param myOwn - if \c true, returns a listener set by this sub-mesh,
2297  *        else returns a listener listening to events of this sub-mesh
2298  * \retval EventListenerData* - found data, maybe NULL
2299  */
2300 //================================================================================
2301
2302 EventListenerData* SMESH_subMesh::GetEventListenerData(const string& listenerName,
2303                                                        const bool    myOwn) const
2304 {
2305   if ( myOwn )
2306   {
2307     list< OwnListenerData >::const_iterator d;
2308     for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
2309     {
2310       if ( _father->MeshExists( d->myMeshID ) && listenerName == d->myListener->GetName())
2311         return d->mySubMesh->GetEventListenerData( listenerName, !myOwn );
2312     }
2313   }
2314   else
2315   {
2316     map< EventListener*, EventListenerData* >::const_iterator l_d = _eventListeners.begin();
2317     for ( ; l_d != _eventListeners.end(); ++l_d )
2318       if ( listenerName == l_d->first->GetName() )
2319         return l_d->second;
2320   }
2321   return 0;
2322 }
2323
2324 //================================================================================
2325 /*!
2326  * \brief Notify stored event listeners on the occured event
2327  * \param event - algo_event or compute_event itself
2328  * \param eventType - algo_event or compute_event
2329  * \param hyp - hypothesis, if eventType is algo_event
2330  */
2331 //================================================================================
2332
2333 void SMESH_subMesh::notifyListenersOnEvent( const int         event,
2334                                             const event_type  eventType,
2335                                             SMESH_Hypothesis* hyp)
2336 {
2337   list< pair< EventListener*, EventListenerData* > > eventListeners( _eventListeners.begin(),
2338                                                                      _eventListeners.end());
2339   list< pair< EventListener*, EventListenerData* > >::iterator l_d = eventListeners.begin();
2340   for ( ; l_d != eventListeners.end(); ++l_d )
2341   {
2342     std::pair< EventListener*, EventListenerData* > li_da = *l_d;
2343     if ( !_eventListeners.count( li_da.first )) continue;
2344
2345     if ( li_da.first->myBusySM.insert( this ).second )
2346     {
2347       const bool isDeletable = li_da.first->IsDeletable();
2348
2349       li_da.first->ProcessEvent( event, eventType, this, li_da.second, hyp );
2350
2351       if ( !isDeletable || _eventListeners.count( li_da.first ))
2352         li_da.first->myBusySM.erase( this ); // a listener is hopefully not dead
2353     }
2354   }
2355 }
2356
2357 //================================================================================
2358 /*!
2359  * \brief Unregister the listener and delete listener's data
2360  * \param listener - the event listener
2361  */
2362 //================================================================================
2363
2364 void SMESH_subMesh::DeleteEventListener(EventListener* listener)
2365 {
2366   map< EventListener*, EventListenerData* >::iterator l_d =
2367     _eventListeners.find( listener );
2368   if ( l_d != _eventListeners.end() && l_d->first )
2369   {
2370     if ( l_d->second && l_d->second->IsDeletable() )
2371     {
2372       delete l_d->second;
2373     }
2374     l_d->first->myBusySM.erase( this );
2375     if ( l_d->first->IsDeletable() )
2376     {
2377       l_d->first->BeforeDelete( this, l_d->second );
2378       delete l_d->first;
2379     }
2380     _eventListeners.erase( l_d );
2381   }
2382 }
2383
2384 //================================================================================
2385 /*!
2386  * \brief Delete event listeners depending on algo of this submesh
2387  */
2388 //================================================================================
2389
2390 void SMESH_subMesh::deleteOwnListeners()
2391 {
2392   list< OwnListenerData >::iterator d;
2393   for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
2394   {
2395     SMESH_Mesh* mesh = _father->FindMesh( d->myMeshID );
2396     if ( !mesh || !mesh->GetSubMeshContaining( d->mySubMeshID ))
2397       continue;
2398     d->mySubMesh->DeleteEventListener( d->myListener );
2399   }
2400   _ownListeners.clear();
2401 }
2402
2403 //=======================================================================
2404 //function : loadDependentMeshes
2405 //purpose  : loads dependent meshes on SUBMESH_LOADED event
2406 //=======================================================================
2407
2408 void SMESH_subMesh::loadDependentMeshes()
2409 {
2410   list< OwnListenerData >::iterator d;
2411   for ( d = _ownListeners.begin(); d != _ownListeners.end(); ++d )
2412     if ( _father != d->mySubMesh->_father )
2413       d->mySubMesh->_father->Load();
2414
2415   // map< EventListener*, EventListenerData* >::iterator l_d = _eventListeners.begin();
2416   // for ( ; l_d != _eventListeners.end(); ++l_d )
2417   //   if ( l_d->second )
2418   //   {
2419   //     const list<SMESH_subMesh*>& smList = l_d->second->mySubMeshes;
2420   //     list<SMESH_subMesh*>::const_iterator sm = smList.begin();
2421   //     for ( ; sm != smList.end(); ++sm )
2422   //       if ( _father != (*sm)->_father )
2423   //         (*sm)->_father->Load();
2424   //   }
2425 }
2426
2427 //================================================================================
2428 /*!
2429  * \brief Do something on a certain event
2430  * \param event - algo_event or compute_event itself
2431  * \param eventType - algo_event or compute_event
2432  * \param subMesh - the submesh where the event occures
2433  * \param data - listener data stored in the subMesh
2434  * \param hyp - hypothesis, if eventType is algo_event
2435  * 
2436  * The base implementation translates CLEAN event to the subMesh
2437  * stored in listener data. Also it sends SUBMESH_COMPUTED event in case of
2438  * successful COMPUTE event.
2439  */
2440 //================================================================================
2441
2442 void SMESH_subMeshEventListener::ProcessEvent(const int          event,
2443                                               const int          eventType,
2444                                               SMESH_subMesh*     subMesh,
2445                                               EventListenerData* data,
2446                                               const SMESH_Hypothesis*  /*hyp*/)
2447 {
2448   if ( data && !data->mySubMeshes.empty() &&
2449        eventType == SMESH_subMesh::COMPUTE_EVENT)
2450   {
2451     ASSERT( data->mySubMeshes.front() != subMesh );
2452     list<SMESH_subMesh*>::iterator smIt = data->mySubMeshes.begin();
2453     list<SMESH_subMesh*>::iterator smEnd = data->mySubMeshes.end();
2454     switch ( event ) {
2455     case SMESH_subMesh::CLEAN:
2456       for ( ; smIt != smEnd; ++ smIt)
2457         (*smIt)->ComputeStateEngine( event );
2458       break;
2459     case SMESH_subMesh::COMPUTE:
2460     case SMESH_subMesh::COMPUTE_SUBMESH:
2461       if ( subMesh->GetComputeState() == SMESH_subMesh::COMPUTE_OK )
2462         for ( ; smIt != smEnd; ++ smIt)
2463           (*smIt)->ComputeStateEngine( SMESH_subMesh::SUBMESH_COMPUTED );
2464       break;
2465     default:;
2466     }
2467   }
2468 }
2469
2470 namespace {
2471
2472   //================================================================================
2473   /*!
2474    * \brief Iterator over submeshes and optionally prepended or appended one
2475    */
2476   //================================================================================
2477
2478   struct _Iterator : public SMDS_Iterator<SMESH_subMesh*>
2479   {
2480     _Iterator(SMDS_Iterator<SMESH_subMesh*>* subIt,
2481               SMESH_subMesh*                 prepend,
2482               SMESH_subMesh*                 append): myAppend(append), myIt(subIt)
2483     {
2484       myCur = prepend ? prepend : myIt->more() ? myIt->next() : append;
2485       if ( myCur == append ) append = 0;
2486     }
2487     /// Return true if and only if there are other object in this iterator
2488     virtual bool more()
2489     {
2490       return myCur;
2491     }
2492     /// Return the current object and step to the next one
2493     virtual SMESH_subMesh* next()
2494     {
2495       SMESH_subMesh* res = myCur;
2496       if ( myIt->more() ) { myCur = myIt->next(); }
2497       else                { myCur = myAppend; myAppend = 0; }
2498       return res;
2499     }
2500     /// ~
2501     ~_Iterator()
2502     { delete myIt; }
2503     ///
2504     SMESH_subMesh                 *myAppend, *myCur;
2505     SMDS_Iterator<SMESH_subMesh*> *myIt;
2506   };
2507 }
2508
2509 //================================================================================
2510 /*!
2511  * \brief  Return iterator on the submeshes this one depends on
2512   * \param includeSelf - this submesh to be returned also
2513   * \param reverse - if true, complex shape submeshes go first
2514  */
2515 //================================================================================
2516
2517 SMESH_subMeshIteratorPtr SMESH_subMesh::getDependsOnIterator(const bool includeSelf,
2518                                                              const bool reverse) const
2519 {
2520   SMESH_subMesh *me = (SMESH_subMesh*) this;
2521   SMESH_subMesh *prepend=0, *append=0;
2522   if ( includeSelf ) {
2523     if ( reverse ) prepend = me;
2524     else            append = me;
2525   }
2526   typedef map < int, SMESH_subMesh * > TMap;
2527   if ( reverse )
2528   {
2529     return SMESH_subMeshIteratorPtr
2530       ( new _Iterator( new SMDS_mapReverseIterator<TMap>( me->DependsOn() ), prepend, append ));
2531   }
2532   {
2533     return SMESH_subMeshIteratorPtr
2534       ( new _Iterator( new SMDS_mapIterator<TMap>( me->DependsOn() ), prepend, append ));
2535   }
2536 }
2537
2538 //================================================================================
2539 /*!
2540  * \brief Returns ancestor sub-meshes. Finds them if not yet found.
2541  */
2542 //================================================================================
2543
2544 const std::vector< SMESH_subMesh * > & SMESH_subMesh::GetAncestors() const
2545 {
2546   if ( _ancestors.empty() &&
2547        !_subShape.IsSame( _father->GetShapeToMesh() ))
2548   {
2549     const TopTools_ListOfShape& ancShapes = _father->GetAncestors( _subShape );
2550
2551     SMESH_subMesh* me = const_cast< SMESH_subMesh* >( this );
2552     me->_ancestors.reserve( ancShapes.Extent() );
2553
2554     TopTools_MapOfShape map;
2555    
2556     for ( TopTools_ListIteratorOfListOfShape it( ancShapes ); it.More(); it.Next() )
2557       if ( SMESH_subMesh* sm = _father->GetSubMeshContaining( it.Value() ))
2558         if ( map.Add( it.Value() ))
2559           me->_ancestors.push_back( sm );
2560   }
2561
2562   return _ancestors;
2563 }
2564
2565 //================================================================================
2566 /*!
2567  * \brief Clears the vector of ancestor sub-meshes
2568  */
2569 //================================================================================
2570
2571 void SMESH_subMesh::ClearAncestors()
2572 {
2573   _ancestors.clear();
2574 }
2575
2576 //================================================================================
2577 /*!
2578  * \brief  Find common submeshes (based on shared sub-shapes with other
2579   * \param theOther submesh to check
2580   * \param theSetOfCommon set of common submesh
2581  */
2582 //================================================================================
2583
2584 bool SMESH_subMesh::FindIntersection(const SMESH_subMesh*            theOther,
2585                                      std::set<const SMESH_subMesh*>& theSetOfCommon ) const
2586 {
2587   size_t oldNb = theSetOfCommon.size();
2588
2589   // check main submeshes
2590   const map <int, SMESH_subMesh*>::const_iterator otherEnd = theOther->_mapDepend.end();
2591   if ( theOther->_mapDepend.find(this->GetId()) != otherEnd )
2592     theSetOfCommon.insert( this );
2593   if ( _mapDepend.find(theOther->GetId()) != _mapDepend.end() )
2594     theSetOfCommon.insert( theOther );
2595
2596   // check common submeshes
2597   map <int, SMESH_subMesh*>::const_iterator mapIt = _mapDepend.begin();
2598   for( ; mapIt != _mapDepend.end(); mapIt++ )
2599     if ( theOther->_mapDepend.find((*mapIt).first) != otherEnd )
2600       theSetOfCommon.insert( (*mapIt).second );
2601   return oldNb < theSetOfCommon.size();
2602 }