Salome HOME
Merge from BR_Dev_For_6_3_1 03/06/2011
[modules/smesh.git] / src / StdMeshers / StdMeshers_Projection_2D.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      : StdMeshers_Projection_2D.cxx
25 // Module    : SMESH
26 // Created   : Fri Oct 20 11:37:07 2006
27 // Author    : Edward AGAPOV (eap)
28 //
29 #include "StdMeshers_Projection_2D.hxx"
30
31 #include "StdMeshers_ProjectionSource2D.hxx"
32 #include "StdMeshers_ProjectionUtils.hxx"
33 #include "StdMeshers_FaceSide.hxx"
34
35 #include "SMDS_EdgePosition.hxx"
36 #include "SMDS_FacePosition.hxx"
37 #include "SMESHDS_Hypothesis.hxx"
38 #include "SMESHDS_SubMesh.hxx"
39 #include "SMESH_Block.hxx"
40 #include "SMESH_Comment.hxx"
41 #include "SMESH_Gen.hxx"
42 #include "SMESH_Mesh.hxx"
43 #include "SMESH_MesherHelper.hxx"
44 #include "SMESH_Pattern.hxx"
45 #include "SMESH_subMesh.hxx"
46 #include "SMESH_subMeshEventListener.hxx"
47
48 #include "utilities.h"
49
50 #include <BRep_Tool.hxx>
51 #include <Bnd_B2d.hxx>
52 #include <TopExp.hxx>
53 #include <TopExp_Explorer.hxx>
54 #include <TopTools_ListIteratorOfListOfShape.hxx>
55 #include <TopoDS.hxx>
56 #include <gp_Ax2.hxx>
57 #include <gp_Ax3.hxx>
58
59
60 using namespace std;
61
62 #define RETURN_BAD_RESULT(msg) { MESSAGE(")-: Error: " << msg); return false; }
63
64 typedef StdMeshers_ProjectionUtils TAssocTool;
65
66 //=======================================================================
67 //function : StdMeshers_Projection_2D
68 //purpose  : 
69 //=======================================================================
70
71 StdMeshers_Projection_2D::StdMeshers_Projection_2D(int hypId, int studyId, SMESH_Gen* gen)
72   :SMESH_2D_Algo(hypId, studyId, gen)
73 {
74   _name = "Projection_2D";
75   _shapeType = (1 << TopAbs_FACE);      // 1 bit per shape type
76
77   _compatibleHypothesis.push_back("ProjectionSource2D");
78   _sourceHypo = 0;
79 }
80
81 //================================================================================
82 /*!
83  * \brief Destructor
84  */
85 //================================================================================
86
87 StdMeshers_Projection_2D::~StdMeshers_Projection_2D()
88 {}
89
90 //=======================================================================
91 //function : CheckHypothesis
92 //purpose  : 
93 //=======================================================================
94
95 bool StdMeshers_Projection_2D::CheckHypothesis(SMESH_Mesh&                          theMesh,
96                                                const TopoDS_Shape&                  theShape,
97                                                SMESH_Hypothesis::Hypothesis_Status& theStatus)
98 {
99   list <const SMESHDS_Hypothesis * >::const_iterator itl;
100
101   const list <const SMESHDS_Hypothesis * >&hyps = GetUsedHypothesis(theMesh, theShape);
102   if ( hyps.size() == 0 )
103   {
104     theStatus = HYP_MISSING;
105     return false;  // can't work with no hypothesis
106   }
107
108   if ( hyps.size() > 1 )
109   {
110     theStatus = HYP_ALREADY_EXIST;
111     return false;
112   }
113
114   const SMESHDS_Hypothesis *theHyp = hyps.front();
115
116   string hypName = theHyp->GetName();
117
118   theStatus = HYP_OK;
119
120   if (hypName == "ProjectionSource2D")
121   {
122     _sourceHypo = static_cast<const StdMeshers_ProjectionSource2D *>(theHyp);
123
124     // Check hypo parameters
125
126     SMESH_Mesh* srcMesh = _sourceHypo->GetSourceMesh();
127     SMESH_Mesh* tgtMesh = & theMesh;
128     if ( !srcMesh )
129       srcMesh = tgtMesh;
130
131     // check vertices
132     if ( _sourceHypo->HasVertexAssociation() )
133     {
134       // source vertices
135       TopoDS_Shape edge = TAssocTool::GetEdgeByVertices
136         ( srcMesh, _sourceHypo->GetSourceVertex(1), _sourceHypo->GetSourceVertex(2) );
137       if ( edge.IsNull() ||
138            !SMESH_MesherHelper::IsSubShape( edge, srcMesh ) ||
139            !SMESH_MesherHelper::IsSubShape( edge, _sourceHypo->GetSourceFace() ))
140       {
141         theStatus = HYP_BAD_PARAMETER;
142         SCRUTE((edge.IsNull()));
143         SCRUTE((SMESH_MesherHelper::IsSubShape( edge, srcMesh )));
144         SCRUTE((SMESH_MesherHelper::IsSubShape( edge, _sourceHypo->GetSourceFace() )));
145       }
146       else
147       {
148         // target vertices
149         edge = TAssocTool::GetEdgeByVertices
150           ( tgtMesh, _sourceHypo->GetTargetVertex(1), _sourceHypo->GetTargetVertex(2) );
151         if ( edge.IsNull() || !SMESH_MesherHelper::IsSubShape( edge, tgtMesh ))
152         {
153           theStatus = HYP_BAD_PARAMETER;
154           SCRUTE((edge.IsNull()));
155           SCRUTE((SMESH_MesherHelper::IsSubShape( edge, tgtMesh )));
156         }
157         // PAL16203
158         else if ( !_sourceHypo->IsCompoundSource() &&
159                   !SMESH_MesherHelper::IsSubShape( edge, theShape ))
160         {
161           theStatus = HYP_BAD_PARAMETER;
162           SCRUTE((SMESH_MesherHelper::IsSubShape( edge, theShape )));
163         }
164       }
165     }
166     // check a source face
167     if ( !SMESH_MesherHelper::IsSubShape( _sourceHypo->GetSourceFace(), srcMesh ) ||
168          ( srcMesh == tgtMesh && theShape == _sourceHypo->GetSourceFace() ))
169     {
170       theStatus = HYP_BAD_PARAMETER;
171       SCRUTE((SMESH_MesherHelper::IsSubShape( _sourceHypo->GetSourceFace(), srcMesh )));
172       SCRUTE((srcMesh == tgtMesh));
173       SCRUTE(( theShape == _sourceHypo->GetSourceFace() ));
174     }
175   }
176   else
177   {
178     theStatus = HYP_INCOMPATIBLE;
179   }
180   return ( theStatus == HYP_OK );
181 }
182
183 namespace {
184
185   //================================================================================
186   /*!
187    * \brief define if a node is new or old
188    * \param node - node to check
189    * \retval bool - true if the node existed before Compute() is called
190    */
191   //================================================================================
192
193   bool isOldNode( const SMDS_MeshNode* node )
194   {
195     // old nodes are shared by edges and new ones are shared
196     // only by faces created by mapper
197     SMDS_ElemIteratorPtr invEdge = node->GetInverseElementIterator(SMDSAbs_Edge);
198     bool isOld = invEdge->more();
199     return isOld;
200   }
201
202   //================================================================================
203   /*!
204    * \brief Class to remove mesh built by pattern mapper on edges
205    * and vertices in the case of failure of projection algo.
206    * It does it's job at destruction
207    */
208   //================================================================================
209
210   class MeshCleaner {
211     SMESH_subMesh* sm;
212   public:
213     MeshCleaner( SMESH_subMesh* faceSubMesh ): sm(faceSubMesh) {}
214     ~MeshCleaner() { Clean(sm); }
215     void Release() { sm = 0; } // mesh will not be removed
216     static void Clean( SMESH_subMesh* sm, bool withSub=true )
217     {
218       if ( !sm ) return;
219       // PAL16567, 18920. Remove face nodes as well
220 //       switch ( sm->GetSubShape().ShapeType() ) {
221 //       case TopAbs_VERTEX:
222 //       case TopAbs_EDGE: {
223         SMDS_NodeIteratorPtr nIt = sm->GetSubMeshDS()->GetNodes();
224         SMESHDS_Mesh* mesh = sm->GetFather()->GetMeshDS();
225         while ( nIt->more() ) {
226           const SMDS_MeshNode* node = nIt->next();
227           if ( !isOldNode( node ) )
228             mesh->RemoveNode( node );
229         }
230         // do not break but iterate over DependsOn()
231 //       }
232 //       default:
233         if ( !withSub ) return;
234         SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(false,false);
235         while ( smIt->more() )
236           Clean( smIt->next(), false );
237 //       }
238     }
239   };
240
241   //================================================================================
242   /*!
243    * \brief find new nodes belonging to one free border of mesh on face
244     * \param sm - submesh on edge or vertex containg nodes to choose from
245     * \param face - the face bound the submesh
246     * \param u2nodes - map to fill with nodes
247     * \param seamNodes - set of found nodes
248     * \retval bool - is a success
249    */
250   //================================================================================
251
252   bool getBoundaryNodes ( SMESH_subMesh*                        sm,
253                           const TopoDS_Face&                    face,
254                           map< double, const SMDS_MeshNode* > & u2nodes,
255                           set< const SMDS_MeshNode* > &         seamNodes)
256   {
257     u2nodes.clear();
258     seamNodes.clear();
259     if ( !sm || !sm->GetSubMeshDS() )
260       RETURN_BAD_RESULT("Null submesh");
261
262     SMDS_NodeIteratorPtr nIt = sm->GetSubMeshDS()->GetNodes();
263     switch ( sm->GetSubShape().ShapeType() ) {
264
265     case TopAbs_VERTEX: {
266       while ( nIt->more() ) {
267         const SMDS_MeshNode* node = nIt->next();
268         if ( isOldNode( node ) ) continue;
269         u2nodes.insert( make_pair( 0., node ));
270         seamNodes.insert( node );
271         return true;
272       }
273       break;
274     }
275     case TopAbs_EDGE: {
276       
277       // Get submeshes of sub-vertices
278       const map< int, SMESH_subMesh * >& subSM = sm->DependsOn();
279       if ( subSM.size() != 2 )
280         RETURN_BAD_RESULT("there must be 2 submeshes of sub-vertices"
281                           " but we have " << subSM.size());
282       SMESH_subMesh* smV1 = subSM.begin()->second;
283       SMESH_subMesh* smV2 = subSM.rbegin()->second;
284       if ( !smV1->IsMeshComputed() || !smV2->IsMeshComputed() )
285         RETURN_BAD_RESULT("Empty vertex submeshes");
286
287       // Look for a new node on V1
288       nIt = smV1->GetSubMeshDS()->GetNodes();
289       const SMDS_MeshNode* nV1 = 0;
290       while ( nIt->more() && !nV1 ) {
291         const SMDS_MeshNode* node = nIt->next();
292         if ( !isOldNode( node ) ) nV1 = node;
293       }
294       if ( !nV1 )
295         RETURN_BAD_RESULT("No new node found on V1");
296
297       // Find a new node connected to nV1 and belonging to edge submesh;
298       const SMDS_MeshNode* nE = 0;
299       SMESHDS_SubMesh* smDS = sm->GetSubMeshDS();
300       SMDS_ElemIteratorPtr vElems = nV1->GetInverseElementIterator(SMDSAbs_Face);
301       while ( vElems->more() && !nE ) {
302         const SMDS_MeshElement* elem = vElems->next();
303         int nbNodes = elem->NbNodes();
304         if ( elem->IsQuadratic() )
305           nbNodes /= 2;
306         int iV1 = elem->GetNodeIndex( nV1 );
307         // try next after nV1
308         int iE = SMESH_MesherHelper::WrapIndex( iV1 + 1, nbNodes );
309         if ( smDS->Contains( elem->GetNode( iE ) ))
310           nE = elem->GetNode( iE );
311         if ( !nE ) {
312           // try node before nV1
313           iE = SMESH_MesherHelper::WrapIndex( iV1 - 1, nbNodes );
314           if ( smDS->Contains( elem->GetNode( iE )))
315             nE = elem->GetNode( iE );
316         }
317         if ( nE && elem->IsQuadratic() ) { // find medium node between nV1 and nE
318           if ( Abs( iV1 - iE ) == 1 )
319             nE = elem->GetNode( Min ( iV1, iE ) + nbNodes );
320           else
321             nE = elem->GetNode( elem->NbNodes() - 1 );
322         }
323       }
324       if ( !nE )
325         RETURN_BAD_RESULT("new node on edge not found");
326
327       // Get the whole free border of a face
328       list< const SMDS_MeshNode* > bordNodes;
329       list< const SMDS_MeshElement* > bordFaces;
330       if ( !SMESH_MeshEditor::FindFreeBorder (nV1, nE, nV1, bordNodes, bordFaces ))
331         RETURN_BAD_RESULT("free border of a face not found by nodes " <<
332                           nV1->GetID() << " " << nE->GetID() );
333
334       // Insert nodes of the free border to the map until node on V2 encountered
335       SMESHDS_SubMesh* v2smDS = smV2->GetSubMeshDS();
336       list< const SMDS_MeshNode* >::iterator bordIt = bordNodes.begin();
337       bordIt++; // skip nV1
338       for ( ; bordIt != bordNodes.end(); ++bordIt ) {
339         const SMDS_MeshNode* node = *bordIt;
340         if ( v2smDS->Contains( node ))
341           break;
342         if ( node->GetPosition()->GetTypeOfPosition() != SMDS_TOP_EDGE )
343           RETURN_BAD_RESULT("Bad node position type: node " << node->GetID() <<
344                             " pos type " << node->GetPosition()->GetTypeOfPosition());
345         const SMDS_EdgePosition* pos =
346           static_cast<const SMDS_EdgePosition*>(node->GetPosition());
347         u2nodes.insert( make_pair( pos->GetUParameter(), node ));
348         seamNodes.insert( node );
349       }
350       if ( u2nodes.size() != seamNodes.size() )
351         RETURN_BAD_RESULT("Bad node params on edge " << sm->GetId() <<
352                           ", " << u2nodes.size() << " != " << seamNodes.size() );
353       return true;
354     }
355     default:;
356     }
357     RETURN_BAD_RESULT ("Unexpected submesh type");
358
359   } // bool getBoundaryNodes()
360
361   //================================================================================
362   /*!
363    * \brief Preform projection in case if tgtFace.IsPartner( srcFace ) and in case
364    * if projection by transformation is possible
365    */
366   //================================================================================
367
368   bool projectPartner(const TopoDS_Face&                tgtFace,
369                       const TopoDS_Face&                srcFace,
370                       SMESH_Mesh *                      tgtMesh,
371                       SMESH_Mesh *                      srcMesh,
372                       const TAssocTool::TShapeShapeMap& shape2ShapeMap)
373   {
374     MESSAGE("projectPartner");
375     const double tol = 1.e-7*srcMesh->GetMeshDS()->getMaxDim();
376
377     gp_Trsf trsf; // transformation to get location of target nodes from source ones
378     if ( tgtFace.IsPartner( srcFace ))
379     {
380       gp_Trsf srcTrsf = srcFace.Location();
381       gp_Trsf tgtTrsf = tgtFace.Location();
382       trsf = srcTrsf.Inverted() * tgtTrsf;
383     }
384     else
385     {
386       // Try to find the transformation
387
388       // make any local coord systems of src and tgt faces
389       vector<gp_Pnt> srcPP, tgtPP; // 3 points on face boundaries to make axes of CS
390       SMESH_subMesh * srcSM = srcMesh->GetSubMesh( srcFace );
391       SMESH_subMeshIteratorPtr smIt = srcSM->getDependsOnIterator(/*includeSelf=*/false,false);
392       srcSM = smIt->next(); // sm of a vertex
393       while ( smIt->more() && srcPP.size() < 3 )
394       {
395         srcSM = smIt->next();
396         SMESHDS_SubMesh* srcSmds = srcSM->GetSubMeshDS();
397         if ( !srcSmds ) continue;
398         SMDS_NodeIteratorPtr nIt = srcSmds->GetNodes();
399         while ( nIt->more() )
400         {
401           SMESH_TNodeXYZ p ( nIt->next());
402           bool pOK = false;
403           switch ( srcPP.size() )
404           {
405           case 0: pOK = true; break;
406
407           case 1: pOK = ( srcPP[0].SquareDistance( p ) > 10*tol ); break;
408             
409           case 2:
410             {
411               gp_Vec p0p1( srcPP[0], srcPP[1] ), p0p( srcPP[0], p );
412               // pOK = !p0p1.IsParallel( p0p, tol );
413               pOK = !p0p1.IsParallel( p0p, 3.14/20 ); // angle min 18 degrees
414               break;
415             }
416           }
417           if ( !pOK )
418             continue;
419
420           // find corresponding point on target shape
421           pOK = false;
422           gp_Pnt tgtP;
423           const TopoDS_Shape& tgtShape = shape2ShapeMap( srcSM->GetSubShape() );
424           if ( tgtShape.ShapeType() == TopAbs_VERTEX )
425           {
426             tgtP = BRep_Tool::Pnt( TopoDS::Vertex( tgtShape ));
427             pOK = true;
428             //cout << "V - nS " << p._node->GetID() << " - nT " << SMESH_Algo::VertexNode(TopoDS::Vertex( tgtShape),tgtMesh->GetMeshDS())->GetID() << endl;
429           }
430           else if ( tgtPP.size() > 0 )
431           {
432             if ( SMESHDS_SubMesh* tgtSmds = tgtMesh->GetMeshDS()->MeshElements( tgtShape ))
433             {
434               double srcDist = srcPP[0].Distance( p );
435               double eTol = BRep_Tool::Tolerance( TopoDS::Edge( tgtShape ));
436               if (eTol < tol) eTol = tol;
437               SMDS_NodeIteratorPtr nItT = tgtSmds->GetNodes();
438               while ( nItT->more() && !pOK )
439               {
440                 const SMDS_MeshNode* n = nItT->next();
441                 tgtP = SMESH_TNodeXYZ( n );
442                 pOK = ( fabs( srcDist - tgtPP[0].Distance( tgtP )) < 2*eTol );
443                 //cout << "E - nS " << p._node->GetID() << " - nT " << n->GetID()<< " OK - " << pOK<< " " << fabs( srcDist - tgtPP[0].Distance( tgtP ))<< " tol " << eTol<< endl;
444               }
445             }
446           }
447           if ( !pOK )
448             continue;
449
450           srcPP.push_back( p );
451           tgtPP.push_back( tgtP );
452         }
453       }
454       if ( srcPP.size() != 3 )
455         return false;
456
457       // make transformation
458       gp_Trsf fromTgtCS, toSrcCS; // from/to global CS
459       gp_Ax2 srcCS( srcPP[0], gp_Vec( srcPP[0], srcPP[1] ), gp_Vec( srcPP[0], srcPP[2]));
460       gp_Ax2 tgtCS( tgtPP[0], gp_Vec( tgtPP[0], tgtPP[1] ), gp_Vec( tgtPP[0], tgtPP[2]));
461       toSrcCS  .SetTransformation( gp_Ax3( srcCS ));
462       fromTgtCS.SetTransformation( gp_Ax3( tgtCS ));
463       fromTgtCS.Invert();
464
465       trsf = fromTgtCS * toSrcCS;
466     }
467
468     // Fill map of src to tgt nodes with nodes on edges
469
470     map<const SMDS_MeshNode* , const SMDS_MeshNode*> src2tgtNodes;
471     map<const SMDS_MeshNode* , const SMDS_MeshNode*>::iterator srcN_tgtN;
472
473     for ( TopExp_Explorer srcEdge( srcFace, TopAbs_EDGE); srcEdge.More(); srcEdge.Next() )
474     {
475       const TopoDS_Shape& tgtEdge = shape2ShapeMap( srcEdge.Current() );
476
477       map< double, const SMDS_MeshNode* > srcNodes, tgtNodes;
478       if ( !SMESH_Algo::GetSortedNodesOnEdge( srcMesh->GetMeshDS(),
479                                               TopoDS::Edge( srcEdge.Current() ),
480                                               /*ignoreMediumNodes = */true,
481                                               srcNodes )
482            ||
483            !SMESH_Algo::GetSortedNodesOnEdge( tgtMesh->GetMeshDS(),
484                                               TopoDS::Edge( tgtEdge ),
485                                               /*ignoreMediumNodes = */true,
486                                               tgtNodes )
487            ||
488            srcNodes.size() != tgtNodes.size())
489         return false;
490
491       if ( !tgtEdge.IsPartner( srcEdge.Current() ))
492       {
493         // check that transformation is OK by three nodes
494         gp_Pnt p0S = SMESH_TNodeXYZ( (srcNodes.begin())  ->second);
495         gp_Pnt p1S = SMESH_TNodeXYZ( (srcNodes.rbegin()) ->second);
496         gp_Pnt p2S = SMESH_TNodeXYZ( (++srcNodes.begin())->second);
497
498         gp_Pnt p0T = SMESH_TNodeXYZ( (tgtNodes.begin())  ->second);
499         gp_Pnt p1T = SMESH_TNodeXYZ( (tgtNodes.rbegin()) ->second);
500         gp_Pnt p2T = SMESH_TNodeXYZ( (++tgtNodes.begin())->second);
501
502         // transform source points, they must coincide with target ones
503         if ( p0T.SquareDistance( p0S.Transformed( trsf )) > tol ||
504              p1T.SquareDistance( p1S.Transformed( trsf )) > tol ||
505              p2T.SquareDistance( p2S.Transformed( trsf )) > tol )
506         {
507           //cout << "KO trsf, 3 dist: "
508           //<< p0T.SquareDistance( p0S.Transformed( trsf ))<< ", "
509           //<< p1T.SquareDistance( p1S.Transformed( trsf ))<< ", "
510           //<< p2T.SquareDistance( p2S.Transformed( trsf ))<< ", "<<endl;
511           return false;
512         }
513       }
514
515       map< double, const SMDS_MeshNode* >::iterator u_tn = tgtNodes.begin();
516       map< double, const SMDS_MeshNode* >::iterator u_sn = srcNodes.begin();
517       for ( ; u_tn != tgtNodes.end(); ++u_tn, ++u_sn)
518         src2tgtNodes.insert( make_pair( u_sn->second, u_tn->second ));
519     }
520
521     // Make new faces
522
523     // prepare the helper adding quadratic elements if necessary
524     SMESH_MesherHelper helper( *tgtMesh );
525     helper.SetSubShape( tgtFace );
526     helper.IsQuadraticSubMesh( tgtFace );
527     helper.SetElementsOnShape( true );
528
529     SMESH_MesherHelper srcHelper( *srcMesh );
530     srcHelper.SetSubShape( srcFace );
531
532     const SMDS_MeshNode* nullNode = 0;
533
534     // indices of nodes to create properly oriented faces
535     int tri1 = 1, tri2 = 2, quad1 = 1, quad3 = 3;
536     if ( trsf.Form() != gp_Identity )
537       std::swap( tri1, tri2 ), std::swap( quad1, quad3 );
538
539     SMESHDS_SubMesh* srcSubDS = srcMesh->GetMeshDS()->MeshElements( srcFace );
540     SMDS_ElemIteratorPtr elemIt = srcSubDS->GetElements();
541     vector< const SMDS_MeshNode* > tgtNodes;
542     while ( elemIt->more() ) // loop on all mesh faces on srcFace
543     {
544       const SMDS_MeshElement* elem = elemIt->next();
545       const int nbN = elem->NbCornerNodes(); 
546       tgtNodes.resize( nbN );
547       for ( int i = 0; i < nbN; ++i ) // loop on nodes of the source element
548       {
549         const SMDS_MeshNode* srcNode = elem->GetNode(i);
550         srcN_tgtN = src2tgtNodes.insert( make_pair( srcNode, nullNode )).first;
551         if ( srcN_tgtN->second == nullNode )
552         {
553           // create a new node
554           gp_Pnt tgtP = gp_Pnt(srcNode->X(),srcNode->Y(),srcNode->Z()).Transformed( trsf );
555           SMDS_MeshNode* n = helper.AddNode( tgtP.X(), tgtP.Y(), tgtP.Z() );
556           srcN_tgtN->second = n;
557
558           gp_Pnt2d srcUV = srcHelper.GetNodeUV( srcFace, srcNode,
559                                                 elem->GetNode( helper.WrapIndex(i+1,nbN)));
560           n->SetPosition( new SMDS_FacePosition( srcUV.X(), srcUV.Y() ));
561         }
562         tgtNodes[i] = srcN_tgtN->second;
563       }
564       // create a new face
565       switch ( nbN )
566       {
567       case 3: helper.AddFace(tgtNodes[0], tgtNodes[tri1], tgtNodes[tri2]); break;
568       case 4: helper.AddFace(tgtNodes[0], tgtNodes[quad1], tgtNodes[2], tgtNodes[quad3]); break;
569       }
570     }
571     return true;
572
573   } //   bool projectPartner()
574
575   //================================================================================
576   /*!
577    * \brief Preform projection in case if the faces are similar in 2D space
578    */
579   //================================================================================
580
581   bool projectBy2DSimilarity(const TopoDS_Face&                tgtFace,
582                              const TopoDS_Face&                srcFace,
583                              SMESH_Mesh *                      tgtMesh,
584                              SMESH_Mesh *                      srcMesh,
585                              const TAssocTool::TShapeShapeMap& shape2ShapeMap)
586   {
587     // 1) Preparation
588
589     // get ordered src EDGEs
590     TError err;
591     TSideVector srcWires =
592       StdMeshers_FaceSide::GetFaceWires( srcFace, *srcMesh,/*theIgnoreMediumNodes = */false, err);
593     if ( err && !err->IsOK() )
594       return false;
595
596     // make corresponding sequence of tgt EDGEs
597     TSideVector tgtWires( srcWires.size() );
598     for ( unsigned iW = 0; iW < srcWires.size(); ++iW )
599     {
600       list< TopoDS_Edge > tgtEdges;
601       StdMeshers_FaceSidePtr srcWire = srcWires[iW];
602       for ( int iE = 0; iE < srcWire->NbEdges(); ++iE )
603         tgtEdges.push_back( TopoDS::Edge( shape2ShapeMap( srcWire->Edge( iE ))));
604
605       tgtWires[ iW ].reset( new StdMeshers_FaceSide( tgtFace, tgtEdges, tgtMesh,
606                                                      /*theIsForward = */ true,
607                                                      /*theIgnoreMediumNodes = */false));
608       if ( srcWires[iW]->GetUVPtStruct().size() !=
609            tgtWires[iW]->GetUVPtStruct().size())
610         return false;
611     }
612
613     // 2) Find transformation
614
615     gp_Trsf2d trsf;
616     {
617       // get ordered nodes data
618       const vector<UVPtStruct>& srcUVs = srcWires[0]->GetUVPtStruct();
619       const vector<UVPtStruct>& tgtUVs = tgtWires[0]->GetUVPtStruct();
620
621       // get 2 pairs of corresponding UVs
622       gp_XY srcP0( srcUVs[0].u, srcUVs[0].v );
623       gp_XY srcP1( srcUVs[1].u, srcUVs[1].v );
624       gp_XY tgtP0( tgtUVs[0].u, tgtUVs[0].v );
625       gp_XY tgtP1( tgtUVs[1].u, tgtUVs[1].v );
626
627       // make transformation
628       gp_Trsf2d fromTgtCS, toSrcCS; // from/to global CS
629       gp_Ax2d srcCS( srcP0, gp_Vec2d( srcP0, srcP1 ));
630       gp_Ax2d tgtCS( tgtP0, gp_Vec2d( tgtP0, tgtP1 ));
631       toSrcCS  .SetTransformation( srcCS );
632       fromTgtCS.SetTransformation( tgtCS );
633       fromTgtCS.Invert();
634
635       trsf = fromTgtCS * toSrcCS;
636
637       // check transformation
638       const double tol = 1e-5 * gp_Vec2d( srcP0, srcP1 ).Magnitude();
639       const int nbCheckPnt = Min( 10, srcUVs.size()-2 );
640       const int dP = ( srcUVs.size()-2 ) / nbCheckPnt;
641       for ( unsigned iP = 2; iP < srcUVs.size(); iP += dP )
642       {
643         gp_Pnt2d srcUV( srcUVs[iP].u, srcUVs[iP].v );
644         gp_Pnt2d tgtUV( tgtUVs[iP].u, tgtUVs[iP].v );
645         gp_Pnt2d tgtUV2 = srcUV.Transformed( trsf );
646         if ( tgtUV.Distance( tgtUV2 ) > tol )
647           return false;
648       }
649     }
650
651     // 3) Projection
652
653     typedef map<const SMDS_MeshNode* , const SMDS_MeshNode*, TIDCompare> TN2NMap;
654     TN2NMap src2tgtNodes;
655     TN2NMap::iterator srcN_tgtN;
656
657     // fill src2tgtNodes in with nodes on EDGEs
658     for ( unsigned iW = 0; iW < srcWires.size(); ++iW )
659     {
660       const vector<UVPtStruct>& srcUVs = srcWires[iW]->GetUVPtStruct();
661       const vector<UVPtStruct>& tgtUVs = tgtWires[iW]->GetUVPtStruct();
662       for ( unsigned i = 0; i < srcUVs.size(); ++i )
663         src2tgtNodes.insert( make_pair( srcUVs[i].node, tgtUVs[i].node ));
664     }
665
666     // make elements
667     SMESH_MesherHelper helper( *tgtMesh );
668     helper.SetSubShape( tgtFace );
669     helper.IsQuadraticSubMesh( tgtFace );
670     helper.SetElementsOnShape( true );
671     Handle(Geom_Surface) tgtSurface = BRep_Tool::Surface( tgtFace );
672     
673     SMESH_MesherHelper srcHelper( *srcMesh );
674     srcHelper.SetSubShape( srcFace );
675
676     const SMDS_MeshNode* nullNode = 0;
677
678     SMESHDS_SubMesh* srcSubDS = srcMesh->GetMeshDS()->MeshElements( srcFace );
679     SMDS_ElemIteratorPtr elemIt = srcSubDS->GetElements();
680     vector< const SMDS_MeshNode* > tgtNodes;
681     bool uvOK;
682     while ( elemIt->more() ) // loop on all mesh faces on srcFace
683     {
684       const SMDS_MeshElement* elem = elemIt->next();
685       const int nbN = elem->NbCornerNodes(); 
686       tgtNodes.resize( nbN );
687       for ( int i = 0; i < nbN; ++i ) // loop on nodes of the source element
688       {
689         const SMDS_MeshNode* srcNode = elem->GetNode(i);
690         srcN_tgtN = src2tgtNodes.insert( make_pair( srcNode, nullNode )).first;
691         if ( srcN_tgtN->second == nullNode )
692         {
693           // create a new node
694           gp_Pnt2d srcUV = srcHelper.GetNodeUV( srcFace, srcNode,
695                                                 elem->GetNode( helper.WrapIndex(i+1,nbN)), &uvOK);
696           
697           gp_Pnt2d tgtUV = srcUV.Transformed( trsf );
698           gp_Pnt   tgtP  = tgtSurface->Value( tgtUV.X(), tgtUV.Y() );
699           SMDS_MeshNode* n = helper.AddNode( tgtP.X(), tgtP.Y(), tgtP.Z() );
700           n->SetPosition( new SMDS_FacePosition( tgtUV.X(), tgtUV.Y() ));
701           srcN_tgtN->second = n;
702         }
703         tgtNodes[i] = srcN_tgtN->second;
704       }
705       // create a new face (with reversed orientation)
706       switch ( nbN )
707       {
708       case 3: helper.AddFace(tgtNodes[0], tgtNodes[2], tgtNodes[1]); break;
709       case 4: helper.AddFace(tgtNodes[0], tgtNodes[3], tgtNodes[2], tgtNodes[1]); break;
710       }
711     }
712     return true;
713
714   } // bool projectBy2DSimilarity()
715
716
717 } // namespace
718
719
720 //=======================================================================
721 //function : Compute
722 //purpose  : 
723 //=======================================================================
724
725 bool StdMeshers_Projection_2D::Compute(SMESH_Mesh& theMesh, const TopoDS_Shape& theShape)
726 {
727   MESSAGE("Projection_2D Compute");
728   if ( !_sourceHypo )
729     return false;
730
731   SMESH_Mesh * srcMesh = _sourceHypo->GetSourceMesh();
732   SMESH_Mesh * tgtMesh = & theMesh;
733   if ( !srcMesh )
734     srcMesh = tgtMesh;
735
736   SMESHDS_Mesh * meshDS = theMesh.GetMeshDS();
737
738   // ---------------------------
739   // Make subshapes association
740   // ---------------------------
741
742   TopoDS_Face tgtFace = TopoDS::Face( theShape.Oriented(TopAbs_FORWARD));
743   TopoDS_Shape srcShape = _sourceHypo->GetSourceFace().Oriented(TopAbs_FORWARD);
744
745   TAssocTool::TShapeShapeMap shape2ShapeMap;
746   TAssocTool::InitVertexAssociation( _sourceHypo, shape2ShapeMap, tgtFace );
747   if ( !TAssocTool::FindSubShapeAssociation( tgtFace, tgtMesh, srcShape, srcMesh,
748                                              shape2ShapeMap)  ||
749        !shape2ShapeMap.IsBound( tgtFace ))
750     return error(COMPERR_BAD_SHAPE,"Topology of source and target faces seems different" );
751
752   TopoDS_Face srcFace = TopoDS::Face( shape2ShapeMap( tgtFace ).Oriented(TopAbs_FORWARD));
753
754   // ----------------------------------------------
755   // Assure that mesh on a source Face is computed
756   // ----------------------------------------------
757
758   SMESH_subMesh* srcSubMesh = srcMesh->GetSubMesh( srcFace );
759   SMESH_subMesh* tgtSubMesh = tgtMesh->GetSubMesh( tgtFace );
760
761   if ( tgtMesh == srcMesh ) {
762     if ( !TAssocTool::MakeComputed( srcSubMesh ))
763       return error(COMPERR_BAD_INPUT_MESH,"Source mesh not computed");
764   }
765   else {
766     if ( !srcSubMesh->IsMeshComputed() )
767       return error(COMPERR_BAD_INPUT_MESH,"Source mesh not computed");
768   }
769
770   // try to project from same face with different location
771   if ( projectPartner( tgtFace, srcFace, tgtMesh, srcMesh, shape2ShapeMap ))
772     return true;
773
774   if ( projectBy2DSimilarity( tgtFace, srcFace, tgtMesh, srcMesh, shape2ShapeMap ))
775     return true;
776
777   // --------------------
778   // Prepare to mapping 
779   // --------------------
780
781   SMESH_MesherHelper helper( theMesh );
782   helper.SetSubShape( tgtFace );
783
784   // Check if node projection to a face is needed
785   Bnd_B2d uvBox;
786   SMDS_ElemIteratorPtr faceIt = srcSubMesh->GetSubMeshDS()->GetElements();
787   int nbFaceNodes = 0;
788   for ( ; nbFaceNodes < 3 && faceIt->more();  ) {
789     const SMDS_MeshElement* face = faceIt->next();
790     SMDS_ElemIteratorPtr nodeIt = face->nodesIterator();
791     while ( nodeIt->more() ) {
792       const SMDS_MeshNode* node = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
793       if ( node->GetPosition()->GetTypeOfPosition() == SMDS_TOP_FACE ) {
794         nbFaceNodes++;
795         uvBox.Add( helper.GetNodeUV( srcFace, node ));
796       }
797     }
798   }
799   const bool toProjectNodes =
800     ( nbFaceNodes > 0 && ( uvBox.IsVoid() || uvBox.SquareExtent() < DBL_MIN ));
801
802   // Load pattern from the source face
803   SMESH_Pattern mapper;
804   mapper.Load( srcMesh, srcFace, toProjectNodes );
805   if ( mapper.GetErrorCode() != SMESH_Pattern::ERR_OK )
806     return error(COMPERR_BAD_INPUT_MESH,"Can't load mesh pattern from the source face");
807
808   // Find the first target vertex corresponding to first vertex of the <mapper>
809   // and <theReverse> flag needed to call mapper.Apply()
810
811   TopoDS_Vertex srcV1 = TopoDS::Vertex( mapper.GetSubShape( 1 ));
812   if ( srcV1.IsNull() )
813     RETURN_BAD_RESULT("Mesh is not bound to the face");
814   if ( !shape2ShapeMap.IsBound( srcV1 ))
815     RETURN_BAD_RESULT("Not associated vertices, srcV1 " << srcV1.TShape().operator->() );
816   TopoDS_Vertex tgtV1 = TopoDS::Vertex( shape2ShapeMap( srcV1 ));
817
818   if ( !SMESH_MesherHelper::IsSubShape( srcV1, srcFace ))
819     RETURN_BAD_RESULT("Wrong srcV1 " << srcV1.TShape().operator->());
820   if ( !SMESH_MesherHelper::IsSubShape( tgtV1, tgtFace ))
821     RETURN_BAD_RESULT("Wrong tgtV1 " << tgtV1.TShape().operator->());
822
823   // try to find out orientation by order of edges
824   bool reverse = false;
825   list< TopoDS_Edge > tgtEdges, srcEdges;
826   list< int > nbEdgesInWires;
827   SMESH_Block::GetOrderedEdges( tgtFace, tgtV1, tgtEdges, nbEdgesInWires);
828   SMESH_Block::GetOrderedEdges( srcFace, srcV1, srcEdges, nbEdgesInWires);
829   if ( nbEdgesInWires.front() > 1 ) // possible to find out
830   {
831     TopoDS_Edge srcE1 = srcEdges.front(), tgtE1 = tgtEdges.front();
832     TopoDS_Shape srcE1bis = shape2ShapeMap( tgtE1 );
833     reverse = ( ! srcE1.IsSame( srcE1bis ));
834   }
835   else if ( nbEdgesInWires.front() == 1 )
836   {
837     // TODO::Compare orientation of curves in a sole edge
838     //RETURN_BAD_RESULT("Not implemented case");
839   }
840   else
841   {
842     RETURN_BAD_RESULT("Bad result from SMESH_Block::GetOrderedEdges()");
843   }
844
845   // --------------------
846   // Perform 2D mapping 
847   // --------------------
848
849   // Compute mesh on a target face
850
851   mapper.Apply( tgtFace, tgtV1, reverse );
852   if ( mapper.GetErrorCode() != SMESH_Pattern::ERR_OK )
853     return error("Can't apply source mesh pattern to the face");
854
855   // Create the mesh
856
857   const bool toCreatePolygons = false, toCreatePolyedrs = false;
858   mapper.MakeMesh( tgtMesh, toCreatePolygons, toCreatePolyedrs );
859   if ( mapper.GetErrorCode() != SMESH_Pattern::ERR_OK )
860     return error("Can't make mesh by source mesh pattern");
861
862   // it will remove mesh built by pattern mapper on edges and vertices
863   // in failure case
864   MeshCleaner cleaner( tgtSubMesh );
865
866   // -------------------------------------------------------------------------
867   // mapper doesn't take care of nodes already existing on edges and vertices,
868   // so we must merge nodes created by it with existing ones 
869   // -------------------------------------------------------------------------
870
871   SMESH_MeshEditor editor( tgtMesh );
872   SMESH_MeshEditor::TListOfListOfNodes groupsOfNodes;
873
874   // Make groups of nodes to merge
875
876   // loop on edge and vertex submeshes of a target face
877   SMESH_subMeshIteratorPtr smIt = tgtSubMesh->getDependsOnIterator(false,false);
878   while ( smIt->more() )
879   {
880     SMESH_subMesh*     sm = smIt->next();
881     SMESHDS_SubMesh* smDS = sm->GetSubMeshDS();
882
883     // Sort new and old nodes of a submesh separately
884
885     bool isSeam = helper.IsRealSeam( sm->GetId() );
886
887     enum { NEW_NODES = 0, OLD_NODES };
888     map< double, const SMDS_MeshNode* > u2nodesMaps[2], u2nodesOnSeam;
889     map< double, const SMDS_MeshNode* >::iterator u_oldNode, u_newNode, u_newOnSeam, newEnd;
890     set< const SMDS_MeshNode* > seamNodes;
891
892     // mapper puts on a seam edge nodes from 2 edges
893     if ( isSeam && ! getBoundaryNodes ( sm, tgtFace, u2nodesOnSeam, seamNodes ))
894       RETURN_BAD_RESULT("getBoundaryNodes() failed");
895
896     SMDS_NodeIteratorPtr nIt = smDS->GetNodes();
897     while ( nIt->more() )
898     {
899       const SMDS_MeshNode* node = nIt->next();
900       bool isOld = isOldNode( node );
901
902       if ( !isOld && isSeam ) { // new node on a seam edge
903         if ( seamNodes.find( node ) != seamNodes.end())
904           continue; // node is already in the map
905       }
906
907       // sort nodes on edges by their position
908       map< double, const SMDS_MeshNode* > & pos2nodes = u2nodesMaps[isOld ? OLD_NODES : NEW_NODES];
909       switch ( node->GetPosition()->GetTypeOfPosition() )
910       {
911       case  SMDS_TOP_VERTEX: {
912         pos2nodes.insert( make_pair( 0, node ));
913         break;
914       }
915       case  SMDS_TOP_EDGE:   {
916         const SMDS_EdgePosition* pos =
917           static_cast<const SMDS_EdgePosition*>(node->GetPosition());
918         pos2nodes.insert( make_pair( pos->GetUParameter(), node ));
919         break;
920       }
921       default:
922         RETURN_BAD_RESULT("Wrong node position type: "<<
923                           node->GetPosition()->GetTypeOfPosition());
924       }
925     }
926     if ( u2nodesMaps[ NEW_NODES ].size() != u2nodesMaps[ OLD_NODES ].size() )
927     {
928       if ( u2nodesMaps[ NEW_NODES ].size() == 0         &&
929            sm->GetSubShape().ShapeType() == TopAbs_EDGE &&
930            helper.IsDegenShape( sm->GetId() )             )
931         // NPAL15894 (tt88bis.py) - project mesh built by NETGEN_1d_2D that
932         // does not make segments/nodes on degenerated edges
933         continue;
934
935       RETURN_BAD_RESULT("Different nb of old and new nodes on shape #"<< sm->GetId() <<" "<<
936                         u2nodesMaps[ OLD_NODES ].size() << " != " <<
937                         u2nodesMaps[ NEW_NODES ].size());
938     }
939     if ( isSeam && u2nodesMaps[ OLD_NODES ].size() != u2nodesOnSeam.size() ) {
940       RETURN_BAD_RESULT("Different nb of old and seam nodes " <<
941                         u2nodesMaps[ OLD_NODES ].size() << " != " << u2nodesOnSeam.size());
942     }
943     // Make groups of nodes to merge
944     u_oldNode = u2nodesMaps[ OLD_NODES ].begin(); 
945     u_newNode = u2nodesMaps[ NEW_NODES ].begin();
946     newEnd    = u2nodesMaps[ NEW_NODES ].end();
947     u_newOnSeam = u2nodesOnSeam.begin();
948     for ( ; u_newNode != newEnd; ++u_newNode, ++u_oldNode ) {
949       groupsOfNodes.push_back( list< const SMDS_MeshNode* >() );
950       groupsOfNodes.back().push_back( u_oldNode->second );
951       groupsOfNodes.back().push_back( u_newNode->second );
952       if ( isSeam )
953         groupsOfNodes.back().push_back( (u_newOnSeam++)->second );
954     }
955   }
956
957   // Merge
958
959   int nbFaceBeforeMerge = tgtSubMesh->GetSubMeshDS()->NbElements();
960   editor.MergeNodes( groupsOfNodes );
961   int nbFaceAtferMerge = tgtSubMesh->GetSubMeshDS()->NbElements();
962   if ( nbFaceBeforeMerge != nbFaceAtferMerge )
963     return error(COMPERR_BAD_INPUT_MESH, "Probably invalid node parameters on geom faces");
964
965   // ---------------------------
966   // Check elements orientation
967   // ---------------------------
968
969   TopoDS_Face face = tgtFace;
970   if ( !theMesh.IsMainShape( tgtFace ))
971   {
972     // find the main shape
973     TopoDS_Shape mainShape = meshDS->ShapeToMesh();
974     switch ( mainShape.ShapeType() ) {
975     case TopAbs_SHELL:
976     case TopAbs_SOLID: break;
977     default:
978       TopTools_ListIteratorOfListOfShape ancestIt = theMesh.GetAncestors( face );
979       for ( ; ancestIt.More(); ancestIt.Next() ) {
980         TopAbs_ShapeEnum type = ancestIt.Value().ShapeType();
981         if ( type == TopAbs_SOLID ) {
982           mainShape = ancestIt.Value();
983           break;
984         } else if ( type == TopAbs_SHELL ) {
985           mainShape = ancestIt.Value();
986         }
987       }
988     }
989     // find tgtFace in the main solid or shell to know it's true orientation.
990     TopExp_Explorer exp( mainShape, TopAbs_FACE );
991     for ( ; exp.More(); exp.Next() ) {
992       if ( tgtFace.IsSame( exp.Current() )) {
993         face = TopoDS::Face( exp.Current() );
994         break;
995       }
996     }
997   }
998   // Fix orientation
999   if ( SMESH_Algo::IsReversedSubMesh( face, meshDS ))
1000   {
1001     SMDS_ElemIteratorPtr eIt = meshDS->MeshElements( face )->GetElements();
1002     while ( eIt->more() ) {
1003       const SMDS_MeshElement* e = eIt->next();
1004       if ( e->GetType() == SMDSAbs_Face && !editor.Reorient( e ))
1005         RETURN_BAD_RESULT("Pb of SMESH_MeshEditor::Reorient()");
1006     }
1007   }
1008
1009   cleaner.Release(); // do not remove mesh
1010
1011   return true;
1012 }
1013
1014
1015 //=======================================================================
1016 //function : Evaluate
1017 //purpose  : 
1018 //=======================================================================
1019
1020 bool StdMeshers_Projection_2D::Evaluate(SMESH_Mesh& theMesh,
1021                                         const TopoDS_Shape& theShape,
1022                                         MapShapeNbElems& aResMap)
1023 {
1024   if ( !_sourceHypo )
1025     return false;
1026
1027   SMESH_Mesh * srcMesh = _sourceHypo->GetSourceMesh();
1028   SMESH_Mesh * tgtMesh = & theMesh;
1029   if ( !srcMesh )
1030     srcMesh = tgtMesh;
1031
1032   // ---------------------------
1033   // Make subshapes association
1034   // ---------------------------
1035
1036   TopoDS_Face tgtFace = TopoDS::Face( theShape.Oriented(TopAbs_FORWARD));
1037   TopoDS_Shape srcShape = _sourceHypo->GetSourceFace().Oriented(TopAbs_FORWARD);
1038
1039   TAssocTool::TShapeShapeMap shape2ShapeMap;
1040   TAssocTool::InitVertexAssociation( _sourceHypo, shape2ShapeMap, tgtFace );
1041   if ( !TAssocTool::FindSubShapeAssociation( tgtFace, tgtMesh, srcShape, srcMesh,
1042                                              shape2ShapeMap)  ||
1043        !shape2ShapeMap.IsBound( tgtFace ))
1044     return error(COMPERR_BAD_SHAPE,"Topology of source and target faces seems different" );
1045
1046   TopoDS_Face srcFace = TopoDS::Face( shape2ShapeMap( tgtFace ).Oriented(TopAbs_FORWARD));
1047
1048   // ----------------------------------------------
1049   // Assure that mesh on a source Face is computed
1050   // ----------------------------------------------
1051
1052   SMESH_subMesh* srcSubMesh = srcMesh->GetSubMesh( srcFace );
1053
1054   if ( !srcSubMesh->IsMeshComputed() )
1055     return error(COMPERR_BAD_INPUT_MESH,"Source mesh not computed");
1056
1057
1058   std::vector<int> aVec(SMDSEntity_Last);
1059   for(int i=SMDSEntity_Node; i<SMDSEntity_Last; i++) aVec[i] = 0;
1060
1061   aVec[SMDSEntity_Node] = srcSubMesh->GetSubMeshDS()->NbNodes();
1062
1063   //bool quadratic = false;
1064   SMDS_ElemIteratorPtr elemIt = srcSubMesh->GetSubMeshDS()->GetElements();
1065   while ( elemIt->more() ) {
1066     const SMDS_MeshElement* E  = elemIt->next();
1067     if( E->NbNodes()==3 ) {
1068       aVec[SMDSEntity_Triangle]++;
1069     }
1070     else if( E->NbNodes()==4 ) {
1071       aVec[SMDSEntity_Quadrangle]++;
1072     }
1073     else if( E->NbNodes()==6 && E->IsQuadratic() ) {
1074       aVec[SMDSEntity_Quad_Triangle]++;
1075     }
1076     else if( E->NbNodes()==8 && E->IsQuadratic() ) {
1077       aVec[SMDSEntity_Quad_Quadrangle]++;
1078     }
1079     else {
1080       aVec[SMDSEntity_Polygon]++;
1081     }
1082   }
1083
1084   SMESH_subMesh * sm = theMesh.GetSubMesh(theShape);
1085   aResMap.insert(std::make_pair(sm,aVec));
1086
1087   return true;
1088 }
1089
1090
1091 //=============================================================================
1092 /*!
1093  * \brief Sets a default event listener to submesh of the source face
1094   * \param subMesh - submesh where algo is set
1095  *
1096  * This method is called when a submesh gets HYP_OK algo_state.
1097  * After being set, event listener is notified on each event of a submesh.
1098  * Arranges that CLEAN event is translated from source submesh to
1099  * the submesh
1100  */
1101 //=============================================================================
1102
1103 void StdMeshers_Projection_2D::SetEventListener(SMESH_subMesh* subMesh)
1104 {
1105   TAssocTool::SetEventListener( subMesh,
1106                                 _sourceHypo->GetSourceFace(),
1107                                 _sourceHypo->GetSourceMesh() );
1108 }