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