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