]> SALOME platform Git repositories - modules/geom.git/blob - src/GEOMImpl/GEOMImpl_IMeasureOperations.cxx
Salome HOME
Merge from V6_main 01/04/2013
[modules/geom.git] / src / GEOMImpl / GEOMImpl_IMeasureOperations.cxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include <Standard_Stream.hxx>
24
25 #include <GEOMImpl_IMeasureOperations.hxx>
26
27 #include <GEOMImpl_Types.hxx>
28 #include <GEOMImpl_MeasureDriver.hxx>
29 #include <GEOMImpl_IMeasure.hxx>
30 #include <GEOMImpl_IShapesOperations.hxx>
31
32 #include <GEOMUtils.hxx>
33
34 #include <GEOMAlgo_ShapeInfo.hxx>
35 #include <GEOMAlgo_ShapeInfoFiller.hxx>
36
37 #include <GEOM_Function.hxx>
38 #include <GEOM_PythonDump.hxx>
39
40 #include <NMTTools_CheckerSI.hxx>
41
42 #include <NMTDS_Tools.hxx>
43 #include <NMTDS_InterfPool.hxx>
44 #include <NMTDS_PInterfPool.hxx>
45 #include <NMTDS_PairBoolean.hxx>
46 #include <NMTDS_ShapesDataStructure.hxx>
47 #include <NMTDS_ListIteratorOfListOfPairBoolean.hxx>
48
49 #include <Basics_OCCTVersion.hxx>
50
51 #include <utilities.h>
52 #include <OpUtil.hxx>
53 #include <Utils_ExceptHandlers.hxx>
54
55 // OCCT Includes
56 #include <TFunction_DriverTable.hxx>
57 #include <TFunction_Driver.hxx>
58 #include <TFunction_Logbook.hxx>
59 #include <TDF_Tool.hxx>
60
61 #include <BRep_Builder.hxx>
62 #include <BRep_TFace.hxx>
63 #include <BRep_Tool.hxx>
64 #include <BRepAdaptor_Surface.hxx>
65 #include <BRepBuilderAPI_Copy.hxx>
66 #include <BRepBndLib.hxx>
67 #include <BRepCheck.hxx>
68 #include <BRepCheck_ListIteratorOfListOfStatus.hxx>
69 #include <BRepCheck_Result.hxx>
70 #include <BRepCheck_Shell.hxx>
71 #include <BRepClass3d_SolidClassifier.hxx>
72 #include <BRepExtrema_DistShapeShape.hxx>
73 #include <BRepGProp.hxx>
74 #include <BRepTools.hxx>
75
76 #include <Bnd_Box.hxx>
77
78 #include <TopAbs.hxx>
79 #include <TopExp.hxx>
80 #include <TopoDS.hxx>
81 #include <TopoDS_Edge.hxx>
82 #include <TopoDS_Face.hxx>
83 #include <TopoDS_Shape.hxx>
84 #include <TopoDS_Vertex.hxx>
85 #include <TopoDS_Compound.hxx>
86 #include <TopoDS_Iterator.hxx>
87 #include <TopExp_Explorer.hxx>
88 #include <TopTools_MapOfShape.hxx>
89 #include <TopTools_ListOfShape.hxx>
90 #include <TopTools_ListIteratorOfListOfShape.hxx>
91
92 #include <ShapeAnalysis.hxx>
93 #include <ShapeAnalysis_Surface.hxx>
94 #include <ShapeFix_Shape.hxx>
95
96 #include <GeomAPI_IntSS.hxx>
97 #include <GeomAPI_ProjectPointOnCurve.hxx>
98
99 #include <GeomAbs_SurfaceType.hxx>
100
101 #include <Geom_BezierSurface.hxx>
102 #include <Geom_BSplineSurface.hxx>
103 #include <Geom_Circle.hxx>
104 #include <Geom_ConicalSurface.hxx>
105 #include <Geom_CylindricalSurface.hxx>
106 #include <Geom_Line.hxx>
107 #include <Geom_OffsetSurface.hxx>
108 #include <Geom_Plane.hxx>
109 #include <Geom_RectangularTrimmedSurface.hxx>
110 #include <Geom_SphericalSurface.hxx>
111 #include <Geom_Surface.hxx>
112 #include <Geom_SurfaceOfLinearExtrusion.hxx>
113 #include <Geom_SurfaceOfRevolution.hxx>
114 #include <Geom_ToroidalSurface.hxx>
115
116 #include <GeomLProp_CLProps.hxx>
117 #include <GeomLProp_SLProps.hxx>
118
119 #include <GProp_GProps.hxx>
120 #include <GProp_PrincipalProps.hxx>
121
122 #include <gp_Pln.hxx>
123 #include <gp_Lin.hxx>
124
125 #include <Standard_Failure.hxx>
126 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
127
128 //=============================================================================
129 /*!
130  *  Constructor
131  */
132 //=============================================================================
133 GEOMImpl_IMeasureOperations::GEOMImpl_IMeasureOperations (GEOM_Engine* theEngine, int theDocID)
134 : GEOM_IOperations(theEngine, theDocID)
135 {
136   MESSAGE("GEOMImpl_IMeasureOperations::GEOMImpl_IMeasureOperations");
137 }
138
139 //=============================================================================
140 /*!
141  *  Destructor
142  */
143 //=============================================================================
144 GEOMImpl_IMeasureOperations::~GEOMImpl_IMeasureOperations()
145 {
146   MESSAGE("GEOMImpl_IMeasureOperations::~GEOMImpl_IMeasureOperations");
147 }
148
149 //=============================================================================
150 /*! Get kind and parameters of the given shape.
151  */
152 //=============================================================================
153 GEOMImpl_IMeasureOperations::ShapeKind GEOMImpl_IMeasureOperations::KindOfShape
154                              (Handle(GEOM_Object) theShape,
155                               Handle(TColStd_HSequenceOfInteger)& theIntegers,
156                               Handle(TColStd_HSequenceOfReal)&    theDoubles)
157 {
158   SetErrorCode(KO);
159   ShapeKind aKind = SK_NO_SHAPE;
160
161   if (theIntegers.IsNull()) theIntegers = new TColStd_HSequenceOfInteger;
162   else                      theIntegers->Clear();
163
164   if (theDoubles.IsNull()) theDoubles = new TColStd_HSequenceOfReal;
165   else                     theDoubles->Clear();
166
167   if (theShape.IsNull())
168     return aKind;
169
170   Handle(GEOM_Function) aRefShape = theShape->GetLastFunction();
171   if (aRefShape.IsNull()) return aKind;
172
173   TopoDS_Shape aShape = aRefShape->GetValue();
174   if (aShape.IsNull()) return aKind;
175
176   int geom_type = theShape->GetType();
177
178   // check if it's advanced shape
179   if ( geom_type > ADVANCED_BASE ) {
180     SetErrorCode(OK);
181     return SK_ADVANCED;
182   }
183
184   // Call algorithm
185   GEOMAlgo_ShapeInfoFiller aSF;
186   aSF.SetShape(aShape);
187   aSF.Perform();
188   Standard_Integer iErr = aSF.ErrorStatus();
189   if (iErr) {
190     SetErrorCode("Error in GEOMAlgo_ShapeInfoFiller");
191     return SK_NO_SHAPE;
192   }
193   const GEOMAlgo_ShapeInfo& anInfo = aSF.Info();
194
195   // Interprete results
196   TopAbs_ShapeEnum aType = anInfo.Type();
197   switch (aType)
198   {
199   case TopAbs_COMPOUND:
200   case TopAbs_COMPSOLID:
201     {
202       // (+) geompy.kind.COMPOUND     nb_solids nb_faces nb_edges nb_vertices
203       // (+) geompy.kind.COMPSOLID    nb_solids nb_faces nb_edges nb_vertices
204       // ??? "nb_faces" - all faces or only 'standalone' faces?
205       if (aType == TopAbs_COMPOUND)
206         aKind = SK_COMPOUND;
207       else
208         aKind = SK_COMPSOLID;
209
210       //theIntegers->Append(anInfo.NbSubShapes(TopAbs_COMPOUND));
211       //theIntegers->Append(anInfo.NbSubShapes(TopAbs_COMPSOLID));
212       theIntegers->Append(anInfo.NbSubShapes(TopAbs_SOLID));
213       theIntegers->Append(anInfo.NbSubShapes(TopAbs_FACE));
214       theIntegers->Append(anInfo.NbSubShapes(TopAbs_EDGE));
215       theIntegers->Append(anInfo.NbSubShapes(TopAbs_VERTEX));
216     }
217     break;
218
219   case TopAbs_SHELL:
220     {
221       // (+) geompy.kind.SHELL  geompy.info.closed   nb_faces nb_edges nb_vertices
222       // (+) geompy.kind.SHELL  geompy.info.unclosed nb_faces nb_edges nb_vertices
223       aKind = SK_SHELL;
224
225       theIntegers->Append((int)anInfo.KindOfClosed());
226
227       theIntegers->Append(anInfo.NbSubShapes(TopAbs_FACE));
228       theIntegers->Append(anInfo.NbSubShapes(TopAbs_EDGE));
229       theIntegers->Append(anInfo.NbSubShapes(TopAbs_VERTEX));
230     }
231     break;
232
233   case TopAbs_WIRE:
234     {
235       // (+) geompy.kind.WIRE  geompy.info.closed   nb_edges nb_vertices
236       // (+) geompy.kind.WIRE  geompy.info.unclosed nb_edges nb_vertices
237       aKind = SK_WIRE;
238
239       theIntegers->Append((int)anInfo.KindOfClosed());
240
241       theIntegers->Append(anInfo.NbSubShapes(TopAbs_EDGE));
242       theIntegers->Append(anInfo.NbSubShapes(TopAbs_VERTEX));
243     }
244     break;
245
246   case TopAbs_SOLID:
247     {
248       aKind = SK_SOLID;
249
250       GEOMAlgo_KindOfName aKN = anInfo.KindOfName();
251       switch (aKN)
252       {
253       case GEOMAlgo_KN_SPHERE:
254         // (+) geompy.kind.SPHERE  xc yc zc  R
255         {
256           aKind = SK_SPHERE;
257
258           gp_Pnt aC = anInfo.Location();
259           theDoubles->Append(aC.X());
260           theDoubles->Append(aC.Y());
261           theDoubles->Append(aC.Z());
262
263           theDoubles->Append(anInfo.Radius1());
264         }
265         break;
266       case GEOMAlgo_KN_CYLINDER:
267         // (+) geompy.kind.CYLINDER  xb yb zb  dx dy dz  R  H
268         {
269           aKind = SK_CYLINDER;
270
271           gp_Pnt aC = anInfo.Location();
272           theDoubles->Append(aC.X());
273           theDoubles->Append(aC.Y());
274           theDoubles->Append(aC.Z());
275
276           gp_Ax3 anAx3 = anInfo.Position();
277           gp_Dir aD = anAx3.Direction();
278           theDoubles->Append(aD.X());
279           theDoubles->Append(aD.Y());
280           theDoubles->Append(aD.Z());
281
282           theDoubles->Append(anInfo.Radius1());
283           theDoubles->Append(anInfo.Height());
284         }
285         break;
286       case GEOMAlgo_KN_BOX:
287         // (+) geompy.kind.BOX  xc yc zc  ax ay az
288         {
289           aKind = SK_BOX;
290
291           gp_Pnt aC = anInfo.Location();
292           theDoubles->Append(aC.X());
293           theDoubles->Append(aC.Y());
294           theDoubles->Append(aC.Z());
295
296           gp_Ax3 anAx3 = anInfo.Position();
297           gp_Dir aD = anAx3.Direction();
298           gp_Dir aX = anAx3.XDirection();
299
300           // ax ay az
301           if (aD.IsParallel(gp::DZ(), Precision::Angular()) &&
302               aX.IsParallel(gp::DX(), Precision::Angular())) {
303             theDoubles->Append(anInfo.Length()); // ax'
304             theDoubles->Append(anInfo.Width());  // ay'
305             theDoubles->Append(anInfo.Height()); // az'
306           }
307           else if (aD.IsParallel(gp::DZ(), Precision::Angular()) &&
308                    aX.IsParallel(gp::DY(), Precision::Angular())) {
309             theDoubles->Append(anInfo.Width());  // ay'
310             theDoubles->Append(anInfo.Length()); // ax'
311             theDoubles->Append(anInfo.Height()); // az'
312           }
313           else if (aD.IsParallel(gp::DX(), Precision::Angular()) &&
314                    aX.IsParallel(gp::DZ(), Precision::Angular())) {
315             theDoubles->Append(anInfo.Height()); // az'
316             theDoubles->Append(anInfo.Width());  // ay'
317             theDoubles->Append(anInfo.Length()); // ax'
318           }
319           else if (aD.IsParallel(gp::DX(), Precision::Angular()) &&
320                    aX.IsParallel(gp::DY(), Precision::Angular())) {
321             theDoubles->Append(anInfo.Height()); // az'
322             theDoubles->Append(anInfo.Length()); // ax'
323             theDoubles->Append(anInfo.Width());  // ay'
324           }
325           else if (aD.IsParallel(gp::DY(), Precision::Angular()) &&
326                    aX.IsParallel(gp::DZ(), Precision::Angular())) {
327             theDoubles->Append(anInfo.Width());  // ay'
328             theDoubles->Append(anInfo.Height()); // az'
329             theDoubles->Append(anInfo.Length()); // ax'
330           }
331           else if (aD.IsParallel(gp::DY(), Precision::Angular()) &&
332                    aX.IsParallel(gp::DX(), Precision::Angular())) {
333             theDoubles->Append(anInfo.Length()); // ax'
334             theDoubles->Append(anInfo.Height()); // az'
335             theDoubles->Append(anInfo.Width());  // ay'
336           }
337           else {
338             // (+) geompy.kind.ROTATED_BOX  xo yo zo  zx zy zz  xx xy xz  ax ay az
339             aKind = SK_ROTATED_BOX;
340
341             // Direction and XDirection
342             theDoubles->Append(aD.X());
343             theDoubles->Append(aD.Y());
344             theDoubles->Append(aD.Z());
345
346             theDoubles->Append(aX.X());
347             theDoubles->Append(aX.Y());
348             theDoubles->Append(aX.Z());
349
350             // ax ay az
351             theDoubles->Append(anInfo.Length());
352             theDoubles->Append(anInfo.Width());
353             theDoubles->Append(anInfo.Height());
354           }
355         }
356         break;
357       case GEOMAlgo_KN_TORUS:
358         // (+) geompy.kind.TORUS  xc yc zc  dx dy dz  R_1 R_2
359         {
360           aKind = SK_TORUS;
361
362           gp_Pnt aO = anInfo.Location();
363           theDoubles->Append(aO.X());
364           theDoubles->Append(aO.Y());
365           theDoubles->Append(aO.Z());
366
367           gp_Ax3 anAx3 = anInfo.Position();
368           gp_Dir aD = anAx3.Direction();
369           theDoubles->Append(aD.X());
370           theDoubles->Append(aD.Y());
371           theDoubles->Append(aD.Z());
372
373           theDoubles->Append(anInfo.Radius1());
374           theDoubles->Append(anInfo.Radius2());
375         }
376         break;
377       case GEOMAlgo_KN_CONE:
378         // (+) geompy.kind.CONE  xb yb zb  dx dy dz  R_1 R_2  H
379         {
380           aKind = SK_CONE;
381
382           gp_Pnt aO = anInfo.Location();
383           theDoubles->Append(aO.X());
384           theDoubles->Append(aO.Y());
385           theDoubles->Append(aO.Z());
386
387           gp_Ax3 anAx3 = anInfo.Position();
388           gp_Dir aD = anAx3.Direction();
389           theDoubles->Append(aD.X());
390           theDoubles->Append(aD.Y());
391           theDoubles->Append(aD.Z());
392
393           theDoubles->Append(anInfo.Radius1());
394           theDoubles->Append(anInfo.Radius2());
395           theDoubles->Append(anInfo.Height());
396         }
397         break;
398       case GEOMAlgo_KN_POLYHEDRON:
399         // (+) geompy.kind.POLYHEDRON  nb_faces nb_edges nb_vertices
400         {
401           aKind = SK_POLYHEDRON;
402
403           theIntegers->Append(anInfo.NbSubShapes(TopAbs_FACE));
404           theIntegers->Append(anInfo.NbSubShapes(TopAbs_EDGE));
405           theIntegers->Append(anInfo.NbSubShapes(TopAbs_VERTEX));
406         }
407         break;
408       default:
409         // (+) geompy.kind.SOLID  nb_faces nb_edges nb_vertices
410         {
411           theIntegers->Append(anInfo.NbSubShapes(TopAbs_FACE));
412           theIntegers->Append(anInfo.NbSubShapes(TopAbs_EDGE));
413           theIntegers->Append(anInfo.NbSubShapes(TopAbs_VERTEX));
414         }
415       }
416     }
417     break;
418
419   case TopAbs_FACE:
420     {
421       aKind = SK_FACE;
422
423       GEOMAlgo_KindOfName aKN = anInfo.KindOfName();
424       switch (aKN) {
425       case GEOMAlgo_KN_SPHERE:
426         // (+) geompy.kind.SPHERE2D  xc yc zc  R
427         {
428           aKind = SK_SPHERE2D;
429
430           gp_Pnt aC = anInfo.Location();
431           theDoubles->Append(aC.X());
432           theDoubles->Append(aC.Y());
433           theDoubles->Append(aC.Z());
434
435           theDoubles->Append(anInfo.Radius1());
436         }
437         break;
438       case GEOMAlgo_KN_CYLINDER:
439         // (+) geompy.kind.CYLINDER2D  xb yb zb  dx dy dz  R  H
440         {
441           aKind = SK_CYLINDER2D;
442
443           gp_Pnt aO = anInfo.Location();
444           theDoubles->Append(aO.X());
445           theDoubles->Append(aO.Y());
446           theDoubles->Append(aO.Z());
447
448           gp_Ax3 anAx3 = anInfo.Position();
449           gp_Dir aD = anAx3.Direction();
450           theDoubles->Append(aD.X());
451           theDoubles->Append(aD.Y());
452           theDoubles->Append(aD.Z());
453
454           theDoubles->Append(anInfo.Radius1());
455           theDoubles->Append(anInfo.Height());
456         }
457         break;
458       case GEOMAlgo_KN_TORUS:
459         // (+) geompy.kind.TORUS2D  xc yc zc  dx dy dz  R_1 R_2
460         {
461           aKind = SK_TORUS2D;
462
463           gp_Pnt aO = anInfo.Location();
464           theDoubles->Append(aO.X());
465           theDoubles->Append(aO.Y());
466           theDoubles->Append(aO.Z());
467
468           gp_Ax3 anAx3 = anInfo.Position();
469           gp_Dir aD = anAx3.Direction();
470           theDoubles->Append(aD.X());
471           theDoubles->Append(aD.Y());
472           theDoubles->Append(aD.Z());
473
474           theDoubles->Append(anInfo.Radius1());
475           theDoubles->Append(anInfo.Radius2());
476         }
477         break;
478       case GEOMAlgo_KN_CONE:
479         // (+) geompy.kind.CONE2D  xc yc zc  dx dy dz  R_1 R_2  H
480         {
481           aKind = SK_CONE2D;
482
483           gp_Pnt aO = anInfo.Location();
484           theDoubles->Append(aO.X());
485           theDoubles->Append(aO.Y());
486           theDoubles->Append(aO.Z());
487
488           gp_Ax3 anAx3 = anInfo.Position();
489           gp_Dir aD = anAx3.Direction();
490           theDoubles->Append(aD.X());
491           theDoubles->Append(aD.Y());
492           theDoubles->Append(aD.Z());
493
494           theDoubles->Append(anInfo.Radius1());
495           theDoubles->Append(anInfo.Radius2());
496           theDoubles->Append(anInfo.Height());
497         }
498         break;
499       case GEOMAlgo_KN_DISKCIRCLE:
500         // (+) geompy.kind.DISK_CIRCLE  xc yc zc  dx dy dz  R
501         {
502           aKind = SK_DISK_CIRCLE;
503
504           gp_Pnt aC = anInfo.Location();
505           theDoubles->Append(aC.X());
506           theDoubles->Append(aC.Y());
507           theDoubles->Append(aC.Z());
508
509           gp_Ax3 anAx3 = anInfo.Position();
510           gp_Dir aD = anAx3.Direction();
511           theDoubles->Append(aD.X());
512           theDoubles->Append(aD.Y());
513           theDoubles->Append(aD.Z());
514
515           theDoubles->Append(anInfo.Radius1());
516         }
517         break;
518       case GEOMAlgo_KN_DISKELLIPSE:
519         // (+) geompy.kind.DISK_ELLIPSE  xc yc zc  dx dy dz  R_1 R_2
520         {
521           aKind = SK_DISK_ELLIPSE;
522
523           gp_Pnt aC = anInfo.Location();
524           theDoubles->Append(aC.X());
525           theDoubles->Append(aC.Y());
526           theDoubles->Append(aC.Z());
527
528           gp_Ax3 anAx3 = anInfo.Position();
529           gp_Dir aD = anAx3.Direction();
530           theDoubles->Append(aD.X());
531           theDoubles->Append(aD.Y());
532           theDoubles->Append(aD.Z());
533
534           theDoubles->Append(anInfo.Radius1());
535           theDoubles->Append(anInfo.Radius2());
536         }
537         break;
538       case GEOMAlgo_KN_RECTANGLE:
539       case GEOMAlgo_KN_TRIANGLE:
540       case GEOMAlgo_KN_QUADRANGLE:
541       case GEOMAlgo_KN_POLYGON:
542         // (+) geompy.kind.POLYGON  xo yo zo  dx dy dz  nb_edges nb_vertices
543         {
544           aKind = SK_POLYGON;
545
546           gp_Pnt aO = anInfo.Location();
547           theDoubles->Append(aO.X());
548           theDoubles->Append(aO.Y());
549           theDoubles->Append(aO.Z());
550
551           gp_Ax3 anAx3 = anInfo.Position();
552           gp_Dir aD = anAx3.Direction();
553           theDoubles->Append(aD.X());
554           theDoubles->Append(aD.Y());
555           theDoubles->Append(aD.Z());
556
557           theIntegers->Append(anInfo.NbSubShapes(TopAbs_EDGE));
558           theIntegers->Append(anInfo.NbSubShapes(TopAbs_VERTEX));
559         }
560         break;
561       case GEOMAlgo_KN_PLANE: // infinite
562         // (+) geompy.kind.PLANE  xo yo zo  dx dy dz
563         {
564           aKind = SK_PLANE;
565
566           gp_Pnt aC = anInfo.Location();
567           theDoubles->Append(aC.X());
568           theDoubles->Append(aC.Y());
569           theDoubles->Append(aC.Z());
570
571           gp_Ax3 anAx3 = anInfo.Position();
572           gp_Dir aD = anAx3.Direction();
573           theDoubles->Append(aD.X());
574           theDoubles->Append(aD.Y());
575           theDoubles->Append(aD.Z());
576         }
577         break;
578       default:
579         if (anInfo.KindOfShape() == GEOMAlgo_KS_PLANE) {
580           // (+) geompy.kind.PLANAR  xo yo zo  dx dy dz  nb_edges nb_vertices
581
582           aKind = SK_PLANAR;
583
584           gp_Pnt aC = anInfo.Location();
585           theDoubles->Append(aC.X());
586           theDoubles->Append(aC.Y());
587           theDoubles->Append(aC.Z());
588
589           gp_Ax3 anAx3 = anInfo.Position();
590           gp_Dir aD = anAx3.Direction();
591           theDoubles->Append(aD.X());
592           theDoubles->Append(aD.Y());
593           theDoubles->Append(aD.Z());
594
595           theIntegers->Append(anInfo.NbSubShapes(TopAbs_EDGE));
596           theIntegers->Append(anInfo.NbSubShapes(TopAbs_VERTEX));
597         }
598         else {
599           // ??? geompy.kind.FACE  nb_edges nb_vertices _surface_type_id_
600           // (+) geompy.kind.FACE  nb_edges nb_vertices
601
602           theIntegers->Append(anInfo.NbSubShapes(TopAbs_EDGE));
603           theIntegers->Append(anInfo.NbSubShapes(TopAbs_VERTEX));
604         }
605       }
606     }
607     break;
608
609   case TopAbs_EDGE:
610     {
611       aKind = SK_EDGE;
612
613       GEOMAlgo_KindOfName aKN = anInfo.KindOfName();
614       switch (aKN) {
615       case GEOMAlgo_KN_CIRCLE:
616         {
617           // (+) geompy.kind.CIRCLE  xc yc zc  dx dy dz  R
618           aKind = SK_CIRCLE;
619
620           gp_Pnt aC = anInfo.Location();
621           theDoubles->Append(aC.X());
622           theDoubles->Append(aC.Y());
623           theDoubles->Append(aC.Z());
624
625           gp_Ax3 anAx3 = anInfo.Position();
626           gp_Dir aD = anAx3.Direction();
627           theDoubles->Append(aD.X());
628           theDoubles->Append(aD.Y());
629           theDoubles->Append(aD.Z());
630
631           theDoubles->Append(anInfo.Radius1());
632         }
633         break;
634       case GEOMAlgo_KN_ARCCIRCLE:
635         {
636           // (+) geompy.kind.ARC_CIRCLE  xc yc zc  dx dy dz  R  x1 y1 z1  x2 y2 z2
637           aKind = SK_ARC_CIRCLE;
638
639           gp_Pnt aC = anInfo.Location();
640           theDoubles->Append(aC.X());
641           theDoubles->Append(aC.Y());
642           theDoubles->Append(aC.Z());
643
644           gp_Ax3 anAx3 = anInfo.Position();
645           gp_Dir aD = anAx3.Direction();
646           theDoubles->Append(aD.X());
647           theDoubles->Append(aD.Y());
648           theDoubles->Append(aD.Z());
649
650           theDoubles->Append(anInfo.Radius1());
651
652           gp_Pnt aP1 = anInfo.Pnt1();
653           theDoubles->Append(aP1.X());
654           theDoubles->Append(aP1.Y());
655           theDoubles->Append(aP1.Z());
656
657           gp_Pnt aP2 = anInfo.Pnt2();
658           theDoubles->Append(aP2.X());
659           theDoubles->Append(aP2.Y());
660           theDoubles->Append(aP2.Z());
661         }
662         break;
663       case GEOMAlgo_KN_ELLIPSE:
664         {
665           // (+) geompy.kind.ELLIPSE  xc yc zc  dx dy dz  R_1 R_2
666           aKind = SK_ELLIPSE;
667
668           gp_Pnt aC = anInfo.Location();
669           theDoubles->Append(aC.X());
670           theDoubles->Append(aC.Y());
671           theDoubles->Append(aC.Z());
672
673           gp_Ax3 anAx3 = anInfo.Position();
674           gp_Dir aD = anAx3.Direction();
675           theDoubles->Append(aD.X());
676           theDoubles->Append(aD.Y());
677           theDoubles->Append(aD.Z());
678
679           theDoubles->Append(anInfo.Radius1());
680           theDoubles->Append(anInfo.Radius2());
681         }
682         break;
683       case GEOMAlgo_KN_ARCELLIPSE:
684         {
685           // (+) geompy.kind.ARC_ELLIPSE  xc yc zc  dx dy dz  R_1 R_2  x1 y1 z1  x2 y2 z2
686           aKind = SK_ARC_ELLIPSE;
687
688           gp_Pnt aC = anInfo.Location();
689           theDoubles->Append(aC.X());
690           theDoubles->Append(aC.Y());
691           theDoubles->Append(aC.Z());
692
693           gp_Ax3 anAx3 = anInfo.Position();
694           gp_Dir aD = anAx3.Direction();
695           theDoubles->Append(aD.X());
696           theDoubles->Append(aD.Y());
697           theDoubles->Append(aD.Z());
698
699           theDoubles->Append(anInfo.Radius1());
700           theDoubles->Append(anInfo.Radius2());
701
702           gp_Pnt aP1 = anInfo.Pnt1();
703           theDoubles->Append(aP1.X());
704           theDoubles->Append(aP1.Y());
705           theDoubles->Append(aP1.Z());
706
707           gp_Pnt aP2 = anInfo.Pnt2();
708           theDoubles->Append(aP2.X());
709           theDoubles->Append(aP2.Y());
710           theDoubles->Append(aP2.Z());
711         }
712         break;
713       case GEOMAlgo_KN_LINE:
714         {
715           // ??? geompy.kind.LINE  x1 y1 z1  x2 y2 z2
716           // (+) geompy.kind.LINE  x1 y1 z1  dx dy dz
717           aKind = SK_LINE;
718
719           gp_Pnt aO = anInfo.Location();
720           theDoubles->Append(aO.X());
721           theDoubles->Append(aO.Y());
722           theDoubles->Append(aO.Z());
723
724           gp_Dir aD = anInfo.Direction();
725           theDoubles->Append(aD.X());
726           theDoubles->Append(aD.Y());
727           theDoubles->Append(aD.Z());
728         }
729         break;
730       case GEOMAlgo_KN_SEGMENT:
731         {
732           // (+) geompy.kind.SEGMENT  x1 y1 z1  x2 y2 z2
733           aKind = SK_SEGMENT;
734
735           gp_Pnt aP1 = anInfo.Pnt1();
736           theDoubles->Append(aP1.X());
737           theDoubles->Append(aP1.Y());
738           theDoubles->Append(aP1.Z());
739
740           gp_Pnt aP2 = anInfo.Pnt2();
741           theDoubles->Append(aP2.X());
742           theDoubles->Append(aP2.Y());
743           theDoubles->Append(aP2.Z());
744         }
745         break;
746       default:
747         // ??? geompy.kind.EDGE  nb_vertices _curve_type_id_
748         // (+) geompy.kind.EDGE  nb_vertices
749         theIntegers->Append(anInfo.NbSubShapes(TopAbs_VERTEX));
750       }
751     }
752     break;
753
754   case TopAbs_VERTEX:
755     {
756       // (+) geompy.kind.VERTEX  x y z
757       aKind = SK_VERTEX;
758
759       gp_Pnt aP = anInfo.Location();
760       theDoubles->Append(aP.X());
761       theDoubles->Append(aP.Y());
762       theDoubles->Append(aP.Z());
763     }
764     break;
765   }
766
767   SetErrorCode(OK);
768   return aKind;
769 }
770
771 //=============================================================================
772 /*!
773  *  GetPosition
774  */
775 //=============================================================================
776 void GEOMImpl_IMeasureOperations::GetPosition
777                    (Handle(GEOM_Object) theShape,
778                     Standard_Real& Ox, Standard_Real& Oy, Standard_Real& Oz,
779                     Standard_Real& Zx, Standard_Real& Zy, Standard_Real& Zz,
780                     Standard_Real& Xx, Standard_Real& Xy, Standard_Real& Xz)
781 {
782   SetErrorCode(KO);
783
784   //Set default values: global CS
785   Ox = Oy = Oz = Zx = Zy = Xy = Xz = 0.;
786   Zz = Xx = 1.;
787
788   if (theShape.IsNull()) return;
789
790   Handle(GEOM_Function) aRefShape = theShape->GetLastFunction();
791   if (aRefShape.IsNull()) return;
792
793   TopoDS_Shape aShape = aRefShape->GetValue();
794   if (aShape.IsNull()) {
795     SetErrorCode("The Objects has NULL Shape");
796     return;
797   }
798
799   try {
800 #if OCC_VERSION_LARGE > 0x06010000
801     OCC_CATCH_SIGNALS;
802 #endif
803
804     gp_Ax3 anAx3 = GEOMUtils::GetPosition(aShape);
805
806     gp_Pnt anOri = anAx3.Location();
807     gp_Dir aDirZ = anAx3.Direction();
808     gp_Dir aDirX = anAx3.XDirection();
809
810     // Output values
811     anOri.Coord(Ox, Oy, Oz);
812     aDirZ.Coord(Zx, Zy, Zz);
813     aDirX.Coord(Xx, Xy, Xz);
814   }
815   catch (Standard_Failure) {
816     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
817     SetErrorCode(aFail->GetMessageString());
818     return;
819   }
820
821   SetErrorCode(OK);
822 }
823
824 //=============================================================================
825 /*!
826  *  GetCentreOfMass
827  */
828 //=============================================================================
829 Handle(GEOM_Object) GEOMImpl_IMeasureOperations::GetCentreOfMass
830                                                 (Handle(GEOM_Object) theShape)
831 {
832   SetErrorCode(KO);
833
834   if (theShape.IsNull()) return NULL;
835
836   //Add a new CentreOfMass object
837   Handle(GEOM_Object) aCDG = GetEngine()->AddObject(GetDocID(), GEOM_CDG);
838
839   //Add a new CentreOfMass function
840   Handle(GEOM_Function) aFunction =
841     aCDG->AddFunction(GEOMImpl_MeasureDriver::GetID(), CDG_MEASURE);
842   if (aFunction.IsNull()) return NULL;
843
844   //Check if the function is set correctly
845   if (aFunction->GetDriverGUID() != GEOMImpl_MeasureDriver::GetID()) return NULL;
846
847   GEOMImpl_IMeasure aCI (aFunction);
848
849   Handle(GEOM_Function) aRefShape = theShape->GetLastFunction();
850   if (aRefShape.IsNull()) return NULL;
851
852   aCI.SetBase(aRefShape);
853
854   //Compute the CentreOfMass value
855   try {
856 #if OCC_VERSION_LARGE > 0x06010000
857     OCC_CATCH_SIGNALS;
858 #endif
859     if (!GetSolver()->ComputeFunction(aFunction)) {
860       SetErrorCode("Measure driver failed to compute centre of mass");
861       return NULL;
862     }
863   }
864   catch (Standard_Failure) {
865     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
866     SetErrorCode(aFail->GetMessageString());
867     return NULL;
868   }
869
870   //Make a Python command
871   GEOM::TPythonDump(aFunction) << aCDG << " = geompy.MakeCDG(" << theShape << ")";
872
873   SetErrorCode(OK);
874   return aCDG;
875 }
876
877 //=============================================================================
878 /*!
879  *  GetVertexByIndex
880  */
881 //=============================================================================
882 Handle(GEOM_Object) GEOMImpl_IMeasureOperations::GetVertexByIndex
883                                                 (Handle(GEOM_Object) theShape,
884                                                  Standard_Integer theIndex)
885 {
886   SetErrorCode(KO);
887
888   if (theShape.IsNull()) return NULL;
889
890   Handle(GEOM_Function) aRefShape = theShape->GetLastFunction();
891   if (aRefShape.IsNull()) return NULL;
892
893   //Add a new Vertex object
894   Handle(GEOM_Object) aVertex = GetEngine()->AddObject(GetDocID(), GEOM_POINT);
895
896   //Add a function
897   Handle(GEOM_Function) aFunction =
898     aVertex->AddFunction(GEOMImpl_MeasureDriver::GetID(), VERTEX_BY_INDEX);
899   if (aFunction.IsNull()) return NULL;
900
901   //Check if the function is set correctly
902   if (aFunction->GetDriverGUID() != GEOMImpl_MeasureDriver::GetID()) return NULL;
903
904   GEOMImpl_IMeasure aCI (aFunction);
905   aCI.SetBase(aRefShape);
906   aCI.SetIndex(theIndex);
907
908   //Compute
909   try {
910 #if OCC_VERSION_LARGE > 0x06010000
911     OCC_CATCH_SIGNALS;
912 #endif
913     if (!GetSolver()->ComputeFunction(aFunction)) {
914       SetErrorCode("Vertex by index driver failed.");
915       return NULL;
916     }
917   }
918   catch (Standard_Failure) {
919     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
920     SetErrorCode(aFail->GetMessageString());
921     return NULL;
922   }
923
924   //Make a Python command
925   GEOM::TPythonDump(aFunction) << aVertex << " = geompy.GetVertexByIndex(" << theShape << ", " << theIndex << ")";
926
927   SetErrorCode(OK);
928   return aVertex;
929 }
930
931 //=============================================================================
932 /*!
933  *  GetNormal
934  */
935 //=============================================================================
936 Handle(GEOM_Object) GEOMImpl_IMeasureOperations::GetNormal
937                                          (Handle(GEOM_Object) theFace,
938                                           Handle(GEOM_Object) theOptionalPoint)
939 {
940   SetErrorCode(KO);
941
942   if (theFace.IsNull()) return NULL;
943
944   //Add a new Normale object
945   Handle(GEOM_Object) aNorm = GetEngine()->AddObject(GetDocID(), GEOM_VECTOR);
946
947   //Add a new Normale function
948   Handle(GEOM_Function) aFunction =
949     aNorm->AddFunction(GEOMImpl_MeasureDriver::GetID(), VECTOR_FACE_NORMALE);
950   if (aFunction.IsNull()) return NULL;
951
952   //Check if the function is set correctly
953   if (aFunction->GetDriverGUID() != GEOMImpl_MeasureDriver::GetID()) return NULL;
954
955   GEOMImpl_IMeasure aCI (aFunction);
956
957   Handle(GEOM_Function) aFace = theFace->GetLastFunction();
958   if (aFace.IsNull()) return NULL;
959
960   aCI.SetBase(aFace);
961
962   if (!theOptionalPoint.IsNull()) {
963     Handle(GEOM_Function) anOptPnt = theOptionalPoint->GetLastFunction();
964     aCI.SetPoint(anOptPnt);
965   }
966
967   //Compute the Normale value
968   try {
969 #if OCC_VERSION_LARGE > 0x06010000
970     OCC_CATCH_SIGNALS;
971 #endif
972     if (!GetSolver()->ComputeFunction(aFunction)) {
973       SetErrorCode("Measure driver failed to compute normake of face");
974       return NULL;
975     }
976   }
977   catch (Standard_Failure) {
978     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
979     SetErrorCode(aFail->GetMessageString());
980     return NULL;
981   }
982
983   //Make a Python command
984   GEOM::TPythonDump pd (aFunction);
985   pd << aNorm << " = geompy.GetNormal(" << theFace;
986   if (!theOptionalPoint.IsNull()) {
987     pd << ", " << theOptionalPoint;
988   }
989   pd << ")";
990
991   SetErrorCode(OK);
992   return aNorm;
993 }
994
995 //=============================================================================
996 /*!
997  *  GetBasicProperties
998  */
999 //=============================================================================
1000 void GEOMImpl_IMeasureOperations::GetBasicProperties (Handle(GEOM_Object) theShape,
1001                                                       Standard_Real& theLength,
1002                                                       Standard_Real& theSurfArea,
1003                                                       Standard_Real& theVolume)
1004 {
1005   SetErrorCode(KO);
1006
1007   if (theShape.IsNull()) return;
1008
1009   Handle(GEOM_Function) aRefShape = theShape->GetLastFunction();
1010   if (aRefShape.IsNull()) return;
1011
1012   TopoDS_Shape aShape = aRefShape->GetValue();
1013   if (aShape.IsNull()) {
1014     SetErrorCode("The Objects has NULL Shape");
1015     return;
1016   }
1017
1018   //Compute the parameters
1019   GProp_GProps LProps, SProps;
1020   try {
1021 #if OCC_VERSION_LARGE > 0x06010000
1022     OCC_CATCH_SIGNALS;
1023 #endif
1024     BRepGProp::LinearProperties(aShape, LProps);
1025     theLength = LProps.Mass();
1026
1027     BRepGProp::SurfaceProperties(aShape, SProps);
1028     theSurfArea = SProps.Mass();
1029
1030     theVolume = 0.0;
1031     if (aShape.ShapeType() < TopAbs_SHELL) {
1032       for (TopExp_Explorer Exp (aShape, TopAbs_SOLID); Exp.More(); Exp.Next()) {
1033         GProp_GProps VProps;
1034         BRepGProp::VolumeProperties(Exp.Current(), VProps);
1035         theVolume += VProps.Mass();
1036       }
1037     }
1038   }
1039   catch (Standard_Failure) {
1040     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
1041     SetErrorCode(aFail->GetMessageString());
1042     return;
1043   }
1044
1045   SetErrorCode(OK);
1046 }
1047
1048 //=============================================================================
1049 /*!
1050  *  GetInertia
1051  */
1052 //=============================================================================
1053 void GEOMImpl_IMeasureOperations::GetInertia
1054                    (Handle(GEOM_Object) theShape,
1055                     Standard_Real& I11, Standard_Real& I12, Standard_Real& I13,
1056                     Standard_Real& I21, Standard_Real& I22, Standard_Real& I23,
1057                     Standard_Real& I31, Standard_Real& I32, Standard_Real& I33,
1058                     Standard_Real& Ix , Standard_Real& Iy , Standard_Real& Iz)
1059 {
1060   SetErrorCode(KO);
1061
1062   if (theShape.IsNull()) return;
1063
1064   Handle(GEOM_Function) aRefShape = theShape->GetLastFunction();
1065   if (aRefShape.IsNull()) return;
1066
1067   TopoDS_Shape aShape = aRefShape->GetValue();
1068   if (aShape.IsNull()) {
1069     SetErrorCode("The Objects has NULL Shape");
1070     return;
1071   }
1072
1073   //Compute the parameters
1074   GProp_GProps System;
1075
1076   try {
1077 #if OCC_VERSION_LARGE > 0x06010000
1078     OCC_CATCH_SIGNALS;
1079 #endif
1080     if (aShape.ShapeType() == TopAbs_VERTEX ||
1081         aShape.ShapeType() == TopAbs_EDGE ||
1082         aShape.ShapeType() == TopAbs_WIRE) {
1083       BRepGProp::LinearProperties(aShape, System);
1084     } else if (aShape.ShapeType() == TopAbs_FACE ||
1085                aShape.ShapeType() == TopAbs_SHELL) {
1086       BRepGProp::SurfaceProperties(aShape, System);
1087     } else {
1088       BRepGProp::VolumeProperties(aShape, System);
1089     }
1090     gp_Mat I = System.MatrixOfInertia();
1091
1092     I11 = I(1,1);
1093     I12 = I(1,2);
1094     I13 = I(1,3);
1095
1096     I21 = I(2,1);
1097     I22 = I(2,2);
1098     I23 = I(2,3);
1099
1100     I31 = I(3,1);
1101     I32 = I(3,2);
1102     I33 = I(3,3);
1103
1104     GProp_PrincipalProps Pr = System.PrincipalProperties();
1105     Pr.Moments(Ix,Iy,Iz);
1106   }
1107   catch (Standard_Failure) {
1108     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
1109     SetErrorCode(aFail->GetMessageString());
1110     return;
1111   }
1112
1113   SetErrorCode(OK);
1114 }
1115
1116 //=============================================================================
1117 /*!
1118  *  GetBoundingBox
1119  */
1120 //=============================================================================
1121 void GEOMImpl_IMeasureOperations::GetBoundingBox
1122                                      (Handle(GEOM_Object) theShape,
1123                                       Standard_Real& Xmin, Standard_Real& Xmax,
1124                                       Standard_Real& Ymin, Standard_Real& Ymax,
1125                                       Standard_Real& Zmin, Standard_Real& Zmax)
1126 {
1127   SetErrorCode(KO);
1128
1129   if (theShape.IsNull()) return;
1130
1131   Handle(GEOM_Function) aRefShape = theShape->GetLastFunction();
1132   if (aRefShape.IsNull()) return;
1133
1134   TopoDS_Shape aShape = aRefShape->GetValue();
1135   if (aShape.IsNull()) {
1136     SetErrorCode("The Objects has NULL Shape");
1137     return;
1138   }
1139
1140   //Compute the parameters
1141   Bnd_Box B;
1142
1143   try {
1144 #if OCC_VERSION_LARGE > 0x06010000
1145     OCC_CATCH_SIGNALS;
1146 #endif
1147     BRepBuilderAPI_Copy aCopyTool (aShape);
1148     if (!aCopyTool.IsDone()) {
1149       SetErrorCode("GetBoundingBox Error: Bad shape detected");
1150       return;
1151     }
1152
1153     aShape = aCopyTool.Shape();
1154
1155     // remove triangulation to obtain more exact boundaries
1156     BRepTools::Clean(aShape);
1157
1158     BRepBndLib::Add(aShape, B);
1159     B.Get(Xmin, Ymin, Zmin, Xmax, Ymax, Zmax);
1160   }
1161   catch (Standard_Failure) {
1162     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
1163     SetErrorCode(aFail->GetMessageString());
1164     return;
1165   }
1166
1167   SetErrorCode(OK);
1168 }
1169
1170 //=============================================================================
1171 /*!
1172  *  GetBoundingBox
1173  */
1174 //=============================================================================
1175 Handle(GEOM_Object) GEOMImpl_IMeasureOperations::GetBoundingBox
1176                                                 (Handle(GEOM_Object) theShape)
1177 {
1178   SetErrorCode(KO);
1179
1180   if (theShape.IsNull()) return NULL;
1181
1182   //Add a new BoundingBox object
1183   Handle(GEOM_Object) aBnd = GetEngine()->AddObject(GetDocID(), GEOM_BOX);
1184
1185   //Add a new BoundingBox function
1186   Handle(GEOM_Function) aFunction =
1187     aBnd->AddFunction(GEOMImpl_MeasureDriver::GetID(), BND_BOX_MEASURE);
1188   if (aFunction.IsNull()) return NULL;
1189
1190   //Check if the function is set correctly
1191   if (aFunction->GetDriverGUID() != GEOMImpl_MeasureDriver::GetID()) return NULL;
1192
1193   GEOMImpl_IMeasure aCI (aFunction);
1194
1195   Handle(GEOM_Function) aRefShape = theShape->GetLastFunction();
1196   if (aRefShape.IsNull()) return NULL;
1197
1198   aCI.SetBase(aRefShape);
1199
1200   //Compute the BoundingBox value
1201   try {
1202 #if OCC_VERSION_LARGE > 0x06010000
1203     OCC_CATCH_SIGNALS;
1204 #endif
1205     if (!GetSolver()->ComputeFunction(aFunction)) {
1206       SetErrorCode("Measure driver failed to compute a bounding box");
1207       return NULL;
1208     }
1209   }
1210   catch (Standard_Failure) {
1211     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
1212     SetErrorCode(aFail->GetMessageString());
1213     return NULL;
1214   }
1215
1216   //Make a Python command
1217   GEOM::TPythonDump(aFunction) << aBnd << " = geompy.MakeBoundingBox(" << theShape << ")";
1218
1219   SetErrorCode(OK);
1220   return aBnd;
1221 }
1222
1223 //=============================================================================
1224 /*!
1225  *  GetTolerance
1226  */
1227 //=============================================================================
1228 void GEOMImpl_IMeasureOperations::GetTolerance
1229                                (Handle(GEOM_Object) theShape,
1230                                 Standard_Real& FaceMin, Standard_Real& FaceMax,
1231                                 Standard_Real& EdgeMin, Standard_Real& EdgeMax,
1232                                 Standard_Real& VertMin, Standard_Real& VertMax)
1233 {
1234   SetErrorCode(KO);
1235
1236   if (theShape.IsNull()) return;
1237
1238   Handle(GEOM_Function) aRefShape = theShape->GetLastFunction();
1239   if (aRefShape.IsNull()) return;
1240
1241   TopoDS_Shape aShape = aRefShape->GetValue();
1242   if (aShape.IsNull()) {
1243     SetErrorCode("The Objects has NULL Shape");
1244     return;
1245   }
1246
1247   //Compute the parameters
1248   Standard_Real T;
1249   FaceMin = EdgeMin = VertMin = RealLast();
1250   FaceMax = EdgeMax = VertMax = -RealLast();
1251
1252   try {
1253 #if OCC_VERSION_LARGE > 0x06010000
1254     OCC_CATCH_SIGNALS;
1255 #endif
1256     for (TopExp_Explorer ExF (aShape, TopAbs_FACE); ExF.More(); ExF.Next()) {
1257       TopoDS_Face Face = TopoDS::Face(ExF.Current());
1258       T = BRep_Tool::Tolerance(Face);
1259       if (T > FaceMax)
1260         FaceMax = T;
1261       if (T < FaceMin)
1262         FaceMin = T;
1263     }
1264     for (TopExp_Explorer ExE (aShape, TopAbs_EDGE); ExE.More(); ExE.Next()) {
1265       TopoDS_Edge Edge = TopoDS::Edge(ExE.Current());
1266       T = BRep_Tool::Tolerance(Edge);
1267       if (T > EdgeMax)
1268         EdgeMax = T;
1269       if (T < EdgeMin)
1270         EdgeMin = T;
1271     }
1272     for (TopExp_Explorer ExV (aShape, TopAbs_VERTEX); ExV.More(); ExV.Next()) {
1273       TopoDS_Vertex Vertex = TopoDS::Vertex(ExV.Current());
1274       T = BRep_Tool::Tolerance(Vertex);
1275       if (T > VertMax)
1276         VertMax = T;
1277       if (T < VertMin)
1278         VertMin = T;
1279     }
1280   }
1281   catch (Standard_Failure) {
1282     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
1283     SetErrorCode(aFail->GetMessageString());
1284     return;
1285   }
1286
1287   SetErrorCode(OK);
1288 }
1289
1290 //=============================================================================
1291 /*!
1292  *  CheckShape
1293  */
1294 //=============================================================================
1295 bool GEOMImpl_IMeasureOperations::CheckShape (Handle(GEOM_Object)      theShape,
1296                                               const Standard_Boolean   theIsCheckGeom,
1297                                               TCollection_AsciiString& theDump)
1298 {
1299   SetErrorCode(KO);
1300
1301   if (theShape.IsNull()) return false;
1302
1303   Handle(GEOM_Function) aRefShape = theShape->GetLastFunction();
1304   if (aRefShape.IsNull()) return false;
1305
1306   TopoDS_Shape aShape = aRefShape->GetValue();
1307   if (aShape.IsNull()) {
1308     SetErrorCode("The Objects has NULL Shape");
1309     return false;
1310   }
1311
1312   //Compute the parameters
1313   bool isValid = false;
1314   try {
1315 #if OCC_VERSION_LARGE > 0x06010000
1316     OCC_CATCH_SIGNALS;
1317 #endif
1318     BRepCheck_Analyzer ana (aShape, theIsCheckGeom);
1319     if (ana.IsValid()) {
1320       theDump.Clear();
1321       isValid = true;
1322     } else {
1323       StructuralDump(ana, aShape, theDump);
1324     }
1325   }
1326   catch (Standard_Failure) {
1327     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
1328     SetErrorCode(aFail->GetMessageString());
1329     return false;
1330   }
1331
1332   SetErrorCode(OK);
1333   return isValid;
1334 }
1335
1336 //=============================================================================
1337 /*!
1338  *  CheckSelfIntersections
1339  */
1340 //=============================================================================
1341 bool GEOMImpl_IMeasureOperations::CheckSelfIntersections
1342                          (Handle(GEOM_Object)                 theShape,
1343                           Handle(TColStd_HSequenceOfInteger)& theIntersections)
1344 {
1345   SetErrorCode(KO);
1346   bool isGood = false;
1347
1348   if (theIntersections.IsNull())
1349     theIntersections = new TColStd_HSequenceOfInteger;
1350   else
1351     theIntersections->Clear();
1352
1353   if (theShape.IsNull())
1354     return isGood;
1355
1356   Handle(GEOM_Function) aRefShape = theShape->GetLastFunction();
1357   if (aRefShape.IsNull()) return isGood;
1358
1359   TopoDS_Shape aShape = aRefShape->GetValue();
1360   if (aShape.IsNull()) return isGood;
1361
1362   // 0. Prepare data
1363   BRep_Builder aBB;
1364   TopoDS_Compound aCS;
1365   TopoDS_Shape aScopy;
1366   NMTDS_Tools::CopyShape(aShape, aScopy);
1367
1368   // Map sub-shapes and their indices
1369   TopTools_IndexedMapOfShape anIndices;
1370   TopExp::MapShapes(aScopy, anIndices);
1371
1372   aBB.MakeCompound(aCS);
1373   aBB.Add(aCS, aScopy);
1374
1375   NMTTools_CheckerSI aCSI; // checker of self-interferences
1376   aCSI.SetCompositeShape(aCS);
1377
1378   // 1. Launch the checker
1379   aCSI.Perform();
1380   Standard_Integer iErr = aCSI.StopStatus();
1381   if (iErr) {
1382     return false; // Error
1383   }
1384
1385   isGood = true;
1386
1387   // 2. Take the shapes from DS
1388   const NMTDS_ShapesDataStructure& aDS = *(aCSI.DS());
1389   Standard_Integer aNbS = aDS.NumberOfShapesOfTheObject();
1390
1391   // 3. Get the pairs of interfered shapes
1392   NMTDS_PInterfPool pIP = aCSI.IP();
1393   //const NMTDS_ListOfPassKeyBoolean& aLPKB = pIP->Get();
1394   const NMTDS_ListOfPairBoolean& aLPKB = pIP->Get();
1395
1396   Standard_Integer n1, n2;
1397   //NMTDS_ListIteratorOfListOfPassKeyBoolean aIt;
1398   NMTDS_ListIteratorOfListOfPairBoolean aIt;
1399
1400   aIt.Initialize(aLPKB);
1401   for (; aIt.More(); aIt.Next()) {
1402     //const NMTDS_PassKeyBoolean& aPKB = aIt.Value();
1403     const NMTDS_PairBoolean& aPKB = aIt.Value();
1404     aPKB.Ids(n1, n2);
1405
1406     if (n1 > aNbS || n2 > aNbS)
1407       return false; // Error
1408
1409     const TopoDS_Shape& aS1 = aDS.Shape(n1);
1410     const TopoDS_Shape& aS2 = aDS.Shape(n2);
1411
1412     theIntersections->Append(anIndices.FindIndex(aS1));
1413     theIntersections->Append(anIndices.FindIndex(aS2));
1414     isGood = false;
1415   }
1416
1417   SetErrorCode(OK);
1418   return isGood;
1419 }
1420
1421 //=============================================================================
1422 /*!
1423  *  IsGoodForSolid
1424  */
1425 //=============================================================================
1426 TCollection_AsciiString GEOMImpl_IMeasureOperations::IsGoodForSolid (Handle(GEOM_Object) theShape)
1427 {
1428   SetErrorCode(KO);
1429
1430   TCollection_AsciiString aRes = "";
1431
1432   if (theShape.IsNull()) {
1433     aRes = "WRN_NULL_OBJECT_OR_SHAPE";
1434   }
1435   else {
1436     Handle(GEOM_Function) aRefShape = theShape->GetLastFunction();
1437     if (aRefShape.IsNull()) {
1438       aRes = "WRN_NULL_OBJECT_OR_SHAPE";
1439     }
1440     else {
1441       TopoDS_Shape aShape = aRefShape->GetValue();
1442       if (aShape.IsNull()) {
1443         aRes = "WRN_NULL_OBJECT_OR_SHAPE";
1444       }
1445       else {
1446         if (aShape.ShapeType() == TopAbs_COMPOUND) {
1447           TopoDS_Iterator It (aShape, Standard_True, Standard_True);
1448           if (It.More()) aShape = It.Value();
1449         }
1450         if (aShape.ShapeType() == TopAbs_SHELL) {
1451           BRepCheck_Shell chkShell (TopoDS::Shell(aShape));
1452           if (chkShell.Closed() == BRepCheck_NotClosed) {
1453             aRes = "WRN_SHAPE_UNCLOSED";
1454           }
1455         }
1456         else {
1457           aRes = "WRN_SHAPE_NOT_SHELL";
1458         }
1459       }
1460     }
1461   }
1462
1463   if (aRes.IsEmpty())
1464     SetErrorCode(OK);
1465
1466   return aRes;
1467 }
1468
1469 //=============================================================================
1470 /*!
1471  *  WhatIs
1472  */
1473 //=============================================================================
1474 TCollection_AsciiString GEOMImpl_IMeasureOperations::WhatIs (Handle(GEOM_Object) theShape)
1475 {
1476   SetErrorCode(KO);
1477
1478   TCollection_AsciiString Astr;
1479
1480   if (theShape.IsNull()) return Astr;
1481
1482   Handle(GEOM_Function) aRefShape = theShape->GetLastFunction();
1483   if (aRefShape.IsNull()) return Astr;
1484
1485   TopoDS_Shape aShape = aRefShape->GetValue();
1486   if (aShape.IsNull()) {
1487     SetErrorCode("The Objects has NULL Shape");
1488     return Astr;
1489   }
1490
1491   //Compute the parameters
1492   if (aShape.ShapeType() == TopAbs_EDGE) {
1493     if (BRep_Tool::Degenerated(TopoDS::Edge(aShape))) {
1494       Astr = Astr + " It is a degenerated edge \n";
1495     }
1496   }
1497
1498   Astr = Astr + " Number of sub-shapes : \n";
1499
1500   try {
1501 #if OCC_VERSION_LARGE > 0x06010000
1502     OCC_CATCH_SIGNALS;
1503 #endif
1504     int iType, nbTypes [TopAbs_SHAPE];
1505     for (iType = 0; iType < TopAbs_SHAPE; ++iType)
1506       nbTypes[iType] = 0;
1507     nbTypes[aShape.ShapeType()]++;
1508
1509     TopTools_MapOfShape aMapOfShape;
1510     aMapOfShape.Add(aShape);
1511     TopTools_ListOfShape aListOfShape;
1512     aListOfShape.Append(aShape);
1513
1514     TopTools_ListIteratorOfListOfShape itL (aListOfShape);
1515     for (; itL.More(); itL.Next()) {
1516       TopoDS_Iterator it (itL.Value());
1517       for (; it.More(); it.Next()) {
1518         TopoDS_Shape s = it.Value();
1519         if (aMapOfShape.Add(s)) {
1520           aListOfShape.Append(s);
1521           nbTypes[s.ShapeType()]++;
1522         }
1523       }
1524     }
1525
1526     Astr = Astr + " VERTEX : " + TCollection_AsciiString(nbTypes[TopAbs_VERTEX]) + "\n";
1527     Astr = Astr + " EDGE : " + TCollection_AsciiString(nbTypes[TopAbs_EDGE]) + "\n";
1528     Astr = Astr + " WIRE : " + TCollection_AsciiString(nbTypes[TopAbs_WIRE]) + "\n";
1529     Astr = Astr + " FACE : " + TCollection_AsciiString(nbTypes[TopAbs_FACE]) + "\n";
1530     Astr = Astr + " SHELL : " + TCollection_AsciiString(nbTypes[TopAbs_SHELL]) + "\n";
1531     Astr = Astr + " SOLID : " + TCollection_AsciiString(nbTypes[TopAbs_SOLID]) + "\n";
1532     Astr = Astr + " COMPSOLID : " + TCollection_AsciiString(nbTypes[TopAbs_COMPSOLID]) + "\n";
1533     Astr = Astr + " COMPOUND : " + TCollection_AsciiString(nbTypes[TopAbs_COMPOUND]) + "\n";
1534     Astr = Astr + " SHAPE : " + TCollection_AsciiString(aMapOfShape.Extent());
1535   }
1536   catch (Standard_Failure) {
1537     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
1538     SetErrorCode(aFail->GetMessageString());
1539     return Astr;
1540   }
1541
1542   SetErrorCode(OK);
1543   return Astr;
1544 }
1545
1546 //=============================================================================
1547 /*!
1548  *  AreCoordsInside
1549  */
1550 //=============================================================================
1551 std::vector<bool> GEOMImpl_IMeasureOperations::AreCoordsInside(Handle(GEOM_Object) theShape,
1552                                                                const std::vector<double>& coords,
1553                                                                double tolerance)
1554 {
1555   std::vector<bool> res;
1556   if (!theShape.IsNull()) {
1557     Handle(GEOM_Function) aRefShape = theShape->GetLastFunction();
1558     if (!aRefShape.IsNull()) {
1559       TopoDS_Shape aShape = aRefShape->GetValue();
1560       if (!aShape.IsNull()) {
1561         BRepClass3d_SolidClassifier SC(aShape);
1562         unsigned int nb_points = coords.size()/3;
1563         for (int i = 0; i < nb_points; i++) {
1564           double x = coords[3*i];
1565           double y = coords[3*i+1];
1566           double z = coords[3*i+2];
1567           gp_Pnt aPnt(x, y, z);
1568           SC.Perform(aPnt, tolerance);
1569           res.push_back( ( SC.State() == TopAbs_IN ) || ( SC.State() == TopAbs_ON ) );
1570         }
1571       }
1572     }
1573   }
1574   return res;
1575 }
1576
1577 //=======================================================================
1578 //function : CheckSingularCase
1579 //purpose  : auxilary for GetMinDistance()
1580 //           workaround for bugs 19899, 19908 and 19910 from Mantis
1581 //=======================================================================
1582 static double CheckSingularCase(const TopoDS_Shape& aSh1,
1583                                 const TopoDS_Shape& aSh2,
1584                                 gp_Pnt& Ptmp1, gp_Pnt& Ptmp2)
1585 {
1586   bool IsChange1 = false;
1587   double AddDist1 = 0.0;
1588   TopExp_Explorer anExp;
1589   TopoDS_Shape tmpSh1, tmpSh2;
1590   int nbf = 0;
1591   for ( anExp.Init( aSh1, TopAbs_FACE ); anExp.More(); anExp.Next() ) {
1592     nbf++;
1593     tmpSh1 = anExp.Current();
1594   }
1595   if(nbf==1) {
1596     TopoDS_Shape sh = aSh1;
1597     while(sh.ShapeType()==TopAbs_COMPOUND) {
1598       TopoDS_Iterator it(sh);
1599       sh = it.Value();
1600     }
1601     Handle(Geom_Surface) S = BRep_Tool::Surface(TopoDS::Face(tmpSh1));
1602     if( S->IsKind(STANDARD_TYPE(Geom_SphericalSurface)) ||
1603         S->IsKind(STANDARD_TYPE(Geom_ToroidalSurface)) ) {
1604       if( sh.ShapeType()==TopAbs_SHELL || sh.ShapeType()==TopAbs_FACE ) {
1605         // non solid case
1606         double U1,U2,V1,V2;
1607         // changes for 0020677: EDF 1219 GEOM: MinDistance gives 0 instead of 20.88
1608         //S->Bounds(U1,U2,V1,V2); changed by
1609         ShapeAnalysis::GetFaceUVBounds(TopoDS::Face(tmpSh1),U1,U2,V1,V2);
1610         // end of changes for 020677 (dmv)
1611         Handle(Geom_RectangularTrimmedSurface) TrS1 =
1612           new Geom_RectangularTrimmedSurface(S,U1,(U1+U2)/2.,V1,V2);
1613         Handle(Geom_RectangularTrimmedSurface) TrS2 =
1614           new Geom_RectangularTrimmedSurface(S,(U1+U2)/2.,U2,V1,V2);
1615         BRep_Builder B;
1616         TopoDS_Face F1,F2;
1617         TopoDS_Compound Comp;
1618         B.MakeCompound(Comp);
1619         B.MakeFace(F1,TrS1,1.e-7);
1620         B.Add(Comp,F1);
1621         B.MakeFace(F2,TrS2,1.e-7);
1622         B.Add(Comp,F2);
1623         Handle(ShapeFix_Shape) sfs = new ShapeFix_Shape;
1624         sfs->Init(Comp);
1625         sfs->SetPrecision(1.e-6);
1626         sfs->SetMaxTolerance(1.0);
1627         sfs->Perform();
1628         tmpSh1 = sfs->Shape();
1629         IsChange1 = true;
1630       }
1631       else {
1632         if( S->IsKind(STANDARD_TYPE(Geom_SphericalSurface)) ) {
1633           Handle(Geom_SphericalSurface) SS = Handle(Geom_SphericalSurface)::DownCast(S);
1634           gp_Pnt PC = SS->Location();
1635           BRep_Builder B;
1636           TopoDS_Vertex V;
1637           B.MakeVertex(V,PC,1.e-7);
1638           tmpSh1 = V;
1639           AddDist1 = SS->Radius();
1640           IsChange1 = true;
1641         }
1642         else {
1643           Handle(Geom_ToroidalSurface) TS = Handle(Geom_ToroidalSurface)::DownCast(S);
1644           gp_Ax3 ax3 = TS->Position();
1645           Handle(Geom_Circle) C = new Geom_Circle(ax3.Ax2(),TS->MajorRadius());
1646           BRep_Builder B;
1647           TopoDS_Edge E;
1648           B.MakeEdge(E,C,1.e-7);
1649           tmpSh1 = E;
1650           AddDist1 = TS->MinorRadius();
1651           IsChange1 = true;
1652         }
1653       }
1654     }
1655     else
1656       tmpSh1 = aSh1;
1657   }
1658   else
1659     tmpSh1 = aSh1;
1660   bool IsChange2 = false;
1661   double AddDist2 = 0.0;
1662   nbf = 0;
1663   for ( anExp.Init( aSh2, TopAbs_FACE ); anExp.More(); anExp.Next() ) {
1664     nbf++;
1665     tmpSh2 = anExp.Current();
1666   }
1667   if(nbf==1) {
1668     TopoDS_Shape sh = aSh2;
1669     while(sh.ShapeType()==TopAbs_COMPOUND) {
1670       TopoDS_Iterator it(sh);
1671       sh = it.Value();
1672     }
1673     Handle(Geom_Surface) S = BRep_Tool::Surface(TopoDS::Face(tmpSh2));
1674     if( S->IsKind(STANDARD_TYPE(Geom_SphericalSurface)) ||
1675         S->IsKind(STANDARD_TYPE(Geom_ToroidalSurface)) ) {
1676       if( sh.ShapeType()==TopAbs_SHELL || sh.ShapeType()==TopAbs_FACE ) {
1677         // non solid case
1678         double U1,U2,V1,V2;
1679         //S->Bounds(U1,U2,V1,V2);
1680         ShapeAnalysis::GetFaceUVBounds(TopoDS::Face(tmpSh2),U1,U2,V1,V2);
1681         Handle(Geom_RectangularTrimmedSurface) TrS1 =
1682           new Geom_RectangularTrimmedSurface(S,U1,(U1+U2)/2.,V1,V2);
1683         Handle(Geom_RectangularTrimmedSurface) TrS2 =
1684           new Geom_RectangularTrimmedSurface(S,(U1+U2)/2.,U2,V1,V2);
1685         BRep_Builder B;
1686         TopoDS_Face F1,F2;
1687         TopoDS_Compound Comp;
1688         B.MakeCompound(Comp);
1689         B.MakeFace(F1,TrS1,1.e-7);
1690         B.Add(Comp,F1);
1691         B.MakeFace(F2,TrS2,1.e-7);
1692         B.Add(Comp,F2);
1693         Handle(ShapeFix_Shape) sfs = new ShapeFix_Shape;
1694         sfs->Init(Comp);
1695         sfs->SetPrecision(1.e-6);
1696         sfs->SetMaxTolerance(1.0);
1697         sfs->Perform();
1698         tmpSh2 = sfs->Shape();
1699         IsChange2 = true;
1700       }
1701       else {
1702         if( S->IsKind(STANDARD_TYPE(Geom_SphericalSurface)) ) {
1703           Handle(Geom_SphericalSurface) SS = Handle(Geom_SphericalSurface)::DownCast(S);
1704           gp_Pnt PC = SS->Location();
1705           BRep_Builder B;
1706           TopoDS_Vertex V;
1707           B.MakeVertex(V,PC,1.e-7);
1708           tmpSh2 = V;
1709           AddDist2 = SS->Radius();
1710           IsChange2 = true;
1711         }
1712         else if( S->IsKind(STANDARD_TYPE(Geom_ToroidalSurface)) ) {
1713           Handle(Geom_ToroidalSurface) TS = Handle(Geom_ToroidalSurface)::DownCast(S);
1714           gp_Ax3 ax3 = TS->Position();
1715           Handle(Geom_Circle) C = new Geom_Circle(ax3.Ax2(),TS->MajorRadius());
1716           BRep_Builder B;
1717           TopoDS_Edge E;
1718           B.MakeEdge(E,C,1.e-7);
1719           tmpSh2 = E;
1720           AddDist2 = TS->MinorRadius();
1721           IsChange2 = true;
1722         }
1723       }
1724     }
1725     else
1726       tmpSh2 = aSh2;
1727   }
1728   else
1729     tmpSh2 = aSh2;
1730
1731   if( !IsChange1 && !IsChange2 )
1732     return -2.0;
1733
1734   BRepExtrema_DistShapeShape dst(tmpSh1,tmpSh2);
1735   if (dst.IsDone()) {
1736     double MinDist = 1.e9;
1737     gp_Pnt PMin1, PMin2, P1, P2;
1738     for (int i = 1; i <= dst.NbSolution(); i++) {
1739       P1 = dst.PointOnShape1(i);
1740       P2 = dst.PointOnShape2(i);
1741       Standard_Real Dist = P1.Distance(P2);
1742       if (MinDist > Dist) {
1743         MinDist = Dist;
1744         PMin1 = P1;
1745         PMin2 = P2;
1746       }
1747     }
1748     if(MinDist<1.e-7) {
1749       Ptmp1 = PMin1;
1750       Ptmp2 = PMin2;
1751     }
1752     else {
1753       gp_Dir aDir(gp_Vec(PMin1,PMin2));
1754       if( MinDist > (AddDist1+AddDist2) ) {
1755         Ptmp1 = gp_Pnt( PMin1.X() + aDir.X()*AddDist1,
1756                         PMin1.Y() + aDir.Y()*AddDist1,
1757                         PMin1.Z() + aDir.Z()*AddDist1 );
1758         Ptmp2 = gp_Pnt( PMin2.X() - aDir.X()*AddDist2,
1759                         PMin2.Y() - aDir.Y()*AddDist2,
1760                         PMin2.Z() - aDir.Z()*AddDist2 );
1761         return (MinDist - AddDist1 - AddDist2);
1762       }
1763       else {
1764         if( AddDist1 > 0 ) {
1765           Ptmp1 = gp_Pnt( PMin1.X() + aDir.X()*AddDist1,
1766                           PMin1.Y() + aDir.Y()*AddDist1,
1767                           PMin1.Z() + aDir.Z()*AddDist1 );
1768           Ptmp2 = Ptmp1;
1769         }
1770         else {
1771           Ptmp2 = gp_Pnt( PMin2.X() - aDir.X()*AddDist2,
1772                           PMin2.Y() - aDir.Y()*AddDist2,
1773                           PMin2.Z() - aDir.Z()*AddDist2 );
1774           Ptmp1 = Ptmp2;
1775         }
1776       }
1777     }
1778     double res = MinDist - AddDist1 - AddDist2;
1779     if(res<0.) res = 0.0;
1780     return res;
1781   }
1782   return -2.0;
1783 }
1784 /* old variant
1785 static bool CheckSingularCase(const TopoDS_Shape& aSh1,
1786                               const TopoDS_Shape& aSh2,
1787                               gp_Pnt& Ptmp)
1788 {
1789   TopExp_Explorer anExp;
1790   TopoDS_Shape tmpSh1, tmpSh2;
1791   int nbf = 0;
1792   for ( anExp.Init( aSh1, TopAbs_FACE ); anExp.More(); anExp.Next() ) {
1793     nbf++;
1794     tmpSh1 = anExp.Current();
1795   }
1796   if(nbf==1) {
1797     Handle(Geom_Surface) S1 = BRep_Tool::Surface(TopoDS::Face(tmpSh1));
1798     if( S1->IsKind(STANDARD_TYPE(Geom_SphericalSurface)) ||
1799         S1->IsKind(STANDARD_TYPE(Geom_ToroidalSurface)) ) {
1800       nbf = 0;
1801       for ( anExp.Init( aSh2, TopAbs_FACE ); anExp.More(); anExp.Next() ) {
1802         nbf++;
1803         tmpSh2 = anExp.Current();
1804         Handle(Geom_Surface) S2 = BRep_Tool::Surface(TopoDS::Face(tmpSh2));
1805         GeomAPI_IntSS ISS(S1,S2,1.e-7);
1806         if(ISS.IsDone()) {
1807           for(int i=1; i<=ISS.NbLines(); i++) {
1808             Handle(Geom_Curve) C3d = ISS.Line(i);
1809             BRep_Builder B;
1810             TopoDS_Edge E;
1811             B.MakeEdge(E,C3d,1.e-7);
1812             BRepExtrema_DistShapeShape dst(tmpSh2,E);
1813             if (dst.IsDone()) {
1814               gp_Pnt PMin1, PMin2, P1, P2;
1815               double MinDist = 1.e9;
1816               for (int i = 1; i <= dst.NbSolution(); i++) {
1817                 P1 = dst.PointOnShape1(i);
1818                 P2 = dst.PointOnShape2(i);
1819                 Standard_Real Dist = P1.Distance(P2);
1820                 if (MinDist > Dist) {
1821                   MinDist = Dist;
1822                   Ptmp = P1;
1823                 }
1824               }
1825               if(MinDist<1.e-7)
1826                 return true;
1827             }
1828           }
1829         }
1830       }
1831     }
1832   }
1833   nbf = 0;
1834   for ( anExp.Init( aSh2, TopAbs_FACE ); anExp.More(); anExp.Next() ) {
1835     nbf++;
1836     tmpSh1 = anExp.Current();
1837   }
1838   if(nbf==1) {
1839     Handle(Geom_Surface) S1 = BRep_Tool::Surface(TopoDS::Face(tmpSh1));
1840     if( S1->IsKind(STANDARD_TYPE(Geom_SphericalSurface)) ||
1841         S1->IsKind(STANDARD_TYPE(Geom_ToroidalSurface)) ) {
1842       nbf = 0;
1843       for ( anExp.Init( aSh1, TopAbs_FACE ); anExp.More(); anExp.Next() ) {
1844         nbf++;
1845         tmpSh2 = anExp.Current();
1846         Handle(Geom_Surface) S2 = BRep_Tool::Surface(TopoDS::Face(tmpSh2));
1847         GeomAPI_IntSS ISS(S1,S2,1.e-7);
1848         if(ISS.IsDone()) {
1849           for(int i=1; i<=ISS.NbLines(); i++) {
1850             Handle(Geom_Curve) C3d = ISS.Line(i);
1851             BRep_Builder B;
1852             TopoDS_Edge E;
1853             B.MakeEdge(E,C3d,1.e-7);
1854             BRepExtrema_DistShapeShape dst(tmpSh2,E);
1855             if (dst.IsDone()) {
1856               gp_Pnt P1,P2;
1857               double MinDist = 1.e9;
1858               for (int i = 1; i <= dst.NbSolution(); i++) {
1859                 P1 = dst.PointOnShape1(i);
1860                 P2 = dst.PointOnShape2(i);
1861                 Standard_Real Dist = P1.Distance(P2);
1862                 if (MinDist > Dist) {
1863                   MinDist = Dist;
1864                   Ptmp = P1;
1865                 }
1866               }
1867               if(MinDist<1.e-7)
1868                 return true;
1869             }
1870           }
1871         }
1872       }
1873     }
1874   }
1875   return false;
1876 }
1877 */
1878
1879 //=============================================================================
1880 /*!
1881  *  GetMinDistance
1882  */
1883 //=============================================================================
1884 Standard_Real GEOMImpl_IMeasureOperations::GetMinDistance
1885   (Handle(GEOM_Object) theShape1, Handle(GEOM_Object) theShape2,
1886    Standard_Real& X1, Standard_Real& Y1, Standard_Real& Z1,
1887    Standard_Real& X2, Standard_Real& Y2, Standard_Real& Z2)
1888 {
1889   SetErrorCode(KO);
1890   Standard_Real MinDist = 1.e9;
1891
1892   if (theShape1.IsNull() || theShape2.IsNull()) return MinDist;
1893
1894   Handle(GEOM_Function) aRefShape1 = theShape1->GetLastFunction();
1895   Handle(GEOM_Function) aRefShape2 = theShape2->GetLastFunction();
1896   if (aRefShape1.IsNull() || aRefShape2.IsNull()) return MinDist;
1897
1898   TopoDS_Shape aShape1 = aRefShape1->GetValue();
1899   TopoDS_Shape aShape2 = aRefShape2->GetValue();
1900   if (aShape1.IsNull() || aShape2.IsNull()) {
1901     SetErrorCode("One of Objects has NULL Shape");
1902     return MinDist;
1903   }
1904
1905   //Compute the parameters
1906   try {
1907 #if OCC_VERSION_LARGE > 0x06010000
1908     OCC_CATCH_SIGNALS;
1909 #endif
1910
1911     // Issue 0020231: A min distance bug with torus and vertex.
1912     // Make GetMinDistance() return zero if a sole VERTEX is inside any of SOLIDs
1913
1914     // which of shapes consists of only one vertex?
1915     TopExp_Explorer exp1(aShape1,TopAbs_VERTEX), exp2(aShape2,TopAbs_VERTEX);
1916     TopoDS_Shape V1 = exp1.More() ? exp1.Current() : TopoDS_Shape();
1917     TopoDS_Shape V2 = exp2.More() ? exp2.Current() : TopoDS_Shape();
1918     exp1.Next(); exp2.Next();
1919     if ( exp1.More() ) V1.Nullify();
1920     if ( exp2.More() ) V2.Nullify();
1921     // vertex and container of solids
1922     TopoDS_Shape V = V1.IsNull() ? V2 : V1;
1923     TopoDS_Shape S = V1.IsNull() ? aShape1 : aShape2;
1924     if ( !V.IsNull() ) {
1925       // classify vertex against solids
1926       gp_Pnt p = BRep_Tool::Pnt( TopoDS::Vertex( V ) );
1927       for ( exp1.Init( S, TopAbs_SOLID ); exp1.More(); exp1.Next() ) {
1928         BRepClass3d_SolidClassifier classifier( exp1.Current(), p, 1e-6);
1929         if ( classifier.State() == TopAbs_IN ) {
1930           p.Coord(X1, Y1, Z1);
1931           p.Coord(X2, Y2, Z2);
1932           SetErrorCode(OK);
1933           return 0.0;
1934         }
1935       }
1936     }
1937     // End Issue 0020231
1938
1939     // skl 30.06.2008
1940     // additional workaround for bugs 19899, 19908 and 19910 from Mantis
1941     gp_Pnt Ptmp1, Ptmp2;
1942     double dist = CheckSingularCase(aShape1, aShape2, Ptmp1, Ptmp2);
1943     if (dist > -1.0) {
1944       Ptmp1.Coord(X1, Y1, Z1);
1945       Ptmp2.Coord(X2, Y2, Z2);
1946       SetErrorCode(OK);
1947       return dist;
1948     }
1949
1950     BRepExtrema_DistShapeShape dst (aShape1, aShape2);
1951     if (dst.IsDone()) {
1952       gp_Pnt PMin1, PMin2, P1, P2;
1953
1954       for (int i = 1; i <= dst.NbSolution(); i++) {
1955         P1 = dst.PointOnShape1(i);
1956         P2 = dst.PointOnShape2(i);
1957
1958         Standard_Real Dist = P1.Distance(P2);
1959         if (MinDist > Dist) {
1960           MinDist = Dist;
1961           PMin1 = P1;
1962           PMin2 = P2;
1963         }
1964       }
1965
1966       PMin1.Coord(X1, Y1, Z1);
1967       PMin2.Coord(X2, Y2, Z2);
1968     }
1969   }
1970   catch (Standard_Failure) {
1971     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
1972     SetErrorCode(aFail->GetMessageString());
1973     return MinDist;
1974   }
1975
1976   SetErrorCode(OK);
1977   return MinDist;
1978 }
1979
1980 //=======================================================================
1981 /*!
1982  *  Get coordinates of closest points of two shapes
1983  */
1984 //=======================================================================
1985 Standard_Integer GEOMImpl_IMeasureOperations::ClosestPoints (Handle(GEOM_Object) theShape1,
1986                                                              Handle(GEOM_Object) theShape2,
1987                                                              Handle(TColStd_HSequenceOfReal)& theDoubles)
1988 {
1989   SetErrorCode(KO);
1990   Standard_Integer nbSolutions = 0;
1991
1992   if (theShape1.IsNull() || theShape2.IsNull()) return nbSolutions;
1993
1994   Handle(GEOM_Function) aRefShape1 = theShape1->GetLastFunction();
1995   Handle(GEOM_Function) aRefShape2 = theShape2->GetLastFunction();
1996   if (aRefShape1.IsNull() || aRefShape2.IsNull()) return nbSolutions;
1997
1998   TopoDS_Shape aShape1 = aRefShape1->GetValue();
1999   TopoDS_Shape aShape2 = aRefShape2->GetValue();
2000   if (aShape1.IsNull() || aShape2.IsNull()) {
2001     SetErrorCode("One of Objects has NULL Shape");
2002     return nbSolutions;
2003   }
2004
2005   // Compute the extremities
2006   try {
2007 #if OCC_VERSION_LARGE > 0x06010000
2008     OCC_CATCH_SIGNALS;
2009 #endif
2010
2011     // skl 30.06.2008
2012     // additional workaround for bugs 19899, 19908 and 19910 from Mantis
2013     gp_Pnt P1, P2;
2014     double dist = CheckSingularCase(aShape1, aShape2, P1, P2);
2015     if (dist > -1.0) {
2016       nbSolutions = 1;
2017
2018       theDoubles->Append(P1.X());
2019       theDoubles->Append(P1.Y());
2020       theDoubles->Append(P1.Z());
2021       theDoubles->Append(P2.X());
2022       theDoubles->Append(P2.Y());
2023       theDoubles->Append(P2.Z());
2024
2025       SetErrorCode(OK);
2026       return nbSolutions;
2027     }
2028
2029     BRepExtrema_DistShapeShape dst (aShape1, aShape2);
2030     if (dst.IsDone()) {
2031       nbSolutions = dst.NbSolution();
2032       if (theDoubles.IsNull()) theDoubles = new TColStd_HSequenceOfReal;
2033
2034       gp_Pnt P1, P2;
2035       for (int i = 1; i <= nbSolutions; i++) {
2036         P1 = dst.PointOnShape1(i);
2037         P2 = dst.PointOnShape2(i);
2038
2039         theDoubles->Append(P1.X());
2040         theDoubles->Append(P1.Y());
2041         theDoubles->Append(P1.Z());
2042         theDoubles->Append(P2.X());
2043         theDoubles->Append(P2.Y());
2044         theDoubles->Append(P2.Z());
2045       }
2046     }
2047   }
2048   catch (Standard_Failure) {
2049     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
2050     SetErrorCode(aFail->GetMessageString());
2051     return nbSolutions;
2052   }
2053
2054   SetErrorCode(OK);
2055   return nbSolutions;
2056 }
2057
2058 //=======================================================================
2059 /*!
2060  *  Get coordinates of point
2061  */
2062 //=======================================================================
2063 void GEOMImpl_IMeasureOperations::PointCoordinates (Handle(GEOM_Object) theShape,
2064                         Standard_Real& theX, Standard_Real& theY, Standard_Real& theZ)
2065 {
2066   SetErrorCode(KO);
2067
2068   if (theShape.IsNull())
2069     return;
2070
2071   Handle(GEOM_Function) aRefShape = theShape->GetLastFunction();
2072   if (aRefShape.IsNull())
2073     return;
2074
2075   TopoDS_Shape aShape = aRefShape->GetValue();
2076   if (aShape.IsNull() || aShape.ShapeType() != TopAbs_VERTEX)
2077   {
2078     SetErrorCode( "Shape must be a vertex" );
2079     return;
2080   }
2081
2082   try {
2083 #if OCC_VERSION_LARGE > 0x06010000
2084     OCC_CATCH_SIGNALS;
2085 #endif
2086     gp_Pnt aPnt = BRep_Tool::Pnt( TopoDS::Vertex( aShape ) );
2087     theX = aPnt.X();
2088     theY = aPnt.Y();
2089     theZ = aPnt.Z();
2090
2091     SetErrorCode(OK);
2092   }
2093   catch (Standard_Failure)
2094   {
2095     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
2096     SetErrorCode( aFail->GetMessageString() );
2097   }
2098 }
2099
2100 //=======================================================================
2101 /*!
2102  *  Compute angle (in degrees) between two lines
2103  */
2104 //=======================================================================
2105 Standard_Real GEOMImpl_IMeasureOperations::GetAngle (Handle(GEOM_Object) theLine1,
2106                                                      Handle(GEOM_Object) theLine2)
2107 {
2108   if (theLine1->GetType() == GEOM_VECTOR &&
2109       theLine2->GetType() == GEOM_VECTOR)
2110     return GetAngleBtwVectors(theLine1, theLine2);
2111
2112   SetErrorCode(KO);
2113
2114   Standard_Real anAngle = -1.0;
2115
2116   if (theLine1.IsNull() || theLine2.IsNull())
2117     return anAngle;
2118
2119   Handle(GEOM_Function) aRefLine1 = theLine1->GetLastFunction();
2120   Handle(GEOM_Function) aRefLine2 = theLine2->GetLastFunction();
2121   if (aRefLine1.IsNull() || aRefLine2.IsNull())
2122     return anAngle;
2123
2124   TopoDS_Shape aLine1 = aRefLine1->GetValue();
2125   TopoDS_Shape aLine2 = aRefLine2->GetValue();
2126   if (aLine1.IsNull() || aLine2.IsNull() ||
2127       aLine1.ShapeType() != TopAbs_EDGE ||
2128       aLine2.ShapeType() != TopAbs_EDGE)
2129   {
2130     SetErrorCode("Two edges must be given");
2131     return anAngle;
2132   }
2133
2134   try {
2135 #if OCC_VERSION_LARGE > 0x06010000
2136     OCC_CATCH_SIGNALS;
2137 #endif
2138     TopoDS_Edge E1 = TopoDS::Edge(aLine1);
2139     TopoDS_Edge E2 = TopoDS::Edge(aLine2);
2140
2141     double fp,lp;
2142     Handle(Geom_Curve) C1 = BRep_Tool::Curve(E1,fp,lp);
2143     Handle(Geom_Curve) C2 = BRep_Tool::Curve(E2,fp,lp);
2144
2145     if ( C1.IsNull() || C2.IsNull() ||
2146         !C1->IsKind(STANDARD_TYPE(Geom_Line)) ||
2147         !C2->IsKind(STANDARD_TYPE(Geom_Line)))
2148     {
2149       SetErrorCode("The edges must be linear");
2150       return anAngle;
2151     }
2152
2153     Handle(Geom_Line) L1 = Handle(Geom_Line)::DownCast(C1);
2154     Handle(Geom_Line) L2 = Handle(Geom_Line)::DownCast(C2);
2155
2156     gp_Lin aLin1 = L1->Lin();
2157     gp_Lin aLin2 = L2->Lin();
2158
2159     anAngle = aLin1.Angle(aLin2);
2160     anAngle *= 180. / M_PI; // convert radians into degrees
2161
2162     if (anAngle > 90.0) {
2163       anAngle = 180.0 - anAngle;
2164     }
2165
2166     SetErrorCode(OK);
2167   }
2168   catch (Standard_Failure)
2169   {
2170     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
2171     SetErrorCode(aFail->GetMessageString());
2172   }
2173
2174   return anAngle;
2175 }
2176
2177 //=======================================================================
2178 /*!
2179  *  Compute angle (in degrees) between two vectors
2180  */
2181 //=======================================================================
2182 Standard_Real GEOMImpl_IMeasureOperations::GetAngleBtwVectors (Handle(GEOM_Object) theVec1,
2183                                                                Handle(GEOM_Object) theVec2)
2184 {
2185   SetErrorCode(KO);
2186
2187   Standard_Real anAngle = -1.0;
2188
2189   if (theVec1.IsNull() || theVec2.IsNull())
2190     return anAngle;
2191
2192   if (theVec1->GetType() != GEOM_VECTOR || theVec2->GetType() != GEOM_VECTOR) {
2193     SetErrorCode("Two vectors must be given");
2194     return anAngle;
2195   }
2196
2197   Handle(GEOM_Function) aRefVec1 = theVec1->GetLastFunction();
2198   Handle(GEOM_Function) aRefVec2 = theVec2->GetLastFunction();
2199   if (aRefVec1.IsNull() || aRefVec2.IsNull())
2200     return anAngle;
2201
2202   TopoDS_Shape aVec1 = aRefVec1->GetValue();
2203   TopoDS_Shape aVec2 = aRefVec2->GetValue();
2204   if (aVec1.IsNull() || aVec2.IsNull() ||
2205       aVec1.ShapeType() != TopAbs_EDGE ||
2206       aVec2.ShapeType() != TopAbs_EDGE)
2207   {
2208     SetErrorCode("Two edges must be given");
2209     return anAngle;
2210   }
2211
2212   try {
2213 #if OCC_VERSION_LARGE > 0x06010000
2214     OCC_CATCH_SIGNALS;
2215 #endif
2216     TopoDS_Edge aE1 = TopoDS::Edge(aVec1);
2217     TopoDS_Edge aE2 = TopoDS::Edge(aVec2);
2218
2219     TopoDS_Vertex aP11, aP12, aP21, aP22;
2220     TopExp::Vertices(aE1, aP11, aP12, Standard_True);
2221     TopExp::Vertices(aE2, aP21, aP22, Standard_True);
2222     if (aP11.IsNull() || aP12.IsNull() || aP21.IsNull() || aP22.IsNull()) {
2223       SetErrorCode("Bad edge given");
2224       return anAngle;
2225     }
2226
2227     gp_Vec aV1 (BRep_Tool::Pnt(aP11), BRep_Tool::Pnt(aP12));
2228     gp_Vec aV2 (BRep_Tool::Pnt(aP21), BRep_Tool::Pnt(aP22)) ;
2229
2230     anAngle = aV1.Angle(aV2);
2231     anAngle *= 180. / M_PI; // convert radians into degrees
2232
2233     SetErrorCode(OK);
2234   }
2235   catch (Standard_Failure)
2236   {
2237     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
2238     SetErrorCode(aFail->GetMessageString());
2239   }
2240
2241   return anAngle;
2242 }
2243
2244
2245 //=============================================================================
2246 /*!
2247  *  CurveCurvatureByParam
2248  */
2249 //=============================================================================
2250 Standard_Real GEOMImpl_IMeasureOperations::CurveCurvatureByParam
2251                         (Handle(GEOM_Object) theCurve, Standard_Real& theParam)
2252 {
2253   SetErrorCode(KO);
2254   Standard_Real aRes = -1.0;
2255
2256   if(theCurve.IsNull()) return aRes;
2257
2258   Handle(GEOM_Function) aRefShape = theCurve->GetLastFunction();
2259   if(aRefShape.IsNull()) return aRes;
2260
2261   TopoDS_Shape aShape = aRefShape->GetValue();
2262   if(aShape.IsNull()) {
2263     SetErrorCode("One of Objects has NULL Shape");
2264     return aRes;
2265   }
2266
2267   Standard_Real aFP, aLP, aP;
2268   Handle(Geom_Curve) aCurve = BRep_Tool::Curve(TopoDS::Edge(aShape), aFP, aLP);
2269   aP = aFP + (aLP - aFP) * theParam;
2270
2271   if(aCurve.IsNull()) return aRes;
2272
2273   //Compute curvature
2274   try {
2275 #if OCC_VERSION_LARGE > 0x06010000
2276     OCC_CATCH_SIGNALS;
2277 #endif
2278     GeomLProp_CLProps Prop = GeomLProp_CLProps
2279       (aCurve, aP, 2, Precision::Confusion());
2280     aRes = fabs(Prop.Curvature());
2281     SetErrorCode(OK);
2282   }
2283   catch (Standard_Failure) {
2284     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
2285     SetErrorCode(aFail->GetMessageString());
2286     return aRes;
2287   }
2288
2289   if( aRes > Precision::Confusion() )
2290     aRes = 1/aRes;
2291   else
2292     aRes = RealLast();
2293
2294   return aRes;
2295 }
2296
2297
2298 //=============================================================================
2299 /*!
2300  *  CurveCurvatureByPoint
2301  */
2302 //=============================================================================
2303 Standard_Real GEOMImpl_IMeasureOperations::CurveCurvatureByPoint
2304                    (Handle(GEOM_Object) theCurve, Handle(GEOM_Object) thePoint)
2305 {
2306   SetErrorCode(KO);
2307   Standard_Real aRes = -1.0;
2308
2309   if( theCurve.IsNull() || thePoint.IsNull() ) return aRes;
2310
2311   Handle(GEOM_Function) aRefCurve = theCurve->GetLastFunction();
2312   Handle(GEOM_Function) aRefPoint = thePoint->GetLastFunction();
2313   if( aRefCurve.IsNull() || aRefPoint.IsNull() ) return aRes;
2314
2315   TopoDS_Edge anEdge = TopoDS::Edge(aRefCurve->GetValue());
2316   TopoDS_Vertex aPnt = TopoDS::Vertex(aRefPoint->GetValue());
2317   if( anEdge.IsNull() || aPnt.IsNull() ) {
2318     SetErrorCode("One of Objects has NULL Shape");
2319     return aRes;
2320   }
2321
2322   Standard_Real aFP, aLP;
2323   Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFP, aLP);
2324   if(aCurve.IsNull()) return aRes;
2325   gp_Pnt aPoint = BRep_Tool::Pnt(aPnt);
2326
2327   //Compute curvature
2328   try {
2329 #if OCC_VERSION_LARGE > 0x06010000
2330     OCC_CATCH_SIGNALS;
2331 #endif
2332     GeomAPI_ProjectPointOnCurve PPCurve(aPoint, aCurve, aFP, aLP);
2333     if(PPCurve.NbPoints()>0) {
2334       GeomLProp_CLProps Prop = GeomLProp_CLProps
2335         (aCurve, PPCurve.LowerDistanceParameter(), 2, Precision::Confusion());
2336       aRes = fabs(Prop.Curvature());
2337       SetErrorCode(OK);
2338     }
2339   }
2340   catch (Standard_Failure) {
2341     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
2342     SetErrorCode(aFail->GetMessageString());
2343     return aRes;
2344   }
2345
2346   if( aRes > Precision::Confusion() )
2347     aRes = 1/aRes;
2348   else
2349     aRes = RealLast();
2350
2351   return aRes;
2352 }
2353
2354
2355 //=============================================================================
2356 /*!
2357  *  getSurfaceCurvatures
2358  */
2359 //=============================================================================
2360 Standard_Real GEOMImpl_IMeasureOperations::getSurfaceCurvatures
2361                                           (const Handle(Geom_Surface)& aSurf,
2362                                            Standard_Real theUParam,
2363                                            Standard_Real theVParam,
2364                                            Standard_Boolean theNeedMaxCurv)
2365 {
2366   SetErrorCode(KO);
2367   Standard_Real aRes = 1.0;
2368
2369   if (aSurf.IsNull()) return aRes;
2370
2371   try {
2372 #if OCC_VERSION_LARGE > 0x06010000
2373     OCC_CATCH_SIGNALS;
2374 #endif
2375     GeomLProp_SLProps Prop = GeomLProp_SLProps
2376       (aSurf, theUParam, theVParam, 2, Precision::Confusion());
2377     if(Prop.IsCurvatureDefined()) {
2378       if(Prop.IsUmbilic()) {
2379         //cout<<"is umbilic"<<endl;
2380         aRes = fabs(Prop.MeanCurvature());
2381       }
2382       else {
2383         //cout<<"is not umbilic"<<endl;
2384         double c1 = fabs(Prop.MaxCurvature());
2385         double c2 = fabs(Prop.MinCurvature());
2386         if(theNeedMaxCurv)
2387           aRes = Max(c1,c2);
2388         else
2389           aRes = Min(c1,c2);
2390       }
2391       SetErrorCode(OK);
2392     }
2393   }
2394   catch (Standard_Failure) {
2395     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
2396     SetErrorCode(aFail->GetMessageString());
2397     return aRes;
2398   }
2399
2400   if( fabs(aRes) > Precision::Confusion() )
2401     aRes = 1/aRes;
2402   else
2403     aRes = RealLast();
2404
2405   return aRes;
2406 }
2407
2408
2409 //=============================================================================
2410 /*!
2411  *  MaxSurfaceCurvatureByParam
2412  */
2413 //=============================================================================
2414 Standard_Real GEOMImpl_IMeasureOperations::MaxSurfaceCurvatureByParam
2415                                                   (Handle(GEOM_Object) theSurf,
2416                                                    Standard_Real& theUParam,
2417                                                    Standard_Real& theVParam)
2418 {
2419   SetErrorCode(KO);
2420   Standard_Real aRes = -1.0;
2421
2422   if (theSurf.IsNull()) return aRes;
2423
2424   Handle(GEOM_Function) aRefShape = theSurf->GetLastFunction();
2425   if(aRefShape.IsNull()) return aRes;
2426
2427   TopoDS_Shape aShape = aRefShape->GetValue();
2428   if(aShape.IsNull()) {
2429     SetErrorCode("One of Objects has NULL Shape");
2430     return aRes;
2431   }
2432
2433   TopoDS_Face F = TopoDS::Face(aShape);
2434   Handle(Geom_Surface) aSurf = BRep_Tool::Surface(F);
2435
2436   //Compute the parameters
2437   Standard_Real U1,U2,V1,V2;
2438   ShapeAnalysis::GetFaceUVBounds(F,U1,U2,V1,V2);
2439   Standard_Real U = U1 + (U2-U1)*theUParam;
2440   Standard_Real V = V1 + (V2-V1)*theVParam;
2441
2442   return getSurfaceCurvatures(aSurf, U, V, true);
2443 }
2444
2445
2446 //=============================================================================
2447 /*!
2448  *  MaxSurfaceCurvatureByPoint
2449  */
2450 //=============================================================================
2451 Standard_Real GEOMImpl_IMeasureOperations::MaxSurfaceCurvatureByPoint
2452                     (Handle(GEOM_Object) theSurf, Handle(GEOM_Object) thePoint)
2453 {
2454   SetErrorCode(KO);
2455   Standard_Real aRes = -1.0;
2456
2457   if( theSurf.IsNull() || thePoint.IsNull() ) return aRes;
2458
2459   Handle(GEOM_Function) aRefShape = theSurf->GetLastFunction();
2460   Handle(GEOM_Function) aRefPoint = thePoint->GetLastFunction();
2461   if( aRefShape.IsNull() || aRefPoint.IsNull() ) return aRes;
2462
2463   TopoDS_Face aFace = TopoDS::Face(aRefShape->GetValue());
2464   TopoDS_Vertex aPnt = TopoDS::Vertex(aRefPoint->GetValue());
2465   if( aFace.IsNull() || aPnt.IsNull() ) {
2466     SetErrorCode("One of Objects has NULL Shape");
2467     return 0;
2468   }
2469
2470   Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
2471   if(aSurf.IsNull()) return aRes;
2472   gp_Pnt aPoint = BRep_Tool::Pnt(aPnt);
2473
2474   //Compute the parameters
2475   ShapeAnalysis_Surface sas(aSurf);
2476   gp_Pnt2d UV = sas.ValueOfUV(aPoint,Precision::Confusion());
2477
2478   return getSurfaceCurvatures(aSurf, UV.X(), UV.Y(), true);
2479 }
2480
2481
2482 //=============================================================================
2483 /*!
2484  *  MinSurfaceCurvatureByParam
2485  */
2486 //=============================================================================
2487 Standard_Real GEOMImpl_IMeasureOperations::MinSurfaceCurvatureByParam
2488                                                   (Handle(GEOM_Object) theSurf,
2489                                                    Standard_Real& theUParam,
2490                                                    Standard_Real& theVParam)
2491 {
2492   SetErrorCode(KO);
2493   Standard_Real aRes = -1.0;
2494
2495   if (theSurf.IsNull()) return aRes;
2496
2497   Handle(GEOM_Function) aRefShape = theSurf->GetLastFunction();
2498   if(aRefShape.IsNull()) return aRes;
2499
2500   TopoDS_Shape aShape = aRefShape->GetValue();
2501   if(aShape.IsNull()) {
2502     SetErrorCode("One of Objects has NULL Shape");
2503     return aRes;
2504   }
2505
2506   TopoDS_Face F = TopoDS::Face(aShape);
2507   Handle(Geom_Surface) aSurf = BRep_Tool::Surface(F);
2508
2509   //Compute the parameters
2510   Standard_Real U1,U2,V1,V2;
2511   ShapeAnalysis::GetFaceUVBounds(F,U1,U2,V1,V2);
2512   Standard_Real U = U1 + (U2-U1)*theUParam;
2513   Standard_Real V = V1 + (V2-V1)*theVParam;
2514
2515   return getSurfaceCurvatures(aSurf, U, V, false);
2516 }
2517
2518
2519 //=============================================================================
2520 /*!
2521  *  MinSurfaceCurvatureByPoint
2522  */
2523 //=============================================================================
2524 Standard_Real GEOMImpl_IMeasureOperations::MinSurfaceCurvatureByPoint
2525                     (Handle(GEOM_Object) theSurf, Handle(GEOM_Object) thePoint)
2526 {
2527   SetErrorCode(KO);
2528   Standard_Real aRes = -1.0;
2529
2530   if( theSurf.IsNull() || thePoint.IsNull() ) return aRes;
2531
2532   Handle(GEOM_Function) aRefShape = theSurf->GetLastFunction();
2533   Handle(GEOM_Function) aRefPoint = thePoint->GetLastFunction();
2534   if( aRefShape.IsNull() || aRefPoint.IsNull() ) return aRes;
2535
2536   TopoDS_Face aFace = TopoDS::Face(aRefShape->GetValue());
2537   TopoDS_Vertex aPnt = TopoDS::Vertex(aRefPoint->GetValue());
2538   if( aFace.IsNull() || aPnt.IsNull() ) {
2539     SetErrorCode("One of Objects has NULL Shape");
2540     return 0;
2541   }
2542
2543   Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
2544   if(aSurf.IsNull()) return aRes;
2545   gp_Pnt aPoint = BRep_Tool::Pnt(aPnt);
2546
2547   //Compute the parameters
2548   ShapeAnalysis_Surface sas(aSurf);
2549   gp_Pnt2d UV = sas.ValueOfUV(aPoint,Precision::Confusion());
2550
2551   return getSurfaceCurvatures(aSurf, UV.X(), UV.Y(), false);
2552 }
2553
2554
2555 //=======================================================================
2556 //function : StructuralDump
2557 //purpose  : Structural (data exchange) style of output.
2558 //=======================================================================
2559 void GEOMImpl_IMeasureOperations::StructuralDump (const BRepCheck_Analyzer& theAna,
2560                                                   const TopoDS_Shape&       theShape,
2561                                                   TCollection_AsciiString&  theDump)
2562 {
2563   Standard_Integer i;
2564   theDump.Clear();
2565   theDump += " -- The Shape has problems :\n";
2566   theDump += "  Check                                    Count\n";
2567   theDump += " ------------------------------------------------\n";
2568
2569   Standard_Integer last_stat = (Standard_Integer)BRepCheck_CheckFail;
2570   Handle(TColStd_HArray1OfInteger) NbProblems =
2571     new TColStd_HArray1OfInteger(1, last_stat);
2572   for (i = 1; i <= last_stat; i++)
2573     NbProblems->SetValue(i,0);
2574
2575   Handle(TopTools_HSequenceOfShape) sl;
2576   sl = new TopTools_HSequenceOfShape();
2577   TopTools_DataMapOfShapeListOfShape theMap;
2578   theMap.Clear();
2579   GetProblemShapes(theAna, theShape, sl, NbProblems, theMap);
2580   theMap.Clear();
2581
2582   Standard_Integer count = 0;
2583   count = NbProblems->Value((Standard_Integer)BRepCheck_InvalidPointOnCurve);
2584   if (count > 0) {
2585     theDump += "  Invalid Point on Curve ................... ";
2586     theDump += TCollection_AsciiString(count) + "\n";
2587   }
2588   count = NbProblems->Value((Standard_Integer)BRepCheck_InvalidPointOnCurveOnSurface);
2589   if (count > 0) {
2590     theDump += "  Invalid Point on CurveOnSurface .......... ";
2591     theDump += TCollection_AsciiString(count) + "\n";
2592   }
2593   count = NbProblems->Value((Standard_Integer)BRepCheck_InvalidPointOnSurface);
2594   if (count > 0) {
2595     theDump += "  Invalid Point on Surface ................. ";
2596     theDump += TCollection_AsciiString(count) + "\n";
2597   }
2598   count = NbProblems->Value((Standard_Integer)BRepCheck_No3DCurve);
2599   if (count > 0) {
2600     theDump += "  No 3D Curve .............................. ";
2601     theDump += TCollection_AsciiString(count) + "\n";
2602   }
2603   count = NbProblems->Value((Standard_Integer)BRepCheck_Multiple3DCurve);
2604   if (count > 0) {
2605     theDump += "  Multiple 3D Curve ........................ ";
2606     theDump += TCollection_AsciiString(count) + "\n";
2607   }
2608   count = NbProblems->Value((Standard_Integer)BRepCheck_Invalid3DCurve);
2609   if (count > 0) {
2610     theDump += "  Invalid 3D Curve ......................... ";
2611     theDump += TCollection_AsciiString(count) + "\n";
2612   }
2613   count = NbProblems->Value((Standard_Integer)BRepCheck_NoCurveOnSurface);
2614   if (count > 0) {
2615     theDump += "  No Curve on Surface ...................... ";
2616     theDump += TCollection_AsciiString(count) + "\n";
2617   }
2618   count = NbProblems->Value((Standard_Integer)BRepCheck_InvalidCurveOnSurface);
2619   if (count > 0) {
2620     theDump += "  Invalid Curve on Surface ................. ";
2621     theDump += TCollection_AsciiString(count) + "\n";
2622   }
2623   count = NbProblems->Value((Standard_Integer)BRepCheck_InvalidCurveOnClosedSurface);
2624   if (count > 0) {
2625     theDump += "  Invalid Curve on closed Surface .......... ";
2626     theDump += TCollection_AsciiString(count) + "\n";
2627   }
2628   count = NbProblems->Value((Standard_Integer)BRepCheck_InvalidSameRangeFlag);
2629   if (count > 0) {
2630     theDump += "  Invalid SameRange Flag ................... ";
2631     theDump += TCollection_AsciiString(count) + "\n";
2632   }
2633   count = NbProblems->Value((Standard_Integer)BRepCheck_InvalidSameParameterFlag);
2634   if (count > 0) {
2635     theDump += "  Invalid SameParameter Flag ............... ";
2636     theDump += TCollection_AsciiString(count) + "\n";
2637   }
2638   count = NbProblems->Value((Standard_Integer)BRepCheck_InvalidDegeneratedFlag);
2639   if (count > 0) {
2640     theDump += "  Invalid Degenerated Flag ................. ";
2641     theDump += TCollection_AsciiString(count) + "\n";
2642   }
2643   count = NbProblems->Value((Standard_Integer)BRepCheck_FreeEdge);
2644   if (count > 0) {
2645     theDump += "  Free Edge ................................ ";
2646     theDump += TCollection_AsciiString(count) + "\n";
2647   }
2648   count = NbProblems->Value((Standard_Integer)BRepCheck_InvalidMultiConnexity);
2649   if (count > 0) {
2650     theDump += "  Invalid MultiConnexity ................... ";
2651     theDump += TCollection_AsciiString(count) + "\n";
2652   }
2653   count = NbProblems->Value((Standard_Integer)BRepCheck_InvalidRange);
2654   if (count > 0) {
2655     theDump += "  Invalid Range ............................ ";
2656     theDump += TCollection_AsciiString(count) + "\n";
2657   }
2658   count = NbProblems->Value((Standard_Integer)BRepCheck_EmptyWire);
2659   if (count > 0) {
2660     theDump += "  Empty Wire ............................... ";
2661     theDump += TCollection_AsciiString(count) + "\n";
2662   }
2663   count = NbProblems->Value((Standard_Integer)BRepCheck_RedundantEdge);
2664   if (count > 0) {
2665     theDump += "  Redundant Edge ........................... ";
2666     theDump += TCollection_AsciiString(count) + "\n";
2667   }
2668   count = NbProblems->Value((Standard_Integer)BRepCheck_SelfIntersectingWire);
2669   if (count > 0) {
2670     theDump += "  Self Intersecting Wire ................... ";
2671     theDump += TCollection_AsciiString(count) + "\n";
2672   }
2673   count = NbProblems->Value((Standard_Integer)BRepCheck_NoSurface);
2674   if (count > 0) {
2675     theDump += "  No Surface ............................... ";
2676     theDump += TCollection_AsciiString(count) + "\n";
2677   }
2678   count = NbProblems->Value((Standard_Integer)BRepCheck_InvalidWire);
2679   if (count > 0) {
2680     theDump += "  Invalid Wire ............................. ";
2681     theDump += TCollection_AsciiString(count) + "\n";
2682   }
2683   count = NbProblems->Value((Standard_Integer)BRepCheck_RedundantWire);
2684   if (count > 0) {
2685     theDump += "  Redundant Wire ........................... ";
2686     theDump += TCollection_AsciiString(count) + "\n";
2687   }
2688   count = NbProblems->Value((Standard_Integer)BRepCheck_IntersectingWires);
2689   if (count > 0) {
2690     theDump += "  Intersecting Wires ....................... ";
2691     theDump += TCollection_AsciiString(count) + "\n";
2692   }
2693   count = NbProblems->Value((Standard_Integer)BRepCheck_InvalidImbricationOfWires);
2694   if (count > 0) {
2695     theDump += "  Invalid Imbrication of Wires ............. ";
2696     theDump += TCollection_AsciiString(count) + "\n";
2697   }
2698   count = NbProblems->Value((Standard_Integer)BRepCheck_EmptyShell);
2699   if (count > 0) {
2700     theDump += "  Empty Shell .............................. ";
2701     theDump += TCollection_AsciiString(count) + "\n";
2702   }
2703   count = NbProblems->Value((Standard_Integer)BRepCheck_RedundantFace);
2704   if (count > 0) {
2705     theDump += "  Redundant Face ........................... ";
2706     theDump += TCollection_AsciiString(count) + "\n";
2707   }
2708   count = NbProblems->Value((Standard_Integer)BRepCheck_UnorientableShape);
2709   if (count > 0) {
2710     theDump += "  Unorientable Shape ....................... ";
2711     theDump += TCollection_AsciiString(count) + "\n";
2712   }
2713   count = NbProblems->Value((Standard_Integer)BRepCheck_NotClosed);
2714   if (count > 0) {
2715     theDump += "  Not Closed ............................... ";
2716     theDump += TCollection_AsciiString(count) + "\n";
2717   }
2718   count = NbProblems->Value((Standard_Integer)BRepCheck_NotConnected);
2719   if (count > 0) {
2720     theDump += "  Not Connected ............................ ";
2721     theDump += TCollection_AsciiString(count) + "\n";
2722   }
2723   count = NbProblems->Value((Standard_Integer)BRepCheck_SubshapeNotInShape);
2724   if (count > 0) {
2725     theDump += "  Sub-shape not in Shape .................... ";
2726     theDump += TCollection_AsciiString(count) + "\n";
2727   }
2728   count = NbProblems->Value((Standard_Integer)BRepCheck_BadOrientation);
2729   if (count > 0) {
2730     theDump += "  Bad Orientation .......................... ";
2731     theDump += TCollection_AsciiString(count) + "\n";
2732   }
2733   count = NbProblems->Value((Standard_Integer)BRepCheck_BadOrientationOfSubshape);
2734   if (count > 0) {
2735     theDump += "  Bad Orientation of Sub-shape .............. ";
2736     theDump += TCollection_AsciiString(count) + "\n";
2737   }
2738   count = NbProblems->Value((Standard_Integer)BRepCheck_CheckFail);
2739   if (count > 0) {
2740     theDump += "  checkshape failure ....................... ";
2741     theDump += TCollection_AsciiString(count) + "\n";
2742   }
2743
2744   theDump += " ------------------------------------------------\n";
2745   theDump += "*** Shapes with problems : ";
2746   theDump += TCollection_AsciiString(sl->Length()) + "\n";
2747
2748   Standard_Integer nbv, nbe, nbw, nbf, nbs, nbo;
2749   nbv = nbe = nbw = nbf = nbs = nbo = 0;
2750
2751   for (i = 1; i <= sl->Length(); i++) {
2752     TopoDS_Shape shi = sl->Value(i);
2753     TopAbs_ShapeEnum sti = shi.ShapeType();
2754     switch (sti) {
2755       case TopAbs_VERTEX : nbv++; break;
2756       case TopAbs_EDGE   : nbe++; break;
2757       case TopAbs_WIRE   : nbw++; break;
2758       case TopAbs_FACE   : nbf++; break;
2759       case TopAbs_SHELL  : nbs++; break;
2760       case TopAbs_SOLID  : nbo++; break;
2761       default            : break;
2762     }
2763   }
2764
2765   if (nbv > 0) {
2766     theDump += "VERTEX : ";
2767     if (nbv < 10) theDump += " ";
2768     theDump += TCollection_AsciiString(nbv) + "\n";
2769   }
2770   if (nbe > 0) {
2771     theDump += "EDGE   : ";
2772     if (nbe < 10) theDump += " ";
2773     theDump += TCollection_AsciiString(nbe) + "\n";
2774   }
2775   if (nbw > 0) {
2776     theDump += "WIRE   : ";
2777     if (nbw < 10) theDump += " ";
2778     theDump += TCollection_AsciiString(nbw) + "\n";
2779   }
2780   if (nbf > 0) {
2781     theDump += "FACE   : ";
2782     if (nbf < 10) theDump += " ";
2783     theDump += TCollection_AsciiString(nbf) + "\n";
2784   }
2785   if (nbs > 0) {
2786     theDump += "SHELL  : ";
2787     if (nbs < 10) theDump += " ";
2788     theDump += TCollection_AsciiString(nbs) + "\n";
2789   }
2790   if (nbo > 0) {
2791     theDump += "SOLID  : ";
2792     if (nbo < 10) theDump += " ";
2793     theDump += TCollection_AsciiString(nbo) + "\n";
2794   }
2795 }
2796
2797
2798 //=======================================================================
2799 //function : GetProblemShapes
2800 // purpose : for StructuralDump
2801 //=======================================================================
2802 void GEOMImpl_IMeasureOperations::GetProblemShapes (const BRepCheck_Analyzer&           theAna,
2803                                                     const TopoDS_Shape&                 theShape,
2804                                                     Handle(TopTools_HSequenceOfShape)&  sl,
2805                                                     Handle(TColStd_HArray1OfInteger)&   NbProblems,
2806                                                     TopTools_DataMapOfShapeListOfShape& theMap)
2807 {
2808   for (TopoDS_Iterator iter(theShape); iter.More(); iter.Next()) {
2809     GetProblemShapes(theAna, iter.Value(), sl, NbProblems, theMap);
2810   }
2811   TopAbs_ShapeEnum styp = theShape.ShapeType();
2812   BRepCheck_ListIteratorOfListOfStatus itl;
2813   TopTools_ListOfShape empty;
2814   if (!theMap.IsBound(theShape)) {
2815     theMap.Bind(theShape,empty);
2816
2817     if (!theAna.Result(theShape).IsNull()) {
2818       itl.Initialize(theAna.Result(theShape)->Status());
2819       // !!! May be, we have to print all the problems, not only the first one ?
2820       if (itl.Value() != BRepCheck_NoError) {
2821         sl->Append(theShape);
2822         BRepCheck_Status stat = itl.Value();
2823         NbProblems->SetValue((Standard_Integer)stat,
2824                              NbProblems->Value((Standard_Integer)stat) + 1);
2825       }
2826     }
2827   }
2828
2829   switch (styp) {
2830   case TopAbs_EDGE:
2831     GetProblemSub(theAna, theShape, sl, NbProblems, TopAbs_VERTEX, theMap);
2832     break;
2833   case TopAbs_FACE:
2834     GetProblemSub(theAna, theShape, sl, NbProblems, TopAbs_WIRE, theMap);
2835     GetProblemSub(theAna, theShape, sl, NbProblems, TopAbs_EDGE, theMap);
2836     GetProblemSub(theAna, theShape, sl, NbProblems, TopAbs_VERTEX, theMap);
2837     break;
2838   case TopAbs_SHELL:
2839     break;
2840   case TopAbs_SOLID:
2841     GetProblemSub(theAna, theShape, sl, NbProblems, TopAbs_SHELL, theMap);
2842     break;
2843   default:
2844     break;
2845   }
2846 }
2847
2848 //=======================================================================
2849 //function : Contains
2850 //=======================================================================
2851 static Standard_Boolean Contains (const TopTools_ListOfShape& L,
2852                                   const TopoDS_Shape& S)
2853 {
2854   TopTools_ListIteratorOfListOfShape it;
2855   for (it.Initialize(L); it.More(); it.Next()) {
2856     if (it.Value().IsSame(S)) {
2857       return Standard_True;
2858     }
2859   }
2860   return Standard_False;
2861 }
2862
2863 //=======================================================================
2864 //function : GetProblemSub
2865 // purpose : for StructuralDump
2866 //=======================================================================
2867 void GEOMImpl_IMeasureOperations::GetProblemSub (const BRepCheck_Analyzer&           theAna,
2868                                                  const TopoDS_Shape&                 theShape,
2869                                                  Handle(TopTools_HSequenceOfShape)&  sl,
2870                                                  Handle(TColStd_HArray1OfInteger)&   NbProblems,
2871                                                  const TopAbs_ShapeEnum              Subtype,
2872                                                  TopTools_DataMapOfShapeListOfShape& theMap)
2873 {
2874   BRepCheck_ListIteratorOfListOfStatus itl;
2875   TopExp_Explorer exp;
2876   for (exp.Init(theShape, Subtype); exp.More(); exp.Next()) {
2877     const TopoDS_Shape& sub = exp.Current();
2878
2879     const Handle(BRepCheck_Result)& res = theAna.Result(sub);
2880     for (res->InitContextIterator();
2881          res->MoreShapeInContext();
2882          res->NextShapeInContext()) {
2883       if (res->ContextualShape().IsSame(theShape) && !Contains(theMap(sub), theShape)) {
2884         theMap(sub).Append(theShape);
2885         itl.Initialize(res->StatusOnShape());
2886
2887         if (itl.Value() != BRepCheck_NoError) {
2888           Standard_Integer ii = 0;
2889
2890           for (ii = 1; ii <= sl->Length(); ii++)
2891             if (sl->Value(ii).IsSame(sub)) break;
2892
2893           if (ii > sl->Length()) {
2894             sl->Append(sub);
2895             BRepCheck_Status stat = itl.Value();
2896             NbProblems->SetValue((Standard_Integer)stat,
2897                                  NbProblems->Value((Standard_Integer)stat) + 1);
2898           }
2899           for (ii = 1; ii <= sl->Length(); ii++)
2900             if (sl->Value(ii).IsSame(theShape)) break;
2901           if (ii > sl->Length()) {
2902             sl->Append(theShape);
2903             BRepCheck_Status stat = itl.Value();
2904             NbProblems->SetValue((Standard_Integer)stat,
2905                                  NbProblems->Value((Standard_Integer)stat) + 1);
2906           }
2907         }
2908         break;
2909       }
2910     }
2911   }
2912 }