Salome HOME
0020918: EDF 1447 SMESH: Mesh common borders (note 0010726)
[modules/smesh.git] / src / StdMeshers / StdMeshers_Import_1D2D.cxx
1 //  Copyright (C) 2007-2010  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   : StdMeshers_Import_1D2D.cxx
25 //  Module : SMESH
26 //
27 #include "StdMeshers_Import_1D2D.hxx"
28
29 #include "StdMeshers_Import_1D.hxx"
30 #include "StdMeshers_ImportSource.hxx"
31
32 #include "SMDS_MeshElement.hxx"
33 #include "SMDS_MeshNode.hxx"
34 #include "SMESHDS_Group.hxx"
35 #include "SMESHDS_Mesh.hxx"
36 #include "SMESH_Comment.hxx"
37 #include "SMESH_Gen.hxx"
38 #include "SMESH_Group.hxx"
39 #include "SMESH_Mesh.hxx"
40 #include "SMESH_MesherHelper.hxx"
41 #include "SMESH_subMesh.hxx"
42
43 #include "Utils_SALOME_Exception.hxx"
44 #include "utilities.h"
45
46 #include <BRep_Builder.hxx>
47 #include <BRep_Tool.hxx>
48 #include <TopExp.hxx>
49 #include <TopExp_Explorer.hxx>
50 #include <TopoDS.hxx>
51 #include <TopoDS_Compound.hxx>
52 #include <TopoDS_Edge.hxx>
53 #include <TopoDS_Vertex.hxx>
54 #include <Precision.hxx>
55
56 #include <numeric>
57
58 using namespace std;
59
60 //=============================================================================
61 /*!
62  * Creates StdMeshers_Import_1D2D
63  */
64 //=============================================================================
65
66 StdMeshers_Import_1D2D::StdMeshers_Import_1D2D(int hypId, int studyId, SMESH_Gen * gen)
67   :SMESH_2D_Algo(hypId, studyId, gen), _sourceHyp(0)
68 {
69   MESSAGE("StdMeshers_Import_1D2D::StdMeshers_Import_1D2D");
70   _name = "Import_1D2D";
71   _shapeType = (1 << TopAbs_FACE);
72
73   _compatibleHypothesis.push_back("ImportSource2D");
74   _requireDescretBoundary = false;
75 }
76
77 //=============================================================================
78 /*!
79  * Check presence of a hypothesis
80  */
81 //=============================================================================
82
83 bool StdMeshers_Import_1D2D::CheckHypothesis
84                          (SMESH_Mesh&                          aMesh,
85                           const TopoDS_Shape&                  aShape,
86                           SMESH_Hypothesis::Hypothesis_Status& aStatus)
87 {
88   _sourceHyp = 0;
89
90   const list <const SMESHDS_Hypothesis * >&hyps = GetUsedHypothesis(aMesh, aShape);
91   if ( hyps.size() == 0 )
92   {
93     aStatus = SMESH_Hypothesis::HYP_MISSING;
94     return false;  // can't work with no hypothesis
95   }
96
97   if ( hyps.size() > 1 )
98   {
99     aStatus = SMESH_Hypothesis::HYP_ALREADY_EXIST;
100     return false;
101   }
102
103   const SMESHDS_Hypothesis *theHyp = hyps.front();
104
105   string hypName = theHyp->GetName();
106
107   if (hypName == _compatibleHypothesis.front())
108   {
109     _sourceHyp = (StdMeshers_ImportSource1D *)theHyp;
110     aStatus = SMESH_Hypothesis::HYP_OK;
111     return true;
112   }
113
114   aStatus = SMESH_Hypothesis::HYP_INCOMPATIBLE;
115   return true;
116 }
117
118 namespace
119 {
120   /*!
121    * \brief OrientedLink additionally storing a medium node
122    */
123   struct TLink : public SMESH_OrientedLink
124   {
125     const SMDS_MeshNode* _medium;
126     TLink( const SMDS_MeshNode* n1,
127            const SMDS_MeshNode* n2,
128            const SMDS_MeshNode* medium=0)
129       : SMESH_OrientedLink( n1,n2 ), _medium( medium ) {}
130   };
131 }
132
133 //=============================================================================
134 /*!
135  * Import elements from the other mesh 
136  */
137 //=============================================================================
138
139 bool StdMeshers_Import_1D2D::Compute(SMESH_Mesh & theMesh, const TopoDS_Shape & theShape)
140 {
141   if ( !_sourceHyp ) return false;
142
143   const vector<SMESH_Group*>& srcGroups = _sourceHyp->GetGroups();
144   if ( srcGroups.empty() )
145     return error("Invalid source groups");
146
147   SMESH_MesherHelper helper(theMesh);
148   helper.SetSubShape(theShape);
149   SMESHDS_Mesh* tgtMesh = theMesh.GetMeshDS();
150
151   const TopoDS_Face& geomFace = TopoDS::Face( theShape );
152   const double faceTol = helper.MaxTolerance( geomFace );
153   const int shapeID = tgtMesh->ShapeToIndex( geomFace );
154   const bool toCheckOri = (helper.NbAncestors( geomFace, theMesh, TopAbs_SOLID ) == 1 );
155
156   Handle(Geom_Surface) surface = BRep_Tool::Surface( geomFace );
157   const bool reverse = 
158     ( helper.GetSubShapeOri( tgtMesh->ShapeToMesh(), geomFace) == TopAbs_REVERSED );
159   gp_Pnt p; gp_Vec du, dv;
160
161   set<int> subShapeIDs;
162   subShapeIDs.insert( shapeID );
163
164   // get nodes on vertices
165   list < SMESH_TNodeXYZ > vertexNodes;
166   list < SMESH_TNodeXYZ >::iterator vNIt;
167   TopExp_Explorer exp( theShape, TopAbs_VERTEX );
168   for ( ; exp.More(); exp.Next() )
169   {
170     const TopoDS_Vertex& v = TopoDS::Vertex( exp.Current() );
171     if ( !subShapeIDs.insert( tgtMesh->ShapeToIndex( v )).second )
172       continue;
173     const SMDS_MeshNode* n = SMESH_Algo::VertexNode( v, tgtMesh );
174     if ( !n )
175     {
176       _gen->Compute(theMesh,v,/*anUpward=*/true);
177       n = SMESH_Algo::VertexNode( v, tgtMesh );
178       if ( !n ) return false; // very strange
179     }
180     vertexNodes.push_back( SMESH_TNodeXYZ( n ));
181   }
182
183   // to count now many times a link between nodes encounters
184   map<TLink, int> linkCount;
185   map<TLink, int>::iterator link2Nb;
186
187   // =========================
188   // Import faces from groups
189   // =========================
190
191   StdMeshers_Import_1D::TNodeNodeMap* n2n;
192   StdMeshers_Import_1D::TElemElemMap* e2e;
193   vector<const SMDS_MeshNode*> newNodes;
194   for ( int iG = 0; iG < srcGroups.size(); ++iG )
195   {
196     const SMESHDS_GroupBase* srcGroup = srcGroups[iG]->GetGroupDS();
197
198     const int meshID = srcGroup->GetMesh()->GetPersistentId();
199     const SMESH_Mesh* srcMesh = GetMeshByPersistentID( meshID );
200     if ( !srcMesh ) continue;
201     StdMeshers_Import_1D::getMaps( srcMesh, &theMesh, n2n, e2e );
202
203     SMDS_ElemIteratorPtr srcElems = srcGroup->GetElements();
204     SMDS_MeshNode *tmpNode = helper.AddNode(0,0,0);
205     gp_XY uv( Precision::Infinite(), Precision::Infinite() );
206     while ( srcElems->more() ) // loop on group contents
207     {
208       const SMDS_MeshElement* face = srcElems->next();
209       // find or create nodes of a new face
210       newNodes.resize( face->NbNodes() );
211       newNodes.back() = 0;
212       int nbCreatedNodes = 0;
213       SMDS_MeshElement::iterator node = face->begin_nodes();
214       for ( unsigned i = 0; i < newNodes.size(); ++i, ++node )
215       {
216         TNodeNodeMap::iterator n2nIt = n2n->insert( make_pair( *node, (SMDS_MeshNode*)0 )).first;
217         if ( n2nIt->second )
218         {
219           if ( !subShapeIDs.count( n2nIt->second->getshapeId() ))
220             break;
221         }
222         else
223         {
224           // find an existing vertex node
225           for ( vNIt = vertexNodes.begin(); vNIt != vertexNodes.end(); ++vNIt)
226             if ( vNIt->SquareDistance( *node ) < 10 * faceTol * faceTol)
227             {
228               (*n2nIt).second = vNIt->_node;
229               vertexNodes.erase( vNIt );
230               break;
231             }
232         }
233         if ( !n2nIt->second )
234         {
235           // find out if node lies on theShape
236           tmpNode->setXYZ( (*node)->X(), (*node)->Y(), (*node)->Z());
237           if ( helper.CheckNodeUV( geomFace, tmpNode, uv, 10 * faceTol, /*force=*/true ))
238           {
239             SMDS_MeshNode* newNode = tgtMesh->AddNode( (*node)->X(), (*node)->Y(), (*node)->Z());
240             n2nIt->second = newNode;
241             tgtMesh->SetNodeOnFace( newNode, shapeID, uv.X(), uv.Y() );
242             nbCreatedNodes++;
243           }
244         }
245         if ( !(newNodes[i] = n2nIt->second ))
246           break;
247       }
248       if ( !newNodes.back() )
249         continue; // not all nodes of the face lie on theShape
250
251       // try to find already created face
252       SMDS_MeshElement * newFace = 0;
253       if ( nbCreatedNodes == 0 &&
254            tgtMesh->FindElement(newNodes, SMDSAbs_Face, /*noMedium=*/false))
255         continue; // repeated face in source groups already created 
256
257       // check future face orientation
258       if ( toCheckOri )
259       {
260         int iNode = -1;
261         gp_Vec geomNorm;
262         do
263         {
264           uv = helper.GetNodeUV( geomFace, newNodes[++iNode] );
265           surface->D1( uv.X(),uv.Y(), p, du,dv );
266           geomNorm = reverse ? dv^du : du^dv;
267         }
268         while ( geomNorm.SquareMagnitude() < 1e-6 && iNode+1 < face->NbCornerNodes());
269
270         int iNext = helper.WrapIndex( iNode+1, face->NbCornerNodes() );
271         int iPrev = helper.WrapIndex( iNode-1, face->NbCornerNodes() );
272
273         SMESH_TNodeXYZ prevNode( newNodes[iPrev] );
274         SMESH_TNodeXYZ curNode ( newNodes[iNode] );
275         SMESH_TNodeXYZ nextNode( newNodes[iNext] );
276         gp_Vec n1n0( prevNode - curNode);
277         gp_Vec n1n2( nextNode - curNode );
278         gp_Vec meshNorm = n1n2 ^ n1n0;
279
280         if ( geomNorm * meshNorm < 0 )
281           std::reverse( newNodes.begin(), newNodes.end() );
282       }
283
284       // make a new face
285       switch ( newNodes.size() )
286       {
287       case 3:
288         newFace = tgtMesh->AddFace( newNodes[0], newNodes[1], newNodes[2] );
289         break;
290       case 4:
291         newFace = tgtMesh->AddFace( newNodes[0], newNodes[1], newNodes[2], newNodes[3] );
292         break;
293       case 6:
294         newFace = tgtMesh->AddFace( newNodes[0], newNodes[1], newNodes[2],
295                                     newNodes[3], newNodes[4], newNodes[5]);
296         break;
297       case 8:
298         newFace = tgtMesh->AddFace( newNodes[0], newNodes[1], newNodes[2], newNodes[3],
299                                     newNodes[4], newNodes[5], newNodes[6], newNodes[7]);
300         break;
301       default: continue;
302       }
303       tgtMesh->SetMeshElementOnShape( newFace, shapeID );
304       e2e->insert( make_pair( face, newFace ));
305
306       // collect links
307       int nbNodes = face->NbCornerNodes();
308       const SMDS_MeshNode* medium = 0;
309       for ( int i = 0; i < nbNodes; ++i )
310       {
311         const SMDS_MeshNode* n1 = newNodes[i];
312         const SMDS_MeshNode* n2 = newNodes[ (i+1)%nbNodes ];
313         if ( newFace->IsQuadratic() )
314           medium = newNodes[i+nbNodes];
315         link2Nb = linkCount.insert( make_pair( TLink( n1, n2, medium ), 0)).first;
316         ++link2Nb->second;
317       }
318     }
319     helper.GetMeshDS()->RemoveNode(tmpNode);
320   }
321
322   // ==========================================================
323   // Put nodes on geom edges and create edges on them;
324   // check if the whole geom face is covered by imported faces
325   // ==========================================================
326
327   vector< TopoDS_Edge > edges;
328   for ( exp.Init( theShape, TopAbs_EDGE ); exp.More(); exp.Next() )
329     if ( subShapeIDs.insert( tgtMesh->ShapeToIndex( exp.Current() )).second )
330       edges.push_back( TopoDS::Edge( exp.Current() ));
331
332   bool isFaceMeshed = false;
333   if ( SMESHDS_SubMesh* tgtSM = tgtMesh->MeshElements( theShape ))
334   {
335     // the imported mesh is valid if all external links (encountered once)
336     // lie on geom edges
337     subShapeIDs.erase( shapeID ); // to contain edges and vertices only
338     double u, f, l;
339     for ( link2Nb = linkCount.begin(); link2Nb != linkCount.end(); ++link2Nb)
340     {
341       const TLink& link = (*link2Nb).first;
342       int nbFaces = link2Nb->second;
343       if ( nbFaces == 1 )
344       {
345         // check if a not shared link lie on face boundary
346         bool nodesOnBoundary = true;
347         list< TopoDS_Shape > bndShapes;
348         for ( int is1stN = 0; is1stN < 2 && nodesOnBoundary; ++is1stN )
349         {
350           const SMDS_MeshNode* n = is1stN ? link.node1() : link.node2();
351           if ( !subShapeIDs.count( n->getshapeId() ))
352           {
353             for ( unsigned iE = 0; iE < edges.size(); ++iE )
354               if ( helper.CheckNodeU( edges[iE], n, u=0, 10 * faceTol, /*force=*/true ))
355               {
356                 BRep_Tool::Range(edges[iE],f,l);
357                 if ( Abs(u-f) < 2 * faceTol || Abs(u-l) < 2 * faceTol )
358                   // duplicated node on vertex
359                   return error("Source elements overlap one another");
360                 tgtSM->RemoveNode( n, /*isNodeDeleted=*/false );
361                 tgtMesh->SetNodeOnEdge( (SMDS_MeshNode*)n, edges[iE], u );
362                 break;
363               }
364             nodesOnBoundary = subShapeIDs.count( n->getshapeId());
365           }
366           if ( nodesOnBoundary )
367           {
368             TopoDS_Shape s = helper.GetSubShapeByNode( n, tgtMesh );
369             if ( s.ShapeType() == TopAbs_VERTEX )
370               bndShapes.push_front( s ); // vertex first
371             else
372               bndShapes.push_back( s ); // edges last
373           }
374         }
375         if ( !nodesOnBoundary )
376           break; // error: free internal link
377         if ( bndShapes.front().ShapeType() == TopAbs_EDGE &&
378              bndShapes.front() != bndShapes.back() )
379           break; // error: link nodes on different geom edges
380
381         // find geom edge the link is on
382         if ( bndShapes.back().ShapeType() != TopAbs_EDGE )
383         {
384           // find geom edge by two vertices
385           TopoDS_Shape geomEdge;
386           PShapeIteratorPtr edgeIt = helper.GetAncestors( bndShapes.back(), theMesh, TopAbs_EDGE );
387           while ( edgeIt->more() )
388           {
389             geomEdge = *(edgeIt->next());
390             if ( !helper.IsSubShape( bndShapes.front(), geomEdge ))
391               geomEdge.Nullify();
392           }
393           if ( geomEdge.IsNull() )
394             break; // vertices belong to different edges -> error: free internal link
395           bndShapes.push_back( geomEdge );
396         }
397
398         // create an edge if not yet exists
399         newNodes.resize(2);
400         newNodes[0] = link.node1(), newNodes[1] = link.node2();
401         const SMDS_MeshElement* edge = tgtMesh->FindElement( newNodes, SMDSAbs_Edge );
402         if ( edge ) continue;
403
404         if ( link._reversed ) std::swap( newNodes[0], newNodes[1] );
405         if ( link._medium )
406         {
407           newNodes.push_back( link._medium );
408           edge = tgtMesh->AddEdge( newNodes[0], newNodes[1], newNodes[2] );
409
410           TopoDS_Edge geomEdge = TopoDS::Edge(bndShapes.back());
411           helper.CheckNodeU( geomEdge, link._medium, u, 10*faceTol, /*force=*/true );
412           tgtSM->RemoveNode( link._medium, /*isNodeDeleted=*/false );
413           tgtMesh->SetNodeOnEdge( (SMDS_MeshNode*)link._medium, geomEdge, u );
414         }
415         else
416         {
417           edge = tgtMesh->AddEdge( newNodes[0], newNodes[1]);
418         }
419         if ( !edge )
420           return false;
421
422         tgtMesh->SetMeshElementOnShape( edge, bndShapes.back() );
423       }
424       else if ( nbFaces > 2 )
425       {
426         return error( "Non-manifold source mesh");
427       }
428     }
429     isFaceMeshed = ( link2Nb == linkCount.end() && !linkCount.empty());
430     if ( isFaceMeshed )
431     {
432       // check that source faces do not overlap:
433       // there must be only two edges sharing each vertex and bound to sub-edges of theShape
434       SMESH_MeshEditor editor( &theMesh );
435       set<int>::iterator subID = subShapeIDs.begin();
436       for ( ; subID != subShapeIDs.end(); ++subID )
437       {
438         const TopoDS_Shape& s = tgtMesh->IndexToShape( *subID );
439         if ( s.ShapeType() != TopAbs_VERTEX ) continue;
440         const SMDS_MeshNode* n = SMESH_Algo::VertexNode( TopoDS::Vertex(s), tgtMesh );
441         SMDS_ElemIteratorPtr eIt = n->GetInverseElementIterator(SMDSAbs_Edge);
442         int nbEdges = 0;
443         while ( eIt->more() )
444         {
445           const SMDS_MeshElement* edge = eIt->next();
446           int sId = editor.FindShape( edge );
447           nbEdges += subShapeIDs.count( sId );
448         }
449         if ( nbEdges < 2 )
450           return false; // weird
451         if ( nbEdges > 2 )
452           return error( "Source elements overlap one another");
453       }
454     }
455   }
456   if ( !isFaceMeshed )
457     return error( "Source elements don't cover totally the geometrical face" );
458
459   // notify sub-meshes of edges on computation
460   for ( unsigned iE = 0; iE < edges.size(); ++iE )
461     theMesh.GetSubMesh( edges[iE] )->ComputeStateEngine(SMESH_subMesh::CHECK_COMPUTE_STATE);
462
463   // ============
464   // Copy meshes
465   // ============
466
467   vector<SMESH_Mesh*> srcMeshes = _sourceHyp->GetSourceMeshes();
468   for ( unsigned i = 0; i < srcMeshes.size(); ++i )
469     StdMeshers_Import_1D::importMesh( srcMeshes[i], theMesh, _sourceHyp, theShape );
470
471   return true;
472 }
473
474 //=============================================================================
475 /*!
476  * \brief Set needed event listeners and create a submesh for a copied mesh
477  *
478  * This method is called only if a submesh has HYP_OK algo_state.
479  */
480 //=============================================================================
481
482 void StdMeshers_Import_1D2D::SetEventListener(SMESH_subMesh* subMesh)
483 {
484   if ( !_sourceHyp )
485   {
486     const TopoDS_Shape& tgtShape = subMesh->GetSubShape();
487     SMESH_Mesh*         tgtMesh  = subMesh->GetFather();
488     Hypothesis_Status aStatus;
489     CheckHypothesis( *tgtMesh, tgtShape, aStatus );
490   }
491   StdMeshers_Import_1D::setEventListener( subMesh, _sourceHyp );
492 }
493 void StdMeshers_Import_1D2D::SubmeshRestored(SMESH_subMesh* subMesh)
494 {
495   SetEventListener(subMesh);
496 }
497
498 //=============================================================================
499 /*!
500  * Predict nb of mesh entities created by Compute()
501  */
502 //=============================================================================
503
504 bool StdMeshers_Import_1D2D::Evaluate(SMESH_Mesh &         theMesh,
505                                       const TopoDS_Shape & theShape,
506                                       MapShapeNbElems&     aResMap)
507 {
508   if ( !_sourceHyp ) return false;
509
510   const vector<SMESH_Group*>& srcGroups = _sourceHyp->GetGroups();
511   if ( srcGroups.empty() )
512     return error("Invalid source groups");
513
514   vector<int> aVec(SMDSEntity_Last,0);
515
516   bool toCopyMesh, toCopyGroups;
517   _sourceHyp->GetCopySourceMesh(toCopyMesh, toCopyGroups);
518   if ( toCopyMesh ) // the whole mesh is copied
519   {
520     vector<SMESH_Mesh*> srcMeshes = _sourceHyp->GetSourceMeshes();
521     for ( unsigned i = 0; i < srcMeshes.size(); ++i )
522     {
523       SMESH_subMesh* sm = StdMeshers_Import_1D::getSubMeshOfCopiedMesh( theMesh, *srcMeshes[i]);
524       if ( !sm || aResMap.count( sm )) continue; // already counted
525       const SMDS_MeshInfo& aMeshInfo = srcMeshes[i]->GetMeshDS()->GetMeshInfo();
526       for (int i = 0; i < SMDSEntity_Last; i++)
527         aVec[i] = aMeshInfo.NbEntities((SMDSAbs_EntityType)i);
528     }
529   }
530   else
531   {
532     // std-like iterator used to get coordinates of nodes of mesh element
533     typedef SMDS_StdIterator< SMESH_TNodeXYZ, SMDS_ElemIteratorPtr > TXyzIterator;
534
535     SMESH_MesherHelper helper(theMesh);
536     helper.SetSubShape(theShape);
537
538     const TopoDS_Face& geomFace = TopoDS::Face( theShape );
539     const double faceTol = helper.MaxTolerance( geomFace );
540
541     // take into account nodes on vertices
542     TopExp_Explorer exp( theShape, TopAbs_VERTEX );
543     for ( ; exp.More(); exp.Next() )
544       theMesh.GetSubMesh( exp.Current())->Evaluate( aResMap );
545
546     // to count now many times a link between nodes encounters,
547     // negative nb additionally means that a link is quadratic
548     map<SMESH_TLink, int> linkCount;
549     map<SMESH_TLink, int>::iterator link2Nb;
550
551     // count faces and nodes imported from groups
552     set<const SMDS_MeshNode* > allNodes;
553     gp_XY uv;
554     for ( int iG = 0; iG < srcGroups.size(); ++iG )
555     {
556       const SMESHDS_GroupBase* srcGroup = srcGroups[iG]->GetGroupDS();
557       SMDS_ElemIteratorPtr srcElems = srcGroup->GetElements();
558       SMDS_MeshNode *tmpNode =helper.AddNode(0,0,0);
559       while ( srcElems->more() ) // loop on group contents
560       {
561         const SMDS_MeshElement* face = srcElems->next();
562         // find out if face is located on geomEdge by projecting
563         // a gravity center of face to geomFace
564         gp_XYZ gc(0,0,0);
565         gc = accumulate( TXyzIterator(face->nodesIterator()), TXyzIterator(), gc)/face->NbNodes();
566         tmpNode->setXYZ( gc.X(), gc.Y(), gc.Z());
567         if ( helper.CheckNodeUV( geomFace, tmpNode, uv, 10 * faceTol, /*force=*/true ))
568         {
569           ++aVec[ face->GetEntityType() ];
570
571           // collect links
572           int nbConers = face->NbCornerNodes();
573           for ( int i = 0; i < face->NbNodes(); ++i )
574           {
575             const SMDS_MeshNode* n1 = face->GetNode(i);
576             allNodes.insert( n1 );
577             if ( i < nbConers )
578             {
579               const SMDS_MeshNode* n2 = face->GetNode( (i+1)%nbConers );
580               link2Nb = linkCount.insert( make_pair( SMESH_TLink( n1, n2 ), 0)).first;
581               if ( (*link2Nb).second )
582                 link2Nb->second += (link2Nb->second < 0 ) ? -1 : 1;
583               else
584                 link2Nb->second += ( face->IsQuadratic() ) ? -1 : 1;
585             }
586           }
587         }
588       }
589       helper.GetMeshDS()->RemoveNode(tmpNode);
590     }
591
592     int nbNodes = allNodes.size();
593     allNodes.clear();
594
595     // count nodes and edges on geom edges
596
597     double u;
598     for ( exp.Init(theShape, TopAbs_EDGE); exp.More(); exp.Next() )
599     {
600       TopoDS_Edge geomEdge = TopoDS::Edge( exp.Current() );
601       SMESH_subMesh* sm = theMesh.GetSubMesh( geomEdge );
602       vector<int>& edgeVec = aResMap[sm];
603       if ( edgeVec.empty() )
604       {
605         edgeVec.resize(SMDSEntity_Last,0);
606         for ( link2Nb = linkCount.begin(); link2Nb != linkCount.end(); )
607         {
608           const SMESH_TLink& link = (*link2Nb).first;
609           int nbFacesOfLink = Abs( link2Nb->second );
610           bool eraseLink = ( nbFacesOfLink != 1 );
611           if ( nbFacesOfLink == 1 )
612           {
613             if ( helper.CheckNodeU( geomEdge, link.node1(), u, 10*faceTol, /*force=*/true )&&
614                  helper.CheckNodeU( geomEdge, link.node2(), u, 10*faceTol, /*force=*/true ))
615             {
616               bool isQuadratic = ( link2Nb->second < 0 );
617               ++edgeVec[ isQuadratic ? SMDSEntity_Quad_Edge : SMDSEntity_Edge ];
618               ++edgeVec[ SMDSEntity_Node ];
619               --nbNodes;
620               eraseLink = true;
621             }
622           }
623           if ( eraseLink )
624             linkCount.erase(link2Nb++);
625           else
626             link2Nb++;
627         }
628         if ( edgeVec[ SMDSEntity_Node] > 0 )
629           --edgeVec[ SMDSEntity_Node ]; // for one node on vertex
630       }
631       else if ( !helper.IsSeamShape( geomEdge ) ||
632                 geomEdge.Orientation() == TopAbs_FORWARD )
633       {
634         nbNodes -= 1+edgeVec[ SMDSEntity_Node ];
635       }
636     }
637
638     aVec[SMDSEntity_Node] = nbNodes;
639   }
640
641   SMESH_subMesh * sm = theMesh.GetSubMesh(theShape);
642   aResMap.insert(make_pair(sm,aVec));
643
644   return true;
645 }