0026788: Compiler warnings when OCCT_DEBUG is enabled
[occt.git] / src / Draft / Draft_Modification_1.cxx
1 // Created on: 1994-12-02
2 // Created by: Jacques GOUSSARD
3 // Copyright (c) 1994-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17 #include <Adaptor3d_CurveOnSurface.hxx>
18 #include <Adaptor3d_HCurveOnSurface.hxx>
19 #include <Adaptor3d_SurfaceOfLinearExtrusion.hxx>
20 #include <Approx_CurveOnSurface.hxx>
21 #include <BRep_Builder.hxx>
22 #include <BRep_Tool.hxx>
23 #include <BRepExtrema_ExtCF.hxx>
24 #include <BRepExtrema_ExtPC.hxx>
25 #include <BRepLib_MakeFace.hxx>
26 #include <BRepTools.hxx>
27 #include <Draft_EdgeInfo.hxx>
28 #include <Draft_FaceInfo.hxx>
29 #include <Draft_Modification.hxx>
30 #include <Draft_VertexInfo.hxx>
31 #include <ElCLib.hxx>
32 #include <ElSLib.hxx>
33 #include <Extrema_ExtPC.hxx>
34 #include <Geom2d_BezierCurve.hxx>
35 #include <Geom2d_BSplineCurve.hxx>
36 #include <Geom2d_Curve.hxx>
37 #include <Geom2d_Line.hxx>
38 #include <Geom2d_TrimmedCurve.hxx>
39 #include <Geom2dAdaptor_HCurve.hxx>
40 #include <Geom2dAPI_ProjectPointOnCurve.hxx>
41 #include <Geom2dConvert.hxx>
42 #include <Geom2dConvert_CompCurveToBSplineCurve.hxx>
43 #include <Geom_BSplineCurve.hxx>
44 #include <Geom_Circle.hxx>
45 #include <Geom_Conic.hxx>
46 #include <Geom_ConicalSurface.hxx>
47 #include <Geom_Curve.hxx>
48 #include <Geom_CylindricalSurface.hxx>
49 #include <Geom_ElementarySurface.hxx>
50 #include <Geom_Ellipse.hxx>
51 #include <Geom_Hyperbola.hxx>
52 #include <Geom_Line.hxx>
53 #include <Geom_Parabola.hxx>
54 #include <Geom_Plane.hxx>
55 #include <Geom_RectangularTrimmedSurface.hxx>
56 #include <Geom_Surface.hxx>
57 #include <Geom_SurfaceOfLinearExtrusion.hxx>
58 #include <Geom_TrimmedCurve.hxx>
59 #include <GeomAdaptor_Curve.hxx>
60 #include <GeomAdaptor_HCurve.hxx>
61 #include <GeomAdaptor_HSurface.hxx>
62 #include <GeomAdaptor_Surface.hxx>
63 #include <GeomAPI_ProjectPointOnCurve.hxx>
64 #include <GeomAPI_ProjectPointOnSurf.hxx>
65 #include <GeomConvert_CompCurveToBSplineCurve.hxx>
66 #include <GeomInt_IntSS.hxx>
67 #include <GeomProjLib.hxx>
68 #include <gp.hxx>
69 #include <gp_Circ.hxx>
70 #include <gp_Dir.hxx>
71 #include <gp_Elips.hxx>
72 #include <gp_Hypr.hxx>
73 #include <gp_Lin.hxx>
74 #include <gp_Parab.hxx>
75 #include <gp_Pln.hxx>
76 #include <gp_Pnt.hxx>
77 #include <gp_Vec.hxx>
78 #include <IntAna_IntConicQuad.hxx>
79 #include <IntAna_QuadQuadGeo.hxx>
80 #include <IntCurveSurface_HInter.hxx>
81 #include <IntCurveSurface_IntersectionPoint.hxx>
82 #include <Precision.hxx>
83 #include <ProjLib_CompProjectedCurve.hxx>
84 #include <ProjLib_HCompProjectedCurve.hxx>
85 #include <Standard_ConstructionError.hxx>
86 #include <Standard_DomainError.hxx>
87 #include <Standard_Failure.hxx>
88 #include <Standard_NoSuchObject.hxx>
89 #include <Standard_NotImplemented.hxx>
90 #include <StdFail_NotDone.hxx>
91 #include <TColgp_Array1OfPnt2d.hxx>
92 #include <TopExp.hxx>
93 #include <TopExp_Explorer.hxx>
94 #include <TopLoc_Location.hxx>
95 #include <TopoDS.hxx>
96 #include <TopoDS_Edge.hxx>
97 #include <TopoDS_Face.hxx>
98 #include <TopoDS_Shape.hxx>
99 #include <TopoDS_Vertex.hxx>
100 #include <TopTools_ListIteratorOfListOfShape.hxx>
101 #include <TopTools_MapIteratorOfMapOfShape.hxx>
102 #include <TopTools_MapOfShape.hxx>
103
104 static Standard_Boolean Choose(const Draft_IndexedDataMapOfFaceFaceInfo&,
105   Draft_IndexedDataMapOfEdgeEdgeInfo&,
106   const TopoDS_Vertex&,
107   Draft_VertexInfo&,
108   GeomAdaptor_Curve&,
109   GeomAdaptor_Surface&);
110
111 static Standard_Real Parameter(const Handle(Geom_Curve)&,
112   const gp_Pnt&,
113   Standard_Integer&);
114
115 static Standard_Real SmartParameter(Draft_EdgeInfo&,
116   const Standard_Real EdgeTol,
117   const gp_Pnt&,
118   const Standard_Integer,
119   const Handle(Geom_Surface)&,
120   const Handle(Geom_Surface)&);
121
122 static TopAbs_Orientation Orientation(const TopoDS_Shape&,
123   const TopoDS_Face&);
124
125 static Standard_Boolean FindRotation(const gp_Pln&,
126   const TopAbs_Orientation,
127   const gp_Dir&,
128   const Standard_Real,
129   const gp_Pln&,
130   gp_Ax1&,
131   Standard_Real&);
132
133
134 //=======================================================================
135 //function : InternalAdd
136 //purpose  : 
137 //=======================================================================
138
139 Standard_Boolean Draft_Modification::InternalAdd(const TopoDS_Face& F,
140   const gp_Dir& Direction,
141   const Standard_Real Angle,
142   const gp_Pln& NeutralPlane,
143   const Standard_Boolean Flag)
144 {
145
146   if (myFMap.Contains(F)) {
147     return (badShape.IsNull());
148   }
149
150   TopAbs_Orientation oris = Orientation(myShape,F);
151   TopLoc_Location Lo;
152   //gp_Dir NewDirection = Direction;
153   //Standard_Real NewAngle = Angle;
154   Handle(Geom_Surface) S = BRep_Tool::Surface(F,Lo);
155   S = Handle(Geom_Surface)::DownCast(S->Transformed(Lo.Transformation()));
156   if (S->DynamicType() == STANDARD_TYPE(Geom_RectangularTrimmedSurface)) {
157     S = Handle(Geom_RectangularTrimmedSurface)::DownCast(S)->BasisSurface();
158   }
159   Handle(Geom_Surface) NewS;
160   Handle(Geom_Circle) theCircle;
161
162   Standard_Boolean postponed = (Flag == Standard_False);
163   if (postponed) {
164     Handle(Standard_Type) typS = S->DynamicType();
165     if (typS == STANDARD_TYPE(Geom_CylindricalSurface) ||
166         typS == STANDARD_TYPE(Geom_SurfaceOfLinearExtrusion)) {
167       gp_Circ Cir;
168       if (typS == STANDARD_TYPE(Geom_CylindricalSurface)) {
169         gp_Cylinder cyl = 
170           Handle(Geom_CylindricalSurface)::DownCast(S)->Cylinder();
171         gp_Ax1 axcyl = cyl.Axis();
172         Cir = ElSLib::CylinderVIso( cyl.Position(), cyl.Radius(), 0.);
173         gp_Vec VV(cyl.Location(),NeutralPlane.Location());
174         Cir.Translate(VV.Dot(axcyl.Direction())*axcyl.Direction());
175       }
176       else {
177         Handle(Geom_Curve) Cbas = 
178           Handle(Geom_SurfaceOfLinearExtrusion)::DownCast(S)->BasisCurve();
179         gp_Dir theDirextr = 
180           Handle(Geom_SurfaceOfLinearExtrusion)::DownCast(S)->Direction();
181
182         if (Cbas->IsKind(STANDARD_TYPE(Geom_TrimmedCurve))) {
183           Cbas = Handle(Geom_TrimmedCurve)::DownCast(Cbas)->BasisCurve();
184         }
185         if (Cbas->IsKind(STANDARD_TYPE(Geom_Circle))) {
186           Cir = Handle(Geom_Circle)::DownCast(Cbas)->Circ();
187           gp_Dir dircir = Cir.Axis().Direction();
188           if (!Direction.IsParallel(dircir,Precision::Angular())) {
189             badShape = F;
190             errStat = Draft_FaceRecomputation;
191             return Standard_False;
192           }
193         }
194         else {
195           badShape = F;
196           errStat = Draft_FaceRecomputation;
197           return Standard_False;
198         }
199
200         gp_Ax3 Axis = NeutralPlane.Position();
201         Standard_Real L =
202           gp_Vec(Cir.Location(),Axis.Location()).
203           Dot(Axis.Direction());
204         Standard_Real Cos = theDirextr.Dot(Axis.Direction());
205         gp_Vec VV = ( L / Cos) * theDirextr;
206         Cir.Translate(VV);
207       }
208
209       theCircle = new Geom_Circle(Cir);
210
211     }
212     else {
213       postponed = Standard_False;
214     }
215   }
216
217
218   if (!postponed) {
219     NewS = NewSurface(S,oris,Direction,Angle,NeutralPlane); 
220     if (NewS.IsNull()) {
221       badShape = F;
222       errStat = Draft_FaceRecomputation;
223       return Standard_False;
224     }
225     // To avoid some problems with infinite restrictions
226     const Handle(Standard_Type)& typs = NewS->DynamicType();
227     if (typs == STANDARD_TYPE(Geom_CylindricalSurface) ||
228         typs == STANDARD_TYPE(Geom_ConicalSurface)) {
229       Standard_Real umin,umax,vmin,vmax;
230       BRepTools::UVBounds(F,umin,umax,vmin,vmax);
231       if (!Precision::IsNegativeInfinite(vmin) &&
232           !Precision::IsPositiveInfinite(vmax)) {
233         Standard_Real deltav = 10.*(vmax-vmin);
234         if(typs == STANDARD_TYPE(Geom_CylindricalSurface)) {
235           vmin = vmin - deltav;
236           vmax = vmax + deltav;
237         }
238         else {
239           gp_Cone Co = Handle(Geom_ConicalSurface)::DownCast(NewS)->Cone();
240           Standard_Real Vapex = - Co.RefRadius()/Sin(Co.SemiAngle());
241           if (vmin < Vapex) { // vmax should not exceed Vapex
242             if (vmax + deltav > Vapex) {
243               vmax = Vapex;
244               vmin = vmin - 10.*(vmax - vmin);
245               // JAG debug to avoid apex
246               vmax = vmax-Precision::Confusion();
247             }
248             else {
249               vmin = vmin - deltav;
250               vmax = vmax + deltav;
251             }
252           }
253           else { // Vapex <= vmin < vmax
254             if (vmin - deltav < Vapex) {
255               vmin = Vapex;
256               vmax = vmax + 10.*(vmax - vmin);
257               // JAG debug to avoid apex
258               vmin = vmin+Precision::Confusion();
259             }
260             else {
261               vmin = vmin - deltav;
262               vmax = vmax + deltav;
263             }
264           }
265         }
266         NewS = new Geom_RectangularTrimmedSurface(NewS,0.,2.*M_PI,vmin,vmax);
267       }
268     }
269   }
270
271   if (postponed || S != NewS) {
272     Draft_FaceInfo FI(NewS,Standard_True);
273     FI.RootFace(curFace);
274     myFMap.Add(F,FI);
275     if (postponed) {
276       myFMap.ChangeFromKey(F).ChangeCurve() = theCircle;
277     }
278   }    
279
280   TopExp_Explorer expl(F,TopAbs_EDGE);
281   TopTools_MapOfShape MapOfE;
282   while (expl.More() && badShape.IsNull()) {
283     const TopoDS_Edge& edg = TopoDS::Edge(expl.Current());
284     if (!myEMap.Contains(edg)) {
285       Standard_Boolean addedg  = Standard_False;
286       Standard_Boolean addface = Standard_False;
287       TopoDS_Face OtherF;
288       //if (BRep_Tool::IsClosed(edg,F)) {
289       if (BRepTools::IsReallyClosed(edg,F)) {
290         addedg = Standard_True;
291         addface = Standard_False;
292       }
293       else {
294         // Find the other face containing the edge.
295         TopTools_ListIteratorOfListOfShape it;
296         it.Initialize(myEFMap.FindFromKey(edg));
297         Standard_Integer nbother = 0;
298         while (it.More()) {
299           if (!it.Value().IsSame(F)) {
300             if (OtherF.IsNull()) {
301               OtherF = TopoDS::Face(it.Value());
302             }
303             nbother++;
304           }
305           it.Next();
306         }         
307         if (nbother >=2) {
308           badShape = edg;
309           errStat = Draft_EdgeRecomputation;
310         }
311         else if (! OtherF.IsNull() && 
312                  BRep_Tool::Continuity(edg,F,OtherF) >= GeomAbs_G1) {
313           addface= Standard_True;
314           addedg = Standard_True;
315         }
316         else if (nbother == 0) {
317           //        badShape = F;
318         }
319       }
320       if (addedg) {
321         if (postponed) {
322           myFMap.ChangeFromKey(F).Add(OtherF);
323         }
324         Standard_Real f,l;
325         TopLoc_Location L;
326         Handle(Geom_Curve) C = BRep_Tool::Curve(edg,L,f,l);
327         C = Handle(Geom_Curve)::DownCast(C->Transformed(L));
328         if (C->DynamicType() == STANDARD_TYPE(Geom_TrimmedCurve)) {
329           C = Handle(Geom_TrimmedCurve)::DownCast(C)->BasisCurve();
330         }
331         Handle(Geom_Curve) NewC;
332         Draft_EdgeInfo EInf(Standard_True);     
333         if(postponed) {
334           EInf.Add(F);
335           EInf.Add(OtherF);
336
337           // find fixed point 
338           Handle(Geom_Line) aLocalGeom = Handle(Geom_Line)::DownCast(C);
339           if (aLocalGeom.IsNull()) {
340             badShape = edg;
341             errStat = Draft_EdgeRecomputation;
342           }
343           else {
344             gp_Lin lin = aLocalGeom->Lin();
345             IntAna_IntConicQuad ilipl(lin,NeutralPlane,Precision::Angular());
346             if (ilipl.IsDone() && ilipl.NbPoints() != 0){
347               EInf.Tangent(ilipl.Point(1));
348             }
349             else {
350               badShape = edg;
351               errStat = Draft_EdgeRecomputation;
352             }
353           }
354         }
355         else {
356           NewC = NewCurve(C,S,oris,Direction,Angle,NeutralPlane, Flag);
357           if (NewC.IsNull()) {
358             badShape = edg;
359             errStat = Draft_EdgeRecomputation;
360           }
361         }
362
363         Handle(Geom_TrimmedCurve) T = Handle(Geom_TrimmedCurve)::DownCast(NewC);
364         if (!T.IsNull()) NewC = T->BasisCurve();
365         EInf.ChangeGeometry() = NewC;
366
367         EInf.RootFace(curFace);
368         myEMap.Add(edg,EInf);
369         MapOfE.Add(edg);
370         if (addface) {
371           Standard_Boolean Fl = Flag;
372           Handle(Geom_Surface) alocalSurface = BRep_Tool::Surface(OtherF,Lo);
373           if (alocalSurface->DynamicType() == 
374               STANDARD_TYPE(Geom_RectangularTrimmedSurface)) {
375             alocalSurface = Handle(Geom_RectangularTrimmedSurface)::
376               DownCast(alocalSurface)->BasisSurface();
377           }
378           Handle(Standard_Type) typS = alocalSurface->DynamicType();
379           if (typS == STANDARD_TYPE(Geom_CylindricalSurface) || 
380               typS == STANDARD_TYPE(Geom_SurfaceOfLinearExtrusion)) {
381             if ( myFMap.Contains(F)) {
382               if ( Flag == Standard_False && !postponed) {
383                 Standard_Integer IndToReplace = myFMap.FindIndex(F);
384                 if (IndToReplace) {
385                   Standard_Integer LInd = myFMap.Extent();
386                   TopoDS_Face LF = myFMap.FindKey(LInd);
387                   Draft_FaceInfo LFInfo = myFMap.FindFromIndex(LInd);
388                   myFMap.RemoveLast();
389
390                   if (IndToReplace != LInd)
391                     myFMap.Substitute(IndToReplace, LF, LFInfo);
392                 }
393                 TopTools_MapIteratorOfMapOfShape itm(MapOfE);
394                 for ( ; itm.More(); itm.Next())
395                 {
396                   IndToReplace = myEMap.FindIndex(TopoDS::Edge(itm.Key()));
397                   if ( IndToReplace )
398                   {
399                     Standard_Integer LInd = myEMap.Extent();
400                     TopoDS_Edge LE = myEMap.FindKey(LInd);
401                     Draft_EdgeInfo LEInfo = myEMap.FindFromIndex(LInd);
402                     myEMap.RemoveLast();
403
404                     if (IndToReplace != LInd)
405                       myEMap.Substitute(IndToReplace, LE, LEInfo);
406                   }
407                 }
408               }
409             }
410           } 
411           InternalAdd(OtherF,Direction,Angle,NeutralPlane, Fl);
412         }
413       }
414     }
415     expl.Next();
416   }
417   return (badShape.IsNull());
418 }
419
420
421 //=======================================================================
422 //function : Propagate
423 //purpose  : 
424 //=======================================================================
425
426 Standard_Boolean Draft_Modification::Propagate ()
427 {
428
429   if (!badShape.IsNull()) return Standard_False;
430
431   // Set all edges and vertices of modified faces
432   TopoDS_Face F;
433   TopoDS_Edge E;
434   TopoDS_Vertex V;
435   TopExp_Explorer editer;
436   TopExp_Explorer vtiter;
437
438   for (Standard_Integer i = 1; i <= myFMap.Extent(); i++)
439   {
440     const TopoDS_Face& Fc = myFMap.FindKey(i);
441
442     // Exploration of the edges of the face
443     editer.Init(Fc,TopAbs_EDGE);
444     while (editer.More()) {
445       E = TopoDS::Edge(editer.Current());
446
447       if (!myEMap.Contains(E)) {
448         Draft_EdgeInfo EInf(Standard_True);     
449         myEMap.Add(E,EInf);
450       }
451       myEMap.ChangeFromKey(E).Add(Fc);
452
453       // Exploration of the vertices of the edge
454       vtiter.Init(E,TopAbs_VERTEX);
455       while (vtiter.More()) {
456         V = TopoDS::Vertex(vtiter.Current());
457         if (!myVMap.Contains(V)) {
458           Draft_VertexInfo VInf;
459           myVMap.Add(V,VInf);
460         }
461
462         myVMap.ChangeFromKey(V).Add(E);
463         myVMap.ChangeFromKey(V).ChangeParameter(E) = BRep_Tool::Parameter(V, E);
464         vtiter.Next();
465       }
466       editer.Next();
467     }
468   }
469
470
471   TopExp_Explorer anc;
472   Standard_Boolean found;
473
474   // Set edges containing modified vertices.
475
476   for (Standard_Integer i = 1; i <= myVMap.Extent(); i++)
477   {
478     const TopoDS_Vertex& Vt = myVMap.FindKey(i);
479
480     // Exploration of the ancestors of the vertex
481     anc.Init(myShape,TopAbs_EDGE);
482
483     while (anc.More()) {
484       E = TopoDS::Edge(anc.Current());
485       vtiter.Init(E,TopAbs_VERTEX);
486       found = Standard_False;
487       while (vtiter.More()) {
488         if (Vt.IsSame(TopoDS::Vertex(vtiter.Current()))) {
489           found = Standard_True;
490           break;
491         }
492         vtiter.Next();
493       }
494       if (found) {
495         if (!myEMap.Contains(E)) {
496           Draft_EdgeInfo EInf(Standard_False);
497           myEMap.Add(E,EInf);
498         }
499         myVMap.ChangeFromKey(Vt).Add(E);
500         myVMap.ChangeFromKey(Vt).ChangeParameter(E) = BRep_Tool::Parameter(Vt, E);
501       }
502       anc.Next();
503     }
504   }
505
506
507   // Set faces containing modified edges
508   for (Standard_Integer i = 1; i <= myEMap.Extent(); i++)
509   {
510     const TopoDS_Edge& Ed = myEMap.FindKey(i);
511     TopTools_ListIteratorOfListOfShape it;
512     for (it.Initialize(myEFMap.FindFromKey(Ed)); it.More(); it.Next()) {
513       F = TopoDS::Face(it.Value());
514       if (!myFMap.Contains(F)) {
515         TopLoc_Location L;
516         Handle(Geom_Surface) S = BRep_Tool::Surface(F,L);
517         Handle(Geom_Surface) NewS = 
518           Handle(Geom_Surface)::DownCast(S->Transformed(L.Transformation()));
519
520         const Handle(Standard_Type)& typs = S->DynamicType();
521         if (typs == STANDARD_TYPE(Geom_CylindricalSurface) ||
522             typs == STANDARD_TYPE(Geom_ConicalSurface)) {
523           Standard_Real umin,umax,vmin,vmax;
524           BRepTools::UVBounds(F,umin,umax,vmin,vmax);
525           if (!Precision::IsNegativeInfinite(vmin) &&
526               !Precision::IsPositiveInfinite(vmax)) {
527             Standard_Real deltav = 10.*(vmax-vmin);
528             vmin = vmin - deltav;
529             vmax = vmax + deltav;
530             NewS = new Geom_RectangularTrimmedSurface(NewS,0.,2.*M_PI,vmin,vmax);
531           }
532         }
533
534         Draft_FaceInfo FInf(NewS,Standard_False);
535         myFMap.Add(F,FInf);
536       }
537       myEMap.ChangeFromKey(Ed).Add(F);
538     }
539   }
540
541   //  Try to add faces for free borders...
542   // JAG 09.11.95
543   for (Standard_Integer i = 1; i <= myEMap.Extent(); i++)
544   {
545     Draft_EdgeInfo& Einf = myEMap.ChangeFromIndex(i);
546     if (Einf.NewGeometry() && 
547         Einf.Geometry().IsNull() &&     
548         Einf.SecondFace().IsNull()) {
549
550       TopLoc_Location Loc;
551       Handle(Geom_Surface) S1 = BRep_Tool::Surface(Einf.FirstFace(),Loc);
552       S1 = Handle(Geom_Surface)::
553         DownCast(S1->Transformed(Loc.Transformation()));
554       Handle(Geom_Surface) S2;
555
556       Standard_Real f,l;
557       const TopoDS_Edge& EK = myEMap.FindKey(i);
558       Handle(Geom_Curve) C = BRep_Tool::Curve(EK,Loc,f,l);
559       C = Handle(Geom_Curve)::DownCast(C->Transformed(Loc.Transformation()));
560       if (C->DynamicType() == STANDARD_TYPE(Geom_TrimmedCurve)) {
561         C = Handle(Geom_TrimmedCurve)::DownCast(C)->BasisCurve();
562       }
563       if (!S1->IsKind(STANDARD_TYPE(Geom_Plane))) {
564         if (C->IsKind(STANDARD_TYPE(Geom_Conic))) {
565           gp_Ax3 thePl(Handle(Geom_Conic)::DownCast(C)->Position());
566           S2 = new Geom_Plane(thePl);
567         }
568         else if (C->DynamicType() == STANDARD_TYPE(Geom_Line)) {
569           gp_Ax1 axis;
570           if (S1->DynamicType()== STANDARD_TYPE(Geom_RectangularTrimmedSurface)) {
571             axis = Handle(Geom_ElementarySurface)::DownCast
572               (Handle(Geom_RectangularTrimmedSurface)::DownCast(S1)->
573               BasisSurface())->Axis();
574           }
575           else {
576             axis = Handle(Geom_ElementarySurface)::DownCast(S1)->Axis();
577           }
578           gp_Vec they(axis.Location(), C->Value(0.));
579           gp_Dir axz(axis.Direction().Crossed(they));
580           S2=new Geom_Plane(gp_Ax3(axis.Location(),axz,axis.Direction()));
581
582         }
583         else {
584           badShape = EK;
585           errStat = Draft_EdgeRecomputation;
586           break; // leave from for
587         }
588       }
589       else { // on the plane
590         for (Standard_Integer j = 1; j <= myVMap.Extent(); j++)
591         {
592           Draft_VertexInfo& Vinf = myVMap.ChangeFromIndex(j);
593           for (Vinf.InitEdgeIterator();Vinf.MoreEdge();Vinf.NextEdge()) {
594             if (Vinf.Edge().IsSame(EK)) {
595               break;
596             }
597           }
598           if (Vinf.MoreEdge()) {
599             for (Vinf.InitEdgeIterator();Vinf.MoreEdge();Vinf.NextEdge()) {
600               const TopoDS_Edge& edg = Vinf.Edge();
601               if (!edg.IsSame(EK)) {
602                 const Draft_EdgeInfo& EI = myEMap.FindFromKey(edg);
603                 if (!EI.FirstFace().IsSame(Einf.FirstFace()) &&
604                   (EI.SecondFace().IsNull() || 
605                   !EI.SecondFace().IsSame(Einf.FirstFace()))) {
606                     break;
607                 }
608               }
609             }
610             if (Vinf.MoreEdge()) {
611               Handle(Geom_Curve) C2 = BRep_Tool::Curve(Vinf.Edge(), Loc,f,l);
612               Handle(GeomAdaptor_HCurve) HCur;
613               gp_Vec Direc;
614               C2 = Handle(Geom_Curve)::DownCast
615                 (C2->Transformed(Loc.Transformation()));
616               if (C2->DynamicType() == STANDARD_TYPE(Geom_TrimmedCurve)) {
617                 C2 = Handle(Geom_TrimmedCurve)::DownCast(C2)->BasisCurve();
618               }
619               if (C->DynamicType() == STANDARD_TYPE(Geom_Line)) {
620                 Direc = Handle(Geom_Line)::DownCast(C)->Lin().Direction();
621                 HCur = new GeomAdaptor_HCurve(C2);
622
623               }
624               else if (C2->DynamicType() == STANDARD_TYPE(Geom_Line)) {
625                 Direc = Handle(Geom_Line)::DownCast(C2)->Lin().Direction();
626                 HCur = new GeomAdaptor_HCurve(C);
627               }
628               else {
629                 badShape = EK;
630                 errStat = Draft_EdgeRecomputation;
631                 break; // leave from while
632               }
633               Adaptor3d_SurfaceOfLinearExtrusion SLE(HCur,Direc);
634               switch(SLE.GetType()){
635
636               case GeomAbs_Plane :
637                 {
638                   S2 = new Geom_Plane(SLE.Plane());
639                 }
640                 break;
641               case GeomAbs_Cylinder :
642                 {
643                   S2 =   new Geom_CylindricalSurface(SLE.Cylinder());
644                 }
645                 break;
646               default :
647                 {
648                   S2 = new Geom_SurfaceOfLinearExtrusion(HCur->ChangeCurve().
649                     Curve(),
650                     Direc);
651                 }
652                 break;
653               }
654
655             }
656             else {
657               badShape = EK;
658               errStat = Draft_EdgeRecomputation;
659               break; // leave from while
660             }
661             break;
662           }
663           //j++;
664         }
665       }
666
667       if (badShape.IsNull()) {
668         BRep_Builder B;
669         TopoDS_Face TheNewFace;
670         B.MakeFace(TheNewFace,S2,Precision::Confusion());
671         Einf.Add(TheNewFace);
672         Draft_FaceInfo FI(S2,Standard_False);
673         myFMap.Add(TheNewFace,FI);
674       }
675       else {
676         break; // leave from for
677       }
678       // Fin JAG 09.11.95
679     }
680   }
681   return (badShape.IsNull());
682 }
683
684
685
686
687 //=======================================================================
688 //function : Perform
689 //purpose  : 
690 //=======================================================================
691
692 void Draft_Modification::Perform ()
693 {
694   if (!badShape.IsNull())  Standard_ConstructionError::Raise();
695
696   if (!myComp) {
697     myComp = Standard_True;
698     if (!Propagate()) {
699       return;
700     }
701
702     // Calculate eventual faces
703
704     for (Standard_Integer i = 1; i <= myFMap.Extent(); i++) 
705     {
706       const TopoDS_Face& FK = myFMap.FindKey(i);
707       Draft_FaceInfo& Finf = myFMap.ChangeFromIndex(i);
708       if (Finf.NewGeometry() && Finf.Geometry().IsNull()) {
709         const TopoDS_Face& F1 = Finf.FirstFace();
710         const TopoDS_Face& F2 = Finf.SecondFace();
711
712         if (F1.IsNull() || F2.IsNull()) {
713           errStat = Draft_FaceRecomputation;
714           badShape = FK;
715           return;
716         }
717         Handle(Geom_Surface) S1 = myFMap.FindFromKey(F1).Geometry();
718         Handle(Geom_Surface) S2 = myFMap.FindFromKey(F2).Geometry();
719         if (S1.IsNull() || S2.IsNull()) {
720           errStat = Draft_FaceRecomputation;
721           badShape = FK;
722           return;
723         }
724         if (S1->DynamicType() == STANDARD_TYPE(Geom_RectangularTrimmedSurface)) {
725           S1 = Handle(Geom_RectangularTrimmedSurface)::
726             DownCast(S1)->BasisSurface();
727         }
728         if (S2->DynamicType() == STANDARD_TYPE(Geom_RectangularTrimmedSurface)) {
729           S2 = Handle(Geom_RectangularTrimmedSurface)::
730             DownCast(S2)->BasisSurface();
731         }
732         Handle(Geom_Plane) P1 = Handle(Geom_Plane)::DownCast(S1);
733         Handle(Geom_Plane) P2 = Handle(Geom_Plane)::DownCast(S2);
734         if (P1.IsNull() || P2.IsNull()) {
735           errStat = Draft_FaceRecomputation;
736           badShape = FK;
737           return;
738         }
739         gp_Pln pp1 = P1->Pln();
740         gp_Pln pp2 = P2->Pln();
741         IntAna_QuadQuadGeo i2p(pp1,pp2,
742           Precision::Angular(),Precision::Confusion());
743         if (!i2p.IsDone() || i2p.TypeInter() != IntAna_Line) {
744           errStat = Draft_FaceRecomputation;
745           badShape = FK;
746           return;
747         }
748
749         gp_Dir extrdir = i2p.Line(1).Direction();
750
751         // Preserve the same direction as the base face
752         Handle(Geom_Surface) RefSurf = 
753           BRep_Tool::Surface(FK);
754         if (RefSurf->DynamicType() == STANDARD_TYPE(Geom_RectangularTrimmedSurface)) {
755           RefSurf = 
756             Handle(Geom_RectangularTrimmedSurface)::DownCast(RefSurf)
757             ->BasisSurface();
758         }
759         gp_Dir DirRef;
760
761         if ( RefSurf->DynamicType() == STANDARD_TYPE(Geom_CylindricalSurface)) {
762           gp_Ax3 AxeRef = 
763             Handle(Geom_CylindricalSurface)::DownCast(RefSurf)
764             ->Cylinder().Position();
765           DirRef = AxeRef.Direction();
766         }
767         else if (RefSurf->DynamicType() == 
768                  STANDARD_TYPE(Geom_SurfaceOfLinearExtrusion)) {
769           DirRef = 
770             Handle(Geom_SurfaceOfLinearExtrusion)::DownCast(RefSurf)->Direction();
771         }
772
773         if (extrdir.Dot(DirRef) < 0.) extrdir.Reverse();
774
775         // it is possible to accelerate speed by storing the info during
776         // InternalAdd --> modification of FaceInfo to preserve the circle
777
778         Handle(Geom_Circle) CCir = 
779           Handle(Geom_Circle)::DownCast(Finf.Curve());
780         Handle(Geom_Surface) NewS = 
781           new Geom_SurfaceOfLinearExtrusion(CCir, extrdir);    
782
783         Standard_Real umin, umax, vmin, vmax;
784         BRepTools::UVBounds(FK,umin,umax,vmin,vmax);
785         if (!Precision::IsNegativeInfinite(vmin) &&
786             !Precision::IsPositiveInfinite(vmax)) {
787           Standard_Real deltav = 2.*(vmax-vmin);
788           vmin = vmin - deltav;
789           vmax = vmax + deltav;
790         }
791
792         // very temporary
793         else {
794           vmax = 300;
795           vmin = -300;
796         }
797
798         NewS = new Geom_RectangularTrimmedSurface(NewS,0.,1.9*M_PI,vmin,vmax);
799         Finf.ChangeGeometry() = NewS;
800       }
801     }
802
803     // Calculate new edges.
804
805     Handle(Geom_Surface) S1,S2;
806     Handle(Geom_Curve) C, newC;
807     Standard_Real f,l;
808     TopLoc_Location L;
809
810     for (Standard_Integer ii = 1; ii <= myEMap.Extent(); ii++) 
811     {
812       Draft_EdgeInfo& Einf = myEMap.ChangeFromIndex(ii); 
813
814       const TopoDS_Edge& theEdge = TopoDS::Edge(myEMap.FindKey(ii));
815
816       C = BRep_Tool::Curve(theEdge,L,f,l);
817       C = Handle(Geom_Curve)::DownCast(C->Transformed(L.Transformation()));
818
819       if (Einf.NewGeometry() && Einf.Geometry().IsNull()) {
820         gp_Pnt ptfixe;
821         if (!Einf.IsTangent(ptfixe)) {
822           const TopoDS_Face& FirstFace = Einf.FirstFace();
823           const TopoDS_Face& SecondFace = Einf.SecondFace();
824
825           S1 = myFMap.FindFromKey(FirstFace).Geometry();
826           S2 = myFMap.FindFromKey(SecondFace).Geometry();
827
828           Standard_Integer detrompeur = 0;
829
830           // Return FirstVertex and the tangent at this point.
831           TopoDS_Vertex FV = TopExp::FirstVertex(theEdge);
832           TopoDS_Vertex LV = TopExp::LastVertex(theEdge);
833           Standard_Real pmin = 0.;
834           Standard_Real prmfv = BRep_Tool::Parameter(FV, theEdge);
835           Standard_Real prmlv = BRep_Tool::Parameter(LV, theEdge);
836           gp_Pnt pfv, plv;
837           gp_Vec d1fv,d1lv, newd1;
838           C->D1(prmfv,pfv,d1fv);
839           C->D1(prmlv,plv,d1lv);
840
841           Standard_Real TolF1 = BRep_Tool::Tolerance (FirstFace);
842           Standard_Real TolF2 = BRep_Tool::Tolerance (SecondFace);
843
844           //Pass the tolerance of the face to project
845           GeomAPI_ProjectPointOnSurf proj1 (pfv, S1, TolF1);
846           GeomAPI_ProjectPointOnSurf proj2 (plv, S1, TolF1);
847           GeomAPI_ProjectPointOnSurf proj3 (pfv, S2, TolF2);
848           GeomAPI_ProjectPointOnSurf proj4 (plv, S2, TolF2);
849
850           if (proj1.IsDone () && proj2.IsDone ()) {
851             if(proj1.LowerDistance()<= Precision::Confusion() &&
852                proj2.LowerDistance()<= Precision::Confusion())  {
853                detrompeur = 1;
854             }
855           }
856
857           if (proj3.IsDone () && proj4.IsDone ()) {
858             if(proj3.LowerDistance() <= Precision::Confusion() &&
859                proj4.LowerDistance() <= Precision::Confusion())  {
860                detrompeur = 2;
861             }
862           }
863
864           gp_Dir TheDirExtr;
865           gp_Ax3 Axis;
866           Handle(Geom_Curve) TheNewCurve;
867           Standard_Boolean KPart = Standard_False;
868
869           if ( S1->DynamicType() == STANDARD_TYPE(Geom_RectangularTrimmedSurface)) {
870             S1 = Handle(Geom_RectangularTrimmedSurface)::
871               DownCast(S1)->BasisSurface();
872           }
873           if ( S2->DynamicType() == STANDARD_TYPE(Geom_RectangularTrimmedSurface)) {
874             S2 = Handle(Geom_RectangularTrimmedSurface)::
875               DownCast(S2)->BasisSurface();
876           }
877
878           Standard_Boolean PC1 = Standard_True; // KPart on S1
879           if (S1->DynamicType() == STANDARD_TYPE(Geom_SurfaceOfLinearExtrusion) &&
880               S2->DynamicType() == STANDARD_TYPE(Geom_Plane) ) {
881             KPart = Standard_True;
882             Axis = Handle(Geom_Plane)::DownCast(S2)->Position();
883             TheNewCurve = Handle(Geom_SurfaceOfLinearExtrusion)::
884               DownCast(S1)->BasisCurve();
885             TheDirExtr = Handle(Geom_SurfaceOfLinearExtrusion)::
886               DownCast(S1)->Direction();
887           }
888           else if (S2->DynamicType() == STANDARD_TYPE(Geom_SurfaceOfLinearExtrusion) &&
889                    S1->DynamicType() == STANDARD_TYPE(Geom_Plane) ) {
890             KPart = Standard_True;
891             PC1 = Standard_False;
892             Axis = Handle(Geom_Plane)::DownCast(S1)->Position();
893             TheNewCurve = Handle(Geom_SurfaceOfLinearExtrusion)::
894               DownCast(S2)->BasisCurve();
895             TheDirExtr = Handle(Geom_SurfaceOfLinearExtrusion)::
896               DownCast(S2)->Direction();
897           }
898           Handle(Geom_Circle) aCirc ;
899           if ( KPart) {  // very temporary on circles !!!
900             aCirc = Handle(Geom_Circle)::DownCast(TheNewCurve);
901             if (aCirc.IsNull())
902               KPart = Standard_False;
903             else
904             {
905               gp_Dir AxofCirc = aCirc->Position().Direction();
906               if (AxofCirc.IsParallel(Axis.Direction(),Precision::Angular()))
907                 KPart = Standard_True;
908               else
909                 KPart = Standard_False;
910             }
911           }
912
913           Standard_Integer imin;
914           GeomInt_IntSS i2s;
915           if ( KPart) {
916             // direct calculation of NewC
917             Standard_Real aLocalReal =
918               gp_Vec(aCirc->Circ().Location(),Axis.Location()).
919               Dot(Axis.Direction());
920             Standard_Real Cos = TheDirExtr.Dot(Axis.Direction());
921             gp_Vec VV = ( aLocalReal / Cos) * TheDirExtr;
922             newC = Handle(Geom_Curve)::DownCast(TheNewCurve->Translated(VV));
923             // it is possible to calculate PCurve
924             Handle(Geom2d_Line) L2d 
925               = new Geom2d_Line(gp_Pnt2d(0.,aLocalReal/Cos),
926               gp::DX2d());
927
928             if ( PC1) 
929               Einf.ChangeFirstPC() = L2d;
930             else
931               Einf.ChangeSecondPC() = L2d;
932           }
933           else {
934             S1 = myFMap.FindFromKey(Einf.FirstFace()).Geometry();
935             S2 = myFMap.FindFromKey(Einf.SecondFace()).Geometry();
936
937
938             // PCurves are not calculated immediately for 2 reasons:
939             // 1 - If ProjLib should make an Approx, it is stupid to approximate the 
940             //     entire intersection curve.
941             // 2 - Additionally, if YaRev, there is a risk to not be SameRange.
942             i2s.Perform(S1,S2,Precision::Confusion(),
943               Standard_True,Standard_False,Standard_False);
944
945             if (!i2s.IsDone() || i2s.NbLines() <= 0) {
946               errStat = Draft_EdgeRecomputation;
947               badShape = theEdge;
948               return;
949             }
950
951             Standard_Real Dist2, Dist2Min = 0., Glob2Min = RealLast();
952             GeomAdaptor_Curve TheCurve;
953
954             Standard_Integer i,j; //,jmin;
955
956             if (i2s.Line(1)->DynamicType() != STANDARD_TYPE(Geom_BSplineCurve))
957             {
958               imin = 0;
959               for (i=1; i<= i2s.NbLines(); i++) {
960                 TheCurve.Load(i2s.Line(i));
961                 Extrema_ExtPC myExtPC(pfv,TheCurve);
962
963                 Standard_Real locpmin = 0.;
964                 if (myExtPC.IsDone()) {
965                   if(myExtPC.NbExt() >= 1) {
966                     Dist2Min = myExtPC.SquareDistance(1);
967                     locpmin = myExtPC.Point(1).Parameter();
968                   }
969                   if(myExtPC.NbExt() == 2 && Dist2Min > Precision::SquareConfusion()) {
970                     //to avoid incorrectly choosing the image 
971                     //of the first vertex of the initial edge
972                     Standard_Real d1_2 = myExtPC.SquareDistance(1);
973                     Standard_Real d2_2 = myExtPC.SquareDistance(2);
974                     if(d1_2 > 1.21*d2_2) {
975                       Dist2Min = myExtPC.SquareDistance(2);
976                       locpmin = myExtPC.Point(2).Parameter();
977                     }
978                     else if(d2_2 > 1.21*d1_2) {
979                       Dist2Min = myExtPC.SquareDistance(1);
980                       locpmin = myExtPC.Point(1).Parameter();
981                     }
982                     else {
983                       Standard_Real pfvpar = myExtPC.Point(1).Parameter();
984                       Standard_Real plvpar = myExtPC.Point(2).Parameter();
985                       newC = i2s.Line(i);
986
987                       gp_Pnt pfvprim, plvprim;
988
989                       newC->D0(pfvpar,pfvprim);
990                       newC->D0(plvpar,plvprim);
991
992                       Handle(Geom_Surface) theSurf;
993                       if(detrompeur == 1) {
994                         if(S1->DynamicType() == 
995                            STANDARD_TYPE(Geom_RectangularTrimmedSurface))  
996                           S1 = Handle(Geom_RectangularTrimmedSurface)::
997                           DownCast(S1)->BasisSurface();
998                         theSurf = S1;
999
1000                       }
1001                       else if(detrompeur == 2) {
1002                         if(S2->DynamicType() == 
1003                            STANDARD_TYPE(Geom_RectangularTrimmedSurface)) 
1004                           S2 = Handle(Geom_RectangularTrimmedSurface)::
1005                           DownCast(S2)->BasisSurface();
1006                         theSurf = S2;               
1007                       }
1008                       if(detrompeur != 0 && detrompeur != 4) {
1009                         Standard_Real ul = 0., vl = 0., uf = 0., vf = 0.;
1010                         Standard_Real ufprim = 0., ulprim = 0., vfprim = 0., vlprim = 0.;
1011
1012                         if(theSurf->DynamicType() == STANDARD_TYPE(Geom_Plane)) {       
1013                           gp_Pln pl = Handle(Geom_Plane)::DownCast(S2)->Pln();
1014                           ElSLib::Parameters(pl, plv, ul, vl);
1015                           ElSLib::Parameters(pl, pfv, uf, vf);
1016                           ElSLib::Parameters(pl, plvprim, ulprim, vlprim);
1017                           ElSLib::Parameters(pl, pfvprim, ufprim, vfprim);
1018                         }
1019                         else if(theSurf->DynamicType() == 
1020                                 STANDARD_TYPE(Geom_CylindricalSurface)) {
1021                           gp_Cylinder cy = Handle(Geom_CylindricalSurface)
1022                             ::DownCast(S2)->Cylinder();
1023                           ElSLib::Parameters(cy, plv, ul, vl);
1024                           ElSLib::Parameters(cy, pfv, uf, vf);
1025                           ElSLib::Parameters(cy, plvprim, ulprim, vlprim);
1026                           ElSLib::Parameters(cy, pfvprim, ufprim, vfprim);
1027                         }
1028                         else detrompeur = 4;
1029
1030                         if(detrompeur == 1 || detrompeur == 2) {
1031                           gp_Vec2d v1((ul-ufprim), (vl-vfprim));
1032                           gp_Vec2d norm((vf-vfprim), (ufprim-uf)); 
1033                           gp_Vec2d v2((ulprim-ufprim), (vlprim-vfprim));
1034                           if( (v1.Dot(norm))*(v2.Dot(norm)) < 0) {
1035                             Dist2Min = myExtPC.SquareDistance(2);
1036                             locpmin = myExtPC.Point(2).Parameter();
1037                           }
1038                         }
1039                       }
1040                     }
1041                   }
1042                   if (myExtPC.NbExt() == 1 || myExtPC.NbExt() > 2 || detrompeur ==4) {
1043                     Dist2Min = myExtPC.SquareDistance(1);
1044                     locpmin = myExtPC.Point(1).Parameter();
1045                     for (j=2; j<=myExtPC.NbExt(); j++) {
1046                       Dist2 = myExtPC.SquareDistance(j);
1047                       if (Dist2 < Dist2Min) {
1048                         Dist2Min = Dist2;
1049                         locpmin = myExtPC.Point(j).Parameter();
1050                       }
1051                     }
1052                   }
1053                   else if(myExtPC.NbExt() < 1){
1054                     Standard_Real dist1_2,dist2_2;
1055                     gp_Pnt p1b,p2b;
1056                     myExtPC.TrimmedSquareDistances(dist1_2,dist2_2,p1b,p2b);
1057                     if (dist1_2 < dist2_2) {
1058                       Dist2Min = dist1_2;
1059                       locpmin = TheCurve.FirstParameter();
1060                     }
1061                     else {
1062                       Dist2Min = dist2_2;
1063                       locpmin = TheCurve.LastParameter();
1064                     }
1065                   }
1066
1067                   if (Dist2Min  < Glob2Min) {
1068                     Glob2Min = Dist2Min;
1069                     imin = i;
1070                     pmin = locpmin;
1071                   }
1072                 }
1073               }
1074               if (imin == 0) {
1075                 errStat = Draft_EdgeRecomputation;
1076                 badShape = theEdge;
1077                 return;
1078               }
1079
1080               newC = i2s.Line(imin);
1081
1082               newC->D1(pmin,pfv,newd1);
1083               Standard_Boolean YaRev = d1fv.Dot(newd1) <0.; 
1084
1085               if (YaRev)
1086                 newC->Reverse();
1087
1088               if (i2s.HasLineOnS1(imin)) {
1089                 Einf.ChangeFirstPC() = i2s.LineOnS1(imin);
1090                 if ( YaRev) 
1091                   Einf.ChangeFirstPC()->Reverse();
1092               }
1093
1094               if (i2s.HasLineOnS2(imin)) {
1095                 Einf.ChangeSecondPC() = i2s.LineOnS2(imin);
1096                 if ( YaRev) 
1097                   Einf.ChangeSecondPC()->Reverse();
1098               }
1099             } // if (i2s.Line(1)->DynamicType() != STANDARD_TYPE(Geom_BSplineCurve))
1100             else // i2s.Line(1) is BSplineCurve
1101             {
1102               //Find the first curve to glue
1103               TColGeom_SequenceOfCurve Candidates;
1104               if (S1->DynamicType() == STANDARD_TYPE(Geom_CylindricalSurface) ||
1105                   S1->DynamicType() == STANDARD_TYPE(Geom_ConicalSurface)) 
1106               {
1107                 for (i = 1; i <= i2s.NbLines(); i++)
1108                 {
1109                   Handle( Geom_Curve ) aCurve = i2s.Line(i);
1110                   gp_Pnt Pnt = aCurve->Value( aCurve->FirstParameter() );
1111                   GeomAPI_ProjectPointOnSurf projector( Pnt, S1, Precision::Confusion() );
1112                   Standard_Real U, V;
1113                   projector.LowerDistanceParameters( U, V );
1114                   if (Abs(U) <= Precision::Confusion() || Abs(U-2.*M_PI) <= Precision::Confusion())
1115                     Candidates.Append( aCurve );
1116                   else
1117                   {
1118                     Pnt = aCurve->Value( aCurve->LastParameter() );
1119                     projector.Init( Pnt, S1, Precision::Confusion() );
1120                     projector.LowerDistanceParameters( U, V );
1121                     if (Abs(U) <= Precision::Confusion() || Abs(U-2.*M_PI) <= Precision::Confusion())
1122                     {
1123                       aCurve->Reverse();
1124                       Candidates.Append( aCurve );
1125                     }
1126                   }
1127                 }
1128
1129                 if(Candidates.Length() == 0) 
1130                 {
1131                   //errStat = Draft_EdgeRecomputation;
1132                   //badShape = TopoDS::Edge(ite.Key());
1133                   //return;
1134                   for (i = 1; i <= i2s.NbLines(); i++)
1135                     Candidates.Append( i2s.Line(i) );
1136                 }
1137               }
1138               else 
1139               {
1140                 for (i = 1; i <= i2s.NbLines(); i++)
1141                   Candidates.Append( i2s.Line(i) );
1142               }
1143
1144               Handle( Geom_Curve ) FirstCurve;
1145               if (Candidates.Length() > 1)
1146               {
1147                 Dist2Min = RealLast();
1148                 for (i = 1; i <= Candidates.Length(); i++)
1149                 {
1150                   Handle( Geom_Curve ) aCurve = Candidates(i);
1151                   gp_Pnt Pnt = aCurve->Value( aCurve->FirstParameter() );
1152                   Dist2 = Pnt.SquareDistance( pfv );
1153                   if (Dist2 < Dist2Min)
1154                   {
1155                     Dist2Min = Dist2;
1156                     FirstCurve = aCurve;
1157                   }
1158                 }
1159               }
1160               else
1161                 FirstCurve = Candidates(1);
1162
1163               //Glueing
1164               TColGeom_SequenceOfCurve Curves;
1165               for (i = 1; i <= i2s.NbLines(); i++)
1166                 if (FirstCurve != i2s.Line(i))
1167                   Curves.Append( i2s.Line(i) );
1168
1169               TColGeom_SequenceOfCurve ToGlue;
1170               gp_Pnt EndPoint = FirstCurve->Value( FirstCurve->LastParameter() );
1171               Standard_Boolean added = Standard_True;
1172               while (added)
1173               {
1174                 added = Standard_False;
1175                 for (i = 1; i <= Curves.Length(); i++)
1176                 {
1177                   Handle( Geom_Curve ) aCurve = Curves(i);
1178                   gp_Pnt pfirst, plast;
1179                   pfirst = aCurve->Value( aCurve->FirstParameter() );
1180                   plast = aCurve->Value( aCurve->LastParameter() );
1181                   if (pfirst.Distance( EndPoint ) <= Precision::Confusion())
1182                   {
1183                     ToGlue.Append( aCurve );
1184                     EndPoint = plast;
1185                     Curves.Remove(i);
1186                     added = Standard_True;
1187                     break;
1188                   }
1189                   if (plast.Distance( EndPoint ) <= Precision::Confusion())
1190                   {
1191                     aCurve->Reverse();
1192                     ToGlue.Append( aCurve );
1193                     EndPoint = pfirst;
1194                     Curves.Remove(i);
1195                     added = Standard_True;
1196                     break;
1197                   }
1198                 }
1199               }
1200
1201               if (FirstCurve.IsNull()) {
1202                 errStat = Draft_EdgeRecomputation;
1203                 badShape = theEdge;
1204                 return;
1205               }
1206
1207               GeomConvert_CompCurveToBSplineCurve Concat( Handle(Geom_BSplineCurve)::DownCast(FirstCurve) );
1208               for (i = 1; i <= ToGlue.Length(); i++)
1209                 Concat.Add( Handle(Geom_BSplineCurve)::DownCast(ToGlue(i)), Precision::Confusion(), Standard_True );
1210
1211               newC = Concat.BSplineCurve();
1212
1213               TheCurve.Load( newC );
1214               Extrema_ExtPC myExtPC( pfv, TheCurve );
1215               Dist2Min = RealLast();
1216               for (i = 1; i <= myExtPC.NbExt(); i++)
1217               {
1218                 if (myExtPC.IsMin(i))
1219                 {
1220                   Dist2 = myExtPC.SquareDistance(i);
1221                   if (Dist2 < Dist2Min)
1222                   {
1223                     Dist2Min = Dist2;
1224                     pmin = myExtPC.Point(i).Parameter();
1225                   }
1226                 }
1227               }
1228               newC->D1(pmin,pfv,newd1);
1229               Standard_Boolean YaRev = d1fv.Dot(newd1) < 0.; 
1230
1231               if (YaRev)
1232                 newC->Reverse();
1233               /*
1234               if (i2s.HasLineOnS1(imin)) {
1235               Einf.ChangeFirstPC() = i2s.LineOnS1(imin);
1236               if ( YaRev) 
1237               Einf.ChangeFirstPC()->Reverse();
1238               }
1239
1240               if (i2s.HasLineOnS2(imin)) {
1241               Einf.ChangeSecondPC() = i2s.LineOnS2(imin);
1242               if ( YaRev) 
1243               Einf.ChangeSecondPC()->Reverse();
1244               }
1245               */
1246             } // else: i2s.NbLines() > 2 && S1 is Cylinder or Cone
1247
1248             Einf.Tolerance(Max(Einf.Tolerance(), i2s.TolReached3d()));
1249           }  // End step KPart
1250         }
1251         else { // case of tangency
1252           const TopoDS_Face& F1 = Einf.FirstFace();
1253           const TopoDS_Face& F2 = Einf.SecondFace();
1254
1255           Handle(Geom_Surface) aLocalS1 = myFMap.FindFromKey(F1).Geometry();
1256           Handle(Geom_Surface) aLocalS2 = myFMap.FindFromKey(F2).Geometry();
1257           if (aLocalS1.IsNull() || aLocalS2.IsNull()) {
1258             errStat = Draft_EdgeRecomputation;
1259             badShape = theEdge;
1260             return;
1261           }
1262           if (aLocalS1->DynamicType() == STANDARD_TYPE(Geom_RectangularTrimmedSurface)) {
1263             aLocalS1 = Handle(Geom_RectangularTrimmedSurface)::
1264               DownCast(aLocalS1)->BasisSurface();
1265           }
1266           if (aLocalS2->DynamicType() == STANDARD_TYPE(Geom_RectangularTrimmedSurface)) {
1267             aLocalS2 = Handle(Geom_RectangularTrimmedSurface)::
1268               DownCast(aLocalS2)->BasisSurface();
1269           }
1270
1271           gp_Dir dirextr;
1272           //Standard_Boolean dirfound = Standard_False;
1273           if (aLocalS1->DynamicType() == STANDARD_TYPE(Geom_CylindricalSurface)) {
1274             gp_Cylinder cyl = 
1275               Handle(Geom_CylindricalSurface)::DownCast(aLocalS1)->Cylinder();
1276             dirextr = cyl.Axis().Direction();
1277             //dirfound = Standard_True;
1278             // see direction...
1279
1280           }
1281           else if (aLocalS1->DynamicType() == STANDARD_TYPE(Geom_SurfaceOfLinearExtrusion)) {
1282             dirextr = Handle(Geom_SurfaceOfLinearExtrusion)::
1283               DownCast(aLocalS1)->Direction();
1284             //dirfound = Standard_True;
1285             // see direction...
1286
1287             // Here it is possible to calculate PCurve.
1288             Handle(Geom_SurfaceOfLinearExtrusion) SEL = 
1289               Handle(Geom_SurfaceOfLinearExtrusion)::DownCast(aLocalS1);
1290             Handle(Geom_Circle) GCir = 
1291               Handle(Geom_Circle)::DownCast(SEL->BasisCurve());
1292             if ( !GCir.IsNull()) {
1293               Standard_Real U = ElCLib::Parameter(GCir->Circ(),ptfixe);
1294               Handle(Geom2d_Line) PC1 = 
1295                 new Geom2d_Line(gp_Pnt2d(U,0.),gp::DY2d());
1296               Einf.ChangeFirstPC() = PC1;
1297             }
1298           }
1299
1300           else if (aLocalS2->DynamicType() == STANDARD_TYPE(Geom_CylindricalSurface)) {
1301             gp_Cylinder cyl = 
1302               Handle(Geom_CylindricalSurface)::DownCast(aLocalS2)->Cylinder();
1303             dirextr = cyl.Axis().Direction();
1304             // dirfound = Standard_True;
1305             // see direction...
1306
1307           }
1308           else if (aLocalS2->DynamicType() == STANDARD_TYPE(Geom_SurfaceOfLinearExtrusion)) {
1309             dirextr = Handle(Geom_SurfaceOfLinearExtrusion)::
1310               DownCast(aLocalS2)->Direction();
1311             // dirfound = Standard_True;
1312             // see direction...
1313
1314             // Here it is possible to calculate PCurve.
1315             Handle(Geom_SurfaceOfLinearExtrusion) SEL = 
1316               Handle(Geom_SurfaceOfLinearExtrusion)::DownCast(aLocalS2);
1317             Handle(Geom_Circle) GCir = 
1318               Handle(Geom_Circle)::DownCast(SEL->BasisCurve());
1319             if ( !GCir.IsNull()) {
1320               Standard_Real U = ElCLib::Parameter(GCir->Circ(),ptfixe);
1321               Handle(Geom2d_Line) PC2 = 
1322                 new Geom2d_Line(gp_Pnt2d(U,0.),gp::DY2d());
1323               Einf.ChangeSecondPC() = PC2;
1324             }
1325           }
1326           newC = new Geom_Line(ptfixe,dirextr);
1327
1328           gp_Pnt pfv;
1329           gp_Vec d1fv,newd1;
1330           C->D1(0.,pfv,d1fv);
1331           newC->D1(0.,pfv,newd1);
1332           Standard_Boolean YaRev = d1fv.Dot(newd1) <0.; 
1333           if (YaRev) {
1334             newC->Reverse();
1335             if(!Einf.FirstPC().IsNull()) {
1336               Einf.ChangeFirstPC()->Reverse();
1337             }
1338             if(!Einf.SecondPC().IsNull()) {
1339               Einf.ChangeSecondPC()->Reverse();
1340             }
1341           }
1342         }
1343
1344         Handle(Geom_TrimmedCurve) T = Handle(Geom_TrimmedCurve)::DownCast(newC);
1345         if (!T.IsNull()) newC = T->BasisCurve();
1346         Einf.ChangeGeometry() = newC;
1347       }
1348       else if (!Einf.NewGeometry()){
1349         // set existing curve 3D
1350         Handle(Geom_TrimmedCurve) T = Handle(Geom_TrimmedCurve)::DownCast(C);
1351         if (!T.IsNull()) C = T->BasisCurve();
1352         Einf.ChangeGeometry() = C;
1353       }
1354     }
1355
1356     // Calculate new vertices.
1357
1358     Handle(GeomAdaptor_HCurve)   HAC = new GeomAdaptor_HCurve;
1359     Handle(GeomAdaptor_HSurface) HAS = new GeomAdaptor_HSurface;
1360
1361     for (Standard_Integer ii = 1; ii <= myVMap.Extent(); ii++)
1362     {
1363       GeomAdaptor_Curve    AC;
1364       GeomAdaptor_Surface  AS;
1365
1366       const TopoDS_Vertex& TVV = myVMap.FindKey(ii);
1367       Draft_VertexInfo& Vinf = myVMap.ChangeFromIndex(ii);
1368       if (!Choose(myFMap,myEMap,TVV,Vinf,AC,AS)) {
1369
1370         // no concerted edge => alignment of two consecutive edges.
1371         gp_Pnt pvt;
1372         Vinf.ChangeGeometry() = pvt;
1373         Vinf.InitEdgeIterator();
1374         if (Vinf.MoreEdge()) {
1375           const TopoDS_Edge& Edg1 = Vinf.Edge();
1376           //const Draft_EdgeInfo& Einf1 = myEMap(Edg1);
1377           Draft_EdgeInfo& Einf1 = myEMap.ChangeFromKey(Edg1);
1378           gp_Pnt vtori = BRep_Tool::Pnt(TVV);
1379           //Einf1.Geometry()->D0(Vinf.Parameter(Edg1), pvt);
1380           GeomAPI_ProjectPointOnCurve Projector( vtori, Einf1.Geometry() ); //patch
1381           pvt = Projector.NearestPoint();
1382
1383 #ifdef OCCT_DEBUG
1384           static Standard_Integer VertexRecomp = 1;
1385           if (VertexRecomp!=0) {
1386             cout << "pori :" << vtori.X() << " " << vtori.Y() << " " << vtori.Z() << endl;
1387             cout << "  Edg 1 :" << Vinf.Parameter(Edg1) << endl;
1388             cout << "pvt :" << pvt.X() << " " << pvt.Y() << " " << pvt.Z() << endl;
1389           }
1390 #endif
1391
1392           Standard_Real dion=pvt.SquareDistance(vtori);
1393           Vinf.NextEdge();
1394           if (Vinf.MoreEdge()) {
1395             const TopoDS_Edge& Edg2 = Vinf.Edge();
1396             //const Draft_EdgeInfo& Einf2 = myEMap(Edg2);
1397             Draft_EdgeInfo& Einf2 = myEMap.ChangeFromKey(Edg2);
1398             //      Standard_Real f;
1399             gp_Pnt opvt;
1400             Einf2.Geometry()->D0(Vinf.Parameter(Edg2), opvt);
1401
1402 #ifdef OCCT_DEBUG
1403             if (VertexRecomp!=0) {
1404               cout << "  Edg 2 :" << Vinf.Parameter(Vinf.Edge()) << endl;
1405               cout << "opvt " << opvt.X() << " " << opvt.Y() << " " << opvt.Z() << endl;
1406             }
1407 #endif
1408
1409             if (opvt.SquareDistance(vtori) < dion) {
1410               pvt = opvt;
1411             }
1412             //Vinf.ChangeParameter(Edg2) = Parameter(Einf2.Geometry(), pvt);
1413             Standard_Integer done;
1414             Standard_Real param = Parameter(Einf2.Geometry(), pvt, done);
1415             if (done != 0)
1416             {
1417               S1 = myFMap.FindFromKey(Einf2.FirstFace()).Geometry();
1418               S2 = myFMap.FindFromKey(Einf2.SecondFace()).Geometry();
1419               Vinf.ChangeParameter(Edg2) = SmartParameter( Einf2, BRep_Tool::Tolerance(Edg2), pvt, done, S1, S2 );
1420             }
1421             else
1422               Vinf.ChangeParameter(Edg2) = param;
1423           }
1424
1425           Vinf.ChangeGeometry() = pvt;
1426           //Vinf.ChangeParameter(Edg1) = Parameter(Einf1.Geometry(), pvt);
1427           Standard_Integer done;
1428           Standard_Real param = Parameter(Einf1.Geometry(), pvt, done);
1429           if (done != 0)
1430           {
1431             S1 = myFMap.FindFromKey(Einf1.FirstFace()).Geometry();
1432             S2 = myFMap.FindFromKey(Einf1.SecondFace()).Geometry();
1433             Vinf.ChangeParameter(Edg1) = SmartParameter( Einf1, BRep_Tool::Tolerance(Edg1), pvt, done, S1, S2 );
1434           }
1435           else
1436             Vinf.ChangeParameter(Edg1) = param;
1437           continue;
1438         }
1439
1440
1441         errStat = Draft_VertexRecomputation;
1442         badShape = TVV;
1443         return;
1444       }
1445
1446       IntCurveSurface_HInter myintcs;
1447       HAC->Set(AC);
1448       HAS->Set(AS);
1449
1450       myintcs.Perform(HAC,HAS);
1451
1452
1453       if (!myintcs.IsDone()) {
1454         errStat = Draft_VertexRecomputation;
1455         badShape = TVV;
1456         return;
1457       }
1458
1459       gp_Pnt vtori = BRep_Tool::Pnt(TVV);
1460       gp_Pnt pvt;
1461
1462       Standard_Integer nbsol = myintcs.NbPoints();
1463       if (nbsol <= 0)
1464       {
1465         Extrema_ExtCS extr( AC, AS, Precision::PConfusion(), Precision::PConfusion() );
1466
1467         if(!extr.IsDone() || extr.NbExt() == 0) {
1468           errStat = Draft_VertexRecomputation;
1469           badShape = TVV;
1470           return;
1471         }
1472
1473
1474         Standard_Real disref = RealLast();
1475         Standard_Integer iref = 0;
1476         Extrema_POnCurv Pc;
1477         Extrema_POnSurf Ps;
1478         for (Standard_Integer i = 1; i <= extr.NbExt(); i++)
1479         {
1480           extr.Points( i, Pc, Ps );
1481           Standard_Real distemp = Pc.Value().SquareDistance(vtori);
1482           if ( distemp < disref)
1483           {
1484             disref = distemp;
1485             iref = i;
1486           }
1487         } 
1488         extr.Points( iref, Pc, Ps );
1489         pvt = Pc.Value();
1490       }
1491       else
1492       {
1493         Standard_Real disref = RealLast();
1494         Standard_Integer iref = 0;
1495         for (Standard_Integer i = 1; i <= nbsol; i++)
1496         {
1497           Standard_Real distemp = myintcs.Point(i).Pnt().SquareDistance(vtori);
1498           if ( distemp < disref)
1499           {
1500             disref = distemp;
1501             iref = i;
1502           }
1503         } 
1504         pvt = myintcs.Point(iref).Pnt();
1505       }
1506
1507       Vinf.ChangeGeometry() = pvt;
1508
1509       for (Vinf.InitEdgeIterator();Vinf.MoreEdge(); Vinf.NextEdge()) {
1510         const TopoDS_Edge& Edg = Vinf.Edge();
1511         //const Draft_EdgeInfo& Einf = myEMap(Edg);
1512         Draft_EdgeInfo& Einf = myEMap.ChangeFromKey(Edg);
1513         //Vinf.ChangeParameter(Edg) = Parameter(Einf.Geometry(),pvt);
1514         Standard_Integer done;
1515         Standard_Real param = Parameter(Einf.Geometry(), pvt, done);
1516         if (done != 0)
1517         {
1518           S1 = myFMap.FindFromKey(Einf.FirstFace()).Geometry();
1519           S2 = myFMap.FindFromKey(Einf.SecondFace()).Geometry();
1520           Vinf.ChangeParameter(Edg) = SmartParameter( Einf, BRep_Tool::Tolerance(Edg), pvt, done, S1, S2 );
1521         }
1522         else
1523           Vinf.ChangeParameter(Edg) = param;
1524       }
1525     }
1526   }
1527
1528   // small loop of validation/protection
1529
1530   for (Standard_Integer i = 1; i <= myEMap.Extent(); i++) 
1531   {
1532     const TopoDS_Edge& edg = TopoDS::Edge(myEMap.FindKey(i));
1533
1534     TopoDS_Vertex Vf,Vl;
1535     TopExp::Vertices(edg,Vf,Vl);
1536     if (edg.Orientation() == TopAbs_REVERSED) {
1537       Vf.Reverse();
1538       Vl.Reverse();
1539     }
1540     Standard_Real pf,pl,tolerance;
1541     if (!NewParameter(Vf,edg,pf,tolerance)) {
1542       pf = BRep_Tool::Parameter(Vf,edg);
1543     }
1544     if (!NewParameter(Vl,edg,pl,tolerance)) {
1545       pl = BRep_Tool::Parameter(Vl,edg);
1546     }
1547     if (pl <= pf) {
1548       //      const Handle(Geom_Curve) gc=ite.Value().Geometry();
1549       //      if (!gc.IsNull()) {
1550       //        pl = gc->LastParameter();
1551       //        pf = gc->FirstParameter();
1552       //      }
1553       Handle( Geom_Curve ) theCurve = myEMap.FindFromKey(edg).Geometry();
1554       if (theCurve->IsClosed())
1555       {
1556         // pf >= pl
1557         Standard_Real FirstPar = theCurve->FirstParameter(), LastPar = theCurve->LastParameter();
1558         Standard_Real pconf = Precision::PConfusion();
1559         if (Abs( pf - LastPar ) <= pconf)
1560           pf = FirstPar;
1561         else if (Abs( pl - FirstPar ) <= pconf)
1562           pl = LastPar;
1563
1564         if(pl <= pf) {
1565           pl += (LastPar-FirstPar);
1566         }
1567
1568       }
1569       if (pl <= pf) {
1570         errStat = Draft_EdgeRecomputation;
1571         badShape = edg;
1572         return;
1573       }
1574     }
1575     if (myVMap.Contains( Vf ))
1576       myVMap.ChangeFromKey(Vf).ChangeParameter(edg) = pf;
1577     if (myVMap.Contains( Vl ))
1578       myVMap.ChangeFromKey(Vl).ChangeParameter(edg) = pl;
1579   }
1580 }
1581
1582
1583
1584 //=======================================================================
1585 //function : NewSurface
1586 //purpose  : 
1587 //=======================================================================
1588
1589 Handle(Geom_Surface) Draft_Modification::NewSurface
1590   (const Handle(Geom_Surface)& S,
1591   const TopAbs_Orientation Oris,
1592   const gp_Dir& Direction,
1593   const Standard_Real Angle,
1594   const gp_Pln& NeutralPlane) 
1595 {
1596   Handle(Geom_Surface) NewS;
1597
1598   Handle(Standard_Type) TypeS = S->DynamicType();
1599
1600   if (TypeS == STANDARD_TYPE(Geom_Plane)) {
1601     gp_Pln Pl = Handle(Geom_Plane)::DownCast(S)->Pln();
1602     gp_Ax1 Axe;
1603     Standard_Real Theta;
1604     if (FindRotation(Pl,Oris,Direction,Angle,NeutralPlane,Axe,Theta)) {
1605       if ( Abs(Theta) > Precision::Angular()) {
1606         NewS = Handle(Geom_Surface)::DownCast(S->Rotated(Axe,Theta));
1607       }
1608       else {
1609         NewS = S;
1610       }
1611     }
1612   }
1613   else if (TypeS == STANDARD_TYPE(Geom_CylindricalSurface)) {
1614     Standard_Real testdir = Direction.Dot(NeutralPlane.Axis().Direction());
1615     if (Abs(testdir) <= 1.-Precision::Angular()) {      
1616 #ifdef OCCT_DEBUG
1617       cout << "NewSurfaceCyl:Draft_Direction_and_Neutral_Perpendicular" << endl;
1618 #endif
1619       return NewS;      
1620     }     
1621     gp_Cylinder Cy = Handle(Geom_CylindricalSurface)::DownCast(S)->Cylinder();     
1622     testdir = Direction.Dot(Cy.Axis().Direction());
1623     if (Abs(testdir) <= 1.-Precision::Angular()) {
1624 #ifdef OCCT_DEBUG
1625       cout << "NewSurfaceCyl:Draft_Direction_and_Cylinder_Perpendicular" << endl;
1626 #endif
1627       return NewS;
1628     }
1629     if (Abs(Angle) > Precision::Angular())
1630     {
1631       IntAna_QuadQuadGeo i2s;
1632       i2s.Perform(NeutralPlane,Cy,Precision::Angular(),Precision::Confusion());
1633       Standard_Boolean isIntDone = i2s.IsDone();
1634
1635       if(i2s.TypeInter() == IntAna_Ellipse)
1636       {
1637         const gp_Elips anEl = i2s.Ellipse(1);
1638         const Standard_Real aMajorR = anEl.MajorRadius();
1639         const Standard_Real aMinorR = anEl.MinorRadius();
1640         isIntDone = (aMajorR < 100000.0 * aMinorR);
1641       }
1642
1643       if (!isIntDone || i2s.TypeInter() != IntAna_Circle) {
1644 #ifdef OCCT_DEBUG
1645         cout << "NewSurfaceCyl:Draft_Intersection_Neutral_Cylinder_NotDone" << endl;
1646 #endif
1647         return NewS;
1648       } 
1649       gp_Ax3 axcone = Cy.Position();
1650       // Pb : Where is the material???
1651       Standard_Real alpha = Angle;
1652       Standard_Boolean direct(axcone.Direct());
1653       if ((direct && Oris == TopAbs_REVERSED) ||
1654          (!direct && Oris == TopAbs_FORWARD)) {
1655         alpha = -alpha;
1656       }
1657
1658       gp_Pnt Center = i2s.Circle(1).Location();
1659       if (testdir <0.) {
1660         alpha = -alpha;
1661       }
1662       Standard_Real Z = ElCLib::LineParameter(Cy.Axis(),Center);
1663       Standard_Real Rad = Cy.Radius()+Z*Tan(alpha);
1664       if (Rad < 0.) {
1665         Rad = -Rad;
1666       }
1667       else {
1668         alpha = -alpha;
1669       }
1670       gp_Cone co(axcone,alpha,Rad);
1671       NewS = new Geom_ConicalSurface(co);
1672     }
1673     else {
1674       NewS = S;
1675     }
1676   }
1677   else if (TypeS == STANDARD_TYPE(Geom_ConicalSurface)) {
1678
1679     Standard_Real testdir = Direction.Dot(NeutralPlane.Axis().Direction());
1680     if (Abs(testdir) <= 1.-Precision::Angular()) {      
1681 #ifdef OCCT_DEBUG
1682       cout << "NewSurfaceCone:Draft_Direction_and_Neutral_Perpendicular" << endl;
1683 #endif
1684       return NewS;      
1685     }   
1686
1687     gp_Cone Co1 = Handle(Geom_ConicalSurface)::DownCast(S)->Cone();
1688
1689     testdir = Direction.Dot(Co1.Axis().Direction());
1690     if (Abs(testdir) <= 1.-Precision::Angular()) {
1691 #ifdef OCCT_DEBUG
1692       cout << "NewSurfaceCone:Draft_Direction_and_Cone_Perpendicular" << endl;
1693 #endif
1694       return NewS;
1695     }
1696
1697
1698     IntAna_QuadQuadGeo i2s;
1699     i2s.Perform(NeutralPlane,Co1,Precision::Angular(),Precision::Confusion());
1700     if (!i2s.IsDone() || i2s.TypeInter() != IntAna_Circle) {
1701 #ifdef OCCT_DEBUG
1702       cout << "NewSurfaceCone:Draft_Intersection_Neutral_Conical_NotDone" << endl;
1703 #endif
1704       return NewS;
1705     }
1706     gp_Ax3 axcone = Co1.Position();
1707     // Pb : Where is the material???
1708     Standard_Real alpha = Angle;
1709     Standard_Boolean direct(axcone.Direct());
1710     if ((direct && Oris == TopAbs_REVERSED) ||
1711        (!direct && Oris == TopAbs_FORWARD)) {
1712       alpha = -alpha;
1713     }
1714
1715     gp_Pnt Center = i2s.Circle(1).Location();
1716     if (Abs(Angle) > Precision::Angular()) {
1717       if (testdir <0.) {
1718         alpha = -alpha;
1719       }
1720       Standard_Real Z = ElCLib::LineParameter(Co1.Axis(),Center);
1721       Standard_Real Rad = i2s.Circle(1).Radius()+Z*Tan(alpha);
1722       if (Rad < 0.) {
1723         Rad = -Rad;
1724       }
1725       else {
1726         alpha = -alpha;
1727       }
1728       if (Abs(alpha-Co1.SemiAngle()) < Precision::Angular()) {
1729         NewS = S;
1730       }
1731       else {
1732         gp_Cone co(axcone,alpha,Rad);
1733         NewS = new Geom_ConicalSurface(co);
1734       }
1735     }
1736     else {
1737       NewS = new
1738         Geom_CylindricalSurface(gp_Cylinder(axcone,i2s.Circle(1).Radius()));
1739     }
1740   }
1741   else {
1742 #ifdef OCCT_DEBUG
1743     cout << "NewSurface:Draft_SurfNotYetImplemented" << endl;
1744 #endif
1745   }
1746   return NewS;
1747
1748
1749
1750 //=======================================================================
1751 //function : NewCurve
1752 //purpose  : 
1753 //=======================================================================
1754
1755 Handle(Geom_Curve) Draft_Modification::NewCurve
1756   (const Handle(Geom_Curve)& C,
1757   const Handle(Geom_Surface)& S,
1758   const TopAbs_Orientation Oris,
1759   const gp_Dir& Direction,
1760   const Standard_Real Angle,
1761   const gp_Pln& NeutralPlane,
1762   const Standard_Boolean )
1763
1764 {
1765   Handle(Geom_Curve) NewC;
1766
1767   Handle(Standard_Type) TypeS = S->DynamicType();
1768
1769   if (TypeS == STANDARD_TYPE(Geom_Plane)) {
1770     gp_Pln Pl = Handle(Geom_Plane)::DownCast(S)->Pln();
1771     gp_Ax1 Axe;
1772     Standard_Real Theta;
1773     if (FindRotation(Pl,Oris,Direction,Angle,NeutralPlane,Axe,Theta)) {
1774       if ( Abs(Theta) > Precision::Angular()) {
1775         NewC = Handle(Geom_Curve)::DownCast(C->Rotated(Axe,Theta));
1776       }
1777       else {
1778         NewC = C;
1779       }
1780     }
1781     return NewC;
1782   }
1783
1784
1785   if (C->DynamicType() != STANDARD_TYPE(Geom_Line)) {
1786     return NewC;
1787   }
1788
1789
1790   gp_Lin lin = Handle(Geom_Line)::DownCast(C)->Lin();
1791   //  Standard_Real testdir = Direction.Dot(lin.Direction());
1792   //  if (Abs(testdir) <= 1.-Precision::Angular()) {
1793   //    return NewC;
1794   //  }
1795   gp_Dir Norm;
1796   if (TypeS == STANDARD_TYPE(Geom_CylindricalSurface)) {
1797     Standard_Real U,V;
1798     gp_Vec d1u,d1v;
1799     gp_Pnt pbid;
1800     gp_Cylinder Cy = Handle(Geom_CylindricalSurface)::DownCast(S)->Cylinder();
1801     ElSLib::Parameters(Cy,lin.Location(),U,V);
1802     ElSLib::D1(U,V,Cy,pbid,d1u,d1v);
1803     Norm = d1u.Crossed(d1v);
1804   }
1805   else if (TypeS == STANDARD_TYPE(Geom_ConicalSurface)) {
1806     Standard_Real U,V;
1807     gp_Vec d1u,d1v;
1808     gp_Pnt pbid;
1809     gp_Cone Co = Handle(Geom_ConicalSurface)::DownCast(S)->Cone();
1810     ElSLib::Parameters(Co,lin.Location(),U,V);
1811     ElSLib::D1(U,V,Co,pbid,d1u,d1v);
1812     Norm = d1u.Crossed(d1v);
1813   }
1814
1815   IntAna_IntConicQuad ilipl(lin,NeutralPlane,Precision::Angular());
1816   if (ilipl.IsDone() && ilipl.NbPoints() != 0){
1817     if (Oris == TopAbs_REVERSED) {
1818       Norm.Reverse();
1819     }
1820     gp_Ax1 axrot(ilipl.Point(1), Norm.Crossed(Direction));
1821     gp_Lin lires = gp_Lin(gp_Ax1(ilipl.Point(1),Direction)).
1822       Rotated(axrot,Angle);
1823     if (lires.Direction().Dot(lin.Direction()) < 0.) {
1824       lires.Reverse();
1825     }
1826     NewC = new Geom_Line(lires);
1827   }
1828   return NewC;
1829 }
1830
1831
1832 //=======================================================================
1833 //function : Choose
1834 //purpose  : 
1835 //=======================================================================
1836
1837 static Standard_Boolean Choose(const Draft_IndexedDataMapOfFaceFaceInfo& theFMap,
1838   Draft_IndexedDataMapOfEdgeEdgeInfo& theEMap,
1839   const TopoDS_Vertex& Vtx,
1840   Draft_VertexInfo& Vinf,
1841   GeomAdaptor_Curve& AC,
1842   GeomAdaptor_Surface& AS)
1843 {
1844   gp_Vec tgref; 
1845   Vinf.InitEdgeIterator();
1846
1847   // Find a regular edge with null SecondFace
1848   while (Vinf.MoreEdge()) {
1849     const TopoDS_Edge& E1 = Vinf.Edge();
1850     const Draft_EdgeInfo& Einf1 = theEMap.FindFromKey(E1);
1851     if (Einf1.SecondFace().IsNull()) {
1852       break;
1853     }
1854     else {
1855       GeomAbs_Shape te = BRep_Tool::Continuity(E1,Einf1.FirstFace(),
1856         Einf1.SecondFace());
1857       if (te >= GeomAbs_G1) {
1858         break;
1859       }
1860     }
1861     Vinf.NextEdge();
1862   }
1863   if (!Vinf.MoreEdge()) { // take the first edge
1864     Vinf.InitEdgeIterator();
1865   }
1866
1867   const TopoDS_Edge& Eref = Vinf.Edge();
1868   //const Draft_EdgeInfo& Einf = theEMap(Eref);
1869   Draft_EdgeInfo& Einf = theEMap.ChangeFromKey(Eref);
1870
1871   AC.Load(Einf.Geometry());
1872
1873   Standard_Real f,l,prm;
1874   TopLoc_Location Loc;
1875   Handle(Geom_Curve) C = BRep_Tool::Curve(Eref,Loc,f,l);
1876   C = Handle(Geom_Curve)::DownCast(C->Transformed(Loc.Transformation()));
1877   gp_Pnt ptbid;
1878   //prm = Parameter(C,BRep_Tool::Pnt(Vtx));
1879   Standard_Integer done;
1880   Standard_Real param = Parameter( C, BRep_Tool::Pnt(Vtx), done );
1881   if (done != 0)
1882   {
1883     Handle( Geom_Surface ) S1 = theFMap.FindFromKey(Einf.FirstFace()).Geometry();
1884     Handle( Geom_Surface ) S2 = theFMap.FindFromKey(Einf.SecondFace()).Geometry();
1885     prm = SmartParameter( Einf, BRep_Tool::Tolerance(Eref), BRep_Tool::Pnt(Vtx), done, S1, S2 );
1886   }
1887   else
1888     prm = param;
1889   C->D1(prm,ptbid,tgref);
1890
1891
1892   Vinf.InitEdgeIterator();
1893   while (Vinf.MoreEdge()) {
1894     // Find a non tangent edge
1895     const TopoDS_Edge& Edg = Vinf.Edge();
1896     if (!Edg.IsSame(Eref)) {
1897       //const Draft_EdgeInfo& Einfo = theEMap(Edg);
1898       Draft_EdgeInfo& Einfo = theEMap.ChangeFromKey(Edg);
1899       if (!Einfo.SecondFace().IsNull() &&
1900           BRep_Tool::Continuity(Edg,Einfo.FirstFace(),Einfo.SecondFace()) 
1901           <= GeomAbs_C0) {
1902         C = BRep_Tool::Curve(Edg,Loc,f,l);
1903         C = Handle(Geom_Curve)::DownCast(C->Transformed(Loc.Transformation()));
1904         //prm = Parameter(C,BRep_Tool::Pnt(Vtx));
1905         Standard_Integer anewdone;
1906         Standard_Real anewparam = Parameter( C, BRep_Tool::Pnt(Vtx), anewdone );
1907         if (anewdone != 0)
1908         {
1909           Handle( Geom_Surface ) S1 = theFMap.FindFromKey(Einfo.FirstFace()).Geometry();
1910           Handle( Geom_Surface ) S2 = theFMap.FindFromKey(Einfo.SecondFace()).Geometry();
1911           prm = SmartParameter( Einfo, BRep_Tool::Tolerance(Edg), BRep_Tool::Pnt(Vtx), anewdone, S1, S2 );
1912         }
1913         else
1914           prm = anewparam;
1915         gp_Vec tg;
1916         C->D1(prm,ptbid,tg);
1917         if (tg.CrossMagnitude(tgref) > Precision::Confusion()) {
1918           break;
1919         }
1920       }
1921     }
1922     Vinf.NextEdge();
1923   }
1924   if (!Vinf.MoreEdge()) {
1925     return Standard_False;
1926   }
1927
1928   const Draft_EdgeInfo& Einf2 = theEMap.FindFromKey(Vinf.Edge());
1929   if (!Einf.SecondFace().IsNull()) {
1930
1931     if (Einf2.FirstFace().IsSame(Einf.FirstFace()) ||
1932         Einf2.FirstFace().IsSame(Einf.SecondFace())) {
1933       AS.Load(theFMap.FindFromKey(Einf2.SecondFace()).Geometry());
1934     }
1935     else {
1936       AS.Load(theFMap.FindFromKey(Einf2.FirstFace()).Geometry());
1937     }
1938   }
1939   else {
1940     if (Einf2.FirstFace().IsSame(Einf.FirstFace())) {
1941       AS.Load(theFMap.FindFromKey(Einf2.SecondFace()).Geometry());
1942     }
1943     else {
1944       AS.Load(theFMap.FindFromKey(Einf2.FirstFace()).Geometry());
1945     }
1946   }
1947   return Standard_True;
1948 }
1949
1950
1951 //=======================================================================
1952 //function : Parameter
1953 //purpose  : 
1954 //=======================================================================
1955
1956 static Standard_Real Parameter(const Handle(Geom_Curve)& C,
1957   const gp_Pnt& P,
1958   Standard_Integer& done)
1959 {
1960   done = 0;
1961   Handle(Geom_Curve) cbase = C;
1962   Handle(Standard_Type) ctyp = C->DynamicType();
1963   if (ctyp == STANDARD_TYPE(Geom_TrimmedCurve)) {
1964     cbase = Handle(Geom_TrimmedCurve)::DownCast(C)->BasisCurve();
1965     ctyp = cbase->DynamicType();
1966   }
1967   Standard_Real param;
1968   if (ctyp == STANDARD_TYPE(Geom_Line)) {
1969     param = ElCLib::Parameter(Handle(Geom_Line)::DownCast(cbase)->Lin(),P);
1970   }
1971   else if (ctyp == STANDARD_TYPE(Geom_Circle)) {
1972     param = ElCLib::Parameter(Handle(Geom_Circle)::DownCast(cbase)->Circ(),P);
1973     if (Abs(2.*M_PI-param) <=Epsilon(2.*M_PI)) {
1974       param = 0.;
1975     }
1976   }
1977   else if (ctyp == STANDARD_TYPE(Geom_Ellipse)) {
1978     param = ElCLib::Parameter(Handle(Geom_Ellipse)::DownCast(cbase)->Elips(),P);
1979     if (Abs(2.*M_PI-param) <=Epsilon(2.*M_PI)) {
1980       param = 0.;
1981     }
1982   }
1983   else if (ctyp == STANDARD_TYPE(Geom_Parabola)) {
1984     param = ElCLib::Parameter(Handle(Geom_Parabola)::DownCast(cbase)->Parab(),P);
1985   }
1986   else if (ctyp == STANDARD_TYPE(Geom_Hyperbola)) {
1987     param = ElCLib::Parameter(Handle(Geom_Hyperbola)::DownCast(cbase)->Hypr(),P);
1988   }
1989   else {
1990     GeomAdaptor_Curve TheCurve(C);
1991     Extrema_ExtPC myExtPC(P,TheCurve);
1992     if (!myExtPC.IsDone()) {
1993       Standard_Failure::Raise("Draft_Modification_1::Parameter: ExtremaPC not done.");
1994     }
1995     if (myExtPC.NbExt() >= 1) {
1996       Standard_Real Dist2, Dist2Min = myExtPC.SquareDistance(1);
1997       Standard_Integer j, jmin = 1;
1998       for (j = 2; j <= myExtPC.NbExt(); j++) {
1999         Dist2 = myExtPC.SquareDistance(j);
2000         if (Dist2 < Dist2Min) {
2001           Dist2Min = Dist2;
2002           jmin = j;
2003         }
2004       }
2005       param = myExtPC.Point(jmin).Parameter();
2006     }
2007     else {
2008       Standard_Real dist1_2,dist2_2;
2009       gp_Pnt p1b,p2b;
2010       myExtPC.TrimmedSquareDistances(dist1_2,dist2_2,p1b,p2b);
2011       if (dist1_2 < dist2_2) {
2012         done = -1;
2013         param = TheCurve.FirstParameter();
2014       }
2015       else {
2016         done = 1;
2017         param = TheCurve.LastParameter();
2018       }
2019     }
2020
2021     if (cbase->IsPeriodic()) {
2022       Standard_Real Per  = cbase->Period();
2023       Standard_Real Tolp = Precision::Parametric(Precision::Confusion());  
2024       if (Abs(Per-param) <= Tolp) {
2025         param = 0.;
2026       }
2027     }
2028   }
2029   return param;
2030 }
2031
2032 //=======================================================================
2033 //function : SmartParameter
2034 //purpose  : 
2035 //=======================================================================
2036
2037 static Standard_Real SmartParameter(Draft_EdgeInfo& Einf,
2038   const Standard_Real EdgeTol,
2039   const gp_Pnt& Pnt,
2040   const Standard_Integer sign,
2041   const Handle(Geom_Surface)& S1,
2042   const Handle(Geom_Surface)& S2)
2043 {
2044   Handle( Geom2d_Curve ) NewC2d;
2045   Standard_Real Tol = Precision::Confusion();
2046   Standard_Real Etol = EdgeTol;
2047
2048   Handle( Geom2d_Curve ) pcu1 = Einf.FirstPC();
2049   Handle( Geom2d_Curve ) pcu2 = Einf.SecondPC();
2050
2051   if (pcu1.IsNull())
2052   {
2053     Handle( Geom_Curve ) theCurve = Einf.Geometry();
2054     pcu1 = GeomProjLib::Curve2d( theCurve, theCurve->FirstParameter(), theCurve->LastParameter(), S1, Etol );
2055     Einf.ChangeFirstPC() = pcu1;
2056   }
2057   if (pcu2.IsNull())
2058   {
2059     Handle( Geom_Curve ) theCurve = Einf.Geometry();
2060     pcu2 = GeomProjLib::Curve2d( theCurve, theCurve->FirstParameter(), theCurve->LastParameter(), S2, Etol );
2061     Einf.ChangeSecondPC() = pcu2;
2062   }
2063
2064   GeomAPI_ProjectPointOnSurf Projector( Pnt, S1 );
2065   Standard_Real U, V;
2066   Projector.LowerDistanceParameters( U, V );
2067
2068   NewC2d = Einf.FirstPC();
2069   if (NewC2d->DynamicType() == STANDARD_TYPE(Geom2d_TrimmedCurve))
2070     NewC2d = (Handle(Geom2d_TrimmedCurve)::DownCast(NewC2d))->BasisCurve();
2071
2072   gp_Pnt2d P2d( U, V );
2073   Geom2dAPI_ProjectPointOnCurve Projector2d( P2d, NewC2d );
2074   if (Projector2d.NbPoints() == 0 || Projector2d.LowerDistance() > Tol)
2075   {
2076     Handle( Geom2d_BSplineCurve ) BCurve;
2077     if (NewC2d->DynamicType() != STANDARD_TYPE(Geom2d_BSplineCurve))
2078       BCurve = Geom2dConvert::CurveToBSplineCurve( NewC2d );
2079     else
2080       BCurve = Handle( Geom2d_BSplineCurve )::DownCast( NewC2d );
2081     if (sign == -1)
2082     {
2083       TColgp_Array1OfPnt2d PntArray( 1, 2 );
2084       PntArray(1) = P2d;
2085       PntArray(2) = BCurve->Pole(1);
2086       Handle( Geom2d_BezierCurve ) Patch = new Geom2d_BezierCurve( PntArray );
2087       Geom2dConvert_CompCurveToBSplineCurve Concat( BCurve, Convert_QuasiAngular );
2088       Concat.Add( Patch, Tol, Standard_False );
2089       BCurve = Concat.BSplineCurve();
2090     }
2091     else
2092     {
2093       TColgp_Array1OfPnt2d PntArray( 1, 2 );
2094       PntArray(1) = BCurve->Pole( BCurve->NbPoles() );
2095       PntArray(2) = P2d;
2096       Handle( Geom2d_BezierCurve ) Patch = new Geom2d_BezierCurve( PntArray );
2097       Geom2dConvert_CompCurveToBSplineCurve Concat( BCurve, Convert_QuasiAngular );
2098       Concat.Add( Patch, Tol, Standard_True );
2099       BCurve = Concat.BSplineCurve();
2100     }
2101     NewC2d = BCurve;
2102   }
2103   Einf.ChangeFirstPC() = NewC2d;
2104   Handle( Geom2dAdaptor_HCurve ) hcur = new Geom2dAdaptor_HCurve( NewC2d );
2105   Handle( GeomAdaptor_HSurface ) hsur = new GeomAdaptor_HSurface( S1 );
2106   Adaptor3d_CurveOnSurface cons( hcur, hsur );
2107   Handle( Adaptor3d_HCurveOnSurface ) hcons = new Adaptor3d_HCurveOnSurface( cons );
2108   Handle( GeomAdaptor_HSurface ) hsur2 = new GeomAdaptor_HSurface( S2 );
2109   ProjLib_CompProjectedCurve ProjCurve( hsur2, hcons, Tol, Tol );
2110   Handle(ProjLib_HCompProjectedCurve) HProjector = new ProjLib_HCompProjectedCurve();
2111   HProjector->Set( ProjCurve );
2112   Standard_Real Udeb, Ufin;
2113   ProjCurve.Bounds(1, Udeb, Ufin);
2114   Standard_Integer MaxSeg = 20 + HProjector->NbIntervals(GeomAbs_C3);
2115   Approx_CurveOnSurface appr( HProjector, hsur2, Udeb, Ufin, Tol,
2116     GeomAbs_C1, 10, MaxSeg, 
2117     Standard_False, Standard_False );
2118   Einf.ChangeSecondPC() = appr.Curve2d();
2119   Einf.ChangeGeometry() = appr.Curve3d();
2120   Einf.SetNewGeometry( Standard_True );
2121
2122   if (sign == -1)
2123     return Einf.Geometry()->FirstParameter();
2124   else
2125     return Einf.Geometry()->LastParameter();
2126
2127 }
2128
2129 //=======================================================================
2130 //function : Orientation
2131 //purpose  : 
2132 //=======================================================================
2133
2134 static TopAbs_Orientation Orientation(const TopoDS_Shape& S,
2135   const TopoDS_Face& F)
2136 {
2137   //
2138   // change porting NT
2139   //
2140   TopExp_Explorer expl ;
2141   expl.Init(S,
2142     TopAbs_FACE) ;
2143   while (expl.More()) {
2144     if (TopoDS::Face(expl.Current()).IsSame(F)) {
2145       return expl.Current().Orientation();
2146     }
2147     expl.Next();
2148   }
2149   return TopAbs_FORWARD;
2150 }
2151
2152
2153 //=======================================================================
2154 //function : FindRotation
2155 //purpose  : 
2156 //=======================================================================
2157
2158 static Standard_Boolean FindRotation(const gp_Pln& Pl,
2159   const TopAbs_Orientation Oris,
2160   const gp_Dir& Direction,
2161   const Standard_Real Angle,
2162   const gp_Pln& NeutralPlane,
2163   gp_Ax1& Axe,
2164   Standard_Real& theta)
2165 {                                    
2166   IntAna_QuadQuadGeo i2pl(Pl,NeutralPlane,
2167     Precision::Angular(),Precision::Confusion());
2168
2169   if (i2pl.IsDone() && i2pl.TypeInter() == IntAna_Line) {
2170     gp_Lin li = i2pl.Line(1);
2171     // Try to turn around this line
2172     gp_Dir nx = li.Direction();
2173     gp_Dir ny = Pl.Axis().Direction().Crossed(nx);
2174     Standard_Real a = Direction.Dot(nx);
2175     if (Abs(a) <=1-Precision::Angular()) { 
2176       Standard_Real b = Direction.Dot(ny);
2177       Standard_Real c = Direction.Dot(Pl.Axis().Direction());
2178       Standard_Boolean direct(Pl.Position().Direct());
2179       if ((direct && Oris == TopAbs_REVERSED) ||
2180         (!direct && Oris == TopAbs_FORWARD)) {
2181           b = -b;
2182           c = -c;
2183       }
2184       Standard_Real denom = Sqrt(1-a*a);
2185       Standard_Real Sina = Sin(Angle);
2186       if (denom>Abs(Sina)) {
2187         Standard_Real phi = ATan2(b/denom,c/denom);
2188         Standard_Real theta0 = ACos(Sina/denom); 
2189         theta = theta0 - phi;
2190         if (Cos(theta) <0.) {
2191           theta = -theta0 -phi;
2192         }
2193         //  modified by NIZHNY-EAP Tue Nov 16 15:51:38 1999 ___BEGIN___
2194         while (Abs(theta)>M_PI) {
2195           theta = theta + M_PI*(theta<0 ? 1 : -1);
2196         }
2197         //  modified by NIZHNY-EAP Tue Nov 16 15:53:32 1999 ___END___
2198         Axe = li.Position();
2199         return Standard_True;
2200       }
2201     }
2202   }
2203   return Standard_False;
2204 }
2205