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