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