Salome HOME
0023450: Fields are not displayed in GEOM
[modules/geom.git] / src / GEOMImpl / GEOMImpl_ProjectionDriver.cxx
1 // Copyright (C) 2007-2016  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, or (at your option) any later version.
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 #include <Standard_Stream.hxx>
24
25 #include <GEOMImpl_ProjectionDriver.hxx>
26
27 #include <GEOMImpl_IMirror.hxx>
28 #include <GEOMImpl_IProjection.hxx>
29 #include <GEOMImpl_IProjOnCyl.hxx>
30 #include <GEOMImpl_Types.hxx>
31 #include <GEOM_Function.hxx>
32 #include <GEOMUtils.hxx>
33 #include <GEOMUtils_HTrsfCurve2d.hxx>
34
35 #include <Approx_Curve2d.hxx>
36 #include <Bnd_Box2d.hxx>
37 #include <BndLib_Add2dCurve.hxx>
38 #include <BRep_Tool.hxx>
39 #include <BRepAdaptor_Curve2d.hxx>
40 #include <BRepBuilderAPI_Transform.hxx>
41 #include <BRepBuilderAPI_MakeEdge.hxx>
42 #include <BRepBuilderAPI_MakeFace.hxx>
43 #include <BRepBuilderAPI_MakeVertex.hxx>
44 #include <BRepBuilderAPI_MakeWire.hxx>
45 #include <BRepClass_FaceClassifier.hxx>
46 #include <BRepExtrema_DistShapeShape.hxx>
47 #include <BRepLib.hxx>
48 #include <BRepOffsetAPI_NormalProjection.hxx>
49 #include <BRepTools.hxx>
50 #include <BRepTools_WireExplorer.hxx>
51
52 #include <TopAbs.hxx>
53 #include <TopExp.hxx>
54 #include <TopoDS.hxx>
55 #include <TopoDS_Shape.hxx>
56 #include <TopoDS_Edge.hxx>
57 #include <TopoDS_Face.hxx>
58 #include <TopoDS_Vertex.hxx>
59 #include <TopoDS_Wire.hxx>
60 #include <TopTools_ListIteratorOfListOfShape.hxx>
61
62 #include <GeomAPI_ProjectPointOnSurf.hxx>
63 #include <Geom_Curve.hxx>
64 #include <Geom_CylindricalSurface.hxx>
65 #include <Geom_Plane.hxx>
66 #include <Geom2d_TrimmedCurve.hxx>
67
68 #include <gp_Trsf.hxx>
69 #include <gp_Pnt.hxx>
70 #include <gp_Vec.hxx>
71
72 //=======================================================================
73 //function : GetID
74 //purpose  :
75 //======================================================================= 
76 const Standard_GUID& GEOMImpl_ProjectionDriver::GetID()
77 {
78   static Standard_GUID aProjectionDriver ("FF1BBB70-5D14-4df2-980B-3A668264EA16");
79   return aProjectionDriver; 
80 }
81
82
83 //=======================================================================
84 //function : GEOMImpl_ProjectionDriver
85 //purpose  : 
86 //=======================================================================
87
88 GEOMImpl_ProjectionDriver::GEOMImpl_ProjectionDriver() 
89 {
90 }
91
92 //=======================================================================
93 //function : Execute
94 //purpose  :
95 //======================================================================= 
96 Standard_Integer GEOMImpl_ProjectionDriver::Execute(Handle(TFunction_Logbook)& log) const
97 {
98   if (Label().IsNull())  return 0;    
99   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
100
101   if (aFunction.IsNull()) return 0;
102
103   Standard_Integer aType = aFunction->GetType();
104
105   if (aType == PROJECTION_COPY) {
106     // Projection
107     TopoDS_Shape aShape;
108     gp_Trsf aTrsf;
109
110     GEOMImpl_IMirror TI (aFunction);
111
112     Handle(GEOM_Function) anOriginalFunction = TI.GetOriginal();
113     if (anOriginalFunction.IsNull()) return 0;
114
115     TopoDS_Shape anOriginal = anOriginalFunction->GetValue();
116     if (anOriginal.IsNull()) return 0;
117
118     // Source shape (point, edge or wire)
119     if (anOriginal.ShapeType() != TopAbs_VERTEX &&
120         anOriginal.ShapeType() != TopAbs_EDGE &&
121         anOriginal.ShapeType() != TopAbs_WIRE) {
122       Standard_ConstructionError::Raise
123         ("Projection aborted : the source shape is neither a vertex, nor an edge or a wire");
124     }
125
126     // Target face
127     Handle(GEOM_Function) aTargetFunction = TI.GetPlane();
128     if (aTargetFunction.IsNull()) return 0;
129     TopoDS_Shape aFaceShape = aTargetFunction->GetValue();
130     //if (aFaceShape.IsNull() || aFaceShape.ShapeType() != TopAbs_FACE) {
131     //  Standard_ConstructionError::Raise
132     //    ("Projection aborted : the target shape is not a face");
133     //}
134
135     Standard_Real tol = 1.e-4;        
136
137     if (anOriginal.ShapeType() == TopAbs_VERTEX) {
138       if (aFaceShape.IsNull() || aFaceShape.ShapeType() != TopAbs_FACE) {
139         Standard_ConstructionError::Raise
140           ("Projection aborted : the target shape is not a face");
141       }
142       TopoDS_Face aFace = TopoDS::Face(aFaceShape);
143       Handle(Geom_Surface) surface = BRep_Tool::Surface(aFace);
144       double U1, U2, V1, V2;
145       //surface->Bounds(U1, U2, V1, V2);
146       BRepTools::UVBounds(aFace, U1, U2, V1, V2);
147
148       // projector
149       GeomAPI_ProjectPointOnSurf proj;
150       proj.Init(surface, U1, U2, V1, V2, tol);
151
152       gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(anOriginal));
153       proj.Perform(aPnt);
154       if (!proj.IsDone()) {
155         Standard_ConstructionError::Raise
156           ("Projection aborted : the algorithm failed");
157       }
158       int nbPoints = proj.NbPoints();
159       if (nbPoints < 1) {
160         Standard_ConstructionError::Raise("No solution found");
161       }
162
163       Standard_Real U, V;
164       proj.LowerDistanceParameters(U, V);
165       gp_Pnt2d aProjPnt (U, V);
166
167       // classifier
168       BRepClass_FaceClassifier aClsf (aFace, aProjPnt, tol);
169       if (aClsf.State() != TopAbs_IN && aClsf.State() != TopAbs_ON) {
170         bool isSol = false;
171         double minDist = RealLast();
172         for (int i = 1; i <= nbPoints; i++) {
173           Standard_Real Ui, Vi;
174           proj.Parameters(i, Ui, Vi);
175           aProjPnt = gp_Pnt2d(Ui, Vi);
176           aClsf.Perform(aFace, aProjPnt, tol);
177           if (aClsf.State() == TopAbs_IN || aClsf.State() == TopAbs_ON) {
178             isSol = true;
179             double dist = proj.Distance(i);
180             if (dist < minDist) {
181               minDist = dist;
182               U = Ui;
183               V = Vi;
184             }
185           }
186         }
187         if (!isSol) {
188           Standard_ConstructionError::Raise("No solution found");
189         }
190       }
191
192       gp_Pnt surfPnt = surface->Value(U, V);
193
194       aShape = BRepBuilderAPI_MakeVertex(surfPnt).Shape();
195     }
196     else {
197       //see BRepTest_BasicCommands.cxx for example of BRepOffsetAPI_NormalProjection
198       BRepOffsetAPI_NormalProjection OrtProj (aFaceShape);
199       OrtProj.Add(anOriginal);
200
201       // Compute maximal tolerance of projection.
202       TopExp_Explorer anExp(anOriginal,TopAbs_VERTEX);
203       Standard_Real   aMaxTol = Precision::Confusion();
204
205       for(; anExp.More(); anExp.Next()) { 
206         const TopoDS_Vertex aVtx    = TopoDS::Vertex(anExp.Current());
207         const Standard_Real aCurTol = BRep_Tool::Tolerance(aVtx);
208
209         if (aMaxTol < aCurTol) {
210           aMaxTol = aCurTol;
211         }
212       }
213
214       Standard_Real tol2d = Pow(aMaxTol, 2./3);
215       GeomAbs_Shape Continuity = GeomAbs_C2;
216       Standard_Integer MaxDeg = 14;
217       Standard_Integer MaxSeg = 16;
218
219       OrtProj.SetParams(aMaxTol, tol2d, Continuity, MaxDeg, MaxSeg);
220
221       try {
222         OrtProj.Build();
223       } catch (Standard_Failure) {
224         Handle(Standard_Failure) aFail = Standard_Failure::Caught();
225         TCollection_AsciiString aMsg (aFail->GetMessageString());
226         if (!aMsg.Length())
227           aMsg = "Projection aborted : possibly the source shape intersects the cylinder's axis";
228         Standard_ConstructionError::Raise(aMsg.ToCString());
229       }
230       if (!OrtProj.IsDone()) {
231         Standard_ConstructionError::Raise
232           ("Projection aborted : BRepOffsetAPI_NormalProjection failed");
233       }
234
235       aShape = OrtProj.Shape();
236
237       // check that the result shape is an empty compound
238       // (IPAL22905: TC650: Projection on face dialog problems)
239       if( !aShape.IsNull() && aShape.ShapeType() == TopAbs_COMPOUND )
240       {
241         TopoDS_Iterator anIter( aShape );
242         if( !anIter.More() )
243           Standard_ConstructionError::Raise("Projection aborted : empty compound produced");
244       }
245     }
246
247     if (aShape.IsNull()) return 0;
248
249     aFunction->SetValue(aShape);
250     log->SetTouched(Label());
251   } else if (aType == PROJECTION_ON_WIRE) {
252     // Perform projection of point on a wire or an edge.
253     GEOMImpl_IProjection aProj (aFunction);
254     Handle(GEOM_Function) aPointFunction = aProj.GetPoint();
255     Handle(GEOM_Function) aShapeFunction = aProj.GetShape();
256
257     if (aPointFunction.IsNull() || aShapeFunction.IsNull()) {
258       return 0;
259     }
260
261     TopoDS_Shape aPoint = aPointFunction->GetValue();
262     TopoDS_Shape aShape = aShapeFunction->GetValue();
263
264     if (aPoint.IsNull() || aShape.IsNull()) {
265       return 0;
266     }
267
268     // Check shape types.
269     if (aPoint.ShapeType() != TopAbs_VERTEX) {
270       Standard_ConstructionError::Raise
271         ("Projection aborted : the point is not a vertex");
272     }
273
274     if (aShape.ShapeType() != TopAbs_EDGE &&
275         aShape.ShapeType() != TopAbs_WIRE) {
276       Standard_ConstructionError::Raise
277         ("Projection aborted : the shape is neither an edge nor a wire");
278     }
279
280     // Perform projection.
281     BRepExtrema_DistShapeShape aDistShSh(aPoint, aShape, Extrema_ExtFlag_MIN);
282
283     if (aDistShSh.IsDone() == Standard_False) {
284       Standard_ConstructionError::Raise("Projection not done");
285     }
286
287     Standard_Boolean hasValidSolution = Standard_False;
288     Standard_Integer aNbSolutions     = aDistShSh.NbSolution();
289     Standard_Integer i;
290     double           aParam   = 0.;
291     Standard_Real    aTolConf = BRep_Tool::Tolerance(TopoDS::Vertex(aPoint));
292     Standard_Real    aTolAng  = 1.e-4;        
293
294     for (i = 1; i <= aNbSolutions; i++) {
295       Standard_Boolean        isValid       = Standard_False;
296       BRepExtrema_SupportType aSupportType  = aDistShSh.SupportTypeShape2(i);
297       TopoDS_Shape            aSupportShape = aDistShSh.SupportOnShape2(i);
298
299       if (aSupportType == BRepExtrema_IsOnEdge) {
300         // Minimal distance inside edge is really a projection.
301         isValid = Standard_True;
302         aDistShSh.ParOnEdgeS2(i, aParam);
303       } else if (aSupportType == BRepExtrema_IsVertex) {
304         TopExp_Explorer anExp(aShape, TopAbs_EDGE);
305
306         if (aDistShSh.Value() <= aTolConf) {
307           // The point lies on the shape. This means this point
308           // is really a projection.
309           for (; anExp.More() && !isValid; anExp.Next()) {
310             TopoDS_Edge aCurEdge = TopoDS::Edge(anExp.Current());
311
312             if (aCurEdge.IsNull() == Standard_False) {
313               TopoDS_Vertex aVtx[2];
314                         
315               TopExp::Vertices(aCurEdge, aVtx[0], aVtx[1]);
316
317               for (int j = 0; j < 2; j++) {
318                 if (aSupportShape.IsSame(aVtx[j])) {
319                   // The current edge is a projection edge.
320                   isValid       = Standard_True;
321                   aSupportShape = aCurEdge;
322                   aParam        = BRep_Tool::Parameter(aVtx[j], aCurEdge);
323                   break;
324                 }
325               }
326             }
327           }
328         } else {
329           // Minimal distance to vertex is not always a real projection.
330           gp_Pnt aPnt    = BRep_Tool::Pnt(TopoDS::Vertex(aPoint));
331           gp_Pnt aPrjPnt = BRep_Tool::Pnt(TopoDS::Vertex(aSupportShape));
332           gp_Vec aDProjP(aPrjPnt, aPnt);
333
334           for (; anExp.More() && !isValid; anExp.Next()) {
335             TopoDS_Edge aCurEdge = TopoDS::Edge(anExp.Current());
336  
337             if (aCurEdge.IsNull() == Standard_False) {
338               TopoDS_Vertex aVtx[2];
339                           
340               TopExp::Vertices(aCurEdge, aVtx[0], aVtx[1]);
341  
342               for (int j = 0; j < 2; j++) {
343                 if (aSupportShape.IsSame(aVtx[j])) {
344                   // Check if the point is a projection to the current edge.
345                   Standard_Real      anEdgePars[2];
346                   Handle(Geom_Curve) aCurve =
347                     BRep_Tool::Curve(aCurEdge, anEdgePars[0], anEdgePars[1]);
348                   gp_Pnt             aVal;
349                   gp_Vec             aD1;
350
351                   aParam = BRep_Tool::Parameter(aVtx[j], aCurEdge);
352                   aCurve->D1(aParam, aVal, aD1);
353
354                   if (Abs(aD1.Dot(aDProjP)) <= aTolAng) {
355                     // The current edge is a projection edge.
356                     isValid       = Standard_True;
357                     aSupportShape = aCurEdge;
358                     break;
359                   }
360                 }
361               }
362             }
363           }
364         }
365       }
366       
367
368       if (isValid) {
369         if (hasValidSolution) {
370           Standard_ConstructionError::Raise
371             ("Projection aborted : multiple solutions");
372         }
373
374         // Store the valid solution.
375         hasValidSolution = Standard_True;
376
377         // Normalize parameter.
378         TopoDS_Edge aSupportEdge = TopoDS::Edge(aSupportShape);
379         Standard_Real aF, aL;
380
381         BRep_Tool::Range(aSupportEdge, aF, aL);
382
383         if (Abs(aL - aF) <= aTolConf) {
384           Standard_ConstructionError::Raise
385             ("Projection aborted : degenerated projection edge");
386         }
387
388         aParam = (aParam - aF)/(aL - aF);
389         aProj.SetU(aParam);
390
391         // Compute edge index.
392         TopExp_Explorer anExp(aShape, TopAbs_EDGE);
393         int anIndex = 0;
394
395         for (; anExp.More(); anExp.Next(), anIndex++) {
396           if (aSupportShape.IsSame(anExp.Current())) {
397             aProj.SetIndex(anIndex);
398             break;
399           }
400         }
401
402         if (!anExp.More()) {
403           Standard_ConstructionError::Raise
404             ("Projection aborted : Can't define edge index");
405         }
406
407         // Construct a projection vertex.
408         const gp_Pnt &aPntProj = aDistShSh.PointOnShape2(i);
409         TopoDS_Shape  aProj    = BRepBuilderAPI_MakeVertex(aPntProj).Shape();
410         
411         aFunction->SetValue(aProj);
412       }
413     }
414
415     if (!hasValidSolution) {
416       Standard_ConstructionError::Raise("Projection aborted : no projection");
417     }
418   } else if (aType == PROJECTION_ON_CYLINDER) {
419     GEOMImpl_IProjOnCyl   aProj (aFunction);
420     Handle(GEOM_Function) aShapeFunction = aProj.GetShape();
421       
422     if (aShapeFunction.IsNull()) {
423       return 0;
424     }
425
426     TopoDS_Shape aShape = aShapeFunction->GetValue();
427
428     if (aShape.IsNull()) {
429       return 0;
430     }
431
432     // Get the face.
433     const TopAbs_ShapeEnum aType          = aShape.ShapeType();
434     const Standard_Real    aRadius        = aProj.GetRadius();
435     const Standard_Real    aStartAngle    = aProj.GetStartAngle();
436     const Standard_Real    aLengthAngle   = aProj.GetAngleLength();
437     const Standard_Real    aRotationAngle = aProj.GetAngleRotation();
438
439     if (aType != TopAbs_WIRE && aType != TopAbs_FACE) {
440       return 0;
441     }
442
443     if (aRadius <= Precision::Confusion()) {
444       return 0;
445     }
446
447     TopoDS_Shape aProjShape = projectOnCylinder
448       (aShape, aRadius, aStartAngle, aLengthAngle, aRotationAngle);
449
450     if (aProjShape.IsNull()) {
451       return 0;
452     }
453
454     aFunction->SetValue(aProjShape);
455   }
456
457   return 1;
458 }
459
460 //================================================================================
461 /*!
462  * \brief Returns a name of creation operation and names and values of creation parameters
463  */
464 //================================================================================
465
466 bool GEOMImpl_ProjectionDriver::
467 GetCreationInformation(std::string&             theOperationName,
468                        std::vector<GEOM_Param>& theParams)
469 {
470   if (Label().IsNull()) return 0;
471   Handle(GEOM_Function) function = GEOM_Function::GetFunction(Label());
472
473   Standard_Integer aType = function->GetType();
474
475   theOperationName = "PROJECTION";
476
477   switch ( aType ) {
478   case PROJECTION_COPY:
479   {
480     GEOMImpl_IMirror aCI( function );
481
482     AddParam( theParams, "Source object", aCI.GetOriginal() );
483     AddParam( theParams, "Target face", aCI.GetPlane() );
484     break;
485   }
486   case PROJECTION_ON_WIRE:
487   {
488     GEOMImpl_IProjection aProj (function);
489
490     AddParam(theParams, "Point", aProj.GetPoint());
491     AddParam(theParams, "Shape", aProj.GetShape());
492
493     break;
494   }
495   case PROJECTION_ON_CYLINDER:
496   {
497     theOperationName = "PROJ_ON_CYL";
498
499     GEOMImpl_IProjOnCyl aProj (function);
500     const Standard_Real aLengthAngle = aProj.GetAngleLength();
501
502     AddParam(theParams, "Shape",        aProj.GetShape());
503     AddParam(theParams, "Radius",       aProj.GetRadius());
504     AddParam(theParams, "Start angle",  aProj.GetStartAngle());
505
506     if (aLengthAngle >= 0.) {
507       AddParam(theParams, "Length angle", aLengthAngle);
508     }
509
510     AddParam(theParams, "Rotation angle", aProj.GetAngleRotation());
511
512     break;
513   }
514   default:
515     return false;
516   }
517
518   return true;
519 }
520
521 //================================================================================
522 /*!
523  * \brief Performs projection of a planar wire or a face on a cylinder.
524  */
525 //================================================================================
526
527 TopoDS_Shape GEOMImpl_ProjectionDriver::projectOnCylinder
528                                 (const TopoDS_Shape  &theShape,
529                                  const Standard_Real  theRadius,
530                                  const Standard_Real  theStartAngle,
531                                  const Standard_Real  theAngleLength,
532                                  const Standard_Real  theAngleRotation) const
533 {
534   TopoDS_Shape aResult;
535
536   // Get the face.
537   const TopAbs_ShapeEnum aType = theShape.ShapeType();
538   TopoDS_Face            aFace;
539
540   if (aType == TopAbs_WIRE) {
541     // Try to create a planar face.
542     TopoDS_Wire             aWire = TopoDS::Wire(theShape);
543     BRepBuilderAPI_MakeFace aMkFace(aWire, Standard_True);
544
545     if (aMkFace.IsDone()) {
546       aFace = aMkFace.Face();
547     } else {
548       // Check if the wire is a straight line.
549       TopExp_Explorer anEExp(aWire, TopAbs_EDGE);
550       TopoDS_Edge     anEdge;
551
552       for (; anEExp.More(); anEExp.Next()) {
553         anEdge = TopoDS::Edge(anEExp.Current());
554
555         if (!BRep_Tool::Degenerated(anEdge)) {
556           break;
557         }
558       }
559
560       if (anEExp.More()) {
561         // Not degenerated edge found. Try to create a plane.
562         Standard_Real      aPar[2];
563         Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aPar[0], aPar[1]);
564         gp_Pnt             aP0    = aCurve->Value(aPar[0]);
565         gp_Pnt             aP1    = aCurve->Value(0.5*(aPar[1] + aPar[0]));
566         gp_Vec             aX(aP1.XYZ().Subtracted(aP0.XYZ()));
567         Standard_Real      aTolConf = Precision::Confusion();
568
569         if (aX.Magnitude() > aTolConf) {
570           aX.Normalize();
571
572           // Get the plane normal ortogonal to Z axis.
573           gp_Vec aZ(0., 0., 1.);
574           gp_Vec aN = aX.Crossed(aZ);
575
576           if (aN.Magnitude() <= aTolConf) {
577             // aX is parallel to aZ. Get the plane normal ortogonal to Y axis.
578             gp_Vec aY(0., 1., 0.);
579
580             aN = aX.Crossed(aY);
581           }
582
583           if (aN.Magnitude() >      aTolConf) {
584             gp_Ax3                  anAxis(aP0, gp_Dir(aN), gp_Dir(aX));
585             Handle(Geom_Plane)      aPlane = new Geom_Plane(anAxis);
586             BRepBuilderAPI_MakeFace aMkFace(aPlane, aWire);
587
588             if (aMkFace.IsDone()) {
589               aFace = aMkFace.Face();
590             }
591           }
592         }
593       }
594     }
595   } else if (aType == TopAbs_FACE) {
596     aFace = TopoDS::Face(theShape);
597   }
598
599   if (aFace.IsNull()) {
600     return aResult;
601   }
602
603   // Compute 2d translation transformation.
604   TopoDS_Wire            anOuterWire = BRepTools::OuterWire(aFace);
605   BRepTools_WireExplorer aOWExp(anOuterWire, aFace);
606
607   if (!aOWExp.More()) {
608     // NEVERREACHED
609     return aResult;
610   }
611
612   // Rotate 2D presentation of face.
613   TopoDS_Vertex       aFirstVertex = aOWExp.CurrentVertex();
614   TopoDS_Edge         aFirstEdge   = aOWExp.Current();
615   gp_Pnt              aPnt         = BRep_Tool::Pnt(aFirstVertex);
616   BRepAdaptor_Curve2d anAdaptorCurve(aFirstEdge, aFace);
617   Standard_Real       aParam       =
618     BRep_Tool::Parameter(aFirstVertex, aFirstEdge, aFace);
619   gp_Pnt2d            aPntUV;
620   gp_Vec2d            aVecUV;
621   gp_Vec2d            aVecU0(1., 0);
622
623   anAdaptorCurve.D1(aParam, aPntUV, aVecUV);
624
625   if (aVecUV.Magnitude() <= gp::Resolution()) {
626     return aResult;
627   }
628
629   if (aFirstEdge.Orientation() == TopAbs_REVERSED) {
630     aVecUV.Reverse();
631   }
632
633   const Standard_Real    anAngle    = aVecUV.Angle(aVecU0) + theAngleRotation;
634   const Standard_Boolean isToRotate = Abs(anAngle) > Precision::Angular();
635   gp_Trsf2d              aRotTrsf;
636   Bnd_Box2d              aUVBox;
637   Standard_Real          aPar[2];
638
639   if (isToRotate) {
640     aRotTrsf.SetRotation(aPntUV, anAngle);
641   }
642
643   for (; aOWExp.More(); aOWExp.Next()) {
644     TopoDS_Edge                 anEdge   = aOWExp.Current();
645     Handle(Geom2d_Curve)        aCurve   =
646         BRep_Tool::CurveOnSurface(anEdge, aFace, aPar[0], aPar[1]);
647
648     if (aCurve.IsNull()) {
649       continue;
650     }
651
652     if (isToRotate) {
653       aCurve = Handle(Geom2d_Curve)::DownCast(aCurve->Transformed(aRotTrsf));
654     }
655
656     BndLib_Add2dCurve::Add(aCurve, aPar[0], aPar[1], 0., aUVBox);
657   }
658
659   Standard_Real aU[2];
660   Standard_Real aV[2];
661
662   aUVBox.Get(aU[0], aV[0], aU[1], aV[1]);
663
664   // Compute anisotropic transformation from a face's 2d space
665   // to cylinder's 2d space.
666   GEOMUtils::Trsf2d aTrsf2d
667             (1./theRadius, 0., theStartAngle - aU[0]/theRadius,
668              0.,           1., aPnt.Z() - aPntUV.Y());
669
670   // Compute scaling trsf.
671   const Standard_Boolean isToScale = theAngleLength >= Precision::Angular();
672   gp_Trsf2d aScaleTrsf;
673
674   if (isToScale) {
675     // Perform 2d scaling.
676     gp_Pnt2d            aMidPnt(0.5*(aU[1] + aU[0]), 0.5*(aV[1] + aV[0]));
677     const Standard_Real aScaleFactor = theAngleLength*theRadius/(aU[1] - aU[0]);
678
679     aTrsf2d.TransformD0(aMidPnt);
680
681     aScaleTrsf.SetScale(aMidPnt, aScaleFactor);
682   }
683
684   // Get 2d presentation of a face.
685   Handle(Geom_Surface) aCylinder =
686     new Geom_CylindricalSurface(gp_Ax3(), theRadius);
687   GeomAdaptor_Surface  aGACyl(aCylinder);
688   TopExp_Explorer      anExp(aFace, TopAbs_WIRE);
689   TopTools_ListOfShape aWires;
690   Standard_Real        aUResol = aGACyl.UResolution(Precision::Confusion());
691   Standard_Real        aVResol = aGACyl.VResolution(Precision::Confusion());
692
693   for (; anExp.More(); anExp.Next()) {
694     TopoDS_Wire             aWire = TopoDS::Wire(anExp.Current());
695     BRepTools_WireExplorer  aWExp(aWire, aFace);
696     BRepBuilderAPI_MakeWire aMkWire;
697
698     for (; aWExp.More(); aWExp.Next()) {
699       TopoDS_Edge                 anEdge   = aWExp.Current();
700       Handle(Geom2d_Curve)        aCurve   =
701         BRep_Tool::CurveOnSurface(anEdge, aFace, aPar[0], aPar[1]);
702
703       if (aCurve.IsNull()) {
704         continue;
705       }
706
707       if (isToRotate) {
708         aCurve = Handle(Geom2d_Curve)::DownCast(aCurve->Transformed(aRotTrsf));
709       }
710
711       // Transform the curve to cylinder's parametric space.
712       Handle(GEOMUtils::HTrsfCurve2d) aTrsfCurve =
713         new GEOMUtils::HTrsfCurve2d(aCurve, aPar[0], aPar[1], aTrsf2d);
714       Approx_Curve2d                  aConv (aTrsfCurve, aPar[0], aPar[1],
715                                                          aUResol, aVResol, GeomAbs_C1,
716                                              9, 1000);
717
718       if (!aConv.IsDone() && !aConv.HasResult()) {
719         return aResult;
720       }
721
722       Handle(Geom2d_Curve) aCylCurve = aConv.Curve();
723
724       if (isToScale) {
725         aCylCurve->Transform(aScaleTrsf);
726       }
727
728       // Create edge and add it to the wire.
729       BRepBuilderAPI_MakeEdge aMkEdge(aCylCurve, aCylinder);
730
731       if (!aMkEdge.IsDone()) {
732         return aResult;
733       }
734
735       aMkWire.Add(aMkEdge.Edge());
736
737       if (!aMkWire.IsDone()) {
738         return aResult;
739       }
740     }
741
742     if (aWire.IsSame(anOuterWire)) {
743       // Make the outer wire first.
744       aWires.Prepend(aMkWire.Wire());
745     } else {
746       aWires.Append(aMkWire.Wire());
747     }
748   }
749
750   // Create a face.
751   if (aWires.IsEmpty()) {
752     return aResult;
753   }
754
755   TopTools_ListIteratorOfListOfShape aWIter(aWires);
756   TopoDS_Wire                        aWire = TopoDS::Wire(aWIter.Value());
757   BRepBuilderAPI_MakeFace aMkFace(aCylinder, aWire);
758
759   if (!aMkFace.IsDone()) {
760     return aResult;
761   }
762
763   for (aWIter.Next(); aWIter.More(); aWIter.Next()) {
764     aWire = TopoDS::Wire(aWIter.Value());
765     aMkFace.Add(aWire);
766
767     if (!aMkFace.IsDone()) {
768       return aResult;
769     }
770   }
771
772   // Build 3D curves.
773   TopoDS_Face  aCylFace = aMkFace.Face();
774   TopoDS_Shape aResShape;
775
776   BRepLib::BuildCurves3d(aCylFace);
777
778   // Check shape.
779   if (aType == TopAbs_WIRE) {
780     TopExp_Explorer aResExp(aCylFace, TopAbs_WIRE);
781
782     if (aResExp.More()) {
783       aResShape = aResExp.Current();
784     }
785   } else {
786     aResShape = aCylFace;
787   }
788
789   if (aResShape.IsNull() == Standard_False) {
790     if (!GEOMUtils::CheckShape(aResShape, true)) {
791       if (!GEOMUtils::FixShapeTolerance(aResShape)) {
792         return aResult;
793       }
794     }
795
796     aResult = aResShape;
797   }
798
799   return aResult;
800 }
801
802 IMPLEMENT_STANDARD_RTTIEXT (GEOMImpl_ProjectionDriver,GEOM_BaseDriver);