Salome HOME
IPAL22173 TC6.2.0: "Netgen1D-2D" algorithm doesn't work on "flight_solid.brep"
[modules/smesh.git] / src / SMESH / SMESH_Algo.cxx
1 // Copyright (C) 2007-2011  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.
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_Algo.cxx
25 //  Author : Paul RASCLE, EDF
26 //  Module : SMESH
27 //
28 #include "SMESH_Algo.hxx"
29
30 #include "SMDS_EdgePosition.hxx"
31 #include "SMDS_FacePosition.hxx"
32 #include "SMDS_MeshElement.hxx"
33 #include "SMDS_MeshNode.hxx"
34 #include "SMDS_VolumeTool.hxx"
35 #include "SMESHDS_Mesh.hxx"
36 #include "SMESHDS_SubMesh.hxx"
37 #include "SMESH_Comment.hxx"
38 #include "SMESH_Gen.hxx"
39 #include "SMESH_HypoFilter.hxx"
40 #include "SMESH_Mesh.hxx"
41 #include "SMESH_TypeDefs.hxx"
42
43 #include <BRepAdaptor_Curve.hxx>
44 #include <BRepLProp.hxx>
45 #include <BRep_Tool.hxx>
46 #include <GCPnts_AbscissaPoint.hxx>
47 #include <GeomAdaptor_Curve.hxx>
48 #include <Geom_Surface.hxx>
49 #include <TopExp.hxx>
50 #include <TopLoc_Location.hxx>
51 #include <TopTools_ListIteratorOfListOfShape.hxx>
52 #include <TopTools_ListOfShape.hxx>
53 #include <TopoDS.hxx>
54 #include <TopoDS_Edge.hxx>
55 #include <TopoDS_Face.hxx>
56 #include <TopoDS_Vertex.hxx>
57 #include <gp_Pnt.hxx>
58 #include <gp_Pnt2d.hxx>
59 #include <gp_Vec.hxx>
60
61 #include <Standard_ErrorHandler.hxx>
62 #include <Standard_Failure.hxx>
63
64 #include "utilities.h"
65
66 #include <algorithm>
67 #include <limits>
68
69 using namespace std;
70
71 //=============================================================================
72 /*!
73  *  
74  */
75 //=============================================================================
76
77 SMESH_Algo::SMESH_Algo (int hypId, int studyId, SMESH_Gen * gen)
78   : SMESH_Hypothesis(hypId, studyId, gen)
79 {
80   gen->_mapAlgo[hypId] = this;
81
82   _onlyUnaryInput = _requireDescretBoundary = _requireShape = true;
83   _quadraticMesh = _supportSubmeshes = false;
84   _error = COMPERR_OK;
85 }
86
87 //=============================================================================
88 /*!
89  *  
90  */
91 //=============================================================================
92
93 SMESH_Algo::~SMESH_Algo()
94 {
95 }
96
97 //=============================================================================
98 /*!
99  * Usually an algoritm has nothing to save
100  */
101 //=============================================================================
102
103 ostream & SMESH_Algo::SaveTo(ostream & save) { return save; }
104 istream & SMESH_Algo::LoadFrom(istream & load) { return load; }
105
106 //=============================================================================
107 /*!
108  *  
109  */
110 //=============================================================================
111
112 const vector < string > &SMESH_Algo::GetCompatibleHypothesis()
113 {
114   return _compatibleHypothesis;
115 }
116
117 //=============================================================================
118 /*!
119  *  List the hypothesis used by the algorithm associated to the shape.
120  *  Hypothesis associated to father shape -are- taken into account (see
121  *  GetAppliedHypothesis). Relevant hypothesis have a name (type) listed in
122  *  the algorithm. This method could be surcharged by specific algorithms, in 
123  *  case of several hypothesis simultaneously applicable.
124  */
125 //=============================================================================
126
127 const list <const SMESHDS_Hypothesis *> &
128 SMESH_Algo::GetUsedHypothesis(SMESH_Mesh &         aMesh,
129                               const TopoDS_Shape & aShape,
130                               const bool           ignoreAuxiliary)
131 {
132   _usedHypList.clear();
133   SMESH_HypoFilter filter;
134   if ( InitCompatibleHypoFilter( filter, ignoreAuxiliary ))
135   {
136     aMesh.GetHypotheses( aShape, filter, _usedHypList, true );
137     if ( ignoreAuxiliary && _usedHypList.size() > 1 )
138       _usedHypList.clear(); //only one compatible hypothesis allowed
139   }
140   return _usedHypList;
141 }
142
143 //=============================================================================
144 /*!
145  *  List the relevant hypothesis associated to the shape. Relevant hypothesis
146  *  have a name (type) listed in the algorithm. Hypothesis associated to
147  *  father shape -are not- taken into account (see GetUsedHypothesis)
148  */
149 //=============================================================================
150
151 const list<const SMESHDS_Hypothesis *> &
152 SMESH_Algo::GetAppliedHypothesis(SMESH_Mesh &         aMesh,
153                                  const TopoDS_Shape & aShape,
154                                  const bool           ignoreAuxiliary)
155 {
156   _appliedHypList.clear();
157   SMESH_HypoFilter filter;
158   if ( InitCompatibleHypoFilter( filter, ignoreAuxiliary ))
159     aMesh.GetHypotheses( aShape, filter, _appliedHypList, false );
160
161   return _appliedHypList;
162 }
163
164 //=============================================================================
165 /*!
166  *  Compute length of an edge
167  */
168 //=============================================================================
169
170 double SMESH_Algo::EdgeLength(const TopoDS_Edge & E)
171 {
172   double UMin = 0, UMax = 0;
173   if (BRep_Tool::Degenerated(E))
174     return 0;
175   TopLoc_Location L;
176   Handle(Geom_Curve) C = BRep_Tool::Curve(E, L, UMin, UMax);
177   GeomAdaptor_Curve AdaptCurve(C);
178   double length = GCPnts_AbscissaPoint::Length(AdaptCurve, UMin, UMax);
179   return length;
180 }
181
182 //================================================================================
183 /*!
184  * \brief Calculate normal of a mesh face
185  */
186 //================================================================================
187
188 bool SMESH_Algo::FaceNormal(const SMDS_MeshElement* F, gp_XYZ& normal, bool normalized)
189 {
190   if ( !F || F->GetType() != SMDSAbs_Face )
191     return false;
192
193   normal.SetCoord(0,0,0);
194   int nbNodes = F->IsQuadratic() ? F->NbNodes()/2 : F->NbNodes();
195   for ( int i = 0; i < nbNodes-2; ++i )
196   {
197     gp_XYZ p[3];
198     for ( int n = 0; n < 3; ++n )
199     {
200       const SMDS_MeshNode* node = F->GetNode( i + n );
201       p[n].SetCoord( node->X(), node->Y(), node->Z() );
202     }
203     normal += ( p[2] - p[1] ) ^ ( p[0] - p[1] );
204   }
205   double size2 = normal.SquareModulus();
206   bool ok = ( size2 > numeric_limits<double>::min() * numeric_limits<double>::min());
207   if ( normalized && ok )
208     normal /= sqrt( size2 );
209
210   return ok;
211 }
212
213 //================================================================================
214 /*!
215  * \brief Find out elements orientation on a geometrical face
216  * \param theFace - The face correctly oriented in the shape being meshed
217  * \param theMeshDS - The mesh data structure
218  * \retval bool - true if the face normal and the normal of first element
219  *                in the correspoding submesh point in different directions
220  */
221 //================================================================================
222
223 bool SMESH_Algo::IsReversedSubMesh (const TopoDS_Face&  theFace,
224                                     SMESHDS_Mesh*       theMeshDS)
225 {
226   if ( theFace.IsNull() || !theMeshDS )
227     return false;
228
229   // find out orientation of a meshed face
230   int faceID = theMeshDS->ShapeToIndex( theFace );
231   TopoDS_Shape aMeshedFace = theMeshDS->IndexToShape( faceID );
232   bool isReversed = ( theFace.Orientation() != aMeshedFace.Orientation() );
233
234   const SMESHDS_SubMesh * aSubMeshDSFace = theMeshDS->MeshElements( faceID );
235   if ( !aSubMeshDSFace )
236     return isReversed;
237
238   // find element with node located on face and get its normal
239   const SMDS_FacePosition* facePos = 0;
240   int vertexID = 0;
241   gp_Pnt nPnt[3];
242   gp_Vec Ne;
243   bool normalOK = false;
244   SMDS_ElemIteratorPtr iteratorElem = aSubMeshDSFace->GetElements();
245   while ( iteratorElem->more() ) // loop on elements on theFace
246   {
247     const SMDS_MeshElement* elem = iteratorElem->next();
248     if ( elem && elem->NbNodes() > 2 ) {
249       SMDS_ElemIteratorPtr nodesIt = elem->nodesIterator();
250       const SMDS_FacePosition* fPos = 0;
251       int i = 0, vID = 0;
252       while ( nodesIt->more() ) { // loop on nodes
253         const SMDS_MeshNode* node
254           = static_cast<const SMDS_MeshNode *>(nodesIt->next());
255         if ( i == 3 ) i = 2;
256         nPnt[ i++ ].SetCoord( node->X(), node->Y(), node->Z() );
257         // check position
258         const SMDS_PositionPtr& pos = node->GetPosition();
259         if ( !pos ) continue;
260         if ( pos->GetTypeOfPosition() == SMDS_TOP_FACE ) {
261           fPos = dynamic_cast< const SMDS_FacePosition* >( pos );
262         }
263         else if ( pos->GetTypeOfPosition() == SMDS_TOP_VERTEX ) {
264           vID = node->getshapeId();
265         }
266       }
267       if ( fPos || ( !normalOK && vID )) {
268         // compute normal
269         gp_Vec v01( nPnt[0], nPnt[1] ), v02( nPnt[0], nPnt[2] );
270         if ( v01.SquareMagnitude() > RealSmall() &&
271              v02.SquareMagnitude() > RealSmall() )
272         {
273           Ne = v01 ^ v02;
274           normalOK = ( Ne.SquareMagnitude() > RealSmall() );
275         }
276         // we need position on theFace or at least on vertex
277         if ( normalOK ) {
278           vertexID = vID;
279           if ((facePos = fPos))
280             break;
281         }
282       }
283     }
284   }
285   if ( !normalOK )
286     return isReversed;
287
288   // node position on face
289   double u,v;
290   if ( facePos ) {
291     u = facePos->GetUParameter();
292     v = facePos->GetVParameter();
293   }
294   else if ( vertexID ) {
295     TopoDS_Shape V = theMeshDS->IndexToShape( vertexID );
296     if ( V.IsNull() || V.ShapeType() != TopAbs_VERTEX )
297       return isReversed;
298     gp_Pnt2d uv = BRep_Tool::Parameters( TopoDS::Vertex( V ), theFace );
299     u = uv.X();
300     v = uv.Y();
301   }
302   else
303   {
304     return isReversed;
305   }
306
307   // face normal at node position
308   TopLoc_Location loc;
309   Handle(Geom_Surface) surf = BRep_Tool::Surface( theFace, loc );
310   if ( surf.IsNull() || surf->Continuity() < GeomAbs_C1 ) return isReversed;
311   gp_Vec d1u, d1v;
312   surf->D1( u, v, nPnt[0], d1u, d1v );
313   gp_Vec Nf = (d1u ^ d1v).Transformed( loc );
314
315   if ( theFace.Orientation() == TopAbs_REVERSED )
316     Nf.Reverse();
317
318   return Ne * Nf < 0.;
319 }
320
321 //================================================================================
322 /*!
323  * \brief Just return false as the algorithm does not hold parameters values
324  */
325 //================================================================================
326
327 bool SMESH_Algo::SetParametersByMesh(const SMESH_Mesh* /*theMesh*/,
328                                      const TopoDS_Shape& /*theShape*/)
329 {
330   return false;
331 }
332 bool SMESH_Algo::SetParametersByDefaults(const TDefaults& , const SMESH_Mesh*)
333 {
334   return false;
335 }
336 //================================================================================
337 /*!
338  * \brief Fill vector of node parameters on geometrical edge, including vertex nodes
339  * \param theMesh - The mesh containing nodes
340  * \param theEdge - The geometrical edge of interest
341  * \param theParams - The resulting vector of sorted node parameters
342  * \retval bool - false if not all parameters are OK
343  */
344 //================================================================================
345
346 bool SMESH_Algo::GetNodeParamOnEdge(const SMESHDS_Mesh* theMesh,
347                                     const TopoDS_Edge&  theEdge,
348                                     vector< double > &  theParams)
349 {
350   theParams.clear();
351
352   if ( !theMesh || theEdge.IsNull() )
353     return false;
354
355   SMESHDS_SubMesh * eSubMesh = theMesh->MeshElements( theEdge );
356   if ( !eSubMesh || !eSubMesh->GetElements()->more() )
357     return false; // edge is not meshed
358
359   //int nbEdgeNodes = 0;
360   set < double > paramSet;
361   if ( eSubMesh )
362   {
363     // loop on nodes of an edge: sort them by param on edge
364     SMDS_NodeIteratorPtr nIt = eSubMesh->GetNodes();
365     while ( nIt->more() )
366     {
367       const SMDS_MeshNode* node = nIt->next();
368       const SMDS_PositionPtr& pos = node->GetPosition();
369       if ( pos->GetTypeOfPosition() != SMDS_TOP_EDGE )
370         return false;
371       const SMDS_EdgePosition* epos =
372         static_cast<const SMDS_EdgePosition*>(node->GetPosition());
373       if ( !paramSet.insert( epos->GetUParameter() ).second )
374         return false; // equal parameters
375     }
376   }
377   // add vertex nodes params
378   TopoDS_Vertex V1,V2;
379   TopExp::Vertices( theEdge, V1, V2);
380   if ( VertexNode( V1, theMesh ) &&
381        !paramSet.insert( BRep_Tool::Parameter(V1,theEdge) ).second )
382     return false; // there are equal parameters
383   if ( VertexNode( V2, theMesh ) &&
384        !paramSet.insert( BRep_Tool::Parameter(V2,theEdge) ).second )
385     return false; // there are equal parameters
386
387   // fill the vector
388   theParams.resize( paramSet.size() );
389   set < double >::iterator   par    = paramSet.begin();
390   vector< double >::iterator vecPar = theParams.begin();
391   for ( ; par != paramSet.end(); ++par, ++vecPar )
392     *vecPar = *par;
393
394   return theParams.size() > 1;
395 }
396
397 //================================================================================
398 /*!
399  * \brief Fill vector of node parameters on geometrical edge, including vertex nodes
400  * \param theMesh - The mesh containing nodes
401  * \param theEdge - The geometrical edge of interest
402  * \param theParams - The resulting vector of sorted node parameters
403  * \retval bool - false if not all parameters are OK
404  */
405 //================================================================================
406
407 bool SMESH_Algo::GetSortedNodesOnEdge(const SMESHDS_Mesh*                   theMesh,
408                                       const TopoDS_Edge&                    theEdge,
409                                       const bool                            ignoreMediumNodes,
410                                       map< double, const SMDS_MeshNode* > & theNodes)
411 {
412   theNodes.clear();
413
414   if ( !theMesh || theEdge.IsNull() )
415     return false;
416
417   SMESHDS_SubMesh * eSubMesh = theMesh->MeshElements( theEdge );
418   if ( !eSubMesh || !eSubMesh->GetElements()->more() )
419     return false; // edge is not meshed
420
421   int nbNodes = 0;
422   set < double > paramSet;
423   if ( eSubMesh )
424   {
425     // loop on nodes of an edge: sort them by param on edge
426     SMDS_NodeIteratorPtr nIt = eSubMesh->GetNodes();
427     while ( nIt->more() )
428     {
429       const SMDS_MeshNode* node = nIt->next();
430       if ( ignoreMediumNodes ) {
431         SMDS_ElemIteratorPtr elemIt = node->GetInverseElementIterator();
432         if ( elemIt->more() && elemIt->next()->IsMediumNode( node ))
433           continue;
434       }
435       const SMDS_PositionPtr& pos = node->GetPosition();
436       if ( pos->GetTypeOfPosition() != SMDS_TOP_EDGE )
437         return false;
438       const SMDS_EdgePosition* epos =
439         static_cast<const SMDS_EdgePosition*>(node->GetPosition());
440       theNodes.insert( make_pair( epos->GetUParameter(), node ));
441       //MESSAGE("U " << epos->GetUParameter() << " ID " << node->GetID());
442       ++nbNodes;
443     }
444   }
445   // add vertex nodes
446   TopoDS_Vertex v1, v2;
447   TopExp::Vertices(theEdge, v1, v2);
448   const SMDS_MeshNode* n1 = VertexNode( v1, (SMESHDS_Mesh*) theMesh );
449   const SMDS_MeshNode* n2 = VertexNode( v2, (SMESHDS_Mesh*) theMesh );
450   //MESSAGE("Vertices ID " << n1->GetID() << " " << n2->GetID());
451   Standard_Real f, l;
452   BRep_Tool::Range(theEdge, f, l);
453   if ( v1.Orientation() != TopAbs_FORWARD )
454     std::swap( f, l );
455   if ( n1 && ++nbNodes )
456     theNodes.insert( make_pair( f, n1 ));
457   if ( n2 && ++nbNodes )
458     theNodes.insert( make_pair( l, n2 ));
459
460   return theNodes.size() == nbNodes;
461 }
462
463 //================================================================================
464 /*!
465  * \brief Make filter recognize only compatible hypotheses
466  * \param theFilter - the filter to initialize
467  * \param ignoreAuxiliary - make filter ignore compatible auxiliary hypotheses
468  */
469 //================================================================================
470
471 bool SMESH_Algo::InitCompatibleHypoFilter( SMESH_HypoFilter & theFilter,
472                                            const bool         ignoreAuxiliary) const
473 {
474   if ( !_compatibleHypothesis.empty() )
475   {
476     theFilter.Init( theFilter.HasName( _compatibleHypothesis[0] ));
477     for ( int i = 1; i < _compatibleHypothesis.size(); ++i )
478       theFilter.Or( theFilter.HasName( _compatibleHypothesis[ i ] ));
479
480     if ( ignoreAuxiliary )
481       theFilter.AndNot( theFilter.IsAuxiliary() );
482
483     return true;
484   }
485   return false;
486 }
487
488 //================================================================================
489 /*!
490  * \brief Return continuity of two edges
491  * \param E1 - the 1st edge
492  * \param E2 - the 2nd edge
493  * \retval GeomAbs_Shape - regularity at the junction between E1 and E2
494  */
495 //================================================================================
496
497 GeomAbs_Shape SMESH_Algo::Continuity(TopoDS_Edge E1,
498                                      TopoDS_Edge E2)
499 {
500   //E1.Orientation(TopAbs_FORWARD), E2.Orientation(TopAbs_FORWARD); // avoid pb with internal edges
501   if (E1.Orientation() > TopAbs_REVERSED) // INTERNAL
502       E1.Orientation( TopAbs_FORWARD );
503   if (E2.Orientation() > TopAbs_REVERSED) // INTERNAL
504       E2.Orientation( TopAbs_FORWARD );
505   TopoDS_Vertex V = TopExp::LastVertex (E1, true);
506   if ( !V.IsSame( TopExp::FirstVertex(E2, true )))
507     if ( !TopExp::CommonVertex( E1, E2, V ))
508       return GeomAbs_C0;
509   Standard_Real u1 = BRep_Tool::Parameter( V, E1 );
510   Standard_Real u2 = BRep_Tool::Parameter( V, E2 );
511   BRepAdaptor_Curve C1( E1 ), C2( E2 );
512   Standard_Real tol = BRep_Tool::Tolerance( V );
513   Standard_Real angTol = 2e-3;
514   try {
515 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
516     OCC_CATCH_SIGNALS;
517 #endif
518     return BRepLProp::Continuity(C1, C2, u1, u2, tol, angTol);
519   }
520   catch (Standard_Failure) {
521   }
522   return GeomAbs_C0;
523 }
524
525 //================================================================================
526 /*!
527  * \brief Return the node built on a vertex
528  * \param V - the vertex
529  * \param meshDS - mesh
530  * \retval const SMDS_MeshNode* - found node or NULL
531  */
532 //================================================================================
533
534 const SMDS_MeshNode* SMESH_Algo::VertexNode(const TopoDS_Vertex& V,
535                                             const SMESHDS_Mesh*  meshDS)
536 {
537   if ( SMESHDS_SubMesh* sm = meshDS->MeshElements(V) ) {
538     SMDS_NodeIteratorPtr nIt= sm->GetNodes();
539     if (nIt->more())
540       return nIt->next();
541   }
542   return 0;
543 }
544
545 //=======================================================================
546 //function : GetCommonNodes
547 //purpose  : Return nodes common to two elements
548 //=======================================================================
549
550 vector< const SMDS_MeshNode*> SMESH_Algo::GetCommonNodes(const SMDS_MeshElement* e1,
551                                                          const SMDS_MeshElement* e2)
552 {
553   vector< const SMDS_MeshNode*> common;
554   for ( int i = 0 ; i < e1->NbNodes(); ++i )
555     if ( e2->GetNodeIndex( e1->GetNode( i )) >= 0 )
556       common.push_back( e1->GetNode( i ));
557   return common;
558 }
559
560 //=======================================================================
561 //function : GetMeshError
562 //purpose  : Finds topological errors of a sub-mesh
563 //WARNING  : 1D check is NOT implemented so far
564 //=======================================================================
565
566 SMESH_Algo::EMeshError SMESH_Algo::GetMeshError(SMESH_subMesh* subMesh)
567 {
568   EMeshError err = MEr_OK;
569
570   SMESHDS_SubMesh* smDS = subMesh->GetSubMeshDS();
571   if ( !smDS )
572     return MEr_EMPTY;
573
574   switch ( subMesh->GetSubShape().ShapeType() )
575   {
576   case TopAbs_FACE: { // ====================== 2D =====================
577
578     SMDS_ElemIteratorPtr fIt = smDS->GetElements();
579     if ( !fIt->more() )
580       return MEr_EMPTY;
581
582     // We check that olny links on EDGEs encouter once, the rest links, twice
583     set< SMESH_TLink > links;
584     while ( fIt->more() )
585     {
586       const SMDS_MeshElement* f = fIt->next();
587       int nbNodes = f->NbCornerNodes(); // ignore medium nodes
588       for ( int i = 0; i < nbNodes; ++i )
589       {
590         const SMDS_MeshNode* n1 = f->GetNode( i );
591         const SMDS_MeshNode* n2 = f->GetNode(( i+1 ) % nbNodes);
592         std::pair< set< SMESH_TLink >::iterator, bool > it_added =
593           links.insert( SMESH_TLink( n1, n2 ));
594         if ( !it_added.second )
595           // As we do NOT(!) check if mesh is manifold, we believe that a link can
596           // encounter once or twice only (not three times), we erase a link as soon
597           // as it encounters twice to speed up search in the <links> map.
598           links.erase( it_added.first );
599       }
600     }
601     // the links remaining in the <links> should all be on EDGE
602     set< SMESH_TLink >::iterator linkIt = links.begin();
603     for ( ; linkIt != links.end(); ++linkIt )
604     {
605       const SMESH_TLink& link = *linkIt;
606       if ( link.node1()->GetPosition()->GetTypeOfPosition() > SMDS_TOP_EDGE ||
607            link.node2()->GetPosition()->GetTypeOfPosition() > SMDS_TOP_EDGE )
608         return MEr_HOLES;
609     }
610     // TODO: to check orientation
611     break;
612   }
613   case TopAbs_SOLID: { // ====================== 3D =====================
614
615     SMDS_ElemIteratorPtr vIt = smDS->GetElements();
616     if ( !vIt->more() )
617       return MEr_EMPTY;
618
619     SMDS_VolumeTool vTool;
620     while ( !vIt->more() )
621     {
622       if (!vTool.Set( vIt->next() ))
623         continue; // strange
624
625       for ( int iF = 0; iF < vTool.NbFaces(); ++iF )
626         if ( vTool.IsFreeFace( iF ))
627         {
628           int nbN = vTool.NbFaceNodes( iF );
629           const SMDS_MeshNode** nodes =  vTool.GetFaceNodes( iF );
630           for ( int i = 0; i < nbN; ++i )
631             if ( nodes[i]->GetPosition()->GetTypeOfPosition() > SMDS_TOP_FACE )
632               return MEr_HOLES;
633         }
634     }
635     break;
636   }
637   default:;
638   }
639   return err;
640 }
641
642 //================================================================================
643 /*!
644  * \brief Sets event listener to submeshes if necessary
645  * \param subMesh - submesh where algo is set
646  * 
647  * After being set, event listener is notified on each event of a submesh.
648  * By default non listener is set
649  */
650 //================================================================================
651
652 void SMESH_Algo::SetEventListener(SMESH_subMesh* /*subMesh*/)
653 {
654 }
655
656 //================================================================================
657 /*!
658  * \brief Allow algo to do something after persistent restoration
659  * \param subMesh - restored submesh
660  *
661  * This method is called only if a submesh has HYP_OK algo_state.
662  */
663 //================================================================================
664
665 void SMESH_Algo::SubmeshRestored(SMESH_subMesh* /*subMesh*/)
666 {
667 }
668
669 //================================================================================
670 /*!
671  * \brief Computes mesh without geometry
672  * \param aMesh - the mesh
673  * \param aHelper - helper that must be used for adding elements to \aaMesh
674  * \retval bool - is a success
675  */
676 //================================================================================
677
678 bool SMESH_Algo::Compute(SMESH_Mesh & /*aMesh*/, SMESH_MesherHelper* /*aHelper*/)
679 {
680   return error( COMPERR_BAD_INPUT_MESH, "Mesh built on shape expected");
681 }
682
683 #ifdef WITH_SMESH_CANCEL_COMPUTE
684 void SMESH_Algo::CancelCompute()
685 {
686 }
687 #endif
688
689 //================================================================================
690 /*!
691  * \brief store error and comment and then return ( error == COMPERR_OK )
692  */
693 //================================================================================
694
695 bool SMESH_Algo::error(int error, const SMESH_Comment& comment)
696 {
697   _error   = error;
698   _comment = comment;
699   return ( error == COMPERR_OK );
700 }
701
702 //================================================================================
703 /*!
704  * \brief store error and return ( error == COMPERR_OK )
705  */
706 //================================================================================
707
708 bool SMESH_Algo::error(SMESH_ComputeErrorPtr error)
709 {
710   if ( error ) {
711     _error   = error->myName;
712     _comment = error->myComment;
713     _badInputElements = error->myBadElements;
714     return error->IsOK();
715   }
716   return true;
717 }
718
719 //================================================================================
720 /*!
721  * \brief return compute error
722  */
723 //================================================================================
724
725 SMESH_ComputeErrorPtr SMESH_Algo::GetComputeError() const
726 {
727   SMESH_ComputeErrorPtr err = SMESH_ComputeError::New( _error, _comment, this );
728   // hope this method is called by only SMESH_subMesh after this->Compute()
729   err->myBadElements.splice( err->myBadElements.end(),
730                              (list<const SMDS_MeshElement*>&) _badInputElements );
731   return err;
732 }
733
734 //================================================================================
735 /*!
736  * \brief initialize compute error
737  */
738 //================================================================================
739
740 void SMESH_Algo::InitComputeError()
741 {
742   _error = COMPERR_OK;
743   _comment.clear();
744   list<const SMDS_MeshElement*>::iterator elem = _badInputElements.begin();
745   for ( ; elem != _badInputElements.end(); ++elem )
746     if ( (*elem)->GetID() < 1 )
747       delete *elem;
748   _badInputElements.clear();
749 }
750
751 //================================================================================
752 /*!
753  * \brief store a bad input element preventing computation,
754  *        which may be a temporary one i.e. not residing the mesh,
755  *        then it will be deleted by InitComputeError()
756  */
757 //================================================================================
758
759 void SMESH_Algo::addBadInputElement(const SMDS_MeshElement* elem)
760 {
761   if ( elem )
762     _badInputElements.push_back( elem );
763 }