Salome HOME
Fixed 2 fatal error at 3d sketcher dialog opening and when using it.
[modules/geom.git] / src / GEOM_I / GEOM_IHealingOperations_i.cc
1 // Copyright (C) 2007-2012  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 #include <Basics_Utils.hxx>
34
35 #include <TColStd_HSequenceOfTransient.hxx>
36
37 //=============================================================================
38 /*!
39  *   constructor:
40  */
41 //=============================================================================
42
43 GEOM_IHealingOperations_i::GEOM_IHealingOperations_i (PortableServer::POA_ptr thePOA,
44                                                       GEOM::GEOM_Gen_ptr theEngine,
45                                                       ::GEOMImpl_IHealingOperations* theImpl)
46 :GEOM_IOperations_i(thePOA, theEngine, theImpl)
47 {
48   MESSAGE("GEOM_IHealingOperations_i::GEOM_IHealingOperations_i");
49 }
50
51 //=============================================================================
52 /*!
53  *  destructor
54  */
55 //=============================================================================
56
57 GEOM_IHealingOperations_i::~GEOM_IHealingOperations_i()
58 {
59   MESSAGE("GEOM_IHealingOperations_i::~GEOM_IHealingOperations_i");
60 }
61
62 //=============================================================================
63 /*!
64  *  Convert
65  */
66 //=============================================================================
67 Handle(TColStd_HArray1OfInteger) GEOM_IHealingOperations_i::Convert
68                                           (const GEOM::short_array& theInArray)
69 {
70   Handle(TColStd_HArray1OfInteger) anOutArray;
71   int n = theInArray.length();
72   if ( n <= 0 )
73     return anOutArray;
74   anOutArray = new TColStd_HArray1OfInteger( 1, n );
75   for (int i = 0; i < n; i++)
76     anOutArray->SetValue( i+1, theInArray[i] );
77   return anOutArray;
78 }
79
80 //=============================================================================
81 /*!
82  *  Convert
83  */
84 //=============================================================================
85 Handle(TColStd_HArray1OfExtendedString) GEOM_IHealingOperations_i::Convert
86                                          (const GEOM::string_array& theInArray)
87 {
88   Handle(TColStd_HArray1OfExtendedString) anOutArray;
89   int n = theInArray.length();
90   if ( n <= 0 )
91     return anOutArray;
92   anOutArray = new TColStd_HArray1OfExtendedString( 1, n );
93   char* str;
94   for ( int i = 0; i < n; i++ )
95   {
96     str = CORBA::string_dup( theInArray[i] );
97     anOutArray->SetValue( i+1, TCollection_ExtendedString( str ) );
98   }
99   return anOutArray;
100 }
101
102 //=============================================================================
103 /*!
104  *  ProcessShape
105  */
106 //=============================================================================
107 GEOM::GEOM_Object_ptr GEOM_IHealingOperations_i::ProcessShape (GEOM::GEOM_Object_ptr theObject,
108                                                                const GEOM::string_array& theOperations,
109                                                                const GEOM::string_array& theParams,
110                                                                const GEOM::string_array& theValues)
111 {
112   Kernel_Utils::Localizer loc;
113
114   GEOM::GEOM_Object_var aGEOMObject;
115
116   // Set a not done flag
117   GetOperations()->SetNotDone();
118
119   // Check if theOperations has more than 0 elements and theParams and theValues have the same length
120   //if (theOperations.length() <= 0 || theParams.length() != theValues.length())
121   //  return aGEOMObject._retn();
122
123   // Get the object itself
124   Handle(GEOM_Object) anObject = GetObjectImpl(theObject);
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   std::list<std::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     std::list<std::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   std::list<std::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       std::list<std::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   // Get the object itself
233   Handle(GEOM_Object) anObject = GetObjectImpl(theObject);
234   if (anObject.IsNull())
235     return aGEOMObject._retn();
236
237   // if theFaces is empty - it's OK, it means that ALL faces must be removed
238
239   // Perform
240   Handle(GEOM_Object) aNewObject =
241     GetOperations()->SuppressFaces( anObject, Convert( theFaces ) );
242   if (!GetOperations()->IsDone() || aNewObject.IsNull())
243     return aGEOMObject._retn();
244
245   return  GetObject( aNewObject );
246 }
247
248 //=============================================================================
249 /*!
250  *  CloseContour
251  */
252 //=============================================================================
253 GEOM::GEOM_Object_ptr GEOM_IHealingOperations_i::CloseContour (GEOM::GEOM_Object_ptr theObject,
254                                                                const GEOM::short_array& theWires,
255                                                                CORBA::Boolean isCommonVertex)
256 {
257   GEOM::GEOM_Object_var aGEOMObject;
258
259   // Set a not done flag
260   GetOperations()->SetNotDone();
261
262   // Get the object itself
263   Handle(GEOM_Object) anObject = GetObjectImpl(theObject);
264   if (anObject.IsNull())
265     return aGEOMObject._retn();
266
267   // Perform
268   Handle(GEOM_Object) aNewObject =
269     GetOperations()->CloseContour( anObject, Convert( theWires ), isCommonVertex );
270   if (!GetOperations()->IsDone() || aNewObject.IsNull())
271     return aGEOMObject._retn();
272
273   return GetObject(aNewObject);
274 }
275
276 //=============================================================================
277 /*!
278  *  RemoveIntWires
279  */
280 //=============================================================================
281 GEOM::GEOM_Object_ptr GEOM_IHealingOperations_i::RemoveIntWires (GEOM::GEOM_Object_ptr theObject,
282                                                                  const GEOM::short_array& theWires)
283 {
284   GEOM::GEOM_Object_var aGEOMObject;
285
286   // Set a not done flag
287   GetOperations()->SetNotDone();
288
289   // Get the object itself
290   Handle(GEOM_Object) anObject = GetObjectImpl(theObject);
291   if (anObject.IsNull())
292     return aGEOMObject._retn();
293
294   // if theWires is empty - it's OK, it means that ALL wires should be removed
295
296   // Perform
297   Handle(GEOM_Object) aNewObject =
298     GetOperations()->RemoveIntWires( anObject, Convert( theWires ) );
299   if (!GetOperations()->IsDone() || aNewObject.IsNull())
300     return aGEOMObject._retn();
301
302   return GetObject(aNewObject);
303 }
304
305 //=============================================================================
306 /*!
307  *  FillHoles
308  */
309 //=============================================================================
310 GEOM::GEOM_Object_ptr GEOM_IHealingOperations_i::FillHoles (GEOM::GEOM_Object_ptr theObject,
311                                                             const GEOM::short_array& theWires)
312 {
313   GEOM::GEOM_Object_var aGEOMObject;
314
315   // Set a not done flag
316   GetOperations()->SetNotDone();
317
318   // Get the object itself
319   Handle(GEOM_Object) anObject = GetObjectImpl(theObject);
320   if (anObject.IsNull())
321     return aGEOMObject._retn();
322
323   // if theWires is empty - it's OK, it means that ALL wires should be removed
324
325   // Perform
326   Handle(GEOM_Object) aNewObject =
327     GetOperations()->FillHoles( anObject, Convert( theWires ) );
328   if (!GetOperations()->IsDone() || aNewObject.IsNull())
329     return aGEOMObject._retn();
330
331   return GetObject(aNewObject);
332 }
333
334 //=============================================================================
335 /*!
336  *  Sew
337  */
338 //=============================================================================
339 GEOM::GEOM_Object_ptr GEOM_IHealingOperations_i::Sew (GEOM::GEOM_Object_ptr theObject,
340                                                       CORBA::Double theTolerance)
341 {
342   GEOM::GEOM_Object_var aGEOMObject;
343
344   // Set a not done flag
345   GetOperations()->SetNotDone();
346
347   // Check parameters
348   if (theTolerance < 0)
349     return aGEOMObject._retn();
350
351   // Get the object itself
352   Handle(GEOM_Object) anObject = GetObjectImpl(theObject);
353   if (anObject.IsNull())
354     return aGEOMObject._retn();
355
356   // Perform
357   Handle(GEOM_Object) aNewObject =
358     GetOperations()->Sew( anObject, theTolerance );
359   if (!GetOperations()->IsDone() || aNewObject.IsNull())
360     return aGEOMObject._retn();
361
362   return GetObject(aNewObject);
363 }
364
365 //=============================================================================
366 /*!
367  *  DivideEdge
368  */
369 //=============================================================================
370 GEOM::GEOM_Object_ptr GEOM_IHealingOperations_i::DivideEdge (GEOM::GEOM_Object_ptr theObject,
371                                                              CORBA::Short theIndex,
372                                                              CORBA::Double theValue,
373                                                              CORBA::Boolean isByParameter)
374 {
375   GEOM::GEOM_Object_var aGEOMObject;
376
377   // Set a not done flag
378   GetOperations()->SetNotDone();
379
380   // Check parameters
381   if (theValue < 0 || theValue > 1)
382     return aGEOMObject._retn();
383
384   // Get the object itself
385   Handle(GEOM_Object) anObject = GetObjectImpl(theObject);
386   if (anObject.IsNull())
387     return aGEOMObject._retn();
388
389   // Perform
390   Handle(GEOM_Object) aNewObject =
391     GetOperations()->DivideEdge( anObject, theIndex, theValue, isByParameter );
392   if (!GetOperations()->IsDone() || aNewObject.IsNull())
393     return aGEOMObject._retn();
394
395   return GetObject(aNewObject);
396 }
397
398 //=============================================================================
399 /*!
400  *  FuseCollinearEdgesWithinWire
401  */
402 //=============================================================================
403 GEOM::GEOM_Object_ptr GEOM_IHealingOperations_i::FuseCollinearEdgesWithinWire
404                                           (GEOM::GEOM_Object_ptr theWire,
405                                            const GEOM::ListOfGO& theVertices)
406 {
407   GEOM::GEOM_Object_var aGEOMObject;
408
409   //Set a not done flag
410   GetOperations()->SetNotDone();
411
412   //Get the reference objects
413   Handle(GEOM_Object) aWire = GetObjectImpl(theWire);
414   if (aWire.IsNull()) return aGEOMObject._retn();
415
416   int ind, aLen;
417   std::list<Handle(GEOM_Object)> aVerts;
418   //Get the shapes
419   aLen = theVertices.length();
420   for (ind = 0; ind < aLen; ind++) {
421     Handle(GEOM_Object) aSh = GetObjectImpl(theVertices[ind]);
422     if (aSh.IsNull()) return aGEOMObject._retn();
423     aVerts.push_back(aSh);
424   }
425
426   //Perform operation
427   Handle(GEOM_Object) anObject =
428     GetOperations()->FuseCollinearEdgesWithinWire(aWire, aVerts);
429   if (!GetOperations()->IsDone() || anObject.IsNull())
430     return aGEOMObject._retn();
431
432   return GetObject(anObject);
433 }
434
435 //=============================================================================
436 /*!
437  *  GetFreeBoundary
438  */
439 //=============================================================================
440 CORBA::Boolean GEOM_IHealingOperations_i::GetFreeBoundary ( GEOM::GEOM_Object_ptr theObject,
441                                                             GEOM::ListOfGO_out theClosedWires,
442                                                             GEOM::ListOfGO_out theOpenWires )
443 {
444   theClosedWires = new GEOM::ListOfGO;
445   theOpenWires = new GEOM::ListOfGO;
446
447   // Set a not done flag
448   GetOperations()->SetNotDone();
449
450   // Get the object itself
451   Handle(GEOM_Object) anObject = GetObjectImpl(theObject);
452   if (anObject.IsNull())
453     return false;
454
455   Handle(TColStd_HSequenceOfTransient) aClosed = new TColStd_HSequenceOfTransient();
456   Handle(TColStd_HSequenceOfTransient) anOpen  = new TColStd_HSequenceOfTransient();
457   bool res = GetOperations()->GetFreeBoundary( anObject, aClosed, anOpen );
458
459   if ( !GetOperations()->IsDone() || !res )
460     return false;
461
462   int i, n = aClosed->Length();
463   theClosedWires->length( n );
464   for ( i = 1; i <= n; i++ )
465     (*theClosedWires)[i-1] = GetObject(Handle(GEOM_Object)::DownCast(aClosed->Value(i)));
466
467   n = anOpen->Length();
468   theOpenWires->length( n );
469   for ( i = 1, n = anOpen->Length(); i <= n; i++ )
470     (*theOpenWires)[i-1] = GetObject(Handle(GEOM_Object)::DownCast(anOpen->Value(i)));
471
472   return true;
473 }
474
475
476 //=============================================================================
477 /*!
478  *  ChangeOrientation
479  */
480 //=============================================================================
481 GEOM::GEOM_Object_ptr GEOM_IHealingOperations_i::ChangeOrientation (GEOM::GEOM_Object_ptr theObject)
482 {
483   GEOM::GEOM_Object_var aGEOMObject;
484
485   // Set a not done flag
486   GetOperations()->SetNotDone();
487
488   // Check parameters
489   if ( CORBA::is_nil(theObject) )
490     return aGEOMObject._retn();
491
492   aGEOMObject = GEOM::GEOM_Object::_duplicate(theObject);
493
494   // Get the object itself
495   Handle(GEOM_Object) anObject = GetObjectImpl(theObject);
496   if (anObject.IsNull())
497     return aGEOMObject._retn();
498
499   // Perform
500 //  Handle(GEOM_Object) aNewObject =
501     GetOperations()->ChangeOrientation( anObject );
502 //  if (!GetOperations()->IsDone() || aNewObject.IsNull())
503 //    return aGEOMObject._retn();
504
505   //return GetObject(aNewObject);
506   return aGEOMObject._retn();
507 }
508
509
510 //=============================================================================
511 /*!
512  *  ChangeOrientationCopy
513  */
514 //=============================================================================
515 GEOM::GEOM_Object_ptr GEOM_IHealingOperations_i::ChangeOrientationCopy (GEOM::GEOM_Object_ptr theObject)
516 {
517   GEOM::GEOM_Object_var aGEOMObject;
518
519   // Set a not done flag
520   GetOperations()->SetNotDone();
521
522   // Get the object itself
523   Handle(GEOM_Object) anObject = GetObjectImpl(theObject);
524   if (anObject.IsNull())
525     return aGEOMObject._retn();
526
527   // Perform
528   Handle(GEOM_Object) aNewObject =
529     GetOperations()->ChangeOrientationCopy( anObject );
530   if (!GetOperations()->IsDone() || aNewObject.IsNull())
531     return aGEOMObject._retn();
532
533   return GetObject(aNewObject);
534 }
535
536 //=============================================================================
537 /*!
538  *  LimitTolerance
539  */
540 //=============================================================================
541 GEOM::GEOM_Object_ptr GEOM_IHealingOperations_i::LimitTolerance (GEOM::GEOM_Object_ptr theObject,
542                                                                  CORBA::Double theTolerance)
543 {
544   GEOM::GEOM_Object_var aGEOMObject;
545
546   // Set a not done flag
547   GetOperations()->SetNotDone();
548
549   // Get the object itself
550   Handle(GEOM_Object) anObject = GetObjectImpl(theObject);
551   if (anObject.IsNull())
552     return aGEOMObject._retn();
553
554   // Perform
555   Handle(GEOM_Object) aNewObject =
556     GetOperations()->LimitTolerance(anObject, theTolerance);
557   if (!GetOperations()->IsDone() || aNewObject.IsNull())
558     return aGEOMObject._retn();
559
560   return GetObject(aNewObject);
561 }