Salome HOME
Added some try catch
[modules/geom.git] / src / GEOMImpl / GEOMImpl_IAdvancedOperations.cxx
1 // Copyright (C) 2007-2011  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 //  File   : GEOMImpl_IAdvancedOperations.cxx
20 //  Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
21
22 #include <Standard_Stream.hxx>
23
24 #include "GEOMImpl_Types.hxx"
25 #include "GEOMImpl_IAdvancedOperations.hxx"
26 #include "GEOMImpl_IBasicOperations.hxx"
27 #include "GEOMImpl_IBooleanOperations.hxx"
28 #include "GEOMImpl_IShapesOperations.hxx"
29 #include "GEOMImpl_ITransformOperations.hxx"
30 #include "GEOMImpl_IBlocksOperations.hxx"
31 #include "GEOMImpl_I3DPrimOperations.hxx"
32 #include "GEOMImpl_ILocalOperations.hxx"
33 #include "GEOMImpl_IHealingOperations.hxx"
34
35 #include "GEOMImpl_Gen.hxx"
36
37 #include <Basics_OCCTVersion.hxx>
38
39 #include <utilities.h>
40 #include <OpUtil.hxx>
41 #include <Utils_ExceptHandlers.hxx>
42
43 #include "GEOM_Function.hxx"
44 #include "GEOM_PythonDump.hxx"
45
46 #include "GEOMImpl_PipeTShapeDriver.hxx"
47 #include "GEOMImpl_IPipeTShape.hxx"
48 /*@@ insert new functions before this line @@ do not remove this line @@ do not remove this line @@*/
49
50 #include <TopExp.hxx>
51 #include <TopExp_Explorer.hxx>
52 #include <TopoDS.hxx>
53 #include <TopoDS_Vertex.hxx>
54 #include <TopTools_IndexedMapOfShape.hxx>
55
56 #include <gp_Pnt.hxx>
57 #include <gp_Vec.hxx>
58 #include <gp_Ax3.hxx>
59 #include <BRepBuilderAPI_Transform.hxx>
60 #include <BRep_Tool.hxx>
61 #include <cmath>
62
63 #include <TFunction_DriverTable.hxx>
64 #include <TFunction_Driver.hxx>
65 #include <TFunction_Logbook.hxx>
66 #include <TDF_Tool.hxx>
67 #include <Standard_Failure.hxx>
68 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
69
70 #define HALF_LENGTH_MAIN_PIPE     "Main pipe half length" //"Tuyau principal - demi longueur"
71 #define HALF_LENGTH_INCIDENT_PIPE "Incident pipe half length" //"Tuyau incident - demi longueur"
72 #define CIRCULAR_QUARTER_PIPE     "Circular quarter of pipe" //"Circulaire - quart de tuyau"
73 #define THICKNESS                 "Thickness" //"Epaisseur"
74 #define FLANGE                    "Flange" // "Collerette"
75 #define CHAMFER_OR_FILLET         "Chamfer or fillet" //"Chanfrein ou Raccord"
76 #define JUNCTION_FACE_1           "Junction 1" //"Face de jonction 1"
77 #define JUNCTION_FACE_2           "Junction 2" //"Face de jonction 2"
78 #define JUNCTION_FACE_3           "Junction 3" //"Face de jonction 3"
79
80 //=============================================================================
81 /*!
82  *  Constructor
83  */
84 //=============================================================================
85 GEOMImpl_IAdvancedOperations::GEOMImpl_IAdvancedOperations(GEOM_Engine* theEngine, int theDocID) :
86   GEOM_IOperations(theEngine, theDocID)
87 {
88   MESSAGE("GEOMImpl_IAdvancedOperations::GEOMImpl_IAdvancedOperations");
89   myBasicOperations     = new GEOMImpl_IBasicOperations(GetEngine(), GetDocID());
90   myBooleanOperations   = new GEOMImpl_IBooleanOperations(GetEngine(), GetDocID());
91   myShapesOperations    = new GEOMImpl_IShapesOperations(GetEngine(), GetDocID());
92   myTransformOperations = new GEOMImpl_ITransformOperations(GetEngine(), GetDocID());
93   myBlocksOperations    = new GEOMImpl_IBlocksOperations(GetEngine(), GetDocID());
94   my3DPrimOperations    = new GEOMImpl_I3DPrimOperations(GetEngine(), GetDocID());
95   myLocalOperations     = new GEOMImpl_ILocalOperations(GetEngine(), GetDocID());
96   myHealingOperations   = new GEOMImpl_IHealingOperations(GetEngine(), GetDocID());
97 }
98
99 //=============================================================================
100 /*!
101  *  Destructor
102  */
103 //=============================================================================
104 GEOMImpl_IAdvancedOperations::~GEOMImpl_IAdvancedOperations()
105 {
106   MESSAGE("GEOMImpl_IAdvancedOperations::~GEOMImpl_IAdvancedOperations");
107   delete myBasicOperations;
108   delete myBooleanOperations;
109   delete myShapesOperations;
110   delete myTransformOperations;
111   delete myBlocksOperations;
112   delete my3DPrimOperations;
113   delete myLocalOperations;
114   delete myHealingOperations;
115 }
116
117 //=============================================================================
118 /*!
119  *  SetPosition
120  */
121 //=============================================================================
122 gp_Trsf GEOMImpl_IAdvancedOperations::GetPositionTrsf(double theL1, double theL2,
123                                                       Handle(GEOM_Object) theP1,
124                                                       Handle(GEOM_Object) theP2,
125                                                       Handle(GEOM_Object) theP3)
126 {
127   // Old Local Coordinates System oldLCS
128   gp_Pnt P0(0, 0, 0);
129   gp_Pnt P1(-theL1, 0, 0);
130   gp_Pnt P2(theL1, 0, 0);
131   gp_Pnt P3(0, 0, theL2);
132
133   gp_Dir oldX(gp_Vec(P1, P2));
134   gp_Dir oldZ(gp_Vec(P0, P3));
135   gp_Ax3 oldLCS(P0, oldZ, oldX);
136
137   // New Local Coordinates System newLCS
138   double LocX, LocY, LocZ;
139   gp_Pnt newP1 = BRep_Tool::Pnt(TopoDS::Vertex(theP1->GetValue()));
140   gp_Pnt newP2 = BRep_Tool::Pnt(TopoDS::Vertex(theP2->GetValue()));
141   gp_Pnt newP3 = BRep_Tool::Pnt(TopoDS::Vertex(theP3->GetValue()));
142   LocX = (newP1.X() + newP2.X()) / 2.;
143   LocY = (newP1.Y() + newP2.Y()) / 2.;
144   LocZ = (newP1.Z() + newP2.Z()) / 2.;
145   gp_Pnt newO(LocX, LocY, LocZ);
146
147   gp_Dir newX(gp_Vec(newP1, newP2)); // P1P2 Vector
148   gp_Dir newZ(gp_Vec(newO, newP3)); // OP3 Vector
149   gp_Ax3 newLCS = gp_Ax3(newO, newZ, newX);
150
151   gp_Trsf aTrsf;
152   aTrsf.SetDisplacement(oldLCS, newLCS);
153
154   return aTrsf;
155 }
156
157 //=============================================================================
158 /*!
159  *  CheckCompatiblePosition
160  *
161  */
162 //=============================================================================
163 bool GEOMImpl_IAdvancedOperations::CheckCompatiblePosition(double& theL1, double& theL2,
164                                                            Handle(GEOM_Object) theP1,
165                                                            Handle(GEOM_Object) theP2,
166                                                            Handle(GEOM_Object) theP3,
167                                                            double theTolerance)
168 {
169   SetErrorCode(KO);
170   gp_Pnt P1 = BRep_Tool::Pnt(TopoDS::Vertex(theP1->GetValue()));
171   gp_Pnt P2 = BRep_Tool::Pnt(TopoDS::Vertex(theP2->GetValue()));
172   gp_Pnt P3 = BRep_Tool::Pnt(TopoDS::Vertex(theP3->GetValue()));
173
174   double d12 = P1.Distance(P2);
175   double d13 = P1.Distance(P3);
176   double d23 = P2.Distance(P3);
177   //    double d2 = newO.Distance(P3);
178
179   if (Abs(d12) <= Precision::Confusion()) {
180     SetErrorCode("Junctions points P1 and P2 are identical");
181     return false;
182   }
183   if (Abs(d13) <= Precision::Confusion()) {
184     SetErrorCode("Junctions points P1 and P3 are identical");
185     return false;
186   }
187   if (Abs(d23) <= Precision::Confusion()) {
188     SetErrorCode("Junctions points P2 and P3 are identical");
189     return false;
190   }
191
192
193   double newL1 = 0.5 * d12;
194   double newL2 = sqrt(pow(d13,2)-pow(newL1,2));
195   //
196   // theL1*(1-theTolerance) <= newL1 <= theL1*(1+theTolerance)
197   //
198   if (fabs(newL1 - theL1) > Precision::Approximation()) {
199     if ( (newL1 * (1 - theTolerance) -theL1 <= Precision::Approximation()) &&
200          (newL1 * (1 + theTolerance) -theL1 >= Precision::Approximation()) ) {
201       //            std::cerr << "theL1 = newL1" << std::endl;
202       theL1 = newL1;
203     } else {
204       theL1 = -1;
205       SetErrorCode("Dimension for main pipe (L1) is incompatible with new position");
206       return false;
207     }
208   }
209
210   //
211   // theL2*(1-theTolerance) <= newL2  <= theL2*(1+theTolerance)
212   //
213   if (fabs(newL2 - theL2) > Precision::Approximation()) {
214     if ( (newL2 * (1 - theTolerance) -theL2 <= Precision::Approximation()) &&
215          (newL2 * (1 + theTolerance) -theL2 >= Precision::Approximation()) ) {
216       theL2 = newL2;
217     } else {
218       theL2 = -1;
219       SetErrorCode("Dimension for incident pipe (L2) is incompatible with new position");
220       return false;
221     }
222   }
223
224   SetErrorCode(OK);
225   return true;
226
227 }
228
229 //=============================================================================
230 /*!
231  *  Generate the propagation groups of a Pipe T-Shape used for hexa mesh
232  */
233 //=============================================================================
234 bool GEOMImpl_IAdvancedOperations::MakeGroups(Handle(GEOM_Object) theShape, int shapeType,
235                                               double theR1, double theW1, double theL1,
236                                               double theR2, double theW2, double theL2,
237                                               Handle(TColStd_HSequenceOfTransient) theSeq,
238                                               gp_Trsf aTrsf)
239 {
240   SetErrorCode(KO);
241
242   if (theShape.IsNull()) return false;
243
244   TopoDS_Shape aShape = theShape->GetValue();
245   if (aShape.IsNull()) {
246     SetErrorCode("Shape is not defined");
247     return false;
248   }
249
250   gp_Trsf aTrsfInv = aTrsf.Inverted();
251
252 //   int expectedGroups = 0;
253 //   if (shapeType == TSHAPE_BASIC)
254 //     if (Abs(theR2+theW2-theR1-theW1) <= Precision::Approximation())
255 //       expectedGroups = 10;
256 //     else
257 //       expectedGroups = 11;
258 //   else if (shapeType == TSHAPE_CHAMFER || shapeType == TSHAPE_FILLET)
259 //     expectedGroups = 12;
260
261   double aR1Ext = theR1 + theW1;
262   double aR2Ext = theR2 + theW2;
263
264   /////////////////////////
265   //// Groups of Faces ////
266   /////////////////////////
267
268   //
269   // Comment the following lines when GetInPlace bug is solved
270   // == BEGIN
271   // Workaround of GetInPlace bug
272   // Create a bounding box that fits the shape
273   Handle(GEOM_Object) aBox = my3DPrimOperations->MakeBoxDXDYDZ(2*theL1, 2*aR1Ext, aR1Ext+theL2);
274   aBox->GetLastFunction()->SetDescription("");
275   myTransformOperations->TranslateDXDYDZ(aBox, -theL1, -aR1Ext, -aR1Ext);
276   aBox->GetLastFunction()->SetDescription("");
277   // Apply transformation to box
278   BRepBuilderAPI_Transform aTransformationBox(aBox->GetValue(), aTrsf, Standard_False);
279   TopoDS_Shape aBoxShapeTrsf = aTransformationBox.Shape();
280   aBox->GetLastFunction()->SetValue(aBoxShapeTrsf);
281
282   // Get the shell of the box
283   Handle(GEOM_Object) aShell = Handle(GEOM_Object)::DownCast
284     (myShapesOperations->MakeExplode(aBox, TopAbs_SHELL, true)->Value(1));
285   aBox->GetLastFunction()->SetDescription("");
286   aShell->GetLastFunction()->SetDescription("");
287   // Get the common shapes between shell and shape
288   Handle(GEOM_Object) aCommonCompound = myBooleanOperations->MakeBoolean (theShape, aShell, 1); // MakeCommon
289   if (aCommonCompound.IsNull()) {
290     SetErrorCode(myBooleanOperations->GetErrorCode());
291     return false;
292   }
293   aCommonCompound->GetLastFunction()->SetDescription("");
294   // Explode the faces of common shapes => 3 faces
295   Handle(TColStd_HSequenceOfTransient) aCommonFaces =
296     myShapesOperations->MakeExplode(aCommonCompound, TopAbs_FACE, true);
297   aCommonCompound->GetLastFunction()->SetDescription("");
298   std::list<Handle(GEOM_Object)> aCompoundOfFacesList;
299
300   for (int i=0 ; i<= aCommonFaces->Length()-4 ; i+=4) {
301     std::list<Handle(GEOM_Object)> aFacesList;
302     for (int j = 1 ; j <= 4 ; j++) {
303       Handle(GEOM_Object) aFace = Handle(GEOM_Object)::DownCast(aCommonFaces->Value(i+j)); // Junction faces
304       if (!aFace.IsNull()) {
305         aFace->GetLastFunction()->SetDescription("");
306         aFacesList.push_back(aFace);
307       }
308     }
309     Handle(GEOM_Object) aCompoundOfFaces = myShapesOperations->MakeCompound(aFacesList);
310     if (!aCompoundOfFaces.IsNull()) {
311       aCompoundOfFaces->GetLastFunction()->SetDescription("");
312       aCompoundOfFacesList.push_back(aCompoundOfFaces);
313     }
314   }
315
316   if (aCompoundOfFacesList.size() == 3) {
317     Handle(GEOM_Object) aPln1 = aCompoundOfFacesList.front();
318     aCompoundOfFacesList.pop_front();
319     Handle(GEOM_Object) aPln2 = aCompoundOfFacesList.front();
320     aCompoundOfFacesList.pop_front();
321     Handle(GEOM_Object) aPln3 = aCompoundOfFacesList.front();
322     aCompoundOfFacesList.pop_front();
323     // == END
324     //
325
326
327     //     Uncomment the following lines when GetInPlace bug is solved
328     //     == BEGIN
329 //     Handle(GEOM_Object) aP1 = myBasicOperations->MakePointXYZ(-theL1, 0, 0);
330 //     Handle(GEOM_Object) aP2 = myBasicOperations->MakePointXYZ(-0, 0, theL2);
331 //     Handle(GEOM_Object) aP3 = myBasicOperations->MakePointXYZ(theL1, 0, 0);
332 //     aP1->GetLastFunction()->SetDescription("");
333 //     aP2->GetLastFunction()->SetDescription("");
334 //     aP3->GetLastFunction()->SetDescription("");
335 //     Handle(GEOM_Object) aV1 = myBasicOperations->MakeVectorDXDYDZ(-1, 0, 0);
336 //     Handle(GEOM_Object) aV2 = myBasicOperations->MakeVectorDXDYDZ(0, 0, 1);
337 //     Handle(GEOM_Object) aV3 = myBasicOperations->MakeVectorDXDYDZ(1, 0, 0);
338 //     aV1->GetLastFunction()->SetDescription("");
339 //     aV2->GetLastFunction()->SetDescription("");
340 //     aV3->GetLastFunction()->SetDescription("");
341 //     Handle(GEOM_Object) aPln1 = myBasicOperations->MakePlanePntVec(aP1, aV1, 2*(aR1Ext+theL2));
342 //     Handle(GEOM_Object) aPln2 = myBasicOperations->MakePlanePntVec(aP2, aV2, 2*(aR2Ext));
343 //     Handle(GEOM_Object) aPln3 = myBasicOperations->MakePlanePntVec(aP3, aV3, 2*(aR1Ext+theL2));
344 //     aPln1->GetLastFunction()->SetDescription("");
345 //     aPln2->GetLastFunction()->SetDescription("");
346 //     aPln3->GetLastFunction()->SetDescription("");
347 //
348 //     BRepBuilderAPI_Transform aTransformation1(aPln1->GetValue(), aTrsf, Standard_False);
349 //     TopoDS_Shape aTrsf_Shape1 = aTransformation1.Shape();
350 //     aPln1->GetLastFunction()->SetValue(aTrsf_Shape1);
351 //     BRepBuilderAPI_Transform aTransformation2(aPln2->GetValue(), aTrsf, Standard_False);
352 //     TopoDS_Shape aTrsf_Shape2 = aTransformation2.Shape();
353 //     aPln2->GetLastFunction()->SetValue(aTrsf_Shape2);
354 //     BRepBuilderAPI_Transform aTransformation3(aPln3->GetValue(), aTrsf, Standard_False);
355 //     TopoDS_Shape aTrsf_Shape3 = aTransformation3.Shape();
356 //     aPln3->GetLastFunction()->SetValue(aTrsf_Shape3);
357     //     == END
358     //
359
360     Handle(GEOM_Object) junctionFaces1 = myShapesOperations->GetInPlace(theShape, aPln1);
361     if (junctionFaces1.IsNull())
362       junctionFaces1 = myShapesOperations->GetShapesOnShapeAsCompound
363         (aPln1, theShape, TopAbs_FACE,  GEOMAlgo_ST_ONIN);
364     if (!junctionFaces1.IsNull()) {
365       junctionFaces1->GetLastFunction()->SetDescription("");
366       junctionFaces1->SetName("JUNCTION_FACE_1");
367       theSeq->Append(junctionFaces1);
368     }
369     else {
370       SetErrorCode("Junction face 1 not found");
371       //        theSeq->Append(aPln1);
372       //        return false;
373     }
374     Handle(GEOM_Object) junctionFaces2 = myShapesOperations->GetInPlace(theShape, aPln2);
375     if (junctionFaces2.IsNull())
376       junctionFaces2 = myShapesOperations->GetShapesOnShapeAsCompound
377         (aPln2, theShape, TopAbs_FACE,  GEOMAlgo_ST_ONIN);
378     if (!junctionFaces2.IsNull()) {
379       junctionFaces2->GetLastFunction()->SetDescription("");
380       junctionFaces2->SetName("JUNCTION_FACE_2");
381       theSeq->Append(junctionFaces2);
382     }
383     else {
384       SetErrorCode("Junction face 2 not found");
385       //        theSeq->Append(aPln2);
386       //        return false;
387     }
388     Handle(GEOM_Object) junctionFaces3 = myShapesOperations->GetInPlace(theShape, aPln3);
389     if (junctionFaces3.IsNull())
390       junctionFaces3 = myShapesOperations->GetShapesOnShapeAsCompound
391         (aPln3, theShape, TopAbs_FACE,  GEOMAlgo_ST_ONIN);
392     if (!junctionFaces3.IsNull()) {
393       junctionFaces3->GetLastFunction()->SetDescription("");
394       junctionFaces3->SetName("JUNCTION_FACE_3");
395       theSeq->Append(junctionFaces3);
396     }
397     else {
398       SetErrorCode("Junction face 3 not found");
399       //        theSeq->Append(aPln3);
400       //        return false;
401     }
402   // Comment the following lines when GetInPlace bug is solved
403   // == BEGIN
404   }
405   //     == END
406   /////////////////////////
407   //// Groups of Edges ////
408   /////////////////////////
409   // Result of propagate
410
411   Handle(GEOM_Function) aFunction = theShape->GetLastFunction();
412
413   TCollection_AsciiString theDesc = aFunction->GetDescription();
414   Handle(TColStd_HSequenceOfTransient) aSeqPropagate = myBlocksOperations->Propagate(theShape);
415   if (aSeqPropagate.IsNull() || aSeqPropagate->Length() == 0) {
416     SetErrorCode("Propagation groups not found");
417     return false;
418   }
419   Standard_Integer nbEdges, aNbGroups = aSeqPropagate->Length();
420   // Recover previous description to get rid of Propagate dump
421   aFunction->SetDescription(theDesc);
422
423   bool addGroup;
424   bool circularFoundAndAdded = false;
425   bool circularFound10 = false;
426   bool incidentPipeFound = false;
427   bool mainPipeFound = false;
428   bool mainPipeFoundAndAdded = false;
429   bool radialFound =false;
430   bool flangeFound = false;
431   bool flangeFoundAndAdded = false;
432   bool chamferOrFilletFound = false;
433
434   for (int i=1 ; i<= aNbGroups; i++) {
435     addGroup = false;
436
437     Handle(GEOM_Object) aGroup = Handle(GEOM_Object)::DownCast(aSeqPropagate->Value(i));
438     if(aGroup.IsNull())
439       continue;
440
441     TopoDS_Shape aGroupShape = aGroup->GetValue();
442     BRepBuilderAPI_Transform aTransformationShapeInv(aGroupShape, aTrsfInv, Standard_False);
443     TopoDS_Shape aGroupShapeTrsfInv = aTransformationShapeInv.Shape();
444
445     TopTools_IndexedMapOfShape anEdgesMap;
446     TopExp::MapShapes(aGroupShapeTrsfInv,TopAbs_EDGE, anEdgesMap);
447     nbEdges = anEdgesMap.Extent();
448
449     if (shapeType == TSHAPE_BASIC) {
450       if ((nbEdges == 21) || /*R1Ext = R2Ext*/(nbEdges == 17)){
451         addGroup = true;
452         aGroup->SetName("THICKNESS");
453       }
454       else if (nbEdges == 6) {
455         if (!circularFoundAndAdded) {
456           circularFoundAndAdded = true;
457           addGroup = true;
458           aGroup->SetName("CIRCULAR_QUARTER_PIPE");
459         }
460       }
461       else if (nbEdges == 8) {
462         incidentPipeFound = true;
463         mainPipeFound = false;
464         radialFound =false;
465         flangeFound = false;
466
467         TopExp_Explorer Ex(aGroupShapeTrsfInv,TopAbs_VERTEX);
468         while (Ex.More()) {
469           gp_Pnt aP =  BRep_Tool::Pnt(TopoDS::Vertex(Ex.Current()));
470           double x=aP.X(), y=aP.Y(), z=aP.Z();
471
472
473           if ((Abs(x) > aR2Ext + Precision::Confusion()) ||
474               (Abs(y) > aR2Ext + Precision::Confusion())) {
475             incidentPipeFound = false;
476           }
477
478           if ( z < -Precision::Confusion()) {
479             // length of main pipe
480             mainPipeFound = true;
481             if (!mainPipeFoundAndAdded) {
482               mainPipeFoundAndAdded = true;
483               addGroup = true;
484               aGroup->SetName("HALF_LENGTH_MAIN_PIPE");
485             }
486           }
487
488           else if (Abs(x) > (theL1-Precision::Confusion())) {
489             // discretisation circulaire
490             radialFound = true;
491             if (!circularFoundAndAdded) {
492               circularFoundAndAdded = true;
493               addGroup = true;
494               aGroup->SetName("CIRCULAR_QUARTER_PIPE");
495             }
496           }
497           Ex.Next();
498         }
499         if (incidentPipeFound) {
500           addGroup = true;
501           aGroup->SetName("HALF_LENGTH_INCIDENT_PIPE");
502         }
503         if (!addGroup && (!incidentPipeFound &&
504                           !radialFound &&
505                           !mainPipeFound &&
506                           !flangeFound)) {
507           // Flange (collerette)
508           flangeFound = true;
509           addGroup = true;
510           aGroup->SetName("FLANGE");
511         }
512       }
513       else
514         continue;
515     }
516     else if (shapeType == TSHAPE_CHAMFER || shapeType == TSHAPE_FILLET) {
517       if (nbEdges == 25) {
518         addGroup = true;
519         aGroup->SetName("THICKNESS");
520       }
521       else if ((nbEdges == 10) || (nbEdges == 6)) {
522         if (!circularFoundAndAdded) {
523           addGroup = true;
524           circularFoundAndAdded = true;
525           aGroup->SetName("CIRCULAR_QUARTER_PIPE");
526           if (nbEdges == 10) {
527             circularFound10 = true;
528           }
529         }
530         else if (!circularFound10 && nbEdges == 10) {
531           circularFound10 = true;
532           addGroup = true;
533           aGroup->SetName("CIRCULAR_QUARTER_PIPE");
534         }
535       }
536       else if (nbEdges == 8) {
537         incidentPipeFound = true;
538         mainPipeFound = true;
539         flangeFound = false;
540
541         bool isNearZ0 = false;
542         bool isBelowZ0 = false;
543
544         TopExp_Explorer Ex (aGroupShapeTrsfInv,TopAbs_VERTEX);
545         while (Ex.More()) {
546           gp_Pnt aP = BRep_Tool::Pnt(TopoDS::Vertex(Ex.Current()));
547           double x=aP.X(), y=aP.Y(), z=aP.Z();
548
549           // tuy_princ_long_avant & tuy_princ_long_apres
550           //bool isMain = (((z < Precision::Confusion()) || (x < Precision::Confusion())) &&
551           //               ((y <= aR1Ext + Precision::Confusion()) ||
552           //                (y <= -(aR1Ext + Precision::Confusion())) ||
553           //                (y <= theR1 + Precision::Confusion()) ||
554           //                (y == -(theR1 + Precision::Confusion()))));
555           bool isMain = ((z < Precision::Confusion() || x < Precision::Confusion()) &&
556                          (fabs(y) > theR1 - Precision::Confusion() ||
557                           fabs(y) < Precision::Confusion()));
558
559           if (!isMain) {
560             mainPipeFound = false;
561           }
562
563           // collerette
564           //if (z < Precision::Confusion() && !isMain) {
565           //  flangeFound = true;
566           //  if (!flangeFoundAndAdded) {
567           //    flangeFoundAndAdded = true;
568           //    addGroup = true;
569           //    aGroup->SetName("FLANGE");
570           //  }
571           //}
572           if (fabs(z) < Precision::Confusion()) isNearZ0 = true;
573           if (z < - Precision::Confusion()) isBelowZ0 = true;
574
575           // tuyau incident
576           if ((Abs(x) > aR2Ext + Precision::Confusion()) ||
577               (Abs(y) > aR2Ext + Precision::Confusion())) {
578             incidentPipeFound = false;
579           }
580           Ex.Next();
581         }
582         if (mainPipeFound) {
583           addGroup = true;
584           aGroup->SetName("HALF_LENGTH_MAIN_PIPE");
585         }
586         if (incidentPipeFound) {
587           addGroup = true;
588           aGroup->SetName("HALF_LENGTH_INCIDENT_PIPE");
589         }
590         if (isNearZ0 && !isBelowZ0) {
591           flangeFound = true;
592           if (!flangeFoundAndAdded) {
593             flangeFoundAndAdded = true;
594             addGroup = true;
595             aGroup->SetName("FLANGE");
596           }
597         }
598         if (!addGroup && (!incidentPipeFound &&
599                           !mainPipeFound &&
600                           !flangeFound &&
601                           !chamferOrFilletFound)) {
602           addGroup = true;
603           chamferOrFilletFound = true;
604           if (shapeType == TSHAPE_CHAMFER)
605             aGroup->SetName("CHAMFER");
606           else
607             aGroup->SetName("FILLET");
608         }
609       }
610       else
611         continue;
612     }
613     // Add group to the list
614     if (addGroup)
615       theSeq->Append(aGroup);
616   }
617
618   SetErrorCode(OK);
619   return true;
620 }
621
622 bool GEOMImpl_IAdvancedOperations::MakePipeTShapePartition(Handle(GEOM_Object) theShape,
623                                                            double theR1, double theW1, double theL1,
624                                                            double theR2, double theW2, double theL2,
625                                                            double theH, double theW,
626                                                            double theRF, bool isNormal)
627 {
628   SetErrorCode(KO);
629
630   // Build tools for partition operation:
631   // 1 face and 2 planes
632   // Face
633   Handle(GEOM_Object) arete_intersect_int;
634   Handle(GEOM_Object) wire_t, wire_t2, face_t, face_t2;
635   Handle(GEOM_Object) chan_racc;
636   Handle(GEOM_Object) vi1, vi2;
637   Handle(GEOM_Object) Te3;
638
639   try {
640 #if OCC_VERSION_LARGE > 0x06010000
641     OCC_CATCH_SIGNALS;
642 #endif
643     Handle(GEOM_Object) Vector_Z = myBasicOperations->MakeVectorDXDYDZ(0, 0, 1);
644     Vector_Z->GetLastFunction()->SetDescription("");
645
646     // Useful values
647     double aSize = 2*(theL1 + theL2);
648     double aR1Ext = theR1 + theW1;
649     double aR2Ext = theR2 + theW2;
650     double theVertCylinderRadius = aR2Ext + theW + theRF;
651     double theHoriCylinderRadius = aR1Ext + theH + theRF;
652
653     // Common edges on internal cylinder
654     Handle(GEOM_Object) box_i = my3DPrimOperations->MakeBoxDXDYDZ(theR2, theR2, theR1);
655     box_i->GetLastFunction()->SetDescription("");
656     box_i = myTransformOperations->TranslateDXDYDZ(box_i, -theR2, -theR2, 0);
657     box_i->GetLastFunction()->SetDescription("");
658
659     Handle(GEOM_Function) aFunction = theShape->GetLastFunction();
660     TCollection_AsciiString theDesc = aFunction->GetDescription();
661     Handle(TColStd_HSequenceOfTransient) edges_i =
662       myShapesOperations->GetShapesOnBox(box_i, theShape, TopAbs_EDGE, GEOMAlgo_ST_IN);
663     // Recover previous description to get rid of Propagate dump
664     aFunction->SetDescription(theDesc);
665     if (edges_i.IsNull() || edges_i->Length() == 0) {
666       SetErrorCode("Internal edges not found");
667       return false;
668     }
669     for (int i=1; i<=edges_i->Length();i++) {
670       Handle(GEOM_Object) anObj = Handle(GEOM_Object)::DownCast(edges_i->Value(i));
671       anObj->GetLastFunction()->SetDescription("");
672     }
673     arete_intersect_int = Handle(GEOM_Object)::DownCast(edges_i->Value(1));
674
675     // search for vertices located on both internal pipes
676     aFunction = theShape->GetLastFunction();
677     theDesc = aFunction->GetDescription();
678     Handle(TColStd_HSequenceOfTransient) vertices_i =
679       myShapesOperations->GetShapesOnBox(box_i, theShape, TopAbs_VERTEX, GEOMAlgo_ST_ONIN);
680     // Recover previous description to get rid of Propagate dump
681     aFunction->SetDescription(theDesc);
682     if (vertices_i.IsNull() || vertices_i->Length() == 0) {
683       SetErrorCode("Internal vertices not found");
684       return false;
685     }
686
687     for (int i = 1; i <= vertices_i->Length(); i++) {
688       Handle(GEOM_Object) v = Handle(GEOM_Object)::DownCast(vertices_i->Value(i));
689       v->GetLastFunction()->SetDescription("");
690       TopoDS_Vertex aVertex = TopoDS::Vertex(v->GetValue());
691       gp_Pnt aP = BRep_Tool::Pnt(aVertex);
692       if (Abs(aP.X()) <= Precision::Confusion()) {
693         if (Abs(aP.Y()) - theR2 <= Precision::Confusion())
694           vi1 = v;
695       } else if (Abs(aP.Y()) <= Precision::Confusion()) {
696         if (Abs(aP.X()) - theR1 <= Precision::Confusion())
697           vi2 = v;
698       }
699     }
700
701     std::list<Handle(GEOM_Object)> theShapes;
702
703     if (isNormal) {
704       Handle(GEOM_Object) ve1, ve2;
705
706       Handle(GEOM_Object) box_e = my3DPrimOperations->MakeBoxDXDYDZ(aR2Ext, aR2Ext, aR1Ext);
707       box_e->GetLastFunction()->SetDescription("");
708       box_e = myTransformOperations->TranslateDXDYDZ(box_e, -aR2Ext, -aR2Ext, 0);
709       box_e->GetLastFunction()->SetDescription("");
710       // Common edges on external cylinder
711       aFunction = theShape->GetLastFunction();
712       theDesc = aFunction->GetDescription();
713       Handle(TColStd_HSequenceOfTransient) edges_e =
714         myShapesOperations->GetShapesOnBox(box_e, theShape, TopAbs_EDGE, GEOMAlgo_ST_IN);
715       // Recover previous description to get rid of Propagate dump
716       aFunction->SetDescription(theDesc);
717       if (edges_e.IsNull() || edges_e->Length() == 0) {
718         SetErrorCode("External edges not found");
719         return false;
720       }
721       for (int i=1; i<=edges_e->Length();i++) {
722         Handle(GEOM_Object) anObj = Handle(GEOM_Object)::DownCast(edges_e->Value(i));
723         anObj->GetLastFunction()->SetDescription("");
724       }
725
726       // search for vertices located on both external pipes
727       aFunction = theShape->GetLastFunction();
728       theDesc = aFunction->GetDescription();
729       Handle(TColStd_HSequenceOfTransient) vertices_e =
730         myShapesOperations->GetShapesOnBox(box_e, theShape, TopAbs_VERTEX, GEOMAlgo_ST_ONIN);
731       // Recover previous description to get rid of Propagate dump
732       aFunction->SetDescription(theDesc);
733       if (vertices_e.IsNull() || vertices_e->Length() == 0) {
734         SetErrorCode("External vertices not found");
735         return false;
736       }
737
738       for (int i = 1; i <= vertices_e->Length(); i++) {
739         Handle(GEOM_Object) v = Handle(GEOM_Object)::DownCast(vertices_e->Value(i));
740         v->GetLastFunction()->SetDescription("");
741         TopoDS_Vertex aVertex = TopoDS::Vertex(v->GetValue());
742         gp_Pnt aP = BRep_Tool::Pnt(aVertex);
743         if (Abs(aP.X()) <= Precision::Confusion()) {
744           if (Abs(aP.Y()) - theR2 > Precision::Confusion())
745             ve1 = v;
746         } else if (Abs(aP.Y()) <= Precision::Confusion()) {
747           if (Abs(aP.X()) - theR2 > Precision::Confusion())
748             ve2 = v;
749         }
750       }
751       Handle(GEOM_Object) edge_e1, edge_e2;
752       
753       edge_e1 = myBasicOperations->MakeLineTwoPnt(ve1, vi1);
754       if (edge_e1.IsNull()) {
755         SetErrorCode("Edge 1 could not be built");
756         return false;
757       }
758       
759       edge_e2 = myBasicOperations->MakeLineTwoPnt(ve2, vi2);
760       if (edge_e2.IsNull()) {
761         SetErrorCode("Edge 2 could not be built");
762         return false;
763       }
764
765       edge_e1->GetLastFunction()->SetDescription("");
766       edge_e2->GetLastFunction()->SetDescription("");
767
768       std::list<Handle(GEOM_Object)> edge_e_elist;
769       edge_e_elist.push_back(arete_intersect_int);
770       edge_e_elist.push_back(edge_e1);
771       edge_e_elist.push_back(Handle(GEOM_Object)::DownCast(edges_e->Value(1)));
772       edge_e_elist.push_back(edge_e2);
773       wire_t = myShapesOperations->MakeWire(edge_e_elist, 1e-7);
774       if (wire_t.IsNull()) {
775         SetErrorCode("Impossible to build wire");
776         return false;
777       }
778       wire_t->GetLastFunction()->SetDescription("");
779       face_t = myShapesOperations->MakeFace(wire_t, false);
780       if (face_t.IsNull()) {
781         SetErrorCode("Impossible to build face");
782         return false;
783       }
784       face_t->GetLastFunction()->SetDescription("");
785     }
786     else {
787       Handle(GEOM_Object) P1, P2, P3, P4, P5, P6;
788       int idP1, idP2, idP3, idP4;
789       int PZX, PZY;
790       double ZX=0, ZY=0;
791       std::vector<int> LX;
792       std::vector<int> LY;
793       Handle(GEOM_Object) box_e = my3DPrimOperations->MakeBoxDXDYDZ
794         (theVertCylinderRadius, theVertCylinderRadius, theHoriCylinderRadius);
795       box_e->GetLastFunction()->SetDescription("");
796       box_e = myTransformOperations->TranslateDXDYDZ
797         (box_e, -theVertCylinderRadius, -theVertCylinderRadius, 0);
798       box_e->GetLastFunction()->SetDescription("");
799
800       aFunction = theShape->GetLastFunction();
801       theDesc = aFunction->GetDescription();
802       Handle(TColStd_HSequenceOfTransient) extremVertices =
803         myShapesOperations->GetShapesOnBox(box_e, theShape, TopAbs_VERTEX, GEOMAlgo_ST_ONIN);
804       // Recover previous description to get rid of Propagate dump
805       aFunction->SetDescription(theDesc);
806
807       if (extremVertices.IsNull() || extremVertices->Length() == 0) {
808         if (theRF == 0)
809           SetErrorCode("Vertices on chamfer not found");
810         else
811           SetErrorCode("Vertices on fillet not found");
812         return false;
813       }
814
815       theShapes.push_back(theShape);
816       theShapes.push_back(box_e);
817       if (extremVertices->Length() != 6) {
818         //           for (int i=1; i<=extremVertices->Length(); i++){
819         //             theShapes.push_back(Handle(GEOM_Object)::DownCast(extremVertices->Value(i)));
820         //           }
821         //           Handle(GEOM_Object) aCompound = myShapesOperations->MakeCompound(theShapes);
822         //           TopoDS_Shape aCompoundShape = aCompound->GetValue();
823         //           theShape->GetLastFunction()->SetValue(aCompoundShape);
824         SetErrorCode("Bad number of vertices on chamfer found");
825         return false;
826       }
827
828       for (int i=1; i<=extremVertices->Length(); i++){
829         Handle(GEOM_Object) aV = Handle(GEOM_Object)::DownCast(extremVertices->Value(i));
830         aV->GetLastFunction()->SetDescription("");
831         gp_Pnt aP = BRep_Tool::Pnt(TopoDS::Vertex(aV->GetValue()));
832
833         if (Abs(aP.X()) <= Precision::Confusion()) {
834           if (Abs(aP.Y()) - theR2 > Precision::Confusion()) {
835             LX.push_back(i);
836             if  (aP.Z()-ZX > Precision::Confusion()) {
837               ZX = aP.Z();
838               PZX = i;
839             }
840           }
841         }
842         else {
843           if (Abs(aP.X()) - theR2 > Precision::Confusion()) {
844             LY.push_back(i);
845             if (aP.Z() - ZY > Precision::Confusion()) {
846               ZY = aP.Z();
847               PZY = i;
848             }
849           }
850         }
851       }
852
853       idP2 = PZX;
854       idP4 = PZY;
855       idP1 = LX.at(0);
856       if (LX.at(0) == PZX)
857         idP1 = LX.at(1);
858       idP3 = LY.at(0);
859       if (LY.at(0) == PZY)
860         idP3 = LY.at(1);
861
862       P1 = Handle(GEOM_Object)::DownCast(extremVertices->Value(idP1));
863       P2 = Handle(GEOM_Object)::DownCast(extremVertices->Value(idP2));
864       P3 = Handle(GEOM_Object)::DownCast(extremVertices->Value(idP3));
865       P4 = Handle(GEOM_Object)::DownCast(extremVertices->Value(idP4));
866
867       Handle(GEOM_Object) Cote_1 = myBasicOperations->MakeLineTwoPnt(P1, vi1);
868       if (Cote_1.IsNull()) {
869         SetErrorCode("Impossible to build edge in thickness");
870         return false;
871       }
872       Cote_1->GetLastFunction()->SetDescription("");
873
874       Handle(GEOM_Object) Cote_2 = myBasicOperations->MakeLineTwoPnt(vi2, P3);
875       if (Cote_2.IsNull()) {
876         SetErrorCode("Impossible to build edge in thickness");
877         return false;
878       }
879       Cote_2->GetLastFunction()->SetDescription("");
880
881       // edge_chan_princ = arete du chanfrein (ou raccord) sur le tuyau principal
882       // edge_chan_inc = arete du chanfrein (ou raccord) sur le tuyau incident
883       //         std::cerr << "Getting chamfer edge on main pipe" << std::endl;
884       Handle(GEOM_Object) edge_chan_princ = myBlocksOperations->GetEdge(theShape, P1, P3);
885       if (edge_chan_princ.IsNull()) {
886         SetErrorCode("Impossible to find edge on main pipe");
887         return false;
888       }
889       edge_chan_princ->GetLastFunction()->SetDescription("");
890
891       Handle(GEOM_Object) edge_chan_inc = myBlocksOperations->GetEdge(theShape, P2, P4);
892       if (edge_chan_inc.IsNull()) {
893         SetErrorCode("Impossible to find edge on incident pipe");
894         return false;
895       }
896       edge_chan_inc->GetLastFunction()->SetDescription("");
897
898       std::list<Handle(GEOM_Object)> edgeList1;
899       edgeList1.push_back(edge_chan_princ);
900       edgeList1.push_back(Cote_1);
901       edgeList1.push_back(arete_intersect_int);
902       edgeList1.push_back(Cote_2);
903
904       //         std::cerr << "Creating wire 1" << std::endl;
905       wire_t = myShapesOperations->MakeWire(edgeList1, 1e-7);
906       if (wire_t.IsNull()) {
907         SetErrorCode("Impossible to build wire");
908         return false;
909       }
910       wire_t->GetLastFunction()->SetDescription("");
911
912       //         std::cerr << "Creating face 1" << std::endl;
913       face_t = myShapesOperations->MakeFace(wire_t, false);
914       if (face_t.IsNull()) {
915         SetErrorCode("Impossible to build face");
916         return false;
917       }
918       face_t->GetLastFunction()->SetDescription("");
919       theShapes.push_back(face_t);
920
921       gp_Pnt aP2 = BRep_Tool::Pnt(TopoDS::Vertex(P2->GetValue()));
922       gp_Pnt aP5 = BRep_Tool::Pnt(TopoDS::Vertex(vi1->GetValue()));
923       double deltaZ = aP2.Z() - aP5.Z();
924       //         std::cerr << "Creating new point from vi1 with deltaZ = " << deltaZ << std::endl;
925       Handle(GEOM_Object) P5bis = myTransformOperations->TranslateDXDYDZCopy(vi1, 0, 0, deltaZ);
926       if (P5bis.IsNull()) {
927         SetErrorCode("Impossible to translate vertex");
928         return false;
929       }
930       P5bis->GetLastFunction()->SetDescription("");
931
932       gp_Pnt aP4 = BRep_Tool::Pnt(TopoDS::Vertex(P4->GetValue()));
933       gp_Pnt aP6 = BRep_Tool::Pnt(TopoDS::Vertex(vi2->GetValue()));
934       deltaZ = aP4.Z() - aP6.Z();
935       //         std::cerr << "Creating new point from vi2 with deltaZ = " << deltaZ << std::endl;
936       Handle(GEOM_Object) P6bis = myTransformOperations->TranslateDXDYDZCopy(vi2, 0, 0, deltaZ);
937       if (P6bis.IsNull()) {
938         SetErrorCode("Impossible to translate vertex");
939         return false;
940       }
941       P6bis->GetLastFunction()->SetDescription("");
942
943       //         std::cerr << "Creating new line 1 from 2 previous points" << std::endl;
944       Handle(GEOM_Object) Cote_3 = myBasicOperations->MakeLineTwoPnt(P5bis, P2);
945       if (Cote_3.IsNull()) {
946         SetErrorCode("Impossible to build edge in thickness");
947         return false;
948       }
949       Cote_3->GetLastFunction()->SetDescription("");
950
951       //         std::cerr << "Creating new line 2 from 2 previous points" << std::endl;
952       Handle(GEOM_Object) Cote_4 = myBasicOperations->MakeLineTwoPnt(P6bis, P4);
953       if (Cote_4.IsNull()) {
954         SetErrorCode("Impossible to build edge in thickness");
955         return false;
956       }
957       Cote_4->GetLastFunction()->SetDescription("");
958
959       //         std::cerr << "Creating new line 3 from 2 previous points" << std::endl;
960       Handle(GEOM_Object) Cote_5 = myBasicOperations->MakeLineTwoPnt(P5bis, P6bis);
961       if (Cote_4.IsNull()) {
962         SetErrorCode("Impossible to build edge in thickness");
963         return false;
964       }
965       Cote_5->GetLastFunction()->SetDescription("");
966
967       //std::list<Handle(GEOM_Object)> edgeList2;
968       //edgeList2.push_back(edge_chan_inc);
969       //edgeList2.push_back(Cote_3);
970       //edgeList2.push_back(Cote_5);
971       //edgeList2.push_back(Cote_4);
972       //         std::cerr << "Creating wire 2" << std::endl;
973       //wire_t2 = myShapesOperations->MakeWire(edgeList2, 1e-7);
974       //if (wire_t2.IsNull()) {
975       //  SetErrorCode("Impossible to build wire");
976       //  return false;
977       //}
978       //wire_t2->GetLastFunction()->SetDescription("");
979       //         std::cerr << "Creating face 2" << std::endl;
980       //face_t2 = myShapesOperations->MakeFace(wire_t2, false);
981       face_t2 = my3DPrimOperations->MakePrismVecH(edge_chan_inc, Cote_4, - 2.0*theR2);
982       if (face_t2.IsNull()) {
983         SetErrorCode("Impossible to build face");
984         return false;
985       }
986       face_t2->GetLastFunction()->SetDescription("");
987       theShapes.push_back(face_t2);
988     }
989
990     // Planes
991     Handle(GEOM_Object) aP0 = myBasicOperations->MakePointXYZ(0, 0, 0);
992     Handle(GEOM_Object) aVZ = myBasicOperations->MakeVectorDXDYDZ(0, 0, 1);
993     Handle(GEOM_Object) aVXZ = myBasicOperations->MakeVectorDXDYDZ(aR1Ext, 0, 0.5*(theL1+theVertCylinderRadius));
994     Handle(GEOM_Object) aPlnOZ = myBasicOperations->MakePlanePntVec(aP0, aVZ, aSize);
995     Handle(GEOM_Object) aPlnOXZ = myBasicOperations->MakePlanePntVec(aP0, aVXZ, aSize);
996     aP0->GetLastFunction()->SetDescription("");
997     aVZ->GetLastFunction()->SetDescription("");
998     aVXZ->GetLastFunction()->SetDescription("");
999     aPlnOZ->GetLastFunction()->SetDescription("");
1000     aPlnOXZ->GetLastFunction()->SetDescription("");
1001     theShapes.push_back(aPlnOZ);
1002     theShapes.push_back(aPlnOXZ);
1003
1004     // Partition
1005     Handle(TColStd_HSequenceOfTransient) partitionShapes = new TColStd_HSequenceOfTransient;
1006     Handle(TColStd_HSequenceOfTransient) theTools = new TColStd_HSequenceOfTransient;
1007     Handle(TColStd_HSequenceOfTransient) theKeepInside = new TColStd_HSequenceOfTransient;
1008     Handle(TColStd_HSequenceOfTransient) theRemoveInside = new TColStd_HSequenceOfTransient;
1009     Handle(TColStd_HArray1OfInteger) theMaterials;
1010     partitionShapes->Append(theShape);
1011     theTools->Append(aPlnOZ);
1012     if (Abs(aR1Ext - aR2Ext) > Precision::Confusion() )
1013       theTools->Append(aPlnOXZ);
1014     theTools->Append(face_t);
1015     if (!isNormal)
1016       theTools->Append(face_t2);
1017
1018     Te3 = myBooleanOperations->MakePartition
1019               (partitionShapes, theTools, theKeepInside, theRemoveInside,
1020               TopAbs_SOLID, false, theMaterials, 0, false);
1021     if (Te3.IsNull()) {
1022       SetErrorCode("Impossible to build partition of TShape");
1023 //       Handle(GEOM_Object) aCompound = myShapesOperations->MakeCompound(theShapes);
1024 //       TopoDS_Shape aCompoundShape = aCompound->GetValue();
1025 //       theShape->GetLastFunction()->SetValue(aCompoundShape);
1026       return false;
1027     }
1028     Te3->GetLastFunction()->SetDescription("");
1029
1030     // Last verification: result should be a block
1031     std::list<GEOMImpl_IBlocksOperations::BCError> errList;
1032     if (!myBlocksOperations->CheckCompoundOfBlocks(Te3,errList)) {
1033       SetErrorCode("TShape is not a block");
1034       return false;
1035     }
1036     TopoDS_Shape aShape = Te3->GetValue();
1037     theShape->GetLastFunction()->SetValue(aShape);
1038   } catch (Standard_Failure) {
1039     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
1040     SetErrorCode(aFail->GetMessageString());
1041     return false;
1042   }
1043
1044   SetErrorCode(OK);
1045   return true;
1046 }
1047
1048 // Mirror and glue faces
1049 bool GEOMImpl_IAdvancedOperations::MakePipeTShapeMirrorAndGlue(Handle(GEOM_Object) theShape,
1050                                                                double theR1, double theW1, double theL1,
1051                                                                double theR2, double theW2, double theL2)
1052 {
1053   SetErrorCode(KO);
1054
1055   // Useful values
1056   double aSize = 2*(theL1 + theL2);
1057   double aR1Ext = theR1 + theW1;
1058
1059   // Planes
1060   Handle(GEOM_Object) aP0 = myBasicOperations->MakePointXYZ(0, 0, 0);
1061   aP0->GetLastFunction()->SetDescription("");
1062   Handle(GEOM_Object) aVX = myBasicOperations->MakeVectorDXDYDZ(1, 0, 0);
1063   Handle(GEOM_Object) aVY = myBasicOperations->MakeVectorDXDYDZ(0, 1, 0);
1064   aVX->GetLastFunction()->SetDescription("");
1065   aVY->GetLastFunction()->SetDescription("");
1066   Handle(GEOM_Object) aPlane_OX = myBasicOperations->MakePlanePntVec(aP0, aVX, 2*(aR1Ext + theL2));
1067   Handle(GEOM_Object) aPlane_OY = myBasicOperations->MakePlanePntVec(aP0, aVY, aSize);
1068   aPlane_OX->GetLastFunction()->SetDescription("");
1069   aPlane_OY->GetLastFunction()->SetDescription("");
1070
1071   Handle(GEOM_Object) Te4 = myTransformOperations->MirrorPlaneCopy(theShape, aPlane_OX);
1072   if (Te4.IsNull()) {
1073     SetErrorCode("Impossible to build mirror of quarter TShape");
1074     return false;
1075   }
1076
1077   Handle(GEOM_Object) Te5 = myTransformOperations->MirrorPlaneCopy(theShape, aPlane_OY);
1078   if (Te5.IsNull()) {
1079     SetErrorCode("Impossible to build mirror of half TShape");
1080     return false;
1081   }
1082
1083   Handle(GEOM_Object) Te6 = myTransformOperations->MirrorPlaneCopy(Te4, aPlane_OY);
1084   if (Te6.IsNull()) {
1085     SetErrorCode("Impossible to build mirror of half TShape");
1086     return false;
1087   }
1088
1089   std::list<Handle(GEOM_Object)> aShapesList;
1090   aShapesList.push_back(theShape);
1091   aShapesList.push_back(Te4);
1092   aShapesList.push_back(Te5);
1093   aShapesList.push_back(Te6);
1094   Handle(GEOM_Object) Te7 = myShapesOperations->MakeCompound(aShapesList);
1095   if (Te7.IsNull()) {
1096     SetErrorCode("Impossible to build compound");
1097     return false;
1098   }
1099
1100   Handle(GEOM_Object) Te8 = myShapesOperations->MakeGlueFaces(Te7, 1e-7, true);
1101   if (Te8.IsNull()) {
1102     SetErrorCode("Impossible to glue faces of TShape");
1103     return false;
1104   }
1105
1106   TopoDS_Shape aShape = Te8->GetValue();
1107
1108   theShape->GetLastFunction()->SetValue(aShape);
1109
1110   Te4->GetLastFunction()->SetDescription("");
1111   Te5->GetLastFunction()->SetDescription("");
1112   Te6->GetLastFunction()->SetDescription("");
1113   Te7->GetLastFunction()->SetDescription("");
1114   Te8->GetLastFunction()->SetDescription("");
1115
1116   SetErrorCode(OK);
1117   return true;
1118 }
1119
1120 //=============================================================================
1121 /*!
1122  *  MakePipeTShape
1123  *  Create a T-shape object with specified caracteristics for the main and
1124  *  the incident pipes (radius, width, half-length).
1125  *  Center of the shape is (0,0,0). The main plane of the T-shape is XOY.
1126  *  \param theR1 Internal radius of main pipe
1127  *  \param theW1 Width of main pipe
1128  *  \param theL1 Half-length of main pipe
1129  *  \param theR2 Internal radius of incident pipe (R2 < R1)
1130  *  \param theW2 Width of incident pipe (R2+W2 < R1+W1)
1131  *  \param theL2 Half-length of incident pipe
1132  *  \param theHexMesh Boolean indicating if shape is prepared for hex mesh
1133  *  \return List of GEOM_Objects, containing the created shape and propagation groups.
1134  */
1135 //=============================================================================
1136 Handle(TColStd_HSequenceOfTransient)
1137 GEOMImpl_IAdvancedOperations::MakePipeTShape(double theR1, double theW1, double theL1,
1138                                              double theR2, double theW2, double theL2,
1139                                              bool theHexMesh)
1140 {
1141   MESSAGE("GEOMImpl_IAdvancedOperations::MakePipeTShape");
1142   SetErrorCode(KO);
1143   //Add a new object
1144   Handle(GEOM_Object) aShape = GetEngine()->AddObject(GetDocID(), GEOM_TSHAPE);
1145
1146   //Add a new shape function with parameters
1147   Handle(GEOM_Function) aFunction = aShape->AddFunction(GEOMImpl_PipeTShapeDriver::GetID(), TSHAPE_BASIC);
1148   if (aFunction.IsNull()) return NULL;
1149
1150   //Check if the function is set correctly
1151   if (aFunction->GetDriverGUID() != GEOMImpl_PipeTShapeDriver::GetID()) return NULL;
1152
1153   GEOMImpl_IPipeTShape aData(aFunction);
1154
1155   aData.SetR1(theR1);
1156   aData.SetW1(theW1);
1157   aData.SetL1(theL1);
1158   aData.SetR2(theR2);
1159   aData.SetW2(theW2);
1160   aData.SetL2(theL2);
1161   aData.SetHexMesh(theHexMesh);
1162
1163   //Compute the resulting value
1164   try {
1165 #if OCC_VERSION_LARGE > 0x06010000
1166     OCC_CATCH_SIGNALS;
1167 #endif
1168     if (!GetSolver()->ComputeFunction(aFunction)) {
1169       SetErrorCode("TShape driver failed");
1170       return NULL;
1171     }
1172     if (theHexMesh) {
1173       if (!MakePipeTShapePartition(aShape, theR1, theW1, theL1, theR2, theW2, theL2))
1174         return NULL;
1175       if (!MakePipeTShapeMirrorAndGlue(aShape, theR1, theW1, theL1, theR2, theW2, theL2))
1176         return NULL;
1177     }
1178   } catch (Standard_Failure) {
1179     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
1180     SetErrorCode(aFail->GetMessageString());
1181     return NULL;
1182   }
1183
1184
1185   Handle(TColStd_HSequenceOfTransient) aSeq = new TColStd_HSequenceOfTransient;
1186   aSeq->Append(aShape);
1187
1188   if (theHexMesh) {
1189     /*
1190      * Get the groups: BEGIN
1191      */
1192     try {
1193       if (!MakeGroups(aShape, TSHAPE_BASIC, theR1, theW1, theL1, theR2, theW2, theL2, aSeq, gp_Trsf()))
1194         return NULL;
1195     }
1196     catch (Standard_Failure) {
1197       Handle(Standard_Failure) aFail = Standard_Failure::Caught();
1198       SetErrorCode(aFail->GetMessageString());
1199       return NULL;
1200     }
1201
1202     TCollection_AsciiString aListRes, anEntry;
1203     // Iterate over the sequence aSeq
1204     Standard_Integer aNbGroups = aSeq->Length();
1205     Standard_Integer i = 2;
1206     for (; i <= aNbGroups; i++) {
1207       Handle(Standard_Transient) anItem = aSeq->Value(i);
1208       if (anItem.IsNull()) continue;
1209       Handle(GEOM_Object) aGroup = Handle(GEOM_Object)::DownCast(anItem);
1210       if (aGroup.IsNull()) continue;
1211       //Make a Python command
1212       TDF_Tool::Entry(aGroup->GetEntry(), anEntry);
1213       aListRes += anEntry + ", ";
1214     }
1215
1216     aListRes.Trunc(aListRes.Length() - 2);
1217
1218     //Make a Python command
1219     GEOM::TPythonDump(aFunction)
1220       << "[" << aShape << ", " << aListRes.ToCString() << "] = geompy.MakePipeTShape("
1221       << theR1 << ", " << theW1 << ", " << theL1 << ", " << theR2 << ", " << theW2 << ", " << theL2 << ", "
1222       << theHexMesh << ")";
1223   }
1224   /*
1225    * Get the groups: END
1226    */
1227   else {
1228     //Make a Python command
1229     GEOM::TPythonDump(aFunction)
1230       << "[" << aShape << "] = geompy.MakePipeTShape(" << theR1 << ", " << theW1 << ", "
1231       << theL1 << ", " << theR2 << ", " << theW2 << ", " << theL2 << ", " << theHexMesh << ")";
1232   }
1233
1234   SetErrorCode(OK);
1235
1236   return aSeq;
1237 }
1238
1239 //=============================================================================
1240 /*!
1241  *  MakePipeTShapeWithPosition
1242  *  Create a T-shape object with specified caracteristics for the main and
1243  *  the incident pipes (radius, width, half-length).
1244  *  The extremities of the main pipe are located on junctions points P1 and P2.
1245  *  The extremity of the incident pipe is located on junction point P3.
1246  *  \param theR1 Internal radius of main pipe
1247  *  \param theW1 Width of main pipe
1248  *  \param theL1 Half-length of main pipe
1249  *  \param theR2 Internal radius of incident pipe (R2 < R1)
1250  *  \param theW2 Width of incident pipe (R2+W2 < R1+W1)
1251  *  \param theL2 Half-length of incident pipe
1252  *  \param theHexMesh Boolean indicating if shape is prepared for hex mesh
1253  *  \param theP1 1st junction point of main pipe
1254  *  \param theP2 2nd junction point of main pipe
1255  *  \param theP3 Junction point of incident pipe
1256  *  \return List of GEOM_Objects, containing the created shape and propagation groups..
1257  */
1258 //=============================================================================
1259 Handle(TColStd_HSequenceOfTransient)
1260 GEOMImpl_IAdvancedOperations::MakePipeTShapeWithPosition(double theR1, double theW1, double theL1,
1261                                                          double theR2, double theW2, double theL2,
1262                                                          bool theHexMesh,
1263                                                          Handle(GEOM_Object) theP1,
1264                                                          Handle(GEOM_Object) theP2,
1265                                                          Handle(GEOM_Object) theP3)
1266 {
1267   SetErrorCode(KO);
1268   //Add a new object
1269   Handle(GEOM_Object) aShape = GetEngine()->AddObject(GetDocID(), GEOM_TSHAPE);
1270   /////////////////
1271   // TSHAPE CODE
1272   /////////////////
1273   //Add a new shape function with parameters
1274   Handle(GEOM_Function) aFunction = aShape->AddFunction(GEOMImpl_PipeTShapeDriver::GetID(), TSHAPE_BASIC);
1275   if (aFunction.IsNull()) return NULL;
1276
1277   //Check if the function is set correctly
1278   if (aFunction->GetDriverGUID() != GEOMImpl_PipeTShapeDriver::GetID()) return NULL;
1279
1280   // Check new position
1281   if (!CheckCompatiblePosition(theL1, theL2, theP1, theP2, theP3, 0.01)) {
1282     return NULL;
1283   }
1284
1285   GEOMImpl_IPipeTShape aData(aFunction);
1286
1287   aData.SetR1(theR1);
1288   aData.SetW1(theW1);
1289   aData.SetL1(theL1);
1290   aData.SetR2(theR2);
1291   aData.SetW2(theW2);
1292   aData.SetL2(theL2);
1293   aData.SetHexMesh(theHexMesh);
1294
1295   //Compute the resulting value
1296   try {
1297 #if OCC_VERSION_LARGE > 0x06010000
1298     OCC_CATCH_SIGNALS;
1299 #endif
1300     if (!GetSolver()->ComputeFunction(aFunction)) {
1301       SetErrorCode("TShape driver failed");
1302       return NULL;
1303     }
1304   } catch (Standard_Failure) {
1305     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
1306     SetErrorCode(aFail->GetMessageString());
1307     return NULL;
1308   }
1309
1310   if (theHexMesh) {
1311     if (!MakePipeTShapePartition(aShape, theR1, theW1, theL1, theR2, theW2, theL2))
1312       return NULL;
1313     if (!MakePipeTShapeMirrorAndGlue(aShape, theR1, theW1, theL1, theR2, theW2, theL2))
1314       return NULL;
1315   }
1316
1317   TopoDS_Shape Te = aShape->GetValue();
1318
1319   // Set Position
1320   gp_Trsf aTrsf = GetPositionTrsf(theL1, theL2, theP1, theP2, theP3);
1321   BRepBuilderAPI_Transform aTransformation(Te, aTrsf, Standard_False);
1322   TopoDS_Shape aTrsf_Shape = aTransformation.Shape();
1323   aFunction->SetValue(aTrsf_Shape);
1324   Handle(TColStd_HSequenceOfTransient) aSeq = new TColStd_HSequenceOfTransient;
1325   aSeq->Append(aShape);
1326
1327   if (theHexMesh) {
1328     //
1329     // Get the groups: BEGIN
1330     //
1331     try {
1332       if (!MakeGroups(aShape,TSHAPE_BASIC, theR1, theW1, theL1, theR2, theW2, theL2, aSeq, aTrsf)) {
1333         return NULL;
1334       }
1335     }
1336     catch (Standard_Failure) {
1337       Handle(Standard_Failure) aFail = Standard_Failure::Caught();
1338       SetErrorCode(aFail->GetMessageString());
1339       return NULL;
1340     }
1341
1342     TCollection_AsciiString aListRes, anEntry;
1343     // Iterate over the sequence aSeq
1344     Standard_Integer aNbGroups = aSeq->Length();
1345     Standard_Integer i = 2;
1346     for (; i <= aNbGroups; i++) {
1347       Handle(Standard_Transient) anItem = aSeq->Value(i);
1348       if (anItem.IsNull()) continue;
1349       Handle(GEOM_Object) aGroup = Handle(GEOM_Object)::DownCast(anItem);
1350       if (aGroup.IsNull()) continue;
1351       //Make a Python command
1352       TDF_Tool::Entry(aGroup->GetEntry(), anEntry);
1353       aListRes += anEntry + ", ";
1354     }
1355
1356     aListRes.Trunc(aListRes.Length() - 2);
1357
1358     //Make a Python command
1359     GEOM::TPythonDump(aFunction)
1360       << "[" << aShape << ", " << aListRes.ToCString() << "] = geompy.MakePipeTShape("
1361       << theR1 << ", " << theW1 << ", " << theL1 << ", " << theR2 << ", " << theW2 << ", " << theL2 << ", "
1362       << theHexMesh << ", " << theP1 << ", " << theP2 << ", " << theP3 << ")";
1363   }
1364   //
1365   // Get the groups: END
1366   //
1367
1368   else {
1369     //Make a Python command
1370     GEOM::TPythonDump(aFunction)
1371       << "[" << aShape << "] = geompy.MakePipeTShape(" << theR1 << ", " << theW1 << ", "
1372       << theL1 << ", " << theR2 << ", " << theW2 << ", " << theL2 << ", " << theHexMesh << ", " << theP1
1373       << ", " << theP2 << ", " << theP3 << ")";
1374   }
1375
1376   SetErrorCode(OK);
1377
1378   return aSeq;
1379 }
1380
1381 //=============================================================================
1382 /*!
1383  *  MakePipeTShapeChamfer
1384  *  Create a T-shape object with specified caracteristics for the main and
1385  *  the incident pipes (radius, width, half-length). A chamfer is created
1386  *  on the junction of the pipes.
1387  *  Center of the shape is (0,0,0). The main plane of the T-shape is XOY.
1388  *  \param theR1 Internal radius of main pipe
1389  *  \param theW1 Width of main pipe
1390  *  \param theL1 Half-length of main pipe
1391  *  \param theR2 Internal radius of incident pipe (R2 < R1)
1392  *  \param theW2 Width of incident pipe (R2+W2 < R1+W1)
1393  *  \param theL2 Half-length of incident pipe
1394  *  \param theH Height of chamfer.
1395  *  \param theW Width of chamfer.
1396  *  \param theHexMesh Boolean indicating if shape is prepared for hex mesh
1397  *  \return List of GEOM_Objects, containing the created shape and propagation groups.
1398  */
1399 //=============================================================================
1400 Handle(TColStd_HSequenceOfTransient)
1401 GEOMImpl_IAdvancedOperations::MakePipeTShapeChamfer(double theR1, double theW1, double theL1,
1402                                                     double theR2, double theW2, double theL2,
1403                                                     double theH, double theW,
1404                                                     bool theHexMesh)
1405 {
1406   SetErrorCode(KO);
1407   //Add a new object
1408   Handle(GEOM_Object) aShape = GetEngine()->AddObject(GetDocID(), GEOM_TSHAPE);
1409   //Add a new shape function with parameters
1410   Handle(GEOM_Function) aFunction = aShape->AddFunction(GEOMImpl_PipeTShapeDriver::GetID(), TSHAPE_CHAMFER);
1411   if (aFunction.IsNull()) return NULL;
1412
1413   //Check if the function is set correctly
1414   if (aFunction->GetDriverGUID() != GEOMImpl_PipeTShapeDriver::GetID()) return NULL;
1415
1416   GEOMImpl_IPipeTShape aData(aFunction);
1417
1418   aData.SetR1(theR1);
1419   aData.SetW1(theW1);
1420   aData.SetL1(theL1);
1421   aData.SetR2(theR2);
1422   aData.SetW2(theW2);
1423   aData.SetL2(theL2);
1424   aData.SetH(theH);
1425   aData.SetW(theW);
1426   aData.SetHexMesh(theHexMesh);
1427
1428   //Compute the resulting value
1429   try {
1430 #if OCC_VERSION_LARGE > 0x06010000
1431     OCC_CATCH_SIGNALS;
1432 #endif
1433     if (!GetSolver()->ComputeFunction(aFunction)) {
1434       SetErrorCode("TShape driver failed");
1435       return NULL;
1436     }
1437   } catch (Standard_Failure) {
1438     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
1439     SetErrorCode(aFail->GetMessageString());
1440     return NULL;
1441   }
1442
1443   // BEGIN of chamfer
1444   TopoDS_Shape aShapeShape = aShape->GetValue();
1445   TopTools_IndexedMapOfShape anEdgesIndices;
1446   TopExp::MapShapes(aShapeShape, anEdgesIndices);
1447   // Common edges on external cylinders
1448   Handle(GEOM_Object) box_e;
1449   if (theHexMesh) {
1450     box_e = my3DPrimOperations->MakeBoxDXDYDZ(theR2+theW2, theR2+theW2, theR1+theW1);
1451   }
1452   else {
1453     box_e = my3DPrimOperations->MakeBoxDXDYDZ(2*(theR2+theW2), 2*(theR2+theW2), theR1+theW1);
1454   }
1455   box_e->GetLastFunction()->SetDescription("");
1456   box_e = myTransformOperations->TranslateDXDYDZ(box_e, -(theR2+theW2), -(theR2+theW2), 0);
1457   box_e->GetLastFunction()->SetDescription("");
1458
1459   Handle(TColStd_HSequenceOfInteger) edges_e =
1460     myShapesOperations->GetShapesOnBoxIDs(box_e, aShape, TopAbs_EDGE, GEOMAlgo_ST_IN);
1461   box_e->GetLastFunction()->SetDescription("");
1462
1463   if (edges_e.IsNull() || edges_e->Length() == 0) {
1464     SetErrorCode("External edges not found");
1465     return NULL;
1466   }
1467   int nbEdgesInChamfer = 0;
1468   std::list<int> theEdges;
1469   for (int i=1; i<=edges_e->Length();i++) {
1470     int edgeID = edges_e->Value(i);
1471     TopoDS_Shape theEdge = anEdgesIndices.FindKey(edgeID);
1472     TopExp_Explorer Ex(theEdge,TopAbs_VERTEX);
1473     int iv=0;
1474     while (Ex.More()) {
1475       iv ++;
1476       gp_Pnt aPt = BRep_Tool::Pnt(TopoDS::Vertex(Ex.Current()));
1477       if (Abs(aPt.Z() - (theR1+theW1)) <= Precision::Confusion()) {
1478         nbEdgesInChamfer ++;
1479         theEdges.push_back(edgeID);
1480       }
1481       Ex.Next();
1482     }
1483     if (theHexMesh && nbEdgesInChamfer == 1)
1484       break;
1485   }
1486   Handle(GEOM_Object) aChamfer;
1487   try {
1488     aChamfer = myLocalOperations->MakeChamferEdges(aShape, theW, theH, theEdges);
1489   }
1490   catch (Standard_Failure) {
1491     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
1492     SetErrorCode(aFail->GetMessageString());
1493     return NULL;
1494   }
1495   if (aChamfer.IsNull()) {
1496     SetErrorCode("Chamfer can not be computed on the given shape with the given parameters");
1497     return NULL;
1498   }
1499   aChamfer->GetLastFunction()->SetDescription("");
1500
1501   TopoDS_Shape aChamferShape = aChamfer->GetValue();
1502   aFunction->SetValue(aChamferShape);
1503   // END of chamfer
1504
1505   //   bool doMesh = false;
1506   if (theHexMesh) {
1507     //        doMesh = true;
1508     if (!MakePipeTShapePartition(aShape, theR1, theW1, theL1, theR2, theW2, theL2, theH, theW, 0, false)) {
1509       MESSAGE("PipeTShape partition failed");
1510       //            doMesh = false;
1511       return NULL;
1512     }
1513     if (!MakePipeTShapeMirrorAndGlue(aShape, theR1, theW1, theL1, theR2, theW2, theL2)) {
1514       MESSAGE("PipeTShape mirrors and glue failed");
1515       //          doMesh = false;
1516       return NULL;
1517     }
1518   }
1519
1520   Handle(TColStd_HSequenceOfTransient) aSeq = new TColStd_HSequenceOfTransient;
1521   aSeq->Append(aShape);
1522
1523   //    if (doMesh) {
1524   if (theHexMesh) {
1525     //
1526     //         Get the groups: BEGIN
1527     //
1528     //if (!MakeGroups(aShape, TSHAPE_CHAMFER, theR1, theW1, theL1, theR2, theW2, theL2, aSeq, gp_Trsf())) {
1529     //  //Make a Python command
1530     //  GEOM::TPythonDump(aFunction)
1531     //    << "[" << aShape << "] = geompy.MakePipeTShapeChamfer(" << theR1 << ", " << theW1
1532     //    << ", " << theL1 << ", " << theR2 << ", " << theW2 << ", " << theL2 << ", " << theH << ", " << theW
1533     //    << ", " << theHexMesh << ")";
1534     //}
1535     //else {
1536     try {
1537       if (!MakeGroups(aShape, TSHAPE_CHAMFER, theR1, theW1, theL1, theR2, theW2, theL2, aSeq, gp_Trsf()))
1538         return NULL;
1539     }
1540     catch (Standard_Failure) {
1541       Handle(Standard_Failure) aFail = Standard_Failure::Caught();
1542       SetErrorCode(aFail->GetMessageString());
1543       return NULL;
1544     }
1545
1546     TCollection_AsciiString aListRes, anEntry;
1547     // Iterate over the sequence aSeq
1548     Standard_Integer aNbGroups = aSeq->Length();
1549     Standard_Integer i = 2;
1550     for (; i <= aNbGroups; i++) {
1551       Handle(Standard_Transient) anItem = aSeq->Value(i);
1552       if (anItem.IsNull()) continue;
1553       Handle(GEOM_Object) aGroup = Handle(GEOM_Object)::DownCast(anItem);
1554       if (aGroup.IsNull()) continue;
1555       //Make a Python command
1556       TDF_Tool::Entry(aGroup->GetEntry(), anEntry);
1557       aListRes += anEntry + ", ";
1558     }
1559
1560     aListRes.Trunc(aListRes.Length() - 2);
1561
1562     //Make a Python command
1563     GEOM::TPythonDump(aFunction)
1564       << "[" << aShape << ", " << aListRes.ToCString()
1565       << "] = geompy.MakePipeTShapeChamfer(" << theR1 << ", " << theW1 << ", " << theL1 << ", " << theR2
1566       << ", " << theW2 << ", " << theL2 << ", " << theH << ", " << theW << ", " << theHexMesh << ")";
1567     //}
1568   }
1569   //
1570   //     Get the groups: END
1571   //
1572   else {
1573     //Make a Python command
1574     GEOM::TPythonDump(aFunction)
1575       << "[" << aShape << "] = geompy.MakePipeTShapeChamfer(" << theR1 << ", " << theW1
1576       << ", " << theL1 << ", " << theR2 << ", " << theW2 << ", " << theL2 << ", " << theH << ", " << theW
1577       << ", " << theHexMesh << ")";
1578   }
1579
1580   SetErrorCode(OK);
1581
1582   return aSeq;
1583 }
1584
1585 //=============================================================================
1586 /*!
1587  *  MakePipeTShapeChamferWithPosition
1588  *  Create a T-shape object with specified caracteristics for the main and
1589  *  the incident pipes (radius, width, half-length). A chamfer is created
1590  *  on the junction of the pipes.
1591  *  The extremities of the main pipe are located on junctions points P1 and P2.
1592  *  The extremity of the incident pipe is located on junction point P3.
1593  *  \param theR1 Internal radius of main pipe
1594  *  \param theW1 Width of main pipe
1595  *  \param theL1 Half-length of main pipe
1596  *  \param theR2 Internal radius of incident pipe (R2 < R1)
1597  *  \param theW2 Width of incident pipe (R2+W2 < R1+W1)
1598  *  \param theL2 Half-length of incident pipe
1599  *  \param theH Height of chamfer.
1600  *  \param theW Width of chamfer.
1601  *  \param theHexMesh Boolean indicating if shape is prepared for hex mesh
1602  *  \param theP1 1st junction point of main pipe
1603  *  \param theP2 2nd junction point of main pipe
1604  *  \param theP3 Junction point of incident pipe
1605  *  \return List of GEOM_Objects, containing the created shape and propagation groups.
1606  */
1607 //=============================================================================
1608 Handle(TColStd_HSequenceOfTransient)
1609 GEOMImpl_IAdvancedOperations::MakePipeTShapeChamferWithPosition(double theR1, double theW1, double theL1,
1610                                                                 double theR2, double theW2, double theL2,
1611                                                                 double theH, double theW,
1612                                                                 bool theHexMesh,
1613                                                                 Handle(GEOM_Object) theP1,
1614                                                                 Handle(GEOM_Object) theP2,
1615                                                                 Handle(GEOM_Object) theP3)
1616 {
1617   SetErrorCode(KO);
1618   //Add a new object
1619   Handle(GEOM_Object) aShape = GetEngine()->AddObject(GetDocID(), GEOM_TSHAPE);
1620   //Add a new shape function with parameters
1621   Handle(GEOM_Function) aFunction = aShape->AddFunction(GEOMImpl_PipeTShapeDriver::GetID(), TSHAPE_CHAMFER);
1622   if (aFunction.IsNull()) return NULL;
1623
1624   //Check if the function is set correctly
1625   if (aFunction->GetDriverGUID() != GEOMImpl_PipeTShapeDriver::GetID()) return NULL;
1626
1627   // Check new position
1628   if (!CheckCompatiblePosition(theL1, theL2, theP1, theP2, theP3, 0.01)) {
1629     return NULL;
1630   }
1631
1632   GEOMImpl_IPipeTShape aData(aFunction);
1633
1634   aData.SetR1(theR1);
1635   aData.SetW1(theW1);
1636   aData.SetL1(theL1);
1637   aData.SetR2(theR2);
1638   aData.SetW2(theW2);
1639   aData.SetL2(theL2);
1640   aData.SetH(theH);
1641   aData.SetW(theW);
1642   aData.SetHexMesh(theHexMesh);
1643
1644   //Compute the resulting value
1645   try {
1646 #if OCC_VERSION_LARGE > 0x06010000
1647     OCC_CATCH_SIGNALS;
1648 #endif
1649     if (!GetSolver()->ComputeFunction(aFunction)) {
1650       SetErrorCode("TShape driver failed");
1651       return NULL;
1652     }
1653   } catch (Standard_Failure) {
1654     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
1655     SetErrorCode(aFail->GetMessageString());
1656     return NULL;
1657   }
1658
1659   // BEGIN of chamfer
1660   TopoDS_Shape aShapeShape = aShape->GetValue();
1661   TopTools_IndexedMapOfShape anEdgesIndices;
1662   TopExp::MapShapes(aShapeShape, anEdgesIndices);
1663   // Common edges on external cylinders
1664   Handle(GEOM_Object) box_e;
1665   if (theHexMesh) {
1666     box_e = my3DPrimOperations->MakeBoxDXDYDZ(theR2+theW2, theR2+theW2, theR1+theW1);
1667   }
1668   else {
1669     box_e = my3DPrimOperations->MakeBoxDXDYDZ(2*(theR2+theW2), 2*(theR2+theW2), theR1+theW1);
1670   }
1671   box_e->GetLastFunction()->SetDescription("");
1672   box_e = myTransformOperations->TranslateDXDYDZ(box_e, -(theR2+theW2), -(theR2+theW2), 0);
1673   box_e->GetLastFunction()->SetDescription("");
1674
1675   Handle(TColStd_HSequenceOfInteger) edges_e =
1676     myShapesOperations->GetShapesOnBoxIDs(box_e, aShape, TopAbs_EDGE, GEOMAlgo_ST_IN);
1677   box_e->GetLastFunction()->SetDescription("");
1678
1679   if (edges_e.IsNull() || edges_e->Length() == 0) {
1680     SetErrorCode("External edges not found");
1681     return NULL;
1682   }
1683   int nbEdgesInChamfer = 0;
1684   std::list<int> theEdges;
1685   for (int i=1; i<=edges_e->Length();i++) {
1686     int edgeID = edges_e->Value(i);
1687     TopoDS_Shape theEdge = anEdgesIndices.FindKey(edgeID);
1688     TopExp_Explorer Ex(theEdge,TopAbs_VERTEX);
1689     while (Ex.More()) {
1690       gp_Pnt aPt = BRep_Tool::Pnt(TopoDS::Vertex(Ex.Current()));
1691       if (Abs(aPt.Z() - (theR1+theW1)) <= Precision::Confusion()) {
1692         nbEdgesInChamfer ++;
1693         theEdges.push_back(edgeID);
1694       }
1695       Ex.Next();
1696     }
1697     if (theHexMesh && nbEdgesInChamfer == 1)
1698       break;
1699   }
1700   Handle(GEOM_Object) aChamfer;
1701   try {
1702     aChamfer = myLocalOperations->MakeChamferEdges(aShape, theW, theH, theEdges);
1703   }
1704   catch (Standard_Failure) {
1705     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
1706     SetErrorCode(aFail->GetMessageString());
1707     return NULL;
1708   }
1709   if (aChamfer.IsNull()) {
1710     SetErrorCode("Chamfer can not be computed on the given shape with the given parameters");
1711     return NULL;
1712   }
1713   aChamfer->GetLastFunction()->SetDescription("");
1714
1715   TopoDS_Shape aChamferShape = aChamfer->GetValue();
1716   aFunction->SetValue(aChamferShape);
1717   // END of chamfer
1718
1719   if (theHexMesh) {
1720     if (!MakePipeTShapePartition(aShape, theR1, theW1, theL1, theR2, theW2, theL2, theH, theW, 0, false))
1721       return NULL;
1722     if (!MakePipeTShapeMirrorAndGlue(aShape, theR1, theW1, theL1, theR2, theW2, theL2))
1723       return NULL;
1724   }
1725
1726   TopoDS_Shape Te = aShape->GetValue();
1727
1728   // Set Position
1729   gp_Trsf aTrsf = GetPositionTrsf(theL1, theL2, theP1, theP2, theP3);
1730   BRepBuilderAPI_Transform aTransformation(Te, aTrsf, Standard_False);
1731   TopoDS_Shape aTrsf_Shape = aTransformation.Shape();
1732   aFunction->SetValue(aTrsf_Shape);
1733   Handle(TColStd_HSequenceOfTransient) aSeq = new TColStd_HSequenceOfTransient;
1734   aSeq->Append(aShape);
1735   if (theHexMesh) {
1736     /*
1737      * Get the groups: BEGIN
1738      */
1739     try {
1740       if (!MakeGroups(aShape, TSHAPE_CHAMFER, theR1, theW1, theL1, theR2, theW2, theL2, aSeq, aTrsf))
1741         return NULL;
1742     }
1743     catch (Standard_Failure) {
1744       Handle(Standard_Failure) aFail = Standard_Failure::Caught();
1745       SetErrorCode(aFail->GetMessageString());
1746       return NULL;
1747     }
1748
1749     TCollection_AsciiString aListRes, anEntry;
1750     // Iterate over the sequence aSeq
1751     Standard_Integer aNbGroups = aSeq->Length();
1752     Standard_Integer i = 2;
1753     for (; i <= aNbGroups; i++) {
1754       Handle(Standard_Transient) anItem = aSeq->Value(i);
1755       if (anItem.IsNull()) continue;
1756       Handle(GEOM_Object) aGroup = Handle(GEOM_Object)::DownCast(anItem);
1757       if (aGroup.IsNull()) continue;
1758       //Make a Python command
1759       TDF_Tool::Entry(aGroup->GetEntry(), anEntry);
1760       aListRes += anEntry + ", ";
1761     }
1762
1763     aListRes.Trunc(aListRes.Length() - 2);
1764
1765     //Make a Python command
1766     GEOM::TPythonDump(aFunction)
1767       << "[" << aShape << ", " << aListRes.ToCString()
1768       << "] = geompy.MakePipeTShapeChamfer(" << theR1 << ", " << theW1 << ", " << theL1 << ", " << theR2
1769       << ", " << theW2 << ", " << theL2 << ", " << theH << ", " << theW << ", " << theHexMesh << ", "
1770       << theP1 << ", " << theP2 << ", " << theP3 << ")";
1771   }
1772   /*
1773    * Get the groups: END
1774    */
1775   else {
1776     //Make a Python command
1777     GEOM::TPythonDump(aFunction)
1778       << "[" << aShape << "] = geompy.MakePipeTShapeChamfer(" << theR1 << ", " << theW1
1779       << ", " << theL1 << ", " << theR2 << ", " << theW2 << ", " << theL2 << ", " << theH << ", " << theW
1780       << ", " << theHexMesh << ", " << theP1 << ", " << theP2 << ", " << theP3 << ")";
1781   }
1782
1783   SetErrorCode(OK);
1784
1785   return aSeq;
1786 }
1787
1788 //=============================================================================
1789 /*!
1790  *  MakePipeTShapeFillet
1791  *  Create a T-shape object with specified caracteristics for the main and
1792  *  the incident pipes (radius, width, half-length). A fillet is created
1793  *  on the junction of the pipes.
1794  *  Center of the shape is (0,0,0). The main plane of the T-shape is XOY.
1795  *  \param theR1 Internal radius of main pipe
1796  *  \param theW1 Width of main pipe
1797  *  \param theL1 Half-length of main pipe
1798  *  \param theR2 Internal radius of incident pipe (R2 < R1)
1799  *  \param theW2 Width of incident pipe (R2+W2 < R1+W1)
1800  *  \param theL2 Half-length of incident pipe
1801  *  \param theRF Radius of curvature of fillet.
1802  *  \param theHexMesh Boolean indicating if shape is prepared for hex mesh
1803  *  \return List of GEOM_Objects, containing the created shape and propagation groups.
1804  */
1805 //=============================================================================
1806 Handle(TColStd_HSequenceOfTransient)
1807 GEOMImpl_IAdvancedOperations::MakePipeTShapeFillet(double theR1, double theW1, double theL1,
1808                                                    double theR2, double theW2, double theL2,
1809                                                    double theRF, bool theHexMesh)
1810 {
1811   SetErrorCode(KO);
1812   //Add a new object
1813   Handle(GEOM_Object) aShape = GetEngine()->AddObject(GetDocID(), GEOM_TSHAPE);
1814   //Add a new shape function with parameters
1815   Handle(GEOM_Function) aFunction = aShape->AddFunction(GEOMImpl_PipeTShapeDriver::GetID(), TSHAPE_FILLET);
1816   if (aFunction.IsNull()) return NULL;
1817
1818   //Check if the function is set correctly
1819   if (aFunction->GetDriverGUID() != GEOMImpl_PipeTShapeDriver::GetID()) return NULL;
1820
1821   GEOMImpl_IPipeTShape aData(aFunction);
1822
1823   aData.SetR1(theR1);
1824   aData.SetW1(theW1);
1825   aData.SetL1(theL1);
1826   aData.SetR2(theR2);
1827   aData.SetW2(theW2);
1828   aData.SetL2(theL2);
1829   aData.SetRF(theRF);
1830   aData.SetHexMesh(theHexMesh);
1831
1832   //Compute the resulting value
1833   try {
1834 #if OCC_VERSION_LARGE > 0x06010000
1835     OCC_CATCH_SIGNALS;
1836 #endif
1837     if (!GetSolver()->ComputeFunction(aFunction)) {
1838       SetErrorCode("TShape driver failed");
1839       return NULL;
1840     }
1841   } catch (Standard_Failure) {
1842     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
1843     SetErrorCode(aFail->GetMessageString());
1844     return NULL;
1845   }
1846
1847   // BEGIN of fillet
1848   TopoDS_Shape aShapeShape = aShape->GetValue();
1849   TopTools_IndexedMapOfShape anEdgesIndices;
1850   TopExp::MapShapes(aShapeShape, anEdgesIndices);
1851   // Common edges on external cylinders
1852   Handle(GEOM_Object) box_e;
1853   if (theHexMesh) {
1854     box_e = my3DPrimOperations->MakeBoxDXDYDZ(theR2+theW2, theR2+theW2, theR1+theW1);
1855   }
1856   else {
1857     box_e = my3DPrimOperations->MakeBoxDXDYDZ(2*(theR2+theW2), 2*(theR2+theW2), theR1+theW1);
1858   }
1859   box_e->GetLastFunction()->SetDescription("");
1860   box_e = myTransformOperations->TranslateDXDYDZ(box_e, -(theR2+theW2), -(theR2+theW2), 0);
1861   box_e->GetLastFunction()->SetDescription("");
1862
1863   Handle(TColStd_HSequenceOfInteger) edges_e =
1864     myShapesOperations->GetShapesOnBoxIDs(box_e, aShape, TopAbs_EDGE, GEOMAlgo_ST_IN);
1865   box_e->GetLastFunction()->SetDescription("");
1866
1867   if (edges_e.IsNull() || edges_e->Length() == 0) {
1868     SetErrorCode("External edges not found");
1869     return NULL;
1870   }
1871   int nbEdgesInFillet = 0;
1872   std::list<int> theEdges;
1873   for (int i=1; i<=edges_e->Length();i++) {
1874     int edgeID = edges_e->Value(i);
1875     TopoDS_Shape theEdge = anEdgesIndices.FindKey(edgeID);
1876     TopExp_Explorer Ex(theEdge,TopAbs_VERTEX);
1877     while (Ex.More()) {
1878       gp_Pnt aPt = BRep_Tool::Pnt(TopoDS::Vertex(Ex.Current()));
1879       if (Abs(aPt.Z() - (theR1+theW1)) <= Precision::Confusion()) {
1880         nbEdgesInFillet ++;
1881         theEdges.push_back(edgeID);
1882       }
1883       Ex.Next();
1884     }
1885     if (theHexMesh && nbEdgesInFillet == 1)
1886       break;
1887   }
1888
1889   Handle(GEOM_Object) aFillet;
1890   try {
1891     aFillet = myLocalOperations->MakeFilletEdges(aShape, theRF, theEdges);
1892   }
1893   catch (Standard_Failure) {
1894     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
1895     SetErrorCode(aFail->GetMessageString());
1896     return NULL;
1897   }
1898   if (aFillet.IsNull()) {
1899     //SetErrorCode("Fillet can not be computed on the given shape with the given parameters");
1900     SetErrorCode(myLocalOperations->GetErrorCode());
1901     return NULL;
1902   }
1903   aFillet->GetLastFunction()->SetDescription("");
1904
1905   TopoDS_Shape aFilletShape = aFillet->GetValue();
1906   aFunction->SetValue(aFilletShape);
1907   // END of fillet
1908
1909   // BEGIN: Limit tolerances (debug)
1910   Handle(GEOM_Object) aCorr1 = myHealingOperations->LimitTolerance(aShape, 1e-07);
1911   TopoDS_Shape aCorr1Shape = aCorr1->GetValue();
1912   aShape->GetLastFunction()->SetValue(aCorr1Shape);
1913   aCorr1->GetLastFunction()->SetDescription("");
1914   // END: Limit tolerances (debug)
1915
1916   if (theHexMesh) {
1917     if (!MakePipeTShapePartition(aShape, theR1, theW1, theL1, theR2, theW2, theL2, 0, 0, theRF, false))
1918       return NULL;
1919     if (!MakePipeTShapeMirrorAndGlue(aShape, theR1, theW1, theL1, theR2, theW2, theL2))
1920       return NULL;
1921   }
1922
1923   Handle(TColStd_HSequenceOfTransient) aSeq = new TColStd_HSequenceOfTransient;
1924   aSeq->Append(aShape);
1925   if (theHexMesh) {
1926     /*
1927      * Get the groups: BEGIN
1928      */
1929     try {
1930       if (!MakeGroups(aShape, TSHAPE_FILLET, theR1, theW1, theL1, theR2, theW2, theL2, aSeq, gp_Trsf()))
1931         return NULL;
1932     }
1933     catch (Standard_Failure) {
1934       Handle(Standard_Failure) aFail = Standard_Failure::Caught();
1935       SetErrorCode(aFail->GetMessageString());
1936       return NULL;
1937     }
1938
1939     TCollection_AsciiString aListRes, anEntry;
1940     // Iterate over the sequence aSeq
1941     Standard_Integer aNbGroups = aSeq->Length();
1942     Standard_Integer i = 2;
1943     for (; i <= aNbGroups; i++) {
1944       Handle(Standard_Transient) anItem = aSeq->Value(i);
1945       if (anItem.IsNull()) continue;
1946       Handle(GEOM_Object) aGroup = Handle(GEOM_Object)::DownCast(anItem);
1947       if (aGroup.IsNull()) continue;
1948       //Make a Python command
1949       TDF_Tool::Entry(aGroup->GetEntry(), anEntry);
1950       aListRes += anEntry + ", ";
1951     }
1952
1953     aListRes.Trunc(aListRes.Length() - 2);
1954
1955     //Make a Python command
1956     GEOM::TPythonDump(aFunction)
1957       << "[" << aShape << ", " << aListRes.ToCString()
1958       << "] = geompy.MakePipeTShapeFillet(" << theR1 << ", " << theW1 << ", " << theL1 << ", " << theR2
1959       << ", " << theW2 << ", " << theL2 << ", " << theRF << ", " << theHexMesh << ")";
1960   }
1961   /*
1962    * Get the groups: END
1963    */
1964   else {
1965     //Make a Python command
1966     GEOM::TPythonDump(aFunction)
1967       << "[" << aShape << "] = geompy.MakePipeTShapeFillet(" << theR1 << ", " << theW1
1968       << ", " << theL1 << ", " << theR2 << ", " << theW2 << ", " << theL2 << ", " << theRF << ", "
1969       << theHexMesh << ")";
1970   }
1971
1972   SetErrorCode(OK);
1973
1974   return aSeq;
1975 }
1976
1977 //=============================================================================
1978 /*!
1979  *  MakePipeTShapeFilletWithPosition
1980  *  Create a T-shape object with specified caracteristics for the main and
1981  *  the incident pipes (radius, width, half-length). A fillet is created
1982  *  on the junction of the pipes.
1983  *  The extremities of the main pipe are located on junctions points P1 and P2.
1984  *  The extremity of the incident pipe is located on junction point P3.
1985  *  \param theR1 Internal radius of main pipe
1986  *  \param theW1 Width of main pipe
1987  *  \param theL1 Half-length of main pipe
1988  *  \param theR2 Internal radius of incident pipe (R2 < R1)
1989  *  \param theW2 Width of incident pipe (R2+W2 < R1+W1)
1990  *  \param theL2 Half-length of incident pipe
1991  *  \param theRF Radius of curvature of fillet
1992  *  \param theHexMesh Boolean indicating if shape is prepared for hex mesh
1993  *  \param theP1 1st junction point of main pipe
1994  *  \param theP2 2nd junction point of main pipe
1995  *  \param theP3 Junction point of incident pipe
1996  *  \return List of GEOM_Objects, containing the created shape and propagation groups.
1997  */
1998 //=============================================================================
1999 Handle(TColStd_HSequenceOfTransient)
2000 GEOMImpl_IAdvancedOperations::MakePipeTShapeFilletWithPosition(double theR1, double theW1, double theL1,
2001                                                                double theR2, double theW2, double theL2,
2002                                                                double theRF, bool theHexMesh,
2003                                                                Handle(GEOM_Object) theP1,
2004                                                                Handle(GEOM_Object) theP2,
2005                                                                Handle(GEOM_Object) theP3)
2006 {
2007   SetErrorCode(KO);
2008   //Add a new object
2009   Handle(GEOM_Object) aShape = GetEngine()->AddObject(GetDocID(), GEOM_TSHAPE);
2010   //Add a new shape function with parameters
2011   Handle(GEOM_Function) aFunction = aShape->AddFunction(GEOMImpl_PipeTShapeDriver::GetID(), TSHAPE_FILLET);
2012   if (aFunction.IsNull()) return NULL;
2013
2014   //Check if the function is set correctly
2015   if (aFunction->GetDriverGUID() != GEOMImpl_PipeTShapeDriver::GetID()) return NULL;
2016
2017   // Check new position
2018   if (!CheckCompatiblePosition(theL1, theL2, theP1, theP2, theP3, 0.01)) {
2019     return NULL;
2020   }
2021
2022   GEOMImpl_IPipeTShape aData(aFunction);
2023
2024   aData.SetR1(theR1);
2025   aData.SetW1(theW1);
2026   aData.SetL1(theL1);
2027   aData.SetR2(theR2);
2028   aData.SetW2(theW2);
2029   aData.SetL2(theL2);
2030   aData.SetRF(theRF);
2031   aData.SetHexMesh(theHexMesh);
2032
2033   //Compute the resulting value
2034   try {
2035 #if OCC_VERSION_LARGE > 0x06010000
2036     OCC_CATCH_SIGNALS;
2037 #endif
2038     if (!GetSolver()->ComputeFunction(aFunction)) {
2039       SetErrorCode("TShape driver failed");
2040       return NULL;
2041     }
2042   } catch (Standard_Failure) {
2043     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
2044     SetErrorCode(aFail->GetMessageString());
2045     return NULL;
2046   }
2047
2048   // BEGIN of fillet
2049   TopoDS_Shape aShapeShape = aShape->GetValue();
2050   TopTools_IndexedMapOfShape anEdgesIndices;
2051   TopExp::MapShapes(aShapeShape, anEdgesIndices);
2052   // Common edges on external cylinders
2053   Handle(GEOM_Object) box_e;
2054   if (theHexMesh) {
2055     box_e = my3DPrimOperations->MakeBoxDXDYDZ(theR2+theW2, theR2+theW2, theR1+theW1);
2056   }
2057   else {
2058     box_e = my3DPrimOperations->MakeBoxDXDYDZ(2*(theR2+theW2), 2*(theR2+theW2), theR1+theW1);
2059   }
2060   box_e->GetLastFunction()->SetDescription("");
2061   box_e = myTransformOperations->TranslateDXDYDZ(box_e, -(theR2+theW2), -(theR2+theW2), 0);
2062   box_e->GetLastFunction()->SetDescription("");
2063
2064   Handle(TColStd_HSequenceOfInteger) edges_e =
2065     myShapesOperations->GetShapesOnBoxIDs(box_e, aShape, TopAbs_EDGE, GEOMAlgo_ST_IN);
2066   box_e->GetLastFunction()->SetDescription("");
2067
2068   if (edges_e.IsNull() || edges_e->Length() == 0) {
2069     SetErrorCode("External edges not found");
2070     return NULL;
2071   }
2072   int nbEdgesInFillet = 0;
2073   std::list<int> theEdges;
2074   for (int i=1; i<=edges_e->Length();i++) {
2075     int edgeID = edges_e->Value(i);
2076     TopoDS_Shape theEdge = anEdgesIndices.FindKey(edgeID);
2077     TopExp_Explorer Ex(theEdge,TopAbs_VERTEX);
2078     while (Ex.More()) {
2079       gp_Pnt aPt = BRep_Tool::Pnt(TopoDS::Vertex(Ex.Current()));
2080       if (Abs(aPt.Z() - (theR1+theW1)) <= Precision::Confusion()) {
2081         nbEdgesInFillet ++;
2082         theEdges.push_back(edgeID);
2083       }
2084       Ex.Next();
2085     }
2086     if (theHexMesh && nbEdgesInFillet == 1)
2087       break;
2088   }
2089
2090   Handle(GEOM_Object) aFillet;
2091   try {
2092     aFillet = myLocalOperations->MakeFilletEdges(aShape, theRF, theEdges);
2093   }
2094   catch (Standard_Failure) {
2095     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
2096     SetErrorCode(aFail->GetMessageString());
2097     return NULL;
2098   }
2099   if (aFillet.IsNull()) {
2100     SetErrorCode("Fillet can not be computed on the given shape with the given parameters");
2101     return NULL;
2102   }
2103   aFillet->GetLastFunction()->SetDescription("");
2104
2105   TopoDS_Shape aFilletShape = aFillet->GetValue();
2106   aFunction->SetValue(aFilletShape);
2107   // END of fillet
2108
2109   // BEGIN: Limit tolerances (debug)
2110   Handle(GEOM_Object) aCorr1 = myHealingOperations->LimitTolerance(aShape, 1e-07);
2111   TopoDS_Shape aCorr1Shape = aCorr1->GetValue();
2112   aShape->GetLastFunction()->SetValue(aCorr1Shape);
2113   aCorr1->GetLastFunction()->SetDescription("");
2114   // END: Limit tolerances (debug)
2115
2116   if (theHexMesh) {
2117     if (!MakePipeTShapePartition(aShape, theR1, theW1, theL1, theR2, theW2, theL2, 0, 0, theRF, false))
2118       return NULL;
2119     if (!MakePipeTShapeMirrorAndGlue(aShape, theR1, theW1, theL1, theR2, theW2, theL2))
2120       return NULL;
2121   }
2122
2123   TopoDS_Shape Te = aShape->GetValue();
2124
2125   // Set Position
2126   gp_Trsf aTrsf = GetPositionTrsf(theL1, theL2, theP1, theP2, theP3);
2127   BRepBuilderAPI_Transform aTransformation(Te, aTrsf, Standard_False);
2128   TopoDS_Shape aTrsf_Shape = aTransformation.Shape();
2129   aFunction->SetValue(aTrsf_Shape);
2130   Handle(TColStd_HSequenceOfTransient) aSeq = new TColStd_HSequenceOfTransient;
2131   aSeq->Append(aShape);
2132   if (theHexMesh) {
2133     /*
2134      * Get the groups: BEGIN
2135      */
2136     try {
2137       if (!MakeGroups(aShape, TSHAPE_FILLET, theR1, theW1, theL1, theR2, theW2, theL2, aSeq, aTrsf))
2138         return NULL;
2139     }
2140     catch (Standard_Failure) {
2141       Handle(Standard_Failure) aFail = Standard_Failure::Caught();
2142       SetErrorCode(aFail->GetMessageString());
2143       return NULL;
2144     }
2145
2146     TCollection_AsciiString aListRes, anEntry;
2147     // Iterate over the sequence aSeq
2148     Standard_Integer aNbGroups = aSeq->Length();
2149     Standard_Integer i = 2;
2150     for (; i <= aNbGroups; i++) {
2151       Handle(Standard_Transient) anItem = aSeq->Value(i);
2152       if (anItem.IsNull()) continue;
2153       Handle(GEOM_Object) aGroup = Handle(GEOM_Object)::DownCast(anItem);
2154       if (aGroup.IsNull()) continue;
2155       //Make a Python command
2156       TDF_Tool::Entry(aGroup->GetEntry(), anEntry);
2157       aListRes += anEntry + ", ";
2158     }
2159
2160     aListRes.Trunc(aListRes.Length() - 2);
2161
2162     //Make a Python command
2163     GEOM::TPythonDump(aFunction)
2164       << "[" << aShape << ", " << aListRes.ToCString()
2165       << "] = geompy.MakePipeTShapeFillet(" << theR1 << ", " << theW1 << ", " << theL1 << ", " << theR2
2166       << ", " << theW2 << ", " << theL2 << ", " << theRF << ", " << theHexMesh << ", " << theP1 << ", "
2167       << theP2 << ", " << theP3 << ")";
2168   }
2169   /*
2170    * Get the groups: END
2171    */
2172   else {
2173     //Make a Python command
2174     GEOM::TPythonDump(aFunction)
2175       << "[" << aShape << "] = geompy.MakePipeTShapeFillet(" << theR1 << ", " << theW1
2176       << ", " << theL1 << ", " << theR2 << ", " << theW2 << ", " << theL2 << ", " << theRF << ", "
2177       << theHexMesh << ", " << theP1 << ", " << theP2 << ", " << theP3 << ")";
2178   }
2179
2180   SetErrorCode(OK);
2181
2182   return aSeq;
2183 }
2184
2185 /*@@ insert new functions before this line @@ do not remove this line @@ do not remove this line @@*/