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