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