Salome HOME
a2b31ef3a9c8e03f984b59a889f312da730be126
[modules/geom.git] / src / GEOMImpl / GEOMImpl_IShapesOperations.cxx
1 // Copyright (C) 2007-2016  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 //  File      : GEOMImpl_IShapesOperations.cxx
24 //  Created   :
25 //  Author    : modified by Lioka RAZAFINDRAZAKA (CEA) 22/06/2007
26 //  Project   : SALOME
27
28 //#include <Standard_Stream.hxx>
29
30 #include "GEOMImpl_IShapesOperations.hxx"
31
32 #include "GEOMImpl_Types.hxx"
33
34 #include "GEOMImpl_VectorDriver.hxx"
35 #include "GEOMImpl_ShapeDriver.hxx"
36 #include "GEOMImpl_GlueDriver.hxx"
37 #include "GEOMImpl_FillingDriver.hxx"
38
39 #include "GEOMImpl_IExtract.hxx"
40 #include "GEOMImpl_IVector.hxx"
41 #include "GEOMImpl_IShapes.hxx"
42 #include "GEOMImpl_IShapeExtend.hxx"
43 #include "GEOMImpl_IGlue.hxx"
44 #include "GEOMImpl_IFilling.hxx"
45
46 #include "GEOMImpl_Block6Explorer.hxx"
47 #include "GEOMImpl_IHealingOperations.hxx"
48
49 #include "GEOMImpl_Gen.hxx"
50
51 #include "GEOM_Function.hxx"
52 #include "GEOM_ISubShape.hxx"
53 #include "GEOM_PythonDump.hxx"
54
55 #include "GEOMUtils.hxx"
56
57 #include "GEOMAlgo_ClsfBox.hxx"
58 #include "GEOMAlgo_ClsfQuad.hxx"
59 #include "GEOMAlgo_ClsfSolid.hxx"
60 #include "GEOMAlgo_ClsfSurf.hxx"
61 #include "GEOMAlgo_FinderShapeOn2.hxx"
62 #include "GEOMAlgo_GetInPlace.hxx"
63 #include "GEOMAlgo_GetInPlaceAPI.hxx"
64 #include "GEOMAlgo_GlueDetector.hxx"
65
66 #include <utilities.h>
67
68 #include <BRepAdaptor_Curve.hxx>
69 #include <BRepAdaptor_Surface.hxx>
70 #include <BRepTools.hxx>
71 #include <BRep_Builder.hxx>
72 #include <BRep_Tool.hxx>
73 #include <GeomLib_Tool.hxx>
74 #include <Geom_CylindricalSurface.hxx>
75 #include <Geom_Plane.hxx>
76 #include <Geom_SphericalSurface.hxx>
77 #include <Geom_Surface.hxx>
78 #include <Geom_TrimmedCurve.hxx>
79 #include <Precision.hxx>
80 #include <TDF_Tool.hxx>
81 #include <TDataStd_Integer.hxx>
82 #include <TDataStd_ListIteratorOfListOfExtendedString.hxx>
83 #include <TopExp.hxx>
84 #include <TopExp_Explorer.hxx>
85 #include <TopTools_DataMapIteratorOfDataMapOfShapeListOfShape.hxx>
86 #include <TopTools_IndexedMapOfShape.hxx>
87 #include <TopTools_ListIteratorOfListOfShape.hxx>
88 #include <TopTools_MapOfOrientedShape.hxx>
89 #include <TopTools_MapOfShape.hxx>
90 #include <TopTools_SequenceOfShape.hxx>
91 #include <TopoDS.hxx>
92 #include <TopoDS_TShape.hxx>
93 #include <TopoDS_Compound.hxx>
94 #include <TopoDS_Edge.hxx>
95 #include <TopoDS_Face.hxx>
96 #include <TopoDS_Iterator.hxx>
97 #include <TopoDS_Shape.hxx>
98 #include <TopoDS_Solid.hxx>
99 #include <TopoDS_Vertex.hxx>
100 #include <gp_Cylinder.hxx>
101 #include <gp_Pnt.hxx>
102
103 #include <vector>
104
105 #include <Standard_Failure.hxx>
106 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
107
108 // VSR 29/08/2017: 0023327, 0023428: eliminate unnecessary lines in Python dump
109 // Next macro, when defined, causes appearing of SubShapeAllIDs(), SubShapeAllSortedIDs(), GetSameIDs()
110 // and other such commands in Python dump.
111 // See also GEOM_Engine.cxx.
112 // ---------------------------------------
113 // #define DUMP_SUBSHAPE_IDS
114 // ---------------------------------------
115
116 namespace {
117
118   void AddFlatSubShapes(const TopoDS_Shape& S, TopTools_ListOfShape& L, TopTools_MapOfShape& M)
119   {
120     if (S.ShapeType() != TopAbs_COMPOUND) {
121       L.Append(S);
122     }
123     else {
124       TopoDS_Iterator It(S, Standard_True, Standard_True);
125       for (; It.More(); It.Next()) {
126         TopoDS_Shape SS = It.Value();
127         if (M.Add(SS))
128           AddFlatSubShapes(SS, L, M);
129       }
130     }
131   }
132 }
133
134 namespace
135 {
136   const double MAX_TOLERANCE = 1.e-7;
137
138   /**
139    * \brief Returns the vertex from theWhere shape that is coincident with
140    * theVertex.
141    *
142    * \param theWhere the shape where the coinsident vertex is searched.
143    * \param theVertex the vertex to be searched.
144    * \return the coincident vertex if it is found. Otherwise null object.
145    */
146   static TopoDS_Vertex getSameVertex(const TopoDS_Shape  &theWhere,
147                                      const TopoDS_Vertex &theVertex)
148   {
149     TopoDS_Vertex       aResult;
150     gp_Pnt              aPoint = BRep_Tool::Pnt(theVertex);
151     TopExp_Explorer     anExp(theWhere, TopAbs_VERTEX);
152     TopTools_MapOfShape aMap;
153     
154     for(; anExp.More(); anExp.Next()) {
155       const TopoDS_Shape &aLocalShape = anExp.Current();
156
157       if(!aMap.Add(aLocalShape)) {
158         continue;
159       }
160
161       TopoDS_Vertex aVertex = TopoDS::Vertex(aLocalShape);
162       gp_Pnt        aPoint2 = BRep_Tool::Pnt(aVertex);
163
164       if(aPoint.Distance(aPoint2) <= MAX_TOLERANCE) {
165         aResult = aVertex;
166         break;
167       }
168     }
169
170     return aResult;
171   }
172 } // end of namespace
173
174 //=============================================================================
175 /*!
176  *   constructor:
177  */
178 //=============================================================================
179 GEOMImpl_IShapesOperations::GEOMImpl_IShapesOperations (GEOM_Engine* theEngine)
180 : GEOM_IOperations(theEngine)
181 {
182   MESSAGE("GEOMImpl_IShapesOperations::GEOMImpl_IShapesOperations");
183 }
184
185 //=============================================================================
186 /*!
187  *  destructor
188  */
189 //=============================================================================
190 GEOMImpl_IShapesOperations::~GEOMImpl_IShapesOperations()
191 {
192   MESSAGE("GEOMImpl_IShapesOperations::~GEOMImpl_IShapesOperations");
193 }
194
195 //=============================================================================
196 /*!
197  *  MakeEdge
198  */
199 //=============================================================================
200 Handle(GEOM_Object) GEOMImpl_IShapesOperations::MakeEdge
201                      (Handle(GEOM_Object) thePnt1, Handle(GEOM_Object) thePnt2)
202 {
203   SetErrorCode(KO);
204
205   if (thePnt1.IsNull() || thePnt2.IsNull()) return NULL;
206
207   //Add a new Edge object
208   Handle(GEOM_Object) anEdge = GetEngine()->AddObject(GEOM_EDGE);
209
210   //Add a new Vector function
211   Handle(GEOM_Function) aFunction =
212     anEdge->AddFunction(GEOMImpl_VectorDriver::GetID(), VECTOR_TWO_PNT);
213
214   //Check if the function is set correctly
215   if (aFunction->GetDriverGUID() != GEOMImpl_VectorDriver::GetID()) return NULL;
216
217   GEOMImpl_IVector aPI (aFunction);
218
219   Handle(GEOM_Function) aRef1 = thePnt1->GetLastFunction();
220   Handle(GEOM_Function) aRef2 = thePnt2->GetLastFunction();
221   if (aRef1.IsNull() || aRef2.IsNull()) return NULL;
222
223   aPI.SetPoint1(aRef1);
224   aPI.SetPoint2(aRef2);
225
226   //Compute the Edge value
227   try {
228     OCC_CATCH_SIGNALS;
229     if (!GetSolver()->ComputeFunction(aFunction)) {
230       SetErrorCode("Vector driver failed");
231       return NULL;
232     }
233   }
234   catch (Standard_Failure& aFail) {
235     SetErrorCode(aFail.GetMessageString());
236     return NULL;
237   }
238
239   //Make a Python command
240   GEOM::TPythonDump(aFunction) << anEdge << " = geompy.MakeEdge("
241                                << thePnt1 << ", " << thePnt2 << ")";
242
243   SetErrorCode(OK);
244   return anEdge;
245 }
246
247 //=============================================================================
248 /*!
249  *  MakeEdgeOnCurveByLength
250  */
251 //=============================================================================
252 Handle(GEOM_Object) GEOMImpl_IShapesOperations::MakeEdgeOnCurveByLength
253                      (Handle(GEOM_Object) theRefCurve,
254                       const Standard_Real theLength,
255                       Handle(GEOM_Object) theStartPoint)
256 {
257   SetErrorCode(KO);
258
259   if (theRefCurve.IsNull()) return NULL;
260
261   //Add a new Edge object
262   Handle(GEOM_Object) anEdge = GetEngine()->AddObject(GEOM_EDGE);
263
264   //Add a new Vector function
265   Handle(GEOM_Function) aFunction =
266     anEdge->AddFunction(GEOMImpl_ShapeDriver::GetID(), EDGE_CURVE_LENGTH);
267
268   //Check if the function is set correctly
269   if (aFunction->GetDriverGUID() != GEOMImpl_ShapeDriver::GetID()) return NULL;
270
271   GEOMImpl_IVector aPI (aFunction);
272
273   Handle(GEOM_Function) aRef1 = theRefCurve->GetLastFunction();
274   if (aRef1.IsNull()) return NULL;
275   aPI.SetPoint1(aRef1);
276
277   if (!theStartPoint.IsNull()) {
278     Handle(GEOM_Function) aRef2 = theStartPoint->GetLastFunction();
279     aPI.SetPoint2(aRef2);
280   }
281
282   aPI.SetParameter(theLength);
283
284   //Compute the Edge value
285   try {
286     OCC_CATCH_SIGNALS;
287     if (!GetSolver()->ComputeFunction(aFunction)) {
288       SetErrorCode("Vector driver failed");
289       return NULL;
290     }
291   }
292   catch (Standard_Failure& aFail) {
293     SetErrorCode(aFail.GetMessageString());
294     return NULL;
295   }
296
297   //Make a Python command
298   GEOM::TPythonDump(aFunction) << anEdge << " = geompy.MakeEdgeOnCurveByLength("
299                                << theRefCurve << ", " << theLength << ", " << theStartPoint << ")";
300
301   SetErrorCode(OK);
302   return anEdge;
303 }
304
305 //=============================================================================
306 /*!
307  *  MakeEdgeWire
308  */
309 //=============================================================================
310 Handle(GEOM_Object) GEOMImpl_IShapesOperations::MakeEdgeWire
311                     (Handle(GEOM_Object) theWire,
312                      const Standard_Real theLinearTolerance,
313                      const Standard_Real theAngularTolerance)
314 {
315   SetErrorCode(KO);
316
317   if (theWire.IsNull()) return NULL;
318
319   //Add a new Edge object
320   Handle(GEOM_Object) anEdge = GetEngine()->AddObject(GEOM_EDGE);
321
322   //Add a new Vector function
323   Handle(GEOM_Function) aFunction =
324     anEdge->AddFunction(GEOMImpl_ShapeDriver::GetID(), EDGE_WIRE);
325
326   //Check if the function is set correctly
327   if (aFunction->GetDriverGUID() != GEOMImpl_ShapeDriver::GetID()) return NULL;
328
329   GEOMImpl_IShapes aCI (aFunction);
330
331   Handle(GEOM_Function) aWire = theWire->GetLastFunction();
332
333   if (aWire.IsNull()) return NULL;
334
335   aCI.SetBase(aWire);
336   aCI.SetTolerance(theLinearTolerance);
337   aCI.SetAngularTolerance(theAngularTolerance);
338
339   //Compute the Edge value
340   try {
341     OCC_CATCH_SIGNALS;
342     if (!GetSolver()->ComputeFunction(aFunction)) {
343       SetErrorCode("Shape driver failed");
344       return NULL;
345     }
346   }
347   catch (Standard_Failure& aFail) {
348     SetErrorCode(aFail.GetMessageString());
349     return NULL;
350   }
351
352   const double DEF_LIN_TOL = Precision::Confusion();
353   const double DEF_ANG_TOL = Precision::Angular();
354   //Make a Python command
355   if ( theAngularTolerance == DEF_ANG_TOL ) {
356     if ( theLinearTolerance == DEF_LIN_TOL )
357       GEOM::TPythonDump(aFunction) << anEdge  << " = geompy.MakeEdgeWire("
358                                    << theWire << ")";
359     else
360       GEOM::TPythonDump(aFunction) << anEdge  << " = geompy.MakeEdgeWire("
361                                    << theWire << ", " << theLinearTolerance << ")";
362   }
363   else {
364     GEOM::TPythonDump(aFunction) << anEdge  << " = geompy.MakeEdgeWire("
365                                  << theWire << ", " << theLinearTolerance << ", "
366                                  << theAngularTolerance << ")";
367   }
368
369   SetErrorCode(OK);
370   return anEdge;
371 }
372
373 //=============================================================================
374 /*!
375  *  MakeWire
376  */
377 //=============================================================================
378 Handle(GEOM_Object) GEOMImpl_IShapesOperations::MakeWire
379                              (std::list<Handle(GEOM_Object)> theShapes,
380                               const Standard_Real            theTolerance)
381 {
382   SetErrorCode(KO);
383
384   //Add a new object
385   Handle(GEOM_Object) aWire = GetEngine()->AddObject(GEOM_WIRE);
386
387   //Add a new function
388   Handle(GEOM_Function) aFunction =
389     aWire->AddFunction(GEOMImpl_ShapeDriver::GetID(), WIRE_EDGES);
390   if (aFunction.IsNull()) return NULL;
391
392   //Check if the function is set correctly
393   if (aFunction->GetDriverGUID() != GEOMImpl_ShapeDriver::GetID()) return NULL;
394
395   GEOMImpl_IShapes aCI (aFunction);
396   aCI.SetTolerance(theTolerance);
397
398   Handle(TColStd_HSequenceOfTransient) aShapesSeq = new TColStd_HSequenceOfTransient;
399
400   // Shapes
401   std::list<Handle(GEOM_Object)>::iterator it = theShapes.begin();
402   for (; it != theShapes.end(); it++) {
403     Handle(GEOM_Function) aRefSh = (*it)->GetLastFunction();
404     if (aRefSh.IsNull()) {
405       SetErrorCode("NULL argument shape for the shape construction");
406       return NULL;
407     }
408     aShapesSeq->Append(aRefSh);
409   }
410   aCI.SetShapes(aShapesSeq);
411
412   //Compute the shape
413   try {
414     OCC_CATCH_SIGNALS;
415     if (!GetSolver()->ComputeFunction(aFunction)) {
416       SetErrorCode("Shape driver failed");
417       return NULL;
418     }
419   }
420   catch (Standard_Failure& aFail) {
421     SetErrorCode(aFail.GetMessageString());
422     return NULL;
423   }
424
425   //Make a Python command
426   GEOM::TPythonDump pd (aFunction);
427   pd << aWire << " = geompy.MakeWire([";
428
429   // Shapes
430   it = theShapes.begin();
431   if (it != theShapes.end()) {
432     pd << (*it++);
433     while (it != theShapes.end()) {
434       pd << ", " << (*it++);
435     }
436   }
437   pd << "], " << theTolerance << ")";
438
439   SetErrorCode(OK);
440   return aWire;
441 }
442
443 //=============================================================================
444 /*!
445  *  MakeFace
446  */
447 //=============================================================================
448 Handle(GEOM_Object) GEOMImpl_IShapesOperations::MakeFace (Handle(GEOM_Object) theWire,
449                                                           const bool isPlanarWanted)
450 {
451   SetErrorCode(KO);
452
453   if (theWire.IsNull()) return NULL;
454
455   //Add a new Face object
456   Handle(GEOM_Object) aFace = GetEngine()->AddObject(GEOM_FACE);
457
458   //Add a new Shape function for creation of a face from a wire
459   Handle(GEOM_Function) aFunction =
460     aFace->AddFunction(GEOMImpl_ShapeDriver::GetID(), FACE_WIRE);
461   if (aFunction.IsNull()) return NULL;
462
463   //Check if the function is set correctly
464   if (aFunction->GetDriverGUID() != GEOMImpl_ShapeDriver::GetID()) return NULL;
465
466   GEOMImpl_IShapes aCI (aFunction);
467
468   Handle(GEOM_Function) aRefWire = theWire->GetLastFunction();
469
470   if (aRefWire.IsNull()) return NULL;
471
472   aCI.SetBase(aRefWire);
473   aCI.SetIsPlanar(isPlanarWanted);
474
475   //Compute the Face value
476   Standard_Boolean isWarning = Standard_False;
477   try {
478     OCC_CATCH_SIGNALS;
479     if (!GetSolver()->ComputeFunction(aFunction)) {
480       SetErrorCode("Shape driver failed to compute a face");
481       return NULL;
482     }
483   }
484   catch (Standard_Failure& aFail) {
485     SetErrorCode(aFail.GetMessageString());
486     // to provide warning
487     if (!aFunction->GetValue().IsNull()) {
488       isWarning = Standard_True;
489     } else {
490       return NULL;
491     }
492   }
493
494   //Make a Python command
495   GEOM::TPythonDump(aFunction) << aFace << " = geompy.MakeFace("
496     << theWire << ", " << (int)isPlanarWanted << ")";
497
498   // to provide warning
499   if (!isWarning) SetErrorCode(OK);
500   return aFace;
501 }
502
503 //=============================================================================
504 /*!
505  *  MakeFaceWires
506  */
507 //=============================================================================
508 Handle(GEOM_Object) GEOMImpl_IShapesOperations::MakeFaceWires
509                              (std::list<Handle(GEOM_Object)> theShapes,
510                               const bool isPlanarWanted)
511 {
512   SetErrorCode(KO);
513
514   //Add a new object
515   Handle(GEOM_Object) aShape = GetEngine()->AddObject(GEOM_FACE);
516
517   //Add a new function
518   Handle(GEOM_Function) aFunction =
519     aShape->AddFunction(GEOMImpl_ShapeDriver::GetID(), FACE_WIRES);
520   if (aFunction.IsNull()) return NULL;
521
522   //Check if the function is set correctly
523   if (aFunction->GetDriverGUID() != GEOMImpl_ShapeDriver::GetID()) return NULL;
524
525   GEOMImpl_IShapes aCI (aFunction);
526
527   Handle(TColStd_HSequenceOfTransient) aShapesSeq = new TColStd_HSequenceOfTransient;
528
529   // Shapes
530   std::list<Handle(GEOM_Object)>::iterator it = theShapes.begin();
531   for (; it != theShapes.end(); it++) {
532     Handle(GEOM_Function) aRefSh = (*it)->GetLastFunction();
533     if (aRefSh.IsNull()) {
534       SetErrorCode("NULL argument shape for the face construction");
535       return NULL;
536     }
537     aShapesSeq->Append(aRefSh);
538   }
539   aCI.SetShapes(aShapesSeq);
540
541   aCI.SetIsPlanar(isPlanarWanted);
542
543   //Compute the shape
544   Standard_Boolean isWarning = Standard_False;
545   try {
546     OCC_CATCH_SIGNALS;
547     if (!GetSolver()->ComputeFunction(aFunction)) {
548       SetErrorCode("Shape driver failed");
549       return NULL;
550     }
551   }
552   catch (Standard_Failure& aFail) {
553     SetErrorCode(aFail.GetMessageString());
554     // to provide warning
555     if (!aFunction->GetValue().IsNull()) {
556       isWarning = Standard_True;
557     } else {
558       return NULL;
559     }
560   }
561
562   //Make a Python command
563   GEOM::TPythonDump pd (aFunction);
564   pd << aShape << " = geompy.MakeFaceWires([";
565
566   // Shapes
567   it = theShapes.begin();
568   if (it != theShapes.end()) {
569     pd << (*it++);
570     while (it != theShapes.end()) {
571       pd << ", " << (*it++);
572     }
573   }
574   pd << "], " << (int)isPlanarWanted << ")";
575
576   // to provide warning
577   if (!isWarning) SetErrorCode(OK);
578   return aShape;
579 }
580
581 //=============================================================================
582 /*!
583  *  MakeFaceFromSurface
584  */
585 //=============================================================================
586 Handle(GEOM_Object) GEOMImpl_IShapesOperations::MakeFaceFromSurface
587                                               (Handle(GEOM_Object) theFace,
588                                                Handle(GEOM_Object) theWire)
589 {
590   SetErrorCode(KO);
591
592   //Add a new object
593   Handle(GEOM_Object) aShape = GetEngine()->AddObject(GEOM_FACE);
594
595   //Add a new function
596   Handle(GEOM_Function) aFunction =
597     aShape->AddFunction(GEOMImpl_ShapeDriver::GetID(), FACE_FROM_SURFACE);
598
599   if (aFunction.IsNull()) {
600     return NULL;
601   }
602
603   //Check if the function is set correctly
604   if (aFunction->GetDriverGUID() != GEOMImpl_ShapeDriver::GetID()) {
605     return NULL;
606   }
607
608   GEOMImpl_IShapes aCI (aFunction);
609   Handle(TColStd_HSequenceOfTransient) aShapesSeq =
610     new TColStd_HSequenceOfTransient;
611   Handle(GEOM_Function) aRefFace = theFace->GetLastFunction();
612   Handle(GEOM_Function) aRefWire = theWire->GetLastFunction();
613
614   if (aRefFace.IsNull()) {
615     SetErrorCode("NULL argument face for the face construction");
616     return NULL;
617   }
618
619   if (aRefWire.IsNull()) {
620     SetErrorCode("NULL argument wire for the face construction");
621     return NULL;
622   }
623
624   aShapesSeq->Append(aRefFace);
625   aShapesSeq->Append(aRefWire);
626
627   aCI.SetShapes(aShapesSeq);
628
629   //Compute the face
630   try {
631     OCC_CATCH_SIGNALS;
632     if (!GetSolver()->ComputeFunction(aFunction)) {
633       SetErrorCode("Shape driver failed");
634       return NULL;
635     }
636   }
637   catch (Standard_Failure& aFail) {
638     SetErrorCode(aFail.GetMessageString());
639     return NULL;
640   }
641
642   //Make a Python command
643   GEOM::TPythonDump (aFunction) << aShape
644     << " = geompy.MakeFaceFromSurface(" << theFace << ", " << theWire << ")";
645
646   SetErrorCode(OK);
647
648   return aShape;
649 }
650
651 //=============================================================================
652 /*!
653  *  MakeFaceWithConstraints
654  */
655 //=============================================================================
656 Handle(GEOM_Object) GEOMImpl_IShapesOperations::MakeFaceWithConstraints
657                              (std::list<Handle(GEOM_Object)> theConstraints)
658 {
659   SetErrorCode(KO);
660
661   //Add a new object
662   Handle(GEOM_Object) aShape = GetEngine()->AddObject(GEOM_FILLING);
663
664   //Add a new function
665   Handle(GEOM_Function) aFunction =
666     aShape->AddFunction(GEOMImpl_FillingDriver::GetID(), FILLING_ON_CONSTRAINTS);
667   if (aFunction.IsNull()) return NULL;
668
669   //Check if the function is set correctly
670   if (aFunction->GetDriverGUID() != GEOMImpl_FillingDriver::GetID()) return NULL;
671
672   GEOMImpl_IFilling aCI (aFunction);
673   Handle(TColStd_HSequenceOfTransient) aConstraints = new TColStd_HSequenceOfTransient;
674
675   // Shapes
676   std::list<Handle(GEOM_Object)>::iterator it = theConstraints.begin();
677   while (it != theConstraints.end()) {
678     Handle(GEOM_Object) anObject = (*it);
679     if ( anObject.IsNull() || anObject->GetValue().ShapeType() != TopAbs_EDGE ) {
680       SetErrorCode("NULL argument edge for the face construction");
681       return NULL;
682     }
683     Handle(GEOM_Function) aRefSh = anObject->GetLastFunction();
684     aConstraints->Append(aRefSh);
685     it++;
686     if ( it != theConstraints.end() ) {
687       Handle(GEOM_Object) aFace = (*it);
688       if ( aFace.IsNull() ) {
689         // null constraint face - it is a valid case
690         it++;
691         continue;
692       }
693       if ( aFace->GetValue().ShapeType() != TopAbs_FACE )
694         // constraint face can be omitted - it is a valid case
695         continue;
696       // Keep the old error code as IsSubShapeBelongsTo changes it.
697       TCollection_AsciiString anOldCode = GetErrorCode();
698
699       if ( IsSubShapeBelongsTo( anObject, 0, aFace, 0 ) ) {
700         // valid constraint
701         SetErrorCode(anOldCode);
702         aRefSh = aFace->GetLastFunction();
703         aConstraints->Append(aRefSh);
704         it++;
705       }
706       else {
707         // bad constraint
708         SetErrorCode("Face is NULL or not connected to the Edge");
709         return NULL;
710       }
711     }
712   }
713   aCI.SetShapes( aConstraints );
714
715   //Compute the shape
716   Standard_Boolean isWarning = Standard_False;
717   try {
718     OCC_CATCH_SIGNALS;
719     if (!GetSolver()->ComputeFunction(aFunction)) {
720       SetErrorCode("Shape driver failed");
721       return NULL;
722     }
723   }
724   catch (Standard_Failure& aFail) {
725     SetErrorCode(aFail.GetMessageString());
726     // to provide warning
727     if (!aFunction->GetValue().IsNull()) {
728       isWarning = Standard_True;
729     } else {
730       return NULL;
731     }
732   }
733
734   //Make a Python command
735   GEOM::TPythonDump pd (aFunction);
736   pd << aShape << " = geompy.MakeFaceWithConstraints([";
737
738   // Constraints
739   it = theConstraints.begin();
740   if (it != theConstraints.end() ) {
741     pd << (*it++);
742     while (it != theConstraints.end()) {
743       Handle(GEOM_Object) anObject = (*it++);
744       if( !anObject.IsNull() )
745         pd << ", " << anObject;
746     }
747   }
748   pd << "])";
749
750   // to provide warning
751   if (!isWarning) SetErrorCode(OK);
752   return aShape;
753 }
754
755 //=============================================================================
756 /*!
757  *  MakeShell
758  */
759 //=============================================================================
760 Handle(GEOM_Object) GEOMImpl_IShapesOperations::MakeShell
761                              (std::list<Handle(GEOM_Object)> theShapes)
762 {
763   return MakeShape(theShapes, GEOM_SHELL, SHELL_FACES, "MakeShell");
764 }
765
766 //=============================================================================
767 /*!
768  *  MakeSolidShells
769  */
770 //=============================================================================
771 Handle(GEOM_Object) GEOMImpl_IShapesOperations::MakeSolidShells
772                              (std::list<Handle(GEOM_Object)> theShapes)
773 {
774   return MakeShape(theShapes, GEOM_SOLID, SOLID_SHELLS, "MakeSolid");
775 }
776
777 //=============================================================================
778 /*!
779  *  MakeCompound
780  */
781 //=============================================================================
782 Handle(GEOM_Object) GEOMImpl_IShapesOperations::MakeCompound
783                              (std::list<Handle(GEOM_Object)> theShapes)
784 {
785   return MakeShape(theShapes, GEOM_COMPOUND, COMPOUND_SHAPES, "MakeCompound");
786 }
787
788 //=============================================================================
789 /*!
790  *  MakeShape
791  */
792 //=============================================================================
793 Handle(GEOM_Object) GEOMImpl_IShapesOperations::MakeShape
794                              (std::list<Handle(GEOM_Object)> theShapes,
795                               const Standard_Integer         theObjectType,
796                               const Standard_Integer         theFunctionType,
797                               const TCollection_AsciiString& theMethodName)
798 {
799   SetErrorCode(KO);
800
801   //Add a new object
802   Handle(GEOM_Object) aShape = GetEngine()->AddObject(theObjectType);
803
804   //Add a new function
805   Handle(GEOM_Function) aFunction =
806     aShape->AddFunction(GEOMImpl_ShapeDriver::GetID(), theFunctionType);
807   if (aFunction.IsNull()) return NULL;
808
809   //Check if the function is set correctly
810   if (aFunction->GetDriverGUID() != GEOMImpl_ShapeDriver::GetID()) return NULL;
811
812   GEOMImpl_IShapes aCI (aFunction);
813
814   Handle(TColStd_HSequenceOfTransient) aShapesSeq = new TColStd_HSequenceOfTransient;
815
816   // Shapes
817   std::list<Handle(GEOM_Object)>::iterator it = theShapes.begin();
818   for (; it != theShapes.end(); it++) {
819     Handle(GEOM_Function) aRefSh = (*it)->GetLastFunction();
820     if (aRefSh.IsNull()) {
821       SetErrorCode("NULL argument shape for the shape construction");
822       return NULL;
823     }
824     aShapesSeq->Append(aRefSh);
825   }
826   aCI.SetShapes(aShapesSeq);
827
828   //Compute the shape
829   try {
830     OCC_CATCH_SIGNALS;
831     if (!GetSolver()->ComputeFunction(aFunction)) {
832       SetErrorCode("Shape driver failed");
833       return NULL;
834     }
835   }
836   catch (Standard_Failure& aFail) {
837     SetErrorCode(aFail.GetMessageString());
838     return NULL;
839   }
840
841   //Make a Python command
842   GEOM::TPythonDump pd (aFunction);
843   pd << aShape << " = geompy." << theMethodName.ToCString() << "([";
844
845   // Shapes
846   it = theShapes.begin();
847   if (it != theShapes.end()) {
848     pd << (*it++);
849     while (it != theShapes.end()) {
850       pd << ", " << (*it++);
851     }
852   }
853   pd << "])";
854
855   SetErrorCode(OK);
856   return aShape;
857 }
858
859 //=============================================================================
860 /*!
861  *  MakeSolidFromConnectedFaces
862  */
863 //=============================================================================
864 Handle(GEOM_Object) GEOMImpl_IShapesOperations::MakeSolidFromConnectedFaces
865                              (std::list<Handle(GEOM_Object)> theFacesOrShells,
866                               const Standard_Boolean isIntersect)
867 {
868   SetErrorCode(KO);
869
870   //Add a new object
871   Handle(GEOM_Object) aSolid = GetEngine()->AddObject(GEOM_SOLID);
872
873   //Add a new function
874   Handle(GEOM_Function) aFunction =
875     aSolid->AddFunction(GEOMImpl_ShapeDriver::GetID(), SOLID_FACES);
876   if (aFunction.IsNull()) return NULL;
877
878   //Check if the function is set correctly
879   if (aFunction->GetDriverGUID() != GEOMImpl_ShapeDriver::GetID()) return NULL;
880
881   GEOMImpl_IShapes aCI (aFunction);
882
883   Handle(TColStd_HSequenceOfTransient) aShapesSeq = new TColStd_HSequenceOfTransient;
884
885   // Shapes
886   std::list<Handle(GEOM_Object)>::iterator it = theFacesOrShells.begin();
887   for (; it != theFacesOrShells.end(); it++) {
888     Handle(GEOM_Function) aRefSh = (*it)->GetLastFunction();
889     if (aRefSh.IsNull()) {
890       SetErrorCode("NULL argument shape for the shape construction");
891       return NULL;
892     }
893     aShapesSeq->Append(aRefSh);
894   }
895   aCI.SetShapes(aShapesSeq);
896   aCI.SetIsIntersect(isIntersect);
897
898   //Compute the shape
899   try {
900     OCC_CATCH_SIGNALS;
901     if (!GetSolver()->ComputeFunction(aFunction)) {
902       SetErrorCode("Shape driver failed");
903       return NULL;
904     }
905   }
906   catch (Standard_Failure& aFail) {
907     SetErrorCode(aFail.GetMessageString());
908     return NULL;
909   }
910
911   //Make a Python command
912   GEOM::TPythonDump pd (aFunction);
913   pd << aSolid << " = geompy.MakeSolidFromConnectedFaces([";
914
915   // Shapes
916   it = theFacesOrShells.begin();
917   if (it != theFacesOrShells.end()) {
918     pd << (*it++);
919     while (it != theFacesOrShells.end()) {
920       pd << ", " << (*it++);
921     }
922   }
923   pd << "]," << (isIntersect ? "True" : "False") << ")";
924
925   SetErrorCode(OK);
926   return aSolid;
927 }
928
929 //=============================================================================
930 /*!
931  *  MakeGlueFaces
932  */
933 //=============================================================================
934 Handle(GEOM_Object)
935 GEOMImpl_IShapesOperations::MakeGlueFaces (std::list< Handle(GEOM_Object) >& theShapes,
936                                            const Standard_Real               theTolerance,
937                                            const Standard_Boolean            doKeepNonSolids)
938 {
939   SetErrorCode(KO);
940
941   Handle(TColStd_HSequenceOfTransient) objects = GEOM_Object::GetLastFunctions( theShapes );
942   if ( objects.IsNull() || objects->IsEmpty() ) {
943     SetErrorCode("NULL argument shape");
944     return NULL;
945   }
946
947   //Add a new Glued object
948   Handle(GEOM_Object) aGlued = GetEngine()->AddObject(GEOM_GLUED);
949
950   //Add a new Glue function
951   Handle(GEOM_Function) aFunction;
952   aFunction = aGlued->AddFunction(GEOMImpl_GlueDriver::GetID(), GLUE_FACES);
953   if (aFunction.IsNull()) return NULL;
954
955   //Check if the function is set correctly
956   if (aFunction->GetDriverGUID() != GEOMImpl_GlueDriver::GetID()) return NULL;
957
958   GEOMImpl_IGlue aCI (aFunction);
959
960   aCI.SetBase( objects );
961   aCI.SetTolerance(theTolerance);
962   aCI.SetKeepNonSolids(doKeepNonSolids);
963
964   //Compute the sub-shape value
965   Standard_Boolean isWarning = Standard_False;
966   try {
967     OCC_CATCH_SIGNALS;
968     if (!GetSolver()->ComputeFunction(aFunction)) {
969       SetErrorCode("Shape driver failed to glue faces");
970       return NULL;
971     }
972   }
973   catch (Standard_Failure& aFail) {
974     SetErrorCode(aFail.GetMessageString());
975     // to provide warning
976     if (!aFunction->GetValue().IsNull()) {
977       isWarning = Standard_True;
978     } else {
979       return NULL;
980     }
981   }
982
983   //Make a Python command
984   GEOM::TPythonDump(aFunction) << aGlued << " = geompy.MakeGlueFaces("
985     << theShapes << ", " << theTolerance << ")";
986
987   // to provide warning
988   if (!isWarning) SetErrorCode(OK);
989   return aGlued;
990 }
991
992 //=============================================================================
993 /*!
994  *  GetGlueFaces
995  */
996 //=============================================================================
997 /*
998 Handle(TColStd_HSequenceOfTransient) GEOMImpl_IShapesOperations::GetGlueFaces
999                                                 (Handle(GEOM_Object) theShape,
1000                                                  const Standard_Real theTolerance)
1001 {
1002   SetErrorCode(KO);
1003
1004   if (theShape.IsNull()) return NULL;
1005   TopoDS_Shape aShape = theShape->GetValue();
1006   if (aShape.IsNull()) return NULL;
1007
1008   Handle(TColStd_HSequenceOfTransient) aSeq = new TColStd_HSequenceOfTransient;
1009
1010   Standard_Integer iErr;
1011   TopoDS_Shape aS;
1012   GEOMAlgo_Gluer1 aGluer;
1013   GEOMAlgo_ListIteratorOfListOfCoupleOfShapes aItCS;
1014   GEOMAlgo_CoupleOfShapes aCS;
1015   GEOMAlgo_ListOfCoupleOfShapes aLCS;
1016
1017   //aGluer = new GEOMAlgo_Gluer1;
1018   aGluer.SetShape(aShape);
1019   aGluer.SetTolerance(theTolerance);
1020   aGluer.Perform();
1021   iErr = aGluer.ErrorStatus();
1022   if (iErr) return NULL;
1023
1024   TopTools_ListOfShape listShape;
1025   const GEOMAlgo_ListOfCoupleOfShapes& aLCSG = aGluer.GluedFaces();
1026   // Access to faces
1027   aItCS.Initialize(aLCSG);
1028   for (; aItCS.More(); aItCS.Next()) {
1029     const GEOMAlgo_CoupleOfShapes& aCSG = aItCS.Value();
1030     listShape.Append(aCSG.Shape1());
1031   }
1032
1033   TopTools_ListIteratorOfListOfShape itSub (listShape);
1034   TCollection_AsciiString anAsciiList, anEntry;
1035   TopTools_IndexedMapOfShape anIndices;
1036   TopExp::MapShapes(aShape, anIndices);
1037   Handle(TColStd_HArray1OfInteger) anArray;
1038   Handle(GEOM_Object) anObj;
1039   for (int index = 1; itSub.More(); itSub.Next(), ++index) {
1040     TopoDS_Shape aValue = itSub.Value();
1041     anArray = new TColStd_HArray1OfInteger(1,1);
1042     anArray->SetValue(1, anIndices.FindIndex(aValue));
1043     anObj = GetEngine()->AddSubShape(theShape, anArray);
1044     if (!anObj.IsNull()) {
1045       aSeq->Append(anObj);
1046
1047       // for python command
1048       TDF_Tool::Entry(anObj->GetEntry(), anEntry);
1049       anAsciiList += anEntry;
1050       anAsciiList += ",";
1051     }
1052   }
1053
1054   //Make a Python command
1055   if( anAsciiList.Length() > 0 ) {
1056     anAsciiList.Trunc(anAsciiList.Length() - 1);
1057     Handle(GEOM_Function) aFunction = theShape->GetLastFunction();
1058     GEOM::TPythonDump pd (aFunction, true);
1059     pd << "[" << anAsciiList.ToCString();
1060     pd << "] = geompy.GetGlueFaces(" << theShape << ", " << theTolerance << ")";
1061   }
1062
1063   SetErrorCode(OK);
1064
1065   return aSeq;
1066 }
1067 */
1068
1069 //=============================================================================
1070 /*!
1071  *  MakeGlueFacesByList
1072  */
1073 //=============================================================================
1074 Handle(GEOM_Object)
1075 GEOMImpl_IShapesOperations::MakeGlueFacesByList(std::list< Handle(GEOM_Object) >& theShapes,
1076                                                 const Standard_Real               theTolerance,
1077                                                 std::list<Handle(GEOM_Object)> &  theFaces,
1078                                                 const Standard_Boolean            doKeepNonSolids,
1079                                                 const Standard_Boolean            doGlueAllEdges)
1080 {
1081   SetErrorCode(KO);
1082
1083   Handle(TColStd_HSequenceOfTransient) objects = GEOM_Object::GetLastFunctions( theShapes );
1084   if ( objects.IsNull() || objects->IsEmpty() ) {
1085     SetErrorCode("NULL argument shape");
1086     return NULL;
1087   }
1088   Handle(TColStd_HSequenceOfTransient) aFaces = GEOM_Object::GetLastFunctions( theFaces );
1089   if ( aFaces.IsNull() ) {
1090     SetErrorCode("NULL argument shape for the shape construction");
1091     return NULL;
1092   }
1093
1094   //Add a new Glued object
1095   Handle(GEOM_Object) aGlued = GetEngine()->AddObject(GEOM_GLUED);
1096
1097   //Add a new Glue function
1098   Handle(GEOM_Function) aFunction;
1099   aFunction = aGlued->AddFunction(GEOMImpl_GlueDriver::GetID(), GLUE_FACES_BY_LIST);
1100   if (aFunction.IsNull()) return NULL;
1101
1102   //Check if the function is set correctly
1103   if (aFunction->GetDriverGUID() != GEOMImpl_GlueDriver::GetID()) return NULL;
1104
1105   GEOMImpl_IGlue aCI (aFunction);
1106
1107   aCI.SetBase( objects );
1108   aCI.SetTolerance(theTolerance);
1109   aCI.SetKeepNonSolids(doKeepNonSolids);
1110   aCI.SetGlueAllEdges(doGlueAllEdges);
1111   aCI.SetFaces(aFaces);
1112
1113   //Compute the sub-shape value
1114   Standard_Boolean isWarning = Standard_False;
1115   try {
1116     OCC_CATCH_SIGNALS;
1117     if (!GetSolver()->ComputeFunction(aFunction)) {
1118       SetErrorCode("Shape driver failed to glue faces");
1119       return NULL;
1120     }
1121   }
1122   catch (Standard_Failure& aFail) {
1123     SetErrorCode(aFail.GetMessageString());
1124     // to provide warning
1125     if (!aFunction->GetValue().IsNull()) {
1126       isWarning = Standard_True;
1127     } else {
1128       return NULL;
1129     }
1130   }
1131
1132   //Make a Python command
1133
1134   GEOM::TPythonDump pd(aFunction);
1135   pd << aGlued << " = geompy.MakeGlueFacesByList("
1136      << theShapes << ", " << theTolerance << ", " << theFaces << ", "
1137      << (bool)doKeepNonSolids << ", " << (bool)doGlueAllEdges << ")";
1138
1139   // to provide warning
1140   if (!isWarning) SetErrorCode(OK);
1141   return aGlued;
1142 }
1143
1144 //=============================================================================
1145 /*!
1146  *  MakeGlueEdges
1147  */
1148 //=============================================================================
1149 Handle(GEOM_Object)
1150 GEOMImpl_IShapesOperations::MakeGlueEdges (std::list< Handle(GEOM_Object) >& theShapes,
1151                                            const Standard_Real               theTolerance)
1152 {
1153   SetErrorCode(KO);
1154
1155   Handle(TColStd_HSequenceOfTransient) objects = GEOM_Object::GetLastFunctions( theShapes );
1156   if ( objects.IsNull() || objects->IsEmpty() ) {
1157     SetErrorCode("NULL argument shape");
1158     return NULL;
1159   }
1160
1161   //Add a new Glued object
1162   Handle(GEOM_Object) aGlued = GetEngine()->AddObject(GEOM_GLUED);
1163
1164   //Add a new Glue function
1165   Handle(GEOM_Function) aFunction;
1166   aFunction = aGlued->AddFunction(GEOMImpl_GlueDriver::GetID(), GLUE_EDGES);
1167   if (aFunction.IsNull()) return NULL;
1168
1169   //Check if the function is set correctly
1170   if (aFunction->GetDriverGUID() != GEOMImpl_GlueDriver::GetID()) return NULL;
1171
1172   GEOMImpl_IGlue aCI (aFunction);
1173
1174   aCI.SetBase( objects );
1175   aCI.SetTolerance(theTolerance);
1176   aCI.SetKeepNonSolids(true);
1177
1178   //Compute the sub-shape value
1179   Standard_Boolean isWarning = Standard_False;
1180   try {
1181     OCC_CATCH_SIGNALS;
1182     if (!GetSolver()->ComputeFunction(aFunction)) {
1183       SetErrorCode("Shape driver failed to glue edges");
1184       return NULL;
1185     }
1186   }
1187   catch (Standard_Failure& aFail) {
1188     SetErrorCode(aFail.GetMessageString());
1189     // to provide warning
1190     if (!aFunction->GetValue().IsNull()) {
1191       isWarning = Standard_True;
1192     } else {
1193       return NULL;
1194     }
1195   }
1196
1197   //Make a Python command
1198   GEOM::TPythonDump(aFunction) << aGlued << " = geompy.MakeGlueEdges("
1199     << theShapes << ", " << theTolerance << ")";
1200
1201   // to provide warning
1202   if (!isWarning) SetErrorCode(OK);
1203   return aGlued;
1204 }
1205
1206 //=============================================================================
1207 /*!
1208  *  GetGlueShapes
1209  */
1210 //=============================================================================
1211 Handle(TColStd_HSequenceOfTransient)
1212 GEOMImpl_IShapesOperations::GetGlueShapes (std::list< Handle(GEOM_Object) >& theShapes,
1213                                            const Standard_Real               theTolerance,
1214                                            const TopAbs_ShapeEnum            theType)
1215 {
1216   SetErrorCode(KO);
1217
1218   TopoDS_Shape aShape;
1219   TopTools_SequenceOfShape shapes;
1220   std::list< Handle(GEOM_Object) >::iterator s = theShapes.begin();
1221   Handle(GEOM_BaseObject) lastCreatedGO;
1222   for ( ; s != theShapes.end(); ++s )
1223   {
1224     Handle(GEOM_Object) go = *s;
1225     if ( go.IsNull() ) return NULL;
1226     aShape = go->GetValue();
1227     if ( aShape.IsNull() ) return NULL;
1228     shapes.Append( aShape );
1229     lastCreatedGO = GEOM::GetCreatedLast( lastCreatedGO, go );
1230   }
1231   if ( shapes.Length() > 1 )
1232   {
1233     TopoDS_Compound compound;
1234     BRep_Builder builder;
1235     builder.MakeCompound( compound );
1236     for ( int i = 1; i <= shapes.Length(); ++i )
1237       builder.Add( compound, shapes( i ) );
1238
1239     aShape = compound;
1240   }
1241
1242   Handle(TColStd_HSequenceOfTransient) aSeq = new TColStd_HSequenceOfTransient;
1243
1244   GEOMAlgo_GlueDetector aGluer;
1245   aGluer.SetArgument(aShape);
1246   aGluer.SetTolerance(theTolerance);
1247   aGluer.Perform();
1248   Standard_Integer iErr = aGluer.ErrorStatus();
1249   if (iErr) return NULL;
1250
1251   std::vector< TopTools_IndexedMapOfShape* > anIndices( shapes.Length(), NULL );
1252   Handle(TColStd_HArray1OfInteger) anArray;
1253   Handle(GEOM_Object) anObj;
1254
1255   TopTools_ListOfShape listOnePerSet;
1256
1257   const TopTools_DataMapOfShapeListOfShape& aImages = aGluer.Images();
1258   TopTools_DataMapIteratorOfDataMapOfShapeListOfShape aItDMSLS (aImages);
1259   for (int index = 1; aItDMSLS.More(); aItDMSLS.Next(), ++index) {
1260     // some key shape
1261     //const TopoDS_Shape& aSkey = aItDMSLS.Key();
1262
1263     // list of shapes of the argument that can be glued
1264     const TopTools_ListOfShape& aLSD = aItDMSLS.Value();
1265
1266     //listShape.Append(aLSD.First());
1267     TopoDS_Shape aValue = aLSD.First();
1268
1269     if (aValue.ShapeType() == theType) {
1270       listOnePerSet.Append(aValue);
1271     }
1272   }
1273
1274   // for stable order of returned entities
1275   GEOMUtils::SortShapes(listOnePerSet, Standard_False);
1276
1277   TopTools_ListIteratorOfListOfShape aListIt (listOnePerSet);
1278   for (; aListIt.More(); aListIt.Next())
1279   {
1280     TopoDS_Shape aValue = aListIt.Value();
1281     // find a shape to add aValue as a sub-shape
1282     anObj.Nullify();
1283     s = theShapes.begin();
1284     for ( int i = 0; i < shapes.Length(); ++i, ++s )
1285     {
1286       Handle(GEOM_Object) object = *s;
1287       if ( !anIndices[i] ) {
1288         anIndices[i] = new TopTools_IndexedMapOfShape;
1289         TopExp::MapShapes( object->GetValue(), *anIndices[i]);
1290       }
1291       if (int index = anIndices[i]->FindIndex( aValue )) {
1292         anArray = new TColStd_HArray1OfInteger(1,1);
1293         anArray->SetValue(1, index);
1294         anObj = GetEngine()->AddSubShape( object, anArray);
1295         break;
1296       }
1297     }
1298     if (!anObj.IsNull())
1299       aSeq->Append(anObj);
1300   }
1301   for ( size_t i = 0 ; i < anIndices.size(); ++i )
1302     delete anIndices[i];
1303
1304   // Make a Python command
1305   if ( aSeq->Length() > 0)
1306   {
1307     Handle(GEOM_Function) aFunction = lastCreatedGO->GetLastFunction();
1308     GEOM::TPythonDump pd (aFunction, /*append=*/true);
1309     pd << aSeq
1310        << " = geompy." << (theType == TopAbs_FACE ? "GetGlueFaces" : "GetGlueEdges" )
1311        << "( " << theShapes << ", " << theTolerance << ")";
1312   }
1313
1314   SetErrorCode(OK);
1315
1316   return aSeq;
1317 }
1318
1319 //=============================================================================
1320 /*!
1321  *  MakeGlueEdgesByList
1322  */
1323 //=============================================================================
1324 Handle(GEOM_Object)
1325 GEOMImpl_IShapesOperations::MakeGlueEdgesByList (std::list< Handle(GEOM_Object) >& theShapes,
1326                                                  const Standard_Real               theTolerance,
1327                                                  std::list<Handle(GEOM_Object)>&   theEdges)
1328 {
1329   SetErrorCode(KO);
1330
1331   Handle(TColStd_HSequenceOfTransient) objects = GEOM_Object::GetLastFunctions( theShapes );
1332   if ( objects.IsNull() || objects->IsEmpty() ) {
1333     SetErrorCode("NULL argument shape");
1334     return NULL;
1335   }
1336   Handle(TColStd_HSequenceOfTransient) anEdges = GEOM_Object::GetLastFunctions( theEdges );
1337   if ( anEdges.IsNull() ) {
1338     SetErrorCode("NULL argument shape for the shape construction");
1339     return NULL;
1340   }
1341   //Add a new Glued object
1342   Handle(GEOM_Object) aGlued = GetEngine()->AddObject(GEOM_GLUED);
1343
1344   //Add a new Glue function
1345   Handle(GEOM_Function) aFunction;
1346   aFunction = aGlued->AddFunction(GEOMImpl_GlueDriver::GetID(), GLUE_EDGES_BY_LIST);
1347   if (aFunction.IsNull()) return NULL;
1348
1349   //Check if the function is set correctly
1350   if (aFunction->GetDriverGUID() != GEOMImpl_GlueDriver::GetID()) return NULL;
1351
1352   GEOMImpl_IGlue aCI (aFunction);
1353
1354   aCI.SetBase( objects );
1355   aCI.SetTolerance(theTolerance);
1356   aCI.SetKeepNonSolids(true);
1357   aCI.SetFaces(anEdges);
1358
1359   //Compute the sub-shape value
1360   Standard_Boolean isWarning = Standard_False;
1361   try {
1362     OCC_CATCH_SIGNALS;
1363     if (!GetSolver()->ComputeFunction(aFunction)) {
1364       SetErrorCode("Shape driver failed to glue edges");
1365       return NULL;
1366     }
1367   }
1368   catch (Standard_Failure& aFail) {
1369     SetErrorCode(aFail.GetMessageString());
1370     // to provide warning
1371     if (!aFunction->GetValue().IsNull()) {
1372       isWarning = Standard_True;
1373     } else {
1374       return NULL;
1375     }
1376   }
1377
1378   //Make a Python command
1379
1380   GEOM::TPythonDump pd (aFunction);
1381   pd << aGlued << " = geompy.MakeGlueEdgesByList("
1382      << theShapes << ", " << theTolerance << ", " << theEdges << " )";
1383
1384   // to provide warning
1385   if (!isWarning) SetErrorCode(OK);
1386   return aGlued;
1387 }
1388
1389 //=============================================================================
1390 /*!
1391  *  GetExistingSubObjects
1392  */
1393 //=============================================================================
1394 Handle(TColStd_HSequenceOfTransient)
1395 GEOMImpl_IShapesOperations::GetExistingSubObjects(Handle(GEOM_Object)    theShape,
1396                                                   const Standard_Boolean theGroupsOnly)
1397 {
1398   // note: this method does not return fields
1399
1400   Standard_Integer types = theGroupsOnly ? Groups : Groups|SubShapes;
1401   Handle(TColStd_HSequenceOfTransient) results = GetExistingSubObjects(theShape, types);
1402
1403   Handle(GEOM_BaseObject) lastCreatedGO = GEOM::GetCreatedLast(results);
1404   lastCreatedGO = GEOM::GetCreatedLast(lastCreatedGO, theShape);
1405
1406   if (results->Length() > 0) {
1407     // Make a Python command
1408     GEOM::TPythonDump pd (lastCreatedGO->GetLastFunction(), /*append=*/true);
1409     pd << "[";
1410     Standard_Integer i, aLen = results->Length();
1411     for (i = 1; i <= aLen; i++)
1412     {
1413       Handle(GEOM_BaseObject) obj = Handle(GEOM_BaseObject)::DownCast(results->Value(i));
1414       pd << obj << ((i < aLen) ? ", " : "");
1415     }
1416     pd << "] = geompy.GetExistingSubObjects(";
1417     pd << theShape << ", " << (bool)theGroupsOnly << ")";
1418   }
1419  
1420   return results;
1421 }
1422
1423 Handle(TColStd_HSequenceOfTransient)
1424 GEOMImpl_IShapesOperations::GetExistingSubObjects(Handle(GEOM_Object)    theShape,
1425                                                   const Standard_Integer theTypes)
1426 {
1427   SetErrorCode(KO);
1428
1429   if (theShape.IsNull()) return NULL;
1430
1431   Handle(GEOM_Function) aMainShape = theShape->GetLastFunction();
1432   if (aMainShape.IsNull()) return NULL;
1433
1434   Handle(TColStd_HSequenceOfTransient) aSeq = new TColStd_HSequenceOfTransient;
1435   SetErrorCode(NOT_FOUND_ANY);
1436
1437   if (!aMainShape->HasSubShapeReferences()) return aSeq;
1438   const TDataStd_ListOfExtendedString& aListEntries = aMainShape->GetSubShapeReferences();
1439   if (aListEntries.IsEmpty()) return aSeq;
1440
1441   SetErrorCode(KO);
1442
1443   TDataStd_ListIteratorOfListOfExtendedString anIt (aListEntries);
1444   for (; anIt.More(); anIt.Next()) {
1445     TCollection_ExtendedString anEntry = anIt.Value();
1446     Standard_Integer aStrLen = anEntry.LengthOfCString();
1447     char* anEntryStr = new char[aStrLen+1];
1448     anEntry.ToUTF8CString(anEntryStr);
1449     Handle(GEOM_BaseObject) anObj = GetEngine()->GetObject(anEntryStr, false);
1450     if (!anObj.IsNull() ) {
1451       bool isGroup    = anObj->IsKind(STANDARD_TYPE(GEOM_Object)) && anObj->GetType() == GEOM_GROUP;
1452       bool isSubShape = anObj->IsKind(STANDARD_TYPE(GEOM_Object)) && anObj->GetType() != GEOM_GROUP;
1453       bool isField    = anObj->IsKind(STANDARD_TYPE(GEOM_Field));
1454       if ((theTypes & Groups    && isGroup    ) ||
1455           (theTypes & SubShapes && isSubShape ) ||
1456           (theTypes & Fields    && isField    ))
1457         aSeq->Append(anObj);
1458     }
1459     delete [] anEntryStr;
1460   }
1461
1462   if (aSeq->Length() == 0) {
1463     SetErrorCode(NOT_FOUND_ANY);
1464     return aSeq;
1465   }
1466
1467   SetErrorCode(OK);
1468
1469   return aSeq;
1470 }
1471
1472 //=============================================================================
1473 /*!
1474  *  MakeExplode
1475  */
1476 //=============================================================================
1477 Handle(TColStd_HSequenceOfTransient) GEOMImpl_IShapesOperations::MakeExplode
1478                                           (Handle(GEOM_Object)    theShape,
1479                                            const Standard_Integer theShapeType,
1480                                            const Standard_Boolean isSorted,
1481                                            const ExplodeType      theExplodeType)
1482 {
1483   SetErrorCode(KO);
1484
1485   if (theShape.IsNull()) return NULL;
1486   TopoDS_Shape aShape = theShape->GetValue();
1487   if (aShape.IsNull()) return NULL;
1488
1489   Handle(GEOM_Function) aMainShape = theShape->GetLastFunction();
1490
1491   Handle(TColStd_HSequenceOfTransient) aSeq = new TColStd_HSequenceOfTransient;
1492   Handle(GEOM_Object) anObj;
1493   TopTools_MapOfShape mapShape;
1494   TopTools_ListOfShape listShape;
1495
1496   if (aShape.ShapeType() == TopAbs_COMPOUND &&
1497       (theShapeType == TopAbs_SHAPE || theShapeType == TopAbs_FLAT || theShapeType == TopAbs_COMPOUND))
1498   {
1499     TopoDS_Iterator It (aShape, Standard_True, Standard_True);
1500     for (; It.More(); It.Next()) {
1501       TopoDS_Shape SS = It.Value();
1502       if (mapShape.Add(SS)) {
1503         if (theShapeType == TopAbs_FLAT) {
1504           AddFlatSubShapes(SS, listShape, mapShape);
1505         }
1506         else if (theShapeType == TopAbs_SHAPE || theShapeType == SS.ShapeType()) {
1507           listShape.Append(SS);
1508         }
1509         // VSR: for EXPLODE_NEW_INCLUDE_MAIN and EXPLODE_OLD_INCLUDE_MAIN:
1510         // it seems it is necessary to add top-level shape if theShapeType == TopAbs_COMPOUND
1511       }
1512     }
1513   }
1514   else if (theExplodeType != EXPLODE_NEW_EXCLUDE_MAIN || aShape.ShapeType() != theShapeType) // issue 0021079
1515   {
1516     TopExp_Explorer exp (aShape, TopAbs_ShapeEnum(theShapeType));
1517     for (; exp.More(); exp.Next())
1518       if (mapShape.Add(exp.Current()))
1519         listShape.Append(exp.Current());
1520   }
1521
1522   if (listShape.IsEmpty()){
1523     //SetErrorCode("The given shape has no sub-shapes of the requested type");
1524     SetErrorCode(NOT_FOUND_ANY); // NPAL18017
1525     return aSeq;
1526   }
1527
1528   if (isSorted) {
1529     bool isOldSorting = false;
1530     if (theExplodeType == EXPLODE_OLD_INCLUDE_MAIN)
1531       isOldSorting = true;
1532     GEOMUtils::SortShapes(listShape, isOldSorting);
1533   }
1534
1535   TopTools_IndexedMapOfShape anIndices;
1536   TopExp::MapShapes(aShape, anIndices);
1537   Handle(TColStd_HArray1OfInteger) anArray;
1538
1539   TopTools_ListIteratorOfListOfShape itSub (listShape);
1540   TCollection_AsciiString anAsciiList, anEntry;
1541   for (int index = 1; itSub.More(); itSub.Next(), ++index)
1542   {
1543     TopoDS_Shape aValue = itSub.Value();
1544     anArray = new TColStd_HArray1OfInteger(1,1);
1545     anArray->SetValue(1, anIndices.FindIndex(aValue));
1546
1547     //anObj = GetEngine()->AddSubShape(theShape, anArray);
1548     {
1549       anObj = GetEngine()->AddObject(GEOM_SUBSHAPE);
1550       Handle(GEOM_Function) aFunction = anObj->AddFunction(GEOM_Object::GetSubShapeID(), 1);
1551       if (aFunction.IsNull()) return aSeq;
1552
1553       GEOM_ISubShape aSSI (aFunction);
1554       aSSI.SetMainShape(aMainShape);
1555       aSSI.SetIndices(anArray);
1556
1557       // Set function value directly, as we know it.
1558       // Usage of Solver here would lead to significant loss of time,
1559       // because GEOM_SubShapeDriver will build TopTools_IndexedMapOfShape
1560       // on the main shape for each being calculated sub-shape separately.
1561       aFunction->SetValue(aValue);
1562
1563       // Put this subshape in the list of sub-shapes of theMainShape
1564       aMainShape->AddSubShapeReference(aFunction);
1565     }
1566     if (!anObj.IsNull()) {
1567           aSeq->Append(anObj);
1568
1569           // for python command
1570           TDF_Tool::Entry(anObj->GetEntry(), anEntry);
1571           anAsciiList += anEntry;
1572           anAsciiList += ",";
1573         }
1574       }
1575
1576   //Make a Python command
1577   anAsciiList.Trunc(anAsciiList.Length() - 1);
1578
1579   GEOM::TPythonDump pd (aMainShape, /*append=*/true);
1580   pd << "[" << anAsciiList.ToCString() << "] = geompy.";
1581   switch (theExplodeType) {
1582   case EXPLODE_NEW_EXCLUDE_MAIN:
1583     pd << "ExtractShapes(" << theShape << ", "
1584        << TopAbs_ShapeEnum(theShapeType) << ", " << (isSorted ? "True" : "False") << ")";
1585     break;
1586   case EXPLODE_NEW_INCLUDE_MAIN:
1587     pd << "SubShapeAll" << (isSorted ? "SortedCentres(" : "(")
1588        << theShape << ", " << TopAbs_ShapeEnum(theShapeType) << ")";
1589     break;
1590   case EXPLODE_OLD_INCLUDE_MAIN:
1591     pd << "SubShapeAll" << (isSorted ? "Sorted(" : "(")
1592        << theShape << ", " << TopAbs_ShapeEnum(theShapeType) << ")";
1593     break;
1594   default: ;
1595   }
1596   SetErrorCode(OK);
1597
1598   return aSeq;
1599 }
1600
1601 //=============================================================================
1602 /*!
1603  *  SubShapeAllIDs
1604  */
1605 //=============================================================================
1606 Handle(TColStd_HSequenceOfInteger) GEOMImpl_IShapesOperations::SubShapeAllIDs
1607                                           (Handle(GEOM_Object)    theShape,
1608                                            const Standard_Integer theShapeType,
1609                                            const Standard_Boolean isSorted,
1610                                            const ExplodeType      theExplodeType)
1611 {
1612   SetErrorCode(KO);
1613
1614   if (theShape.IsNull()) return NULL;
1615   TopoDS_Shape aShape = theShape->GetValue();
1616   if (aShape.IsNull()) return NULL;
1617
1618   Handle(TColStd_HSequenceOfInteger) aSeq = new TColStd_HSequenceOfInteger;
1619   TopTools_MapOfShape mapShape;
1620   TopTools_ListOfShape listShape;
1621
1622   if (aShape.ShapeType() == TopAbs_COMPOUND &&
1623       (theShapeType == TopAbs_SHAPE || theShapeType == TopAbs_FLAT || theShapeType == TopAbs_COMPOUND))
1624   {
1625     TopoDS_Iterator It (aShape, Standard_True, Standard_True);
1626     for (; It.More(); It.Next()) {
1627       TopoDS_Shape SS = It.Value();
1628       if (mapShape.Add(SS)) {
1629         if (theShapeType == TopAbs_FLAT) {
1630           AddFlatSubShapes(SS, listShape, mapShape);
1631         }
1632         else if (theShapeType == TopAbs_SHAPE || theShapeType == SS.ShapeType()) {
1633           listShape.Append(SS);
1634         }
1635       }
1636     }
1637   }
1638   else if (theExplodeType != EXPLODE_NEW_EXCLUDE_MAIN || aShape.ShapeType() != theShapeType) // issue 0021079
1639   {
1640     TopExp_Explorer exp (aShape, TopAbs_ShapeEnum(theShapeType));
1641     for (; exp.More(); exp.Next())
1642       if (mapShape.Add(exp.Current()))
1643         listShape.Append(exp.Current());
1644   }
1645
1646   if (listShape.IsEmpty()) {
1647     //SetErrorCode("The given shape has no sub-shapes of the requested type");
1648     SetErrorCode(NOT_FOUND_ANY); // NPAL18017
1649     return aSeq;
1650   }
1651
1652   if (isSorted) {
1653     bool isOldSorting = false;
1654     if (theExplodeType == EXPLODE_OLD_INCLUDE_MAIN)
1655       isOldSorting = true;
1656     GEOMUtils::SortShapes(listShape, isOldSorting);
1657   }
1658
1659   TopTools_IndexedMapOfShape anIndices;
1660   TopExp::MapShapes(aShape, anIndices);
1661   Handle(TColStd_HArray1OfInteger) anArray;
1662
1663   TopTools_ListIteratorOfListOfShape itSub (listShape);
1664   for (int index = 1; itSub.More(); itSub.Next(), ++index) {
1665     TopoDS_Shape aValue = itSub.Value();
1666     aSeq->Append(anIndices.FindIndex(aValue));
1667   }
1668
1669   Handle(GEOM_Function) aFunction = theShape->GetLastFunction();
1670
1671   // VSR 29/08/2017: 0023327, 0023428: eliminate unnecessary lines in Python dump
1672 #ifdef DUMP_SUBSHAPE_IDS
1673   //Make a Python command
1674   GEOM::TPythonDump pd (aFunction, /*append=*/true);
1675   pd << "listSubShapeIDs = geompy.SubShapeAll";
1676   switch (theExplodeType) {
1677   case EXPLODE_NEW_EXCLUDE_MAIN:
1678     break;
1679   case EXPLODE_NEW_INCLUDE_MAIN:
1680     pd << (isSorted ? "SortedCentresIDs(" : "IDs(")
1681        << theShape << ", " << TopAbs_ShapeEnum(theShapeType) << ")";
1682     break;
1683   case EXPLODE_OLD_INCLUDE_MAIN:
1684     pd << (isSorted ? "SortedIDs(" : "IDs(")
1685        << theShape << ", " << TopAbs_ShapeEnum(theShapeType) << ")";
1686     break;
1687   default: ;
1688   }
1689 #endif // DUMP_SUBSHAPE_IDS
1690
1691   SetErrorCode(OK);
1692   return aSeq;
1693 }
1694
1695 //=============================================================================
1696 /*!
1697  *  GetSubShape
1698  */
1699 //=============================================================================
1700 Handle(GEOM_Object) GEOMImpl_IShapesOperations::GetSubShape
1701                                           (Handle(GEOM_Object)    theMainShape,
1702                                            const Standard_Integer theID)
1703 {
1704   SetErrorCode(KO);
1705
1706   if (theMainShape.IsNull()) return NULL;
1707
1708   Handle(TColStd_HArray1OfInteger) anArray = new TColStd_HArray1OfInteger(1,1);
1709   anArray->SetValue(1, theID);
1710   Handle(GEOM_Object) anObj = GetEngine()->AddSubShape(theMainShape, anArray,true);
1711   if (anObj.IsNull()) {
1712     SetErrorCode("Can not get a sub-shape with the given ID");
1713     return NULL;
1714   }
1715
1716   Handle(GEOM_Function) aFunction = anObj->GetLastFunction();
1717
1718   //Make a Python command
1719   GEOM::TPythonDump(aFunction) << anObj << " = geompy.GetSubShape("
1720                                << theMainShape << ", [" << theID << "])";
1721
1722   SetErrorCode(OK);
1723   return anObj;
1724 }
1725
1726 //=============================================================================
1727 /*!
1728  *  MakeSubShapes
1729  */
1730 //=============================================================================
1731 Handle(TColStd_HSequenceOfTransient) GEOMImpl_IShapesOperations::MakeSubShapes
1732                                 (Handle(GEOM_Object)              theMainShape,
1733                                  Handle(TColStd_HArray1OfInteger) theIndices)
1734 {
1735   SetErrorCode(KO);
1736
1737   Handle(TColStd_HSequenceOfTransient) aSeq = new TColStd_HSequenceOfTransient;
1738
1739   if (!theIndices->Length()) {
1740     SetErrorCode(NOT_FOUND_ANY);
1741     return aSeq;
1742   }
1743
1744   if (theMainShape.IsNull()) return NULL;
1745   TopoDS_Shape aShape = theMainShape->GetValue();
1746   if (aShape.IsNull()) return NULL;
1747
1748   Handle(GEOM_Function) aMainShape = theMainShape->GetLastFunction();
1749
1750   TopTools_IndexedMapOfShape anIndices;
1751   TopExp::MapShapes(aShape, anIndices);
1752
1753   Handle(TColStd_HArray1OfInteger) anArray;
1754   Handle(GEOM_Object) anObj;
1755
1756   TCollection_AsciiString anAsciiList, anEntry;
1757   Standard_Integer i, low = theIndices->Lower(), up = theIndices->Upper();
1758   for (i = low; i <= up; i++) {
1759     int id = theIndices->Value(i);
1760     if (1 <= id && id <= anIndices.Extent()) {
1761       TopoDS_Shape aValue = anIndices.FindKey(id);
1762       anArray = new TColStd_HArray1OfInteger(1,1);
1763       anArray->SetValue(1, id);
1764
1765       anObj = GetEngine()->AddObject(GEOM_SUBSHAPE);
1766       if (!anObj.IsNull()) {
1767         Handle(GEOM_Function) aFunction = anObj->AddFunction(GEOM_Object::GetSubShapeID(), 1);
1768         if (aFunction.IsNull()) return aSeq;
1769
1770         GEOM_ISubShape aSSI (aFunction);
1771         aSSI.SetMainShape(aMainShape);
1772         aSSI.SetIndices(anArray);
1773
1774         // Set function value directly, as we know it.
1775         // Usage of Solver here would lead to significant loss of time,
1776         // because GEOM_SubShapeDriver will build TopTools_IndexedMapOfShape
1777         // on the main shape for each being calculated sub-shape separately.
1778         aFunction->SetValue(aValue);
1779
1780         // Put this sub-shape in the list of sub-shapes of theMainShape
1781         aMainShape->AddSubShapeReference(aFunction);
1782
1783         aSeq->Append(anObj);
1784
1785         // for python command
1786         TDF_Tool::Entry(anObj->GetEntry(), anEntry);
1787         anAsciiList += anEntry;
1788         anAsciiList += ",";
1789       }
1790     }
1791   }
1792
1793   //Make a Python command
1794   anAsciiList.Trunc(anAsciiList.Length() - 1);
1795
1796   GEOM::TPythonDump pd (aMainShape, /*append=*/true);
1797   pd << "[" << anAsciiList.ToCString() << "] = geompy.SubShapes("
1798      << theMainShape << ", [" ;
1799   for (i = low; i <= up - 1; i++) {
1800     pd << theIndices->Value(i) << ", ";
1801   }
1802   pd << theIndices->Value(up) << "])";
1803
1804   SetErrorCode(OK);
1805
1806   return aSeq;
1807 }
1808
1809 //=============================================================================
1810 /*!
1811  *  GetSubShapeIndex
1812  */
1813 //=============================================================================
1814 Standard_Integer GEOMImpl_IShapesOperations::GetSubShapeIndex (Handle(GEOM_Object) theMainShape,
1815                                                                Handle(GEOM_Object) theSubShape)
1816 {
1817   SetErrorCode(KO);
1818
1819   TopoDS_Shape aMainShape = theMainShape->GetValue();
1820   TopoDS_Shape aSubShape = theSubShape->GetValue();
1821
1822   if (aMainShape.IsNull() || aSubShape.IsNull()) return -1;
1823
1824   TopTools_IndexedMapOfShape anIndices;
1825   TopExp::MapShapes(aMainShape, anIndices);
1826 //   if (anIndices.Contains(aSubShape)) {
1827 //     SetErrorCode(OK);
1828 //     return anIndices.FindIndex(aSubShape);
1829 //   }
1830   int id = anIndices.FindIndex(aSubShape);
1831   if (id > 0)
1832   {
1833     SetErrorCode(OK);
1834     return id;
1835   }
1836   return -1;
1837 }
1838
1839
1840
1841 //=============================================================================
1842 /*!
1843  *  GetSubShapeIndices
1844  */
1845 //=============================================================================
1846 Handle(TColStd_HSequenceOfInteger) GEOMImpl_IShapesOperations::GetSubShapesIndices (Handle(GEOM_Object) theMainShape,
1847                                                                                     std::list<Handle(GEOM_Object)> theSubShapes)
1848 {
1849   MESSAGE("GEOMImpl_IShapesOperations::GetSubShapesIndices")
1850   SetErrorCode(KO);
1851   
1852   Handle(TColStd_HSequenceOfInteger) aSeq = new TColStd_HSequenceOfInteger;
1853   
1854   TopoDS_Shape aMainShape = theMainShape->GetValue();
1855   if (aMainShape.IsNull())
1856   {
1857     MESSAGE("NULL main shape")
1858     return NULL;
1859   }
1860   
1861   TopTools_IndexedMapOfShape anIndices;
1862   TopExp::MapShapes(aMainShape, anIndices);
1863   
1864   std::list<Handle(GEOM_Object)>::iterator it;
1865   for (it=theSubShapes.begin(); it != theSubShapes.end(); ++it)
1866   {
1867     TopoDS_Shape aSubShape = (*it)->GetValue(); 
1868     if (aSubShape.IsNull())
1869     {
1870       MESSAGE("NULL subshape")
1871       return NULL;
1872     }
1873     int id = anIndices.FindIndex(aSubShape);
1874     aSeq->Append(id);
1875   }  
1876   
1877   SetErrorCode(OK);
1878   return aSeq;
1879 }
1880
1881
1882 //=============================================================================
1883 /*!
1884  *  GetTopologyIndex
1885  */
1886 //=============================================================================
1887 Standard_Integer GEOMImpl_IShapesOperations::GetTopologyIndex (Handle(GEOM_Object) theMainShape,
1888                                                                Handle(GEOM_Object) theSubShape)
1889 {
1890   SetErrorCode(OK);
1891
1892   TopoDS_Shape aMainShape = theMainShape->GetValue();
1893   TopoDS_Shape aSubShape = theSubShape->GetValue();
1894
1895   if (aMainShape.IsNull() || aSubShape.IsNull()) {
1896     SetErrorCode("Null argument shape given");
1897     return -1;
1898   }
1899
1900   int index = 1;
1901   if (aSubShape.ShapeType() == TopAbs_COMPOUND) {
1902     TopoDS_Iterator it;
1903     TopTools_ListOfShape CL;
1904     CL.Append(aMainShape);
1905     TopTools_ListIteratorOfListOfShape itC;
1906     for (itC.Initialize(CL); itC.More(); itC.Next()) {
1907       for (it.Initialize(itC.Value()); it.More(); it.Next()) {
1908         if (it.Value().ShapeType() == TopAbs_COMPOUND) {
1909           if (it.Value().IsSame(aSubShape))
1910             return index;
1911           else
1912             index++;
1913           CL.Append(it.Value());
1914         }
1915       }
1916     }
1917   } else {
1918     TopExp_Explorer anExp (aMainShape, aSubShape.ShapeType());
1919     TopTools_MapOfShape M;
1920     for (; anExp.More(); anExp.Next()) {
1921       if (M.Add(anExp.Current())) {
1922         if (anExp.Current().IsSame(aSubShape))
1923           return index;
1924         index++;
1925       }
1926     }
1927   }
1928
1929   SetErrorCode("The sub-shape does not belong to the main shape");
1930   return -1;
1931 }
1932
1933 //=============================================================================
1934 /*!
1935  *  GetShapeTypeString
1936  */
1937 //=============================================================================
1938 TCollection_AsciiString GEOMImpl_IShapesOperations::GetShapeTypeString (Handle(GEOM_Object) theShape)
1939 {
1940   SetErrorCode(KO);
1941
1942   TCollection_AsciiString aTypeName ("Null Shape");
1943
1944   TopoDS_Shape aShape = theShape->GetValue();
1945   if (aShape.IsNull())
1946     return aTypeName;
1947
1948   switch (aShape.ShapeType() )
1949   {
1950   case TopAbs_COMPOUND:
1951     aTypeName = "Compound";
1952     break;
1953   case  TopAbs_COMPSOLID:
1954     aTypeName = "Compound Solid";
1955     break;
1956   case TopAbs_SOLID:
1957     aTypeName = "Solid";
1958     break;
1959   case TopAbs_SHELL:
1960     aTypeName = "Shell";
1961     break;
1962   case TopAbs_FACE:
1963     {
1964       BRepAdaptor_Surface surf (TopoDS::Face(aShape));
1965       if (surf.GetType() == GeomAbs_Plane)
1966         aTypeName = "Plane";
1967       else if (surf.GetType() == GeomAbs_Cylinder)
1968         aTypeName = "Cylindrical Face";
1969       else if (surf.GetType() == GeomAbs_Sphere)
1970         aTypeName = "Spherical Face";
1971       else if (surf.GetType() == GeomAbs_Torus)
1972         aTypeName = "Toroidal Face";
1973       else if (surf.GetType() == GeomAbs_Cone)
1974         aTypeName = "Conical Face";
1975       else
1976         aTypeName = "GEOM::FACE";
1977     }
1978     break;
1979   case TopAbs_WIRE:
1980     aTypeName = "Wire";
1981     break;
1982   case TopAbs_EDGE:
1983     {
1984       BRepAdaptor_Curve curv (TopoDS::Edge(aShape));
1985       if (curv.GetType() == GeomAbs_Line) {
1986         if ((Abs(curv.FirstParameter()) >= 1E6) ||
1987             (Abs(curv.LastParameter()) >= 1E6))
1988           aTypeName = "Line";
1989         else
1990           aTypeName = "Edge";
1991       } else if (curv.GetType() == GeomAbs_Circle) {
1992         if (curv.IsClosed())
1993           aTypeName = "Circle";
1994         else
1995           aTypeName = "Arc";
1996       } else {
1997         aTypeName = "Edge";
1998       }
1999     }
2000     break;
2001   case TopAbs_VERTEX:
2002     aTypeName = "Vertex";
2003     break;
2004   case TopAbs_SHAPE:
2005     aTypeName = "Shape";
2006     break;
2007   default:
2008     aTypeName = "Shape of unknown type";
2009   }
2010
2011   return aTypeName;
2012 }
2013
2014 //=============================================================================
2015 /*!
2016  *  IsSubShapeBelongsTo
2017  */
2018 //=============================================================================
2019 Standard_Boolean GEOMImpl_IShapesOperations::IsSubShapeBelongsTo( Handle(GEOM_Object) theSubObject,
2020                                                                   const Standard_Integer theSubObjectIndex,
2021                                                                   Handle(GEOM_Object) theObject,
2022                                                                   const Standard_Integer theObjectIndex)
2023 {
2024   SetErrorCode(KO);
2025
2026   if ( theObject.IsNull() || theSubObject.IsNull() )
2027     return false;
2028
2029   TopoDS_Shape shape    = theObject->GetValue();
2030   TopoDS_Shape subShape = theSubObject->GetValue();
2031
2032   if ( shape.IsNull() || subShape.IsNull() )
2033     return false;
2034
2035   TopTools_IndexedMapOfShape anIndices;
2036   if ( theObjectIndex > 0 ) {
2037     TopExp::MapShapes( shape, anIndices );
2038     shape = anIndices.FindKey(theObjectIndex);
2039   }
2040   if ( theSubObjectIndex > 0 ) {
2041     TopExp::MapShapes( subShape, anIndices );
2042     subShape = anIndices.FindKey(theSubObjectIndex);
2043   }
2044
2045   TopExp::MapShapes( shape, anIndices );
2046
2047   const Standard_Boolean isBelongTo = anIndices.Contains(subShape);
2048
2049   SetErrorCode(OK);
2050
2051   return isBelongTo;
2052 }
2053
2054 //=============================================================================
2055 /*!
2056  *  NumberOfSubShapes
2057  */
2058 //=============================================================================
2059 Standard_Integer GEOMImpl_IShapesOperations::NumberOfSubShapes
2060                                           (Handle(GEOM_Object)    theShape,
2061                                            const Standard_Integer theShapeType)
2062 {
2063   SetErrorCode(KO);
2064   Standard_Integer nbShapes = 0;
2065
2066   if (theShape.IsNull()) return -1;
2067   TopoDS_Shape aShape = theShape->GetValue();
2068   if (aShape.IsNull()) return -1;
2069
2070   /*
2071   TopTools_MapOfShape mapShape;
2072
2073   if (aShape.ShapeType() == TopAbs_COMPOUND &&
2074       (TopAbs_ShapeEnum(theShapeType) == TopAbs_SHAPE ||
2075        TopAbs_ShapeEnum(theShapeType) == TopAbs_COMPSOLID ||
2076        TopAbs_ShapeEnum(theShapeType) == TopAbs_COMPOUND)) {
2077     TopoDS_Iterator It (aShape, Standard_True, Standard_True);
2078     for (; It.More(); It.Next()) {
2079       if (mapShape.Add(It.Value())) {
2080         if (TopAbs_ShapeEnum(theShapeType) == TopAbs_SHAPE ||
2081             TopAbs_ShapeEnum(theShapeType) == It.Value().ShapeType()) {
2082           nbShapes++;
2083         }
2084       }
2085     }
2086   } else {
2087     TopExp_Explorer exp (aShape, TopAbs_ShapeEnum(theShapeType));
2088     for (; exp.More(); exp.Next())
2089       if (mapShape.Add(exp.Current()))
2090         nbShapes++;
2091   }
2092   */
2093
2094   try {
2095     if (theShapeType == TopAbs_FLAT) {
2096       TopTools_MapOfShape aMapOfShape;
2097       TopTools_ListOfShape aListOfShape;
2098       AddFlatSubShapes(aShape, aListOfShape, aMapOfShape);
2099       nbShapes = aListOfShape.Extent();
2100     }
2101     else {
2102       OCC_CATCH_SIGNALS;
2103       int iType, nbTypes [TopAbs_SHAPE];
2104       for (iType = 0; iType < TopAbs_SHAPE; ++iType)
2105         nbTypes[iType] = 0;
2106       nbTypes[aShape.ShapeType()]++;
2107       
2108       TopTools_MapOfShape aMapOfShape;
2109       aMapOfShape.Add(aShape);
2110       TopTools_ListOfShape aListOfShape;
2111       aListOfShape.Append(aShape);
2112       
2113       TopTools_ListIteratorOfListOfShape itL (aListOfShape);
2114       for (; itL.More(); itL.Next()) {
2115         TopoDS_Iterator it (itL.Value());
2116         for (; it.More(); it.Next()) {
2117           TopoDS_Shape s = it.Value();
2118           if (aMapOfShape.Add(s)) {
2119             aListOfShape.Append(s);
2120             nbTypes[s.ShapeType()]++;
2121           }
2122         }
2123       }
2124       
2125       if (TopAbs_ShapeEnum(theShapeType) == TopAbs_SHAPE)
2126         nbShapes = aMapOfShape.Extent();
2127       else
2128         nbShapes = nbTypes[theShapeType];
2129     }
2130   }
2131   catch (Standard_Failure& aFail) {
2132     SetErrorCode(aFail.GetMessageString());
2133     return -1;
2134   }
2135
2136   SetErrorCode(OK);
2137   return nbShapes;
2138 }
2139
2140 //=============================================================================
2141 /*!
2142  *  ReverseShape
2143  */
2144 //=============================================================================
2145 Handle(GEOM_Object) GEOMImpl_IShapesOperations::ReverseShape(Handle(GEOM_Object) theShape)
2146 {
2147   SetErrorCode(KO);
2148
2149   if (theShape.IsNull()) return NULL;
2150
2151   /*
2152   //Add a new reversed object
2153   Handle(GEOM_Object) aReversed = GetEngine()->AddObject(theShape->GetType());
2154
2155   //Add a new Revese function
2156   Handle(GEOM_Function) aFunction;
2157   aFunction = aReversed->AddFunction(GEOMImpl_ShapeDriver::GetID(), REVERSE_ORIENTATION);
2158   if (aFunction.IsNull()) return NULL;
2159
2160   //Check if the function is set correctly
2161   if (aFunction->GetDriverGUID() != GEOMImpl_ShapeDriver::GetID()) return NULL;
2162
2163   GEOMImpl_IShapes aSI (aFunction);
2164
2165   Handle(GEOM_Function) aRefShape = theShape->GetLastFunction();
2166   if (aRefShape.IsNull()) return NULL;
2167
2168   aSI.SetBase(aRefShape);
2169
2170   //Compute the sub-shape value
2171   try {
2172     OCC_CATCH_SIGNALS;
2173     if (!GetSolver()->ComputeFunction(aFunction)) {
2174       SetErrorCode("Shape driver failed to reverse shape");
2175       return NULL;
2176     }
2177   }
2178   catch (Standard_Failure& aFail) {
2179     SetErrorCode(aFail.GetMessageString());
2180     return NULL;
2181   }
2182
2183   //Make a Python command
2184   GEOM::TPythonDump(aFunction) << aReversed
2185     << " = geompy.ChangeOrientation(" << theShape << ")";
2186
2187   SetErrorCode(OK);
2188   */
2189
2190   Handle(GEOM_Object) aReversed;
2191
2192   GEOM_Engine* anEngine = GetEngine();
2193   //GEOMImpl_Gen* aGen = dynamic_cast<GEOMImpl_Gen*>(anEngine);
2194   GEOMImpl_Gen* aGen = (GEOMImpl_Gen*)anEngine;
2195
2196   if (aGen) {
2197     GEOMImpl_IHealingOperations* anIHealingOperations =
2198       aGen->GetIHealingOperations();
2199     aReversed = anIHealingOperations->ChangeOrientationCopy(theShape);
2200     SetErrorCode(anIHealingOperations->GetErrorCode());
2201   }
2202
2203   return aReversed;
2204 }
2205
2206 //=============================================================================
2207 /*!
2208  *  GetFreeFacesIDs
2209  */
2210 //=============================================================================
2211 Handle(TColStd_HSequenceOfInteger) GEOMImpl_IShapesOperations::GetFreeFacesIDs
2212                                                  (Handle(GEOM_Object) theShape)
2213 {
2214   SetErrorCode(KO);
2215
2216   if (theShape.IsNull()) return NULL;
2217   TopoDS_Shape aShape = theShape->GetValue();
2218   if (aShape.IsNull()) return NULL;
2219
2220   Handle(TColStd_HSequenceOfInteger) aSeq = new TColStd_HSequenceOfInteger;
2221
2222   TopTools_IndexedDataMapOfShapeListOfShape mapFaceBlocks;
2223   GEOMImpl_Block6Explorer::MapShapesAndAncestors
2224     (aShape, TopAbs_FACE, TopAbs_SOLID, mapFaceBlocks);
2225
2226   Standard_Integer ind = 1, nbFaces = mapFaceBlocks.Extent();
2227
2228   if (nbFaces == 0) {
2229     SetErrorCode("The given shape has no faces");
2230     return aSeq;
2231   }
2232
2233   TopTools_IndexedMapOfShape anIndices;
2234   TopExp::MapShapes(aShape, anIndices);
2235
2236   Standard_Integer id;
2237   for (; ind <= nbFaces; ind++) {
2238     if (mapFaceBlocks.FindFromIndex(ind).Extent() != 2) {
2239       id = anIndices.FindIndex(mapFaceBlocks.FindKey(ind));
2240       aSeq->Append(id);
2241     }
2242   }
2243
2244   //The explode doesn't change object so no new function is required.
2245   Handle(GEOM_Function) aFunction = theShape->GetLastFunction();
2246
2247   // VSR 29/08/2017: 0023327, 0023428: eliminate unnecessary lines in Python dump
2248 #ifdef DUMP_SUBSHAPE_IDS
2249   //Make a Python command
2250   GEOM::TPythonDump(aFunction, /*append=*/true)
2251     << "listFreeFacesIDs = geompy.GetFreeFacesIDs(" << theShape << ")";
2252 #endif // DUMP_SUBSHAPE_IDS
2253
2254   SetErrorCode(OK);
2255   return aSeq;
2256 }
2257
2258 //=======================================================================
2259 //function : GetSharedShapes
2260 //purpose  :
2261 //=======================================================================
2262 Handle(TColStd_HSequenceOfTransient) GEOMImpl_IShapesOperations::GetSharedShapes
2263                                                 (Handle(GEOM_Object)    theShape1,
2264                                                  Handle(GEOM_Object)    theShape2,
2265                                                  const Standard_Integer theShapeType)
2266 {
2267   SetErrorCode(KO);
2268
2269   if (theShape1.IsNull() || theShape2.IsNull()) return NULL;
2270
2271   TopoDS_Shape aShape1 = theShape1->GetValue();
2272   TopoDS_Shape aShape2 = theShape2->GetValue();
2273
2274   if (aShape1.IsNull() || aShape2.IsNull()) return NULL;
2275
2276   TopTools_IndexedMapOfShape anIndices;
2277   TopExp::MapShapes(aShape1, anIndices);
2278   Handle(TColStd_HArray1OfInteger) anArray;
2279
2280   TopTools_IndexedMapOfShape mapShape1;
2281   TopExp::MapShapes(aShape1, TopAbs_ShapeEnum(theShapeType), mapShape1);
2282
2283   Handle(GEOM_Object) anObj;
2284   Handle(TColStd_HSequenceOfTransient) aSeq = new TColStd_HSequenceOfTransient;
2285   TCollection_AsciiString anAsciiList, anEntry;
2286
2287   TopTools_MapOfShape mapShape2;
2288   TopExp_Explorer exp (aShape2, TopAbs_ShapeEnum(theShapeType));
2289   for (; exp.More(); exp.Next()) {
2290     TopoDS_Shape aSS = exp.Current();
2291     if (mapShape2.Add(aSS) && mapShape1.Contains(aSS)) {
2292       anArray = new TColStd_HArray1OfInteger(1,1);
2293       anArray->SetValue(1, anIndices.FindIndex(aSS));
2294       anObj = GetEngine()->AddSubShape(theShape1, anArray);
2295       aSeq->Append(anObj);
2296
2297       // for python command
2298       TDF_Tool::Entry(anObj->GetEntry(), anEntry);
2299       anAsciiList += anEntry;
2300       anAsciiList += ",";
2301     }
2302   }
2303
2304   if (aSeq->IsEmpty()) {
2305     SetErrorCode(NOT_FOUND_ANY);
2306     return aSeq;
2307   }
2308
2309   //Make a Python command
2310   anAsciiList.Trunc(anAsciiList.Length() - 1);
2311
2312   Handle(GEOM_Function) aFunction = anObj->GetLastFunction();
2313
2314   GEOM::TPythonDump(aFunction) << "[" << anAsciiList.ToCString()
2315     << "] = geompy.GetSharedShapes(" << theShape1 << ", "
2316       << theShape2 << ", " << TopAbs_ShapeEnum(theShapeType) << ")";
2317
2318   SetErrorCode(OK);
2319   return aSeq;
2320 }
2321
2322 //=======================================================================
2323 //function : GetSharedShapes
2324 //purpose  :
2325 //
2326 // NOTE on the implementation
2327 // 
2328 // 1) Resulting sub-shapes are published as a children of the 1st input shape
2329 //    from theShapes list. Due to this reason only direct sub-shapes of the 1st
2330 //    shape can be contained in the result of the operation (i.e. shares between
2331 //    2nd/3rd, etc couples cannot be retrieved.
2332 // 2) An exception from above case is when a single compound is specified as an
2333 //    input. In this case we search shares between its top-level content, so we
2334 //    are able to search shares between all possible couples of shapes.
2335 // 3) Parameter theMultiShare controls what types of shares to search:
2336 //    - True: get sub-shapes that are shared between ALL input shapes;
2337 //    - False: get shares between couples of input sub-shapes (see points 1 and 2).
2338 //
2339 // Thus, we have the following cases:
2340 // [1] theShapes = N shapes (N>1), theMultiShare = True
2341 //     Result: sub-shapes that are shared by all theShapes
2342 // [2] theShapes = N shapes (N>1), theMultiShare = False
2343 //     Result: sub-shapes of 1st shape from theShapes that are shared with any shape
2344 //     from theShapes
2345 // [3] theShapes = 1 shape, theMultiShare = True
2346 //     Result: sub-shapes that are shared by all top-level sub-objects of theShapes[0]
2347 // [4] theShapes = 1 shape, theMultiShare = False
2348 //     Result: sub-shapes of all possible couples of all top-level sub-objects of
2349 //     theShapes[0].
2350 //=======================================================================
2351 Handle(TColStd_HSequenceOfTransient) GEOMImpl_IShapesOperations::GetSharedShapes
2352                                      (std::list<Handle(GEOM_Object)> & theShapes,
2353                                       const Standard_Integer           theShapeType,
2354                                       const bool                       theMultiShare)
2355 {
2356   SetErrorCode(KO);
2357
2358   int aLen = theShapes.size();
2359   if (aLen < 1) return NULL;
2360
2361   std::list<Handle(GEOM_Object)>::iterator it = theShapes.begin();
2362
2363   // main object is always first in the input list
2364   // it is the object from which sub-shapes indices are taken
2365   // and where results are published
2366   Handle(GEOM_Object) aMainObj = *it;
2367   Handle(GEOM_Function) aMainShape = aMainObj->GetLastFunction();
2368
2369   // collect all shapes from the input list (including first one) for processing
2370   TopTools_SequenceOfShape shapeSeq;
2371   for (; it != theShapes.end(); it++) {
2372     Handle(GEOM_Function) aRefShape = (*it)->GetLastFunction();
2373     if (aRefShape.IsNull()) {
2374       SetErrorCode("NULL shape for GetSharedShapes");
2375       return NULL;
2376     }
2377     TopoDS_Shape aShape = aRefShape->GetValue();
2378     if (aShape.IsNull()) {
2379       SetErrorCode("NULL shape for GetSharedShapes");
2380       return NULL;
2381     }
2382     shapeSeq.Append( aShape );
2383   }
2384
2385   // if only single shape is specified as input
2386   // collect all ites top-level sub-shapes for processing
2387   if ( shapeSeq.Length() == 1 )
2388   {
2389     TopoDS_Shape aShape = shapeSeq.First();
2390     shapeSeq.Clear();
2391     for ( TopoDS_Iterator it( aShape ); it.More(); it.Next() )
2392       shapeSeq.Append( it.Value() );
2393   }
2394
2395   // map all sub-shapes in a main shape to their indices
2396   TopTools_IndexedMapOfShape anIndices;
2397   TopExp::MapShapes(aMainShape->GetValue(), anIndices);
2398   TopTools_MapOfShape mapShape;
2399
2400   // find shared shapes
2401
2402   // here we will collect all shares
2403   TopTools_ListOfShape aShared;
2404
2405   // number of iterations
2406   int nbIters  =  theMultiShare || theShapes.size() > 1 ? 1 : shapeSeq.Length()-1;
2407   // numShares factor to search (i.e. by what nb of shapes each found sub-shape should be shared)
2408   int nbShares =  theMultiShare ? shapeSeq.Length()-1 : 1;
2409     
2410   for ( int iter = 1; iter <= nbIters; iter++) {
2411     for ( int ind = iter+1; ind <= shapeSeq.Length(); ind++) {
2412       if ( ind-1+nbShares > shapeSeq.Length() ) break;
2413       TopoDS_Compound aCurrSelection;
2414       TopoDS_Shape aShape1 = shapeSeq.Value( iter );
2415       TopTools_IndexedMapOfShape mapSelected;
2416       TopExp::MapShapes(aShape1, TopAbs_ShapeEnum(theShapeType), mapSelected);
2417       for ( int s = 0; s < nbShares; s++ ) {
2418         BRep_Builder B;
2419         TopoDS_Compound aCompound;
2420         B.MakeCompound(aCompound);
2421         const TopoDS_Shape& aShape2 = shapeSeq.Value( ind+s );
2422         TopTools_MapOfShape mapShape2;
2423         TopExp_Explorer exp (aShape2, TopAbs_ShapeEnum(theShapeType));
2424         for (; exp.More(); exp.Next()) {
2425           const TopoDS_Shape& aSS = exp.Current();
2426           if (mapShape2.Add(aSS) && mapSelected.Contains(aSS)) {
2427             B.Add(aCompound, aSS);
2428           }
2429         }
2430         mapSelected.Clear();
2431         aCurrSelection = aCompound;
2432         TopExp::MapShapes(aCurrSelection, TopAbs_ShapeEnum(theShapeType), mapSelected);
2433       }
2434       TopoDS_Iterator itSel(aCurrSelection, Standard_True, Standard_True);
2435       for (; itSel.More(); itSel.Next()) {
2436         const TopoDS_Shape& aSS = itSel.Value();
2437         if (mapShape.Add(aSS) )
2438           aShared.Append(aSS);
2439       }
2440     }
2441   }
2442
2443   Handle(TColStd_HSequenceOfTransient) aSeq = new TColStd_HSequenceOfTransient;
2444
2445   if (aShared.IsEmpty()){
2446     SetErrorCode(NOT_FOUND_ANY);
2447     return aSeq;
2448   }
2449
2450   // create GEOM_Object for each found shared shape (collected in aShared)
2451   TCollection_AsciiString anAsciiList;
2452   Handle(GEOM_Object) anObj;
2453   TopTools_ListIteratorOfListOfShape itSub (aShared);
2454   for (; itSub.More(); itSub.Next()) {
2455     TopoDS_Shape aValue = itSub.Value();
2456     Handle(TColStd_HArray1OfInteger) anArray = new TColStd_HArray1OfInteger(1,1);
2457     anArray->SetValue(1, anIndices.FindIndex(aValue));
2458     anObj = GetEngine()->AddSubShape(aMainObj, anArray);
2459     aSeq->Append(anObj);
2460
2461     // for python command
2462     TCollection_AsciiString anEntry;
2463     TDF_Tool::Entry(anObj->GetEntry(), anEntry);
2464     anAsciiList += anEntry;
2465     anAsciiList += ",";
2466   }
2467
2468   // make a Python command
2469   anAsciiList.Trunc(anAsciiList.Length() - 1);
2470
2471   GEOM::TPythonDump pd (anObj->GetLastFunction());
2472   pd << "[" << anAsciiList.ToCString()
2473      << "] = geompy.GetSharedShapesMulti(";
2474
2475   if ( aLen > 1 )
2476     pd << "[";
2477
2478   it = theShapes.begin();
2479   pd << (*it++);
2480   while (it != theShapes.end()) {
2481     pd << ", " << (*it++);
2482   }
2483   if ( aLen > 1 )
2484     pd << "]";
2485
2486   pd << ", " << TopAbs_ShapeEnum(theShapeType) << ", " << theMultiShare << ")";
2487
2488   SetErrorCode(OK);
2489   return aSeq;
2490 }
2491
2492 //=============================================================================
2493 /*!
2494  *
2495  */
2496 //=============================================================================
2497 static GEOM::TPythonDump& operator<< (GEOM::TPythonDump&   theDump,
2498                                       const GEOMAlgo_State theState)
2499 {
2500   switch (theState) {
2501   case GEOMAlgo_ST_IN:
2502     theDump << "GEOM.ST_IN";
2503     break;
2504   case GEOMAlgo_ST_OUT:
2505     theDump << "GEOM.ST_OUT";
2506     break;
2507   case GEOMAlgo_ST_ON:
2508     theDump << "GEOM.ST_ON";
2509     break;
2510   case GEOMAlgo_ST_ONIN:
2511     theDump << "GEOM.ST_ONIN";
2512     break;
2513   case GEOMAlgo_ST_ONOUT:
2514     theDump << "GEOM.ST_ONOUT";
2515     break;
2516   default:
2517     theDump << "GEOM.ST_UNKNOWN";
2518     break;
2519   }
2520   return theDump;
2521 }
2522
2523 //=======================================================================
2524 //function : checkTypeShapesOn
2525 /*!
2526  * \brief Checks if theShapeType parameter of GetShapesOnXXX() is OK
2527  * \param theShapeType - the shape type to check
2528  * \retval bool  - result of the check
2529  */
2530 //=======================================================================
2531 bool GEOMImpl_IShapesOperations::checkTypeShapesOn(const Standard_Integer theShapeType)
2532 {
2533   if (theShapeType != TopAbs_VERTEX &&
2534       theShapeType != TopAbs_EDGE &&
2535       theShapeType != TopAbs_FACE &&
2536       theShapeType != TopAbs_SOLID) {
2537     SetErrorCode("Only solids, vertices, edges or faces can be found by this method");
2538     return false;
2539   }
2540   return true;
2541 }
2542
2543 //=======================================================================
2544 //function : makePlane
2545   /*!
2546    * \brief Creates Geom_Plane
2547     * \param theAx1 - shape object defining plane parameters
2548     * \retval Handle(Geom_Surface) - resulting surface
2549    */
2550 //=======================================================================
2551 Handle(Geom_Surface) GEOMImpl_IShapesOperations::makePlane(const TopoDS_Shape& anAx1)
2552 {
2553   if (anAx1.ShapeType() != TopAbs_EDGE) return NULL;
2554   TopoDS_Edge anEdge = TopoDS::Edge(anAx1);
2555   TopoDS_Vertex V1, V2;
2556   TopExp::Vertices(anEdge, V1, V2, Standard_True);
2557   if (V1.IsNull() || V2.IsNull()) {
2558     SetErrorCode("Bad edge given for the plane normal vector");
2559     return NULL;
2560   }
2561   gp_Pnt aLoc = BRep_Tool::Pnt(V1);
2562   gp_Vec aVec (aLoc, BRep_Tool::Pnt(V2));
2563   if (aVec.Magnitude() < Precision::Confusion()) {
2564     SetErrorCode("Vector with null magnitude given");
2565     return NULL;
2566   }
2567   return new Geom_Plane(aLoc, aVec);
2568 }
2569
2570 //=======================================================================
2571 //function : makeCylinder
2572   /*!
2573    * \brief Creates Geom_CylindricalSurface
2574     * \param theAx1 - edge defining cylinder axis
2575     * \param theRadius - cylinder radius
2576     * \retval Handle(Geom_Surface) - resulting surface
2577    */
2578 //=======================================================================
2579 Handle(Geom_Surface) GEOMImpl_IShapesOperations::makeCylinder(const TopoDS_Shape& anAxis,
2580                                                               const Standard_Real theRadius)
2581 {
2582   //Axis of the cylinder
2583   if (anAxis.ShapeType() != TopAbs_EDGE) {
2584     SetErrorCode("Not an edge given for the axis");
2585     return NULL;
2586   }
2587   TopoDS_Edge anEdge = TopoDS::Edge(anAxis);
2588   TopoDS_Vertex V1, V2;
2589   TopExp::Vertices(anEdge, V1, V2, Standard_True);
2590   if (V1.IsNull() || V2.IsNull()) {
2591     SetErrorCode("Bad edge given for the axis");
2592     return NULL;
2593   }
2594   gp_Pnt aLoc = BRep_Tool::Pnt(V1);
2595   gp_Vec aVec (aLoc, BRep_Tool::Pnt(V2));
2596   if (aVec.Magnitude() < Precision::Confusion()) {
2597     SetErrorCode("Vector with null magnitude given");
2598     return NULL;
2599   }
2600
2601   gp_Ax3 anAx3 (aLoc, aVec);
2602   return new Geom_CylindricalSurface(anAx3, theRadius);
2603 }
2604
2605 //=======================================================================
2606 //function : getShapesOnBoxIDs
2607   /*!
2608    * \brief Find IDs of sub-shapes complying with given status about surface
2609     * \param theBox - the box to check state of sub-shapes against
2610     * \param theShape - the shape to explore
2611     * \param theShapeType - type of sub-shape of theShape
2612     * \param theState - required state
2613     * \retval Handle(TColStd_HSequenceOfInteger) - IDs of found sub-shapes
2614    */
2615 //=======================================================================
2616 Handle(TColStd_HSequenceOfInteger)
2617   GEOMImpl_IShapesOperations::getShapesOnBoxIDs(const Handle(GEOM_Object)& theBox,
2618                                                 const Handle(GEOM_Object)& theShape,
2619                                                 const Standard_Integer theShapeType,
2620                                                 GEOMAlgo_State theState)
2621 {
2622   Handle(TColStd_HSequenceOfInteger) aSeqOfIDs;
2623
2624   TopoDS_Shape aBox = theBox->GetValue();
2625   TopoDS_Shape aShape = theShape->GetValue();
2626
2627   // Check presence of triangulation, build if need
2628   if (theShapeType != TopAbs_VERTEX && !GEOMUtils::CheckTriangulation(aShape)) {
2629     SetErrorCode("Cannot build triangulation on the shape");
2630     return aSeqOfIDs;
2631   }
2632
2633   // Call algo
2634   GEOMAlgo_FinderShapeOn2 aFinder;
2635   Standard_Real aTol = 0.0001; // default value
2636
2637   Handle(GEOMAlgo_ClsfBox) aClsfBox = new GEOMAlgo_ClsfBox;
2638   aClsfBox->SetBox(aBox);
2639
2640   aFinder.SetShape(aShape);
2641   aFinder.SetTolerance(aTol);
2642   aFinder.SetClsf(aClsfBox);
2643   aFinder.SetShapeType( (TopAbs_ShapeEnum)theShapeType );
2644   aFinder.SetState(theState);
2645   aFinder.Perform();
2646
2647   // Interpret results
2648   Standard_Integer iErr = aFinder.ErrorStatus();
2649   // the detailed description of error codes is in GEOMAlgo_FinderShapeOn2.cxx
2650   if (iErr) {
2651     MESSAGE(" iErr : " << iErr);
2652     TCollection_AsciiString aMsg (" iErr : ");
2653     aMsg += TCollection_AsciiString(iErr);
2654     SetErrorCode(aMsg);
2655     return aSeqOfIDs;
2656   }
2657   Standard_Integer iWrn = aFinder.WarningStatus();
2658   // the detailed description of warning codes is in GEOMAlgo_FinderShapeOn2.cxx
2659   if (iWrn) {
2660     MESSAGE(" *** iWrn : " << iWrn);
2661   }
2662
2663   const TopTools_ListOfShape& listSS = aFinder.Shapes(); // the result
2664
2665   if (listSS.Extent() < 1) {
2666     //SetErrorCode("Not a single sub-shape of the requested type found on the given surface");
2667     SetErrorCode(NOT_FOUND_ANY); // NPAL18017
2668     return aSeqOfIDs;
2669   }
2670
2671   // Fill sequence of object IDs
2672   aSeqOfIDs = new TColStd_HSequenceOfInteger;
2673
2674   TopTools_IndexedMapOfShape anIndices;
2675   TopExp::MapShapes(aShape, anIndices);
2676
2677   TopTools_ListIteratorOfListOfShape itSub (listSS);
2678   for (int index = 1; itSub.More(); itSub.Next(), ++index) {
2679     int id = anIndices.FindIndex(itSub.Value());
2680     aSeqOfIDs->Append(id);
2681   }
2682
2683   return aSeqOfIDs;
2684 }
2685
2686 //=======================================================================
2687 //function : GetShapesOnBoxIDs
2688 /*!
2689    * \brief Find sub-shapes complying with given status about surface
2690     * \param theBox - the box to check state of sub-shapes against
2691     * \param theShape - the shape to explore
2692     * \param theShapeType - type of sub-shape of theShape
2693     * \param theState - required state
2694     * \retval Handle(TColStd_HSequenceOfInteger) - IDs of found sub-shapes
2695  */
2696 //=======================================================================
2697 Handle(TColStd_HSequenceOfInteger)
2698     GEOMImpl_IShapesOperations::GetShapesOnBoxIDs(const Handle(GEOM_Object)& theBox,
2699                                                   const Handle(GEOM_Object)& theShape,
2700                                                   const Standard_Integer theShapeType,
2701                                                   GEOMAlgo_State theState)
2702 {
2703   // Find sub-shapes ids
2704   Handle(TColStd_HSequenceOfInteger) aSeqOfIDs =
2705     getShapesOnBoxIDs (theBox, theShape, theShapeType, theState);
2706   if ( aSeqOfIDs.IsNull()  || aSeqOfIDs->Length() == 0 )
2707     return NULL;
2708
2709   // The GetShapesOnBox() doesn't change object so no new function is required.
2710   Handle(GEOM_Function) aFunction = GEOM::GetCreatedLast(theShape,theBox)->GetLastFunction();
2711
2712   // VSR 29/08/2017: 0023327, 0023428: eliminate unnecessary lines in Python dump
2713 #ifdef DUMP_SUBSHAPE_IDS
2714   // Make a Python command
2715   GEOM::TPythonDump(aFunction, /*append=*/true)
2716     << "listShapesOnBoxIDs = geompy.GetShapesOnBoxIDs("
2717     << theBox << ", "
2718     << theShape << ", "
2719     << TopAbs_ShapeEnum(theShapeType) << ", "
2720     << theState << ")";
2721 #endif // DUMP_SUBSHAPE_IDS
2722
2723   SetErrorCode(OK);
2724   return aSeqOfIDs;
2725 }
2726
2727 //=======================================================================
2728 //function : GetShapesOnBox
2729 /*!
2730    * \brief Find sub-shapes complying with given status about surface
2731     * \param theBox - the box to check state of sub-shapes against
2732     * \param theShape - the shape to explore
2733     * \param theShapeType - type of sub-shape of theShape
2734     * \param theState - required state
2735     * \retval Handle(TColStd_HSequenceOfTransient) - found sub-shapes
2736  */
2737 //=======================================================================
2738 Handle(TColStd_HSequenceOfTransient)
2739     GEOMImpl_IShapesOperations::GetShapesOnBox(const Handle(GEOM_Object)& theBox,
2740                                                const Handle(GEOM_Object)&  theShape,
2741                                                const Standard_Integer theShapeType,
2742                                                GEOMAlgo_State theState)
2743 {
2744   // Find sub-shapes ids
2745   Handle(TColStd_HSequenceOfInteger) aSeqOfIDs =
2746     getShapesOnBoxIDs (theBox, theShape, theShapeType, theState);
2747   if ( aSeqOfIDs.IsNull()  || aSeqOfIDs->Length() == 0 )
2748     return NULL;
2749
2750   // Find objects by indices
2751   TCollection_AsciiString anAsciiList;
2752   Handle(TColStd_HSequenceOfTransient) aSeq;
2753   aSeq = getObjectsShapesOn( theShape, aSeqOfIDs, anAsciiList );
2754   if ( aSeq.IsNull() || aSeq->IsEmpty() )
2755     return NULL;
2756
2757   // Make a Python command
2758
2759   Handle(GEOM_Object) anObj = Handle(GEOM_Object)::DownCast( aSeq->Value( 1 ));
2760   Handle(GEOM_Function) aFunction = anObj->GetLastFunction();
2761
2762   GEOM::TPythonDump(aFunction)
2763     << "[" << anAsciiList.ToCString() << "] = geompy.GetShapesOnBox("
2764     << theBox << ", "
2765     << theShape << ", "
2766     << TopAbs_ShapeEnum(theShapeType) << ", "
2767     << theState << ")";
2768
2769   SetErrorCode(OK);
2770   return aSeq;
2771 }
2772
2773 //=======================================================================
2774 //function : getShapesOnShapeIDs
2775 /*!
2776  * \brief Find IDs of sub-shapes complying with given status about surface
2777  * \param theCheckShape - the shape to check state of sub-shapes against
2778  * \param theShape - the shape to explore
2779  * \param theShapeType - type of sub-shape of theShape
2780  * \param theState - required state
2781  * \retval Handle(TColStd_HSequenceOfInteger) - IDs of found sub-shapes
2782  */
2783 //=======================================================================
2784 Handle(TColStd_HSequenceOfInteger)
2785   GEOMImpl_IShapesOperations::getShapesOnShapeIDs
2786                                  (const Handle(GEOM_Object)& theCheckShape,
2787                                   const Handle(GEOM_Object)& theShape,
2788                                   const Standard_Integer theShapeType,
2789                                   GEOMAlgo_State theState)
2790 {
2791   Handle(TColStd_HSequenceOfInteger) aSeqOfIDs;
2792
2793   TopoDS_Shape aCheckShape = theCheckShape->GetValue();
2794   TopoDS_Shape aShape = theShape->GetValue();
2795   TopTools_ListOfShape res;
2796
2797   // Check presence of triangulation, build if need
2798   if (theShapeType != TopAbs_VERTEX && !GEOMUtils::CheckTriangulation(aShape)) {
2799     SetErrorCode("Cannot build triangulation on the shape");
2800     return aSeqOfIDs;
2801   }
2802
2803   // Compute classification tolerance.
2804   TopTools_IndexedMapOfShape aMapVtx;
2805   Standard_Real              aTol = Precision::Confusion();
2806
2807   TopExp::MapShapes(aShape, TopAbs_VERTEX, aMapVtx);
2808
2809   Standard_Integer i;
2810   Standard_Integer aNbVtx = aMapVtx.Extent();
2811
2812   for (i = 1; i <= aNbVtx; ++i) {
2813     const TopoDS_Vertex aVtx    = TopoDS::Vertex(aMapVtx.FindKey(i));
2814     const Standard_Real aVtxTol = BRep_Tool::Tolerance(aVtx);
2815
2816     if (aTol < aVtxTol) {
2817       aTol = aVtxTol;
2818     }
2819   }
2820
2821   // Bound the tolerance value.
2822   if (aTol > 0.0001) {
2823     aTol = 0.0001;
2824   }
2825
2826   // Call algo
2827   GEOMAlgo_FinderShapeOn2 aFinder;
2828
2829   Handle(GEOMAlgo_ClsfSolid) aClsfSolid = new GEOMAlgo_ClsfSolid;
2830   aClsfSolid->SetShape(aCheckShape);
2831
2832   aFinder.SetShape(aShape);
2833   aFinder.SetTolerance(aTol);
2834   aFinder.SetClsf(aClsfSolid);
2835   aFinder.SetShapeType( (TopAbs_ShapeEnum)theShapeType );
2836   aFinder.SetState(theState);
2837   aFinder.Perform();
2838
2839   // Interpret results
2840   Standard_Integer iErr = aFinder.ErrorStatus();
2841   // the detailed description of error codes is in GEOMAlgo_FinderShapeOn2.cxx
2842   if (iErr) {
2843     if (iErr == 41) {
2844       SetErrorCode("theCheckShape must be a solid");
2845     }
2846     else {
2847       MESSAGE(" iErr : " << iErr);
2848       TCollection_AsciiString aMsg (" iErr : ");
2849       aMsg += TCollection_AsciiString(iErr);
2850       SetErrorCode(aMsg);
2851     }
2852     return aSeqOfIDs;
2853   }
2854   Standard_Integer iWrn = aFinder.WarningStatus();
2855   // the detailed description of warning codes is in GEOMAlgo_FinderShapeOn2.cxx
2856   if (iWrn) {
2857     MESSAGE(" *** iWrn : " << iWrn);
2858   }
2859
2860   const TopTools_ListOfShape& listSS = aFinder.Shapes(); // the result
2861
2862   if (listSS.Extent() < 1) {
2863     //SetErrorCode("Not a single sub-shape of the requested type found on the given surface");
2864     SetErrorCode(NOT_FOUND_ANY); // NPAL18017
2865   }
2866
2867   // Fill sequence of object IDs
2868   aSeqOfIDs = new TColStd_HSequenceOfInteger;
2869
2870   TopTools_IndexedMapOfShape anIndices;
2871   TopExp::MapShapes(aShape, anIndices);
2872
2873   TopTools_ListIteratorOfListOfShape itSub (listSS);
2874   for (int index = 1; itSub.More(); itSub.Next(), ++index) {
2875     int id = anIndices.FindIndex(itSub.Value());
2876     aSeqOfIDs->Append(id);
2877   }
2878
2879   return aSeqOfIDs;
2880 }
2881
2882 //=======================================================================
2883 //function : GetShapesOnShapeIDs
2884 /*!
2885  * \brief Find sub-shapes complying with given status about surface
2886  * \param theCheckShape - the shape to check state of sub-shapes against
2887  * \param theShape - the shape to explore
2888  * \param theShapeType - type of sub-shape of theShape
2889  * \param theState - required state
2890  * \retval Handle(TColStd_HSequenceOfInteger) - IDs of found sub-shapes
2891  */
2892 //=======================================================================
2893 Handle(TColStd_HSequenceOfInteger)
2894     GEOMImpl_IShapesOperations::GetShapesOnShapeIDs
2895                             (const Handle(GEOM_Object)& theCheckShape,
2896                              const Handle(GEOM_Object)& theShape,
2897                              const Standard_Integer theShapeType,
2898                              GEOMAlgo_State theState)
2899 {
2900   Handle(TColStd_HSequenceOfInteger) aSeqOfIDs =
2901     getShapesOnShapeIDs (theCheckShape, theShape, theShapeType, theState);
2902
2903   if ( aSeqOfIDs.IsNull()  || aSeqOfIDs->Length() == 0 )
2904     return NULL;
2905
2906   // The GetShapesOnShape() doesn't change object so no new function is required.
2907   Handle(GEOM_Function) aFunction =
2908     GEOM::GetCreatedLast(theShape,theCheckShape)->GetLastFunction();
2909
2910   // VSR 29/08/2017: 0023327, 0023428: eliminate unnecessary lines in Python dump
2911 #ifdef DUMP_SUBSHAPE_IDS
2912   // Make a Python command
2913   GEOM::TPythonDump(aFunction, /*append=*/true)
2914     << "listShapesOnShapeIDs = geompy.GetShapesOnShapeIDs("
2915     << theCheckShape << ", "
2916     << theShape << ", "
2917     << TopAbs_ShapeEnum(theShapeType) << ", "
2918     << theState << ")";
2919 #endif // DUMP_SUBSHAPE_IDS
2920
2921   SetErrorCode(OK);
2922   return aSeqOfIDs;
2923 }
2924
2925 //=======================================================================
2926 //function : GetShapesOnShape
2927 /*!
2928  * \brief Find sub-shapes complying with given status about surface
2929  * \param theCheckShape - the shape to check state of sub-shapes against
2930  * \param theShape - the shape to explore
2931  * \param theShapeType - type of sub-shape of theShape
2932  * \param theState - required state
2933  * \retval Handle(TColStd_HSequenceOfTransient) - found sub-shapes
2934  */
2935 //=======================================================================
2936 Handle(TColStd_HSequenceOfTransient)
2937   GEOMImpl_IShapesOperations::GetShapesOnShape
2938                              (const Handle(GEOM_Object)& theCheckShape,
2939                               const Handle(GEOM_Object)&  theShape,
2940                               const Standard_Integer theShapeType,
2941                               GEOMAlgo_State theState)
2942 {
2943   Handle(TColStd_HSequenceOfInteger) aSeqOfIDs =
2944     getShapesOnShapeIDs (theCheckShape, theShape, theShapeType, theState);
2945   if ( aSeqOfIDs.IsNull()  || aSeqOfIDs->Length() == 0 )
2946     return NULL;
2947
2948   // Find objects by indices
2949   TCollection_AsciiString anAsciiList;
2950   Handle(TColStd_HSequenceOfTransient) aSeq;
2951   aSeq = getObjectsShapesOn( theShape, aSeqOfIDs, anAsciiList );
2952
2953   if ( aSeq.IsNull() || aSeq->IsEmpty() )
2954     return NULL;
2955
2956   // Make a Python command
2957
2958   Handle(GEOM_Object) anObj = Handle(GEOM_Object)::DownCast( aSeq->Value( 1 ));
2959   Handle(GEOM_Function) aFunction = anObj->GetLastFunction();
2960
2961   GEOM::TPythonDump(aFunction)
2962     << "[" << anAsciiList.ToCString() << "] = geompy.GetShapesOnShape("
2963     << theCheckShape << ", "
2964     << theShape << ", "
2965     << TopAbs_ShapeEnum(theShapeType) << ", "
2966     << theState << ")";
2967
2968   SetErrorCode(OK);
2969   return aSeq;
2970 }
2971
2972 //=======================================================================
2973 //function : GetShapesOnShapeAsCompound
2974 //=======================================================================
2975 Handle(GEOM_Object) GEOMImpl_IShapesOperations::GetShapesOnShapeAsCompound
2976                              (const Handle(GEOM_Object)& theCheckShape,
2977                               const Handle(GEOM_Object)&  theShape,
2978                               const Standard_Integer theShapeType,
2979                               GEOMAlgo_State theState)
2980 {
2981   Handle(TColStd_HSequenceOfInteger) aSeqOfIDs =
2982     getShapesOnShapeIDs (theCheckShape, theShape, theShapeType, theState);
2983
2984   if ( aSeqOfIDs.IsNull()  || aSeqOfIDs->Length() == 0 )
2985     return NULL;
2986
2987   // Find objects by indices
2988   TCollection_AsciiString anAsciiList;
2989   Handle(TColStd_HSequenceOfTransient) aSeq;
2990   aSeq = getObjectsShapesOn( theShape, aSeqOfIDs, anAsciiList );
2991
2992   if ( aSeq.IsNull() || aSeq->IsEmpty() )
2993     return NULL;
2994
2995   TopoDS_Compound aCompound;
2996   BRep_Builder B;
2997   B.MakeCompound(aCompound);
2998   int i = 1;
2999   for(; i<=aSeq->Length(); i++) {
3000     Handle(GEOM_Object) anObj = Handle(GEOM_Object)::DownCast(aSeq->Value(i));
3001     TopoDS_Shape aShape_i = anObj->GetValue();
3002     B.Add(aCompound,aShape_i);
3003   }
3004
3005   //Add a new result object
3006   Handle(GEOM_Object) aRes = GetEngine()->AddObject(GEOM_SHAPES_ON_SHAPE);
3007   Handle(GEOM_Function) aFunction =
3008     aRes->AddFunction(GEOMImpl_ShapeDriver::GetID(), SHAPES_ON_SHAPE);
3009   aFunction->SetValue(aCompound);
3010
3011   aSeq->Clear();
3012   aSeq->Append( theCheckShape->GetLastFunction() );
3013   aSeq->Append( theShape->GetLastFunction() );
3014
3015   GEOMImpl_IShapes aCI( aFunction );
3016   aCI.SetShapes( aSeq );
3017   aCI.SetSubShapeType( theShapeType );
3018   aCI.SetTolerance( theState );
3019
3020   GEOM::TPythonDump(aFunction)
3021     << aRes << " = geompy.GetShapesOnShapeAsCompound("
3022     << theCheckShape << ", "
3023     << theShape << ", "
3024     << TopAbs_ShapeEnum(theShapeType) << ", "
3025     << theState << ")";
3026
3027   SetErrorCode(OK);
3028
3029   return aRes;
3030 }
3031
3032 //=============================================================================
3033 /*!
3034  *  GetSubShapeEdgeSorted
3035  */
3036 //=============================================================================
3037 Handle(TColStd_HSequenceOfTransient)
3038     GEOMImpl_IShapesOperations::GetSubShapeEdgeSorted
3039                           (const Handle(GEOM_Object) &theShape,
3040                            const Handle(GEOM_Object) &theStartPoint)
3041 {
3042   // Get the sorted edges indices.
3043   Handle(TColStd_HSequenceOfInteger) aSortedIDs =
3044     getSubShapeEdgeSortedIDs(theShape, theStartPoint);
3045
3046   // Get object by indices.
3047   TCollection_AsciiString              anAsciiList;
3048   Handle(TColStd_HSequenceOfTransient) aSeq =
3049     getObjectsShapesOn(theShape, aSortedIDs, anAsciiList);
3050
3051   if (aSeq.IsNull() || aSeq->IsEmpty()) {
3052     SetErrorCode("Empty sequence of edges");
3053     return NULL;
3054   }
3055
3056   // Make a Python command
3057   Handle(GEOM_Object)   anObj     =
3058     Handle(GEOM_Object)::DownCast(aSeq->Value(1));
3059   Handle(GEOM_Function) aFunction = anObj->GetLastFunction();
3060
3061   GEOM::TPythonDump(aFunction)
3062     << "[" << anAsciiList.ToCString() << "] = geompy.GetSubShapeEdgeSorted("
3063     << theShape << ", " << theStartPoint << ")";
3064
3065   SetErrorCode(OK);
3066
3067   return aSeq;
3068 }
3069
3070 //=============================================================================
3071 /*!
3072  *  GetSubShapesWithTolerance
3073  */
3074 //=============================================================================
3075 Handle(TColStd_HSequenceOfTransient)
3076     GEOMImpl_IShapesOperations::GetSubShapesWithTolerance
3077                      (const Handle(GEOM_Object)            &theShape,
3078                       const Standard_Integer                theShapeType,
3079                       const GEOMUtils::ComparisonCondition  theCondition,
3080                       const Standard_Real                   theTolerance)
3081 {
3082   if (theShape.IsNull()) {
3083     SetErrorCode("NULL GEOM object");
3084     return NULL;
3085   }
3086
3087   TopoDS_Shape aShape = theShape->GetValue();
3088
3089   if (aShape.IsNull()) {
3090     SetErrorCode("NULL Shape");
3091     return NULL;
3092   }
3093
3094   if (theShapeType != TopAbs_FACE && theShapeType != TopAbs_EDGE &&
3095       theShapeType != TopAbs_VERTEX && aShape.ShapeType() >= theShapeType) {
3096     SetErrorCode("Invalid shape type");
3097     return NULL;
3098   }
3099
3100   TopTools_IndexedMapOfShape         anIndices;
3101   TopTools_MapOfShape                aMapFence;
3102   TopExp_Explorer                    anExp(aShape,
3103                                            (TopAbs_ShapeEnum) theShapeType);
3104   Handle(TColStd_HSequenceOfInteger) anIDs = new TColStd_HSequenceOfInteger;
3105
3106   TopExp::MapShapes(aShape, anIndices);
3107
3108   for (; anExp.More(); anExp.Next()) {
3109     const TopoDS_Shape &aSubShape = anExp.Current();
3110
3111     if (aMapFence.Add(aSubShape)) {
3112       // Compute tolerance
3113       Standard_Real aTolerance = -1.;
3114
3115       switch (aSubShape.ShapeType()) {
3116         case TopAbs_FACE:
3117           aTolerance = BRep_Tool::Tolerance(TopoDS::Face(aSubShape));
3118           break;
3119         case TopAbs_EDGE:
3120           aTolerance = BRep_Tool::Tolerance(TopoDS::Edge(aSubShape));
3121           break;
3122         case TopAbs_VERTEX:
3123           aTolerance = BRep_Tool::Tolerance(TopoDS::Vertex(aSubShape));
3124           break;
3125         default:
3126           break;
3127       }
3128
3129       if (aTolerance < 0.) {
3130         continue;
3131       }
3132
3133       // Compare the tolerance with reference value.
3134       if (GEOMUtils::IsFitCondition (theCondition, aTolerance, theTolerance)) {
3135         anIDs->Append(anIndices.FindIndex(aSubShape));
3136       }
3137     }
3138   }
3139
3140   if (anIDs->IsEmpty()) {
3141     SetErrorCode("Empty sequence of sub-shapes");
3142     return NULL;
3143   }
3144
3145   // Get objects by indices.
3146   TCollection_AsciiString              anAsciiList;
3147   Handle(TColStd_HSequenceOfTransient) aSeq =
3148     getObjectsShapesOn(theShape, anIDs, anAsciiList);
3149
3150   if (aSeq.IsNull() || aSeq->IsEmpty()) {
3151     SetErrorCode("Empty sequence of edges");
3152     return NULL;
3153   }
3154
3155   // Make a Python command
3156   Handle(GEOM_Object)   anObj     =
3157     Handle(GEOM_Object)::DownCast(aSeq->Value(1));
3158   Handle(GEOM_Function) aFunction = anObj->GetLastFunction();
3159
3160   GEOM::TPythonDump(aFunction)
3161     << "[" << anAsciiList.ToCString() << "] = geompy.GetSubShapesWithTolerance("
3162     << theShape << ", " << theShapeType << ", " << theCondition << ", "
3163     << theTolerance << ")";
3164
3165   SetErrorCode(OK);
3166
3167   return aSeq;
3168 }
3169
3170 //=============================================================================
3171 /*!
3172  *  MakeExtraction
3173  */
3174 //=============================================================================
3175 Handle(GEOM_Object) GEOMImpl_IShapesOperations::MakeExtraction
3176                      (const Handle(GEOM_Object)              &theShape,
3177                       const Handle(TColStd_HArray1OfInteger) &theSubShapeIDs,
3178                       std::list<ExtractionStat>              &theStats)
3179 {
3180   SetErrorCode(KO);
3181
3182   if (theShape.IsNull()) {
3183     return NULL;
3184   }
3185
3186   //Add a new Result object
3187   Handle(GEOM_Object) aResult =
3188               GetEngine()->AddObject(GEOM_EXTRACTION);
3189
3190   //Add a new Extraction function
3191   Handle(GEOM_Function) aFunction =
3192     aResult->AddFunction(GEOMImpl_ShapeDriver::GetID(), EXTRACTION);
3193
3194   //Check if the function is set correctly
3195   if (aFunction->GetDriverGUID() != GEOMImpl_ShapeDriver::GetID()) {
3196     return NULL;
3197   }
3198
3199   Handle(GEOM_Function) aShape = theShape->GetLastFunction();
3200
3201   if (aShape.IsNull()) {
3202     return NULL;
3203   }
3204
3205   GEOMImpl_IExtract aCI (aFunction);
3206
3207   aCI.SetShape(aShape);
3208   aCI.SetSubShapeIDs(theSubShapeIDs);
3209
3210   //Compute the Edge value
3211   try {
3212     OCC_CATCH_SIGNALS;
3213     if (!GetSolver()->ComputeFunction(aFunction)) {
3214       SetErrorCode("Shape driver failed");
3215
3216       return NULL;
3217     }
3218   }
3219   catch (Standard_Failure& aFail) {
3220     SetErrorCode(aFail.GetMessageString());
3221
3222     return NULL;
3223   }
3224
3225   // Fill in statistics.
3226   theStats.clear();
3227
3228   Handle(TColStd_HArray1OfInteger) aStatIDsArray[3] = 
3229     { aCI.GetRemovedIDs(), aCI.GetModifiedIDs(), aCI.GetAddedIDs() };
3230   int                              i;
3231   int                              j;
3232
3233   for (j = 0; j < 3; ++j) {
3234     if (!aStatIDsArray[j].IsNull()) {
3235       const int      anUpperID = aStatIDsArray[j]->Upper();
3236       ExtractionStat aStat;
3237
3238       for (i = aStatIDsArray[j]->Lower(); i <= anUpperID; ++i) {
3239         aStat.indices.push_back(aStatIDsArray[j]->Value(i));
3240       }
3241
3242       aStat.type = (ExtractionStatType) j;
3243       theStats.push_back(aStat);
3244     }
3245   }
3246
3247   //Make a Python command
3248   GEOM::TPythonDump pd(aFunction);
3249
3250   pd << aResult  << " = geompy.MakeExtraction(" << theShape << ", [";
3251
3252   if (!theSubShapeIDs.IsNull()) {
3253     const int aNbIDs = theSubShapeIDs->Upper();
3254
3255     for (i = theSubShapeIDs->Lower(); i < aNbIDs; ++i) {
3256       pd << theSubShapeIDs->Value(i) << ", ";
3257     }
3258
3259     // Dump the last value without a comma.
3260     pd << theSubShapeIDs->Value(i);
3261   }
3262
3263   pd << "])";
3264
3265   SetErrorCode(OK);
3266
3267   return aResult;
3268 }
3269
3270 //=======================================================================
3271 //function : getShapesOnSurfaceIDs
3272   /*!
3273    * \brief Find IDs of sub-shapes complying with given status about surface
3274     * \param theSurface - the surface to check state of sub-shapes against
3275     * \param theShape - the shape to explore
3276     * \param theShapeType - type of sub-shape of theShape
3277     * \param theState - required state
3278     * \retval Handle(TColStd_HSequenceOfInteger) - IDs of found sub-shapes
3279    */
3280 //=======================================================================
3281 Handle(TColStd_HSequenceOfInteger)
3282   GEOMImpl_IShapesOperations::getShapesOnSurfaceIDs(const Handle(Geom_Surface)& theSurface,
3283                                                     const TopoDS_Shape&         theShape,
3284                                                     TopAbs_ShapeEnum            theShapeType,
3285                                                     GEOMAlgo_State              theState)
3286 {
3287   Handle(TColStd_HSequenceOfInteger) aSeqOfIDs;
3288
3289   // Check presence of triangulation, build if need
3290   if (theShapeType != TopAbs_VERTEX &&
3291       !GEOMUtils::CheckTriangulation(theShape)) {
3292     SetErrorCode("Cannot build triangulation on the shape");
3293     return aSeqOfIDs;
3294   }
3295
3296   // BEGIN: Mantis issue 0020961: Error on a pipe T-Shape
3297   // Compute tolerance
3298   Standard_Real T, VertMax = -RealLast();
3299   try {
3300     OCC_CATCH_SIGNALS;
3301     for (TopExp_Explorer ExV (theShape, TopAbs_VERTEX); ExV.More(); ExV.Next()) {
3302       TopoDS_Vertex Vertex = TopoDS::Vertex(ExV.Current());
3303       T = BRep_Tool::Tolerance(Vertex);
3304       if (T > VertMax)
3305         VertMax = T;
3306     }
3307   }
3308   catch (Standard_Failure& aFail) {
3309     SetErrorCode(aFail.GetMessageString());
3310     return aSeqOfIDs;
3311   }
3312   // END: Mantis issue 0020961
3313
3314   // Call algo
3315   GEOMAlgo_FinderShapeOn2   aFinder;
3316   Handle(GEOMAlgo_ClsfSurf) aClsfSurf = new GEOMAlgo_ClsfSurf;
3317   Standard_Real             aTol      = VertMax; // Mantis issue 0020961
3318
3319   aClsfSurf->SetSurface(theSurface);
3320   aFinder.SetShape(theShape);
3321   aFinder.SetTolerance(aTol);
3322   aFinder.SetClsf(aClsfSurf);
3323   aFinder.SetShapeType(theShapeType);
3324   aFinder.SetState(theState);
3325
3326   // Sets the minimal number of inner points for the faces that do not have own
3327   // inner points at all (for e.g. rectangular planar faces have just 2 triangles).
3328   // Default value=3
3329   aFinder.SetNbPntsMin(3);
3330   // Sets the maximal number of inner points for edges or faces.
3331   // It is useful for the cases when this number is very big (e.g =2000) to improve
3332   // the performance. If this value =0, all inner points will be taken into account.
3333   // Default value=0
3334   aFinder.SetNbPntsMax(100);
3335
3336   aFinder.Perform();
3337
3338   // Interpret results
3339   Standard_Integer iErr = aFinder.ErrorStatus();
3340   // the detailed description of error codes is in GEOMAlgo_FinderShapeOn2.cxx
3341   if (iErr) {
3342     MESSAGE(" iErr : " << iErr);
3343     TCollection_AsciiString aMsg (" iErr : ");
3344     aMsg += TCollection_AsciiString(iErr);
3345     SetErrorCode(aMsg);
3346     return aSeqOfIDs;
3347   }
3348   Standard_Integer iWrn = aFinder.WarningStatus();
3349   // the detailed description of warning codes is in GEOMAlgo_FinderShapeOn2.cxx
3350   if (iWrn) {
3351     MESSAGE(" *** iWrn : " << iWrn);
3352   }
3353
3354   const TopTools_ListOfShape& listSS = aFinder.Shapes(); // the result
3355
3356   if (listSS.Extent() < 1) {
3357     //SetErrorCode("Not a single sub-shape of the requested type found on the given surface");
3358     SetErrorCode(NOT_FOUND_ANY); // NPAL18017
3359     return aSeqOfIDs;
3360   }
3361
3362   // Fill sequence of object IDs
3363   aSeqOfIDs = new TColStd_HSequenceOfInteger;
3364
3365   TopTools_IndexedMapOfShape anIndices;
3366   TopExp::MapShapes(theShape, anIndices);
3367
3368   TopTools_ListIteratorOfListOfShape itSub (listSS);
3369   for (int index = 1; itSub.More(); itSub.Next(), ++index) {
3370     int id = anIndices.FindIndex(itSub.Value());
3371     aSeqOfIDs->Append(id);
3372   }
3373
3374   return aSeqOfIDs;
3375 }
3376
3377 //=======================================================================
3378 //function : getObjectsShapesOn
3379 /*!
3380  * \brief Find shape objects and their entries by their ids
3381  * \param theShapeIDs - incoming shape ids
3382  * \param theShapeEntries - outgoing entries like "entry1, entry2, ..."
3383  * \retval Handle(TColStd_HSequenceOfTransient) - found shape objects
3384  */
3385 //=======================================================================
3386 Handle(TColStd_HSequenceOfTransient) GEOMImpl_IShapesOperations::
3387  getObjectsShapesOn(const Handle(GEOM_Object)&                theShape,
3388                     const Handle(TColStd_HSequenceOfInteger)& theShapeIDs,
3389                     TCollection_AsciiString &                 theShapeEntries)
3390 {
3391   Handle(TColStd_HSequenceOfTransient) aSeq;
3392
3393   if ( !theShapeIDs.IsNull() && theShapeIDs->Length() > 0 )
3394   {
3395     aSeq = new TColStd_HSequenceOfTransient;
3396     Handle(TColStd_HArray1OfInteger) anArray = new TColStd_HArray1OfInteger(1,1);
3397     TCollection_AsciiString anEntry;
3398     for ( int i = 1; i <= theShapeIDs->Length(); ++i )
3399     {
3400       anArray->SetValue(1, theShapeIDs->Value( i ));
3401       Handle(GEOM_Object) anObj = GetEngine()->AddSubShape(theShape, anArray);
3402       aSeq->Append( anObj );
3403
3404       TDF_Tool::Entry(anObj->GetEntry(), anEntry);
3405       if ( i != 1 ) theShapeEntries += ",";
3406       theShapeEntries += anEntry;
3407     }
3408   }
3409   return aSeq;
3410 }
3411
3412 //=============================================================================
3413 /*!
3414  *  getSubShapeEdgeSortedIDs
3415  */
3416 //=============================================================================
3417 Handle(TColStd_HSequenceOfInteger)
3418     GEOMImpl_IShapesOperations::getSubShapeEdgeSortedIDs
3419                                (const Handle(GEOM_Object) &theShape,
3420                                 const Handle(GEOM_Object) &theStartPoint)
3421 {
3422   Handle(TColStd_HSequenceOfInteger) aResult;
3423
3424   if (theShape.IsNull() || theStartPoint.IsNull()) {
3425     SetErrorCode("NULL GEOM object");
3426     return aResult;
3427   }
3428
3429   const TopoDS_Shape aShape      = theShape->GetValue();
3430   const TopoDS_Shape aStartPoint = theStartPoint->GetValue();
3431
3432   if (aShape.IsNull() || aStartPoint.IsNull()) {
3433     SetErrorCode("NULL Shape");
3434     return aResult;
3435   }
3436
3437   if (aStartPoint.ShapeType() != TopAbs_VERTEX) {
3438     SetErrorCode("Starting point is not a vertex");
3439     return aResult;
3440   }
3441
3442   TopExp_Explorer      anExp(aShape, TopAbs_EDGE);
3443   TopTools_MapOfShape  aMapFence;
3444   TopTools_ListOfShape anEdges;
3445
3446   for (; anExp.More(); anExp.Next()) {
3447     const TopoDS_Shape &anEdge = anExp.Current();
3448
3449     if (aMapFence.Add(anEdge)) {
3450       anEdges.Append(anEdge);
3451     }
3452   }
3453
3454   if (anEdges.IsEmpty()) {
3455     SetErrorCode("Shape doesn't contain edges");
3456     return aResult;
3457   }
3458
3459   // Step 1: Sort edges
3460   GEOMUtils::SortShapes(anEdges, Standard_False);
3461
3462   TopTools_ListIteratorOfListOfShape anIter(anEdges);
3463   TopoDS_Vertex                      aV[2];
3464   TopTools_DataMapOfShapeListOfShape aMapVE;
3465
3466   // Step 2: Fill the map vertex - list of edges.
3467   for (; anIter.More(); anIter.Next()) {
3468     TopoDS_Edge anEdge = TopoDS::Edge(anIter.Value());
3469
3470     TopExp::Vertices(anEdge, aV[0], aV[1]);
3471
3472     const Standard_Integer aNbV = aV[0].IsSame(aV[1]) ? 1 : 2;
3473     Standard_Integer       i;
3474
3475     for (i = 0; i < aNbV; ++i) {
3476       if (aV[i].IsNull() == Standard_False) {
3477         if (!aMapVE.IsBound(aV[i])) {
3478           // There is no this vertex in the map.
3479           aMapVE.Bind(aV[i], TopTools_ListOfShape());
3480         }
3481
3482         // Add the edge to the list bound with the vertex aV[i].
3483         TopTools_ListOfShape &aLEdges = aMapVE.ChangeFind(aV[i]);
3484
3485         aLEdges.Append(anEdge);
3486       }
3487     }
3488   }
3489
3490   // Step 3: Find starting point in aMapVE.
3491   TopoDS_Vertex aStartVtx = TopoDS::Vertex(aStartPoint);
3492
3493   if (!aMapVE.IsBound(aStartVtx)) {
3494     aStartVtx = getSameVertex(aShape, aStartVtx);
3495
3496     if (aStartVtx.IsNull()) {
3497       SetErrorCode("Invalid Starting point");
3498       return aResult;
3499     }
3500   }
3501
3502   TopTools_IndexedMapOfShape anIndices;
3503   TopTools_MapOfShape        aMapVFence;
3504   TopoDS_Shape               aCurVtx  = aStartVtx;
3505   TopoDS_Edge                aCurEdge =
3506     TopoDS::Edge(aMapVE.Find(aCurVtx).First());
3507
3508   aResult = new TColStd_HSequenceOfInteger;
3509   TopExp::MapShapes(aShape, anIndices);
3510
3511   // Step 4: Fill the list of sorted edges.
3512   while (aMapVFence.Add(aCurVtx)) {
3513     // Append the ID of the current edge to the list of sorted.
3514     aResult->Append(anIndices.FindIndex(aCurEdge));
3515     TopExp::Vertices(aCurEdge, aV[0], aV[1]);
3516
3517     // Get the next vertex.
3518     if (aCurVtx.IsSame(aV[0])) {
3519       if (aCurVtx.IsSame(aV[1])) {
3520         // There is no next vertex.
3521         break;
3522       } else {
3523         aCurVtx = aV[1];
3524       }
3525     } else {
3526       aCurVtx = aV[0];
3527     }
3528
3529     if (aCurVtx.IsNull()) {
3530       // There is no next vertex.
3531       break;
3532     }
3533
3534     // Get the next edge.
3535     const TopTools_ListOfShape         &aLEdges = aMapVE.Find(aCurVtx);
3536     TopTools_ListIteratorOfListOfShape  anEIter(aLEdges);
3537
3538     for (; anEIter.More(); anEIter.Next()) {
3539       const TopoDS_Shape &aLocalEdge = anEIter.Value();
3540
3541       if (aLocalEdge.IsNull() == Standard_False) {
3542         if (!aCurEdge.IsSame(aLocalEdge)) {
3543           aCurEdge = TopoDS::Edge(aLocalEdge);
3544           break;
3545         }
3546       }
3547     }
3548
3549     if (!anEIter.More()) {
3550       // There is no next edge.
3551       break;
3552     }
3553   }
3554
3555   return aResult;
3556 }
3557
3558 //=======================================================================
3559 //function : getShapesOnSurface
3560 /*!
3561    * \brief Find sub-shapes complying with given status about surface
3562     * \param theSurface - the surface to check state of sub-shapes against
3563     * \param theShape - the shape to explore
3564     * \param theShapeType - type of sub-shape of theShape
3565     * \param theState - required state
3566     * \param theShapeEntries - outgoing entries like "entry1, entry2, ..."
3567     * \retval Handle(TColStd_HSequenceOfInteger) - IDs of found sub-shapes
3568  */
3569 //=======================================================================
3570 Handle(TColStd_HSequenceOfTransient)
3571     GEOMImpl_IShapesOperations::getShapesOnSurface(const Handle(Geom_Surface)& theSurface,
3572                                                    const Handle(GEOM_Object)&  theShape,
3573                                                    TopAbs_ShapeEnum            theShapeType,
3574                                                    GEOMAlgo_State              theState,
3575                                                    TCollection_AsciiString &   theShapeEntries)
3576 {
3577   // Find sub-shapes ids
3578   Handle(TColStd_HSequenceOfInteger) aSeqOfIDs =
3579     getShapesOnSurfaceIDs (theSurface, theShape->GetValue(), theShapeType, theState);
3580   if ( aSeqOfIDs.IsNull()  || aSeqOfIDs->Length() == 0 )
3581     return NULL;
3582
3583   return getObjectsShapesOn( theShape, aSeqOfIDs, theShapeEntries );
3584 }
3585
3586 //=============================================================================
3587 /*!
3588  *  GetShapesOnPlane
3589  */
3590 //=============================================================================
3591 Handle(TColStd_HSequenceOfTransient) GEOMImpl_IShapesOperations::GetShapesOnPlane
3592                                         (const Handle(GEOM_Object)& theShape,
3593                                          const Standard_Integer     theShapeType,
3594                                          const Handle(GEOM_Object)& theAx1,
3595                                          const GEOMAlgo_State       theState)
3596 {
3597   SetErrorCode(KO);
3598
3599   if (theShape.IsNull() || theAx1.IsNull()) return NULL;
3600
3601   TopoDS_Shape aShape = theShape->GetValue();
3602   TopoDS_Shape anAx1  = theAx1->GetValue();
3603
3604   if (aShape.IsNull() || anAx1.IsNull()) return NULL;
3605
3606   TopAbs_ShapeEnum aShapeType = TopAbs_ShapeEnum(theShapeType);
3607   if ( !checkTypeShapesOn( theShapeType ))
3608     return NULL;
3609
3610   // Create plane
3611   Handle(Geom_Surface) aPlane = makePlane( anAx1 );
3612   if ( aPlane.IsNull() )
3613     return NULL;
3614
3615   // Find objects
3616   TCollection_AsciiString anAsciiList;
3617   Handle(TColStd_HSequenceOfTransient) aSeq;
3618   aSeq = getShapesOnSurface( aPlane, theShape, aShapeType, theState, anAsciiList );
3619   if ( aSeq.IsNull() || aSeq->Length() == 0 )
3620     return NULL;
3621
3622   // Make a Python command
3623
3624   Handle(GEOM_Object) anObj = Handle(GEOM_Object)::DownCast( aSeq->Value( 1 ));
3625   Handle(GEOM_Function) aFunction = anObj->GetLastFunction();
3626
3627   GEOM::TPythonDump(aFunction) << "[" << anAsciiList.ToCString()
3628     << "] = geompy.GetShapesOnPlane(" << theShape << ", "
3629       << aShapeType << ", " << theAx1 << ", " << theState << ")";
3630
3631   SetErrorCode(OK);
3632   return aSeq;
3633 }
3634
3635 //=============================================================================
3636 /*!
3637  *  GetShapesOnPlaneWithLocation
3638  */
3639 //=============================================================================
3640 Handle(TColStd_HSequenceOfTransient) GEOMImpl_IShapesOperations::GetShapesOnPlaneWithLocation
3641                                         (const Handle(GEOM_Object)& theShape,
3642                                          const Standard_Integer     theShapeType,
3643                                          const Handle(GEOM_Object)& theAx1,
3644                                          const Handle(GEOM_Object)& thePnt,
3645                                          const GEOMAlgo_State       theState)
3646 {
3647   SetErrorCode(KO);
3648
3649   if (theShape.IsNull() || theAx1.IsNull() || thePnt.IsNull()) return NULL;
3650
3651   TopoDS_Shape aShape = theShape->GetValue();
3652   TopoDS_Shape anAx1  = theAx1->GetValue();
3653   TopoDS_Shape anPnt = thePnt->GetValue();
3654
3655   if (aShape.IsNull() || anAx1.IsNull() || anPnt.IsNull()) return NULL;
3656
3657   TopAbs_ShapeEnum aShapeType = TopAbs_ShapeEnum(theShapeType);
3658   if ( !checkTypeShapesOn( theShapeType ))
3659     return NULL;
3660
3661   // Create plane
3662   if ( anAx1.ShapeType() != TopAbs_EDGE || anPnt.ShapeType() != TopAbs_VERTEX ) return NULL;
3663   TopoDS_Vertex V1, V2, V3;
3664   TopoDS_Edge anEdge = TopoDS::Edge(anAx1);
3665   TopExp::Vertices(anEdge, V1, V2, Standard_True);
3666
3667   if (V1.IsNull() || V2.IsNull()) {
3668     SetErrorCode("Bad edge given for the plane normal vector");
3669     return NULL;
3670   }
3671   V3 = TopoDS::Vertex(anPnt);
3672
3673   if(V3.IsNull()) {
3674     SetErrorCode("Bad vertex given for the plane location");
3675       return NULL;
3676   }
3677   gp_Pnt aLoc = BRep_Tool::Pnt(V3);
3678   gp_Vec aVec(BRep_Tool::Pnt(V1),BRep_Tool::Pnt(V2));
3679
3680   if (aVec.Magnitude() < Precision::Confusion()) {
3681     SetErrorCode("Vector with null magnitude given");
3682     return NULL;
3683   }
3684   Handle(Geom_Surface) aPlane = new Geom_Plane(aLoc, aVec);
3685
3686   if ( aPlane.IsNull() )
3687     return NULL;
3688
3689   // Find objects
3690   TCollection_AsciiString anAsciiList;
3691   Handle(TColStd_HSequenceOfTransient) aSeq;
3692   aSeq = getShapesOnSurface( aPlane, theShape, aShapeType, theState, anAsciiList );
3693   if ( aSeq.IsNull() || aSeq->Length() == 0 )
3694     return NULL;
3695
3696   // Make a Python command
3697
3698   Handle(GEOM_Object) anObj = Handle(GEOM_Object)::DownCast( aSeq->Value( 1 ));
3699   Handle(GEOM_Function) aFunction = anObj->GetLastFunction();
3700
3701   GEOM::TPythonDump(aFunction) << "[" << anAsciiList.ToCString()
3702     << "] = geompy.GetShapesOnPlaneWithLocation(" << theShape << ", "
3703     << aShapeType << ", " << theAx1 << ", "<< thePnt <<", " << theState << ")";
3704
3705   SetErrorCode(OK);
3706   return aSeq;
3707 }
3708
3709 //=============================================================================
3710 /*!
3711  *  GetShapesOnCylinder
3712  */
3713 //=============================================================================
3714 Handle(TColStd_HSequenceOfTransient) GEOMImpl_IShapesOperations::GetShapesOnCylinder
3715                                           (const Handle(GEOM_Object)& theShape,
3716                                            const Standard_Integer     theShapeType,
3717                                            const Handle(GEOM_Object)& theAxis,
3718                                            const Standard_Real        theRadius,
3719                                            const GEOMAlgo_State       theState)
3720 {
3721   SetErrorCode(KO);
3722
3723   if (theShape.IsNull() || theAxis.IsNull()) return NULL;
3724
3725   TopoDS_Shape aShape = theShape->GetValue();
3726   TopoDS_Shape anAxis = theAxis->GetValue();
3727
3728   if (aShape.IsNull() || anAxis.IsNull()) return NULL;
3729
3730   TopAbs_ShapeEnum aShapeType = TopAbs_ShapeEnum(theShapeType);
3731   if ( !checkTypeShapesOn( aShapeType ))
3732     return NULL;
3733
3734   // Create a cylinder surface
3735   Handle(Geom_Surface) aCylinder = makeCylinder( anAxis, theRadius );
3736   if ( aCylinder.IsNull() )
3737     return NULL;
3738
3739   // Find objects
3740   TCollection_AsciiString anAsciiList;
3741   Handle(TColStd_HSequenceOfTransient) aSeq;
3742   aSeq = getShapesOnSurface( aCylinder, theShape, aShapeType, theState, anAsciiList );
3743   if ( aSeq.IsNull() || aSeq->Length() == 0 )
3744     return NULL;
3745
3746   // Make a Python command
3747
3748   Handle(GEOM_Object) anObj = Handle(GEOM_Object)::DownCast( aSeq->Value( 1 ));
3749   Handle(GEOM_Function) aFunction = anObj->GetLastFunction();
3750
3751   GEOM::TPythonDump(aFunction) << "[" << anAsciiList.ToCString()
3752     << "] = geompy.GetShapesOnCylinder(" << theShape << ", " << aShapeType
3753       << ", " << theAxis << ", " << theRadius << ", " << theState << ")";
3754
3755   SetErrorCode(OK);
3756   return aSeq;
3757 }
3758
3759 //=============================================================================
3760 /*!
3761  *  GetShapesOnCylinderWithLocation
3762  */
3763 //=============================================================================
3764 Handle(TColStd_HSequenceOfTransient) GEOMImpl_IShapesOperations::GetShapesOnCylinderWithLocation
3765                                           (const Handle(GEOM_Object)& theShape,
3766                                            const Standard_Integer     theShapeType,
3767                                            const Handle(GEOM_Object)& theAxis,
3768                                            const Handle(GEOM_Object)& thePnt,
3769                                            const Standard_Real        theRadius,
3770                                            const GEOMAlgo_State       theState)
3771 {
3772   SetErrorCode(KO);
3773
3774   if (theShape.IsNull() || theAxis.IsNull() || thePnt.IsNull()) return NULL;
3775
3776   TopoDS_Shape aShape = theShape->GetValue();
3777   TopoDS_Shape anAxis = theAxis->GetValue();
3778   TopoDS_Shape aPnt   = thePnt->GetValue();
3779
3780   if (aShape.IsNull() || anAxis.IsNull() || aPnt.IsNull()) return NULL;
3781
3782   if (aPnt.ShapeType() != TopAbs_VERTEX )
3783   {
3784     SetErrorCode("Bottom location point must be vertex");
3785     return NULL;
3786   }
3787
3788   TopAbs_ShapeEnum aShapeType = TopAbs_ShapeEnum(theShapeType);
3789   if ( !checkTypeShapesOn( aShapeType ))
3790     return NULL;
3791
3792   // Create a cylinder surface
3793   Handle(Geom_Surface) aCylinder = makeCylinder( anAxis, theRadius );
3794   if ( aCylinder.IsNull() )
3795     return NULL;
3796
3797   // translate the surface
3798   Handle(Geom_CylindricalSurface) aCylSurface =
3799     Handle(Geom_CylindricalSurface)::DownCast( aCylinder );
3800   if ( aCylSurface.IsNull() )
3801   {
3802     SetErrorCode("Unexpected surface type instead of Geom_CylindricalSurface");
3803     return NULL;
3804   }
3805   gp_Pnt fromLoc = aCylSurface->Cylinder().Location();
3806   gp_Pnt toLoc   = BRep_Tool::Pnt( TopoDS::Vertex( aPnt ));
3807   aCylinder->Translate( fromLoc, toLoc );
3808
3809   // Find objects
3810   TCollection_AsciiString anAsciiList;
3811   Handle(TColStd_HSequenceOfTransient) aSeq;
3812   aSeq = getShapesOnSurface( aCylinder, theShape, aShapeType, theState, anAsciiList );
3813   if ( aSeq.IsNull() || aSeq->Length() == 0 )
3814     return NULL;
3815
3816   // Make a Python command
3817
3818   Handle(GEOM_Object) anObj = Handle(GEOM_Object)::DownCast( aSeq->Value( 1 ));
3819   Handle(GEOM_Function) aFunction = anObj->GetLastFunction();
3820
3821   GEOM::TPythonDump(aFunction)
3822     << "[" << anAsciiList.ToCString()
3823     << "] = geompy.GetShapesOnCylinderWithLocation(" << theShape << ", " << aShapeType << ", "
3824     << theAxis << ", " << thePnt << ", " << theRadius << ", " << theState << ")";
3825
3826   SetErrorCode(OK);
3827   return aSeq;
3828 }
3829
3830 //=============================================================================
3831 /*!
3832  *  GetShapesOnSphere
3833  */
3834 //=============================================================================
3835 Handle(TColStd_HSequenceOfTransient) GEOMImpl_IShapesOperations::GetShapesOnSphere
3836                                           (const Handle(GEOM_Object)& theShape,
3837                                            const Standard_Integer     theShapeType,
3838                                            const Handle(GEOM_Object)& theCenter,
3839                                            const Standard_Real        theRadius,
3840                                            const GEOMAlgo_State       theState)
3841 {
3842   SetErrorCode(KO);
3843
3844   if (theShape.IsNull() || theCenter.IsNull()) return NULL;
3845
3846   TopoDS_Shape aShape  = theShape->GetValue();
3847   TopoDS_Shape aCenter = theCenter->GetValue();
3848
3849   if (aShape.IsNull() || aCenter.IsNull()) return NULL;
3850
3851   TopAbs_ShapeEnum aShapeType = TopAbs_ShapeEnum(theShapeType);
3852   if ( !checkTypeShapesOn( aShapeType ))
3853     return NULL;
3854
3855   // Center of the sphere
3856   if (aCenter.ShapeType() != TopAbs_VERTEX) return NULL;
3857   gp_Pnt aLoc = BRep_Tool::Pnt(TopoDS::Vertex(aCenter));
3858
3859   gp_Ax3 anAx3 (aLoc, gp::DZ());
3860   Handle(Geom_SphericalSurface) aSphere =
3861     new Geom_SphericalSurface(anAx3, theRadius);
3862
3863   // Find objects
3864   TCollection_AsciiString anAsciiList;
3865   Handle(TColStd_HSequenceOfTransient) aSeq;
3866   aSeq = getShapesOnSurface( aSphere, theShape, aShapeType, theState, anAsciiList );
3867   if ( aSeq.IsNull() || aSeq->Length() == 0 )
3868     return NULL;
3869
3870   // Make a Python command
3871
3872   Handle(GEOM_Object) anObj = Handle(GEOM_Object)::DownCast( aSeq->Value( 1 ));
3873   Handle(GEOM_Function) aFunction = anObj->GetLastFunction();
3874
3875   GEOM::TPythonDump(aFunction) << "[" << anAsciiList.ToCString()
3876     << "] = geompy.GetShapesOnSphere(" << theShape << ", " << aShapeType
3877       << ", " << theCenter << ", " << theRadius << ", " << theState << ")";
3878
3879   SetErrorCode(OK);
3880   return aSeq;
3881 }
3882
3883 //=============================================================================
3884 /*!
3885  *  GetShapesOnPlaneIDs
3886  */
3887 //=============================================================================
3888 Handle(TColStd_HSequenceOfInteger) GEOMImpl_IShapesOperations::GetShapesOnPlaneIDs
3889                                         (const Handle(GEOM_Object)& theShape,
3890                                          const Standard_Integer     theShapeType,
3891                                          const Handle(GEOM_Object)& theAx1,
3892                                          const GEOMAlgo_State       theState)
3893 {
3894   SetErrorCode(KO);
3895
3896   if (theShape.IsNull() || theAx1.IsNull()) return NULL;
3897
3898   TopoDS_Shape aShape = theShape->GetValue();
3899   TopoDS_Shape anAx1  = theAx1->GetValue();
3900
3901   if (aShape.IsNull() || anAx1.IsNull()) return NULL;
3902
3903   TopAbs_ShapeEnum aShapeType = TopAbs_ShapeEnum(theShapeType);
3904   if ( !checkTypeShapesOn( aShapeType ))
3905     return NULL;
3906
3907   // Create plane
3908   Handle(Geom_Surface) aPlane = makePlane( anAx1 );
3909   if ( aPlane.IsNull() )
3910     return NULL;
3911
3912   // Find object IDs
3913   Handle(TColStd_HSequenceOfInteger) aSeq;
3914   aSeq = getShapesOnSurfaceIDs( aPlane, aShape, aShapeType, theState );
3915
3916   // The GetShapesOnPlaneIDs() doesn't change object so no new function is required.
3917   Handle(GEOM_Function) aFunction = GEOM::GetCreatedLast(theShape,theAx1)->GetLastFunction();
3918
3919   // VSR 29/08/2017: 0023327, 0023428: eliminate unnecessary lines in Python dump
3920 #ifdef DUMP_SUBSHAPE_IDS
3921   // Make a Python command
3922   GEOM::TPythonDump(aFunction, /*append=*/true)
3923     << "listShapesOnPlane = geompy.GetShapesOnPlaneIDs"
3924     << "(" << theShape << "," << aShapeType << "," << theAx1 << "," << theState << ")";
3925 #endif // DUMP_SUBSHAPE_IDS
3926
3927   SetErrorCode(OK);
3928   return aSeq;
3929 }
3930
3931 //=============================================================================
3932 /*!
3933  *  GetShapesOnPlaneWithLocationIDs
3934  */
3935 //=============================================================================
3936 Handle(TColStd_HSequenceOfInteger) GEOMImpl_IShapesOperations::GetShapesOnPlaneWithLocationIDs
3937                                         (const Handle(GEOM_Object)& theShape,
3938                                          const Standard_Integer     theShapeType,
3939                                          const Handle(GEOM_Object)& theAx1,
3940                                          const Handle(GEOM_Object)& thePnt,
3941                                          const GEOMAlgo_State       theState)
3942 {
3943   SetErrorCode(KO);
3944
3945   if (theShape.IsNull() || theAx1.IsNull() || thePnt.IsNull()) return NULL;
3946
3947   TopoDS_Shape aShape = theShape->GetValue();
3948   TopoDS_Shape anAx1  = theAx1->GetValue();
3949   TopoDS_Shape anPnt  = thePnt->GetValue();
3950
3951   if (aShape.IsNull() || anAx1.IsNull() || anPnt.IsNull()) return NULL;
3952
3953   TopAbs_ShapeEnum aShapeType = TopAbs_ShapeEnum(theShapeType);
3954   if ( !checkTypeShapesOn( aShapeType ))
3955     return NULL;
3956
3957   // Create plane
3958   if (anAx1.ShapeType() != TopAbs_EDGE || anPnt.ShapeType() != TopAbs_VERTEX) return NULL;
3959   TopoDS_Edge anEdge = TopoDS::Edge(anAx1);
3960   TopoDS_Vertex V1, V2, V3;
3961   TopExp::Vertices(anEdge, V1, V2, Standard_True);
3962   if (V1.IsNull() || V2.IsNull()) {
3963     SetErrorCode("Bad edge given for the plane normal vector");
3964     return NULL;
3965   }
3966   V3 = TopoDS::Vertex(anPnt);
3967   if(V3.IsNull()) {
3968     SetErrorCode("Bad vertex given for the plane location");
3969       return NULL;
3970   }
3971   gp_Pnt aLoc = BRep_Tool::Pnt(V3);
3972   gp_Vec aVec(BRep_Tool::Pnt(V1),BRep_Tool::Pnt(V2));
3973   if (aVec.Magnitude() < Precision::Confusion()) {
3974     SetErrorCode("Vector with null magnitude given");
3975     return NULL;
3976   }
3977
3978   Handle(Geom_Surface) aPlane = new Geom_Plane(aLoc, aVec);
3979   if ( aPlane.IsNull() )
3980     return NULL;
3981
3982   // Find object IDs
3983   Handle(TColStd_HSequenceOfInteger) aSeq;
3984   aSeq = getShapesOnSurfaceIDs( aPlane, aShape, aShapeType, theState );
3985
3986   // The GetShapesOnPlaneIDs() doesn't change object so no new function is required.
3987   Handle(GEOM_Function) aFunction = GEOM::GetCreatedLast(theShape,theAx1)->GetLastFunction();
3988
3989   // VSR 29/08/2017: 0023327, 0023428: eliminate unnecessary lines in Python dump
3990 #ifdef DUMP_SUBSHAPE_IDS
3991   // Make a Python command
3992   GEOM::TPythonDump(aFunction, /*append=*/true)
3993     << "listShapesOnPlane = geompy.GetShapesOnPlaneWithLocationIDs"
3994     << "(" << theShape << ", " << aShapeType << ", " << theAx1 << ", "<< thePnt << ", "  << theState << ")";
3995 #endif // DUMP_SUBSHAPE_IDS
3996
3997   SetErrorCode(OK);
3998   return aSeq;
3999 }
4000
4001 //=============================================================================
4002 /*!
4003  *  GetShapesOnCylinderIDs
4004  */
4005 //=============================================================================
4006 Handle(TColStd_HSequenceOfInteger) GEOMImpl_IShapesOperations::GetShapesOnCylinderIDs
4007                                           (const Handle(GEOM_Object)& theShape,
4008                                            const Standard_Integer     theShapeType,
4009                                            const Handle(GEOM_Object)& theAxis,
4010                                            const Standard_Real        theRadius,
4011                                            const GEOMAlgo_State       theState)
4012 {
4013   SetErrorCode(KO);
4014
4015   if (theShape.IsNull() || theAxis.IsNull()) return NULL;
4016
4017   TopoDS_Shape aShape = theShape->GetValue();
4018   TopoDS_Shape anAxis = theAxis->GetValue();
4019
4020   if (aShape.IsNull() || anAxis.IsNull()) return NULL;
4021
4022   TopAbs_ShapeEnum aShapeType = TopAbs_ShapeEnum(theShapeType);
4023   if ( !checkTypeShapesOn( aShapeType ))
4024     return NULL;
4025
4026   // Create a cylinder surface
4027   Handle(Geom_Surface) aCylinder = makeCylinder( anAxis, theRadius );
4028   if ( aCylinder.IsNull() )
4029     return NULL;
4030
4031   // Find object IDs
4032   Handle(TColStd_HSequenceOfInteger) aSeq;
4033   aSeq = getShapesOnSurfaceIDs( aCylinder, aShape, aShapeType, theState );
4034
4035   // The GetShapesOnCylinder() doesn't change object so no new function is required.
4036   Handle(GEOM_Function) aFunction = GEOM::GetCreatedLast(theShape,theAxis)->GetLastFunction();
4037
4038   // VSR 29/08/2017: 0023327, 0023428: eliminate unnecessary lines in Python dump
4039 #ifdef DUMP_SUBSHAPE_IDS
4040   // Make a Python command
4041   GEOM::TPythonDump(aFunction, /*append=*/true)
4042     << "listShapesOnCylinder = geompy.GetShapesOnCylinderIDs"
4043     << "(" << theShape << ", " << aShapeType << ", " << theAxis << ", "
4044     << theRadius << ", " << theState << ")";
4045 #endif // DUMP_SUBSHAPE_IDS
4046
4047   SetErrorCode(OK);
4048   return aSeq;
4049 }
4050
4051 //=============================================================================
4052 /*!
4053  *  GetShapesOnCylinderWithLocationIDs
4054  */
4055 //=============================================================================
4056 Handle(TColStd_HSequenceOfInteger) GEOMImpl_IShapesOperations::GetShapesOnCylinderWithLocationIDs
4057                                           (const Handle(GEOM_Object)& theShape,
4058                                            const Standard_Integer     theShapeType,
4059                                            const Handle(GEOM_Object)& theAxis,
4060                                            const Handle(GEOM_Object)& thePnt,
4061                                            const Standard_Real        theRadius,
4062                                            const GEOMAlgo_State       theState)
4063 {
4064   SetErrorCode(KO);
4065
4066   if (theShape.IsNull() || theAxis.IsNull() || thePnt.IsNull()) return NULL;
4067
4068   TopoDS_Shape aShape = theShape->GetValue();
4069   TopoDS_Shape anAxis = theAxis->GetValue();
4070   TopoDS_Shape aPnt   = thePnt->GetValue();
4071
4072   if (aShape.IsNull() || anAxis.IsNull() || aPnt.IsNull()) return NULL;
4073
4074   if (aPnt.ShapeType() != TopAbs_VERTEX )
4075   {
4076     SetErrorCode("Bottom location point must be vertex");
4077     return NULL;
4078   }
4079
4080   TopAbs_ShapeEnum aShapeType = TopAbs_ShapeEnum(theShapeType);
4081   if ( !checkTypeShapesOn( aShapeType ))
4082     return NULL;
4083
4084   // Create a cylinder surface
4085   Handle(Geom_Surface) aCylinder = makeCylinder( anAxis, theRadius );
4086   if ( aCylinder.IsNull() )
4087     return NULL;
4088
4089   // translate the surface
4090   Handle(Geom_CylindricalSurface) aCylSurface =
4091     Handle(Geom_CylindricalSurface)::DownCast( aCylinder );
4092   if ( aCylSurface.IsNull() )
4093   {
4094     SetErrorCode("Unexpected surface type instead of Geom_CylindricalSurface");
4095     return NULL;
4096   }
4097   gp_Pnt fromLoc = aCylSurface->Cylinder().Location();
4098   gp_Pnt toLoc   = BRep_Tool::Pnt( TopoDS::Vertex( aPnt ));
4099   aCylinder->Translate( fromLoc, toLoc );
4100
4101   // Find object IDs
4102   Handle(TColStd_HSequenceOfInteger) aSeq;
4103   aSeq = getShapesOnSurfaceIDs( aCylinder, aShape, aShapeType, theState );
4104
4105   // The GetShapesOnCylinder() doesn't change object so no new function is required.
4106   Handle(GEOM_Function) aFunction =
4107     GEOM::GetCreatedLast(theShape, GEOM::GetCreatedLast(thePnt,theAxis))->GetLastFunction();
4108
4109   // VSR 29/08/2017: 0023327, 0023428: eliminate unnecessary lines in Python dump
4110 #ifdef DUMP_SUBSHAPE_IDS
4111   // Make a Python command
4112   GEOM::TPythonDump(aFunction, /*append=*/true)
4113     << "listShapesOnCylinder = geompy.GetShapesOnCylinderWithLocationIDs"
4114     << "(" << theShape << ", " << aShapeType << ", " << theAxis << ", "
4115     << thePnt << ", " << theRadius << ", " << theState << ")";
4116 #endif // DUMP_SUBSHAPE_IDS
4117
4118   SetErrorCode(OK);
4119   return aSeq;
4120 }
4121
4122 //=============================================================================
4123 /*!
4124  *  GetShapesOnSphereIDs
4125  */
4126 //=============================================================================
4127 Handle(TColStd_HSequenceOfInteger) GEOMImpl_IShapesOperations::GetShapesOnSphereIDs
4128                                           (const Handle(GEOM_Object)& theShape,
4129                                            const Standard_Integer     theShapeType,
4130                                            const Handle(GEOM_Object)& theCenter,
4131                                            const Standard_Real        theRadius,
4132                                            const GEOMAlgo_State       theState)
4133 {
4134   SetErrorCode(KO);
4135
4136   if (theShape.IsNull() || theCenter.IsNull()) return NULL;
4137
4138   TopoDS_Shape aShape  = theShape->GetValue();
4139   TopoDS_Shape aCenter = theCenter->GetValue();
4140
4141   if (aShape.IsNull() || aCenter.IsNull()) return NULL;
4142
4143   TopAbs_ShapeEnum aShapeType = TopAbs_ShapeEnum(theShapeType);
4144   if ( !checkTypeShapesOn( aShapeType ))
4145     return NULL;
4146
4147   // Center of the sphere
4148   if (aCenter.ShapeType() != TopAbs_VERTEX) return NULL;
4149   gp_Pnt aLoc = BRep_Tool::Pnt(TopoDS::Vertex(aCenter));
4150
4151   gp_Ax3 anAx3 (aLoc, gp::DZ());
4152   Handle(Geom_SphericalSurface) aSphere =
4153     new Geom_SphericalSurface(anAx3, theRadius);
4154
4155   // Find object IDs
4156   Handle(TColStd_HSequenceOfInteger) aSeq;
4157   aSeq = getShapesOnSurfaceIDs( aSphere, aShape, aShapeType, theState );
4158
4159   // The GetShapesOnSphere() doesn't change object so no new function is required.
4160   Handle(GEOM_Function) aFunction = GEOM::GetCreatedLast(theShape,theCenter)->GetLastFunction();
4161
4162   // VSR 29/08/2017: 0023327, 0023428: eliminate unnecessary lines in Python dump
4163 #ifdef DUMP_SUBSHAPE_IDS
4164   // Make a Python command
4165   GEOM::TPythonDump(aFunction, /*append=*/true)
4166     << "listShapesOnSphere = geompy.GetShapesOnSphereIDs"
4167     << "(" << theShape << ", " << aShapeType << ", " << theCenter << ", "
4168     << theRadius << ", " << theState << ")";
4169 #endif // DUMP_SUBSHAPE_IDS
4170
4171   SetErrorCode(OK);
4172   return aSeq;
4173 }
4174
4175 //=======================================================================
4176 //function : getShapesOnQuadrangleIDs
4177   /*!
4178    * \brief Find IDs of sub-shapes complying with given status about quadrangle
4179     * \param theShape - the shape to explore
4180     * \param theShapeType - type of sub-shape of theShape
4181     * \param theTopLeftPoint - top left quadrangle corner
4182     * \param theTopRightPoint - top right quadrangle corner
4183     * \param theBottomLeftPoint - bottom left quadrangle corner
4184     * \param theBottomRightPoint - bottom right quadrangle corner
4185     * \param theState - required state
4186     * \retval Handle(TColStd_HSequenceOfInteger) - IDs of found sub-shapes
4187    */
4188 //=======================================================================
4189 Handle(TColStd_HSequenceOfInteger)
4190   GEOMImpl_IShapesOperations::getShapesOnQuadrangleIDs (const Handle(GEOM_Object)& theShape,
4191                                                         const Standard_Integer     theShapeType,
4192                                                         const Handle(GEOM_Object)& theTopLeftPoint,
4193                                                         const Handle(GEOM_Object)& theTopRightPoint,
4194                                                         const Handle(GEOM_Object)& theBottomLeftPoint,
4195                                                         const Handle(GEOM_Object)& theBottomRightPoint,
4196                                                         const GEOMAlgo_State       theState)
4197 {
4198   SetErrorCode(KO);
4199
4200   if ( theShape.IsNull() ||
4201        theTopLeftPoint.IsNull() ||
4202        theTopRightPoint.IsNull() ||
4203        theBottomLeftPoint.IsNull() ||
4204        theBottomRightPoint.IsNull() )
4205     return NULL;
4206
4207   TopoDS_Shape aShape = theShape->GetValue();
4208   TopoDS_Shape aTL = theTopLeftPoint->GetValue();
4209   TopoDS_Shape aTR = theTopRightPoint->GetValue();
4210   TopoDS_Shape aBL = theBottomLeftPoint->GetValue();
4211   TopoDS_Shape aBR = theBottomRightPoint->GetValue();
4212
4213   if (aShape.IsNull() ||
4214       aTL.IsNull() ||
4215       aTR.IsNull() ||
4216       aBL.IsNull() ||
4217       aBR.IsNull() ||
4218       aTL.ShapeType() != TopAbs_VERTEX ||
4219       aTR.ShapeType() != TopAbs_VERTEX ||
4220       aBL.ShapeType() != TopAbs_VERTEX ||
4221       aBR.ShapeType() != TopAbs_VERTEX )
4222     return NULL;
4223
4224   TopAbs_ShapeEnum aShapeType = TopAbs_ShapeEnum(theShapeType);
4225   if ( !checkTypeShapesOn( aShapeType ))
4226     return NULL;
4227
4228   Handle(TColStd_HSequenceOfInteger) aSeqOfIDs;
4229
4230   // Check presence of triangulation, build if need
4231   if (theShapeType != TopAbs_VERTEX && !GEOMUtils::CheckTriangulation(aShape)) {
4232     SetErrorCode("Cannot build triangulation on the shape");
4233     return aSeqOfIDs;
4234   }
4235
4236   // Call algo
4237   gp_Pnt aPntTL = BRep_Tool::Pnt(TopoDS::Vertex(aTL));
4238   gp_Pnt aPntTR = BRep_Tool::Pnt(TopoDS::Vertex(aTR));
4239   gp_Pnt aPntBL = BRep_Tool::Pnt(TopoDS::Vertex(aBL));
4240   gp_Pnt aPntBR = BRep_Tool::Pnt(TopoDS::Vertex(aBR));
4241
4242   GEOMAlgo_FinderShapeOn2  aFinder;
4243   Handle(GEOMAlgo_ClsfQuad) aClsfQuad = new GEOMAlgo_ClsfQuad;
4244
4245   Standard_Real aTol = 0.0001; // default value
4246
4247   aClsfQuad->SetCorners(aPntTL, aPntTR, aPntBL, aPntBR);
4248   aFinder.SetShape(aShape);
4249   aFinder.SetTolerance(aTol);
4250   aFinder.SetClsf(aClsfQuad);
4251   aFinder.SetShapeType(aShapeType);
4252   aFinder.SetState(theState);
4253
4254   // Sets the minimal number of inner points for the faces that do not have own
4255   // inner points at all (for e.g. rectangular planar faces have just 2 triangles).
4256   // Default value=3
4257   aFinder.SetNbPntsMin(3);
4258   // Sets the maximal number of inner points for edges or faces.
4259   // It is useful for the cases when this number is very big (e.g =2000) to improve
4260   // the performance. If this value =0, all inner points will be taken into account.
4261   // Default value=0
4262   aFinder.SetNbPntsMax(100);
4263
4264   aFinder.Perform();
4265
4266   // Interpret results
4267   Standard_Integer iErr = aFinder.ErrorStatus();
4268   // the detailed description of error codes is in GEOMAlgo_FinderShapeOn2.cxx
4269   if (iErr) {
4270     MESSAGE(" iErr : " << iErr);
4271     TCollection_AsciiString aMsg (" iErr : ");
4272     aMsg += TCollection_AsciiString(iErr);
4273     SetErrorCode(aMsg);
4274     return aSeqOfIDs;
4275   }
4276   Standard_Integer iWrn = aFinder.WarningStatus();
4277   // the detailed description of warning codes is in GEOMAlgo_FinderShapeOn2.cxx
4278   if (iWrn) {
4279     MESSAGE(" *** iWrn : " << iWrn);
4280   }
4281
4282   const TopTools_ListOfShape& listSS = aFinder.Shapes(); // the result
4283
4284   if (listSS.Extent() < 1) {
4285     //SetErrorCode("Not a single sub-shape of the requested type found on the given surface");
4286     SetErrorCode(NOT_FOUND_ANY); // NPAL18017
4287     return aSeqOfIDs;
4288   }
4289
4290   // Fill sequence of object IDs
4291   aSeqOfIDs = new TColStd_HSequenceOfInteger;
4292
4293   TopTools_IndexedMapOfShape anIndices;
4294   TopExp::MapShapes(aShape, anIndices);
4295
4296   TopTools_ListIteratorOfListOfShape itSub (listSS);
4297   for (int index = 1; itSub.More(); itSub.Next(), ++index) {
4298     int id = anIndices.FindIndex(itSub.Value());
4299     aSeqOfIDs->Append(id);
4300   }
4301   return aSeqOfIDs;
4302 }
4303
4304 //=======================================================================
4305 //function : GetShapesOnQuadrangle
4306   /*!
4307    * \brief Find sub-shapes complying with given status about quadrangle
4308     * \param theShape - the shape to explore
4309     * \param theShapeType - type of sub-shape of theShape
4310     * \param theTopLeftPoint - top left quadrangle corner
4311     * \param theTopRightPoint - top right quadrangle corner
4312     * \param theBottomLeftPoint - bottom left quadrangle corner
4313     * \param theBottomRightPoint - bottom right quadrangle corner
4314     * \param theState - required state
4315     * \retval Handle(TColStd_HSequenceOfInteger) - IDs of found sub-shapes
4316    */
4317 //=======================================================================
4318 Handle(TColStd_HSequenceOfTransient)
4319     GEOMImpl_IShapesOperations::GetShapesOnQuadrangle (const Handle(GEOM_Object)& theShape,
4320                                                        const Standard_Integer     theShapeType,
4321                                                        const Handle(GEOM_Object)& theTopLeftPoint,
4322                                                        const Handle(GEOM_Object)& theTopRightPoint,
4323                                                        const Handle(GEOM_Object)& theBottomLeftPoint,
4324                                                        const Handle(GEOM_Object)& theBottomRightPoint,
4325                                                        const GEOMAlgo_State       theState)
4326 {
4327   // Find indices
4328   Handle(TColStd_HSequenceOfInteger) aSeqOfIDs =
4329     getShapesOnQuadrangleIDs( theShape,
4330                               theShapeType,
4331                               theTopLeftPoint,
4332                               theTopRightPoint,
4333                               theBottomLeftPoint,
4334                               theBottomRightPoint,
4335                               theState);
4336   if ( aSeqOfIDs.IsNull() || aSeqOfIDs->IsEmpty() )
4337     return NULL;
4338
4339   // Find objects by indices
4340   TCollection_AsciiString anAsciiList;
4341   Handle(TColStd_HSequenceOfTransient) aSeq;
4342   aSeq = getObjectsShapesOn( theShape, aSeqOfIDs, anAsciiList );
4343   if ( aSeq.IsNull() || aSeq->IsEmpty() )
4344     return NULL;
4345
4346   // Make a Python command
4347
4348   Handle(GEOM_Object) anObj = Handle(GEOM_Object)::DownCast( aSeq->Value( 1 ));
4349   Handle(GEOM_Function) aFunction = anObj->GetLastFunction();
4350
4351   GEOM::TPythonDump(aFunction)
4352     << "[" << anAsciiList.ToCString() << "] = geompy.GetShapesOnQuadrangle("
4353     << theShape << ", "
4354     << TopAbs_ShapeEnum(theShapeType) << ", "
4355     << theTopLeftPoint << ", "
4356     << theTopRightPoint << ", "
4357     << theBottomLeftPoint << ", "
4358     << theBottomRightPoint << ", "
4359     << theState << ")";
4360
4361   SetErrorCode(OK);
4362   return aSeq;
4363 }
4364
4365 //=======================================================================
4366 //function : GetShapesOnQuadrangleIDs
4367   /*!
4368    * \brief Find IDs of sub-shapes complying with given status about quadrangle
4369     * \param theShape - the shape to explore
4370     * \param theShapeType - type of sub-shape of theShape
4371     * \param theTopLeftPoint - top left quadrangle corner
4372     * \param theTopRightPoint - top right quadrangle corner
4373     * \param theBottomLeftPoint - bottom left quadrangle corner
4374     * \param theBottomRightPoint - bottom right quadrangle corner
4375     * \param theState - required state
4376     * \retval Handle(TColStd_HSequenceOfInteger) - IDs of found sub-shapes
4377    */
4378 //=======================================================================
4379 Handle(TColStd_HSequenceOfInteger)
4380   GEOMImpl_IShapesOperations::GetShapesOnQuadrangleIDs (const Handle(GEOM_Object)& theShape,
4381                                                         const Standard_Integer     theShapeType,
4382                                                         const Handle(GEOM_Object)& theTopLeftPoint,
4383                                                         const Handle(GEOM_Object)& theTopRightPoint,
4384                                                         const Handle(GEOM_Object)& theBottomLeftPoint,
4385                                                         const Handle(GEOM_Object)& theBottomRightPoint,
4386                                                         const GEOMAlgo_State       theState)
4387 {
4388   // Find indices
4389   Handle(TColStd_HSequenceOfInteger) aSeqOfIDs =
4390     getShapesOnQuadrangleIDs( theShape,
4391                               theShapeType,
4392                               theTopLeftPoint,
4393                               theTopRightPoint,
4394                               theBottomLeftPoint,
4395                               theBottomRightPoint,
4396                               theState);
4397   if ( aSeqOfIDs.IsNull() || aSeqOfIDs->IsEmpty() )
4398     return NULL;
4399
4400   // Make a Python command
4401
4402   // The GetShapesOnCylinder() doesn't change object so no new function is required.
4403   Handle(GEOM_BaseObject) lastObj = GEOM::GetCreatedLast(theShape,theTopLeftPoint);
4404   lastObj = GEOM::GetCreatedLast(lastObj,theTopRightPoint);
4405   lastObj = GEOM::GetCreatedLast(lastObj,theBottomRightPoint);
4406   lastObj = GEOM::GetCreatedLast(lastObj,theBottomLeftPoint);
4407   Handle(GEOM_Function) aFunction = lastObj->GetLastFunction();
4408
4409   // VSR 29/08/2017: 0023327, 0023428: eliminate unnecessary lines in Python dump
4410 #ifdef DUMP_SUBSHAPE_IDS
4411   GEOM::TPythonDump(aFunction, /*append=*/true)
4412     << "listShapesOnQuadrangle = geompy.GetShapesOnQuadrangleIDs("
4413     << theShape << ", "
4414     << TopAbs_ShapeEnum(theShapeType) << ", "
4415     << theTopLeftPoint << ", "
4416     << theTopRightPoint << ", "
4417     << theBottomLeftPoint << ", "
4418     << theBottomRightPoint << ", "
4419     << theState << ")";
4420 #endif // DUMP_SUBSHAPE_IDS
4421
4422   SetErrorCode(OK);
4423   return aSeqOfIDs;
4424 }
4425
4426 //=============================================================================
4427 /*!
4428  *  case GetInPlace:
4429  *  default:
4430  */
4431 //=============================================================================
4432 Handle(GEOM_Object) GEOMImpl_IShapesOperations::GetInPlace (Handle(GEOM_Object) theShapeWhere,
4433                                                             Handle(GEOM_Object) theShapeWhat)
4434 {
4435   SetErrorCode(KO);
4436
4437   if (theShapeWhere.IsNull() || theShapeWhat.IsNull()) return NULL;
4438
4439   TopoDS_Shape aWhere = theShapeWhere->GetValue();
4440   TopoDS_Shape aWhat  = theShapeWhat->GetValue();
4441
4442   if (aWhere.IsNull() || aWhat.IsNull()) {
4443     SetErrorCode("Error: aWhere and aWhat TopoDS_Shape are Null.");
4444     return NULL;
4445   }
4446
4447   // Searching for the sub-shapes inside the ShapeWhere shape
4448   GEOMAlgo_GetInPlace aGIP;
4449
4450   if (!GEOMAlgo_GetInPlaceAPI::GetInPlace(aWhere, aWhat, aGIP)) {
4451     SetErrorCode("Error in GEOMAlgo_GetInPlace");
4452     return NULL;
4453   }
4454
4455   // Add direct result.
4456   TopTools_ListOfShape        aLSA;
4457   const TopoDS_Shape         &aShapeResult = aGIP.Result();
4458   TopTools_MapOfShape         aMFence;
4459   TopTools_IndexedMapOfShape  aWhereIndices;
4460   Standard_Integer            aShapeType = -1;
4461
4462   TopExp::MapShapes(aWhere, aWhereIndices);
4463
4464   if (aShapeResult.IsNull() == Standard_False) {
4465     TopoDS_Iterator  anIt(aShapeResult);
4466
4467     for (; anIt.More(); anIt.Next()) {
4468       const TopoDS_Shape &aPart = anIt.Value();
4469
4470       if(aWhereIndices.Contains(aPart) && aMFence.Add(aPart)) {
4471         const TopAbs_ShapeEnum aType = aPart.ShapeType();
4472
4473         if (aShapeType == -1) {
4474           // Initialization.
4475           aShapeType = aType;
4476         } else if (aShapeType != TopAbs_SHAPE && aShapeType != aType) {
4477           // Different types.
4478           aShapeType = TopAbs_SHAPE;
4479         }
4480
4481         aLSA.Append(aPart);
4482       }
4483     }
4484   }
4485
4486   if (aLSA.Extent() == 0) {
4487     SetErrorCode(NOT_FOUND_ANY); // Not found any Results
4488     return NULL;
4489   }
4490
4491   Handle(TColStd_HArray1OfInteger) aModifiedArray = new TColStd_HArray1OfInteger (1, aLSA.Extent());
4492   TopTools_ListIteratorOfListOfShape anIterModif (aLSA);
4493   for (Standard_Integer imod = 1; anIterModif.More(); anIterModif.Next(), imod++) {
4494     aModifiedArray->SetValue(imod, aWhereIndices.FindIndex(anIterModif.Value()));
4495   }
4496
4497   //Add a new object
4498   Handle(GEOM_Object) aResult = GetEngine()->AddSubShape(theShapeWhere, aModifiedArray);
4499   if (aResult.IsNull()) {
4500     SetErrorCode("Error in algorithm: result found, but cannot be returned.");
4501     return NULL;
4502   }
4503
4504   const Standard_Boolean isSameType = (aShapeType != TopAbs_SHAPE);
4505
4506   if ((aModifiedArray->Length() > 1 && isSameType) ||
4507       theShapeWhat->GetType() == GEOM_GROUP) {
4508     //Set a GROUP type
4509     aResult->SetType(GEOM_GROUP);
4510
4511     //Set a sub-shape type
4512     TopoDS_Shape aFirstFound = aLSA.First();
4513     TopAbs_ShapeEnum aShapeType = aFirstFound.ShapeType();
4514
4515     TDF_Label aFreeLabel = aResult->GetFreeLabel();
4516     TDataStd_Integer::Set(aFreeLabel, (Standard_Integer)aShapeType);
4517   }
4518
4519   //Make a Python command
4520   Handle(GEOM_Function) aFunction = aResult->GetFunction(1);
4521
4522   GEOM::TPythonDump(aFunction) << aResult << " = geompy.GetInPlace("
4523     << theShapeWhere << ", " << theShapeWhat << ", True)";
4524
4525   SetErrorCode(OK);
4526   return aResult;
4527 }
4528
4529 //=============================================================================
4530 /*!
4531  *  case GetInPlaceOld:
4532  *  default:
4533  */
4534 //=============================================================================
4535 Handle(GEOM_Object) GEOMImpl_IShapesOperations::GetInPlaceOld
4536                             (Handle(GEOM_Object) theShapeWhere,
4537                              Handle(GEOM_Object) theShapeWhat)
4538 {
4539   SetErrorCode(KO);
4540
4541   if (theShapeWhere.IsNull() || theShapeWhat.IsNull()) return NULL;
4542
4543   TopoDS_Shape           aWhere = theShapeWhere->GetValue();
4544   TopoDS_Shape           aWhat  = theShapeWhat->GetValue();
4545   TopTools_ListOfShape   aModifiedList;
4546   const Standard_Integer iErr   =
4547     GEOMAlgo_GetInPlaceAPI::GetInPlaceOld(aWhere, aWhat, aModifiedList);
4548
4549   if (iErr) {
4550     switch (iErr) {
4551       case 1:
4552         SetErrorCode("Error: aWhere and aWhat TopoDS_Shape are Null.");
4553         break;
4554       case 2:
4555         SetErrorCode
4556           ("Error: An attempt to extract a shape of not supported type.");
4557         break;
4558       case 3:
4559         SetErrorCode(NOT_FOUND_ANY);
4560         break;
4561       default:
4562         SetErrorCode("Shape driver failed");
4563         break;
4564     }
4565
4566     return NULL;
4567   }
4568
4569   TopTools_IndexedMapOfShape aWhereIndices;
4570   TopExp::MapShapes(aWhere, aWhereIndices);
4571
4572   Handle(TColStd_HArray1OfInteger)   aModifiedArray =
4573     new TColStd_HArray1OfInteger (1, aModifiedList.Extent());
4574   TopTools_ListIteratorOfListOfShape anIterModif (aModifiedList);
4575   Standard_Integer                   imod;
4576   Standard_Integer                   aShapeType = -1;
4577
4578   for (imod = 1; anIterModif.More(); anIterModif.Next(), imod++) {
4579     const Standard_Integer anIndex =
4580       aWhereIndices.FindIndex(anIterModif.Value());
4581     const TopAbs_ShapeEnum aType   = anIterModif.Value().ShapeType();
4582
4583     if (aShapeType == -1) {
4584       // Initialization.
4585       aShapeType = aType;
4586     } else if (aShapeType != TopAbs_SHAPE && aShapeType != aType) {
4587       // Different types.
4588       aShapeType = TopAbs_SHAPE;
4589     }
4590
4591     aModifiedArray->SetValue(imod, anIndex);
4592   }
4593
4594   //Add a new object
4595   Handle(GEOM_Object) aResult =
4596     GetEngine()->AddSubShape(theShapeWhere, aModifiedArray);
4597
4598   if (aResult.IsNull()) {
4599     SetErrorCode("Error in algorithm: result found, but cannot be returned.");
4600     return NULL;
4601   }
4602
4603   const Standard_Boolean isSameType = (aShapeType != TopAbs_SHAPE);
4604
4605   if ((aModifiedArray->Length() > 1 && isSameType) ||
4606       theShapeWhat->GetType() == GEOM_GROUP) {
4607     //Set a GROUP type
4608     aResult->SetType(GEOM_GROUP);
4609
4610     //Set a sub-shape type
4611     TopoDS_Shape aFirstFound = aWhereIndices.FindKey(aModifiedArray->Value(1));
4612     TopAbs_ShapeEnum aShapeType = aFirstFound.ShapeType();
4613
4614     TDF_Label aFreeLabel = aResult->GetFreeLabel();
4615     TDataStd_Integer::Set(aFreeLabel, (Standard_Integer)aShapeType);
4616   }
4617
4618   //Make a Python command
4619   Handle(GEOM_Function) aFunction = aResult->GetFunction(1);
4620
4621   GEOM::TPythonDump(aFunction) << aResult << " = geompy.GetInPlace("
4622     << theShapeWhere << ", " << theShapeWhat << ", False)";
4623
4624   SetErrorCode(OK);
4625
4626   return aResult;
4627 }
4628
4629 //=======================================================================
4630 //function : GetInPlaceByHistory
4631 //purpose  :
4632 //=======================================================================
4633 Handle(GEOM_Object) GEOMImpl_IShapesOperations::GetInPlaceByHistory
4634                                           (Handle(GEOM_Object) theShapeWhere,
4635                                            Handle(GEOM_Object) theShapeWhat)
4636 {
4637   SetErrorCode(KO);
4638
4639   if (theShapeWhere.IsNull() || theShapeWhat.IsNull()) return NULL;
4640
4641   TopoDS_Shape aWhere = theShapeWhere->GetValue();
4642   TopoDS_Shape aWhat  = theShapeWhat->GetValue();
4643
4644   if (aWhere.IsNull() || aWhat.IsNull()) return NULL;
4645
4646   Handle(GEOM_Function) aWhereFunction = theShapeWhere->GetLastFunction();
4647   if (aWhereFunction.IsNull()) return NULL;
4648
4649   //Fill array of indices
4650   TopTools_IndexedMapOfShape aWhereIndices;
4651
4652   TopExp::MapShapes(aWhere, aWhereIndices);
4653
4654   // process shape
4655   TopTools_ListOfShape aModifiedList;
4656   bool isFound = GEOMAlgo_GetInPlaceAPI::GetInPlaceByHistory
4657     (aWhereFunction, aWhereIndices, aWhat, aModifiedList);
4658
4659   if (!isFound || aModifiedList.Extent() < 1) {
4660     SetErrorCode("Error: No history found for the sought shape or its sub-shapes.");
4661     return NULL;
4662   }
4663
4664   Handle(TColStd_HArray1OfInteger)   aModifiedArray =
4665     new TColStd_HArray1OfInteger (1, aModifiedList.Extent());
4666   TopTools_ListIteratorOfListOfShape anIterModif (aModifiedList);
4667   Standard_Integer                   imod;
4668   Standard_Integer                   aShapeType = -1;
4669
4670   for (imod = 1; anIterModif.More(); anIterModif.Next(), imod++) {
4671     const Standard_Integer anIndex =
4672       aWhereIndices.FindIndex(anIterModif.Value());
4673     const TopAbs_ShapeEnum aType   = anIterModif.Value().ShapeType();
4674
4675     if (aShapeType == -1) {
4676       // Initialization.
4677       aShapeType = aType;
4678     } else if (aShapeType != TopAbs_SHAPE && aShapeType != aType) {
4679       // Different types.
4680       aShapeType = TopAbs_SHAPE;
4681     }
4682
4683     aModifiedArray->SetValue(imod, anIndex);
4684   }
4685
4686   //Add a new object
4687   Handle(GEOM_Object) aResult = GetEngine()->AddSubShape(theShapeWhere, aModifiedArray);
4688   if (aResult.IsNull()) {
4689     SetErrorCode("Error in algorithm: result found, but cannot be returned.");
4690     return NULL;
4691   }
4692
4693   const Standard_Boolean isSameType = (aShapeType != TopAbs_SHAPE);
4694
4695   if ((aModifiedArray->Length() > 1 && isSameType) ||
4696       theShapeWhat->GetType() == GEOM_GROUP) {
4697     //Set a GROUP type
4698     aResult->SetType(GEOM_GROUP);
4699
4700     //Set a sub-shape type
4701     TopoDS_Shape aFirstFound = aWhereIndices.FindKey(aModifiedArray->Value(1));
4702     TopAbs_ShapeEnum aShapeType = aFirstFound.ShapeType();
4703
4704     TDF_Label aFreeLabel = aResult->GetFreeLabel();
4705     TDataStd_Integer::Set(aFreeLabel, (Standard_Integer)aShapeType);
4706   }
4707
4708   //Make a Python command
4709   Handle(GEOM_Function) aFunction = aResult->GetFunction(1);
4710
4711   GEOM::TPythonDump(aFunction) << aResult << " = geompy.GetInPlaceByHistory("
4712                                << theShapeWhere << ", " << theShapeWhat << ")";
4713
4714   SetErrorCode(OK);
4715   return aResult;
4716 }
4717
4718 //=======================================================================
4719 //function : isSameEdge
4720 //purpose  : Returns True if two edges coincide
4721 //=======================================================================
4722 static bool isSameEdge(const TopoDS_Edge& theEdge1, const TopoDS_Edge& theEdge2)
4723 {
4724   TopoDS_Vertex V11, V12, V21, V22;
4725   TopExp::Vertices(theEdge1, V11, V12);
4726   TopExp::Vertices(theEdge2, V21, V22);
4727   gp_Pnt P11 = BRep_Tool::Pnt(V11);
4728   gp_Pnt P12 = BRep_Tool::Pnt(V12);
4729   gp_Pnt P21 = BRep_Tool::Pnt(V21);
4730   gp_Pnt P22 = BRep_Tool::Pnt(V22);
4731   bool coincide = false;
4732
4733   //Check that ends of edges coincide
4734   if(P11.Distance(P21) <= MAX_TOLERANCE) {
4735     if(P12.Distance(P22) <= MAX_TOLERANCE) coincide =  true;
4736   }
4737   else if(P11.Distance(P22) <= MAX_TOLERANCE) {
4738     if(P12.Distance(P21) <= MAX_TOLERANCE) coincide = true;
4739   }
4740
4741   if(!coincide) return false;
4742
4743   if (BRep_Tool::Degenerated(theEdge1))
4744     if (BRep_Tool::Degenerated(theEdge2)) return true;
4745     else return false;
4746   else
4747     if (BRep_Tool::Degenerated(theEdge2)) return false;
4748
4749   double U11, U12, U21, U22;
4750   Handle(Geom_Curve) C1 = BRep_Tool::Curve(theEdge1, U11, U12);
4751   Handle(Geom_Curve) C2 = BRep_Tool::Curve(theEdge2, U21, U22);
4752
4753   //Check that both edges has the same geometry
4754   double range = U12-U11;
4755   double U = U11+ range/3.0;
4756   gp_Pnt P1 = C1->Value(U);     //Compute a point on one third of the edge's length
4757   U = U11+range*2.0/3.0;
4758   gp_Pnt P2 = C1->Value(U);     //Compute a point on two thirds of the edge's length
4759
4760   C2 = new Geom_TrimmedCurve(C2, U21, U22);
4761
4762   if(!GeomLib_Tool::Parameter(C2, P1, MAX_TOLERANCE, U) ||  U < U21 || U > U22)
4763     return false;
4764
4765   if(P1.Distance(C2->Value(U)) > MAX_TOLERANCE) return false;
4766
4767   if(!GeomLib_Tool::Parameter(C2, P2, MAX_TOLERANCE, U) || U < U21 || U > U22)
4768     return false;
4769
4770   if(P2.Distance(C2->Value(U)) > MAX_TOLERANCE) return false;
4771
4772   return true;
4773 }
4774
4775 //=======================================================================
4776 //function : isSameFace
4777 //purpose  : Returns True if two faces coincide
4778 //=======================================================================
4779 static bool isSameFace(const TopoDS_Face& theFace1, const TopoDS_Face& theFace2)
4780 {
4781   TopExp_Explorer E(theFace1, TopAbs_EDGE);
4782   TopTools_ListOfShape LS1, LS2;
4783   for(; E.More(); E.Next()) LS1.Append(E.Current());
4784
4785   E.Init(theFace2, TopAbs_EDGE);
4786   for(; E.More(); E.Next()) LS2.Append(E.Current());
4787
4788   //Compare the number of edges in the faces
4789   if(LS1.Extent() != LS2.Extent()) return false;
4790
4791   double aMin = RealFirst(), aMax = RealLast();
4792   double xminB1=aMax, yminB1=aMax, zminB1=aMax, xminB2=aMax, yminB2=aMax, zminB2=aMax;
4793   double xmaxB1=aMin, ymaxB1=aMin, zmaxB1=aMin, xmaxB2=aMin, ymaxB2=aMin, zmaxB2=aMin;
4794
4795   for(E.Init(theFace1, TopAbs_VERTEX); E.More(); E.Next()) {
4796     gp_Pnt P = BRep_Tool::Pnt(TopoDS::Vertex(E.Current()));
4797     if(P.X() < xminB1) xminB1 = P.X();
4798     if(P.Y() < yminB1) yminB1 = P.Y();
4799     if(P.Z() < zminB1) zminB1 = P.Z();
4800     if(P.X() > xmaxB1) xmaxB1 = P.X();
4801     if(P.Y() > ymaxB1) ymaxB1 = P.Y();
4802     if(P.Z() > zmaxB1) zmaxB1 = P.Z();
4803   }
4804
4805   for(E.Init(theFace2, TopAbs_VERTEX); E.More(); E.Next()) {
4806     gp_Pnt P = BRep_Tool::Pnt(TopoDS::Vertex(E.Current()));
4807     if(P.X() < xminB2) xminB2 = P.X();
4808     if(P.Y() < yminB2) yminB2 = P.Y();
4809     if(P.Z() < zminB2) zminB2 = P.Z();
4810     if(P.X() > xmaxB2) xmaxB2 = P.X();
4811     if(P.Y() > ymaxB2) ymaxB2 = P.Y();
4812     if(P.Z() > zmaxB2) zmaxB2 = P.Z();
4813   }
4814
4815   //Compare the bounding boxes of both faces
4816   if(gp_Pnt(xminB1, yminB1, zminB1).Distance(gp_Pnt(xminB2, yminB2, zminB2)) > MAX_TOLERANCE)
4817     return false;
4818
4819   if(gp_Pnt(xmaxB1, ymaxB1, zmaxB1).Distance(gp_Pnt(xmaxB2, ymaxB2, zmaxB2)) > MAX_TOLERANCE)
4820     return false;
4821
4822   Handle(Geom_Surface) S1 = BRep_Tool::Surface(theFace1);
4823   Handle(Geom_Surface) S2 = BRep_Tool::Surface(theFace2);
4824
4825   //Check if there a coincidence of two surfaces at least in two points
4826   double U11, U12, V11, V12, U21, U22, V21, V22;
4827   BRepTools::UVBounds(theFace1, U11, U12, V11, V12);
4828   BRepTools::UVBounds(theFace2, U21, U22, V21, V22);
4829
4830   double rangeU = U12-U11;
4831   double rangeV = V12-V11;
4832   double U = U11 + rangeU/3.0;
4833   double V = V11 + rangeV/3.0;
4834   gp_Pnt P1 = S1->Value(U, V);
4835   U = U11+rangeU*2.0/3.0;
4836   V = V11+rangeV*2.0/3.0;
4837   gp_Pnt P2 = S1->Value(U, V);
4838
4839   if (!GeomLib_Tool::Parameters(S2, P1, MAX_TOLERANCE, U, V) || U < U21 || U > U22 || V < V21 || V > V22)
4840     return false;
4841
4842   if (P1.Distance(S2->Value(U,V)) > MAX_TOLERANCE) return false;
4843
4844   if (!GeomLib_Tool::Parameters(S2, P2, MAX_TOLERANCE, U, V) || U < U21 || U > U22 || V < V21 || V > V22)
4845     return false;
4846
4847   if (P2.Distance(S2->Value(U, V)) > MAX_TOLERANCE) return false;
4848
4849   //Check that each edge of the Face1 has a counterpart in the Face2
4850   TopTools_MapOfOrientedShape aMap;
4851   TopTools_ListIteratorOfListOfShape LSI1(LS1);
4852   for(; LSI1.More(); LSI1.Next()) {
4853     TopoDS_Edge E = TopoDS::Edge(LSI1.Value());
4854     bool isFound = false;
4855     TopTools_ListIteratorOfListOfShape LSI2(LS2);
4856     for(; LSI2.More(); LSI2.Next()) {
4857       TopoDS_Shape aValue = LSI2.Value();
4858       if(aMap.Contains(aValue)) continue; //To avoid checking already found edge several times
4859       if(isSameEdge(E, TopoDS::Edge(aValue))) {
4860         aMap.Add(aValue);
4861         isFound = true;
4862         break;
4863       }
4864     }
4865     if(!isFound) return false;
4866   }
4867
4868   return true;
4869 }
4870
4871 //=======================================================================
4872 //function : isSameSolid
4873 //purpose  : Returns True if two solids coincide
4874 //=======================================================================
4875 bool isSameSolid(const TopoDS_Solid& theSolid1, const TopoDS_Solid& theSolid2)
4876 {
4877   TopExp_Explorer E(theSolid1, TopAbs_FACE);
4878   TopTools_ListOfShape LS1, LS2;
4879   for(; E.More(); E.Next()) LS1.Append(E.Current());
4880   E.Init(theSolid2, TopAbs_FACE);
4881   for(; E.More(); E.Next()) LS2.Append(E.Current());
4882
4883   if(LS1.Extent() != LS2.Extent()) return false;
4884
4885   double aMin = RealFirst(), aMax = RealLast();
4886   double xminB1=aMax, yminB1=aMax, zminB1=aMax, xminB2=aMax, yminB2=aMax, zminB2=aMax;
4887   double xmaxB1=aMin, ymaxB1=aMin, zmaxB1=aMin, xmaxB2=aMin, ymaxB2=aMin, zmaxB2=aMin;
4888
4889   for(E.Init(theSolid1, TopAbs_VERTEX); E.More(); E.Next()) {
4890     gp_Pnt P = BRep_Tool::Pnt(TopoDS::Vertex(E.Current()));
4891     if(P.X() < xminB1) xminB1 = P.X();
4892     if(P.Y() < yminB1) yminB1 = P.Y();
4893     if(P.Z() < zminB1) zminB1 = P.Z();
4894     if(P.X() > xmaxB1) xmaxB1 = P.X();
4895     if(P.Y() > ymaxB1) ymaxB1 = P.Y();
4896     if(P.Z() > zmaxB1) zmaxB1 = P.Z();
4897   }
4898
4899   for(E.Init(theSolid2, TopAbs_VERTEX); E.More(); E.Next()) {
4900     gp_Pnt P = BRep_Tool::Pnt(TopoDS::Vertex(E.Current()));
4901     if(P.X() < xminB2) xminB2 = P.X();
4902     if(P.Y() < yminB2) yminB2 = P.Y();
4903     if(P.Z() < zminB2) zminB2 = P.Z();
4904     if(P.X() > xmaxB2) xmaxB2 = P.X();
4905     if(P.Y() > ymaxB2) ymaxB2 = P.Y();
4906     if(P.Z() > zmaxB2) zmaxB2 = P.Z();
4907   }
4908
4909   //Compare the bounding boxes of both solids
4910   if(gp_Pnt(xminB1, yminB1, zminB1).Distance(gp_Pnt(xminB2, yminB2, zminB2)) > MAX_TOLERANCE)
4911     return false;
4912
4913   if(gp_Pnt(xmaxB1, ymaxB1, zmaxB1).Distance(gp_Pnt(xmaxB2, ymaxB2, zmaxB2)) > MAX_TOLERANCE)
4914     return false;
4915
4916   //Check that each face of the Solid1 has a counterpart in the Solid2
4917   TopTools_MapOfOrientedShape aMap;
4918   TopTools_ListIteratorOfListOfShape LSI1(LS1);
4919   for(; LSI1.More(); LSI1.Next()) {
4920     TopoDS_Face F = TopoDS::Face(LSI1.Value());
4921     bool isFound = false;
4922     TopTools_ListIteratorOfListOfShape LSI2(LS2);
4923     for(; LSI2.More(); LSI2.Next()) {
4924       if(aMap.Contains(LSI2.Value())) continue; //To avoid checking already found faces several times
4925       if(isSameFace(F, TopoDS::Face(LSI2.Value()))) {
4926         aMap.Add(LSI2.Value());
4927         isFound = true;
4928         break;
4929       }
4930     }
4931     if(!isFound) return false;
4932   }
4933
4934   return true;
4935 }
4936
4937 //=======================================================================
4938 //function : GetSame
4939 //purpose  :
4940 //=======================================================================
4941 Handle(GEOM_Object) GEOMImpl_IShapesOperations::GetSame(const Handle(GEOM_Object)& theShapeWhere,
4942                                                         const Handle(GEOM_Object)& theShapeWhat)
4943 {
4944   SetErrorCode(KO);
4945   if (theShapeWhere.IsNull() || theShapeWhat.IsNull()) return NULL;
4946
4947   TopoDS_Shape aWhere = theShapeWhere->GetValue();
4948   TopoDS_Shape aWhat  = theShapeWhat->GetValue();
4949
4950   if (aWhere.IsNull() || aWhat.IsNull()) return NULL;
4951
4952   int anIndex = -1;
4953   bool isFound = false;
4954   TopoDS_Shape aSubShape;
4955   TopTools_MapOfShape aMap;
4956
4957   if (aWhat.ShapeType() == TopAbs_COMPOUND || aWhat.ShapeType() == TopAbs_COMPSOLID) {
4958     TopoDS_Iterator It (aWhat, Standard_True, Standard_True);
4959     if (It.More()) aWhat = It.Value();
4960     It.Next();
4961     if (It.More()) {
4962       SetErrorCode("Compounds of two or more shapes are not allowed for aWhat argument");
4963       return NULL;
4964     }
4965   }
4966
4967   switch (aWhat.ShapeType()) {
4968     case TopAbs_VERTEX: {
4969       aSubShape = getSameVertex(aWhere, TopoDS::Vertex(aWhat));
4970       isFound   = !aSubShape.IsNull();
4971       break;
4972                         }
4973     case TopAbs_EDGE: {
4974       TopoDS_Edge anEdge = TopoDS::Edge(aWhat);
4975       TopExp_Explorer E(aWhere, TopAbs_EDGE);
4976       for(; E.More(); E.Next()) {
4977         if(!aMap.Add(E.Current())) continue;
4978         if(isSameEdge(anEdge, TopoDS::Edge(E.Current()))) {
4979           aSubShape = E.Current();
4980           isFound = true;
4981           break;
4982         }
4983       }
4984       break;
4985                       }
4986     case TopAbs_FACE: {
4987       TopoDS_Face aFace = TopoDS::Face(aWhat);
4988       TopExp_Explorer E(aWhere, TopAbs_FACE);
4989       for(; E.More(); E.Next()) {
4990         if(!aMap.Add(E.Current())) continue;
4991         if(isSameFace(aFace, TopoDS::Face(E.Current()))) {
4992           aSubShape = E.Current();
4993           isFound = true;
4994           break;
4995         }
4996       }
4997       break;
4998                       }
4999     case TopAbs_SOLID: {
5000       TopoDS_Solid aSolid = TopoDS::Solid(aWhat);
5001       TopExp_Explorer E(aWhere, TopAbs_SOLID);
5002       for(; E.More(); E.Next()) {
5003         if(!aMap.Add(E.Current())) continue;
5004         if(isSameSolid(aSolid, TopoDS::Solid(E.Current()))) {
5005           aSubShape = E.Current();
5006           isFound = true;
5007           break;
5008         }
5009       }
5010       break;
5011                        }
5012     default:
5013       return NULL;
5014   }
5015
5016   if (isFound) {
5017     TopTools_IndexedMapOfShape anIndices;
5018     TopExp::MapShapes(aWhere, anIndices);
5019     if (anIndices.Contains(aSubShape))
5020       anIndex = anIndices.FindIndex(aSubShape);
5021   }
5022
5023   if (anIndex < 0) return NULL;
5024
5025   Handle(TColStd_HArray1OfInteger) anArray = new TColStd_HArray1OfInteger(1,1);
5026
5027   anArray->SetValue(1, anIndex);
5028
5029   Handle(GEOM_Object) aResult = GetEngine()->AddSubShape(theShapeWhere, anArray);
5030   Handle(GEOM_Function) aFunction = aResult->GetLastFunction();
5031
5032   GEOM::TPythonDump(aFunction) << aResult << " = geompy.GetSame("
5033     << theShapeWhere << ", " << theShapeWhat << ")";
5034
5035   SetErrorCode(OK);
5036
5037   return aResult;
5038 }
5039
5040
5041 //=======================================================================
5042 //function : GetSameIDs
5043 //purpose  :
5044 //=======================================================================
5045 Handle(TColStd_HSequenceOfInteger) GEOMImpl_IShapesOperations::GetSameIDs
5046                                                        (const Handle(GEOM_Object)& theShapeWhere,
5047                                                         const Handle(GEOM_Object)& theShapeWhat)
5048 {
5049   SetErrorCode(KO);
5050   if (theShapeWhere.IsNull() || theShapeWhat.IsNull()) return NULL;
5051
5052   TopoDS_Shape aWhere = theShapeWhere->GetValue();
5053   TopoDS_Shape aWhat  = theShapeWhat->GetValue();
5054
5055   if (aWhere.IsNull() || aWhat.IsNull()) return NULL;
5056
5057   TopTools_ListOfShape listShape;
5058   TopTools_MapOfShape aMap;
5059
5060   if (aWhat.ShapeType() == TopAbs_COMPOUND || aWhat.ShapeType() == TopAbs_COMPSOLID) {
5061     TopoDS_Iterator It (aWhat, Standard_True, Standard_True);
5062     if (It.More()) aWhat = It.Value();
5063     It.Next();
5064     if (It.More()) {
5065       SetErrorCode("Compounds of two or more shapes are not allowed for aWhat argument");
5066       return NULL;
5067     }
5068   }
5069
5070   switch (aWhat.ShapeType()) {
5071     case TopAbs_VERTEX: {
5072       gp_Pnt P = BRep_Tool::Pnt(TopoDS::Vertex(aWhat));
5073       TopExp_Explorer E(aWhere, TopAbs_VERTEX);
5074       for(; E.More(); E.Next()) {
5075         if(!aMap.Add(E.Current())) continue;
5076         gp_Pnt P2 = BRep_Tool::Pnt(TopoDS::Vertex(E.Current()));
5077         if(P.Distance(P2) <= MAX_TOLERANCE) {
5078           listShape.Append(E.Current());
5079         }
5080       }
5081       break;
5082                         }
5083     case TopAbs_EDGE: {
5084       TopoDS_Edge anEdge = TopoDS::Edge(aWhat);
5085       TopExp_Explorer E(aWhere, TopAbs_EDGE);
5086       for(; E.More(); E.Next()) {
5087         if(!aMap.Add(E.Current())) continue;
5088         if(isSameEdge(anEdge, TopoDS::Edge(E.Current()))) {
5089           listShape.Append(E.Current());
5090         }
5091       }
5092       break;
5093                       }
5094     case TopAbs_FACE: {
5095       TopoDS_Face aFace = TopoDS::Face(aWhat);
5096       TopExp_Explorer E(aWhere, TopAbs_FACE);
5097       for(; E.More(); E.Next()) {
5098         if(!aMap.Add(E.Current())) continue;
5099         if(isSameFace(aFace, TopoDS::Face(E.Current()))) {
5100           listShape.Append(E.Current());
5101         }
5102       }
5103       break;
5104                       }
5105     case TopAbs_SOLID: {
5106       TopoDS_Solid aSolid = TopoDS::Solid(aWhat);
5107       TopExp_Explorer E(aWhere, TopAbs_SOLID);
5108       for(; E.More(); E.Next()) {
5109         if(!aMap.Add(E.Current())) continue;
5110         if(isSameSolid(aSolid, TopoDS::Solid(E.Current()))) {
5111           listShape.Append(E.Current());
5112         }
5113       }
5114       break;
5115                        }
5116     default:
5117       return NULL;
5118   }
5119
5120   if ( !listShape.IsEmpty() ) {
5121     TopTools_IndexedMapOfShape anIndices;
5122     TopExp::MapShapes(aWhere, anIndices);
5123     TopTools_ListIteratorOfListOfShape itSub (listShape);
5124     Handle(TColStd_HSequenceOfInteger) aSeq = new TColStd_HSequenceOfInteger;
5125     for (; itSub.More(); itSub.Next()) {
5126       if (anIndices.Contains(itSub.Value()))
5127         aSeq->Append(anIndices.FindIndex(itSub.Value()));
5128     }
5129     SetErrorCode(OK);
5130     // The GetSameIDs() doesn't change object so no new function is required.
5131     Handle(GEOM_Function) aFunction = GEOM::GetCreatedLast(theShapeWhere,theShapeWhat)->GetLastFunction();
5132
5133   // VSR 29/08/2017: 0023327, 0023428: eliminate unnecessary lines in Python dump
5134 #ifdef DUMP_SUBSHAPE_IDS
5135   // Make a Python command
5136   GEOM::TPythonDump(aFunction, /*append=*/true)
5137     << "listSameIDs = geompy.GetSameIDs("
5138     << theShapeWhere << ", "
5139     << theShapeWhat << ")";
5140 #endif // DUMP_SUBSHAPE_IDS
5141     return aSeq;
5142   } else {
5143     SetErrorCode(NOT_FOUND_ANY);
5144     return NULL;
5145   }
5146 }
5147
5148 //=======================================================================
5149 //function : ExtendEdge
5150 //purpose  :
5151 //=======================================================================
5152 Handle(GEOM_Object) GEOMImpl_IShapesOperations::ExtendEdge
5153                                       (const Handle(GEOM_Object) &theEdge,
5154                                        const Standard_Real        theMin,
5155                                        const Standard_Real        theMax)
5156 {
5157   SetErrorCode(KO);
5158
5159   if (theEdge.IsNull()) {
5160     return NULL;
5161   }
5162
5163   //Add a new Edge object
5164   Handle(GEOM_Object) aResEdge = GetEngine()->AddObject(GEOM_EDGE);
5165
5166   //Add a new Vector function
5167   Handle(GEOM_Function) aFunction =
5168     aResEdge->AddFunction(GEOMImpl_ShapeDriver::GetID(), EDGE_UV);
5169
5170   //Check if the function is set correctly
5171   if (aFunction->GetDriverGUID() != GEOMImpl_ShapeDriver::GetID()) {
5172     return NULL;
5173   }
5174
5175   GEOMImpl_IShapeExtend aCI (aFunction);
5176
5177   Handle(GEOM_Function) anEdge = theEdge->GetLastFunction();
5178
5179   if (anEdge.IsNull()) {
5180     return NULL;
5181   }
5182
5183   aCI.SetShape(anEdge);
5184   aCI.SetUMin(theMin);
5185   aCI.SetUMax(theMax);
5186
5187   //Compute the Edge value
5188   try {
5189     OCC_CATCH_SIGNALS;
5190     if (!GetSolver()->ComputeFunction(aFunction)) {
5191       SetErrorCode("Shape driver failed");
5192
5193       return NULL;
5194     }
5195   }
5196   catch (Standard_Failure& aFail) {
5197     SetErrorCode(aFail.GetMessageString());
5198
5199     return NULL;
5200   }
5201
5202   //Make a Python command
5203   GEOM::TPythonDump(aFunction)
5204              << aResEdge  << " = geompy.ExtendEdge("
5205              << theEdge << ", " << theMin << ", " << theMax << ")";
5206
5207   SetErrorCode(OK);
5208
5209   return aResEdge;
5210 }
5211
5212 //=======================================================================
5213 //function : ExtendFace
5214 //purpose  :
5215 //=======================================================================
5216 Handle(GEOM_Object) GEOMImpl_IShapesOperations::ExtendFace
5217                                       (const Handle(GEOM_Object) &theFace,
5218                                        const Standard_Real        theUMin,
5219                                        const Standard_Real        theUMax,
5220                                        const Standard_Real        theVMin,
5221                                        const Standard_Real        theVMax)
5222 {
5223   SetErrorCode(KO);
5224
5225   if (theFace.IsNull()) {
5226     return NULL;
5227   }
5228
5229   //Add a new Face object
5230   Handle(GEOM_Object) aResFace = GetEngine()->AddObject(GEOM_FACE);
5231
5232   //Add a new Vector function
5233   Handle(GEOM_Function) aFunction =
5234     aResFace->AddFunction(GEOMImpl_ShapeDriver::GetID(), FACE_UV);
5235
5236   //Check if the function is set correctly
5237   if (aFunction->GetDriverGUID() != GEOMImpl_ShapeDriver::GetID()) {
5238     return NULL;
5239   }
5240
5241   GEOMImpl_IShapeExtend aCI (aFunction);
5242
5243   Handle(GEOM_Function) aFace = theFace->GetLastFunction();
5244
5245   if (aFace.IsNull()) {
5246     return NULL;
5247   }
5248
5249   aCI.SetShape(aFace);
5250   aCI.SetUMin(theUMin);
5251   aCI.SetUMax(theUMax);
5252   aCI.SetVMin(theVMin);
5253   aCI.SetVMax(theVMax);
5254
5255   //Compute the Face value
5256   try {
5257     OCC_CATCH_SIGNALS;
5258     if (!GetSolver()->ComputeFunction(aFunction)) {
5259       SetErrorCode("Shape driver failed");
5260
5261       return NULL;
5262     }
5263   }
5264   catch (Standard_Failure& aFail) {
5265     SetErrorCode(aFail.GetMessageString());
5266
5267     return NULL;
5268   }
5269
5270   //Make a Python command
5271   GEOM::TPythonDump(aFunction)
5272              << aResFace  << " = geompy.ExtendFace("
5273              << theFace << ", " << theUMin << ", " << theUMax << ", "
5274              << theVMin << ", " << theVMax << ")";
5275
5276   SetErrorCode(OK);
5277
5278   return aResFace;
5279 }
5280
5281 //=======================================================================
5282 //function : MakeSurfaceFromFace
5283 //purpose  :
5284 //=======================================================================
5285 Handle(GEOM_Object) GEOMImpl_IShapesOperations::MakeSurfaceFromFace
5286                                       (const Handle(GEOM_Object) &theFace)
5287 {
5288   SetErrorCode(KO);
5289
5290   if (theFace.IsNull()) {
5291     return NULL;
5292   }
5293
5294   //Add a new Face object
5295   Handle(GEOM_Object) aResFace = GetEngine()->AddObject(GEOM_FACE);
5296
5297   //Add a new Vector function
5298   Handle(GEOM_Function) aFunction =
5299     aResFace->AddFunction(GEOMImpl_ShapeDriver::GetID(), SURFACE_FROM_FACE);
5300
5301   //Check if the function is set correctly
5302   if (aFunction->GetDriverGUID() != GEOMImpl_ShapeDriver::GetID()) {
5303     return NULL;
5304   }
5305
5306   GEOMImpl_IShapeExtend aCI (aFunction);
5307
5308   Handle(GEOM_Function) aFace = theFace->GetLastFunction();
5309
5310   if (aFace.IsNull()) {
5311     return NULL;
5312   }
5313
5314   aCI.SetShape(aFace);
5315
5316   //Compute the Face value
5317   try {
5318     OCC_CATCH_SIGNALS;
5319     if (!GetSolver()->ComputeFunction(aFunction)) {
5320       SetErrorCode("Shape driver failed");
5321
5322       return NULL;
5323     }
5324   }
5325   catch (Standard_Failure& aFail) {
5326     SetErrorCode(aFail.GetMessageString());
5327
5328     return NULL;
5329   }
5330
5331   //Make a Python command
5332   GEOM::TPythonDump(aFunction)
5333              << aResFace  << " = geompy.MakeSurfaceFromFace("
5334              << theFace << ")";
5335
5336   SetErrorCode(OK);
5337
5338   return aResFace;
5339 }