]> SALOME platform Git repositories - modules/geom.git/blob - src/GEOMImpl/GEOMImpl_IAdvancedOperations.cxx
Salome HOME
Merge from V6_5_BR 05/06/2012
[modules/geom.git] / src / GEOMImpl / GEOMImpl_IAdvancedOperations.cxx
1 // Copyright (C) 2007-2012  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 //       std::cout << "Coords: " << aP.X() << ", " << aP.Y() << ", " << aP.Z() << std::endl;
693       if (Abs(aP.X()) <= Precision::Confusion()) {
694         if (Abs(aP.Y()) - theR1 <= Precision::Confusion()) {
695           vi1 = v;
696         }
697       } else if (Abs(aP.Y()) <= Precision::Confusion()) {
698         if (Abs(aP.X()) - theR1 <= Precision::Confusion()) {
699           vi2 = v;
700         }
701       }
702     }
703
704     std::list<Handle(GEOM_Object)> theShapes;
705
706     if (isNormal) {
707       Handle(GEOM_Object) ve1, ve2;
708
709       Handle(GEOM_Object) box_e = my3DPrimOperations->MakeBoxDXDYDZ(aR2Ext, aR2Ext, aR1Ext);
710       box_e->GetLastFunction()->SetDescription("");
711       box_e = myTransformOperations->TranslateDXDYDZ(box_e, -aR2Ext, -aR2Ext, 0);
712       box_e->GetLastFunction()->SetDescription("");
713       // Common edges on external cylinder
714       aFunction = theShape->GetLastFunction();
715       theDesc = aFunction->GetDescription();
716       Handle(TColStd_HSequenceOfTransient) edges_e =
717         myShapesOperations->GetShapesOnBox(box_e, theShape, TopAbs_EDGE, GEOMAlgo_ST_IN);
718       // Recover previous description to get rid of Propagate dump
719       aFunction->SetDescription(theDesc);
720       if (edges_e.IsNull() || edges_e->Length() == 0) {
721         SetErrorCode("External edges not found");
722         return false;
723       }
724       for (int i=1; i<=edges_e->Length();i++) {
725         Handle(GEOM_Object) anObj = Handle(GEOM_Object)::DownCast(edges_e->Value(i));
726         anObj->GetLastFunction()->SetDescription("");
727       }
728
729       // search for vertices located on both external pipes
730       aFunction = theShape->GetLastFunction();
731       theDesc = aFunction->GetDescription();
732       Handle(TColStd_HSequenceOfTransient) vertices_e =
733         myShapesOperations->GetShapesOnBox(box_e, theShape, TopAbs_VERTEX, GEOMAlgo_ST_ONIN);
734       // Recover previous description to get rid of Propagate dump
735       aFunction->SetDescription(theDesc);
736       if (vertices_e.IsNull() || vertices_e->Length() == 0) {
737         SetErrorCode("External vertices not found");
738         return false;
739       }
740
741       for (int i = 1; i <= vertices_e->Length(); i++) {
742         Handle(GEOM_Object) v = Handle(GEOM_Object)::DownCast(vertices_e->Value(i));
743         v->GetLastFunction()->SetDescription("");
744         TopoDS_Vertex aVertex = TopoDS::Vertex(v->GetValue());
745         gp_Pnt aP = BRep_Tool::Pnt(aVertex);
746 //         std::cout << "Coords: " << aP.X() << ", " << aP.Y() << ", " << aP.Z() << std::endl;
747         if (Abs(aP.X()) <= Precision::Confusion()) {
748           if (Abs(aP.Y()) - theR2 > Precision::Confusion()) {
749             ve1 = v;
750           }
751         } else if (Abs(aP.Y()) <= Precision::Confusion()) {
752           if (Abs(aP.X()) - theR2 > Precision::Confusion()) {
753             ve2 = v;
754           }
755         }
756         if ( !ve1.IsNull() && !ve2.IsNull())
757           break;
758       }
759       Handle(GEOM_Object) edge_e1, edge_e2;
760       
761       edge_e1 = myBasicOperations->MakeLineTwoPnt(ve1, vi1);
762       if (edge_e1.IsNull()) {
763         SetErrorCode("Edge 1 could not be built");
764         return false;
765       }
766       
767       edge_e2 = myBasicOperations->MakeLineTwoPnt(ve2, vi2);
768       if (edge_e2.IsNull()) {
769         SetErrorCode("Edge 2 could not be built");
770         return false;
771       }
772
773       edge_e1->GetLastFunction()->SetDescription("");
774       edge_e2->GetLastFunction()->SetDescription("");
775
776       std::list<Handle(GEOM_Object)> edge_e_elist;
777       edge_e_elist.push_back(arete_intersect_int);
778       edge_e_elist.push_back(edge_e1);
779       edge_e_elist.push_back(Handle(GEOM_Object)::DownCast(edges_e->Value(1)));
780       edge_e_elist.push_back(edge_e2);
781       wire_t = myShapesOperations->MakeWire(edge_e_elist, 1e-7);
782       if (wire_t.IsNull()) {
783         SetErrorCode("Impossible to build wire");
784         return false;
785       }
786       wire_t->GetLastFunction()->SetDescription("");
787       face_t = myShapesOperations->MakeFace(wire_t, false);
788       if (face_t.IsNull()) {
789         SetErrorCode("Impossible to build face");
790         return false;
791       }
792       face_t->GetLastFunction()->SetDescription("");
793       
794       theShapes.push_back(theShape);
795       theShapes.push_back(vi1);
796       theShapes.push_back(vi2);
797       theShapes.push_back(ve1);
798       theShapes.push_back(ve2);
799       theShapes.push_back(edge_e1);
800       theShapes.push_back(edge_e2);
801       theShapes.push_back(wire_t);
802       theShapes.push_back(face_t);
803     }
804     else {
805       Handle(GEOM_Object) P1, P2, P3, P4, P5, P6;
806       int idP1, idP2, idP3, idP4;
807       int PZX, PZY;
808       double ZX=0, ZY=0;
809       std::vector<int> LX;
810       std::vector<int> LY;
811       Handle(GEOM_Object) box_e = my3DPrimOperations->MakeBoxDXDYDZ
812         (theVertCylinderRadius, theVertCylinderRadius, theHoriCylinderRadius);
813       box_e->GetLastFunction()->SetDescription("");
814       box_e = myTransformOperations->TranslateDXDYDZ
815         (box_e, -theVertCylinderRadius, -theVertCylinderRadius, 0);
816       box_e->GetLastFunction()->SetDescription("");
817
818       aFunction = theShape->GetLastFunction();
819       theDesc = aFunction->GetDescription();
820       Handle(TColStd_HSequenceOfTransient) extremVertices =
821         myShapesOperations->GetShapesOnBox(box_e, theShape, TopAbs_VERTEX, GEOMAlgo_ST_ONIN);
822       // Recover previous description to get rid of Propagate dump
823       aFunction->SetDescription(theDesc);
824
825       if (extremVertices.IsNull() || extremVertices->Length() == 0) {
826         if (theRF == 0)
827           SetErrorCode("Vertices on chamfer not found");
828         else
829           SetErrorCode("Vertices on fillet not found");
830         return false;
831       }
832
833       theShapes.push_back(theShape);
834       theShapes.push_back(box_e);
835       if (extremVertices->Length() != 6) {
836         //           for (int i=1; i<=extremVertices->Length(); i++){
837         //             theShapes.push_back(Handle(GEOM_Object)::DownCast(extremVertices->Value(i)));
838         //           }
839         //           Handle(GEOM_Object) aCompound = myShapesOperations->MakeCompound(theShapes);
840         //           TopoDS_Shape aCompoundShape = aCompound->GetValue();
841         //           theShape->GetLastFunction()->SetValue(aCompoundShape);
842         SetErrorCode("Bad number of vertices on chamfer found");
843         return false;
844       }
845
846       for (int i=1; i<=extremVertices->Length(); i++){
847         Handle(GEOM_Object) aV = Handle(GEOM_Object)::DownCast(extremVertices->Value(i));
848         aV->GetLastFunction()->SetDescription("");
849         gp_Pnt aP = BRep_Tool::Pnt(TopoDS::Vertex(aV->GetValue()));
850
851         if (Abs(aP.X()) <= Precision::Confusion()) {
852           if (Abs(aP.Y()) - theR2 > Precision::Confusion()) {
853             LX.push_back(i);
854             if  (aP.Z()-ZX > Precision::Confusion()) {
855               ZX = aP.Z();
856               PZX = i;
857             }
858           }
859         }
860         else {
861           if (Abs(aP.X()) - theR2 > Precision::Confusion()) {
862             LY.push_back(i);
863             if (aP.Z() - ZY > Precision::Confusion()) {
864               ZY = aP.Z();
865               PZY = i;
866             }
867           }
868         }
869       }
870
871       idP2 = PZX;
872       idP4 = PZY;
873       idP1 = LX.at(0);
874       if (LX.at(0) == PZX)
875         idP1 = LX.at(1);
876       idP3 = LY.at(0);
877       if (LY.at(0) == PZY)
878         idP3 = LY.at(1);
879
880       P1 = Handle(GEOM_Object)::DownCast(extremVertices->Value(idP1));
881       P2 = Handle(GEOM_Object)::DownCast(extremVertices->Value(idP2));
882       P3 = Handle(GEOM_Object)::DownCast(extremVertices->Value(idP3));
883       P4 = Handle(GEOM_Object)::DownCast(extremVertices->Value(idP4));
884
885       Handle(GEOM_Object) Cote_1 = myBasicOperations->MakeLineTwoPnt(P1, vi1);
886       if (Cote_1.IsNull()) {
887         SetErrorCode("Impossible to build edge in thickness");
888         return false;
889       }
890       Cote_1->GetLastFunction()->SetDescription("");
891
892       Handle(GEOM_Object) Cote_2 = myBasicOperations->MakeLineTwoPnt(vi2, P3);
893       if (Cote_2.IsNull()) {
894         SetErrorCode("Impossible to build edge in thickness");
895         return false;
896       }
897       Cote_2->GetLastFunction()->SetDescription("");
898
899       // edge_chan_princ = arete du chanfrein (ou raccord) sur le tuyau principal
900       // edge_chan_inc = arete du chanfrein (ou raccord) sur le tuyau incident
901       //         std::cerr << "Getting chamfer edge on main pipe" << std::endl;
902       Handle(GEOM_Object) edge_chan_princ = myBlocksOperations->GetEdge(theShape, P1, P3);
903       if (edge_chan_princ.IsNull()) {
904         SetErrorCode("Impossible to find edge on main pipe");
905         return false;
906       }
907       edge_chan_princ->GetLastFunction()->SetDescription("");
908
909       Handle(GEOM_Object) edge_chan_inc = myBlocksOperations->GetEdge(theShape, P2, P4);
910       if (edge_chan_inc.IsNull()) {
911         SetErrorCode("Impossible to find edge on incident pipe");
912         return false;
913       }
914       edge_chan_inc->GetLastFunction()->SetDescription("");
915
916       std::list<Handle(GEOM_Object)> edgeList1;
917       edgeList1.push_back(edge_chan_princ);
918       edgeList1.push_back(Cote_1);
919       edgeList1.push_back(arete_intersect_int);
920       edgeList1.push_back(Cote_2);
921
922       //         std::cerr << "Creating wire 1" << std::endl;
923       wire_t = myShapesOperations->MakeWire(edgeList1, 1e-7);
924       if (wire_t.IsNull()) {
925         SetErrorCode("Impossible to build wire");
926         return false;
927       }
928       wire_t->GetLastFunction()->SetDescription("");
929
930       //         std::cerr << "Creating face 1" << std::endl;
931       face_t = myShapesOperations->MakeFace(wire_t, false);
932       if (face_t.IsNull()) {
933         SetErrorCode("Impossible to build face");
934         return false;
935       }
936       face_t->GetLastFunction()->SetDescription("");
937       theShapes.push_back(face_t);
938
939       gp_Pnt aP2 = BRep_Tool::Pnt(TopoDS::Vertex(P2->GetValue()));
940       gp_Pnt aP5 = BRep_Tool::Pnt(TopoDS::Vertex(vi1->GetValue()));
941       double deltaZ = aP2.Z() - aP5.Z();
942       //         std::cerr << "Creating new point from vi1 with deltaZ = " << deltaZ << std::endl;
943       Handle(GEOM_Object) P5bis = myTransformOperations->TranslateDXDYDZCopy(vi1, 0, 0, deltaZ);
944       if (P5bis.IsNull()) {
945         SetErrorCode("Impossible to translate vertex");
946         return false;
947       }
948       P5bis->GetLastFunction()->SetDescription("");
949
950       gp_Pnt aP4 = BRep_Tool::Pnt(TopoDS::Vertex(P4->GetValue()));
951       gp_Pnt aP6 = BRep_Tool::Pnt(TopoDS::Vertex(vi2->GetValue()));
952       deltaZ = aP4.Z() - aP6.Z();
953       //         std::cerr << "Creating new point from vi2 with deltaZ = " << deltaZ << std::endl;
954       Handle(GEOM_Object) P6bis = myTransformOperations->TranslateDXDYDZCopy(vi2, 0, 0, deltaZ);
955       if (P6bis.IsNull()) {
956         SetErrorCode("Impossible to translate vertex");
957         return false;
958       }
959       P6bis->GetLastFunction()->SetDescription("");
960
961       //         std::cerr << "Creating new line 1 from 2 previous points" << std::endl;
962       Handle(GEOM_Object) Cote_3 = myBasicOperations->MakeLineTwoPnt(P5bis, P2);
963       if (Cote_3.IsNull()) {
964         SetErrorCode("Impossible to build edge in thickness");
965         return false;
966       }
967       Cote_3->GetLastFunction()->SetDescription("");
968
969       //         std::cerr << "Creating new line 2 from 2 previous points" << std::endl;
970       Handle(GEOM_Object) Cote_4 = myBasicOperations->MakeLineTwoPnt(P6bis, P4);
971       if (Cote_4.IsNull()) {
972         SetErrorCode("Impossible to build edge in thickness");
973         return false;
974       }
975       Cote_4->GetLastFunction()->SetDescription("");
976
977       //         std::cerr << "Creating new line 3 from 2 previous points" << std::endl;
978       Handle(GEOM_Object) Cote_5 = myBasicOperations->MakeLineTwoPnt(P5bis, P6bis);
979       if (Cote_4.IsNull()) {
980         SetErrorCode("Impossible to build edge in thickness");
981         return false;
982       }
983       Cote_5->GetLastFunction()->SetDescription("");
984
985       //std::list<Handle(GEOM_Object)> edgeList2;
986       //edgeList2.push_back(edge_chan_inc);
987       //edgeList2.push_back(Cote_3);
988       //edgeList2.push_back(Cote_5);
989       //edgeList2.push_back(Cote_4);
990       //         std::cerr << "Creating wire 2" << std::endl;
991       //wire_t2 = myShapesOperations->MakeWire(edgeList2, 1e-7);
992       //if (wire_t2.IsNull()) {
993       //  SetErrorCode("Impossible to build wire");
994       //  return false;
995       //}
996       //wire_t2->GetLastFunction()->SetDescription("");
997       //         std::cerr << "Creating face 2" << std::endl;
998       //face_t2 = myShapesOperations->MakeFace(wire_t2, false);
999       face_t2 = my3DPrimOperations->MakePrismVecH(edge_chan_inc, Cote_4, - 2.0*theR2);
1000       if (face_t2.IsNull()) {
1001         SetErrorCode("Impossible to build face");
1002         return false;
1003       }
1004       face_t2->GetLastFunction()->SetDescription("");
1005       theShapes.push_back(face_t2);
1006     }
1007
1008     // Planes
1009     Handle(GEOM_Object) aP0 = myBasicOperations->MakePointXYZ(0, 0, 0);
1010     Handle(GEOM_Object) aVZ = myBasicOperations->MakeVectorDXDYDZ(0, 0, 1);
1011     Handle(GEOM_Object) aVXZ = myBasicOperations->MakeVectorDXDYDZ(aR1Ext, 0, 0.5*(theL1+theVertCylinderRadius));
1012     Handle(GEOM_Object) aPlnOZ = myBasicOperations->MakePlanePntVec(aP0, aVZ, aSize);
1013     Handle(GEOM_Object) aPlnOXZ = myBasicOperations->MakePlanePntVec(aP0, aVXZ, aSize);
1014     aP0->GetLastFunction()->SetDescription("");
1015     aVZ->GetLastFunction()->SetDescription("");
1016     aVXZ->GetLastFunction()->SetDescription("");
1017     aPlnOZ->GetLastFunction()->SetDescription("");
1018     aPlnOXZ->GetLastFunction()->SetDescription("");
1019     theShapes.push_back(aPlnOZ);
1020     theShapes.push_back(aPlnOXZ);
1021
1022     // Partition
1023     Handle(TColStd_HSequenceOfTransient) partitionShapes = new TColStd_HSequenceOfTransient;
1024     Handle(TColStd_HSequenceOfTransient) theTools = new TColStd_HSequenceOfTransient;
1025     Handle(TColStd_HSequenceOfTransient) theKeepInside = new TColStd_HSequenceOfTransient;
1026     Handle(TColStd_HSequenceOfTransient) theRemoveInside = new TColStd_HSequenceOfTransient;
1027     Handle(TColStd_HArray1OfInteger) theMaterials;
1028     partitionShapes->Append(theShape);
1029     theTools->Append(aPlnOZ);
1030     if (Abs(aR1Ext - aR2Ext) > Precision::Confusion() )
1031       theTools->Append(aPlnOXZ);
1032     theTools->Append(face_t);
1033     if (!isNormal)
1034       theTools->Append(face_t2);
1035
1036     Te3 = myBooleanOperations->MakePartition
1037               (partitionShapes, theTools, theKeepInside, theRemoveInside,
1038               TopAbs_SOLID, false, theMaterials, 0, false);
1039     if (Te3.IsNull()) {
1040       SetErrorCode("Impossible to build partition of TShape");
1041       return false;
1042     }
1043     Te3->GetLastFunction()->SetDescription("");
1044
1045     // Last verification: result should be a block
1046     std::list<GEOMImpl_IBlocksOperations::BCError> errList;
1047     if (!myBlocksOperations->CheckCompoundOfBlocks(Te3,errList)) {
1048       SetErrorCode("TShape is not a compound of block");
1049       return false;
1050     }
1051     
1052 //     // BEGIN Compound of created shapes - Only for debug purpose
1053 //     theShapes.clear();
1054 //     theShapes.push_back(theShape);
1055 //     theShapes.push_back(aPlnOZ);
1056 //     if (Abs(aR1Ext - aR2Ext) > Precision::Confusion() )
1057 //       theShapes.push_back(aPlnOXZ);
1058 //     theShapes.push_back(face_t);
1059 //     if (!isNormal)
1060 //       theShapes.push_back(face_t2);
1061 // 
1062 //     Handle(GEOM_Object) aCompound = myShapesOperations->MakeCompound(theShapes);
1063 //     TopoDS_Shape aCompoundShape = aCompound->GetValue();
1064 //     theShape->GetLastFunction()->SetValue(aCompoundShape);
1065 //     // END Compound of created shapes - Only for debug purpose
1066     
1067     TopoDS_Shape aShape = Te3->GetValue();
1068     theShape->GetLastFunction()->SetValue(aShape);
1069   } catch (Standard_Failure) {
1070     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
1071     SetErrorCode(aFail->GetMessageString());
1072     return false;
1073   }
1074
1075   SetErrorCode(OK);
1076   return true;
1077 }
1078
1079 // Mirror and glue faces
1080 bool GEOMImpl_IAdvancedOperations::MakePipeTShapeMirrorAndGlue(Handle(GEOM_Object) theShape,
1081                                                                double theR1, double theW1, double theL1,
1082                                                                double theR2, double theW2, double theL2)
1083 {
1084   SetErrorCode(KO);
1085
1086   // Useful values
1087   double aSize = 2*(theL1 + theL2);
1088   double aR1Ext = theR1 + theW1;
1089
1090   // Planes
1091   Handle(GEOM_Object) aP0 = myBasicOperations->MakePointXYZ(0, 0, 0);
1092   aP0->GetLastFunction()->SetDescription("");
1093   Handle(GEOM_Object) aVX = myBasicOperations->MakeVectorDXDYDZ(1, 0, 0);
1094   Handle(GEOM_Object) aVY = myBasicOperations->MakeVectorDXDYDZ(0, 1, 0);
1095   aVX->GetLastFunction()->SetDescription("");
1096   aVY->GetLastFunction()->SetDescription("");
1097   Handle(GEOM_Object) aPlane_OX = myBasicOperations->MakePlanePntVec(aP0, aVX, 2*(aR1Ext + theL2));
1098   Handle(GEOM_Object) aPlane_OY = myBasicOperations->MakePlanePntVec(aP0, aVY, aSize);
1099   aPlane_OX->GetLastFunction()->SetDescription("");
1100   aPlane_OY->GetLastFunction()->SetDescription("");
1101
1102   Handle(GEOM_Object) Te4 = myTransformOperations->MirrorPlaneCopy(theShape, aPlane_OX);
1103   if (Te4.IsNull()) {
1104     SetErrorCode("Impossible to build mirror of quarter TShape");
1105     return false;
1106   }
1107
1108   Handle(GEOM_Object) Te5 = myTransformOperations->MirrorPlaneCopy(theShape, aPlane_OY);
1109   if (Te5.IsNull()) {
1110     SetErrorCode("Impossible to build mirror of half TShape");
1111     return false;
1112   }
1113
1114   Handle(GEOM_Object) Te6 = myTransformOperations->MirrorPlaneCopy(Te4, aPlane_OY);
1115   if (Te6.IsNull()) {
1116     SetErrorCode("Impossible to build mirror of half TShape");
1117     return false;
1118   }
1119
1120   std::list<Handle(GEOM_Object)> aShapesList;
1121   aShapesList.push_back(theShape);
1122   aShapesList.push_back(Te4);
1123   aShapesList.push_back(Te5);
1124   aShapesList.push_back(Te6);
1125   Handle(GEOM_Object) Te7 = myShapesOperations->MakeCompound(aShapesList);
1126   if (Te7.IsNull()) {
1127     SetErrorCode("Impossible to build compound");
1128     return false;
1129   }
1130
1131   Handle(GEOM_Object) Te8 = myShapesOperations->MakeGlueFaces(Te7, 1e-7, true);
1132   if (Te8.IsNull()) {
1133     SetErrorCode("Impossible to glue faces of TShape");
1134     return false;
1135   }
1136
1137   TopoDS_Shape aShape = Te8->GetValue();
1138
1139   theShape->GetLastFunction()->SetValue(aShape);
1140
1141   Te4->GetLastFunction()->SetDescription("");
1142   Te5->GetLastFunction()->SetDescription("");
1143   Te6->GetLastFunction()->SetDescription("");
1144   Te7->GetLastFunction()->SetDescription("");
1145   Te8->GetLastFunction()->SetDescription("");
1146
1147   SetErrorCode(OK);
1148   return true;
1149 }
1150
1151 //=============================================================================
1152 /*!
1153  *  MakePipeTShape
1154  *  \brief Create a T-shape object with specified caracteristics for the main and
1155  *         the incident pipes (radius, width, half-length).
1156  *         Center of the shape is (0,0,0). The main plane of the T-shape is XOY.
1157  *  \param theR1 Internal radius of main pipe
1158  *  \param theW1 Width of main pipe
1159  *  \param theL1 Half-length of main pipe
1160  *  \param theR2 Internal radius of incident pipe (R2 < R1)
1161  *  \param theW2 Width of incident pipe (R2+W2 < R1+W1)
1162  *  \param theL2 Half-length of incident pipe
1163  *  \param theHexMesh Boolean indicating if shape is prepared for hex mesh
1164  *  \return List of GEOM_Objects, containing the created shape and propagation groups.
1165  */
1166 //=============================================================================
1167 Handle(TColStd_HSequenceOfTransient)
1168   GEOMImpl_IAdvancedOperations::MakePipeTShape(double theR1, double theW1, double theL1,
1169                                                double theR2, double theW2, double theL2,
1170                                                bool theHexMesh)
1171 {
1172   MESSAGE("GEOMImpl_IAdvancedOperations::MakePipeTShape");
1173   SetErrorCode(KO);
1174   //Add a new object
1175   Handle(GEOM_Object) aShape = GetEngine()->AddObject(GetDocID(), GEOM_TSHAPE);
1176
1177   //Add a new shape function with parameters
1178   Handle(GEOM_Function) aFunction = aShape->AddFunction(GEOMImpl_PipeTShapeDriver::GetID(), TSHAPE_BASIC);
1179   if (aFunction.IsNull()) return NULL;
1180
1181   //Check if the function is set correctly
1182   if (aFunction->GetDriverGUID() != GEOMImpl_PipeTShapeDriver::GetID()) return NULL;
1183
1184   GEOMImpl_IPipeTShape aData(aFunction);
1185
1186   aData.SetR1(theR1);
1187   aData.SetW1(theW1);
1188   aData.SetL1(theL1);
1189   aData.SetR2(theR2);
1190   aData.SetW2(theW2);
1191   aData.SetL2(theL2);
1192   aData.SetHexMesh(theHexMesh);
1193
1194   //Compute the resulting value
1195   try {
1196 #if OCC_VERSION_LARGE > 0x06010000
1197     OCC_CATCH_SIGNALS;
1198 #endif
1199     if (!GetSolver()->ComputeFunction(aFunction)) {
1200       SetErrorCode("TShape driver failed");
1201       return NULL;
1202     }
1203     if (theHexMesh) {
1204       if (!MakePipeTShapePartition(aShape, theR1, theW1, theL1, theR2, theW2, theL2))
1205         return NULL;
1206       if (!MakePipeTShapeMirrorAndGlue(aShape, theR1, theW1, theL1, theR2, theW2, theL2))
1207         return NULL;
1208     }
1209   } catch (Standard_Failure) {
1210     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
1211     SetErrorCode(aFail->GetMessageString());
1212     return NULL;
1213   }
1214
1215
1216   Handle(TColStd_HSequenceOfTransient) aSeq = new TColStd_HSequenceOfTransient;
1217   aSeq->Append(aShape);
1218
1219   if (theHexMesh) {
1220     /*
1221      * Get the groups: BEGIN
1222      */
1223     try {
1224       if (!MakeGroups(aShape, TSHAPE_BASIC, theR1, theW1, theL1, theR2, theW2, theL2, aSeq, gp_Trsf()))
1225         return NULL;
1226     }
1227     catch (Standard_Failure) {
1228       Handle(Standard_Failure) aFail = Standard_Failure::Caught();
1229       SetErrorCode(aFail->GetMessageString());
1230       return NULL;
1231     }
1232
1233     TCollection_AsciiString aListRes, anEntry;
1234     // Iterate over the sequence aSeq
1235     Standard_Integer aNbGroups = aSeq->Length();
1236     Standard_Integer i = 2;
1237     for (; i <= aNbGroups; i++) {
1238       Handle(Standard_Transient) anItem = aSeq->Value(i);
1239       if (anItem.IsNull()) continue;
1240       Handle(GEOM_Object) aGroup = Handle(GEOM_Object)::DownCast(anItem);
1241       if (aGroup.IsNull()) continue;
1242       //Make a Python command
1243       TDF_Tool::Entry(aGroup->GetEntry(), anEntry);
1244       aListRes += anEntry + ", ";
1245     }
1246
1247     aListRes.Trunc(aListRes.Length() - 2);
1248
1249     //Make a Python command
1250     GEOM::TPythonDump(aFunction)
1251       << "[" << aShape << ", " << aListRes.ToCString() << "] = geompy.MakePipeTShape("
1252       << theR1 << ", " << theW1 << ", " << theL1 << ", " << theR2 << ", " << theW2 << ", " << theL2 << ", "
1253       << theHexMesh << ")";
1254   }
1255   /*
1256    * Get the groups: END
1257    */
1258   else {
1259     //Make a Python command
1260     GEOM::TPythonDump(aFunction)
1261       << "[" << aShape << "] = geompy.MakePipeTShape(" << theR1 << ", " << theW1 << ", "
1262       << theL1 << ", " << theR2 << ", " << theW2 << ", " << theL2 << ", " << theHexMesh << ")";
1263   }
1264
1265   SetErrorCode(OK);
1266
1267   return aSeq;
1268 }
1269
1270 //=============================================================================
1271 /*!
1272  *  MakePipeTShapeWithPosition
1273  *  Create a T-shape object with specified caracteristics for the main and
1274  *  the incident pipes (radius, width, half-length).
1275  *  The extremities of the main pipe are located on junctions points P1 and P2.
1276  *  The extremity of the incident pipe is located on junction point P3.
1277  *  \param theR1 Internal radius of main pipe
1278  *  \param theW1 Width of main pipe
1279  *  \param theL1 Half-length of main pipe
1280  *  \param theR2 Internal radius of incident pipe (R2 < R1)
1281  *  \param theW2 Width of incident pipe (R2+W2 < R1+W1)
1282  *  \param theL2 Half-length of incident pipe
1283  *  \param theHexMesh Boolean indicating if shape is prepared for hex mesh
1284  *  \param theP1 1st junction point of main pipe
1285  *  \param theP2 2nd junction point of main pipe
1286  *  \param theP3 Junction point of incident pipe
1287  *  \return List of GEOM_Objects, containing the created shape and propagation groups..
1288  */
1289 //=============================================================================
1290 Handle(TColStd_HSequenceOfTransient)
1291 GEOMImpl_IAdvancedOperations::MakePipeTShapeWithPosition(double theR1, double theW1, double theL1,
1292                                                          double theR2, double theW2, double theL2,
1293                                                          bool theHexMesh,
1294                                                          Handle(GEOM_Object) theP1,
1295                                                          Handle(GEOM_Object) theP2,
1296                                                          Handle(GEOM_Object) theP3)
1297 {
1298   SetErrorCode(KO);
1299   //Add a new object
1300   Handle(GEOM_Object) aShape = GetEngine()->AddObject(GetDocID(), GEOM_TSHAPE);
1301   /////////////////
1302   // TSHAPE CODE
1303   /////////////////
1304   //Add a new shape function with parameters
1305   Handle(GEOM_Function) aFunction = aShape->AddFunction(GEOMImpl_PipeTShapeDriver::GetID(), TSHAPE_BASIC);
1306   if (aFunction.IsNull()) return NULL;
1307
1308   //Check if the function is set correctly
1309   if (aFunction->GetDriverGUID() != GEOMImpl_PipeTShapeDriver::GetID()) return NULL;
1310
1311   // Check new position
1312   if (!CheckCompatiblePosition(theL1, theL2, theP1, theP2, theP3, 0.01)) {
1313     return NULL;
1314   }
1315
1316   GEOMImpl_IPipeTShape aData(aFunction);
1317
1318   aData.SetR1(theR1);
1319   aData.SetW1(theW1);
1320   aData.SetL1(theL1);
1321   aData.SetR2(theR2);
1322   aData.SetW2(theW2);
1323   aData.SetL2(theL2);
1324   aData.SetHexMesh(theHexMesh);
1325
1326   //Compute the resulting value
1327   try {
1328 #if OCC_VERSION_LARGE > 0x06010000
1329     OCC_CATCH_SIGNALS;
1330 #endif
1331     if (!GetSolver()->ComputeFunction(aFunction)) {
1332       SetErrorCode("TShape driver failed");
1333       return NULL;
1334     }
1335   } catch (Standard_Failure) {
1336     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
1337     SetErrorCode(aFail->GetMessageString());
1338     return NULL;
1339   }
1340
1341   if (theHexMesh) {
1342     if (!MakePipeTShapePartition(aShape, theR1, theW1, theL1, theR2, theW2, theL2))
1343       return NULL;
1344     if (!MakePipeTShapeMirrorAndGlue(aShape, theR1, theW1, theL1, theR2, theW2, theL2))
1345       return NULL;
1346   }
1347
1348   TopoDS_Shape Te = aShape->GetValue();
1349
1350   // Set Position
1351   gp_Trsf aTrsf = GetPositionTrsf(theL1, theL2, theP1, theP2, theP3);
1352   BRepBuilderAPI_Transform aTransformation(Te, aTrsf, Standard_False);
1353   TopoDS_Shape aTrsf_Shape = aTransformation.Shape();
1354   aFunction->SetValue(aTrsf_Shape);
1355   Handle(TColStd_HSequenceOfTransient) aSeq = new TColStd_HSequenceOfTransient;
1356   aSeq->Append(aShape);
1357
1358   if (theHexMesh) {
1359     //
1360     // Get the groups: BEGIN
1361     //
1362     try {
1363       if (!MakeGroups(aShape,TSHAPE_BASIC, theR1, theW1, theL1, theR2, theW2, theL2, aSeq, aTrsf)) {
1364         return NULL;
1365       }
1366     }
1367     catch (Standard_Failure) {
1368       Handle(Standard_Failure) aFail = Standard_Failure::Caught();
1369       SetErrorCode(aFail->GetMessageString());
1370       return NULL;
1371     }
1372
1373     TCollection_AsciiString aListRes, anEntry;
1374     // Iterate over the sequence aSeq
1375     Standard_Integer aNbGroups = aSeq->Length();
1376     Standard_Integer i = 2;
1377     for (; i <= aNbGroups; i++) {
1378       Handle(Standard_Transient) anItem = aSeq->Value(i);
1379       if (anItem.IsNull()) continue;
1380       Handle(GEOM_Object) aGroup = Handle(GEOM_Object)::DownCast(anItem);
1381       if (aGroup.IsNull()) continue;
1382       //Make a Python command
1383       TDF_Tool::Entry(aGroup->GetEntry(), anEntry);
1384       aListRes += anEntry + ", ";
1385     }
1386
1387     aListRes.Trunc(aListRes.Length() - 2);
1388
1389     //Make a Python command
1390     GEOM::TPythonDump(aFunction)
1391       << "[" << aShape << ", " << aListRes.ToCString() << "] = geompy.MakePipeTShape("
1392       << theR1 << ", " << theW1 << ", " << theL1 << ", " << theR2 << ", " << theW2 << ", " << theL2 << ", "
1393       << theHexMesh << ", " << theP1 << ", " << theP2 << ", " << theP3 << ")";
1394   }
1395   //
1396   // Get the groups: END
1397   //
1398
1399   else {
1400     //Make a Python command
1401     GEOM::TPythonDump(aFunction)
1402       << "[" << aShape << "] = geompy.MakePipeTShape(" << theR1 << ", " << theW1 << ", "
1403       << theL1 << ", " << theR2 << ", " << theW2 << ", " << theL2 << ", " << theHexMesh << ", " << theP1
1404       << ", " << theP2 << ", " << theP3 << ")";
1405   }
1406
1407   SetErrorCode(OK);
1408
1409   return aSeq;
1410 }
1411
1412 //=============================================================================
1413 /*!
1414  *  MakePipeTShapeChamfer
1415  *  Create a T-shape object with specified caracteristics for the main and
1416  *  the incident pipes (radius, width, half-length). A chamfer is created
1417  *  on the junction of the pipes.
1418  *  Center of the shape is (0,0,0). The main plane of the T-shape is XOY.
1419  *  \param theR1 Internal radius of main pipe
1420  *  \param theW1 Width of main pipe
1421  *  \param theL1 Half-length of main pipe
1422  *  \param theR2 Internal radius of incident pipe (R2 < R1)
1423  *  \param theW2 Width of incident pipe (R2+W2 < R1+W1)
1424  *  \param theL2 Half-length of incident pipe
1425  *  \param theH Height of chamfer.
1426  *  \param theW Width of chamfer.
1427  *  \param theHexMesh Boolean indicating if shape is prepared for hex mesh
1428  *  \return List of GEOM_Objects, containing the created shape and propagation groups.
1429  */
1430 //=============================================================================
1431 Handle(TColStd_HSequenceOfTransient)
1432 GEOMImpl_IAdvancedOperations::MakePipeTShapeChamfer(double theR1, double theW1, double theL1,
1433                                                     double theR2, double theW2, double theL2,
1434                                                     double theH, double theW,
1435                                                     bool theHexMesh)
1436 {
1437   SetErrorCode(KO);
1438   //Add a new object
1439   Handle(GEOM_Object) aShape = GetEngine()->AddObject(GetDocID(), GEOM_TSHAPE);
1440   //Add a new shape function with parameters
1441   Handle(GEOM_Function) aFunction = aShape->AddFunction(GEOMImpl_PipeTShapeDriver::GetID(), TSHAPE_CHAMFER);
1442   if (aFunction.IsNull()) return NULL;
1443
1444   //Check if the function is set correctly
1445   if (aFunction->GetDriverGUID() != GEOMImpl_PipeTShapeDriver::GetID()) return NULL;
1446
1447   GEOMImpl_IPipeTShape aData(aFunction);
1448
1449   aData.SetR1(theR1);
1450   aData.SetW1(theW1);
1451   aData.SetL1(theL1);
1452   aData.SetR2(theR2);
1453   aData.SetW2(theW2);
1454   aData.SetL2(theL2);
1455   aData.SetH(theH);
1456   aData.SetW(theW);
1457   aData.SetHexMesh(theHexMesh);
1458
1459   //Compute the resulting value
1460   try {
1461 #if OCC_VERSION_LARGE > 0x06010000
1462     OCC_CATCH_SIGNALS;
1463 #endif
1464     if (!GetSolver()->ComputeFunction(aFunction)) {
1465       SetErrorCode("TShape driver failed");
1466       return NULL;
1467     }
1468   } catch (Standard_Failure) {
1469     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
1470     SetErrorCode(aFail->GetMessageString());
1471     return NULL;
1472   }
1473
1474   // BEGIN of chamfer
1475   TopoDS_Shape aShapeShape = aShape->GetValue();
1476   TopTools_IndexedMapOfShape anEdgesIndices;
1477   TopExp::MapShapes(aShapeShape, anEdgesIndices);
1478   // Common edges on external cylinders
1479   Handle(GEOM_Object) box_e;
1480   if (theHexMesh) {
1481     box_e = my3DPrimOperations->MakeBoxDXDYDZ(theR2+theW2, theR2+theW2, theR1+theW1);
1482   }
1483   else {
1484     box_e = my3DPrimOperations->MakeBoxDXDYDZ(2*(theR2+theW2), 2*(theR2+theW2), theR1+theW1);
1485   }
1486   box_e->GetLastFunction()->SetDescription("");
1487   box_e = myTransformOperations->TranslateDXDYDZ(box_e, -(theR2+theW2), -(theR2+theW2), 0);
1488   box_e->GetLastFunction()->SetDescription("");
1489
1490   Handle(TColStd_HSequenceOfInteger) edges_e =
1491     myShapesOperations->GetShapesOnBoxIDs(box_e, aShape, TopAbs_EDGE, GEOMAlgo_ST_IN);
1492   box_e->GetLastFunction()->SetDescription("");
1493
1494   if (edges_e.IsNull() || edges_e->Length() == 0) {
1495     SetErrorCode("External edges not found");
1496     return NULL;
1497   }
1498   int nbEdgesInChamfer = 0;
1499   std::list<int> theEdges;
1500   for (int i=1; i<=edges_e->Length();i++) {
1501     int edgeID = edges_e->Value(i);
1502     TopoDS_Shape theEdge = anEdgesIndices.FindKey(edgeID);
1503     TopExp_Explorer Ex(theEdge,TopAbs_VERTEX);
1504     int iv=0;
1505     while (Ex.More()) {
1506       iv ++;
1507       gp_Pnt aPt = BRep_Tool::Pnt(TopoDS::Vertex(Ex.Current()));
1508       if (Abs(aPt.Z() - (theR1+theW1)) <= Precision::Confusion()) {
1509         nbEdgesInChamfer ++;
1510         theEdges.push_back(edgeID);
1511       }
1512       Ex.Next();
1513     }
1514     if (theHexMesh && nbEdgesInChamfer == 1)
1515       break;
1516   }
1517   Handle(GEOM_Object) aChamfer;
1518   try {
1519     aChamfer = myLocalOperations->MakeChamferEdges(aShape, theW, theH, theEdges);
1520   }
1521   catch (Standard_Failure) {
1522     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
1523     SetErrorCode(aFail->GetMessageString());
1524     return NULL;
1525   }
1526   if (aChamfer.IsNull()) {
1527     SetErrorCode("Chamfer can not be computed on the given shape with the given parameters");
1528     return NULL;
1529   }
1530   aChamfer->GetLastFunction()->SetDescription("");
1531
1532   TopoDS_Shape aChamferShape = aChamfer->GetValue();
1533   aFunction->SetValue(aChamferShape);
1534   // END of chamfer
1535
1536   //   bool doMesh = false;
1537   if (theHexMesh) {
1538     //        doMesh = true;
1539     if (!MakePipeTShapePartition(aShape, theR1, theW1, theL1, theR2, theW2, theL2, theH, theW, 0, false)) {
1540       MESSAGE("PipeTShape partition failed");
1541       //            doMesh = false;
1542       return NULL;
1543     }
1544     if (!MakePipeTShapeMirrorAndGlue(aShape, theR1, theW1, theL1, theR2, theW2, theL2)) {
1545       MESSAGE("PipeTShape mirrors and glue failed");
1546       //          doMesh = false;
1547       return NULL;
1548     }
1549   }
1550
1551   Handle(TColStd_HSequenceOfTransient) aSeq = new TColStd_HSequenceOfTransient;
1552   aSeq->Append(aShape);
1553
1554   //    if (doMesh) {
1555   if (theHexMesh) {
1556     //
1557     //         Get the groups: BEGIN
1558     //
1559     //if (!MakeGroups(aShape, TSHAPE_CHAMFER, theR1, theW1, theL1, theR2, theW2, theL2, aSeq, gp_Trsf())) {
1560     //  //Make a Python command
1561     //  GEOM::TPythonDump(aFunction)
1562     //    << "[" << aShape << "] = geompy.MakePipeTShapeChamfer(" << theR1 << ", " << theW1
1563     //    << ", " << theL1 << ", " << theR2 << ", " << theW2 << ", " << theL2 << ", " << theH << ", " << theW
1564     //    << ", " << theHexMesh << ")";
1565     //}
1566     //else {
1567     try {
1568       if (!MakeGroups(aShape, TSHAPE_CHAMFER, theR1, theW1, theL1, theR2, theW2, theL2, aSeq, gp_Trsf()))
1569         return NULL;
1570     }
1571     catch (Standard_Failure) {
1572       Handle(Standard_Failure) aFail = Standard_Failure::Caught();
1573       SetErrorCode(aFail->GetMessageString());
1574       return NULL;
1575     }
1576
1577     TCollection_AsciiString aListRes, anEntry;
1578     // Iterate over the sequence aSeq
1579     Standard_Integer aNbGroups = aSeq->Length();
1580     Standard_Integer i = 2;
1581     for (; i <= aNbGroups; i++) {
1582       Handle(Standard_Transient) anItem = aSeq->Value(i);
1583       if (anItem.IsNull()) continue;
1584       Handle(GEOM_Object) aGroup = Handle(GEOM_Object)::DownCast(anItem);
1585       if (aGroup.IsNull()) continue;
1586       //Make a Python command
1587       TDF_Tool::Entry(aGroup->GetEntry(), anEntry);
1588       aListRes += anEntry + ", ";
1589     }
1590
1591     aListRes.Trunc(aListRes.Length() - 2);
1592
1593     //Make a Python command
1594     GEOM::TPythonDump(aFunction)
1595       << "[" << aShape << ", " << aListRes.ToCString()
1596       << "] = geompy.MakePipeTShapeChamfer(" << theR1 << ", " << theW1 << ", " << theL1 << ", " << theR2
1597       << ", " << theW2 << ", " << theL2 << ", " << theH << ", " << theW << ", " << theHexMesh << ")";
1598     //}
1599   }
1600   //
1601   //     Get the groups: END
1602   //
1603   else {
1604     //Make a Python command
1605     GEOM::TPythonDump(aFunction)
1606       << "[" << aShape << "] = geompy.MakePipeTShapeChamfer(" << theR1 << ", " << theW1
1607       << ", " << theL1 << ", " << theR2 << ", " << theW2 << ", " << theL2 << ", " << theH << ", " << theW
1608       << ", " << theHexMesh << ")";
1609   }
1610
1611   SetErrorCode(OK);
1612
1613   return aSeq;
1614 }
1615
1616 //=============================================================================
1617 /*!
1618  *  MakePipeTShapeChamferWithPosition
1619  *  Create a T-shape object with specified caracteristics for the main and
1620  *  the incident pipes (radius, width, half-length). A chamfer is created
1621  *  on the junction of the pipes.
1622  *  The extremities of the main pipe are located on junctions points P1 and P2.
1623  *  The extremity of the incident pipe is located on junction point P3.
1624  *  \param theR1 Internal radius of main pipe
1625  *  \param theW1 Width of main pipe
1626  *  \param theL1 Half-length of main pipe
1627  *  \param theR2 Internal radius of incident pipe (R2 < R1)
1628  *  \param theW2 Width of incident pipe (R2+W2 < R1+W1)
1629  *  \param theL2 Half-length of incident pipe
1630  *  \param theH Height of chamfer.
1631  *  \param theW Width of chamfer.
1632  *  \param theHexMesh Boolean indicating if shape is prepared for hex mesh
1633  *  \param theP1 1st junction point of main pipe
1634  *  \param theP2 2nd junction point of main pipe
1635  *  \param theP3 Junction point of incident pipe
1636  *  \return List of GEOM_Objects, containing the created shape and propagation groups.
1637  */
1638 //=============================================================================
1639 Handle(TColStd_HSequenceOfTransient)
1640 GEOMImpl_IAdvancedOperations::MakePipeTShapeChamferWithPosition(double theR1, double theW1, double theL1,
1641                                                                 double theR2, double theW2, double theL2,
1642                                                                 double theH, double theW,
1643                                                                 bool theHexMesh,
1644                                                                 Handle(GEOM_Object) theP1,
1645                                                                 Handle(GEOM_Object) theP2,
1646                                                                 Handle(GEOM_Object) theP3)
1647 {
1648   SetErrorCode(KO);
1649   //Add a new object
1650   Handle(GEOM_Object) aShape = GetEngine()->AddObject(GetDocID(), GEOM_TSHAPE);
1651   //Add a new shape function with parameters
1652   Handle(GEOM_Function) aFunction = aShape->AddFunction(GEOMImpl_PipeTShapeDriver::GetID(), TSHAPE_CHAMFER);
1653   if (aFunction.IsNull()) return NULL;
1654
1655   //Check if the function is set correctly
1656   if (aFunction->GetDriverGUID() != GEOMImpl_PipeTShapeDriver::GetID()) return NULL;
1657
1658   // Check new position
1659   if (!CheckCompatiblePosition(theL1, theL2, theP1, theP2, theP3, 0.01)) {
1660     return NULL;
1661   }
1662
1663   GEOMImpl_IPipeTShape aData(aFunction);
1664
1665   aData.SetR1(theR1);
1666   aData.SetW1(theW1);
1667   aData.SetL1(theL1);
1668   aData.SetR2(theR2);
1669   aData.SetW2(theW2);
1670   aData.SetL2(theL2);
1671   aData.SetH(theH);
1672   aData.SetW(theW);
1673   aData.SetHexMesh(theHexMesh);
1674
1675   //Compute the resulting value
1676   try {
1677 #if OCC_VERSION_LARGE > 0x06010000
1678     OCC_CATCH_SIGNALS;
1679 #endif
1680     if (!GetSolver()->ComputeFunction(aFunction)) {
1681       SetErrorCode("TShape driver failed");
1682       return NULL;
1683     }
1684   } catch (Standard_Failure) {
1685     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
1686     SetErrorCode(aFail->GetMessageString());
1687     return NULL;
1688   }
1689
1690   // BEGIN of chamfer
1691   TopoDS_Shape aShapeShape = aShape->GetValue();
1692   TopTools_IndexedMapOfShape anEdgesIndices;
1693   TopExp::MapShapes(aShapeShape, anEdgesIndices);
1694   // Common edges on external cylinders
1695   Handle(GEOM_Object) box_e;
1696   if (theHexMesh) {
1697     box_e = my3DPrimOperations->MakeBoxDXDYDZ(theR2+theW2, theR2+theW2, theR1+theW1);
1698   }
1699   else {
1700     box_e = my3DPrimOperations->MakeBoxDXDYDZ(2*(theR2+theW2), 2*(theR2+theW2), theR1+theW1);
1701   }
1702   box_e->GetLastFunction()->SetDescription("");
1703   box_e = myTransformOperations->TranslateDXDYDZ(box_e, -(theR2+theW2), -(theR2+theW2), 0);
1704   box_e->GetLastFunction()->SetDescription("");
1705
1706   Handle(TColStd_HSequenceOfInteger) edges_e =
1707     myShapesOperations->GetShapesOnBoxIDs(box_e, aShape, TopAbs_EDGE, GEOMAlgo_ST_IN);
1708   box_e->GetLastFunction()->SetDescription("");
1709
1710   if (edges_e.IsNull() || edges_e->Length() == 0) {
1711     SetErrorCode("External edges not found");
1712     return NULL;
1713   }
1714   int nbEdgesInChamfer = 0;
1715   std::list<int> theEdges;
1716   for (int i=1; i<=edges_e->Length();i++) {
1717     int edgeID = edges_e->Value(i);
1718     TopoDS_Shape theEdge = anEdgesIndices.FindKey(edgeID);
1719     TopExp_Explorer Ex(theEdge,TopAbs_VERTEX);
1720     while (Ex.More()) {
1721       gp_Pnt aPt = BRep_Tool::Pnt(TopoDS::Vertex(Ex.Current()));
1722       if (Abs(aPt.Z() - (theR1+theW1)) <= Precision::Confusion()) {
1723         nbEdgesInChamfer ++;
1724         theEdges.push_back(edgeID);
1725       }
1726       Ex.Next();
1727     }
1728     if (theHexMesh && nbEdgesInChamfer == 1)
1729       break;
1730   }
1731   Handle(GEOM_Object) aChamfer;
1732   try {
1733     aChamfer = myLocalOperations->MakeChamferEdges(aShape, theW, theH, theEdges);
1734   }
1735   catch (Standard_Failure) {
1736     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
1737     SetErrorCode(aFail->GetMessageString());
1738     return NULL;
1739   }
1740   if (aChamfer.IsNull()) {
1741     SetErrorCode("Chamfer can not be computed on the given shape with the given parameters");
1742     return NULL;
1743   }
1744   aChamfer->GetLastFunction()->SetDescription("");
1745
1746   TopoDS_Shape aChamferShape = aChamfer->GetValue();
1747   aFunction->SetValue(aChamferShape);
1748   // END of chamfer
1749
1750   if (theHexMesh) {
1751     if (!MakePipeTShapePartition(aShape, theR1, theW1, theL1, theR2, theW2, theL2, theH, theW, 0, false))
1752       return NULL;
1753     if (!MakePipeTShapeMirrorAndGlue(aShape, theR1, theW1, theL1, theR2, theW2, theL2))
1754       return NULL;
1755   }
1756
1757   TopoDS_Shape Te = aShape->GetValue();
1758
1759   // Set Position
1760   gp_Trsf aTrsf = GetPositionTrsf(theL1, theL2, theP1, theP2, theP3);
1761   BRepBuilderAPI_Transform aTransformation(Te, aTrsf, Standard_False);
1762   TopoDS_Shape aTrsf_Shape = aTransformation.Shape();
1763   aFunction->SetValue(aTrsf_Shape);
1764   Handle(TColStd_HSequenceOfTransient) aSeq = new TColStd_HSequenceOfTransient;
1765   aSeq->Append(aShape);
1766   if (theHexMesh) {
1767     /*
1768      * Get the groups: BEGIN
1769      */
1770     try {
1771       if (!MakeGroups(aShape, TSHAPE_CHAMFER, theR1, theW1, theL1, theR2, theW2, theL2, aSeq, aTrsf))
1772         return NULL;
1773     }
1774     catch (Standard_Failure) {
1775       Handle(Standard_Failure) aFail = Standard_Failure::Caught();
1776       SetErrorCode(aFail->GetMessageString());
1777       return NULL;
1778     }
1779
1780     TCollection_AsciiString aListRes, anEntry;
1781     // Iterate over the sequence aSeq
1782     Standard_Integer aNbGroups = aSeq->Length();
1783     Standard_Integer i = 2;
1784     for (; i <= aNbGroups; i++) {
1785       Handle(Standard_Transient) anItem = aSeq->Value(i);
1786       if (anItem.IsNull()) continue;
1787       Handle(GEOM_Object) aGroup = Handle(GEOM_Object)::DownCast(anItem);
1788       if (aGroup.IsNull()) continue;
1789       //Make a Python command
1790       TDF_Tool::Entry(aGroup->GetEntry(), anEntry);
1791       aListRes += anEntry + ", ";
1792     }
1793
1794     aListRes.Trunc(aListRes.Length() - 2);
1795
1796     //Make a Python command
1797     GEOM::TPythonDump(aFunction)
1798       << "[" << aShape << ", " << aListRes.ToCString()
1799       << "] = geompy.MakePipeTShapeChamfer(" << theR1 << ", " << theW1 << ", " << theL1 << ", " << theR2
1800       << ", " << theW2 << ", " << theL2 << ", " << theH << ", " << theW << ", " << theHexMesh << ", "
1801       << theP1 << ", " << theP2 << ", " << theP3 << ")";
1802   }
1803   /*
1804    * Get the groups: END
1805    */
1806   else {
1807     //Make a Python command
1808     GEOM::TPythonDump(aFunction)
1809       << "[" << aShape << "] = geompy.MakePipeTShapeChamfer(" << theR1 << ", " << theW1
1810       << ", " << theL1 << ", " << theR2 << ", " << theW2 << ", " << theL2 << ", " << theH << ", " << theW
1811       << ", " << theHexMesh << ", " << theP1 << ", " << theP2 << ", " << theP3 << ")";
1812   }
1813
1814   SetErrorCode(OK);
1815
1816   return aSeq;
1817 }
1818
1819 //=============================================================================
1820 /*!
1821  *  MakePipeTShapeFillet
1822  *  Create a T-shape object with specified caracteristics for the main and
1823  *  the incident pipes (radius, width, half-length). A fillet is created
1824  *  on the junction of the pipes.
1825  *  Center of the shape is (0,0,0). The main plane of the T-shape is XOY.
1826  *  \param theR1 Internal radius of main pipe
1827  *  \param theW1 Width of main pipe
1828  *  \param theL1 Half-length of main pipe
1829  *  \param theR2 Internal radius of incident pipe (R2 < R1)
1830  *  \param theW2 Width of incident pipe (R2+W2 < R1+W1)
1831  *  \param theL2 Half-length of incident pipe
1832  *  \param theRF Radius of curvature of fillet.
1833  *  \param theHexMesh Boolean indicating if shape is prepared for hex mesh
1834  *  \return List of GEOM_Objects, containing the created shape and propagation groups.
1835  */
1836 //=============================================================================
1837 Handle(TColStd_HSequenceOfTransient)
1838 GEOMImpl_IAdvancedOperations::MakePipeTShapeFillet(double theR1, double theW1, double theL1,
1839                                                    double theR2, double theW2, double theL2,
1840                                                    double theRF, bool theHexMesh)
1841 {
1842   SetErrorCode(KO);
1843   //Add a new object
1844   Handle(GEOM_Object) aShape = GetEngine()->AddObject(GetDocID(), GEOM_TSHAPE);
1845   //Add a new shape function with parameters
1846   Handle(GEOM_Function) aFunction = aShape->AddFunction(GEOMImpl_PipeTShapeDriver::GetID(), TSHAPE_FILLET);
1847   if (aFunction.IsNull()) return NULL;
1848
1849   //Check if the function is set correctly
1850   if (aFunction->GetDriverGUID() != GEOMImpl_PipeTShapeDriver::GetID()) return NULL;
1851
1852   GEOMImpl_IPipeTShape aData(aFunction);
1853
1854   aData.SetR1(theR1);
1855   aData.SetW1(theW1);
1856   aData.SetL1(theL1);
1857   aData.SetR2(theR2);
1858   aData.SetW2(theW2);
1859   aData.SetL2(theL2);
1860   aData.SetRF(theRF);
1861   aData.SetHexMesh(theHexMesh);
1862
1863   //Compute the resulting value
1864   try {
1865 #if OCC_VERSION_LARGE > 0x06010000
1866     OCC_CATCH_SIGNALS;
1867 #endif
1868     if (!GetSolver()->ComputeFunction(aFunction)) {
1869       SetErrorCode("TShape driver failed");
1870       return NULL;
1871     }
1872   } catch (Standard_Failure) {
1873     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
1874     SetErrorCode(aFail->GetMessageString());
1875     return NULL;
1876   }
1877
1878   // BEGIN of fillet
1879   TopoDS_Shape aShapeShape = aShape->GetValue();
1880   TopTools_IndexedMapOfShape anEdgesIndices;
1881   TopExp::MapShapes(aShapeShape, anEdgesIndices);
1882   // Common edges on external cylinders
1883   Handle(GEOM_Object) box_e;
1884   if (theHexMesh) {
1885     box_e = my3DPrimOperations->MakeBoxDXDYDZ(theR2+theW2, theR2+theW2, theR1+theW1);
1886   }
1887   else {
1888     box_e = my3DPrimOperations->MakeBoxDXDYDZ(2*(theR2+theW2), 2*(theR2+theW2), theR1+theW1);
1889   }
1890   box_e->GetLastFunction()->SetDescription("");
1891   box_e = myTransformOperations->TranslateDXDYDZ(box_e, -(theR2+theW2), -(theR2+theW2), 0);
1892   box_e->GetLastFunction()->SetDescription("");
1893
1894   Handle(TColStd_HSequenceOfInteger) edges_e =
1895     myShapesOperations->GetShapesOnBoxIDs(box_e, aShape, TopAbs_EDGE, GEOMAlgo_ST_IN);
1896   box_e->GetLastFunction()->SetDescription("");
1897
1898   if (edges_e.IsNull() || edges_e->Length() == 0) {
1899     SetErrorCode("External edges not found");
1900     return NULL;
1901   }
1902   int nbEdgesInFillet = 0;
1903   std::list<int> theEdges;
1904   for (int i=1; i<=edges_e->Length();i++) {
1905     int edgeID = edges_e->Value(i);
1906     TopoDS_Shape theEdge = anEdgesIndices.FindKey(edgeID);
1907     TopExp_Explorer Ex(theEdge,TopAbs_VERTEX);
1908     while (Ex.More()) {
1909       gp_Pnt aPt = BRep_Tool::Pnt(TopoDS::Vertex(Ex.Current()));
1910       if (Abs(aPt.Z() - (theR1+theW1)) <= Precision::Confusion()) {
1911         nbEdgesInFillet ++;
1912         theEdges.push_back(edgeID);
1913       }
1914       Ex.Next();
1915     }
1916     if (theHexMesh && nbEdgesInFillet == 1)
1917       break;
1918   }
1919
1920   Handle(GEOM_Object) aFillet;
1921   try {
1922     aFillet = myLocalOperations->MakeFilletEdges(aShape, theRF, theEdges);
1923   }
1924   catch (Standard_Failure) {
1925     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
1926     SetErrorCode(aFail->GetMessageString());
1927     return NULL;
1928   }
1929   if (aFillet.IsNull()) {
1930     //SetErrorCode("Fillet can not be computed on the given shape with the given parameters");
1931     SetErrorCode(myLocalOperations->GetErrorCode());
1932     return NULL;
1933   }
1934   aFillet->GetLastFunction()->SetDescription("");
1935
1936   TopoDS_Shape aFilletShape = aFillet->GetValue();
1937   aFunction->SetValue(aFilletShape);
1938   // END of fillet
1939
1940 // VSR: debug issues 0021568 and 0021550 (15/05/2012) - BEGIN (1)
1941 // the following block, when enabled, leads to partitioning problems
1942 #if 0
1943 // VSR: debug issues 0021568 and 0021550 (15/05/2012) - END (1)
1944   // BEGIN: Limit tolerances (debug)
1945   Handle(GEOM_Object) aCorr1 = myHealingOperations->LimitTolerance(aShape, 1e-07);
1946   TopoDS_Shape aCorr1Shape = aCorr1->GetValue();
1947   aShape->GetLastFunction()->SetValue(aCorr1Shape);
1948   aCorr1->GetLastFunction()->SetDescription("");
1949   // END: Limit tolerances (debug)
1950 // VSR: debug issues 0021568 and 0021550 (15/05/2012) - BEGIN (2)
1951 #endif
1952 // VSR: debug issues 0021568 and 0021550 (15/05/2012) - END (2)
1953
1954   if (theHexMesh) {
1955     if (!MakePipeTShapePartition(aShape, theR1, theW1, theL1, theR2, theW2, theL2, 0, 0, theRF, false))
1956       return NULL;
1957     if (!MakePipeTShapeMirrorAndGlue(aShape, theR1, theW1, theL1, theR2, theW2, theL2))
1958       return NULL;
1959   }
1960
1961   Handle(TColStd_HSequenceOfTransient) aSeq = new TColStd_HSequenceOfTransient;
1962   aSeq->Append(aShape);
1963   if (theHexMesh) {
1964     /*
1965      * Get the groups: BEGIN
1966      */
1967     try {
1968       if (!MakeGroups(aShape, TSHAPE_FILLET, theR1, theW1, theL1, theR2, theW2, theL2, aSeq, gp_Trsf()))
1969         return NULL;
1970     }
1971     catch (Standard_Failure) {
1972       Handle(Standard_Failure) aFail = Standard_Failure::Caught();
1973       SetErrorCode(aFail->GetMessageString());
1974       return NULL;
1975     }
1976
1977     TCollection_AsciiString aListRes, anEntry;
1978     // Iterate over the sequence aSeq
1979     Standard_Integer aNbGroups = aSeq->Length();
1980     Standard_Integer i = 2;
1981     for (; i <= aNbGroups; i++) {
1982       Handle(Standard_Transient) anItem = aSeq->Value(i);
1983       if (anItem.IsNull()) continue;
1984       Handle(GEOM_Object) aGroup = Handle(GEOM_Object)::DownCast(anItem);
1985       if (aGroup.IsNull()) continue;
1986       //Make a Python command
1987       TDF_Tool::Entry(aGroup->GetEntry(), anEntry);
1988       aListRes += anEntry + ", ";
1989     }
1990
1991     aListRes.Trunc(aListRes.Length() - 2);
1992
1993     //Make a Python command
1994     GEOM::TPythonDump(aFunction)
1995       << "[" << aShape << ", " << aListRes.ToCString()
1996       << "] = geompy.MakePipeTShapeFillet(" << theR1 << ", " << theW1 << ", " << theL1 << ", " << theR2
1997       << ", " << theW2 << ", " << theL2 << ", " << theRF << ", " << theHexMesh << ")";
1998   }
1999   /*
2000    * Get the groups: END
2001    */
2002   else {
2003     //Make a Python command
2004     GEOM::TPythonDump(aFunction)
2005       << "[" << aShape << "] = geompy.MakePipeTShapeFillet(" << theR1 << ", " << theW1
2006       << ", " << theL1 << ", " << theR2 << ", " << theW2 << ", " << theL2 << ", " << theRF << ", "
2007       << theHexMesh << ")";
2008   }
2009
2010   SetErrorCode(OK);
2011
2012   return aSeq;
2013 }
2014
2015 //=============================================================================
2016 /*!
2017  *  MakePipeTShapeFilletWithPosition
2018  *  \brief Create a T-shape object with specified caracteristics for the main and
2019  *  the incident pipes (radius, width, half-length). A fillet is created
2020  *  on the junction of the pipes.
2021  *  The extremities of the main pipe are located on junctions points P1 and P2.
2022  *  The extremity of the incident pipe is located on junction point P3.
2023  *  \param theR1 Internal radius of main pipe
2024  *  \param theW1 Width of main pipe
2025  *  \param theL1 Half-length of main pipe
2026  *  \param theR2 Internal radius of incident pipe (R2 < R1)
2027  *  \param theW2 Width of incident pipe (R2+W2 < R1+W1)
2028  *  \param theL2 Half-length of incident pipe
2029  *  \param theRF Radius of curvature of fillet
2030  *  \param theHexMesh Boolean indicating if shape is prepared for hex mesh
2031  *  \param theP1 1st junction point of main pipe
2032  *  \param theP2 2nd junction point of main pipe
2033  *  \param theP3 Junction point of incident pipe
2034  *  \return List of GEOM_Objects, containing the created shape and propagation groups.
2035  */
2036 //=============================================================================
2037 Handle(TColStd_HSequenceOfTransient)
2038 GEOMImpl_IAdvancedOperations::MakePipeTShapeFilletWithPosition(double theR1, double theW1, double theL1,
2039                                                                double theR2, double theW2, double theL2,
2040                                                                double theRF, bool theHexMesh,
2041                                                                Handle(GEOM_Object) theP1,
2042                                                                Handle(GEOM_Object) theP2,
2043                                                                Handle(GEOM_Object) theP3)
2044 {
2045   SetErrorCode(KO);
2046   //Add a new object
2047   Handle(GEOM_Object) aShape = GetEngine()->AddObject(GetDocID(), GEOM_TSHAPE);
2048   //Add a new shape function with parameters
2049   Handle(GEOM_Function) aFunction = aShape->AddFunction(GEOMImpl_PipeTShapeDriver::GetID(), TSHAPE_FILLET);
2050   if (aFunction.IsNull()) return NULL;
2051
2052   //Check if the function is set correctly
2053   if (aFunction->GetDriverGUID() != GEOMImpl_PipeTShapeDriver::GetID()) return NULL;
2054
2055   // Check new position
2056   if (!CheckCompatiblePosition(theL1, theL2, theP1, theP2, theP3, 0.01)) {
2057     return NULL;
2058   }
2059
2060   GEOMImpl_IPipeTShape aData(aFunction);
2061
2062   aData.SetR1(theR1);
2063   aData.SetW1(theW1);
2064   aData.SetL1(theL1);
2065   aData.SetR2(theR2);
2066   aData.SetW2(theW2);
2067   aData.SetL2(theL2);
2068   aData.SetRF(theRF);
2069   aData.SetHexMesh(theHexMesh);
2070
2071   //Compute the resulting value
2072   try {
2073 #if OCC_VERSION_LARGE > 0x06010000
2074     OCC_CATCH_SIGNALS;
2075 #endif
2076     if (!GetSolver()->ComputeFunction(aFunction)) {
2077       SetErrorCode("TShape driver failed");
2078       return NULL;
2079     }
2080   } catch (Standard_Failure) {
2081     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
2082     SetErrorCode(aFail->GetMessageString());
2083     return NULL;
2084   }
2085
2086   // BEGIN of fillet
2087   TopoDS_Shape aShapeShape = aShape->GetValue();
2088   TopTools_IndexedMapOfShape anEdgesIndices;
2089   TopExp::MapShapes(aShapeShape, anEdgesIndices);
2090   // Common edges on external cylinders
2091   Handle(GEOM_Object) box_e;
2092   if (theHexMesh) {
2093     box_e = my3DPrimOperations->MakeBoxDXDYDZ(theR2+theW2, theR2+theW2, theR1+theW1);
2094   }
2095   else {
2096     box_e = my3DPrimOperations->MakeBoxDXDYDZ(2*(theR2+theW2), 2*(theR2+theW2), theR1+theW1);
2097   }
2098   box_e->GetLastFunction()->SetDescription("");
2099   box_e = myTransformOperations->TranslateDXDYDZ(box_e, -(theR2+theW2), -(theR2+theW2), 0);
2100   box_e->GetLastFunction()->SetDescription("");
2101
2102   Handle(TColStd_HSequenceOfInteger) edges_e =
2103     myShapesOperations->GetShapesOnBoxIDs(box_e, aShape, TopAbs_EDGE, GEOMAlgo_ST_IN);
2104   box_e->GetLastFunction()->SetDescription("");
2105
2106   if (edges_e.IsNull() || edges_e->Length() == 0) {
2107     SetErrorCode("External edges not found");
2108     return NULL;
2109   }
2110   int nbEdgesInFillet = 0;
2111   std::list<int> theEdges;
2112   for (int i=1; i<=edges_e->Length();i++) {
2113     int edgeID = edges_e->Value(i);
2114     TopoDS_Shape theEdge = anEdgesIndices.FindKey(edgeID);
2115     TopExp_Explorer Ex(theEdge,TopAbs_VERTEX);
2116     while (Ex.More()) {
2117       gp_Pnt aPt = BRep_Tool::Pnt(TopoDS::Vertex(Ex.Current()));
2118       if (Abs(aPt.Z() - (theR1+theW1)) <= Precision::Confusion()) {
2119         nbEdgesInFillet ++;
2120         theEdges.push_back(edgeID);
2121       }
2122       Ex.Next();
2123     }
2124     if (theHexMesh && nbEdgesInFillet == 1)
2125       break;
2126   }
2127
2128   Handle(GEOM_Object) aFillet;
2129   try {
2130     aFillet = myLocalOperations->MakeFilletEdges(aShape, theRF, theEdges);
2131   }
2132   catch (Standard_Failure) {
2133     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
2134     SetErrorCode(aFail->GetMessageString());
2135     return NULL;
2136   }
2137   if (aFillet.IsNull()) {
2138     SetErrorCode("Fillet can not be computed on the given shape with the given parameters");
2139     return NULL;
2140   }
2141   aFillet->GetLastFunction()->SetDescription("");
2142
2143   TopoDS_Shape aFilletShape = aFillet->GetValue();
2144   aFunction->SetValue(aFilletShape);
2145   // END of fillet
2146
2147 // VSR: debug issues 0021568 and 0021550 (15/05/2012) - BEGIN (3)
2148 // the following block, when enabled, leads to partitioning problems
2149 #if 0
2150 // VSR: debug issues 0021568 and 0021550 (15/05/2012) - END (3)
2151   // BEGIN: Limit tolerances (debug)
2152   Handle(GEOM_Object) aCorr1 = myHealingOperations->LimitTolerance(aShape, 1e-07);
2153   TopoDS_Shape aCorr1Shape = aCorr1->GetValue();
2154   aShape->GetLastFunction()->SetValue(aCorr1Shape);
2155   aCorr1->GetLastFunction()->SetDescription("");
2156   // END: Limit tolerances (debug)
2157 // VSR: debug issues 0021568 and 0021550 (15/05/2012) - BEGIN (4)
2158 #endif
2159 // VSR: debug issues 0021568 and 0021550 (15/05/2012) - END (4)
2160
2161   if (theHexMesh) {
2162     if (!MakePipeTShapePartition(aShape, theR1, theW1, theL1, theR2, theW2, theL2, 0, 0, theRF, false))
2163       return NULL;
2164     if (!MakePipeTShapeMirrorAndGlue(aShape, theR1, theW1, theL1, theR2, theW2, theL2))
2165       return NULL;
2166   }
2167
2168   TopoDS_Shape Te = aShape->GetValue();
2169
2170   // Set Position
2171   gp_Trsf aTrsf = GetPositionTrsf(theL1, theL2, theP1, theP2, theP3);
2172   BRepBuilderAPI_Transform aTransformation(Te, aTrsf, Standard_False);
2173   TopoDS_Shape aTrsf_Shape = aTransformation.Shape();
2174   aFunction->SetValue(aTrsf_Shape);
2175   Handle(TColStd_HSequenceOfTransient) aSeq = new TColStd_HSequenceOfTransient;
2176   aSeq->Append(aShape);
2177   if (theHexMesh) {
2178     /*
2179      * Get the groups: BEGIN
2180      */
2181     try {
2182       if (!MakeGroups(aShape, TSHAPE_FILLET, theR1, theW1, theL1, theR2, theW2, theL2, aSeq, aTrsf))
2183         return NULL;
2184     }
2185     catch (Standard_Failure) {
2186       Handle(Standard_Failure) aFail = Standard_Failure::Caught();
2187       SetErrorCode(aFail->GetMessageString());
2188       return NULL;
2189     }
2190
2191     TCollection_AsciiString aListRes, anEntry;
2192     // Iterate over the sequence aSeq
2193     Standard_Integer aNbGroups = aSeq->Length();
2194     Standard_Integer i = 2;
2195     for (; i <= aNbGroups; i++) {
2196       Handle(Standard_Transient) anItem = aSeq->Value(i);
2197       if (anItem.IsNull()) continue;
2198       Handle(GEOM_Object) aGroup = Handle(GEOM_Object)::DownCast(anItem);
2199       if (aGroup.IsNull()) continue;
2200       //Make a Python command
2201       TDF_Tool::Entry(aGroup->GetEntry(), anEntry);
2202       aListRes += anEntry + ", ";
2203     }
2204
2205     aListRes.Trunc(aListRes.Length() - 2);
2206
2207     //Make a Python command
2208     GEOM::TPythonDump(aFunction)
2209       << "[" << aShape << ", " << aListRes.ToCString()
2210       << "] = geompy.MakePipeTShapeFillet(" << theR1 << ", " << theW1 << ", " << theL1 << ", " << theR2
2211       << ", " << theW2 << ", " << theL2 << ", " << theRF << ", " << theHexMesh << ", " << theP1 << ", "
2212       << theP2 << ", " << theP3 << ")";
2213   }
2214   /*
2215    * Get the groups: END
2216    */
2217   else {
2218     //Make a Python command
2219     GEOM::TPythonDump(aFunction)
2220       << "[" << aShape << "] = geompy.MakePipeTShapeFillet(" << theR1 << ", " << theW1
2221       << ", " << theL1 << ", " << theR2 << ", " << theW2 << ", " << theL2 << ", " << theRF << ", "
2222       << theHexMesh << ", " << theP1 << ", " << theP2 << ", " << theP3 << ")";
2223   }
2224
2225   SetErrorCode(OK);
2226
2227   return aSeq;
2228 }
2229
2230 /*@@ insert new functions before this line @@ do not remove this line @@ do not remove this line @@*/