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