Salome HOME
Synchronize adm files
[modules/geom.git] / src / GEOM_I / GEOM_IHealingOperations_i.cc
1 // Copyright (C) 2007-2014  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, or (at your option) any later version.
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
23 #include <Standard_Stream.hxx>
24
25 #include <list>
26
27 #include "GEOM_IHealingOperations_i.hh"
28 #include "GEOM_Engine.hxx"
29 #include "GEOM_Object.hxx"
30
31 #include "utilities.h"
32 #include "OpUtil.hxx"
33 #include "Utils_ExceptHandlers.hxx"
34 #include <Basics_Utils.hxx>
35
36 #include <TColStd_HSequenceOfTransient.hxx>
37
38 //=============================================================================
39 /*!
40  *   constructor:
41  */
42 //=============================================================================
43
44 GEOM_IHealingOperations_i::GEOM_IHealingOperations_i (PortableServer::POA_ptr thePOA,
45                                                       GEOM::GEOM_Gen_ptr theEngine,
46                                                       ::GEOMImpl_IHealingOperations* theImpl)
47 :GEOM_IOperations_i(thePOA, theEngine, theImpl)
48 {
49   MESSAGE("GEOM_IHealingOperations_i::GEOM_IHealingOperations_i");
50 }
51
52 //=============================================================================
53 /*!
54  *  destructor
55  */
56 //=============================================================================
57
58 GEOM_IHealingOperations_i::~GEOM_IHealingOperations_i()
59 {
60   MESSAGE("GEOM_IHealingOperations_i::~GEOM_IHealingOperations_i");
61 }
62
63 //=============================================================================
64 /*!
65  *  Convert
66  */
67 //=============================================================================
68 Handle(TColStd_HArray1OfInteger) GEOM_IHealingOperations_i::Convert
69                                           (const GEOM::short_array& theInArray)
70 {
71   Handle(TColStd_HArray1OfInteger) anOutArray;
72   int n = theInArray.length();
73   if ( n <= 0 )
74     return anOutArray;
75   anOutArray = new TColStd_HArray1OfInteger( 1, n );
76   for (int i = 0; i < n; i++)
77     anOutArray->SetValue( i+1, theInArray[i] );
78   return anOutArray;
79 }
80
81 //=============================================================================
82 /*!
83  *  Convert
84  */
85 //=============================================================================
86 Handle(TColStd_HArray1OfExtendedString) GEOM_IHealingOperations_i::Convert
87                                          (const GEOM::string_array& theInArray)
88 {
89   Handle(TColStd_HArray1OfExtendedString) anOutArray;
90   int n = theInArray.length();
91   if ( n <= 0 )
92     return anOutArray;
93   anOutArray = new TColStd_HArray1OfExtendedString( 1, n );
94   for ( int i = 0; i < n; i++ )
95     anOutArray->SetValue( i+1, TCollection_ExtendedString( theInArray[i].in() ) );
96
97   return anOutArray;
98 }
99
100 //=============================================================================
101 /*!
102  *  ProcessShape
103  */
104 //=============================================================================
105 GEOM::GEOM_Object_ptr GEOM_IHealingOperations_i::ProcessShape (GEOM::GEOM_Object_ptr theObject,
106                                                                const GEOM::string_array& theOperations,
107                                                                const GEOM::string_array& theParams,
108                                                                const GEOM::string_array& theValues)
109 {
110   Kernel_Utils::Localizer loc;
111
112   GEOM::GEOM_Object_var aGEOMObject;
113
114   // Set a not done flag
115   GetOperations()->SetNotDone();
116
117   // Check if theOperations has more than 0 elements and theParams and theValues have the same length
118   //if (theOperations.length() <= 0 || theParams.length() != theValues.length())
119   //  return aGEOMObject._retn();
120
121   // Get the object itself
122   Handle(GEOM_Object) anObject = GetObjectImpl(theObject);
123   if (anObject.IsNull())
124     return aGEOMObject._retn();
125
126   // Perform
127   Handle(GEOM_Object) aNewObject = GetOperations()->ShapeProcess( anObject,
128     Convert( theOperations ), Convert( theParams ), Convert( theValues ) );
129   if ( !GetOperations()->IsDone() || aNewObject.IsNull() )
130     return aGEOMObject._retn();
131
132   return GetObject( aNewObject );
133 }
134
135 //=============================================================================
136 /*!
137  *  GetShapeProcessParameters
138  */
139 //=============================================================================
140 void GEOM_IHealingOperations_i::GetShapeProcessParameters(GEOM::string_array_out theOperations,
141                                                           GEOM::string_array_out theParams,
142                                                           GEOM::string_array_out theValues)
143 {
144   GEOM::string_array_var anOpArray = new GEOM::string_array();
145   GEOM::string_array_var aParArray = new GEOM::string_array();
146   GEOM::string_array_var aValArray = new GEOM::string_array();
147
148   // retrieve the values as stl-lists
149   std::list<std::string> operationsList, paramsList, valuesList;
150   GetOperations()->GetShapeProcessParameters( operationsList, paramsList, valuesList );
151   const int opSize = operationsList.size(),
152   parSize = paramsList.size(),
153   valSize = valuesList.size();
154
155   if ( opSize >= 0 && parSize >= 0 && parSize == valSize ) {
156     // allocate the CORBA arrays, sizes == returned lists' sizes
157     anOpArray->length(opSize);
158     aParArray->length(parSize);
159     aValArray->length(valSize);
160
161     // fill the local CORBA arrays with values from lists
162     std::list<std::string>::iterator opIt, parIt, valIt;
163     int i = 0;
164     for ( opIt = operationsList.begin(); opIt != operationsList.end(); i++,++opIt )
165       anOpArray[i] = CORBA::string_dup( (*opIt).c_str() );
166
167     for ( i = 0, parIt = paramsList.begin(), valIt = valuesList.begin();
168           parIt != paramsList.end(); i++, ++parIt,++valIt ) {
169       aParArray[i] = CORBA::string_dup( (*parIt).c_str() );
170       aValArray[i] = CORBA::string_dup( (*valIt).c_str() );
171     }
172   }
173
174   // initialize out-parameters with local arrays
175   theOperations = anOpArray._retn();
176   theParams = aParArray._retn();
177   theValues = aValArray._retn();
178 }
179
180 //=============================================================================
181 /*!
182  *  GetOperatorParameters
183  */
184 //=============================================================================
185 void GEOM_IHealingOperations_i::GetOperatorParameters (const char* theOperator,
186                                                        GEOM::string_array_out theParams,
187                                                        GEOM::string_array_out theValues)
188 {
189   GEOM::string_array_var aParArray = new GEOM::string_array();
190   GEOM::string_array_var aValArray = new GEOM::string_array();
191
192   // retrieve the values as stl-lists
193   std::list<std::string> paramsList, valuesList;
194   if ( GetOperations()->GetOperatorParameters( theOperator, paramsList, valuesList ) ) {
195     const int parSize = paramsList.size(), valSize = valuesList.size();
196
197     if ( parSize == valSize ) {
198       aParArray->length(parSize);
199       aValArray->length(valSize);
200
201       // fill the local CORBA arrays with values from lists
202       std::list<std::string>::iterator parIt, valIt;
203       int i;
204       for ( i = 0, parIt = paramsList.begin(), valIt = valuesList.begin();
205             parIt != paramsList.end(); i++, ++parIt,++valIt ) {
206         aParArray[i] = CORBA::string_dup( (*parIt).c_str() );
207         aValArray[i] = CORBA::string_dup( (*valIt).c_str() );
208       }
209     }
210   }
211
212   // initialize out-parameters with local arrays
213   theParams = aParArray._retn();
214   theValues = aValArray._retn();
215 }
216
217 //=============================================================================
218 /*!
219  *  SuppressFaces
220  */
221 //=============================================================================
222 GEOM::GEOM_Object_ptr GEOM_IHealingOperations_i::SuppressFaces (GEOM::GEOM_Object_ptr theObject,
223                                                                 const GEOM::short_array& theFaces)
224 {
225   GEOM::GEOM_Object_var aGEOMObject;
226
227   // Set a not done flag
228   GetOperations()->SetNotDone();
229
230   // Get the object itself
231   Handle(GEOM_Object) anObject = GetObjectImpl(theObject);
232   if (anObject.IsNull())
233     return aGEOMObject._retn();
234
235   // if theFaces is empty - it's OK, it means that ALL faces must be removed
236
237   // Perform
238   Handle(GEOM_Object) aNewObject =
239     GetOperations()->SuppressFaces( anObject, Convert( theFaces ) );
240   if (!GetOperations()->IsDone() || aNewObject.IsNull())
241     return aGEOMObject._retn();
242
243   return  GetObject( aNewObject );
244 }
245
246 //=============================================================================
247 /*!
248  *  CloseContour
249  */
250 //=============================================================================
251 GEOM::GEOM_Object_ptr GEOM_IHealingOperations_i::CloseContour (GEOM::GEOM_Object_ptr theObject,
252                                                                const GEOM::short_array& theWires,
253                                                                CORBA::Boolean isCommonVertex)
254 {
255   GEOM::GEOM_Object_var aGEOMObject;
256
257   // Set a not done flag
258   GetOperations()->SetNotDone();
259
260   // Get the object itself
261   Handle(GEOM_Object) anObject = GetObjectImpl(theObject);
262   if (anObject.IsNull())
263     return aGEOMObject._retn();
264
265   // Perform
266   Handle(GEOM_Object) aNewObject =
267     GetOperations()->CloseContour( anObject, Convert( theWires ), isCommonVertex );
268   if (!GetOperations()->IsDone() || aNewObject.IsNull())
269     return aGEOMObject._retn();
270
271   return GetObject(aNewObject);
272 }
273
274 //=============================================================================
275 /*!
276  *  RemoveIntWires
277  */
278 //=============================================================================
279 GEOM::GEOM_Object_ptr GEOM_IHealingOperations_i::RemoveIntWires (GEOM::GEOM_Object_ptr theObject,
280                                                                  const GEOM::short_array& theWires)
281 {
282   GEOM::GEOM_Object_var aGEOMObject;
283
284   // Set a not done flag
285   GetOperations()->SetNotDone();
286
287   // Get the object itself
288   Handle(GEOM_Object) anObject = GetObjectImpl(theObject);
289   if (anObject.IsNull())
290     return aGEOMObject._retn();
291
292   // if theWires is empty - it's OK, it means that ALL wires should be removed
293
294   // Perform
295   Handle(GEOM_Object) aNewObject =
296     GetOperations()->RemoveIntWires( anObject, Convert( theWires ) );
297   if (!GetOperations()->IsDone() || aNewObject.IsNull())
298     return aGEOMObject._retn();
299
300   return GetObject(aNewObject);
301 }
302
303 //=============================================================================
304 /*!
305  *  FillHoles
306  */
307 //=============================================================================
308 GEOM::GEOM_Object_ptr GEOM_IHealingOperations_i::FillHoles (GEOM::GEOM_Object_ptr theObject,
309                                                             const GEOM::short_array& theWires)
310 {
311   GEOM::GEOM_Object_var aGEOMObject;
312
313   // Set a not done flag
314   GetOperations()->SetNotDone();
315
316   // Get the object itself
317   Handle(GEOM_Object) anObject = GetObjectImpl(theObject);
318   if (anObject.IsNull())
319     return aGEOMObject._retn();
320
321   // if theWires is empty - it's OK, it means that ALL wires should be removed
322
323   // Perform
324   Handle(GEOM_Object) aNewObject =
325     GetOperations()->FillHoles( anObject, Convert( theWires ) );
326   if (!GetOperations()->IsDone() || aNewObject.IsNull())
327     return aGEOMObject._retn();
328
329   return GetObject(aNewObject);
330 }
331
332 //=============================================================================
333 /*!
334  *  Sew
335  */
336 //=============================================================================
337 GEOM::GEOM_Object_ptr GEOM_IHealingOperations_i::Sew (GEOM::GEOM_Object_ptr theObject,
338                                                       CORBA::Double theTolerance)
339 {
340   GEOM::GEOM_Object_var aGEOMObject;
341
342   // Set a not done flag
343   GetOperations()->SetNotDone();
344
345   // Check parameters
346   if (theTolerance < 0)
347     return aGEOMObject._retn();
348
349   // Get the object itself
350   Handle(GEOM_Object) anObject = GetObjectImpl(theObject);
351   if (anObject.IsNull())
352     return aGEOMObject._retn();
353
354   // Perform
355   Handle(GEOM_Object) aNewObject =
356     GetOperations()->Sew( anObject, theTolerance, false );
357   if (!GetOperations()->IsDone() || aNewObject.IsNull())
358     return aGEOMObject._retn();
359
360   return GetObject(aNewObject);
361 }
362
363 //=============================================================================
364 /*!
365  *  SewAllowNonManifold
366  */
367 //=============================================================================
368 GEOM::GEOM_Object_ptr GEOM_IHealingOperations_i::SewAllowNonManifold (GEOM::GEOM_Object_ptr theObject,
369                                                                       CORBA::Double theTolerance)
370 {
371   GEOM::GEOM_Object_var aGEOMObject;
372
373   // Set a not done flag
374   GetOperations()->SetNotDone();
375
376   // Check parameters
377   if (theTolerance < 0)
378     return aGEOMObject._retn();
379
380   // Get the object itself
381   Handle(GEOM_Object) anObject = GetObjectImpl(theObject);
382   if (anObject.IsNull())
383     return aGEOMObject._retn();
384
385   // Perform
386   Handle(GEOM_Object) aNewObject =
387     GetOperations()->Sew( anObject, theTolerance, true );
388   if (!GetOperations()->IsDone() || aNewObject.IsNull())
389     return aGEOMObject._retn();
390
391   return GetObject(aNewObject);
392 }
393
394 //=============================================================================
395 /*!
396  *  RemoveInternalFaces
397  */
398 //=============================================================================
399 GEOM::GEOM_Object_ptr GEOM_IHealingOperations_i::RemoveInternalFaces (GEOM::GEOM_Object_ptr theCompound)
400 {
401   GEOM::GEOM_Object_var aGEOMObject;
402
403   // Set a not done flag
404   GetOperations()->SetNotDone();
405
406   // Get the object
407   Handle(GEOM_Object) anObject = GetObjectImpl(theCompound);
408   if (anObject.IsNull())
409     return aGEOMObject._retn();
410
411   // Perform
412   Handle(GEOM_Object) aNewObject = GetOperations()->RemoveInternalFaces(anObject);
413   if (!GetOperations()->IsDone() || aNewObject.IsNull())
414     return aGEOMObject._retn();
415
416   return GetObject(aNewObject);
417 }
418
419 //=============================================================================
420 /*!
421  *  DivideEdge
422  */
423 //=============================================================================
424 GEOM::GEOM_Object_ptr GEOM_IHealingOperations_i::DivideEdge (GEOM::GEOM_Object_ptr theObject,
425                                                              CORBA::Short theIndex,
426                                                              CORBA::Double theValue,
427                                                              CORBA::Boolean isByParameter)
428 {
429   GEOM::GEOM_Object_var aGEOMObject;
430
431   // Set a not done flag
432   GetOperations()->SetNotDone();
433
434   // Check parameters
435   if (theValue < 0 || theValue > 1)
436     return aGEOMObject._retn();
437
438   // Get the object itself
439   Handle(GEOM_Object) anObject = GetObjectImpl(theObject);
440   if (anObject.IsNull())
441     return aGEOMObject._retn();
442
443   // Perform
444   Handle(GEOM_Object) aNewObject =
445     GetOperations()->DivideEdge( anObject, theIndex, theValue, isByParameter );
446   if (!GetOperations()->IsDone() || aNewObject.IsNull())
447     return aGEOMObject._retn();
448
449   return GetObject(aNewObject);
450 }
451
452 //=============================================================================
453 /*!
454  *  FuseCollinearEdgesWithinWire
455  */
456 //=============================================================================
457 GEOM::GEOM_Object_ptr GEOM_IHealingOperations_i::FuseCollinearEdgesWithinWire
458                                           (GEOM::GEOM_Object_ptr theWire,
459                                            const GEOM::ListOfGO& theVertices)
460 {
461   GEOM::GEOM_Object_var aGEOMObject;
462
463   //Set a not done flag
464   GetOperations()->SetNotDone();
465
466   //Get the reference objects
467   Handle(GEOM_Object) aWire = GetObjectImpl(theWire);
468   if (aWire.IsNull()) return aGEOMObject._retn();
469
470   int ind, aLen;
471   std::list<Handle(GEOM_Object)> aVerts;
472   //Get the shapes
473   aLen = theVertices.length();
474   for (ind = 0; ind < aLen; ind++) {
475     Handle(GEOM_Object) aSh = GetObjectImpl(theVertices[ind]);
476     if (aSh.IsNull()) return aGEOMObject._retn();
477     aVerts.push_back(aSh);
478   }
479
480   //Perform operation
481   Handle(GEOM_Object) anObject =
482     GetOperations()->FuseCollinearEdgesWithinWire(aWire, aVerts);
483   if (!GetOperations()->IsDone() || anObject.IsNull())
484     return aGEOMObject._retn();
485
486   return GetObject(anObject);
487 }
488
489 //=============================================================================
490 /*!
491  *  GetFreeBoundary
492  */
493 //=============================================================================
494 CORBA::Boolean GEOM_IHealingOperations_i::GetFreeBoundary ( GEOM::GEOM_Object_ptr theObject,
495                                                             GEOM::ListOfGO_out theClosedWires,
496                                                             GEOM::ListOfGO_out theOpenWires )
497 {
498   theClosedWires = new GEOM::ListOfGO;
499   theOpenWires = new GEOM::ListOfGO;
500
501   // Set a not done flag
502   GetOperations()->SetNotDone();
503
504   // Get the object itself
505   Handle(GEOM_Object) anObject = GetObjectImpl(theObject);
506   if (anObject.IsNull())
507     return false;
508
509   Handle(TColStd_HSequenceOfTransient) aClosed = new TColStd_HSequenceOfTransient();
510   Handle(TColStd_HSequenceOfTransient) anOpen  = new TColStd_HSequenceOfTransient();
511   bool res = GetOperations()->GetFreeBoundary( anObject, aClosed, anOpen );
512
513   if ( !GetOperations()->IsDone() || !res )
514     return false;
515
516   int i, n = aClosed->Length();
517   theClosedWires->length( n );
518   for ( i = 1; i <= n; i++ )
519     (*theClosedWires)[i-1] = GetObject(Handle(GEOM_Object)::DownCast(aClosed->Value(i)));
520
521   n = anOpen->Length();
522   theOpenWires->length( n );
523   for ( i = 1, n = anOpen->Length(); i <= n; i++ )
524     (*theOpenWires)[i-1] = GetObject(Handle(GEOM_Object)::DownCast(anOpen->Value(i)));
525
526   return true;
527 }
528
529
530 //=============================================================================
531 /*!
532  *  ChangeOrientation
533  */
534 //=============================================================================
535 GEOM::GEOM_Object_ptr GEOM_IHealingOperations_i::ChangeOrientation (GEOM::GEOM_Object_ptr theObject)
536 {
537   GEOM::GEOM_Object_var aGEOMObject;
538
539   // Set a not done flag
540   GetOperations()->SetNotDone();
541
542   // Check parameters
543   if ( CORBA::is_nil(theObject) )
544     return aGEOMObject._retn();
545
546   aGEOMObject = GEOM::GEOM_Object::_duplicate(theObject);
547
548   // Get the object itself
549   Handle(GEOM_Object) anObject = GetObjectImpl(theObject);
550   if (anObject.IsNull())
551     return aGEOMObject._retn();
552
553   // Perform
554 //  Handle(GEOM_Object) aNewObject =
555     GetOperations()->ChangeOrientation( anObject );
556 //  if (!GetOperations()->IsDone() || aNewObject.IsNull())
557 //    return aGEOMObject._retn();
558
559   //return GetObject(aNewObject);
560   return aGEOMObject._retn();
561 }
562
563
564 //=============================================================================
565 /*!
566  *  ChangeOrientationCopy
567  */
568 //=============================================================================
569 GEOM::GEOM_Object_ptr GEOM_IHealingOperations_i::ChangeOrientationCopy (GEOM::GEOM_Object_ptr theObject)
570 {
571   GEOM::GEOM_Object_var aGEOMObject;
572
573   // Set a not done flag
574   GetOperations()->SetNotDone();
575
576   // Get the object itself
577   Handle(GEOM_Object) anObject = GetObjectImpl(theObject);
578   if (anObject.IsNull())
579     return aGEOMObject._retn();
580
581   // Perform
582   Handle(GEOM_Object) aNewObject =
583     GetOperations()->ChangeOrientationCopy( anObject );
584   if (!GetOperations()->IsDone() || aNewObject.IsNull())
585     return aGEOMObject._retn();
586
587   return GetObject(aNewObject);
588 }
589
590 //=============================================================================
591 /*!
592  *  LimitTolerance
593  */
594 //=============================================================================
595 GEOM::GEOM_Object_ptr GEOM_IHealingOperations_i::LimitTolerance (GEOM::GEOM_Object_ptr theObject,
596                                                                  CORBA::Double theTolerance)
597 {
598   GEOM::GEOM_Object_var aGEOMObject;
599
600   // Set a not done flag
601   GetOperations()->SetNotDone();
602
603   // Get the object itself
604   Handle(GEOM_Object) anObject = GetObjectImpl(theObject);
605   if (anObject.IsNull())
606     return aGEOMObject._retn();
607
608   // Perform
609   Handle(GEOM_Object) aNewObject =
610     GetOperations()->LimitTolerance(anObject, theTolerance);
611   if (!GetOperations()->IsDone() || aNewObject.IsNull())
612     return aGEOMObject._retn();
613
614   return GetObject(aNewObject);
615 }