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