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