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