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