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