Salome HOME
EDF 2281: Transaltions
[modules/geom.git] / src / GEOM_I / GEOM_IHealingOperations_i.cc
1 // Copyright (C) 2007-2012  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
23 #include <Standard_Stream.hxx>
24
25 #include <list>
26
27 #include "GEOM_IHealingOperations_i.hh"
28 #include "GEOM_Engine.hxx"
29 #include "GEOM_Object.hxx"
30
31 #include "utilities.h"
32 #include "OpUtil.hxx"
33 #include "Utils_ExceptHandlers.hxx"
34 #include <Basics_Utils.hxx>
35
36 #include <TColStd_HSequenceOfTransient.hxx>
37
38 //=============================================================================
39 /*!
40  *   constructor:
41  */
42 //=============================================================================
43
44 GEOM_IHealingOperations_i::GEOM_IHealingOperations_i (PortableServer::POA_ptr thePOA,
45                                                       GEOM::GEOM_Gen_ptr theEngine,
46                                                       ::GEOMImpl_IHealingOperations* theImpl)
47 :GEOM_IOperations_i(thePOA, theEngine, theImpl)
48 {
49   MESSAGE("GEOM_IHealingOperations_i::GEOM_IHealingOperations_i");
50 }
51
52 //=============================================================================
53 /*!
54  *  destructor
55  */
56 //=============================================================================
57
58 GEOM_IHealingOperations_i::~GEOM_IHealingOperations_i()
59 {
60   MESSAGE("GEOM_IHealingOperations_i::~GEOM_IHealingOperations_i");
61 }
62
63 //=============================================================================
64 /*!
65  *  Convert
66  */
67 //=============================================================================
68 Handle(TColStd_HArray1OfInteger) GEOM_IHealingOperations_i::Convert
69                                           (const GEOM::short_array& theInArray)
70 {
71   Handle(TColStd_HArray1OfInteger) anOutArray;
72   int n = theInArray.length();
73   if ( n <= 0 )
74     return anOutArray;
75   anOutArray = new TColStd_HArray1OfInteger( 1, n );
76   for (int i = 0; i < n; i++)
77     anOutArray->SetValue( i+1, theInArray[i] );
78   return anOutArray;
79 }
80
81 //=============================================================================
82 /*!
83  *  Convert
84  */
85 //=============================================================================
86 Handle(TColStd_HArray1OfExtendedString) GEOM_IHealingOperations_i::Convert
87                                          (const GEOM::string_array& theInArray)
88 {
89   Handle(TColStd_HArray1OfExtendedString) anOutArray;
90   int n = theInArray.length();
91   if ( n <= 0 )
92     return anOutArray;
93   anOutArray = new TColStd_HArray1OfExtendedString( 1, n );
94   char* str;
95   for ( int i = 0; i < n; i++ )
96   {
97     str = CORBA::string_dup( theInArray[i] );
98     anOutArray->SetValue( i+1, TCollection_ExtendedString( str ) );
99   }
100   return anOutArray;
101 }
102
103 //=============================================================================
104 /*!
105  *  ProcessShape
106  */
107 //=============================================================================
108 GEOM::GEOM_Object_ptr GEOM_IHealingOperations_i::ProcessShape (GEOM::GEOM_Object_ptr theObject,
109                                                                const GEOM::string_array& theOperations,
110                                                                const GEOM::string_array& theParams,
111                                                                const GEOM::string_array& theValues)
112 {
113   Kernel_Utils::Localizer loc;
114
115   GEOM::GEOM_Object_var aGEOMObject;
116
117   // Set a not done flag
118   GetOperations()->SetNotDone();
119
120   // Check if theOperations has more than 0 elements and theParams and theValues have the same length
121   //if (theOperations.length() <= 0 || theParams.length() != theValues.length())
122   //  return aGEOMObject._retn();
123
124   // Get the object itself
125   Handle(GEOM_Object) anObject = GetObjectImpl(theObject);
126   if (anObject.IsNull())
127     return aGEOMObject._retn();
128
129   // Perform
130   Handle(GEOM_Object) aNewObject = GetOperations()->ShapeProcess( anObject,
131     Convert( theOperations ), Convert( theParams ), Convert( theValues ) );
132   if ( !GetOperations()->IsDone() || aNewObject.IsNull() )
133     return aGEOMObject._retn();
134
135   return GetObject( aNewObject );
136 }
137
138 //=============================================================================
139 /*!
140  *  GetShapeProcessParameters
141  */
142 //=============================================================================
143 void GEOM_IHealingOperations_i::GetShapeProcessParameters(GEOM::string_array_out theOperations,
144                                                           GEOM::string_array_out theParams,
145                                                           GEOM::string_array_out theValues)
146 {
147   GEOM::string_array_var anOpArray = new GEOM::string_array();
148   GEOM::string_array_var aParArray = new GEOM::string_array();
149   GEOM::string_array_var aValArray = new GEOM::string_array();
150
151   // retrieve the values as stl-lists
152   std::list<std::string> operationsList, paramsList, valuesList;
153   GetOperations()->GetShapeProcessParameters( operationsList, paramsList, valuesList );
154   const int opSize = operationsList.size(),
155   parSize = paramsList.size(),
156   valSize = valuesList.size();
157
158   if ( opSize >= 0 && parSize >= 0 && parSize == valSize ) {
159     // allocate the CORBA arrays, sizes == returned lists' sizes
160     anOpArray->length(opSize);
161     aParArray->length(parSize);
162     aValArray->length(valSize);
163
164     // fill the local CORBA arrays with values from lists
165     std::list<std::string>::iterator opIt, parIt, valIt;
166     int i = 0;
167     for ( opIt = operationsList.begin(); opIt != operationsList.end(); i++,++opIt )
168       anOpArray[i] = CORBA::string_dup( (*opIt).c_str() );
169
170     for ( i = 0, parIt = paramsList.begin(), valIt = valuesList.begin();
171           parIt != paramsList.end(); i++, ++parIt,++valIt ) {
172       aParArray[i] = CORBA::string_dup( (*parIt).c_str() );
173       aValArray[i] = CORBA::string_dup( (*valIt).c_str() );
174     }
175   }
176
177   // initialize out-parameters with local arrays
178   theOperations = anOpArray._retn();
179   theParams = aParArray._retn();
180   theValues = aValArray._retn();
181 }
182
183 //=============================================================================
184 /*!
185  *  GetOperatorParameters
186  */
187 //=============================================================================
188 void GEOM_IHealingOperations_i::GetOperatorParameters (const char* theOperator,
189                                                        GEOM::string_array_out theParams,
190                                                        GEOM::string_array_out theValues)
191 {
192   GEOM::string_array_var aParArray = new GEOM::string_array();
193   GEOM::string_array_var aValArray = new GEOM::string_array();
194
195   // retrieve the values as stl-lists
196   std::list<std::string> paramsList, valuesList;
197   if ( GetOperations()->GetOperatorParameters( theOperator, paramsList, valuesList ) ) {
198     const int parSize = paramsList.size(), valSize = valuesList.size();
199
200     if ( parSize == valSize ) {
201       aParArray->length(parSize);
202       aValArray->length(valSize);
203
204       // fill the local CORBA arrays with values from lists
205       std::list<std::string>::iterator parIt, valIt;
206       int i;
207       for ( i = 0, parIt = paramsList.begin(), valIt = valuesList.begin();
208             parIt != paramsList.end(); i++, ++parIt,++valIt ) {
209         aParArray[i] = CORBA::string_dup( (*parIt).c_str() );
210         aValArray[i] = CORBA::string_dup( (*valIt).c_str() );
211       }
212     }
213   }
214
215   // initialize out-parameters with local arrays
216   theParams = aParArray._retn();
217   theValues = aValArray._retn();
218 }
219
220 //=============================================================================
221 /*!
222  *  SuppressFaces
223  */
224 //=============================================================================
225 GEOM::GEOM_Object_ptr GEOM_IHealingOperations_i::SuppressFaces (GEOM::GEOM_Object_ptr theObject,
226                                                                 const GEOM::short_array& theFaces)
227 {
228   GEOM::GEOM_Object_var aGEOMObject;
229
230   // Set a not done flag
231   GetOperations()->SetNotDone();
232
233   // Get the object itself
234   Handle(GEOM_Object) anObject = GetObjectImpl(theObject);
235   if (anObject.IsNull())
236     return aGEOMObject._retn();
237
238   // if theFaces is empty - it's OK, it means that ALL faces must be removed
239
240   // Perform
241   Handle(GEOM_Object) aNewObject =
242     GetOperations()->SuppressFaces( anObject, Convert( theFaces ) );
243   if (!GetOperations()->IsDone() || aNewObject.IsNull())
244     return aGEOMObject._retn();
245
246   return  GetObject( aNewObject );
247 }
248
249 //=============================================================================
250 /*!
251  *  CloseContour
252  */
253 //=============================================================================
254 GEOM::GEOM_Object_ptr GEOM_IHealingOperations_i::CloseContour (GEOM::GEOM_Object_ptr theObject,
255                                                                const GEOM::short_array& theWires,
256                                                                CORBA::Boolean isCommonVertex)
257 {
258   GEOM::GEOM_Object_var aGEOMObject;
259
260   // Set a not done flag
261   GetOperations()->SetNotDone();
262
263   // Get the object itself
264   Handle(GEOM_Object) anObject = GetObjectImpl(theObject);
265   if (anObject.IsNull())
266     return aGEOMObject._retn();
267
268   // Perform
269   Handle(GEOM_Object) aNewObject =
270     GetOperations()->CloseContour( anObject, Convert( theWires ), isCommonVertex );
271   if (!GetOperations()->IsDone() || aNewObject.IsNull())
272     return aGEOMObject._retn();
273
274   return GetObject(aNewObject);
275 }
276
277 //=============================================================================
278 /*!
279  *  RemoveIntWires
280  */
281 //=============================================================================
282 GEOM::GEOM_Object_ptr GEOM_IHealingOperations_i::RemoveIntWires (GEOM::GEOM_Object_ptr theObject,
283                                                                  const GEOM::short_array& theWires)
284 {
285   GEOM::GEOM_Object_var aGEOMObject;
286
287   // Set a not done flag
288   GetOperations()->SetNotDone();
289
290   // Get the object itself
291   Handle(GEOM_Object) anObject = GetObjectImpl(theObject);
292   if (anObject.IsNull())
293     return aGEOMObject._retn();
294
295   // if theWires is empty - it's OK, it means that ALL wires should be removed
296
297   // Perform
298   Handle(GEOM_Object) aNewObject =
299     GetOperations()->RemoveIntWires( anObject, Convert( theWires ) );
300   if (!GetOperations()->IsDone() || aNewObject.IsNull())
301     return aGEOMObject._retn();
302
303   return GetObject(aNewObject);
304 }
305
306 //=============================================================================
307 /*!
308  *  FillHoles
309  */
310 //=============================================================================
311 GEOM::GEOM_Object_ptr GEOM_IHealingOperations_i::FillHoles (GEOM::GEOM_Object_ptr theObject,
312                                                             const GEOM::short_array& theWires)
313 {
314   GEOM::GEOM_Object_var aGEOMObject;
315
316   // Set a not done flag
317   GetOperations()->SetNotDone();
318
319   // Get the object itself
320   Handle(GEOM_Object) anObject = GetObjectImpl(theObject);
321   if (anObject.IsNull())
322     return aGEOMObject._retn();
323
324   // if theWires is empty - it's OK, it means that ALL wires should be removed
325
326   // Perform
327   Handle(GEOM_Object) aNewObject =
328     GetOperations()->FillHoles( anObject, Convert( theWires ) );
329   if (!GetOperations()->IsDone() || aNewObject.IsNull())
330     return aGEOMObject._retn();
331
332   return GetObject(aNewObject);
333 }
334
335 //=============================================================================
336 /*!
337  *  Sew
338  */
339 //=============================================================================
340 GEOM::GEOM_Object_ptr GEOM_IHealingOperations_i::Sew (GEOM::GEOM_Object_ptr theObject,
341                                                       CORBA::Double theTolerance)
342 {
343   GEOM::GEOM_Object_var aGEOMObject;
344
345   // Set a not done flag
346   GetOperations()->SetNotDone();
347
348   // Check parameters
349   if (theTolerance < 0)
350     return aGEOMObject._retn();
351
352   // Get the object itself
353   Handle(GEOM_Object) anObject = GetObjectImpl(theObject);
354   if (anObject.IsNull())
355     return aGEOMObject._retn();
356
357   // Perform
358   Handle(GEOM_Object) aNewObject =
359     GetOperations()->Sew( anObject, theTolerance );
360   if (!GetOperations()->IsDone() || aNewObject.IsNull())
361     return aGEOMObject._retn();
362
363   return GetObject(aNewObject);
364 }
365
366 //=============================================================================
367 /*!
368  *  DivideEdge
369  */
370 //=============================================================================
371 GEOM::GEOM_Object_ptr GEOM_IHealingOperations_i::DivideEdge (GEOM::GEOM_Object_ptr theObject,
372                                                              CORBA::Short theIndex,
373                                                              CORBA::Double theValue,
374                                                              CORBA::Boolean isByParameter)
375 {
376   GEOM::GEOM_Object_var aGEOMObject;
377
378   // Set a not done flag
379   GetOperations()->SetNotDone();
380
381   // Check parameters
382   if (theValue < 0 || theValue > 1)
383     return aGEOMObject._retn();
384
385   // Get the object itself
386   Handle(GEOM_Object) anObject = GetObjectImpl(theObject);
387   if (anObject.IsNull())
388     return aGEOMObject._retn();
389
390   // Perform
391   Handle(GEOM_Object) aNewObject =
392     GetOperations()->DivideEdge( anObject, theIndex, theValue, isByParameter );
393   if (!GetOperations()->IsDone() || aNewObject.IsNull())
394     return aGEOMObject._retn();
395
396   return GetObject(aNewObject);
397 }
398
399 //=============================================================================
400 /*!
401  *  GetFreeBoundary
402  */
403 //=============================================================================
404 CORBA::Boolean GEOM_IHealingOperations_i::GetFreeBoundary ( GEOM::GEOM_Object_ptr theObject,
405                                                             GEOM::ListOfGO_out theClosedWires,
406                                                             GEOM::ListOfGO_out theOpenWires )
407 {
408   theClosedWires = new GEOM::ListOfGO;
409   theOpenWires = new GEOM::ListOfGO;
410
411   // Set a not done flag
412   GetOperations()->SetNotDone();
413
414   // Get the object itself
415   Handle(GEOM_Object) anObject = GetObjectImpl(theObject);
416   if (anObject.IsNull())
417     return false;
418
419   Handle(TColStd_HSequenceOfTransient) aClosed = new TColStd_HSequenceOfTransient();
420   Handle(TColStd_HSequenceOfTransient) anOpen  = new TColStd_HSequenceOfTransient();
421   bool res = GetOperations()->GetFreeBoundary( anObject, aClosed, anOpen );
422
423   if ( !GetOperations()->IsDone() || !res )
424     return false;
425
426   int i, n = aClosed->Length();
427   theClosedWires->length( n );
428   for ( i = 1; i <= n; i++ )
429     (*theClosedWires)[i-1] = GetObject(Handle(GEOM_Object)::DownCast(aClosed->Value(i)));
430
431   n = anOpen->Length();
432   theOpenWires->length( n );
433   for ( i = 1, n = anOpen->Length(); i <= n; i++ )
434     (*theOpenWires)[i-1] = GetObject(Handle(GEOM_Object)::DownCast(anOpen->Value(i)));
435
436   return true;
437 }
438
439
440 //=============================================================================
441 /*!
442  *  ChangeOrientation
443  */
444 //=============================================================================
445 GEOM::GEOM_Object_ptr GEOM_IHealingOperations_i::ChangeOrientation (GEOM::GEOM_Object_ptr theObject)
446 {
447   GEOM::GEOM_Object_var aGEOMObject;
448
449   // Set a not done flag
450   GetOperations()->SetNotDone();
451
452   // Check parameters
453   if ( CORBA::is_nil(theObject) )
454     return aGEOMObject._retn();
455
456   aGEOMObject = GEOM::GEOM_Object::_duplicate(theObject);
457
458   // Get the object itself
459   Handle(GEOM_Object) anObject = GetObjectImpl(theObject);
460   if (anObject.IsNull())
461     return aGEOMObject._retn();
462
463   // Perform
464 //  Handle(GEOM_Object) aNewObject =
465     GetOperations()->ChangeOrientation( anObject );
466 //  if (!GetOperations()->IsDone() || aNewObject.IsNull())
467 //    return aGEOMObject._retn();
468
469   //return GetObject(aNewObject);
470   return aGEOMObject._retn();
471 }
472
473
474 //=============================================================================
475 /*!
476  *  ChangeOrientationCopy
477  */
478 //=============================================================================
479 GEOM::GEOM_Object_ptr GEOM_IHealingOperations_i::ChangeOrientationCopy (GEOM::GEOM_Object_ptr theObject)
480 {
481   GEOM::GEOM_Object_var aGEOMObject;
482
483   // Set a not done flag
484   GetOperations()->SetNotDone();
485
486   // Get the object itself
487   Handle(GEOM_Object) anObject = GetObjectImpl(theObject);
488   if (anObject.IsNull())
489     return aGEOMObject._retn();
490
491   // Perform
492   Handle(GEOM_Object) aNewObject =
493     GetOperations()->ChangeOrientationCopy( anObject );
494   if (!GetOperations()->IsDone() || aNewObject.IsNull())
495     return aGEOMObject._retn();
496
497   return GetObject(aNewObject);
498 }
499
500 //=============================================================================
501 /*!
502  *  LimitTolerance
503  */
504 //=============================================================================
505 GEOM::GEOM_Object_ptr GEOM_IHealingOperations_i::LimitTolerance (GEOM::GEOM_Object_ptr theObject,
506                                                                  CORBA::Double theTolerance)
507 {
508   GEOM::GEOM_Object_var aGEOMObject;
509
510   // Set a not done flag
511   GetOperations()->SetNotDone();
512
513   // Get the object itself
514   Handle(GEOM_Object) anObject = GetObjectImpl(theObject);
515   if (anObject.IsNull())
516     return aGEOMObject._retn();
517
518   // Perform
519   Handle(GEOM_Object) aNewObject =
520     GetOperations()->LimitTolerance(anObject, theTolerance);
521   if (!GetOperations()->IsDone() || aNewObject.IsNull())
522     return aGEOMObject._retn();
523
524   return GetObject(aNewObject);
525 }