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