Salome HOME
Remove warnings
[modules/geom.git] / src / GEOMImpl / GEOMImpl_ShapeDriver.cxx
1 //  Copyright (C) 2007-2010  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.
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 <GEOMImpl_ShapeDriver.hxx>
24
25 #include <GEOMImpl_IShapes.hxx>
26 #include <GEOMImpl_Types.hxx>
27 #include <GEOMImpl_Block6Explorer.hxx>
28
29 #include <GEOM_Function.hxx>
30
31 // OCCT Includes
32 #include <ShapeFix_Wire.hxx>
33 #include <ShapeFix_Edge.hxx>
34 #include <ShapeFix_Shape.hxx>
35
36 #include <BRep_Tool.hxx>
37 #include <BRep_Builder.hxx>
38 #include <BRepAlgo_FaceRestrictor.hxx>
39 #include <BRepBuilderAPI_Sewing.hxx>
40 #include <BRepBuilderAPI_Copy.hxx>
41 #include <BRepCheck.hxx>
42 #include <BRepCheck_Analyzer.hxx>
43 #include <BRepCheck_Shell.hxx>
44 #include <BRepClass3d_SolidClassifier.hxx>
45 #include <BRepBuilderAPI_MakeWire.hxx>
46 #include <BRepBuilderAPI_Sewing.hxx>
47 #include <BRepLib.hxx>
48 #include <BRepLib_MakeEdge.hxx>
49 #include <BRepTools_WireExplorer.hxx>
50 #include <BRepAdaptor_Curve.hxx>
51
52 #include <ShapeAnalysis_FreeBounds.hxx>
53 #include <ElCLib.hxx>
54
55 #include <TopAbs.hxx>
56 #include <TopoDS.hxx>
57 #include <TopoDS_Shape.hxx>
58 #include <TopoDS_Edge.hxx>
59 #include <TopoDS_Wire.hxx>
60 #include <TopoDS_Shell.hxx>
61 #include <TopoDS_Solid.hxx>
62 #include <TopoDS_Compound.hxx>
63 #include <TopoDS_Iterator.hxx>
64 #include <TopExp.hxx>
65 #include <TopExp_Explorer.hxx>
66
67 #include <TopTools_MapOfShape.hxx>
68 #include <TopTools_HSequenceOfShape.hxx>
69
70 #include <TColStd_SequenceOfReal.hxx>
71 #include <TColStd_HSequenceOfTransient.hxx>
72 #include <TColStd_Array1OfReal.hxx>
73 #include <TColGeom_SequenceOfCurve.hxx>
74 #include <TColGeom_Array1OfBSplineCurve.hxx>
75 #include <TColGeom_HArray1OfBSplineCurve.hxx>
76
77 #include <GeomAbs_CurveType.hxx>
78 #include <Geom_TrimmedCurve.hxx>
79 #include <GeomConvert_CompCurveToBSplineCurve.hxx>
80 #include <GeomConvert.hxx>
81 #include <GeomLProp.hxx>
82
83 #include <Precision.hxx>
84 #include <Standard_NullObject.hxx>
85 #include <Standard_TypeMismatch.hxx>
86 #include <Standard_ConstructionError.hxx>
87
88 //=======================================================================
89 //function : GetID
90 //purpose  :
91 //=======================================================================
92 const Standard_GUID& GEOMImpl_ShapeDriver::GetID()
93 {
94   static Standard_GUID aShapeDriver("FF1BBB54-5D14-4df2-980B-3A668264EA16");
95   return aShapeDriver;
96 }
97
98
99 //=======================================================================
100 //function : GEOMImpl_ShapeDriver
101 //purpose  :
102 //=======================================================================
103 GEOMImpl_ShapeDriver::GEOMImpl_ShapeDriver()
104 {
105 }
106
107 //=======================================================================
108 //function : Execute
109 //purpose  :
110 //=======================================================================
111 Standard_Integer GEOMImpl_ShapeDriver::Execute(TFunction_Logbook& log) const
112 {
113   if (Label().IsNull()) return 0;
114   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
115
116   GEOMImpl_IShapes aCI (aFunction);
117   Standard_Integer aType = aFunction->GetType();
118
119   TopoDS_Shape aShape;
120   BRep_Builder B;
121
122   if (aType == WIRE_EDGES) {
123     Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
124     TopoDS_Wire aWire;
125     B.MakeWire(aWire);
126
127     // add edges
128     for (unsigned int ind = 1; ind <= aShapes->Length(); ind++) {
129       Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
130       TopoDS_Shape aShape_i = aRefShape->GetValue();
131       if (aShape_i.IsNull()) {
132         Standard_NullObject::Raise("Shape for wire construction is null");
133       }
134      if (aShape_i.ShapeType() == TopAbs_EDGE || aShape_i.ShapeType() == TopAbs_WIRE) {
135        TopExp_Explorer exp (aShape_i, TopAbs_EDGE);
136        for (; exp.More(); exp.Next())
137          B.Add(aWire, TopoDS::Edge(exp.Current()));
138      } else {
139        Standard_TypeMismatch::Raise
140          ("Shape for wire construction is neither an edge nor a wire");
141      }
142     }
143
144     // fix edges order
145     Handle(ShapeFix_Wire) aFW = new ShapeFix_Wire;
146     aFW->Load(aWire);
147     aFW->FixReorder();
148     
149     if (aFW->StatusReorder(ShapeExtend_FAIL1)) {
150       Standard_ConstructionError::Raise("Wire construction failed: several loops detected");
151     } else if (aFW->StatusReorder(ShapeExtend_FAIL)) {
152       Standard_ConstructionError::Raise("Wire construction failed");
153     } else {
154     }
155
156     // IMP 0019766: Building a Wire from unconnected edges by introducing a tolerance
157     Standard_Real aTolerance = aCI.GetTolerance();
158     if (aTolerance < Precision::Confusion())
159       aTolerance = Precision::Confusion();
160     
161     aFW->ClosedWireMode() = Standard_False;
162     aFW->FixConnected(aTolerance);
163     if (aFW->StatusConnected(ShapeExtend_FAIL)) {
164       Standard_ConstructionError::Raise("Wire construction failed: cannot build connected wire");
165     }
166     // IMP 0019766
167     if (aFW->StatusConnected(ShapeExtend_DONE3)) {
168       // Confused with <prec> but not Analyzer.Precision(), set the same
169       aFW->FixGapsByRangesMode() = Standard_True;
170       if (aFW->FixGaps3d()) {
171         Handle(ShapeExtend_WireData) sbwd = aFW->WireData();
172         Handle(ShapeFix_Edge) aFe = new ShapeFix_Edge;
173         for (Standard_Integer iedge = 1; iedge <= sbwd->NbEdges(); iedge++) {
174           TopoDS_Edge aEdge = TopoDS::Edge(sbwd->Edge(iedge));
175           aFe->FixVertexTolerance(aEdge);
176           aFe->FixSameParameter(aEdge);
177         }
178       }
179       else if (aFW->StatusGaps3d(ShapeExtend_FAIL)) {
180         Standard_ConstructionError::Raise("Wire construction failed: cannot fix 3d gaps");
181       }
182     }
183       aShape = aFW->WireAPIMake();
184   }
185   else if (aType == FACE_WIRE) {
186     Handle(GEOM_Function) aRefBase = aCI.GetBase();
187     TopoDS_Shape aShapeBase = aRefBase->GetValue();
188     if (aShapeBase.IsNull()) Standard_NullObject::Raise("Argument Shape is null");
189     TopoDS_Wire W;
190     if (aShapeBase.ShapeType() == TopAbs_WIRE) {
191       W = TopoDS::Wire(aShapeBase);
192       // check the wire is closed
193       TopoDS_Vertex aV1, aV2;
194       TopExp::Vertices(W, aV1, aV2);
195       if ( !aV1.IsNull() && !aV2.IsNull() && aV1.IsSame(aV2) )
196         aShapeBase.Closed(true);
197       else
198         Standard_NullObject::Raise
199           ("Shape for face construction is not closed");
200     }
201     else if (aShapeBase.ShapeType() == TopAbs_EDGE && aShapeBase.Closed()) {
202       BRepBuilderAPI_MakeWire MW;
203       MW.Add(TopoDS::Edge(aShapeBase));
204       if (!MW.IsDone()) {
205         Standard_ConstructionError::Raise("Wire construction failed");
206       }
207           W = MW;
208     }
209     else {
210       Standard_NullObject::Raise
211         ("Shape for face construction is neither a wire nor a closed edge");
212     }
213     GEOMImpl_Block6Explorer::MakeFace(W, aCI.GetIsPlanar(), aShape);
214     if (aShape.IsNull()) {
215       Standard_ConstructionError::Raise("Face construction failed");
216     }
217   }
218   else if (aType == FACE_WIRES) {
219     // Try to build a face from a set of wires and edges
220     int ind;
221
222     Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
223     int nbshapes = aShapes->Length();
224     if (nbshapes < 1) {
225       Standard_ConstructionError::Raise("No wires or edges given");
226     }
227
228     // 1. Extract all edges from the given arguments
229     TopTools_MapOfShape aMapEdges;
230     Handle(TopTools_HSequenceOfShape) aSeqEdgesIn = new TopTools_HSequenceOfShape;
231
232     for (ind = 1; ind <= nbshapes; ind++) {
233       Handle(GEOM_Function) aRefSh_i = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
234       TopoDS_Shape aSh_i = aRefSh_i->GetValue();
235
236       TopExp_Explorer anExpE_i (aSh_i, TopAbs_EDGE);
237       for (; anExpE_i.More(); anExpE_i.Next()) {
238         if (aMapEdges.Add(anExpE_i.Current())) {
239           aSeqEdgesIn->Append(anExpE_i.Current());
240         }
241       }
242     }
243
244     // 2. Connect edges to wires of maximum length
245     Handle(TopTools_HSequenceOfShape) aSeqWiresOut;
246     ShapeAnalysis_FreeBounds::ConnectEdgesToWires(aSeqEdgesIn, Precision::Confusion(),
247                                                   /*shared*/Standard_False, aSeqWiresOut);
248
249     // 3. Separate closed wires
250     Handle(TopTools_HSequenceOfShape) aSeqClosedWires = new TopTools_HSequenceOfShape;
251     Handle(TopTools_HSequenceOfShape) aSeqOpenWires = new TopTools_HSequenceOfShape;
252     for (ind = 1; ind <= aSeqWiresOut->Length(); ind++) {
253       if (aSeqWiresOut->Value(ind).Closed())
254         aSeqClosedWires->Append(aSeqWiresOut->Value(ind));
255       else
256         aSeqOpenWires->Append(aSeqWiresOut->Value(ind));
257     }
258
259     if (aSeqClosedWires->Length() < 1) {
260       Standard_ConstructionError::Raise
261         ("There is no closed contour can be built from the given arguments");
262     }
263
264     // 4. Build a face / list of faces from all the obtained closed wires
265
266     // 4.a. Basic face
267     TopoDS_Shape aFFace;
268     TopoDS_Wire aW1 = TopoDS::Wire(aSeqClosedWires->Value(1));
269     GEOMImpl_Block6Explorer::MakeFace(aW1, aCI.GetIsPlanar(), aFFace);
270     if (aFFace.IsNull()) {
271       Standard_ConstructionError::Raise("Face construction failed");
272     }
273
274     // 4.b. Add other wires
275     if (aSeqClosedWires->Length() == 1) {
276       aShape = aFFace;
277     }
278     else {
279       TopoDS_Compound C;
280       BRep_Builder aBuilder;
281       aBuilder.MakeCompound(C);
282       BRepAlgo_FaceRestrictor FR;
283
284       TopAbs_Orientation OriF = aFFace.Orientation();
285       TopoDS_Shape aLocalS = aFFace.Oriented(TopAbs_FORWARD);
286       FR.Init(TopoDS::Face(aLocalS), Standard_False, Standard_True);
287
288       for (ind = 1; ind <= aSeqClosedWires->Length(); ind++) {
289         TopoDS_Wire aW = TopoDS::Wire(aSeqClosedWires->Value(ind));
290         FR.Add(aW);
291       }
292
293       FR.Perform();
294
295       if (FR.IsDone()) {
296         int k = 0;
297         TopoDS_Shape aFace;
298         for (; FR.More(); FR.Next()) {
299           aFace = FR.Current().Oriented(OriF);
300           aBuilder.Add(C, aFace);
301           k++;
302         }
303         if (k == 1) {
304           aShape = aFace;
305         } else {
306           aShape = C;
307         }
308       }
309     }
310
311     // 5. Add all open wires to the result
312     if (aSeqOpenWires->Length() > 0) {
313       //Standard_ConstructionError::Raise("There are some open wires");
314       TopoDS_Compound C;
315       BRep_Builder aBuilder;
316       if (aSeqClosedWires->Length() == 1) {
317         aBuilder.MakeCompound(C);
318         aBuilder.Add(C, aShape);
319       }
320       else {
321         C = TopoDS::Compound(aShape);
322       }
323
324       for (ind = 1; ind <= aSeqOpenWires->Length(); ind++) {
325         aBuilder.Add(C, aSeqOpenWires->Value(ind));
326       }
327
328       aShape = C;
329     }
330   }
331   else if (aType == SHELL_FACES) {
332     Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
333     unsigned int ind, nbshapes = aShapes->Length();
334
335     // add faces
336     BRepBuilderAPI_Sewing aSewing(Precision::Confusion()*10.0);
337     for (ind = 1; ind <= nbshapes; ind++) {
338       Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
339       TopoDS_Shape aShape_i = aRefShape->GetValue();
340       if (aShape_i.IsNull()) {
341         Standard_NullObject::Raise("Face for shell construction is null");
342       }
343       aSewing.Add(aShape_i);
344     }
345
346     aSewing.Perform();
347
348     TopoDS_Shape sh = aSewing.SewedShape();
349     if( sh.ShapeType()==TopAbs_FACE && nbshapes==1 ) {
350       // case for creation of shell from one face - PAL12722 (skl 26.06.2006)
351       TopoDS_Shell ss;
352       B.MakeShell(ss);
353       B.Add(ss,sh);
354       aShape = ss;
355     }
356     else {
357       //TopExp_Explorer exp (aSewing.SewedShape(), TopAbs_SHELL);
358       TopExp_Explorer exp (sh, TopAbs_SHELL);
359       Standard_Integer ish = 0;
360       for (; exp.More(); exp.Next()) {
361         aShape = exp.Current();
362         ish++;
363       }
364
365       if (ish != 1)
366         aShape = aSewing.SewedShape();
367     }
368
369   }
370   else if (aType == SOLID_SHELL) {
371     Handle(GEOM_Function) aRefShell = aCI.GetBase();
372     TopoDS_Shape aShapeShell = aRefShell->GetValue();
373     if (aShapeShell.IsNull() || aShapeShell.ShapeType() != TopAbs_SHELL) {
374       Standard_NullObject::Raise("Shape for solid construction is null or not a shell");
375     }
376
377     BRepCheck_Shell chkShell(TopoDS::Shell(aShapeShell));
378     if(chkShell.Closed() == BRepCheck_NotClosed) return 0;
379
380     TopoDS_Solid Sol;
381     B.MakeSolid(Sol);
382     B.Add(Sol, aShapeShell);
383     BRepClass3d_SolidClassifier SC (Sol);
384     SC.PerformInfinitePoint(Precision::Confusion());
385     if (SC.State() == TopAbs_IN) {
386       B.MakeSolid(Sol);
387       B.Add(Sol, aShapeShell.Reversed());
388     }
389
390     aShape = Sol;
391
392   }
393   else if (aType == SOLID_SHELLS) {
394     Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
395     unsigned int ind, nbshapes = aShapes->Length();
396     Standard_Integer ish = 0;
397     TopoDS_Solid Sol;
398     B.MakeSolid(Sol);
399
400     // add shapes
401     for (ind = 1; ind <= nbshapes; ind++) {
402       Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
403       TopoDS_Shape aShapeShell = aRefShape->GetValue();
404       if (aShapeShell.IsNull()) {
405         Standard_NullObject::Raise("Shell for solid construction is null");
406       }
407       if (aShapeShell.ShapeType() == TopAbs_SHELL) {
408         B.Add(Sol, aShapeShell);
409         ish++;
410       }
411     }
412     if ( ish == 0 ) return 0;
413     BRepClass3d_SolidClassifier SC (Sol);
414     SC.PerformInfinitePoint(Precision::Confusion());
415     if (SC.State() == TopAbs_IN)
416       aShape = Sol.Reversed();
417     else
418       aShape = Sol;
419   }
420   else if (aType == COMPOUND_SHAPES) {
421     Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
422     unsigned int ind, nbshapes = aShapes->Length();
423
424     // add shapes
425     TopoDS_Compound C;
426     B.MakeCompound(C);
427     for (ind = 1; ind <= nbshapes; ind++) {
428       Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
429       TopoDS_Shape aShape_i = aRefShape->GetValue();
430       if (aShape_i.IsNull()) {
431         Standard_NullObject::Raise("Shape for compound construction is null");
432       }
433       B.Add(C, aShape_i);
434     }
435
436     aShape = C;
437
438   }
439   else if (aType == REVERSE_ORIENTATION) {
440     Handle(GEOM_Function) aRefShape = aCI.GetBase();
441     TopoDS_Shape aShape_i = aRefShape->GetValue();
442     if (aShape_i.IsNull()) {
443        Standard_NullObject::Raise("Shape for reverse is null");
444     }
445
446     BRepBuilderAPI_Copy Copy(aShape_i);
447     if( Copy.IsDone() ) {
448       TopoDS_Shape tds = Copy.Shape();
449       if( tds.IsNull() ) {
450         Standard_ConstructionError::Raise("Orientation aborted : Can not reverse the shape");
451       }
452
453       if( tds.Orientation() == TopAbs_FORWARD)
454         tds.Orientation(TopAbs_REVERSED);
455       else
456         tds.Orientation(TopAbs_FORWARD);
457
458       aShape = tds;
459     }
460   }
461   else if (aType == EDGE_WIRE) {
462     Handle(GEOM_Function) aRefBase = aCI.GetBase();
463     TopoDS_Shape aWire = aRefBase->GetValue();
464     Standard_Real LinTol = aCI.GetTolerance();
465     Standard_Real AngTol = aCI.GetAngularTolerance();
466     if (aWire.IsNull()) Standard_NullObject::Raise("Argument Wire is null");
467
468     TopoDS_Edge ResEdge;
469
470     BRepLib::BuildCurves3d(aWire);
471     Handle(ShapeFix_Shape) Fixer = new ShapeFix_Shape(aWire);
472     Fixer->SetPrecision(LinTol);
473     Fixer->SetMaxTolerance(LinTol);
474     Fixer->Perform();
475     TopoDS_Wire theWire = TopoDS::Wire(Fixer->Shape());
476
477     TColGeom_SequenceOfCurve CurveSeq;
478     TopTools_SequenceOfShape LocSeq;
479     TColStd_SequenceOfReal FparSeq;
480     TColStd_SequenceOfReal LparSeq;
481     TColStd_SequenceOfReal TolSeq;
482     GeomAbs_CurveType CurType;
483     TopoDS_Vertex FirstVertex, LastVertex;
484
485     BRepTools_WireExplorer wexp(theWire) ;
486     for (; wexp.More(); wexp.Next())
487     {
488       TopoDS_Edge anEdge = wexp.Current();
489       Standard_Real fpar, lpar;
490       TopLoc_Location aLoc;
491       Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aLoc, fpar, lpar);
492       if (aCurve.IsNull())
493         continue;
494
495       BRepAdaptor_Curve BAcurve(anEdge);
496       GeomAbs_CurveType aType = BAcurve.GetType();
497
498       Handle(Geom_Curve) aBasisCurve = BAcurve.Curve().Curve();
499
500       if (aBasisCurve->IsPeriodic())
501         ElCLib::AdjustPeriodic(aBasisCurve->FirstParameter(), aBasisCurve->LastParameter(),
502                                Precision::PConfusion(), fpar, lpar);
503
504       if (CurveSeq.IsEmpty())
505       {
506         CurveSeq.Append(aCurve);
507         TopoDS_Shape aLocShape;
508         aLocShape.Location(aLoc);
509         aLocShape.Orientation(wexp.Orientation());
510         LocSeq.Append(aLocShape);
511         FparSeq.Append(fpar);
512         LparSeq.Append(lpar);
513         CurType = aType;
514         FirstVertex = wexp.CurrentVertex();
515       }
516       else
517       {
518         Standard_Boolean Done = Standard_False;
519         Standard_Real NewFpar, NewLpar;
520         GeomAdaptor_Curve GAprevcurve(CurveSeq.Last());
521         TopoDS_Vertex CurVertex = wexp.CurrentVertex();
522         TopoDS_Vertex CurFirstVer = TopExp::FirstVertex(anEdge);
523         TopAbs_Orientation ConnectByOrigin = (CurVertex.IsSame(CurFirstVer))? TopAbs_FORWARD : TopAbs_REVERSED;
524         if (aCurve == CurveSeq.Last())
525         {
526           NewFpar = fpar;
527           NewLpar = lpar;
528           if (aBasisCurve->IsPeriodic())
529           {
530             if (NewLpar < NewFpar)
531               NewLpar += aBasisCurve->Period();
532             if (ConnectByOrigin == TopAbs_FORWARD)
533               ElCLib::AdjustPeriodic(FparSeq.Last(),
534                                      FparSeq.Last() + aBasisCurve->Period(),
535                                      Precision::PConfusion(), NewFpar, NewLpar);
536             else
537               ElCLib::AdjustPeriodic(FparSeq.Last() - aBasisCurve->Period(),
538                                      FparSeq.Last(),
539                                      Precision::PConfusion(), NewFpar, NewLpar);
540           }
541           Done = Standard_True;
542         }
543         else if (aType == CurType &&
544                  aType != GeomAbs_BezierCurve &&
545                  aType != GeomAbs_BSplineCurve &&
546                  aType != GeomAbs_OtherCurve)
547         {
548           switch (aType)
549           {
550           case GeomAbs_Line:
551             {
552               gp_Lin aLine    = BAcurve.Line();
553               gp_Lin PrevLine = GAprevcurve.Line(); 
554               if (aLine.Contains(PrevLine.Location(), LinTol) &&
555                   aLine.Direction().IsParallel(PrevLine.Direction(), AngTol))
556               {
557                 gp_Pnt P1 = ElCLib::Value(fpar, aLine);
558                 gp_Pnt P2 = ElCLib::Value(lpar, aLine);
559                 NewFpar = ElCLib::Parameter(PrevLine, P1);
560                 NewLpar = ElCLib::Parameter(PrevLine, P2);
561                 if (NewLpar < NewFpar)
562                 {
563                   Standard_Real MemNewFpar = NewFpar;
564                   NewFpar = NewLpar;
565                   NewLpar = MemNewFpar;
566                   ConnectByOrigin = TopAbs::Reverse(ConnectByOrigin);
567                 }
568                 Done = Standard_True;
569               }
570               break;
571             }
572           case GeomAbs_Circle:
573             {
574               gp_Circ aCircle    = BAcurve.Circle();
575               gp_Circ PrevCircle = GAprevcurve.Circle();
576               if (aCircle.Location().Distance(PrevCircle.Location()) <= LinTol &&
577                   Abs(aCircle.Radius() - PrevCircle.Radius()) <= LinTol &&
578                   aCircle.Axis().IsParallel(PrevCircle.Axis(), AngTol))
579               {
580                 if (aCircle.Axis().Direction() * PrevCircle.Axis().Direction() < 0.)
581                 {
582                   Standard_Real memfpar = fpar;
583                   fpar = lpar;
584                   lpar = memfpar;
585                   ConnectByOrigin = TopAbs::Reverse(ConnectByOrigin);
586                 }
587                 gp_Pnt P1 = ElCLib::Value(fpar, aCircle);
588                 gp_Pnt P2 = ElCLib::Value(lpar, aCircle);
589                 NewFpar = ElCLib::Parameter(PrevCircle, P1);
590                 NewLpar = ElCLib::Parameter(PrevCircle, P2);
591                 if (NewLpar < NewFpar)
592                   NewLpar += 2.*PI;
593                 //Standard_Real MemNewFpar = NewFpar, MemNewLpar =  NewLpar;
594                 if (ConnectByOrigin == TopAbs_FORWARD)
595                   ElCLib::AdjustPeriodic(FparSeq.Last(),
596                                          FparSeq.Last() + 2.*PI,
597                                          Precision::PConfusion(), NewFpar, NewLpar);
598                 else
599                   ElCLib::AdjustPeriodic(FparSeq.Last() - 2.*PI,
600                                          FparSeq.Last(),
601                                          Precision::PConfusion(), NewFpar, NewLpar);
602                 Done = Standard_True;
603               }
604               break;
605             }
606           case GeomAbs_Ellipse:
607             {
608               gp_Elips anEllipse   = BAcurve.Ellipse();
609               gp_Elips PrevEllipse = GAprevcurve.Ellipse();
610               if (anEllipse.Focus1().Distance(PrevEllipse.Focus1()) <= LinTol &&
611                   anEllipse.Focus2().Distance(PrevEllipse.Focus2()) <= LinTol &&
612                   Abs(anEllipse.MajorRadius() - PrevEllipse.MajorRadius()) <= LinTol &&
613                   Abs(anEllipse.MinorRadius() - PrevEllipse.MinorRadius()) <= LinTol &&
614                   anEllipse.Axis().IsParallel(PrevEllipse.Axis(), AngTol))
615               {
616                 if (anEllipse.Axis().Direction() * PrevEllipse.Axis().Direction() < 0.)
617                 {
618                   Standard_Real memfpar = fpar;
619                   fpar = lpar;
620                   lpar = memfpar;
621                   ConnectByOrigin = TopAbs::Reverse(ConnectByOrigin);
622                 }
623                 gp_Pnt P1 = ElCLib::Value(fpar, anEllipse);
624                 gp_Pnt P2 = ElCLib::Value(lpar, anEllipse);
625                 NewFpar = ElCLib::Parameter(PrevEllipse, P1);
626                 NewLpar = ElCLib::Parameter(PrevEllipse, P2);
627                 if (NewLpar < NewFpar)
628                   NewLpar += 2.*PI;
629                 if (ConnectByOrigin == TopAbs_FORWARD)
630                   ElCLib::AdjustPeriodic(FparSeq.Last(),
631                                          FparSeq.Last() + 2.*PI,
632                                          Precision::PConfusion(), NewFpar, NewLpar);
633                 else
634                   ElCLib::AdjustPeriodic(FparSeq.Last() - 2.*PI,
635                                          FparSeq.Last(),
636                                          Precision::PConfusion(), NewFpar, NewLpar);
637                 Done = Standard_True;
638               }
639               break;
640             }
641           case GeomAbs_Hyperbola:
642             {
643               gp_Hypr aHypr    = BAcurve.Hyperbola();
644               gp_Hypr PrevHypr = GAprevcurve.Hyperbola();
645               if (aHypr.Focus1().Distance(PrevHypr.Focus1()) <= LinTol &&
646                   aHypr.Focus2().Distance(PrevHypr.Focus2()) <= LinTol &&
647                   Abs(aHypr.MajorRadius() - PrevHypr.MajorRadius()) <= LinTol &&
648                   Abs(aHypr.MinorRadius() - PrevHypr.MinorRadius()) <= LinTol &&
649                   aHypr.Axis().IsParallel(PrevHypr.Axis(), AngTol))
650               {
651                 gp_Pnt P1 = ElCLib::Value(fpar, aHypr);
652                 gp_Pnt P2 = ElCLib::Value(lpar, aHypr);
653                 NewFpar = ElCLib::Parameter(PrevHypr, P1);
654                 NewLpar = ElCLib::Parameter(PrevHypr, P2);
655                 if (NewLpar < NewFpar)
656                 {
657                   Standard_Real MemNewFpar = NewFpar;
658                   NewFpar = NewLpar;
659                   NewLpar = MemNewFpar;
660                   ConnectByOrigin = TopAbs::Reverse(ConnectByOrigin);
661                 }
662                 Done = Standard_True;
663               }
664               break;
665             }
666           case GeomAbs_Parabola:
667             {
668               gp_Parab aParab    = BAcurve.Parabola();
669               gp_Parab PrevParab = GAprevcurve.Parabola();
670               if (aParab.Location().Distance(PrevParab.Location()) <= LinTol &&
671                   aParab.Focus().Distance(PrevParab.Focus()) <= LinTol &&
672                   Abs(aParab.Focal() - PrevParab.Focal()) <= LinTol &&
673                   aParab.Axis().IsParallel(PrevParab.Axis(), AngTol))
674               {
675                 gp_Pnt P1 = ElCLib::Value(fpar, aParab);
676                 gp_Pnt P2 = ElCLib::Value(lpar, aParab);
677                 NewFpar = ElCLib::Parameter(PrevParab, P1);
678                 NewLpar = ElCLib::Parameter(PrevParab, P2);
679                 if (NewLpar < NewFpar)
680                 {
681                   Standard_Real MemNewFpar = NewFpar;
682                   NewFpar = NewLpar;
683                   NewLpar = MemNewFpar;
684                   ConnectByOrigin = TopAbs::Reverse(ConnectByOrigin);
685                 }
686                 Done = Standard_True;
687               }
688               break;
689             }
690           } //end of switch (aType)
691         } // end of else if (aType == CurType && ...
692         if (Done)
693         {
694           if (NewFpar < FparSeq.Last())
695             FparSeq(FparSeq.Length()) = NewFpar;
696           else
697             LparSeq(LparSeq.Length()) = NewLpar;
698         }
699         else
700         {
701           CurveSeq.Append(aCurve);
702           TopoDS_Shape aLocShape;
703           aLocShape.Location(aLoc);
704           aLocShape.Orientation(wexp.Orientation());
705           LocSeq.Append(aLocShape);
706           FparSeq.Append(fpar);
707           LparSeq.Append(lpar);
708           TolSeq.Append(BRep_Tool::Tolerance(CurVertex));
709           CurType = aType;
710         }
711       } // end of else (CurveSeq.IsEmpty()) -> not first time
712     } // end for (; wexp.More(); wexp.Next())
713
714     LastVertex = wexp.CurrentVertex();
715     TolSeq.Append(BRep_Tool::Tolerance(LastVertex));
716
717     if (!CurveSeq.IsEmpty())
718     {
719       Standard_Integer nb_curve = CurveSeq.Length();   //number of curves
720       TColGeom_Array1OfBSplineCurve tab(0,nb_curve-1);                    //array of the curves
721       TColStd_Array1OfReal tabtolvertex(0,nb_curve-1); //(0,nb_curve-2);  //array of the tolerances
722       
723       Standard_Integer i;
724       
725       if (nb_curve > 1)
726       {
727         for (i = 1; i <= nb_curve; i++)
728         {
729           if (CurveSeq(i)->IsInstance(STANDARD_TYPE(Geom_TrimmedCurve)))
730             CurveSeq(i) = (*((Handle(Geom_TrimmedCurve)*)&(CurveSeq(i))))->BasisCurve();
731           
732           Handle(Geom_TrimmedCurve) aTrCurve = new Geom_TrimmedCurve(CurveSeq(i), FparSeq(i), LparSeq(i));
733           tab(i-1) = GeomConvert::CurveToBSplineCurve(aTrCurve);
734           tab(i-1)->Transform(LocSeq(i).Location().Transformation());
735           GeomConvert::C0BSplineToC1BSplineCurve(tab(i-1), Precision::Confusion());
736           if (LocSeq(i).Orientation() == TopAbs_REVERSED)
737             tab(i-1)->Reverse();
738           
739           //Temporary
740           //char* name = new char[100];
741           //sprintf(name, "c%d", i);
742           //DrawTrSurf::Set(name, tab(i-1));
743           
744           if (i > 1)
745             tabtolvertex(i-2) = TolSeq(i-1);
746         } // end for (i = 1; i <= nb_curve; i++)
747         tabtolvertex(nb_curve-1) = TolSeq(TolSeq.Length());
748         
749         Standard_Boolean closed_flag = Standard_False;
750         Standard_Real closed_tolerance = 0.;
751         if (FirstVertex.IsSame(LastVertex) &&
752             GeomLProp::Continuity(tab(0), tab(nb_curve-1),
753                                   tab(0)->FirstParameter(),
754                                   tab(nb_curve-1)->LastParameter(),
755                                   Standard_False, Standard_False, LinTol, AngTol) >= GeomAbs_G1)
756         {
757           closed_flag = Standard_True ;
758           closed_tolerance = BRep_Tool::Tolerance(FirstVertex);
759         }
760         
761         Handle(TColGeom_HArray1OfBSplineCurve)  concatcurve;     //array of the concatenated curves
762         Handle(TColStd_HArray1OfInteger)        ArrayOfIndices;  //array of the remining Vertex
763         GeomConvert::ConcatC1(tab,
764                               tabtolvertex,
765                               ArrayOfIndices,
766                               concatcurve,
767                               closed_flag,
768                               closed_tolerance);   //C1 concatenation
769         
770         if (concatcurve->Length() > 1)
771         {
772           GeomConvert_CompCurveToBSplineCurve Concat(concatcurve->Value(concatcurve->Lower()));
773           
774           for (i = concatcurve->Lower()+1; i <= concatcurve->Upper(); i++)
775             Concat.Add( concatcurve->Value(i), LinTol, Standard_True );
776           
777           concatcurve->SetValue(concatcurve->Lower(), Concat.BSplineCurve());
778         }
779         
780         ResEdge = BRepLib_MakeEdge(concatcurve->Value(concatcurve->Lower()),
781                                    FirstVertex, LastVertex);
782       }
783       else
784       {
785         if (CurveSeq(1)->IsInstance(STANDARD_TYPE(Geom_TrimmedCurve)))
786           CurveSeq(1) = (*((Handle(Geom_TrimmedCurve)*)&(CurveSeq(i))))->BasisCurve();
787         
788         CurveSeq(1)->Transform(LocSeq(1).Location().Transformation());
789         ResEdge = BRepLib_MakeEdge(CurveSeq(1),
790                                    FirstVertex, LastVertex,
791                                    FparSeq(1), LparSeq(1));
792       }
793     }
794       
795     aShape = ResEdge;
796   }
797
798   if (aShape.IsNull()) return 0;
799
800   // Check shape validity
801   BRepCheck_Analyzer ana (aShape, false);
802   if (!ana.IsValid()) {
803     //Standard_ConstructionError::Raise("Algorithm have produced an invalid shape result");
804   }
805
806   aFunction->SetValue(aShape);
807
808   log.SetTouched(Label());
809
810   return 1;
811 }
812
813
814 //=======================================================================
815 //function :  GEOMImpl_ShapeDriver_Type_
816 //purpose  :
817 //=======================================================================
818 Standard_EXPORT Handle_Standard_Type& GEOMImpl_ShapeDriver_Type_()
819 {
820
821   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
822   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
823   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
824   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
825   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
826   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
827
828
829   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
830   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_ShapeDriver",
831                                                          sizeof(GEOMImpl_ShapeDriver),
832                                                          1,
833                                                          (Standard_Address)_Ancestors,
834                                                          (Standard_Address)NULL);
835
836   return _aType;
837 }
838
839 //=======================================================================
840 //function : DownCast
841 //purpose  :
842 //=======================================================================
843 const Handle(GEOMImpl_ShapeDriver) Handle(GEOMImpl_ShapeDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
844 {
845   Handle(GEOMImpl_ShapeDriver) _anOtherObject;
846
847   if (!AnObject.IsNull()) {
848      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_ShapeDriver))) {
849        _anOtherObject = Handle(GEOMImpl_ShapeDriver)((Handle(GEOMImpl_ShapeDriver)&)AnObject);
850      }
851   }
852
853   return _anOtherObject;
854 }