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