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