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