Salome HOME
0020184: EDF SMESH 952: Projection 2D does not work
[modules/smesh.git] / src / StdMeshers / StdMeshers_Projection_2D.cxx
1 //  Copyright (C) 2007-2008  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 //  SMESH SMESH : implementaion of SMESH idl descriptions
23 // File      : StdMeshers_Projection_2D.cxx
24 // Module    : SMESH
25 // Created   : Fri Oct 20 11:37:07 2006
26 // Author    : Edward AGAPOV (eap)
27 //
28 #include "StdMeshers_Projection_2D.hxx"
29
30 #include "StdMeshers_ProjectionSource2D.hxx"
31 #include "StdMeshers_ProjectionUtils.hxx"
32
33 #include "SMESHDS_Hypothesis.hxx"
34 #include "SMESHDS_SubMesh.hxx"
35 #include "SMESH_Block.hxx"
36 #include "SMESH_Gen.hxx"
37 #include "SMESH_Mesh.hxx"
38 #include "SMESH_MeshEditor.hxx"
39 #include "SMESH_Pattern.hxx"
40 #include "SMESH_subMesh.hxx"
41 #include "SMESH_subMeshEventListener.hxx"
42 #include "SMESH_Comment.hxx"
43 #include "SMDS_EdgePosition.hxx"
44
45 #include "utilities.h"
46
47 #include <BRep_Tool.hxx>
48 #include <Bnd_B2d.hxx>
49 #include <TopExp.hxx>
50 #include <TopExp_Explorer.hxx>
51 #include <TopTools_ListIteratorOfListOfShape.hxx>
52 #include <TopoDS.hxx>
53
54
55 using namespace std;
56
57 #define RETURN_BAD_RESULT(msg) { MESSAGE(")-: Error: " << msg); return false; }
58
59 typedef StdMeshers_ProjectionUtils TAssocTool;
60
61 //=======================================================================
62 //function : StdMeshers_Projection_2D
63 //purpose  : 
64 //=======================================================================
65
66 StdMeshers_Projection_2D::StdMeshers_Projection_2D(int hypId, int studyId, SMESH_Gen* gen)
67   :SMESH_2D_Algo(hypId, studyId, gen)
68 {
69   _name = "Projection_2D";
70   _shapeType = (1 << TopAbs_FACE);      // 1 bit per shape type
71
72   _compatibleHypothesis.push_back("ProjectionSource2D");
73   _sourceHypo = 0;
74 }
75
76 //================================================================================
77 /*!
78  * \brief Destructor
79  */
80 //================================================================================
81
82 StdMeshers_Projection_2D::~StdMeshers_Projection_2D()
83 {}
84
85 //=======================================================================
86 //function : CheckHypothesis
87 //purpose  : 
88 //=======================================================================
89
90 bool StdMeshers_Projection_2D::CheckHypothesis(SMESH_Mesh&                          theMesh,
91                                                const TopoDS_Shape&                  theShape,
92                                                SMESH_Hypothesis::Hypothesis_Status& theStatus)
93 {
94   list <const SMESHDS_Hypothesis * >::const_iterator itl;
95
96   const list <const SMESHDS_Hypothesis * >&hyps = GetUsedHypothesis(theMesh, theShape);
97   if ( hyps.size() == 0 )
98   {
99     theStatus = HYP_MISSING;
100     return false;  // can't work with no hypothesis
101   }
102
103   if ( hyps.size() > 1 )
104   {
105     theStatus = HYP_ALREADY_EXIST;
106     return false;
107   }
108
109   const SMESHDS_Hypothesis *theHyp = hyps.front();
110
111   string hypName = theHyp->GetName();
112
113   theStatus = HYP_OK;
114
115   if (hypName == "ProjectionSource2D")
116   {
117     _sourceHypo = static_cast<const StdMeshers_ProjectionSource2D *>(theHyp);
118
119     // Check hypo parameters
120
121     SMESH_Mesh* srcMesh = _sourceHypo->GetSourceMesh();
122     SMESH_Mesh* tgtMesh = & theMesh;
123     if ( !srcMesh )
124       srcMesh = tgtMesh;
125
126     // check vertices
127     if ( _sourceHypo->HasVertexAssociation() )
128     {
129       // source vertices
130       TopoDS_Shape edge = TAssocTool::GetEdgeByVertices
131         ( srcMesh, _sourceHypo->GetSourceVertex(1), _sourceHypo->GetSourceVertex(2) );
132       if ( edge.IsNull() ||
133            !TAssocTool::IsSubShape( edge, srcMesh ) ||
134            !TAssocTool::IsSubShape( edge, _sourceHypo->GetSourceFace() ))
135       {
136         theStatus = HYP_BAD_PARAMETER;
137         SCRUTE((edge.IsNull()));
138         SCRUTE((TAssocTool::IsSubShape( edge, srcMesh )));
139         SCRUTE((TAssocTool::IsSubShape( edge, _sourceHypo->GetSourceFace() )));
140       }
141       else
142       {
143         // target vertices
144         edge = TAssocTool::GetEdgeByVertices
145           ( tgtMesh, _sourceHypo->GetTargetVertex(1), _sourceHypo->GetTargetVertex(2) );
146         if ( edge.IsNull() || !TAssocTool::IsSubShape( edge, tgtMesh ))
147         {
148           theStatus = HYP_BAD_PARAMETER;
149           SCRUTE((edge.IsNull()));
150           SCRUTE((TAssocTool::IsSubShape( edge, tgtMesh )));
151         }
152         // PAL16203
153         else if ( !_sourceHypo->IsCompoundSource() &&
154                   !TAssocTool::IsSubShape( edge, theShape ))
155         {
156           theStatus = HYP_BAD_PARAMETER;
157           SCRUTE((TAssocTool::IsSubShape( edge, theShape )));
158         }
159       }
160     }
161     // check a source face
162     if ( !TAssocTool::IsSubShape( _sourceHypo->GetSourceFace(), srcMesh ) ||
163          ( srcMesh == tgtMesh && theShape == _sourceHypo->GetSourceFace() ))
164     {
165       theStatus = HYP_BAD_PARAMETER;
166       SCRUTE((TAssocTool::IsSubShape( _sourceHypo->GetSourceFace(), srcMesh )));
167       SCRUTE((srcMesh == tgtMesh));
168       SCRUTE(( theShape == _sourceHypo->GetSourceFace() ));
169     }
170   }
171   else
172   {
173     theStatus = HYP_INCOMPATIBLE;
174   }
175   return ( theStatus == HYP_OK );
176 }
177
178 namespace {
179
180
181   //================================================================================
182   /*!
183    * \brief define if a node is new or old
184     * \param node - node to check
185     * \retval bool - true if the node existed before Compute() is called
186    */
187   //================================================================================
188
189   bool isOldNode( const SMDS_MeshNode* node )
190   {
191     // old nodes are shared by edges and new ones are shared
192     // only by faces created by mapper
193     SMDS_ElemIteratorPtr invEdge = node->GetInverseElementIterator(SMDSAbs_Edge);
194     bool isOld = invEdge->more();
195     return isOld;
196   }
197
198   //================================================================================
199   /*!
200    * \brief Class to remove mesh built by pattern mapper on edges
201    * and vertices in the case of failure of projection algo.
202    * It does it's job at destruction
203    */
204   //================================================================================
205
206   class MeshCleaner {
207     SMESH_subMesh* sm;
208   public:
209     MeshCleaner( SMESH_subMesh* faceSubMesh ): sm(faceSubMesh) {}
210     ~MeshCleaner() { Clean(sm); }
211     void Release() { sm = 0; } // mesh will not be removed
212     static void Clean( SMESH_subMesh* sm, bool withSub=true )
213     {
214       if ( !sm ) return;
215       // PAL16567, 18920. Remove face nodes as well
216 //       switch ( sm->GetSubShape().ShapeType() ) {
217 //       case TopAbs_VERTEX:
218 //       case TopAbs_EDGE: {
219         SMDS_NodeIteratorPtr nIt = sm->GetSubMeshDS()->GetNodes();
220         SMESHDS_Mesh* mesh = sm->GetFather()->GetMeshDS();
221         while ( nIt->more() ) {
222           const SMDS_MeshNode* node = nIt->next();
223           if ( !isOldNode( node ) )
224             mesh->RemoveNode( node );
225         }
226         // do not break but iterate over DependsOn()
227 //       }
228 //       default:
229         if ( !withSub ) return;
230         SMESH_subMeshIteratorPtr smIt = sm->getDependsOnIterator(false,false);
231         while ( smIt->more() )
232           Clean( smIt->next(), false );
233 //       }
234     }
235   };
236
237   //================================================================================
238   /*!
239    * \brief find new nodes belonging to one free border of mesh on face
240     * \param sm - submesh on edge or vertex containg nodes to choose from
241     * \param face - the face bound the submesh
242     * \param u2nodes - map to fill with nodes
243     * \param seamNodes - set of found nodes
244     * \retval bool - is a success
245    */
246   //================================================================================
247
248   bool getBoundaryNodes ( SMESH_subMesh*                        sm,
249                           const TopoDS_Face&                    face,
250                           map< double, const SMDS_MeshNode* > & u2nodes,
251                           set< const SMDS_MeshNode* > &         seamNodes)
252   {
253     u2nodes.clear();
254     seamNodes.clear();
255     if ( !sm || !sm->GetSubMeshDS() )
256       RETURN_BAD_RESULT("Null submesh");
257
258     SMDS_NodeIteratorPtr nIt = sm->GetSubMeshDS()->GetNodes();
259     switch ( sm->GetSubShape().ShapeType() ) {
260
261     case TopAbs_VERTEX: {
262       while ( nIt->more() ) {
263         const SMDS_MeshNode* node = nIt->next();
264         if ( isOldNode( node ) ) continue;
265         u2nodes.insert( make_pair( 0., node ));
266         seamNodes.insert( node );
267         return true;
268       }
269       break;
270     }
271     case TopAbs_EDGE: {
272       
273       // Get submeshes of sub-vertices
274       const map< int, SMESH_subMesh * >& subSM = sm->DependsOn();
275       if ( subSM.size() != 2 )
276         RETURN_BAD_RESULT("there must be 2 submeshes of sub-vertices"
277                           " but we have " << subSM.size());
278       SMESH_subMesh* smV1 = subSM.begin()->second;
279       SMESH_subMesh* smV2 = subSM.rbegin()->second;
280       if ( !smV1->IsMeshComputed() || !smV2->IsMeshComputed() )
281         RETURN_BAD_RESULT("Empty vertex submeshes");
282
283       // Look for a new node on V1
284       nIt = smV1->GetSubMeshDS()->GetNodes();
285       const SMDS_MeshNode* nV1 = 0;
286       while ( nIt->more() && !nV1 ) {
287         const SMDS_MeshNode* node = nIt->next();
288         if ( !isOldNode( node ) ) nV1 = node;
289       }
290       if ( !nV1 )
291         RETURN_BAD_RESULT("No new node found on V1");
292
293       // Find a new node connected to nV1 and belonging to edge submesh;
294       const SMDS_MeshNode* nE = 0;
295       SMESHDS_SubMesh* smDS = sm->GetSubMeshDS();
296       SMDS_ElemIteratorPtr vElems = nV1->GetInverseElementIterator(SMDSAbs_Face);
297       while ( vElems->more() && !nE ) {
298         const SMDS_MeshElement* elem = vElems->next();
299         int nbNodes = elem->NbNodes();
300         if ( elem->IsQuadratic() )
301           nbNodes /= 2;
302         int iV1 = elem->GetNodeIndex( nV1 );
303         // try next after nV1
304         int iE = SMESH_MesherHelper::WrapIndex( iV1 + 1, nbNodes );
305         if ( smDS->Contains( elem->GetNode( iE ) ))
306           nE = elem->GetNode( iE );
307         if ( !nE ) {
308           // try node before nV1
309           iE = SMESH_MesherHelper::WrapIndex( iV1 - 1, nbNodes );
310           if ( smDS->Contains( elem->GetNode( iE )))
311             nE = elem->GetNode( iE );
312         }
313         if ( nE && elem->IsQuadratic() ) { // find medium node between nV1 and nE
314           if ( Abs( iV1 - iE ) == 1 )
315             nE = elem->GetNode( Min ( iV1, iE ) + nbNodes );
316           else
317             nE = elem->GetNode( elem->NbNodes() - 1 );
318         }
319       }
320       if ( !nE )
321         RETURN_BAD_RESULT("new node on edge not found");
322
323       // Get the whole free border of a face
324       list< const SMDS_MeshNode* > bordNodes;
325       list< const SMDS_MeshElement* > bordFaces;
326       if ( !SMESH_MeshEditor::FindFreeBorder (nV1, nE, nV1, bordNodes, bordFaces ))
327         RETURN_BAD_RESULT("free border of a face not found by nodes " <<
328                           nV1->GetID() << " " << nE->GetID() );
329
330       // Insert nodes of the free border to the map until node on V2 encountered
331       SMESHDS_SubMesh* v2smDS = smV2->GetSubMeshDS();
332       list< const SMDS_MeshNode* >::iterator bordIt = bordNodes.begin();
333       bordIt++; // skip nV1
334       for ( ; bordIt != bordNodes.end(); ++bordIt ) {
335         const SMDS_MeshNode* node = *bordIt;
336         if ( v2smDS->Contains( node ))
337           break;
338         if ( node->GetPosition()->GetTypeOfPosition() != SMDS_TOP_EDGE )
339           RETURN_BAD_RESULT("Bad node position type: node " << node->GetID() <<
340                             " pos type " << node->GetPosition()->GetTypeOfPosition());
341         const SMDS_EdgePosition* pos =
342           static_cast<const SMDS_EdgePosition*>(node->GetPosition().get());
343         u2nodes.insert( make_pair( pos->GetUParameter(), node ));
344         seamNodes.insert( node );
345       }
346       if ( u2nodes.size() != seamNodes.size() )
347         RETURN_BAD_RESULT("Bad node params on edge " << sm->GetId() <<
348                           ", " << u2nodes.size() << " != " << seamNodes.size() );
349       return true;
350     }
351     default:;
352     }
353     RETURN_BAD_RESULT ("Unexpected submesh type");
354
355   } // bool getBoundaryNodes()
356
357 } // namespace
358
359 //=======================================================================
360 //function : Compute
361 //purpose  : 
362 //=======================================================================
363
364 bool StdMeshers_Projection_2D::Compute(SMESH_Mesh& theMesh, const TopoDS_Shape& theShape)
365 {
366   if ( !_sourceHypo )
367     return false;
368
369   SMESH_Mesh * srcMesh = _sourceHypo->GetSourceMesh();
370   SMESH_Mesh * tgtMesh = & theMesh;
371   if ( !srcMesh )
372     srcMesh = tgtMesh;
373
374   SMESHDS_Mesh * meshDS = theMesh.GetMeshDS();
375
376   // ---------------------------
377   // Make subshapes association
378   // ---------------------------
379
380   TopoDS_Face tgtFace = TopoDS::Face( theShape.Oriented(TopAbs_FORWARD));
381   TopoDS_Shape srcShape = _sourceHypo->GetSourceFace().Oriented(TopAbs_FORWARD);
382
383   TAssocTool::TShapeShapeMap shape2ShapeMap;
384   TAssocTool::InitVertexAssociation( _sourceHypo, shape2ShapeMap, tgtFace );
385   if ( !TAssocTool::FindSubShapeAssociation( tgtFace, tgtMesh, srcShape, srcMesh,
386                                              shape2ShapeMap)  ||
387        !shape2ShapeMap.IsBound( tgtFace ))
388     return error(COMPERR_BAD_SHAPE,"Topology of source and target faces seems different" );
389
390   TopoDS_Face srcFace = TopoDS::Face( shape2ShapeMap( tgtFace ).Oriented(TopAbs_FORWARD));
391
392   // ----------------------------------------------
393   // Assure that mesh on a source Face is computed
394   // ----------------------------------------------
395
396   SMESH_subMesh* srcSubMesh = srcMesh->GetSubMesh( srcFace );
397   SMESH_subMesh* tgtSubMesh = tgtMesh->GetSubMesh( tgtFace );
398
399   if ( tgtMesh == srcMesh ) {
400     if ( !TAssocTool::MakeComputed( srcSubMesh ))
401       return error(COMPERR_BAD_INPUT_MESH,"Source mesh not computed");
402   }
403   else {
404     if ( !srcSubMesh->IsMeshComputed() )
405       return error(COMPERR_BAD_INPUT_MESH,"Source mesh not computed");
406   }
407
408   // --------------------
409   // Prepare to mapping 
410   // --------------------
411
412   SMESH_MesherHelper helper( theMesh );
413   helper.SetSubShape( tgtFace );
414
415   // Check if node projection to a face is needed
416   Bnd_B2d uvBox;
417   SMDS_ElemIteratorPtr faceIt = srcSubMesh->GetSubMeshDS()->GetElements();
418   int nbFaceNodes = 0;
419   for ( ; nbFaceNodes < 3 && faceIt->more();  ) {
420     const SMDS_MeshElement* face = faceIt->next();
421     SMDS_ElemIteratorPtr nodeIt = face->nodesIterator();
422     while ( nodeIt->more() ) {
423       const SMDS_MeshNode* node = static_cast<const SMDS_MeshNode*>( nodeIt->next() );
424       if ( node->GetPosition()->GetTypeOfPosition() == SMDS_TOP_FACE ) {
425         nbFaceNodes++;
426         uvBox.Add( helper.GetNodeUV( srcFace, node ));
427       }
428     }
429   }
430   const bool toProjectNodes =
431     ( nbFaceNodes > 0 && ( uvBox.IsVoid() || uvBox.SquareExtent() < DBL_MIN ));
432
433   // Load pattern from the source face
434   SMESH_Pattern mapper;
435   mapper.Load( srcMesh, srcFace, toProjectNodes );
436   if ( mapper.GetErrorCode() != SMESH_Pattern::ERR_OK )
437     return error(COMPERR_BAD_INPUT_MESH,"Can't load mesh pattern from the source face");
438
439   // Find the first target vertex corresponding to first vertex of the <mapper>
440   // and <theReverse> flag needed to call mapper.Apply()
441
442   TopoDS_Vertex srcV1 = TopoDS::Vertex( mapper.GetSubShape( 1 ));
443   if ( srcV1.IsNull() )
444     RETURN_BAD_RESULT("Mesh is not bound to the face");
445   if ( !shape2ShapeMap.IsBound( srcV1 ))
446     RETURN_BAD_RESULT("Not associated vertices, srcV1 " << srcV1.TShape().operator->() );
447   TopoDS_Vertex tgtV1 = TopoDS::Vertex( shape2ShapeMap( srcV1 ));
448
449   if ( !TAssocTool::IsSubShape( srcV1, srcFace ))
450     RETURN_BAD_RESULT("Wrong srcV1 " << srcV1.TShape().operator->());
451   if ( !TAssocTool::IsSubShape( tgtV1, tgtFace ))
452     RETURN_BAD_RESULT("Wrong tgtV1 " << tgtV1.TShape().operator->());
453
454   // try to find out orientation by order of edges
455   bool reverse = false;
456   list< TopoDS_Edge > tgtEdges, srcEdges;
457   list< int > nbEdgesInWires;
458   SMESH_Block::GetOrderedEdges( tgtFace, tgtV1, tgtEdges, nbEdgesInWires);
459   SMESH_Block::GetOrderedEdges( srcFace, srcV1, srcEdges, nbEdgesInWires);
460   if ( nbEdgesInWires.front() > 1 ) // possible to find out
461   {
462     TopoDS_Edge srcE1 = srcEdges.front(), tgtE1 = tgtEdges.front();
463     TopoDS_Shape srcE1bis = shape2ShapeMap( tgtE1 );
464     reverse = ( ! srcE1.IsSame( srcE1bis ));
465   }
466   else if ( nbEdgesInWires.front() == 1 )
467   {
468     // TODO::Compare orientation of curves in a sole edge
469     //RETURN_BAD_RESULT("Not implemented case");
470   }
471   else
472   {
473     RETURN_BAD_RESULT("Bad result from SMESH_Block::GetOrderedEdges()");
474   }
475
476   // --------------------
477   // Perform 2D mapping 
478   // --------------------
479
480   // Compute mesh on a target face
481
482   mapper.Apply( tgtFace, tgtV1, reverse );
483   if ( mapper.GetErrorCode() != SMESH_Pattern::ERR_OK )
484     return error("Can't apply source mesh pattern to the face");
485
486   // Create the mesh
487
488   const bool toCreatePolygons = false, toCreatePolyedrs = false;
489   mapper.MakeMesh( tgtMesh, toCreatePolygons, toCreatePolyedrs );
490   if ( mapper.GetErrorCode() != SMESH_Pattern::ERR_OK )
491     return error("Can't make mesh by source mesh pattern");
492
493   // it will remove mesh built by pattern mapper on edges and vertices
494   // in failure case
495   MeshCleaner cleaner( tgtSubMesh );
496
497   // -------------------------------------------------------------------------
498   // mapper doesn't take care of nodes already existing on edges and vertices,
499   // so we must merge nodes created by it with existing ones 
500   // -------------------------------------------------------------------------
501
502   SMESH_MeshEditor editor( tgtMesh );
503   SMESH_MeshEditor::TListOfListOfNodes groupsOfNodes;
504
505   // Make groups of nodes to merge
506
507   // loop on edge and vertex submeshes of a target face
508   SMESH_subMeshIteratorPtr smIt = tgtSubMesh->getDependsOnIterator(false,false);
509   while ( smIt->more() )
510   {
511     SMESH_subMesh*     sm = smIt->next();
512     SMESHDS_SubMesh* smDS = sm->GetSubMeshDS();
513
514     // Sort new and old nodes of a submesh separately
515
516     bool isSeam = helper.IsRealSeam( sm->GetId() );
517
518     enum { NEW_NODES = 0, OLD_NODES };
519     map< double, const SMDS_MeshNode* > u2nodesMaps[2], u2nodesOnSeam;
520     map< double, const SMDS_MeshNode* >::iterator u_oldNode, u_newNode, u_newOnSeam, newEnd;
521     set< const SMDS_MeshNode* > seamNodes;
522
523     // mapper puts on a seam edge nodes from 2 edges
524     if ( isSeam && ! getBoundaryNodes ( sm, tgtFace, u2nodesOnSeam, seamNodes ))
525       RETURN_BAD_RESULT("getBoundaryNodes() failed");
526
527     SMDS_NodeIteratorPtr nIt = smDS->GetNodes();
528     while ( nIt->more() )
529     {
530       const SMDS_MeshNode* node = nIt->next();
531       bool isOld = isOldNode( node );
532
533       if ( !isOld && isSeam ) { // new node on a seam edge
534         if ( seamNodes.find( node ) != seamNodes.end())
535           continue; // node is already in the map
536       }
537
538       // sort nodes on edges by their position
539       map< double, const SMDS_MeshNode* > & pos2nodes = u2nodesMaps[isOld ? OLD_NODES : NEW_NODES];
540       switch ( node->GetPosition()->GetTypeOfPosition() )
541       {
542       case  SMDS_TOP_VERTEX: {
543         pos2nodes.insert( make_pair( 0, node ));
544         break;
545       }
546       case  SMDS_TOP_EDGE:   {
547         const SMDS_EdgePosition* pos =
548           static_cast<const SMDS_EdgePosition*>(node->GetPosition().get());
549         pos2nodes.insert( make_pair( pos->GetUParameter(), node ));
550         break;
551       }
552       default:
553         RETURN_BAD_RESULT("Wrong node position type: "<<
554                           node->GetPosition()->GetTypeOfPosition());
555       }
556     }
557     if ( u2nodesMaps[ NEW_NODES ].size() != u2nodesMaps[ OLD_NODES ].size() )
558     {
559       if ( u2nodesMaps[ NEW_NODES ].size() == 0         &&
560            sm->GetSubShape().ShapeType() == TopAbs_EDGE &&
561            helper.IsDegenShape( sm->GetId() )             )
562         // NPAL15894 (tt88bis.py) - project mesh built by NETGEN_1d_2D that
563         // does not make segments/nodes on degenerated edges
564         continue;
565
566       RETURN_BAD_RESULT("Different nb of old and new nodes on shape #"<< sm->GetId() <<" "<<
567                         u2nodesMaps[ OLD_NODES ].size() << " != " <<
568                         u2nodesMaps[ NEW_NODES ].size());
569     }
570     if ( isSeam && u2nodesMaps[ OLD_NODES ].size() != u2nodesOnSeam.size() ) {
571       RETURN_BAD_RESULT("Different nb of old and seam nodes " <<
572                         u2nodesMaps[ OLD_NODES ].size() << " != " << u2nodesOnSeam.size());
573     }
574     // Make groups of nodes to merge
575     u_oldNode = u2nodesMaps[ OLD_NODES ].begin(); 
576     u_newNode = u2nodesMaps[ NEW_NODES ].begin();
577     newEnd    = u2nodesMaps[ NEW_NODES ].end();
578     u_newOnSeam = u2nodesOnSeam.begin();
579     for ( ; u_newNode != newEnd; ++u_newNode, ++u_oldNode ) {
580       groupsOfNodes.push_back( list< const SMDS_MeshNode* >() );
581       groupsOfNodes.back().push_back( u_oldNode->second );
582       groupsOfNodes.back().push_back( u_newNode->second );
583       if ( isSeam )
584         groupsOfNodes.back().push_back( (u_newOnSeam++)->second );
585     }
586   }
587
588   // Merge
589
590   int nbFaceBeforeMerge = tgtSubMesh->GetSubMeshDS()->NbElements();
591   editor.MergeNodes( groupsOfNodes );
592   int nbFaceAtferMerge = tgtSubMesh->GetSubMeshDS()->NbElements();
593   if ( nbFaceBeforeMerge != nbFaceAtferMerge )
594     return error(COMPERR_BAD_INPUT_MESH, "Probably invalid node parameters on geom faces");
595
596   // ---------------------------
597   // Check elements orientation
598   // ---------------------------
599
600   TopoDS_Face face = tgtFace;
601   if ( !theMesh.IsMainShape( tgtFace ))
602   {
603     // find the main shape
604     TopoDS_Shape mainShape = meshDS->ShapeToMesh();
605     switch ( mainShape.ShapeType() ) {
606     case TopAbs_SHELL:
607     case TopAbs_SOLID: break;
608     default:
609       TopTools_ListIteratorOfListOfShape ancestIt = theMesh.GetAncestors( face );
610       for ( ; ancestIt.More(); ancestIt.Next() ) {
611         TopAbs_ShapeEnum type = ancestIt.Value().ShapeType();
612         if ( type == TopAbs_SOLID ) {
613           mainShape = ancestIt.Value();
614           break;
615         } else if ( type == TopAbs_SHELL ) {
616           mainShape = ancestIt.Value();
617         }
618       }
619     }
620     // find tgtFace in the main solid or shell to know it's true orientation.
621     TopExp_Explorer exp( mainShape, TopAbs_FACE );
622     for ( ; exp.More(); exp.Next() ) {
623       if ( tgtFace.IsSame( exp.Current() )) {
624         face = TopoDS::Face( exp.Current() );
625         break;
626       }
627     }
628   }
629   // Fix orientation
630   if ( SMESH_Algo::IsReversedSubMesh( face, meshDS ))
631   {
632     SMDS_ElemIteratorPtr eIt = meshDS->MeshElements( face )->GetElements();
633     while ( eIt->more() ) {
634       const SMDS_MeshElement* e = eIt->next();
635       if ( e->GetType() == SMDSAbs_Face && !editor.Reorient( e ))
636         RETURN_BAD_RESULT("Pb of SMESH_MeshEditor::Reorient()");
637     }
638   }
639
640   cleaner.Release(); // do not remove mesh
641
642   return true;
643 }
644
645 //=============================================================================
646 /*!
647  * \brief Sets a default event listener to submesh of the source face
648   * \param subMesh - submesh where algo is set
649  *
650  * This method is called when a submesh gets HYP_OK algo_state.
651  * After being set, event listener is notified on each event of a submesh.
652  * Arranges that CLEAN event is translated from source submesh to
653  * the submesh
654  */
655 //=============================================================================
656
657 void StdMeshers_Projection_2D::SetEventListener(SMESH_subMesh* subMesh)
658 {
659   TAssocTool::SetEventListener( subMesh,
660                                 _sourceHypo->GetSourceFace(),
661                                 _sourceHypo->GetSourceMesh() );
662 }