Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[modules/geom.git] / src / GEOM_I / GEOM_IMeasureOperations_i.cc
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 "GEOM_IMeasureOperations_i.hh"
24
25 #include "utilities.h"
26 #include "OpUtil.hxx"
27
28 #include "GEOM_Engine.hxx"
29 #include "GEOM_Object.hxx"
30
31 //=============================================================================
32 /*!
33  *   constructor:
34  */
35 //=============================================================================
36 GEOM_IMeasureOperations_i::GEOM_IMeasureOperations_i (PortableServer::POA_ptr thePOA,
37                                                     GEOM::GEOM_Gen_ptr theEngine,
38                                                     ::GEOMImpl_IMeasureOperations* theImpl)
39 :GEOM_IOperations_i(thePOA, theEngine, theImpl)
40 {
41   MESSAGE("GEOM_IMeasureOperations_i::GEOM_IMeasureOperations_i");
42 }
43
44 //=============================================================================
45 /*!
46  *  destructor
47  */
48 //=============================================================================
49 GEOM_IMeasureOperations_i::~GEOM_IMeasureOperations_i()
50 {
51   MESSAGE("GEOM_IMeasureOperations_i::~GEOM_IMeasureOperations_i");
52 }
53
54 //=============================================================================
55 /*!
56  *  KindOfShape
57  */
58 //=============================================================================
59 GEOM::GEOM_IKindOfShape::shape_kind GEOM_IMeasureOperations_i::KindOfShape
60                                    (GEOM::GEOM_Object_ptr  theShape,
61                                     GEOM::ListOfLong_out   theIntegers,
62                                     GEOM::ListOfDouble_out theDoubles)
63 {
64   GEOMImpl_IMeasureOperations::ShapeKind aKind = GEOMImpl_IMeasureOperations::SK_NO_SHAPE;
65
66   // allocate the CORBA arrays
67   GEOM::ListOfLong_var anIntegersArray = new GEOM::ListOfLong();
68   GEOM::ListOfDouble_var aDoublesArray = new GEOM::ListOfDouble();
69
70   //Get the reference shape
71   Handle(GEOM_Object) aShape = GetOperations()->GetEngine()->GetObject
72     (theShape->GetStudyID(), theShape->GetEntry());
73
74   if (!aShape.IsNull()) {
75     Handle(TColStd_HSequenceOfInteger) anIntegers = new TColStd_HSequenceOfInteger;
76     Handle(TColStd_HSequenceOfReal)    aDoubles   = new TColStd_HSequenceOfReal;
77
78     // Detect kind of shape and parameters
79     aKind = GetOperations()->KindOfShape(aShape, anIntegers, aDoubles);
80
81     int nbInts = anIntegers->Length();
82     int nbDbls = aDoubles->Length();
83
84     anIntegersArray->length(nbInts);
85     aDoublesArray->length(nbDbls);
86
87     for (int ii = 0; ii < nbInts; ii++) {
88       anIntegersArray[ii] = anIntegers->Value(ii + 1);
89     }
90     for (int id = 0; id < nbDbls; id++) {
91       aDoublesArray[id] = aDoubles->Value(id + 1);
92     }
93   }
94
95   // initialize out-parameters with local arrays
96   theIntegers = anIntegersArray._retn();
97   theDoubles  = aDoublesArray._retn();
98   return (GEOM::GEOM_IKindOfShape::shape_kind)aKind;
99 }
100
101 //=============================================================================
102 /*!
103  *  GetPosition
104  */
105 //=============================================================================
106 void GEOM_IMeasureOperations_i::GetPosition
107                  (GEOM::GEOM_Object_ptr theShape,
108                   CORBA::Double& Ox, CORBA::Double& Oy, CORBA::Double& Oz,
109                   CORBA::Double& Zx, CORBA::Double& Zy, CORBA::Double& Zz,
110                   CORBA::Double& Xx, CORBA::Double& Xy, CORBA::Double& Xz)
111 {
112   //Set a not done flag
113   GetOperations()->SetNotDone();
114
115   //Set default values: global CS
116   Ox = Oy = Oz = Zx = Zy = Xy = Xz = 0.;
117   Zz = Xx = 1.;
118
119   if (theShape == NULL) return;
120
121   //Get the reference shape
122   Handle(GEOM_Object) aShape = GetOperations()->GetEngine()->GetObject
123     (theShape->GetStudyID(), theShape->GetEntry());
124
125   if (aShape.IsNull()) return;
126
127   // Get shape parameters
128   GetOperations()->GetPosition(aShape, Ox,Oy,Oz, Zx,Zy,Zz, Xx,Xy,Xz);
129 }
130
131 //=============================================================================
132 /*!
133  *  GetCentreOfMass
134  */
135 //=============================================================================
136 GEOM::GEOM_Object_ptr GEOM_IMeasureOperations_i::GetCentreOfMass
137                                               (GEOM::GEOM_Object_ptr theShape)
138 {
139   GEOM::GEOM_Object_var aGEOMObject;
140
141   //Set a not done flag
142   GetOperations()->SetNotDone();
143
144   if (CORBA::is_nil(theShape)) return aGEOMObject._retn();
145
146   //Get the reference shape
147   Handle(GEOM_Object) aShape = GetOperations()->GetEngine()->GetObject
148     (theShape->GetStudyID(), theShape->GetEntry());
149
150   if (aShape.IsNull()) return aGEOMObject._retn();
151
152   // Make Point - centre of mass of theShape
153   Handle(GEOM_Object) anObject = GetOperations()->GetCentreOfMass(aShape);
154   if (!GetOperations()->IsDone() || anObject.IsNull())
155     return aGEOMObject._retn();
156
157   return GetObject(anObject);
158 }
159
160 //=============================================================================
161 /*!
162  *  GetNormal
163  */
164 //=============================================================================
165 GEOM::GEOM_Object_ptr GEOM_IMeasureOperations_i::GetNormal
166                                        (GEOM::GEOM_Object_ptr theFace,
167                                         GEOM::GEOM_Object_ptr theOptionalPoint)
168 {
169   GEOM::GEOM_Object_var aGEOMObject;
170
171   //Set a not done flag
172   GetOperations()->SetNotDone();
173
174   if (CORBA::is_nil(theFace)) return aGEOMObject._retn();
175
176   //Get the reference shape
177   Handle(GEOM_Object) aFace = GetOperations()->GetEngine()->GetObject
178     (theFace->GetStudyID(), theFace->GetEntry());
179
180   if (aFace.IsNull()) return aGEOMObject._retn();
181
182   // Make Vector - normal to theFace (in point theOptionalPoint if the face is not planar)
183   Handle(GEOM_Object) anOptionalPoint;
184   if (!CORBA::is_nil(theOptionalPoint)) {
185     anOptionalPoint = GetOperations()->GetEngine()->GetObject
186       (theOptionalPoint->GetStudyID(), theOptionalPoint->GetEntry());
187   }
188   Handle(GEOM_Object) anObject = GetOperations()->GetNormal(aFace, anOptionalPoint);
189   if (!GetOperations()->IsDone() || anObject.IsNull())
190     return aGEOMObject._retn();
191
192   return GetObject(anObject);
193 }
194
195 //=============================================================================
196 /*!
197  *  GetBasicProperties
198  */
199 //=============================================================================
200 void GEOM_IMeasureOperations_i::GetBasicProperties (GEOM::GEOM_Object_ptr theShape,
201                                                     CORBA::Double& theLength,
202                                                     CORBA::Double& theSurfArea,
203                                                     CORBA::Double& theVolume)
204 {
205   //Set a not done flag
206   GetOperations()->SetNotDone();
207
208   if (theShape == NULL) return;
209
210   //Get the reference shape
211   Handle(GEOM_Object) aShape = GetOperations()->GetEngine()->GetObject
212     (theShape->GetStudyID(), theShape->GetEntry());
213
214   if (aShape.IsNull()) return;
215
216   // Get shape parameters
217   GetOperations()->GetBasicProperties(aShape, theLength, theSurfArea, theVolume);
218 }
219
220 //=============================================================================
221 /*!
222  *  GetInertia
223  */
224 //=============================================================================
225 void GEOM_IMeasureOperations_i::GetInertia
226   (GEOM::GEOM_Object_ptr theShape,
227    CORBA::Double& I11, CORBA::Double& I12, CORBA::Double& I13,
228    CORBA::Double& I21, CORBA::Double& I22, CORBA::Double& I23,
229    CORBA::Double& I31, CORBA::Double& I32, CORBA::Double& I33,
230    CORBA::Double& Ix , CORBA::Double& Iy , CORBA::Double& Iz)
231 {
232   //Set a not done flag
233   GetOperations()->SetNotDone();
234
235   if (theShape == NULL) return;
236
237   //Get the reference shape
238   Handle(GEOM_Object) aShape = GetOperations()->GetEngine()->GetObject
239     (theShape->GetStudyID(), theShape->GetEntry());
240
241   if (aShape.IsNull()) return;
242
243   // Get shape parameters
244   GetOperations()->GetInertia(aShape,
245                               I11, I12, I13,
246                               I21, I22, I23,
247                               I31, I32, I33,
248                               Ix , Iy , Iz);
249 }
250
251 //=============================================================================
252 /*!
253  *  GetBoundingBox
254  */
255 //=============================================================================
256 void GEOM_IMeasureOperations_i::GetBoundingBox (GEOM::GEOM_Object_ptr theShape,
257                                                 CORBA::Double& Xmin, CORBA::Double& Xmax,
258                                                 CORBA::Double& Ymin, CORBA::Double& Ymax,
259                                                 CORBA::Double& Zmin, CORBA::Double& Zmax)
260 {
261   //Set a not done flag
262   GetOperations()->SetNotDone();
263
264   if (theShape == NULL) return;
265
266   //Get the reference shape
267   Handle(GEOM_Object) aShape = GetOperations()->GetEngine()->GetObject
268     (theShape->GetStudyID(), theShape->GetEntry());
269
270   if (aShape.IsNull()) return;
271
272   // Get shape parameters
273   GetOperations()->GetBoundingBox(aShape, Xmin, Xmax, Ymin, Ymax, Zmin, Zmax);
274 }
275
276 //=============================================================================
277 /*!
278  *  GetTolerance
279  */
280 //=============================================================================
281 void GEOM_IMeasureOperations_i::GetTolerance
282                                 (GEOM::GEOM_Object_ptr theShape,
283                                  CORBA::Double& FaceMin, CORBA::Double& FaceMax,
284                                  CORBA::Double& EdgeMin, CORBA::Double& EdgeMax,
285                                  CORBA::Double& VertMin, CORBA::Double& VertMax)
286 {
287   //Set a not done flag
288   GetOperations()->SetNotDone();
289
290   if (theShape == NULL) return;
291
292   //Get the reference shape
293   Handle(GEOM_Object) aShape = GetOperations()->GetEngine()->GetObject
294     (theShape->GetStudyID(), theShape->GetEntry());
295
296   if (aShape.IsNull()) return;
297
298   // Get shape parameters
299   GetOperations()->GetTolerance(aShape,
300                                 FaceMin, FaceMax,
301                                 EdgeMin, EdgeMax,
302                                 VertMin, VertMax);
303 }
304
305 //=============================================================================
306 /*!
307  *  CheckShape
308  */
309 //=============================================================================
310 CORBA::Boolean GEOM_IMeasureOperations_i::CheckShape (GEOM::GEOM_Object_ptr theShape,
311                                                       CORBA::String_out     theDescription)
312 {
313   //Set a not done flag
314   GetOperations()->SetNotDone();
315
316   if (theShape == NULL)
317   {
318     theDescription = CORBA::string_dup("null");
319     return 0;
320   }
321
322   //Get the reference shape
323   Handle(GEOM_Object) aShape = GetOperations()->GetEngine()->GetObject
324     (theShape->GetStudyID(), theShape->GetEntry());
325
326   if (aShape.IsNull())
327   {
328     theDescription = CORBA::string_dup("null2");
329     return 0;
330   }
331
332   // Get shape parameters
333   TCollection_AsciiString aDump;
334   if (GetOperations()->CheckShape(aShape, /*check_geom = */false, aDump))
335   {
336     theDescription = CORBA::string_dup("OK");
337     return 1;
338   }
339   theDescription = CORBA::string_dup(aDump.ToCString());
340   return 0;
341 }
342
343 CORBA::Boolean GEOM_IMeasureOperations_i::CheckShapeWithGeometry (GEOM::GEOM_Object_ptr theShape,
344                                                                   CORBA::String_out     theDescription)
345 {
346   //Set a not done flag
347   GetOperations()->SetNotDone();
348
349   if (theShape == NULL)
350   {
351     theDescription = CORBA::string_dup("null");
352     return 0;
353   }
354
355   //Get the reference shape
356   Handle(GEOM_Object) aShape = GetOperations()->GetEngine()->GetObject
357     (theShape->GetStudyID(), theShape->GetEntry());
358
359   if (aShape.IsNull())
360   {
361     theDescription = CORBA::string_dup("null2");
362     return 0;
363   }
364
365   // Get shape parameters
366   TCollection_AsciiString aDump;
367   if (GetOperations()->CheckShape(aShape, /*check_geom = */true, aDump))
368   {
369     theDescription = CORBA::string_dup("OK");
370     return 1;
371   }
372   theDescription = CORBA::string_dup(aDump.ToCString());
373   return 0;
374 }
375
376 //=============================================================================
377 /*!
378  *  WhatIs
379  */
380 //=============================================================================
381 char* GEOM_IMeasureOperations_i::WhatIs (GEOM::GEOM_Object_ptr theShape)
382 {
383   //Set a not done flag
384   GetOperations()->SetNotDone();
385
386   if (theShape == NULL) return NULL;
387
388   //Get the reference shape
389   Handle(GEOM_Object) aShape = GetOperations()->GetEngine()->GetObject
390     (theShape->GetStudyID(), theShape->GetEntry());
391
392   if (aShape.IsNull()) return NULL;
393
394   // Get shape parameters
395   TCollection_AsciiString aDescription = GetOperations()->WhatIs(aShape);
396   return CORBA::string_dup(aDescription.ToCString());
397 }
398
399 //=============================================================================
400 /*!
401  *  GetMinDistance
402  */
403 //=============================================================================
404 CORBA::Double GEOM_IMeasureOperations_i::GetMinDistance
405   (GEOM::GEOM_Object_ptr theShape1, GEOM::GEOM_Object_ptr theShape2,
406    CORBA::Double& X1, CORBA::Double& Y1, CORBA::Double& Z1,
407    CORBA::Double& X2, CORBA::Double& Y2, CORBA::Double& Z2)
408 {
409   //Set a not done flag
410   GetOperations()->SetNotDone();
411
412   if (theShape1 == NULL || theShape2 == NULL) return -1.0;
413
414   //Get the reference shape
415   Handle(GEOM_Object) aShape1 = GetOperations()->GetEngine()->GetObject
416     (theShape1->GetStudyID(), theShape1->GetEntry());
417   Handle(GEOM_Object) aShape2 = GetOperations()->GetEngine()->GetObject
418     (theShape2->GetStudyID(), theShape2->GetEntry());
419
420   if (aShape1.IsNull() || aShape2.IsNull()) return -1.0;
421
422   // Get shape parameters
423   return GetOperations()->GetMinDistance(aShape1, aShape2, X1, Y1, Z1, X2, Y2, Z2);
424 }
425
426 //=============================================================================
427 /*!
428  *  PointCoordinates
429  */
430 //=============================================================================
431 void GEOM_IMeasureOperations_i::PointCoordinates (GEOM::GEOM_Object_ptr theShape,
432                                                   CORBA::Double& X, CORBA::Double& Y, CORBA::Double& Z)
433
434 {
435   //Set a not done flag
436   GetOperations()->SetNotDone();
437
438   if ( theShape->_is_nil() )
439     return;
440
441   //Get the reference shape
442   Handle(GEOM_Object) aShape = GetOperations()->GetEngine()->GetObject(
443     theShape->GetStudyID(), theShape->GetEntry() );
444
445   if ( aShape.IsNull() )
446     return;
447
448   // Get shape parameters
449   GetOperations()->PointCoordinates( aShape, X, Y, Z );
450 }
451
452 //=============================================================================
453 /*!
454  *  GetAngle
455  */
456 //=============================================================================
457 CORBA::Double GEOM_IMeasureOperations_i::GetAngle (GEOM::GEOM_Object_ptr theShape1,
458                                                    GEOM::GEOM_Object_ptr theShape2)
459 {
460   //Set a not done flag
461   GetOperations()->SetNotDone();
462
463   if (theShape1 == NULL || theShape2 == NULL) return -1.0;
464
465   //Get the reference shapes
466   Handle(GEOM_Object) aShape1 = GetOperations()->GetEngine()->GetObject
467     (theShape1->GetStudyID(), theShape1->GetEntry());
468   Handle(GEOM_Object) aShape2 = GetOperations()->GetEngine()->GetObject
469     (theShape2->GetStudyID(), theShape2->GetEntry());
470
471   if (aShape1.IsNull() || aShape2.IsNull()) return -1.0;
472
473   // Get the angle
474   return GetOperations()->GetAngle(aShape1, aShape2);
475 }