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