Salome HOME
NPAL17269: Performance pb. when creating a group with GUI.
[modules/geom.git] / src / GEOMImpl / GEOMImpl_IMeasureOperations.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 //
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License.
8 //
9 // This library is distributed in the hope that it will be useful
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20
21 #include <Standard_Stream.hxx>
22
23 #include <GEOMImpl_IMeasureOperations.hxx>
24
25 #include <GEOMImpl_Types.hxx>
26 #include <GEOMImpl_MeasureDriver.hxx>
27 #include <GEOMImpl_IMeasure.hxx>
28
29 #include <GEOMAlgo_ShapeInfo.hxx>
30 #include <GEOMAlgo_ShapeInfoFiller.hxx>
31
32 #include <GEOM_Function.hxx>
33 #include <GEOM_PythonDump.hxx>
34
35 #include <utilities.h>
36 #include <OpUtil.hxx>
37 #include <Utils_ExceptHandlers.hxx>
38
39 // OCCT Includes
40 #include <TFunction_DriverTable.hxx>
41 #include <TFunction_Driver.hxx>
42 #include <TFunction_Logbook.hxx>
43 #include <TDF_Tool.hxx>
44
45 #include <BRep_Tool.hxx>
46 #include <BRepAdaptor_Surface.hxx>
47 #include <BRepBndLib.hxx>
48 #include <BRepCheck.hxx>
49 #include <BRepCheck_Result.hxx>
50 #include <BRepCheck_ListIteratorOfListOfStatus.hxx>
51 #include <BRepExtrema_DistShapeShape.hxx>
52 #include <BRepGProp.hxx>
53 #include <BRepTools.hxx>
54
55 #include <Bnd_Box.hxx>
56
57 #include <GProp_GProps.hxx>
58 #include <GProp_PrincipalProps.hxx>
59
60 #include <TopAbs.hxx>
61 #include <TopoDS.hxx>
62 #include <TopoDS_Edge.hxx>
63 #include <TopoDS_Face.hxx>
64 #include <TopoDS_Shape.hxx>
65 #include <TopoDS_Vertex.hxx>
66 #include <TopoDS_Iterator.hxx>
67 #include <TopExp_Explorer.hxx>
68 #include <TopTools_MapOfShape.hxx>
69 #include <TopTools_ListOfShape.hxx>
70 #include <TopTools_ListIteratorOfListOfShape.hxx>
71
72 #include <GeomAbs_SurfaceType.hxx>
73 #include <Geom_Surface.hxx>
74 #include <Geom_Plane.hxx>
75 #include <Geom_SphericalSurface.hxx>
76 #include <Geom_CylindricalSurface.hxx>
77 #include <Geom_ToroidalSurface.hxx>
78 #include <Geom_ConicalSurface.hxx>
79 #include <Geom_SurfaceOfLinearExtrusion.hxx>
80 #include <Geom_SurfaceOfRevolution.hxx>
81 #include <Geom_BezierSurface.hxx>
82 #include <Geom_BSplineSurface.hxx>
83 #include <Geom_RectangularTrimmedSurface.hxx>
84 #include <Geom_OffsetSurface.hxx>
85 #include <Geom_Line.hxx>
86
87 #include <gp_Pln.hxx>
88 #include <gp_Lin.hxx>
89
90 #include <Standard_Failure.hxx>
91 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
92
93 //=============================================================================
94 /*!
95  *  Constructor
96  */
97 //=============================================================================
98 GEOMImpl_IMeasureOperations::GEOMImpl_IMeasureOperations (GEOM_Engine* theEngine, int theDocID)
99 : GEOM_IOperations(theEngine, theDocID)
100 {
101   MESSAGE("GEOMImpl_IMeasureOperations::GEOMImpl_IMeasureOperations");
102 }
103
104 //=============================================================================
105 /*!
106  *  Destructor
107  */
108 //=============================================================================
109 GEOMImpl_IMeasureOperations::~GEOMImpl_IMeasureOperations()
110 {
111   MESSAGE("GEOMImpl_IMeasureOperations::~GEOMImpl_IMeasureOperations");
112 }
113
114 //=============================================================================
115 /*! Get kind and parameters of the given shape.
116  */
117 //=============================================================================
118 GEOMImpl_IMeasureOperations::ShapeKind GEOMImpl_IMeasureOperations::KindOfShape
119                              (Handle(GEOM_Object) theShape,
120                               Handle(TColStd_HSequenceOfInteger)& theIntegers,
121                               Handle(TColStd_HSequenceOfReal)&    theDoubles)
122 {
123   SetErrorCode(KO);
124   ShapeKind aKind = SK_NO_SHAPE;
125
126   if (theIntegers.IsNull()) theIntegers = new TColStd_HSequenceOfInteger;
127   else                      theIntegers->Clear();
128
129   if (theDoubles.IsNull()) theDoubles = new TColStd_HSequenceOfReal;
130   else                     theDoubles->Clear();
131
132   if (theShape.IsNull())
133     return aKind;
134
135   Handle(GEOM_Function) aRefShape = theShape->GetLastFunction();
136   if (aRefShape.IsNull()) return aKind;
137
138   TopoDS_Shape aShape = aRefShape->GetValue();
139   if (aShape.IsNull()) return aKind;
140
141   // Call algorithm
142   GEOMAlgo_ShapeInfoFiller aSF;
143   aSF.SetShape(aShape);
144   aSF.Perform();
145   Standard_Integer iErr = aSF.ErrorStatus();
146   if (iErr) {
147     SetErrorCode("Error in GEOMAlgo_ShapeInfoFiller");
148     return SK_NO_SHAPE;
149   }
150   const GEOMAlgo_ShapeInfo& anInfo = aSF.Info();
151
152   // Interprete results
153   TopAbs_ShapeEnum aType = anInfo.Type();
154   switch (aType)
155   {
156   case TopAbs_COMPOUND:
157   case TopAbs_COMPSOLID:
158     {
159       // (+) geompy.kind.COMPOUND     nb_solids nb_faces nb_edges nb_vertices
160       // (+) geompy.kind.COMPSOLID    nb_solids nb_faces nb_edges nb_vertices
161       // ??? "nb_faces" - all faces or only 'standalone' faces?
162       if (aType == TopAbs_COMPOUND)
163         aKind = SK_COMPOUND;
164       else
165         aKind = SK_COMPSOLID;
166
167       //theIntegers->Append(anInfo.NbSubShapes(TopAbs_COMPOUND));
168       //theIntegers->Append(anInfo.NbSubShapes(TopAbs_COMPSOLID));
169       theIntegers->Append(anInfo.NbSubShapes(TopAbs_SOLID));
170       theIntegers->Append(anInfo.NbSubShapes(TopAbs_FACE));
171       theIntegers->Append(anInfo.NbSubShapes(TopAbs_EDGE));
172       theIntegers->Append(anInfo.NbSubShapes(TopAbs_VERTEX));
173     }
174     break;
175
176   case TopAbs_SHELL:
177     {
178       // (+) geompy.kind.SHELL  geompy.info.closed   nb_faces nb_edges nb_vertices
179       // (+) geompy.kind.SHELL  geompy.info.unclosed nb_faces nb_edges nb_vertices
180       aKind = SK_SHELL;
181
182       theIntegers->Append((int)anInfo.KindOfClosed());
183
184       theIntegers->Append(anInfo.NbSubShapes(TopAbs_FACE));
185       theIntegers->Append(anInfo.NbSubShapes(TopAbs_EDGE));
186       theIntegers->Append(anInfo.NbSubShapes(TopAbs_VERTEX));
187     }
188     break;
189
190   case TopAbs_WIRE:
191     {
192       // (+) geompy.kind.WIRE  geompy.info.closed   nb_edges nb_vertices
193       // (+) geompy.kind.WIRE  geompy.info.unclosed nb_edges nb_vertices
194       aKind = SK_WIRE;
195
196       theIntegers->Append((int)anInfo.KindOfClosed());
197
198       theIntegers->Append(anInfo.NbSubShapes(TopAbs_EDGE));
199       theIntegers->Append(anInfo.NbSubShapes(TopAbs_VERTEX));
200     }
201     break;
202
203   case TopAbs_SOLID:
204     {
205       aKind = SK_SOLID;
206
207       GEOMAlgo_KindOfName aKN = anInfo.KindOfName();
208       switch (aKN)
209       {
210       case GEOMAlgo_KN_SPHERE:
211         // (+) geompy.kind.SPHERE  xc yc zc  R
212         {
213           aKind = SK_SPHERE;
214
215           gp_Pnt aC = anInfo.Location();
216           theDoubles->Append(aC.X());
217           theDoubles->Append(aC.Y());
218           theDoubles->Append(aC.Z());
219
220           theDoubles->Append(anInfo.Radius1());
221         }
222         break;
223       case GEOMAlgo_KN_CYLINDER:
224         // (+) geompy.kind.CYLINDER  xb yb zb  dx dy dz  R  H
225         {
226           aKind = SK_CYLINDER;
227
228           gp_Pnt aC = anInfo.Location();
229           theDoubles->Append(aC.X());
230           theDoubles->Append(aC.Y());
231           theDoubles->Append(aC.Z());
232
233           gp_Ax3 anAx3 = anInfo.Position();
234           gp_Dir aD = anAx3.Direction();
235           theDoubles->Append(aD.X());
236           theDoubles->Append(aD.Y());
237           theDoubles->Append(aD.Z());
238
239           theDoubles->Append(anInfo.Radius1());
240           theDoubles->Append(anInfo.Height());
241         }
242         break;
243       case GEOMAlgo_KN_BOX:
244         // (+) geompy.kind.BOX  xc yc zc  ax ay az
245         {
246           aKind = SK_BOX;
247
248           gp_Pnt aC = anInfo.Location();
249           theDoubles->Append(aC.X());
250           theDoubles->Append(aC.Y());
251           theDoubles->Append(aC.Z());
252
253           gp_Ax3 anAx3 = anInfo.Position();
254           gp_Dir aD = anAx3.Direction();
255           gp_Dir aX = anAx3.XDirection();
256
257           // ax ay az
258           if (aD.IsParallel(gp::DZ(), Precision::Angular()) &&
259               aX.IsParallel(gp::DX(), Precision::Angular())) {
260             theDoubles->Append(anInfo.Length()); // ax'
261             theDoubles->Append(anInfo.Width());  // ay'
262             theDoubles->Append(anInfo.Height()); // az'
263           }
264           else if (aD.IsParallel(gp::DZ(), Precision::Angular()) &&
265                    aX.IsParallel(gp::DY(), Precision::Angular())) {
266             theDoubles->Append(anInfo.Width());  // ay'
267             theDoubles->Append(anInfo.Length()); // ax'
268             theDoubles->Append(anInfo.Height()); // az'
269           }
270           else if (aD.IsParallel(gp::DX(), Precision::Angular()) &&
271                    aX.IsParallel(gp::DZ(), Precision::Angular())) {
272             theDoubles->Append(anInfo.Height()); // az'
273             theDoubles->Append(anInfo.Width());  // ay'
274             theDoubles->Append(anInfo.Length()); // ax'
275           }
276           else if (aD.IsParallel(gp::DX(), Precision::Angular()) &&
277                    aX.IsParallel(gp::DY(), Precision::Angular())) {
278             theDoubles->Append(anInfo.Height()); // az'
279             theDoubles->Append(anInfo.Length()); // ax'
280             theDoubles->Append(anInfo.Width());  // ay'
281           }
282           else if (aD.IsParallel(gp::DY(), Precision::Angular()) &&
283                    aX.IsParallel(gp::DZ(), Precision::Angular())) {
284             theDoubles->Append(anInfo.Width());  // ay'
285             theDoubles->Append(anInfo.Height()); // az'
286             theDoubles->Append(anInfo.Length()); // ax'
287           }
288           else if (aD.IsParallel(gp::DY(), Precision::Angular()) &&
289                    aX.IsParallel(gp::DX(), Precision::Angular())) {
290             theDoubles->Append(anInfo.Length()); // ax'
291             theDoubles->Append(anInfo.Height()); // az'
292             theDoubles->Append(anInfo.Width());  // ay'
293           }
294           else {
295             // (+) geompy.kind.ROTATED_BOX  xo yo zo  zx zy zz  xx xy xz  ax ay az
296             aKind = SK_ROTATED_BOX;
297
298             // Direction and XDirection
299             theDoubles->Append(aD.X());
300             theDoubles->Append(aD.Y());
301             theDoubles->Append(aD.Z());
302
303             theDoubles->Append(aX.X());
304             theDoubles->Append(aX.Y());
305             theDoubles->Append(aX.Z());
306
307             // ax ay az
308             theDoubles->Append(anInfo.Length());
309             theDoubles->Append(anInfo.Width());
310             theDoubles->Append(anInfo.Height());
311           }
312         }
313         break;
314       case GEOMAlgo_KN_TORUS:
315         // (+) geompy.kind.TORUS  xc yc zc  dx dy dz  R_1 R_2
316         {
317           aKind = SK_TORUS;
318
319           gp_Pnt aO = anInfo.Location();
320           theDoubles->Append(aO.X());
321           theDoubles->Append(aO.Y());
322           theDoubles->Append(aO.Z());
323
324           gp_Ax3 anAx3 = anInfo.Position();
325           gp_Dir aD = anAx3.Direction();
326           theDoubles->Append(aD.X());
327           theDoubles->Append(aD.Y());
328           theDoubles->Append(aD.Z());
329
330           theDoubles->Append(anInfo.Radius1());
331           theDoubles->Append(anInfo.Radius2());
332         }
333         break;
334       case GEOMAlgo_KN_CONE:
335         // (+) geompy.kind.CONE  xb yb zb  dx dy dz  R_1 R_2  H
336         {
337           aKind = SK_CONE;
338
339           gp_Pnt aO = anInfo.Location();
340           theDoubles->Append(aO.X());
341           theDoubles->Append(aO.Y());
342           theDoubles->Append(aO.Z());
343
344           gp_Ax3 anAx3 = anInfo.Position();
345           gp_Dir aD = anAx3.Direction();
346           theDoubles->Append(aD.X());
347           theDoubles->Append(aD.Y());
348           theDoubles->Append(aD.Z());
349
350           theDoubles->Append(anInfo.Radius1());
351           theDoubles->Append(anInfo.Radius2());
352           theDoubles->Append(anInfo.Height());
353         }
354         break;
355       case GEOMAlgo_KN_POLYHEDRON:
356         // (+) geompy.kind.POLYHEDRON  nb_faces nb_edges nb_vertices
357         {
358           aKind = SK_POLYHEDRON;
359
360           theIntegers->Append(anInfo.NbSubShapes(TopAbs_FACE));
361           theIntegers->Append(anInfo.NbSubShapes(TopAbs_EDGE));
362           theIntegers->Append(anInfo.NbSubShapes(TopAbs_VERTEX));
363         }
364         break;
365       default:
366         // (+) geompy.kind.SOLID  nb_faces nb_edges nb_vertices
367         {
368           theIntegers->Append(anInfo.NbSubShapes(TopAbs_FACE));
369           theIntegers->Append(anInfo.NbSubShapes(TopAbs_EDGE));
370           theIntegers->Append(anInfo.NbSubShapes(TopAbs_VERTEX));
371         }
372       }
373     }
374     break;
375
376   case TopAbs_FACE:
377     {
378       aKind = SK_FACE;
379
380       GEOMAlgo_KindOfName aKN = anInfo.KindOfName();
381       switch (aKN) {
382       case GEOMAlgo_KN_SPHERE:
383         // (+) geompy.kind.SPHERE2D  xc yc zc  R
384         {
385           aKind = SK_SPHERE2D;
386
387           gp_Pnt aC = anInfo.Location();
388           theDoubles->Append(aC.X());
389           theDoubles->Append(aC.Y());
390           theDoubles->Append(aC.Z());
391
392           theDoubles->Append(anInfo.Radius1());
393         }
394         break;
395       case GEOMAlgo_KN_CYLINDER:
396         // (+) geompy.kind.CYLINDER2D  xb yb zb  dx dy dz  R  H
397         {
398           aKind = SK_CYLINDER2D;
399
400           gp_Pnt aO = anInfo.Location();
401           theDoubles->Append(aO.X());
402           theDoubles->Append(aO.Y());
403           theDoubles->Append(aO.Z());
404
405           gp_Ax3 anAx3 = anInfo.Position();
406           gp_Dir aD = anAx3.Direction();
407           theDoubles->Append(aD.X());
408           theDoubles->Append(aD.Y());
409           theDoubles->Append(aD.Z());
410
411           theDoubles->Append(anInfo.Radius1());
412           theDoubles->Append(anInfo.Height());
413         }
414         break;
415       case GEOMAlgo_KN_TORUS:
416         // (+) geompy.kind.TORUS2D  xc yc zc  dx dy dz  R_1 R_2
417         {
418           aKind = SK_TORUS2D;
419
420           gp_Pnt aO = anInfo.Location();
421           theDoubles->Append(aO.X());
422           theDoubles->Append(aO.Y());
423           theDoubles->Append(aO.Z());
424
425           gp_Ax3 anAx3 = anInfo.Position();
426           gp_Dir aD = anAx3.Direction();
427           theDoubles->Append(aD.X());
428           theDoubles->Append(aD.Y());
429           theDoubles->Append(aD.Z());
430
431           theDoubles->Append(anInfo.Radius1());
432           theDoubles->Append(anInfo.Radius2());
433         }
434         break;
435       case GEOMAlgo_KN_CONE:
436         // (+) geompy.kind.CONE2D  xc yc zc  dx dy dz  R_1 R_2  H
437         {
438           aKind = SK_CONE2D;
439
440           gp_Pnt aO = anInfo.Location();
441           theDoubles->Append(aO.X());
442           theDoubles->Append(aO.Y());
443           theDoubles->Append(aO.Z());
444
445           gp_Ax3 anAx3 = anInfo.Position();
446           gp_Dir aD = anAx3.Direction();
447           theDoubles->Append(aD.X());
448           theDoubles->Append(aD.Y());
449           theDoubles->Append(aD.Z());
450
451           theDoubles->Append(anInfo.Radius1());
452           theDoubles->Append(anInfo.Radius2());
453           theDoubles->Append(anInfo.Height());
454         }
455         break;
456       case GEOMAlgo_KN_DISKCIRCLE:
457         // (+) geompy.kind.DISK_CIRCLE  xc yc zc  dx dy dz  R
458         {
459           aKind = SK_DISK_CIRCLE;
460
461           gp_Pnt aC = anInfo.Location();
462           theDoubles->Append(aC.X());
463           theDoubles->Append(aC.Y());
464           theDoubles->Append(aC.Z());
465
466           gp_Ax3 anAx3 = anInfo.Position();
467           gp_Dir aD = anAx3.Direction();
468           theDoubles->Append(aD.X());
469           theDoubles->Append(aD.Y());
470           theDoubles->Append(aD.Z());
471
472           theDoubles->Append(anInfo.Radius1());
473         }
474         break;
475       case GEOMAlgo_KN_DISKELLIPSE:
476         // (+) geompy.kind.DISK_ELLIPSE  xc yc zc  dx dy dz  R_1 R_2
477         {
478           aKind = SK_DISK_ELLIPSE;
479
480           gp_Pnt aC = anInfo.Location();
481           theDoubles->Append(aC.X());
482           theDoubles->Append(aC.Y());
483           theDoubles->Append(aC.Z());
484
485           gp_Ax3 anAx3 = anInfo.Position();
486           gp_Dir aD = anAx3.Direction();
487           theDoubles->Append(aD.X());
488           theDoubles->Append(aD.Y());
489           theDoubles->Append(aD.Z());
490
491           theDoubles->Append(anInfo.Radius1());
492           theDoubles->Append(anInfo.Radius2());
493         }
494         break;
495       case GEOMAlgo_KN_RECTANGLE:
496       case GEOMAlgo_KN_TRIANGLE:
497       case GEOMAlgo_KN_QUADRANGLE:
498       case GEOMAlgo_KN_POLYGON:
499         // (+) geompy.kind.POLYGON  xo yo zo  dx dy dz  nb_edges nb_vertices
500         {
501           aKind = SK_POLYGON;
502
503           gp_Pnt aO = anInfo.Location();
504           theDoubles->Append(aO.X());
505           theDoubles->Append(aO.Y());
506           theDoubles->Append(aO.Z());
507
508           gp_Ax3 anAx3 = anInfo.Position();
509           gp_Dir aD = anAx3.Direction();
510           theDoubles->Append(aD.X());
511           theDoubles->Append(aD.Y());
512           theDoubles->Append(aD.Z());
513
514           theIntegers->Append(anInfo.NbSubShapes(TopAbs_EDGE));
515           theIntegers->Append(anInfo.NbSubShapes(TopAbs_VERTEX));
516         }
517         break;
518       case GEOMAlgo_KN_PLANE: // infinite
519         // (+) geompy.kind.PLANE  xo yo zo  dx dy dz
520         {
521           aKind = SK_PLANE;
522
523           gp_Pnt aC = anInfo.Location();
524           theDoubles->Append(aC.X());
525           theDoubles->Append(aC.Y());
526           theDoubles->Append(aC.Z());
527
528           gp_Ax3 anAx3 = anInfo.Position();
529           gp_Dir aD = anAx3.Direction();
530           theDoubles->Append(aD.X());
531           theDoubles->Append(aD.Y());
532           theDoubles->Append(aD.Z());
533         }
534         break;
535       default:
536         if (anInfo.KindOfShape() == GEOMAlgo_KS_PLANE) {
537           // (+) geompy.kind.PLANAR  xo yo zo  dx dy dz  nb_edges nb_vertices
538
539           aKind = SK_PLANAR;
540
541           gp_Pnt aC = anInfo.Location();
542           theDoubles->Append(aC.X());
543           theDoubles->Append(aC.Y());
544           theDoubles->Append(aC.Z());
545
546           gp_Ax3 anAx3 = anInfo.Position();
547           gp_Dir aD = anAx3.Direction();
548           theDoubles->Append(aD.X());
549           theDoubles->Append(aD.Y());
550           theDoubles->Append(aD.Z());
551
552           theIntegers->Append(anInfo.NbSubShapes(TopAbs_EDGE));
553           theIntegers->Append(anInfo.NbSubShapes(TopAbs_VERTEX));
554         }
555         else {
556           // ??? geompy.kind.FACE  nb_edges nb_vertices _surface_type_id_
557           // (+) geompy.kind.FACE  nb_edges nb_vertices
558
559           theIntegers->Append(anInfo.NbSubShapes(TopAbs_EDGE));
560           theIntegers->Append(anInfo.NbSubShapes(TopAbs_VERTEX));
561         }
562       }
563     }
564     break;
565
566   case TopAbs_EDGE:
567     {
568       aKind = SK_EDGE;
569
570       GEOMAlgo_KindOfName aKN = anInfo.KindOfName();
571       switch (aKN) {
572       case GEOMAlgo_KN_CIRCLE:
573         {
574           // (+) geompy.kind.CIRCLE  xc yc zc  dx dy dz  R
575           aKind = SK_CIRCLE;
576
577           gp_Pnt aC = anInfo.Location();
578           theDoubles->Append(aC.X());
579           theDoubles->Append(aC.Y());
580           theDoubles->Append(aC.Z());
581
582           gp_Ax3 anAx3 = anInfo.Position();
583           gp_Dir aD = anAx3.Direction();
584           theDoubles->Append(aD.X());
585           theDoubles->Append(aD.Y());
586           theDoubles->Append(aD.Z());
587
588           theDoubles->Append(anInfo.Radius1());
589         }
590         break;
591       case GEOMAlgo_KN_ARCCIRCLE:
592         {
593           // (+) geompy.kind.ARC_CIRCLE  xc yc zc  dx dy dz  R  x1 y1 z1  x2 y2 z2
594           aKind = SK_ARC_CIRCLE;
595
596           gp_Pnt aC = anInfo.Location();
597           theDoubles->Append(aC.X());
598           theDoubles->Append(aC.Y());
599           theDoubles->Append(aC.Z());
600
601           gp_Ax3 anAx3 = anInfo.Position();
602           gp_Dir aD = anAx3.Direction();
603           theDoubles->Append(aD.X());
604           theDoubles->Append(aD.Y());
605           theDoubles->Append(aD.Z());
606
607           theDoubles->Append(anInfo.Radius1());
608
609           gp_Pnt aP1 = anInfo.Pnt1();
610           theDoubles->Append(aP1.X());
611           theDoubles->Append(aP1.Y());
612           theDoubles->Append(aP1.Z());
613
614           gp_Pnt aP2 = anInfo.Pnt2();
615           theDoubles->Append(aP2.X());
616           theDoubles->Append(aP2.Y());
617           theDoubles->Append(aP2.Z());
618         }
619         break;
620       case GEOMAlgo_KN_ELLIPSE:
621         {
622           // (+) geompy.kind.ELLIPSE  xc yc zc  dx dy dz  R_1 R_2
623           aKind = SK_ELLIPSE;
624
625           gp_Pnt aC = anInfo.Location();
626           theDoubles->Append(aC.X());
627           theDoubles->Append(aC.Y());
628           theDoubles->Append(aC.Z());
629
630           gp_Ax3 anAx3 = anInfo.Position();
631           gp_Dir aD = anAx3.Direction();
632           theDoubles->Append(aD.X());
633           theDoubles->Append(aD.Y());
634           theDoubles->Append(aD.Z());
635
636           theDoubles->Append(anInfo.Radius1());
637           theDoubles->Append(anInfo.Radius2());
638         }
639         break;
640       case GEOMAlgo_KN_ARCELLIPSE:
641         {
642           // (+) geompy.kind.ARC_ELLIPSE  xc yc zc  dx dy dz  R_1 R_2  x1 y1 z1  x2 y2 z2
643           aKind = SK_ARC_ELLIPSE;
644
645           gp_Pnt aC = anInfo.Location();
646           theDoubles->Append(aC.X());
647           theDoubles->Append(aC.Y());
648           theDoubles->Append(aC.Z());
649
650           gp_Ax3 anAx3 = anInfo.Position();
651           gp_Dir aD = anAx3.Direction();
652           theDoubles->Append(aD.X());
653           theDoubles->Append(aD.Y());
654           theDoubles->Append(aD.Z());
655
656           theDoubles->Append(anInfo.Radius1());
657           theDoubles->Append(anInfo.Radius2());
658
659           gp_Pnt aP1 = anInfo.Pnt1();
660           theDoubles->Append(aP1.X());
661           theDoubles->Append(aP1.Y());
662           theDoubles->Append(aP1.Z());
663
664           gp_Pnt aP2 = anInfo.Pnt2();
665           theDoubles->Append(aP2.X());
666           theDoubles->Append(aP2.Y());
667           theDoubles->Append(aP2.Z());
668         }
669         break;
670       case GEOMAlgo_KN_LINE:
671         {
672           // ??? geompy.kind.LINE  x1 y1 z1  x2 y2 z2
673           // (+) geompy.kind.LINE  x1 y1 z1  dx dy dz
674           aKind = SK_LINE;
675
676           gp_Pnt aO = anInfo.Location();
677           theDoubles->Append(aO.X());
678           theDoubles->Append(aO.Y());
679           theDoubles->Append(aO.Z());
680
681           gp_Dir aD = anInfo.Direction();
682           theDoubles->Append(aD.X());
683           theDoubles->Append(aD.Y());
684           theDoubles->Append(aD.Z());
685         }
686         break;
687       case GEOMAlgo_KN_SEGMENT:
688         {
689           // (+) geompy.kind.SEGMENT  x1 y1 z1  x2 y2 z2
690           aKind = SK_SEGMENT;
691
692           gp_Pnt aP1 = anInfo.Pnt1();
693           theDoubles->Append(aP1.X());
694           theDoubles->Append(aP1.Y());
695           theDoubles->Append(aP1.Z());
696
697           gp_Pnt aP2 = anInfo.Pnt2();
698           theDoubles->Append(aP2.X());
699           theDoubles->Append(aP2.Y());
700           theDoubles->Append(aP2.Z());
701         }
702         break;
703       default:
704         // ??? geompy.kind.EDGE  nb_vertices _curve_type_id_
705         // (+) geompy.kind.EDGE  nb_vertices
706         theIntegers->Append(anInfo.NbSubShapes(TopAbs_VERTEX));
707       }
708     }
709     break;
710
711   case TopAbs_VERTEX:
712     {
713       // (+) geompy.kind.VERTEX  x y z
714       aKind = SK_VERTEX;
715
716       gp_Pnt aP = anInfo.Location();
717       theDoubles->Append(aP.X());
718       theDoubles->Append(aP.Y());
719       theDoubles->Append(aP.Z());
720     }
721     break;
722   }
723
724   SetErrorCode(OK);
725   return aKind;
726 }
727
728 //=============================================================================
729 /*! Get LCS, corresponding to the given shape.
730  *  Origin of the LCS is situated at the shape's center of mass.
731  *  Axes of the LCS are obtained from shape's location or,
732  *  if the shape is a planar face, from position of its plane.
733  */
734 //=============================================================================
735 gp_Ax3 GEOMImpl_IMeasureOperations::GetPosition (const TopoDS_Shape& theShape)
736 {
737   gp_Ax3 aResult;
738
739   if (theShape.IsNull())
740     return aResult;
741
742   // Axes
743   aResult.Transform(theShape.Location().Transformation());
744   if (theShape.ShapeType() == TopAbs_FACE) {
745     Handle(Geom_Surface) aGS = BRep_Tool::Surface(TopoDS::Face(theShape));
746     if (!aGS.IsNull() && aGS->IsKind(STANDARD_TYPE(Geom_Plane))) {
747       Handle(Geom_Plane) aGPlane = Handle(Geom_Plane)::DownCast(aGS);
748       gp_Pln aPln = aGPlane->Pln();
749       aResult = aPln.Position();
750     }
751   }
752
753   // Origin
754   gp_Pnt aPnt;
755   if (theShape.ShapeType() == TopAbs_VERTEX) {
756     aPnt = BRep_Tool::Pnt(TopoDS::Vertex(theShape));
757   }
758   else {
759     GProp_GProps aSystem;
760     if (theShape.ShapeType() == TopAbs_EDGE || theShape.ShapeType() == TopAbs_WIRE)
761       BRepGProp::LinearProperties(theShape, aSystem);
762     else if (theShape.ShapeType() == TopAbs_FACE || theShape.ShapeType() == TopAbs_SHELL)
763       BRepGProp::SurfaceProperties(theShape, aSystem);
764     else
765       BRepGProp::VolumeProperties(theShape, aSystem);
766
767     aPnt = aSystem.CentreOfMass();
768   }
769
770   aResult.SetLocation(aPnt);
771
772   return aResult;
773 }
774
775 //=============================================================================
776 /*!
777  *  GetPosition
778  */
779 //=============================================================================
780 void GEOMImpl_IMeasureOperations::GetPosition
781                    (Handle(GEOM_Object) theShape,
782                     Standard_Real& Ox, Standard_Real& Oy, Standard_Real& Oz,
783                     Standard_Real& Zx, Standard_Real& Zy, Standard_Real& Zz,
784                     Standard_Real& Xx, Standard_Real& Xy, Standard_Real& Xz)
785 {
786   SetErrorCode(KO);
787
788   //Set default values: global CS
789   Ox = Oy = Oz = Zx = Zy = Xy = Xz = 0.;
790   Zz = Xx = 1.;
791
792   if (theShape.IsNull()) return;
793
794   Handle(GEOM_Function) aRefShape = theShape->GetLastFunction();
795   if (aRefShape.IsNull()) return;
796
797   TopoDS_Shape aShape = aRefShape->GetValue();
798   if (aShape.IsNull()) {
799     SetErrorCode("The Objects has NULL Shape");
800     return;
801   }
802
803   try {
804 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
805     OCC_CATCH_SIGNALS;
806 #endif
807
808     gp_Ax3 anAx3 = GetPosition(aShape);
809
810     gp_Pnt anOri = anAx3.Location();
811     gp_Dir aDirZ = anAx3.Direction();
812     gp_Dir aDirX = anAx3.XDirection();
813
814     // Output values
815     anOri.Coord(Ox, Oy, Oz);
816     aDirZ.Coord(Zx, Zy, Zz);
817     aDirX.Coord(Xx, Xy, Xz);
818   }
819   catch (Standard_Failure) {
820     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
821     SetErrorCode(aFail->GetMessageString());
822     return;
823   }
824
825   SetErrorCode(OK);
826 }
827
828 //=============================================================================
829 /*!
830  *  GetCentreOfMass
831  */
832 //=============================================================================
833 Handle(GEOM_Object) GEOMImpl_IMeasureOperations::GetCentreOfMass
834                                                 (Handle(GEOM_Object) theShape)
835 {
836   SetErrorCode(KO);
837
838   if (theShape.IsNull()) return NULL;
839
840   //Add a new CentreOfMass object
841   Handle(GEOM_Object) aCDG = GetEngine()->AddObject(GetDocID(), GEOM_CDG);
842
843   //Add a new CentreOfMass function
844   Handle(GEOM_Function) aFunction =
845     aCDG->AddFunction(GEOMImpl_MeasureDriver::GetID(), CDG_MEASURE);
846   if (aFunction.IsNull()) return NULL;
847
848   //Check if the function is set correctly
849   if (aFunction->GetDriverGUID() != GEOMImpl_MeasureDriver::GetID()) return NULL;
850
851   GEOMImpl_IMeasure aCI (aFunction);
852
853   Handle(GEOM_Function) aRefShape = theShape->GetLastFunction();
854   if (aRefShape.IsNull()) return NULL;
855
856   aCI.SetBase(aRefShape);
857
858   //Compute the CentreOfMass value
859   try {
860 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
861     OCC_CATCH_SIGNALS;
862 #endif
863     if (!GetSolver()->ComputeFunction(aFunction)) {
864       SetErrorCode("Measure driver failed to compute centre of mass");
865       return NULL;
866     }
867   }
868   catch (Standard_Failure) {
869     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
870     SetErrorCode(aFail->GetMessageString());
871     return NULL;
872   }
873
874   //Make a Python command
875   GEOM::TPythonDump(aFunction) << aCDG << " = geompy.MakeCDG(" << theShape << ")";
876
877   SetErrorCode(OK);
878   return aCDG;
879 }
880
881 //=============================================================================
882 /*!
883  *  GetBasicProperties
884  */
885 //=============================================================================
886 void GEOMImpl_IMeasureOperations::GetBasicProperties (Handle(GEOM_Object) theShape,
887                                                       Standard_Real& theLength,
888                                                       Standard_Real& theSurfArea,
889                                                       Standard_Real& theVolume)
890 {
891   SetErrorCode(KO);
892
893   if (theShape.IsNull()) return;
894
895   Handle(GEOM_Function) aRefShape = theShape->GetLastFunction();
896   if (aRefShape.IsNull()) return;
897
898   TopoDS_Shape aShape = aRefShape->GetValue();
899   if (aShape.IsNull()) {
900     SetErrorCode("The Objects has NULL Shape");
901     return;
902   }
903
904   //Compute the parameters
905   GProp_GProps LProps, SProps;
906   try {
907 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
908     OCC_CATCH_SIGNALS;
909 #endif
910     BRepGProp::LinearProperties(aShape, LProps);
911     theLength = LProps.Mass();
912
913     BRepGProp::SurfaceProperties(aShape, SProps);
914     theSurfArea = SProps.Mass();
915
916     theVolume = 0.0;
917     if (aShape.ShapeType() < TopAbs_SHELL) {
918       for (TopExp_Explorer Exp (aShape, TopAbs_SOLID); Exp.More(); Exp.Next()) {
919         GProp_GProps VProps;
920         BRepGProp::VolumeProperties(Exp.Current(), VProps);
921         theVolume += VProps.Mass();
922       }
923     }
924   }
925   catch (Standard_Failure) {
926     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
927     SetErrorCode(aFail->GetMessageString());
928     return;
929   }
930
931   SetErrorCode(OK);
932 }
933
934 //=============================================================================
935 /*!
936  *  GetInertia
937  */
938 //=============================================================================
939 void GEOMImpl_IMeasureOperations::GetInertia
940                    (Handle(GEOM_Object) theShape,
941                     Standard_Real& I11, Standard_Real& I12, Standard_Real& I13,
942                     Standard_Real& I21, Standard_Real& I22, Standard_Real& I23,
943                     Standard_Real& I31, Standard_Real& I32, Standard_Real& I33,
944                     Standard_Real& Ix , Standard_Real& Iy , Standard_Real& Iz)
945 {
946   SetErrorCode(KO);
947
948   if (theShape.IsNull()) return;
949
950   Handle(GEOM_Function) aRefShape = theShape->GetLastFunction();
951   if (aRefShape.IsNull()) return;
952
953   TopoDS_Shape aShape = aRefShape->GetValue();
954   if (aShape.IsNull()) {
955     SetErrorCode("The Objects has NULL Shape");
956     return;
957   }
958
959   //Compute the parameters
960   GProp_GProps System;
961
962   try {
963 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
964     OCC_CATCH_SIGNALS;
965 #endif
966     if (aShape.ShapeType() == TopAbs_VERTEX ||
967         aShape.ShapeType() == TopAbs_EDGE ||
968         aShape.ShapeType() == TopAbs_WIRE) {
969       BRepGProp::LinearProperties(aShape, System);
970     } else if (aShape.ShapeType() == TopAbs_FACE ||
971                aShape.ShapeType() == TopAbs_SHELL) {
972       BRepGProp::SurfaceProperties(aShape, System);
973     } else {
974       BRepGProp::VolumeProperties(aShape, System);
975     }
976     gp_Mat I = System.MatrixOfInertia();
977
978     I11 = I(1,1);
979     I12 = I(1,2);
980     I13 = I(1,3);
981
982     I21 = I(2,1);
983     I22 = I(2,2);
984     I23 = I(2,3);
985
986     I31 = I(3,1);
987     I32 = I(3,2);
988     I33 = I(3,3);
989
990     GProp_PrincipalProps Pr = System.PrincipalProperties();
991     Pr.Moments(Ix,Iy,Iz);
992   }
993   catch (Standard_Failure) {
994     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
995     SetErrorCode(aFail->GetMessageString());
996     return;
997   }
998
999   SetErrorCode(OK);
1000 }
1001
1002 //=============================================================================
1003 /*!
1004  *  GetBoundingBox
1005  */
1006 //=============================================================================
1007 void GEOMImpl_IMeasureOperations::GetBoundingBox
1008                                      (Handle(GEOM_Object) theShape,
1009                                       Standard_Real& Xmin, Standard_Real& Xmax,
1010                                       Standard_Real& Ymin, Standard_Real& Ymax,
1011                                       Standard_Real& Zmin, Standard_Real& Zmax)
1012 {
1013   SetErrorCode(KO);
1014
1015   if (theShape.IsNull()) return;
1016
1017   Handle(GEOM_Function) aRefShape = theShape->GetLastFunction();
1018   if (aRefShape.IsNull()) return;
1019
1020   TopoDS_Shape aShape = aRefShape->GetValue();
1021   if (aShape.IsNull()) {
1022     SetErrorCode("The Objects has NULL Shape");
1023     return;
1024   }
1025
1026   //Compute the parameters
1027   Bnd_Box B;
1028
1029   try {
1030 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
1031     OCC_CATCH_SIGNALS;
1032 #endif
1033     BRepBndLib::Add(aShape, B);
1034     B.Get(Xmin, Ymin, Zmin, Xmax, Ymax, Zmax);
1035   }
1036   catch (Standard_Failure) {
1037     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
1038     SetErrorCode(aFail->GetMessageString());
1039     return;
1040   }
1041
1042   SetErrorCode(OK);
1043 }
1044
1045 //=============================================================================
1046 /*!
1047  *  GetTolerance
1048  */
1049 //=============================================================================
1050 void GEOMImpl_IMeasureOperations::GetTolerance
1051                                (Handle(GEOM_Object) theShape,
1052                                 Standard_Real& FaceMin, Standard_Real& FaceMax,
1053                                 Standard_Real& EdgeMin, Standard_Real& EdgeMax,
1054                                 Standard_Real& VertMin, Standard_Real& VertMax)
1055 {
1056   SetErrorCode(KO);
1057
1058   if (theShape.IsNull()) return;
1059
1060   Handle(GEOM_Function) aRefShape = theShape->GetLastFunction();
1061   if (aRefShape.IsNull()) return;
1062
1063   TopoDS_Shape aShape = aRefShape->GetValue();
1064   if (aShape.IsNull()) {
1065     SetErrorCode("The Objects has NULL Shape");
1066     return;
1067   }
1068
1069   //Compute the parameters
1070   Standard_Real T;
1071   FaceMin = EdgeMin = VertMin = RealLast();
1072   FaceMax = EdgeMax = VertMax = -RealLast();
1073
1074   try {
1075 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
1076     OCC_CATCH_SIGNALS;
1077 #endif
1078     for (TopExp_Explorer ExF (aShape, TopAbs_FACE); ExF.More(); ExF.Next()) {
1079       TopoDS_Face Face = TopoDS::Face(ExF.Current());
1080       T = BRep_Tool::Tolerance(Face);
1081       if (T > FaceMax)
1082         FaceMax = T;
1083       if (T < FaceMin)
1084         FaceMin = T;
1085     }
1086     for (TopExp_Explorer ExE (aShape, TopAbs_EDGE); ExE.More(); ExE.Next()) {
1087       TopoDS_Edge Edge = TopoDS::Edge(ExE.Current());
1088       T = BRep_Tool::Tolerance(Edge);
1089       if (T > EdgeMax)
1090         EdgeMax = T;
1091       if (T < EdgeMin)
1092         EdgeMin = T;
1093     }
1094     for (TopExp_Explorer ExV (aShape, TopAbs_VERTEX); ExV.More(); ExV.Next()) {
1095       TopoDS_Vertex Vertex = TopoDS::Vertex(ExV.Current());
1096       T = BRep_Tool::Tolerance(Vertex);
1097       if (T > VertMax)
1098         VertMax = T;
1099       if (T < VertMin)
1100         VertMin = T;
1101     }
1102   }
1103   catch (Standard_Failure) {
1104     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
1105     SetErrorCode(aFail->GetMessageString());
1106     return;
1107   }
1108
1109   SetErrorCode(OK);
1110 }
1111
1112 //=============================================================================
1113 /*!
1114  *  CheckShape
1115  */
1116 //=============================================================================
1117 bool GEOMImpl_IMeasureOperations::CheckShape (Handle(GEOM_Object)      theShape,
1118                                               const Standard_Boolean   theIsCheckGeom,
1119                                               TCollection_AsciiString& theDump)
1120 {
1121   SetErrorCode(KO);
1122
1123   if (theShape.IsNull()) return false;
1124
1125   Handle(GEOM_Function) aRefShape = theShape->GetLastFunction();
1126   if (aRefShape.IsNull()) return false;
1127
1128   TopoDS_Shape aShape = aRefShape->GetValue();
1129   if (aShape.IsNull()) {
1130     SetErrorCode("The Objects has NULL Shape");
1131     return false;
1132   }
1133
1134   //Compute the parameters
1135   bool isValid = false;
1136   try {
1137 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
1138     OCC_CATCH_SIGNALS;
1139 #endif
1140     BRepCheck_Analyzer ana (aShape, theIsCheckGeom);
1141     if (ana.IsValid()) {
1142       theDump.Clear();
1143       isValid = true;
1144     } else {
1145       StructuralDump(ana, aShape, theDump);
1146     }
1147   }
1148   catch (Standard_Failure) {
1149     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
1150     SetErrorCode(aFail->GetMessageString());
1151     return false;
1152   }
1153
1154   SetErrorCode(OK);
1155   return isValid;
1156 }
1157
1158 //=============================================================================
1159 /*!
1160  *  WhatIs
1161  */
1162 //=============================================================================
1163 TCollection_AsciiString GEOMImpl_IMeasureOperations::WhatIs (Handle(GEOM_Object) theShape)
1164 {
1165   SetErrorCode(KO);
1166
1167   TCollection_AsciiString Astr;
1168
1169   if (theShape.IsNull()) return Astr;
1170
1171   Handle(GEOM_Function) aRefShape = theShape->GetLastFunction();
1172   if (aRefShape.IsNull()) return Astr;
1173
1174   TopoDS_Shape aShape = aRefShape->GetValue();
1175   if (aShape.IsNull()) {
1176     SetErrorCode("The Objects has NULL Shape");
1177     return Astr;
1178   }
1179
1180   //Compute the parameters
1181   if (aShape.ShapeType() == TopAbs_EDGE) {
1182     if (BRep_Tool::Degenerated(TopoDS::Edge(aShape))) {
1183       Astr = Astr + " It is a degenerated edge \n";
1184     }
1185   }
1186
1187   Astr = Astr + " Number of sub-shapes : \n";
1188
1189   try {
1190 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
1191     OCC_CATCH_SIGNALS;
1192 #endif
1193     int iType, nbTypes [TopAbs_SHAPE];
1194     for (iType = 0; iType < TopAbs_SHAPE; ++iType)
1195       nbTypes[iType] = 0;
1196     nbTypes[aShape.ShapeType()]++;
1197
1198     TopTools_MapOfShape aMapOfShape;
1199     aMapOfShape.Add(aShape);
1200     TopTools_ListOfShape aListOfShape;
1201     aListOfShape.Append(aShape);
1202
1203     TopTools_ListIteratorOfListOfShape itL (aListOfShape);
1204     for (; itL.More(); itL.Next()) {
1205       TopoDS_Iterator it (itL.Value());
1206       for (; it.More(); it.Next()) {
1207         TopoDS_Shape s = it.Value();
1208         if (aMapOfShape.Add(s)) {
1209           aListOfShape.Append(s);
1210           nbTypes[s.ShapeType()]++;
1211         }
1212       }
1213     }
1214
1215     Astr = Astr + " VERTEX : " + TCollection_AsciiString(nbTypes[TopAbs_VERTEX]) + "\n";
1216     Astr = Astr + " EDGE : " + TCollection_AsciiString(nbTypes[TopAbs_EDGE]) + "\n";
1217     Astr = Astr + " WIRE : " + TCollection_AsciiString(nbTypes[TopAbs_WIRE]) + "\n";
1218     Astr = Astr + " FACE : " + TCollection_AsciiString(nbTypes[TopAbs_FACE]) + "\n";
1219     Astr = Astr + " SHELL : " + TCollection_AsciiString(nbTypes[TopAbs_SHELL]) + "\n";
1220     Astr = Astr + " SOLID : " + TCollection_AsciiString(nbTypes[TopAbs_SOLID]) + "\n";
1221     Astr = Astr + " COMPSOLID : " + TCollection_AsciiString(nbTypes[TopAbs_COMPSOLID]) + "\n";
1222     Astr = Astr + " COMPOUND : " + TCollection_AsciiString(nbTypes[TopAbs_COMPOUND]) + "\n";
1223     Astr = Astr + " SHAPE : " + TCollection_AsciiString(aMapOfShape.Extent());
1224   }
1225   catch (Standard_Failure) {
1226     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
1227     SetErrorCode(aFail->GetMessageString());
1228     return Astr;
1229   }
1230
1231   SetErrorCode(OK);
1232   return Astr;
1233 }
1234
1235 //=============================================================================
1236 /*!
1237  *  GetMinDistance
1238  */
1239 //=============================================================================
1240 Standard_Real GEOMImpl_IMeasureOperations::GetMinDistance
1241   (Handle(GEOM_Object) theShape1, Handle(GEOM_Object) theShape2,
1242    Standard_Real& X1, Standard_Real& Y1, Standard_Real& Z1,
1243    Standard_Real& X2, Standard_Real& Y2, Standard_Real& Z2)
1244 {
1245   SetErrorCode(KO);
1246   Standard_Real MinDist = 1.e9;
1247
1248   if (theShape1.IsNull() || theShape2.IsNull()) return MinDist;
1249
1250   Handle(GEOM_Function) aRefShape1 = theShape1->GetLastFunction();
1251   Handle(GEOM_Function) aRefShape2 = theShape2->GetLastFunction();
1252   if (aRefShape1.IsNull() || aRefShape2.IsNull()) return MinDist;
1253
1254   TopoDS_Shape aShape1 = aRefShape1->GetValue();
1255   TopoDS_Shape aShape2 = aRefShape2->GetValue();
1256   if (aShape1.IsNull() || aShape2.IsNull()) {
1257     SetErrorCode("One of Objects has NULL Shape");
1258     return MinDist;
1259   }
1260
1261   //Compute the parameters
1262   try {
1263 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
1264     OCC_CATCH_SIGNALS;
1265 #endif
1266     BRepExtrema_DistShapeShape dst (aShape1, aShape2);
1267     if (dst.IsDone()) {
1268       gp_Pnt PMin1, PMin2, P1, P2;
1269
1270       for (int i = 1; i <= dst.NbSolution(); i++) {
1271         P1 = dst.PointOnShape1(i);
1272         P2 = dst.PointOnShape2(i);
1273
1274         Standard_Real Dist = P1.Distance(P2);
1275         if (MinDist > Dist) {
1276           MinDist = Dist;
1277           PMin1 = P1;
1278           PMin2 = P2;
1279         }
1280       }
1281
1282       PMin1.Coord(X1, Y1, Z1);
1283       PMin2.Coord(X2, Y2, Z2);
1284     }
1285   }
1286   catch (Standard_Failure) {
1287     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
1288     SetErrorCode(aFail->GetMessageString());
1289     return MinDist;
1290   }
1291
1292   SetErrorCode(OK);
1293   return MinDist;
1294 }
1295
1296 //=======================================================================
1297 /*!
1298  *  Get coordinates of point
1299  */
1300 //=======================================================================
1301 void GEOMImpl_IMeasureOperations::PointCoordinates (Handle(GEOM_Object) theShape,
1302                         Standard_Real& theX, Standard_Real& theY, Standard_Real& theZ)
1303 {
1304   SetErrorCode(KO);
1305
1306   if (theShape.IsNull())
1307     return;
1308
1309   Handle(GEOM_Function) aRefShape = theShape->GetLastFunction();
1310   if (aRefShape.IsNull())
1311     return;
1312
1313   TopoDS_Shape aShape = aRefShape->GetValue();
1314   if (aShape.IsNull() || aShape.ShapeType() != TopAbs_VERTEX)
1315   {
1316     SetErrorCode( "Shape must be a vertex" );
1317     return;
1318   }
1319
1320   try {
1321 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
1322     OCC_CATCH_SIGNALS;
1323 #endif
1324     gp_Pnt aPnt = BRep_Tool::Pnt( TopoDS::Vertex( aShape ) );
1325     theX = aPnt.X();
1326     theY = aPnt.Y();
1327     theZ = aPnt.Z();
1328
1329     SetErrorCode(OK);
1330   }
1331   catch (Standard_Failure)
1332   {
1333     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
1334     SetErrorCode( aFail->GetMessageString() );
1335   }
1336 }
1337
1338 //=======================================================================
1339 /*!
1340  *  Compute angle (in degrees) between two lines
1341  */
1342 //=======================================================================
1343 Standard_Real GEOMImpl_IMeasureOperations::GetAngle (Handle(GEOM_Object) theLine1,
1344                                                      Handle(GEOM_Object) theLine2)
1345 {
1346   SetErrorCode(KO);
1347
1348   Standard_Real anAngle = -1.0;
1349
1350   if (theLine1.IsNull() || theLine2.IsNull())
1351     return anAngle;
1352
1353   Handle(GEOM_Function) aRefLine1 = theLine1->GetLastFunction();
1354   Handle(GEOM_Function) aRefLine2 = theLine2->GetLastFunction();
1355   if (aRefLine1.IsNull() || aRefLine2.IsNull())
1356     return anAngle;
1357
1358   TopoDS_Shape aLine1 = aRefLine1->GetValue();
1359   TopoDS_Shape aLine2 = aRefLine2->GetValue();
1360   if (aLine1.IsNull() || aLine2.IsNull() ||
1361       aLine1.ShapeType() != TopAbs_EDGE ||
1362       aLine2.ShapeType() != TopAbs_EDGE)
1363   {
1364     SetErrorCode("Two edges must be given");
1365     return anAngle;
1366   }
1367
1368   try {
1369 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
1370     OCC_CATCH_SIGNALS;
1371 #endif
1372     TopoDS_Edge E1 = TopoDS::Edge(aLine1);
1373     TopoDS_Edge E2 = TopoDS::Edge(aLine2);
1374
1375     double fp,lp;
1376     Handle(Geom_Curve) C1 = BRep_Tool::Curve(E1,fp,lp);
1377     Handle(Geom_Curve) C2 = BRep_Tool::Curve(E2,fp,lp);
1378
1379     if (!C1->IsKind(STANDARD_TYPE(Geom_Line)) ||
1380         !C2->IsKind(STANDARD_TYPE(Geom_Line)))
1381     {
1382       SetErrorCode("The edges must be linear");
1383       return anAngle;
1384     }
1385
1386     Handle(Geom_Line) L1 = Handle(Geom_Line)::DownCast(C1);
1387     Handle(Geom_Line) L2 = Handle(Geom_Line)::DownCast(C2);
1388
1389     gp_Lin aLin1 = L1->Lin();
1390     gp_Lin aLin2 = L2->Lin();
1391
1392     anAngle = aLin1.Angle(aLin2);
1393     anAngle /= PI180; // convert radians into degrees
1394
1395     SetErrorCode(OK);
1396   }
1397   catch (Standard_Failure)
1398   {
1399     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
1400     SetErrorCode(aFail->GetMessageString());
1401   }
1402
1403   return anAngle;
1404 }
1405
1406 //=======================================================================
1407 //function : StructuralDump
1408 //purpose  : Structural (data exchange) style of output.
1409 //=======================================================================
1410 void GEOMImpl_IMeasureOperations::StructuralDump (const BRepCheck_Analyzer& theAna,
1411                                                   const TopoDS_Shape&       theShape,
1412                                                   TCollection_AsciiString&  theDump)
1413 {
1414   Standard_Integer i;
1415   theDump.Clear();
1416   theDump += " -- The Shape has problems :\n";
1417   theDump += "  Check                                    Count\n";
1418   theDump += " ------------------------------------------------\n";
1419
1420   Standard_Integer last_stat = (Standard_Integer)BRepCheck_CheckFail;
1421   Handle(TColStd_HArray1OfInteger) NbProblems =
1422     new TColStd_HArray1OfInteger(1, last_stat);
1423   for (i = 1; i <= last_stat; i++)
1424     NbProblems->SetValue(i,0);
1425
1426   Handle(TopTools_HSequenceOfShape) sl;
1427   sl = new TopTools_HSequenceOfShape();
1428   TopTools_DataMapOfShapeListOfShape theMap;
1429   theMap.Clear();
1430   GetProblemShapes(theAna, theShape, sl, NbProblems, theMap);
1431   theMap.Clear();
1432
1433   Standard_Integer count = 0;
1434   count = NbProblems->Value((Standard_Integer)BRepCheck_InvalidPointOnCurve);
1435   if (count > 0) {
1436     theDump += "  Invalid Point on Curve ................... ";
1437     theDump += TCollection_AsciiString(count) + "\n";
1438   }
1439   count = NbProblems->Value((Standard_Integer)BRepCheck_InvalidPointOnCurveOnSurface);
1440   if (count > 0) {
1441     theDump += "  Invalid Point on CurveOnSurface .......... ";
1442     theDump += TCollection_AsciiString(count) + "\n";
1443   }
1444   count = NbProblems->Value((Standard_Integer)BRepCheck_InvalidPointOnSurface);
1445   if (count > 0) {
1446     theDump += "  Invalid Point on Surface ................. ";
1447     theDump += TCollection_AsciiString(count) + "\n";
1448   }
1449   count = NbProblems->Value((Standard_Integer)BRepCheck_No3DCurve);
1450   if (count > 0) {
1451     theDump += "  No 3D Curve .............................. ";
1452     theDump += TCollection_AsciiString(count) + "\n";
1453   }
1454   count = NbProblems->Value((Standard_Integer)BRepCheck_Multiple3DCurve);
1455   if (count > 0) {
1456     theDump += "  Multiple 3D Curve ........................ ";
1457     theDump += TCollection_AsciiString(count) + "\n";
1458   }
1459   count = NbProblems->Value((Standard_Integer)BRepCheck_Invalid3DCurve);
1460   if (count > 0) {
1461     theDump += "  Invalid 3D Curve ......................... ";
1462     theDump += TCollection_AsciiString(count) + "\n";
1463   }
1464   count = NbProblems->Value((Standard_Integer)BRepCheck_NoCurveOnSurface);
1465   if (count > 0) {
1466     theDump += "  No Curve on Surface ...................... ";
1467     theDump += TCollection_AsciiString(count) + "\n";
1468   }
1469   count = NbProblems->Value((Standard_Integer)BRepCheck_InvalidCurveOnSurface);
1470   if (count > 0) {
1471     theDump += "  Invalid Curve on Surface ................. ";
1472     theDump += TCollection_AsciiString(count) + "\n";
1473   }
1474   count = NbProblems->Value((Standard_Integer)BRepCheck_InvalidCurveOnClosedSurface);
1475   if (count > 0) {
1476     theDump += "  Invalid Curve on closed Surface .......... ";
1477     theDump += TCollection_AsciiString(count) + "\n";
1478   }
1479   count = NbProblems->Value((Standard_Integer)BRepCheck_InvalidSameRangeFlag);
1480   if (count > 0) {
1481     theDump += "  Invalid SameRange Flag ................... ";
1482     theDump += TCollection_AsciiString(count) + "\n";
1483   }
1484   count = NbProblems->Value((Standard_Integer)BRepCheck_InvalidSameParameterFlag);
1485   if (count > 0) {
1486     theDump += "  Invalid SameParameter Flag ............... ";
1487     theDump += TCollection_AsciiString(count) + "\n";
1488   }
1489   count = NbProblems->Value((Standard_Integer)BRepCheck_InvalidDegeneratedFlag);
1490   if (count > 0) {
1491     theDump += "  Invalid Degenerated Flag ................. ";
1492     theDump += TCollection_AsciiString(count) + "\n";
1493   }
1494   count = NbProblems->Value((Standard_Integer)BRepCheck_FreeEdge);
1495   if (count > 0) {
1496     theDump += "  Free Edge ................................ ";
1497     theDump += TCollection_AsciiString(count) + "\n";
1498   }
1499   count = NbProblems->Value((Standard_Integer)BRepCheck_InvalidMultiConnexity);
1500   if (count > 0) {
1501     theDump += "  Invalid MultiConnexity ................... ";
1502     theDump += TCollection_AsciiString(count) + "\n";
1503   }
1504   count = NbProblems->Value((Standard_Integer)BRepCheck_InvalidRange);
1505   if (count > 0) {
1506     theDump += "  Invalid Range ............................ ";
1507     theDump += TCollection_AsciiString(count) + "\n";
1508   }
1509   count = NbProblems->Value((Standard_Integer)BRepCheck_EmptyWire);
1510   if (count > 0) {
1511     theDump += "  Empty Wire ............................... ";
1512     theDump += TCollection_AsciiString(count) + "\n";
1513   }
1514   count = NbProblems->Value((Standard_Integer)BRepCheck_RedundantEdge);
1515   if (count > 0) {
1516     theDump += "  Redundant Edge ........................... ";
1517     theDump += TCollection_AsciiString(count) + "\n";
1518   }
1519   count = NbProblems->Value((Standard_Integer)BRepCheck_SelfIntersectingWire);
1520   if (count > 0) {
1521     theDump += "  Self Intersecting Wire ................... ";
1522     theDump += TCollection_AsciiString(count) + "\n";
1523   }
1524   count = NbProblems->Value((Standard_Integer)BRepCheck_NoSurface);
1525   if (count > 0) {
1526     theDump += "  No Surface ............................... ";
1527     theDump += TCollection_AsciiString(count) + "\n";
1528   }
1529   count = NbProblems->Value((Standard_Integer)BRepCheck_InvalidWire);
1530   if (count > 0) {
1531     theDump += "  Invalid Wire ............................. ";
1532     theDump += TCollection_AsciiString(count) + "\n";
1533   }
1534   count = NbProblems->Value((Standard_Integer)BRepCheck_RedundantWire);
1535   if (count > 0) {
1536     theDump += "  Redundant Wire ........................... ";
1537     theDump += TCollection_AsciiString(count) + "\n";
1538   }
1539   count = NbProblems->Value((Standard_Integer)BRepCheck_IntersectingWires);
1540   if (count > 0) {
1541     theDump += "  Intersecting Wires ....................... ";
1542     theDump += TCollection_AsciiString(count) + "\n";
1543   }
1544   count = NbProblems->Value((Standard_Integer)BRepCheck_InvalidImbricationOfWires);
1545   if (count > 0) {
1546     theDump += "  Invalid Imbrication of Wires ............. ";
1547     theDump += TCollection_AsciiString(count) + "\n";
1548   }
1549   count = NbProblems->Value((Standard_Integer)BRepCheck_EmptyShell);
1550   if (count > 0) {
1551     theDump += "  Empty Shell .............................. ";
1552     theDump += TCollection_AsciiString(count) + "\n";
1553   }
1554   count = NbProblems->Value((Standard_Integer)BRepCheck_RedundantFace);
1555   if (count > 0) {
1556     theDump += "  Redundant Face ........................... ";
1557     theDump += TCollection_AsciiString(count) + "\n";
1558   }
1559   count = NbProblems->Value((Standard_Integer)BRepCheck_UnorientableShape);
1560   if (count > 0) {
1561     theDump += "  Unorientable Shape ....................... ";
1562     theDump += TCollection_AsciiString(count) + "\n";
1563   }
1564   count = NbProblems->Value((Standard_Integer)BRepCheck_NotClosed);
1565   if (count > 0) {
1566     theDump += "  Not Closed ............................... ";
1567     theDump += TCollection_AsciiString(count) + "\n";
1568   }
1569   count = NbProblems->Value((Standard_Integer)BRepCheck_NotConnected);
1570   if (count > 0) {
1571     theDump += "  Not Connected ............................ ";
1572     theDump += TCollection_AsciiString(count) + "\n";
1573   }
1574   count = NbProblems->Value((Standard_Integer)BRepCheck_SubshapeNotInShape);
1575   if (count > 0) {
1576     theDump += "  Subshape not in Shape .................... ";
1577     theDump += TCollection_AsciiString(count) + "\n";
1578   }
1579   count = NbProblems->Value((Standard_Integer)BRepCheck_BadOrientation);
1580   if (count > 0) {
1581     theDump += "  Bad Orientation .......................... ";
1582     theDump += TCollection_AsciiString(count) + "\n";
1583   }
1584   count = NbProblems->Value((Standard_Integer)BRepCheck_BadOrientationOfSubshape);
1585   if (count > 0) {
1586     theDump += "  Bad Orientation of Subshape .............. ";
1587     theDump += TCollection_AsciiString(count) + "\n";
1588   }
1589   count = NbProblems->Value((Standard_Integer)BRepCheck_CheckFail);
1590   if (count > 0) {
1591     theDump += "  checkshape failure ....................... ";
1592     theDump += TCollection_AsciiString(count) + "\n";
1593   }
1594
1595   theDump += " ------------------------------------------------\n";
1596   theDump += "*** Shapes with problems : ";
1597   theDump += TCollection_AsciiString(sl->Length()) + "\n";
1598
1599   Standard_Integer nbv, nbe, nbw, nbf, nbs, nbo;
1600   nbv = nbe = nbw = nbf = nbs = nbo = 0;
1601
1602   for (i = 1; i <= sl->Length(); i++) {
1603     TopoDS_Shape shi = sl->Value(i);
1604     TopAbs_ShapeEnum sti = shi.ShapeType();
1605     switch (sti) {
1606       case TopAbs_VERTEX : nbv++; break;
1607       case TopAbs_EDGE   : nbe++; break;
1608       case TopAbs_WIRE   : nbw++; break;
1609       case TopAbs_FACE   : nbf++; break;
1610       case TopAbs_SHELL  : nbs++; break;
1611       case TopAbs_SOLID  : nbo++; break;
1612       default            : break;
1613     }
1614   }
1615
1616   if (nbv > 0) {
1617     theDump += "VERTEX : ";
1618     if (nbv < 10) theDump += " ";
1619     theDump += TCollection_AsciiString(nbv) + "\n";
1620   }
1621   if (nbe > 0) {
1622     theDump += "EDGE   : ";
1623     if (nbe < 10) theDump += " ";
1624     theDump += TCollection_AsciiString(nbe) + "\n";
1625   }
1626   if (nbw > 0) {
1627     theDump += "WIRE   : ";
1628     if (nbw < 10) theDump += " ";
1629     theDump += TCollection_AsciiString(nbw) + "\n";
1630   }
1631   if (nbf > 0) {
1632     theDump += "FACE   : ";
1633     if (nbf < 10) theDump += " ";
1634     theDump += TCollection_AsciiString(nbf) + "\n";
1635   }
1636   if (nbs > 0) {
1637     theDump += "SHELL  : ";
1638     if (nbs < 10) theDump += " ";
1639     theDump += TCollection_AsciiString(nbs) + "\n";
1640   }
1641   if (nbo > 0) {
1642     theDump += "SOLID  : ";
1643     if (nbo < 10) theDump += " ";
1644     theDump += TCollection_AsciiString(nbo) + "\n";
1645   }
1646 }
1647
1648 //=======================================================================
1649 //function : GetProblemShapes
1650 // purpose : for StructuralDump
1651 //=======================================================================
1652 void GEOMImpl_IMeasureOperations::GetProblemShapes (const BRepCheck_Analyzer&           theAna,
1653                                                     const TopoDS_Shape&                 theShape,
1654                                                     Handle(TopTools_HSequenceOfShape)&  sl,
1655                                                     Handle(TColStd_HArray1OfInteger)&   NbProblems,
1656                                                     TopTools_DataMapOfShapeListOfShape& theMap)
1657 {
1658   for (TopoDS_Iterator iter(theShape); iter.More(); iter.Next()) {
1659     GetProblemShapes(theAna, iter.Value(), sl, NbProblems, theMap);
1660   }
1661   TopAbs_ShapeEnum styp = theShape.ShapeType();
1662   BRepCheck_ListIteratorOfListOfStatus itl;
1663   TopTools_ListOfShape empty;
1664   if (!theMap.IsBound(theShape)) {
1665     theMap.Bind(theShape,empty);
1666
1667     if (!theAna.Result(theShape).IsNull()) {
1668       itl.Initialize(theAna.Result(theShape)->Status());
1669       // !!! May be, we have to print all the problems, not only the first one ?
1670       if (itl.Value() != BRepCheck_NoError) {
1671         sl->Append(theShape);
1672         BRepCheck_Status stat = itl.Value();
1673         NbProblems->SetValue((Standard_Integer)stat,
1674                              NbProblems->Value((Standard_Integer)stat) + 1);
1675       }
1676     }
1677   }
1678
1679   switch (styp) {
1680   case TopAbs_EDGE:
1681     GetProblemSub(theAna, theShape, sl, NbProblems, TopAbs_VERTEX, theMap);
1682     break;
1683   case TopAbs_FACE:
1684     GetProblemSub(theAna, theShape, sl, NbProblems, TopAbs_WIRE, theMap);
1685     GetProblemSub(theAna, theShape, sl, NbProblems, TopAbs_EDGE, theMap);
1686     GetProblemSub(theAna, theShape, sl, NbProblems, TopAbs_VERTEX, theMap);
1687     break;
1688   case TopAbs_SHELL:
1689     break;
1690   case TopAbs_SOLID:
1691     GetProblemSub(theAna, theShape, sl, NbProblems, TopAbs_SHELL, theMap);
1692     break;
1693   default:
1694     break;
1695   }
1696 }
1697
1698 //=======================================================================
1699 //function : Contains
1700 //=======================================================================
1701 static Standard_Boolean Contains (const TopTools_ListOfShape& L,
1702                                   const TopoDS_Shape& S)
1703 {
1704   TopTools_ListIteratorOfListOfShape it;
1705   for (it.Initialize(L); it.More(); it.Next()) {
1706     if (it.Value().IsSame(S)) {
1707       return Standard_True;
1708     }
1709   }
1710   return Standard_False;
1711 }
1712
1713 //=======================================================================
1714 //function : GetProblemSub
1715 // purpose : for StructuralDump
1716 //=======================================================================
1717 void GEOMImpl_IMeasureOperations::GetProblemSub (const BRepCheck_Analyzer&           theAna,
1718                                                  const TopoDS_Shape&                 theShape,
1719                                                  Handle(TopTools_HSequenceOfShape)&  sl,
1720                                                  Handle(TColStd_HArray1OfInteger)&   NbProblems,
1721                                                  const TopAbs_ShapeEnum              Subtype,
1722                                                  TopTools_DataMapOfShapeListOfShape& theMap)
1723 {
1724   BRepCheck_ListIteratorOfListOfStatus itl;
1725   TopExp_Explorer exp;
1726   for (exp.Init(theShape, Subtype); exp.More(); exp.Next()) {
1727     const TopoDS_Shape& sub = exp.Current();
1728
1729     const Handle(BRepCheck_Result)& res = theAna.Result(sub);
1730     for (res->InitContextIterator();
1731          res->MoreShapeInContext();
1732          res->NextShapeInContext()) {
1733       if (res->ContextualShape().IsSame(theShape) && !Contains(theMap(sub), theShape)) {
1734         theMap(sub).Append(theShape);
1735         itl.Initialize(res->StatusOnShape());
1736
1737         if (itl.Value() != BRepCheck_NoError) {
1738           Standard_Integer ii = 0;
1739
1740           for (ii = 1; ii <= sl->Length(); ii++)
1741             if (sl->Value(ii).IsSame(sub)) break;
1742
1743           if (ii > sl->Length()) {
1744             sl->Append(sub);
1745             BRepCheck_Status stat = itl.Value();
1746             NbProblems->SetValue((Standard_Integer)stat,
1747                                  NbProblems->Value((Standard_Integer)stat) + 1);
1748           }
1749           for (ii = 1; ii <= sl->Length(); ii++)
1750             if (sl->Value(ii).IsSame(theShape)) break;
1751           if (ii > sl->Length()) {
1752             sl->Append(theShape);
1753             BRepCheck_Status stat = itl.Value();
1754             NbProblems->SetValue((Standard_Integer)stat,
1755                                  NbProblems->Value((Standard_Integer)stat) + 1);
1756           }
1757         }
1758         break;
1759       }
1760     }
1761   }
1762 }