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