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