Salome HOME
Merge branch 'master' into V7_5_BR
[modules/geom.git] / src / GEOMImpl / GEOMImpl_ShapeDriver.cxx
1 // Copyright (C) 2007-2014  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_IVector.hxx>
28 #include <GEOMImpl_Types.hxx>
29 #include <GEOMImpl_Block6Explorer.hxx>
30
31 #include <GEOM_Function.hxx>
32 #include <GEOMUtils_Hatcher.hxx>
33
34 // OCCT Includes
35 #include <ShapeFix_Wire.hxx>
36 #include <ShapeFix_Edge.hxx>
37 #include <ShapeFix_Shape.hxx>
38
39 #include <BRep_Builder.hxx>
40 #include <BRep_Tool.hxx>
41 #include <BRepAdaptor_Curve.hxx>
42 #include <BRepAlgo_FaceRestrictor.hxx>
43 #include <BRepBuilderAPI_Copy.hxx>
44 #include <BRepBuilderAPI_Sewing.hxx>
45 #include <BRepBuilderAPI_MakeWire.hxx>
46 #include <BRepBuilderAPI_MakeEdge.hxx>
47 #include <BRepBuilderAPI_MakeSolid.hxx>
48 #include <BRepCheck.hxx>
49 #include <BRepCheck_Analyzer.hxx>
50 #include <BRepCheck_Shell.hxx>
51 #include <BRepClass3d_SolidClassifier.hxx>
52 #include <BRepLib.hxx>
53 #include <BRepLib_MakeEdge.hxx>
54 #include <BRepTools_WireExplorer.hxx>
55
56 #include <ShapeAnalysis.hxx>
57 #include <ShapeAnalysis_FreeBounds.hxx>
58
59 #include <TopAbs.hxx>
60 #include <TopExp.hxx>
61 #include <TopExp_Explorer.hxx>
62 #include <TopoDS.hxx>
63 #include <TopoDS_Shape.hxx>
64 #include <TopoDS_Edge.hxx>
65 #include <TopoDS_Wire.hxx>
66 #include <TopoDS_Shell.hxx>
67 #include <TopoDS_Solid.hxx>
68 #include <TopoDS_Compound.hxx>
69 #include <TopoDS_Iterator.hxx>
70
71 #include <TopTools_MapOfShape.hxx>
72 #include <TopTools_HSequenceOfShape.hxx>
73
74 #include <ElCLib.hxx>
75
76 #include <GCPnts_AbscissaPoint.hxx>
77
78 #include <Geom_TrimmedCurve.hxx>
79 #include <Geom_Surface.hxx>
80 #include <GeomAbs_CurveType.hxx>
81 #include <GeomConvert_CompCurveToBSplineCurve.hxx>
82 #include <GeomConvert.hxx>
83 #include <GeomLProp.hxx>
84
85 #include <TColStd_SequenceOfReal.hxx>
86 #include <TColStd_HSequenceOfTransient.hxx>
87 #include <TColStd_Array1OfReal.hxx>
88 #include <TColGeom_SequenceOfCurve.hxx>
89 #include <TColGeom_Array1OfBSplineCurve.hxx>
90 #include <TColGeom_HArray1OfBSplineCurve.hxx>
91
92 #include <Precision.hxx>
93
94 #include <Standard_NullObject.hxx>
95 #include <Standard_TypeMismatch.hxx>
96 #include <Standard_ConstructionError.hxx>
97
98 //modified by NIZNHY-PKV Wed Dec 28 13:48:20 2011f
99 //static
100 //  void KeepEdgesOrder(const Handle(TopTools_HSequenceOfShape)& aEdges,
101 //                    const Handle(TopTools_HSequenceOfShape)& aWires);
102 //modified by NIZNHY-PKV Wed Dec 28 13:48:23 2011t
103
104 //=======================================================================
105 //function : GetID
106 //purpose  :
107 //=======================================================================
108 const Standard_GUID& GEOMImpl_ShapeDriver::GetID()
109 {
110   static Standard_GUID aShapeDriver("FF1BBB54-5D14-4df2-980B-3A668264EA16");
111   return aShapeDriver;
112 }
113
114
115 //=======================================================================
116 //function : GEOMImpl_ShapeDriver
117 //purpose  :
118 //=======================================================================
119 GEOMImpl_ShapeDriver::GEOMImpl_ShapeDriver()
120 {
121 }
122
123 //=======================================================================
124 //function : Execute
125 //purpose  :
126 //=======================================================================
127 Standard_Integer GEOMImpl_ShapeDriver::Execute(TFunction_Logbook& log) const
128 {
129   if (Label().IsNull()) return 0;
130   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
131
132   GEOMImpl_IShapes aCI (aFunction);
133   Standard_Integer aType = aFunction->GetType();
134
135   TopoDS_Shape aShape;
136   TCollection_AsciiString aWarning;
137   TopAbs_ShapeEnum anExpectedType = TopAbs_SHAPE;
138
139   BRep_Builder B;
140
141   if (aType == WIRE_EDGES) {
142     anExpectedType = TopAbs_WIRE;
143
144     Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
145
146     Standard_Real aTolerance = aCI.GetTolerance();
147     if (aTolerance < Precision::Confusion())
148       aTolerance = Precision::Confusion();
149
150     aShape = MakeWireFromEdges(aShapes, aTolerance);
151   }
152   else if (aType == FACE_WIRE) {
153     anExpectedType = TopAbs_FACE;
154
155     Handle(GEOM_Function) aRefBase = aCI.GetBase();
156     TopoDS_Shape aShapeBase = aRefBase->GetValue();
157     if (aShapeBase.IsNull()) Standard_NullObject::Raise("Argument Shape is null");
158     TopoDS_Wire W;
159     if (aShapeBase.ShapeType() == TopAbs_WIRE) {
160       W = TopoDS::Wire(aShapeBase);
161       // check the wire is closed
162       TopoDS_Vertex aV1, aV2;
163       TopExp::Vertices(W, aV1, aV2);
164       if ( !aV1.IsNull() && !aV2.IsNull() && aV1.IsSame(aV2) )
165         aShapeBase.Closed(true);
166       else
167         Standard_NullObject::Raise
168           ("Shape for face construction is not closed");
169     }
170     else if (aShapeBase.ShapeType() == TopAbs_EDGE && aShapeBase.Closed()) {
171       BRepBuilderAPI_MakeWire MW;
172       MW.Add(TopoDS::Edge(aShapeBase));
173       if (!MW.IsDone()) {
174         Standard_ConstructionError::Raise("Wire construction failed");
175       }
176       W = MW;
177     }
178     else {
179       Standard_NullObject::Raise
180         ("Shape for face construction is neither a wire nor a closed edge");
181     }
182     aWarning = GEOMImpl_Block6Explorer::MakeFace(W, aCI.GetIsPlanar(), aShape);
183     if (aShape.IsNull()) {
184       Standard_ConstructionError::Raise("Face construction failed");
185     }
186   }
187   else if (aType == FACE_WIRES) {
188     anExpectedType = TopAbs_FACE;
189
190     // Try to build a face from a set of wires and edges
191     int ind;
192
193     Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
194     int nbshapes = aShapes->Length();
195     if (nbshapes < 1) {
196       Standard_ConstructionError::Raise("No wires or edges given");
197     }
198
199     // 1. Extract all edges from the given arguments
200     TopTools_MapOfShape aMapEdges;
201     Handle(TopTools_HSequenceOfShape) aSeqEdgesIn = new TopTools_HSequenceOfShape;
202
203     for (ind = 1; ind <= nbshapes; ind++) {
204       Handle(GEOM_Function) aRefSh_i = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
205       TopoDS_Shape aSh_i = aRefSh_i->GetValue();
206
207       TopExp_Explorer anExpE_i (aSh_i, TopAbs_EDGE);
208       for (; anExpE_i.More(); anExpE_i.Next()) {
209         if (aMapEdges.Add(anExpE_i.Current())) {
210           aSeqEdgesIn->Append(anExpE_i.Current());
211         }
212       }
213     }
214
215     // 2. Connect edges to wires of maximum length
216     Handle(TopTools_HSequenceOfShape) aSeqWiresOut;
217     ShapeAnalysis_FreeBounds::ConnectEdgesToWires(aSeqEdgesIn, Precision::Confusion(),
218                                                   /*shared*/Standard_False, aSeqWiresOut);
219     //modified by NIZNHY-PKV Wed Dec 28 13:46:55 2011f
220     //KeepEdgesOrder(aSeqEdgesIn, aSeqWiresOut);
221     //modified by NIZNHY-PKV Wed Dec 28 13:46:59 2011t
222
223     // 3. Separate closed wires
224     Handle(TopTools_HSequenceOfShape) aSeqClosedWires = new TopTools_HSequenceOfShape;
225     Handle(TopTools_HSequenceOfShape) aSeqOpenWires = new TopTools_HSequenceOfShape;
226     for (ind = 1; ind <= aSeqWiresOut->Length(); ind++) {
227       if (aSeqWiresOut->Value(ind).Closed())
228         aSeqClosedWires->Append(aSeqWiresOut->Value(ind));
229       else
230         aSeqOpenWires->Append(aSeqWiresOut->Value(ind));
231     }
232
233     if (aSeqClosedWires->Length() < 1) {
234       Standard_ConstructionError::Raise
235         ("There is no closed contour can be built from the given arguments");
236     }
237
238     // 4. Build a face / list of faces from all the obtained closed wires
239
240     // 4.a. Basic face
241     TopoDS_Shape aFFace;
242     TopoDS_Wire aW1 = TopoDS::Wire(aSeqClosedWires->Value(1));
243     aWarning = GEOMImpl_Block6Explorer::MakeFace(aW1, aCI.GetIsPlanar(), aFFace);
244     if (aFFace.IsNull()) {
245       Standard_ConstructionError::Raise("Face construction failed");
246     }
247
248     // 4.b. Add other wires
249     if (aSeqClosedWires->Length() == 1) {
250       aShape = aFFace;
251     }
252     else {
253       TopoDS_Compound C;
254       BRep_Builder aBuilder;
255       aBuilder.MakeCompound(C);
256       BRepAlgo_FaceRestrictor FR;
257
258       TopAbs_Orientation OriF = aFFace.Orientation();
259       TopoDS_Shape aLocalS = aFFace.Oriented(TopAbs_FORWARD);
260       FR.Init(TopoDS::Face(aLocalS), Standard_False, Standard_True);
261
262       for (ind = 1; ind <= aSeqClosedWires->Length(); ind++) {
263         TopoDS_Wire aW = TopoDS::Wire(aSeqClosedWires->Value(ind));
264         FR.Add(aW);
265       }
266
267       FR.Perform();
268
269       if (FR.IsDone()) {
270         int k = 0;
271         TopoDS_Shape aFace;
272         for (; FR.More(); FR.Next()) {
273           aFace = FR.Current().Oriented(OriF);
274           aBuilder.Add(C, aFace);
275           k++;
276         }
277         if (k == 1) {
278           aShape = aFace;
279         } else {
280           aShape = C;
281         }
282       }
283     }
284
285     // 5. Add all open wires to the result
286     if (aSeqOpenWires->Length() > 0) {
287       //Standard_ConstructionError::Raise("There are some open wires");
288       TopoDS_Compound C;
289       BRep_Builder aBuilder;
290       if (aSeqClosedWires->Length() == 1) {
291         aBuilder.MakeCompound(C);
292         aBuilder.Add(C, aShape);
293       }
294       else {
295         C = TopoDS::Compound(aShape);
296       }
297
298       for (ind = 1; ind <= aSeqOpenWires->Length(); ind++) {
299         aBuilder.Add(C, aSeqOpenWires->Value(ind));
300       }
301
302       aShape = C;
303     }
304   }
305   else if (aType == SHELL_FACES) {
306     anExpectedType = TopAbs_SHELL;
307
308     Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
309     unsigned int ind, nbshapes = aShapes->Length();
310
311     // add faces
312     BRepBuilderAPI_Sewing aSewing (Precision::Confusion()*10.0);
313     for (ind = 1; ind <= nbshapes; ind++) {
314       Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
315       TopoDS_Shape aShape_i = aRefShape->GetValue();
316       if (aShape_i.IsNull()) {
317         Standard_NullObject::Raise("Face for shell construction is null");
318       }
319       aSewing.Add(aShape_i);
320     }
321
322     aSewing.Perform();
323
324     TopoDS_Shape sh = aSewing.SewedShape();
325
326     if (sh.ShapeType()==TopAbs_FACE && nbshapes==1) {
327       // case for creation of shell from one face - PAL12722 (skl 26.06.2006)
328       TopoDS_Shell ss;
329       B.MakeShell(ss);
330       B.Add(ss,sh);
331       aShape = ss;
332     }
333     else {
334       //TopExp_Explorer exp (aSewing.SewedShape(), TopAbs_SHELL);
335       TopExp_Explorer exp (sh, TopAbs_SHELL);
336       Standard_Integer ish = 0;
337       for (; exp.More(); exp.Next()) {
338         aShape = exp.Current();
339         ish++;
340       }
341
342       if (ish != 1) {
343         // try the case of one face (Mantis issue 0021809)
344         TopExp_Explorer expF (sh, TopAbs_FACE);
345         Standard_Integer ifa = 0;
346         for (; expF.More(); expF.Next()) {
347           aShape = expF.Current();
348           ifa++;
349         }
350
351         if (ifa == 1) {
352           TopoDS_Shell ss;
353           B.MakeShell(ss);
354           B.Add(ss,aShape);
355           aShape = ss;
356         }
357         else {
358           aShape = aSewing.SewedShape();
359         }
360       }
361     }
362
363   }
364   else if (aType == SOLID_SHELLS) {
365     anExpectedType = TopAbs_SOLID;
366
367     Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
368     unsigned int ind, nbshapes = aShapes->Length();
369     Standard_Integer ish = 0;
370     BRepBuilderAPI_MakeSolid aMkSolid;
371
372     // add shapes
373     for (ind = 1; ind <= nbshapes; ind++) {
374       Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
375       TopoDS_Shape aShapeShell = aRefShape->GetValue();
376       if (aShapeShell.IsNull()) {
377         Standard_NullObject::Raise("Shell for solid construction is null");
378       }
379       if (aShapeShell.ShapeType() == TopAbs_COMPOUND) {
380         TopoDS_Iterator It (aShapeShell, Standard_True, Standard_True);
381         if (It.More()) aShapeShell = It.Value();
382       }
383       if (aShapeShell.ShapeType() == TopAbs_SHELL) {
384         aMkSolid.Add(TopoDS::Shell(aShapeShell));
385         ish++;
386       }
387     }
388     if (ish == 0 || !aMkSolid.IsDone()) return 0;
389
390     TopoDS_Solid Sol = aMkSolid.Solid();
391     BRepClass3d_SolidClassifier SC (Sol);
392     SC.PerformInfinitePoint(Precision::Confusion());
393     if (SC.State() == TopAbs_IN)
394       aShape = Sol.Reversed();
395     else
396       aShape = Sol;
397   }
398   else if (aType == COMPOUND_SHAPES) {
399     anExpectedType = TopAbs_COMPOUND;
400
401     Handle(TColStd_HSequenceOfTransient) aShapes = aCI.GetShapes();
402     unsigned int ind, nbshapes = aShapes->Length();
403
404     // add shapes
405     TopoDS_Compound C;
406     B.MakeCompound(C);
407     for (ind = 1; ind <= nbshapes; ind++) {
408       Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(aShapes->Value(ind));
409       TopoDS_Shape aShape_i = aRefShape->GetValue();
410       if (aShape_i.IsNull()) {
411         Standard_NullObject::Raise("Shape for compound construction is null");
412       }
413       B.Add(C, aShape_i);
414     }
415
416     aShape = C;
417
418   }
419   else if (aType == EDGE_WIRE) {
420     anExpectedType = TopAbs_EDGE;
421
422     Handle(GEOM_Function) aRefBase = aCI.GetBase();
423     TopoDS_Shape aWire = aRefBase->GetValue();
424     Standard_Real LinTol = aCI.GetTolerance();
425     Standard_Real AngTol = aCI.GetAngularTolerance();
426     if (aWire.IsNull()) Standard_NullObject::Raise("Argument Wire is null");
427
428     aShape = MakeEdgeFromWire(aWire, LinTol, AngTol);
429   }
430   else if (aType == EDGE_CURVE_LENGTH) {
431     anExpectedType = TopAbs_EDGE;
432
433     GEOMImpl_IVector aVI (aFunction);
434
435     // RefCurve
436     Handle(GEOM_Function) aRefCurve = aVI.GetPoint1();
437     if (aRefCurve.IsNull()) Standard_NullObject::Raise("Argument Curve is null");
438     TopoDS_Shape aRefShape1 = aRefCurve->GetValue();
439     if (aRefShape1.ShapeType() != TopAbs_EDGE) {
440       Standard_TypeMismatch::Raise
441         ("Edge On Curve creation aborted : curve shape is not an edge");
442     }
443     TopoDS_Edge aRefEdge = TopoDS::Edge(aRefShape1);
444     TopoDS_Vertex V1, V2;
445     TopExp::Vertices(aRefEdge, V1, V2, Standard_True);
446
447     // RefPoint
448     TopoDS_Vertex aRefVertex;
449     Handle(GEOM_Function) aRefPoint = aVI.GetPoint2();
450     if (aRefPoint.IsNull()) {
451       aRefVertex = V1;
452     }
453     else {
454       TopoDS_Shape aRefShape2 = aRefPoint->GetValue();
455       if (aRefShape2.ShapeType() != TopAbs_VERTEX) {
456         Standard_TypeMismatch::Raise
457           ("Edge On Curve creation aborted : start point shape is not a vertex");
458       }
459       aRefVertex = TopoDS::Vertex(aRefShape2);
460     }
461     gp_Pnt aRefPnt = BRep_Tool::Pnt(aRefVertex);
462
463     // Length
464     Standard_Real aLength = aVI.GetParameter();
465     //Standard_Real aCurveLength = IntTools::Length(aRefEdge);
466     //if (aLength > aCurveLength) {
467     //  Standard_ConstructionError::Raise
468     //    ("Edge On Curve creation aborted : given length is greater than edges length");
469     //}
470     if (fabs(aLength) < Precision::Confusion()) {
471       Standard_ConstructionError::Raise
472         ("Edge On Curve creation aborted : given length is smaller than Precision::Confusion()");
473     }
474
475     // Check orientation
476     Standard_Real UFirst, ULast;
477     Handle(Geom_Curve) EdgeCurve = BRep_Tool::Curve(aRefEdge, UFirst, ULast);
478     Handle(Geom_Curve) ReOrientedCurve = EdgeCurve;
479
480     Standard_Real dU = ULast - UFirst;
481     Standard_Real par1 = UFirst + 0.1 * dU;
482     Standard_Real par2 = ULast  - 0.1 * dU;
483
484     gp_Pnt P1 = EdgeCurve->Value(par1);
485     gp_Pnt P2 = EdgeCurve->Value(par2);
486
487     if (aRefPnt.SquareDistance(P2) < aRefPnt.SquareDistance(P1)) {
488       ReOrientedCurve = EdgeCurve->Reversed();
489       UFirst = EdgeCurve->ReversedParameter(ULast);
490     }
491
492     // Get the point by length
493     GeomAdaptor_Curve AdapCurve = GeomAdaptor_Curve(ReOrientedCurve);
494     GCPnts_AbscissaPoint anAbsPnt (AdapCurve, aLength, UFirst);
495     Standard_Real aParam = anAbsPnt.Parameter();
496
497     if (AdapCurve.IsClosed() && aLength < 0.0) {
498       Standard_Real aTmp = aParam;
499       aParam = UFirst;
500       UFirst = aTmp;
501     }
502
503     BRepBuilderAPI_MakeEdge aME (ReOrientedCurve, UFirst, aParam);
504     if (aME.IsDone())
505       aShape = aME.Shape();
506   } else if (aType == SHAPE_ISOLINE) {
507     GEOMImpl_IIsoline     aII (aFunction);
508     Handle(GEOM_Function) aRefFace = aII.GetFace();
509     TopoDS_Shape          aShapeFace = aRefFace->GetValue();
510
511     if (aShapeFace.ShapeType() == TopAbs_FACE) {
512       TopoDS_Face   aFace  = TopoDS::Face(aShapeFace);
513       bool          isUIso = aII.GetIsUIso();
514       Standard_Real aParam = aII.GetParameter();
515       Standard_Real U1,U2,V1,V2;
516
517       // Construct a real geometric parameter.
518       aFace.Orientation(TopAbs_FORWARD);
519       ShapeAnalysis::GetFaceUVBounds(aFace,U1,U2,V1,V2);
520
521       if (isUIso) {
522         aParam = U1 + (U2 - U1)*aParam;
523       } else {
524         aParam = V1 + (V2 - V1)*aParam;
525       }
526
527       aShape = MakeIsoline(aFace, isUIso, aParam);
528     } else {
529       Standard_NullObject::Raise
530         ("Shape for isoline construction is not a face");
531     }
532   }
533   else {
534   }
535
536   if (aShape.IsNull()) return 0;
537
538   // Check shape validity
539   BRepCheck_Analyzer ana (aShape, false);
540   if (!ana.IsValid()) {
541     //Standard_ConstructionError::Raise("Algorithm have produced an invalid shape result");
542     // For Mantis issue 0021772: EDF 2336 GEOM: Non valid face created from two circles
543     Handle(ShapeFix_Shape) aSfs = new ShapeFix_Shape (aShape);
544     aSfs->Perform();
545     aShape = aSfs->Shape();
546   }
547
548   // Check if the result shape type is compatible with the expected.
549   const TopAbs_ShapeEnum aShType = aShape.ShapeType();
550
551   if (anExpectedType != TopAbs_SHAPE && anExpectedType != aShType) {
552     Standard_ConstructionError::Raise("Result type check failed");
553   }
554
555   aFunction->SetValue(aShape);
556
557   log.SetTouched(Label());
558
559   if (!aWarning.IsEmpty())
560     Standard_Failure::Raise(aWarning.ToCString());
561
562   return 1;
563 }
564
565 TopoDS_Wire GEOMImpl_ShapeDriver::MakeWireFromEdges(const Handle(TColStd_HSequenceOfTransient)& theEdgesFuncs,
566                                                     const Standard_Real theTolerance)
567 {
568   BRep_Builder B;
569
570   TopoDS_Wire aWire;
571   B.MakeWire(aWire);
572
573   // add edges
574   for (unsigned int ind = 1; ind <= theEdgesFuncs->Length(); ind++) {
575     Handle(GEOM_Function) aRefShape = Handle(GEOM_Function)::DownCast(theEdgesFuncs->Value(ind));
576     TopoDS_Shape aShape_i = aRefShape->GetValue();
577     if (aShape_i.IsNull()) {
578       Standard_NullObject::Raise("Shape for wire construction is null");
579     }
580     if (aShape_i.ShapeType() == TopAbs_EDGE || aShape_i.ShapeType() == TopAbs_WIRE) {
581       TopExp_Explorer exp (aShape_i, TopAbs_EDGE);
582       for (; exp.More(); exp.Next())
583         B.Add(aWire, TopoDS::Edge(exp.Current()));
584     } else {
585       Standard_TypeMismatch::Raise
586         ("Shape for wire construction is neither an edge nor a wire");
587     }
588   }
589
590   // fix edges order
591   Handle(ShapeFix_Wire) aFW = new ShapeFix_Wire;
592   aFW->Load(aWire);
593   aFW->FixReorder();
594
595   if (aFW->StatusReorder(ShapeExtend_FAIL1)) {
596     Standard_ConstructionError::Raise("Wire construction failed: several loops detected");
597   }
598   else if (aFW->StatusReorder(ShapeExtend_FAIL)) {
599     Standard_ConstructionError::Raise("Wire construction failed");
600   }
601   else {
602   }
603
604   // IMP 0019766: Building a Wire from unconnected edges by introducing a tolerance
605   aFW->ClosedWireMode() = Standard_False;
606   aFW->FixConnected(theTolerance);
607   if (aFW->StatusConnected(ShapeExtend_FAIL)) {
608     Standard_ConstructionError::Raise("Wire construction failed: cannot build connected wire");
609   }
610   // IMP 0019766
611   if (aFW->StatusConnected(ShapeExtend_DONE3)) {
612     // Confused with <prec> but not Analyzer.Precision(), set the same
613     aFW->FixGapsByRangesMode() = Standard_True;
614     if (aFW->FixGaps3d()) {
615       Handle(ShapeExtend_WireData) sbwd = aFW->WireData();
616       Handle(ShapeFix_Edge) aFe = new ShapeFix_Edge;
617       for (Standard_Integer iedge = 1; iedge <= sbwd->NbEdges(); iedge++) {
618         TopoDS_Edge aEdge = TopoDS::Edge(sbwd->Edge(iedge));
619         aFe->FixVertexTolerance(aEdge);
620         aFe->FixSameParameter(aEdge);
621       }
622     }
623     else if (aFW->StatusGaps3d(ShapeExtend_FAIL)) {
624       Standard_ConstructionError::Raise("Wire construction failed: cannot fix 3d gaps");
625     }
626   }
627   aWire = aFW->WireAPIMake();
628
629   return aWire;
630 }
631
632 TopoDS_Edge GEOMImpl_ShapeDriver::MakeEdgeFromWire(const TopoDS_Shape& aWire,
633                                                    const Standard_Real LinTol,
634                                                    const Standard_Real AngTol)
635 {
636     TopoDS_Edge ResEdge;
637
638     BRepLib::BuildCurves3d(aWire);
639     Handle(ShapeFix_Shape) Fixer = new ShapeFix_Shape(aWire);
640     Fixer->SetPrecision(LinTol);
641     Fixer->SetMaxTolerance(LinTol);
642     Fixer->Perform();
643     TopoDS_Wire theWire = TopoDS::Wire(Fixer->Shape());
644
645     TColGeom_SequenceOfCurve CurveSeq;
646     TopTools_SequenceOfShape LocSeq;
647     TColStd_SequenceOfReal FparSeq;
648     TColStd_SequenceOfReal LparSeq;
649     TColStd_SequenceOfReal TolSeq;
650     GeomAbs_CurveType CurType;
651     TopoDS_Vertex FirstVertex, LastVertex;
652
653     BRepTools_WireExplorer wexp(theWire) ;
654     for (; wexp.More(); wexp.Next())
655     {
656       TopoDS_Edge anEdge = wexp.Current();
657       Standard_Real fpar, lpar;
658       TopLoc_Location aLoc;
659       Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aLoc, fpar, lpar);
660       if (aCurve.IsNull())
661         continue;
662
663       BRepAdaptor_Curve BAcurve(anEdge);
664       GeomAbs_CurveType aType = BAcurve.GetType();
665
666       Handle(Geom_Curve) aBasisCurve = BAcurve.Curve().Curve();
667
668       if (aBasisCurve->IsPeriodic())
669         ElCLib::AdjustPeriodic(aBasisCurve->FirstParameter(), aBasisCurve->LastParameter(),
670                                Precision::PConfusion(), fpar, lpar);
671
672       if (CurveSeq.IsEmpty())
673       {
674         CurveSeq.Append(aCurve);
675         TopoDS_Shape aLocShape;
676         aLocShape.Location(aLoc);
677         aLocShape.Orientation(wexp.Orientation());
678         LocSeq.Append(aLocShape);
679         FparSeq.Append(fpar);
680         LparSeq.Append(lpar);
681         CurType = aType;
682         FirstVertex = wexp.CurrentVertex();
683       }
684       else
685       {
686         Standard_Boolean Done = Standard_False;
687         Standard_Real NewFpar, NewLpar;
688         GeomAdaptor_Curve GAprevcurve(CurveSeq.Last());
689         TopoDS_Vertex CurVertex = wexp.CurrentVertex();
690         TopoDS_Vertex CurFirstVer = TopExp::FirstVertex(anEdge);
691         TopAbs_Orientation ConnectByOrigin = (CurVertex.IsSame(CurFirstVer))? TopAbs_FORWARD : TopAbs_REVERSED;
692         if (aCurve == CurveSeq.Last())
693         {
694           NewFpar = fpar;
695           NewLpar = lpar;
696           if (aBasisCurve->IsPeriodic())
697           {
698             if (NewLpar < NewFpar)
699               NewLpar += aBasisCurve->Period();
700             if (ConnectByOrigin == TopAbs_FORWARD)
701               ElCLib::AdjustPeriodic(FparSeq.Last(),
702                                      FparSeq.Last() + aBasisCurve->Period(),
703                                      Precision::PConfusion(), NewFpar, NewLpar);
704             else
705               ElCLib::AdjustPeriodic(FparSeq.Last() - aBasisCurve->Period(),
706                                      FparSeq.Last(),
707                                      Precision::PConfusion(), NewFpar, NewLpar);
708           }
709           Done = Standard_True;
710         }
711         else if (aType == CurType &&
712                  aType != GeomAbs_BezierCurve &&
713                  aType != GeomAbs_BSplineCurve &&
714                  aType != GeomAbs_OtherCurve)
715         {
716           switch (aType)
717           {
718           case GeomAbs_Line:
719             {
720               gp_Lin aLine    = BAcurve.Line();
721               gp_Lin PrevLine = GAprevcurve.Line();
722               if (aLine.Contains(PrevLine.Location(), LinTol) &&
723                   aLine.Direction().IsParallel(PrevLine.Direction(), AngTol))
724               {
725                 gp_Pnt P1 = ElCLib::Value(fpar, aLine);
726                 gp_Pnt P2 = ElCLib::Value(lpar, aLine);
727                 NewFpar = ElCLib::Parameter(PrevLine, P1);
728                 NewLpar = ElCLib::Parameter(PrevLine, P2);
729                 if (NewLpar < NewFpar)
730                 {
731                   Standard_Real MemNewFpar = NewFpar;
732                   NewFpar = NewLpar;
733                   NewLpar = MemNewFpar;
734                   ConnectByOrigin = TopAbs::Reverse(ConnectByOrigin);
735                 }
736                 Done = Standard_True;
737               }
738               break;
739             }
740           case GeomAbs_Circle:
741             {
742               gp_Circ aCircle    = BAcurve.Circle();
743               gp_Circ PrevCircle = GAprevcurve.Circle();
744               if (aCircle.Location().Distance(PrevCircle.Location()) <= LinTol &&
745                   Abs(aCircle.Radius() - PrevCircle.Radius()) <= LinTol &&
746                   aCircle.Axis().IsParallel(PrevCircle.Axis(), AngTol))
747               {
748                 if (aCircle.Axis().Direction() * PrevCircle.Axis().Direction() < 0.)
749                 {
750                   Standard_Real memfpar = fpar;
751                   fpar = lpar;
752                   lpar = memfpar;
753                   ConnectByOrigin = TopAbs::Reverse(ConnectByOrigin);
754                 }
755                 gp_Pnt P1 = ElCLib::Value(fpar, aCircle);
756                 gp_Pnt P2 = ElCLib::Value(lpar, aCircle);
757                 NewFpar = ElCLib::Parameter(PrevCircle, P1);
758                 NewLpar = ElCLib::Parameter(PrevCircle, P2);
759                 if (NewLpar < NewFpar)
760                   NewLpar += 2.*M_PI;
761                 //Standard_Real MemNewFpar = NewFpar, MemNewLpar =  NewLpar;
762                 if (ConnectByOrigin == TopAbs_FORWARD)
763                   ElCLib::AdjustPeriodic(FparSeq.Last(),
764                                          FparSeq.Last() + 2.*M_PI,
765                                          Precision::PConfusion(), NewFpar, NewLpar);
766                 else
767                   ElCLib::AdjustPeriodic(FparSeq.Last() - 2.*M_PI,
768                                          FparSeq.Last(),
769                                          Precision::PConfusion(), NewFpar, NewLpar);
770                 Done = Standard_True;
771               }
772               break;
773             }
774           case GeomAbs_Ellipse:
775             {
776               gp_Elips anEllipse   = BAcurve.Ellipse();
777               gp_Elips PrevEllipse = GAprevcurve.Ellipse();
778               if (anEllipse.Focus1().Distance(PrevEllipse.Focus1()) <= LinTol &&
779                   anEllipse.Focus2().Distance(PrevEllipse.Focus2()) <= LinTol &&
780                   Abs(anEllipse.MajorRadius() - PrevEllipse.MajorRadius()) <= LinTol &&
781                   Abs(anEllipse.MinorRadius() - PrevEllipse.MinorRadius()) <= LinTol &&
782                   anEllipse.Axis().IsParallel(PrevEllipse.Axis(), AngTol))
783               {
784                 if (anEllipse.Axis().Direction() * PrevEllipse.Axis().Direction() < 0.)
785                 {
786                   Standard_Real memfpar = fpar;
787                   fpar = lpar;
788                   lpar = memfpar;
789                   ConnectByOrigin = TopAbs::Reverse(ConnectByOrigin);
790                 }
791                 gp_Pnt P1 = ElCLib::Value(fpar, anEllipse);
792                 gp_Pnt P2 = ElCLib::Value(lpar, anEllipse);
793                 NewFpar = ElCLib::Parameter(PrevEllipse, P1);
794                 NewLpar = ElCLib::Parameter(PrevEllipse, P2);
795                 if (NewLpar < NewFpar)
796                   NewLpar += 2.*M_PI;
797                 if (ConnectByOrigin == TopAbs_FORWARD)
798                   ElCLib::AdjustPeriodic(FparSeq.Last(),
799                                          FparSeq.Last() + 2.*M_PI,
800                                          Precision::PConfusion(), NewFpar, NewLpar);
801                 else
802                   ElCLib::AdjustPeriodic(FparSeq.Last() - 2.*M_PI,
803                                          FparSeq.Last(),
804                                          Precision::PConfusion(), NewFpar, NewLpar);
805                 Done = Standard_True;
806               }
807               break;
808             }
809           case GeomAbs_Hyperbola:
810             {
811               gp_Hypr aHypr    = BAcurve.Hyperbola();
812               gp_Hypr PrevHypr = GAprevcurve.Hyperbola();
813               if (aHypr.Focus1().Distance(PrevHypr.Focus1()) <= LinTol &&
814                   aHypr.Focus2().Distance(PrevHypr.Focus2()) <= LinTol &&
815                   Abs(aHypr.MajorRadius() - PrevHypr.MajorRadius()) <= LinTol &&
816                   Abs(aHypr.MinorRadius() - PrevHypr.MinorRadius()) <= LinTol &&
817                   aHypr.Axis().IsParallel(PrevHypr.Axis(), AngTol))
818               {
819                 gp_Pnt P1 = ElCLib::Value(fpar, aHypr);
820                 gp_Pnt P2 = ElCLib::Value(lpar, aHypr);
821                 NewFpar = ElCLib::Parameter(PrevHypr, P1);
822                 NewLpar = ElCLib::Parameter(PrevHypr, P2);
823                 if (NewLpar < NewFpar)
824                 {
825                   Standard_Real MemNewFpar = NewFpar;
826                   NewFpar = NewLpar;
827                   NewLpar = MemNewFpar;
828                   ConnectByOrigin = TopAbs::Reverse(ConnectByOrigin);
829                 }
830                 Done = Standard_True;
831               }
832               break;
833             }
834           case GeomAbs_Parabola:
835             {
836               gp_Parab aParab    = BAcurve.Parabola();
837               gp_Parab PrevParab = GAprevcurve.Parabola();
838               if (aParab.Location().Distance(PrevParab.Location()) <= LinTol &&
839                   aParab.Focus().Distance(PrevParab.Focus()) <= LinTol &&
840                   Abs(aParab.Focal() - PrevParab.Focal()) <= LinTol &&
841                   aParab.Axis().IsParallel(PrevParab.Axis(), AngTol))
842               {
843                 gp_Pnt P1 = ElCLib::Value(fpar, aParab);
844                 gp_Pnt P2 = ElCLib::Value(lpar, aParab);
845                 NewFpar = ElCLib::Parameter(PrevParab, P1);
846                 NewLpar = ElCLib::Parameter(PrevParab, P2);
847                 if (NewLpar < NewFpar)
848                 {
849                   Standard_Real MemNewFpar = NewFpar;
850                   NewFpar = NewLpar;
851                   NewLpar = MemNewFpar;
852                   ConnectByOrigin = TopAbs::Reverse(ConnectByOrigin);
853                 }
854                 Done = Standard_True;
855               }
856               break;
857             }
858           } //end of switch (aType)
859         } // end of else if (aType == CurType && ...
860         if (Done)
861         {
862           if (NewFpar < FparSeq.Last())
863             FparSeq(FparSeq.Length()) = NewFpar;
864           else
865             LparSeq(LparSeq.Length()) = NewLpar;
866         }
867         else
868         {
869           CurveSeq.Append(aCurve);
870           TopoDS_Shape aLocShape;
871           aLocShape.Location(aLoc);
872           aLocShape.Orientation(wexp.Orientation());
873           LocSeq.Append(aLocShape);
874           FparSeq.Append(fpar);
875           LparSeq.Append(lpar);
876           TolSeq.Append(BRep_Tool::Tolerance(CurVertex));
877           CurType = aType;
878         }
879       } // end of else (CurveSeq.IsEmpty()) -> not first time
880     } // end for (; wexp.More(); wexp.Next())
881
882     LastVertex = wexp.CurrentVertex();
883     TolSeq.Append(BRep_Tool::Tolerance(LastVertex));
884
885     FirstVertex.Orientation(TopAbs_FORWARD);
886     LastVertex.Orientation(TopAbs_REVERSED);
887
888     if (!CurveSeq.IsEmpty())
889     {
890       Standard_Integer nb_curve = CurveSeq.Length();   //number of curves
891       TColGeom_Array1OfBSplineCurve tab(0,nb_curve-1);                    //array of the curves
892       TColStd_Array1OfReal tabtolvertex(0,nb_curve-1); //(0,nb_curve-2);  //array of the tolerances
893
894       Standard_Integer i;
895
896       if (nb_curve > 1)
897       {
898         for (i = 1; i <= nb_curve; i++)
899         {
900           if (CurveSeq(i)->IsInstance(STANDARD_TYPE(Geom_TrimmedCurve)))
901             CurveSeq(i) = (*((Handle(Geom_TrimmedCurve)*)&(CurveSeq(i))))->BasisCurve();
902
903           Handle(Geom_TrimmedCurve) aTrCurve = new Geom_TrimmedCurve(CurveSeq(i), FparSeq(i), LparSeq(i));
904           tab(i-1) = GeomConvert::CurveToBSplineCurve(aTrCurve);
905           tab(i-1)->Transform(LocSeq(i).Location().Transformation());
906           GeomConvert::C0BSplineToC1BSplineCurve(tab(i-1), Precision::Confusion());
907           if (LocSeq(i).Orientation() == TopAbs_REVERSED)
908             tab(i-1)->Reverse();
909
910           //Temporary
911           //char* name = new char[100];
912           //sprintf(name, "c%d", i);
913           //DrawTrSurf::Set(name, tab(i-1));
914
915           if (i > 1)
916             tabtolvertex(i-2) = TolSeq(i-1);
917         } // end for (i = 1; i <= nb_curve; i++)
918         tabtolvertex(nb_curve-1) = TolSeq(TolSeq.Length());
919
920         Standard_Boolean closed_flag = Standard_False;
921         Standard_Real closed_tolerance = 0.;
922         if (FirstVertex.IsSame(LastVertex) &&
923             GeomLProp::Continuity(tab(0), tab(nb_curve-1),
924                                   tab(0)->FirstParameter(),
925                                   tab(nb_curve-1)->LastParameter(),
926                                   Standard_False, Standard_False, LinTol, AngTol) >= GeomAbs_G1)
927         {
928           closed_flag = Standard_True ;
929           closed_tolerance = BRep_Tool::Tolerance(FirstVertex);
930         }
931
932         Handle(TColGeom_HArray1OfBSplineCurve)  concatcurve;     //array of the concatenated curves
933         Handle(TColStd_HArray1OfInteger)        ArrayOfIndices;  //array of the remining Vertex
934         GeomConvert::ConcatC1(tab,
935                               tabtolvertex,
936                               ArrayOfIndices,
937                               concatcurve,
938                               closed_flag,
939                               closed_tolerance);   //C1 concatenation
940
941         if (concatcurve->Length() > 1)
942         {
943           GeomConvert_CompCurveToBSplineCurve Concat(concatcurve->Value(concatcurve->Lower()));
944
945           for (i = concatcurve->Lower()+1; i <= concatcurve->Upper(); i++)
946             Concat.Add( concatcurve->Value(i), LinTol, Standard_True );
947
948           concatcurve->SetValue(concatcurve->Lower(), Concat.BSplineCurve());
949         }
950         // rnc : prevents the driver from building an edge without C1 continuity
951         if (concatcurve->Value(concatcurve->Lower())->Continuity()==GeomAbs_C0){
952           Standard_ConstructionError::Raise("Construction aborted : The given Wire has sharp bends between some Edges, no valid Edge can be built");
953         }
954
955         Standard_Boolean isValidEndVtx = Standard_True;
956
957         if (closed_flag) {
958           // Check if closed curve is reordered.
959           Handle(Geom_Curve) aCurve  = concatcurve->Value(concatcurve->Lower());
960           Standard_Real      aFPar   = aCurve->FirstParameter();
961           gp_Pnt             aPFirst;
962           gp_Pnt             aPntVtx = BRep_Tool::Pnt(FirstVertex);
963           Standard_Real      aTolVtx = BRep_Tool::Tolerance(FirstVertex);
964
965           aCurve->D0(aFPar, aPFirst);
966
967           if (!aPFirst.IsEqual(aPntVtx, aTolVtx)) {
968             // The curve is reordered. Find the new first and last vertices.
969             TopTools_IndexedMapOfShape aMapVtx;
970             TopExp::MapShapes(theWire, TopAbs_VERTEX, aMapVtx);
971
972             const Standard_Integer aNbVtx = aMapVtx.Extent();
973             Standard_Integer       iVtx;
974
975             for (iVtx = 1; iVtx <= aNbVtx; iVtx++) {
976               const TopoDS_Vertex aVtx = TopoDS::Vertex(aMapVtx.FindKey(iVtx));
977               const gp_Pnt        aPnt = BRep_Tool::Pnt(aVtx);
978               const Standard_Real aTol = BRep_Tool::Tolerance(aVtx);
979
980               if (aPFirst.IsEqual(aPnt, aTol)) {
981                 // The coinsident vertex is found.
982                 FirstVertex = aVtx;
983                 LastVertex  = aVtx;
984                 FirstVertex.Orientation(TopAbs_FORWARD);
985                 LastVertex.Orientation(TopAbs_REVERSED);
986                 break;
987               }
988             }
989
990             if (iVtx > aNbVtx) {
991               // It is necessary to create new vertices.
992               isValidEndVtx = Standard_False;
993             }
994           }
995         }
996
997         if (isValidEndVtx) {
998           ResEdge = BRepLib_MakeEdge(concatcurve->Value(concatcurve->Lower()),
999                                      FirstVertex, LastVertex,
1000                                      concatcurve->Value(concatcurve->Lower())->FirstParameter(),
1001                                      concatcurve->Value(concatcurve->Lower())->LastParameter());
1002         } else {
1003           ResEdge = BRepLib_MakeEdge(concatcurve->Value(concatcurve->Lower()),
1004                                      concatcurve->Value(concatcurve->Lower())->FirstParameter(),
1005                                      concatcurve->Value(concatcurve->Lower())->LastParameter());
1006         }
1007       }
1008       else
1009       {
1010         if (CurveSeq(1)->IsInstance(STANDARD_TYPE(Geom_TrimmedCurve)))
1011           CurveSeq(1) = (*((Handle(Geom_TrimmedCurve)*)&(CurveSeq(1))))->BasisCurve();
1012
1013         Handle(Geom_Curve) aNewCurve =
1014           Handle(Geom_Curve)::DownCast(CurveSeq(1)->Copy());
1015
1016         aNewCurve->Transform(LocSeq(1).Location().Transformation());
1017
1018         if (LocSeq(1).Orientation() == TopAbs_REVERSED) {
1019           const TopoDS_Vertex aVtxTmp = FirstVertex;
1020
1021           FirstVertex = LastVertex;
1022           LastVertex  = aVtxTmp;
1023           FirstVertex.Orientation(TopAbs_FORWARD);
1024           LastVertex.Orientation(TopAbs_REVERSED);
1025         }
1026
1027         ResEdge = BRepLib_MakeEdge(aNewCurve,
1028                                    FirstVertex, LastVertex,
1029                                    FparSeq(1), LparSeq(1));
1030
1031         if (LocSeq(1).Orientation() == TopAbs_REVERSED) {
1032           ResEdge.Reverse();
1033         }
1034       }
1035     }
1036
1037     return ResEdge;
1038 }
1039
1040 //=============================================================================
1041 /*!
1042  * \brief Returns an isoline for a face.
1043  */
1044 //=============================================================================
1045
1046 TopoDS_Shape GEOMImpl_ShapeDriver::MakeIsoline
1047                             (const TopoDS_Face &theFace,
1048                              const bool         IsUIso,
1049                              const double       theParameter) const
1050 {
1051   TopoDS_Shape          aResult;
1052   GEOMUtils::Hatcher    aHatcher(theFace);
1053   const GeomAbs_IsoType aType = (IsUIso ? GeomAbs_IsoU : GeomAbs_IsoV);
1054
1055   aHatcher.Init(aType, theParameter);
1056   aHatcher.Perform();
1057
1058   if (!aHatcher.IsDone()) {
1059     Standard_ConstructionError::Raise("MakeIsoline : Hatcher failure");
1060   }
1061
1062   const Handle(TColStd_HArray1OfInteger) &anIndices =
1063     (IsUIso ? aHatcher.GetUIndices() : aHatcher.GetVIndices());
1064
1065   if (anIndices.IsNull()) {
1066     Standard_ConstructionError::Raise("MakeIsoline : Null hatching indices");
1067   }
1068
1069   const Standard_Integer anIsoInd = anIndices->Lower();
1070   const Standard_Integer aHatchingIndex = anIndices->Value(anIsoInd);
1071
1072   if (aHatchingIndex == 0) {
1073     Standard_ConstructionError::Raise("MakeIsoline : Invalid hatching index");
1074   }
1075
1076   const Standard_Integer aNbDomains =
1077     aHatcher.GetNbDomains(aHatchingIndex);
1078
1079   if (aNbDomains < 0) {
1080     Standard_ConstructionError::Raise("MakeIsoline : Invalid number of domains");
1081   }
1082
1083   // The hatching is performed successfully. Create the 3d Curve.
1084   Handle(Geom_Surface) aSurface   = BRep_Tool::Surface(theFace);
1085   Handle(Geom_Curve)   anIsoCurve = (IsUIso ?
1086     aSurface->UIso(theParameter) : aSurface->VIso(theParameter));
1087   Handle(Geom2d_Curve) aPIsoCurve =
1088     aHatcher.GetHatching(aHatchingIndex);
1089   const Standard_Real  aTol = Precision::Confusion();
1090   Standard_Integer     anIDom = 1;
1091   Standard_Real        aV1;
1092   Standard_Real        aV2;
1093   BRep_Builder         aBuilder;
1094   Standard_Integer     aNbEdges = 0;
1095
1096   for (; anIDom <= aNbDomains; anIDom++) {
1097     if (aHatcher.GetDomain(aHatchingIndex, anIDom, aV1, aV2)) {
1098       // Check first and last parameters.
1099       if (!aHatcher.IsDomainInfinite(aHatchingIndex, anIDom)) {
1100         // Create an edge.
1101         TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge(anIsoCurve, aV1, aV2);
1102
1103         // Update it with a parametric curve on face.
1104         aBuilder.UpdateEdge(anEdge, aPIsoCurve, theFace, aTol);
1105         aNbEdges++;
1106
1107         if (aNbEdges > 1) {
1108           // Result is a compond.
1109           if (aNbEdges == 2) {
1110             // Create a new compound.
1111             TopoDS_Compound aCompound;
1112
1113             aBuilder.MakeCompound(aCompound);
1114             aBuilder.Add(aCompound, aResult);
1115             aResult = aCompound;
1116           }
1117
1118           // Add an edge to the compound.
1119           aBuilder.Add(aResult, anEdge);
1120         } else {
1121           // Result is the edge.
1122           aResult = anEdge;
1123         }
1124       }
1125     }
1126   }
1127
1128   if (aNbEdges == 0) {
1129     Standard_ConstructionError::Raise("MakeIsoline : Empty result");
1130   }
1131
1132   return aResult;
1133 }
1134
1135 //================================================================================
1136 /*!
1137  * \brief Returns a name of creation operation and names and values of creation parameters
1138  */
1139 //================================================================================
1140
1141 bool GEOMImpl_ShapeDriver::
1142 GetCreationInformation(std::string&             theOperationName,
1143                        std::vector<GEOM_Param>& theParams)
1144 {
1145   if (Label().IsNull()) return 0;
1146   Handle(GEOM_Function) function = GEOM_Function::GetFunction(Label());
1147
1148   GEOMImpl_IShapes aCI( function );
1149   Standard_Integer aType = function->GetType();
1150
1151   switch ( aType ) {
1152   case WIRE_EDGES:
1153     theOperationName = "WIRE";
1154     AddParam( theParams, "Wires/edges", aCI.GetShapes() );
1155     AddParam( theParams, "Tolerance", aCI.GetTolerance() );
1156     break;
1157   case FACE_WIRE:
1158     theOperationName = "FACE";
1159     AddParam( theParams, "Wire/edge", aCI.GetBase() );
1160     AddParam( theParams, "Is planar wanted", aCI.GetIsPlanar() );
1161     break;
1162   case FACE_WIRES:
1163     theOperationName = "FACE";
1164     AddParam( theParams, "Wires/edges", aCI.GetShapes() );
1165     AddParam( theParams, "Is planar wanted", aCI.GetIsPlanar() );
1166     break;
1167   case SHELL_FACES:
1168     theOperationName = "SHELL";
1169     AddParam( theParams, "Objects", aCI.GetShapes() );
1170     break;
1171   case SOLID_SHELLS:
1172     theOperationName = "SOLID";
1173     AddParam( theParams, "Objects", aCI.GetShapes() );
1174     break;
1175   case COMPOUND_SHAPES:
1176     theOperationName = "COMPOUND";
1177     AddParam( theParams, "Objects", aCI.GetShapes() );
1178     break;
1179   case EDGE_WIRE:
1180     theOperationName = "EDGE";
1181     AddParam( theParams, "Wire", aCI.GetBase() );
1182     AddParam( theParams, "Linear Tolerance", aCI.GetTolerance() );
1183     AddParam( theParams, "Angular Tolerance", aCI.GetAngularTolerance() );
1184     break;
1185   case EDGE_CURVE_LENGTH:
1186     theOperationName = "EDGE";
1187     {
1188       GEOMImpl_IVector aCI( function );
1189       AddParam( theParams, "Edge", aCI.GetPoint1() );
1190       AddParam( theParams, "Start point", aCI.GetPoint2() );
1191       AddParam( theParams, "Length", aCI.GetParameter() );
1192     }
1193     break;
1194   case SHAPES_ON_SHAPE:
1195   {
1196     theOperationName = "GetShapesOnShapeAsCompound";
1197     Handle(TColStd_HSequenceOfTransient) shapes = aCI.GetShapes();
1198     if ( !shapes.IsNull() && shapes->Length() > 0 )
1199       AddParam( theParams, "Check shape", shapes->Value(1) );
1200     if ( !shapes.IsNull() && shapes->Length() > 1 )
1201       AddParam( theParams, "Shape", shapes->Value(2) );
1202     AddParam( theParams, "Shape type", TopAbs_ShapeEnum( aCI.GetSubShapeType() ));
1203     AddParam( theParams, "State", TopAbs_State((int) aCI.GetTolerance() ));
1204     break;
1205   }
1206   case SHAPE_ISOLINE:
1207   {
1208     GEOMImpl_IIsoline aII (function);
1209
1210     theOperationName = "ISOLINE";
1211     AddParam(theParams, "Face", aII.GetFace());
1212     AddParam(theParams, "Isoline type", (aII.GetIsUIso() ? "U" : "V"));
1213     AddParam(theParams, "Parameter", aII.GetParameter());
1214     break;
1215   }
1216   default:
1217     return false;
1218   }
1219
1220   return true;
1221 }
1222
1223 IMPLEMENT_STANDARD_HANDLE (GEOMImpl_ShapeDriver,GEOM_BaseDriver);
1224 IMPLEMENT_STANDARD_RTTIEXT (GEOMImpl_ShapeDriver,GEOM_BaseDriver);
1225
1226 //modified by NIZNHY-PKV Wed Dec 28 13:48:31 2011f
1227 #include <TopoDS_Iterator.hxx>
1228 #include <TopTools_HSequenceOfShape.hxx>
1229 #include <ShapeAnalysis_FreeBounds.hxx>
1230 #include <TopTools_MapOfShape.hxx>
1231 #include <TopTools_MapOfOrientedShape.hxx>
1232 #include <BRep_Builder.hxx>
1233 #include <TopoDS_Wire.hxx>
1234
1235 //=======================================================================
1236 //function : KeepEdgesOrder
1237 //purpose  : 
1238 //=======================================================================
1239 /*
1240 void KeepEdgesOrder(const Handle(TopTools_HSequenceOfShape)& aEdges,
1241                     const Handle(TopTools_HSequenceOfShape)& aWires)
1242 {
1243   Standard_Integer aNbWires, aNbEdges;
1244   // 
1245   if (aEdges.IsNull()) {
1246     return;
1247   }
1248   //
1249   if (aWires.IsNull()) {
1250     return;
1251   }
1252   //
1253   aNbEdges=aEdges->Length();
1254   aNbWires=aWires->Length();
1255   if (!aNbEdges || !aNbWires) {
1256     return;
1257   }
1258   //-----
1259   Standard_Boolean bClosed;
1260   Standard_Integer i, j;
1261   TopoDS_Wire aWy;
1262   TopoDS_Iterator aIt;
1263   BRep_Builder aBB;
1264   TopTools_MapOfOrientedShape aMEx;
1265   //
1266   for (i=1; i<=aNbWires; ++i) {
1267     const TopoDS_Shape& aWx=aWires->Value(i);
1268     //
1269     aMEx.Clear();
1270     aIt.Initialize (aWx);
1271     for (; aIt.More(); aIt.Next()) {
1272       const TopoDS_Shape& aEx=aIt.Value();
1273       aMEx.Add(aEx);
1274     }
1275     // aWy
1276     aBB.MakeWire (aWy);
1277     for (j=1; j<=aNbEdges; ++j) {
1278       const TopoDS_Shape& aE=aEdges->Value(j);
1279       if (aMEx.Contains(aE)) {
1280         aBB.Add(aWy, aE);
1281       }
1282     }
1283     //
1284     bClosed=aWx.Closed();
1285     aWy.Closed(bClosed);
1286     //
1287     aWires->Append(aWy);
1288   }// for (i=1; i<=aNbWires; ++i) {
1289   //
1290   aWires->Remove(1, aNbWires);
1291 }
1292 */
1293 //modified by NIZNHY-PKV Wed Dec 28 13:48:34 2011t