Salome HOME
Moving to GUI module
[modules/geom.git] / src / GEOM_I / GEOM_IMeasureOperations_i.cc
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 #include <Standard_Stream.hxx>
23
24 #include "GEOM_IMeasureOperations_i.hh"
25
26 #include "utilities.h"
27 #include "OpUtil.hxx"
28
29 #include "GEOM_Engine.hxx"
30 #include "GEOM_Object.hxx"
31
32 //=============================================================================
33 /*!
34  *   constructor:
35  */
36 //=============================================================================
37 GEOM_IMeasureOperations_i::GEOM_IMeasureOperations_i (PortableServer::POA_ptr thePOA,
38                                                     GEOM::GEOM_Gen_ptr theEngine,
39                                                     ::GEOMImpl_IMeasureOperations* theImpl)
40 :GEOM_IOperations_i(thePOA, theEngine, theImpl)
41 {
42   MESSAGE("GEOM_IMeasureOperations_i::GEOM_IMeasureOperations_i");
43 }
44
45 //=============================================================================
46 /*!
47  *  destructor
48  */
49 //=============================================================================
50 GEOM_IMeasureOperations_i::~GEOM_IMeasureOperations_i()
51 {
52   MESSAGE("GEOM_IMeasureOperations_i::~GEOM_IMeasureOperations_i");
53 }
54
55 //=============================================================================
56 /*!
57  *  KindOfShape
58  */
59 //=============================================================================
60 GEOM::GEOM_IKindOfShape::shape_kind GEOM_IMeasureOperations_i::KindOfShape
61                                    (GEOM::GEOM_Object_ptr  theShape,
62                                     GEOM::ListOfLong_out   theIntegers,
63                                     GEOM::ListOfDouble_out theDoubles)
64 {
65   GEOMImpl_IMeasureOperations::ShapeKind aKind = GEOMImpl_IMeasureOperations::SK_NO_SHAPE;
66
67   // allocate the CORBA arrays
68   GEOM::ListOfLong_var anIntegersArray = new GEOM::ListOfLong();
69   GEOM::ListOfDouble_var aDoublesArray = new GEOM::ListOfDouble();
70
71   //Get the reference shape
72   Handle(GEOM_Object) aShape = GetOperations()->GetEngine()->GetObject
73     (theShape->GetStudyID(), theShape->GetEntry());
74
75   if (!aShape.IsNull()) {
76     Handle(TColStd_HSequenceOfInteger) anIntegers = new TColStd_HSequenceOfInteger;
77     Handle(TColStd_HSequenceOfReal)    aDoubles   = new TColStd_HSequenceOfReal;
78
79     // Detect kind of shape and parameters
80     aKind = GetOperations()->KindOfShape(aShape, anIntegers, aDoubles);
81
82     int nbInts = anIntegers->Length();
83     int nbDbls = aDoubles->Length();
84
85     anIntegersArray->length(nbInts);
86     aDoublesArray->length(nbDbls);
87
88     for (int ii = 0; ii < nbInts; ii++) {
89       anIntegersArray[ii] = anIntegers->Value(ii + 1);
90     }
91     for (int id = 0; id < nbDbls; id++) {
92       aDoublesArray[id] = aDoubles->Value(id + 1);
93     }
94   }
95
96   // initialize out-parameters with local arrays
97   theIntegers = anIntegersArray._retn();
98   theDoubles  = aDoublesArray._retn();
99   return (GEOM::GEOM_IKindOfShape::shape_kind)aKind;
100 }
101
102 //=============================================================================
103 /*!
104  *  GetPosition
105  */
106 //=============================================================================
107 void GEOM_IMeasureOperations_i::GetPosition
108                  (GEOM::GEOM_Object_ptr theShape,
109                   CORBA::Double& Ox, CORBA::Double& Oy, CORBA::Double& Oz,
110                   CORBA::Double& Zx, CORBA::Double& Zy, CORBA::Double& Zz,
111                   CORBA::Double& Xx, CORBA::Double& Xy, CORBA::Double& Xz)
112 {
113   //Set a not done flag
114   GetOperations()->SetNotDone();
115
116   //Set default values: global CS
117   Ox = Oy = Oz = Zx = Zy = Xy = Xz = 0.;
118   Zz = Xx = 1.;
119
120   if (theShape == NULL) return;
121
122   //Get the reference shape
123   Handle(GEOM_Object) aShape = GetOperations()->GetEngine()->GetObject
124     (theShape->GetStudyID(), theShape->GetEntry());
125
126   if (aShape.IsNull()) return;
127
128   // Get shape parameters
129   GetOperations()->GetPosition(aShape, Ox,Oy,Oz, Zx,Zy,Zz, Xx,Xy,Xz);
130 }
131
132 //=============================================================================
133 /*!
134  *  GetCentreOfMass
135  */
136 //=============================================================================
137 GEOM::GEOM_Object_ptr GEOM_IMeasureOperations_i::GetCentreOfMass
138                                               (GEOM::GEOM_Object_ptr theShape)
139 {
140   GEOM::GEOM_Object_var aGEOMObject;
141
142   //Set a not done flag
143   GetOperations()->SetNotDone();
144
145   if (CORBA::is_nil(theShape)) return aGEOMObject._retn();
146
147   //Get the reference shape
148   Handle(GEOM_Object) aShape = GetOperations()->GetEngine()->GetObject
149     (theShape->GetStudyID(), theShape->GetEntry());
150
151   if (aShape.IsNull()) return aGEOMObject._retn();
152
153   // Make Point - centre of mass of theShape
154   Handle(GEOM_Object) anObject = GetOperations()->GetCentreOfMass(aShape);
155   if (!GetOperations()->IsDone() || anObject.IsNull())
156     return aGEOMObject._retn();
157
158   return GetObject(anObject);
159 }
160
161 //=============================================================================
162 /*!
163  *  GetNormal
164  */
165 //=============================================================================
166 GEOM::GEOM_Object_ptr GEOM_IMeasureOperations_i::GetNormal
167                                        (GEOM::GEOM_Object_ptr theFace,
168                                         GEOM::GEOM_Object_ptr theOptionalPoint)
169 {
170   GEOM::GEOM_Object_var aGEOMObject;
171
172   //Set a not done flag
173   GetOperations()->SetNotDone();
174
175   if (CORBA::is_nil(theFace)) return aGEOMObject._retn();
176
177   //Get the reference shape
178   Handle(GEOM_Object) aFace = GetOperations()->GetEngine()->GetObject
179     (theFace->GetStudyID(), theFace->GetEntry());
180
181   if (aFace.IsNull()) return aGEOMObject._retn();
182
183   // Make Vector - normal to theFace (in point theOptionalPoint if the face is not planar)
184   Handle(GEOM_Object) anOptionalPoint;
185   if (!CORBA::is_nil(theOptionalPoint)) {
186     anOptionalPoint = GetOperations()->GetEngine()->GetObject
187       (theOptionalPoint->GetStudyID(), theOptionalPoint->GetEntry());
188   }
189   Handle(GEOM_Object) anObject = GetOperations()->GetNormal(aFace, anOptionalPoint);
190   if (!GetOperations()->IsDone() || anObject.IsNull())
191     return aGEOMObject._retn();
192
193   return GetObject(anObject);
194 }
195
196 //=============================================================================
197 /*!
198  *  GetBasicProperties
199  */
200 //=============================================================================
201 void GEOM_IMeasureOperations_i::GetBasicProperties (GEOM::GEOM_Object_ptr theShape,
202                                                     CORBA::Double& theLength,
203                                                     CORBA::Double& theSurfArea,
204                                                     CORBA::Double& theVolume)
205 {
206   //Set a not done flag
207   GetOperations()->SetNotDone();
208
209   if (theShape == NULL) return;
210
211   //Get the reference shape
212   Handle(GEOM_Object) aShape = GetOperations()->GetEngine()->GetObject
213     (theShape->GetStudyID(), theShape->GetEntry());
214
215   if (aShape.IsNull()) return;
216
217   // Get shape parameters
218   GetOperations()->GetBasicProperties(aShape, theLength, theSurfArea, theVolume);
219 }
220
221 //=============================================================================
222 /*!
223  *  GetInertia
224  */
225 //=============================================================================
226 void GEOM_IMeasureOperations_i::GetInertia
227   (GEOM::GEOM_Object_ptr theShape,
228    CORBA::Double& I11, CORBA::Double& I12, CORBA::Double& I13,
229    CORBA::Double& I21, CORBA::Double& I22, CORBA::Double& I23,
230    CORBA::Double& I31, CORBA::Double& I32, CORBA::Double& I33,
231    CORBA::Double& Ix , CORBA::Double& Iy , CORBA::Double& Iz)
232 {
233   //Set a not done flag
234   GetOperations()->SetNotDone();
235
236   if (theShape == NULL) return;
237
238   //Get the reference shape
239   Handle(GEOM_Object) aShape = GetOperations()->GetEngine()->GetObject
240     (theShape->GetStudyID(), theShape->GetEntry());
241
242   if (aShape.IsNull()) return;
243
244   // Get shape parameters
245   GetOperations()->GetInertia(aShape,
246                               I11, I12, I13,
247                               I21, I22, I23,
248                               I31, I32, I33,
249                               Ix , Iy , Iz);
250 }
251
252 //=============================================================================
253 /*!
254  *  GetBoundingBox
255  */
256 //=============================================================================
257 void GEOM_IMeasureOperations_i::GetBoundingBox (GEOM::GEOM_Object_ptr theShape,
258                                                 CORBA::Double& Xmin, CORBA::Double& Xmax,
259                                                 CORBA::Double& Ymin, CORBA::Double& Ymax,
260                                                 CORBA::Double& Zmin, CORBA::Double& Zmax)
261 {
262   //Set a not done flag
263   GetOperations()->SetNotDone();
264
265   if (theShape == NULL) return;
266
267   //Get the reference shape
268   Handle(GEOM_Object) aShape = GetOperations()->GetEngine()->GetObject
269     (theShape->GetStudyID(), theShape->GetEntry());
270
271   if (aShape.IsNull()) return;
272
273   // Get shape parameters
274   GetOperations()->GetBoundingBox(aShape, Xmin, Xmax, Ymin, Ymax, Zmin, Zmax);
275 }
276
277 //=============================================================================
278 /*!
279  *  GetTolerance
280  */
281 //=============================================================================
282 void GEOM_IMeasureOperations_i::GetTolerance
283                                 (GEOM::GEOM_Object_ptr theShape,
284                                  CORBA::Double& FaceMin, CORBA::Double& FaceMax,
285                                  CORBA::Double& EdgeMin, CORBA::Double& EdgeMax,
286                                  CORBA::Double& VertMin, CORBA::Double& VertMax)
287 {
288   //Set a not done flag
289   GetOperations()->SetNotDone();
290
291   if (theShape == NULL) return;
292
293   //Get the reference shape
294   Handle(GEOM_Object) aShape = GetOperations()->GetEngine()->GetObject
295     (theShape->GetStudyID(), theShape->GetEntry());
296
297   if (aShape.IsNull()) return;
298
299   // Get shape parameters
300   GetOperations()->GetTolerance(aShape,
301                                 FaceMin, FaceMax,
302                                 EdgeMin, EdgeMax,
303                                 VertMin, VertMax);
304 }
305
306 //=============================================================================
307 /*!
308  *  CheckShape
309  */
310 //=============================================================================
311 CORBA::Boolean GEOM_IMeasureOperations_i::CheckShape (GEOM::GEOM_Object_ptr theShape,
312                                                       CORBA::String_out     theDescription)
313 {
314   //Set a not done flag
315   GetOperations()->SetNotDone();
316
317   if (theShape == NULL)
318   {
319     theDescription = CORBA::string_dup("null");
320     return 0;
321   }
322
323   //Get the reference shape
324   Handle(GEOM_Object) aShape = GetOperations()->GetEngine()->GetObject
325     (theShape->GetStudyID(), theShape->GetEntry());
326
327   if (aShape.IsNull())
328   {
329     theDescription = CORBA::string_dup("null2");
330     return 0;
331   }
332
333   // Get shape parameters
334   TCollection_AsciiString aDump;
335   if (GetOperations()->CheckShape(aShape, /*check_geom = */false, aDump))
336   {
337     theDescription = CORBA::string_dup("OK");
338     return 1;
339   }
340   theDescription = CORBA::string_dup(aDump.ToCString());
341   return 0;
342 }
343
344 CORBA::Boolean GEOM_IMeasureOperations_i::CheckShapeWithGeometry (GEOM::GEOM_Object_ptr theShape,
345                                                                   CORBA::String_out     theDescription)
346 {
347   //Set a not done flag
348   GetOperations()->SetNotDone();
349
350   if (theShape == NULL)
351   {
352     theDescription = CORBA::string_dup("null");
353     return 0;
354   }
355
356   //Get the reference shape
357   Handle(GEOM_Object) aShape = GetOperations()->GetEngine()->GetObject
358     (theShape->GetStudyID(), theShape->GetEntry());
359
360   if (aShape.IsNull())
361   {
362     theDescription = CORBA::string_dup("null2");
363     return 0;
364   }
365
366   // Get shape parameters
367   TCollection_AsciiString aDump;
368   if (GetOperations()->CheckShape(aShape, /*check_geom = */true, aDump))
369   {
370     theDescription = CORBA::string_dup("OK");
371     return 1;
372   }
373   theDescription = CORBA::string_dup(aDump.ToCString());
374   return 0;
375 }
376
377 //=============================================================================
378 /*!
379  *  WhatIs
380  */
381 //=============================================================================
382 char* GEOM_IMeasureOperations_i::WhatIs (GEOM::GEOM_Object_ptr theShape)
383 {
384   //Set a not done flag
385   GetOperations()->SetNotDone();
386
387   if (theShape == NULL) return NULL;
388
389   //Get the reference shape
390   Handle(GEOM_Object) aShape = GetOperations()->GetEngine()->GetObject
391     (theShape->GetStudyID(), theShape->GetEntry());
392
393   if (aShape.IsNull()) return NULL;
394
395   // Get shape parameters
396   TCollection_AsciiString aDescription = GetOperations()->WhatIs(aShape);
397   return CORBA::string_dup(aDescription.ToCString());
398 }
399
400 //=============================================================================
401 /*!
402  *  GetMinDistance
403  */
404 //=============================================================================
405 CORBA::Double GEOM_IMeasureOperations_i::GetMinDistance
406   (GEOM::GEOM_Object_ptr theShape1, GEOM::GEOM_Object_ptr theShape2,
407    CORBA::Double& X1, CORBA::Double& Y1, CORBA::Double& Z1,
408    CORBA::Double& X2, CORBA::Double& Y2, CORBA::Double& Z2)
409 {
410   //Set a not done flag
411   GetOperations()->SetNotDone();
412
413   if (theShape1 == NULL || theShape2 == NULL) return -1.0;
414
415   //Get the reference shape
416   Handle(GEOM_Object) aShape1 = GetOperations()->GetEngine()->GetObject
417     (theShape1->GetStudyID(), theShape1->GetEntry());
418   Handle(GEOM_Object) aShape2 = GetOperations()->GetEngine()->GetObject
419     (theShape2->GetStudyID(), theShape2->GetEntry());
420
421   if (aShape1.IsNull() || aShape2.IsNull()) return -1.0;
422
423   // Get shape parameters
424   return GetOperations()->GetMinDistance(aShape1, aShape2, X1, Y1, Z1, X2, Y2, Z2);
425 }
426
427 //=============================================================================
428 /*!
429  *  PointCoordinates
430  */
431 //=============================================================================
432 void GEOM_IMeasureOperations_i::PointCoordinates (GEOM::GEOM_Object_ptr theShape,
433                                                   CORBA::Double& X, CORBA::Double& Y, CORBA::Double& Z)
434
435 {
436   //Set a not done flag
437   GetOperations()->SetNotDone();
438
439   if ( theShape->_is_nil() )
440     return;
441
442   //Get the reference shape
443   Handle(GEOM_Object) aShape = GetOperations()->GetEngine()->GetObject(
444     theShape->GetStudyID(), theShape->GetEntry() );
445
446   if ( aShape.IsNull() )
447     return;
448
449   // Get shape parameters
450   GetOperations()->PointCoordinates( aShape, X, Y, Z );
451 }
452
453 //=============================================================================
454 /*!
455  *  GetAngle
456  */
457 //=============================================================================
458 CORBA::Double GEOM_IMeasureOperations_i::GetAngle (GEOM::GEOM_Object_ptr theShape1,
459                                                    GEOM::GEOM_Object_ptr theShape2)
460 {
461   //Set a not done flag
462   GetOperations()->SetNotDone();
463
464   if (theShape1 == NULL || theShape2 == NULL) return -1.0;
465
466   //Get the reference shapes
467   Handle(GEOM_Object) aShape1 = GetOperations()->GetEngine()->GetObject
468     (theShape1->GetStudyID(), theShape1->GetEntry());
469   Handle(GEOM_Object) aShape2 = GetOperations()->GetEngine()->GetObject
470     (theShape2->GetStudyID(), theShape2->GetEntry());
471
472   if (aShape1.IsNull() || aShape2.IsNull()) return -1.0;
473
474   // Get the angle
475   return GetOperations()->GetAngle(aShape1, aShape2);
476 }
477
478
479 //=============================================================================
480 /*!
481  *  CurveCurvatureByParam
482  */
483 //=============================================================================
484 CORBA::Double GEOM_IMeasureOperations_i::CurveCurvatureByParam
485                        (GEOM::GEOM_Object_ptr theCurve, CORBA::Double theParam)
486 {
487   //Set a not done flag
488   GetOperations()->SetNotDone();
489
490   if(theCurve==NULL) return -1.0;
491
492   //Get the reference shape
493   Handle(GEOM_Object) aShape = GetOperations()->GetEngine()->GetObject
494     (theCurve->GetStudyID(), theCurve->GetEntry());
495
496   if(aShape.IsNull()) return -1.0;
497
498   return GetOperations()->CurveCurvatureByParam(aShape,theParam);
499 }
500
501
502 //=============================================================================
503 /*!
504  *  CurveCurvatureByPoint
505  */
506 //=============================================================================
507 CORBA::Double GEOM_IMeasureOperations_i::CurveCurvatureByPoint
508                (GEOM::GEOM_Object_ptr theCurve, GEOM::GEOM_Object_ptr thePoint)
509 {
510   //Set a not done flag
511   GetOperations()->SetNotDone();
512
513   if( theCurve==NULL || thePoint==NULL ) return -1.0;
514
515   //Get the reference shape
516   Handle(GEOM_Object) aShape = GetOperations()->GetEngine()->GetObject
517     (theCurve->GetStudyID(), theCurve->GetEntry());
518   Handle(GEOM_Object) aPoint = GetOperations()->GetEngine()->GetObject
519     (thePoint->GetStudyID(), thePoint->GetEntry());
520
521   if( aShape.IsNull() || aPoint.IsNull() ) return -1.0;
522
523   return GetOperations()->CurveCurvatureByPoint(aShape,aPoint);
524 }
525
526
527 //=============================================================================
528 /*!
529  *  MaxSurfaceCurvatureByParam
530  */
531 //=============================================================================
532 CORBA::Double GEOM_IMeasureOperations_i::MaxSurfaceCurvatureByParam
533                                                 (GEOM::GEOM_Object_ptr theSurf,
534                                                  CORBA::Double theUParam,
535                                                  CORBA::Double theVParam)
536 {
537   //Set a not done flag
538   GetOperations()->SetNotDone();
539
540   if(theSurf==NULL) return -1.0;
541
542   //Get the reference shape
543   Handle(GEOM_Object) aShape = GetOperations()->GetEngine()->GetObject
544     (theSurf->GetStudyID(), theSurf->GetEntry());
545
546   if(aShape.IsNull()) return -1.0;
547
548   return GetOperations()->MaxSurfaceCurvatureByParam(aShape,theUParam,theVParam);
549 }
550
551
552 //=============================================================================
553 /*!
554  *  MaxSurfaceCurvatureByPoint
555  */
556 //=============================================================================
557 CORBA::Double GEOM_IMeasureOperations_i::MaxSurfaceCurvatureByPoint
558                 (GEOM::GEOM_Object_ptr theSurf, GEOM::GEOM_Object_ptr thePoint)
559 {
560   //Set a not done flag
561   GetOperations()->SetNotDone();
562
563   if( theSurf==NULL || thePoint==NULL ) return -1.0;
564
565   //Get the reference shape
566   Handle(GEOM_Object) aShape = GetOperations()->GetEngine()->GetObject
567     (theSurf->GetStudyID(), theSurf->GetEntry());
568   Handle(GEOM_Object) aPoint = GetOperations()->GetEngine()->GetObject
569     (thePoint->GetStudyID(), thePoint->GetEntry());
570
571   if( aShape.IsNull() || aPoint.IsNull() ) return -1.0;
572
573   return GetOperations()->MaxSurfaceCurvatureByPoint(aShape,aPoint);
574 }
575
576
577 //=============================================================================
578 /*!
579  *  MinSurfaceCurvatureByParam
580  */
581 //=============================================================================
582 CORBA::Double GEOM_IMeasureOperations_i::MinSurfaceCurvatureByParam
583                                                 (GEOM::GEOM_Object_ptr theSurf,
584                                                  CORBA::Double theUParam,
585                                                  CORBA::Double theVParam)
586 {
587   //Set a not done flag
588   GetOperations()->SetNotDone();
589
590   if(theSurf==NULL) return -1.0;
591
592   //Get the reference shape
593   Handle(GEOM_Object) aShape = GetOperations()->GetEngine()->GetObject
594     (theSurf->GetStudyID(), theSurf->GetEntry());
595
596   if(aShape.IsNull()) return -1.0;
597
598   return GetOperations()->MinSurfaceCurvatureByParam(aShape,theUParam,theVParam);
599 }
600
601
602 //=============================================================================
603 /*!
604  *  MinSurfaceCurvatureByPoint
605  */
606 //=============================================================================
607 CORBA::Double GEOM_IMeasureOperations_i::MinSurfaceCurvatureByPoint
608                 (GEOM::GEOM_Object_ptr theSurf, GEOM::GEOM_Object_ptr thePoint)
609 {
610   //Set a not done flag
611   GetOperations()->SetNotDone();
612
613   if( theSurf==NULL || thePoint==NULL ) return -1.0;
614
615   //Get the reference shape
616   Handle(GEOM_Object) aShape = GetOperations()->GetEngine()->GetObject
617     (theSurf->GetStudyID(), theSurf->GetEntry());
618   Handle(GEOM_Object) aPoint = GetOperations()->GetEngine()->GetObject
619     (thePoint->GetStudyID(), thePoint->GetEntry());
620
621   if( aShape.IsNull() || aPoint.IsNull() ) return -1.0;
622
623   return GetOperations()->MinSurfaceCurvatureByPoint(aShape,aPoint);
624 }