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