]> SALOME platform Git repositories - modules/geom.git/blob - src/GEOMImpl/GEOMImpl_ShapeDriver.cxx
Salome HOME
35224af79f4d042e5f04f99a70c68eb8316b854a
[modules/geom.git] / src / GEOMImpl / GEOMImpl_ShapeDriver.cxx
1 // Copyright (C) 2007-2015  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include <GEOMImpl_ShapeDriver.hxx>
24
25 #include <GEOMImpl_IIsoline.hxx>
26 #include <GEOMImpl_IShapes.hxx>
27 #include <GEOMImpl_IShapeExtend.hxx>
28 #include <GEOMImpl_IVector.hxx>
29 #include <GEOMImpl_Types.hxx>
30 #include <GEOMImpl_Block6Explorer.hxx>
31
32 #include <GEOM_Function.hxx>
33 #include <GEOMUtils_Hatcher.hxx>
34 #include <GEOMAlgo_State.hxx>
35
36 // OCCT Includes
37 #include <ShapeFix_Wire.hxx>
38 #include <ShapeFix_Edge.hxx>
39 #include <ShapeFix_Shape.hxx>
40
41 #include <BRep_Builder.hxx>
42 #include <BRep_Tool.hxx>
43 #include <BRepAdaptor_Curve.hxx>
44 #include <BRepAlgo_FaceRestrictor.hxx>
45 #include <BRepBuilderAPI_Copy.hxx>
46 #include <BRepBuilderAPI_Sewing.hxx>
47 #include <BRepBuilderAPI_MakeFace.hxx>
48 #include <BRepBuilderAPI_MakeWire.hxx>
49 #include <BRepBuilderAPI_MakeEdge.hxx>
50 #include <BRepBuilderAPI_MakeSolid.hxx>
51 #include <BRepCheck.hxx>
52 #include <BRepCheck_Analyzer.hxx>
53 #include <BRepCheck_Shell.hxx>
54 #include <BRepClass3d_SolidClassifier.hxx>
55 #include <BRepLib.hxx>
56 #include <BRepLib_MakeEdge.hxx>
57 #include <BRepTools_WireExplorer.hxx>
58
59 #include <ShapeAnalysis.hxx>
60 #include <ShapeAnalysis_FreeBounds.hxx>
61
62 #include <TNaming_CopyShape.hxx>
63
64 #include <TopAbs.hxx>
65 #include <TopExp.hxx>
66 #include <TopExp_Explorer.hxx>
67 #include <TopoDS.hxx>
68 #include <TopoDS_Shape.hxx>
69 #include <TopoDS_Edge.hxx>
70 #include <TopoDS_Wire.hxx>
71 #include <TopoDS_Shell.hxx>
72 #include <TopoDS_Solid.hxx>
73 #include <TopoDS_Compound.hxx>
74 #include <TopoDS_Iterator.hxx>
75
76 #include <TopTools_MapOfShape.hxx>
77 #include <TopTools_HSequenceOfShape.hxx>
78
79 #include <ElCLib.hxx>
80
81 #include <GCPnts_AbscissaPoint.hxx>
82
83 #include <Geom_TrimmedCurve.hxx>
84 #include <Geom_RectangularTrimmedSurface.hxx>
85 #include <Geom_Surface.hxx>
86 #include <GeomAbs_CurveType.hxx>
87 #include <GeomConvert_CompCurveToBSplineCurve.hxx>
88 #include <GeomConvert.hxx>
89 #include <GeomLProp.hxx>
90
91 #include <TColStd_IndexedDataMapOfTransientTransient.hxx>
92 #include <TColStd_SequenceOfReal.hxx>
93 #include <TColStd_HSequenceOfTransient.hxx>
94 #include <TColStd_Array1OfReal.hxx>
95 #include <TColGeom_SequenceOfCurve.hxx>
96 #include <TColGeom_Array1OfBSplineCurve.hxx>
97 #include <TColGeom_HArray1OfBSplineCurve.hxx>
98
99 #include <Precision.hxx>
100
101 #include <Standard_NullObject.hxx>
102 #include <Standard_TypeMismatch.hxx>
103 #include <Standard_ConstructionError.hxx>
104
105 #include <BOPAlgo_PaveFiller.hxx>
106 #include <BOPAlgo_MakerVolume.hxx>
107
108 #include <list>
109
110 //modified by NIZNHY-PKV Wed Dec 28 13:48:20 2011f
111 //static
112 //  void KeepEdgesOrder(const Handle(TopTools_HSequenceOfShape)& aEdges,
113 //                    const Handle(TopTools_HSequenceOfShape)& aWires);
114 //modified by NIZNHY-PKV Wed Dec 28 13:48:23 2011t
115
116 //=======================================================================
117 //function : GetID
118 //purpose  :
119 //=======================================================================
120 const Standard_GUID& GEOMImpl_ShapeDriver::GetID()
121 {
122   static Standard_GUID aShapeDriver("FF1BBB54-5D14-4df2-980B-3A668264EA16");
123   return aShapeDriver;
124 }
125
126
127 //=======================================================================
128 //function : GEOMImpl_ShapeDriver
129 //purpose  :
130 //=======================================================================
131 GEOMImpl_ShapeDriver::GEOMImpl_ShapeDriver()
132 {
133 }
134
135 //=======================================================================
136 //function : Execute
137 //purpose  :
138 //=======================================================================
139 Standard_Integer GEOMImpl_ShapeDriver::Execute(TFunction_Logbook& log) const
140 {
141   if (Label().IsNull()) return 0;
142   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
143
144   GEOMImpl_IShapes aCI (aFunction);
145   Standard_Integer aType = aFunction->GetType();
146
147   TopoDS_Shape aShape;
148   TCollection_AsciiString aWarning;
149   std::list<TopAbs_ShapeEnum> anExpectedType;
150
151   BRep_Builder B;
152
153   if (aType == WIRE_EDGES) {
154     anExpectedType.push_back(TopAbs_WIRE);
155
156     Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
157
158     Standard_Real aTolerance = aCI.GetTolerance();
159     if (aTolerance < Precision::Confusion())
160       aTolerance = Precision::Confusion();
161
162     aShape = MakeWireFromEdges(aShapes, aTolerance);
163   }
164   else if (aType == FACE_WIRE) {
165     anExpectedType.push_back(TopAbs_FACE);
166
167     Handle(GEOM_Function) aRefBase = aCI.GetBase();
168     TopoDS_Shape aShapeBase = aRefBase->GetValue();
169     if (aShapeBase.IsNull()) Standard_NullObject::Raise("Argument Shape is null");
170     TopoDS_Wire W;
171     if (aShapeBase.ShapeType() == TopAbs_WIRE) {
172       W = TopoDS::Wire(aShapeBase);
173       // check the wire is closed
174       TopoDS_Vertex aV1, aV2;
175       TopExp::Vertices(W, aV1, aV2);
176       if ( !aV1.IsNull() && !aV2.IsNull() && aV1.IsSame(aV2) )
177         aShapeBase.Closed(true);
178       else
179         Standard_NullObject::Raise
180           ("Shape for face construction is not closed");
181     }
182     else if (aShapeBase.ShapeType() == TopAbs_EDGE && BRep_Tool::IsClosed(aShapeBase)) {
183       BRepBuilderAPI_MakeWire MW;
184       MW.Add(TopoDS::Edge(aShapeBase));
185       if (!MW.IsDone()) {
186         Standard_ConstructionError::Raise("Wire construction failed");
187       }
188       W = MW;
189     }
190     else {
191       Standard_NullObject::Raise
192         ("Shape for face construction is neither a wire nor a closed edge");
193     }
194     aWarning = GEOMImpl_Block6Explorer::MakeFace(W, aCI.GetIsPlanar(), aShape);
195     if (aShape.IsNull()) {
196       Standard_ConstructionError::Raise("Face construction failed");
197     }
198   }
199   else if (aType == FACE_WIRES) {
200     anExpectedType.push_back(TopAbs_FACE);
201
202     // Try to build a face from a set of wires and edges
203     int ind;
204
205     Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
206     int nbshapes = aShapes->Length();
207     if (nbshapes < 1) {
208       Standard_ConstructionError::Raise("No wires or edges given");
209     }
210
211     // 1. Extract all edges from the given arguments
212     TopTools_MapOfShape aMapEdges;
213     Handle(TopTools_HSequenceOfShape) aSeqEdgesIn = new TopTools_HSequenceOfShape;
214     TColStd_IndexedDataMapOfTransientTransient aMapTShapes;
215
216     for (ind = 1; ind <= nbshapes; ind++) {
217       Handle(GEOM_Function) aRefSh_i = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
218       TopoDS_Shape aSh_i = aRefSh_i->GetValue();
219
220       TopExp_Explorer anExpE_i (aSh_i, TopAbs_EDGE);
221       for (; anExpE_i.More(); anExpE_i.Next()) {
222         if (aMapEdges.Add(anExpE_i.Current())) {
223           // Copy the original shape.
224           TopoDS_Shape aShapeCopy;
225
226           TNaming_CopyShape::CopyTool
227             (anExpE_i.Current(), aMapTShapes, aShapeCopy);
228           aSeqEdgesIn->Append(aShapeCopy);
229         }
230       }
231     }
232
233     if (aSeqEdgesIn->IsEmpty()) {
234       Standard_ConstructionError::Raise("No edges given");
235     }
236
237     // 2. Connect edges to wires of maximum length
238     Handle(TopTools_HSequenceOfShape) aSeqWiresOut;
239     ShapeAnalysis_FreeBounds::ConnectEdgesToWires(aSeqEdgesIn, Precision::Confusion(),
240                                                   /*shared*/Standard_False, aSeqWiresOut);
241     //modified by NIZNHY-PKV Wed Dec 28 13:46:55 2011f
242     //KeepEdgesOrder(aSeqEdgesIn, aSeqWiresOut);
243     //modified by NIZNHY-PKV Wed Dec 28 13:46:59 2011t
244
245     // 3. Separate closed wires
246     Handle(TopTools_HSequenceOfShape) aSeqClosedWires = new TopTools_HSequenceOfShape;
247     Handle(TopTools_HSequenceOfShape) aSeqOpenWires = new TopTools_HSequenceOfShape;
248     for (ind = 1; ind <= aSeqWiresOut->Length(); ind++) {
249       if (aSeqWiresOut->Value(ind).Closed())
250         aSeqClosedWires->Append(aSeqWiresOut->Value(ind));
251       else
252         aSeqOpenWires->Append(aSeqWiresOut->Value(ind));
253     }
254
255     if (aSeqClosedWires->Length() < 1) {
256       Standard_ConstructionError::Raise
257         ("There is no closed contour can be built from the given arguments");
258     }
259
260     // 4. Build a face / list of faces from all the obtained closed wires
261
262     // 4.a. Basic face
263     TopoDS_Shape aFFace;
264     TopoDS_Wire aW1 = TopoDS::Wire(aSeqClosedWires->Value(1));
265     aWarning = GEOMImpl_Block6Explorer::MakeFace(aW1, aCI.GetIsPlanar(), aFFace);
266     if (aFFace.IsNull()) {
267       Standard_ConstructionError::Raise("Face construction failed");
268     }
269
270     // 4.b. Add other wires
271     if (aSeqClosedWires->Length() == 1) {
272       aShape = aFFace;
273     }
274     else {
275       TopoDS_Compound C;
276       BRep_Builder aBuilder;
277       aBuilder.MakeCompound(C);
278       BRepAlgo_FaceRestrictor FR;
279
280       TopAbs_Orientation OriF = aFFace.Orientation();
281       TopoDS_Shape aLocalS = aFFace.Oriented(TopAbs_FORWARD);
282       FR.Init(TopoDS::Face(aLocalS), Standard_False, Standard_True);
283
284       for (ind = 1; ind <= aSeqClosedWires->Length(); ind++) {
285         TopoDS_Wire aW = TopoDS::Wire(aSeqClosedWires->Value(ind));
286         FR.Add(aW);
287       }
288
289       FR.Perform();
290
291       if (FR.IsDone()) {
292         int k = 0;
293         TopoDS_Shape aFace;
294         for (; FR.More(); FR.Next()) {
295           aFace = FR.Current().Oriented(OriF);
296           aBuilder.Add(C, aFace);
297           k++;
298         }
299         if (k == 1) {
300           aShape = aFace;
301         } else {
302           aShape = C;
303         }
304       }
305     }
306
307     // 5. Add all open wires to the result
308     if (aSeqOpenWires->Length() > 0) {
309       //Standard_ConstructionError::Raise("There are some open wires");
310       TopoDS_Compound C;
311       BRep_Builder aBuilder;
312       if (aSeqClosedWires->Length() == 1) {
313         aBuilder.MakeCompound(C);
314         aBuilder.Add(C, aShape);
315       }
316       else {
317         C = TopoDS::Compound(aShape);
318       }
319
320       for (ind = 1; ind <= aSeqOpenWires->Length(); ind++) {
321         aBuilder.Add(C, aSeqOpenWires->Value(ind));
322       }
323
324       aShape = C;
325     }
326   }
327   else if (aType == FACE_FROM_SURFACE) {
328     anExpectedType.push_back(TopAbs_FACE);
329
330     Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
331
332     if (aShapes.IsNull() == Standard_False) {
333       Standard_Integer aNbShapes = aShapes->Length();
334
335       if (aNbShapes == 2) {
336         Handle(GEOM_Function) aRefFace =
337           Handle(GEOM_Function)::DownCast(aShapes->Value(1));
338         Handle(GEOM_Function) aRefWire =
339           Handle(GEOM_Function)::DownCast(aShapes->Value(2));
340
341         if (aRefFace.IsNull() == Standard_False &&
342             aRefWire.IsNull() == Standard_False) {
343           TopoDS_Shape aShFace = aRefFace->GetValue();
344           TopoDS_Shape aShWire = aRefWire->GetValue();
345
346           if (aShFace.IsNull()    == Standard_False &&
347               aShFace.ShapeType() == TopAbs_FACE    &&
348               aShWire.IsNull()    == Standard_False &&
349               aShWire.ShapeType() == TopAbs_WIRE) {
350             TopoDS_Face             aFace = TopoDS::Face(aShFace);
351             TopoDS_Wire             aWire = TopoDS::Wire(aShWire);
352             Handle(Geom_Surface)    aSurf = BRep_Tool::Surface(aFace);
353             BRepBuilderAPI_MakeFace aMkFace(aSurf, aWire);
354
355             if (aMkFace.IsDone()) {
356               aShape = aMkFace.Shape();
357             }
358           }
359         }
360       }
361     }
362   }
363   else if (aType == SHELL_FACES) {
364     anExpectedType.push_back(TopAbs_SHELL);
365
366     Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
367     unsigned int ind, nbshapes = aShapes->Length();
368
369     // add faces
370     BRepBuilderAPI_Sewing aSewing (Precision::Confusion()*10.0);
371     for (ind = 1; ind <= nbshapes; ind++) {
372       Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
373       TopoDS_Shape aShape_i = aRefShape->GetValue();
374       if (aShape_i.IsNull()) {
375         Standard_NullObject::Raise("Face for shell construction is null");
376       }
377       aSewing.Add(aShape_i);
378     }
379
380     aSewing.Perform();
381
382     TopoDS_Shape sh = aSewing.SewedShape();
383
384     if (sh.ShapeType()==TopAbs_FACE && nbshapes==1) {
385       // case for creation of shell from one face - PAL12722 (skl 26.06.2006)
386       TopoDS_Shell ss;
387       B.MakeShell(ss);
388       B.Add(ss,sh);
389       aShape = ss;
390     }
391     else {
392       //TopExp_Explorer exp (aSewing.SewedShape(), TopAbs_SHELL);
393       TopExp_Explorer exp (sh, TopAbs_SHELL);
394       Standard_Integer ish = 0;
395       for (; exp.More(); exp.Next()) {
396         aShape = exp.Current();
397         ish++;
398       }
399
400       if (ish != 1) {
401         // try the case of one face (Mantis issue 0021809)
402         TopExp_Explorer expF (sh, TopAbs_FACE);
403         Standard_Integer ifa = 0;
404         for (; expF.More(); expF.Next()) {
405           aShape = expF.Current();
406           ifa++;
407         }
408
409         if (ifa == 1) {
410           TopoDS_Shell ss;
411           B.MakeShell(ss);
412           B.Add(ss,aShape);
413           aShape = ss;
414         }
415         else {
416           aShape = aSewing.SewedShape();
417         }
418       }
419     }
420
421   }
422   else if (aType == SOLID_SHELLS) {
423     anExpectedType.push_back(TopAbs_SOLID);
424
425     Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
426     unsigned int ind, nbshapes = aShapes->Length();
427     Standard_Integer ish = 0;
428     BRepBuilderAPI_MakeSolid aMkSolid;
429
430     // add shapes
431     for (ind = 1; ind <= nbshapes; ind++) {
432       Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
433       TopoDS_Shape aShapeShell = aRefShape->GetValue();
434       if (aShapeShell.IsNull()) {
435         Standard_NullObject::Raise("Shell for solid construction is null");
436       }
437       if (aShapeShell.ShapeType() == TopAbs_COMPOUND) {
438         TopoDS_Iterator It (aShapeShell, Standard_True, Standard_True);
439         for (; It.More(); It.Next()) {
440           TopoDS_Shape aSubShape = It.Value();
441           if (aSubShape.ShapeType() == TopAbs_SHELL) {
442             aMkSolid.Add(TopoDS::Shell(aSubShape));
443             ish++;
444           }
445           else
446             Standard_TypeMismatch::Raise
447               ("Shape for solid construction is neither a shell nor a compound of shells");
448         }
449       }
450       else if (aShapeShell.ShapeType() == TopAbs_SHELL) {
451         aMkSolid.Add(TopoDS::Shell(aShapeShell));
452         ish++;
453       }
454     }
455     if (ish == 0 || !aMkSolid.IsDone()) return 0;
456
457     TopoDS_Solid Sol = aMkSolid.Solid();
458     BRepClass3d_SolidClassifier SC (Sol);
459     SC.PerformInfinitePoint(Precision::Confusion());
460     if (SC.State() == TopAbs_IN)
461       aShape = Sol.Reversed();
462     else
463       aShape = Sol;
464   }
465   else if (aType == COMPOUND_SHAPES) {
466     anExpectedType.push_back(TopAbs_COMPOUND);
467
468     Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
469     unsigned int ind, nbshapes = aShapes->Length();
470
471     // add shapes
472     TopoDS_Compound C;
473     B.MakeCompound(C);
474     for (ind = 1; ind <= nbshapes; ind++) {
475       Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
476       TopoDS_Shape aShape_i = aRefShape->GetValue();
477       if (aShape_i.IsNull()) {
478         Standard_NullObject::Raise("Shape for compound construction is null");
479       }
480       B.Add(C, aShape_i);
481     }
482
483     aShape = C;
484
485   }
486   else if (aType == EDGE_WIRE) {
487     anExpectedType.push_back(TopAbs_EDGE);
488
489     Handle(GEOM_Function) aRefBase = aCI.GetBase();
490     TopoDS_Shape aWire = aRefBase->GetValue();
491     Standard_Real LinTol = aCI.GetTolerance();
492     Standard_Real AngTol = aCI.GetAngularTolerance();
493     if (aWire.IsNull()) Standard_NullObject::Raise("Argument Wire is null");
494
495     aShape = MakeEdgeFromWire(aWire, LinTol, AngTol);
496   }
497   else if (aType == SOLID_FACES) {
498     anExpectedType.push_back(TopAbs_SOLID);
499     anExpectedType.push_back(TopAbs_COMPOUND);
500     anExpectedType.push_back(TopAbs_COMPSOLID);
501     
502     Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
503     unsigned int ind, nbshapes = aShapes->Length();
504     
505     // add faces
506     BOPCol_ListOfShape aLS;
507     for (ind = 1; ind <= nbshapes; ind++) {
508       Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
509       TopoDS_Shape aShape_i = aRefShape->GetValue();
510       if (aShape_i.IsNull()) {
511         Standard_NullObject::Raise("Shape for solid construction is null");
512       }
513       if (aShape_i.ShapeType() == TopAbs_COMPOUND) {
514         TopoDS_Iterator It (aShape_i, Standard_True, Standard_True);
515         for (; It.More(); It.Next()) {
516           TopoDS_Shape aSubShape = It.Value();
517           if (aSubShape.ShapeType() == TopAbs_FACE || aSubShape.ShapeType() == TopAbs_SHELL)
518             aLS.Append(aSubShape);
519           else
520             Standard_TypeMismatch::Raise
521               ("Shape for solid construction is neither a list of faces and/or shells "
522                "nor a compound of faces and/or shells");
523         }
524       }
525       aLS.Append(aShape_i);
526     }
527
528     BOPAlgo_MakerVolume aMV;
529     aMV.SetArguments(aLS);
530     aMV.SetIntersect(aCI.GetIsIntersect());
531     aMV.Perform();
532     if (aMV.ErrorStatus()) return 0;
533
534     aShape = aMV.Shape();
535   }
536   else if (aType == EDGE_CURVE_LENGTH) {
537     anExpectedType.push_back(TopAbs_EDGE);
538
539     GEOMImpl_IVector aVI (aFunction);
540
541     // RefCurve
542     Handle(GEOM_Function) aRefCurve = aVI.GetPoint1();
543     if (aRefCurve.IsNull()) Standard_NullObject::Raise("Argument Curve is null");
544     TopoDS_Shape aRefShape1 = aRefCurve->GetValue();
545     if (aRefShape1.ShapeType() != TopAbs_EDGE) {
546       Standard_TypeMismatch::Raise
547         ("Edge On Curve creation aborted : curve shape is not an edge");
548     }
549     TopoDS_Edge aRefEdge = TopoDS::Edge(aRefShape1);
550     TopoDS_Vertex V1, V2;
551     TopExp::Vertices(aRefEdge, V1, V2, Standard_True);
552
553     // RefPoint
554     TopoDS_Vertex aRefVertex;
555     Handle(GEOM_Function) aRefPoint = aVI.GetPoint2();
556     if (aRefPoint.IsNull()) {
557       aRefVertex = V1;
558     }
559     else {
560       TopoDS_Shape aRefShape2 = aRefPoint->GetValue();
561       if (aRefShape2.ShapeType() != TopAbs_VERTEX) {
562         Standard_TypeMismatch::Raise
563           ("Edge On Curve creation aborted : start point shape is not a vertex");
564       }
565       aRefVertex = TopoDS::Vertex(aRefShape2);
566     }
567     gp_Pnt aRefPnt = BRep_Tool::Pnt(aRefVertex);
568
569     // Length
570     Standard_Real aLength = aVI.GetParameter();
571     //Standard_Real aCurveLength = IntTools::Length(aRefEdge);
572     //if (aLength > aCurveLength) {
573     //  Standard_ConstructionError::Raise
574     //    ("Edge On Curve creation aborted : given length is greater than edges length");
575     //}
576     if (fabs(aLength) < Precision::Confusion()) {
577       Standard_ConstructionError::Raise
578         ("Edge On Curve creation aborted : given length is smaller than Precision::Confusion()");
579     }
580
581     // Check orientation
582     Standard_Real UFirst, ULast;
583     Handle(Geom_Curve) EdgeCurve = BRep_Tool::Curve(aRefEdge, UFirst, ULast);
584     Handle(Geom_Curve) ReOrientedCurve = EdgeCurve;
585
586     Standard_Real dU = ULast - UFirst;
587     Standard_Real par1 = UFirst + 0.1 * dU;
588     Standard_Real par2 = ULast  - 0.1 * dU;
589
590     gp_Pnt P1 = EdgeCurve->Value(par1);
591     gp_Pnt P2 = EdgeCurve->Value(par2);
592
593     if (aRefPnt.SquareDistance(P2) < aRefPnt.SquareDistance(P1)) {
594       ReOrientedCurve = EdgeCurve->Reversed();
595       UFirst = EdgeCurve->ReversedParameter(ULast);
596     }
597
598     // Get the point by length
599     GeomAdaptor_Curve AdapCurve = GeomAdaptor_Curve(ReOrientedCurve);
600     GCPnts_AbscissaPoint anAbsPnt (AdapCurve, aLength, UFirst);
601     Standard_Real aParam = anAbsPnt.Parameter();
602
603     if (AdapCurve.IsClosed() && aLength < 0.0) {
604       Standard_Real aTmp = aParam;
605       aParam = UFirst;
606       UFirst = aTmp;
607     }
608
609     BRepBuilderAPI_MakeEdge aME (ReOrientedCurve, UFirst, aParam);
610     if (aME.IsDone())
611       aShape = aME.Shape();
612   } else if (aType == SHAPE_ISOLINE) {
613     GEOMImpl_IIsoline     aII (aFunction);
614     Handle(GEOM_Function) aRefFace = aII.GetFace();
615     TopoDS_Shape          aShapeFace = aRefFace->GetValue();
616
617     if (aShapeFace.ShapeType() == TopAbs_FACE) {
618       TopoDS_Face   aFace  = TopoDS::Face(aShapeFace);
619       bool          isUIso = aII.GetIsUIso();
620       Standard_Real aParam = aII.GetParameter();
621       Standard_Real U1,U2,V1,V2;
622
623       // Construct a real geometric parameter.
624       aFace.Orientation(TopAbs_FORWARD);
625       ShapeAnalysis::GetFaceUVBounds(aFace,U1,U2,V1,V2);
626
627       if (isUIso) {
628         aParam = U1 + (U2 - U1)*aParam;
629       } else {
630         aParam = V1 + (V2 - V1)*aParam;
631       }
632
633       aShape = MakeIsoline(aFace, isUIso, aParam);
634     } else {
635       Standard_NullObject::Raise
636         ("Shape for isoline construction is not a face");
637     }
638   } else if (aType == EDGE_UV) {
639     anExpectedType.push_back(TopAbs_EDGE);
640     GEOMImpl_IShapeExtend aSE (aFunction);
641     Handle(GEOM_Function) aRefEdge   = aSE.GetShape();
642     TopoDS_Shape          aShapeEdge = aRefEdge->GetValue();
643
644     if (aShapeEdge.ShapeType() == TopAbs_EDGE) {
645       TopoDS_Edge anEdge = TopoDS::Edge(aShapeEdge);
646
647       aShape = ExtendEdge(anEdge, aSE.GetUMin(), aSE.GetUMax());
648     }
649   } else if (aType == FACE_UV) {
650     anExpectedType.push_back(TopAbs_FACE);
651
652     GEOMImpl_IShapeExtend aSE (aFunction);
653     Handle(GEOM_Function) aRefFace   = aSE.GetShape();
654     TopoDS_Shape          aShapeFace = aRefFace->GetValue();
655
656     if (aShapeFace.ShapeType() == TopAbs_FACE) {
657       TopoDS_Face aFace = TopoDS::Face(aShapeFace);
658
659       aFace.Orientation(TopAbs_FORWARD);
660       aShape = ExtendFace(aFace, aSE.GetUMin(), aSE.GetUMax(),
661                           aSE.GetVMin(), aSE.GetVMax()); 
662     }
663   } else if (aType == SURFACE_FROM_FACE) {
664     anExpectedType.push_back(TopAbs_FACE);
665
666     GEOMImpl_IShapeExtend aSE (aFunction);
667     Handle(GEOM_Function) aRefFace   = aSE.GetShape();
668     TopoDS_Shape          aShapeFace = aRefFace->GetValue();
669
670     if (aShapeFace.ShapeType() == TopAbs_FACE) {
671       TopoDS_Face          aFace    = TopoDS::Face(aShapeFace);
672       Handle(Geom_Surface) aSurface = BRep_Tool::Surface(aFace);
673
674       if (aSurface.IsNull() == Standard_False) {
675         Handle(Standard_Type) aType = aSurface->DynamicType();
676         Standard_Real         aU1;
677         Standard_Real         aU2;
678         Standard_Real         aV1;
679         Standard_Real         aV2;
680
681          // Get U, V bounds of the face.
682         aFace.Orientation(TopAbs_FORWARD);
683         ShapeAnalysis::GetFaceUVBounds(aFace, aU1, aU2, aV1, aV2);
684
685         // Get the surface of original type
686         while (aType == STANDARD_TYPE(Geom_RectangularTrimmedSurface)) {
687           Handle(Geom_RectangularTrimmedSurface) aTrSurface =
688             Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurface);
689
690           aSurface = aTrSurface->BasisSurface();
691           aType    = aSurface->DynamicType();
692         }
693
694         const Standard_Real     aTol = BRep_Tool::Tolerance(aFace);
695         BRepBuilderAPI_MakeFace aMF(aSurface, aU1, aU2, aV1, aV2, aTol);
696
697         if (aMF.IsDone()) {
698           aShape = aMF.Shape();
699         }
700       }
701     }
702   }
703   else {
704   }
705
706   if (aShape.IsNull()) return 0;
707
708   // Check shape validity
709   BRepCheck_Analyzer ana (aShape, true);
710   if (!ana.IsValid()) {
711     //Standard_ConstructionError::Raise("Algorithm have produced an invalid shape result");
712     // For Mantis issue 0021772: EDF 2336 GEOM: Non valid face created from two circles
713     Handle(ShapeFix_Shape) aSfs = new ShapeFix_Shape (aShape);
714     aSfs->Perform();
715     aShape = aSfs->Shape();
716   }
717
718   // Check if the result shape type is compatible with the expected.
719   const TopAbs_ShapeEnum aShType = aShape.ShapeType();
720
721   if (!anExpectedType.empty()) {
722     bool ok = false;
723     std::list<TopAbs_ShapeEnum>::const_iterator it;
724     for (it = anExpectedType.begin(); it != anExpectedType.end() && !ok; ++it)
725       ok = (*it == TopAbs_SHAPE || *it == aShType);
726     if (!ok)
727       Standard_ConstructionError::Raise("Result type check failed");
728   }
729
730   aFunction->SetValue(aShape);
731
732   log.SetTouched(Label());
733
734   if (!aWarning.IsEmpty())
735     Standard_Failure::Raise(aWarning.ToCString());
736
737   return 1;
738 }
739
740 TopoDS_Wire GEOMImpl_ShapeDriver::MakeWireFromEdges(const Handle(TColStd_HSequenceOfTransient)& theEdgesFuncs,
741                                                     const Standard_Real theTolerance)
742 {
743   BRep_Builder B;
744
745   TopoDS_Wire aWire;
746   B.MakeWire(aWire);
747
748   // add edges
749   for (unsigned int ind = 1; ind <= theEdgesFuncs->Length(); ind++) {
750     Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(theEdgesFuncs->Value(ind));
751     TopoDS_Shape aShape_i = aRefShape->GetValue();
752     if (aShape_i.IsNull()) {
753       Standard_NullObject::Raise("Shape for wire construction is null");
754     }
755     if (aShape_i.ShapeType() == TopAbs_EDGE || aShape_i.ShapeType() == TopAbs_WIRE) {
756       TopExp_Explorer exp (aShape_i, TopAbs_EDGE);
757       for (; exp.More(); exp.Next())
758         B.Add(aWire, TopoDS::Edge(exp.Current()));
759     } else {
760       Standard_TypeMismatch::Raise
761         ("Shape for wire construction is neither an edge nor a wire");
762     }
763   }
764
765   // fix edges order
766   Handle(ShapeFix_Wire) aFW = new ShapeFix_Wire;
767   aFW->Load(aWire);
768   aFW->FixReorder();
769
770   if (aFW->StatusReorder(ShapeExtend_FAIL1)) {
771     Standard_ConstructionError::Raise("Wire construction failed: several loops detected");
772   }
773   else if (aFW->StatusReorder(ShapeExtend_FAIL)) {
774     Standard_ConstructionError::Raise("Wire construction failed");
775   }
776   else {
777   }
778
779   // IMP 0019766: Building a Wire from unconnected edges by introducing a tolerance
780   aFW->ClosedWireMode() = Standard_False;
781   aFW->FixConnected(theTolerance);
782   if (aFW->StatusConnected(ShapeExtend_FAIL)) {
783     Standard_ConstructionError::Raise("Wire construction failed: cannot build connected wire");
784   }
785   // IMP 0019766
786   if (aFW->StatusConnected(ShapeExtend_DONE3)) {
787     // Confused with <prec> but not Analyzer.Precision(), set the same
788     aFW->FixGapsByRangesMode() = Standard_True;
789     if (aFW->FixGaps3d()) {
790       Handle(ShapeExtend_WireData) sbwd = aFW->WireData();
791       Handle(ShapeFix_Edge) aFe = new ShapeFix_Edge;
792       for (Standard_Integer iedge = 1; iedge <= sbwd->NbEdges(); iedge++) {
793         TopoDS_Edge aEdge = TopoDS::Edge(sbwd->Edge(iedge));
794         aFe->FixVertexTolerance(aEdge);
795         aFe->FixSameParameter(aEdge);
796       }
797     }
798     else if (aFW->StatusGaps3d(ShapeExtend_FAIL)) {
799       Standard_ConstructionError::Raise("Wire construction failed: cannot fix 3d gaps");
800     }
801   }
802   aWire = aFW->WireAPIMake();
803
804   return aWire;
805 }
806
807 TopoDS_Edge GEOMImpl_ShapeDriver::MakeEdgeFromWire(const TopoDS_Shape& aWire,
808                                                    const Standard_Real LinTol,
809                                                    const Standard_Real AngTol)
810 {
811     TopoDS_Edge ResEdge;
812
813     BRepLib::BuildCurves3d(aWire);
814     Handle(ShapeFix_Shape) Fixer = new ShapeFix_Shape(aWire);
815     Fixer->SetPrecision(LinTol);
816     Fixer->SetMaxTolerance(LinTol);
817     Fixer->Perform();
818     TopoDS_Wire theWire = TopoDS::Wire(Fixer->Shape());
819
820     TColGeom_SequenceOfCurve CurveSeq;
821     TopTools_SequenceOfShape LocSeq;
822     TColStd_SequenceOfReal FparSeq;
823     TColStd_SequenceOfReal LparSeq;
824     TColStd_SequenceOfReal TolSeq;
825     GeomAbs_CurveType CurType;
826     TopoDS_Vertex FirstVertex, LastVertex;
827     Standard_Real aPntShiftDist = 0.;
828
829     BRepTools_WireExplorer wexp(theWire) ;
830     for (; wexp.More(); wexp.Next())
831     {
832       TopoDS_Edge anEdge = wexp.Current();
833       Standard_Real fpar, lpar;
834       TopLoc_Location aLoc;
835       Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aLoc, fpar, lpar);
836       if (aCurve.IsNull())
837         continue;
838
839       BRepAdaptor_Curve BAcurve(anEdge);
840       GeomAbs_CurveType aType = BAcurve.GetType();
841
842       Handle(Geom_Curve) aBasisCurve = BAcurve.Curve().Curve();
843
844       if (aBasisCurve->IsPeriodic())
845         ElCLib::AdjustPeriodic(aBasisCurve->FirstParameter(), aBasisCurve->LastParameter(),
846                                Precision::PConfusion(), fpar, lpar);
847
848       if (CurveSeq.IsEmpty())
849       {
850         CurveSeq.Append(aCurve);
851         TopoDS_Shape aLocShape;
852         aLocShape.Location(aLoc);
853         aLocShape.Orientation(wexp.Orientation());
854         LocSeq.Append(aLocShape);
855         FparSeq.Append(fpar);
856         LparSeq.Append(lpar);
857         CurType = aType;
858         FirstVertex = wexp.CurrentVertex();
859       }
860       else
861       {
862         Standard_Boolean Done = Standard_False;
863         Standard_Real NewFpar, NewLpar;
864         Handle(Geom_Geometry) aTrsfGeom = CurveSeq.Last()->Transformed
865                       (LocSeq.Last().Location().Transformation());
866         GeomAdaptor_Curve GAprevcurve(Handle(Geom_Curve)::DownCast(aTrsfGeom));
867         TopoDS_Vertex CurVertex = wexp.CurrentVertex();
868         TopoDS_Vertex CurFirstVer = TopExp::FirstVertex(anEdge);
869         TopAbs_Orientation ConnectByOrigin = (CurVertex.IsSame(CurFirstVer))? TopAbs_FORWARD : TopAbs_REVERSED;
870         if (aCurve == CurveSeq.Last() && aLoc.IsEqual(LocSeq.Last().Location()))
871         {
872           NewFpar = fpar;
873           NewLpar = lpar;
874           if (aBasisCurve->IsPeriodic())
875           {
876             if (NewLpar < NewFpar)
877               NewLpar += aBasisCurve->Period();
878             if (ConnectByOrigin == TopAbs_FORWARD)
879               ElCLib::AdjustPeriodic(FparSeq.Last(),
880                                      FparSeq.Last() + aBasisCurve->Period(),
881                                      Precision::PConfusion(), NewFpar, NewLpar);
882             else
883               ElCLib::AdjustPeriodic(FparSeq.Last() - aBasisCurve->Period(),
884                                      FparSeq.Last(),
885                                      Precision::PConfusion(), NewFpar, NewLpar);
886           }
887           Done = Standard_True;
888         }
889         else if (aType == CurType &&
890                  aType != GeomAbs_BezierCurve &&
891                  aType != GeomAbs_BSplineCurve &&
892                  aType != GeomAbs_OtherCurve)
893         {
894           switch (aType)
895           {
896           case GeomAbs_Line:
897             {
898               gp_Lin aLine    = BAcurve.Line();
899               gp_Lin PrevLine = GAprevcurve.Line();
900               if (aLine.Contains(PrevLine.Location(), LinTol) &&
901                   aLine.Direction().IsParallel(PrevLine.Direction(), AngTol))
902               {
903                 gp_Pnt P1 = ElCLib::Value(fpar, aLine);
904                 gp_Pnt P2 = ElCLib::Value(lpar, aLine);
905                 NewFpar = ElCLib::Parameter(PrevLine, P1);
906                 NewLpar = ElCLib::Parameter(PrevLine, P2);
907
908                 // Compute shift
909                 if (ConnectByOrigin == TopAbs_FORWARD) {
910                   gp_Pnt aNewP2 = ElCLib::Value(NewLpar, PrevLine);
911
912                   aPntShiftDist += P2.Distance(aNewP2);
913                 } else {
914                   gp_Pnt aNewP1 = ElCLib::Value(NewFpar, PrevLine);
915
916                   aPntShiftDist += P1.Distance(aNewP1);
917                 }
918
919                 if (NewLpar < NewFpar)
920                 {
921                   Standard_Real MemNewFpar = NewFpar;
922                   NewFpar = NewLpar;
923                   NewLpar = MemNewFpar;
924                   ConnectByOrigin = TopAbs::Reverse(ConnectByOrigin);
925                 }
926                 Done = Standard_True;
927               }
928               break;
929             }
930           case GeomAbs_Circle:
931             {
932               gp_Circ aCircle    = BAcurve.Circle();
933               gp_Circ PrevCircle = GAprevcurve.Circle();
934               if (aCircle.Location().Distance(PrevCircle.Location()) <= LinTol &&
935                   Abs(aCircle.Radius() - PrevCircle.Radius()) <= LinTol &&
936                   aCircle.Axis().IsParallel(PrevCircle.Axis(), AngTol))
937               {
938                 const Standard_Boolean isFwd = ConnectByOrigin == TopAbs_FORWARD;
939
940                 if (aCircle.Axis().Direction() * PrevCircle.Axis().Direction() < 0.)
941                 {
942                   Standard_Real memfpar = fpar;
943                   fpar = lpar;
944                   lpar = memfpar;
945                   ConnectByOrigin = TopAbs::Reverse(ConnectByOrigin);
946                 }
947                 gp_Pnt P1 = ElCLib::Value(fpar, aCircle);
948                 gp_Pnt P2 = ElCLib::Value(lpar, aCircle);
949                 NewFpar = ElCLib::Parameter(PrevCircle, P1);
950                 NewLpar = ElCLib::Parameter(PrevCircle, P2);
951
952                 // Compute shift
953                 if (isFwd) {
954                   gp_Pnt aNewP2 = ElCLib::Value(NewLpar, PrevCircle);
955
956                   aPntShiftDist += P2.Distance(aNewP2);
957                 } else {
958                   gp_Pnt aNewP1 = ElCLib::Value(NewFpar, PrevCircle);
959
960                   aPntShiftDist += P1.Distance(aNewP1);
961                 }
962
963                 if (NewLpar < NewFpar)
964                   NewLpar += 2.*M_PI;
965                 //Standard_Real MemNewFpar = NewFpar, MemNewLpar =  NewLpar;
966                 if (ConnectByOrigin == TopAbs_FORWARD)
967                   ElCLib::AdjustPeriodic(FparSeq.Last(),
968                                          FparSeq.Last() + 2.*M_PI,
969                                          Precision::PConfusion(), NewFpar, NewLpar);
970                 else
971                   ElCLib::AdjustPeriodic(FparSeq.Last() - 2.*M_PI,
972                                          FparSeq.Last(),
973                                          Precision::PConfusion(), NewFpar, NewLpar);
974                 Done = Standard_True;
975               }
976               break;
977             }
978           case GeomAbs_Ellipse:
979             {
980               gp_Elips anEllipse   = BAcurve.Ellipse();
981               gp_Elips PrevEllipse = GAprevcurve.Ellipse();
982               if (anEllipse.Focus1().Distance(PrevEllipse.Focus1()) <= LinTol &&
983                   anEllipse.Focus2().Distance(PrevEllipse.Focus2()) <= LinTol &&
984                   Abs(anEllipse.MajorRadius() - PrevEllipse.MajorRadius()) <= LinTol &&
985                   Abs(anEllipse.MinorRadius() - PrevEllipse.MinorRadius()) <= LinTol &&
986                   anEllipse.Axis().IsParallel(PrevEllipse.Axis(), AngTol))
987               {
988                 const Standard_Boolean isFwd = ConnectByOrigin == TopAbs_FORWARD;
989
990                 if (anEllipse.Axis().Direction() * PrevEllipse.Axis().Direction() < 0.)
991                 {
992                   Standard_Real memfpar = fpar;
993                   fpar = lpar;
994                   lpar = memfpar;
995                   ConnectByOrigin = TopAbs::Reverse(ConnectByOrigin);
996                 }
997                 gp_Pnt P1 = ElCLib::Value(fpar, anEllipse);
998                 gp_Pnt P2 = ElCLib::Value(lpar, anEllipse);
999                 NewFpar = ElCLib::Parameter(PrevEllipse, P1);
1000                 NewLpar = ElCLib::Parameter(PrevEllipse, P2);
1001
1002                 // Compute shift
1003                 if (isFwd) {
1004                   gp_Pnt aNewP2 = ElCLib::Value(NewLpar, PrevEllipse);
1005
1006                   aPntShiftDist += P2.Distance(aNewP2);
1007                 } else {
1008                   gp_Pnt aNewP1 = ElCLib::Value(NewFpar, PrevEllipse);
1009
1010                   aPntShiftDist += P1.Distance(aNewP1);
1011                 }
1012
1013                 if (NewLpar < NewFpar)
1014                   NewLpar += 2.*M_PI;
1015                 if (ConnectByOrigin == TopAbs_FORWARD)
1016                   ElCLib::AdjustPeriodic(FparSeq.Last(),
1017                                          FparSeq.Last() + 2.*M_PI,
1018                                          Precision::PConfusion(), NewFpar, NewLpar);
1019                 else
1020                   ElCLib::AdjustPeriodic(FparSeq.Last() - 2.*M_PI,
1021                                          FparSeq.Last(),
1022                                          Precision::PConfusion(), NewFpar, NewLpar);
1023                 Done = Standard_True;
1024               }
1025               break;
1026             }
1027           case GeomAbs_Hyperbola:
1028             {
1029               gp_Hypr aHypr    = BAcurve.Hyperbola();
1030               gp_Hypr PrevHypr = GAprevcurve.Hyperbola();
1031               if (aHypr.Focus1().Distance(PrevHypr.Focus1()) <= LinTol &&
1032                   aHypr.Focus2().Distance(PrevHypr.Focus2()) <= LinTol &&
1033                   Abs(aHypr.MajorRadius() - PrevHypr.MajorRadius()) <= LinTol &&
1034                   Abs(aHypr.MinorRadius() - PrevHypr.MinorRadius()) <= LinTol &&
1035                   aHypr.Axis().IsParallel(PrevHypr.Axis(), AngTol))
1036               {
1037                 gp_Pnt P1 = ElCLib::Value(fpar, aHypr);
1038                 gp_Pnt P2 = ElCLib::Value(lpar, aHypr);
1039                 NewFpar = ElCLib::Parameter(PrevHypr, P1);
1040                 NewLpar = ElCLib::Parameter(PrevHypr, P2);
1041
1042                 // Compute shift
1043                 if (ConnectByOrigin == TopAbs_FORWARD) {
1044                   gp_Pnt aNewP2 = ElCLib::Value(NewLpar, PrevHypr);
1045
1046                   aPntShiftDist += P2.Distance(aNewP2);
1047                 } else {
1048                   gp_Pnt aNewP1 = ElCLib::Value(NewFpar, PrevHypr);
1049
1050                   aPntShiftDist += P1.Distance(aNewP1);
1051                 }
1052
1053                 if (NewLpar < NewFpar)
1054                 {
1055                   Standard_Real MemNewFpar = NewFpar;
1056                   NewFpar = NewLpar;
1057                   NewLpar = MemNewFpar;
1058                   ConnectByOrigin = TopAbs::Reverse(ConnectByOrigin);
1059                 }
1060                 Done = Standard_True;
1061               }
1062               break;
1063             }
1064           case GeomAbs_Parabola:
1065             {
1066               gp_Parab aParab    = BAcurve.Parabola();
1067               gp_Parab PrevParab = GAprevcurve.Parabola();
1068               if (aParab.Location().Distance(PrevParab.Location()) <= LinTol &&
1069                   aParab.Focus().Distance(PrevParab.Focus()) <= LinTol &&
1070                   Abs(aParab.Focal() - PrevParab.Focal()) <= LinTol &&
1071                   aParab.Axis().IsParallel(PrevParab.Axis(), AngTol))
1072               {
1073                 gp_Pnt P1 = ElCLib::Value(fpar, aParab);
1074                 gp_Pnt P2 = ElCLib::Value(lpar, aParab);
1075                 NewFpar = ElCLib::Parameter(PrevParab, P1);
1076                 NewLpar = ElCLib::Parameter(PrevParab, P2);
1077
1078                 // Compute shift
1079                 if (ConnectByOrigin == TopAbs_FORWARD) {
1080                   gp_Pnt aNewP2 = ElCLib::Value(NewLpar, PrevParab);
1081
1082                   aPntShiftDist += P2.Distance(aNewP2);
1083                 } else {
1084                   gp_Pnt aNewP1 = ElCLib::Value(NewFpar, PrevParab);
1085
1086                   aPntShiftDist += P1.Distance(aNewP1);
1087                 }
1088
1089                 if (NewLpar < NewFpar)
1090                 {
1091                   Standard_Real MemNewFpar = NewFpar;
1092                   NewFpar = NewLpar;
1093                   NewLpar = MemNewFpar;
1094                   ConnectByOrigin = TopAbs::Reverse(ConnectByOrigin);
1095                 }
1096                 Done = Standard_True;
1097               }
1098               break;
1099             }
1100           } //end of switch (aType)
1101         } // end of else if (aType == CurType && ...
1102         if (Done)
1103         {
1104           if (NewFpar < FparSeq.Last())
1105             FparSeq(FparSeq.Length()) = NewFpar;
1106           else
1107             LparSeq(LparSeq.Length()) = NewLpar;
1108         }
1109         else
1110         {
1111           CurveSeq.Append(aCurve);
1112           TopoDS_Shape aLocShape;
1113           aLocShape.Location(aLoc);
1114           aLocShape.Orientation(wexp.Orientation());
1115           LocSeq.Append(aLocShape);
1116           FparSeq.Append(fpar);
1117           LparSeq.Append(lpar);
1118           TolSeq.Append(aPntShiftDist + BRep_Tool::Tolerance(CurVertex));
1119           aPntShiftDist = 0.;
1120           CurType = aType;
1121         }
1122       } // end of else (CurveSeq.IsEmpty()) -> not first time
1123     } // end for (; wexp.More(); wexp.Next())
1124
1125     LastVertex = wexp.CurrentVertex();
1126     TolSeq.Append(aPntShiftDist + BRep_Tool::Tolerance(LastVertex));
1127
1128     FirstVertex.Orientation(TopAbs_FORWARD);
1129     LastVertex.Orientation(TopAbs_REVERSED);
1130
1131     if (!CurveSeq.IsEmpty())
1132     {
1133       Standard_Integer nb_curve = CurveSeq.Length();   //number of curves
1134       TColGeom_Array1OfBSplineCurve tab(0,nb_curve-1);                    //array of the curves
1135       TColStd_Array1OfReal tabtolvertex(0,nb_curve-1); //(0,nb_curve-2);  //array of the tolerances
1136
1137       Standard_Integer i;
1138
1139       if (nb_curve > 1)
1140       {
1141         for (i = 1; i <= nb_curve; i++)
1142         {
1143           if (CurveSeq(i)->IsInstance(STANDARD_TYPE(Geom_TrimmedCurve)))
1144             CurveSeq(i) = (*((Handle(Geom_TrimmedCurve)*)&(CurveSeq(i))))->BasisCurve();
1145
1146           Handle(Geom_TrimmedCurve) aTrCurve = new Geom_TrimmedCurve(CurveSeq(i), FparSeq(i), LparSeq(i));
1147           tab(i-1) = GeomConvert::CurveToBSplineCurve(aTrCurve);
1148           tab(i-1)->Transform(LocSeq(i).Location().Transformation());
1149           GeomConvert::C0BSplineToC1BSplineCurve(tab(i-1), Precision::Confusion());
1150           if (LocSeq(i).Orientation() == TopAbs_REVERSED)
1151             tab(i-1)->Reverse();
1152
1153           //Temporary
1154           //char* name = new char[100];
1155           //sprintf(name, "c%d", i);
1156           //DrawTrSurf::Set(name, tab(i-1));
1157
1158           if (i > 1)
1159             tabtolvertex(i-2) = TolSeq(i-1);
1160         } // end for (i = 1; i <= nb_curve; i++)
1161         tabtolvertex(nb_curve-1) = TolSeq(TolSeq.Length());
1162
1163         Standard_Boolean closed_flag = Standard_False;
1164         Standard_Real closed_tolerance = 0.;
1165         if (FirstVertex.IsSame(LastVertex) &&
1166             GeomLProp::Continuity(tab(0), tab(nb_curve-1),
1167                                   tab(0)->FirstParameter(),
1168                                   tab(nb_curve-1)->LastParameter(),
1169                                   Standard_False, Standard_False, LinTol, AngTol) >= GeomAbs_G1)
1170         {
1171           closed_flag = Standard_True ;
1172           closed_tolerance = BRep_Tool::Tolerance(FirstVertex);
1173         }
1174
1175         Handle(TColGeom_HArray1OfBSplineCurve)  concatcurve;     //array of the concatenated curves
1176         Handle(TColStd_HArray1OfInteger)        ArrayOfIndices;  //array of the remining Vertex
1177         GeomConvert::ConcatC1(tab,
1178                               tabtolvertex,
1179                               ArrayOfIndices,
1180                               concatcurve,
1181                               closed_flag,
1182                               closed_tolerance);   //C1 concatenation
1183
1184         if (concatcurve->Length() > 1)
1185         {
1186           GeomConvert_CompCurveToBSplineCurve Concat(concatcurve->Value(concatcurve->Lower()));
1187
1188           for (i = concatcurve->Lower()+1; i <= concatcurve->Upper(); i++)
1189             Concat.Add( concatcurve->Value(i), LinTol, Standard_True );
1190
1191           concatcurve->SetValue(concatcurve->Lower(), Concat.BSplineCurve());
1192         }
1193         // rnc : prevents the driver from building an edge without C1 continuity
1194         if (concatcurve->Value(concatcurve->Lower())->Continuity()==GeomAbs_C0){
1195           Standard_ConstructionError::Raise("Construction aborted : The given Wire has sharp bends between some Edges, no valid Edge can be built");
1196         }
1197
1198         Standard_Boolean isValidEndVtx = Standard_True;
1199
1200         if (closed_flag) {
1201           // Check if closed curve is reordered.
1202           Handle(Geom_Curve) aCurve  = concatcurve->Value(concatcurve->Lower());
1203           Standard_Real      aFPar   = aCurve->FirstParameter();
1204           gp_Pnt             aPFirst;
1205           gp_Pnt             aPntVtx = BRep_Tool::Pnt(FirstVertex);
1206           Standard_Real      aTolVtx = BRep_Tool::Tolerance(FirstVertex);
1207
1208           aCurve->D0(aFPar, aPFirst);
1209
1210           if (!aPFirst.IsEqual(aPntVtx, aTolVtx)) {
1211             // The curve is reordered. Find the new first and last vertices.
1212             TopTools_IndexedMapOfShape aMapVtx;
1213             TopExp::MapShapes(theWire, TopAbs_VERTEX, aMapVtx);
1214
1215             const Standard_Integer aNbVtx = aMapVtx.Extent();
1216             Standard_Integer       iVtx;
1217
1218             for (iVtx = 1; iVtx <= aNbVtx; iVtx++) {
1219               const TopoDS_Vertex aVtx = TopoDS::Vertex(aMapVtx.FindKey(iVtx));
1220               const gp_Pnt        aPnt = BRep_Tool::Pnt(aVtx);
1221               const Standard_Real aTol = BRep_Tool::Tolerance(aVtx);
1222
1223               if (aPFirst.IsEqual(aPnt, aTol)) {
1224                 // The coinsident vertex is found.
1225                 FirstVertex = aVtx;
1226                 LastVertex  = aVtx;
1227                 FirstVertex.Orientation(TopAbs_FORWARD);
1228                 LastVertex.Orientation(TopAbs_REVERSED);
1229                 break;
1230               }
1231             }
1232
1233             if (iVtx > aNbVtx) {
1234               // It is necessary to create new vertices.
1235               isValidEndVtx = Standard_False;
1236             }
1237           }
1238         }
1239
1240         if (isValidEndVtx) {
1241           ResEdge = BRepLib_MakeEdge(concatcurve->Value(concatcurve->Lower()),
1242                                      FirstVertex, LastVertex,
1243                                      concatcurve->Value(concatcurve->Lower())->FirstParameter(),
1244                                      concatcurve->Value(concatcurve->Lower())->LastParameter());
1245         } else {
1246           ResEdge = BRepLib_MakeEdge(concatcurve->Value(concatcurve->Lower()),
1247                                      concatcurve->Value(concatcurve->Lower())->FirstParameter(),
1248                                      concatcurve->Value(concatcurve->Lower())->LastParameter());
1249         }
1250       }
1251       else
1252       {
1253         if (CurveSeq(1)->IsInstance(STANDARD_TYPE(Geom_TrimmedCurve)))
1254           CurveSeq(1) = (*((Handle(Geom_TrimmedCurve)*)&(CurveSeq(1))))->BasisCurve();
1255
1256         Handle(Geom_Curve) aNewCurve =
1257           Handle(Geom_Curve)::DownCast(CurveSeq(1)->Copy());
1258
1259         aNewCurve->Transform(LocSeq(1).Location().Transformation());
1260
1261         if (LocSeq(1).Orientation() == TopAbs_REVERSED) {
1262           const TopoDS_Vertex aVtxTmp = FirstVertex;
1263
1264           FirstVertex = LastVertex;
1265           LastVertex  = aVtxTmp;
1266           FirstVertex.Orientation(TopAbs_FORWARD);
1267           LastVertex.Orientation(TopAbs_REVERSED);
1268         }
1269
1270         ResEdge = BRepLib_MakeEdge(aNewCurve,
1271                                    FirstVertex, LastVertex,
1272                                    FparSeq(1), LparSeq(1));
1273
1274         if (LocSeq(1).Orientation() == TopAbs_REVERSED) {
1275           ResEdge.Reverse();
1276         }
1277       }
1278     }
1279
1280     return ResEdge;
1281 }
1282
1283 //=============================================================================
1284 /*!
1285  * \brief Returns an isoline for a face.
1286  */
1287 //=============================================================================
1288
1289 TopoDS_Shape GEOMImpl_ShapeDriver::MakeIsoline
1290                             (const TopoDS_Face &theFace,
1291                              const bool         IsUIso,
1292                              const double       theParameter) const
1293 {
1294   TopoDS_Shape          aResult;
1295   GEOMUtils::Hatcher    aHatcher(theFace);
1296   const GeomAbs_IsoType aType = (IsUIso ? GeomAbs_IsoU : GeomAbs_IsoV);
1297
1298   aHatcher.Init(aType, theParameter);
1299   aHatcher.Perform();
1300
1301   if (!aHatcher.IsDone()) {
1302     Standard_ConstructionError::Raise("MakeIsoline : Hatcher failure");
1303   }
1304
1305   const Handle(TColStd_HArray1OfInteger) &anIndices =
1306     (IsUIso ? aHatcher.GetUIndices() : aHatcher.GetVIndices());
1307
1308   if (anIndices.IsNull()) {
1309     Standard_ConstructionError::Raise("MakeIsoline : Null hatching indices");
1310   }
1311
1312   const Standard_Integer anIsoInd = anIndices->Lower();
1313   const Standard_Integer aHatchingIndex = anIndices->Value(anIsoInd);
1314
1315   if (aHatchingIndex == 0) {
1316     Standard_ConstructionError::Raise("MakeIsoline : Invalid hatching index");
1317   }
1318
1319   const Standard_Integer aNbDomains =
1320     aHatcher.GetNbDomains(aHatchingIndex);
1321
1322   if (aNbDomains < 0) {
1323     Standard_ConstructionError::Raise("MakeIsoline : Invalid number of domains");
1324   }
1325
1326   // The hatching is performed successfully. Create the 3d Curve.
1327   Handle(Geom_Surface) aSurface   = BRep_Tool::Surface(theFace);
1328   Handle(Geom_Curve)   anIsoCurve = (IsUIso ?
1329     aSurface->UIso(theParameter) : aSurface->VIso(theParameter));
1330   Handle(Geom2d_Curve) aPIsoCurve =
1331     aHatcher.GetHatching(aHatchingIndex);
1332   const Standard_Real  aTol = Precision::Confusion();
1333   Standard_Integer     anIDom = 1;
1334   Standard_Real        aV1;
1335   Standard_Real        aV2;
1336   BRep_Builder         aBuilder;
1337   Standard_Integer     aNbEdges = 0;
1338
1339   for (; anIDom <= aNbDomains; anIDom++) {
1340     if (aHatcher.GetDomain(aHatchingIndex, anIDom, aV1, aV2)) {
1341       // Check first and last parameters.
1342       if (!aHatcher.IsDomainInfinite(aHatchingIndex, anIDom)) {
1343         // Create an edge.
1344         TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge(anIsoCurve, aV1, aV2);
1345
1346         // Update it with a parametric curve on face.
1347         aBuilder.UpdateEdge(anEdge, aPIsoCurve, theFace, aTol);
1348         aNbEdges++;
1349
1350         if (aNbEdges > 1) {
1351           // Result is a compond.
1352           if (aNbEdges == 2) {
1353             // Create a new compound.
1354             TopoDS_Compound aCompound;
1355
1356             aBuilder.MakeCompound(aCompound);
1357             aBuilder.Add(aCompound, aResult);
1358             aResult = aCompound;
1359           }
1360
1361           // Add an edge to the compound.
1362           aBuilder.Add(aResult, anEdge);
1363         } else {
1364           // Result is the edge.
1365           aResult = anEdge;
1366         }
1367       }
1368     }
1369   }
1370
1371   if (aNbEdges == 0) {
1372     Standard_ConstructionError::Raise("MakeIsoline : Empty result");
1373   }
1374
1375   return aResult;
1376 }
1377
1378 //=============================================================================
1379 /*!
1380  * \brief Returns an extended edge.
1381  */
1382 //=============================================================================
1383
1384 TopoDS_Shape GEOMImpl_ShapeDriver::ExtendEdge
1385                          (const TopoDS_Edge   &theEdge,
1386                           const Standard_Real  theMin,
1387                           const Standard_Real  theMax) const
1388 {
1389   TopoDS_Shape        aResult;
1390   Standard_Real       aF;
1391   Standard_Real       aL;
1392   Handle(Geom_Curve)  aCurve   = BRep_Tool::Curve(theEdge, aF, aL);
1393   const Standard_Real aTol     = BRep_Tool::Tolerance(theEdge);
1394   Standard_Real       aRange2d = aL - aF;
1395
1396   if (aCurve.IsNull() == Standard_False && aRange2d > aTol) {
1397     Standard_Real aMin = aF + aRange2d*theMin;
1398     Standard_Real aMax = aF + aRange2d*theMax;
1399
1400     Handle(Standard_Type) aType = aCurve->DynamicType();
1401
1402     // Get the curve of original type
1403     while (aType == STANDARD_TYPE(Geom_TrimmedCurve)) {
1404       Handle(Geom_TrimmedCurve) aTrCurve =
1405         Handle(Geom_TrimmedCurve)::DownCast(aCurve);
1406
1407       aCurve = aTrCurve->BasisCurve();
1408       aType  = aCurve->DynamicType();
1409     }
1410
1411     if (aCurve->IsPeriodic()) {
1412       // The curve is periodic. Check if a new range is less then a period.
1413       if (aMax - aMin > aCurve->Period()) {
1414         aMax = aMin + aCurve->Period();
1415       }
1416     } else {
1417       // The curve is not periodic. Check if aMin and aMax within bounds.
1418       aMin = Max(aMin, aCurve->FirstParameter());
1419       aMax = Min(aMax, aCurve->LastParameter());
1420     }
1421
1422     if (aMax - aMin > aTol) {
1423       // Create a new edge.
1424       BRepBuilderAPI_MakeEdge aME (aCurve, aMin, aMax);
1425
1426       if (aME.IsDone()) {
1427         aResult = aME.Shape();
1428       }
1429     }
1430   }
1431
1432   return aResult;
1433 }
1434
1435 //=============================================================================
1436 /*!
1437  * \brief Returns an extended face.
1438  */
1439 //=============================================================================
1440
1441 TopoDS_Shape GEOMImpl_ShapeDriver::ExtendFace
1442                          (const TopoDS_Face   &theFace,
1443                           const Standard_Real  theUMin,
1444                           const Standard_Real  theUMax,
1445                           const Standard_Real  theVMin,
1446                           const Standard_Real  theVMax) const
1447 {
1448   TopoDS_Shape         aResult;
1449   Handle(Geom_Surface) aSurface = BRep_Tool::Surface(theFace);
1450   const Standard_Real  aTol     = BRep_Tool::Tolerance(theFace);
1451   Standard_Real        aU1;
1452   Standard_Real        aU2;
1453   Standard_Real        aV1;
1454   Standard_Real        aV2;
1455
1456   // Get U, V bounds of the face.
1457   ShapeAnalysis::GetFaceUVBounds(theFace, aU1, aU2, aV1, aV2);
1458
1459   const Standard_Real aURange = aU2 - aU1;
1460   const Standard_Real aVRange = aV2 - aV1;
1461
1462   if (aSurface.IsNull() == Standard_False &&
1463       aURange > aTol && aURange > aTol) {
1464     Handle(Standard_Type) aType = aSurface->DynamicType();
1465
1466     // Get the surface of original type
1467     while (aType == STANDARD_TYPE(Geom_RectangularTrimmedSurface)) {
1468       Handle(Geom_RectangularTrimmedSurface) aTrSurface =
1469         Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurface);
1470
1471       aSurface = aTrSurface->BasisSurface();
1472       aType    = aSurface->DynamicType();
1473     }
1474
1475     Standard_Real aUMin = aU1 + aURange*theUMin;
1476     Standard_Real aUMax = aU1 + aURange*theUMax;
1477     Standard_Real aVMin = aV1 + aVRange*theVMin;
1478     Standard_Real aVMax = aV1 + aVRange*theVMax;
1479
1480     aSurface->Bounds(aU1, aU2, aV1, aV2);
1481
1482     if (aSurface->IsUPeriodic()) {
1483       // The surface is U-periodic. Check if a new U range is less
1484       // then a period.
1485       if (aUMax - aUMin > aSurface->UPeriod()) {
1486         aUMax = aUMin + aSurface->UPeriod();
1487       }
1488     } else {
1489       // The surface is not V-periodic. Check if aUMin and aUMax
1490       // within bounds.
1491       aUMin = Max(aUMin, aU1);
1492       aUMax = Min(aUMax, aU2);
1493     }
1494
1495     if (aSurface->IsVPeriodic()) {
1496       // The surface is V-periodic. Check if a new V range is less
1497       // then a period.
1498       if (aVMax - aVMin > aSurface->VPeriod()) {
1499         aVMax = aVMin + aSurface->VPeriod();
1500       }
1501     } else {
1502       // The surface is not V-periodic. Check if aVMin and aVMax
1503       // within bounds.
1504       aVMin = Max(aVMin, aV1);
1505       aVMax = Min(aVMax, aV2);
1506     }
1507
1508     if (aUMax - aUMin > aTol && aVMax - aVMin > aTol) {
1509       // Create a new edge.
1510       BRepBuilderAPI_MakeFace aMF
1511         (aSurface, aUMin, aUMax, aVMin, aVMax, aTol);
1512     
1513       if (aMF.IsDone()) {
1514         aResult = aMF.Shape();
1515       }
1516     }
1517   }
1518
1519   return aResult;
1520 }
1521
1522 //================================================================================
1523 /*!
1524  * \brief Returns a name of creation operation and names and values of creation parameters
1525  */
1526 //================================================================================
1527
1528 bool GEOMImpl_ShapeDriver::
1529 GetCreationInformation(std::string&             theOperationName,
1530                        std::vector<GEOM_Param>& theParams)
1531 {
1532   if (Label().IsNull()) return 0;
1533   Handle(GEOM_Function) function = GEOM_Function::GetFunction(Label());
1534
1535   GEOMImpl_IShapes aCI( function );
1536   Standard_Integer aType = function->GetType();
1537
1538   switch ( aType ) {
1539   case WIRE_EDGES:
1540     theOperationName = "WIRE";
1541     AddParam( theParams, "Wires/edges", aCI.GetShapes() );
1542     AddParam( theParams, "Tolerance", aCI.GetTolerance() );
1543     break;
1544   case FACE_WIRE:
1545     theOperationName = "FACE";
1546     AddParam( theParams, "Wire/edge", aCI.GetBase() );
1547     AddParam( theParams, "Is planar wanted", aCI.GetIsPlanar() );
1548     break;
1549   case FACE_WIRES:
1550     theOperationName = "FACE";
1551     AddParam( theParams, "Wires/edges", aCI.GetShapes() );
1552     AddParam( theParams, "Is planar wanted", aCI.GetIsPlanar() );
1553     break;
1554   case FACE_FROM_SURFACE:
1555   {
1556     theOperationName = "FACE";
1557
1558     Handle(TColStd_HSequenceOfTransient) shapes = aCI.GetShapes();
1559
1560     if (shapes.IsNull() == Standard_False) {
1561       Standard_Integer aNbShapes = shapes->Length();
1562
1563       if (aNbShapes > 0) {
1564         AddParam(theParams, "Face", shapes->Value(1));
1565
1566         if (aNbShapes > 1) {
1567           AddParam(theParams, "Wire", shapes->Value(2));
1568         }
1569       }
1570     }
1571     break;
1572   }
1573   case SHELL_FACES:
1574     theOperationName = "SHELL";
1575     AddParam( theParams, "Objects", aCI.GetShapes() );
1576     break;
1577   case SOLID_SHELLS:
1578     theOperationName = "SOLID";
1579     AddParam( theParams, "Objects", aCI.GetShapes() );
1580     break;
1581   case SOLID_FACES:
1582     theOperationName = "SOLID_FROM_FACES";
1583     AddParam( theParams, "Objects", aCI.GetShapes() );
1584     AddParam( theParams, "Is intersect", aCI.GetIsIntersect() );
1585     break;
1586   case COMPOUND_SHAPES:
1587     theOperationName = "COMPOUND";
1588     AddParam( theParams, "Objects", aCI.GetShapes() );
1589     break;
1590   case EDGE_WIRE:
1591     theOperationName = "EDGE";
1592     AddParam( theParams, "Wire", aCI.GetBase() );
1593     AddParam( theParams, "Linear Tolerance", aCI.GetTolerance() );
1594     AddParam( theParams, "Angular Tolerance", aCI.GetAngularTolerance() );
1595     break;
1596   case EDGE_CURVE_LENGTH:
1597     theOperationName = "EDGE";
1598     {
1599       GEOMImpl_IVector aCI( function );
1600       AddParam( theParams, "Edge", aCI.GetPoint1() );
1601       AddParam( theParams, "Start point", aCI.GetPoint2() );
1602       AddParam( theParams, "Length", aCI.GetParameter() );
1603     }
1604     break;
1605   case SHAPES_ON_SHAPE:
1606   {
1607     theOperationName = "GetShapesOnShapeAsCompound";
1608     Handle(TColStd_HSequenceOfTransient) shapes = aCI.GetShapes();
1609     if ( !shapes.IsNull() && shapes->Length() > 0 )
1610       AddParam( theParams, "Check shape", shapes->Value(1) );
1611     if ( !shapes.IsNull() && shapes->Length() > 1 )
1612       AddParam( theParams, "Shape", shapes->Value(2) );
1613     AddParam( theParams, "Shape type", TopAbs_ShapeEnum( aCI.GetSubShapeType() ));
1614     AddParam( theParams, "State" );
1615     GEOMAlgo_State st = GEOMAlgo_State( (int) ( aCI.GetTolerance()+0.1 ) );
1616     const char* stName[] = { "UNKNOWN","IN","OUT","ON","ONIN","ONOUT","INOUT" };
1617     if ( 0 <= st && st <= GEOMAlgo_ST_INOUT )
1618       theParams.back() << stName[ st ];
1619     else
1620       theParams.back() << (int) st;
1621     break;
1622   }
1623   case SHAPE_ISOLINE:
1624   {
1625     GEOMImpl_IIsoline aII (function);
1626
1627     theOperationName = "ISOLINE";
1628     AddParam(theParams, "Face", aII.GetFace());
1629     AddParam(theParams, "Isoline type", (aII.GetIsUIso() ? "U" : "V"));
1630     AddParam(theParams, "Parameter", aII.GetParameter());
1631     break;
1632   }
1633   case EDGE_UV:
1634   {
1635     GEOMImpl_IShapeExtend aSE (function);
1636
1637     theOperationName = "EDGE_EXTEND";
1638     AddParam(theParams, "Edge", aSE.GetShape());
1639     AddParam(theParams, "Min", aSE.GetUMin());
1640     AddParam(theParams, "Max", aSE.GetUMax());
1641     break;
1642   }
1643   case FACE_UV:
1644   {
1645     GEOMImpl_IShapeExtend aSE (function);
1646
1647     theOperationName = "FACE_EXTEND";
1648     AddParam(theParams, "Face", aSE.GetShape());
1649     AddParam(theParams, "UMin", aSE.GetUMin());
1650     AddParam(theParams, "UMax", aSE.GetUMax());
1651     AddParam(theParams, "VMin", aSE.GetVMin());
1652     AddParam(theParams, "VMax", aSE.GetVMax());
1653     break;
1654   }
1655   case SURFACE_FROM_FACE:
1656   {
1657     GEOMImpl_IShapeExtend aSE (function);
1658
1659     theOperationName = "SURFACE_FROM_FACE";
1660     AddParam(theParams, "Face", aSE.GetShape());
1661     break;
1662   }
1663   default:
1664     return false;
1665   }
1666
1667   return true;
1668 }
1669
1670 IMPLEMENT_STANDARD_HANDLE (GEOMImpl_ShapeDriver,GEOM_BaseDriver);
1671 IMPLEMENT_STANDARD_RTTIEXT (GEOMImpl_ShapeDriver,GEOM_BaseDriver);
1672
1673 //modified by NIZNHY-PKV Wed Dec 28 13:48:31 2011f
1674 #include <TopoDS_Iterator.hxx>
1675 #include <TopTools_HSequenceOfShape.hxx>
1676 #include <ShapeAnalysis_FreeBounds.hxx>
1677 #include <TopTools_MapOfShape.hxx>
1678 #include <TopTools_MapOfOrientedShape.hxx>
1679 #include <BRep_Builder.hxx>
1680 #include <TopoDS_Wire.hxx>
1681
1682 //=======================================================================
1683 //function : KeepEdgesOrder
1684 //purpose  : 
1685 //=======================================================================
1686 /*
1687 void KeepEdgesOrder(const Handle(TopTools_HSequenceOfShape)& aEdges,
1688                     const Handle(TopTools_HSequenceOfShape)& aWires)
1689 {
1690   Standard_Integer aNbWires, aNbEdges;
1691   // 
1692   if (aEdges.IsNull()) {
1693     return;
1694   }
1695   //
1696   if (aWires.IsNull()) {
1697     return;
1698   }
1699   //
1700   aNbEdges=aEdges->Length();
1701   aNbWires=aWires->Length();
1702   if (!aNbEdges || !aNbWires) {
1703     return;
1704   }
1705   //-----
1706   Standard_Boolean bClosed;
1707   Standard_Integer i, j;
1708   TopoDS_Wire aWy;
1709   TopoDS_Iterator aIt;
1710   BRep_Builder aBB;
1711   TopTools_MapOfOrientedShape aMEx;
1712   //
1713   for (i=1; i<=aNbWires; ++i) {
1714     const TopoDS_Shape& aWx=aWires->Value(i);
1715     //
1716     aMEx.Clear();
1717     aIt.Initialize (aWx);
1718     for (; aIt.More(); aIt.Next()) {
1719       const TopoDS_Shape& aEx=aIt.Value();
1720       aMEx.Add(aEx);
1721     }
1722     // aWy
1723     aBB.MakeWire (aWy);
1724     for (j=1; j<=aNbEdges; ++j) {
1725       const TopoDS_Shape& aE=aEdges->Value(j);
1726       if (aMEx.Contains(aE)) {
1727         aBB.Add(aWy, aE);
1728       }
1729     }
1730     //
1731     bClosed=aWx.Closed();
1732     aWy.Closed(bClosed);
1733     //
1734     aWires->Append(aWy);
1735   }// for (i=1; i<=aNbWires; ++i) {
1736   //
1737   aWires->Remove(1, aNbWires);
1738 }
1739 */
1740 //modified by NIZNHY-PKV Wed Dec 28 13:48:34 2011t