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